Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using only the most recent 50 bars

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

    Using only the most recent 50 bars

    hi,

    still getting used to programming and ninja script so here is a simple question - how do I work backwards in my custom indicator to start from the most recent bar and go back to only 50 bars to perform certain calculations, instead of starting from the extreme left of the chart and working it's way through all the bars to the most recent one. this I'd like to achieve in the interest of saving time.

    also, if I assign a certain indicator value to a bar' during that most recent 50bars rolling window and the bar falls out of it with passage of time, does the chart lose that indicator value or does it keep it as long as that particular chart is open ?

    sorry I have no script for this, only a generic question and I hope I'm clear in what I'm asking here...

    many thanks in advance for your help

    #2
    buylosellhi, the easiest way would be simply limiting the # of bars you load on the chart which you can et in the Format Data Series box, then for example if you choose to load 500 bars you can start your OnBarUpdate() section of code with if (CurrentBar < 450) return;

    This following code lines in your script would then calculate on the last 50 bars of the chart only as the calls for OnBarUpdate() are limited to start from chartbar 450 on going forward.

    Comment


      #3
      Bertrand,

      thanks for your reply. quick follow up question - on a realtime streaming chart how do I 'load' only 500 bars. is that using the BarsLookBack ?

      thanks much

      Comment


        #4
        Yes, you would load 500 bars for example only when creating the chart (Bars to load property). But this would only be for loading up the chart, additional bars as the build in realtime are then added.

        To add to what I previously wrote, this below snippet would for example calculate an SMA on the last 50 bars only and not be called for other bars on the chart for that you would normally call OnBarUpdate() as well -

        Code:
        protected override void OnBarUpdate()
        {
        	if (Count - CurrentBar < 50)
        	    Value.Set(SMA(Period)[0]);
        }

        Comment


          #5
          great thanks. and as bars that were in the 50 period window and were assigned SMA values roll off that window with passage of time, do they retain their associated SMA values ?

          thanks again :-)

          Comment


            #6
            Yes, in my example below those values calc'ed would be retained.

            Comment


              #7
              thanks a lot for your help

              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
              573 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X