I am doing backtest on CMEGROUP currency future BP. i find one thing which is very difficult to understand. When i try to close the position at 12:17, i get the amount / status as 1 / 0 at 12:18. This means that there is still open position and the order status is still open. If i change the time from 12:18 to 12:19 and re-run the program, the amount / status becomes 0 / 1, which is correct. My question is : I try to close the open position at 12:17, and so i expect everything should be done finished completed by 12:18 (this is future and bid/ask spread and volume should be no problem) but now it is obvious that the position can only be closed at 12:19. Anyone knows why ?
def initialize(context):
context.underlying = continuous_future('BP',roll='calendar')
def handle_data (context,data):
if str(get_datetime('US/Eastern').date()) == "2017-01-26" and str(get_datetime('US/Eastern').time()) == "12:11:00":
context.orderid=order(data.current(context.underlying,'contract'),1) # create new position
if str(get_datetime('US/Eastern').date()) == "2017-01-26" and str(get_datetime('US/Eastern').time()) == "12:17:00":
context.orderid=order_target(data.current(context.underlying,'contract'),0) # close position
if str(get_datetime('US/Eastern').date()) == "2017-01-26" and str(get_datetime('US/Eastern').time()) == "12:18:00":
print context.portfolio.positions[data.current(context.underlying,'contract')].amount # check if there is still open position
print get_order(context.orderid).status # check status code