hxxjava wrote:
aqua-pink wrote:
您好,请问
发起条件单
cond_order = ConditionOrder(... ...)
self.send_condition_order(cond_order)这一部分的入参应该是什么样的?
可以作为on_trade()成交后设定条件单吗?这里给出一个例子代码来参考,根据你自己的情况去修改:
def execute(self,strategy:CtaTemplate): """ 一段将交易指令转化为条件单的例子 ,其中: self.price:开仓价 self.stop_price:止盈价 self.exit_price:止损价 """ if self.offset == Offset.OPEN: open_condition = Condition.LE if self.direction == Direction.LONG else Condition.BE stop_condition = Condition.BT if self.direction == Direction.LONG else Condition.LT exit_condition = Condition.LT if self.direction == Direction.LONG else Condition.BT # 开仓条件单 stop_order = ConditionOrder(strategy_name=strategy.strategy_name,vt_symbol=self.vt_symbol, direction=self.direction,offset=self.offset, price=self.stop_price,volume=self.volume/2,condition=stop_condition) # 开仓条件单 open_order = ConditionOrder(strategy_name=strategy.strategy_name,vt_symbol=self.vt_symbol, direction=self.direction,offset=self.offset, price=self.price,volume=self.volume,condition=open_condition) # 止损条件单 exit_order = ConditionOrder(strategy_name=strategy.strategy_name,vt_symbol=self.vt_symbol, direction=self.direction,offset=self.offset, price=self.exit_price,volume=self.volume,condition=exit_condition) for cond_order in [open_order,stop_order,exit_order]: result = strategy.send_condition_order(cond_order) print(f"{strategy.strategy_name}发送开仓条件单{'成功' if result else '成功'}:{cond_order}") elif self.offset == Offset.CLOSE: tj1 = self.direction == Direction.LONG and strategy.pos < 0 tj2 = self.direction == Direction.SHORT and strategy.pos > 0 if tj1 or tj2: exit_condition = Condition.LT if self.direction == Direction.LONG else Condition.BT exit_order = ConditionOrder(strategy_name=strategy.strategy_name,vt_symbol=self.vt_symbol, direction=self.direction,offset=self.offset, price=self.price,volume=abs(strategy.pos),condition=exit_condition, execute_price=ExecutePrice.MARKET) result = strategy.send_condition_order(exit_order) print(f"{strategy.strategy_name}发送平仓条件单{'成功' if result else '成功'}:{exit_order}")
影响条件单执行方式的主要参数:
- condition,price —— 条件单触发点条件
- direction,offset —— 委托方向和开平选择
- excute_price —— 条件单被触发时的执行价格,设定价、市场价还是极限价
- 极限价:买入为涨停价,卖出为跌停价
采用条件单的好处
- 策略的全部委托在条件单管理器中都可以看到
- 不像停止单,价格必须预先确定,条件单的执行价是随行就市的,你可以头一天发出条件单,执行价最后为第二天的涨停价。
非常感谢大佬的解释,我继续理解理解!感谢!
您好,请问
发起条件单
cond_order = ConditionOrder(... ...)
self.send_condition_order(cond_order)
这一部分的入参应该是什么样的?
可以作为on_trade()成交后设定条件单吗?