Hello,
I am searching for a code which can give me the previous two day's OHLC values. Also i want the opening price for today market.
e.g. Today it is 23-10-2017. so i will need OHLC value for 20-10-2017 and 19-10-2017. Also i will require to get the opening price for particular symbol when market open on 23-10-2017.
Thanks,
JayJayD
The tool you need for storing past values is the RollingWindow, another alternative is using History
To get today’s open you can use a Schelude the first minute before the market opens and store in a field.
Here is an example:
public class BasicTemplateDailyAlgorithm : QCAlgorithm { private RollingWindow<TradeBar> _dailyBars = new RollingWindow<TradeBar>(3); private TradeBar _todayFirstMinuteBar; public override void Initialize() { SetStartDate(2013, 10, 07); //Set Start Date SetEndDate(2013, 10, 18); //Set End Date SetCash(100000); //Set Strategy Cash AddEquity("SPY", Resolution.Daily); Schedule.On(DateRules.EveryDay(), TimeRules.AfterMarketOpen("SPY", 1), () => { _todayFirstMinuteBar = History<TradeBar>("SPY", TimeSpan.FromMinutes(5), Resolution.Minute).Last(); }); } public override void OnData(Slice slice) { _dailyBars.Add(slice.Bars["SPY"]); if (_dailyBars.IsReady) { var todaysOpen = _todayFirstMinuteBar.Open; // Implement your logic here. } } }
Dee Menzies
Thanks JayJayD,
I have used the code you provided, but it gives data till previous date only. I will set the start date to "2017-10-23" and end date to "2017-10-26". The code should provide minute data starting from 2017-10-23 09:30:00 to 2017-10-26 03:23:00, if the datetime.now is 2017-10-26 03:24:00.
Alexandre Catarino
For minute-data of the past 2 days, please use:
var lastTwoDayData = History("SPY", TimeSpan.FromDays(2), Resolution.Minute);
Please note that it fetches the last two days, not the last two trading days, so you need to addapt the code to handle Mondays, Tuesdays and days after holidays.
Dee Menzies
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!