运行 zipline 回测时如何解决 AssertionError

How to solve AssertionError when running backtest on zipline

我正在阅读 Andreas Clenow 所著的《交易发展》一书,并且在尝试 运行 zipilne 上的第一次回测代码时 运行 遇到了 AssertionError。我是 python 和 zipline 的新手,感谢任何有关如何解决此错误的指导。这是从本书网站上获取的源代码:

# This ensures that our graphs will be shown properly in the notebook.
%matplotlib inline

#Import pandas as pd

import pandas as pd

# Import Zipline functions that we need
from zipline import run_algorithm
from zipline.api import order_target_percent, symbol

# Import date and time zone libraries
from datetime import datetime
import pytz

# Import visualization
import matplotlib.pyplot as plt


def initialize(context):
    # Which stock to trade
    context.stock = symbol('AAPL')

    # Moving average window
    context.index_average_window = 100

def handle_data(context, data):
    # Request history for the stock
    equities_hist = data.history(context.stock, "close", 
                                 context.index_average_window, "1d")

    # Check if price is above moving average
    if equities_hist[-1] > equities_hist.mean():
        stock_weight = 1.0
    else:
        stock_weight = 0.0

    # Place order
    order_target_percent(context.stock, stock_weight)

def analyze(context, perf):
    fig = plt.figure(figsize=(12, 8))

    # First chart
    ax = fig.add_subplot(311)
    ax.set_title('Strategy Results')
    ax.semilogy(perf['portfolio_value'], linestyle='-', 
                label='Equity Curve', linewidth=3.0)
    ax.legend()
    ax.grid(False)

    # Second chart
    ax = fig.add_subplot(312)
    ax.plot(perf['gross_leverage'], 
            label='Exposure', linestyle='-', linewidth=1.0)
    ax.legend()
    ax.grid(True)

    # Third chart
    ax = fig.add_subplot(313)
    ax.plot(perf['returns'], label='Returns', linestyle='-.', linewidth=1.0)
    ax.legend()
    ax.grid(True)

# Set start and end date
start_date = datetime(1996, 1, 1, tzinfo=pytz.UTC)
end_date = datetime(2018, 12, 31, tzinfo=pytz.UTC)

# Fire off the backtest
results = run_algorithm(
    start=start_date, 
    end=end_date, 
    initialize=initialize, 
    analyze=analyze, 
    handle_data=handle_data, 
    capital_base=10000, 
    data_frequency = 'daily', bundle='quandl' 
) 

在 运行 之后我得到以下错误。

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-23-0e4e6c712343> in <module>()
     75     handle_data=handle_data,
     76     capital_base=10000,
---> 77     data_frequency = 'daily', bundle='quandl'
     78 ) 
     79 

/opt/anaconda3/envs/zipenv/lib/python3.5/site-packages/zipline/utils/run_algo.py in run_algorithm(start, end, initialize, capital_base, handle_data, before_trading_start, analyze, data_frequency, bundle, bundle_timestamp, trading_calendar, metrics_set, benchmark_returns, default_extension, extensions, strict_extensions, environ, blotter)
    391         environ=environ,
    392         blotter=blotter,
--> 393         benchmark_spec=benchmark_spec,
    394     )
    395 

/opt/anaconda3/envs/zipenv/lib/python3.5/site-packages/zipline/utils/run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter, benchmark_spec)
    200             trading_calendar=trading_calendar,
    201             capital_base=capital_base,
--> 202             data_frequency=data_frequency,
    203         ),
    204         metrics_set=metrics_set,

/opt/anaconda3/envs/zipenv/lib/python3.5/site-packages/zipline/finance/trading.py in __init__(self, start_session, end_session, trading_calendar, capital_base, emission_rate, data_frequency, arena)
     36                  arena='backtest'):
     37 
---> 38         assert type(start_session) == pd.Timestamp
     39         assert type(end_session) == pd.Timestamp
     40 

AssertionError: 

感谢任何有关如何解决此问题的帮助。

Your current start_date and end_date variable should produce:

# Set start and end date
start_date = datetime(1996, 1, 1, tzinfo=pytz.UTC)
end_date = datetime(2018, 12, 31, tzinfo=pytz.UTC)
type(start_date)

输出 [1]:

datetime.datetime

Please try:-

解决方案[1]:

# Set start and end date
start_date = pd.Timestamp('1996-1-1', tz='utc')
end_date = pd.Timestamp('2018-12-31', tz='utc')
type(start_date)

输出 [1]:

pandas._libs.tslib.Timestamp

解决方案[2]:

# Set start and end date
start_date = pd.to_datetime('1996-1-1', utc=True)
end_date = pd.to_datetime('2018-12-31', utc=True)
type(start_date)

输出 [2]:

pandas._libs.tslib.Timestamp