Algorithm Reference

Rolling Window

Introduction

RollingWindow is an array of data that allows for reverse list access semantics, where the object with index [0] refers to the most recent item in the window and index [Length-1] refers to the last item in the window, where Length is the number of elements in the window.

We can store any type of object in a RollingWindow, since it is a generic type, with a given max length:

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

These rolling arrays are updated by adding a new element of its type with the Add method:

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

The most recent element, the previous and the last item for the decimal case are:

	var currentClose = closeWindow[0];
	var previousClose = closeWindow[1];
	var oldestClose = closeWindow[closeWindow.Count-1];
	
	currentClose = self.closeWindow[0]
	previousClose = self.closeWindow[1]
	oldestClose = self.closeWindow[self.closeWindow.Count-1]
	

We recommend using RollingWindows to hold periods of data instead of making multiple Historical Data Requests. It is much more efficient as we just need to update the RollingWindow with the latest data point, while a History call fetches the whole requested period and synchronizes the data.

// In Initialize, create the rolling windows
public override void Initialize()
{
    // Create a Rolling Window to keep the 4 decimal
    closeWindow = new RollingWindow<decimal>(4);
    // Create a Rolling Window to keep the 2 TradeBar
    tradeBarWindow = new RollingWindow<TradeBar>(2);
    // Create a Rolling Window to keep the 2 QuoteBar
    quoteBarWindow = new RollingWindow<QuoteBar>(2);
}

// In OnData, update the rolling windows
 public override void OnData(Slice data)
{
    // Add SPY bar close in the rolling window
    closeWindow.Add(data["SPY"].Close);
    // Add SPY TradeBar in rolling window
    tradeBarWindow.Add(data["SPY"]);
    // Add EURUSD QuoteBar in rolling window
    quoteBarWindow.Add(data["EURUSD"]);
}
# In Initialize, create the rolling windows
def Initialize(self):
    # Create a Rolling Window to keep the 4 decimal
    self.closeWindow = RollingWindow[decimal](4)
    # Create a Rolling Window to keep the 2 TradeBar
    self.tradeBarWindow = RollingWindow[TradeBar](2)
    # Create a Rolling Window to keep the 2 QuoteBar
    self.quoteBarWindow = RollingWindow[QuoteBar](2)

# In OnData, update the rolling windows
 def OnData(self, data):
    # Add SPY bar close in the rolling window
    self.closeWindow.Add(data["SPY"].Close)
    # Add SPY TradeBar in rolling window
    self.tradeBarWindow.Add(data["SPY"])
    # Add EURUSD QuoteBar in rolling window
    self.quoteBarWindow.Add(data["EURUSD"])

Combining with Indicators

A particularly common and helpful use of the RollingWindow class is to store past indicator values. The following examples create an indicator and adds its values to a rolling window when the indicator is updated.

	SMA("SPY", 5).Updated += (sender, updated) => smaWin.Add(updated);
	smaWin = new RollingWindow<IndicatorDataPoint>(5);
   
	self.SMA("SPY", 5).Updated += self.SmaUpdated
	self.smaWin = RollingWindow[IndicatorDataPoint](5)

	# Adds updated values to rolling window
	def SmaUpdated(self, sender, updated):
		self.smaWin.Add(updated)

The most recent element, the previous and the last item for this case are:

	var currentSma = smaWin[0];
	var previousSma = smaWin[1];
	var oldestSma = smaWin[ smaWin.Count - 1 ];
	
	currentSma = self.smaWin[0]
	previousSma = self.smaWin[1]
	oldestSma = self.smaWin[ smaWin.Count - 1 ]
// In Initialize, create the rolling windows
public override void Initialize()
{
    // Creates an indicator and adds to a rolling window when it is updated
    SMA("SPY", 5).Updated += (sender, updated) => smaWin.Add(updated);
    smaWin = new RollingWindow<IndicatorDataPoint>(5);
}
# In Initialize, create the rolling windows
def Initialize(self):
    # Creates an indicator and adds to a rolling window when it is updated
    self.SMA("SPY", 5).Updated += self.SmaUpdated
    self.smaWin = RollingWindow[IndicatorDataPoint](5)

# Adds updated values to rolling window
def SmaUpdated(self, sender, updated):
    self.smaWin.Add(updated)

You can also see our Tutorials and Videos. You can also get in touch with us via Chat.

Did you find this page Helpful ?