【问题标题】:backtest with local data in zipline在 zipline 中使用本地数据进行回测
【发布时间】:2015-04-03 01:47:06
【问题描述】:

我正在使用 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

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

【问题讨论】:

    标签: python-2.7 pandas zipline


    【解决方案1】:
    data.index=pd.to_datetime(data.index)
    data.index=data.index.tz_localize(pytz.utc)
    

    【讨论】:

    • 欢迎来到 SO。通常,您希望给出比两行代码更彻底的答案。你能解释一下为什么这能解决问题吗?
    • isinstance(data.index, pd.tseries.index.DatetimeIndex) 是未匹配类型。更改 data.index 的类型将得到另一个错误。那是时区不正确。所以第二行是编辑时区。
    • 最好将您的答案编辑为:“您的第一次尝试引发断言错误,因为 (something) 的类型不匹配。您的第二次尝试仍然存在 (something) 的问题。修复不匹配的类型,你想添加行(第一行)。但这仍然会出现时区不正确的问题,所以你要添加行(第二行)。 (但我不得不承认,我不知道 pandas 或 zipline,所以我只是猜测它们的格式。
    • @Teepeemm 你很聪明。
    • 感谢您的夸奖。但我的目标是帮助您了解 SO 的工作原理。我还希望您的第一个 SO 回答能够获得我认为应得的赞成票,而不是目前可能应得的反对票。我认为我最初评论的 4 位支持者也可能同意。
    【解决方案2】:

    下一个代码适用于我。是教程示例“我的第一个算法”(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'
    

    【讨论】:

      猜你喜欢
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 2020-08-14
      • 1970-01-01
      • 2016-03-13
      • 2016-08-16
      相关资源
      最近更新 更多