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

OrderState.PartFill handling unmanaged orders

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

    OrderState.PartFill handling unmanaged orders

    The following OnExecution code is taken from an example template found on the forum for "unmanaged"OCO orders. I'm trying understand what happens in the case of partial fill executions. My understanding is that an order may be OrderState.PartFilled multiple times before being coming to OrderState.Filled state during its lifetime.

    For example a long limit entry order of 10 contracts, may be completely filled by the following 3 OnExecution events, first a PartFilled event with say 5 contracts, a second PartFilled event with 4 contracts and finally reaching a Filled state with the last contract.

    Given that my order life cycle understating is correct, my question, in the case of an unmanaged approach, with the above example, will 3 pairs of stops and targets be issued or one pair?

    If 3 sets of stops and targets are issued, would the code below need to be enhanced to track each order object separately, perhaps in a list, such that they could be cancelled or updated or set to null elsewhere. In my example, would only the 3rd issued order object for the last target and stops be available? Given that the same variables are used for storing the object instance.

    If only one pair of stop/targets are ever issued, then Ninja's unmanaged order is actually doing lots of behind the scenes work and updating the first pair of stops and targets. If so the code will work just fine.


    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 (longEntry != null && longEntry == execution.Order)
    			{
    				if (execution.Order.OrderState == OrderState.Filled  // long entry is filled -yeah!
    					|| execution.Order.OrderState == OrderState.PartFilled // entry is partially filled - how to handle partial fill???????
    					|| (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))  // long entry was cancelled before any entries
    				{					
    					
    					//OCO stops and targets
    					stopLossLong = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, execution.Order.Filled, 0, execution.Order.AvgFillPrice - 4 * TickSize, "LongExits", "StopLossLong"); 
    
    					targetLong = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, execution.Order.Filled, execution.Order.AvgFillPrice + 8 * TickSize, 0, "LongExits", "TargetLong");
    					
    					// Resets the longEntry object to null after the order has been filled
    					if (execution.Order.OrderState != OrderState.PartFilled) 
    					{
    						longEntry 	= null;
    					}
    				}
    			}			
    			if (shortEntry != null && shortEntry == execution.Order)
    			{				
    				if (execution.Order.OrderState == OrderState.Filled 
    					|| execution.Order.OrderState == OrderState.PartFilled 
    					|| (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    				{					
    					stopLossShort = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, execution.Order.Filled, 0, execution.Order.AvgFillPrice + 4 * TickSize, "ShortExits", "StopLossShort"); 
    
    					targetShort = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Limit, execution.Order.Filled, execution.Order.AvgFillPrice - 8 * TickSize, 0, "ShortExits", "TargetShort");
    					
    					// Resets the shortEntry object to null after the order has been filled
    					if (execution.Order.OrderState != OrderState.PartFilled)
    					{
    						shortEntry 	= null;
    					}
    				}
    			}
    			
    			// Reset our stop order and target orders' IOrder objects after our position is closed.
    			if ((stopLossLong != null && stopLossLong == execution.Order) || (targetLong != null && targetLong == execution.Order))
    			{
    				if (execution.Order.OrderState == OrderState.Filled 
    					|| execution.Order.OrderState == OrderState.PartFilled)  
    				{
    					stopLossLong = null;
    					targetLong = null;
    				}
    			}
    			if ((stopLossShort != null && stopLossShort == execution.Order) || (targetShort != null && targetShort == execution.Order))
    			{
    				if (execution.Order.OrderState == OrderState.Filled 
    					|| execution.Order.OrderState == OrderState.PartFilled)
    				{
    					stopLossShort = null;
    					targetShort = null;
    				}
    			}
    		}

    #2
    Hello lavalampmj,

    Thank you for your post.

    You have the correct understanding of OrderState.PartFilled and OrderState.Filled. You need to control the orders completely in Unmanaged Order Approach. You could submit an order for each partial fill or wait until the full quantity of the entry order is filled. In the code you provide there would be one Stop Loss and one Profit Target. Each time the execution/order of the entry is updated through OnExecution() the Stop Loss and Profit Target are updated with the new quantity.

    Please let me know if you have any questions.

    Comment


      #3
      Thanks, That's clear. One other quick question related to the code.

      At the bottom of the code example, why is the author setting the Target and Stop orders to Null if either of their OnExecution is OrderState.PartFill or OrderState.Filled.

      Shouldn't setting to Null only occur if either order's state is OrderState.Filled?

      Comment


        #4
        Hello lavalampmj,

        Thank you for your response.

        In the example the null is set for the Orders on Fill or PartFill just to perform that task immediately and there is really no problem with this unless you need to wait for the exit order to fully fill before doing some task based on the Orders being null. You could wait for only a Fill to occur if you prefer.

        Please let me know if you have any questions.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by TraderBCL, Today, 04:38 AM
        2 responses
        11 views
        0 likes
        Last Post TraderBCL  
        Started by martin70, 03-24-2023, 04:58 AM
        14 responses
        105 views
        0 likes
        Last Post martin70  
        Started by Radano, 06-10-2021, 01:40 AM
        19 responses
        607 views
        0 likes
        Last Post Radano
        by Radano
         
        Started by KenneGaray, Today, 03:48 AM
        0 responses
        4 views
        0 likes
        Last Post KenneGaray  
        Started by thanajo, 05-04-2021, 02:11 AM
        4 responses
        471 views
        0 likes
        Last Post tradingnasdaqprueba  
        Working...
        X