Announcement

Collapse
No announcement yet.

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
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Thank you for the additional information Brandon!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          54 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          130 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          72 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          44 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          49 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X