Hi, I was looking for some help to compare SPY's previous day's price. Basically I want to buy SPY at 9:45am if the current price is greater than yesterday's closing price. The code below is what I have. I'm not sure how to access the previous day's closing price. Thanks for the help!
class EmotionalFluorescentPinkSalmon(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 1, 1)
self.SetEndDate(2021, 1, 1)
self.SetCash(100000)
self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
def OnData(self, data):
if self.Time.hour == 9 and self.Time.minute == 45 and not self.Portfolio.Invested:
self.SetHoldings(self.spy, 1)
if self.Time.hour == 15 and self.Time.minute == 45:
self.Liquidate(self.spy)
Vladimir
Arsal Khan,
Â
Try this way.
Â
Varad Kabade
Hi Arsal Khan,
There are multiple ways to store the previous day's close. Easiest would be to create an Identity indicator with daily resolution:
Â
; therefore, in the OnData method, we can access it. Alternatively, we can also use RollingWindow to store the previous day's closing price.
Best,
Varad Kabade
Arsal Khan
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!