Trading using opening prices in daily simulations

78 views
Skip to first unread message

Dario Biasini

unread,
Jun 4, 2017, 5:24:00 AM6/4/17
to Zipline Python Opensource Backtester
Hello,

i have downloaded daily data for a set of tickers (open close) and would like to run an open to open (or open to close) simulation with daily frequency in zipline.

I am struggling to do this however as the simulation always seems to use closing prices and handle_data() on a daily simulation always seems to get called at market close (4pm eastern time/8pm utc time).

If I try to trade a stock zipline always uses the previous closing price (ie if i trade on 08/18 handle_data is called on 08/18 at 4pm and the trade uses the closing price of 08/17 instead of the opening price of 08/18 which is what i would like). I would like the rebalancing to happen in the "morning" of each trading day by using the opening prices.

Moreover, if i close a position it also uses the closing price on the previous day: so for example if i close a position on the on the 08/20 handle_data is called on the 08/20 at 4pm and uses the closing price on the 08/19.

I tried using 'schedule_function(rebalance, date_rules.every_day(), time_rule = AfterOpen(minutes=1) )' but this gives the same results as handle_data so I am guessing (which makes sense) that time_rules is not applied for daily simulations...

How can i force the rebalancing to happen at market open using the opening price without altering the data i import? (ie could, i guess, for example, replace the closing prices on the 08/17 with the opening prices on the 08/20 etc... to 'trick' zipline into using those instead... but would prefer not to)

What if i wanted to run an open to close simulation but close using the closing price of the day i decide to close, and not the previous closing?

Thank you,

Walt

unread,
Jun 23, 2017, 12:38:13 PM6/23/17
to Zipline Python Opensource Backtester
To execute at opening prices you have to create a new slippage model and make it use the opening price. I think if you copy the FixedSlippage model and just replace the line: "price = data.current(order.asset, "close")" by "price = data.current(order.asset, "open")" should do the trick. This file is in zipline -> finance -> slippage.py.

To trade open to close you have to create the same slippage model as before. The problems is that zipline doesn't let you change the slippage model during the backtest. So you have to find where the restriction is imposed and remove it. So you would order a security to execute at the open and then change the slippage model to be able to execute at the close and vice versa. I have done this before but I don't remember where the restriction is.

Hope this helps
Reply all
Reply to author
Forward
0 new messages