Don't Miss
Home » Articles » Trading Evolved – Errata and Updates

Trading Evolved – Errata and Updates

A book like Trading Evolved will without a doubt have errors. On top of errors, updates may also be needed as new versions of dependent software packages are released. I will keep a running log on this site of issues found, and suggested solutions. After this list grows long enough, I will update the book itself, and put a version number on it.

This article will be continuously updated.

 

Book Version 1.0

Version 1.0 of the book was released on August 8, 2019.

Unable to Install Zipline with Conda version 4.7.10

Conda version 4.7 seems to break some dependencies and may result in an inability to install Zipline using the syntax “conda install zipline -c quantopian”.

Workaround 1 – Downgrade Conda

https://github.com/quantopian/zipline/issues/2514

Workaround 2 – Install using pip

https://www.zipline.io/install.html#installing-with-pip

 

Typo in “Patching the Framework”

On page 96 there’s a typo and the wrong file name is stated. This was the very last thing I did on the book before shipping it, as the issue that needed patching appeared at the last moment.

The file to be changed is “benchmarks.py”, not “loader.py”.

Here is the complete, corrected version.

Zipline, like most software in Python world is not only free, but open source. You can see and edit all the code as you like, and one big advantage of this is that we can make changes and even fix bugs when needed. You may wonder why you would ever want to go and edit someone else’s code, but it can be very useful to have this possibility.

While writing this book, an interesting example of such a situation came up. One day, all the backtests suddenly stopped working, with an error message about failing to load benchmark data from some web address. As it turns out, there is a web call executed during backtests, where Zipline tries to pull data from an online source. This source, much like what happened to many other free data sources, suddenly stopped working without warning.

As of writing, this issue has not yet been addressed. But no worries, we can do it ourselves easily. We don’t actually need this benchmark data for anything that we will do in this book, and the easiest fix is to simply remove the web call and just return empty data.

My version of Zipline is 1.3, and if you’re working with the same version, you likely need to do this fix manually. It won’t take long.

First locate a file called benchmarks.py, which should be in your Zipline installation folder. You can use the file system search tools to locate it. If you are on Windows, the path will probably be similar to this.

C:\ProgramData\Anaconda3\envs\zip35\Lib\site-packages\zipline\data

It’s always a good idea to keep a backup of the original code, so rename this file to keep a copy. Then make a new file, with the name benchmarks.py, and put the following code in it before saving.

This code will simply return a data series from 1930 to 2030 with all zero values, which will effectively sidestep this web call issue, with no negative impact.

Addition to Patch

You may also need to bypass the cache in loader.py, in the same folder:

https://github.com/quantopian/zipline/issues/2480#issuecomment-504387554

 

Zipline Installation: failed with current_repodata.json

Error:

Collecting package metadata (current_repodata.json): done
Solving environment: failed with current_repodata.json, will retry with next repodata source.

Solution:

1) Update Conda to the latest version:

2) Install the free channel:

3) install Zipline:

 

Trouble Ingesting Quandl

Error:

“ValueError: Boolean array expected for the condition, not float64”

Solution:

Downgrading Pandas to 0.18.1

 

Jupyter version 5.0.0 not starting

Solution:

Downgrading “tornado” from 5.1.1 to 4.5.3

 

Trouble ingesting Quandl on Mac

Try

 

Typo Chapter 23 – Bundle Name

In chapter 23, you’re advised to ingest the random stocks bundle using

But that’s not the name of the bundle that we just created a few lines above. The name in used in the bundle was random_stock_data, so we need to use that name when ingesting.

Similar typo in regards to the futures bundle. The name of the file in the sample code is random_futures_data:

And

Note that you can call your bundles whatever you like, but the file names need to match the references, and the registered bundle name needs to match the ingest call.

