Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing indicator value from x bars ago

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

    Accessing indicator value from x bars ago

    Hi, this is probably a basic question, but I'm having a hard time incorporating previous bars into the logic. Here's what I want to do: Draw an arrow when the MACD value of bar 0 ago is .01 greater than the MACD value of bar 1 ago.


    if (MACD(12, 26, 9)[0] - MACD(12, 26, 9)[1] > 0.01)
    {
    DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 4 * TickSize, Color.Red);
    }

    How can I reference the previous bars? It will compile, but the arrows will not plot. The arrow will plot if the bars ago are set to [0]...for example something like

    if (MACD(12, 26, 9)[0] > 0.01)
    {
    DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 4 * TickSize, Color.Red);
    }

    Thanks

    #2
    Originally posted by BishopM05 View Post
    Hi, this is probably a basic question, but I'm having a hard time incorporating previous bars into the logic. Here's what I want to do: Draw an arrow when the MACD value of bar 0 ago is .01 greater than the MACD value of bar 1 ago.


    if (MACD(12, 26, 9)[0] - MACD(12, 26, 9)[1] > 0.01)
    {
    DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 4 * TickSize, Color.Red);
    }

    How can I reference the previous bars? It will compile, but the arrows will not plot. The arrow will plot if the bars ago are set to [0]...for example something like

    if (MACD(12, 26, 9)[0] > 0.01)
    {
    DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 4 * TickSize, Color.Red);
    }

    Thanks
    Any messages in the log?

    Comment


      #3
      Yes, I'm getting the not enough bars error. However, the chart is displaying more than 5 bars...

      "Error on calling 'OnBarUpdate' method for indicator 'MACDMarker' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

      Comment


        #4
        It doesn't matter how many bars are on the chart, OnBarUpdate() is called for every bar on the chart. Your formula references the previous bars MACD value, and on the first bar there is no previous bar.

        So in OnBarUpdate() before your code include a check for enough bars:
        if (CurrentBar < 1) return;

        The number of bars to check for is not always the same, but for what you've posted 1 should be enough.

        VT

        Comment


          #5
          Thanks for the explanation!

          Comment

          Latest Posts

          Collapse

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