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

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
    Paul H.NinjaTrader Customer Service

    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");
                    }
                }
        Paul H.NinjaTrader Customer Service

        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
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Just sent it over!
              Thanks!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by bortz, 11-06-2023, 08:04 AM
              47 responses
              1,610 views
              0 likes
              Last Post aligator  
              Started by jaybedreamin, Today, 05:56 PM
              0 responses
              9 views
              0 likes
              Last Post jaybedreamin  
              Started by DJ888, 04-16-2024, 06:09 PM
              6 responses
              19 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by Jon17, Today, 04:33 PM
              0 responses
              6 views
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              16 views
              0 likes
              Last Post Javierw.ok  
              Working...
              X