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
          5 responses
          56 views
          0 likes
          Last Post NinjaTrader_Eduardo  
          Started by PeakTry, Today, 10:49 AM
          0 responses
          2 views
          0 likes
          Last Post PeakTry
          by PeakTry
           
          Started by llanqui, Today, 10:32 AM
          0 responses
          5 views
          0 likes
          Last Post llanqui
          by llanqui
           
          Started by StockTrader88, 03-06-2021, 08:58 AM
          45 responses
          3,992 views
          3 likes
          Last Post johntraderuser2  
          Started by TAJTrades, Today, 09:46 AM
          0 responses
          8 views
          0 likes
          Last Post TAJTrades  
          Working...
          X