gpt4 book ai didi

python - 在 zipline 上运行回测时如何解决 AssertionError

转载 作者:行者123 更新时间:2023-12-04 01:27:56 26 4
gpt4 key购买 nike

我正在阅读由 Andreas Clenow 编写的 Trading Evolved 一书,并且在尝试在 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:

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

最佳答案

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

关于python - 在 zipline 上运行回测时如何解决 AssertionError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61513097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com