Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to code bars since condition

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

    How to code bars since condition

    Hi NT8,

    How do I code bars since a condition other than entry. similar to the bars since entry/exit condition, I'd like to reference the number of bars since another condition.

    Such as

    If
    (MACD cross 0)

    Then
    (variable = bars since that event)

    Thank you!

    #2
    I'd like to clarify that I can't use the look back period, what I am trying to do is replicate the Bars since entry for a different condition, so that I can do this:

    Condition (Bars Since last Condition > 10)

    ^ I don't believe the look back period can do this because it checks if a condition happened within the look back period and I'd like to set if a condition happened to bars greater than a lookback period.

    Comment


      #3
      Hello Austiner87, thanks for writing in.

      We have the MRO (Most Recent Occurrence) method that will return the bar index where a condition happened:
      https://ninjatrader.com/support/help...urence_mro.htm

      There is an example on that page on how to use it.

      If you have any further questions please let me know.

      Comment


        #4

        Hi NT8, I need some more help on this:

        // Prints the high price of the most recent up bar over the last 10 bars (current bar + look back period's 9 bars before that)
        int barsAgo = MRO(() => Close[0] > Open[0], 1, 9);
        if (barsAgo > -1)
        Print("The bar high was " + High[barsAgo]);

        ^using the example, how would I check for if a variable increased by 1 (+=1) greater than 12 bars ago. my assumption is below, can you please let me know if this is correct?

        int barsAgo = MRO((variable += 1) => Close[0] > Open[0], 1,12);
        if (barsAgo >= 12)


        Comment


          #5
          Hello Austiner87, thanks for your reply.

          The (variable) => is a way to pass in parameters into the lambda function. To check if a variable increased by one you would need to store the variable in a custom Series<T> object:

          https://ninjatrader.com/support/help.../?iseriest.htm

          Then you can compare MySeries[0] against MySeries[11 to 1] to see if MySeries[0] == MySeries[x]+1

          Make sure to test thoroughly with Print statements to check the output.

          Please let me know if I can assist any further.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          45 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          21 views
          0 likes
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          31 views
          1 like
          Last Post CaptainJack  
          Started by CarlTrading, 03-30-2026, 11:51 AM
          0 responses
          50 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 03-30-2026, 11:48 AM
          0 responses
          42 views
          0 likes
          Last Post CarlTrading  
          Working...
          X