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

Object reference

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

    Object reference

    Hi, I'm writing an indicator that adds a higher level 'min' bars to my lower level tick chart. I'm getting this error message (Error on calling 'OnBarUpdate' method for indicator 'MyBackgroundHAHMA' on bar 0: Object reference not set to an instance of an object) but I cannot determine why ... below is the code that is generating this message. any insight would be appreciated; thanks

    protected override void OnBarUpdate()
    {
    // if(CurrentBar < 2)return; //Make sure there are at least 2 bars printed before processin

    Print(" ");
    Print(instName + " MyBackgroundHAHMA: " + Time[0].ToString());
    Print(instName + " BarsInProgress: " + BarsInProgress);

    if (BarsInProgress == 1) // process Min Chart
    {
    if (CurrentBar == 0)
    {
    HA_C.Set(Close[0]);
    HA_O.Set(Open[0]);
    HA2_C.Set(Close[0]);
    HA2_O.Set(Open[0]);
    return;
    }
    // HA Thma (yellow)
    HA2_O.Set((((Open[1] + High[1] + Low[1] + Close[1]) / 4) + HA2_O[1]) / 2);
    HA2_C.Set((((Open[0] + High[0] + Low[0] + Close[0]) / 4) + HA2_O[0] + Math.Max(High[0], HA2_O[0]) + Math.Min(Low[0], HA2_O[0])) / 4);
    MyHeiken = (3 * hma1[0] - 3 * hma2[0] + hma3[0]);

    // Price Thma (red)
    MyThma = (3 * hma1t[0] - 3 * hma2t[0] + hma3t[0]);
    }



    if (BarsInProgress == 0) // process current instrument
    {
    Print(instName + " MyThma: " + MyThma + " MyHeiken: " + MyHeiken);
    if (MyThma >= MyHeiken) BackColorSeries[0] = risingColor;
    else BackColorSeries[0] = fallingColor;
    }


    #2
    pman,

    I see you commented this out :

    // if(CurrentBar < 2)return; //Make sure there are at least 2 bars printed before processin

    I would suggest keeping it or rearranging your code here.

    if ( CurrentBar < 1 )
    {
    return;
    }
    else if (CurrentBar == 1 )
    {
    HA_C.Set(Close[0]);
    HA_O.Set(Open[0]);
    HA2_C.Set(Close[0]);
    HA2_O.Set(Open[0]);
    return;
    }
    else
    {
    //everything else
    }
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      tx ... when I uncomment that section, I get this message:

      Error on calling 'OnBarUpdate' method for indicator 'MyBackgroundHAHMA' on bar 2: Object reference not set to an instance of an object.

      Comment


        #4
        pman,

        This looks like a separate issue perhaps.

        I would suggest using a Try/Catch here. These can print out what line is causing the issue to the output window.

        Adam P.NinjaTrader Customer Service

        Comment


          #5
          ok ...did that ... tx. Isolated the problem down to this statement:

          HA2_O.Set((((Open[1] + High[1] + Low[1] + Close[1]) / 4) + HA2_O[1]) / 2);


          Error message: 10/26/2012 12:15:00 AM System.NullReferenceException: Object reference not set to an instance of an object.
          at NinjaTrader.Indicator.MyBackgroundHAHMA.OnBarUpdat e()
          Error on calling 'OnBarUpdate' method for indicator 'MyBackgroundHAHMA' on bar 2: Object reference not set to an instance of an object
          .

          Comment


            #6
            pman,

            It could be this doesn't exist yet : HA2_O[1]

            You may want to do this :

            if ( CurrentBar < 1 )
            {
            return;
            }
            else if (CurrentBar <= 2 )
            {
            HA_C.Set(Close[0]);
            HA_O.Set(Open[0]);
            HA2_C.Set(Close[0]);
            HA2_O.Set(Open[0]);
            return;
            }
            else
            {
            //everything else
            }
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by pman777 View Post
              Hi, I'm writing an indicator that adds a higher level 'min' bars to my lower level tick chart. I'm getting this error message (Error on calling 'OnBarUpdate' method for indicator 'MyBackgroundHAHMA' on bar 0: Object reference not set to an instance of an object) but I cannot determine why ... below is the code that is generating this message. any insight would be appreciated; thanks

              protected override void OnBarUpdate()
              {
              // if(CurrentBar < 2)return; //Make sure there are at least 2 bars printed before processin

              Print(" ");
              Print(instName + " MyBackgroundHAHMA: " + Time[0].ToString());
              Print(instName + " BarsInProgress: " + BarsInProgress);

              if (BarsInProgress == 1) // process Min Chart
              {
              if (CurrentBar == 0)
              {
              HA_C.Set(Close[0]);
              HA_O.Set(Open[0]);
              HA2_C.Set(Close[0]);
              HA2_O.Set(Open[0]);
              return;
              }
              // HA Thma (yellow)
              HA2_O.Set((((Open[1] + High[1] + Low[1] + Close[1]) / 4) + HA2_O[1]) / 2);
              HA2_C.Set((((Open[0] + High[0] + Low[0] + Close[0]) / 4) + HA2_O[0] + Math.Max(High[0], HA2_O[0]) + Math.Min(Low[0], HA2_O[0])) / 4);
              MyHeiken = (3 * hma1[0] - 3 * hma2[0] + hma3[0]);

              // Price Thma (red)
              MyThma = (3 * hma1t[0] - 3 * hma2t[0] + hma3t[0]);
              }



              if (BarsInProgress == 0) // process current instrument
              {
              Print(instName + " MyThma: " + MyThma + " MyHeiken: " + MyHeiken);
              if (MyThma >= MyHeiken) BackColorSeries[0] = risingColor;
              else BackColorSeries[0] = fallingColor;
              }
              You have a multi timeframe setup. You need to check for CurrentBar validity on both barSeries.

              Comment


                #8
                will try ... thanks!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Barry Milan, Today, 10:35 PM
                2 responses
                8 views
                0 likes
                Last Post Barry Milan  
                Started by WeyldFalcon, 12-10-2020, 06:48 PM
                14 responses
                1,428 views
                0 likes
                Last Post Handclap0241  
                Started by DJ888, Yesterday, 06:09 PM
                2 responses
                9 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by jeronymite, 04-12-2024, 04:26 PM
                3 responses
                41 views
                0 likes
                Last Post jeronymite  
                Started by bill2023, Today, 08:51 AM
                2 responses
                16 views
                0 likes
                Last Post bill2023  
                Working...
                X