QuantRocket logo
Disclaimer


Zipline Intro › Part 8: Debug Zipline Strategies


Debugging Zipline Strategies¶

The recommended workflow for developing a Zipline strategy is to start in a notebook, where you can develop your code interactively, then transfer the finished code to a .py file for backtesting. Once you've transitioned to the backtesting phase, you can use JupyterLab's built-in debugger to debug your strategy. The debugger allows you to set breakpoints and inspect variables as they change over the duration of your backtest simulation. This notebooks explains how to use the debugger.

Introducing the %zipline_backtest IPython magic command for debugging¶

To debug Zipline with the JupyterLab debugger, we can't use the quantrocket.zipline.backtest function that we used in an earlier lesson. That's because this function executes the backtest in a separate Docker container from JupyterLab, the zipline container. Instead, we can use a special IPython magic command, %zipline_backtest, that runs the backtest inside JupyterLab. The magic command works similarly to quantrocket.zipline.backtest but lacks some of its bells and whistles and is only intended for debugging purposes.

The debugging steps are as follows:

  1. Open the algorithm file winners.py in the JupyterLab editor.
  2. Right-click in the file and select "Create Console for Editor"
  3. Select the entire contents of the file (Ctrl+A on Windows or Cmd+A on Mac), then click Ctrl+Enter to load the algorithm into the console.
  4. Enable the debugger by clicking the debugger icon in the editor or console.
  5. Set breakpoints by clicking (in the editor or console) next to the line(s) you want the debugger to stop on. A red dot will appear beside the line.
  6. Run the backtest by typing the %zipline_backtest magic command in the console, specifying a start date and end date and, optionally, the bundle name. (Run %zipline_backtest? to see the function docstring and all available options.)
%zipline_backtest -s 2017-01-01 -e 2017-02-01
  1. The backtest will begin to run and the debugger will pause execution at the first breakpoint.
  2. In the Variables section of the Debugger window in JupyterLab, click the magnifying glass next to the variable name to inspect the value of the variable. This will open a new tab displaying the variable's value.
  3. In the Callstack section of the Debugger window, click the Play icon (▷) to resume execution. Execution will resume until the next breakpoint is reached.

See the video below for a step-by-step demonstration.

In [1]:
from IPython.display import VimeoVideo
VimeoVideo('925185155', width=700, height=394)
Out[1]:
⚠️ Due to an open issue in the current version of JupyterLab used in QuantRocket, please use Ctrl + Enter to copy code from the file editor to the console, not Shift + Enter as stated in the video.

Back to Introduction