1

我正在使用 zipline 对本地数据进行回测,但似乎不成功。
从日期时间导入日期时间导入 pytz 将熊猫导入为 pd

from zipline.algorithm import TradingAlgorithm


import zipline.utils.factory as factory


class BuyApple(TradingAlgorithm):

    def handle_data(self, data):
        self.order('AAPL', 1)


if __name__ == '__main__':


    data = pd.read_csv('AAPL.csv')



    simple_algo = BuyApple()

    results = simple_algo.run(data)

上面是我的代码,当我运行这个脚本时,我收到了消息:

[2015-04-03 01:41:53.712035] WARNING: Loader: No benchmark data found for date range.
start_date=2015-04-03 00:00:00+00:00, end_date=2015-04-03 01:41:53.632300, url=http://ichart.finance.yahoo.com/table.csv?a=3&c=2015&b=3&e=3&d=3&g=d&f=2015&s=%5EGSPC
Traceback (most recent call last):
  File "bollinger.py", line 31, in <module>
    results = simple_algo.run(data)
  File "/home/xinzhou/.local/lib/python2.7/site-packages/zipline-0.7.0-py2.7.egg/zipline/algorithm.py", line 372, in run
    source = DataFrameSource(source)
  File "/home/xinzhou/.local/lib/python2.7/site-packages/zipline-0.7.0-py2.7.egg/zipline/sources/data_frame_source.py", line 42, in __init__
    assert isinstance(data.index, pd.tseries.index.DatetimeIndex)
AssertionError

然后我将我的代码更改为以下内容:

from datetime import datetime
import pytz
import pandas as pd

from zipline.algorithm import TradingAlgorithm


import zipline.utils.factory as factory


class BuyApple(TradingAlgorithm):

    def handle_data(self, data):
        self.order('AAPL', 1)


if __name__ == '__main__':
    start = datetime(2000, 1, 9, 14, 30, 0, 0, pytz.utc)

    end = datetime(2001, 1, 10, 21, 0, 0, 0, pytz.utc)

    data = pd.read_csv('AAPL.csv', parse_dates=True, index_col=0)

    sim_params = factory.create_simulation_parameters(
       start=start, end=end, capital_base=10000)
    sim_params.data_frequency = '1d'
    sim_params.emission_rate = '1d'

    simple_algo = BuyApple()

    results = simple_algo.run(data)

assert isinstance(data.index, pd.tseries.index.DatetimeIndex)
AssertionError

离开了。但是在我的终端中,它保留在此消息中:

[2015-04-03 01:44:28.141657] WARNING: Loader: No benchmark data found for date range.
start_date=2015-04-03 00:00:00+00:00, end_date=2015-04-03 01:44:28.028243, url=http://ichart.finance.yahoo.com/table.csv?a=3&c=2015&b=3&e=3&d=3&g=d&f=2015&s=%5EGSPC

如何解决这个问题呢?谢谢。

4

2 回答 2

0
data.index=pd.to_datetime(data.index)
data.index=data.index.tz_localize(pytz.utc)
于 2015-05-08T00:43:31.367 回答
0

下一个代码适用于我。是教程示例“我的第一个算法”(http://www.zipline.io/tutorial/)的一个版本。数据必须按日期升序排列。作为一个普通的 python 程序运行(python yourfilename.py):

import pytz
from datetime import datetime

from zipline.algorithm import TradingAlgorithm
from zipline.api import order, record, symbol

import pandas as pd

# Load data manually csv
#Date,Open,High,Low,Close,Volume,Adj Close
#1984-09-07,26.5,26.87,26.25,26.5,2981600,3.02
#...
parse = lambda x: pytz.utc.localize(datetime.strptime(x, '%Y-%m-%d'))
data=pd.read_csv('aapl.csv', parse_dates=['Date'], index_col=0,date_parser=parse)

# Define algorithm
def initialize(context):
    pass

def handle_data(context, data):
    order('Close',10)
    record(AAPL=data['Close'])

# Create algorithm object passing in initialize and
# handle_data functions
algo_obj = TradingAlgorithm(initialize=initialize, 
                            handle_data=handle_data)

# Run algorithm
perf_manual = algo_obj.run(data)

# Print
perf_manual.to_csv('output.csv'
于 2015-07-28T02:25:17.370 回答