Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limited Running Total

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

    Limited Running Total

    Hi

    I have a question I hope someone can help me with. I am developing a strategy and as part of it, it calculates a variable every day on the first bar of the session. I am trying to get a running total of the variable for the past 30 days. I am able to get a running total of it indefinitely with =+ but I need it to be limited to the past 30 updates.

    Is there any way to limit it?

    Any suggestions will be greatly appreciated.

    Cheers,

    #2
    Hello fevs7,

    Thank you for writing in and welcome to the NinjaTrader Support Forum!

    You can create an int variable to be used as a counter. On every new session, increment the counter by one.

    Before incrementing the variable you have created, check that the counter is not over 30.

    As an example:
    Code:
    private int counter = 0;
    
    protected override void OnBarUpdate()
    {
         if (Bars.FirstBarOfSession)
         {
              counter++;
    
              if (counter <= 30)
              {
                   // increment your variable
              }
         }
    }
    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick feedback! OK thanks for that. I will give it a go over the weekend and let you know how it goes

      Comment


        #4
        Hi Zachary

        I had a look at your suggestion and it is pretty much what I have at the moment.

        It gives me the correct answer on day 30 but the problem comes in on day 31 and onwards. On day 31 it is supposed to add the variable for that day and subtract the variable for day 1(which could be a different number from the one on day 31) or, differently put, the running total from day 2 to 31 is required. On day 32 the total from day 3 to 32 is required etc

        The alternative would be to create 30 additional variables to keep track of each of the preceding 30 day’s variables and then on each day after day 30 move them down the queue in 30 increments, before the variable for that day is calculated and added to the final spot, before all 30 is finally added together. But to get that list filled over 30 days and then updated on each preceding day seems onerous. If there is a function/method available that can do that in less code I would be very grateful if you could point me in the right direction.

        Comment


          #5
          Hello fevs7,

          Rather than creating multiple variables to hold your values, you can utilize a List: http://www.dotnetperls.com/list

          To make use of a List, ensure that you add this using statement to the Using declarations region:
          Code:
          using System.Collections.Generic;
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            OK great. Thanks!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            597 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            343 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            556 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            555 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X