class VentralParticlePrism(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2012, 7, 17) # Set Start Date
self.SetEndDate(2020, 7, 17)
self.SetCash(250000000) # Set Strategy Cash
self.AddEquity("QQQ", Resolution.Minute)
self.AddEquity("TQQQ", Resolution.Minute)
self.AddEquity("UVXY", Resolution.Minute)
self.LongMAQQQ = self.SMA("QQQ", 730, Resolution.Daily)
self.ShortMAQQQ = self.SMA("QQQ", 15, Resolution.Daily)
self.SchlongMAQQQ = self.SMA("QQQ", 120, Resolution.Daily)
self.SchortMAQQQ = self.SMA("QQQ", 29, Resolution.Daily)
self.QuotientOne = (self.LongMAQQQ.Current.Value)/(self.ShortMAQQQ.Current.Value)
self.QuotientTwo = (self.SchlongMAQQQ.Current.Value)/(self.SchortMAQQQ.Current.Value)
#Here is my problem.
self.SlopeAngleOne = self.MOMP(self.QuotientOne, 5)
self.SlopeAngleTwo = self.MOMP(self.QuotientTwo, 28)
#How do I calculate this? I want to find the momentum of this indicator over the given period.
self.Schedule.On(
self.DateRules.EveryDay("QQQ"),
self.TimeRules.AfterMarketOpen(120),
self.Derp)
def Derp(self):
if (self.SlopeAngleOne <= -0.001) and (self.SlopeAngleTwo <= 0.00033):
self.SetHoldings("TQQQ", 0.8)
self.SetHoldings("QQQ", 0)
self.SetHoldings("UVXY", 0.08)
elif self.SlopeAngleTwo >= 0.00033:
self.SetHoldings("TQQQ", 0.3)
self.SetHoldings("QQQ", 0.3)
self.SetHoldings("UVXY", 0.15)
else:
self.SetHoldings("TQQQ", 0)
self.SetHoldings("QQQ", 0)
self.SetHoldings("UVXY", 0)
I just want to find the percentage change in the Quotients' values over the selected number of days. Thanks in advance for any help!
Shile Wen
Hi Samural,
For this, we would need to create MomentumPercent indicator objects instead of self.MOMP objects, and manually update this indicator in OnData.Â
Please see the necessary changes in the attached backtest.
Best,
Shile Wen
Shile Wen
Hi Gil,
The reason the times look wrong is the orders are timestamped at midnight, which is why the date-time for an order at the midnight of 2020-07-14 looks like 2020-07-15 00:00:00, as 2020-07-14 24:00:00 is not a real date-time.
Best,
Shile Wen
Samuel Schoening
Hello Shile,
  Thank you very much for your help so far! I implemented your changes but I am still getting an error? I have a similar strategy that is suffering from the same problem that could use a bit of your insight! This is intended to be one of the primary algorithms I use to make trading decisions. Thanks for all your help so far - you are the man!
