The strategy code is provided in sell-gap.py.
To "install" the strategy, execute the following cell to move the strategy file to the /codeload/zipline
directory, where Zipline looks:
# make directory if doesn't exist
!mkdir -p /codeload/zipline
!mv sell-gap.py /codeload/zipline/
A summary of functions in the strategy file is provided below:
make_pipeline()
, imported from our helper module created earlier, we define the pipeline using the code from our earlier notebook.initialize()
, we attach the pipeline to the algo and schedule the custom functions find_down_gaps
, short_down_gaps
, and close_positions
.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.)find_down_gaps()
, which is scheduled to run at 9:31 AM, we identify stocks that gapped down and opened below their moving average.short_down_gaps()
, which is scheduled to run at 9:40 AM, we short the stocks identified earlier.close_positions()
, which is scheduled to run 5 minutes before the market closes, we place market orders to liquidate our positions.