Hello, I'm trying to come up with a drawdown protection per security. I tried the maxDrawdown per security function but it doesn't work unless the symbol is a part of Morningstar database.
How do I go about adding, say 1% trailing stop per security to the code below? Thank you.
import pandas as pd
from datetime import datetime
class SectorMomentumAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 1, 1)
self.SetEndDate(datetime.now())
self.SetCash(10000)
# create a dictionary to store momentum indicators for all symbols
self.data = {}
period = 3*21
# choose ten sector ETFs
self.symbols = ["SPY",
"QQQ",
"VXX",
"TLT",
"VXX",
"TSLA",
"AAPL",
"XLF",
"VNQ",
"WCLD"]
# warm up the MOM indicator
self.SetWarmUp(period)
for symbol in self.symbols:
self.AddEquity(symbol, Resolution.Daily)
self.data[symbol] = self.MOM(symbol, period, Resolution.Daily)
# shcedule the function to fire at the month start
self.Schedule.On(self.DateRules.WeekStart("XLK"), self.TimeRules.AfterMarketOpen("XLK"), self.Rebalance)
def OnData(self, data):
pass
def Rebalance(self):
if self.IsWarmingUp: return
top3 = pd.Series(self.data).sort_values(ascending = False)[:3]
for kvp in self.Portfolio:
security_hold = kvp.Value
if security_hold.Type != SecurityType.Equity:
continue
# liquidate the security which is no longer in the top3 momentum list
if security_hold.Invested and (security_hold.Symbol.Value not in top3.index):
self.Liquidate(security_hold.Symbol)
for symbol in top3.index:
self.SetHoldings(symbol, 1/len(top3))