def get_price_order(quote, direction, run_mode): """ 计算出有效的下单价格 能用有效价下单且能防钓鱼 quote obj 行情报价 direction str 买卖方向 "BUY" "SELL" run_mode str "backtest"回测模式 "simulated"模拟 "live"实盘 price_order = get_price_order(quote, "BUY", run_mode) """ if quote.last_price > 0: # 有过成交、确保自己不是第一个报价者以防流动性不足吃大亏 # 回测 或 模拟 模式 只以对价下单[因为任何下单都不会提交到历史或模拟数据中] if run_mode in ["backtest", "simulated"]: if direction == "SELL": if quote.bid_price1 > 0: return quote.bid_price1 # 对价下单 else: return False if direction == "BUY": if quote.ask_price1 > 0: return quote.ask_price1 # 对价下单 else: return False # 实盘模式 if run_mode == "live": # 如果是涨停时 if 0 < quote.bid_price1 == quote.upper_limit: # 涨停时涨停价下单 对价或排队价下单 return quote.bid_price1 # 如果是跌停时 if 0 < quote.ask_price1 == quote.lower_limit: # 跌停时跌停价下单 对价或排队价下单 return quote.ask_price1 if direction == "SELL": if quote.ask_price1 > 0: # 有卖一价 return quote.ask_price1 - quote.price_tick # 卖一价减1跳 加塞价下单 elif quote.bid_price1 > 0: # 没有卖一价但有买一价 不是涨停可能是流动性不足 return quote.bid_price1 # 对价下单 if direction == "BUY": if quote.bid_price1 > 0: # 有买一价 return quote.bid_price1 + quote.price_tick # 买一价加1跳 加塞价下单 elif quote.ask_price1 > 0: # 没有买一价但有卖一价 不是跌停可能是流动性不足 return quote.ask_price1 # 对价下单 else: return False # 未有过成交不下单、确保自己不是第一个报价者以防流动性不足吃大亏