Accessing order execution price when closing position

Hi there,

Is there a way to access the execution price of an order placed through the Zipline library, specifically when a position is fully existed? There are properties of the Order object that contain the amount of shares ordered and the amount filled, but the execution price is not offered.

When a position is opened, the execution price can be inferred from the cost_basis property of the Position object.

When a position is partially closed, the execution price can be inferred using the last_sale_price property of the same Position object.

However, when a position is entirely closed, the Position object appears to return None (I assume because the position is no longer active), and so the last_sale_price cannot be accessed.

Thanks in advance,
Paul.

Transactions (zipline.finance.transaction.Transaction) are returned in the backtest results and can be examined there. I'm not aware of a convenient way to access them from within the algorithm, if that's what you're asking for. I imagine the rationale for that API design is that analyzing execution prices would typically belong to post-trade analysis rather than be part of the strategy's trading logic.

I'm running custom analytics that correlate return on individual transactions with dozens of pricing indicators that are most efficiently calculated during the backtest execution (when consistently adjusted historical prices are already queried). I suppose I could lookup the prices after the backtest completes - it just adds an extra step/script to connect the standard backtest results to the indicators I'm collecting.

Since zipline already records trade execution prices to user accessible objects, seems it would be an easy fix to expose the final exit price during the actual backtest (e.g. before the Position object becomes inaccessible).

Zipline is a complex library and unfortunately in this case it would not be an easy change to expose the transaction objects.

You can reuse code for your strategy and analysis script, and use the Data object in a research notebook to get the price history exactly as you would in a backtest.