有时投资决策是使用不同的时间框架做出的:
-
每周评估趋势
-
每天运行条目
或者5分钟对60分钟。
这意味着需要组合多个时间帧的数据backtrader 来支持这种组合。
对它的本机支持已经内置。最终用户只能遵循以下规则:
-
具有最小时间帧(因此柱数较大的数据)必须是要添加到Cerebro实例中的第1个数据
-
数据必须正确对齐日期时间,以便平台从中产生任何意义
除此之外,最终用户可以自由地在较短/较长的时间范围内按预期应用指针。答案是肯定的:
- 应用于较大时间帧的指针将产生较少的柱线
该平台还将考虑以下因素
- 较大时间帧的最小周期
最小周期,这可能会产生副作用,即在将策略添加到 Cerebro 开始行动之前,必须消耗几个数量级的较小时间帧柱。
内置cerebro.resample 的将用于创建更大的时间范围。
下面是一些示例,但首先是测试脚本的酱汁。
# Load the Data
datapath = args.dataname or '../../datas/2006-day-001.txt'
data = btfeeds.BacktraderCSVData(dataname=datapath)
cerebro.adddata(data) # First add the original data - smaller timeframe
tframes = dict(daily=bt.TimeFrame.Days, weekly=bt.TimeFrame.Weeks,
monthly=bt.TimeFrame.Months)
# Handy dictionary for the argument timeframe conversion
# Resample the data
if args.noresample:
datapath = args.dataname2 or '../../datas/2006-week-001.txt'
data2 = btfeeds.BacktraderCSVData(dataname=datapath)
# And then the large timeframe
cerebro.adddata(data2)
else:
cerebro.resampledata(data, timeframe=tframes[args.timeframe],
compression=args.compression)
# Run over everything
cerebro.run()
步骤:
-
加载数据
-
根据用户指定的参数对其进行重新采样
该脚本还允许加载第 2 个 数据
-
将数据添加到 cerebro
-
将重新采样的数据(更大的时间帧)添加到 cerebro
-
跑
示例 1 - 每日和每周
文稿的调用:
$ ./multitimeframe-example.py --timeframe weekly --compression 1
和输出图表:
范例 2 - 每日和每日压缩(2 个 bar 到 1)
文稿的调用:
$ ./multitimeframe-example.py --timeframe daily --compression 2
和输出图表:
范例 3 - 使用 SMA 的策略
虽然绘图很好,但这里的关键问题是显示更大的时间框架如何影响系统,特别是当它归结为起点时。
该脚本可以添加--indicators 一个策略,该策略在较小和较大的时间帧数据上创建 周期为10 的简单移动平均线。
如果只考虑较小的时间范围:
-
next将在10根柱线后首先调用,这是简单移动平均线产生值所需的时间注意:请记住,Strategy 监控已创建的指针,并且仅在所有指针都生成值时才调用
next。基本原理是最终用户已添加指针以在逻辑中使用它们,因此,如果指针未生成任何值,则不应发生任何逻辑。
但是在这种情况下,较大的时间帧(每周)延迟调用next ,直到每周数据产生一个值的简单移动平均线,这需要...10周。
该脚本将nextstart 覆盖仅调用一次且缺省为调用 next 以显示首次调用时的脚本。
调用 1:
只有较小的时间帧,每天,得到一个简单的移动平均线
命令 line 和输出
$ ./multitimeframe-example.py --timeframe weekly --compression 1 --indicators --onlydaily -------------------------------------------------- nextstart called with len 10 --------------------------------------------------
和图表。
调用 2:
两个时间帧都得到一个简单的移动平均线
命令 line:
$ ./multitimeframe-example.py --timeframe weekly --compression 1 --indicators -------------------------------------------------- nextstart called with len 50 -------------------------------------------------- -------------------------------------------------- nextstart called with len 51 -------------------------------------------------- -------------------------------------------------- nextstart called with len 52 -------------------------------------------------- -------------------------------------------------- nextstart called with len 53 -------------------------------------------------- -------------------------------------------------- nextstart called with len 54 --------------------------------------------------
这里要注意两件事:
-
该策略不是在10 个周期后调用,而是在50个周期后调用1st 。
之所以如此,是因为应用于较大(每周)时间帧的简单移动平均线在10周后产生一个值......那就是
10 weeks * 5 days / week … 50 days -
nextstart被调用 5 次,而不仅仅是 1 次。这是混合时间帧并将指针(在本例中只有一个)应用于较大时间帧的自然副作用。
较大的时间帧简单移动平均线产生相同值的 5 倍,而 5 个日柱线被消耗。
而且由于周期的开始被较大的时间帧
nextstart控制,因此被调用了5次。
和图表。
结论
多个时间帧数据可以在没有特殊对象或调整的情况下使用backtrader :只需先添加较小的时间帧即可。
测试脚本。
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
import backtrader as bt
import backtrader.feeds as btfeeds
import backtrader.indicators as btind
class SMAStrategy(bt.Strategy):
params = (
('period', 10),
('onlydaily', False),
)
def __init__(self):
self.sma_small_tf = btind.SMA(self.data, period=self.p.period)
if not self.p.onlydaily:
self.sma_large_tf = btind.SMA(self.data1, period=self.p.period)
def nextstart(self):
print('--------------------------------------------------')
print('nextstart called with len', len(self))
print('--------------------------------------------------')
super(SMAStrategy, self).nextstart()
def runstrat():
args = parse_args()
# Create a cerebro entity
cerebro = bt.Cerebro(stdstats=False)
# Add a strategy
if not args.indicators:
cerebro.addstrategy(bt.Strategy)
else:
cerebro.addstrategy(
SMAStrategy,
# args for the strategy
period=args.period,
onlydaily=args.onlydaily,
)
# Load the Data
datapath = args.dataname or '../../datas/2006-day-001.txt'
data = btfeeds.BacktraderCSVData(dataname=datapath)
cerebro.adddata(data) # First add the original data - smaller timeframe
tframes = dict(daily=bt.TimeFrame.Days, weekly=bt.TimeFrame.Weeks,
monthly=bt.TimeFrame.Months)
# Handy dictionary for the argument timeframe conversion
# Resample the data
if args.noresample:
datapath = args.dataname2 or '../../datas/2006-week-001.txt'
data2 = btfeeds.BacktraderCSVData(dataname=datapath)
# And then the large timeframe
cerebro.adddata(data2)
else:
cerebro.resampledata(data, timeframe=tframes[args.timeframe],
compression=args.compression)
# Run over everything
cerebro.run()
# Plot the result
cerebro.plot(style='bar')
def parse_args():
parser = argparse.ArgumentParser(
description='Multitimeframe test')
parser.add_argument('--dataname', default='', required=False,
help='File Data to Load')
parser.add_argument('--dataname2', default='', required=False,
help='Larger timeframe file to load')
parser.add_argument('--noresample', action='store_true',
help='Do not resample, rather load larger timeframe')
parser.add_argument('--timeframe', default='weekly', required=False,
choices=['daily', 'weekly', 'monhtly'],
help='Timeframe to resample to')
parser.add_argument('--compression', default=1, required=False, type=int,
help='Compress n bars into 1')
parser.add_argument('--indicators', action='store_true',
help='Wether to apply Strategy with indicators')
parser.add_argument('--onlydaily', action='store_true',
help='Indicator only to be applied to daily timeframe')
parser.add_argument('--period', default=10, required=False, type=int,
help='Period to apply to indicator')
return parser.parse_args()
if __name__ == '__main__':
runstrat()