Orders
Order sides
Section titled “Order sides”Equity traders already think in terms of four different order sides: Buy/Sell, and SellShort/Cover. Futures traders generally only think in terms of Buy/Sell since short selling is an integral part of the futures market (for every long there is naturally an offsetting short). The backtester operates more like an equity trader, and indeed, it can keep track of Reg-T margin requirements and debit/credit interest on cash and short balances for equity strategies. The backtester does require the use of four different order sides to keep track of long and short positions separately even when testing futures contracts.
Given the four different order sides, this means a call to SellShort will be ignored if you currently have an open long position. You would need to explictly close this long position first by calling Sell before issuing an opening SellShort order. Alternatively, you can set the AutoReverseOnOppositeEntry property to true in OnStrategyStart() and the backtester will do that work for you. Methods used to close a position like Sell or Cover can safely be called when there is an opposite position or no position at all. If the orders aren’t appropriate for the current position, they will be ignored. This reduces the need for writing extra logic around order placement.
Order types
Section titled “Order types”So far our examples have only used Buy() and Sell() which generate market orders for execution on the open of the next bar. The other key order methods include:
BuyLimitBuyStopBuyClose
Of course there are corresponding order methods for Sell, SellShort, and Cover as well. All these methods are convenience methods that create and submit the appropriate order object under the hood. If for some reason you need more control, you can always submit orders directly. For example, below we check if there is a long position and then submit a sellstop order directly to the SubmitOrder method.
if (MarketPosition == PositionSide.Long){ SubmitOrder(new StopOrder(OrderSide.Sell, CurrentPosition.Quantity, Symbol, CurrentPosition.Stop) { SignalName = "Protective stop" });}But there is rarely a need to do this. After all, abstracting away the underlying complexity to speed the testing process is a major part of the backtester’s appeal.
Time in force
Section titled “Time in force”All orders have a default TimeInForce of Day. In the context of the backtester, this means orders are good for the next bar, whether that bar is daily, weekly or even one minute. For efficiency in backtesting, it’s generally advisable to use the built-in convenience methods which use the default TimeInForce. If you prefer, you can create and manage orders with other TimeInForce settings but it would then be incumbent upon you to manage these orders. For example, if you wanted to manually submit stop orders with a TimeInForce of GTC, to cancel them you would need to call CancelOrder() and pass in the order.
var pending = GetPendingOrders().OfType<StopOrder>().Where(x => x.Side == OrderSide.Sell);
foreach (var sellStop in pending){ CancelOrder(sellStop);}