Zipline Ingest -b quandl command not working

In Chapter 4 fourth notebook (“04_single_factor_zipline.ipynb”), there was an issue with the line:

%%zipline --start 2015-1-1 --end 2018-1-1 --output single_factor.pickle --no-benchmark --bundle quandl

This was because I had not yet ingested the data, but when I go to do this using the following:

import quandl quandl.ApiConfig.api_key = '[MY_API_KEY]'

and then the following:

!zipline ingest -b quandl

I get the following error message:

Can anyone help me figure this out, or suggest suitable alternatives to zipline and its data sources for this notebook?

Thanks!

Short answer: Put below code in a celll at the top of your notebook, and make sure it is executed.

import os
os.environ[‘QUANDL_API_KEY’] = ‘your_api_key_here’

Long answer: I asked ChatGPT and got this:

To resolve the error message:

ValueError: Please set your QUANDL_API_KEY environment variable and retry.

you need to set your Quandl API key as an environment variable so that Zipline can access it. Here’s how you can do that:

1. Obtain Your Quandl API Key

  • Sign Up or Log In: Visit Quandl (now Nasdaq Data Link) and create an account if you haven’t already.
  • Find Your API Key: After logging in, go to your account settings to locate your unique API key.

2. Set the QUANDL_API_KEY Environment Variable

You have several options to set the environment variable:

Option A: Set Within Your Jupyter Notebook

Add the following code at the beginning of your notebook:

import os
os.environ['QUANDL_API_KEY'] = 'your_api_key_here'

Replace 'your_api_key_here' with the API key you obtained from Quandl.

Option B: Set in the Terminal Before Launching JupyterLab

If you launch JupyterLab from a terminal or command prompt, you can set the environment variable there.

  • For Unix/Linux/macOS:

    export QUANDL_API_KEY=your_api_key_here
    jupyter lab
    
  • For Windows Command Prompt:

    set QUANDL_API_KEY=your_api_key_here
    jupyter lab
    

Option C: Set as a System Environment Variable

This method makes the environment variable available system-wide.

  • Windows:

    1. Open Control Panel > System and Security > System.
    2. Click on Advanced system settings.
    3. Click Environment Variables.
    4. Under User variables or System variables, click New.
    5. Set the Variable name to QUANDL_API_KEY and Variable value to your API key.
    6. Click OK to save.
  • macOS/Linux:

    1. Open your terminal.

    2. Edit your shell profile file (e.g., ~/.bash_profile, ~/.bashrc, or ~/.zshrc).

    3. Add the following line:

      export QUANDL_API_KEY=your_api_key_here
      
    4. Save the file and run source ~/.bash_profile (replace with your profile file) to apply the changes.

3. Restart JupyterLab

After setting the environment variable, restart your JupyterLab session to ensure the changes take effect.

Alternative: Pass the API Key Directly in Code

If you prefer not to set an environment variable, you can pass the API key directly in your code (if the library supports it):

from zipline.data import bundles
bundles.register('quantopian-quandl', bundles.quandl.QuandlBundle(api_key='your_api_key_here'))

Note: Replace 'your_api_key_here' with your actual Quandl API key.


By setting the QUANDL_API_KEY environment variable or passing the API key directly in your code, you should eliminate the error and be able to use Zipline with Quandl data successfully.