class VentralParticlePrism(QCAlgorithm): def Initialize(self): self.SetStartDate(2012, 7, 17) # Set Start Date self.SetEndDate(2020, 7, 17) self.SetCash(250000000) # Set Strategy Cash self.AddEquity("QQQ", Resolution.Minute) self.AddEquity("TQQQ", Resolution.Minute) self.AddEquity("UVXY", Resolution.Minute) self.vma = self.SMA("QQQ", 365, Resolution.Daily, Field.Volume) #I need the MOMP of this 'object', self.vma --> The moving average of trading volume for 'QQQ' # over the course of a year. self.vmaSlope = MomentumPercent(14) #Do I need to specify resolution? self.Schedule.On( self.DateRules.EveryDay("QQQ"), self.TimeRules.AfterMarketOpen("QQQ", 5), self.Derp) self.SetWarmup(365) def OnData(self, data): if self.IsWarmingUp: return self.vmaSlope.Update(self.Time, self.vma) #Here is where I am getting the error message: Runtime Error: Trying to dynamically access a method # that does not exist throws a TypeError exception. To # prevent the exception, ensure each parameter type matches # those required by the Update method. Please checkout the # API documentation. # at OnData in main.py:line 24 # TypeError : No method matches given arguments for Update def Derp(self): if not self.vmaSlope.IsReady: return if self.vmaSlope.Current.Value <= -0.1: self.SetHoldings("TQQQ", 0.9) self.SetHoldings("QQQ", 0) self.SetHoldings("UVXY", 0.1) if self.vmaSlope.Current.Value <= -0.01: self.Liquidate("TQQQ")
Â
Samuel Schoening
I would also like to add that the error is specifically found at:
- def OnData(self, data):
- Â Â if self.IsWarmingUp:
- Â Â Â return
- -->self.vmaSlope.Update(self.Time, self.vma)<-
Thanks again, stay healthy!Shile Wen
Hi Samual,
Please replace self.vmaSlope.Update(self.Time, self.vma) on line 25 with self.vmaSlope.Update(self.Time, self.vma.Current.Value). For future reference .Current.Value field is how we access the current value of an indicator.
Best,
Shile Wen
Samuel Schoening
class VentralParticlePrism(QCAlgorithm): def Initialize(self): self.SetStartDate(2000, 7, 22) # Set Start Date self.SetEndDate(2020, 7, 22) self.SetCash(250000000) # Set Strategy Cash self.UniverseSettings.Resolution = Resolution.Daily self.AddEquity("QQQ", Resolution.Minute) self.AddEquity("TQQQ", Resolution.Minute) self.AddEquity("UVXY", Resolution.Minute) self.AddEquity("TLT", Resolution.Minute) self.vma = self.SMA("QQQ", 365, Resolution.Daily, Field.Volume) self.vmaSlope = MomentumPercent(12) self.BuyThreshold = ((self.vma.Current.Value)*(-0.123))*100 self.SellThreshold = ((self.vma.Current.Value)*(-0.1))*100 self.Schedule.On( self.DateRules.EveryDay("QQQ"), self.TimeRules.AfterMarketOpen("QQQ", 7), self.Derp) #Whenever I raise the number of minutes to anything over 10, #it buys and sells once every single trading day throughout #the entire backtest. I don't understand this at all. self.SetWarmup(365) def OnData(self, data): if self.IsWarmingUp: return self.vmaSlope.Update(self.Time, self.vma.Current.Value) def Derp(self): if not self.vmaSlope.IsReady: return if (self.vmaSlope.Current.Value >= self.SellThreshold): self.Liquidate("QQQ") self.SetHoldings("UVXY", 0) self.SetHoldings("TLT", 0.01) if not self.Securities["QQQ"].Invested: if self.vmaSlope.Current.Value <= self.BuyThreshold: self.SetHoldings("TLT", 0) self.SetHoldings("QQQ", 1) self.SetHoldings("UVXY", 0)
I just don't understand how to get the more accurate minute purchase data to run the algorithm as intended. Essentially, the one year moving average of trading volume is determined to be in a downward trend (according to the values at Buy and Sell Threshold). As soon as this is confirmed over the course of a day, I am trying to have QQQ purchased the very next day. For some reason it just continuously buys and sells everyday if I make it trade any time later than 10 minutes after market open. If you are familiar with TradingView (I use it to visulaize a proposed strategy before actually testing it on QuantConnect), the data matches the results of the QC backtest with daily resolution on the ETF's. I just don't understand why making it minute based with such a long moving average would trigger a buy and sell so often every day that I end up doing so much worse. Most importantly, this proposed algorithm did not even trigger during the DotCom crash, which is obviously a result of it tracking volume trends. However, adjusting the ETF's to minute data, it trades frequently and manages to shed most of its value then, and once again in 2008. I don't know if I am missing something simple but let me know what you think when you get a chance! I would appreciate any help from anyone but Shile is clearly the man!
Â
(The scheduling situation is the primary concern I guess)
(I have no idea why it would make ~4000 trades)
Please find the backtest of this algo below followed by a second post with the daily resolution backtest! Thanks for any help as always!
Â
Â
Samuel Schoening
Looking at the list of orders on the daily resolution, I am still shocked to see that 1441 trades were made - but I guess that is potentially accurate? Regardless, TradingView only triggered 124. I don't know if any of this is helpful but I figured it couldn't hurt to provide background information on this project.
Trading View PineScript:
strategy(title="Slope of VMA Strategy", overlay=true)
src = (volume)
len1 = input(365, minval=1, title="Volume Moving Average")
offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
out1 = sma(src, len1)
heat = ((change(out1,(input(12, minval=1, title="ChangeOverXTime"))))/(out1)*100)
BThreshold = input(-0.123, "Buy Threshold")
SThreshold = input(-0.1, "Sell Threshold")
plot(out1, color=color.blue, title="SF", offset=offset)
plot(volume, color=color.yellow, title="Valyum")
plot(strategy.equity)
if (heat <= BThreshold)
  strategy.entry("VMACrossLE", strategy.long, comment="VMACrossLE")
if (heat > SThreshold)
  strategy.exit("VMACrossLX", "VMACrossLE", qty_percent=100, stop=close)
Samuel Schoening
The last one was not the daily resolution backtest, my apologies - here it is:
I am trying to figure out if the consolidation method is applicable here - I want the minute price data at open but the data for the indicator must be updated on a daily basis. The previous day confirms the trade at close - market opens the next day and the trade is placed within the first few hours or minutes.
Shile Wen
Hi Samuel,
This is possible by using consolidation, but it is not necessary. We can check for minute prices during open in OnData, and update our indicator once per day to emulate Daily updates using an On Market Close Scheduled Event (example thread).
Best,
Shile WenÂ
Samuel Schoening
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!