Is it possible to have an effect trigger the BracketRiskModel defined in the SetRiskManagement? See the below algo example I typed up really quick. Theres a couple indicators. The algo enters a trade when price closes above the 20 sma. I want to trigger the SetRiskManagement model on that 2nd block when the SMA20 crosses the 50
from AlgorithmImports import *
#endregion
from QuantConnect.Data.Market import TradeBar
from risk import BracketRiskModel
class RollingWindowAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022,1,1) #Set Start Date
#self.SetEndDate(2021,10,1)
self.Tickers = ('SPY','QQQ')
self.SetCash(100000)
for symbol in self.tickers:
self.AddEquity(symbol, Resolution.Hour).Symbol
sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
symbolData = SymbolData(symbol, sma20, sma50)
self.symbolDataBySymbol[symbol] = symbolData
self.up_value = 0.1
self.down_value = 0.1
self.SetRiskManagement(BracketRiskModel(self.down_value, self.up_value))
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 10), self.TradeSignals)
def OnData(self, data):
pass
def TradeSignals(self):
if self.trade == False:
return
for symbol, symbolData in self.symbolDataBySymbol.items():
if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma20.Current.Value):
self.SetHoldings(symbol, .1, False, "Buy Signal")
for symbol, symbolData in self.symbolDataBySymbol.items():
if self.Portfolio[symbol].Invested and (symbolData.sma20.Current.Value > symbolData.sma50.Current.Value):
#self.SetRiskManagement(BracketRiskModel(self.down_value, self.up_value)) to take effect now
class SymbolData:
def __init__(self, symbol, sma20,sma50):
self.Symbol = symbol
self.sma20 = sma20
self.sma50 = sma50
The risk.py file that is referenced is isnt important to the question, could just be the standard risk model. Something like:
self.AddRiskManagement(TrailingStopRiskManagementModel(0.04))
Derek Melchin
Hi Axist,
When using a risk management model, the risk logic usually goes in the ManageRisk method of the framework module. LEAN automatically calls this method, so we don't need to manually call it from the algorithm class.
However, to call a method of the risk management model, save a reference to the model in the initialize method.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Axist
Thanks for this, that makes sense. What if I wanted to exempt securities or invested holdings from the self.SetRiskManagement(self.risk_model) ?
Say for example something like this in the following trailing stop risk management model which is being called from in the main.py file. This following code will error because the ‘Securities’ object is not recongized despite the fact that there's a reference to the “from main import *” file at the top.
Axist
Actually found the answer I was looking for above here, nevermind!
Had to do with how to refer to self.securities like value when dealing with:
Axist
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!