The problem with the snippet being that pd.ols
chokes on regular Python array-like structures (array.array
, list
, etc) needing pandas
specific structures.
The adapted and working code
class OLS_Beta(bt.indicators.PeriodN):
_mindatas = 2 # ensure at least 2 data feeds are passed
lines = (('beta'),)
params = (('period', 30),)
def next(self):
y, x = (pd.Series(d.get(size=self.p.period)) for d in self.datas)
r_beta = pd.ols(y=y, x=x, window_type='full_sample')
self.lines.beta[0] = r_beta.beta['x']
In which the values from the dataX
feeds is put into a pd.Series
instance. There is, imho, no need to use a rolling operation because pd.ols
only receives the needed data each time (the latest available data).
The same concept can be applied other pandas
operations and I guess to the code ported over to statsmodel