'zipline quandl bundle not collecting data in run_algorithm() function
I am following a book Trading evolved: Anyone can build a killer trading strategy in Pythom
- (section 7: Backtesting trading strategies, page 68)
- and I have the following code.
Code & data:
# 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 = pd.Timestamp('1996-1-1', tz='utc')
end_date = pd.Timestamp('2018-12-31', tz='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'
)
However I get the following error:
FileNotFoundError Traceback (most recent call last)
~/anaconda3/envs/zipline_env/lib/python3.6/site-packages/zipline/data/bundles/core.py in most_recent_data(bundle_name, timestamp, environ)
471 candidates = os.listdir(
--> 472 pth.data_path([bundle_name], environ=environ),
473 )
FileNotFoundError: [Errno 2] No such file or directory: '/home/bscuser/.zipline/data/quandl'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-4-c4c8f7e5b9b0> in <module>
75 handle_data=handle_data,
76 capital_base=10000,
---> 77 data_frequency = 'daily', bundle='quandl'
78 )
~/anaconda3/envs/zipline_env/lib/python3.6/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)
407 environ=environ,
408 blotter=blotter,
--> 409 benchmark_spec=benchmark_spec,
410 )
411
~/anaconda3/envs/zipline_env/lib/python3.6/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)
89 bundle,
90 environ,
---> 91 bundle_timestamp,
92 )
93
~/anaconda3/envs/zipline_env/lib/python3.6/site-packages/zipline/data/bundles/core.py in load(name, environ, timestamp)
511 if timestamp is None:
512 timestamp = pd.Timestamp.utcnow()
--> 513 timestr = most_recent_data(name, timestamp, environ=environ)
514 return BundleData(
515 asset_finder=AssetFinder(
~/anaconda3/envs/zipline_env/lib/python3.6/site-packages/zipline/data/bundles/core.py in most_recent_data(bundle_name, timestamp, environ)
487 'maybe you need to run: $ zipline ingest -b {bundle}'.format(
488 bundle=bundle_name,
--> 489 timestamp=timestamp,
490 ),
491 )
ValueError: no data for bundle 'quandl' on or before 2022-03-02 19:41:10.170941+00:00
maybe you need to run: $ zipline ingest -b quandl
On the very last line I believe that the problem is to do with the Quandl
bundle - data_frequency = 'daily', bundle='quandl'
. So my question is how can I get the zipline
library to work with the quandl
data? or yahoo finance
data?
Solution 1:[1]
Assuming you properly ingested the quandl bundle (instructions in the book). I believe the free quandl data only runs through 12-31-17, so try these dates (working for me):
start = pd.Timestamp('2003-01-01', tz='UTC')
end = pd.Timestamp('2017-12-31', tz='UTC')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BeardOverflow |