量化交易回测框架Backtrader给策略增加买卖手续费

简介

一般交易,交易所都要收手续费,这次在策略里面卖买的同时增加了手续费。具体可以参看Backtrader官方文档quickstart

原理

broker是经纪人/交易所的角色,通过broker.setcommission方法设置手续费

实践

实现目标:

  1. 买卖还是和以前的一样,手续费设置为:千一 0.001
  2. 显示出每次交易的有手续费和没有手续费的成交价格
  3. 显示每次教程成功后,毛利(gross price)和净利(net price)
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 29 12:18:17 2020

@author: horace pei
"""
#############################################################
#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
        # buy price
        self.buyprice = None
        # buy commission
        self.buycomm = None
    
    #订单状态改变回调方法 be notified through notify_order(order) of any status change in an order
    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, Price: %.2f, Cost: %.2f, Comm %.2f' %
                    (order.executed.price,
                     order.executed.value,
                     order.executed.comm))
                self.buyprice = order.executed.price
                self.buycomm = order.executed.comm
            elif order.issell():
               self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                         (order.executed.price,
                          order.executed.value,
                          order.executed.comm))
            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

    #交易状态改变回调方法 be notified through notify_trade(trade) of any opening/updating/closing trade
    def notify_trade(self, trade):
        if not trade.isclosed:
            return
        # 每笔交易收益 毛利和净利
        self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                 (trade.pnl, trade.pnlcomm))

    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(当前账户持股情况,size,price等等)
        if not self.position:
            # Not yet ... we MIGHT BUY if ...
            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!!! (with default parameters)
                        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
            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
#############################################################
def get_dataframe():
     # Get a pandas dataframe
    datapath = './data/stockinfo.csv'
    tmpdatapath = './data/stockinfo_tmp.csv'
    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")
    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)
    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.adddata(data)
    # Set our desired cash start(给经纪人,可以理解为交易所股票账户充钱)
    cerebro.broker.setcash(100000.0)
     # Set the commission - 0.1%(设置交易手续费,双向收取)
    cerebro.broker.setcommission(commission=0.001)
    # 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())

分析:

买的输出:

2020-04-01, BUY CREATE, 5.21
2020-04-02, BUY EXECUTED, Price: 5.20, Cost: 5.20, Comm 0.01

4月1日,触发了买的动作,4月2日开盘价 5.20购买1股,成本价5.20,手续费0.01。
卖的输出:

2020-02-11, SELL CREATE, 5.38
2020-02-12, SELL EXECUTED, Price: 5.39, Cost: 5.00, Comm 0.01
2020-02-12, OPERATION PROFIT, GROSS 0.39, NET 0.38

2月11日,触发了卖的动作,2月12日以开盘价 5.39出售1股,成本价5.20,手续费0.01,
这次交易毛利是0.39,扣除手续费后,净利是0.38。

整体上还还是比较清晰的。

全代码

全代码请到github上clone了。github地址:[qtbt](https://github.com/horacepei/qtbt.git)

你可能感兴趣的:(量化交易回测框架Backtrader给策略增加买卖手续费)