Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EnterLongLimit() and Null

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

    EnterLongLimit() and Null

    I get in my strategy, after a few runs, NULL IOrder variables. (I realised because I was attempting to cancel the orders and I couldn't believe) they were NULL right after EnterLongLimit/EnterShortLimit. To compensate this, i wrote some helper functions, like this:
    Code:
    IOrder ReliableEnterLongLimit(int qt,double price,string sig)
    		  {
    		   IOrder tryout;
    		   tryout=null;
    		   while (tryout==null)
    	         {
    			   tryout=EnterLongLimit(qt,price,sig);
    			   if (tryout==null)
    			     System.Threading.Thread.Sleep(100);
    			 }
    		   return(tryout);
    		  }
    but now the strategy enters a lockdown ; ninja goes Not responding and I have to close forcibly.
    Now, first, why can I get a NULL from an EnterLongLimit/EnterShortLimit , and second, how to go around it?

    #2
    fxeconomist,

    This issue could be caused by a number of things. For example, wrong pricing, invalid order submission, etc. I suspect your platform is hanging do to the System.Threading.Thread.Sleep() command, possibly being caused by the code looping back to that statement over and over. Unfortunately that command is not supported. I would suggest reading the following sections of the help guide, in particular "Internal Order Handling Rules that Reduce Unwanted Positions" in the "Managed Approach" section as well as the "Advanced Order Management" section.

    Managed Approach : http://www.ninjatrader.com/support/h...d_approach.htm

    Advanced Order Handling : http://www.ninjatrader.com/support/h...r_handling.htm

    Checking for Null Order references : http://www.ninjatrader.com/support/f...ead.php?t=4226

    Please let me know if I can assist further.
    Last edited by NinjaTrader_AdamP; 10-24-2011, 09:27 AM.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hi Adam,

      Well it hangs up also if I don't use the Sleep call. To me, the question is simple. I want that EnterLongLimit() and EnterShortLimit() return a non-null result. I used the Sleep call to allow the application to try later, in another context, rather than just continuously attempting to place the order. Normally, since there is no execution involved and the function just posts the limit order, there should be no Null result unless the connection is lost. I think the method should successfully return a non-null in 90% of the cases, thus even in a while loop it should be done by the 3rd run of the cycle... I am getting this strange result after a series of removals:


      **NT** Enabling NinjaScript strategy 'HitRate/810c62e26e884c1c8a3478ac36be38ab' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True MaxRestarts=4 in 5 minutes
      ticks...
      FGBL 12-11:: SELL Trigger(10) Hit!
      FGBL 12-11:: Sent Sell order for 1 @134.9
      FGBL 12-11:: Market moved. Order cancelled!
      ticks...
      FGBL 12-11:: SELL Trigger(10) Hit!
      FGBL 12-11:: Sent Sell order for 1 @134.9
      FGBL 12-11:: Market moved. Order cancelled!
      ticks...
      FGBL 12-11:: SELL Trigger(10) Hit!
      FGBL 12-11:: Sent Sell order for 1 @134.9
      FGBL 12-11:: Market moved. Order cancelled!
      ticks...
      FGBL 12-11:: SELL Trigger(10) Hit!
      FGBL 12-11:: Sent Sell order for 1 @134.9
      FGBL 12-11:: WTF ORDER IS NULL!
      **NT** Error on calling 'OnMarketData' method for strategy 'HitRate/810c62e26e884c1c8a3478ac36be38ab': Object reference not set to an instance of an object.

      Of course, market moved, it attempted to close the last order, just that it had null reference...:

      Code:
      TradingOn=2;
      					TradingPrice=myBid;
      					mainOrderFilled=0;
      					mainOrder=EnterShortLimit(qty,myBid,"main");	
      					Print(Instrument.FullName+":: Sent Sell order for "+Convert.ToString(qty)+" @"+Convert.ToString(myBid));
      					if (mainOrder==null)
      					  Print(Instrument.FullName+":: WTF ORDER IS NULL!");

      Comment


        #4
        fxeconomist,

        Thanks for clarification.

        A sleep call on one indicator/strategy will affect all others since they run in the same thread for live trading. This being the case, if one strategy/indicators calls this it will cause all the others to sleep. This will disable all processing of ticks.

        Orders are multi-threaded, which means that depending on the race condition iOrder objects may not hold anything until the next OnBarUpdate or until the order goes into the state PendingSubmit. A race condition is essentially the timing of events not being in the order you expect them in code which can lead precisely to the issue you describe, however it can be fixed by proper order management and using the correct event-based methods.



        You could try using http://www.ninjatrader.com/support/h...nexecution.htm in your handling of orders.

        Everything in NinjaTrader is event driven, an here are some more examples of methods you can use for various events. http://www.ninjatrader.com/support/h...n_program2.htm

        Please let me know if I can assist further.
        Last edited by NinjaTrader_AdamP; 10-24-2011, 01:39 PM.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          I have a similar issue.

          However, the problem is that the submitted order never appears anywhere.
          Use case:
          1. IOrder order = EnterLongLimit(BarsInProgress, true, 1, limitPrice, tag);
          order is NULL at this point. Fine.

          Originally posted by NinjaTrader_AdamP View Post
          depending on the race condition iOrder objects may not hold anything until the next OnBarUpdate or until the order goes into the state PendingSubmit.
          So I expect that eventually I will have this order in OnBarUpdate function. The problem, as I said, this is not the case. WHY?

          Is there any way to diagnose the problem better? More debug/error information maybe?

          Comment


            #6
            Hi alex.nt,

            As a best practice here, you may want to provide a more unique name for your orders. order is the name used in all our examples for the advanced handlers and may cause confusion for the compiler if you also use the same.

            Then to debug and track messages related to your orders, enable TraceOrders = true; in the Initialize() method, and you'll have messages related to strategy orders in the Tools > Output window.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              What do you mean when saying "more unique name for your orders" - a variable name for the IOrder object or signalName?

              It is impossible to have unique variable names in a program that works with variable number of orders. The whole purpose of programming would be ruined if there was a requirement to have unique variable name per object.

              As for signalName, I set them to unique values using DateTime.

              Is there any way to diagnose better the reason of IOrder == NULL? I mean there must be some error associated with a particular case, right?

              Thanks

              I wonder how can I diagnose

              Comment


                #8
                alex,

                Thanks for your note,

                What do you mean when saying "more unique name for your orders" - a variable name for the IOrder object or signalName?
                I believe he meant to use something like : IOrder unique_order = EnterLongLimit( <inputs> ); , i.e. do not use "order".

                It is impossible to have unique variable names in a program that works with variable number of orders. The whole purpose of programming would be ruined if there was a requirement to have unique variable name per object.
                You can track orders by assigning an IOrder object to the an order that called OnOrderUpdate() or OnExecution(). From a lower level perspective its simply acquiring the pointer to the object for the order that called either method. Its not a unique name, but its a unique pointer.

                Is there any way to diagnose better the reason of IOrder == NULL? I mean there must be some error associated with a particular case, right?
                Perhaps. In the line : IOrder order = EnterLongLimit(BarsInProgress, true, 1, limitPrice, tag);

                What is limitPrice?

                It is a good idea to use Try/catch blocks to see if there are any errors while running code, as well as liberal use of print statements.
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_AdamP View Post
                  alex,

                  Thanks for your note,



                  I believe he meant to use something like : IOrder unique_order = EnterLongLimit( <inputs> ); , i.e. do not use "order".
                  m-mm, why would a name of the reference matter? I think we have a miscommunication here. Of course, I store these IOrder references in Dictionary... as far as I have been told there is no order Id string.

                  This is done on bar update under certain conditions:

                  // unique signal name, based on GUID
                  string tag = OrderTag(indicatorName);

                  // LimitPriceOffsetTicks is a positive integer
                  double offset = LimitPriceOffsetTicks * TickSize;

                  // BuySell enum contains Buy and Sell members only
                  double limitPrice = Close[0] + (buySell == BuySell.Buy ? -offset : offset);

                  IOrder order = null;
                  if (buySell == BuySell.Buy)
                  {
                  order = EnterLongLimit(BarsInProgress, true, 1, limitPrice, tag);
                  }
                  else
                  {
                  order = EnterShortLimit(BarsInProgress, true, 1, limitPrice, tag);
                  }

                  // here I add it to collection and sometimes it is NULL here
                  m_orderManager.Add(order);

                  IOrder may be NULL here, fine, but it eventually must appear in OnOrderUpdate(), correct?

                  I make the following checks in OnOrderUpdate():

                  if (order == null)
                  {
                  return;
                  }

                  if (order.OrderState == OrderState.Cancelled
                  || order.OrderState == OrderState.Filled
                  || order.OrderState == OrderState.Rejected)
                  {
                  // Print out information about the order
                  m_strategy.Out(order.ToString() + ", " + order.OrderState);
                  }

                  Neither NT traces nor this function detect anything. It looks like EnterLongLimit results in NULL and everything is stopped at this point. The only thought I have is to print IOrder into log in spite of OrderState. May be an order goes into Working state (but shouldn't that be reflected in NT traces?) See, the thing is that NT traces are silent too.

                  as for Try/catch blocks - yes, they are used, at higher level. There is no exception detected.

                  Comment


                    #10
                    alex.nt,

                    In OnOrderUpdate() you should be getting some updates when an order is pending, etc. Are you not getting your print statement in OnOrderUpdate() to display at all when you run this strategy?

                    Have you checked what the LimitPrice is in a print statement before your orders are submitted and made sure it makes sense for the current market data?
                    Adam P.NinjaTrader Customer Service

                    Comment


                      #11
                      In OnOrderUpdate() I print the message if only order state is Cancelled or Filled or Rejected. And nothing is printed. There is nothing from NT traces as well.

                      Limit price as I showed is at least equal to Close[0] (or less in case of Buy), so the price is OK. After all, this logic works almost always.

                      Comment


                        #12
                        alex,

                        Yes, however its helpful to check in print statements.

                        I would suggest also printing pending states for the orders.

                        There are some error codes you can also check for to see if there is some issue : http://www.ninjatrader.com/support/h...tml?iorder.htm

                        NativeError
                        Possible values are:

                        ErrorCode.BrokerOrderError
                        ErrorCode.InvalidInstrument
                        ErrorCode.LoginFailed
                        ErrorCode.NoError
                        ErrorCode.NotConnected
                        ErrorCode.NotSupported
                        ErrorCode.OrderRejected
                        ErrorCode.Panic
                        ErrorCode.ServerConnectionIsBroken
                        ErrorCode.UnableToCancelOrder
                        ErrorCode.UnableToChangeOrder
                        ErrorCode.UnableToSubmitOrder
                        ErrorCode.UserAbort

                        Where are you assigning the buySell variable from? Is this an OrderAction object? If your order is null, then this may not be defined yet.

                        Keep in mind there are 4 states for this :

                        OrderAction.Buy
                        OrderAction.BuyToCover
                        OrderAction.Sell
                        OrderAction.SellShort

                        I look forward to helping you resolve your issue.
                        Adam P.NinjaTrader Customer Service

                        Comment


                          #13
                          I have added more diagnostic into OnOrderUpdate: now I print order in all cases + print error (if it is not equal to NoError), will let you know the results.

                          buySell parameter comes from the function, it is NOT OrderAction. It is a custom type and set to either Buy or Sell. It defines what function to use: EnterLongLimit or EnterShortLimit.

                          Comment


                            #14
                            Update.
                            Here is what is going on:
                            1. The strategy is in long position, NO profit target & NO stop loss.
                            2. on bar update if condition#1=true then it attempts to exit a position via ExitLong(), which results in this trace:
                            1/23/2012 8:03:59 PM Entered internal PlaceOrder() method at 1/23/2012 8:03:59 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

                            3. Then, at the same bar update, i.e. right after ExitLong() call, if condition#2=true then the strategy attempts to submit sell limit order via EnterShortLimit call, which results in this trace:
                            1/23/2012 8:03:59 PM Entered internal PlaceOrder() method at 1/23/2012 8:03:59 PM: BarsInProgress=0 Action=SellShort OrderType=Limit Quantity=1 LimitPrice=1314.25 StopPrice=0 SignalName='4fe37b9a-5c69-412f-bcc4-45121158e08e' FromEntrySignal=''

                            And EnterShortLimit mentioned above returns NULL IOrder. After that there is no reference to this order in logs/traces.

                            So I guess the question is: Is it legal to submit sell limit order right after submitting market order to exit long position? If not, what is the best practice for such scenarios?

                            Thanks!

                            Comment


                              #15
                              Originally posted by alex.nt View Post
                              Update.
                              Here is what is going on:
                              1. The strategy is in long position, NO profit target & NO stop loss.
                              2. on bar update if condition#1=true then it attempts to exit a position via ExitLong(), which results in this trace:
                              1/23/2012 8:03:59 PM Entered internal PlaceOrder() method at 1/23/2012 8:03:59 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

                              3. Then, at the same bar update, i.e. right after ExitLong() call, if condition#2=true then the strategy attempts to submit sell limit order via EnterShortLimit call, which results in this trace:
                              1/23/2012 8:03:59 PM Entered internal PlaceOrder() method at 1/23/2012 8:03:59 PM: BarsInProgress=0 Action=SellShort OrderType=Limit Quantity=1 LimitPrice=1314.25 StopPrice=0 SignalName='4fe37b9a-5c69-412f-bcc4-45121158e08e' FromEntrySignal=''

                              And EnterShortLimit mentioned above returns NULL IOrder. After that there is no reference to this order in logs/traces.

                              So I guess the question is: Is it legal to submit sell limit order right after submitting market order to exit long position? If not, what is the best practice for such scenarios?

                              Thanks!
                              If the original position has no protective and/or target exits, then just issue the reverse order, and it will reverse. If long, enter an EnterShrotLimit() and vice-versa. (Do not first issue an exit order).

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by nandhumca, Yesterday, 03:41 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by The_Sec, Yesterday, 03:37 PM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by vecnopus, Today, 06:15 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post vecnopus  
                              Started by Aviram Y, Today, 05:29 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post Aviram Y  
                              Started by quantismo, 04-17-2024, 05:13 PM
                              3 responses
                              27 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X