30 comments

  1. Steven Petersen

    Good new book so far. The following are no big deal but though I’d mention them.

    “If the volatility is exactly half in Microsoft, you would theoretically achieve the same risk level if you invest twice as much in Tesla.” should be “…twice as much AS in Tesla” ?

    “You can create both a folder and a new Python file through that dropdown on the right side, which you can see in Figure 5‑2.” should be “…a new Python FOLDER” ? There is an option to create a file so I got confused.

    • Yes, that first one is unfortunate. Leaving out ‘as’ gave the sentence the opposite meaning…

      But the second sentence seems fine to me, unless I’m still missing something. It’s not a Python folder, it’s a regular folder that you can create. Or ‘directory’ if you’re so inclined.

      So you can create folders in Jupyter, as well as Jupyter Notebook python files. Click the dropdown, and you’ll see.

  2. hi Andreas, thank you for the book. Great book. Hope to be even better at backtesting in time to come. By the way, the sp500 downloaded did not show sp500 values but it showed all the components within instead. My understanding from the book is 2 columns, 1 date and the other would be values under sp500. Am i right to say that? Thank you for taking the time to read this.

    • The file under “/Chapter 6 Pandas” contains a price series of the index, for use in the code in that chapter.

      The file under “/data/index_members/” contains the historical components.

      Yes, naming both files sp500.csv was unfortunate…

      Also, please write an Amazon review. These days far too many authors are paying for bulk reviews, having hundreds of reviews in the first week. I don’t resort to that, but I do resort to nagging on people to write reviews. 🙂

  3. Zipline Installation: failed with current_repodata.json

    Missing a dash led to another error. For step 2 it should be:

    conda config –set restore_free_channel true

    I’m loving the book so far. I’m no coder, but have found my interest in investing a useful tool to motivate me to solve problems and tinker.

  4. https://github.com/ContinuumIO/anaconda-issues/issues/1423

    I had an issue trying to install the nb_conda package (pg.101) and while I didn’t save the error message it was something to do with nb_conda not being compatible with Python 3.5. Anyway I used the following command in the terminal from the link above to get it to install
    conda install -c conda-forge nb_conda

  5. Jupyter notebook not downloaded in my zip35 environment, open in jupyter greyed out, and cannot download notebook==6.0

    also can’t ingest quandl already downgraded pandas, or at least i think i did, just used

    conda install pandas=0.18.1

  6. which version of conda I should use to install zipline.

    First part of the message says
    “Workaround 1 – Downgrade Conda” , conda install conda=4.6.11

    The last part of the message says
    1) Update Conda to the latest version:

    What should I do?

  7. Just acquired “Trading Evolved” and want to thank you for your efforts in writing the book. I am finding it extremely useful in my quest to become proficient in using Python for financial analysis and equities trading.

    In the course of following along with the book in Jupyter Notebook I encountered an error in running “first Zipline backtest” as follows:

    ValueError: SQLite file ‘C:\\Users\\r1100/.zipline\\data\\quandl\\2019-08-21T20;57;11.306361\\assets-6.sqlite’ doesn’t exist

    So my question is what is this file and what is required to bring it into existence? Thank you.

    • That sounds like an error during bundle ingest. If you interrupt the ingest process, or if it for any reason fails, this error shows up. Try ingesting it again.

      Also, the Quandl bundle is very limited and I would very much recommend making your own bundle, for your own data, as explained in the later chapters in the book.

    • I don’t recall doing anything that would cause the ingest process to be interrupted. I will certainly try the ingest process again but with the verbose mode on (note I mostly use iPython) to get a better idea of what is going on should the error repeat. I think I will start making my own bundles – I prefer having more control over the overall process.

    • Oh, and be sure to start the kernel in the notebook after ingesting. In case you had the notebook running while doing the ingest, you’ll need to restart the kernel.

  8. Stephen Hankinson

    Great book Andreas. I’ve enjoyed all of your books to date.

    I noticed an issue with the dividend code when trying to import data with dividends. The divs.append method inside the process_stocks function is creating a local copy of the divs DataFrame which doesn’t apply the changes to the divs DataFrame in the random_stock_data function. You can confirm this by adding a print(divs) right before the adjustment_writer.write call, if using a dataset with dividends, and you’ll see the DataFrame being written is empty.

    A simple fix is to change the dividend check to the following:


    # If there's dividend data, add that to the dividend DataFrame
    if 'dividend' in df.columns:

    # Get the current highest index
    idx = len(divs)

    # Append this stock's dividends to the list of all dividends
    tmp = df[df['dividend'] != 0.0]['dividend']
    for ex_date, amount in tmp.iteritems():
    divs.loc[idx] = [sid, amount, ex_date, pd.NaT, pd.NaT, pd.NaT]
    idx = idx + 1

    Another thing I noticed when importing data from alphavantage is that the if your data is ordered newest to oldest you’ll get an error when importing it. This can be fixed by adding df.sort_index(inplace=True) right after the pd.read_csv call.

  9. https://github.com/quantopian/zipline/issues/1591

    im trapped here, please help, already changed the zipline-scrypt.py file, did not work, getting this

    Traceback (most recent call last):
    File “C:\Users\JAY\Anaconda3\envs\zip35\Scripts\zipline-script.py”, line 11, in
    load_entry_point(‘zipline==1.3.0’, ‘console_scripts’, ‘zipline’)()
    File “C:\Users\JAY\Anaconda3\envs\zip35\lib\site-packages\pkg_resources\__init__.py”, line 484, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
    File “C:\Users\JAY\Anaconda3\envs\zip35\lib\site-packages\pkg_resources\__init__.py”, line 2707, in load_entry_point
    return ep.load()
    File “C:\Users\JAY\Anaconda3\envs\zip35\lib\site-packages\pkg_resources\__init__.py”, line 2325, in load
    return self.resolve()
    File “C:\Users\JAY\Anaconda3\envs\zip35\lib\site-packages\pkg_resources\__init__.py”, line 2331, in resolve
    module = __import__(self.module_name, fromlist=[‘__name__’], level=0)
    File “C:\Users\JAY\Anaconda3\envs\zip35\lib\site-packages\zipline\__init__.py”, line 23, in
    from . import data
    File “C:\Users\JAY\Anaconda3\envs\zip35\lib\site-packages\zipline\data\__init__.py”, line 1, in
    from . import loader
    File “C:\Users\JAY\Anaconda3\envs\zip35\lib\site-packages\zipline\data\loader.py”, line 22, in
    from .benchmarks import get_benchmark_returns
    File “C:\Users\JAY\Anaconda3\envs\zip35\lib\site-packages\zipline\data\benchmarks.py”, line 23
    data = data[‘close’] return data.sort_index().iloc[1:]
    ^
    SyntaxError: invalid syntax.

    I’ve been stuck with setting up the environment for quite a while now

    • Hello Jay,

      It looks like a syntax error on line number 23 of the file benchmarks.py:

      File “C:\Users\JAY\Anaconda3\envs\zip35\lib\site-packages\zipline\data\benchmarks.py”, line 23
      data = data[‘close’] return data.sort_index().iloc[1:]
      ^
      SyntaxError: invalid syntax.

      I think you have the return statement in the same line as data = data[‘close’]

      Please change the content of this file “benchmark.py” exactly as follows:

      import pandas as pd
      from datetime import datetime
      from trading_calendars import get_calendar

      def get_benchmark_returns(symbol):
      cal = get_calendar(‘NYSE’)
      first_date = datetime(1930,1,1).date()
      last_date = datetime(2030,1,1).date()
      dates = cal.sessions_in_range(first_date,last_date)
      data = pd.DataFrame(0.0,index=dates,columns=[‘close’])
      data = data[‘close’]
      return data.sort_index().iloc[1:]

  10. Hi Andreas,

    Thank you very much for writing this book.

    When attempting to run the code in “Your First Zipline Backtest”, I get an error when trying to run the moving average window that says “NameError: name ‘context’ is not defined”.

    I’m sure this is something basic that I am missing, though it stops the whole thing from working. Would you happen to know what I am missing in this case.

    Thanks,
    James

  11. Working on a mac.

    After ingesting the quandl bundle, I ran the first zipline backtest and got the following error:

    ValueError: No JSON object could be decoded

  12. Hey Andreas, great book so far.

    In regard to the ‘Making a correlation Graph’, my correlation graph came out a bit different compared to the one showed on the book. (more spikes)

    I used the set of data you provided which is ‘indexes’ in chapter 6 pandas folder.

    Don’t know if it’s the matplotlib format/version or its the way the dataset in computed. Should we expect similar differences throughout the book?

    Thank you.

  13. Hi Andreas, great book and i am digging my way through it. Right stuff at the right time so to speak. Got environment set up on desktop – check! However, laptop is posing and issue re Quandl – despite all same as desktop, i am getting the dreaded “failed to create process”. i tried your suggested remedy of apply a double does of “” to the relevant file but alas…still same result ie “failed t0 create process”. Do you have other good ideas of where to look/what to do? Thanks in advance.

    Michael

    • Hi Michael,

      This is most likely an issue regarding installation path and probably there are spaces in your path. There’s a thread somewhere in the Zipline GitHub about this, and the solution likely involves modding the path with double quotes. I understand you tried this, but it’s probably something highly related.

      Sorry I can’t be more specific, but it’s difficult without actually seeing your exact setup. I’m pretty sure you’re on the right track though. This is a classic error relating to Windows and spaces in the path to the interpreter.

  14. Hello Andreas thanks for the book,

    On page 96 what do you mean “You may also need to bypass the cache in loader.py”?
    Do you mean override the whole file or just the section of the code between the triple quotes?

    Thanks

  15. Hey Andreas,

    You are commendable for writing this book, it is well written, clear, and exactly what young python developers are looking for!

    Was wondering if you, or anyone else watching this thread would be able to assist here. I am having the same issue as mentioned on this github post (https://github.com/quantopian/zipline/issues/2532), and as insult to injury I am unable to downgrade Anaconda to 4.6.11. It looks like this issue has arisen fairly recently based on what the community is saying.

    Has anyone else had this issue?

  16. I can not get past this error, JSONDecodeError: Expecting value: line 1 column 1 (char 0). I’ve read all the github threads, and have tried changing the benchmarks.py and loaders.py files, but to no avail.

    Any suggestions, on mac BTW.

    Thanks

  17. Thank you for this community here. I am currently facing a zipline issue. Used to work okay when I followed the instructions in chapter 7. Ingested the customized bundle as mentioned in chapter 23 but it did not work. Was unsure why so I uninstall anaconda and reinstalled everything. The same issue remains, zipline does not seem to be available/working.

    (zip35) C:\Users\chuam>zipline ingest -b quandl
    ‘zipline’ is not recognized as an internal or external command,
    operable program or batch file.

    The above is what I was given from the terminal. Not sure what is going on. Appreciate your advice. I have browsed through various forums the past one week but to no avail. Look forward to overcoming this. Thank you once again Andreas!

Leave a Reply to MH Cancel reply

Your email address will not be published. Required fields are marked *

*