Historical Data

Rolling Window

Introduction

A RollingWindow is an array of a fixed-size that holds trailing data. It's more efficient to use RollingWindow objects to hold periods of data than to make multiple historical data requests. With a RollingWindow, you just update the latest data point while a History call fetches all of the data over the period you request.

Supported Types

RollingWindow objects can store any native or C# types.

closeWindow = new RollingWindow<decimal>(4);
tradeBarWindow = new RollingWindow<TradeBar>(2);
quoteBarWindow = new RollingWindow<QuoteBar>(2);	
self.close_window = RollingWindow[float](4)
self.trade_bar_window = RollingWindow[TradeBar](2)
self.quote_bar_window = RollingWindow[QuoteBar](2)

To be notified when RollingWindow objects support additional types, subscribe to GitHub Issue #6199.

Add Data

To add data to a RollingWindow, call the Add method.

closeWindow.Add(data["SPY"].Close);
tradeBarWindow.Add(data["SPY"]);
quoteBarWindow.Add(data["EURUSD"]);
self.close_window.Add(data["SPY"].Close)
self.trade_bar_window.Add(data["SPY"])
self.quote_bar_window.Add(data["EURUSD"])

Warm Up

To warm up a RollingWindow, make a history request and then iterate through the result to add the data to the RollingWindow.

var spy = AddEquity("SPY", Resolution.Daily).Symbol;
var historyTradeBar = History<TradeBar>(spy, 10, Resolution.Daily);
var historyQuoteBar = History<QuoteBar>(spy, 10, Resolution.Minute);

// Warm up the close price and trade bar rolling windows with the previous 10-day trade bar data
var closePriceWindow = new RollingWindow<decimal>(10);
var tradeBarWindow = new RollingWindow<TradeBar>(10);
foreach (var tradeBar in historyTradeBar)
{
    closePriceWindow.Add(tradeBar.Close);
    tradeBarWindow.Add(tradeBar);
}

// Warm up the quote bar rolling window with the previous 10-minute quote bar data
var quoteBarWindow = new RollingWindow<QuoteBar>(10);
foreach (var quoteBar in historyQuoteBar)
{
    quoteBarWindow.Add(quoteBar);
}
spy = self.AddEquity("SPY", Resolution.Daily).Symbol
history_trade_bar = self.History[TradeBar](spy, 10, Resolution.Daily)
history_quote_bar = self.History[QuoteBar](spy, 10, Resolution.Minute)

# Warm up the close price and trade bar rolling windows with the previous 10-day trade bar data
close_price_window = RollingWindow[float](10)
trade_bar_window = RollingWindow[TradeBar](10)
for trade_bar in history_trade_bar:
    close_price_window.Add(trade_bar.Close)
    trade_bar_window.Add(trade_bar)

# Warm up the quote bar rolling window with the previous 10-minute quote bar data
quote_bar_window = RollingWindow[QuoteBar](10)
for quote_bar in history_quote_bar:
    quote_bar_window.Add(quote_bar)

Check Readiness

To check if a RollingWindow is full, use its IsReady flag.

if (!closeWindow.IsReady) 
{
    return;
}
if not self.close_window.IsReady:
    return

Access Data

RollingWindow objects operate on a first-in, first-out process to allow for reverse list access semantics. Index 0 refers to the most recent item in the window and the largest index refers to the last item in the window.

var currentClose = closeWindow[0];
var previousClose = closeWindow[1];
var oldestClose = closeWindow[closeWindow.Count-1];
current_close = self.close_window[0]
previous_close = self.close_window[1]
oldest_close = self.close_window[self.close_window.Count-1]

To get the item that was most recently removed from the RollingWindow, use the MostRecentlyRemoved property.

var removedClose = closeWindow.MostRecentlyRemoved;
removed_close = self.close_window.MostRecentlyRemoved

Combine with Indicators

To track historical indicator values, use a RollingWindow. Indicators emit an Updated event when they update. To create a RollingWindow of indicator points, attach an event handler function to the Updated member that adds the last value of the indicator to the RollingWindow. The value is an IndicatorDataPoint object that represents a piece of data at a specific time.

public override void Initialize()
{
   // Create an indicator and adds to a RollingWindow when it is updated
   smaWindow = new RollingWindow<IndicatorDataPoint>(5);
   SMA("SPY", 5).Updated += (sender, updated) => smaWindow.Add(updated);
}
def Initialize(self) -> None:
    # Creates an indicator and adds to a RollingWindow when it is updated
    self.sma_window = RollingWindow[IndicatorDataPoint](5)
    self.SMA("SPY", 5).Updated += (lambda sender, updated: self.sma_window.Add(updated))

To view how to access individual members in an indicator, see Get Indicator Values.

Cast to Other Types

You can cast a RollingWindow to a list or a DataFrame. To cast a RollingWindow to a DataFrame, the RollingWindow must contain Slice, Tick, QuoteBar, or TradeBar objects. If the RollingWindow contains ticks, the ticks must have unique timestamps.

You can cast a RollingWindow to a list.

var closes = closeWindow.ToList();
closes = list(self.close_window)

tick_df = self.PandasConverter.GetDataFrame[Tick](self.tick_window)
trade_bar_df = self.PandasConverter.GetDataFrame[TradeBar](self.trade_bar_window)
quote_bar_df = self.PandasConverter.GetDataFrame[QuoteBar](self.quote_bar_window)

Delete Data

To remove all of the elements from a RollingWindow, call the Reset method.

closeWindow.Reset();
self.close_window.Reset()

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: