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

Close[BarsAgo] fails

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

    Close[BarsAgo] fails

    I'm converting my indicators from NT7 to NT8 and trying to reference OHLC Volume or Time using the typical Close[BarsAgo] is only working on the most recent 3 bars.

    Below is from the Visual Studio Immediate Window.

    Trying to access 39 bars ago fails as does anything over 2.

    ?Close.Count
    439
    ?BarsAgo
    39
    ?Close[BarsAgo]
    'Close[BarsAgo]' threw an exception of type 'System.NullReferenceException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2147467261
    HelpLink: null
    InnerException: null
    Message: "Object reference not set to an instance of an object."
    Source: "NinjaTrader.Core"
    StackTrace: " at NinjaTrader.Data.BarsSeries.GetClose(Int32 index)\r\n at NinjaTrader.Data.Bars.GetClose(Int32 index)\r\n at NinjaTrader.NinjaScript.PriceSeries.get_Item(Int32 barsAgo)"
    TargetSite: {Double GetClose(Int32)}
    ?Close[0]
    53.96
    ?Close[1]
    53.92
    ?Close[2]
    53.92
    ?Close[3]
    'Close[3]' threw an exception of type 'System.NullReferenceException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2147467261
    HelpLink: null
    InnerException: null
    Message: "Object reference not set to an instance of an object."
    Source: "NinjaTrader.Core"
    StackTrace: " at NinjaTrader.Data.BarsSeries.GetClose(Int32 index)\r\n at NinjaTrader.Data.Bars.GetClose(Int32 index)\r\n at NinjaTrader.NinjaScript.PriceSeries.get_Item(Int32 barsAgo)"
    TargetSite: {Double GetClose(Int32)}
    ?CurrentBar
    437




    #2
    Hello ctrent,

    I'm not certain about the Visual Studio errors, but are there errors in NinjaTrader on the Log tab of the Control Center?

    What is the line of code causing the error?

    Is there a null object?

    Is there an invalid index or barAgo value used?

    Is there a CurrentBar check needed?




    if you make a new indicator and add only the code:

    if (CurrentBar < 5)
    return;

    Print(string.Format("{0} | Time[5]: {1}, Close[5]: {2}, Volume[5]: {3}", Time[0], Time[5], Close[5], Volume[5]));

    Does this error?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      That simple code in OnBarUpdate works fine in a new indicator as well as in the one I'm seeing the other behavior.

      My indicator doesn't really run OnBarUpdate. It is even driven. What I'm doing is using the selecting a Historical Bar by placing a DrawingTool on it then walking forward from there to get Theoretical Trade Results. In NT7 this worked fine. Get the BarsAgo value of the DrawingTool then using Close[BarsAgo] step forward and cacluate the results.

      In NT8 only the most recent 3 bars were returning price data even though as seen in the VisualStuido output above there were 439 bars of data with CurrentBar = 437.

      Oddly enough Today it is returning values until Close[13] before returning the error below.

      I have reworked by NT8 code to use Close.GetValueAt(BarIndex) and it works fine maybe better. But this is unexpected behavior I'd like to understand.

      Comment


        #4
        Hello ctrent,

        If you are attempting to access bar data outside of a data driven method, you must use TriggerCustomEvent() to synchronize the series first.

        May I confirm you are using TriggerCustomEvent()?

        Below is a link to the help guide.


        Using .GetValueAt() is an alternative to using TriggerCustomEvent.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          That explains it.

          I was not aware of this and was not using the TriggerCustomEvent() methods.

          Thanks
          Chris

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Rapine Heihei, Today, 08:19 PM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by Rapine Heihei, Today, 08:25 PM
          0 responses
          3 views
          0 likes
          Last Post Rapine Heihei  
          Started by f.saeidi, Today, 08:01 PM
          1 response
          4 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by Rapine Heihei, Today, 07:51 PM
          0 responses
          6 views
          0 likes
          Last Post Rapine Heihei  
          Started by frslvr, 04-11-2024, 07:26 AM
          5 responses
          96 views
          1 like
          Last Post caryc123  
          Working...
          X