async def submit_orders(self, delta_position): """ 根据当前最新的delta_position来执行下单操作 """ delta = tools.nearest(delta_position['BTC'], self.syminfo.size_tick) #下单量 if abs(delta) < 0.01: #原因是现货买入交易手续费从'货'中扣,极为恶心的设计 return if delta > 0: #做多 s, e = await self.create_order(self.gw, self.symbols[0], ORDER_ACTION_BUY, self.last_kline.close+100, abs(delta)) #限价单模拟市价单 if e: logger.error("error:", e, caller=self) elif delta < 0: #做空 s, e = await self.create_order(self.gw, self.symbols[0], ORDER_ACTION_SELL, self.last_kline.close-100, abs(delta)) #限价单模拟市价单 if e: logger.error("error:", e, caller=self)
async def create_order(self, action, price, quantity, order_type=ORDER_TYPE_LIMIT): """ 下单 """ if not self._last_kline or not self._last_kline.usable: return None, "无法创建订单" #获取符号相关信息 syminfo = config.backtest["feature"][self._platform]["syminfo"][self._symbol] price_tick = syminfo["price_tick"] #价格变动最小精度 size_tick = syminfo["size_tick"] #下单数量变动最小精度 size_limit = syminfo["size_limit"] #下单数量最小限制 value_tick = syminfo["value_tick"] #下单金额变动最小精度 value_limit = syminfo["value_limit"] #下单金额最小限制 base_currency = syminfo["base_currency"] #基础币种,交易标的,或者说就是'货' settlement_currency = syminfo["settlement_currency"] #结算币种,或者说就是'钱' #输入参数验证 if order_type == ORDER_TYPE_MARKET: if price: return None, "无法创建订单,市价单价格必须填0" if action == ORDER_ACTION_BUY: #市价买单quantity代表的是下单金额 if quantity < value_limit: return None, "无法创建订单,下单金额太少" if not self.precision_verify(quantity, value_tick): return None, "无法创建订单,下单金额精度错误" else: if quantity < size_limit: return None, "无法创建订单,下单数量太少" if not self.precision_verify(quantity, size_tick): return None, "无法创建订单,下单数量精度错误" else: if price <= 0: return None, "无法创建订单,价格必须大于0" if not self.precision_verify(price, price_tick): return None, "无法创建订单,价格精度错误" if quantity < size_limit: return None, "无法创建订单,下单数量太少" if not self.precision_verify(quantity, size_tick): return None, "无法创建订单,下单数量精度错误" #获取当前时间 ts = ModelAPI.current_milli_timestamp() # if order_type == ORDER_TYPE_MARKET: #市价单 if action == ORDER_ACTION_BUY: #买 bc = self._trader._assets[base_currency] sc = self._trader._assets[settlement_currency] if quantity > sc['free']: return None, "账户余额不够" #收盘均价模拟成交价 tradeprice = tools.nearest(self._last_kline.close_avg_fillna, price_tick) #市价买单quantity指的是'钱',逼真模拟 tradevolmue, left_money = self.market_buy_order_cross(quantity, tradeprice, size_tick) #市价买单quantity指的是'钱' #tradevolmue = quantity/tradeprice #对于现货交易,手续费是从接收币种里面扣除 fee = tradevolmue*self.taker_commission_rate #订单通知 order_no = self.next_order_no() o = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "order_no": order_no, "action": action, "symbol": self._symbol, "price": 0, "quantity": tools.nearest(quantity, value_tick), "remain": tools.nearest(left_money, value_tick), "status": ORDER_STATUS_FILLED, "order_type": order_type, "ctime": ts, "utime": ts #avg_price #trade_type } order = Order(**o) if self.cb.on_order_update_callback: await self.cb.on_order_update_callback(order) #成交通知 fill_no = self.next_fill_no() f = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "fill_no": fill_no, "order_no": order_no, "side": action, #成交方向,买还是卖 "symbol": self._symbol, "price": tools.nearest(tradeprice, price_tick), #成交价格 "quantity": tools.nearest(tradevolmue, size_tick), #成交数量 "liquidity": LIQUIDITY_TYPE_TAKER, #maker成交还是taker成交 "fee": tools.nearest(fee, size_tick), "ctime": ts } fill = Fill(**f) if self.cb.on_fill_update_callback: await self.cb.on_fill_update_callback(fill) #账户资产通知 #'货'增加 bc['free'] += (tradevolmue-fee) bc['free'] = tools.nearest(bc['free'], size_tick) bc['total'] = bc['free'] + bc['locked'] bc['total'] = tools.nearest(bc['total'], size_tick) #'钱'减少 sc['free'] -= quantity #市价买单quantity指的是'钱' sc['free'] = tools.nearest(sc['free'], value_tick) sc['total'] = sc['free'] + sc['locked'] sc['total'] = tools.nearest(sc['total'], value_tick) # ast = Asset(self._platform, self._account, self._trader._assets, ts, True) if self.cb.on_asset_update_callback: await self.cb.on_asset_update_callback(ast) elif action == ORDER_ACTION_SELL: #卖 bc = self._trader._assets[base_currency] sc = self._trader._assets[settlement_currency] if quantity > bc['free']: return None, "账户币不足" #收盘均价模拟成交价 tradeprice = tools.nearest(self._last_kline.close_avg_fillna, price_tick) trademoney = quantity*tradeprice #对于现货交易,手续费是从接收币种里面扣除 fee = trademoney*self.taker_commission_rate trademoney -= fee #订单通知 order_no = self.next_order_no() o = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "order_no": order_no, "action": action, "symbol": self._symbol, "price": 0, "quantity": tools.nearest(quantity, size_tick), "remain": 0, "status": ORDER_STATUS_FILLED, "order_type": order_type, "ctime": ts, "utime": ts #avg_price #trade_type } order = Order(**o) if self.cb.on_order_update_callback: await self.cb.on_order_update_callback(order) #成交通知 fill_no = self.next_fill_no() f = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "fill_no": fill_no, "order_no": order_no, "side": action, #成交方向,买还是卖 "symbol": self._symbol, "price": tools.nearest(tradeprice, price_tick), #成交价格 "quantity": tools.nearest(quantity, size_tick), #成交数量 "liquidity": LIQUIDITY_TYPE_TAKER, #maker成交还是taker成交 "fee": tools.nearest(fee, value_tick), "ctime": ts } fill = Fill(**f) if self.cb.on_fill_update_callback: await self.cb.on_fill_update_callback(fill) #账户资产通知 #'货'减少 bc['free'] -= quantity bc['free'] = tools.nearest(bc['free'], size_tick) bc['total'] = bc['free'] + bc['locked'] bc['total'] = tools.nearest(bc['total'], size_tick) #'钱'增加 sc['free'] += trademoney sc['free'] = tools.nearest(sc['free'], value_tick) sc['total'] = sc['free'] + sc['locked'] sc['total'] = tools.nearest(sc['total'], value_tick) # ast = Asset(self._platform, self._account, self._trader._assets, ts, True) if self.cb.on_asset_update_callback: await self.cb.on_asset_update_callback(ast) elif order_type == ORDER_TYPE_LIMIT: #限价单 if action == ORDER_ACTION_BUY: #买 bc = self._trader._assets[base_currency] sc = self._trader._assets[settlement_currency] if quantity*price > sc['free']: return None, "账户余额不够" #如果下单价格小于当前价格,那意味着无法成交,订单将进入订单薄挂着 if price < self._last_kline.close_avg_fillna: #订单通知 order_no = self.next_order_no() o = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "order_no": order_no, "action": action, "symbol": self._symbol, "price": tools.nearest(price, price_tick), "quantity": tools.nearest(quantity, size_tick), "remain": tools.nearest(quantity, size_tick), "status": ORDER_STATUS_SUBMITTED, "order_type": order_type, "ctime": ts, "utime": ts #avg_price #trade_type } order = Order(**o) self._orders[order_no] = order #进入订单簿 if self.cb.on_order_update_callback: await self.cb.on_order_update_callback(order) #账户资产通知 #'钱'需要被锁定一部分 sc['locked'] += quantity*price #挂单部分所占用的资金需要被锁定 sc['locked'] = tools.nearest(sc['locked'], value_tick) sc['free'] = sc['total'] - sc['locked'] sc['free'] = tools.nearest(sc['free'], value_tick) # ast = Asset(self._platform, self._account, self._trader._assets, ts, True) if self.cb.on_asset_update_callback: await self.cb.on_asset_update_callback(ast) else: #直接成交 #收盘均价模拟成交价 tradeprice = tools.nearest(self._last_kline.close_avg_fillna, price_tick) tradevolmue = quantity #直接模拟全部成交 trademoney = tradeprice*tradevolmue #成交金额 #对于现货交易,手续费是从接收币种里面扣除 fee = tradevolmue*self.taker_commission_rate #订单通知 order_no = self.next_order_no() o = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "order_no": order_no, "action": action, "symbol": self._symbol, "price": tools.nearest(price, price_tick), "quantity": tools.nearest(quantity, size_tick), "remain": 0, "status": ORDER_STATUS_FILLED, "order_type": order_type, "ctime": ts, "utime": ts #avg_price #trade_type } order = Order(**o) if self.cb.on_order_update_callback: await self.cb.on_order_update_callback(order) #成交通知 fill_no = self.next_fill_no() f = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "fill_no": fill_no, "order_no": order_no, "side": action, #成交方向,买还是卖 "symbol": self._symbol, "price": tools.nearest(tradeprice, price_tick), #成交价格 "quantity": tools.nearest(tradevolmue, size_tick), #成交数量 "liquidity": LIQUIDITY_TYPE_TAKER, #maker成交还是taker成交 "fee": tools.nearest(fee, size_tick), "ctime": ts } fill = Fill(**f) if self.cb.on_fill_update_callback: await self.cb.on_fill_update_callback(fill) #账户资产通知 #'货'增加 bc['free'] += (tradevolmue-fee) bc['free'] = tools.nearest(bc['free'], size_tick) bc['total'] = bc['free'] + bc['locked'] bc['total'] = tools.nearest(bc['total'], size_tick) #'钱'减少 sc['free'] -= trademoney sc['free'] = tools.nearest(sc['free'], value_tick) sc['total'] = sc['free'] + sc['locked'] sc['total'] = tools.nearest(sc['total'], value_tick) # ast = Asset(self._platform, self._account, self._trader._assets, ts, True) if self.cb.on_asset_update_callback: await self.cb.on_asset_update_callback(ast) elif action == ORDER_ACTION_SELL: #卖 bc = self._trader._assets[base_currency] sc = self._trader._assets[settlement_currency] if quantity > bc['free']: return None, "账户币不足" #如果下单价格大于当前价格,那意味着无法成交,订单将进入订单薄挂着 if price > self._last_kline.close_avg_fillna: #订单通知 order_no = self.next_order_no() o = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "order_no": order_no, "action": action, "symbol": self._symbol, "price": tools.nearest(price, price_tick), "quantity": tools.nearest(quantity, size_tick), "remain": tools.nearest(quantity, size_tick), "status": ORDER_STATUS_SUBMITTED, "order_type": order_type, "ctime": ts, "utime": ts #avg_price #trade_type } order = Order(**o) self._orders[order_no] = order #进入订单簿 if self.cb.on_order_update_callback: await self.cb.on_order_update_callback(order) #账户资产通知 #'货'需要被锁定一部分 bc['locked'] += quantity #挂单部分所占用的'货'需要被锁定 bc['locked'] = tools.nearest(bc['locked'], size_tick) bc['free'] = bc['total'] - bc['locked'] bc['free'] = tools.nearest(bc['free'], size_tick) # ast = Asset(self._platform, self._account, self._trader._assets, ts, True) if self.cb.on_asset_update_callback: await self.cb.on_asset_update_callback(ast) else: #直接成交 #收盘均价模拟成交价 tradeprice = tools.nearest(self._last_kline.close_avg_fillna, price_tick) trademoney = quantity*tradeprice #对于现货交易,手续费是从接收币种里面扣除 fee = trademoney*self.taker_commission_rate trademoney -= fee #订单通知 order_no = self.next_order_no() o = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "order_no": order_no, "action": action, "symbol": self._symbol, "price": tools.nearest(price, price_tick), "quantity": tools.nearest(quantity, size_tick), "remain": 0, "status": ORDER_STATUS_FILLED, "order_type": order_type, "ctime": ts, "utime": ts #avg_price #trade_type } order = Order(**o) if self.cb.on_order_update_callback: await self.cb.on_order_update_callback(order) #成交通知 fill_no = self.next_fill_no() f = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "fill_no": fill_no, "order_no": order_no, "side": action, #成交方向,买还是卖 "symbol": self._symbol, "price": tools.nearest(tradeprice, price_tick), #成交价格 "quantity": tools.nearest(quantity, size_tick), #成交数量 "liquidity": LIQUIDITY_TYPE_TAKER, #maker成交还是taker成交 "fee": tools.nearest(fee, value_tick), "ctime": ts } fill = Fill(**f) if self.cb.on_fill_update_callback: await self.cb.on_fill_update_callback(fill) #账户资产通知 #'货'减少 bc['free'] -= quantity bc['free'] = tools.nearest(bc['free'], size_tick) bc['total'] = bc['free'] + bc['locked'] bc['total'] = tools.nearest(bc['total'], size_tick) #'钱'增加 sc['free'] += trademoney sc['free'] = tools.nearest(sc['free'], value_tick) sc['total'] = sc['free'] + sc['locked'] sc['total'] = tools.nearest(sc['total'], value_tick) # ast = Asset(self._platform, self._account, self._trader._assets, ts, True) if self.cb.on_asset_update_callback: await self.cb.on_asset_update_callback(ast) elif order_type == ORDER_TYPE_IOC: raise NotImplementedError #返回订单号 return order_no, None
async def make_trade(self): """ 尝试和订单列表中的订单进行撮合成交 """ #遍历订单簿里面所有挂单 os = copy.copy(self._orders) for (k, o) in os.items(): syminfo = config.backtest["feature"][self._platform]["syminfo"][self._symbol] price_tick = syminfo["price_tick"] #价格变动最小精度 size_tick = syminfo["size_tick"] #下单数量变动最小精度 value_tick = syminfo["value_tick"] #下单金额变动最小精度 base_currency = syminfo["base_currency"] #基础币种,交易标的,或者说就是'货' settlement_currency = syminfo["settlement_currency"] #结算币种,或者说就是'钱' bc = self._trader._assets[base_currency] sc = self._trader._assets[settlement_currency] if o.action == ORDER_ACTION_BUY: #买单 if o.price >= self._last_kline.close_avg_fillna: #当前价格可以成交 ts = ModelAPI.current_milli_timestamp() #收盘均价模拟成交价 tradeprice = tools.nearest(self._last_kline.close_avg_fillna, price_tick) tradevolmue = o.quantity #直接模拟全部成交 trademoney = tradeprice*tradevolmue #成交金额 #对于现货交易,手续费是从接收币种里面扣除 fee = tradevolmue*self.maker_commission_rate #订单通知 o.remain = 0 o.status = ORDER_STATUS_FILLED o.utime = ts if self.cb.on_order_update_callback: await self.cb.on_order_update_callback(o) #成交通知 fill_no = self.next_fill_no() f = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "fill_no": fill_no, "order_no": o.order_no, "side": o.action, #成交方向,买还是卖 "symbol": self._symbol, "price": tools.nearest(tradeprice, price_tick), #成交价格 "quantity": tools.nearest(tradevolmue, size_tick), #成交数量 "liquidity": LIQUIDITY_TYPE_MAKER, #maker成交还是taker成交 "fee": tools.nearest(fee, size_tick), "ctime": ts } fill = Fill(**f) if self.cb.on_fill_update_callback: await self.cb.on_fill_update_callback(fill) #账户资产通知 #'货'增加 bc['free'] += (tradevolmue-fee) bc['free'] = tools.nearest(bc['free'], size_tick) bc['total'] = bc['free'] + bc['locked'] bc['total'] = tools.nearest(bc['total'], size_tick) #释放挂单占用的'钱' sc['locked'] -= o.quantity*o.price sc['locked'] = tools.nearest(sc['locked'], value_tick) sc['free'] = sc['total'] - sc['locked'] #'钱'减少 sc['free'] -= trademoney sc['free'] = tools.nearest(sc['free'], value_tick) sc['total'] = sc['free'] + sc['locked'] sc['total'] = tools.nearest(sc['total'], value_tick) # ast = Asset(self._platform, self._account, self._trader._assets, ts, True) if self.cb.on_asset_update_callback: await self.cb.on_asset_update_callback(ast) #删除订单簿中的订单 del self._orders[o.order_no] elif o.action == ORDER_ACTION_SELL: #卖单 if o.price <= self._last_kline.close_avg_fillna: #当前价格可以成交 ts = ModelAPI.current_milli_timestamp() #收盘均价模拟成交价 tradeprice = tools.nearest(self._last_kline.close_avg_fillna, price_tick) trademoney = o.quantity*tradeprice #模拟全部成交 #对于现货交易,手续费是从接收币种里面扣除 fee = trademoney*self.maker_commission_rate trademoney -= fee #订单通知 o.remain = 0 o.status = ORDER_STATUS_FILLED o.utime = ts if self.cb.on_order_update_callback: await self.cb.on_order_update_callback(o) #成交通知 fill_no = self.next_fill_no() f = { "platform": self._platform, "account": self._account, "strategy": self._strategy, "fill_no": fill_no, "order_no": o.order_no, "side": o.action, #成交方向,买还是卖 "symbol": self._symbol, "price": tools.nearest(tradeprice, price_tick), #成交价格 "quantity": tools.nearest(o.quantity, size_tick), #成交数量 "liquidity": LIQUIDITY_TYPE_MAKER, #maker成交还是taker成交 "fee": tools.nearest(fee, value_tick), "ctime": ts } fill = Fill(**f) if self.cb.on_fill_update_callback: await self.cb.on_fill_update_callback(fill) #账户资产通知 #释放挂单占用的'货' bc['locked'] -= o.quantity bc['locked'] = tools.nearest(bc['locked'], size_tick) bc['free'] = bc['total'] - bc['locked'] #'货'减少 bc['free'] -= o.quantity bc['free'] = tools.nearest(bc['free'], size_tick) bc['total'] = bc['free'] + bc['locked'] bc['total'] = tools.nearest(bc['total'], size_tick) #'钱'增加 sc['free'] += trademoney sc['free'] = tools.nearest(sc['free'], value_tick) sc['total'] = sc['free'] + sc['locked'] sc['total'] = tools.nearest(sc['total'], value_tick) # ast = Asset(self._platform, self._account, self._trader._assets, ts, True) if self.cb.on_asset_update_callback: await self.cb.on_asset_update_callback(ast) #删除订单簿中的订单 del self._orders[o.order_no]