Hello,
I'd like to tweak the Risk Model in my Alpha Model algorithm. The default for the
'MaximumUnrealizedProfitPercentPerSecurity' risk model is 5%, but I'd like to make it 10%. I tried to change it from 5% to 10% by doing this:
self.SetRiskManagement(MaximumUnrealizedProfitPercentPerSecurity(maximumUnrealizedProfitPercent = 0.1))
but that didn't do the trick. How can I get that changed to 10%?
Thanks!
Sean
Adam W
That should work. Can you attach a backtest, or maybe add some Debug/Log messages to the Risk model to see when it's being triggered? Possibly, no securities are hitting above the threshold.
S O'Keeffe
Hi Adam,
Sure here is a backtest! Thanks for responding. Sorry I'm not sure how to add Debug/Log messages to the Risk model, but I'd be happy to if you can point me in the right direction to learn to do that.
Thanks,
Sean
Adam W
The framework models can access the instance of the base algorithm, i.e. algorithm.Log('something') in ManageRisk.
class MaximumUnrealizedProfitPercentPerSecurity(RiskManagementModel): '''Provides an implementation of IRiskManagementModel that limits the unrealized profit per holding to the specified percentage''' def __init__(self, maximumUnrealizedProfitPercent = 0.05): '''Initializes a new instance of the MaximumUnrealizedProfitPercentPerSecurity class Args: maximumUnrealizedProfitPercent: The maximum percentage unrealized profit allowed for any single security holding, defaults to 5% drawdown per security''' self.maximumUnrealizedProfitPercent = abs(maximumUnrealizedProfitPercent) def ManageRisk(self, algorithm, targets): '''Manages the algorithm's risk at each time step Args: algorithm: The algorithm instance targets: The current portfolio targets to be assessed for risk''' targets = [] for kvp in algorithm.Securities: security = kvp.Value if not security.Invested: continue pnl = security.Holdings.UnrealizedProfitPercent if pnl > self.maximumUnrealizedProfitPercent: ### For debugging, add this to see when it is being called algorithm.Log('Risk model triggered') # liquidate targets.append(PortfolioTarget(security.Symbol, 0)) return targets
S O'Keeffe
Hi Adam,
Thanks! I think I added that code correctly, but it looks like it only says "Risk model triggered" in the Log. Maybe I'm not looking in the right place? It looks like the algorithm is selling exactly when VXRT reaches 5% unrealized profit
Thanks,
Sean
Adam W
Oh I just added that to show you how you could use Debug/Logs in a Risk Model. You can actually pass any string as argument, i.e.:
algorithm.Log(f'Risk model triggered for {kvp.Key.Value} with unrealized PnL {pnl*100:.2f}%.')
to more clearly see what's happening. For more info, see this.
With that modification, the logs show:
2020-07-14 00:00:00 Launching analysis for 20cc2eb2730d07c53c9d45784873b379 with LEAN Engine v2.4.0.0.8789 2020-07-14 09:33:52 Risk model triggered for VXRT at unrealized PnL 10%. Liquidating... 2020-07-14 09:38:15 Risk model triggered for VXRT at unrealized PnL 11%. Liquidating... 2020-07-16 00:00:00 Algorithm Id:(20cc2eb2730d07c53c9d45784873b379) completed in 161.49 seconds at 58k data points per second. Processing total of 9,368,323 data points.
so it seems that the risk model is working as intended. If the algorithm's behavior isn't what you expected, keep in mind that any active insights even after liquidation can cause re-orders, see this recent discussion here.
Not quite sure I fully understand the strategy's logic, but if you can describe the specific behavior you are expecting from the risk model I can take a look later.
S O'Keeffe
Oh shoot sorry it did work! Thank you so much! I don't know which backtest I was looking at when I thought it was still executing at 5% unrealized profit. I have a few more pieces to add in to finish the strategy of a kind of short squeeze idea.
S O'Keeffe
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!