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: PeriodN

Calculates a linear regression using statsmodel.OLS (Ordinary least squares) of data1 on data0

Uses pandas and statsmodels

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

packages = ()
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: PeriodN

Calculates the zscore for data0 and data1. Although it doesn’t directly use any external package, it relies on OLS_SlopeInterceptN which uses pandas and statsmodels

__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: PeriodN

Calculates a regression of data1 on data0 using pandas.ols

Uses pandas

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

packages = ()
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: PeriodN

Calculates the score (coint_t) and pvalue for a given period for the data feeds

Uses pandas and statsmodels (for coint)

next()[source]

Calculate cointegration test for the current period.

Uses statsmodels coint function to test for cointegration.

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

packages = ()
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