我需要将自己的数据用于滑索项目。每当我尝试时,我都会收到此错误:
/Library/Python/2.7/site-packages/zipline/sources/data_source.pyc in <dictcomp>((target, (mapping_func, source_key)))
47 """
48 row = {target: mapping_func(raw_row[source_key])
---> 49 for target, (mapping_func, source_key)
50 in self.mapping.items()}
51 row.update({'source_id': self.get_hash()})
ValueError: cannot convert float NaN to integer
这是我正在运行的交易算法:
from zipline.algorithm import TradingAlgorithm
from zipline.api import order_target, order, record, symbol, history, add_history
import numpy as np
from pandas import Series, DataFrame, Panel
import pandas as pd
# Define algorithm
def initialize(context):
context.dateIndex = 0
def handle_data(context, data):
today = data.major_axis[context.dateIndex]
if today > data.US9663871021[data.US9663871021.close.notnull()].index[0] and today < data.US9663871021[data.US9663871021.close.notnull()].last_valid_index():
order(symbol('US9663871021'), 10)
record(US9663871021=data[symbol('US9663871021')].price)
if today > data.US7954351067[data.US7954351067.close.notnull()].index[0] and today < data.US7954351067[data.US7954351067.close.notnull()].last_valid_index():
order(symbol('US7954351067'), 10)
record(US7954351067=data[symbol('US7954351067')].price)
if today == data.US9663871021[data.US9663871021.close.notnull()].last_valid_index():
order_target(symbol('US9663871021'), 0)
record(US9663871021=data[symbol('US9663871021')].price)
if today == data.US7954351067[data.US7954351067.close.notnull()].last_valid_index():
order_target(symbol('US7954351067'), 0)
record(US9663871021=data[symbol('US7954351067')].price)
context.dateIndex = context.dateIndex + 1
def prepDf(fileName):
df = pd.io.parsers.read_csv(fileName, index_col=[0],parse_dates=[0], na_values=["#N/A N/A"],
names=["date", "open","high","low","close","volume","mkt_cap"])
df["price"] = df.close
df.index = df.index.tz_localize('UTC')
df = df[df.close.notnull()]
return df
fileName = #fill in file name
fileName2 = #fill in file name
dictionaryOfDfs = {"US9663871021" : prepDf(fileName), "US7954351067": prepDf(fileName2)}
data = Panel(dictionaryOfDfs)
algo_obj = TradingAlgorithm(initialize=initialize,
handle_data=handle_data)
# Run algorithm
perf_manual = algo_obj.run(data)
这个想法是我在数据应该是非 NaN 时买入,并在系列结束前卖出头寸。除此之外,应该不需要数据,但 zipline 坚持认为 NaN 会导致错误,即使不应该使用该值。