Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

error when using swing indicator in a strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    error when using swing indicator in a strategy

    I tried to use swing indictor to find previous high/low and the high/low before that. Here is my code
    int BarsAgo = Swing(SwingStrength).SwingHighBar(0, 1, 300);
    //if(BarsAgo < 6 || CurrentBar <= BarsAgo) return;
    if(BarsAgo ==-1 || CurrentBar <= BarsAgo) return;
    Print(string.Format("{0} | CurrentBar: {1}, BarsAgo: {2}", Time[0], CurrentBar, BarsAgo));
    double swingHigh = Swing(SwingStrength).SwingHigh[BarsAgo];

    int BarsAgo1 = Swing(SwingStrength).SwingLowBar(0, 1, 300);
    if(BarsAgo1 ==-1 || CurrentBar <= BarsAgo1) return;
    Print(string.Format("{0} | CurrentBar: {1}, BarsAgo1: {2}", Time[0], CurrentBar, BarsAgo1));
    double swingLow = Swing(SwingStrength).SwingLow[BarsAgo1];
    //Print(swingHigh);

    int BarsAgo2 = Swing(SwingStrength).SwingHighBar(0, 2, 300);
    if(BarsAgo2 ==-1 || CurrentBar <= BarsAgo2) return;
    Print(string.Format("{0} | CurrentBar: {1}, BarsAgo2: {2}", Time[0], CurrentBar, BarsAgo2));
    double swingHighPrevious = Swing(SwingStrength).SwingHigh[BarsAgo2];

    //int BarsAgo3 = Swing(SwingStrength).SwingLowBar(0, 2, Bars.BarsSinceNewTradingDay);
    int BarsAgo3 = Swing(SwingStrength).SwingLowBar(0, 2, 300);
    Print(string.Format("{0} | CurrentBar: {1}, BarsAgo3: {2}", Time[0], CurrentBar, BarsAgo3));
    if(BarsAgo3 ==-1 || CurrentBar <= BarsAgo3) return;
    double swingLowPrevious = Swing(SwingStrength).SwingLow[BarsAgo3];

    Sometime I find the BarAgo3 value is -1 which is how many bars before the current bar is the low before the previous low. I am wondering why it happens.​

    #2
    Hello Playdc,

    Thank you for your post.

    If the Swing value is returning -1, this means that the swing point is not found within the look back period.

    This is noted in the Help Guide page for Swing:

    "Return Value: An int value representing the number of bars ago. Returns a value of -1 if a swing point is not found within the look back period."



    Please let us know if you have any further questions.

    Comment


      #3
      Hi, I understand -1 means it does not find the low in the look back period. What I am confused is I set to look back period to be 300, why on bar 1779, BarAgos3 is 255, then on bar 1780, it becomes -1. Here are the outputs

      2024/12/4 8:19:56 | CurrentBar: 1779, BarsAgo: 93
      2024/12/4 8:19:56 | CurrentBar: 1779, BarsAgo1: 111
      2024/12/4 8:19:56 | CurrentBar: 1779, BarsAgo2: 128
      2024/12/4 8:19:56 | CurrentBar: 1779, BarsAgo3: 255
      2024/12/4 8:20:00 | CurrentBar: 1780, BarsAgo: 94
      2024/12/4 8:20:00 | CurrentBar: 1780, BarsAgo1: 112
      2024/12/4 8:20:00 | CurrentBar: 1780, BarsAgo2: 129
      2024/12/4 8:20:00 | CurrentBar: 1780, BarsAgo3: -1
      2024/12/4 8:20:01 | CurrentBar: 1781, BarsAgo: 95
      2024/12/4 8:20:01 | CurrentBar: 1781, BarsAgo1: 113
      2024/12/4 8:20:01 | CurrentBar: 1781, BarsAgo2: 130
      2024/12/4 8:20:01 | CurrentBar: 1781, BarsAgo3: -1​
      Last edited by Playdc; 12-06-2024, 02:26 PM.

      Comment


        #4
        Hello Playdc,

        Simply because it is returning a value on one bar, this doesn't mean it is going to return a value on the next bar. The Swing indicator is recalculating bar to bar. It's also not clear within the context of what is posted here if SwingStrength is changing bar to bar, this could also have an effect on the calculation.

        The Swing indicator's logic is open-source, so you could examine it's logic within the NinjaScript Editor.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        62 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        134 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        75 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X