I am having trouble implementing my code. Can someone help me fix the code to make it work. Thank you so much for the help! I would attach the backtest but it is not even backtesting properly, sorry I am not very good at python. I attempted the bootcamp for quantconnect but it is approaching the problem from a different way then I wanted. Please explain if what I am doing is wrong.
- It needs to have stacked EMAs 8, 21, 34, 55 on the 4 hour and 1 hour time frame to accept the trade.
- I also want to plot 2 charts, one for the EMAs to be stacked on the 1 hour and another chart for the 4 hour.
# region imports
from AlgorithmImports import *
# endregion
class AlertFluorescentPinkRhinoceros(QCAlgorithm):
#Establish start date of backtest, cash amount for back testing, and time frames for data
def Initialize(self):
self.SetStartDate(2021, 5, 11) # Set Start Date
self.SetCash(10000) # Set Strategy Cash
self.ticker = "SPY" #Select Ticker
#Stacked EMA lengths
EMA_1 = 8
EMA_2 = 21
EMA_3 = 34
EMA_4 = 55
#Pull timeframe data
self.ticker1H = self.AddEquity(self.ticker, Resolution.Hour).Symbol
self.ticker4H = self.AddEquity(self.ticker, TimeSpan.FromMinutes(240)).Symbol
#self.ticker5M = self.AddEquity(self.ticker, TimeSpan.FromMinutes(5)).Symbol
#Check that the 1Hr 8EMA, 21EMA, 34EMA, and 55EMA are all stacked
self.ema1H_1 = self.EMA(self.ticker1H, EMA_1, Resolution.Hour)
self.ema1H_2 = self.EMA(self.ticker1H, EMA_2, Resolution.Hour)
self.ema1H_3 = self.EMA(self.ticker1H, EMA_3, Resolution.Hour)
self.ema1H_4 = self.EMA(self.ticker1H, EMA_4, Resolution.Hour)
#Check that the 4Hr 8EMA, 21EMA, 34EMA, and 55EMA are all stacked
self.ema4H_1 = self.EMA(self.ticker4H, EMA_1, TimeSpan.FromMinutes(240))
self.ema4H_2 = self.EMA(self.ticker4H, EMA_2, TimeSpan.FromMinutes(240))
self.ema4H_3 = self.EMA(self.ticker4H, EMA_3, TimeSpan.FromMinutes(240))
self.ema4H_4 = self.EMA(self.ticker4H, EMA_4, TimeSpan.FromMinutes(240))
#Pump data prior to start date timing
self.SetWarmUp(EMA_4, Resolution.Hour)
self.SetWarmUp(EMA_4, TImeSpan.FromMinutes(240))
#self.SetWarmUp(EMA_4, TimeSpan.FromMinutes(5))
def OnData(self, data):
#Plot the 1Hr indicators
#self.Plot("EMA_1", "Value" self.ema1H_1.Current.Value)
#self.Plot("EMA_2", "Value" self.ema1H_2.Current.Value)
#self.Plot("EMA_3", "Value" self.ema1H_3.Current.Value)
#self.Plot("EMA_4", "Value" self.ema1H_4.Current.Value)
if self.IsWarmingUp:
return
if ((self.ema1H_1.Current.Value > self.ema1H_2.Current.Value) and \
(self.ema1H_2.Current.Value > self.ema1H_3.Current.Value) and \
(self.ema1H_3.Current.Value > self.ema1H_4.Current.Value)) and \
((self.ema4H_1.Current.Value > self.ema4H_2.Current.Value) and \
(self.ema4H_2.Current.Value > self.ema4H_3.Current.Value) and \
(self.ema4H_3.Current.Value > self.ema4H_4.Current.Value)):
if not self.Portfolio(self.ticker1H).IsLong:
self.SetHoldings(self.ticker1H, 1)
#if not self.Portfolio[self.ticker5M].IsLong:
#self.SetHoldings(self.ticker5M, 1)
else:
self.Liquidate()
Louis Szeto
Hi Jeremy123
It is not possible to subscribe a security in custom timespan, like TimeSpan.FromMinutes(240), nor a good practice to double-subscribe the same ticker. Instead, we can
Follow this docs to see how to do it.
Best
Louis
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.
Jeremy123
thank you. Louis. I will give this a try and let you know if I have difficulties.
Jeremy123
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!