Trading and Orders

Key Concepts

Introduction

LEAN has dozens of methods to create, update, and cancel orders. You can place orders automatically with helper methods or manually through methods on the algorithm API. You can fetch, update, and cancel manual orders with order tickets. As the state of your orders change, LEAN creates events that notify your algorithm.

In backtesting, LEAN simulates order fills with historical data, but you can create your own fill, fee, slippage, and margin models via plugin points. You control how optimistic or pessimistic order fills are with transaction model classes. For more information about these types of models, see Reality Modeling.

Orders and Tickets

When you create an order with one of the order methods, LEAN creates Order and OrderTicket objects. The order ticket is sent to your brokerage. As the brokerage processes your order, it returns another order ticket and it's compared against the order to see if the order is satisfied. Orders are asynchronous in live trading, so if you want to change an order, you must request it with the order ticket. Order changes are not guaranteed since your order may fill by the time brokerage receives the request.

Symbol Properties

The SymbolProperties are a property of the Security object. LEAN uses some of the SymbolProperties to prevent invalid orders and to calculate order quantities for a given target.

The following table describes the properties of the SymbolProperties class:

PropertyData TypeDescription
ContractMultiplierdecimalfloatThe contract multiplier for the security
DescriptionstringThe description of the security
LotSizedecimalfloatThe lot size (lot size of the order) for the security
MarketTickerstringThe market ticker
MinimumOrderSizedecimalfloatThe minimum order size allowed For crypto/forex pairs it's expected to be expressed in base or quote currency i.e For BTC/USD the minimum order size allowed with GDAX is 0.0001 BTC while on Binance the minimum order size allowed is 10 USD
MinimumPriceVariationdecimalfloatThe minimum price variation (tick size) for the security
PriceMagnifierdecimalfloatAllows normalizing live asset prices to US Dollars for Lean consumption. In some exchanges, for some securities, data is expressed in cents like for example for corn futures ('ZC').
QuoteCurrencystringThe quote currency of the security

To get the SymbolProperties, use the property on the Security object.

var symbolProperties = Securities["BTCUSD"].SymbolProperties;
var lotSize = symbolProperties.LotSize;
var minimumOrderSize = symbolProperties.MinimumOrderSize;
var minimumPriceVariation = symbolProperties.MinimumPriceVariation;
symbol_properties = self.Securities["BTCUSD"].SymbolProperties
lot_size = symbol_properties.LotSize
minimum_order_size = symbol_properties.MinimumOrderSize
minimum_price_variation = symbol_properties.MinimumPriceVariation

Trade Models

Reality models make backtests as realistic as possible to how the strategy would perform in live trading.

Live Trading Considerations

In live trading, orders fill asynchronously. We send your order to the API of your brokerage and wait for their response to update the state of your algorithm and portfolio. The timing of live order fills doesn't depend on the resolution of your security subscriptions. When your order fills, the fill price and fee is set by your brokerage. You can add event handlers to your algorithm to monitor the brokerage connection and brokerage messages.

In backtesting, the trade fill timing depends on the resolution of your security subscription. For example, if you subscribe to a security with minute resolution data, the algorithm only receives data in one-minute time slices. As a result, the fill model can only evaluate if the order should fill on a minute-by-minute frequency.

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: