Backtrader 文档学习- Broker - Cheat-On-Open

Backtrader 文档学习- Broker - Cheat-On-Open

1.概述

V1.9.44.116增加了Cheat On Open的支持。对于全押的人来说,这似乎是一个必需的功能,用bar的收盘价后进行计算,希望与开盘价相匹配。
当开盘价差距(上涨或下跌,取决于买入或卖出是否有效)以及现金不足,进行全仓操作时,这种情况下就会失败。迫使broker 退回操作。
尽管人们可以尝试用积极正向的指数方法来预测未来,需要预先加载数据,而数据并不总是可用的。

使用模式:

cerebro = bt.Cerebro(cheat_on_open=True)

  • 在系统中激活一个额外的循环,该循环调用策略中的方法next_open、nextstart_open和prenext_open。
    需要增加一组新的定价方法,明确区分常规定价方法和cheat定价方法,常规定价方法是在被检查价格不再可用且未来不可知的情况下运作的。
    这也避免了对常规next方法进行2次调用。
    当在xxx_open方法内部时,以下内容是正确的:
  • 指标尚未重新计算,并保留了在等效的xxx常规方法中上次循环中得到的值。
  • broker尚未评估新循环的挂单,可以引入新订单,如果可能将对其进行评估。
    请注意:
  • Cerebro还有一个broker_coo(默认值:True)参数,告诉cerebro如果激活了作弊模式,则在可能的情况下也应尝试在broker中激活它。
    模拟broker有一个名为:coo的参数和一个名为set_coo的方法。

2.尝试开盘作弊

(1)核心代码
    # 买入卖出点操作
    def operate(self, fromopen):
        # 如有挂起的订单,返回,无操作
        if self.order is not None:
            return
        
        # 如有仓位
        if self.position:
            # 信号点小于0 ,平仓
            if self.signal < 0:
                self.order = self.close()
        
        # 信号点大于0
        elif self.signal > 0:
            print('{} Send Buy, fromopen {}, close {}'.format(
                self.data.datetime.date(),
                fromopen, self.data.close[0])
            )
            # 买入
            self.order = self.buy()

    # 正常交易 cheat-on-open = False
    def next(self):
        print('{} next, open {} close {}'.format(
            self.data.datetime.date(),
            self.data.open[0], self.data.close[0])
        )

        if self.cheating:
            return
        self.operate(fromopen=False)

    # 作弊交易 cheat-on-open = True
    def next_open(self):
        if not self.cheating:
            return
        self.operate(fromopen=True)

(2)cheat_on_open=False

常规执行:

python ./cheat-on-open.py --cerebro cheat_on_open=False
2005-02-14 next, open 3079.93 close 3075.76
2005-02-15 next, open 3075.2 close 3086.95
2005-02-16 next, open 3087.3 close 3068.55
2005-02-17 next, open 3068.79 close 3067.34
2005-02-18 next, open 3067.26 close 3072.04
2005-02-21 next, open 3072.31 close 3063.64
2005-02-22 next, open 3062.99 close 3045.24
2005-02-23 next, open 3042.65 close 3028.08
2005-02-24 next, open 3030.17 close 3024.8
2005-02-25 next, open 3029.07 close 3062.72
2005-02-28 next, open 3063.85 close 3058.35
2005-03-01 next, open 3056.45 close 3078.44
2005-03-02 next, open 3078.89 close 3082.71
2005-03-03 next, open 3080.71 close 3078.11
2005-03-04 next, open 3079.93 close 3106.86
2005-03-07 next, open 3106.98 close 3114.54
2005-03-08 next, open 3113.82 close 3097.34
2005-03-09 next, open 3098.91 close 3081.99
2005-03-10 next, open 3079.01 close 3053.62
2005-03-11 next, open 3058.37 close 3060.36
2005-03-14 next, open 3060.06 close 3060.72
2005-03-15 next, open 3062.77 close 3083.73
2005-03-16 next, open 3083.33 close 3032.13
2005-03-17 next, open 3032.84 close 3039.8
2005-03-18 next, open 3040.38 close 3053.54
2005-03-21 next, open 3052.39 close 3038.14
2005-03-22 next, open 3040.55 close 3050.44
2005-03-23 next, open 3040.66 close 3036.85
2005-03-24 next, open 3039.55 close 3060.67
2005-03-29 next, open 3060.02 close 3068.49
2005-03-30 next, open 3067.3 close 3056.21
2005-03-31 next, open 3059.1 close 3055.73
2005-04-01 next, open 3055.18 close 3061.11
2005-04-04 next, open 3060.0 close 3042.17
2005-04-05 next, open 3046.56 close 3064.07
2005-04-06 next, open 3066.05 close 3076.23
2005-04-07 next, open 3073.4 close 3090.72
2005-04-08 next, open 3092.07 close 3088.92
2005-04-08 Send Buy, fromopen False, close 3088.92
2005-04-11 Buy Executed at price 3088.47
2005-04-11 next, open 3088.47 close 3080.6

订单:

  • 在2005-04-08收盘后,以3088.92收盘价发布订单
  • 在2005-04-11以3088.47的开盘价执行订单
    看一下当时数据:

