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 andrewtrades, Today, 04:57 PM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by chbruno, Today, 04:10 PM
      0 responses
      6 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Started by josh18955, 03-25-2023, 11:16 AM
      6 responses
      436 views
      0 likes
      Last Post Delerium  
      Started by FAQtrader, Today, 03:35 PM
      0 responses
      9 views
      0 likes
      Last Post FAQtrader  
      Started by rocketman7, Today, 09:41 AM
      5 responses
      20 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X