Backtrader中文笔记之Renko Bricks
The Renko Bricks is a different way to present the evolution of prices in which price plays a more important role than time. This has been introduced as a filter in release 1.9.54.122
of backtrader
Stockcharts has a good reference on the Renko Bricks. See Renko Bricks @StockCharts
Renko块是一种不同的方式来呈现价格的演变,其中价格的作用比时间更重要。这在backtrader的1.9.54.122版本中作为过滤器引入
股票图表对Renko块有很好的参考价值。参见Renko Bricks@股票图表
Some examples
Note
The size=35
and align=10.0
parameters are appropriate for the sample data in the backtrader repository. Those values have to be fine tuned for each data asset.
注意:
size=35和align=10.0参数适用于backtrader存储库中的示例数据。这些值必须针对每个数据资产进行微调。
First, let’s put the Renko Bricks alone in a chart:
首先,让我们把Renko砖单独放在图表中:
$ ./renko.py --renko size=35,align=10.0 --plot
输出:
One can see that the chart immediately reveals some resistance/support areas, which is one of the main advantages of the Renko Bricks. It should also be obvious that the temporal evolution in the X-Axis is no longer constant and is stretched or compressed, depending on whether the price action was more static during the period or moved several bricks.
你可以看到图表立即显示了一些阻力/支撑区域,这是Renko砖块的主要优势之一。同样明显的是,x轴上的时间演化不再是恒定的,而是被拉伸或压缩,这取决于在此期间价格走势是更加稳定还是移动了几块砖。
To better see and appreciate the effects, let’s put the normal price bars and the Renko Bricks together on a chart:
为了更好地观察和欣赏效果,让我们把正常价格条和Renko砖放在一个图表上:
$ ./renko.py --renko size=35,align=10.0 --plot --dual
输出
The strechting and compression of time periods is now a lot more obvious. One other factor to consider is how the focus on the price action also has an effect on the indicators which are applied, as shown by the two RSI
indicators which have been placed on the chart.
现在对时间段的限制与压缩更加明显了。另一个需要考虑的因素是,对价格行为的关注如何也会对所应用的指标产生影响,如图表上的两个RSI指标所示。
Sample Usage
$ ./renko.py --help usage: renko.py [-h] [--data0 DATA0] [--fromdate FROMDATE] [--todate TODATE] [--cerebro kwargs] [--broker kwargs] [--sizer kwargs] [--strat kwargs] [--plot [kwargs]] [--renko kwargs] [--dual] Renko bricks sample optional arguments: -h, --help show this help message and exit --data0 DATA0 Data to read in (default: ../../datas/2005-2006-day-001.txt) --fromdate FROMDATE Date[time] in YYYY-MM-DD[THH:MM:SS] format (default: ) --todate TODATE Date[time] in YYYY-MM-DD[THH:MM:SS] format (default: ) --cerebro kwargs kwargs in key=value format (default: ) --broker kwargs kwargs in key=value format (default: ) --sizer kwargs kwargs in key=value format (default: ) --strat kwargs kwargs in key=value format (default: ) --plot [kwargs] kwargs in key=value format (default: ) --renko kwargs kwargs in key=value format (default: ) --dual put the filter on a second version of the data (default: False)
Sample Code
from __future__ import (absolute_import, division, print_function, unicode_literals) import argparse import datetime import backtrader as bt class St(bt.Strategy): params = dict( ) def __init__(self): for d in self.datas: bt.ind.RSI(d) def next(self): pass def runstrat(args=None): args = parse_args(args) cerebro = bt.Cerebro() # Data feed kwargs kwargs = dict() # Parse from/to-date dtfmt, tmfmt = '%Y-%m-%d', 'T%H:%M:%S' for a, d in ((getattr(args, x), x) for x in ['fromdate', 'todate']): if a: strpfmt = dtfmt + tmfmt * ('T' in a) kwargs[d] = datetime.datetime.strptime(a, strpfmt) data0 = bt.feeds.BacktraderCSVData(dataname=args.data0, **kwargs) fkwargs = dict() fkwargs.update(**eval('dict(' + args.renko + ')')) if not args.dual: data0.addfilter(bt.filters.Renko, **fkwargs) cerebro.adddata(data0) else: cerebro.adddata(data0) data1 = data0.clone() data1.addfilter(bt.filters.Renko, **fkwargs) cerebro.adddata(data1) # Broker cerebro.broker = bt.brokers.BackBroker(**eval('dict(' + args.broker + ')')) # Sizer cerebro.addsizer(bt.sizers.FixedSize, **eval('dict(' + args.sizer + ')')) # Strategy cerebro.addstrategy(St, **eval('dict(' + args.strat + ')')) # Execute kwargs = dict(stdstats=False) kwargs.update(**eval('dict(' + args.cerebro + ')')) cerebro.run(**kwargs) if args.plot: # Plot if requested to kwargs = dict(style='candle') kwargs.update(**eval('dict(' + args.plot + ')')) cerebro.plot(**kwargs) def parse_args(pargs=None): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=( 'Renko bricks sample' ) ) parser.add_argument('--data0', default='../../datas/2005-2006-day-001.txt', required=False, help='Data to read in') # Defaults for dates parser.add_argument('--fromdate', required=False, default='', help='Date[time] in YYYY-MM-DD[THH:MM:SS] format') parser.add_argument('--todate', required=False, default='', help='Date[time] in YYYY-MM-DD[THH:MM:SS] format') parser.add_argument('--cerebro', required=False, default='', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--broker', required=False, default='', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--sizer', required=False, default='', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--strat', required=False, default='', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--plot', required=False, default='', nargs='?', const='{}', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--renko', required=False, default='', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--dual', required=False, action='store_true', help='put the filter on a second version of the data') return parser.parse_args(pargs) if __name__ == '__main__': runstrat()