When I am in the event "OnData(Slice data)", is there a way for me to get Close from 5 candles ago, or do I need to track the values in the array?
Don't have an account? Join QuantConnect Today
QuantConnect Community Discussions
QUANTCONNECT COMMUNITY
LEAN is the open-source algorithmic trading engine powering QuantConnect. Founded in 2012 LEAN has been built by a global community of 180+ engineers and powers more than 300+ hedge funds today.
Join QuantConnect's Discord server for real-time support, where a vibrant community of traders and developers awaits to help you with any of your QuantConnect needs.
The Open-Quant League is a quarterly competition between universities and investment clubs for the best-performing strategy. The previous quarter's code is open-sourced, and competitors must adapt to survive.
Accessing past Close data in OnData event: track array or use RollingWindow.
Continue ReadingRefer to our Research Guidelines for high quality research posts.
Create an account on QuantConnect for the latest community delivered to your inbox.
Sign Up Today
|
|
|||||||
|
|
||||||||
|
Historic Slice Data
Dmitriy | November 2018
When I am in the event "OnData(Slice data)", is there a way for me to get Close from 5 candles ago, or do I need to track the values in the array?
QuantConnectâ„¢ 2025. All Rights Reserved
Jing Wu
You can create a rolling window with the length 5 in Initialize() and save the bar data in a rolling window in OnData().
public override void Initialize() { // Create a Rolling Window to keep the 5 decimal closeWindow = new RollingWindow<decimal>(5); } // In OnData, update the rolling windows public override void OnData(Slice data) { // Add bar close in the rolling window closeWindow.Add(data[symbol].Close);
Then you can retrieve the close value with the index of the window. Before that, please use "closeWindow.IsReady" to check if the rolling window is fully initialized.
For details, please see the documentation
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.
Dmitriy
So I am observing that when RollingWindow objectr reaches it's size it begins to populate from 0 again. I thought it would continue to populate the most recent value in the last index and shift all values down.
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.
Jing Wu
The rolling window populates the most recent value. You obtain the zero value probably because the price "data[symbol].Close" is zero. It is better to add a check
public override void OnData(Slice data) { // Add bar close in the rolling window if data.ContainsKey(symbol): closeWindow.Add(data[symbol].Close); }
If the issue still exists, please attach your algorithm so we can further help with your algorithm.
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.
Dmitriy
No. I was watching it debugger. Let's say I setup RollingWindow<decimal>(3). I have close values of 20, 21, 22, 23, 24... So what I see is that for first 3 values the objectis populated as [0] = 20, [1] = 21, [2] = 22, and on 4th value it restarts at 0, so I see object as [0] = 23, [1] = 21, [2] = 22
What I expected was that values would shift, so I would see this [0] = 21, [1] = 22, [3] = 23. So basically I would always have the most recent 3 values in the object.
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.
Jing Wu
You should always have the most recent value in the rolling window. Please read the documentation
The most recent bar always has the index 0. For example, you have a list of close as 20 (2018-06-01), 21 (2018-06-02), 22(2018-06-03), 23(2018-06-04), 24(2018-06-05), window[0] is always replaced with the most recent bar price.
2018-06-01, window[0]=20
2018-06-02, window[0]=21, window[1]=20
2018-06-03, window[0]=22, window[1]=21, window[2]=20 (Now the rolling window is ready)
2018-06-04, window[0]=23, window[1]=22, window[2]=21
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!