hi guys,
I am looking at a way to get prev day's close for stocks when I am subscribed to tick/second/min data?
I searched and only saw suggestions with rollingwindow which isn't exactly something like Security.PrevClose and not programmatically clean.
Rolling window ones I found:
https://www.quantconnect.com/forum/discussion/669/how-to-look-back-at-previous-bars
https://www.quantconnect.com/forum/discussion/1332/get-previous-close-price
with the second above requiring to count number of minuites back if I want to check previous close at any time during the trading day.
Is there a good way to get prev close?
Thanks in advance, Herman
Alexandre Catarino
We can accomplish that in two lines!
Use the Identity indicator helper to get the previous day information automatically:
// Define the field: private Identity _prevClose; // In Initialize: _prevClose = Identity("SPY", Resolution.Daily, Field.Close);
Jared Broad
Just a note; "Identity" provides the current value. Assuming you have lower resolution (say minute) data flowing through the algorithm the code above will work as the daily close is only passed in at the end of the day. If your algorithm has only daily data this won't work as all the daily bars will be synchronized and arrive at midnight.
There currently isn't an explicit "yesterday's close" property but its easy to get by a simple variable. If there's enough demand we can add a helper to achieve this.
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.
Herman Ho
Cool, so to clarify:
Identity("SPY", Resolution.Daily, Field.Close);
but SPY would be subscribed at the minute resolution, that would indeed return previous close price?
Alexandre Catarino
Yes. As long as the resolution of the data we subscribed is lower than the Identity indicator resolution, this approach works.
Here is yet another approach:
// Define the field: private decimal _prevClose; // Sets up the field when the day ends public override void OnEndOfDay() { _prevClose = Securities["SPY"].Close; }
Herman Ho
I see, thanks! In live, to get the prev close I assume need to run it with some warm up then?
Herman Ho
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!