I'm working on a new algorithm that starts by getting the past N days worth of opening quotes, including today. I can get the historical data just fine, but I'm struggling to figure out how to get the current day's opening price. In the below code, which is called right after warmup finishes, the current price/opening price are 0.0. The backtest starts at 10am, so if I add an event to run at market open, the event doesn't fire.
The below code set to run from Oct 3 to 4, 2022 produces this output:
2022-10-03 11:00:00 :time 2022-09-15 392.905585 2022-09-16 391.282255 2022-09-17 384.180000 2022-09-20 382.270000 2022-09-21 385.100000 2022-09-22 386.060000 2022-09-23 376.500000 2022-09-24 370.580000 2022-09-27 366.410000 2022-09-28 368.010000 2022-09-29 364.440000 2022-09-30 366.830000 2022-10-01 361.870000
2022-10-03 11:00:00 :2022-10-03 10:00:00 SPY
2022-10-03 11:00:00 :2022-10-03 10:00:00 open: 0.0
2022-10-03 11:00:00 :2022-10-03 10:00:00 price now: 0.0
2022-10-04 09:31:00 :2022-10-04 09:30:00 print price
2022-10-04 09:31:00 :2022-10-04 09:30:00 spy open is : 366.38
Would appreciate any advice--I'm not seeing a good answer to this on the forum. Thanks!
def _BuildHistory(self, sym):
# get the history for the time period. Remember if we print this,
# it will be off by 1 day as the time is the END of the period (so actual
# day is 1 before date printed with it)
try:
historyDF = self.History([sym], self.NUM_DAYS+1, Resolution.Daily)
except:
self.Debug('Failed to load history for '+sym)
return
try:
equityHistory = historyDF.loc[sym]
except:
self.Debug('No history located for '+sym)
return
try:
openPrices = equityHistory['open']
except:
self.Debug('Failed to get open history for '+sym)
return
self.Log(openPrices)
self.Log(str(self.Time) +' '+str(self.Securities[sym]))
self.Log(str(self.Time) +' '+'open: '+str(self.Securities[sym].Open))
self.Log(str(self.Time) +' '+'price now: '+str(self.Securities[sym].Price))
def _PrintPrice(self):
self.Log(str(self.Time) +' '+'print price')
self.Log(str(self.Time) +' '+'spy open is : '+str(self.Securities['SPY'].Open))
def _Initialize(self):
self.AddEquity("SPY", Resolution.Minute)
self._BuildHistory('SPY')
if self.Securities['SPY'].Open < 0.001:
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen('SPY', 0), self._PrintPrice)
else:
self.Log(str(self.Time) +' '+'spy price: '+str(self.Securites['SPY'].Open))
return
Derek Melchin
Hi Josh,
The code above prints 0 for the first opening price because the algorithm hasn't received any data for SPY yet. The first minute-bar of the day that spans from 9:30-9:31 AM isn't available until 9:31 AM. To make _PrintPrice print the opening price of the first minute-bar, set the Scheduled Event to fire 1 minute after the market open (9:31 AM).
If we trade illiquid securities, there is no guarantee that there will be trades during the first minute. In these cases, we can just get the first bar of each day in OnData instead of using a Scheduled Event.
See the attached backtest for reference.
Best,
Derek Melchin
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.
Josh Anon
Thanks, but I don't think that's quite right:
Also I'd like to figure out how to get the current day's opening price without having to schedule an event to run at market open so that if I start the strategy at 11am, it will still get the current day's opening for its initialization calculations. Thanks!
Derek Melchin
Hi Josh,
Please attach a full algorithm so we can reproduce the logs above.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Josh Anon
Here you go, and here's the output from the log. Note how _PrintPrice is never called on day 1.
Derek Melchin
Hi Josh,
The warm-up feature uses the subscriptions we add in Initialize to gather the historical data that warms up the algorithm. If we don't create any subscriptions, it can lead to unexpected warm-up issues.
Additionally, the algorithm above sets the Scheduled Event to run every day at 9:30 AM. Since the algorithm uses minute-resolution, the first bar of the day arrives at 9:31 AM. In the attached algorithm, we changed the Scheduled Event to run everyday SPY trades at 9:31 AM.
In this case, if we call _BuildHistory during Initialize, the algorithm hasn't received any data yet, so self.Securities[sym].Open is 0. To set the prices to the last known price, we can use the following security initializer:
See the attached backtest for reference.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Josh Anon
Thanks!
Josh Anon
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!