backtrader.indicators.ols module¶
OLS Indicator Module - Ordinary least squares regression.
This module provides indicators using OLS (Ordinary Least Squares) regression for statistical analysis.
- Classes:
OLS_Slope_InterceptN: Calculates slope and intercept via OLS. OLS_TransformationN: Calculates OLS transformed values. OLS_BetaN: Calculates beta via OLS. CointN: Tests for cointegration between series.
Example
- class MyStrategy(bt.Strategy):
- def __init__(self):
# Calculate OLS regression slope and intercept between two data feeds self.ols = bt.indicators.OLS_Slope_InterceptN(self.data0, self.data1, period=30)
# Calculate OLS transformation (spread, zscore) self.ols_trans = bt.indicators.OLS_TransformationN(self.data0, self.data1, period=30)
- def next(self):
# Trade based on zscore of the spread if self.ols_trans.zscore[0] > 2:
# Spread is too high - short data0, long data1 self.sell(data=self.data0) self.buy(data=self.data1)
- elif self.ols_trans.zscore[0] < -2:
# Spread is too low - long data0, short data1 self.buy(data=self.data0) self.sell(data=self.data1)
- class backtrader.indicators.ols.OLS_Slope_InterceptN[source]¶
Bases:
PeriodNCalculates a linear regression using
statsmodel.OLS(Ordinary least squares) of data1 on data0Uses
pandasandstatsmodels- packages = (('pandas', 'pd'), ('statsmodels.api', 'sm'))¶
- next()[source]¶
Calculate OLS slope and intercept for the current bar.
Uses statsmodels OLS to perform linear regression.
- frompackages = ()¶
- once(start, end)¶
Implement once using next for batch calculation.
This is used when next is overridden but once is not. It loops through the range and calls next for each step.
- Parameters:
start – Starting index
end – Ending index
- oncestart(start, end)¶
Implement oncestart using nextstart for batch calculation.
This is used when nextstart is overridden but oncestart is not.
- Parameters:
start – Starting index
end – Ending index
- preonce(start, end)¶
Implement preonce using prenext for batch calculation.
This is a generic implementation if prenext is overridden but preonce is not. It loops through the range and calls prenext for each step.
- Parameters:
start – Starting index
end – Ending index
- class backtrader.indicators.ols.OLS_TransformationN[source]¶
Bases:
PeriodNCalculates the
zscorefor data0 and data1. Although it doesn’t directly use any external package, it relies onOLS_SlopeInterceptNwhich usespandasandstatsmodels- __init__(*args, **kwargs)¶
- next()[source]¶
Calculate OLS transformation values for the current bar.
Computes spread, mean, std, and zscore based on OLS regression.
- once(start, end)[source]¶
Calculate OLS transformation in runonce mode.
Computes spread, mean, std, and zscore values across all bars.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.ols.OLS_BetaN[source]¶
Bases:
PeriodNCalculates a regression of data1 on data0 using
pandas.olsUses
pandas- packages = (('pandas', 'pd'),)¶
- next()[source]¶
Calculate beta via OLS regression for the current bar.
Uses pandas OLS to calculate regression beta.
- frompackages = ()¶
- once(start, end)¶
Implement once using next for batch calculation.
This is used when next is overridden but once is not. It loops through the range and calls next for each step.
- Parameters:
start – Starting index
end – Ending index
- oncestart(start, end)¶
Implement oncestart using nextstart for batch calculation.
This is used when nextstart is overridden but oncestart is not.
- Parameters:
start – Starting index
end – Ending index
- preonce(start, end)¶
Implement preonce using prenext for batch calculation.
This is a generic implementation if prenext is overridden but preonce is not. It loops through the range and calls prenext for each step.
- Parameters:
start – Starting index
end – Ending index
- class backtrader.indicators.ols.CointN[source]¶
Bases:
PeriodNCalculates the score (coint_t) and pvalue for a given
periodfor the data feedsUses
pandasandstatsmodels(forcoint)- packages = (('pandas', 'pd'),)¶
- frompackages = (('statsmodels.tsa.stattools', 'coint'),)¶
- next()[source]¶
Calculate cointegration test for the current period.
Uses statsmodels coint function to test for cointegration.
- once(start, end)¶
Implement once using next for batch calculation.
This is used when next is overridden but once is not. It loops through the range and calls next for each step.
- Parameters:
start – Starting index
end – Ending index
- oncestart(start, end)¶
Implement oncestart using nextstart for batch calculation.
This is used when nextstart is overridden but oncestart is not.
- Parameters:
start – Starting index
end – Ending index
- preonce(start, end)¶
Implement preonce using prenext for batch calculation.
This is a generic implementation if prenext is overridden but preonce is not. It loops through the range and calls prenext for each step.
- Parameters:
start – Starting index
end – Ending index