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:

  1. #region imports
  2. from AlgorithmImports import *
  3. from main import *
  4. #endregion
  5. from QuantConnect.Algorithm.Framework.Risk import RiskManagementModel
  6. # bracket risk model class
  7. class BracketRiskModel(RiskManagementModel):
  8. '''Creates a trailing stop loss for the maximumDrawdownPercent value and a profit taker for the maximumUnrealizedProfitPercent value'''
  9. def __init__(self,maximumDrawdownPercent = 0.1, maximumUnrealizedProfitPercent = 0.1):
  10. #it was maximumDrawdownPercent maximumUnrealizedProfitPercent .05
  11. self.maximumDrawdownPercent = -abs(maximumDrawdownPercent)
  12. self.trailingHighs = dict()
  13. self.maximumUnrealizedProfitPercent = abs(maximumUnrealizedProfitPercent)
  14. def ManageRisk(self, algorithm, targets):
  15. riskAdjustedTargets = list()
  16. for kvp in algorithm.Securities:
  17. symbol = kvp.Key
  18. security = kvp.Value
  19. # Remove if not invested
  20. #
  21. #THIS IS WHERE ITS ERRORING. What is the correct way to have this ignore a specific security, in this case the SPY?
  22. #
  23. if not security.Invested and security.symbol['SPY']:
  24. self.trailingHighs.pop(symbol, None)
  25. continue
  26. pnl = security.Holdings.UnrealizedProfitPercent
  27. if pnl > self.maximumUnrealizedProfitPercent:
  28. # liquidate
  29. algorithm.Debug(f"Profit Taken: {security.Symbol}")
  30. algorithm.Log(f"Profit Taken: {security.Symbol}")
  31. riskAdjustedTargets.append(PortfolioTarget(security.Symbol, 0))
  32. return riskAdjustedTargets
  33. # Add newly invested securities
  34. if symbol not in self.trailingHighs:
  35. self.trailingHighs[symbol] = security.Holdings.AveragePrice # Set to average holding cost
  36. continue
  37. # Check for new highs and update - set to tradebar high
  38. if self.trailingHighs[symbol] < security.High:
  39. self.trailingHighs[symbol] = security.High
  40. continue
  41. # Check for securities past the drawdown limit
  42. securityHigh = self.trailingHighs[symbol]
  43. drawdown = (security.Low / securityHigh) - 1
  44. if drawdown < self.maximumDrawdownPercent:
  45. # liquidate
  46. algorithm.Debug(f"Losses Taken: {security.Symbol}")
  47. algorithm.Log(f"Losses Taken: {security.Symbol}")
  48. riskAdjustedTargets.append(PortfolioTarget(symbol, 0))
  49. return riskAdjustedTargets
+ Expand

Author

Axist

November 2022