2005-04-07,3073.40,3092.99,3070.02,3090.72,0,0
2005-04-08,3092.07,3100.72,3083.87,3088.92,0,0
2005-04-11,3088.47,3088.47,3073.75,3080.60,0,0

图示:
Backtrader 文档学习- Broker - Cheat-On-Open_第1张图片

(3)cheat_on_open=True

作弊执行:

python ./cheat-on-open.py --cerebro cheat_on_open=True
2005-02-14 next, open 3079.93 close 3075.76
2005-02-15 next, open 3075.2 close 3086.95
2005-02-16 next, open 3087.3 close 3068.55
2005-02-17 next, open 3068.79 close 3067.34
2005-02-18 next, open 3067.26 close 3072.04
2005-02-21 next, open 3072.31 close 3063.64
2005-02-22 next, open 3062.99 close 3045.24
2005-02-23 next, open 3042.65 close 3028.08
2005-02-24 next, open 3030.17 close 3024.8
2005-02-25 next, open 3029.07 close 3062.72
2005-02-28 next, open 3063.85 close 3058.35
2005-03-01 next, open 3056.45 close 3078.44
2005-03-02 next, open 3078.89 close 3082.71
2005-03-03 next, open 3080.71 close 3078.11
2005-03-04 next, open 3079.93 close 3106.86
2005-03-07 next, open 3106.98 close 3114.54
2005-03-08 next, open 3113.82 close 3097.34
2005-03-09 next, open 3098.91 close 3081.99
2005-03-10 next, open 3079.01 close 3053.62
2005-03-11 next, open 3058.37 close 3060.36
2005-03-14 next, open 3060.06 close 3060.72
2005-03-15 next, open 3062.77 close 3083.73
2005-03-16 next, open 3083.33 close 3032.13
2005-03-17 next, open 3032.84 close 3039.8
2005-03-18 next, open 3040.38 close 3053.54
2005-03-21 next, open 3052.39 close 3038.14
2005-03-22 next, open 3040.55 close 3050.44
2005-03-23 next, open 3040.66 close 3036.85
2005-03-24 next, open 3039.55 close 3060.67
2005-03-29 next, open 3060.02 close 3068.49
2005-03-30 next, open 3067.3 close 3056.21
2005-03-31 next, open 3059.1 close 3055.73
2005-04-01 next, open 3055.18 close 3061.11
2005-04-04 next, open 3060.0 close 3042.17
2005-04-05 next, open 3046.56 close 3064.07
2005-04-06 next, open 3066.05 close 3076.23
2005-04-07 next, open 3073.4 close 3090.72
2005-04-08 next, open 3092.07 close 3088.92
2005-04-11 Send Buy, fromopen True, close 3080.6
2005-04-11 Buy Executed at price 3088.47
2005-04-11 next, open 3088.47 close 3080.6

对比当时数据:

2005-04-07,3073.40,3092.99,3070.02,3090.72,0,0
2005-04-08,3092.07,3100.72,3083.87,3088.92,0,0
2005-04-11,3088.47,3088.47,3073.75,3080.60,0,0

  • 在2005-04-11开盘前发布
  • 在2005-04-11以3088.47的开盘价执行

注意区别:

  • 4月8日未触发订单
  • 4月11日触发订单,当天成交订单。
  • 对于交易结果是一样的。

图示:
Backtrader 文档学习- Broker - Cheat-On-Open_第2张图片
结论:
开盘欺骗允许在开盘前发布订单,允许对全仓场景的所有投注进行精确计算。

4. 源码

#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2023 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import argparse
import datetime

import backtrader as bt


class St(bt.Strategy):
    params = dict(
        periods=[10, 30],
        matype=bt.ind.SMA,
    )

    def __init__(self):
        self.cheating = self.cerebro.p.cheat_on_open
        mas = [self.p.matype(period=x) for x in self.p.periods]
        self.signal = bt.ind.CrossOver(*mas)
        self.order = None

    def notify_order(self, order):
        if order.status != order.Completed:
            return

        self.order = None
        print('{} {} Executed at price {}'.format(
            bt.num2date(order.executed.dt).date(),
            'Buy' * order.isbuy() or 'Sell', order.executed.price)
        )

    def operate(self, fromopen):
        if self.order is not None:
            return
        if self.position:
            if self.signal < 0:
                self.order = self.close()
        elif self.signal > 0:
            print('{} Send Buy, fromopen {}, close {}'.format(
                self.data.datetime.date(),
                fromopen, self.data.close[0])
            )
            self.order = self.buy()

    def next(self):
        print('{} next, open {} close {}'.format(
            self.data.datetime.date(),
            self.data.open[0], self.data.close[0])
        )

        if self.cheating:
            return
        self.operate(fromopen=False)

    def next_open(self):
        if not self.cheating:
            return
        self.operate(fromopen=True)


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=(
            'Cheat-On-Open 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 ./cheat-on-open.py --help
usage: cheat-on-open.py [-h] [--data0 DATA0] [--fromdate FROMDATE]
                        [--todate TODATE] [--cerebro kwargs] [--broker kwargs]
                        [--sizer kwargs] [--strat kwargs] [--plot [kwargs]]

Cheat-On-Open 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: )

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