HI all,
I am confused what was wrong form me, as the following code that why cannot close all open position ?
After do the order function , I recheck position again, But is still exist!
def close_orders(context,data):
for sec in context.sids:
order_share = context.portfolio.positions[sec].amount
print("open order_share=",sec.symbol, " share=",order_share)
if order_share > 0:
order(sec,-order_share )
elif order_share < 0:
order(sec,order_share )
#check all close order success
for sec in context.sids:
order_share = context.portfolio.positions[sec].amount
print("check order_share=",sec.symbol," share=",order_share)
Logs message:
2005-05-12PRINT('open order_share=', u'XLF', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLE', ' share=', 303)
2005-05-12PRINT('open order_share=', u'XLU', ' share=', -47)
2005-05-12PRINT('open order_share=', u'XLK', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLB', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLP', ' share=', 2816)
2005-05-12PRINT('open order_share=', u'XLY', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLI', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLV', ' share=', 710)
2005-05-12PRINT('check order_share=', u'XLF', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLE', ' share=', 303)
2005-05-12PRINT('check order_share=', u'XLU', ' share=', -47)
2005-05-12PRINT('check order_share=', u'XLK', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLB', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLP', ' share=', 2816)
2005-05-12PRINT('check order_share=', u'XLY', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLI', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLV', ' share=', 710)