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.