注意
pandas 并且必须安装其依赖项
支持Pandas Dataframes似乎受到很多人的关注,他们依赖于已经可用的解析代码来分析不同的数据源(包括CSV)和Pandas提供的其他功能。
数据馈送的重要声明。
注意
这些只是 声明。不要盲目拷贝此代码。请参阅以下示例中的实际用法
class PandasData(feed.DataBase):
'''
The ``dataname`` parameter inherited from ``feed.DataBase`` is the pandas
DataFrame
'''
params = (
# Possible values for datetime (must always be present)
# None : datetime is the "index" in the Pandas Dataframe
# -1 : autodetect position or case-wise equal name
# >= 0 : numeric index to the colum in the pandas dataframe
# string : column name (as index) in the pandas dataframe
('datetime', None),
# Possible values below:
# None : column not present
# -1 : autodetect position or case-wise equal name
# >= 0 : numeric index to the colum in the pandas dataframe
# string : column name (as index) in the pandas dataframe
('open', -1),
('high', -1),
('low', -1),
('close', -1),
('volume', -1),
('openinterest', -1),
)
上面的类摘录PandasData 显示了关键:
-
dataname实例化期间类的参数保存 Pandas 数据帧此参数继承自基类
feed.DataBase -
新参数在 中具有常规字段的名称,
DataSeries并遵循这些约定-
datetime(默认值:无) -
无:日期时间是熊猫数据帧中的“索引”
-
-1 :自动检测位置或大小写相等名称
-
= 0 : pandas 数据帧中 colum 的数字索引
-
字符串:熊猫数据帧中的列名(作为索引)
-
open、high、low、 、highclose、volumeopeninterest( 默认值:-1 表示所有 ) -
无:列不存在
-
-1 :自动检测位置或大小写相等名称
-
= 0 : pandas 数据帧中 colum 的数字索引
-
字符串:熊猫数据帧中的列名(作为索引)
-
一个小样本应该能够加载2006年标准样本,由Pandas而不是直接解析 backtrader
运行范例以使用 CSV 数据中离开的「标头」 :
$ ./panda-test.py
--------------------------------------------------
Open High Low Close Volume OpenInterest
Date
2006-01-02 3578.73 3605.95 3578.73 3604.33 0 0
2006-01-03 3604.08 3638.42 3601.84 3614.34 0 0
2006-01-04 3615.23 3652.46 3615.23 3652.46 0 0
相同,但告诉脚本跳过标头:
$ ./panda-test.py --noheaders
--------------------------------------------------
1 2 3 4 5 6
0
2006-01-02 3578.73 3605.95 3578.73 3604.33 0 0
2006-01-03 3604.08 3638.42 3601.84 3614.34 0 0
2006-01-04 3615.23 3652.46 3615.23 3652.46 0 0
第 2次 运行使用 tells pandas.read_csv:
-
跳过第一个输入列(
skiprows关键字参数设置为 1) -
不要寻找标题列(
header关键字参数设置为 None)
backtrader对 Pandas 的支持会尝试自动检测是否使用了列名或数字索引,并相应地采取行动,尝试提供最佳匹配。
下图是对成功的致敬。熊猫数据帧已正确加载(在这两种情况下)
测试的范例代码。
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
import backtrader as bt
import backtrader.feeds as btfeeds
import pandas
def runstrat():
args = parse_args()
# Create a cerebro entity
cerebro = bt.Cerebro(stdstats=False)
# Add a strategy
cerebro.addstrategy(bt.Strategy)
# Get a pandas dataframe
datapath = ('../../datas/2006-day-001.txt')
# Simulate the header row isn't there if noheaders requested
skiprows = 1 if args.noheaders else 0
header = None if args.noheaders else 0
dataframe = pandas.read_csv(datapath,
skiprows=skiprows,
header=header,
parse_dates=True,
index_col=0)
if not args.noprint:
print('--------------------------------------------------')
print(dataframe)
print('--------------------------------------------------')
# Pass it to the backtrader datafeed and add it to the cerebro
data = bt.feeds.PandasData(dataname=dataframe)
cerebro.adddata(data)
# Run over everything
cerebro.run()
# Plot the result
cerebro.plot(style='bar')
def parse_args():
parser = argparse.ArgumentParser(
description='Pandas test script')
parser.add_argument('--noheaders', action='store_true', default=False,
required=False,
help='Do not use header rows')
parser.add_argument('--noprint', action='store_true', default=False,
help='Print the dataframe')
return parser.parse_args()
if __name__ == '__main__':
runstrat()