stefan-jansen / zipline-reloaded

Zipline, a Pythonic Algorithmic Trading Library
https://zipline.ml4trading.io
Apache License 2.0
1.03k stars 199 forks source link

Cannot place order for AAPL, as it has de-listed. #238

Closed swisstackle closed 4 months ago

swisstackle commented 4 months ago

Dear Zipline Maintainers,

Before I tell you about my issue, let me describe my environment:

Environment

Lubuntu Python 3.10.0 Anaconda Jupyter Notebook

* Operating System: Linux lubuntu 6.5.0-25-generic #25~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Feb 20 16:09:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux * * Python Version: 3.10.0 * Python Bitness: 64` * How did you install Zipline: conda * Python packages: * # Name Version Build Channel _libgcc_mutex 0.1 main _openmp_mutex 5.1 1_gnu archspec 0.2.3 pyhd3eb1b0_0 boltons 23.0.0 py312h06a4308_0 brotli-python 1.0.9 py312h6a678d5_7 bzip2 1.0.8 h5eee18b_5 c-ares 1.19.1 h5eee18b_0 ca-certificates 2023.12.12 h06a4308_0 certifi 2024.2.2 py312h06a4308_0 cffi 1.16.0 py312h5eee18b_0 charset-normalizer 2.0.4 pyhd3eb1b0_0 conda 24.1.2 py312h06a4308_0 conda-libmamba-solver 23.12.0 pyhd3eb1b0_1 conda-package-handling 2.2.0 py312h06a4308_0 conda-package-streaming 0.9.0 py312h06a4308_0 distro 1.8.0 py312h06a4308_0 expat 2.5.0 h6a678d5_0 fmt 9.1.0 hdb19cb5_0 icu 73.1 h6a678d5_0 idna 3.4 py312h06a4308_0 jsonpatch 1.32 pyhd3eb1b0_0 jsonpointer 2.1 pyhd3eb1b0_0 krb5 1.20.1 h143b758_1 ld_impl_linux-64 2.38 h1181459_1 libarchive 3.6.2 h6ac8c49_2 libcurl 8.5.0 h251f7ec_0 libedit 3.1.20230828 h5eee18b_0 libev 4.33 h7f8727e_1 libffi 3.4.4 h6a678d5_0 libgcc-ng 11.2.0 h1234567_1 libgomp 11.2.0 h1234567_1 libmamba 1.5.3 haf1ee3a_0 libmambapy 1.5.3 py312h2dafd23_0 libnghttp2 1.57.0 h2d74bed_0 libsolv 0.7.24 he621ea3_0 libssh2 1.10.0 hdbd6064_2 libstdcxx-ng 11.2.0 h1234567_1 libuuid 1.41.5 h5eee18b_0 libxml2 2.10.4 hf1b16e4_1 lz4-c 1.9.4 h6a678d5_0 menuinst 2.0.2 py312h06a4308_0 ncurses 6.4 h6a678d5_0 openssl 3.0.13 h7f8727e_0 packaging 23.1 py312h06a4308_0 pcre2 10.42 hebb0a14_0 pip 23.3.1 py312h06a4308_0 platformdirs 3.10.0 py312h06a4308_0 pluggy 1.0.0 py312h06a4308_1 pybind11-abi 4 hd3eb1b0_1 pycosat 0.6.6 py312h5eee18b_0 pycparser 2.21 pyhd3eb1b0_0 pysocks 1.7.1 py312h06a4308_0 python 3.12.2 h996f2a0_0 readline 8.2 h5eee18b_0 reproc 14.2.4 h295c915_1 reproc-cpp 14.2.4 h295c915_1 requests 2.31.0 py312h06a4308_1 ruamel.yaml 0.17.21 py312h5eee18b_0 setuptools 68.2.2 py312h06a4308_0 sqlite 3.41.2 h5eee18b_0 tk 8.6.12 h1ccaba5_0 tqdm 4.65.0 py312he106c6f_0 truststore 0.8.0 py312h06a4308_0 tzdata 2024a h04d1e81_0 urllib3 2.1.0 py312h06a4308_1 wheel 0.41.2 py312h06a4308_0 xz 5.4.6 h5eee18b_0 yaml-cpp 0.8.0 h6a678d5_0 zlib 1.2.13 h5eee18b_0 zstandard 0.19.0 py312h5eee18b_0 zstd 1.5.5 hc292b87_0

Now that you know a little about me, let me tell you about the issue I am having:

Description of Issue

Here is how you can reproduce this issue on your machine:

Notebook

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 matplotlib.pyplot as plt
def initialize(context):
    context.stock = symbol('AAPL')
    context.index_average_window = 100
def handle_data(context, data):
    equities_hist = data.history(context.stock, "close", context.index_average_window, "1d")
    if equities_hist[-1] > equities_hist.mean():
        stock_weight = 1.0
    else:
        stock_weight = 0.0
    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)

start_date = pd.Timestamp('1997-01-01')
end_date = pd.Timestamp('2021-03-30')
results = run_algorithm(
    start = start_date.tz_localize(None),
    end = end_date.tz_localize(None),
    initialize=initialize,
    analyze=analyze,
    handle_data=handle_data,
    capital_base=10000,
    data_frequency='daily',
    bundle='quandl'
)

What steps have you taken to resolve this already?

Google'd it. Im not sure if the AAPL thing is a data issue or if it has something to do with Zipline.

Anything else?

...

Sincerely, Alain

mbecker8600 commented 4 months ago

This is typically thrown when the data doesn't exist within your bundle. How did you create your quandl bundles? Also the free quandl bundle may not have data going back that far

swisstackle commented 4 months ago

Yes, you were right. Fixed it by restricting the daterange from 1997 to 2018.