Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

best way to calculate stuff in strategy

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

    best way to calculate stuff in strategy

    Hi,

    I have made a lot of strategies now.
    In a lot of strats, stuff is calculated for certain periods back.
    Lets say I want to check if an indicator is below 40 for the last 10 bars.

    When using COBC I can do that by 2 different ways:
    1 at every current bar calculate it for 10 bars back (e.g. with a For loop)
    2 or just simple check it at every bar and "stack" the number of bars in a counter.
    I have used option 1 a lot but lately I am using option 2 more in my strats.

    The reason is that I want to program lean and mean and ask as less as possible from the CPU (specially when COBC=false), but also don't want to get problems while backtesting.
    Can someone give some advise what's the best way?
    So I can program it for the future directly in the best way.

    Tia.
    Anne

    #2
    Originally posted by trail View Post
    Hi,

    I have made a lot of strategies now.
    In a lot of strats, stuff is calculated for certain periods back.
    Lets say I want to check if an indicator is below 40 for the last 10 bars.

    When using COBC I can do that by 2 different ways:
    1 at every current bar calculate it for 10 bars back (e.g. with a For loop)
    2 or just simple check it at every bar and "stack" the number of bars in a counter.
    I have used option 1 a lot but lately I am using option 2 more in my strats.

    The reason is that I want to program lean and mean and ask as less as possible from the CPU (specially when COBC=false), but also don't want to get problems while backtesting.
    Can someone give some advise what's the best way?
    So I can program it for the future directly in the best way.

    Tia.
    Anne
    for loops, and all other loops for that matter, are to be avoided if at all possible. You are processing a stream, so it is always more efficient to process the data as it comes in, using accumulation methods. I only use loops if they appear to be the unavoidable solution to the problem. Most rolling sums can be done much more efficiently by "sum by difference in accumulation", than with a loop, albeit the loop initially seems more intuitive.

    Comment


      #3
      Hi Anne,

      Thank you for your post.

      You would want use the MIN(); to the lowest value of the indicator over a period of time.

      Example with a SMA -
      Code:
      double myValue = MIN(SMA(14), 10)[0];
      
      if( myValue < 40)
      {
      //do something here
      }
      This will assign the lowest value of the SMA over the last ten bars to myValue and then check if the the value is less than 40.

      http://www.ninjatrader.com/support/h...inimum_min.htm

      Let me know if I can be of further assistance.
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        Thanks Cal and Koganam for your quick response.

        Koganam, I already expected that accumulation is more efficient. I will use this method more now in the future.

        Anne

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        647 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        369 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X