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:
Argument | Data Type | Description | Default Value |
---|---|---|---|
maximumDrawdownPercent maximum_drawdown_percent | decimal float | The maximum percentage drawdown allowed for any single security holding | 0.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:
Argument | Data Type | Description | Default Value |
---|---|---|---|
maximumDrawdownPercent maximum_drawdown_percent | decimal float | Maximum spread accepted comparing to current price in percentage | 0.05 (5%) |
isTrailing is_trailing | bool | If false False , the drawdown is relative to the starting value of the portfolio. If true True , the drawdown is relative the last maximum portfolio value | False false |
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:
Argument | Data Type | Description | Default Value |
---|---|---|---|
maximumUnrealizedProfitPercent maximum_unrealized_profit_percent | decimal float | The maximum percentage unrealized profit allowed for any single security holding | 0.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:
Argument | Data Type | Description | Default Value |
---|---|---|---|
maximumSectorExposure maximum_sector_exposure | decimal float | The maximum exposure for any sector | 0.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:
Argument | Data Type | Description | Default Value |
---|---|---|---|
maximumDrawdownPercent maximum_drawdown_percent | decimal float | The maximum percentage drawdown allowed for algorithm portfolio compared with the highest unrealized profit | 0.05 (5%) |
To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.