Hi, I want to implement a risk management function but it seems with ou without my algo is returning the same stats.So I would like to know what doesn't work in my implementation.
Thank You
class MaximumDrawdownPercentPortfolio(RiskManagementModel):
def __init__(self, maximumDrawdownPercent = 0.15, isTrailing = False):
self.maximumDrawdownPercent = -abs(maximumDrawdownPercent)
self.isTrailing = isTrailing
self.initialised = False
self.portfolioHigh = 0;
def ManageRisk(self, algorithm, targets):
currentValue = algorithm.Portfolio.TotalPortfolioValue
if not self.initialised:
self.portfolioHigh = currentValue # Set initial portfolio value
self.initialised = True
# Update trailing high value if in trailing mode
if self.isTrailing and self.portfolioHigh < currentValue:
self.portfolioHigh = currentValue
return [] # return if new high reached
pnl = self.GetTotalDrawdownPercent(currentValue)
if pnl < self.maximumDrawdownPercent:
return self.Liquidate()
return []
def GetTotalDrawdownPercent(self, currentValue):
return (float(currentValue) / float(self.portfolioHigh)) - 1.0
Rahul Chowdhury
Hey Wawes,
This is MaximumDrawdownPercentPortfolio. First, there isn't any Liquidate method within the MaximumDrawdownPercentPortfolio class, you would need to reference algorithm.Liquidate(). Also, ManageRisk needs to return a list of PortfolioTargets, so you can't return Liquidate(). Regardless, Liquidate() shouldn't be called in the RiskManagementModel, Liquidate() is an execution concern, not a risk concern.
Instead on line 26, you should return
return [ PortfolioTarget(target.Symbol, 0) for target in targets ]
Elyes Mahjoubi
Wawes23
Thank you and sorry for this error but when I assign my risk management function to a low drawdown percentage (0.02 ) i make deeper drawdown than with using a drawdown level of 0.05.
I have another question : why when assigning a level of maximum drawdown with this function it's never respected. Because for a maximum assigned drawdown of 5% my portfolio does a 18% drawdown.
Thank you
Rahul Chowdhury
Hi Wawes,
Depending on your resolution and portfolio volatility, your portfolio value may move a large amount in the time period between risk rebalancing. For example, if you are using daily resolution, the risk management model manages risk once a day. During the time in between risk rebalancing, it is possible for your portfolio value to move by more than the maximum drawdown.
Could you attach a backtest with this risk management model that provides a repeatable case? It will help us greatly in deciphering what exactly is happening.
Wawes23
Hi,
I'm doing a drawdown of 31.200% but i want it to not get over 20% and it seems that with or without the function I'am doing the same results.
Thank you for your help
Rahul Chowdhury
Hi Wawes,
To implement a risk management model you need to use
self.SetRiskManagement(MaximumDrawdownPercentPortfolio())
This sets the risk management module of the framework to the MaximumDrawdownPercentPortfolio.
However, since you are using a classic algorithm, it isn't good practice to implement framework modules. If you want to use a risk management model, you should implement a framework algorithm(https://www.quantconnect.com/docs/algorithm-framework/overview), which uses an AlphaModel to emit insights.I see that you completed the Algorithm Framework Bootcamp(https://www.quantconnect.com/terminal/#lesson-272/The-Algorithm-Framework), but I encourage you to review it. It will refresh your memory on how the framework works.
When using a classic algorithm, it is best to implement the risk management yourself. For example, in OnData we can check if our portfolio PNL is less than the maximum drawdown; if it is, we can liquidate our portfolio.
Since your algorithm rebalances every day, this method of risk management doesn't dramatically affect your algorithm's performance.
Wawes23
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!