Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limiting Historical Computations in an Indicator

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

    Limiting Historical Computations in an Indicator

    I'm looking to reduce the computational load by implementing a condition that stops calculations beyond a certain bar index. Essentially, I want the program to ignore bars that are too far back.

    I've managed to prevent the plot from displaying beyond a specified bar by making the color transparent, but this approach doesn’t actually reduce the computational effort—it just hides the result visually.

    I believe the following code snippet should accomplish what I need:

    protected override void (OnBarUpdate()
    {
    if (CurrentBar < 40)
    return;
    }

    However, when I apply this, it doesn't seem to work as expected. The history still shows beyond the point where the calculcations should stop.

    I've included images of two sample indicators where I attempted to implement this, along with bits of the relevant code.

    I would appreciate any input on how to make this work effectively. If it can be made to work for the Colored Region indicator, it would likely result in significant computational savings, especially when compared to a simpler two-colored EMA indicator. The actual indicator files are also attached as .cs files for easier review.

    sandman​
    Attached Files

    #2
    Hello sandman,

    Thank you for your inquiry.

    If you set BarsRequired to 40 in Initialize(), are you still seeing the same behavior?

    Comment


      #3
      Gaby. Thanks I tried what you suggested but unfortunately that does not change anything.

      Could it be that NT initializes indicators with a default set of bars, which cannot be overriden by my wanted condition? In other words, NT preloads more bars than I want it to load.
      (Mind you I am not a programmer; I copy and paste code.)

      The reference included in your reply above is from the strategy section not the indicator section of the help guide - wouldn't that make a difference?

      sandman

      Comment


        #4
        Hello sandman,

        Did you remove the old script instance and add a new instance before testing?

        Apologies, I sent the strategy page but there is an indicator page - the code is the same though.



        I recommend testing out the sample script below. This script prevents the indicator from plotting until the specific number of bars (NumBars input) have been processed.

        The plotting is controlled strictly via the CurrentBar check, so BarsRequired is set to 0.

        To see this more clearly, I recommend testing on a chart with only 1 day loaded.
        Attached Files

        Comment


          #5
          Yes, I had removed the old and used a new instance.
          Thanks for the sample. I downloaded and used it. I thought it would only plot the last five bars but it shows all. See attached. Does it show something else when you run it?

          sandman
          Attached Files

          Comment


            #6
            Hello sandman,

            Are you looking at the start of the chart (scrolled all the way to the left?)

            It's not going to only plot the last 5 bars if NumBars is 5. If NumBars is 5, it's not going to plot until after the first 5 historical bars.

            Please see this video of me testing out the indicator.

            World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


            Are you wanting the indicator to only plot on the last 5 bars constantly? This would require the indicator to calculate on all bars, since it would need to constantly recalculate which bars were the last five bars and go back and set previous plots to transparent.

            Comment


              #7
              Hi Gaby.

              Only wanting to plot the last 5 bars constantly? That is what I think I had originally in mind - but with what I now think I understand that would defeat the purpose of saving computational resources. I had not looked to the left side/start of the chart. Thanks for your reply and also the video which made it clear. I just had to increase the NumBars sufficiently to get to what I wanted.
              The enclosed upper chart in the image (attached) shows the regular colored region. In the lower chart I increased the NumBars to 115 and, yeah!, it only shows the colored region for the current day which is what I wanted. Number of Days loaded is 5 Days (which I need for other indicators).

              My big question is this: Does this in fact relieve my overworked PC by now not having to compute and draw region and MAs for these 115 bars? Or is it still doing so and not just showing it?

              sandman
              Attached Files

              Comment


                #8
                Hello sandman,

                OnBarUpdate() is still being called for every single bar (no way to prevent this) but the current bar check at the top of OBU will prevent all further processing. Having fewer draw objects on the chart can help improve overall performance of the platform.

                Comment


                  #9
                  Got it. Thank you.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  558 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  324 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  101 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  545 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  547 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X