Don't Miss
Home » Trading Evolved

Trading Evolved

Order now!

More than any other technological change, the rise of Python in the past years has dramatically leveled the playing field in systematic trading. In just a matter of years, Python has emerged as one of the most powerful, versatile and widely used tool for systematic traders.

When I started my journey into Python land, I was struck by two key insights. These two realizations and what they imply prompted me to write a new book, which not only is a whole new type of book for me, but also by far the largest book I have authored.

First, I was incredibly impressed by the power of this language. I have been programming computers since I was ten years old, and it feels very odd to be impressed by a programming language. After all, a programming language is just a tool, and you rarely get impressed by a new hammer.

But Python really is amazing. Compared to other environments, it’s very easy to learn Python and you can get financial analytical tasks done very quickly. Tasks which would take hundreds of lines of code in C# and similar can be done in a single line.

I really do believe that anyone can learn Python and that it is an extremely useful skill for anyone working with backtesting and implementation of trading ideas.

But my second realization was that documentation about Python was very poor. Almost all documentation, books and articles assume that you already know pretty much everything. I didn’t find the online forums particularly helpful either, with most replies simply pointing out that the poster should do his homework before asking silly questions.

The problem I saw with Python is that there was a clear lack of understandable documentation. I found some great books on Python, but they tend to assume that you’re already a programmer, or a data scientist. That’s fine for people like me, who already have a background in such things, but it doesn’t help newcomers to the field.

I wrote this book for two reasons. First, to fill this gap. To open up the field of Python backtesting to all the traders out there using inferior tools. I want to broaden the appeal of Python and get everyone from advanced algo traders to happy hobby traders in on the game.

Trading Evolved does not assume any previous programming knowledge. At all. It does assume that you are sufficiently interested in trading that you are willing to put in a bit of time and effort.

This book is unlike any of my previous books. This book aims to take you from zero to a level of knowledge about Python backtesting that can make you dangerous out there. But this is not just a programming book. This is a trading book.

My previous books explained one trading strategy per book. This book explains many trading strategies. While my previous books tried to explain all the rules in enough detail that you could try to replicate it at home, Trading Evolved actually shows you all of the source code.

I hope you’ll like the book, and I very much appreciate if you would do an Amazon review.

Kindle or Paperback

Trading Evolved will be available in both Kindle and Paperback. So which one should you pick?

This is a big book, at nearly 500 pages and it’s packed with information. I suspect that most people will want to be able to quickly flip back and forth in the book, looking up syntax and information. They probably want to put sticky notes in there, scribble comments and underline. It’s a book that you will most likely read more than once. Perhaps even read a chapter or two, and then move to a computer to try out your new skills. Those things are clearly easier with a paperback.

I read a lot of books, mostly on my Kindle device. This kind of book though, I would personally prefer to have as a paperback on my desk.

For this reason, I have differentiated the pricing significantly. The paperback will have a premium price tag. This is a unique book, with content not found in any other book, and it is a substantial book with nearly 500 pages packed with practical, in-depth content, which took me a year to write. But I will price the Kindle version at just $9.99, to reflect the fact that I personally see the hardcover as more useful. I will also allow those who bought the paperback version to buy the Kindle in addition for just $2.99, so that you can have both versions.

I hope you will find this a fair proposition.

Updates and Errata

As expected, some updates are necessary. In a book like this, it’s impossible to get everything perfect for all kinds of environments and to predict ever updating third party software.

You will find an updating article of issues and solutions here.

Downloads

There are two separate downloads for this book. The source code file contains all code samples used in the book, while the data file contains random data generated for testing, as discussed in the book.

Table of Contents

1      About this Book                                                                          1

The Trading Strategies in this Book                                                                                                                       2

How to Read this Book                                                                                                                                               3

How this book is written                                                                                                                                            4

How the code is written                                                                                                                                             5

Errata                                                                                                                                                                                 5

Support                                                                                                                                                                             6

2      Systematic Trading                                                                   7

Trading Approach Validation                                                                                                                                   7

Scientific Approach                                                                                                                                                      8

Consistent Methodology                                                                                                                                        10

Time Management                                                                                                                                                     11

3      Developing Trading Models                                                13

Model Purpose                                                                                                                                                            13

Rules and Variations                                                                                                                                                 15

Handling Data                                                                                                                                                             17

Asset Class                                                                                                                                                                    18

Investment Universe                                                                                                                                                19

Allocation and Risk Level                                                                                                                                        20

Entry and Exit Rules                                                                                                                                                  20

Rebalancing                                                                                                                                                                  21

4      Financial Risk                                                                            23

Quantifying Risk                                                                                                                                                         23

Mark to Market                                                                                                                                                            25

Common Risk Fallacies                                                                                                                                            26

Risk as Currency to Buy Performance                                                                                                                 30

5      Introduction to Python                                                       34

Some Assembly Required                                                                                                                                        34

Python Emerges as the Logical Choice                                                                                                              36

Programming Teaching Approach                                                                                                                       37

Installing Python on your Computer                                                                                                                 38

Let’s Run Some Code                                                                                                                                                39

Working with Jupyter Notebook                                                                                                                          46

Dictionary Lookup                                                                                                                                                     47

Conditional Logic                                                                                                                                                       50

Common Mistakes                                                                                                                                                     51

Installing Libraries                                                                                                                                                     54

6      Bring out the Pandas                                                             57

Documentation and Help                                                                                                                                       61

Simple Python Simulation                                                                                                                                      65

Making a Correlation Graph                                                                                                                                  71

Prettier Graphs                                                                                                                                                            76

7      Backtesting Trading Strategies                                        84

Python Backtesting Engines                                                                                                                                   86

Zipline and Quantopian                                                                                                                                          88

Pros and Cons                                                                                                                                                              90

Installing Zipline                                                                                                                                                        91

Problems with Installing Zipline                                                                                                                          95

Zipline and Data                                                                                                                                                         95

Ingesting the Quandl Bundle                                                                                                                                 97

Installing Useful Libraries                                                                                                                                       99

Where to Write Backtest Algos                                                                                                                            100

Your First Zipline Backtest                                                                                                                                   101

Portfolio Backtest                                                                                                                                                    108

Data Used for this Book                                                                                                                                         115

8      Analyzing Backtest Results                                               116

Installing PyFolio                                                                                                                                                    116

Portfolio Algorithm to Analyze                                                                                                                           117

Analyzing Performance with PyFolio                                                                                                               122

Custom Analysis                                                                                                                                                       129

Day Snapshot                                                                                                                                                            131

Custom Time Series Analytics                                                                                                                             135

9      Exchange Traded Funds                                                      142

The Good                                                                                                                                                                    142

The Bad                                                                                                                                                                        143

The Worst                                                                                                                                                                    149

Shorting Exchange Traded Funds                                                                                                                      155

10   Constructing ETF Models                                                   158

Asset Allocation Model                                                                                                                                          160

11   Equities                                                                                      167

The Most Difficult Asset Class                                                                                                                             167

A word on Methodology                                                                                                                                        169

Equity Investment Universe                                                                                                                                169

Dividends                                                                                                                                                                    171

12   Systematic Momentum                                                         174

Replicating this Model                                                                                                                                           174

Momentum Model Rules Summary                                                                                                                  175

Investment Universe                                                                                                                                              176

Momentum Ranking                                                                                                                                               177

Position Allocation                                                                                                                                                  185

Momentum Model Logic                                                                                                                                       187

Downside Protection                                                                                                                                              190

Momentum Model Source Code                                                                                                                        192

Performance                                                                                                                                                               204

Equity Momentum Model Results                                                                                                                    205

13   Futures Models                                                                      211

Futures Basics                                                                                                                                                           212

Futures Mechanics and Terminology                                                                                                               214

Futures and Currency Exposure                                                                                                                         218

Futures and Leverage                                                                                                                                             219

14   Futures Modeling and Backtesting                               221

Continuations                                                                                                                                                           226

Zipline Continuation Behavior                                                                                                                           229

Contracts, Continuations and Rolling                                                                                                             230

15   Futures Trend Following                                                   233

Principles of Trend Following                                                                                                                             234

Revisiting the Core Trend Model                                                                                                                       236

Model Purpose                                                                                                                                                          237

Investment Universe                                                                                                                                              238

Trading Frequency                                                                                                                                                  239

Position Allocation                                                                                                                                                  239

Entry Rules                                                                                                                                                                 244

Exit Rules                                                                                                                                                                     245

Costs and Slippage                                                                                                                                                  246

Interest on Liquidity                                                                                                                                               247

Trend Model Source Code                                                                                                                                    248

Core Trend Model Results                                                                                                                                    260

16   Time Return Trend Model                                                   266

Investment Universe                                                                                                                                              267

Trading Frequency                                                                                                                                                  267

Position Allocation                                                                                                                                                  268

Trading Rules                                                                                                                                                             268

Dynamic Performance Chart                                                                                                                               269

Time Return Source Code                                                                                                                                     270

Time Return Model Performance                                                                                                                       275

Rebalancing                                                                                                                                                                281

17   Counter Trend Trading                                                       283

Counter Model Logic                                                                                                                                              285

Quantifying Pullbacks                                                                                                                                            287

Rules Summary                                                                                                                                                         288

Counter Trend Source Code                                                                                                                                289

Counter Trend Results                                                                                                                                           293

18   Trading the Curve                                                                 298

Term Structure Basics                                                                                                                                             298

Quantifying Term Structure Effect                                                                                                                     302

