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 customizable 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"].set_fee_model(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.set_security_initializer(MySecurityInitializer(self.brokerage_model, FuncSecuritySeeder(self.get_last_known_prices)))

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

You can set the following portfolio-level models:

  • Margin call model
  • Portfolio.SetMarginCallModel(new DefaultMarginCallModel(Portfolio, DefaultOrderProperties));
    self.portfolio.set_margin_call_model(DefaultMarginCallModel(self.portfolio, self.default_order_properties))
  • Risk free interest rate model
  • SetRiskFreeInterestRateModel(new InterestRateProvider());
    self.set_risk_free_interest_rate_model(InterestRateProvider())

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: