定时平仓,可以的:
while True: q_close = api.get_quote(symbol) api.wait_update() time_condiction = q_close.datetime[11:16]==close_time if time_condiction: order_stoploss = api.insert_order(symbol=symbol, direction=direction_close, offset=offset, volume=volume, limit_price=market_price) api.wait_update() break
但是加上止损后,不行了:
for key, value in position_dict.items(): offset = 'CLOSE' #print(exchange) if value.exchange_id == 'SHFE': offset = 'CLOSETODAY' q_close = api.get_quote(key) api.wait_update() time_condiction = q_close.datetime[11:16]==end_time #if pos_len < int(pos_full/2): # time_condiction = True if value.pos_long_today > 0: direction_close = 'SELL' market_price = q_close.lower_limit open_price = 开仓价格(开仓后马上用last_price记录下来的) volume = value.pos_long_today stop_profit_condiction = (q_close.last_price-open_price)/open_price*1000 >= stop_profit_pct stop_loss_condiction = (open_price-q_close.last_price)/open_price*1000 >= stop_loss_pct #price_condiction = stop_profit_condiction or stop_loss_condiction price_condiction = stop_loss_condiction if price_condiction or time_condiction: #if time_condiction: api.insert_order(symbol=key, direction=direction_close, offset=offset, volume=volume, limit_price=market_price) api.wait_update() api.insert_order(symbol=key, direction=direction_close, offset=offset, volume=volume, limit_price=market_price) ''' if stop_loss_condiction: print('止损') if stop_profit_condiction: print('止盈') ''' if value.pos_short_today > 0: direction_close = 'BUY' market_price = q_close.upper_limit open_price = q_close.open volume = value.pos_short_today stop_profit_condiction = (open_price-q_close.last_price)/open_price*1000 >= stop_profit_pct stop_loss_condiction = (q_close.last_price-open_price)/open_price*1000 >= stop_loss_pct #price_condiction = stop_profit_condiction or stop_loss_condiction price_condiction = stop_loss_condiction if price_condiction or time_condiction: #if time_condiction: api.insert_order(symbol=key, direction=direction_close, offset=offset, volume=volume, limit_price=market_price) api.wait_update() api.insert_order(symbol=key, direction=direction_close, offset=offset, volume=volume, limit_price=market_price) ''' print('方向:看跌') print('开仓价:',open_price) print('当前价:',q_close.last_price) if stop_loss_condiction: print('止损') if stop_profit_condiction: print('止盈') '''
ringo 已回答的问题 2020年12月11日