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.

示例

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[源代码]

基类:PeriodN

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

Uses pandas and statsmodels

packages = (('pandas', 'pd'), ('statsmodels.api', 'sm'))
next()[源代码]

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.

参数:
  • 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.

参数:
  • 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.

参数:
  • start -- Starting index

  • end -- Ending index

class backtrader.indicators.ols.OLS_TransformationN[源代码]

基类: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()[源代码]

Calculate OLS transformation values for the current bar.

Computes spread, mean, std, and zscore based on OLS regression.

once(start, end)[源代码]

Calculate OLS transformation in runonce mode.

Computes spread, mean, std, and zscore values across all bars.

frompackages = ()
packages = ()
class backtrader.indicators.ols.OLS_BetaN[源代码]

基类:PeriodN

Calculates a regression of data1 on data0 using pandas.ols

Uses pandas

packages = (('pandas', 'pd'),)
next()[源代码]

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.

参数:
  • 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.

参数:
  • 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.

参数:
  • start -- Starting index

  • end -- Ending index

class backtrader.indicators.ols.CointN[源代码]

基类:PeriodN

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

Uses pandas and statsmodels (for coint)

packages = (('pandas', 'pd'),)
frompackages = (('statsmodels.tsa.stattools', 'coint'),)
next()[源代码]

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.

参数:
  • 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.

参数:
  • 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.

参数:
  • start -- Starting index

  • end -- Ending index