Trading and Orders

Order Properties

Introduction

Order properties enable you to customize how the brokerage executes your orders. The DefaultOrderPropertiesdefault_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 DefaultOrderPropertiesdefault_order_properties or pass an order properties object to the order method.

Time In Force

The TimeInForcetime_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:

MemberExampleDescription
GoodTilCanceledGOOD_TIL_CANCELEDTimeInForce.GoodTilCanceledGOOD_TIL_CANCELEDOrder is valid until filled (default)
DayDAYTimeInForce.DayDAYOrder is valid until filled or the market closes
GoodTilDate(DateTime expiry)good_til_date(expiry: datetime)TimeInForce.GoodTilDate(new DateTime(2019, 6, 19, 12, 0, 0))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.GoodTilCanceledGOOD_TIL_CANCELED). To update the value, set the DefaultOrderProperties.TimeInForcedefault_order_properties.time_in_force before you place an order or pass an orderPropertiesorder_properties 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.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.DayTimeInForce.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 GoodTilDategood_til_date time in force. If you submit a MOO or MOC order with the GoodTilDategood_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 OutsideRegularTradingHoursoutside_regular_trading_hours property to let orders fill during pre-market and post-market trading hours
  • A PostOnlypost_only property to force an order to only add liquidity to a market
  • A Hiddenhidden property to make an order not show on the order book
  • A ReduceOnlyreduce_only property to signal the order must only decrease your position size
  • FeeInBasefee_in_base and FeeInQuotefee_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
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.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)

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: