算法示例
时间加权平均算法
- 将委托数量平均分布在某个时间区域内;
- 每隔一段时间用指定的价格挂出买单(或者卖单)。
- 买入情况:买一价低于目标价格时,发出委托,委托数量在剩余委托量与委托分割量中取最小值。
- 卖出情况:卖一价高于目标价格时,发出委托,委托数量在剩余委托量与委托分割量中取最小值。
- def on_timer(self):
- """"""
- self.timer_count += 1
- self.total_count += 1
- self.put_variables_event()
-
- if self.total_count >= self.time:
- self.write_log("执行时间已结束,停止算法")
- self.stop()
- return
-
- if self.timer_count < self.interval:
- return
- self.timer_count = 0
-
- tick = self.get_tick(self.vt_symbol)
- if not tick:
- return
-
- self.cancel_all()
-
- left_volume = self.volume - self.traded
- order_volume = min(self.order_volume, left_volume)
-
- if self.direction == Direction.LONG:
- if tick.ask_price_1 <= self.price:
- self.buy(self.vt_symbol, self.price,
- order_volume, offset=self.offset)
- else:
- if tick.bid_price_1 >= self.price:
- self.sell(self.vt_symbol, self.price,
- order_volume, offset=self.offset)
冰山算法
- 在某个价位挂单,但是只挂一部分,直到全部成交。
- 买入情况:先检查撤单:最新Tick卖一价低于目标价格,执行撤单;若无活动委托,发出委托:委托数量在剩余委托量与挂出委托量中取最小值。
- 卖出情况:先检查撤单:最新Tick买一价高于目标价格,执行撤单;若无活动委托,发出委托:委托数量在剩余委托量与挂出委托量中取最小值。
- def on_timer(self):
- """"""
- self.timer_count += 1
-
- if self.timer_count < self.interval:
- self.put_variables_event()
- return
-
- self.timer_count = 0
-
- contract = self.get_contract(self.vt_symbol)
- if not contract:
- return
-
- # If order already finished, just send new order
- if not self.vt_orderid:
- order_volume = self.volume - self.traded
- order_volume = min(order_volume, self.display_volume)
-
- if self.direction == Direction.LONG:
- self.vt_orderid = self.buy(
- self.vt_symbol,
- self.price,
- order_volume,
- offset=self.offset
- )
- else:
- self.vt_orderid = self.sell(
- self.vt_symbol,
- self.price,
- order_volume,
- offset=self.offset
- )
- # Otherwise check for cancel
- else:
- if self.direction == Direction.LONG:
- if self.last_tick.ask_price_1 <= self.price:
- self.cancel_order(self.vt_orderid)
- self.vt_orderid = ""
- self.write_log(u"最新Tick卖一价,低于买入委托价格,之前委托可能丢失,强制撤单")
- else:
- if self.last_tick.bid_price_1 >= self.price:
- self.cancel_order(self.vt_orderid)
- self.vt_orderid = ""
- self.write_log(u"最新Tick买一价,高于卖出委托价格,之前委托可能丢失,强制撤单")
-
- self.put_variables_event()
狙击手算法
- 监控最新tick推送的行情,发现好的价格立刻报价成交。
- 买入情况:最新Tick卖一价低于目标价格时,发出委托,委托数量在剩余委托量与卖一量中取最小值。
- 卖出情况:最新Tick买一价高于目标价格时,发出委托,委托数量在剩余委托量与买一量中取最小值。
- def on_tick(self, tick: TickData):
- """"""
- if self.vt_orderid:
- self.cancel_all()
- return
-
- if self.direction == Direction.LONG:
- if tick.ask_price_1 <= self.price:
- order_volume = self.volume - self.traded
- order_volume = min(order_volume, tick.ask_volume_1)
-
- self.vt_orderid = self.buy(
- self.vt_symbol,
- self.price,
- order_volume,
- offset=self.offset
- )
- else:
- if tick.bid_price_1 >= self.price:
- order_volume = self.volume - self.traded
- order_volume = min(order_volume, tick.bid_volume_1)
-
- self.vt_orderid = self.sell(
- self.vt_symbol,
- self.price,
- order_volume,
- offset=self.offset
- )
-
- self.put_variables_event()
条件委托算法
- 监控最新tick推送的行情,发现行情突破立刻报价成交。
- 买入情况:Tick最新价高于目标价格时,发出委托,委托价为目标价格加上超价。
- 卖出情况:Tick最新价低于目标价格时,发出委托,委托价为目标价格减去超价。
- def on_tick(self, tick: TickData):
- """"""
- if self.vt_orderid:
- return
-
- if self.direction == Direction.LONG:
- if tick.last_price >= self.stop_price:
- price = self.stop_price + self.price_add
-
- if tick.limit_up:
- price = min(price, tick.limit_up)
-
- self.vt_orderid = self.buy(
- self.vt_symbol,
- price,
- self.volume,
- offset=self.offset
- )
- self.write_log(f"停止单已触发,代码:{self.vt_symbol},方向:{self.direction}, 价格:{self.stop_price},数量:{self.volume},开平:{self.offset}")
-
- else:
- if tick.last_price <= self.stop_price:
- price = self.stop_price - self.price_add
-
- if tick.limit_down:
- price = max(price, tick.limit_down)
-
- self.vt_orderid = self.buy(
- self.vt_symbol,
- price,
- self.volume,
- offset=self.offset
- )
- self.write_log(f"停止单已触发,代码:{self.vt_symbol},方向:{self.direction}, 价格:{self.stop_price},数量:{self.volume},开平:{self.offset}")
-
- self.put_variables_event()
最优限价算法
- 监控最新tick推送的行情,发现好的价格立刻报价成交。
- 买入情况:先检查撤单:最新Tick买一价不等于目标价格时,执行撤单;若无活动委托,发出委托:委托价格为最新Tick买一价,委托数量为剩余委托量。
- 卖出情况:先检查撤单:最新Tick买一价不等于目标价格时,执行撤单;若无活动委托,发出委托:委托价格为最新Tick卖一价,委托数量为剩余委托量。
- def on_tick(self, tick: TickData):
- """"""
- self.last_tick = tick
-
- if self.direction == Direction.LONG:
- if not self.vt_orderid:
- self.buy_best_limit()
- elif self.order_price != self.last_tick.bid_price_1:
- self.cancel_all()
- else:
- if not self.vt_orderid:
- self.sell_best_limit()
- elif self.order_price != self.last_tick.ask_price_1:
- self.cancel_all()
-
- self.put_variables_event()
-
- def buy_best_limit(self):
- """"""
- order_volume = self.volume - self.traded
- self.order_price = self.last_tick.bid_price_1
- self.vt_orderid = self.buy(
- self.vt_symbol,
- self.order_price,
- order_volume,
- offset=self.offset
- )
-
- def sell_best_limit(self):
- """"""
- order_volume = self.volume - self.traded
- self.order_price = self.last_tick.ask_price_1
- self.vt_orderid = self.sell(
- self.vt_symbol,
- self.order_price,
- order_volume,
- offset=self.offset
- )