How do you build the cost of margin into the backtests in Zipline? Thanks.
Zipline doesn’t natively model margin interest costs. But the pyfolio tear sheet shows your leverage, from which you can calculate the interest cost that your strategy would incur over the backtest period, using whatever your assumed interest rate for margin loans is.
You could get more complicated by parsing the results CSV, looking at the available cash in the positions DataFrame, applying a daily margin rate to negative cash balances to get a daily margin interest cost, converting the daily interest cost to a percentage by dividing it by the portfolio value for that day, then subtracting that percentage from the returns DataFrame, in which case pyfolio will show the equity curve adjusted for margin costs. But I would start with the easier approach mentioned above.
Makes sense and thanks for the suggestions Brian.