Back

From RollingWindow to numpy array

Hi
I understand that the recommended way to obtain history data is using RolllingWindow instead of History. But in my algorithm I need to get the data in some matricial way. That is, in numpy array or in pandas DataFrame. So I supose that the logical path is to transforme the RollingWindow data into a array. But if you know another method, I will be very thanksfull if you can show me how.
I'm using the following code to create the array. But, this is not a very efficient method. In fact, it would be better to simply use History method, because returns the data just in the format needed for the algorithm.

def OnData(self,data):
self.data = []
self.assets = ["AAPL", "IBM"]
for asset in self.assets:
self.closes = []
if self.Data[asset].IsReady():
for i in range(self.Bars.Count - 1):
self.closes.append(self.Data[asset].Bars[i].Close)
self.data.apppend(self.closes)
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.


I don't use Python personally, but don't you think the following would help you? Why can you not use History, like you proposed yourself?

https://www.quantconnect.com/docs/algorithm-reference/historical-data

 

Also I'm not sure about your assertion that "RollingWindow is the preferred way to obtain historical data". In fact, RollingWindows are meant to store data for use across different iterations of the algorithm. You can't really use it to "obtain" historical data.

0

As an alternative to Lean RollingWindow, you can use python collections.deque.
It is easily convertible to a numpy array:

import numpy as np
from collections import deque

# In Initilialize
self.queue = deque(maxlen=period)

# In OnData or elsewhere
self.queue.appendleft(value)
arr = np.array(self.queue)
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.


While looking for a solution to the same problem, I found this post.

This is how I do it now

closes = np.fromiter([i.Close for i in self.Bars], dtype=float, count=self.Bars.Count)

 

0

Hi all.

Thanks for sharing. You can also fill a DataFrame with prices and use the tail() and value() methods to create a rolling window numpy array:

### In Initialize()
# Create empty DataFrame
self.df = pd.DataFrame(columns = self.tickers)

### In OnData()
# Create an empty dictionary
dict = {}

# Fill the dictionary with prices
for ticker in self.tickers:
price = self.Securities[ticker].Price
dict[ticker] = price

# Append dictionary to DataFrame
self.df = self.df.append(dict,ignore_index=True)

# Return if DataFrame is not filled
if self.IsWarmingUp: return

# Retrieve last values in DataFrame and convert to numpy array
self.df.tail(self.period).values

I've attached a backtest where I use the method I describe above to retrieve a rolling window numpy array filled with prices.

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.


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