Reality Modeling

Key Concepts


Reality models make backtests as realistic as possible to how the strategy would perform in live trading. These reality models model the behavior of things like the portfolio, brokerage, fills, slippage, options, and strategy capacity. Some reality models are set on a security basis and some are set at the portfolio level. The default models assume you trade highly liquid assets. If you trade high volumes or on illiquid assets, you should create custom reality models to be more realistic.

Security Level Models

LEAN's philosophy is to make the models per security as much as possible. The following models are security-level models:

To set a security-level reality model, call the set reality model method on the Security object. To get the correct method, see the preceding documentation page for each type of model.

// Set IBM to have a constant $1 transaction fee 
Securities["IBM"].SetFeeModel(new ConstantFeeModel(1)); 
# Set IBM to have a constant $1 transaction fee

You can also set the security-specific models inside a security initializer.

//In Initialize

private void CustomSecurityInitializer(Security security)
    security.SetFeeModel(new ConstantFeeModel(1));
#In Initialize

def CustomSecurityInitializer(self, security: Security) -> None:

Portfolio Level Models

A portfolio is a semi-closed system where the state updates over time as the value and quantity of assets in the portfolio fluctuate. It's a semi-closed system because the portfolio usually incurs transaction fees and you can add and deposit capital from the portfolio. LEAN models the portfolio holdings to track and update the average price of each asset with every transaction.

At the portfolio level, you can set the margin call model. To set the margin call model, call the SetMarginCallModel method.

Portfolio.SetMarginCallModel(new DefaultMarginCallModel(Portfolio, DefaultOrderProperties));
self.Portfolio.SetMarginCallModel(DefaultMarginCallModel(self.Portfolio, self.DefaultOrderProperties))

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: