Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
What price is used when buying or selling?
API

I am backtesing at minute level and I can see that when I buy or sell a security the price used to update my portfolio is the closing price of the next minute bar. Shouldn't it be used the closing price of current handle_event bar?

--------Example--------

In my algorithm I set the following in initialize:

set_commission(commission.PerTrade(cost=0.00))  
set_slippage(slippage.VolumeShareSlippage(volume_limit=0.25, price_impact=0.0))  

When I backtest I can see that every time I take long or short positions on "AAPL", the price used to update context.portfolio.cash is the close price of next handle_data:

---handle_data---
portfolio 100000.000000 cash 100000.000000 positions 0.000000 returns 0.000000
"AAPL" open 47.079513 close 47.155229 volume 1354710

Taking shot position "AAPL" (num -100): order_target(symbol('AAPL'), -100)

---handle_data---
portfolio 100000.000000 cash 104709.951340 positions -4709.951340 returns 0.000000
"AAPL" open 47.150943 close 47.099513 volume 498474

---handle_data---
portfolio 99999.285700 cash 104709.951340 positions -4710.665640 returns -0.000007
"AAPL" open 47.099656 close 47.106656 volume 490886

Take long position "AAPL" (num 100): order_target(symbol('AAPL'), +100)

---handle_data---
portfolio 99988.428340 cash 95266.905340 positions 4721.523000 returns -0.000116
"AAPL" open 47.106656 close 47.215230 volume 372385

2 responses

Hi Luca,

It is a form of built-in slippage. When real-money trading, orders are sent immediately to Interactive Brokers, as they are encountered in the code. So, in theory, your order could get filled almost immediately (10's of milliseconds delay?). For backtesting, the minutely closing price is used as a conservative best-estimate for the fill price.

Note that you can formulate your own slippage model. For example, see https://www.quantopian.com/posts/trade-at-the-open-slippage-model which was written for daily bars, but will work on minute bars, too. You can shift the fill price closer to the minutely open, if you prefer.

Grant

Thank you Grant, it explains the trick.