Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to only process the last 500 bars to save cpu and ram

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

    How to only process the last 500 bars to save cpu and ram

    TV has something like this
    indicator("EMA", overlay = true, max_bars_back = 500)

    in order to save memory and cpu
    because my indicator draws a lot

    how do I only process the last bars?
    basically only process 500 bars at a time

    sure you may say, well only display 500 bars
    but what if I wanna see 2000 bars because I wanna see the EMA
    and I only wanna see the last 500 bars of my indicator?

    #2
    Hello AaronKoRn,

    The easiest way would be to only load 500 bars when you create the chart. If you need to load 2000 bars because of another indicator that would cause your indicator to also observe all 2000 bars.

    To prevent your script from processing you would need to use logic to find out what bar you are on and then return to do nothing for that bar.

    You could use math to find out how many bars had processed or how many are left to process.

    Code:
    if(State == State.Historical)
    {
        int leftToProcess = (Count - 2) - CurrentBar; 
        if(leftToProcess > 500) return; 
    }

    Comment


      #3
      Hi Jesse, Yes that is what I want

      my mistake, when I put it on OnStateChange
      but it didn't work

      but when I put on OnBarUpdate
      it works
      Last edited by AaronKoRn; 10-13-2022, 08:33 PM.

      Comment

      Latest Posts

      Collapse

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