QuantRocket logo

Trade inspection

This notebook shows how to inspect individual trades.

First, we can load the transactions and identify a trade to look at. Here we load transactions for a particular month:

In [1]:
from quantrocket.zipline import ZiplineBacktestResult
result = ZiplineBacktestResult.from_csv("sell_gap_backtest_results.csv")
transactions = result.transactions

# Set UTC to America/New_York time for convenience
transactions = transactions.tz_convert("America/New_York").tz_localize(None)
transactions.loc["2020-08"]
Out[1]:
columnamountorder_idpricesidsymboltxn_dollars
date
2020-08-06 09:41:00-9411bb86b1d46584cc1ae0955a7dd2993e553.280Equity(FIBBG000D898T9 [CAH])Equity(FIBBG000D898T9 [CAH])50136.480
2020-08-06 09:41:00-1318c2b1a8bddcdf430390c62bab183fcbab37.900Equity(FIBBG000BWNFZ9 [WDC])Equity(FIBBG000BWNFZ9 [WDC])49952.200
2020-08-06 15:56:0094190756e66fc95473fb9fcdb25bb3b29bc53.000Equity(FIBBG000D898T9 [CAH])Equity(FIBBG000D898T9 [CAH])-49873.000
2020-08-06 15:56:001318534a2eed6ac3482cb8ed95e5059f725737.295Equity(FIBBG000BWNFZ9 [WDC])Equity(FIBBG000BWNFZ9 [WDC])-49154.810
2020-08-07 09:41:00-1422f3cd04eedcd4e20b986568928d8d173350.695Equity(FIBBG000DSMS70 [ILMN])Equity(FIBBG000DSMS70 [ILMN])49798.690
2020-08-07 09:41:00-409faa1bf2239b44f759bb3fc19c6d00e70121.770Equity(FIBBG000BGZT72 [AYX])Equity(FIBBG000BGZT72 [AYX])49803.930
2020-08-07 09:41:00-209ab49ad4ffc484306a4f1d65fb0b461c6237.735Equity(FIBBG000GPXKX9 [FLT])Equity(FIBBG000GPXKX9 [FLT])49686.615
2020-08-07 09:41:00-389140cd87845ab496fba9c9b89252862c6128.510Equity(FIBBG000BCMBG4 [FTNT])Equity(FIBBG000BCMBG4 [FTNT])49990.390
2020-08-07 09:41:00-4805201bea2ec574fcba4e74792dcd90134104.165Equity(FIBBG000R5HWZ1 [XLRN])Equity(FIBBG000R5HWZ1 [XLRN])49999.200
2020-08-07 09:41:00-10849951ae92da4b47578cea0f17bf59db9846.080Equity(FIBBG000BZ9223 [EOG])Equity(FIBBG000BZ9223 [EOG])49950.720
2020-08-07 15:56:001423d76619466854e8fa914d64bf3ee78c7353.960Equity(FIBBG000DSMS70 [ILMN])Equity(FIBBG000DSMS70 [ILMN])-50262.320
2020-08-07 15:56:004093bb57b969b3a4678990327cb42537d25121.860Equity(FIBBG000BGZT72 [AYX])Equity(FIBBG000BGZT72 [AYX])-49840.740
2020-08-07 15:56:00209d8bf7b277e3e47ab9bc2c68968b3006a241.020Equity(FIBBG000GPXKX9 [FLT])Equity(FIBBG000GPXKX9 [FLT])-50373.180
2020-08-07 15:56:003894e0acb8bc56c4340abca9d30d75948ca131.080Equity(FIBBG000BCMBG4 [FTNT])Equity(FIBBG000BCMBG4 [FTNT])-50990.120
2020-08-07 15:56:00480ed16464f95b144ef868b6e3c30ca44cd99.290Equity(FIBBG000R5HWZ1 [XLRN])Equity(FIBBG000R5HWZ1 [XLRN])-47659.200
2020-08-07 15:56:001084aa43e9157c4d41218f23b766d288800048.900Equity(FIBBG000BZ9223 [EOG])Equity(FIBBG000BZ9223 [EOG])-53007.600
2020-08-13 09:41:00-1160231407663f014f739aa776979030b63a43.028Equity(FIBBG000C3J3C9 [CSCO])Equity(FIBBG000C3J3C9 [CSCO])49912.480
2020-08-13 15:56:0011605e4a595c5ade443e9428ad8962f3c02442.760Equity(FIBBG000C3J3C9 [CSCO])Equity(FIBBG000C3J3C9 [CSCO])-49601.600

Let's pick a single stock, CSCO:

In [2]:
SID = "FIBBG000C3J3C9"
DATE = "2020-08-13"

trade = transactions[transactions.symbol.str.contains(SID)].loc[DATE]
trade
Out[2]:
columnamountorder_idpricesidsymboltxn_dollars
date
2020-08-13 09:41:00-1160231407663f014f739aa776979030b63a43.028Equity(FIBBG000C3J3C9 [CSCO])Equity(FIBBG000C3J3C9 [CSCO])49912.48
2020-08-13 15:56:0011605e4a595c5ade443e9428ad8962f3c02442.760Equity(FIBBG000C3J3C9 [CSCO])Equity(FIBBG000C3J3C9 [CSCO])-49601.60

We use the data object to load minutes prices for this sid and date, looking back 390 minutes from the session close to get the entire trading day:

In [3]:
from zipline.research import get_data, sid
data = get_data(f"{DATE} 16:00:00")

minute_prices = data.history(sid(SID), "close", 390, "1m")

# Zipline timestamps are in UTC, convert to New York time for convenience
minute_prices.index = minute_prices.index.tz_convert("America/New_York").tz_localize(None)

Then we plot the minute prices and add trade markers for our buy and sell transactions:

In [4]:
# Plot minute prices
ax = minute_prices.plot(title=f"Buy and sell transactions for {SID} on {DATE}")
    
# Add the trade markers
trade[trade.amount<0].price.plot(ax=ax, marker="v", color="red", label="Sell")
trade[trade.amount>0].price.plot(ax=ax, marker="^", color="green", label="Buy")

ax.legend()
Out[4]:
<matplotlib.legend.Legend at 0x7fc3f333b860>