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)

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.

public class OrderPropertiesAlgorithm : QCAlgorithm
{
    private Symbol _spy;
    private decimal _lastPrice = 0m;

    public override void Initialize()
    {
        SetStartDate(2022, 1, 1);
        SetEndDate(2022, 2, 1);
        
        // Add extended market hour SPY data for trading.
        _spy = AddEquity("SPY", extendedMarketHours: true).Symbol;

        // Add a Scheduled Event to trade 2 hours enter a position during pre-market.
        Schedule.On(DateRules.EveryDay(_spy), TimeRules.BeforeMarketOpen(_spy, 120), OpenPosition);
        // Add a Scheduled Event to exit the position 15 minutes after market open.
        Schedule.On(DateRules.EveryDay(_spy), TimeRules.AfterMarketOpen(_spy, 15), () => Liquidate());
        // Add a Scheduled Event to cache the daily close price so you can generate signals the next day.
        Schedule.On(DateRules.EveryDay(_spy), TimeRules.BeforeMarketClose(_spy), GetLastPrice);
        // Add a warm-up period so the algorithm trades on the first day in live mode.
        SetWarmUp(TimeSpan.FromDays(1));
    }

    private void OpenPosition()
    {
        if (_lastPrice > 0m)
        {
            var currentPrice = Securities[_spy].Price;

            // Define the order properties to trade in pre-market. The order will only valid for 3 minutes to avoid 
            // stale fills.
            var orderProperties = new InteractiveBrokersOrderProperties
            {
                TimeInForce = TimeInForce.GoodTilDate(Time.AddMinutes(3)),
                OutsideRegularTradingHours = 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.
            LimitOrder(_spy, currentPrice > _lastPrice ? 10 : -10, currentPrice, orderProperties: orderProperties);
        }
    }

    private void GetLastPrice()
    {
        _lastPrice = Securities[_spy].Price;
    }
}
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.

public class OrderPropertiesAlgorithm : QCAlgorithm
{
    public override void Initialize()
    {
        SetStartDate(2023, 1, 1);
        SetEndDate(2023, 8, 1);

        // Define the order properties to trade with an FA group.
        DefaultOrderProperties = new InteractiveBrokersOrderProperties
        {
            FaGroup = "TestGroupEQ",        // FA group
            FaMethod = "NetLiq",            // Allocation by net liquidation
            Account = "FA123456"            // FA account
        };

        // Add a universe that selects the 10 most liquid US Equities at the start of each week.
        var spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
        UniverseSettings.Schedule.On(DateRules.WeekStart(spy));
        var universe = AddUniverse(Universe.DollarVolume.Top(10));

        // Add a Scheduled Event to rebalance at the start of each week.
        Schedule.On(
            DateRules.WeekStart(spy),
            TimeRules.At(10, 0),
            // Form an equal-weighted portfolio of the universe constituents.
            () => SetHoldings(
                universe.Selected.Select(symbol => new PortfolioTarget(symbol, 0.1m)).ToList(), 
                // Liquidate the assets that aren't in the universe anymore.
                liquidateExistingHoldings: true
            )
        );
    }
}
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
            )
        )

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: