Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 futtrader, 04-21-2024, 01:50 AM
          4 responses
          41 views
          0 likes
          Last Post futtrader  
          Started by Option Whisperer, Today, 09:55 AM
          1 response
          11 views
          0 likes
          Last Post bltdavid  
          Started by port119, Today, 02:43 PM
          0 responses
          7 views
          0 likes
          Last Post port119
          by port119
           
          Started by Philippe56140, Today, 02:35 PM
          0 responses
          7 views
          0 likes
          Last Post Philippe56140  
          Started by 00nevest, Today, 02:27 PM
          0 responses
          7 views
          0 likes
          Last Post 00nevest  
          Working...
          X