Curve Model Logic                                                                                                                                                   304

Curve Trading Source Code                                                                                                                                 306

Curve Trading Results                                                                                                                                            314

Model Considerations                                                                                                                                           317

19   Comparing and Combining Models                                  319

Combining the Models                                                                                                                                          322

Implementing a Portfolio of Models                                                                                                                325

20   Performance Visualization and Combinations          327

Storing Model Results                                                                                                                                            327

How the Model Performance Analysis was done                                                                                         328

How the Combined Portfolio Analysis was done                                                                                        332

21   You can’t beat all of the Monkeys all of the Time   336

Mr. Bubbles goes to Wall Street                                                                                                                          338

The Problem is with the Index                                                                                                                            346

Finding Mr. Bubbles                                                                                                                                                349

22   Guest Chapter: Measuring Relative Performance     353

23   Importing your Data                                                             370

Making a Bundle                                                                                                                                                       371

Zipline and Futures Data                                                                                                                                      381

Futures Data Bundle                                                                                                                                               383

Patching the Framework                                                                                                                                       392

24   Data and Databases                                                               394

Your Very Own Securities Database                                                                                                                  397

Installing MySQL Server                                                                                                                                        398

Making an Equities Time-Series Table                                                                                                            400

Populating the Database                                                                                                                                      402

Querying the Database                                                                                                                                          408

Making a Database Bundle                                                                                                                                   411

25   Final Words – Path Forward                                               415

Build Your Own Models                                                                                                                                         415

Other Backtesting Engines                                                                                                                                   416

How to Make Money in the Markets                                                                                                                 418

References                                                                                      419

Index  420

 

 

