I am practicing using the QuantConnect Framework. I am using the EMA example that QC has provided for us, and tried applying some of the QC framework to it.
How can I get the EMA for all Forex equities and trade them? I used using another For each loop be it didn't work.
Is there anything I could improve?
class WarmupHistoryAlgorithm(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2018,5,2) #Set Start Date
self.SetEndDate(2020,5,2) #Set End Date
self.SetCash(100000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
forex = self.AddForex("EURUSD", Resolution.Daily)
forex = self.AddForex("NZDUSD", Resolution.Daily)
self.SetAlpha(EMAAlphaModel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
pass
class EMAAlphaModel(AlphaModel):
def __init__(self):
self.fast_period = 20
self.slow_period = 60
self.fast = ExponentialMovingAverage("EURUSD", self.fast_period)
self.slow = ExponentialMovingAverage("EURUSD", self.slow_period)
self.period = timedelta(hours=2)
def Update(self, algorithm, data):
if not self.slow.IsReady:
return
for security in algorithm.ActiveSecurities.Values:
if self.fast.Current.Value > self.slow.Current.Value:
return Insight.Group(
[
Insight.Price(security.Symbol, self.period, InsightDirection.Up)
])
if self.fast.Current.Value < self.slow.Current.Value:
return Insight.Group(
[
Insight.Price(security.Symbol, self.period, InsightDirection.Down)
])
return []
def OnSecuritiesChanged(self, algorithm, changes):
for security in algorithm.ActiveSecurities.Values:
history = algorithm.History([security.Symbol], self.slow_period + 1)
for index, row in history.loc[security.Symbol].iterrows():
self.fast.Update(index, row["close"])
self.slow.Update(index, row["close"])
.