backtrader2 / backtrader

Python Backtesting library for trading strategies
https://www.backtrader.com
GNU General Public License v3.0
235 stars 54 forks source link

OLS_Slope_InterceptN slope, intercept should be intercept, slope #28

Closed neilsmurphy closed 4 years ago

neilsmurphy commented 4 years ago

Andre Tavares posted this question in the community:

André Tavares 9 days ago

The class OLS_Slope_InterceptN calculates slope and intercept. However I think it is on the wrong order. It is as follows:

slope, intercept = sm.OLS(p0, p1).fit().params

I think the correct is:

intercept, slope = sm.OLS(p0, p1).fit().params

I am right?

The code in question can be found in the indicators/ols.py module at line 58.

My cursory look at statsmodel documentation suggests that the backtrader implementation is incorrect and backwards. \

Here are a few helpful links. https://www.statsmodels.org/devel/generated/statsmodels.regression.linear_model.OLS.html https://www.statsmodels.org/stable/examples/notebooks/generated/ols.html

Thoughts?

neilsmurphy commented 4 years ago

Suggested solution by @rafaeltg:

Removing self.p.prepend_constant in order to avoid invalid result parameters order. With prepend = True intercept is the first parameter and slope the second.

https://github.com/mementum/backtrader/pull/412

neilsmurphy commented 4 years ago

There is a pull request for this in mementum/backtrader. I have asked the creator of the PR to issue it here.

PR#412

neilsmurphy commented 4 years ago

Merged in mementum/backtrader.