Hi,
I wrote a simple HMA strategy that includes a rolling window. In this strategy, I compare the current HMA value with the two-period-before HMA value. Somehow, the rolling window does not work. Can anyone take a look at it and tell me why? TThanks
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from datetime import datetime, timedelta
class HmaAlgorithm(QCAlgorithm):
def Initialize(self):
''' Initialize the data and resolution you require for your strategy '''
self.SetStartDate(2018, 1, 1)
self.SetCash(25000);
self.qqq = self.AddEquity("QQQ", Resolution.Hour)
self.hma = self.HMA("QQQ", 12, Resolution.Hour)
self.hma.Updated += self.HmaUpdated
self.hmaWindow = RollingWindow[IndicatorDataPoint](12)
# Set WarmUp period
self.SetWarmUp(12)
def OnData(self, data):
# Return if no data or if indicator is not ready
if not (data.ContainsKey("QQQ") or self.hma.IsReady): return
currentHma = self.hmaWindow[0]
previousHma = self.hmaWindow[2]
# Retrieve current price
#price = self.Securities["QQQ"].Price
if not self.Portfolio.Invested and currentHma > previousHma:
self.SetHoldings("QQQ",1)
elif not self.Portfolio.Invested and currentHma < previousHma:
self.SetHoldings("QQQ",-1)
# Liquidate
if self.Portfolio.IsLong and currentHma < previousHma:
self.Liquidate()
elif self.Portfolio.IsShort and currentHma > previousHma:
self.Liquidate()
def HmaUpdated(self, sender, updated):
self.hmaWindow.Add(updated)
Vladimir
Ken Lu
I have simplified your algorithm a bit.
Is this what you are trying to achieve?
Vladimir
Just some correction:
if not (self.qqq in data.Bars and self.hma.IsReady and self.delayed_hma.IsReady): return
Ken Lu
Hi Vladimir,
Thank you so much. This is what I was trying to achieve.
Ken Lu
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!