# Reality Modeling

## Key Concepts

### Introduction

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
self.Securities["IBM"].SetFeeModel(ConstantFeeModel(1))

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

// In Initialize
SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices)));

// Outside of the algorithm class
class MySecurityInitializer : BrokerageModelSecurityInitializer
{
public MySecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder)
: base(brokerageModel, securitySeeder) {}

public override void Initialize(Security security)
{
// First, call the superclass definition
// This method sets the reality models of each security using the default reality models of the brokerage model
base.Initialize(security);

// Next, overwrite some of the reality models
security.SetFeeModel(new ConstantFeeModel(1));
}
}
# In Initialize
self.SetSecurityInitializer(MySecurityInitializer(self.BrokerageModel, FuncSecuritySeeder(self.GetLastKnownPrices)))

# Outside of the algorithm class
class MySecurityInitializer(BrokerageModelSecurityInitializer):

def __init__(self, brokerage_model: IBrokerageModel, security_seeder: ISecuritySeeder) -> None:
super().__init__(brokerage_model, security_seeder)

def Initialize(self, security: Security) -> None:
# First, call the superclass definition
# This method sets the reality models of each security using the default reality models of the brokerage model
super().Initialize(security)

# Next, overwrite some of the reality models
security.SetFeeModel(ConstantFeeModel(1))

### 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: