• python量化交易学习笔记(三)——第一个策略回测程序Demo6


    C:Python38python.exe F:/test/test/demo6.py
    Starting Portfolio Value: 100000.00
    2019-10-08, Close, 5.22
    2019-10-09, Close, 5.27
    2019-10-10, Close, 5.26
    2019-10-11, Close, 5.24
    2019-10-14, Close, 5.23
    2019-10-15, Close, 5.17
    2019-10-16, Close, 5.20
    2019-10-17, Close, 5.25
    2019-10-18, Close, 5.12
    2019-10-21, Close, 5.10
    2019-10-22, Close, 5.25
    2019-10-23, Close, 5.23
    2019-10-24, Close, 5.29
    2019-10-25, Close, 5.29
    2019-10-28, Close, 5.22
    2019-10-29, Close, 5.23
    2019-10-30, Close, 5.17
    2019-10-31, Close, 5.12
    2019-11-01, Close, 5.23
    2019-11-04, Close, 5.24
    2019-11-05, Close, 5.22
    2019-11-06, Close, 5.12
    2019-11-07, Close, 5.15
    2019-11-08, Close, 5.12
    2019-11-11, Close, 5.02
    2019-11-12, Close, 5.02
    2019-11-13, Close, 5.00
    2019-11-14, Close, 5.07
    2019-11-15, Close, 5.00
    2019-11-18, Close, 4.94
    2019-11-19, Close, 5.05
    2019-11-20, Close, 5.07
    2019-11-21, Close, 5.00
    2019-11-22, Close, 4.95
    2019-11-25, Close, 4.98
    2019-11-26, Close, 4.95
    2019-11-27, Close, 4.92
    2019-11-28, Close, 4.89
    2019-11-29, Close, 4.91
    2019-12-02, Close, 4.91
    2019-12-03, Close, 4.95
    2019-12-04, Close, 4.94
    2019-12-05, Close, 5.05
    2019-12-06, Close, 5.10
    2019-12-09, Close, 5.10
    2019-12-10, Close, 5.03
    2019-12-11, Close, 5.06
    2019-12-12, Close, 5.02
    2019-12-13, Close, 5.03
    2019-12-16, Close, 5.01
    2019-12-17, Close, 5.09
    2019-12-18, Close, 5.10
    2019-12-19, Close, 5.06
    2019-12-20, Close, 5.00
    2019-12-23, Close, 4.95
    2019-12-24, Close, 4.98
    2019-12-25, Close, 5.20
    2019-12-26, Close, 5.26
    2019-12-27, Close, 5.16
    2019-12-30, Close, 5.18
    2019-12-31, Close, 5.21
    2020-01-02, Close, 5.21
    2020-01-03, Close, 5.27
    2020-01-06, Close, 5.23
    2020-01-07, Close, 5.22
    2020-01-08, Close, 5.08
    2020-01-09, Close, 5.24
    2020-01-10, Close, 5.21
    2020-01-13, Close, 5.21
    2020-01-14, Close, 5.17
    2020-01-15, Close, 5.11
    2020-01-16, Close, 5.06
    2020-01-17, Close, 5.01
    2020-01-20, Close, 4.99
    2020-01-21, Close, 4.99
    2020-01-22, Close, 4.99
    2020-01-23, Close, 4.88
    2020-02-03, Close, 4.39
    2020-02-04, Close, 4.43
    2020-02-05, Close, 4.43
    2020-02-06, Close, 4.66
    2020-02-07, Close, 4.73
    2020-02-10, Close, 4.72
    2020-02-11, Close, 4.70
    2020-02-12, Close, 4.77
    2020-02-13, Close, 4.68
    2020-02-14, Close, 4.66
    2020-02-17, Close, 4.75
    2020-02-18, Close, 4.67
    2020-02-19, Close, 4.64
    2020-02-20, Close, 4.66
    2020-02-21, Close, 4.77
    2020-02-24, Close, 4.70
    2020-02-25, Close, 4.73
    2020-02-26, Close, 4.85
    2020-02-27, Close, 4.86
    2020-02-28, Close, 4.84
    Final Portfolio Value: 100000.00
    
    Process finished with exit code 0
    

      

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    import datetime  # 用于datetime对象操作
    import os.path  # 用于管理路径
    import sys  # 用于在argvTo[0]中找到脚本名称
    import backtrader as bt # 引入backtrader框架
    
    # 创建策略
    class TestStrategy(bt.Strategy):
        def log(self, txt, dt=None):
            ''' 策略的日志函数'''
            dt = dt or self.datas[0].datetime.date(0)
            print('%s, %s' % (dt.isoformat(), txt))
        def __init__(self):
            # 引用data[0]数据的收盘价数据
            self.dataclose = self.datas[0].close
        def next(self):
            # 日志输出收盘价数据
            self.log('Close, %.2f' % self.dataclose[0])
    
    # 创建cerebro实体
    cerebro = bt.Cerebro()
    # 添加策略
    cerebro.addstrategy(TestStrategy)
    # 先找到脚本的位置,然后根据脚本与数据的相对路径关系找到数据位置
    # 这样脚本从任意地方被调用,都可以正确地访问到数据
    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = os.path.join(modpath, 'F:/GZH/自动化交易/历史数据/sh.600173history_k_data2021-12-31-2021-12-31.csv')
    # 创建价格数据
    data = bt.feeds.GenericCSVData(
        dataname = datapath,
        fromdate = datetime.datetime(2019, 10, 1),
        todate = datetime.datetime(2020, 2, 29),
        nullvalue = 0.0,
        dtformat = ('%Y/%m/%d'),
        datetime = 0,
        open = 1,
        high = 2,
        low = 3,
        close = 4,
        volume = 5,
        openinterest = -1
    )
    # 在Cerebro中添加价格数据
    cerebro.adddata(data)
    # 设置启动资金
    cerebro.broker.setcash(100000.0)
    # 打印开始信息
    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    # 遍历所有数据
    cerebro.run()
    # 打印最后结果
    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    

      程序中创建了一个backtrader.Strategy的子类,并将其添加至cerebro中。这里实际就是新建了一个自定义的空策略,在这个策略里可以添加买入卖出条件,供框架进行回测。当前的策略只是做了按天输出收盘价格。

    几点简单的解释:

    当__init__方法被调用时,策略就有了一个数据列表,这个列表是标准的Python语言列表,存储的是按顺序加载的数据
    self.dataclose = self.datas[0].close引用列表中的收盘价数据,用于后续交易
    next方法在每个K线数据上都会被调用
    ————————————————
    参考:https://blog.csdn.net/m0_46603114/article/details/104971989

  • 相关阅读:
    杭州电子科技大学程序设计竞赛(2016’12)- 网络同步赛 1001
    AtCoder Beginner Contest 050 ABC题
    2016年第四届湘潭大学新生趣味程序设计竞赛
    华东交通大学2016年ACM“双基”程序设计竞赛 1008
    移动端报表JS开发示例
    unity shader入门
    现代控制理论思考题----倒立摆小车控制算法研究
    Linux驱动基础:msm平台,modem等framework加载
    简谈高通Trustzone的实现
    现代控制理论课件分享及课后思考题(初稿待完善)
  • 原文地址:https://www.cnblogs.com/gzhbk/p/14893510.html
Copyright © 2020-2023  润新知