Zipline Strategy Code

The strategy code is provided in sell-gap.py.

Install strategy file

To "install" the strategy, execute the following cell to move the strategy file to the /codeload/zipline directory, where Zipline looks:

In [1]:
# make directory if doesn't exist
!mkdir -p /codeload/zipline

!mv sell-gap.py /codeload/zipline/

Strategy overview

A summary of functions in the strategy file is provided below:

  • In make_pipeline(), imported from our helper module created earlier, we define the pipeline using the code from our earlier notebook.
  • In initialize(), we attach the pipeline to the algo and schedule the custom functions find_down_gaps, short_down_gaps, and close_positions.
  • In before_trading_start(), we gather the pipeline output for the day and, in live trading, initiate real-time data collection. (We also instruct the real-time service to automatically cancel data collection at 9:32 AM, shortly after our strategy has checked the opening prices.)
  • In find_down_gaps(), which is scheduled to run at 9:31 AM, we identify stocks that gapped down and opened below their moving average.
  • In short_down_gaps(), which is scheduled to run at 9:40 AM, we short the stocks identified earlier.
  • In close_positions(), which is scheduled to run 5 minutes before the market closes, we place market orders to liquidate our positions.

Next Up

Part 5: Backtest