Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with setting up array, with OnBarUpdate please.

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

    Help with setting up array, with OnBarUpdate please.

    Hello, Id like some help with this please I keep getting this error...
    **NT** Error on calling 'OnBarUpdate' method for strategy 'Long/18da4c7079bd424498f83d4a0daf6838': Object reference not set to an instance of an object.
    I know the numbers are off next to the close, but can someone please help me get rid of this error? Right now I am strictly looking at placing orders off of the bar closures. Not trying to add SMA or anything else yet. Just a work in progress.

    Thank you for your help
    Code:
    protected override void OnBarUpdate()
            {
    		  if (BarsInProgress != 0) 
            return;
    		
    		if (BarsInProgress == 0)
                // Condition set 1
                if (Close[-5] > Close[0]
                    && Close[-5] > Close[-1])
                {
                    EnterLong(1000, "entry1");
                }
    
                // Condition set 2
                if (Close[-5] < Close[0]
                    && Close[-5] < Close[-1])
                {
                    ExitLong("1000", "entry1");
                }
            }

    #2
    Hello habit04,

    Thanks for your post and welcome to the forums!

    You would not be able to use a negative barsago index such as -1 or -5. To get the value of the previous bar you would use [1] and 5 bars ago would be [5]

    To prevent trying to access 5 bars ago when the first bar is loaded type error you will want to add a CurrentBar check. As you are checking for BarsInProgress that would indicate that you are using a multiseries or multi time frame and in that case you would want to use a CurrentBars[n] check where n refers to the bars in progress (or barsarray). If you are only working in BarsInProgress 0 then you may only need something like:

    if (CurrentBars[0] < 5) return; // do not process further until enough bars

    If you are working with or referencing the other Barsarray objects then you may need to add a CurrentBars[] check for those bars as well.

    References:
    http://ninjatrader.com/support/helpG...urrentbars.htm
    http://ninjatrader.com/support/helpG...nstruments.htm

    Comment


      #3
      Thank you for the quick reply Paul!

      Yes I am only working with a single time frame which is one minute for now. I will try to use the code you showed me seems simple enough, when I say " if (CurrentBars[0] < 5) " Are those referencing the close of the bars?
      I will look at what you referenced me as well. And do I have to initialize anything?

      I have this so far " Add("$USDJPY", PeriodType.Minute, 1);
      "
      Thanks again, I sure appreciate the help!

      Comment


        #4
        Hello habit04,

        Thanks for your reply.

        Just to clarify, when you apply the strategy to a chart, the strategy adopts the charts bars as BarsInProgress == 0.

        You would not need to use the Add() method if the chart contains the bars needed. So if you do not use Add() then you would not need to use BarsInProgress references or CurrentBars but instead use CurrentBar.

        If that is correct (Only using the chart bars), then you code could look like:

        Code:
        protected override void OnBarUpdate()
                {
                    if (CurrentBar < 5)  return;
        
                    // Condition set 1
                    if (Close[5] > Close[0]
                        && Close[5] > Close[1])
                    {
                        EnterLong(1000, "entry1");
                    }
        
                    // Condition set 2
                    if (Close[5] < Close[0]
                        && Close[5] < Close[0])
                    {
                        ExitLong(1000, "entry1");
                    }
                }

        Comment


          #5
          Email

          Hello Paul, I just sent you a message with some some code if you could please lol at it. Your code seems to work the same as mine. I still get a no object error. Do I have to setup datasets?
          I am unsure how to do this...
          thanks for all your help!

          Comment


            #6
            Hello,

            Thanks for your reply.

            Please send your strategy source code to PlatformSupport[at]NinjaTrader[dot]com with a subject of atten: Paul and a link to this thread. (Note that [at] and [dot] are replaced by the normal symbols @ and . )

            You can attach the file to the e-mail. the file can be found in Documents>Ninjatrader7>bin>Custom>strategy> and will be named Long1.cs

            Thank-you

            Comment


              #7
              Just sent it over!
              Thanks!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              566 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              330 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              101 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              547 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              548 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X