Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error with Order Handling Rules

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

    Error with Order Handling Rules

    I am testing all of my code on a demo account now and I am running into an error. It looks like it is with the following section.

    The Output error that I am getting is:

    **NT** An Enter() method to submit an entry order at '6/17/2014 9:00:00 AM' has been ignored. Please search on the term 'Internal Order Handling Rules' in the Help Guide for detailed explanation.
    and this one:

    **NT** Error on calling 'OnExecution' method for strategy 'LondonBreakout/5d6a1d570e77498fbb5465a4c5ed090d': Object reference not set to an instance of an object.
    Therefore, it appears to be in the OnExecution. It appears that, so far, other areas of my code OnExecution & OnBarUpdate are working fine so I am not posting that for sake of ease. If you feel that you need more code to help me determine what is going on, just let me know.

    The purpose of this code is:

    In OnBarUpdate I have 3 sections for trend direction. Therefore, if TrendDirection.NoTrend, then it should place a long & and short Stop Entry Order (LongEntryOCO & ShortEntryOCO respectively). It appears that the error is, at this point, only happening when it is TrendDirection.NoTrend. The following is the OnExection portion that is connected to TrendDirection.NoTrend.

    These are "OCO" orders so that when the Long is triggered, then the short should be cancelled and a Stop Loss order to exit at a loss and a Sell Limit Order to exit at a profit should be placed. It will also then restrict any further trading for the day so that only one order is entered per day & per side.

    Okay, I think that is what you will need to help me figure it out. Let me know if you need anything else. And, as always, thank you very much for your assistance.

    Code:
    		protected override void OnExecution(IExecution execution)
    		{
    			if (execution.Order != null)
    			{
    				// Setting Stop Loss & Profit Target (Limit) Orders -- Long Only & Short Only
    				if (execution.Order.OrderState != OrderState.Filled)
    					return;
    				else if (LongEntryOCO != null
    					&& LongEntryOCO == execution.Order)
    				{
    					LongStopOCO = ExitLongStop(0, true, positionSize, (rangeLow - pipBuffer),"LongStopOCO","BreakoutLongOCO");
    					Print(Time[0] + " Long Stop OCO" + LongStopOCO);
    					LongTargetOCO = ExitLongLimit(0, true, positionSize, LongEntryOCO.AvgFillPrice + (LongEntryOCO.AvgFillPrice - (rangeLow - pipBuffer)), "LongTargetOCO", "BreakoutLongOCO");
    					Print(Time[0] + " Long Target OCO " + LongTargetOCO);
    					CancelOrder(ShortEntryOCO);
    					restrictTrade = true;
    				}
    			
    				else if (ShortEntryOCO != null
    					&& ShortEntryOCO == execution.Order)
    				{
    					ShortStopOCO = ExitShortStop(0, true, positionSize, (rangeHigh + pipBuffer), "ShortStopOCO", "BreakoutShortOCO");
    					Print(Time[0] + " Short Stop OCO " + ShortStopOCO);
    					ShortTargetOCO = ExitShortLimit(0, true, positionSize, ShortEntryOCO.AvgFillPrice - ((rangeHigh + pipBuffer) - ShortEntry.AvgFillPrice), "ShortTargetOCO", "BreakoutShortOCO");
    					Print(Time[0] + "Short Target OCO " + ShortTargetOCO);
    					CancelOrder(LongEntryOCO);
    					restrictTrade = true;
    				}
    			}
    		}

    #2
    In further researching this issue, I have found this from another forum. Do you expect that this is the issue?

    Please see Post #2 by RyanM
    Support for the development of custom automated trading strategies using NinjaScript.


    I don't want to go changing the code unless there is a good chance that this is the problem (or at least one of the problems). So if you could confirm that this is a high possibility, then I will go through and start trying to figure it out. Also, could you please let me know what I need to be doing in order to simulate one of the orders? Thank you!

    Comment


      #3
      jg123, yes correct you're running into the issue of the ignored order as it was caught by NT's internal order management rules. So in NoTrend you would basically try to place an entry bracket then, so each each entry for a different order direction? This I would expect to get caught by the handling rules.

      https://www.ninjatrader.com/support/...d_approach.htm (bottom section)

      'Methods that generate orders to enter a position will be ignored if: the strategy position is flat and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction'

      One option to workaround would be monitoring price direction for a breakout instead and then going with the breakout with one needed order only.

      Also for the OCO assumptions, in managed mode these orders would not be OCO natively, so your code would need to explicitly CancelOrder the unneeded leg.

      Comment


        #4
        Thanks for the confirmation.

        I found that I was submitting an EnterLongStop & EnterShortStop simultaneously in the OnBarUpdate() which would have been causing the issue. I did explicitly have a CancelOrder in the OnExecution, but it couldn't even get to that point because the OnBarUpdate was trying to submit both orders.

        As a result, I have tried to simulate an OCO order by waiting for the market price to reach the entry criteria prior to submitting a Market Order rather than simply using an EnterLongStop or EnterShortStop. The exact code will come a bit later. First, here is the error that I am getting in the Output Window:

        6/19/2014 10:00:00 AM Entered internal PlaceOrder() method at 6/19/2014 10:00:00 AM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=0.05M LimitPrice=0 StopPrice=0 SignalName='BreakoutShortOCO' FromEntrySignal=''
        6/19/2014 10:00:00 AM Entered internal PlaceOrder() method at 6/19/2014 10:00:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=0.05M LimitPrice=0 StopPrice=102.01 SignalName='ShortStopOCO' FromEntrySignal='BreakoutShortOCO'
        **NT** Error on calling 'OnExecution' method for strategy 'AABreakout2/9b876538e6174c6e86f4fe60ccafea0a': Object reference not set to an instance of an object.
        6/19/2014 10:00:00 AM CancelAllOrders: BarsInProgress=0
        This is not making sense to me because in OnExecution I have checked for null, which seems to be the most common reason for this error (if I remember correctly).

        Can you please double check to make sure that I am checking correctly for null? If that is not the problem, do you have any other ideas of what could be going wrong?

        In OnBarUpdate() I have put in this code for TrendDirection.NoTrend (OnBarUpdate()):

        Code:
        			else if (direction == TrendDirection.NoTrend)
        			{
        				if (LongEntryOCO == null
        					&& ShortEntryOCO == null
        					&& ToTime(Time[0]) > ToTime(endRangeHr, endRangeMin, 00)
        					&& ToTime(Time[0]) < ToTime(removeOrderHr, removeOrderMin,00)
        					&& High[0] == (rangeHigh + pipBuffer)
        					&& restrictTrade == false)
        				{
        					LongEntryOCO = EnterLong(positionSize, "BreakoutLongOCO");
        					restrictTrade = true;
        				}
        				
        				else if (ShortEntryOCO == null
        					&& LongEntryOCO == null
        					&& ToTime(Time[0]) > ToTime(endRangeHr, endRangeMin, 00)
        					&& ToTime(Time[0]) < ToTime(removeOrderHr, removeOrderMin, 00)
        					&& Low[0] == (rangeLow - pipBuffer)
        					&& restrictTrade == false)
        				{
        					ShortEntryOCO = EnterShort(positionSize, "BreakoutShortOCO");
        					restrictTrade = true;
        				}
        			}
        And the following code in the OnExecution()

        Code:
        				else if (LongEntryOCO != null
        					&& LongEntryOCO == execution.Order)
        				{
        					LongStopOCO = ExitLongStop(0, true, positionSize, (rangeLow - pipBuffer),"LongStopOCO","BreakoutLongOCO");
        					LongTargetOCO = ExitLongLimit(0, true, positionSize, LongEntryOCO.AvgFillPrice + (LongEntryOCO.AvgFillPrice - (rangeLow - pipBuffer)), "LongTargetOCO", "BreakoutLongOCO");
        					restrictTrade = true;
        				}
        				
        				else if (ShortEntryOCO != null
        					&& ShortEntryOCO == execution.Order)
        				{
        					ShortStopOCO = ExitShortStop(0, true, positionSize, (rangeHigh + pipBuffer), "ShortStopOCO", "BreakoutShortOCO");
        					ShortTargetOCO = ExitShortLimit(0, true, positionSize, ShortEntryOCO.AvgFillPrice - ((rangeHigh + pipBuffer) - ShortEntry.AvgFillPrice), "ShortTargetOCO", "BreakoutShortOCO");
        					restrictTrade = true;
        				}
        (The idea of submitting a Market order when the price criteria is met is not my favorite solution as the orders are then resident on my computer rather than with IB - thus not being properly submitted in the case of an internet disconnection.)

        Criteria for Longs (opposite for shorts) (In case you want it):
        • No Current Long Orders
        • No Current Short Orders
        • Current Time is within a particular range of time
        • high of current candle is "x" + "y" number of pips
        • restrict trade bool flag is set to false (to insure that only one trade is made per day)



        Finally: I am looking for what I can isolate out or Print in order to find out exactly what is going on, but I can not think of what I can do here.

        Comment


          #5
          jg123, for the null object, I would closely check into the OnExecution ShortEntryOCO part, since you seem to have a typo here and referencing the ShortEntry.AvgFillPrice (which is different from you do in the long section and an object you not checked for being null before accessing > hence the issue likely).

          I understand the option in managed mode to monitor price and decide then for one order is not ideal in this scenario for you, an alternative then would be switching over to unmanaged that would void of the managed mode order handling rules.

          Generally for more powerful debugging when a runtime exception is seen, you will find C#'s try / catch very helpful - http://www.ninjatrader.com/support/f...ead.php?t=9825

          Plus piecewise isolation, to catch your type for example you could have just worked one side and see if the same error was thrown. You want to isolate the code down to the core working basics and only then introduce additional complexities.

          Comment


            #6
            Thank you, Bertrand.

            This was some very good information and I am adding this to my notes for future reference.

            Eventually I will be taking this into the Unmanaged mode. That may require me using a NinjaScript consultant - but I will definitely be trying to learn it on my own for a little bit before I start going to them.

            Thanks again for the help and the further information on debugging. (a relatively thorough "debugging" webinar may be a great piece of education for NT to offer. It could show 3 examples of using try/catch and 3 examples of Print and 3 examples of commenting stuff out, etc. I think that would be incredibly beneficial for users)

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            574 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X