作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的第一个问题,请原谅我的错误我一直在阅读 Andreas Clenow 的《Trading Evolved》,这是一本关于使用 Python 进行回测和金融的书这是我收到的代码和错误
%matplotlib inline
# 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 pandas as pd
# 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)
#start_date = pd.to_datetime('1996-1-1', utc=True)
#end_date = pd.to_datetime('2018-12-31', utc=True)
start_date = pd.to_datetime('1996-1-1', utc=True)
end_date = pd.to_datetime('2018-12-31', utc=True)
# 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'
)
我收到的错误是这样的
NoBenchmark Traceback (most recent call last)_RunAlgoError: No
benchmark_spec
was provided, andzipline.api.set_benchmark
was not called ininitialize
.
提前致谢
最佳答案
我遇到了同样的问题,对我有用的是将 initialize() 中的基准设置为 False(您必须从 zipline.api 导入 set_benchmark )
def initialize(context):
# Which stock to trade
context.stock = symbol('AAPL')
# Moving average window
context.index_average_window = 100
set_benchmark(False)
关于Python ZIPLINE :_RunAlgoError: No `` benchmark_spec `` was provided, and ` `zipline. api.set_benchmark `` was not called in ``初始化``,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63154282/
这是我的第一个问题,请原谅我的错误我一直在阅读 Andreas Clenow 的《Trading Evolved》,这是一本关于使用 Python 进行回测和金融的书这是我收到的代码和错误 %matp
我是一名优秀的程序员,十分优秀!