Skip to content

Commit

Permalink
Fixing OLS resulting params order and coint parameter name (mementum#412
Browse files Browse the repository at this point in the history
)

Co-authored-by: rafaelg <[email protected]>
  • Loading branch information
rafaeltg and rafaelg authored Jul 3, 2020
1 parent 8d36259 commit 6182758
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions backtrader/indicators/ols.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ class OLS_Slope_InterceptN(PeriodN):
squares) of data1 on data0
Uses ``pandas`` and ``statsmodels``
Use ``prepend_constant`` to influence the paramter ``prepend`` of
sm.add_constant
'''
_mindatas = 2 # ensure at least 2 data feeds are passed

Expand All @@ -48,14 +45,13 @@ class OLS_Slope_InterceptN(PeriodN):
lines = ('slope', 'intercept',)
params = (
('period', 10),
('prepend_constant', True),
)

def next(self):
p0 = pd.Series(self.data0.get(size=self.p.period))
p1 = pd.Series(self.data1.get(size=self.p.period))
p1 = sm.add_constant(p1, prepend=self.p.prepend_constant)
slope, intercept = sm.OLS(p0, p1).fit().params
p1 = sm.add_constant(p1)
intercept, slope = sm.OLS(p0, p1).fit().params

self.lines.slope[0] = slope
self.lines.intercept[0] = intercept
Expand Down Expand Up @@ -122,11 +118,11 @@ class CointN(PeriodN):
lines = ('score', 'pvalue',)
params = (
('period', 10),
('regression', 'c'), # see statsmodel.tsa.statttools
('trend', 'c'), # see statsmodel.tsa.statttools
)

def next(self):
x, y = (pd.Series(d.get(size=self.p.period)) for d in self.datas)
score, pvalue, _ = coint(x, y, regression=self.p.regression)
score, pvalue, _ = coint(x, y, trend=self.p.trend)
self.lines.score[0] = score
self.lines.pvalue[0] = pvalue

0 comments on commit 6182758

Please sign in to comment.