Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Errormessage 4 sec after profittargt hit

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

    Errormessage 4 sec after profittargt hit

    Hello,
    Im experiencing a problem with my script and I try to find out for hours without success. Once profit target is hit (I have 2 contracts, the first I take off with profit target, the 2nd with conditions and ExitShort/ExitLong) often - not always - 4 seconds later I have the following message in log tab
    03.09.2020 15:32:03 Default Strategy 'Tx30xNQxAbsxRSIv12': Error on calling 'OnBarUpdate' method on bar 2257033: Object reference not set to an instance of an object.
    What might be the reason please?

    Thank you!
    Tony

    #2
    Hello Tony, thanks for your post.

    This message means you are trying to access the properties or methods of an object that is not initialized, you should check for null on any order objects before accessing them:

    if(MyOrderObject != null)
    {
    //do something with the order.
    }

    To find exactly what line of code is failing, you can attach Visual Studio to the NinjaTrader process and run the script to reproduce the error. When the error is hit, the breakpoint will be created and Visual Studio will show you the exact line of code that is causing the problem.

    Kind regards.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Something has been set (or reset) to null.

      Comment


        #4
        Hello,

        I do the entry with 2 contracts, for contract1 I have a setstoploss and a setprofittarget. For conctract2 I have only a setstoploss.

        So when the contract1 hits the target there is nothing to set to null, or to check for null before. Yes/No?
        For contract2 with the setstoploss there is also nothing to check. Yes/No?

        So it seems that the only "myorderobject" is the entryorder that should set to null when entry is filled. In the meantime I have added this to the code and its set immediately after entry to null. But the problem persists, when T1 is hit then I get the error-message.

        I do not know what else I could check for null when working with setstoploss and setprofittarget.

        (?)

        Thank you!
        Tony
        Last edited by tonynt; 09-04-2020, 07:22 AM. Reason: add information

        Comment


          #5
          Hi Tony thanks for your reply.

          If the custom Order object is the only user-created complex object then it is likely the cause of the problem. You can use Visual Studio to see this for yourself. When the exception is hit, Visual Studio will show you what is null.

          Best regards.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Hello,

            thank you for your reply. I dont understand what you mean by ".... comples". I never read before "complex" in the forum. I have few "user-created" doubles, ints and bools but they dont have to be checked for null. Yes/No?

            The 2 entries entryOrderSr1a=EnterShortLimit(4,true,1,GetCurrent Bid(), "rsiS1a"); and entryOrderSr1b=EnterShortLimit(4,true,1,GetCurrent Bid()-1*TickSize, "rsiS1b"); are the only "custom-created orders" and I set them to null after the entry, this can not cause the message(?)

            (When this new error would not have occurred Im at the end with everything. I have no idea how Visualstudio is working and Im afraid I have no more power to start anything new)

            Thank you!
            Tony

            Comment


              #7
              Hello Tony, thanks for your reply.

              By a complex object, I mean an object that is not one of the primitive data types in C# (an Order object, or a custom class). Visual Studio or Print() methods placed throughout the strategy must be used to find out what is null, I'm not able to know what is null.

              Best regards.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Hello,

                I can understand that you dont know what is null here. My question was - maybe inaccurate in english, sorry if so - if "user-created" doubles, ints, bools have to be set to null. Or only orders have to. (or what other "types" of data)

                Thank you!
                Tony

                Comment


                  #9
                  Hi Tony, thanks for your reply.

                  In C#, you can make int, bools, doubles, etc "Nullable", but in this case, they are not nullable, so they are not causing the object reference exception.

                  Kind regards.

                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello,

                    thank you for clearifying this. Im testing to find out the reason and what I do not understand is why the entryorder is not set to null with entry of entryOrderSr1a when I have in onexecution
                    if (entryOrderSr1a == execution.Order)
                    {
                    if (execution.Order.OrderState == OrderState.Filled)
                    {
                    avgFillSr1a=execution.Order.AverageFillPrice;
                    tradequantity=Positions[4].Quantity;
                    entryOrderSr1a=null;
                    }

                    What might be wrong there please?

                    For debugging purposes I print the orders and some bools in the chart and there I see that its not set to null after the entry.

                    Thank you!
                    Tony

                    Comment


                      #11
                      Hello tonynt, thanks for your reply.

                      Once the entryOrderSr1a is set to null, it should be set to null. The assignment is not ignored unless the code does not actually run, that would imply OrderState is not filled. Did you add a Print right after the null assignment?
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello,

                        thank you for your reply. I do not remember how to add the print statements correctly for having the informations needed. Maybe you can post the snippet?

                        Thank you!
                        Tony

                        Comment


                          #13
                          Hello Tony, thanks for your reply.

                          If you print it out like this, the object should be null:

                          if (entryOrderSr1a == execution.Order)
                          {
                          if (execution.Order.OrderState == OrderState.Filled)
                          {
                          avgFillSr1a=execution.Order.AverageFillPrice;
                          tradequantity=Positions[4].Quantity;
                          entryOrderSr1a=null;
                          if(entryOrderSr1a == null)
                          {
                          Print("entryOrderSr1a is null");
                          }
                          }
                          Chris L.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by pdennis, Today, 07:43 AM
                          1 response
                          5 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by NinjaTrader_Brandon, Today, 07:49 AM
                          0 responses
                          7 views
                          0 likes
                          Last Post NinjaTrader_Brandon  
                          Started by b16_aln, Today, 05:15 AM
                          1 response
                          7 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by Negustrader, Today, 01:01 AM
                          1 response
                          16 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by soon123, Yesterday, 10:50 PM
                          1 response
                          10 views
                          0 likes
                          Last Post NinjaTrader_ChrisL  
                          Working...
                          X