How could I improve this basic code?

Back

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"])

Update Backtest








 
0

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.


Hi Evoke,

One thing I noticed is that the EMA indicators aren't being updated past the initial warmup, so this can be something to be address in MOMAlphaModel's OnData.

Furthermore, I noticed that the Alpha Model is implemented for only EURUSD, but the NZDUSD could mess up the calculations, so for an Alpha with multiple symbols I suggest using the dictionary + symbol data pattern, which can be seen in this BootCamp.

Best,
Derek Melchin

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed