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

Error on calling 'OnStateChange' method: Object reference not set to...

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

    Error on calling 'OnStateChange' method: Object reference not set to...

    Hello,

    Super newbie here.

    I'm trying to access the value of the current ATR in my Strategy. Here's a simplified/trimmed snippet of my code:


    Code:
    private Bollinger Bollinger1;
    private ATR ATR1;
    
    
    else if (State == State.DataLoaded)
    {
    Bollinger1 = Bollinger(Close, BBStdDev, Convert.ToInt32(BBLength));
    ATR1 = ATR(14);
    Print(ATR1);
    SetProfitTarget(@"GoLong", CalculationMode.Price, (Close[0] + 40) ); // Set by Strategy Builder
    SetStopLoss(@"GoLong", CalculationMode.Price, (Close[0] - 10) , false); // Set by Strategy Builder
    
    }

    But the Print line is producing this in my Output window:
    ​ATR = ATR(MES MAR24 (10 Minute),14)
    I thought it would be the value of ATR. I guess I must be doing something wrong. I want to be working with the value of ATR because ultimately I want to use the value of ATR to calculate my Stop Loss and Profit Target.

    Thank you,
    Jamie

    #2
    The error is simple -- at the point at which State.DataLoaded
    is called, no bars have been processed yet.

    State.DataLoaded means all the historical data needed for
    all data series has been downloaded or read from disk, and
    the bars for all these data series have been built.

    But, your indicator hasn't processed any of those bars yet,
    because that happens next with a bunch of calls to your
    OnBarUpdate.

    In these three lines of code,
    Bollinger1 = Bollinger(Close, BBStdDev, Convert.ToInt32(BBLength));
    ATR1 = ATR(14);
    Print(ATR1);​


    the first two are valid, because all they do is save off a
    reference to those indicators.

    The last line, the Print line, is an error. You cannot
    access the value of ATR1 inside State.DataLoaded.
    Besides that should be Print(ATR1[0]); -- you forgot
    the BarsAgo [0] index. Even with the BarsAgo index,
    there is no value at [0] yet available.

    Why?
    Because, like I said, no bars have been processed yet.
    A bar is considered 'processed' when OnBarUpdate is
    called for that bar . Because you said ATR(14), ​this
    means you want 14 bars to pass so that the ATR value
    in ATR1[0] is of any use.

    Bottom Line:
    As far as using the value of ATR to calculate your Stop
    Loss and Profit Target -- there is absolute no way to do
    that from inside State.DataLoaded.

    Make sense?

    Last edited by bltdavid; 12-30-2023, 03:03 PM.

    Comment


      #3
      Yes, that does make sense. Thank you very much bitdavid!

      Comment


        #4
        Hello domiflichi,

        Thanks for your post.

        bltdavid has provided some great information on this topic.

        The print statement would should be removed from State.DataLoaded and called in OnBarUpdate().

        You could then calculate your stop and target in OnBarUpdate using ATR1 and dynamically call Set methods in OnBarUpdate().

        Please review the help guide documentation below for more information.

        SetStopLoss(): https://ninjatrader.com/support/help...etstoploss.htm
        SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm
        Make sure you have enough bars: https://ninjatrader.com/support/help...nough_bars.htm

        Here is a reference sample demonstrating using Set methods in OnBarUpdate(): https://ninjatrader.com/support/help...of_stop_lo.htm
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thank you for the additional information Brandon!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by llanqui, 04-28-2024, 10:32 AM
          8 responses
          35 views
          0 likes
          Last Post llanqui
          by llanqui
           
          Started by llanqui, 04-28-2024, 03:53 AM
          3 responses
          27 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by geotrades1, Today, 08:33 AM
          6 responses
          19 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by dcriador, Today, 10:45 AM
          0 responses
          1 view
          0 likes
          Last Post dcriador  
          Started by RaddiFX, Yesterday, 09:55 PM
          5 responses
          32 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X