使用zipline中的本地数据进行回测

时间:2015-04-03 01:47:06

标签: python-2.7 pandas zipline

我使用zipline用本地数据进行回测,但似乎不成功 从datetime导入日期时间     进口pytz     将pandas导入为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

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

2 个答案:

答案 0 :(得分:0)

data.index=pd.to_datetime(data.index)
data.index=data.index.tz_localize(pytz.utc)

答案 1 :(得分:0)

下一个代码对我有用。这是教程示例的一个版本&#34;我的第一个算法&#34; (http://www.zipline.io/tutorial/)。Data必须按日期按升序排列。作为普通的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'