Zipline help - using your own trades

Hi all,

I’m new to this forum, so hoping to get a little help or a push in the right direction.

Does anyone know if it is possible to supply zipline with a dataframe of transactions and have it run a backtest to produce performance metrics for you? I know this is out of scope of the ML4T book, but I wanted to try a new scenario.

Scenario / Use case

I have a strategy which has produced some trades based on a weekly rebalance. Based the rebalance trades, I open some discretionary positions and close others in my trading account.

Is there a way I can plug these historic and current discretionary trades into zipline so that I can compare output performance metrics against the automated strategy?

Effectively I want to compare performance of a strategy vs the market - but not sure if the above is the way to achieve it.

Logically, I would think you can, as you are effectively telling zipline what to buy/sell, price and quantity - the same way you would with a signal of some sort.

I’ve tried using ChatGPT to help with this scenario, but when I run the trades through zipline, they do not execute, and I just get back the starting capital every day of the backtest.

Any help or a push in the right direction would be appreciated.

N

My understanding is Zipline needs to ingest your data into a bundle before it can use it. That being said, I am yet to get Zipline to work for me. How’d you get it to work for you thus far?

That’s correct, you will need to ingest data into Zipline prior to running your backtest. For this, you need to make sure you have data stored on your machine which is accessible by zipline, along with it being in the correct format (OHLC + dividends). I’m using a custom bundle for my data, based on .csv files as input. I have put the necessary code and files paths in the extension.py file to register the bundle, then I just ingest it before running the algo’s.

When I mention supplying trades, I mean making zipline execute trades which I have already executed in the market.

An example of the code I’m trying to use is below - the “handle_data” function is the one I’m having issues with as zipline is not recognising the transactions when the algo runs.

Essentially, I want to pass trade_records into the algo, then have it run and execute these trades based on the transaction in the dataframe - from there, I wan’t to review / investigate the reporting metrics produced by zipline in pyfolio or from the run_algorithm output.

from zipline.api import order, record, symbol, set_commission, commission, set_slippage, slippage
from zipline import run_algorithm
from datetime import datetime
import pytz
import pandas as pd

# Global trade data
trade_records = pd.DataFrame({
    "date": ["2024-06-23", "2024-07-03", "2024-06-24", "2024-07-01", "2024-06-25"],
    "ticker": ["AAPL", "AAPL", "JPM", "GOOG", "NVDA"], 
    "price": [196, 216, 197, 183.2, 120.52]
    "action": ["buy", "sell", "buy", "buy", "buy"],
    "quantity": [50, 25, 100, 10, 20],
})
trade_records["date"] = pd.to_datetime(trade_records["date"])

def initialize(context):
    # Convert tickers to Zipline symbols
    context.assets = {ticker: symbol(ticker) for ticker in trade_records["ticker"].unique()}
    context.trade_schedule = trade_records

    # Set up commission and slippage
    set_commission(commission.PerShare(cost=0.005))  # $0.005 per share
    set_slippage(slippage.FixedSlippage(spread=0.01))  # 1 cent slippage

Thanks