Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

What causes a null return from EnterShort and EnterLong"

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

    What causes a null return from EnterShort and EnterLong"

    Hi,

    I'm trying to scale into my positions and am having some problems on or after the 3rd entry.

    The following code snippet yields the below output.

    At first I thought this might be the sim101 account running out of trading capital, but that doesn't seem to be the problem as I cut the trading size down to 50% of the account size and that didn't help.

    Entries per direction is set to 4....any help GREATLY appreciated!

    John


    entryCount++;
    Print(
    "Attempting to scale in EnterShort " + signalname);
    Print(
    "entryCount is: " + entryCount);
    Print(
    "shareSize is: " + shareSize);
    Print(
    "Should be submitting " + shareSize*(entryCount+1) + " shares");
    entryOrder = EnterShort(shareSize*(entryCount+
    1), signalname);
    Print(
    "Order Token is: " + entryOrder.Token);
    Print(
    "Setting lastEntryPrice to 0");
    lastEntryPrice =
    0.0;


    Attempting to scale in EnterShort [4165] Short_VRSI2_Extreme
    entryCount is: 1
    shareSize is: 51
    Should be submitting 102 shares
    Order Token is: cd09352bd9304c58bc8f6279e8fdb6c9
    Setting lastEntryPrice to 0
    Checking a fill
    entryOrder.Token is: cd09352bd9304c58bc8f6279e8fdb6c9
    execution.Order.Token is: cd09352bd9304c58bc8f6279e8fdb6c9
    Token matched
    Setting lastEntryPrice to: 0
    Attempting to scale in EnterShort [4165] Short_VRSI2_Extreme
    entryCount is: 2
    shareSize is: 51
    Should be submitting 153 shares
    7/27/2009 1:21:00 PM Catch 2 block error System.NullReferenceException: Object reference not set to an instance of an object.
    at NinjaTrader.Strategy.SpyIntraday6.OnBarUpdate()
    Attempting to scale in EnterShort [4165] Short_VRSI2_Extreme
    entryCount is: 3
    shareSize is: 51
    Should be submitting 204 shares
    7/27/2009 1:22:00 PM Catch 2 block error System.NullReferenceException: Object reference not set to an instance of an object.
    at NinjaTrader.Strategy.SpyIntraday6.OnBarUpdate()

    #2
    Hi John, you nailed it in your subject line. Having IOrder objects set to null (and when) is a key concept for advanced order handling. You may want to take a look at this reference page to get an idea of how NinjaTrader works "under the hood".
    AustinNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Austin View Post
      Hi John, you nailed it in your subject line. Having IOrder objects set to null (and when) is a key concept for advanced order handling. You may want to take a look at this reference page to get an idea of how NinjaTrader works "under the hood".

      Austin, I've been through that page trying to figure out what is going on - I do compare tokens when actually checking for fills, but the problem is that the EnterShort() command is coming back with a null pointer reference.

      I've checked the documentation and can't find anything that lists the valid conditions that could cause a EnterShort() or EnterLong() to return a null pointer......and that in turn means I don't know what to fix!

      John

      Comment


        #4
        John, before you try to access certain IOrder objects, it is necessary to make sure the IOrder is not null. This page describes how to do so. Basically, your code could look something like this:
        Code:
        [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]if (entryOrder != null)
        {
        [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] Print([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Order Token is: "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + entryOrder.Token);[/SIZE][/FONT][/SIZE][/FONT]
        }
        AustinNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Austin View Post
          John, before you try to access certain IOrder objects, it is necessary to make sure the IOrder is not null. This page describes how to do so. Basically, your code could look something like this:
          Code:
          [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]if (entryOrder != null)[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
          [/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Print([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Order Token is: "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + entryOrder.Token);[/SIZE][/FONT][/SIZE][/FONT]
          }

          Austin,

          Thank you, but unfortunately this still doesn't help me......

          I know how to handle null pointers - what I don't understand is why a call to EnterShort() or EnterLong() that should work as far as I can tell would ever return a null pointer!

          Can you give me any insight into the situations that cause this to happen?

          John

          Comment


            #6
            Originally posted by jlbishop View Post
            I know how to handle null pointers - what I don't understand is why a call to EnterShort() or EnterLong() that should work as far as I can tell would ever return a null pointer!
            The error in your logs is from trying to access a null object's property (entryOrder.Token), not from a call to EnterLong/Short. You can place a Print command between EnterShort and the next line that's supposed to print the token to verify this.

            If you look at the IOrder reference samples, you'll notice IOrders are set to null after they're either cancelled or filled (or partially filled).

            I'm not sure if this is how NinjaTrader handles IOrders by itself (I'm fairly sure it is), but I'll check with someone who would know after he gets back from lunch.

            Code:
            protected override void OnOrderUpdate(IOrder order)
                    {
                        // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
                        if (entryOrder != null && entryOrder.Token == order.Token)
                        {    
                            // Reset the entryOrder object to null if order was cancelled without any fill
                            if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                            {
                                entryOrder = null;
                            }
                        }
                    }
            Code:
            protected override void OnExecution(IExecution execution)
                    {
                        /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
                        which ensures your strategy has received the execution which is used for internal signal tracking. */
                        if (entryOrder != null && entryOrder.Token == execution.Order.Token)
                        {
                            if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                            {
                                // Resets the entryOrder object to null after the order has been filled or partially filled
                                if (execution.Order.OrderState != OrderState.PartFilled)
                                {
                                    entryOrder     = null;
                                }
                            }
                        }
                    }
            AustinNinjaTrader Customer Service

            Comment


              #7
              Austin,

              Just to be clear: I'm not trying to figure out how to handle the null value - for some reason NT is not entering short/long when I believe it should and the result of that inability is a Null pointer.....which I will handle better in my code once I figure out what is going on, but my priority is figuring out what is causing the market order to fail!

              John



              Originally posted by NinjaTrader_Austin View Post
              The error in your logs is from trying to access a null object's property (entryOrder.Token), not from a call to EnterLong/Short. You can place a Print command between EnterShort and the next line that's supposed to print the token to verify this.

              If you look at the IOrder reference samples, you'll notice IOrders are set to null after they're either cancelled or filled (or partially filled).

              I'm not sure if this is how NinjaTrader handles IOrders by itself (I'm fairly sure it is), but I'll check with someone who would know after he gets back from lunch.

              Code:
              protected override void OnOrderUpdate(IOrder order)
                      {
                          // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
                          if (entryOrder != null && entryOrder.Token == order.Token)
                          {    
                              // Reset the entryOrder object to null if order was cancelled without any fill
                              if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                              {
                                  entryOrder = null;
                              }
                          }
                      }
              Code:
              protected override void OnExecution(IExecution execution)
                      {
                          /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
                          which ensures your strategy has received the execution which is used for internal signal tracking. */
                          if (entryOrder != null && entryOrder.Token == execution.Order.Token)
                          {
                              if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                              {
                                  // Resets the entryOrder object to null after the order has been filled or partially filled
                                  if (execution.Order.OrderState != OrderState.PartFilled)
                                  {
                                      entryOrder     = null;
                                  }
                              }
                          }
                      }

              Comment


                #8
                Originally posted by jlbishop View Post
                Austin,

                Just to be clear: I'm not trying to figure out how to handle the null value - for some reason NT is not entering short/long when I believe it should and the result of that inability is a Null pointer.....which I will handle better in my code once I figure out what is going on, but my priority is figuring out what is causing the market order to fail!

                John
                Right. EnterLong() and EnterShort() aren't supposed to return null values, but I will double check that with a coworker. That is why I was suggesting the market orders aren't the problem.

                Can you place a Print() statement between your market order and the Print statement following the order to be 100% accurate on the source of the error? How about something like this:
                Code:
                Print("before the market order");
                [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]entryOrder = EnterShort(shareSize*(entryCount+[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]), signalname);
                Print("after the market order, before the order token check");
                Print([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Order Token is: "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + entryOrder.Token);
                Print("after the order token check");
                [/SIZE][/FONT][/SIZE][/FONT]
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  I had the same problem.

                  Activate order trading (TraceOrder= true) then you will see what happens under the hood.

                  There are some conditions where no order is generated. These are documented then in the log or the trace. SADLY there is no way to get information.

                  Reasons are multiple orders. Thre is a bug in NT 6.5 that wont supposedly get fixed (sorry, laughable for something that critical) that if you create a stop and then cancel it, you can not create a new one until the signal closes.

                  Sadly - a LOT WORSE for me.... if you create a limit to enter a position, and cancel it, you seemingly also can not create a new limit to enter the position. The old limit does not get properly cleaned up. Whops I call that a major problem.

                  Comment


                    #10
                    Not related NetTecture. The issue in this thread is not checking for null references before accessing properties of the object. This is an object oriented programming misstep as discussed by this tip: http://www.ninjatrader-support2.com/...ead.php?t=4226
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      I disagree. The question was under what circumstances null pointers happpen. And they do happen in perfect valid orders due to this bug.

                      The Original question was why he got back a null pointer.

                      Comment


                        #12
                        NetTecture,

                        I guarantee you if the user addresses the code as discussed in the tip it will work.

                        Will not work.
                        Code:
                        entryOrder = EnterShort();
                        Print(entryOrder.Token);
                        Will work.
                        Code:
                        entryOrder = EnterShort();
                        if (entryOrder != null)
                            Print(entryOrder.Token);
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Josh View Post
                          NetTecture,

                          I guarantee you if the user addresses the code as discussed in the tip it will work.

                          Will not work.
                          Code:
                          entryOrder = EnterShort();
                          Print(entryOrder.Token);
                          Will work.
                          Code:
                          entryOrder = EnterShort();
                          if (entryOrder != null)
                              Print(entryOrder.Token);
                          Ah, those wonderfull first level support answers - soemtimes technically correct information, but not helpfull.

                          May I remember you of the question of the user?

                          Just to be clear: I'm not trying to figure out how to handle the null value - for some reason NT is not entering short/long when I believe it should and the result of that inability is a Null pointer.....which I will handle better in my code once I figure out what is going on, but my priority is figuring out what is causing the market order to fail!
                          Now explain me how your answer actually is relevant to this question?

                          Here is a hint: it is not. It is technically correct, but it is a bad grade for actually not answering the question provided.

                          Comment


                            #14
                            NetTecture,

                            My response was addressed to you not jlbishop. Austin was already working in baby steps to demonstrate the order of events and why it behaves the way it does to jlbishop.

                            jlbishop,

                            From Austin's response you will see printouts of this immediately:
                            Code:
                            before the market order[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]
                            after the market order, before the order token check[/SIZE][/FONT][/SIZE][/FONT]
                            Now if you go to the next step and add Print()s into OnOrderUpdate() which is the event in which the IOrder object actually takes on the order you will see the prints from there come in after the print of your "after the market order". There is no "failure" in submitting the order, but rather the issue here is simply checking properties too early. You are trying to access Token properties when the token has not been generated yet. The generation is complete when you receive the first OnOrderUpdate() event.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Let's stay on track......

                              Guys,

                              I am trying to find out why null pointers are returned from EnterShort() and EnterLong() calls.

                              I fully realize I can check for and handle the null pointer if it should occur, but what I can't fix with my present level of knowledge/information is WHY I'm getting the null pointer in the first place.

                              At this point I would suggest treating this as a bug in NT - it is certainly preventing me from trading live until I can get it figured out. What log/debug information do you need that would be helpful?

                              Note that the situation that causes this bug to manifest (scaling in to a position >2 times) only happens about once every 2-3 days so it's hard to catch.

                              It is worth noting that the same logic works just fine for the first scale in - i.e. I get a valid pointer back from EnterShort/EnterLong, but fails on the second attempt, so I'm stuck with assuming that something is happening under the hood that I'm not aware of.....

                              John

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Taddypole, 04-26-2024, 02:47 PM
                              5 responses
                              35 views
                              0 likes
                              Last Post eDanny
                              by eDanny
                               
                              Started by kujista, 04-23-2024, 06:23 AM
                              6 responses
                              48 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by giulyko00, 04-24-2024, 12:03 PM
                              7 responses
                              36 views
                              0 likes
                              Last Post eDanny
                              by eDanny
                               
                              Started by NM_eFe, Today, 10:13 AM
                              0 responses
                              12 views
                              0 likes
                              Last Post NM_eFe
                              by NM_eFe
                               
                              Started by hdge4u, Yesterday, 12:23 PM
                              1 response
                              11 views
                              0 likes
                              Last Post hdge4u
                              by hdge4u
                               
                              Working...
                              X