I THINK I've figured how to cancel an order in live trading:
TradeManager.TradingProfiles[0].CancelOrder(curOrderID);
But this does not seem to work when backtesting...
How are orders cancelled in backtesting; I want to cancel a "stop-limit" order if not filled within a specified number of bars...
Thanks!
Cancel an order in backtest [SOLVED]
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Cancel an order in backtest
Orders that are not re-submitted are cancelled. So if you stop sending your stop-limit order, it will be removed.How are orders cancelled in backtesting; I want to cancel a "stop-limit" order if not filled within a specified number of bars...
In pseudo-code, something like:
Code: Select all
// Only submit orders when there are less then 10 bars;
// otherwise, don't send order (and cancel previously send order)
if (numberOfBars < 10 && myEntryCondition = true)
{
stopLmt.Send(theSellStopPrice, theLimitPrice);
}
Re: Cancel an order in backtest
this is what I expect happens when a stop-limit is submitted:
1. the order is submitted to the broker with the stop & limit and creates an orderID
2. the position is "flat" until the order is filled.
3. the position is the "long" or "short"
when trading "live" :
if a "cancel(orderID)" is sent to the profile before the order is filled (step 2), the order is cancelled.
From your reply, in backtesting, this is different :
since there is really no account, the order is just open until the stop & limit are met and then it becomes a position...
So the "cancel" does nothing and in backtesting mode has to be coded differently...
Pseudo code:
1.submit order
2.save stop & limit
3.check each bar
4.if stop & limit is met, the order creates position
5.if order "canceled", clear stop & limit and stop checking
...This means that alternate code path is needed when in backtest mode; YUCK!
Inconvenient, but do-able.
Let me know if I still am missing something or if my assumptions are incorrect.
Thanks!
1. the order is submitted to the broker with the stop & limit and creates an orderID
2. the position is "flat" until the order is filled.
3. the position is the "long" or "short"
when trading "live" :
if a "cancel(orderID)" is sent to the profile before the order is filled (step 2), the order is cancelled.
From your reply, in backtesting, this is different :
since there is really no account, the order is just open until the stop & limit are met and then it becomes a position...
So the "cancel" does nothing and in backtesting mode has to be coded differently...
Pseudo code:
1.submit order
2.save stop & limit
3.check each bar
4.if stop & limit is met, the order creates position
5.if order "canceled", clear stop & limit and stop checking
...This means that alternate code path is needed when in backtest mode; YUCK!
Inconvenient, but do-able.
Let me know if I still am missing something or if my assumptions are incorrect.
Thanks!
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Cancel an order in backtest [SOLVED]
In real-time trading, orders can be cancelled just like the example I gave for backtesting.(...)
From your reply, in backtesting, this is different :
(...)
So the "cancel" does nothing and in backtesting mode has to be coded differently...
(...)
...This means that alternate code path is needed when in backtest mode; YUCK!
Inconvenient, but do-able.
I think you're confusing managed with unmanaged orders:
When it comes to managed orders, these are cancelled as soon they are not resubmitted any more (and act the same in real-time and backtests).
Unmanaged orders, on the other hand, need to be actively cancelled by you. In that case, you'll need to order ID. And given that unmanaged orders cannot be backtested, you need different code for the backtest and real-time trading.
Also see managed and umanaged orders.