学习通过策略买入,通过策略卖出也是不可或缺的。框架有3个功能为了我们提供的实现策略卖出基础:
1 可以通过偏移来访问数据集
2 通过买和卖的方法,实现生成对应的订单
3 如果订单的状态发生变化,策略还会调用notify方法,通知自己实现后续处理
这里先实现一个简单的策略:
无论涨跌,是否赚钱或者亏损,过5个交易日后卖出
为了简单命令,这里做一个约定,只有空仓的时候才能购买,如过有持股的情况下就不会执行买入
# -*- coding: utf-8 -*-
"""
backtrader手册样例代码
@author: 一块自由的砖
"""
#############################################################
#import
#############################################################
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import os,sys
import pandas as pd
import backtrader as bt
#############################################################
#global const values
#############################################################
#############################################################
#static function
#############################################################
#############################################################
#class
#############################################################
# Create a Stratey
class TestStrategy(bt.Strategy):
def log(self, txt, dt=None):
''' Logging function for this strategy,这里定义日志的显示格式'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries(获取收盘价)
self.dataclose = self.datas[0].close
# To keep track of pending orders
self.order = None
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enough cash
if order.status in [order.Completed]:
if order.isbuy():
self.log('BUY EXECUTED, %.2f' % order.executed.price)
elif order.issell():
self.log('SELL EXECUTED, %.2f' % order.executed.price)
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
# Write down: no pending order
self.order = None
def next(self):
# Simply log the closing price of the series from the reference(调用日志方法,显示收盘价)
self.log('Close, %.2f' % self.dataclose[0])
# Check if an order is pending ... if yes, we cannot send a 2nd one
if self.order:
return
# Check if we are in the market(检查是否持股)
if not self.position:
# 判定当前交易日的收盘价是否小于昨日的
if self.dataclose[0] < self.dataclose[-1]:
# current close less than previous close(判定昨日的收盘价是否小于前日的)
if self.dataclose[-1] < self.dataclose[-2]:
# previous close less than the previous close
# BUY, BUY, BUY!!! 触发购买动作
self.log('BUY CREATE, %.2f' % self.dataclose[0])
# Keep track of the created order to avoid a 2nd order
self.order = self.buy()
else:
# Already in the market ... we might sell,持股检查是否已经大于5个交易日
if len(self) >= (self.bar_executed + 5):
# SELL, SELL, SELL!!! (with all possible default parameters)
self.log('SELL CREATE, %.2f' % self.dataclose[0])
# Keep track of the created order to avoid a 2nd order
self.order = self.sell()
#############################################################
#global values
#############################################################
#############################################################
#global function
#############################################################
# 通过读取cvs文件,获取想要的数据
def get_dataframe():
# Get a pandas dataframe(这里是股票数据文件放的目录路径)
datapath = 'qtbt\data\stockinfo.csv'
# 数据转换用临时文件路径和名称,用完后删除
tmpdatapath = datapath + '.tmp'
print('-----------------------read csv---------------------------')
dataframe = pd.read_csv(datapath,
skiprows=0,
header=0,
parse_dates=True,
index_col=0)
# 定义交易日期格式
dataframe.trade_date = pd.to_datetime(dataframe.trade_date, format="%Y%m%d")
# 原始cvs数据有很多列,这里组织需要使用的数据列,生成目标数据的tmp数据文件后,读取需要的数据
dataframe['openinterest'] = '0'
feedsdf = dataframe[['trade_date', 'open', 'high', 'low', 'close', 'vol', 'openinterest']]
feedsdf.columns =['datetime', 'open', 'high', 'low', 'close', 'volume', 'openinterest']
# 按照交易日期升序排列
feedsdf.set_index(keys='datetime', inplace =True)
# 生成临时文件
feedsdf.iloc[::-1].to_csv(tmpdatapath)
# 获取需要使用的数据
feedsdf = pd.read_csv(tmpdatapath, skiprows=0, header=0, parse_dates=True, index_col=0)
# 删除tmp临时文件
if os.path.isfile(tmpdatapath):
os.remove(tmpdatapath)
print(tmpdatapath+" removed!")
# 返回需要的数据
return feedsdf
########################################################################
#main
########################################################################
if __name__ == '__main__':
# Create a cerebro entity(创建cerebro)
cerebro = bt.Cerebro()
# Add a strategy(加入自定义策略,可以设置自定义参数,方便调节)
cerebro.addstrategy(TestStrategy)
# Get a pandas dataframe(获取dataframe格式股票数据)
feedsdf = get_dataframe()
# Pass it to the backtrader datafeed and add it to the cerebro(加入数据)
data = bt.feeds.PandasData(dataname=feedsdf)
# 加入数据到Cerebro
cerebro.adddata(data)
# Add a FixedSize sizer according to the stake(国内1手是100股,最小的交易单位)
cerebro.addsizer(bt.sizers.FixedSize, stake=100)
# Set our desired cash start(给经纪人,可以理解为交易所股票账户充钱)
cerebro.broker.setcash(100000.0)
# Print out the starting conditions(输出账户金额)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
# Run over everything(执行回测)
cerebro.run()
# Print out the final result(输出账户金额)
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
执行后输出:
-----------------------read csv---------------------------
qtbt\data\stockinfo.csv.tmp removed!
Starting Portfolio Value: 100000.00
2020-01-02, Close, 12.47
2020-01-03, Close, 12.60
2020-01-06, Close, 12.46
2020-01-07, Close, 12.50
2020-01-08, Close, 12.32
2020-01-09, Close, 12.37
2020-01-10, Close, 12.39
2020-01-13, Close, 12.41
2020-01-14, Close, 12.43
2020-01-15, Close, 12.25
2020-01-16, Close, 12.20
2020-01-16, BUY CREATE, 12.20
2020-01-17, BUY EXECUTED, 12.22
...
...
2021-09-17, Close, 9.11
2021-09-22, Close, 9.03
2021-09-23, Close, 9.03
2021-09-24, Close, 9.02
2021-09-24, SELL CREATE, 9.02
2021-09-27, SELL EXECUTED, 9.02
2021-09-27, Close, 9.02
2021-09-28, Close, 9.03
2021-09-29, Close, 9.02
2021-09-30, Close, 9.00
2021-09-30, BUY CREATE, 9.00
Final Portfolio Value: 99810.00