Risk Management

Supported Models

Introduction

This page describes the pre-built Risk Management 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 Risk Management model.

Null Model

The NullRiskManagementModel is the default Risk Management model. It doesn't adjust any of the PortfolioTarget objects it receives from the Portfolio Construction model.

// Adding the null risk management model doesn't affect the portfolio targets.
AddRiskManagement(new NullRiskManagementModel());
# Adding the null risk management model doesn't affect the portfolio targets.
self.add_risk_management(NullRiskManagementModel())

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

Maximum Security Drawdown Model

The MaximumDrawdownPercentPerSecurity model monitors the unrealized profit percentage of each security in the portfolio. When the percentage drops below a threshold relative to the opening price, it liquidates the position and cancels all insights in the Insight Manager that are for the security. This model can operate even when the Portfolio Construction model provides an empty list of PortfolioTarget objects.

// Add risk management model to limit maximum drawdown per security, reducing position size to prevent further losses.
AddRiskManagement(new MaximumDrawdownPercentPerSecurity());
# Add risk management model to limit maximum drawdown per security, reducing position size to prevent further losses.
self.add_risk_management(MaximumDrawdownPercentPerSecurity())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
maximumDrawdownPercentmaximum_drawdown_percentdecimalfloatThe maximum percentage drawdown allowed for any single security holding0.05 (5%)

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

Maximum Portfolio Drawdown Model

The MaximumDrawdownPercentPortfolio model monitors the portfolio drawdown. The drawdown can be relative to the starting portfolio value or the maximum portfolio value. When the portfolio value drops below a percentage threshold, the model liquidates the portfolio and cancels all insights in the Insight Manager. To liquidate the portfolio, the model must receive at least 1 PortfolioTarget after the drawdown threshold is passed. After the portfolio is liquidated, the model resets. This model can operate even when the Portfolio Construction model provides an empty list of PortfolioTarget objects.

// Add risk management model to limit maximum drawdown for the entire portfolio, protecting overall portfolio value from significant losses.
AddRiskManagement(new MaximumDrawdownPercentPortfolio());
# Add risk management model to limit maximum drawdown for the entire portfolio, protecting overall portfolio value from significant losses.
self.add_risk_management(MaximumDrawdownPercentPortfolio())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
maximumDrawdownPercentmaximum_drawdown_percentdecimalfloatMaximum spread accepted comparing to current price in percentage0.05 (5%)
isTrailingis_trailingboolIf falseFalse, the drawdown is relative to the starting value of the portfolio. If trueTrue, the drawdown is relative the last maximum portfolio valueFalsefalse

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

Maximum Unrealized Profit Model

The MaximumUnrealizedProfitPercentPerSecurity model monitors the unrealized profit of each security in the portfolio. When the unrealized profit exceeds a profit threshold, it liquidates the position and cancels all insights in the Insight Manager that are for the security. This model can operate even when the Portfolio Construction model provides an empty list of PortfolioTarget objects.

// Add risk management model to secure gains by limiting the maximum unrealized profit for each security, locking in profits before potential reversals.
AddRiskManagement(new MaximumUnrealizedProfitPercentPerSecurity());
# Add risk management model to secure gains by limiting the maximum unrealized profit for each security, locking in profits before potential reversals.
self.add_risk_management(MaximumUnrealizedProfitPercentPerSecurity())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
maximumUnrealizedProfitPercentmaximum_unrealized_profit_percentdecimalfloatThe maximum percentage unrealized profit allowed for any single security holding0.05 (5%)

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

Maximum Sector Exposure Model

The MaximumSectorExposureRiskManagementModel limits the absolute portfolio exposure in a each industry sector to a predefined maximum percentage. If the absolute portfolio exposure exceeds the maximum percentage, the weight of each Equity in the sector is scaled down so the sector doesn't exceed the maximum percentage. This process requires assets that are selected by Morningstar fundamental data. This model can operate even when the Portfolio Construction model provides an empty list of PortfolioTarget objects.

// Add risk management model to limit exposure to any single sector, ensuring diversification and reducing sector-specific risk.
AddRiskManagement(new MaximumSectorExposureRiskManagementModel());
# Add risk management model to limit exposure to any single sector, ensuring diversification and reducing sector-specific risk.
self.add_risk_management(MaximumSectorExposureRiskManagementModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
maximumSectorExposuremaximum_sector_exposuredecimalfloatThe maximum exposure for any sector0.2 (20%)

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

Trailing Stop Model

The TrailingStopRiskManagementModel monitors the drawdown of each security in the portfolio. When the peak-to-trough drawdown of the unrealized profit exceeds a threshold, it liquidates the position and cancels all insights in the Insight Manager that are for the security. This model can operate even when the Portfolio Construction model provides an empty list of PortfolioTarget objects.

// Add risk management model to implement trailing stops, which adjusts stop-loss levels as the price moves favorably, helping to lock in gains and limit losses.
AddRiskManagement(new TrailingStopRiskManagementModel());
# Add risk management model to implement trailing stops, which adjusts stop-loss levels as the price moves favorably, helping to lock in gains and limit losses.
self.add_risk_management(TrailingStopRiskManagementModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
maximumDrawdownPercentmaximum_drawdown_percentdecimalfloatThe maximum percentage drawdown allowed for algorithm portfolio compared with the highest unrealized profit0.05 (5%)

To view the implementation of this model, see the LEAN GitHub repositoryLEAN 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: