DaveN
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Cancelling Order if Conditions Change
Collapse
X
-
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 -
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
-
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
-
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
-
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"); } } }
Thanks
DaveN
Comment
-
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
-
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
-
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; } } }
Maybe something like (entryOrder.Name == "LAbv_Band")?
DaveM
Comment
-
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.SellShortAdam P.NinjaTrader Customer Service
Comment
-
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; } } }
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)
Comment
-
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
-
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 Today, 10:23 PM | ||
Started by Belfortbucks, Today, 09:29 PM
|
0 responses
7 views
0 likes
|
Last Post
by Belfortbucks
Today, 09:29 PM
|
||
Started by zstheorist, Today, 07:52 PM
|
0 responses
7 views
0 likes
|
Last Post
by zstheorist
Today, 07:52 PM
|
||
Started by pmachiraju, 11-01-2023, 04:46 AM
|
8 responses
151 views
0 likes
|
Last Post
by rehmans
Today, 06:44 PM
|
||
Started by mattbsea, Today, 05:44 PM
|
0 responses
6 views
0 likes
|
Last Post
by mattbsea
Today, 05:44 PM
|
Comment