161 comments

  1. Holding Pattern

    Is there a way to pre-order the hardcover book ?

  2. I’m at 50% with the book love it so far, I’m here for the backtesting time 🙂

    I highly recommend the book for anyone looking to get started with trading seriously and creating your own strategies.

    The book is a shortcut i wish was available 1+ year ago when I had to jump between books just to make sense on a secretive it seemed at the time industry.

    Thanks Andreas for writing it

    • Thanks, Mark! Much appreciated. And it’s exactly the kind of comment that I’m hoping for.

      My starting point, with all my books, has been that I want to write books for a younger version of myself. Books which I wish had been available to me earlier on. When I publisher asks me who the target audience is, my response is always “me, X years ago”.

      It would be great if you could write an Amazon review.

  3. Exactly the book I was thirsting for!
    Bravo!
    Picked up the print as well as Kindle version. Am at page 99 – super stoked!
    Many thanks!

  4. Andreas,

    Hello. I picked up the hardcover book and enjoying it thus far. Even though you wrote it such that an inexperienced person could learn Python, I still find it challenging. Perhaps it’s just me.

    That said, there are many typos throughout. The second edition will need some editing.

    Thanks.

  5. Andreas,

    Hello. You mentioned in “Trading Evolved” that you have tested the Python coding in your book across various platforms (Windows, Linux and Mac OS). If so, I want to offer a suggestion to recreate these environments so that Python and its associated libraries can be replicated in other users personal computers.

    Pages 66-67 of the Conda documentation [ here ==> https://readthedocs.com/projects/continuumio-conda/downloads/pdf/master/ ] describe how to export a file that contains all the packages and versions of an environment where Python, and most importantly Zipline, have been successfully tested and used.

    If you have created those environments across the three platforms, exporting the “environment.yml” file for each platform, and sharing it on this website would go a long way to resolving installation and implementation problems. Users can download the file for their platform and then install it on their personal computer to recreate the tested environment.

    I am an accountant and not a coder. So I may be over simplifying what is involved. Nevertheless, this sounds like a convenient way to get your readers up and running very quickly.

    Thanks for your consideration.

    Ed

  6. Hi Andreas,

    I have tried getting zipline running a couple of years ago and failed, picked up your book and with your patches it finally works. Thank you so much!

    Right no I’m struggling with getting my own bundles with futures “minute” data to work, I have managed to patch zipline and ingest it but I have problems loading it into my algorithms. I was wondering if you have managed to do this as it is not described in your book and if so do you mind share your knowledge.

    And again thanks for a great book!

    • Hi Hakan,

      I haven’t worked with minute futures data for Zipline, but I know that minute level data can be a little trickier.

      If I try it out and solve it, I’ll report back.

      I had a few requests to set up a forum here for readers to discuss Zipline stuff. It would be fun to get a community going to discuss, I’m just worried that it will turn into a place where everyone asks questions, and no one replies…

      Also, I’d be grateful if you could write an Amazon review for the book, Hakan!

      Andreas

  7. I got zipline up and running and I have also reached the bundles and ingest. Set QUANDL_API_KEY works (at least no error messages). I changed the file loader.py as per page 96. When I run “zipline ingest -b quandl” (page 99) I get an error message. Look like it’s “first_date = datetime(1930,1,1)” that cause problems. I can’t find anything about ingest on the internet that could help me. It can be difficult to find out whether it’s a software or user issue. I don’t want to write loads of support questions that no one answer. Instead I just ask if anyone else also got stuck here and perhaps how the problem was solved.

    • Hey Henk,

      Post your error message and I’ll take a look. Perhaps me or someone else has seen it before.

      In my view, getting past the Zipline installation part is the main hurdle. The Quandl stuff you won’t really need, and I mostly included it to get some sort of data up early in the book.

      For any sort of even semi serious stuff, you’ll need to hook up your own data. If it’s just Quandl holding you up, I’d advise to simply skip it. You can’t do all that much with Quandl anyhow.

      More important is to set up your own bundle and connect to your own data. That’s explained in the back, chapters 23 and 24.

      Andreas

      • I resolved the same issue with two steps:

        conda uninstall gevent

        pip install tables==3.5.2 #seems 3.4.4 is incompatible so should be upgraded

  8. Hello Andreas,

    Thanks for writing this excellent book. I had early awaited for this book once you announced about it on twitter and probably the first one to purchase it on Amazon. The information you have shared is not easily available to retail traders and you have shared enough information to enable retail traders to compete with professionals.

    I have already written my review on Amazon and have recommended this book on couple of popular trading forums. Thanks again and hope to see more such books from you.

    PS: Please have the book edited in next version as there are many typos and errors, nothing major but it will help improve the readability.

    Regards,
    Nilesh

  9. @Andreas, It looks like Amazon reviews from regional websites are not showing up in their global website, my review https://www.amazon.in/gp/customer-reviews/R2HQ7B4CWVWDL3/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASIN=B07VDLX55H is not shown at amazon.com ..:-)

  10. Hi Andreas,

    Just getting started with the book. Seems very promising. I was just going to start with the example on page 58, but can’t find the SPX data on the Dropbox link. AM I missing something? I’ve tried doing the exercise by taking the file from the stocks folder (editing to have just date and close), but I’m getting weird charts -it’s like both price and SMA jump around vertically a lot over very very short time-frames so it’s like someone has scribbled over the chart, just following the trend. I’m assuming there’s something I’m missing with those data as it relates with this exercise.

    Thank you very much

    Best,
    David

    • Hi David,

      The file you’re looking for is in the folder for that chapter. Under Chapter 6 folder, there’s a file called sp500.csv which contains historical data for that index.

      Andreas

  11. hello
    i bought this book from Amazon and tried to execute the example code of chapter7 “First Zipline Backtest.ipynb”

    i got these error responses:
    —————————————————————————
    ValueError Traceback (most recent call last)
    in ()
    3
    4 # Import Zipline functions that we need
    —-> 5 from zipline import run_algorithm
    6 from zipline.api import order_target_percent, symbol
    7

    ~\Anaconda3\envs\env_zipline\lib\site-packages\zipline\__init__.py in ()
    22
    23 from . import data
    —> 24 from . import finance
    25 from . import gens
    26 from . import utils

    ~\Anaconda3\envs\env_zipline\lib\site-packages\zipline\finance\__init__.py in ()
    14 # limitations under the License.
    15
    —> 16 from . import execution, trading
    17
    18 __all__ = [

    ~\Anaconda3\envs\env_zipline\lib\site-packages\zipline\finance\trading.py in ()
    21 from trading_calendars import get_calendar
    22
    —> 23 from zipline.assets import AssetDBWriter, AssetFinder
    24 from zipline.assets.continuous_futures import CHAIN_PREDICATES
    25 from zipline.data.loader import load_market_data

    ~\Anaconda3\envs\env_zipline\lib\site-packages\zipline\assets\__init__.py in ()
    14 # limitations under the License.
    15
    —> 16 from ._assets import (
    17 Asset,
    18 Equity,

    __init__.pxd in init zipline.assets._assets()

    ValueError: numpy.ufunc has the wrong size, try recompiling. Expected 192, got 216

    could you help me to solve these errors ?
    Thank you very much

    sorry,English is not my native language
    Hope you can understand what i mean

  12. Hi guys,

    I am unable to buy kindle copy of the book. Seems like it’s unable currently for purchase.
    I just want to confirm that is it just me who is getting this message or it’s really unable. I am curious whether amazon is blocking purchase from some countries . I have read free 6 chapters from the sample available on amazon and really like the book.
    https://www.amazon.com/Trading-Evolved-Anyone-Killer-Strategies-ebook/dp/B07VDLX55H/ref=gwm_tlc_pi?pf_rd_s=grid-6&pf_rd_t=Gateway-AmazonGlobal&pf_rd_i=mobile&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=DGTD604QC29CMG32XKT5&pf_rd_p=c47c1a64-54da-406b-819c-14c61f519944&pd_rd_i=B07VDLX55H

    • Hi Wajahat,

      The book is available on Kindle, and I can see quite a few Kindle copies sold today.

      At times Amazon have some odd regional rules. Based on your IP, I’m guessing that you’re in Pakistan, and perhaps Amazon.com doesn’t sell Kindle there. I had a similar issue with Australia.

      Sorry, this is all Amazon policy stuff, and I have no control at all over it. I suggest to contact Amazon customer support and asking.

      Andreas

  13. Hej Andreas,

    I got some of the errors like the “JSON decoding” stuff as well. The solution was to correct indentation errors in some files. That was perhaps the punishment for expecting code to run after I just copied and pasted it into my JP Notebook 😉

    (Win 10 Pro, Anaconda 1.9.7, Python 3.5, zipline 1.3.0)

    If somebody still has an issue with the ingestion of the data, you could try the following:
    – Start an environment from Anaconda with “Open Terminal” (environment is activated then)
    – At the prompt type: zipline bundles (that shows all available bundles on your machine)
    – Either it shows the bundles or you’ll see the error traceback which makes it easy to correct the certain files

    This worked for me as it showed some wrong indentations in the “benchmark.py” and “loader.py” files. The backtest run after correcting the typos.

    And not to forget: Thanks for the book, Andreas!

    Best regards
    Joerg

  14. Hi,
    thanks for the great book! I hope to have an Amazon review out soon.
    I wanted to seek some clarification on “ZIPLINE cannot handle other currencies”, i.e. only 1 main base currency which is rather disappointing for a global futures portfolio. Is there a workaround available?
    Having EUR.USD daily data and creating a look-up table to do on the spot currency conversion on Entry/Exit seems simple enough, what is the big issue with ZIPLINE and FX/Currencies?

    • Thanks, Matthias!

      The single world currency is a clear issue with Zipline. It’s of course a bigger issue for equities than futures.

      The good news is that Zipline is open source, and anyone can fork it. If you’d like to take a shot at fixing it…

      I didn’t dig into this part of the framework, but my understanding is that this is quite a deep issue and not easy to mend.

      A possible workaround for international equities would be to pre-adjust your time series before ingesting the bundle. That is, relcalculate all series to a common base currency first, then test your strategies on that.

      • Andreas,

        Regarding currency exposure when trading international futures, could I also use a pre-adjusted time series, too? Are there any other possible workarounds for international futures?

        Thanks,
        Nikolay

      • No, don’t use preadjusted series for futures. That’s a possible workaround for stocks, but wouldn’t work for futures. A closer approximation would be to just pretend it’s all dollar and import as-is.

        With futures you only have an fx exposure on the pnl, not the notional.

  15. Hi Andreas,
    another great book. Thanks so much, also for the two others before!
    At the end of the book when briefly discussing other backteting engines you are mentioning options trading. Are there any backtesting solutions you would recommend to retail options traders?
    Thanks,
    Markus

    • Hi Markus,

      My sincere advise to retail traders is to stay clear of options. In particular if the reason for using options it to gain leverage. Options is a pro game where hobby guys are at a severe disadvantage.

      Timeo Danaos et dona ferentes.

      Having said that, I am not aware of any decent retail backtester that properly supports options.

  16. After reading Stocks on the Move, I implemented my own modified version in a custom backtest engine in python. I researched the backtest engines available at the time, zipline included, and found out all of them were too complicated.

    Now with your book I finally was able to do a much more professional and robust backtest than with my home made engine, with zipline. Thanks Andreas!

  17. Hi Andreas/anyone

    I bought Trading Evolved from Amazon (Kindle format). I must say this is an AMAZING book. Thank you Andreas for sharing your invaluable knowledge. Your hard work and your effort are greatly appreciated. Finally I have a chance to set up my own testing environment with open source programming language.

    I have been following the book step by step. I managed to install everything including zipline. I have patched the framework (benchmark.py and loader.py) and it seems that it can ingest quandl ok. So I am up to “Your First Zipline Backtest” now.

    Here I am stuck. I try to run the code but it gives me errors so I am unable to reproduce the charts. I don’t know how to fix it. I have no experience in Python programming in the past.

    Is there a forum I can ask questions? It seems that people are asking technical questions on the Errata and Updates page but I am unable to post comment for unknown reasons…

    Can Andreas/anyone please point me to where I can ask technical questions from the book and get answers?

    Thank you so much for your help.

    • Hi Henry,

      Thank you for your kinds words, and thanks for pointing out the comments issue!

      I just figured out why you couldn’t comment. As comment spam has been increasing lately, I had set the site to automatically close comments on articles older than two weeks. That’s usually enough, but clearly not for a constantly updating article like the errata page. Should be fixed now.

      I had meant to set up a forum of some sort, but didn’t find the time. On advise, I looked into using GitHub for this as well, but I found the GitHub community quite hostile to that idea. Pretty much the same kind of replies that you tend to get on StackOverflow: “Instead of helping you with your issue, I’m going to tell you how stupid you are for trying this in the first place.” Odd why all these people sign up to such sites just to post replies of that sort. Reminds me of the standard reply that UNIX guys had to every question back in the 90s. “RTFM”.

      For now, here are some places where people have posted lots of questions and answers:

      https://www.followingthetrend.com/2019/08/trading-evolved-errata-and-updates/
      https://www.quantopian.com/posts/new-book-on-quantopian-slash-zipline-backtesting-and-modeling

      There’s some great advise in the Quantopian thread, that I haven’t yet copied over to this site.

      I’m following these discussions as well and will jump in to try to help out.

      Also, and I know I say this all the time: Please write an Amazon review for the book. These days, that’s the most important thing for a book, and as opposed to so many others, I refuse to pay for fake reviews or have ‘giveaways’ to people who review. It only matters when it’s a real person and real review.

      • Thank you Andreas. I will post my technical question on those two threads.

        I tried to leave a positive review on Amazon for your book. HAHA unfortunately I did not meet the minimum eligibility requirement: you need to spend at least $50 on Amazon in the last 12 months to be able to leave a review. I spent below that so very sorry I am unable to help you on that…

        If I get to a stage that I am eligible for doing reviews. I will definitely do one for your book.

      • Ah, that explains. I didn’t know that they started with that rule, but it makes sense. The amount of fake reviews on Amazon was really spiraling out of control, and hopefully these new rules are helping.

        A few years ago I saw a new book launched in the same space as my books, by an author I’ve met a few times. If you’ve had books on Amazon, you learn to interpret the Amazon Ranking numbers, and I could see on his that he probably sold about 300-400 books the first week. And he had 300 reviews in the same time…

        This particular author is a nice guy, so I won’t call him out, but it was painfully clear that he bought a few hundred fake reviews.

        My experience is that around half a percent of people who buy the book will post a review. Slightly higher if you keep bugging people about it. 🙂

      • Couldn’t agree more ref the petty techno-arrogance (a lá “RTFM”) from some in the likes of StackOverflow! But in reality, who needs them!

  18. Hi, Andreas. I purchased your kindle book. It is a great one. Really like it. I have a question on charter 12. For calculating annual slope, your code is ann_slope = (np.power(np.exp(slope), 252) -1)*100. Should it be ann_slope = (np.power(1+np.exp(slope)), 252) -1)*100?
    I will appreciate your response.

  19. Hi,

    I bought the book and I’m now trying to run your futures trading systems on my platform. I installed zipline and python 35 correctly, at least your equity model works. However when I’m trying to your your Jupyter Notebook files for the futures system I get an error message about the “bundle”.

    In your Trend model I get the message: “UnknownBundle: No bundle registered with the name ‘rand_fut'” and as well there is a message “widget Javascript not detected”.

    Can anyone help?

  20. Hello,

    Enjoying the book so far! Was wondering about the correlation examples in Chapter 6? I am using ndx.csv and sp500.csv to do the correlation. I noticed that the data for ndx vs sp500 begin at different dates? Do we need to clean the data up a bit to match in case we want to execute correlations from the beginning of the data instead of the last few rows? Thanks in advance!

  21. The book is fantastic, I’m so grateful that you put this together. I feel like you would be one of the most interesting people to discuss investing with. The way you approach investing and algo trading is logical sound and fascinating. Can’t thank you enough for the work you put into this.

  22. Hi Andreas,

    I just finished reading the book. Awesome one of a kind! Thanks so much. I also enjoyed your other two. Question: what is the copyright (if any) on the source code you have in the book. I want to incorporate some of it into my open source backtester, Pinkfish. How should credit your work if no copyright. I could add a comment at the beginning of each derived function or module at a minimum.

    Farrell

  23. Hi Andreas,

    thanks very much for writing this book. I had been developing algos only with “retail” platforms like Tradestation, Multicharts, Investox but since Im working on the Institutional side i finally got over the hump and started using Python. Your book is a blessing in learning to code algorithmic trading models with Python. Im half-way through and you can count on my positive Amazon review once im done.
    Greetings from Uetlihof, Zurich. 🙂

    Dom

    PS.: I found a few typos and will also send you those for the next version.

    • Glad you liked it, Dom. I should send an invoice to CS! 🙂

      I live just a couple of kilometer from Üetlihof…

      • 🙂 hahaha yes you should. please add a markup for my retrocessions :). How about CS (ok its gonna be myself) invites you to lunch/dinner some day?
        If yes, lets make a trade: If I find > 20 typo’s then the follow-up lunch is on you?

      • I’ll tack on an Agio for you as well.

        I’m not a big fan of the Retro/Agio business model that Zurich has been spinning on for so long, but at least it’s slowly on the way out.

        Well, if you’re a D, MD or higher, lunch is on you. I’ll drop you a mail.

  24. Hi Andreas, I am currently at the end of chapter 8. Everything has gone smooth untill trying to run a tear down sheet. First Issue came when I followed your code. There is an issue with the APIs of google and Yahoo (yes i know they are depreciated), but if bencmark_rets=None – it defaults to returns of SPY – which takes it from google or Yahoo. Which doenst work. Is there a way around this?

    Second, I tried going around by creating my own benchmark. I simply took your SPY.csv file, sliced to the correct timeline and calculated the returns. However, now I have the problem of :

    in _maybe_utc_convert
    raise TypeError(‘Cannot join tz-naive with tz-aware ‘
    TypeError: Cannot join tz-naive with tz-aware DatetimeIndex

    could you help me with this?

  25. Hi Andreas,

    Fantastic book !! I am enjoying it and learning so much from it.
    My focus is on the FX spot market, and I know the limitation of Zipline in this asset class.

    In your book, you give some comments on other backtesting engines, but from your knowledge, can you recommend it an engine that works with FX spot?

    Many thanks in advance

    • Thanks Fredy. Would be great if you could review it. 🙂

      I don’t much background in systematic spot trading and haven’t really investigated spot fx backtesters. I’m sure someone reading this could give advice though. Lot’s of smart people commenting and helping here and I’m very grateful for that.

      • Thanks for your reply, Andreas !! I much appreciate it.

        Hopefully, someone reading this could help me.

        I’ve done some research, and Quantconnect does Foreign exchange but its library “LEAN” is in C#.

  26. Hi, I’m a Chinese reader, Fantastic book ! But for some reason, i can’t visit dorpbox websize(maybe all Chinese reader can not visit this url), can not download the code and test data, can you replace download url ? or send a copy to my email,thank! Thanks very much!

    • Ni hao,

      I had no idea Dropbox was blocked in PRC. The file is too large for email. What other file sharing service works in China?

      • Thanks for your reply! I much appreciate it and sorry for my terrible english。

        I solved the problem. By the way, i have your other two books, very great and i love it。

  27. Hi Andreas or fellow readers,

    I’m going through your futures momentum system and woul want to understand which contracts you are trading. Can you tell me which contracts I find behind the abbreviations ? ( I did look on bloomberg and in the web, but to me it’s not clear):

    Currencies:
    – CU

    Agricultural
    – BL

    other commodities:
    – LG

    equities:
    – VX
    – YM

    rates:
    – ED
    – FV
    – TU
    – TY
    – US

    thanks in advance!

    • Hey Peter,

      You’re right, I should have put comments in the code for this. The symbols I used in the samples are all from CSI and they have a tendency to use non-standard tickers at times.

      CU – Euro currency futures
      BL – Milling wheat – Doh! This shouldn’t have been included. It’s quoted in Euro and should have been removed. My mistake.
      LG – Gas Oil
      VX – VIX Index futures
      YM – Dow futures
      ED – Eurodollar futures (stir)
      FV – US 5Y treasury
      TU – US 10y treasury
      TY – US 2y treasury
      US – US 30y bond

  28. All interested,

    I have created a project that keeps the Historical S&P 500 components since 1996 updated. I started with the CSV file provided with the source code for Trading Evolved, and update it with a changes file and python script.

    https://github.com/fja05680/sp500

    Thanks to Andreas for providing the starting point!

  29. Hi Andreas,
    I just bought the book from Amazon and I’m charging through it.
    I have a CSI futures data subscription and I’m considering using Norgate in order to introduce equities to my models.
    I checked Norgates futures markets and they cover all of the markets (44) that I am currently trading /monitoring.
    Should I just use Norgate for all data then?
    If not then Why do you prefer CSI dates over Norgate data for futures?

    Cheersm
    David

    • I used CSI for the book, but now that Norgate has made an excellent Zipline API, I would think that’s a better option. Easier anyhow. When I started out writing the book, Norgate didn’t have the necessary api to connect Zipline, but they do now.

  30. Thank you Andreas, that is helpful – and you’ve saved me the cost of the book 20 times over already!

    • Much appreciated, David. If you feel a need to pay the balance, I take payment in reviews and/or beer. I just came back from your country a few days ago, and I suppose I missed my chance at the beer option. I was speaking in the state South of yours, assuming the IP location software is accurate.

  31. Hello all,

    I am running the 1st Zipline Backtest from the book only to stumble on the following error:

    ~\.conda\envs\zip35\lib\site-packages\trading_calendars\calendar_utils.py in get_calendar(self, name)
    179 except KeyError:
    180 # We don’t have a factory registered for this name. Barf.
    –> 181 raise InvalidCalendarName(calendar_name=name)
    182
    183 # Cache the calendar for future use.

    InvalidCalendarName: The requested TradingCalendar, XNYS, does not exist.

    If anyone can advise how to get over the error I would greatly appreciate.

    • perhaps I should add that tried modyfing the

      cal = get_calendar(‘ NYSE’)

      in benchmarks.py

      to ‘NASDAQ’ and ‘XNYS’ but got the same error msg that the requested trading calendar does not exist.

  32. Where are the data files for chapter 8? Specifically short_etfs.cvs and oil_etf_vs_spot.csv? I downloaded the data files for the book from this site, but there are not in there. Thanks!

  33. Hi Andreas
    Reading your book and have question. Could you tell me in Asset Allocation Model (in your book) possible to use bundle =’random_stock_data’ in case of ETF back test?
    I tried to use that but for some reasons Equity curve looks like heart cardiogram. I assume the issue in rebalance. But cant understand exact reason.

    • The random data is just that. Random. I wouldn’t be surprised at all if it looks like a Jackson Pollock painting.

      • Hi Andreas, thanks for reply.
        Yes I have understood about random data in case of utilize your Excel files in Chapter 12 (Systematic Momentum). In Chaper 23 you proposed random_stock_data.py which allow to backtest data offline. So as I have ETFs data I wanted to utilize random_stock_data.py (as bundle) in ETF weight strategy (Chapter 10). Its almost working, generally Equity curve goes in right direction but in the middle of the month curve looks like zigzag and then continue corectly. I supposed that something wrong with rebalance comand. In order to understand whats going on I created Excel file with simple data series (prices) like 1st day 10, 2nd day 11,3rd 12, 4th day 13( OHLC same prices) etc which are not random, that show exactly up trend and in case of utilize 1ETF 100% weight we have to see straight ascending line. But I see straight ascending line with zigzag. Or am I wrong? Thanks for your time and attention.

  34. Hi Andreas,
    I bought Trading Evolved back in September, and I would like your offer of buying the Kindle version for $2,99. How can I get it at that price?
    The book is brilliant by the way, I’m looking similar content to expand my knowledge. Any suggestions on where to look?
    Thanks in advance,
    Hasse

    • Hi Hasse,

      I just got the same question a couple of days ago and started researching why this didn’t seem to work anymore.

      Turns out, Amazon discontinued their Matchbook program in November without stating a reason. Matchbook was what they called the system to allow for people who bought the print edition of a book to get a discount on the Kindle. Every author/publisher had the option to allow this, but now they suddenly stopped the whole thing without explanation.

      https://goodereader.com/blog/kindle/amazon-is-no-longer-offering-print-and-ebook-discounts

      Sorry, not much I can do about the odd policies of Amazon…

      • Hi Andreas,
        That’s no problem. I went and bought the Kindle version anyways, so that I have it at hand at any time 🙂
        Again, thanks for a great book. Funny thing is, that many of the things I knew already, but in your book you managed to connect the dots – bringing it to another dimension for me.
        PS. A small hint for everyone: On both Android and iOS devices, you can enable voice over/read aloud under the accessibility settings. This effectively turns the Kindle app into an audio book. There are lots of ways to adjust the voices to your liking.
        Hasse

  35. Hi Andreas,

    I am a total newbie…
    I checked Quantopian’s website and I am a bit confused on how to actually use an Algo for trading. I understand the backtesting part but I don’t understand how to get buy/sell signals from the Algo.
    Looking at your table of content, it seems that your book is also only focused on backtesting not on actual trading?

    Am I missing something? Does your book show how to use the Algo’s to actually get a signal to buy or sell? If so, in what chapter is that covered?

  36. In your book:
    For convenience, place this csv file in the same folder where you will save the Python code. You can put it anywhere you like of course, but if it’s not in the same folder you will have to specify a path in the code, so that we can find the file.

    This is NOT HELPFUL
    USING WINDOWS – where does Jupyter save the Python code?
    In which folder should I put the csv-file?
    After one hour searching the internet – I am no wiser
    and therefore stuck because your wording is more than vague…

    • Johann, this is just basic Windows usage. It depends on how you have installed your applications. Probably it’s under your documents folder. If you can’t find it, just make a new notebook with a unique name and search for it in Windows Explorer.

  37. Hi Andreas,

    Thanks for the book. I’ve been using Quantopian for a couple of years and I really learned and enjoyed with the Futures sections of the book.

    Which sources of data would you recommend for equities and futures? You previously mentioned CSI and Norgate. Would like to know options for minute data and daily data.
    Also, any recommendation for Fundamentals?
    Always thinking in affordable options for retail trader.

    Best,

  38. I am able to ingest data from Quandl and run examples till chapter 11. Can I run chapter 12 momentum strategy against Quandl data? I was hoping to get more familiar with content of the book and testing before subscribing to data.

    Another issue is that I get the following error:

    702 except KeyError:
    703 # no equity has ever held this symbol
    –> 704 raise SymbolNotFound(symbol=symbol)
    705
    706 if not as_of_date:

    SymbolNotFound: Symbol ‘TMC-200006’ was not found.

    I can see why this would happen because ‘TMC-200006’ is coming from index membership file. This will fail because ‘TMC-200006’ is not a valid symbol name. I don’t know how is this supposed to work.

    • Looks like chapter 11 and buyond WILL require norgate data. Symbol like TMC-200006 seems specific to Norgate (it means TMC stopped trading after 06/2000). I contacted Norgate and found MacOS is not supported.

      Has anyone succeeded in going past chapter 11 on MacOS? If yes, what commertial data did you use?

      Thanks
      Deepinder

  39. Hi, I just started reading the book trading evolved; and so far I am enjoying it.

    However, I am having problems with the code on page 59; the file I downloaded from your website; the sp500.csv does not match the format illustrated on page 59; I downloaded the file from this website.

    As a result I am having problems with the code. Can you tell me where I may find a file with a similar orientation as suggested on page 59; so that I may use it to learn.

    Thank you.

  40. Richard Hepworth

    Hey Andreas, really enjoyed the book, read from cover to cover in a day and now going back through following step by step. I’m having some real problems with the zipline installation, it’s the same issue that’s mentioned in the book, but the solution proposed, doesn’t work for me. The line:

    conda config – set restore_free_channel true

    Kicks out the following error

    usage: conda [-h] [-V] command …
    conda: error: unrecognized arguments: -set restore_free_channel true

    Google draws a blank!

    Any ideas why it’s not recognising this code? I’m on mac OSX 10.15.2

    • Richard Hepworth

      Ok think I sorted this, for some reason it wasn’t recognising the Python3.5 env I set up in Navigator, so I had to do the process manually.

      In terminal

      conda create -n zipline-env python=3.5
      conda activate zipline-env
      conda install -c Quantopian zipline

      Just in case anybody has the same problem

  41. Jimmy Hendricks

    Your book fills a huge gap in the zipline open source community. I’ve been able to run most of the examples in the book without fail.

    In the chapter on pyfolio however I cannot get the visuals to work. I run from a command line and it seems the charts must only be suited for a Jupiter notebook.

    I cannot seem to locate any help on the web so if you have any information on running Pyfolio from command line and fixing these format issues that would be great.

    I’m using MacOS and Anaconda install in a python 3.5 env.

  42. Hi Andreas,

    Questions about Fundamental data And Pipline Api in Zipline

    Because of this book, I start to learn quantopian/zipline framework again. I tried quantopian platform before for some simple backtests. But when I tried installing zipline on my own computer, there were so many questions I could not solve. This book really solved quite a lot for me. That’s why I’d like to try this library again. It even encouraged me to try seting up my own database which I didn’t think of.

    However, I’d like to learn more about how to handle fundamental data in zipline. Can we ingest them along with the data bundles? I love quantopian’s pipeline api to screen and filter out our universe with historical data, fundamental data and some estimates done by others. How can I set this feature up in zipline?

    • Hi Ken,

      The short answer is that it’s impossible, since Zipline doesn’t support fundamental data or custom fields.

      The longer answer is that it’s totally possible, with a bit of trickery. I haven’t done this myself, but it should work just fine. Here’s how I would approach it.

      Create your own internal ticker symbology. Say you’d like to add P/E ratio data. You might chose to make symbols such as IBM_PE, or similar. Make a custom bundle that ingests 1. your normal price data and 2. your custom fundamentals, using your own made up symbology.

      Zipline will think that these are stocks, but you know better and so will your code. In your actual algo, when you evaluate whether to buy IBM, you just pull the data for IBM_PE and use that for your decisions.

      TL;DR: Make fake stocks that hold your custom data.

      • Thanks for you answer. I believe it’d work fine too. It’ll save a lot of time of me researching.

        You said you haven’t tried this before. So how did you backtest with fundamental data in zipline before? Using csv files / Directly selecting from your own dataset?

        Again thanks a lot.

  43. Hi Andreas,
    I read your book and enjoyed it. However, I am still struggling making a universe in zipline. something similar to the code below in Quantopian. Could you please elaborate?

    from quantopian.pipeline import factors, filters, classifiers

    def Q500US():
    return filters.make_us_equity_universe(
    target_size=500,
    rankby=factors.AverageDollarVolume(window_length=200),
    mask=filters.default_us_equity_universe_mask(),
    groupby=classifiers.fundamentals.Sector(),
    max_group_weight=0.3,
    smoothing_func=lambda f: f.downsample(‘month_start’),
    )

    • Hi Amir,

      That function uses the Quantopian pipline, pulling their internal data. You’d have to do your own locally. There are examples of that in the chapters on equity models in the book.

      Andreas

      • Thanks Andreas. How do you ingest ETF data into zipline for example ETFs from quandl that costs $250/mo. Is there any tutorial for ingesting ETF into zipline?

      • ETFs are from a technical perspective no different from stocks, just ingest them as if they were stocks.

  44. Hi Andreas, thank you for writing the book, it is very informative, but I have problem with ingesting data that I hope you can help me with (google search does not yield much result in terms of guidance to ingest data).

    I am currently subscribed to the quandl continuous futures data: https://www.quandl.com/databases/SCF/documentation

    But looking at your example, it seems that ingesting require individual futures data ?
    Any easy way to input this data into zipline ? And once I get them inside, do I have to then update the bundle everyday manually to get my trading signal?

    thank you

    • Zipline is built for use with individual futures, not continuations. You may need to rebuild some zipline stuff if you really want to use continuations. And yes, you do have to update the bundle, manually or scripted.

  45. Hi Andreas!
    Thank you for your amazing work, your books have really steered me in the right direction getting in to trading and finance.

    I have a question regarding the zipline platform and how you are addressing an issue I’ve encountered when working with backtesting:

    I’ve found it quite problematic calculating available cash on the same bar as the trading is taking place.
    Let’s say I’m selling a few stocks and then want to buy new ones based on the cash I have available after selling. The zipline engine is built to place orders on the next day’s closing price not the current day (probably to prevent “look ahead bias”). This means that I can never be sure how much cash is going in and out at the same day as I’m rebalancing, due to price fluctuation causing no control over leverage. (Also, it’s worth mentioning that the limitOrder ExecutionStyle does not seem to be working as expected).

    I’ve made a workaround on this where I have a custom slippage class which is placing the orders on the same closing price as the day where the rebalancing is done. By doing this I’m able to track every order and easily have control over commission and costs for each trade at the same day as the rebalancing is handled, using the open orders.

    Thinking a bit critical on this workaround it introduces a bit of “look ahead bias” doesn’t it? My thinking is that in a real-life trading scenario you will not be able to do your calculations at the exact same price as the actual filled order. On the other hand, you’ll probably never trade in an unknown state where you do not know how much cash you have available which can cause negative cash balance.
    I have no experience with other backtesting engines or for that matter any real-life trading at the moment.

    My questions are then:
    Do you have any thoughts on the issue? Will the workaround paint an unrealistic picture of the real world rendering the results as useless, or is the impact minor? Also, is it another way I should be thinking about this when using zipline, is it a concept or something that I’m missing here?

    I’m sorry if this issue is not too closely related to this particular book. However, I really wanted to ask someone whom I trust on the subject and not getting answers from random people with no “real experience”. Also I thought it is some what relevance to the concept of selling unqualified stocks then “Start at the top of the rankings list and buy until you have no more cash.” which is described in “Stocks on the move”

    Thanks again Andreas, I’m really grateful for the way you write, full transparency and no bullshit are always much appreciated!!

  46. Thanks for writing the book, Andreas, it seemed absolutely promising. I’ve read with keen enthusiasm the first 10 chapters and installed + ingested successfully everything according to your guideline.

    Unfortunately I’m getting errors after errors, conflicts/incompatibilities and deprecation issues when running the files. I tried to follow leads on stackoverflow, github and the like, some tricks worked but it just ended up create more problems down the line or ever uninstalled key libraries like zipline which were impossible to re-install correctly. I re-created env and re-installed everything multiple times, tried to recreate your exact env, spent countless nights spend trying to fix things but at the end i could never get it to run properly. I’m probably too inexperienced with Python, so this is entirely my fault I presume!

    It’d have been great to have this book in a course content on udemy with a tech support for it, like some of the courses on the platform have. Just a thought. Will keep looking back here should you provide any update on the book / jupyter files that fix some of the deprecation issues. Thanks again for your work.

  47. Hi Andreas,

    I found your book to be extremely helpful with setting up my back testing software using Zipline.

    I wanted to make you aware, if you were not already, that when ingesting futures data into Zipline, it automatically rounds the data to 3 decimal places. I was running a test on JPY/USD and it cut off 3 digits.

    I ended up modifying _equities.pyx and us_equity_pricing.py (all in the zipline/data folder). I changed 1000 to 1000000, and 0.001 to .000001 for all instances.

    Sorry to bother you if you were aware of this, maybe it will help others. If you were aware of this, is this the correct fix?

    Thank you,
    Joe

    • Here is a much less involved solution for this Zipline rounding issue, that I have now adopted. Multiply all price data that extends beyond 3 decimal places and divide the contract size (contract multiplier) by the same multiplier used on the price data. Also remember to account for quotes in cents and percentage points. I perform this task on my CSI data prior to populating my SQL database used to create my data bundle.

      I have also found some floating point errors in the Zipline price data history during simulations. I have only seen this in the 3rd decimal location and have not quantified a probability of occurrence. Since I am looking at medium to longer term systems on daily data, my trades are not executed until the following day. My assumption is the floating point error will be accounted for in the simulation uncertainty (come out in the wash).

      While mentioning the issue of trades being executed the day after; I would like to reach out to ask if anyone has discovered a way to execute stops the day of the trigger. I have thought about scheduling a pre-market algorithm and submitting my stop trade if the trigger is reached by a day high or day low, while the high or low is NOT the close. Any suggestions would be much appreciated. The daily lag is negatively affecting my skew (trend system).

      -Joe

  48. Hi Andreas,
    Thank you for your book. Very approachable even for someone starting with python in financial domain.
    Do you see the examples of trading futures presented in the book can be applied also to E mini S&P 500 contracts?

    Thanks
    Tomasz

  49. Hi Andreas. I’ve been avidly working my way through all three of your books and all three of Rob Carver’s too. I’ve decided for my first trading system I’m going to opt for your momentum model from Stocks on the Move (Clenow 1 – Carver 0). After learning Python and SQL over the past couple of weeks I’ve built and tested both a notebook and securities database on US equities data. Stage 1, tick.

    However, as I believe there is some significant downside risk in the equity markets then I’m interested in including something in my portfolio that could take advantage of this. My idea was to widen my scope to include some safe haven ETFs which I’m hoping should be negatively correlated with the equity markets (I realise this is not always the case). However, as I’m based in the UK then I believe I’ll need to add an LSE dataset to simulate access to appropriate ETFs. This approach will also need some modification to the 200 MA rule to cope with the negative correlation. Last and by no means least there will be lots of fun and games with Zipline as it can only cope with one currency.

    I guess I have two questions:

    1. Is my logic sound on using the UK listed safe haven ETFs in a dual country momentum model?
    2. Have you successfully navigated the Zipline currency constraint? Could I be biting off more than I can chew?

    • Just be careful with the dual currency situation. The Sterling has been known to move a bit in the past years, and it’s reasonable to expect further volatility down the road. You need to have a plan for how to handle the currency impact.

      Adding multiple currency support to Zipline would be a neat trick. I haven’t had the need and haven’t actually tried, but from what I know of the framework it could be quite difficult. Let me know if you succeed!

  50. Dear Andreas,

    I would like to extend the new core TF strategy in this book (EMAC + Breakout) with multiple speeds, so I have a fast, medium and slow speed. What kind of moving average combinations would you recommend for each, and should each speed also have (a) different breakout look back period and (b) different stop loss distance?

    Best regards,
    Emre Tezel

  51. Hi Andreas,

    Thank you for the amazing book, its a bit rare to find a high quality book in this space.

    Do you mind hosting the code on GitHub so its easier to lookup the notebooks?
    I can do it if you want and add colab.research.google.com integration too, so everything can run from the browser.

    The end result would be something like this:
    https://github.com/ahmedengu/Lean/blob/master/Algorithm.Python/quantconnect_lean.ipynb
    which is runnable from the browser:
    https://colab.research.google.com/github/ahmedengu/Lean/blob/master/Algorithm.Python/quantconnect_lean.ipynb

    Best Regards,
    Ahmed

    • Hi Ahmed,

      Feel free to put the code up there if you like. I was initially planning on putting it on GitHub, but got a bit put off by the general attitudes there. My code samples relies on some CSV data among other things, and I was informed in a less than polite way that GitHub is not my file storage and that I should keep my csv files off it. Other questions and attempts at engaging on that site has all too often lead to the usual RTFM and ‘go google’ replies. It may not be as bad as Stackoverview, but it was enough for me to just move on and self host…

  52. Juan Pablo Grimaldi

    Hi Andreas,

    First of all I wanted you for the excellent work you have done in Trading Evolved. It tied all the loose ends I needed to finally start exploring the Algo trading world.

    In chapter 23, I’ve followed all the steps to build a MySQL database, using your random stock data to test it, but it loads the data very slowly into the database. I don’t get any issues interrupting the code, but it takes minutes to load a single row.

    Any help would be greatly appreciated!

    Regards,

    Juan Pablo

    • That sounds far too slow. Check your database installation and settings. There are various tricks to speeding up ingestion, but if it takes minutes per row it must be some local installation issue, not the code.

      In general, you’d want to try to send fewer and larger insert statements, as that’s much faster.

  53. Hi Andreas,
    Thanks for the amazing book. I really enjoyed it especially when I got to know about different financial instruments. One thing I need to address is the “stop loss” and its implementation in zipline. Will you please share an example using order or order_target_percent? I couldn’t find any valuable sources online.

    Thank you.

    • You just do the math in the daily trading routine. Calculate your stop loss level and enter an order. The function order_target_percent will set your position size to X%, so if you want to close your position you provide it with value 0. If you use order() you’ll need to provide number of shares/contracts to buy or sell.

      • Thanks Anders. The same function has a stop order which checks a value or a stop loss function but this method was simple. I am more looking into designing a complex risk algorithm not like personal trading.

  54. Hi Andreas,
    i have a question about importing my data in chater23. in terminal i got the following message.
    TypeError: ‘str’ object is not callable

    i might be misunderstood what to dowith extension.py.
    Do i have to add
    from zipline.data.bundles import register,random_stock_data
    register(‘random_stock_data’,random_stock_data.random_stock_data,calendar_name=’NYSE’)
    into extension.py which exists under zipline folder?

    Thank you

    • Impossible to tell without seeing your code, but I suspect that you have an issue with duplicate names. This is a common error if you accidentally give a string variable the same name as an existing function, and thereby reassign its meaning.

      e.g.
      print = “this is a string”
      print(print) # <-crash

  55. David Faulkner

    Andreas,

    Great book. I’ll be writing an Amazon review when I’ve finished going through it. In the meantime can you point me to where I can find the ac_equities_db bundle file as used in Chapter 10. I’ve been able to find all the other files (so far) but I can’t find this one anywhere.

    It has taken me a few days to get everything set up so that everything (or nearly everything) is working ok. but it’s been a great learning experience – even if a little frustrating at times. Many thanks to you and others (at Quantopian site) for helpful suggestions/solutions.

    I look forward to finishing the book and being in a position to develop/design/test my own systems. I had originally anticipated using Python to write my own backtester but I can see that, if I can master zipline, following your examples I should be able to develop a far better backtester – particularly when it comes to the analytics.

    HedgeHunter

    • Hi David,

      The name ‘ac_equities_db’ is just the name I used in development. The later chapters of the book, 23 and 24 I believe, explains how you can build your own bundle.

      Andreas

  56. Hello, Thanks for the book, it has been very interesting.
    How in earth do you get the benchmark to appear in the graphs in the pyfolio tearsheets , also the how does one get to compare the various ratio’s etc?
    I have managed to get the a set of returns from the benchmark to appear in the results, by adding a benchmark in.

    appreciate your time, thanks in advance.

    • OK Managed to figure it out, I pulled the data for SPXTR and calculated daily returns, put it into a CSV and then pulled it into PyFolio. One quick thing though I calculated log returns, I assume zipline uses those, they appear to from the doc’s.
      Hopefully will clear up any confusion, I originally tried to set the benchmark in the backtest in context with the following code:
      security = symbol(‘$SPXTR’)
      set_benchmark(security)
      that had odd results in pyfolio, the output appeared to be cumulative in Zipline and looked odd, using the log returns from a CSV looked much better.

  57. One of the reasons I bought your book was to step me though the install of zipline. It seems to be finicky to say the least. I get the following dump in Jypter when trying to run your “First Zipline Backtest” from this site. Please advise how I might get beyond this error.

    —————————————————————————
    JSONDecodeError Traceback (most recent call last)
    in ()
    71 handle_data=handle_data,
    72 capital_base=10000,
    —> 73 data_frequency = ‘daily’, bundle=’quandl’
    74 )

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\site-packages\zipline\utils\run_algo.py in run_algorithm(start, end, initialize, capital_base, handle_data, before_trading_start, analyze, data_frequency, data, bundle, bundle_timestamp, trading_calendar, metrics_set, default_extension, extensions, strict_extensions, environ, blotter)
    428 local_namespace=False,
    429 environ=environ,
    –> 430 blotter=blotter,
    431 )

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\site-packages\zipline\utils\run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, data, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter)
    157 trading_calendar=trading_calendar,
    158 trading_day=trading_calendar.day,
    –> 159 trading_days=trading_calendar.schedule[start:end].index,
    160 )
    161 first_trading_day =\

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\site-packages\zipline\finance\trading.py in __init__(self, load, bm_symbol, exchange_tz, trading_calendar, trading_day, trading_days, asset_db_path, future_chain_predicates, environ)
    101 trading_day,
    102 trading_days,
    –> 103 self.bm_symbol,
    104 )
    105

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\site-packages\zipline\data\loader.py in load_market_data(trading_day, trading_days, bm_symbol, environ)
    147 # date so that we can compute returns for the first date.
    148 trading_day,
    –> 149 environ,
    150 )
    151 tc = ensure_treasury_data(

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\site-packages\zipline\data\loader.py in ensure_benchmark_data(symbol, first_date, last_date, now, trading_day, environ)
    214
    215 try:
    –> 216 data = get_benchmark_returns(symbol)
    217 data.to_csv(get_data_filepath(filename, environ))
    218 except (OSError, IOError, HTTPError):

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\site-packages\zipline\data\benchmarks.py in get_benchmark_returns(symbol)
    33 ‘https://api.iextrading.com/1.0/stock/{}/chart/5y’.format(symbol)
    34 )
    —> 35 data = r.json()
    36
    37 df = pd.DataFrame(data)

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\site-packages\requests\models.py in json(self, **kwargs)
    824 # used.
    825 pass
    –> 826 return complexjson.loads(self.text, **kwargs)
    827
    828 @property

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    317 parse_int is None and parse_float is None and
    318 parse_constant is None and object_pairs_hook is None and not kw):
    –> 319 return _default_decoder.decode(s)
    320 if cls is None:
    321 cls = JSONDecoder

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\json\decoder.py in decode(self, s, _w)
    337
    338 “””
    –> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340 end = _w(s, end).end()
    341 if end != len(s):

    C:\Users\tbrug\Anaconda3\envs\zipline35\lib\json\decoder.py in raw_decode(self, s, idx)
    355 obj, end = self.scan_once(s, idx)
    356 except StopIteration as err:
    –> 357 raise JSONDecodeError(“Expecting value”, s, err.value) from None
    358 return obj, end

    JSONDecodeError: Expecting value: line 1 column 1 (char 0)

  58. I can’t get the first program to run in Zipline. When i use your source code for “First Zipline Backtest” it chokes in run_algorithm.

    Please advise

    —————————————————————————
    JSONDecodeError Traceback (most recent call last)
    in ()
    71 handle_data=handle_data,
    72 capital_base=10000,
    —> 73 data_frequency = ‘daily’, bundle=’quandl’

  59. Hi Andreas,

    Great book! For equities backtesting, can you share your thoughts on how to manage trading halts/suspension? The book’s logic is to forward fill missing data. Considering that we review positions monthly, it should be fine for trading halts due to pending announcement as these trading halt typically last a couple of days. For trading suspension (which can last up to a few months), the back tester is being “trick to think” that the volatility of the stock has decreased (as we forward fill last know data) and will indirectly buy more of this stocks. In extreme cases, we may end up with the suspended stocks having being allocated 99% of the portfolio with all other stocks are being sold off. Thanks!

  60. Hi Andreas!
    I have bought the printed edition of Trading Evolved and I’m fascinated by this new world you are discovering to me.
    I’m a software engineer, so I’m handling fine the programming part of your book, but I’m struggling a bit with the basic concepts of trading.
    Could you, or any of your readers, recommend a book that explains the basic trading concepts you mention in your book?
    Thank you for writing this book!

  61. Hi Andreas,

    Currently I’m working my way through your book and I love it so far. Thank you for providing this information!
    However, I run into a problem with installing the package nb_conda for the zip35 environment (which was written about on page 103). When I try to install the package I get the error message:
    —-
    UnsatisfiableError: The following specifications were found to be incompatible with each other:

    Output in format: Requested package -> Available versions
    —-
    I also tried to install it via the terminal using pip, but got a similar error message…
    Does anyone know how to solve the problem and install the package nb_conda?

    Greetings from Switzerland 😉
    Kay

    • Same issue as you Kay. I can’t get past this point. The nb_conda version it is trying to install is 2.2.1
      This is the text I get in the navigator pop up window:
      ***
      No packages will be modified

      UnsatisfiableError: The following specifications were found to be incompatible with each other:

      Output in format: Requested package -> Available versions

      ***

      This is what I get from the command line (from the zipline “activated” environment):
      **************************
      (zipline-env) PS C:\Users\EP> conda install nb_conda
      Collecting package metadata (current_repodata.json): done
      Solving environment: failed with initial frozen solve. Retrying with flexible solve.
      Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
      Collecting package metadata (repodata.json): done
      Solving environment: failed with initial frozen solve. Retrying with flexible solve.
      Solving environment: –
      Found conflicts! Looking for incompatible packages.
      This can take several minutes. Press CTRL-C to abort.
      failed

      UnsatisfiableError: The following specifications were found to be incompatible with each other:

      Output in format: Requested package -> Available versions

      (zipline-env) PS C:\Users\EP>
      **************************

      It doesn’t specify what is incompatible with what.
      Any help is appreciated. Thanks.

  62. Dear Andreas, really a nice book, looking forward to buying the next one. I enjoy your way of expelling and the fact that you take care of every single details, something that sometimes can create problems with non-expert programmers.
    I have a question for you and in general for the community, I was wondering how I can access and visualise data and data frame tables form the handle_data. In particular I wish to see the structures and the data of data.history functions. Just to be even more clear I copy and paste a peace of your code:

    hist = data.history(context.universe, “close”, context.history_window, “1d”)

    I wish to investigate the hist data frame. Is there a way to visualise this outside the run run_algorithm.

  63. Recently retired, involved in computer science masters / machine learning out of interest and to keep the mind active. Python’s very relevant in the masters but bought Trading Evolved as an interesting diversification from purely technical stuff. Investing is also an interest area, and having spent some years in financial services, looking forward to reading about the union of these two areas. Even better if some practical and financially worthwhile benefits emerge!

  64. Andreas,

    I read the book and it looks very promising. I say promising because I have not been able to run the different scripts/tests. I was stuck at the first zipline backtest (could not open a notebook on 3.5). I was able to move forward thanks to Edward Hayman’s steps from the Quantopian posts. I’m looking forward to running the rest of the scripts and, more so, to creating my own algos to test.

    Just a quick heads up on a message I got while re-creating the Python 3.5 environment using Edward’s steps:
    “DEPRECATION: Python 3.5 reached the end of its life on September 13th, 2020. Please upgrade your Python as Python 3.5 is no longer maintained. pip 21.0 will drop support for Python 3.5 in January 2021. pip 21.0 will remove support for this functionality”

    I hope this just means everything will continue to work but in an “unsupported” fashion, rather than everything stops working come January.

    Thanks.

  65. Hi Andreas,

    I am in the part of Momentum strategy (Chapter 12) but when I run the scripts there is one error that says:

    # Second, get the index makeup for all days prior to today.
    all_prior = context.index_members.loc[context.index_members.index < today]

    TypeError: Cannot compare type 'Timestamp' with type 'int'

    Can you help me finding the error?

    Thanks in adavance

  66. hello, the quandl, quantopian and zipline libraryes used in this book are still working or not?

  67. Hi,

    Can you provide a code to add support for multicurrency portfolio. As you have explained in the book that no serious algorithm could be imagined without diversification.

    I did run for a while a multi currency portfolio and i wish to see if i can replicate my portfolio using zipline

  68. Hi Andreas and everyone,

    Just finished reading this book.

    I understand running the code can be challenging given the dynamic nature of zipline and the python ecosystem. So, I’ve packaged the code into a docker environment.

    Here’s the repo:

    https://github.com/hsm207/trading_evolved

    The code runs on zipline 1.4.1 and Python 3.6.

    Feel free to raise an issue if you discover any bugs.

    • Thank you for that! Great work!

    • Hi Mohd,

      I get this when try to execute “docker build” command:

      executor failed running [/bin/sh -c pip install ‘numpy>=1.11.1,=0.17.1,=0.18.1,<1.0.0' && ./configure –prefix=/usr && make && make install && pip install TA-Lib && pip install matplotlib && pip install jupyter]: exit code: 1

      Is this because of something got outdated or because I use Mac M1?
      Thank you!

  69. Hi Andreas,

    As a newbie in python, I do enjoy very much the step-by-step teaching in your book. Currently, I am running Chapter 12 ‘s algo. One thing that I have problem understand is in the rebalance function where you concat two dataframes into the momentum_concat. My understanding is the concat should produce a new dataframe but when I run it in debug mode, I see momentum_concat becomes a series. If I add a line with similar code like this:
    momentum_concat = pd.concat([momentum_list1, momentum_list2])
    m= pd.concat([momentum_hist1, momentum_hist2])

    and I get:

    ipdb> type(momentum_concat)

    ipdb> type(m)

    Why their types are so different? Would there be impact on sequent codes?

    Thanks!

  70. Andreas,

    Quantopian closed down their Community Services a couple of weeks ago.
    https://www.quantopian.com/posts/quantopians-community-services-are-closing

    Can you please comment on the following:
    1. viability of the Zipline code base. Who is still developing it, if at all? Any better options as of this writing?
    2. Can you please comment on the data provider situation as of this writing?

    • Too bad about Q, but I can’t say I’m surprised. The writing has been on the wall for years.

      Zipline is still one of the best, perhaps the best, freely available Python backtester. Perhaps it will do better as an open source project now. None of the things in my book are affected by this though.

      Not sure what you’re looking for on the data provider part. In my view, Norgate is probably the easiest for those using Zipline, as they have an integrated api for that. Very easy compared to putting together your own.

  71. Andreas,

    In response to the earlier data question, understood about Norgate data… In the book you cover data sources but I believe that you also mention that it would be better to have a blog/discussion on this site regarding data providers since the situation changes, etc. If there is such a discussion/resource please let me know. If there isn’t, all clear that Norgate is the best option. Though a discussion could prove helpful.

    Lastly, I’ve been able to ingest data via the csvdir bundle method. In the book chapter 24 you cover and provide code for a database bundle. But you don’t instruct on how to ingest that bundle; you don’t provide an extension.py file for it. Perhaps I missed something but how do you implement the database bundle?

    Many thanks once again.

  72. Hi Andreas,

    I was also a member by Quantopian. I know your book from your post there.

    1.
    Where can I find your correction on the book?

    2.
    Do you also use the zipline for live trading? Seems there is quite a few materials about this.

    Regards

    Thomas

    • Hallo Thomas,

      Shame about Q, in particular how they just nuked five years of forum posts without any real notice.

      There was a great discussion around Trading Evolved on that site, and now that’s all in Davy Jones’ locker. You’ll find most of it on this site, but all the valuable discussions and comments are gone forever. Try this one for now: https://www.followingthetrend.com/2019/08/trading-evolved-errata-and-updates/

      I don’t use Zipline Live and never tried it. I met the coders a few years ago in NYC, but that’s as close as I got.

      I just got in contact with the QuantInsti guys and might do some projects with them. They have an interesting online backtester, based on Zipline. Have a look at https://blueshift.quantinsti.com/

      Full disclosure: I do not at this time have a commercial arrangement with them, but that might change in the near future.

  73. Hi Andreas,

    thanks for writing this great book. I am on installing zipline (page 92) and when I get to create a new environment my anaconda only lets me do it with a 3.8 python version,no other chances, and as I see zipline is only supporting python3.6 at its latest. how can i continue onward?

    Best regards and thank you,

    Javi

  74. I’ve written a blog post that describes the process to get you started with zipline plus ingesting futures data from CSI data via a zipline bundle: https://weisser-zwerg.dev/posts/trading_evolved_1

    Just in case this might be helpful for others.

  75. Hello,
    Running the The Futures Trend Model code in Jupyter notebook and come up with the following error

    Exception ignored in: ‘zipline.assets.continuous_futures.OrderedContracts.contract_before_auto_close’
    AttributeError: ‘NoneType’ object has no attribute ‘next’ (not sure if this is a critial error)

    but then it finally breaks here:

    ~\Anaconda3\envs\zip\lib\site-packages\zipline\assets\continuous_futures.pyx in zipline.assets.continuous_futures.OrderedContracts.contract_at_offset()

    ~\Anaconda3\envs\zip\lib\site-packages\zipline\assets\continuous_futures.pyx in zipline.assets.continuous_futures.OrderedContracts.contract_at_offset()

    KeyError: 0

    Im using your random futures bundle data for this.
    Any help is appreciated.

  76. Hi,

    I am on chapter 15 – Future Trends Following.

    When I attempt to run the ‘TrendModel’, the compilation throws an error stating:

    No bundle named: ‘futures’. This seems correct as I cannot find the bundle futures, but it also did not fix it’self when i used the bundle: core_trend (I moved this file to the same folder, and tried moving it one folder deeper within the same folder aswell).

    ———–

    Yet when I run chapter 16’s ‘TimeReturn’ test, it uses the bundle ‘futures’. It compiles pretty much perfectly. Yet I cant find the bundle it is referencing. I am unsure as to why chapter 16 works with the bundle: futures and why chapter 15 cant find it.

    • Hi Ben,

      You need to make your own bundle and connect it to your own data. The bundle named ‘futures’ was just the one I happened to use locally for this backtest. The details on how to construct such a bundle are near the end of the book.

  77. Hi Andreas Clenow,thank you for your great book,i read through it three times.
    But i face a problem i ingest the future data and they are good at DataPotal,but the create_continuous_future in volume are strange,The continuous_future only use few month in one year not all the 12 month.,Please help..thank you.

  78. Hi Andreas. I’m having issues with Chapter 12 and the Momentum Model. Downloading Zipline was a huge trauma with older versions of Pandas etc but finally got it installed. Now I keep getting an error which I’m told is zipline related. Is Zipline even still working now that Quantopian has gone??

    AttributeError: module ‘pandas.core.indexing’ has no attribute ‘get_indexers_list’

    Thanks for getting me into Pyhton. It’s been really good for finding Stat Arb cointegrated pairs!

    • So we meet again, Mr. Weasel…

      Zipline can be a little fickle, and you should definitely install it in it’s own environment and keep it neat and clean. I’ve been using Zipline for years and I still use it daily without any (serious) issues.

      The demise of Q was sad but not in any way unexpected. The good news is that Zipline has passed onto the open source community and is doing just fine.

      As for your specific error, it’s really the same case as for the other, similar questions in comment section. I haven’t seen the error myself, and while I can spend half an hour searching web forums, so can anyone else. And even if I find a possible solution, I couldn’t verify it as I don’t have the same error on my own machine.

  79. In Indonesia, our stocks market only contain around 700 stocks, our popular indexes only have 45-100 stocks. Should I use your method for all stocks we have (700)?

    Thanks,
    Jack

  80. I’ve been trying to replicate the futures trend following strategy backtest using futures data from Norgate (with a view to applying to to CFDs). I chose Norgate over CSI since Norgate provide a very nice zipline integration library: https://pypi.org/project/zipline-norgatedata/

    The trouble I’m having is that my backtest results don’t match up with the results in the book very well. For example. in the first month of the backtest Jan 2001 you record -4.6%, whereas I see a loss of -1.4%. For 2002 you record +18%, whereas I see just 1.8%. You can see the full table of returns here: https://imgur.com/a/blN9QWT

    The code and settings are identical (different pandas version, but I think it’s unlikely this would have a material impact).

    The only _data_ difference that’s obvious to me is that Norgate don’t provide a “TW” future. It’s not made too clear what the root symbols correspond to in the book (fyi, These are the other CSI->Norgate mappings I made https://pastebin.com/S5cJMJWF), but according to CSI data (http://www.csidata.com/factsheets.php?type=commodity&format=htmltable) TW is the exchange symbol for the MSCI Taiwan Index. Is that correct? If so, is it really a fair assumption that this would index would have been included in the investment universe 20 years ago? If not, any ideas where else the divergence in performance would be coming from?

    Thanks again, Mark

  81. Hello Mr. Clenow,

    I am trying to do the first zipline backtest, but I am running into huge problems where I don’t know where they come from. I use the code like in your book and I get an error message saying my variable where i assigned the historical data is not defined even though technically I have defined it.

    def handle_data(context,data):
    eq_hist = data.history(context.stock,”close”, context.index_average_window,”1d”)

    if eq_hist[-1] > eq_hist.mean():
    stock_weight = 1.0
    else:
    stock_weight = 0.0

    —————————————————————————
    NameError Traceback (most recent call last)
    in ()
    —-> 1 if eq_hist[-1] > eq_hist.mean():
    2 stock_weight = 1.0
    3 else:
    4 stock_weight = 0.0

    NameError: name ‘eq_hist’ is not defined

    I hope you see this and will try to help. I would even offer to do this via a zoom call because maybe I have to show if I installed zipline correctly…

    Sincerely
    Luca

  82. Hi Andreas,

    I thought it would be impossible to install Zipline and replicate the models in your book on a web based notebook instead of installing any software on PC, like Anaconda.
    Recently I came across an online environment for Jupyter notebooks called Datalore.
    The only issue I have with this online platform is with the ingesting of bundles. I end up getting this error:

    “ValueError: SQLite file ‘/home/jovyan/.zipline/data/quandl/2021-12-17T17;15;06.490671/assets-7.sqlite’ doesn’t exist.”

    I have passed all the necessary code to ingest quandl:

    csvdir
    quandl 2021-12-17 17:15:06.490671
    quandl 2021-12-17 17:14:33.839606
    quandl 2021-12-17 17:12:08.429244
    quantopian-quandl

    but I still get the ValueError

    I think it has got something to do with the platform being online and not creating a proper file directory to store the data in memory or something

    Would you know if there is any way to work around it ?

    Best

    Francois

    • Sounds like the ingest failed. Check your ingest code and output.

      • Hi Andreas,

        Thank you for getting back.

        I managed to ingest the bundle last night and ran the souce code again. The backtests are running without any issues.

        It was a conda update I did and a re-installation of pyfolio.
        There was also a TimeStamp issue – I found a solution to this problem on Github.

        I wanted to ask. Is your novel available on Kindle ?

        Best

        Francois

      • Good to hear you solved it. That error is something that you’ll see whenever an ingest failed. The folder was created, so Zipline checks the newest folder, finds nothing inside it or missing files, and crashes…

        In my view, it’s time to move away from Quandl though. After they were bought up, they’re not exactly focused on the whole free data for everyone anymore.

        And yes, my novel is available on Kindle, paperback and soon hardcover as well. I hope you like it, and please leave a review.

        https://smarturl.it/amostprivatebank

  83. Hi Andreas,

    the trend following code in the book seems to have a small glitch:

    after you identify a breakout, the algo just gets the current contract with:
    contract = data.current( continuation, ‘contract’)

    and then goes on to trade it.

    The problem with this approach is that quite often the continuation doesn’t roll to the next contract until just a few days until expiration. Sometimes it even rolls at day 0 (this one happens quite often with VIX futures). So, what happens in these cases is, say, the current contract has days_to_auto_close = 3; algo submits buy order on friday at market close. The order fills on monday, but since the contract expires on the same day, it enters and exits at the same time, and the net position is zero. The roll_futures() function doesn’t get a chance to do its job as it only works on existing positions. So, in that way the algo skips positions that it should have taken.

    One way to fix this is to check days to auto close of the contract that’s to be traded. If less than 5, then simply request the next contract in the continuation by calling it again with offset=1, and then submit an order with this contract.

Leave a Reply

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

*