当前位置: 首页 > 工具软件 > Indicator > 使用案例 >

Backtrader(十一) - Indicator指标

爱花蜂
2023-12-01

简述

backtrader支持 内置指标 和 talib 指标
相关文档
内置指标:https://www.backtrader.com/docu/indautoref/
talib指标:https://www.cnblogs.com/forest128/p/13823649.html

策略中使用内置指标:

class SmaCross(bt.Strategy):
    params = dict(period=5)

    def __init__(self):
        self.move_average = bt.ind.MovingAverageSimple(
            self.data.close,
            period=self.params.period
        )

策略中使用talib指标:
mac安装ta-lib:

brew install ta-lib

anaconda 安装TA-Lib

pip install TA-Lib

class TALibStrategy(bt.Strategy):
    params = (('period', 20), )

    def __init__(self):
        # ta-lib移动平均指标
        self.sma = bt.talib. T3(self.data, timeperiod=self.p.period)

自定义指标

backtrader的运算符

python的逻辑运算符都被Backtrader覆盖了,使其可以直接作用于线对象整体,进行矢量化运算

pythonbacktrader
andAnd
orOr
ifIf
anyAny
allAll
cmpCmp
maxMax
minMin
sumSum
reduceReduce
DivByZero
DivZeroByZero

tips:
cmp(x,y) 函数用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1。

在策略类的init方法中定义新指标

'''在策略类的init方法中定义新指标'''

import backtrader as bt
from feed import feed
from logger import lg


class MyStrategy(bt.Strategy):
    params = dict(period1=20, period2=25, period4=10, period3=5)

    def __init__(self):
        sma1 = bt.ind.MovingAverageSimple(
            self.data.close,
            period=self.params.period1
        )

        sma2 = bt.ind.MovingAverageSimple(
            sma1,
            period=self.params.period2
        )

        # 通过算数计算创建新指标线
        something = sma2 - sma1 + self.data.close

        sma3 = bt.ind.MovingAverageSimple(
            something,
            period=self.params.period3
        )

        self.greater = sma3 > sma1


        self.buysig = bt.And(sma1 > self.data.close, sma1 > self.data.high)
        self.high_or_low = bt.If(sma1 > self.data.close, self.data.low, self.data.high)
        self.high_or_30 = bt.If(sma1 > self.data.close, 30.0, self.data.high)
        # 生成开盘价除以收盘价形成的线,若某天的收盘价为0,则最终指标该日取 99999
        self.testIndicator = bt.DivByZero(self.data.open, self.data.close, zero=99999)
        # 若分母为0,则取8888;若分子分母都为0,取99999
        self.testIndicator2 = bt.DivZeroByZero(self.data.open, self.data.close, 8888, 99999)


if __name__ == '__main__':
    cerebro = bt.Cerebro()
    cerebro.adddata(feed)
    cerebro.addstrategy(MyStrategy)

    cerebro.run(stdstats=False)

    cerebro.plot()

注意 __init__中的指标画图时不会展示出来

自定义指标类(__init__中)

'''自定义新指标类 __init__ 中'''

import backtrader as bt
from feed import feed
from logger import lg


class OverUnderMovAv(bt.Indicator):
    lines = (
        'overunder',
    )

    params = dict(
        period=10
    )

    def __init__(self):
        movav = bt.ind.MovingAverageSimple(
            self.data,
            period=self.params.period
        )
        self.l.overunder = bt.Cmp(movav, self.data)

    def next(self):
        pass



class Mystrategy(bt.Strategy):
    params = dict(
        period=20
    )

    def __init__(self):
        self.overunder = OverUnderMovAv(self.data.close, period=self.params.period)





if __name__ == '__main__':
    cerebro = bt.Cerebro()
    cerebro.adddata(feed)
    cerebro.addstrategy(Mystrategy)

    cerebro.run(stdstats=False)

    cerebro.plot()

这个指标采用了backtrader内置的指标进行运算,无需自己处理最小周期

自定义指标类(next中)

'''自定义新指标类 next 中'''
import math
from feed import feed
import backtrader as bt

class SimpleMovingAverage1(bt.Indicator):
    lines = ('sma',)
    params = (
        ('period', 20),
    )

    def __init__(self):
        self.addminperiod(
            self.params.period
        )

    def next(self):
        datasum = math.fsum(
            self.data.get(size=self.p.period)
        )

        self.lines.sma[0] = datasum / self.p.period


class Mystrategy(bt.Strategy):
    params = dict(
        period=20
    )

    def __init__(self):
        self.overunder = SimpleMovingAverage1(self.data.close, period=self.params.period)



if __name__ == '__main__':
    cerebro = bt.Cerebro()
    cerebro.adddata(feed)
    cerebro.addstrategy(Mystrategy)

    cerebro.run(stdstats=False)

    cerebro.plot()

当需要自定义最小周期时可以使用next创建 指标类,在__init__中使用self.addminperiod()定义最小周期

注意 指标类形式创建的指标会在画图中展示

 类似资料: