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-08"]
Let's pick a single stock, CSCO:
SID = "FIBBG000C3J3C9"
DATE = "2020-08-13"
trade = transactions[transactions.symbol.str.contains(SID)].loc[DATE]
trade
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()