The below snippet of code is giving me the following error:
Runtime Error: 'Equity' object has no attribute 'symbol'
at ManageRisk
if not security.Invested and security.symbol['SPY']:
…
I know its because I am not exempting the SPY properly within a kvp list. How would I go about exempting specific equities from this sample risk management model?
The trailing Stop code:
#region imports
from AlgorithmImports import *
from main import *
#endregion
from QuantConnect.Algorithm.Framework.Risk import RiskManagementModel
# bracket risk model class
class BracketRiskModel(RiskManagementModel):
'''Creates a trailing stop loss for the maximumDrawdownPercent value and a profit taker for the maximumUnrealizedProfitPercent value'''
def __init__(self,maximumDrawdownPercent = 0.1, maximumUnrealizedProfitPercent = 0.1):
#it was maximumDrawdownPercent maximumUnrealizedProfitPercent .05
self.maximumDrawdownPercent = -abs(maximumDrawdownPercent)
self.trailingHighs = dict()
self.maximumUnrealizedProfitPercent = abs(maximumUnrealizedProfitPercent)
def ManageRisk(self, algorithm, targets):
riskAdjustedTargets = list()
for kvp in algorithm.Securities:
symbol = kvp.Key
security = kvp.Value
# Remove if not invested
#
#THIS IS WHERE ITS ERRORING. What is the correct way to have this ignore a specific security, in this case the SPY?
#
if not security.Invested and security.symbol['SPY']:
self.trailingHighs.pop(symbol, None)
continue
pnl = security.Holdings.UnrealizedProfitPercent
if pnl > self.maximumUnrealizedProfitPercent:
# liquidate
algorithm.Debug(f"Profit Taken: {security.Symbol}")
algorithm.Log(f"Profit Taken: {security.Symbol}")
riskAdjustedTargets.append(PortfolioTarget(security.Symbol, 0))
return riskAdjustedTargets
# Add newly invested securities
if symbol not in self.trailingHighs:
self.trailingHighs[symbol] = security.Holdings.AveragePrice # Set to average holding cost
continue
# Check for new highs and update - set to tradebar high
if self.trailingHighs[symbol] < security.High:
self.trailingHighs[symbol] = security.High
continue
# Check for securities past the drawdown limit
securityHigh = self.trailingHighs[symbol]
drawdown = (security.Low / securityHigh) - 1
if drawdown < self.maximumDrawdownPercent:
# liquidate
algorithm.Debug(f"Losses Taken: {security.Symbol}")
algorithm.Log(f"Losses Taken: {security.Symbol}")
riskAdjustedTargets.append(PortfolioTarget(symbol, 0))
return riskAdjustedTargets
Lucas
HI Axist
I think it is because that you write:
and not
If that does not work, please attach the whole code and a backtest, if that is possible
Have a good day
Lucas
Axist
I had tried that but was getting:
Runtime Error: 'Symbol' object is not subscriptable
at ManageRisk
if not security.Invested and security.Symbol['SPY']
Clearly that must be the right way, but I am not trying to give it a list of tickers here. If its necessary, I'll provide a subset of the main algorithm, but I feel like to exempt equities from the RiskManagementModel would occur within this file?
Nico Xenox
Hey Axist,
you could try to debug the code and stop at line 30. then check the values of securit/symbol/securit.symbol.
for example:
If security.Invested works then probably you could try security.Value[‘SPY’]
Axist
all three just stated the error that those values didnt exist.
If I cant exempt tickers from a RiskManagementModel, maybe I can do this the opposite way? Take the logic out of the TrailingStop.py file and put it directly into the algorithm?
Nico Xenox
Hey Axist,
do you get values for the “for kvp in algorithm.Securities”? If not then you could try that out what you mentioned
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!