Hello,
I tried to produce my very first own ba
cktest with a bsic strategy, the EMA crossover.
I have used a sample code and modified it to fit the requirements I want to backtest.
Unfortunatelly, I'm unable to produce any backtesting data with my code and can't find the problem.
It woumld be great if someone could help.
Many thanks in advance !
MarketWizard
class EMA_CrossOver(QCAlgorithm):
def Initialize(self):
# Set our main strategy parameters
self.SetStartDate(2012,1, 1) # Set Start Date
self.SetEndDate(2015,1,1) # Set End Date
self.SetCash(100000) # Set Strategy Cash
#Set EMA Values
Slow_EMA_Period = 50
Fast_EMA_Period = 20
self.Allocate = 0.50 # Percentage of captital to allocate
self.AddEquity("AAPL", Resolution.Daily)
#build the indicators
self.EMA_Slow = self.EMA("AAPL", Slow_EMA_Period, Resolution.Daily)
self.EMA_Fast = self.EMA("AAPL", Fast_EMA_Period, Resolution.Daily)
#self.SetWarmUp(200)
history = self.History(["AAPL"], 200, Resolution.Daily)
for time, row in history.loc["AAPL"].iterrows():
self.EMA_Slow.Update(time, row["close"])
self.EMA_Fast.Update(time, row["close"])
def OnData(self, data):
if self.IsWarmingUp:
return
if not self.EMA_Slow.IsReady or not self.EMA_Fast.IsReady:
return
if not self.Portfolio.Invested:
if self.EMA_Fast.Current.Value > self.EMA_Fast.Current.Value:
# Buy Apple
self.SetHoldings("AAPL", 1.0)
else:
if self.EMA_Fast.Current.Value < self.EMA_Fast.Current.Value:
# Sell Apple
self.Liquidate("AAPL")
Jasper van Merle
On line 41 and line 45 you need to change one of EMA_Fast to EMA_Slow. At the moment you're doing "if a > a" and "if a < a", which is obviously never true so your SetHoldings and Liquidate calls never get triggered.
MarketWizard
Thank you Jasper van Merle, that helped me a lot !!
MarketWizard
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!