Hi all,

I'm attempting to develop a risk management module that sets a static stop loss based on the ATR of the security at its purchase date. I've attempted to put it together something like this, but is currently returning errors:

class ATRRiskManagementModel(RiskManagementModel):
   '''Provides an implementation of IRiskManagementModel that limits drawdown based upon 2 x 20 Day ATR'''

   def __init__(self, atrPeriod = 20):
       '''Initializes a new instance of the ATRRiskManagementModel class
       Args:
           ATRPeriod: 20'''
       self.atrPeriod = atrPeriod

   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 = []
       
       # check if security is long/short
       # get the value of the security
       # subtract/add 2*ATR 
       # append targets/liquidate
       # dont forget history request for the indicator
       
       for kvp in algorithm.Securities:
           self.symbolData[added.Symbol] = SymbolData(algorithm, kvp, self.atrPeriod)

           if not security.Invested:
               continue
           
           if algorithm.Securities.IsLong:
               lossLevel = security.Price - (2*self.atr.Current.Value)
               if lossLevel == security.Price:
                   # liquidate
                   targets.append(PortfolioTarget(security.Symbol, 0))
                   
           if algorithm.Securities.IsShort:
               lossLevel = security.Price + (2*self.atr.Current.Value)
               if lossLevel == security.Price:
                   # liquidate
                   targets.append(PortfolioTarget(security.Symbol, 0))

       return targets

class SymbolData:
   def __init__(self, algorithm, security, atrPeriod):
       self.Security = security
       self.atr = AverageTrueRange(atrPeriod, MovingAverageType.Simple)
       
       history = algorithm.History(security.Symbol, 20, Resolution.Daily)
       for bar in history.itertuples():
           tradeBar = TradeBar(bar.Index[1], bar.Index[0], bar.open, bar.high, bar.low, bar.close, bar.volume, timedelta(1))
           self.momentum.Update(bar.Index[1], bar.close)

Is anyone familiar with setting stop losses according to indicator values? Some suggestions would be greatly appreciated.

Regards,

Nick

Author