How to make the order filled on the same day?

64 views
Skip to first unread message

Nick

unread,
Aug 21, 2017, 10:58:44 PM8/21/17
to Zipline Python Opensource Backtester
Hey All, 

I'm running zipline on my local machine. 

Previouly, I'm using following to run my algorithm. 

def initialize(context):
    context.day = 0
    schedule_function(handle_daily_data, date_rules.every_day())

where

def handle_daily_data(context, data):
    sym = context.security
    context.day += 1
    if context.day%10 == 0:
        if np.floor(context.day/10)%2 == 0:
            order_target(sym, 100)
        elif np.floor(context.day/10)%2 == 1:
            order_target(sym, 0)

The code above is a test. For the 10, 30, 50, ... days, I put a buy order; for the 20, 40, 60, ... days, I put a sell order. I expect the order to be filled on the same day, because in my real trading system, I can just use 5 minutes before close price information as Close, to do the real calculation, and if certain criteria met, I can buy/sell on the same day. 

Zipline is trying to mimic the real market, that's probably why if I put order "after market is close", it will not filled and will get filled the next day. But I do want to have the order fill the desired day. I can use different slippage model to make it more realistic to the real market. 

Is there a way to do this? I tried the following, by using the BeforeClose

from zipline.utils.events import BeforeClose

schedule_function(handle_beforeClose, time_rule=BeforeClose(minutes=30))

  def handle_beforeClose(context, data):
    sym = context.security
    context.day += 1
    if context.day%10 == 0:
        if np.floor(context.day/10)%2 == 0:
            order_target(sym, 100)
        elif np.floor(context.day/10)%2 == 1:
            order_target(sym, 0)

But still, the order doesn't get filled on the same day. 

Any suggestion is welcome. Thanks in advance. 

Ed Bartosh

unread,
Aug 22, 2017, 3:39:28 AM8/22/17
to Nick, Zipline Python Opensource Backtester
Hi Nick,

Let me guess. You're using daily time frame, right?

In this case filling orders at the same bar would introduce look ahead bias as close price is not known until the day end.
If you want to fill orders during the day you may want to consider using minute time frame.

Regards,
Ed

--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
BR,
Ed
Reply all
Reply to author
Forward
0 new messages