AD:(本人录制的backtrader视频课程,大家多多支持哦~ https://edu.csdn.net/course/detail/9040)
无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。教程链接:https://www.cbedai.net/qtlyx
backtrader是一个量化策略的回测分析平台,功能还是很强大的。
安装很简单,和别的lib安装一模一样,pip install backtrader。
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import datetime # For datetime objects
import os.path # To manage paths
import sys # To find out the script name (in argv[0])
import pandas as pd
from WindPy import w
# Import the backtrader platform
import backtrader as bt
# Create a Stratey
class TestStrategy(bt.Strategy):
def log(self, txt, dt=None):
''' Logging function fot 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(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 enougth cash
if order.status in [order.Completed, order.Canceled, order.Margin]:
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)
# 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:
# 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()
if __name__ == '__main__':
# Create a cerebro entity
cerebro = bt.Cerebro()
# Add a strategy
cerebro.addstrategy(TestStrategy)
# Create a Data Feed
# 本地数据,笔者用Wind获取的东风汽车数据以csv形式存储在本地。
# parase_dates = True是为了读取csv为dataframe的时候能够自动识别datetime格式的字符串,big作为index
# 注意,这里最后的pandas要符合backtrader的要求的格式
dataframe = pd.read_csv('dfqc.csv', index_col=0, parse_dates=True)
dataframe['openinterest'] = 0
data = bt.feeds.PandasData(dataname=dataframe,
fromdate = datetime.datetime(2015, 1, 1),
todate = datetime.datetime(2016, 12, 31)
)
# Add the Data Feed to Cerebro
cerebro.adddata(data)
# 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())
# Plot the result
cerebro.plot()
上面这段代码整个框架来自于backtrader的官网(https://www.backtrader.com),笔者自己修改了获取数据的方式。大家安装完backtrader之后可以复制过去运行一样,一般来说,运行通过就说明环境没有问题(数据文件下文有提到)。接下来我们就分析一下这个不赚钱的策略。
我们想想,量化回测,首先得有数据。
在backtrader里面,DataFeed就是这样一个数据的概念。
dataframe = pd.read_csv('dfqc.csv', index_col=0, parse_dates=True)
dataframe['openinterest'] = 0
data = bt.feeds.PandasData(dataname=dataframe,
fromdate = datetime.datetime(2015, 1, 1),
todate = datetime.datetime(2016, 12, 31)
)
这一段代码从本地csv文件获得某一个股票的交易数据,包括open,close,high,low,volume。大家注意到,这里我们的存储形式的pandas。backtrader是支持pandas这样的一种数据格式的,无形中方便了很多
关于该数据文件,大家可以去笔者的百度云下载。
链接:https://pan.baidu.com/s/1slnULbv 密码:wi3k
后面的很多教程案例,都会用到这一数据。
backtrader对每种数据的来源都有一定的标准,要不然咱们相信,不订立标准,框架怎么会知道每一列数据都是干嘛的呢。backtrader对于pandas的标准就是这些列的名字得是open,close,high,low,volume,openinterest。我们这里没有用到openinterest,所以把它设置为零就可以了,这里提示一下,backtrader要求pandas下的DataFeed,pandas的DataFrame的index是时间。
数据的问题解决了,那么数据要交给回测平台。这里整个回测功能的实现都是依赖于cercbro这个类实现的。这个单词的意思,在西班牙语里就是大脑。
我们创造一个大脑,
# Create a cerebro entity
cerebro = bt.Cerebro()
然后把之前的数据喂给大脑:
# Add the Data Feed to Cerebro
cerebro.adddata(data)
基础建设基本就完成了。
接下来就是技术性的较量,策略啦。
class TestStrategy(bt.Strategy):
每一个策略都是一个类,是一个继承bt.Strategy类的父类。具体里面的细节,下次再说。
假设我们写好了一个策略,换句话说,就是构造好了一个Strategy类,那么我们就应该把它喂给大脑。
# Add a strategy
cerebro.addstrategy(TestStrategy)
好了,都齐全了。但是别忘了,我们还应该制定初始资金。
# Set our desired cash start
cerebro.broker.setcash(100000.0)
cerebro里面的broker成员就是管这事的。我们给了他100000元。
到这里,该有的都有了,我们run就可以了。cerebro.run()方法就是把上述东西跑起来。
跑完之后,我们想看一下效果,人嘛,可视化的最好,所以:
# Plot the result
cerebro.plot()
就能把结果跑出来。
很显然,亏钱了,但是不要紧,程序对了,万里长征第一步对了。
俗话说的好,万事开头难,中间也难,结尾更难。
最后总结一下,一个策略,你需要的是数据,而且要让回测平台认识,所以是满足一定规则整理的数据。然后就是回测的“脑子”。这些基础设施有了,写好你的策略。策略其实就是一个类,重写类里的方法就实现了一个个策略。接下来,run就可以了。so easy!