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:
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-02"]
column | amount | order_id | price | sid | symbol | txn_dollars |
---|---|---|---|---|---|---|
date | ||||||
2020-02-03 09:41:00 | -439 | 571593b1c8534b2c8e5da775987f0950 | 113.600910 | Equity(FIBBG000K82ZT8 [CHKP]) | Equity(FIBBG000K82ZT8 [CHKP]) | 49870.799270 |
2020-02-03 15:56:00 | 439 | 3fc0f5dd0c234182ac159bf35610bae0 | 113.604071 | Equity(FIBBG000K82ZT8 [CHKP]) | Equity(FIBBG000K82ZT8 [CHKP]) | -49872.187169 |
2020-02-06 09:41:00 | -202 | 50801cf0507c40dfbc09ef6abfeefd22 | 245.486332 | Equity(FIBBG000BCZYD3 [BDX]) | Equity(FIBBG000BCZYD3 [BDX]) | 49588.239064 |
2020-02-06 09:41:00 | -766 | 587fae042a074dadaded67a518da68b5 | 64.910521 | Equity(FIBBG005D7PF34 [CDAY]) | Equity(FIBBG005D7PF34 [CDAY]) | 49721.459086 |
2020-02-06 09:41:00 | -484 | 07b1673fcddb478f8a069e501b29a68c | 103.368980 | Equity(FIBBG000BH3GZ2 [YUM]) | Equity(FIBBG000BH3GZ2 [YUM]) | 50030.586320 |
2020-02-06 15:56:00 | 202 | 313461191c374d869b9b2f224c8a71fd | 253.095906 | Equity(FIBBG000BCZYD3 [BDX]) | Equity(FIBBG000BCZYD3 [BDX]) | -51125.373012 |
2020-02-06 15:56:00 | 766 | f5d3502a8ab2403d9812e2225d6e51a3 | 65.749719 | Equity(FIBBG005D7PF34 [CDAY]) | Equity(FIBBG005D7PF34 [CDAY]) | -50364.284754 |
2020-02-06 15:56:00 | 484 | 9d35328893c943a7ac93476be57cb592 | 103.806133 | Equity(FIBBG000BH3GZ2 [YUM]) | Equity(FIBBG000BH3GZ2 [YUM]) | -50242.168130 |
2020-02-07 09:41:00 | -434 | 8aa478626fa449da9b7a05bf7ad7319d | 114.935509 | Equity(FIBBG000BS1YV5 [TTWO]) | Equity(FIBBG000BS1YV5 [TTWO]) | 49882.010906 |
2020-02-07 15:56:00 | 434 | 3be18c93e902485e9576231d6164655e | 112.868850 | Equity(FIBBG000BS1YV5 [TTWO]) | Equity(FIBBG000BS1YV5 [TTWO]) | -48985.081117 |
2020-02-11 09:41:00 | -1061 | fb36dbe3424a4781a98e2787dd7b320e | 47.105864 | Equity(FIBBG000BNNKG9 [MAS]) | Equity(FIBBG000BNNKG9 [MAS]) | 49979.321704 |
2020-02-11 15:56:00 | 1061 | 623bff42fe5a48f2a01e63a106db2b19 | 47.669297 | Equity(FIBBG000BNNKG9 [MAS]) | Equity(FIBBG000BNNKG9 [MAS]) | -50577.123586 |
2020-02-12 09:41:00 | -2601 | 0598868281104f028f74c90e31ea5748 | 19.158251 | Equity(FIBBG003PHHZT1 [MRNA]) | Equity(FIBBG003PHHZT1 [MRNA]) | 49830.610331 |
2020-02-12 09:41:00 | -1898 | 557b82d263f94fab8da03481fda928bb | 26.257120 | Equity(FIBBG000BB5373 [WU]) | Equity(FIBBG000BB5373 [WU]) | 49836.014709 |
2020-02-12 15:56:00 | 2601 | eb19f5b577524765a00e7994c0b8e289 | 19.010701 | Equity(FIBBG003PHHZT1 [MRNA]) | Equity(FIBBG003PHHZT1 [MRNA]) | -49446.834601 |
2020-02-12 15:56:00 | 1898 | 6bf88d6d1b554d4da8773443852f09c6 | 26.092826 | Equity(FIBBG000BB5373 [WU]) | Equity(FIBBG000BB5373 [WU]) | -49524.182799 |
2020-02-13 09:41:00 | -385 | 769dad98a3d14042a102d8b465ce1f23 | 130.160940 | Equity(FIBBG000BLSL58 [IFF]) | Equity(FIBBG000BLSL58 [IFF]) | 50111.961900 |
2020-02-13 09:41:00 | -213 | 0a6aa8bc5f934da186745a2d3813486f | 234.749554 | Equity(FIBBG000CC7LQ7 [ZBRA]) | Equity(FIBBG000CC7LQ7 [ZBRA]) | 50001.655002 |
2020-02-13 09:41:00 | -928 | 09837d31a8e744fa930615f4d34ffeac | 53.703884 | Equity(FIBBG000FP1N32 [NTAP]) | Equity(FIBBG000FP1N32 [NTAP]) | 49837.204352 |
2020-02-13 09:41:00 | -1059 | 404c422735664664b2012a378c48f909 | 47.065876 | Equity(FIBBG000C3J3C9 [CSCO]) | Equity(FIBBG000C3J3C9 [CSCO]) | 49842.762684 |
2020-02-13 15:56:00 | 385 | 33f2873cc3ec46b59a3e6b94d6a0023f | 136.931067 | Equity(FIBBG000BLSL58 [IFF]) | Equity(FIBBG000BLSL58 [IFF]) | -52718.460795 |
2020-02-13 15:56:00 | 213 | b87e4f06f8aa49998e322505eedb6050 | 234.240251 | Equity(FIBBG000CC7LQ7 [ZBRA]) | Equity(FIBBG000CC7LQ7 [ZBRA]) | -49893.173463 |
2020-02-13 15:56:00 | 928 | 39404446aeae4398ae9d544a2f75272b | 55.161544 | Equity(FIBBG000FP1N32 [NTAP]) | Equity(FIBBG000FP1N32 [NTAP]) | -51189.912368 |
2020-02-13 15:56:00 | 1059 | 3cb7996fa0fa4d23a163584dee06d39a | 47.379210 | Equity(FIBBG000C3J3C9 [CSCO]) | Equity(FIBBG000C3J3C9 [CSCO]) | -50174.582860 |
2020-02-18 09:41:00 | -296 | ee5a8511a06d448f9274daaa82eade47 | 169.209222 | Equity(FIBBG000BMTFR4 [KLAC]) | Equity(FIBBG000BMTFR4 [KLAC]) | 50085.929712 |
2020-02-18 15:56:00 | 296 | 6420d720e32d4cf1b4547f35d5d6701f | 169.470826 | Equity(FIBBG000BMTFR4 [KLAC]) | Equity(FIBBG000BMTFR4 [KLAC]) | -50163.364496 |
2020-02-21 09:41:00 | -986 | 813bee6c29c742059d45b9647470e535 | 50.534835 | Equity(FIBBG000BFL116 [FSLR]) | Equity(FIBBG000BFL116 [FSLR]) | 49827.347310 |
2020-02-21 15:56:00 | 986 | ea8f8f9d55aa4d88b19e328b8f839bc7 | 50.610179 | Equity(FIBBG000BFL116 [FSLR]) | Equity(FIBBG000BFL116 [FSLR]) | -49901.636001 |
2020-02-24 09:41:00 | -27 | ac512812f7a445f3adc39d62dc1d6bf2 | 1782.984944 | Equity(FIBBG000BLBVN4 [BKNG]) | Equity(FIBBG000BLBVN4 [BKNG]) | 48140.593488 |
2020-02-24 09:41:00 | -175 | c26c99d95eb845818843634c526efbf7 | 282.795136 | Equity(FIBBG000CH5208 [UNH]) | Equity(FIBBG000CH5208 [UNH]) | 49489.148800 |
2020-02-24 09:41:00 | -241 | 2b5454d749564cf385d7bdd1bf18a914 | 205.623294 | Equity(FIBBG00KXRCDP0 [CI]) | Equity(FIBBG00KXRCDP0 [CI]) | 49555.213975 |
2020-02-24 09:41:00 | -245 | 4c9cdc9d2879499189ef9676867d6b47 | 202.969091 | Equity(FIBBG000FKJRC5 [EL]) | Equity(FIBBG000FKJRC5 [EL]) | 49727.427295 |
2020-02-24 09:41:00 | -395 | 6a6dd3fe440a48c08437621153da3e2c | 126.327090 | Equity(FIBBG001YV1SM4 [OKTA]) | Equity(FIBBG001YV1SM4 [OKTA]) | 49899.200748 |
2020-02-24 09:41:00 | -216 | 3dca7b076dec4009b14e976cb34a7cb8 | 153.254010 | Equity(FIBBG001CGB489 [VEEV]) | Equity(FIBBG001CGB489 [VEEV]) | 33102.866160 |
2020-02-24 09:41:00 | -148 | ed8684186c9b49a8a8c556aad4df22c7 | 225.232410 | Equity(FIBBG000BRXP69 [EW]) | Equity(FIBBG000BRXP69 [EW]) | 33334.396680 |
2020-02-24 09:41:00 | -235 | 02e6ff5000854cbba29d8d4841fb1e0c | 211.766451 | Equity(FIBBG00GVR8YQ9 [LIN]) | Equity(FIBBG00GVR8YQ9 [LIN]) | 49765.115985 |
2020-02-24 09:41:00 | -327 | 8e24b501451b480e8384839724d9c7d8 | 152.199327 | Equity(FIBBG000BC9938 [VMW]) | Equity(FIBBG000BC9938 [VMW]) | 49769.179766 |
2020-02-24 09:41:00 | -240 | 1ef3d66f91204bbdaaeb80ad2e982a53 | 207.057864 | Equity(FIBBG000D9D830 [ACN]) | Equity(FIBBG000D9D830 [ACN]) | 49693.887360 |
2020-02-24 09:42:00 | -109 | 3dca7b076dec4009b14e976cb34a7cb8 | 153.572914 | Equity(FIBBG001CGB489 [VEEV]) | Equity(FIBBG001CGB489 [VEEV]) | 16739.447659 |
2020-02-24 09:42:00 | -73 | ed8684186c9b49a8a8c556aad4df22c7 | 225.332380 | Equity(FIBBG000BRXP69 [EW]) | Equity(FIBBG000BRXP69 [EW]) | 16449.263740 |
2020-02-24 15:56:00 | 27 | 152916b11e8f422aade91658ac531095 | 1798.139280 | Equity(FIBBG000BLBVN4 [BKNG]) | Equity(FIBBG000BLBVN4 [BKNG]) | -48549.760560 |
2020-02-24 15:56:00 | 175 | e85b41b447574f84abd7e9c70f5852f2 | 278.353481 | Equity(FIBBG000CH5208 [UNH]) | Equity(FIBBG000CH5208 [UNH]) | -48711.859175 |
2020-02-24 15:56:00 | 241 | 76c2ee88452142c786f685d4d0ed359b | 201.020288 | Equity(FIBBG00KXRCDP0 [CI]) | Equity(FIBBG00KXRCDP0 [CI]) | -48445.889408 |
2020-02-24 15:56:00 | 245 | ac6d1690e67c4a3c9383377037925c4b | 198.629571 | Equity(FIBBG000FKJRC5 [EL]) | Equity(FIBBG000FKJRC5 [EL]) | -48664.244895 |
2020-02-24 15:56:00 | 395 | 2ad45f05130545ba96755c0de8f6d2ae | 127.138130 | Equity(FIBBG001YV1SM4 [OKTA]) | Equity(FIBBG001YV1SM4 [OKTA]) | -50219.561350 |
2020-02-24 15:56:00 | 325 | 47a207a8a6da477bbcd8fb43e5150dc7 | 154.236257 | Equity(FIBBG001CGB489 [VEEV]) | Equity(FIBBG001CGB489 [VEEV]) | -50126.783525 |
2020-02-24 15:56:00 | 221 | 183070bae3a3402e81ef74a6ee4b9561 | 226.868040 | Equity(FIBBG000BRXP69 [EW]) | Equity(FIBBG000BRXP69 [EW]) | -50137.836840 |
2020-02-24 15:56:00 | 235 | 528e8b4111fc43d397c71207faf6cc0a | 210.613165 | Equity(FIBBG00GVR8YQ9 [LIN]) | Equity(FIBBG00GVR8YQ9 [LIN]) | -49494.093775 |
2020-02-24 15:56:00 | 327 | bdeb4c4db0b7479a929606504c39d673 | 152.225654 | Equity(FIBBG000BC9938 [VMW]) | Equity(FIBBG000BC9938 [VMW]) | -49777.788858 |
2020-02-24 15:56:00 | 240 | 2c31706c36804634966bd4835605b6d4 | 204.711395 | Equity(FIBBG000D9D830 [ACN]) | Equity(FIBBG000D9D830 [ACN]) | -49130.734800 |
2020-02-25 09:41:00 | -772 | e6455bd472784a0cb6bc0c45857767b2 | 64.420668 | Equity(FIBBG0063GCHH8 [SHAK]) | Equity(FIBBG0063GCHH8 [SHAK]) | 49732.755696 |
2020-02-25 15:56:00 | 772 | 23ae7933cd9e43e69ccbde8fd186741b | 63.023902 | Equity(FIBBG0063GCHH8 [SHAK]) | Equity(FIBBG0063GCHH8 [SHAK]) | -48654.451958 |
2020-02-27 09:41:00 | -591 | a43cf670d8be495dbbbd8fdd1b7de67a | 83.724875 | Equity(FIBBG00GCTWDJ3 [CVNA]) | Equity(FIBBG00GCTWDJ3 [CVNA]) | 49481.401125 |
2020-02-27 09:41:00 | -790 | ddd101b7ada64201aba004bec4acdf69 | 63.101064 | Equity(FIBBG000BV8DN6 [TJX]) | Equity(FIBBG000BV8DN6 [TJX]) | 49849.840560 |
2020-02-27 15:56:00 | 591 | 5111220aee8e47a6be6e14028553a3c6 | 89.331792 | Equity(FIBBG00GCTWDJ3 [CVNA]) | Equity(FIBBG00GCTWDJ3 [CVNA]) | -52795.088777 |
2020-02-27 15:56:00 | 790 | 48296b8800ba407f851b91047daf05c2 | 60.538156 | Equity(FIBBG000BV8DN6 [TJX]) | Equity(FIBBG000BV8DN6 [TJX]) | -47825.143240 |
2020-02-28 09:41:00 | -1691 | dc4d746a9cb44c53be7897bf18ebc5f4 | 29.555131 | Equity(FIBBG000PVRDL2 [WPM]) | Equity(FIBBG000PVRDL2 [WPM]) | 49977.726183 |
2020-02-28 09:41:00 | -2644 | 5a91389017ec4c0ca8323b642c613252 | 18.874336 | Equity(FIBBG000BB07P9 [GOLD]) | Equity(FIBBG000BB07P9 [GOLD]) | 49903.744384 |
2020-02-28 15:56:00 | 1691 | 76b724acc6da4a9cb6b0bd1106780e07 | 28.458535 | Equity(FIBBG000PVRDL2 [WPM]) | Equity(FIBBG000PVRDL2 [WPM]) | -48123.382685 |
2020-02-28 15:56:00 | 2644 | fc585cab41f5466d944a0ae984315ed1 | 18.950684 | Equity(FIBBG000BB07P9 [GOLD]) | Equity(FIBBG000BB07P9 [GOLD]) | -50105.607174 |
Let's pick a single stock, TTWO:
SID = "FIBBG000BS1YV5"
DATE = "2020-02-07"
trade = transactions[transactions.sid==SID].loc[DATE]
trade
column | amount | order_id | price | sid | symbol | txn_dollars |
---|---|---|---|---|---|---|
date | ||||||
2020-02-07 09:41:00 | -434 | 8aa478626fa449da9b7a05bf7ad7319d | 114.935509 | Equity(FIBBG000BS1YV5 [TTWO]) | Equity(FIBBG000BS1YV5 [TTWO]) | 49882.010906 |
2020-02-07 15:56:00 | 434 | 3be18c93e902485e9576231d6164655e | 112.868850 | Equity(FIBBG000BS1YV5 [TTWO]) | Equity(FIBBG000BS1YV5 [TTWO]) | -48985.081117 |
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:
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:
# 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()
<matplotlib.legend.Legend at 0x7f5ac86ed370>