Backtrader 文档学习-Order StopTrail(Limit)

Backtrader 文档学习-Order StopTrail(Limit)

1.概述

版本1.9.36.116之后支持[StopTrail, StopTrailLimit and OCO]的订单类型,并支持broker的实时交互 。
StopTrail订单,它是一种追踪止损订单。当市场价格朝定义的交易方向移动时,StopTrail订单会自动调整止损价,以便保护您的利润。当市场价格达到您的止损价格时,订单将被触发并执行。类似于追踪止盈的概念。

  • OCO:始终将第一笔order作为oco的参数。
  • StopTrailLimit:broker模拟和IB broker具有相同的行为。指定:价格作为初始停止触发价格(还要指定trailamount),然后plimit作为初始限价。两者之间的差异将确定limitoffset(限价与停止触发价格保持的距离)。

使用模式完全集成到策略实例的标准buy、sell和close市场操作方法中。注意:

  • 指标希望的执行类型,如exectype=bt.Order.StopTrail
  • 跟踪价格必须使用固定距离,或使用基于百分比的距离计算
    • 固定距离:trailamount=10
    • 基于距离的百分比:trailpercent=0.02(即:2%)

如果通过发出买单进入多头市场,则使用StopTrail和trailamount的卖单如下所示:

  • 如果未指定price,则使用最新的收盘价
  • trailamount从price中减去以找到stop价格(或触发价格) stop = price - trailamount
  • next 的迭代中,broker检查触发价格是否已达到
    • 如果是:订单将使用Market执行类型方法执行
    • 如果不是,则通过使用最新的close并减去trailamount距离来重新计算停止价格
    • 如果新价格上涨,则更新
    • 如果新价格会下降(或根本不会改变),则会被丢弃
      就是说:通过buy进入市场,跟踪止损价格向上跟随价格,但如果价格开始下跌,则保持不变,以潜在地确保利润。
      如果通过sell进入市场,发出带有StopTrail的买单只是做相反的事情,即:价格向下跟随。
  • 通过buy进入市场,跟踪止损价格向上跟随价格
  • 通过sell进入市场,跟踪止损价格向下跟随价格

2.使用模式

# For a StopTrail going downwards
# last price will be used as reference
self.buy(size=1, exectype=bt.Order.StopTrail, trailamount=0.25)
# or
self.buy(size=1, exectype=bt.Order.StopTrail, price=10.50, trailamount=0.25)

# For a StopTrail going upwards
# last price will be used as reference
self.sell(size=1, exectype=bt.Order.StopTrail, trailamount=0.25)
# or
self.sell(size=1, exectype=bt.Order.StopTrail, price=10.50, trailamount=0.25)

可以指定trailpercent而不是trailamount,距离价格的距离将被计算为价格的百分比

# For a StopTrail going downwards with 2% distance
# last price will be used as reference
self.buy(size=1, exectype=bt.Order.StopTrail, trailpercent=0.02)
# or
self.buy(size=1, exectype=bt.Order.StopTrail, price=10.50, trailpercent=0.02)

# For a StopTrail going upwards with 2% difference
# last price will be used as reference
self.sell(size=1, exectype=bt.Order.StopTrail, trailpercent=0.02)
# or
self.sell(size=1, exectype=bt.Order.StopTrail, price=10.50, trailpercent=0.02)

3.示例

使用StopTrailLimit:

  • 唯一区别在于当触发跟踪止损价格时会发生什么。
    In this case the order is executed as a Limit order (the same behavior a StopLimit order has, but in this case with a dynamic triggering price)
    在这个例子中,order将作为Limit 订单执行(与StopLimit订单具有相同的行为,但在此情况下是动态的触发价格)。

注意:必须指定plimit=x.x来买或卖,这将是限价
注意:限价不像stop/trigger价格那样动态改变

(1)trailamount=50.0

An example is always worth a thousand words and hence the usual backtrader sample, which
好例子胜过千言万语,举例说明,它是一个标准的backtrader示例:

  • 使用移动均线交叉来进入市场
  • 使用跟踪止损来退出市场。
    执行50点的固定价格距离 :
    核心代码:
            if self.p.trailamount:
                tcheck = self.data.close - self.p.trailamount
            else:
                tcheck = self.data.close * (1.0 - self.p.trailpercent)
            print(','.join(
                map(str, [self.datetime.date(), self.data.close[0],
                          self.order.created.price, tcheck])
                )
            )
            print('-' * 10)
        else:
            if self.p.trailamount:
                tcheck = self.data.close - self.p.trailamount
            else:
                tcheck = self.data.close * (1.0 - self.p.trailpercent)
            print(','.join(
                map(str, [self.datetime.date(), self.data.close[0],
                          self.order.created.price, tcheck])
                )
            )

执行:

python ./trail.py --plot --strat trailamount=50.0

Backtrader 文档学习-Order StopTrail(Limit)_第1张图片
输出:

