注意
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()