Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Avoiding crossing in-flight orders with limit orders and scaled entries

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

    Avoiding crossing in-flight orders with limit orders and scaled entries

    In Initialize(), I have:

    Code:
    Unmanaged = false;
    CalculateOnBarClose = false;
    In variables, I have:

    Code:
    IOrder order = null;
    int numContracts = 0;
    In OnBarUpdate(), I have:

    Code:
    // if going long, specify number of contracts in numContracts, if getting out of a long position set numContracts = 0
    
    order = GoLong(numContracts, order);
    In the execution function, I have the following:

    Code:
    		public IOrder GoLong(int numContracts, IOrder order)
    		{
    			if (order == null || order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Filled)
    			{
    				if (Position.MarketPosition == MarketPosition.Long)
    				{
    					if (Position.Quantity > numContracts)
    						return ExitLongLimit(Math.Min(Position.Quantity - numContracts, (int) GetCurrentBidVolume()), GetCurrentBid());
    					else if (Position.Quantity < numContracts && numContracts > 0)
    						return EnterLongLimit(Math.Min(numContracts - Position.Quantity, (int) GetCurrentAskVolume()), GetCurrentAsk(), 
    							Time[0].ToLongTimeString());
    				}
    				else return EnterLongLimit(Math.Min(numContracts, (int) GetCurrentAskVolume()), GetCurrentAsk(), Time[0].ToLongTimeString());
    				if (numContracts >= 0)
    					return ExitLongLimit(Math.Min(Position.Quantity, (int) GetCurrentBidVolume()), GetCurrentBid());
    			}
    			else return order;
    		}
    My question is whether the check of OrderState at the top of the execution function is sufficient to make sure that orders won't cross, yet still allow me to execute on every tick, with, in the worst case, orders canceling at the end of the bar, if not filled or cancelled. i.e. Is there any other OrderState that I need to check for if I don't want to have to wait any longer than the end of a bar to resubmit new orders?

    #2
    kaydgee, I think you would also need to cover the PartFilled orderstate in your script. Per default the unfilled orders would expire on the next OnBarUpdate() call with the managed approach that you use.

    Comment


      #3
      Bertrand,

      Thank you for clarifying. I guess I was under the mistaken impression that when CalculateOnBarClose == false, orders are only cancelled at the end of the bar, NOT on every call to OnBarUpdate().

      In any case, though, won't the order just keep working if OrderState == PartFilled, and then revert to an OrderState of Cancelled on the next call to OnBarUpdate()?

      If I check for OrderState == PartFilled, and then reissue an order, won't I have the situation that I'm trying to avoid? i.e. 2 working orders possibly causing an overfill?

      Comment


        #4
        You're welcome - correct the order would be kept working until filled then, unless you cancel order it via your code. I would reissue any entry order then, but you would need to ensure the incoming partials are taken care off by your exit orders. This would be better done then in OnExecution directly in such a case where you directly monitor for the incoming fills and amend exit orders on those seen.

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          ... but you would need to ensure the incoming partials are taken care off by your exit orders. This would be better done then in OnExecution directly in such a case where you directly monitor for the incoming fills and amend exit orders on those seen.
          If you notice, my ExitLongLimit() is driven by Position.Quantity. Won't this automatically account for partial fills?

          Comment


            #6
            Correct, this would update the exit order as well each time your routine is called - and Position.Quantity would be guaranteed to be update only on next OnBarUpdate() call, so if you moved this part to OnExecution() the timing for the exits would be likely more to the point.

            The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            666 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            377 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            110 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            575 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            580 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X