Skip to content
All library documents

Testing Backtest Sensitivity to Additional Slippage in Pyfolio

Notebook pyfolio

Summary

This tutorial explains how to use Pyfolio’s transaction tear sheet to examine how strategy performance changes under different slippage assumptions. It describes the `slippage` argument to `create_full_tear_sheet`: a specified basis-point penalty is applied to returns before most tear-sheet plots are produced, while the slippage sweep plots show performance under added slippage assumptions. The example loads returns, positions, and transactions, aligns their indexes to UTC, and generates a tear sheet.

A central caveat is that the sweep represents slippage added on top of whatever is already reflected in the input returns. If those returns already include simulated transaction costs, setting a positive argument applies another penalty to the main tear-sheet calculations; passing zero allows the sweep plots to be generated without further adjusting those returns. The document illustrates the workflow with notebook output images but gives no numerical findings or strategy-specific conclusions. Results depend on the supplied backtest data and the assumptions used for the slippage sweep.

Key ideas

  • Pyfolio can plot strategy performance across different slippage assumptions in its transaction tear sheet.
  • The slippage argument applies a basis-point adjustment to returns used by most tear-sheet outputs.
  • Slippage sweep plots represent additional slippage relative to the unadjusted returns passed to the plotting process.
  • When input returns already include a slippage penalty, setting the argument to zero avoids applying another penalty to the main analysis.
  • The example demonstrates setup and plotting but reports no numerical conclusions.

Tags

Full text
# Slippage Analysis


# Slippage Analysis

When evaluating a strategy using backtest results, we often want to know how sensitive it's performance is to implementation shortfall or slippage. pyfolio's transactions tear sheet can create "slippage sweep" plots that display strategy performance under various slippage assumptions. 

Additional per-dollar slippage can be applied to returns before running a tear sheet by providing `create_full_tear_sheet` with the a level of slippage in basis points (1% == 100 basis points) as the `slippage` keyword argument. The slippage plots in the transactions tear sheet will display returns with slippage added to the **unadjusted** returns. 

For example, if you run a backtest with no transaction costs and call `create_full_tear_sheet(returns, positions, transactions, slippage=5)`, 5 bps of slippage will be applied to `returns` before all plots and figures, with the exception of the slippage sweep plots, are generated.

It is important to emphasize that the slippage plots will display performance under **additional** slippage. If the passed performance data already has slippage applied, the 5 bps slippage equity curve will represent performance under 5 bps of slippage in addition to the already simulated slippage penalty. If slippage is already applied to the performance results, pass `slippage=0` to the `create_full_tear_sheet` to trigger the creation of the additional slippage sweep plots without applying any additional slippage to the returns time series used throughout the rest of the tear sheet.

```python
%matplotlib inline
import pyfolio as pf
import gzip
import pandas as pd

# silence warnings
import warnings
warnings.filterwarnings('ignore')
```

```python
transactions = pd.read_csv(gzip.open('../tests/test_data/test_txn.csv.gz'),
                    index_col=0, parse_dates=True)
positions = pd.read_csv(gzip.open('../tests/test_data/test_pos.csv.gz'),
                    index_col=0, parse_dates=True)
returns = pd.read_csv(gzip.open('../tests/test_data/test_returns.csv.gz'),
                    index_col=0, parse_dates=True, header=None)[1]
returns.index = returns.index.tz_localize("UTC")
positions.index = positions.index.tz_localize("UTC")
transactions.index = transactions.index.tz_localize("UTC")
```

```python
pf.create_full_tear_sheet(returns, positions, transactions, slippage=0)
```

```python

```
![notebook output](figures/p1_1.png)
![notebook output](figures/p1_2.png)
![notebook output](figures/p1_3.png)
![notebook output](figures/p1_4.png)

Shown in full with attribution under the source's licence. Licence: Apache-2.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.