我正在构建一个事件驱动的反向测试模块,使用Quant Start的tutorial作为粗略的准则,并且我想出了一个有趣的错误。
我的错误发生在self._convert_csv_files()
的末尾我正在尝试重新索引self.symbol_data中的每个数据框,以反映每个数据框索引的并集,在此过程中创建一个生成器对象。
以下代码是我的课程:
class HistoricCSVDataHandler(DataHandler):
"""
HistoricCSVDataHandler is designed to read CSV files for each requested
symbol from a disk and provide an interface to obtain the "latest" bar in a
manner identical to a live trading interface.
"""
def __init__(self, events, csv_dir, symbol_list, start_date, end_date):
"""
Initialises the historic data handler by requesting the location of the
CSV files and a list of symbols.
It would be assumed that all files are of the form 'symbol.csv,' where
symbol is a string in the list.
Parameters:
events - The Event queue.
csv_dir - Absolute directory path to the csv files.
symbol_list - A list of symbol strings.
start_date - A datetime to which we will set the start of the bar index
end_date - A datetime to which we will set the end of the bar index
"""
self.events = events
self.csv_dir = csv_dir
self.symbol_list = symbol_list
self.bar_index = pd.bdate_range(start_date, end_date)
self.symbol_data = {}
self.latest_symbol_data = {}
self.calls = {}
self.puts = {}
self.continue_backtest= True
self._open_convert_csv_files()
def _open_convert_csv_files(self):
"""
Opens the CSV files from the data directory, converting them into
pandas DataFrames within a symbol dictionary.
For this handler it will be assumed that the date is taken from DTN
IQFeed. Thus, its format will be respected.
"""
comb_index = None
for s in self.symbol_list:
# print "Parsing: %s" % s
# Load the CSV file with no header information, indexed on date
self.symbol_data[s] = pd.io.parsers.read_csv(
os.path.join(self.csv_dir,'%s.csv' % s),
header=0, index_col=0,
names=['open',
'high',
'low',
'close',
'volume',
'adj close'
],
parse_dates=True
).sort()
# Combine the index and pad forward values
if comb_index is None:
comb_index = self.symbol_data[s].index
else:
comb_index = comb_index.union(self.symbol_data[s].index)
# Set the latest sybmol_data to None
self.latest_symbol_data[s] = []
self.bar_index = self.bar_index.intersection(comb_index)
self.calls, self.puts = trades.parse_trades()
# Reindex the DataFrames
for s in self.symbol_list:
# print "Reindexing: %s" % s
self.symbol_data[s] = self.symbol_data[s].reindex(
index=bar_index, method='pad'
).iterrows()
以下是%paste后的追溯:
In [1]: %paste
# backtest.py
import dateutil.rrule as dr
import dateutil.parser as dp
import dateutil.relativedelta as drel
import pandas as pd
import csv
import Queue
import datetime
import cg
# Import Backtesting Classes
from event import MarketEvent, SignalEvent, OrderEvent, FillEvent
from data import HistoricCSVDataHandler
# Set input file
csv_symbol_list = "symbols.csv"
# Create symbol list
symbols = []
symbols_with_currency = []
symbols_with_currency = cg.create_symbols_list(csv_symbol_list)
for i in symbols_with_currency:
symbols.append(i[0])
# Amount of total portfolio reserved for cash
percent_cash = 0.30
# Declare parameters
events = Queue.Queue()
path = "/Users/XXX/Documents/repos/cg2/data"
start_date = datetime.datetime(2010,12,1)
end_date = datetime.datetime(2013,12,31)
# Declare the components with respective parameters
bars = HistoricCSVDataHandler(events, path, symbols, start_date, end_date)
## -- End pasted text --
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-4859f3953713> in <module>()
35
36 # Declare the components with respective parameters
---> 37 bars = HistoricCSVDataHandler(events, path, symbols, start_date, end_date)
/Users/XXX/Documents/repos/cg2/data.py in __init__(self, events, csv_dir, symbol_list, start_date, end_date)
81 self.continue_backtest= True
82
---> 83 self._open_convert_csv_files()
84
85 def _open_convert_csv_files(self):
/Users/XXX/Documents/repos/cg2/data.py in _open_convert_csv_files(self)
126 for s in self.symbol_list:
127 # print "Reindexing: %s" % s
--> 128 self.symbol_data[s] = self.symbol_data[s].reindex(
129 index=self.bar_index, method='pad'
130 ).iterrows()
AttributeError: 'generator' object has no attribute 'reindex'
如果我评论出困扰的声明:
'''
# Reindex the DataFrames
for s in self.symbol_list:
# print "Reindexing: %s" % s
self.symbol_data[s] = self.symbol_data[s].reindex(
index=self.bar_index, method='pad'
).iterrows()
'''
我可以检查所使用的每个组件的类型:
In [5]: type(bars.symbol_list)
Out[5]: list
In [6]: type(bars.symbol_data['A'])
Out[6]: pandas.core.frame.DataFrame
In [7]: type(bars.bar_index)
Out[7]: pandas.tseries.index.DatetimeIndex
In [8]: type(bars.symbol_data['A'].index)
Out[8]: pandas.tseries.index.DatetimeIndex