Trying to keep things simple here and coding the trading logic to run base on system time. I am having difficulty saving the open and closing futures as objects to use in trading logic. Code and backtests are below. Any suggestions?
Unable to connect backtest because it is erroring out. Here is the code
import datetime
class ScikitLearnLinearRegressionAlgorithm(QCAlgorithm):
openPeriod = None
closePeriod = None
def Initialize(self):
self.SetTimeZone("America/New_York")
self.SetStartDate(2015, 1, 1)
self.SetEndDate(2021, 12, 31)
self.NQ = self.AddFuture(Futures.Indices.NASDAQ100EMini)
self.NQ.SetFilter(lambda x: x.FrontMonth())
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose(self.NQ, 0), self.ClosePositions)
def OnData(self, data):
now = datetime.datetime.now()
if now.hour == 9 and now.minute == 30:
self.closePeriod = self.Securities[self.NQ].Close
if now.hour == 16 and now.minute == 00:
self.openPeriod = self.Securities[self.NQ].Open
if self.closePeriod is None or self.openPeriod is None:
return
change = (self.closePeriod - self.openPeriod) / self.openPeriod
if now.hour == 9 and now.minute == 31 and self.change >= 0:
self.SetHoldings(self.NQ, 1)
elif now.hour == 9 and now.minute == 31 and self.change < 0:
self.SetHoldings(self.NQ, -1)
def ClosePositions(self):
self.Liquidate()
# for chain in data.FutureChains.Values:
# contracts = chain.Contracts
# for contract in contracts.Values:
# history = self.History(contract.Symbol, 30, Resolution.Minute)
# self.Log(history.to_string())
# self.Quit()
# return
Dario Teodori
Hi James,
self.TimeRules.BeforeMarketClose takes a symbol.
You may replae self.NQ with self.NQ.Symbol
self.TimeRules.BeforeMarketClose(self.NQ, 0) for self.TimeRules.BeforeMarketClose(self.NQ.Symbol, 0)Â
Â
Regards,
Dario
Fred Painchaud
Hi James,
SetHoldings also take a Symbol (or a list of PortfolioTargets):
self.SetHoldings(self.NQ.Symbol, 1)
self.SetHoldings(self.NQ.Symbol, -1)
Fred
James Hawkins
Thank you both, Dario Teodori Fred Painchaud , for your guidance, I have solved for the symbol issue. However, when the futures algorithm runs, nothing happens now. Is it possible there is an issue with my logic tree (?) , which should act as follows:
My thought process here, is the first trading day will not trade because trading occurs only if the two period objects have pricing and the 1600 pricing is always going to be the pricing of the previous day since I am use time-specific trading logic to trade before its object is updated.Â
The final goal here seems simple enough- Â take the Nasdaq100Emini front month contract price at 0930 and compare it to the 1600 price from previous day to determine change and, if change is positive we go long at market open or, if change is negative we go short at market open. And close all positions at market close.Â
Any thoughts on why nothing is trading here?
Dario Teodori
Hi James,
I ran your code and noticed that datetime.datetime.now() returns present time (01/17/2022 not test backtest step time). Use something like self.UtcTime.hour and self.UtcTime.minute instead. You may want to handle the time conversion between UTC and EST
Regards,
Dario
Â
Fred Painchaud
Hi James,
Had the time to look at your algo. Dario was right, plus there were a few other bugs.
See the attached backtest which now trades.
Fred
James Hawkins
Fred Painchaud Dario Teodori I've further modified the code. Interestingly enough, when I switch the openPeriod conditional time from 1600 to 1800, it ceases to work. I suspect QC is treating this algorithm like a standard 930-4pm market, yet, futures trade 24/7, so is there another step needed to ensure the futures pricing is being pulled from the correct CME market and all hours set to trade hours?
Also, what's up with the excessively high fees being generated? I do not recall seeing such high fees in my other algorithms. Have there been any recent changes to QC's algo trading fee structures lately? Thoughts?
Fred Painchaud
Hi James,
Always thought E-mini was not trading between 5:00pm and 6:00pm every day. Also not sure it trades on the weekend, like between Friday night and Sunday night. But I know little on futures.
Also, note that you get bar once they have passed. So a bar at time 9:30, for instance, is the data between 9:29 and 9:30, not 9:30 to 9:31.
Fred
James Hawkins
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!