How to solve AssertionError when running backtest on zipline
Asked Answered
S

1

5

I'm working through the book Trading Evolved by Andreas Clenow and am running into an AssertionError when trying to run the code for the first backtest on zipilne. I am new to python and zipline and appreciate any guidance on how to solve this error. Here is the source code taken from the book's website:

# 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' 
) 

After running this I get the below error.

---------------------------------------------------------------------------
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: 

Appreciate any help on how to solve this.

Spilt answered 29/4, 2020 at 22:57 Comment(1)
Ya, I am having same thing.Eicher
G
7

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)

out [1]:

datetime.datetime

Please try:-

Solution [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)

out [1]:

pandas._libs.tslib.Timestamp

Solution [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)

out [2]:

pandas._libs.tslib.Timestamp
Guanine answered 27/7, 2020 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.