Trading and Orders
Order Properties
Introduction
Order properties enable you to customize how the brokerage executes your orders. The default_order_properties
of your algorithm sets the order properties for all of your orders. To adjust the order properties of an order, you can change the default_order_properties
or pass an order properties object to the order method.
Time In Force
The time_in_force
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 |
---|---|---|
GOOD_TIL_CANCELED | TimeInForce. GOOD_TIL_CANCELED | Order is valid until filled (default) |
DAY | TimeInForce. DAY | Order is valid until filled or the market closes |
good_til_date(expiry: datetime) | time_in_force.good_til_date(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.
GOOD_TIL_CANCELED
).
To update the value, set the default_order_properties.time_in_force
before you place an order or pass an order_properties
argument to the order method.
# Set a Limit Order to be good until market close self.default_order_properties.time_in_force = TimeInForce.DAY self.limit_order("IBM", 100, 120) # Set a Limit Order to be good until noon order_properties = OrderProperties() order_properties.time_in_force = TimeInForce.good_til_date(datetime(2019, 6, 19, 12, 0, 0)) self.limit_order("IBM", 100, 120, order_properties=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 good_til_date
time in force. If you submit a MOO or MOC order with the good_til_date
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
outside_regular_trading_hours
property to let orders fill during pre-market and post-market trading hours - A
post_only
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
reduce_only
property to signal the order must only decrease your position size fee_in_base
andfee_in_quote
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 ticket = self.limit_order("SPY", 100, 221.05, "Original tag") # Update the tag with UpdateTag ticket.update_tag("Updated tag") # Update the tag with UpdateOrderFields update_settings = UpdateOrderFields() update_settings.tag = "Another tag" ticket.update(update_settings)
Examples
The following examples demonstrate some common practices for utilizing order properties.
Example 1: Extended Market Hour Orders
The following algorithm trades buys or sells SPY during pre-market to capture momentum in the overnight gap. It exits the positions at 15 minutes after the market opens. To trade in the extended market hours without stale fills, it set the respective order properties.
class OrderPropertiesAlgorithm(QCAlgorithm): _last_price = None def initialize(self) -> None: self.set_start_date(2022, 1, 1) self.set_end_date(2022, 2, 1) # Add extended market hour SPY data for trading. self._spy = self.add_equity("SPY", extended_market_hours=True).symbol # Add a Scheduled Event to trade 2 hours enter a position during pre-market. self.schedule.on( self.date_rules.every_day(self._spy), self.time_rules.before_market_open(self._spy, 120), self._open_position ) # Add a Scheduled Event to exit the position 15 minutes after market open. self.schedule.on( self.date_rules.every_day(self._spy), self.time_rules.after_market_open(self._spy, 15), self.liquidate ) # Add a Scheduled Event to cache the daily close price so you can generate signals the next day. self.schedule.on( self.date_rules.every_day(self._spy), self.time_rules.before_market_close(self._spy), self._get_last_price ) # Add a warm-up period so the algorithm trades on the first day in live mode. self.set_warm_up(timedelta(1)) def _open_position(self) -> None: if self._last_price: current_price = self.securities[self._spy].price # Define the order properties to trade in pre-market. The order will only valid for 3 minutes to avoid # stale fills. order_properties = OrderProperties() order_properties.time_in_force = TimeInForce.good_til_date(self.time + timedelta(minutes=3)) order_properties.outside_regular_trading_hours = True # Buy if the overnight direction is upward, indicating positive market sentiment. # Sell if the overnight direction is downward, indicating negative market sentiment. # Note that pre-market orders do not accept market order. quantity = 10 if current_price > self._last_price else -10 self.limit_order(self._spy, quantity, current_price, order_properties=order_properties) def _get_last_price(self) -> None: self._last_price = self.securities[self._spy].price
Example 2: Financial Advisor Managing Client Funds
The following algorithm forms an equal-weight portfolio at the start of each week with the 10 most liquid US Equities. It demonstrates how order properties can give financial advisors easy control over client funds.
class OrderPropertiesAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2023, 1, 1) self.set_end_date(2023, 8, 1) # Define the order properties to trade with an FA group. self.default_order_properties = InteractiveBrokersOrderProperties() self.default_order_properties.fa_group = "TestGroupEQ" # FA group self.default_order_properties.fa_method = "EqualQuantity" # Allocation by net liquidation self.default_order_properties.account = "DU123456" # FA account # Add a universe that selects the 10 most liquid US Equities at the start of each week. spy = Symbol.create('SPY', SecurityType.EQUITY, Market.USA) self.universe_settings.schedule.on(self.date_rules.week_start(spy)) universe = self.add_universe(self.universe.dollar_volume.top(10)) # Add a Scheduled Event to rebalance at the start of each week. self.schedule.on( self.date_rules.week_start(spy), self.time_rules.at(10, 0), lambda: self.set_holdings( # Form an equal-weighted portfolio of the universe constituents. [PortfolioTarget(symbol, 0.1) for symbol in universe.selected], # Liquidate the assets that aren't in the universe anymore. liquidate_existing_holdings=True ) )