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))
Shile Wen
Hi John,
I suggest going through the Trailing Stop BootCamp on how to do this.
Best,
Shile Wen
Ryan Riordon
Hello John.
Maybe replace self.SetHoldings(symbol, 1/len(top3)) with:
price = self.Securities[symbol].Price
self.StopMarketOrder(symbol, self.cashused/price, price*0.8) wher you set self.cashused in initialized
or
self.StopMarketOrder(symbol, self.Portfolio.TotalPortfolioValue/price, price*0.8).
John Ener
Unfortunately it didn't work self.cashused not defined
Ryan Riordon
John,
When it says "somthing" not defined it means that the word or input is not defined or lacks info. In this case this tells me you probably forgot to put self.cashused = "put any amount here eg. 10000" in initialize.
John Ener
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!