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







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.


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

 
";