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

OnExecutionUpdate Bracket Orders - Edge Case

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

    OnExecutionUpdate Bracket Orders - Edge Case

    I've been referring to the example strategy for utilising ExecutionUpdate and OrderUpdate for the purpose of tracking when the Entry order is filled in order to implement bracket orders for protective stop and targets. The example is here:

    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()


    I got it working great and have been using the replay connection to test out various scenarios. I'm using the unmanaged approach.

    During my testing I submitted a Sell Limit entry order below current price - it was by accident, actually; I got some numbers wrong: its a scenario that I had not considered to test.

    Although sell limit below current price is an unusual scenario it is a valid one - since an order to sell at a limit price will sell at the defined limit price.... or better! Thus submitting a Sell Limit below current price results in an immediate fill. As current price is even higher than the limit price then this is a better price to sell at and so the order is entered and immediately filled. When I did this I noticed that I subsequently had a naked order with no stop or target...

    The problem is that the technique prescribed in the examples depends on the utilisation of a a reference to the Entry Order. We check that the Order being handled by the OnExecution/OrderUpdate Callback is, in-fact, our entry order; and if so: submit our stop and target orders.

    Unfortunately, in the scenario I have described, the entry-order has been submitted, filled, and is working... and the OnOrderUpdate and OnExecutionUpdate callbacks have fired even before the SubmitOrderUnmanaged method has returned me a reference which I can store and check. Its too late because it has already happened.

    Any thoughts on the best way to handle this edge case?

    #2
    Hello reach4thelasers,

    Thank you for your patience. To make sure I am completely understanding what you are saying:

    1) Your strategy submits either a buy limit above the market or a sell limit below the market and attempts to store it in an IOrder. EX: IOrder myOrder = SubmitOrder(/*etc*/);

    2) Because of how this situation is handled internally in NinjaTrader, the IOrder object is not being set before OnOrderUpdate and OnExecution are getting called and as a result your conditions are not being met in OnOrderUpdate or OnExecution for the Stop Loss and Profit Target orders to get attached to it.

    I will test this on my end and update you as soon as possible.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      Hello reach4thelasers,

      I have not been able to replicate this behavior using the following code (Please let me know if I am missing something):
      Code:
              protected override void Initialize()
              {
                  CalculateOnBarClose = true;
      			Unmanaged = true;
      			TraceOrders = true;
              }
              protected override void OnBarUpdate()
              {
      			if(myOrder == null)
      			{
      				myOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, GetCurrentAsk() - TickSize * 10, 0, "", "EnterLong");
      			}
              }
      		protected override void OnExecution(IExecution e)
      		{
      			if(e.Order != null && e.Order == myOrder && e.Order.OrderState == OrderState.Filled)
      			{
      				stopLoss = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, 1, 0, e.Order.AvgFillPrice + TickSize * 10, "", "StopLoss");
      				profitTarget = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, e.Order.AvgFillPrice - TickSize * 10, 0, "", "ProfitTarget");
      			}
      		}
      I am not positive that this issue was caused by the exact scenario you described. If you have your log and trace files from when this issue occurred and can provide them, I can look into this further. If you do not wish to post them on the forums, please feel free to email platformsupport[AT]ninjatrader[DOT]com with the subject line: "ATTN Michael M http://www.ninjatrader.com/support/forum/showthread.php?t=78823".

      If you want to add logic to your strategy to check if you have an open position that has no associated stop loss or profit targets, there are a lot of ways you could go about doing this. This would depend on how you have structured your current code.

      Please let me know if I may be of further assistance.
      Michael M.NinjaTrader Quality Assurance

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Skifree, Today, 03:41 AM
      3 responses
      12 views
      0 likes
      Last Post Skifree
      by Skifree
       
      Started by traderqz, Yesterday, 09:06 AM
      5 responses
      32 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by guillembm, Today, 11:25 AM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by owensd, 04-21-2024, 11:34 PM
      9 responses
      34 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by trilliantrader, 04-10-2024, 09:33 PM
      7 responses
      25 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X