python自动化交易学习笔记(四)第一个策略回测程序Demo7(添加买入条件-当股价连续下跌2天后买入)

Demo6中自定义了一个策略,但未设置买入卖出条件,Demo7中将添加买入条件(当日收盘价小于前一日收盘价)

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])
        # 当日收盘价小于前一日收盘价
        if self.dataclose[0] < self.dataclose[-1]:
            # 前一日收盘价小于前前日收盘价
            if self.dataclose[-1] < self.dataclose[-2]:
                # 买买买
                self.log('买入开仓, %.2f' % self.dataclose[0])
                self.buy()

# 创建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('初始投资组合价值: %.2f' % cerebro.broker.getvalue())
# 遍历所有数据
cerebro.run()
# 打印最后结果
print('最终投资组合价值: %.2f' % cerebro.broker.getvalue())

 

加入了简单的买入逻辑:当股价连续下跌2天后买入,只需要在next方法中添加相关代码即可。从程序输出中可以看到会有一些买入的操作。当前的程序中尚未提及订单是否被成功交易,在下一个程序中将通过侦听订单状态提醒来判断交易是否执行。

此外,还有如下需要说明的地方:

如果没有特殊指明,self.datas[0]就是被操作的股票,这里操作的就是卧龙地产(601703)。

交易的数量(stake)在后台用position sizer来表示,被赋予一个固定的值,默认值为1。后续程序将展示如果进行设置。
在市场上执行交易订单时,代理会用第二天的开盘价来判断订单是否执行成功,这是因为第二天的开盘价是判断当日收盘价后的第一时刻数据。
目前订单尚未加入佣金费数据。

 

C:\Python38\python.exe F:/test/test/Demo7.py
初始投资组合价值: 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-11, 信号出现(买买买)-买入-开仓, 5.24
2019-10-14, Close, 5.23
2019-10-14, 信号出现(买买买)-买入-开仓, 5.23
2019-10-15, Close, 5.17
2019-10-15, 信号出现(买买买)-买入-开仓, 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-21, 信号出现(买买买)-买入-开仓, 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-10-31, 信号出现(买买买)-买入-开仓, 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-06, 信号出现(买买买)-买入-开仓, 5.12
2019-11-07, Close, 5.15
2019-11-08, Close, 5.12
2019-11-11, Close, 5.02
2019-11-11, 信号出现(买买买)-买入-开仓, 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-18, 信号出现(买买买)-买入-开仓, 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-22, 信号出现(买买买)-买入-开仓, 4.95
2019-11-25, Close, 4.98
2019-11-26, Close, 4.95
2019-11-27, Close, 4.92
2019-11-27, 信号出现(买买买)-买入-开仓, 4.92
2019-11-28, Close, 4.89
2019-11-28, 信号出现(买买买)-买入-开仓, 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-20, 信号出现(买买买)-买入-开仓, 5.00
2019-12-23, Close, 4.95
2019-12-23, 信号出现(买买买)-买入-开仓, 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-07, 信号出现(买买买)-买入-开仓, 5.22
2020-01-08, Close, 5.08
2020-01-08, 信号出现(买买买)-买入-开仓, 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-15, 信号出现(买买买)-买入-开仓, 5.11
2020-01-16, Close, 5.06
2020-01-16, 信号出现(买买买)-买入-开仓, 5.06
2020-01-17, Close, 5.01
2020-01-17, 信号出现(买买买)-买入-开仓, 5.01
2020-01-20, Close, 4.99
2020-01-20, 信号出现(买买买)-买入-开仓, 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-03, 信号出现(买买买)-买入-开仓, 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-11, 信号出现(买买买)-买入-开仓, 4.70
2020-02-12, Close, 4.77
2020-02-13, Close, 4.68
2020-02-14, Close, 4.66
2020-02-14, 信号出现(买买买)-买入-开仓, 4.66
2020-02-17, Close, 4.75
2020-02-18, Close, 4.67
2020-02-19, Close, 4.64
2020-02-19, 信号出现(买买买)-买入-开仓, 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
最终投资组合价值: 99997.03

Process finished with exit code 0

 

  ————————————————

参考:https://blog.csdn.net/m0_46603114/article/details/104972462

posted @ 2021-06-17 15:25  A汉克先生  阅读(408)  评论(0编辑  收藏  举报