Trading and Orders

Pre-trade Risk Control

Introduction

LEAN performs pre-order checks to ensure that the order meets some requirements to ensure that the order is valid and can be executed. These checks are include risk controls such as validation of order size and price limits, and whether the order can be executed by a given brokerage at a given time.

Basic Validation

Orders undergo basic validation checks to ensure they meet certain criteria before being submitted. The following list outlines some of the key checks performed:

  1. Security: Ensures that your algorithm has a valid data subscription for the security and receives its market data.
  2. Your algorithm cannot place an order for SPY if it hasn't added it via AddEquity("SPY")self.add_equity("SPY") or universe selection.

  3. Tradable: Ensures that the security is tradable or in the market.
  4. Your algorithm cannot place orders for indices, delisted securities, expired contracts, or any assets removed from your universe. For example, the option filter or universe selection removed a contract because it doesn't meet the criteria set by the filter.

  5. Market Hours: Checks that the order can be placed at the current time.
  6. For example, market orders cannot be placed outside of regular trading hours. LEAN converts market orders to market-on-open orders if possible.

  7. Price: Checks that the last known price is not zero.
  8. LEAN requires the last known price for buying power calculations.

  9. Order Size: Ensures that the order quantity is not zero or less than the lot size.

Order Type

QuantConnect supports several order types. To see the requirements of each order, see the Requirements section of the documentation for the order types. LEAN doesn't allow placing orders that don't meet these requirements. For example, your algorithm cannot place a market-on-close order from cryto-currencies because they operate 24/7.

Buying Power

When you place an order, LEAN uses the buying power model to determine whether the order should be submitted so you avoid placing orders that would be rejected by the brokerage. Buying power calculations can be very complex and depend on many factors, including the brokerage or even the time of day. The main factors include your current portfolio holdings, available cash, and margin requirements.

The buying power calculations are restricted to portfolio your algorithm is connected to. QuantConnect doesn't support multi-account risk controls.

Brokerage

QuantConnect supports several brokerages. Each brokerage trades different asset classes and support different order types. LEAN doesn't allow placing orders that don't meet these requirements. For example, your algorithm cannot place a trailing stop order for most brokerages.

By default, LEAN uses the default brokerage model that suports all order types and all asset classes. To avoid issues with unsupported order types and asset classes when live trading, set the brokerage model to a brokerage that supports them.

SetBrokerageModel(BrokerageName.TradeStation);
self.set_brokerage_model(BrokerageName.TRADE_STATION)

QuantConnect ensures your algorithm is always online, checking its status every few seconds. While a strategy is live it is also always connected to your brokerage, ensuring there is a route to quickly place trades when needed.

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: