Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

looping an indicator over several bars

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

    looping an indicator over several bars

    Hi, trying to determine a rising condition over multiple bars. Rather than compare each set of bars manually I thought I would try a loop. But the loop does not seem to be working. So I imagine I am doing something wrong. The loop initiates with 0 comparing LinRegSlope1[0] > LinRegSlope1[1] then the next should increment [1] > [2] etcetera... The count never prints so it is not ever satisfying the condition. I hope someone can shed some light on this.

    private int loopBack =10;
    if (BarsInProgress > 20)
    {
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    for(int barsMin = 0, barsMax = lookBack; barsMin < barsMax; barsMin ++)
    {
    if (LinRegSlope1[barsMin] > LinRegSlope1[barsMin + 1])
    Count = Count +1;
    Print("Count: " + Count);
    }
    }
    }
    if ( Count > 3) LinRegSlopeRising = true; else LinRegSlopeRising = false;

    #2
    Hello set2win,

    Thanks for your post.

    I suspect you want BarsInProgress to actually be CurrentBar. BarsInProgress would identify which data series is calling the OnBarUpdate() and I doubt (and hope) you don't have 20 data series.

    The for loop has some syntax issues for(int barsMin = 0, barsMax = lookBack; barsMin < barsMax; barsMin ++) you are seperating the first parameter from the second with a comma instead of a semicolon. You would not assign a value in there like barsMax = lookBack, you could assign that ahead of the for loop or not use barMax at all. Perhaps something like this: for(int barsMin = 0; barsMin < LookBack; barsMin++)

    Where you have Count = Count+1; while that would work you can also write it this way: Count++;

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I think I figured out something that seems to be working:
      I did eliminate the barsinprogress and just added if (CurrentBars[0] < 10) return;
      I did see the separation in barsMin and ++.
      So with some print statements I have checked some values and it now is performing I think as it should.
      And the backtesting seems to be doing what I hoped which is to check for so many bars back that the indicator is increasing. I can experiment with the lookback and the barCount to see what is best performance. The barsMax is to only check the last so many bars.

      barCount = 4;
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      for(int barsMin = 0; barsMin < lookBack; barsMin++)
      {
      if (LinRegSlope1[barsMin] > LinRegSlope1[barsMin + 1])
      {
      Count = Count +1;
      if ( Count > barCount) LinRegSlopeRising = true; else LinRegSlopeRising = false;
      }
      }
      Count = 0; /// resets the count
      }
      Last edited by set2win; 04-15-2021, 01:12 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by diegomezhur, Yesterday, 03:28 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by Zinger357, Today, 09:01 AM
      0 responses
      2 views
      0 likes
      Last Post Zinger357  
      Started by shortrader, Today, 07:15 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by WHICKED, Yesterday, 10:02 PM
      3 responses
      21 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by at8494, Today, 07:55 AM
      2 responses
      14 views
      0 likes
      Last Post at8494
      by at8494
       
      Working...
      X