Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancelling Order if Conditions Change

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

    Cancelling Order if Conditions Change

    I have a strategy which looks for entries with the trend when there is a larger than normal excursion away from the centered average price. Since a trend can last for multiple bars, the strategy will place a limit order and the limit order will persist unless new conditions emerge to trade in the other direction. The problem with this approach occurs near the end of a trend, when instead of getting in on an aberrant price move that reverts to the mean, the trade gets into a position that is exactly wrong for the end of a trend. I would like to monitor conditions, and if the trend begins to end, cancel the order, but that would require that I either enter all orders using the unmanaged technique or mix between managed and unmanaged approaches. is that possible? Is it possible to enter orders using the managed approach to exit with a profit or a stop, but to cancel existing managed orders, should entry conditions change while the order is in place but not yet filled?
    DaveN

    #2
    daven,

    Unfortunately there is no way to mix these two approaches. However, you may be able to do this with the managed approach.

    When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until


    You can update your Limit/Stop orders as time progresses rather than have to cancel them.

    EnterLongLimit(1,Low[0],"MySignal");

    Will continually update this order given a signal name "MySignal" to a new price.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      I'm trying to modify the sample cancel orders strategy to accomplish what I want. I have one question.
      The example only shows trading in one direction, a long trade. If I want to add a short trade as well do I just use a different name for short entry vs the long entry to keep things straight, (with, of course, a different set of conditions for the short entry vs the long entry)?
      Thanks
      DaveN

      Comment


        #4
        daven,

        Yes, you could do that. Separating by signal name may make it easier to track the two different orders using the managed approach.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Also, in the example in the onOrderUpdate section if the entryOrder is cancelled and entryOrder = null, the strategy changes the limit entry order to a mar****rder. What is the purpose of that change? I don't want to enter an order as a market order I only want to enter at the limit price or better so should I just leave that portion of the onOrderUpdate section out?
          Thanks
          DaveN

          Comment


            #6
            daven,

            Could you post the code you are referring to here? I am not sure I understand what section you are referring to. Also, just telling me what lines they are in the original code would help.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Here's the section I mean, it is lines 76 through 91 in the strategy:

              Code:
              		protected override void OnOrderUpdate(IOrder order)
              		{
              			// Checks for all updates to entryOrder.
              			if (entryOrder != null && entryOrder == order)
              			{	
              				// Check if entryOrder is cancelled.
              				if (order.OrderState == OrderState.Cancelled)
              				{
              					// Reset entryOrder back to null
              					entryOrder = null;
              					
              					// Replace entry limit order with a market order.
              					mar****rder = EnterLong(1, "market order");
              				}
              			}
              		}
              I believe it is the section of code which deals with orders that are changed.
              Thanks
              DaveN

              Comment


                #8
                daven,

                I believe you can leave that part out. It looks like just an example of how to convert a limit to a market order if some conditions are met.
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Okay, so leaving that out will help. Now I need to enter the profit and stop orders for both the long and short entry conditions once the order is filled. Can I determine which fill I have (long or short) by testing for the entry name (in the case of my strategy it is either "LAbv_Band", for a long, or SBlo_Band" for a short. If this is the way to do it, what is the syntax for that query? Alternatively do I just check if market position is long or short, and then branch off of that?
                  I appreciate you helping me with this, thanks.
                  DaveN

                  Comment


                    #10
                    Dave,

                    Likely you'll need to use a few reference samples here. For example the one below shows you how to detect when an order is filled, then you can submit protective orders immediately.

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


                    OnExecution is likely the only section you would need, however you can also check out the OnOrderUpdate.

                    OnExecution is called whenever an order is filled.

                    OnOrderUpdate is called whenever an order status is updated, like if you were to change the price of a limit order for example, or if an order goes from being in a "submitted" state to a "working" state which just means it went from being sent to the broker to being confirmed to be working on the exchange.
                    Adam P.NinjaTrader Customer Service

                    Comment


                      #11
                      So within both samples I have looked at, SampleonOrderUpdate, and Sample CancelOrders, The onExecution section is basically the same in each of the sample codes. So how do I modify this area to test if my filled order is a long or short order?

                      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 (entryOrder != null && entryOrder == execution.Order)
                      			{
                      				if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                      				{
                      					// Stop-Loss order 4 ticks below our entry price
                      					stopOrder 	= ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 4 * TickSize, "MyStop", "MyEntry");
                      					
                      					// Target order 8 ticks above our entry price
                      					targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
                      					
                      					// Resets the entryOrder object to null after the order has been filled or partially filled
                      					if (execution.Order.OrderState != OrderState.PartFilled)
                      					{
                      						entryOrder 	= null;
                      					}
                      				}
                      			}
                      Can I add a test in the first line (entryorder != null && entryOrder == execution.Order) that looks for either the long or short order label to determine whether I need to enter long stops and profits or short stops and profits?
                      Maybe something like (entryOrder.Name == "LAbv_Band")?
                      DaveM
                      Last edited by daven; 10-30-2012, 10:45 AM. Reason: left something out

                      Comment


                        #12
                        Dave,

                        execution.Order is the IOrder object whose fill caused the OnExecution method to be called.

                        If you look at : http://www.ninjatrader.com/support/h...tml?iorder.htm

                        IOrder objects have a internal variable : IOrder.OrderAction

                        OrderAction
                        Possible values are:

                        OrderAction.Buy
                        OrderAction.BuyToCover
                        OrderAction.Sell
                        OrderAction.SellShort
                        Adam P.NinjaTrader Customer Service

                        Comment


                          #13
                          I tried testing by order name and none of my stops or profit targets are working. Orders all exit by an Exit on Close Exit name so clearly neither stops nor profits are working. Here is what I used, and it did compile just fine:

                          Code:
                          			if (entryOrder != null && entryOrder == execution.Order && execution.Order.Name == "SBlo_Band")
                          			{
                          				// This second if-statement is meant to only let fills and cancellations filter through.
                          				if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                          				{
                          					// Simple stop and target
                          					stopOrder = ExitShortStop(0, true, 1, execution.Price + ATRTicks(14).Max_ATR[0] , "short_stop", "SBlo_Band");
                          					targetOrder = ExitShortLimit(0, true, 1, execution.Price - ATRTicks( 14).ATRinTicks[0] + Offset * TickSize, "short_target", "SBlo_Band");
                          					
                          					// Resets the entryOrder object to null after the order has been filled
                          					if (execution.Order.OrderState != OrderState.PartFilled)
                          					{
                          						entryOrder 	= null;
                          					}
                          				}
                          			}
                          any suggestions about how to modify the above code to get stops and profits working?

                          Alternatively using the last sample you reference do I test for long or short as follows:

                          Code:
                          if (entryOrder != null && entryOrder == execution.Order && Order.OrderAction == OrderAction.Buy)
                          The problem with this approach is I have to test the order four times, Buy, buytoCover, Sell, and SellShort.

                          Comment


                            #14
                            That wouldn't even compile, so I'm not sure what to do next? The problem with your samples is they only show one trade direction, and the logic for determinine whether the program did a buy or sell is apparently not very straight forward, (at least not for me). it would sure help if you had a few sample codes that showed the full logic branching of testing for a long entry or a short entry.
                            DaveN

                            Comment


                              #15
                              Dave,

                              Short entry is pretty much the same layout just with some syntax shifted around. E.g. instead of EnterLong(), use EnterShort(), or instead of MarketPosition.Long use MarketPosition.Short.

                              These two reference samples are doing things a bit differently. Likely there are variables one is using the other is not. The idea of the reference samples is to show you sort of "What you can do" and "How to do it" however keeping in mind likely you wont need everything inside the reference sample.

                              It is also recommended to really strip down your strategy logic and then add complexity as you go. Get some basic thing working first, then add things later.

                              Let's take a step back here. Can you tell me what you are trying to do kind of in basic steps here? You have a limit or stop order that is in the market, when it fills you want a stop loss but how do you want this calculated?
                              Adam P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              158 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Today, 09:29 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Belfortbucks  
                              Started by zstheorist, Today, 07:52 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post zstheorist  
                              Started by pmachiraju, 11-01-2023, 04:46 AM
                              8 responses
                              151 views
                              0 likes
                              Last Post rehmans
                              by rehmans
                               
                              Started by mattbsea, Today, 05:44 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post mattbsea  
                              Working...
                              X