Как решить AssertionError при запуске backtest на zipline - PullRequest
0 голосов
/ 30 апреля 2020

Я работаю над книгой «Развитие торговли» Андреаса Кленова и сталкиваюсь с ошибкой AssertionError, когда пытаюсь запустить код для первого тестирования на zipilne. Я новичок в 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: 

Ценю любую помощь в том, как решить эту проблему.

...