Trading and Orders
Order Properties
Introduction
Order properties enable you to customize how the brokerage executes your orders. The DefaultOrderProperties
of your algorithm sets the order properties for all of your orders. To adjust the order properties of an order, you can change the DefaultOrderProperties
or pass an order properties object to the order method.
Time In Force
The TimeInForce
property determines how long an order should remain open if it doesn't fill. This property doesn't apply to market orders since they usually fill immediately. Time in force is useful to automatically cancel old trades. The following table describes the available TimeInForce options:
Member | Example | Description |
---|---|---|
GoodTilCanceled | TimeInForce.GoodTilCanceled | Order is valid until filled (default) |
Day | TimeInForce.Day | Order is valid until filled or the market closes |
GoodTilDate(DateTime expiry) GoodTilDate(expiry: datetime) | TimeInForce.GoodTilDate(new DateTime(2019, 6, 19, 12, 0, 0)) GoodTilDate(datetime(2019, 6, 19, 12, 0, 0)) | Order is valid until filled or the specified expiration time |
By default, orders remain open until they are canceled (TimeInForce.GoodTilCanceled
).
To update the value, set the DefaultOrderProperties.TimeInForce
before you place an order or pass an orderProperties
argument to the order method.
// Set a Limit Order to be good until market close DefaultOrderProperties.TimeInForce = TimeInForce.Day; LimitOrder("IBM", 100, 120); // Set a Limit Order to be good until noon LimitOrder("IBM", 100, 120, orderProperties: new OrderProperties { TimeInForce = TimeInForce.GoodTilDate(new DateTime(2019, 6, 19, 12, 0, 0)) });
# Set a Limit Order to be good until market close self.DefaultOrderProperties.TimeInForce = TimeInForce.Day self.LimitOrder("IBM", 100, 120) # Set a Limit Order to be good until noon order_properties = OrderProperties() order_properties.TimeInForce = TimeInForce.GoodTilDate(datetime(2019, 6, 19, 12, 0, 0)) self.LimitOrder("IBM", 100, 120, orderProperties=order_properties)
If you trade a market that's open 24 hours a day with daily data, TimeInForce.Day
won't work because the order cancels at the market close time but your algorithm receives daily data at midnight.
Market on open (MOO) and market on close (MOC) orders don't support the GoodTilDate
time in force. If you submit a MOO or MOC order with the GoodTilDate
time in force, LEAN automatically adjusts the time in force to be GoodTilCanceled
.
The brokerage you use may not support all of the TimeInForce
options. To see the options that your brokerage supports, see the Orders section of the brokerage model documentation.
Brokerage-Specific Properties
Some brokerages support additional order properties so you can customize how your orders execute. Some examples include the following order properties:
- Financial Advisor group orders
- An
OutsideRegularTradingHours
property to let orders fill during pre-market and post-market trading hours - A
PostOnly
property to force an order to only add liquidity to a market - A
Hidden
property to make an order not show on the order book - A
ReduceOnly
property to signal the order must only decrease your position size FeeInBase
andFeeInQuote
properties to set which currency you pay fees in for a Crypto trade
To view the order properties your brokerage supports, see the Orders section of the brokerage model documentation.
Tags
You can tag orders to aid your strategy development. Tags can be any string of up to 100 characters. Tags aren't part of the OrderProperties
object, but they are a property of the Order
class you can set. To set an order tag, pass it as an argument when you create the order or use the order update methods.
// Tag an order on creation var ticket = LimitOrder("SPY", 100, 221.05, tag: "Original tag"); // Update the tag with UpdateTag ticket.UpdateTag("Updated tag"); // Update the tag with UpdateOrderFields ticket.Update(new() { Tag = "Another tag" });
# Tag an order on creation ticket = self.LimitOrder("SPY", 100, 221.05, "Original tag") # Update the tag with UpdateTag ticket.UpdateTag("Updated tag") # Update the tag with UpdateOrderFields update_settings = UpdateOrderFields() update_settings.Tag = "Another tag" ticket.Update(update_settings)