Zipline tutorial using Moonchart raises error

Hi, I'm following intro_zipline tutorials.

When i run following cell in Part7-Zipline-Parameter-Scans.ipynb notebook,

from moonchart import ParamscanTearsheet
ParamscanTearsheet.from_csv("winners_MOMENTUM_WINDOW.csv")

Error raises:

/opt/conda/lib/python3.9/site-packages/moonchart/perf.py:118: UserWarning: Found returns which are 25 standard deviations from the mean, consider removing them with the `trim_outliers` parameter
  warnings.warn("Found returns which are {0} standard deviations from the "
---------------------------------------------------------------------------
IntCastingNaNError                        Traceback (most recent call last)
Input In [10], in <cell line: 2>()
      1 from moonchart import ParamscanTearsheet
----> 2 ParamscanTearsheet.from_csv("winners_MOMENTUM_WINDOW.csv")

I didn't modify any code, and so far the other examples have worked fine.

Thank you in advance.

I think you're probably using the full dataset and some illiquid stocks are creeping into the portfolio and resulting in NaNs. The tutorial was written for the free sample data and needs to be made a little more robust for the full dataset. I'll post an update later with a solution.

You're right, I was using 'usstock-1d' as the default bundle, and replacing the default bundle with 'usstock-free-1min' solved the problem!

Thank you so much, and I hope the example to be a bit more bundle-robust.

The example strategy is designed to be as simple as possible, but the simplicity results in inadequate risk management when run on the full dataset. Here is what's going on:

The strategy only holds 3 positions at a time and takes fixed position sizes of $100K per asset. Using a simple momentum screen with short moving average windows in combination with the full dataset, the positions tend to be highly volatile stocks. As a result, the example strategy suffered steep losses in 2021 as stocks with big gains subsequently declined. Neverthless, the strategy keeps taking $100K positions as its captial dwindles and eventually runs out of capital. This results in a NaN CAGR, which caused the error you saw.

To make the strategy a bit more robust, I’m going to change the $100K fixed position sizes to be percentage based, so that the strategy doesn’t over-leverage. If you want to make this change in your copy, you can change this line in winners.py:

# otherwise, buy a fixed $100K position per asset
algo.order_target_value(asset, 100e3, style=MarketOrder())

to:

# otherwise, allocate 1/6th of capital per asset
algo.order_target_percent(asset, 1/6, style=MarketOrder())

Other possible solutions include holding more than 3 positions and/or filtering out highly volatile stocks.

1 Like