Structuring Zipline/PyFolio backtesting modules
Typical Zipline backtesting code defines three functions:
initialize
: This method is called before any simulated trading happens; it's used to enrich the context object with the definition of tickers and other key trading information. It also enables commission and slippage considerations.handle_data
: This method downloads the market data, calculates the trading signals, and places the trades. This is where you put the actual trading logic on entry/exit positions.analyze
: This method is called to perform trading analytics. In our code, we will use pyfolio's standard analytics. Notice that thepf.utils.extract_rets_pos_txn_from_zipline(perf)
function returns any returns, positions, and transactions for custom analytics.
Finally, the code defines the start date and the end date and performs backtesting by calling the run_algorithm
method. This method returns a comprehensive summary of all the trades to...