美文网首页
5.3 Mixing Timeframes in Indicat

5.3 Mixing Timeframes in Indicat

作者: wanggs66 | 来源:发表于2020-04-24 23:29 被阅读0次

Backtrader 从1.3.0.92之后支持不同timframes的数据混合使用。

  • obj(clockref=None, line=-1)

    • clockref If clockref is None, the surrounding object (in the examples a Strategy) will be the reference to adapt larger timeframes (for example: Months) to smaller/faster timeframes (for example: Days)
  • line If the default -1 is given, all lines are coupled. If another integer (for example, 0 or 1) a single line will be coupled and fetched by index (from obj.lines[x]). If a string is passed, the line will be fetched by name.

Integrated in the regular () syntax, data feeds from different timeframes can be mixed in indicators, always taking into account that cerebro needs to be instantiated or created with runonce=False.

Example:
import argparse

import backtrader as bt
import backtrader.feeds as btfeeds
import backtrader.indicators as btind
import backtrader.utils.flushfile


class St(bt.Strategy):
    params = dict(multi=True)

    def __init__(self):
        self.pp = pp = btind.PivotPoint(self.data1)
        pp.plotinfo.plot = False  # deactivate plotting

        if self.p.multi:
            pp1 = pp()  # couple the entire indicators
            self.sellsignal = self.data0.close < pp1.s1
        else:
            self.sellsignal = self.data0.close < pp.s1()

    def next(self):
        txt = ','.join(
            ['%04d' % len(self),
             '%04d' % len(self.data0),
             '%04d' % len(self.data1),
             self.data.datetime.date(0).isoformat(),
             '%.2f' % self.data0.close[0],
             '%.2f' % self.pp.s1[0],
             '%.2f' % self.sellsignal[0]])

        print(txt)


def runstrat():
    args = parse_args()

    cerebro = bt.Cerebro()
    data =   btfeeds.BacktraderCSVData(dataname=args.data)
    cerebro.adddata(data)
    cerebro.resampledata(data,   timeframe=bt.TimeFrame.Months)

    cerebro.addstrategy(St, multi=args.multi)

    cerebro.run(stdstats=False, runonce=False)
    if args.plot:
        cerebro.plot(style='bar')


def parse_args():
    parser = argparse.ArgumentParser(
      formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description='Sample for pivot point and cross plotting')

    parser.add_argument('--data', required=False,
                        default='../../datas/2005-2006-day-001.txt',
                        help='Data to be read in')

    parser.add_argument('--multi', required=False, action='store_true',
                    help='Couple all lines of the indicator')

    parser.add_argument('--plot', required=False, action='store_true',
                    help=('Plot the result'))

    return parser.parse_args()


if __name__ == '__main__':
    runstrat()

Notes:

pivotpoint.s1() is returning an internal LinesCoupler object which follows the rhythm of the larger scope. This coupler fills itself with the latest delivered value from the real s1 (starting with a default value of NaN)

相关文章

网友评论

      本文标题:5.3 Mixing Timeframes in Indicat

      本文链接:https://www.haomeiwen.com/subject/gvccwhtx.html