book
Checkout our new book! Hands on AI Trading with Python, QuantConnect, and AWS Learn More arrow

Execution

Supported Models

Introduction

This page describes the pre-built Execution models in LEAN. The number of models grows over time. To add a model to LEAN, make a pull request to the GitHub repository. If none of these models perform exactly how you want, create a custom Execution model.

Null Model

The NullExecutionModel doesn't place any trades.

Select Language:
# Set the execution model to NullExecutionModel, which disables execution functionality for debugging or when the execution logic is not needed for the current strategy.
self.set_execution(NullExecutionModel())

To view the implementation of this model, see the LEAN GitHub repository.

Immediate Model

The ImmediateExecutionModel is the default Execution model. It uses market orders to immediately fill portfolio targets. It's similar to placing market orders in line with your algorithm logic.

Select Language:
# Use ImmediateExecutionModel for executing trades instantly at market price to minimize execution delay. This may incur extra slippage.
self.set_execution(ImmediateExecutionModel())

To view the implementation of this model, see the LEAN GitHub repository.

Spread Model

The SpreadExecutionModel executes trades with market orders if the exchange is open and the bid-ask spread is within a threshold percentage. The model only works if the security subscription provides QuoteBar data. The spread percentage is calculated as

abp

where a is the ask price, b is the bid price, and p is the price.

Select Language:
# Use SpreadExecutionModel to manage trade execution based on bid-ask spreads, aiming to optimize trade execution costs and reduce slippage.
self.set_execution(SpreadExecutionModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
accepting_spread_percentfloatMaximum spread accepted comparing to current price in percentage0.005 (0.5%)

To view the implementation of this model, see the LEAN GitHub repository.

Standard Deviation Model

The StandardDeviationExecutionModel seeks to fill orders when the price is more than 2 standard deviations lower than the average price over a trailing period. The intent is to find dips in the market to place trades. In strongly trending markets, this procedure can result in delayed trade placement since it might be a while before the next price spike or dip.

Select Language:
# Use StandardDeviationExecutionModel to execute trades based on volatility measures, aiming to adapt trade sizes and timing according to market volatility.
self.set_execution(StandardDeviationExecutionModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
periodfloatPeriod of the standard deviation indicator60
deviationsintThe number of deviations away from the mean before submitting an order2
resolutionResolutionThe resolution of the STD and SMA indicatorsResolution.MINUTE

To view the implementation of this model, see the LEAN GitHub repository.

VWAP Model

The VolumeWeightedAveragePriceExecutionModel works to fill your orders at or better than the volume-weighted average price (VWAP) for the trading day. This is a best-effort algorithm, so no guarantee can be made that it will reach the VWAP.

VWAP execution model implementation
VWAP Execution Model Fill Placements

The model uses market orders and it only works if the security subscription provides intraday data. It sets a maximum order size of 1% of the current bar's volume, but you can update the maximum order size through the maximum_order_quantity_percent_volume member.

Select Language:
# Use VolumeWeightedAveragePriceExecutionModel to execute trades based on volume-weighted average price, aiming to minimize market impact and achieve better execution prices.
self.set_execution(VolumeWeightedAveragePriceExecutionModel())

To view the implementation of this model, see the LEAN GitHub repository.

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: