Indicators
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
history
call fetches all of the data over the period you request. 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.
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
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"])
To update the data at a specific index, set the value for that index. If the index doesn't currently exist, it increases the size and fills the empty indices with a default value (zero or null
None
).
closeWindow[0] = data["SPY"].Close; tradeBarWindow[0] = data["SPY"]; quoteBarWindow[0] = data["EURUSD"];
self._close_window[0] = data["SPY"].close self._trade_bar_window[0] = data["SPY"] self._quote_bar_window[0] = 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.add_equity("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)
Adjust Size
To adjust the RollingWindow
size, set the Size
size
property.
closeWindow.Size = 3; tradeBarWindow.Size = 3; quoteBarWindow.Size = 3;
self._close_window.size = 3 self._trade_bar_window.size = 3 self._quote_bar_window.size = 3
When you decrease the size, it removes the oldest values that no longer fit in the RollingWindow
. When you explicitly increase the Size
size
member, it doesn't automatically add any new elements to the RollingWindow
. However, if you set the value of an index in the RollingWindow
and the index doesn't currently exist, it fills the empty indices with a default value (zero or null
None
). For example, the following code increases the Size
size
to 10, sets the 10th element to 3, and sets the 4th-9th elements to the default value:
closeWindow[9] = 3;
self._close_window[9] = 3
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
most_recently_removed
property.
var removedClose = closeWindow.MostRecentlyRemoved;
removed_close = self._close_window.most_recently_removed
Combine with Indicators
The Window
window
property of the indicators is a built-in RollingWindow
that stores historical values. It holds a collection of IndicatorDataPoint
objects, enabling quick access to the most recent historical indicator values for analysis, calculations, or comparisons in trading and financial strategies.
Default size of the in-built RollignWindow
is 2, but you can adjust its size by setting the Size
size
according to your need.
To warm up the indicator together with its RollignWindow
, you can call the IndicatorHistory
indicator_history
method to do so.
public class IndicatorRolingWindowAlgorithm : QCAlgorithm { // Manual SMA indicator example. private SimpleMovingAverage _sma = new(20); private Beta _beta; public override void Initialize() { var spy = AddEquity("SPY").Symbol; var qqq = AddEquity("QQQ").Symbol; // Automatic Beta indicator example. _beta = B(spy, qqq, 20); // Adjust the window size to 5 to access the previous 5 indicator data points. _sma.Window.Size = 5; _beta.Window.Size = 5; // Warm up the indicator for immediate usage; also, warm up the window. // You can use IndicatorHistory to control the number of bars you warm up with. IndicatorHistory(_sma, spy, _sma.WarmUpPeriod + _sma.Window.Size, Resolution.Daily); // For indicators using 2+ symbols, use an iterable symbol list. IndicatorHistory(_beta, new[] { spy, qqq }, _beta.WarmUpPeriod + _beta.Window.Size, Resolution.Daily); } }
class IndicatorRolingWindowAlgorithm(QCAlgorithm): def initialize(self) -> None: spy = self.add_equity("SPY").symbol qqq = self.add_equity("QQQ").symbol # Manual SMA indicator example. self._sma = SimpleMovingAverage(20) # Automatic Beta indicator example. self._beta = self.b(spy, qqq, 20) # Adjust the window size to 5 to access the previous 5 indicator data points. self._sma.window.size = 5 self._beta.window.size = 5 # Warm up the indicator for immediate usage; also, warm up the window. # You can use indicator_history to control the number of bars to warm up with.self.indicator_history(self._sma, spy, self._sma.warm_up_period + self._sma.window.size, Resolution.DAILY) # For indicators using 2+ symbols, use an iterable symbol list. self.indicator_history(self._beta, [spy, qqq], self._beta.warm_up_period + self._beta.window.size, Resolution.DAILY)
Combine with Consolidators
To store a history of consolidated bars, in the consolidation handler, add the consolidated bar to the RollingWindow
.
_consolidator.DataConsolidated += (sender, consolidatedBar) => tradeBarWindow.Add(consolidatedBar);
self._consolidator.data_consolidated += self._on_data_consolidated # Define consolidator handler function as a class method def _on_data_consolidated(self, sender, consolidated_bar): self._trade_bar_window.add(consolidated_bar)
Cast to Other Types
You can cast a RollingWindow
to a list or a DataFrame. If you cast it to a list, reverse the list so the most recent element is at the last index of the list. This is the order the elements would be in if you added the elements to the list with the Add
method. 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. If you cast it to a list, reverse the list so the most recent element is at the last index of the list. This is the order the elements would be in if you added the elements to the list with the Add
add
method.
var closes = closeWindow.Reverse().ToList();
closes = list(self._close_window)[::-1] tick_df = self.pandas_converter.get_data_frame[Tick](list(self._tick_window)[::-1]) trade_bar_df = self.pandas_converter.get_data_frame[TradeBar](list(self._trade_bar_window)[::-1]) quote_bar_df = self.pandas_converter.get_data_frame[QuoteBar](list(self._quote_bar_window)[::-1])