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 Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      648 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
      574 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X