backtrader.indicators.myind module

Custom Indicators Module - User-defined indicators.

This module contains custom indicator algorithms including: - MaBetweenHighAndLow: Check if MA is within price range - BarsLast: Count bars since condition was met - NewDiff: Guotai Junan alpha factor indicator

示例

To use these custom indicators in your strategy:

import backtrader as bt

class MyStrategy(bt.Strategy):
    def __init__(self):
        # Create MA Between High and Low indicator
        self.ma_hl = bt.indicators.MaBetweenHighAndLow(
            self.data, period=5
        )

        # Create Bars Last indicator
        self.bars_last = bt.indicators.BarsLast(
            self.data, period=5
        )

        # Create NewDiff indicator (Guotai Junan alpha factor)
        self.new_diff = bt.indicators.NewDiff(
            self.data, period=6
        )

    def next(self):
        # Use the indicators in your trading logic
        if self.ma_hl.target[0] > 0:
            # MA is within high-low range
            self.buy()

        if self.bars_last.bar_num[0] < 3:
            # Condition was met less than 3 bars ago
            self.sell()

        # Access the alpha factor value
        alpha_value = self.new_diff.factor[0]
class backtrader.indicators.myind.MaBetweenHighAndLow[源代码]

基类:Indicator

Check if moving average is between high and low prices.

Returns 1.0 when SMA is within the bar's high-low range, 0.0 otherwise.

__init__(*args, **kwargs)
next()[源代码]

Check if MA is between high and low for current bar.

Returns 1.0 if MA is within range, 0.0 otherwise.

once(start, end)[源代码]

Check MA against high/low range in runonce mode.

Returns 1.0 where MA is within range, 0.0 otherwise.

frompackages = ()
packages = ()
class backtrader.indicators.myind.BarsLast[源代码]

基类:Indicator

Count bars since condition was last met.

Tracks the number of bars that have passed since a specified condition (default: MaBetweenHighAndLow) was last true.

__init__(*args, **kwargs)
next()[源代码]

Count bars since condition was last met.

Resets to 0 when condition is true, increments otherwise.

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

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.

参数:
  • start -- Starting index

  • end -- Ending index

class backtrader.indicators.myind.NewDiff[源代码]

基类:Indicator

Guotai Junan alpha factor indicator.

Calculates a proprietary alpha factor based on price movement relative to previous close and the high/low range.

Formula:

SUM((CLOSE==DELAY(CLOSE,1)?0:CLOSE-(CLOSE>DELAY(CLOSE,1)? MIN(LOW,DELAY(CLOSE,1)):MAX(HIGH,DELAY(CLOSE,1)))), period)

__init__(*args, **kwargs)
next()[源代码]

Calculate NewDiff factor for the current bar.

Sums adjusted price differences over the period.

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

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.

参数:
  • start -- Starting index

  • end -- Ending index