Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to assign a value from a previous bar to the current bar

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

    How to assign a value from a previous bar to the current bar

    Hi,


    This is driving me crazy...... I have a simple calculation running which plots a line based on the max and min value of the last 5 bars.
    The calculation looks like this:

    Price[0] = ((MAX(High, 5)[0] + MIN(Low, 5[0])) / 2.

    This plots just fine and is as I would expect.

    But, I want to add another plot which offsets these values by 1 bar. I.e I want the current value of my new plot to be assigned the last previous value of the existing plot.

    I have tried this:

    Price2[0] = Price[1] but this doesn't work because I assume at the start of time there is no value for Price[1].


    How can I get around this? I have tried all sorts to no avail. It's such a simple thing yet I can't work it out.


    Thanks.

    #2
    Hello ballston,

    Thank you for the post.

    Are you seeing an error? What you provided should work but you are correct, on bar 0 the [1] bars ago will not exist so you would need a condition like:

    Code:
    if(CurrentBar < 1) return;
    I look forward to being of further assistance.

    Comment


      #3
      Hi,

      The compiler doesn't come up with an error, but there are no plots.

      The Ninjascript output says: Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.


      How do I use what you have said in context?

      Would it be:

      Price[0] = ((MAX(High, 5)[0] + MIN(Low, 5[0])) / 2

      if(CurrentBar < 1) return;
      Price2[0] = Price[1];



      Thanks.
      Last edited by ballston; 07-29-2021, 03:17 PM.

      Comment


        #4
        Hi again,


        I got it working thank you. I knew it was simple!
        Thanks for your help.

        Rds,
        Ben

        Comment


          #5
          Hello ballston,

          Yes that would be correct, the line following the current bar check would not happen on bar 0 but would happen on bar 1 once a plot value had been set.

          I look forward to being of further assistance.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          582 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          338 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
          554 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          552 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X