**************************************************
2005-02-14,3075.76,3025.76,3025.76
----------
2005-02-15,3086.95,3036.95,3036.95
2005-02-16,3068.55,3036.95,3018.55
2005-02-17,3067.34,3036.95,3017.34
2005-02-18,3072.04,3036.95,3022.04
2005-02-21,3063.64,3036.95,3013.64
...
...
**************************************************
2005-05-19,3051.79,3001.79,3001.79
----------
2005-05-20,3050.45,3001.79,3000.45
2005-05-23,3070.98,3020.98,3020.98
2005-05-24,3066.55,3020.98,3016.55
2005-05-25,3059.84,3020.98,3009.84
2005-05-26,3086.08,3036.08,3036.08
2005-05-27,3084.0,3036.08,3034.0
2005-05-30,3096.54,3046.54,3046.54
2005-05-31,3076.75,3046.54,3026.75
2005-06-01,3125.88,3075.88,3075.88
2005-06-02,3131.03,3081.03,3081.03
2005-06-03,3114.27,3081.03,3064.27
2005-06-06,3099.2,3081.03,3049.2
2005-06-07,3134.82,3084.82,3084.82
2005-06-08,3125.59,3084.82,3075.59
2005-06-09,3122.93,3084.82,3072.93
2005-06-10,3143.85,3093.85,3093.85
2005-06-13,3159.83,3109.83,3109.83
2005-06-14,3162.86,3112.86,3112.86
2005-06-15,3147.55,3112.86,3097.55
2005-06-16,3160.09,3112.86,3110.09
2005-06-17,3178.48,3128.48,3128.48
2005-06-20,3162.14,3128.48,3112.14
2005-06-21,3179.62,3129.62,3129.62
2005-06-22,3182.08,3132.08,3132.08
2005-06-23,3190.8,3140.8,3140.8
2005-06-24,3161.0,3140.8,3111.0
...
...
...
**************************************************
2006-12-19,4100.48,4050.48,4050.48
----------
2006-12-20,4118.54,4068.54,4068.54
2006-12-21,4112.1,4068.54,4062.1
2006-12-22,4073.5,4068.54,4023.5
2006-12-27,4134.86,4084.86,4084.86
2006-12-28,4130.66,4084.86,4080.66
2006-12-29,4119.94,4084.86,4069.94

输出说明:

3075.76 +0.5 =3076.26
打印输入格式: 日期, 收盘价 ,price限价 ,stoplimit价格=self.data.close - self.p.trailamount
而不是等待通常的交叉向下模式,系统使用跟踪止损退出市场。看第一个操作的例子:

  • 收盘价格进入多头:3075.76
  • 系统计算的跟踪止损价格:3025.76(距离50个单位)
  • 示例计算的跟踪止损价格:3025.76(每行显示的最后价格)

这之后第一次计算:

  • 收盘价上涨到3086.95,止损价格调整为3036.95
  • 接下来的收盘价没有超过3086.95,触发价格没有改变
(2)trailamount=30.0

同样的模式可以在另外两个操作中看到。
为了便于比较,只有30点固定距离的执行(只有图表)

python ./trail.py --plot --strat trailamount=30.0

Backtrader 文档学习-Order StopTrail(Limit)_第2张图片

(3)trailpercent=0.02

按百分比2%

$ ./trail.py --plot --strat trailpercent=0.02

图示:
Backtrader 文档学习-Order StopTrail(Limit)_第3张图片

4.代码

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import argparse
import datetime

import backtrader as bt


class St(bt.Strategy):
    params = dict(
        ma=bt.ind.SMA,
        p1=10,
        p2=30,
        stoptype=bt.Order.StopTrail,
        trailamount=0.0,
        trailpercent=0.0,
    )

    def __init__(self):
        ma1, ma2 = self.p.ma(period=self.p.p1), self.p.ma(period=self.p.p2)
        self.crup = bt.ind.CrossUp(ma1, ma2)
        self.order = None

    def next(self):
        if not self.position:
            if self.crup:
                o = self.buy()
                self.order = None
                print('*' * 50)

        elif self.order is None:
            self.order = self.sell(exectype=self.p.stoptype,
                                   trailamount=self.p.trailamount,
                                   trailpercent=self.p.trailpercent)

            if self.p.trailamount:
                tcheck = self.data.close - self.p.trailamount
            else:
                tcheck = self.data.close * (1.0 - self.p.trailpercent)
            print(','.join(
                map(str, [self.datetime.date(), self.data.close[0],
                          self.order.created.price, tcheck])
                )
            )
            print('-' * 10)
        else:
            if self.p.trailamount:
                tcheck = self.data.close - self.p.trailamount
            else:
                tcheck = self.data.close * (1.0 - self.p.trailpercent)
            print(','.join(
                map(str, [self.datetime.date(), self.data.close[0],
                          self.order.created.price, tcheck])
                )
            )


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)

    # Data feed
    data0 = bt.feeds.BacktraderCSVData(dataname=args.data0, **kwargs)
    cerebro.adddata(data0)

    # 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
    cerebro.run(**eval('dict(' + args.cerebro + ')'))

    if args.plot:  # Plot if requested to
        cerebro.plot(**eval('dict(' + args.plot + ')'))


def parse_args(pargs=None):
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=(
            'StopTrail 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')

    return parser.parse_args(pargs)


if __name__ == '__main__':
    runstrat()

5.Help

python ./trail.py --help
usage: trail.py [-h] [--data0 DATA0] [--fromdate FROMDATE] [--todate TODATE]
                [--cerebro kwargs] [--broker kwargs] [--sizer kwargs]
                [--strat kwargs] [--plot [kwargs]]

StopTrail 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: )

6. Futures and Spot Compensation

期货和现货补偿 ,不了解期货业务,暂时不学习。

你可能感兴趣的:(BackTrader,数据分析,人工智能,量化,BackTrader)