Back

Historic Slice Data

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?

Update Backtest







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 

https://www.quantconnect.com/docs/algorithm-reference/rolling-window
0

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.

0

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.

0

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.

0

You should always have the most recent value in the rolling window. Please read the documentation

https://www.quantconnect.com/docs/algorithm-reference/rolling-window

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

0

Update Backtest





0

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.


Loading...

This discussion is closed