def _convert_asset_format(self, data): for d in data: c = d["currency"] self._assets[c]["free"] = float(d["available"]) self._assets[c]["locked"] = float(d["hold"]) self._assets[c]["total"] = float(d["balance"]) return Asset(self._platform, self._account, self._assets, tools.get_cur_timestamp_ms(), True)
async def on_asset_update_callback(self, asset: Asset): """ 账户资产更新 """ if self.is_upper: #如果币种符号需要转换成大写,就进行转换 _assets = defaultdict( lambda: {k: 0.0 for k in {'free', 'locked', 'total'}}) for (k, v) in asset.assets.items(): _assets[k.upper()] = v asset.assets = _assets await self._original_on_asset_update_callback(asset)
async def init_asset(self): """ 读取回测配置信息中的初始化资产,通知上层策略 """ ts = ModelAPI.current_milli_timestamp() d = config.backtest["feature"][self._platform]["asset"] for (k, v) in d.items(): self._assets[k]["free"] = float(v) self._assets[k]["total"] = float(v) #通知上层策略 ast = Asset(self._platform, self._account, self._assets, ts, True) if self.cb.on_asset_update_callback: await self.cb.on_asset_update_callback(ast)
async def get_assets(self): """ 获取交易账户资产信息 Args: None Returns: assets: Asset if successfully, otherwise it's None. error: Error information, otherwise it's None. """ ts = ModelAPI.current_milli_timestamp() ast = Asset(self._platform, self._account, self._assets, ts, True) return ast, None
async def get_assets(self): """ 获取交易账户资产信息 Args: None Returns: assets: Asset if successfully, otherwise it's None. error: Error information, otherwise it's None. """ #{"result": {"backstopProvider": false, "collateral": 110.094266926, "freeCollateral": 109.734306926, "initialMarginRequirement": 0.2, "leverage": 5.0, "liquidating": false, "maintenanceMarginRequirement": 0.03, "makerFee": 0.0002, "marginFraction": 61.1703338848761, "openMarginFraction": 61.170278323147016, "positionLimit": null, "positionLimitUsed": 2.15976, "positions": [{"collateralUsed": 0.35996, "cost": -1.7999, "entryPrice": 179.99, "estimatedLiquidationPrice": 11184.0172926, "future": "ETH-PERP", "initialMarginRequirement": 0.2, "longOrderSize": 0.0, "maintenanceMarginRequirement": 0.03, "netSize": -0.01, "openSize": 0.01, "realizedPnl": 0.01723393, "shortOrderSize": 0.0, "side": "sell", "size": 0.01, "unrealizedPnl": 0.0001}], "takerFee": 0.0007, "totalAccountValue": 110.094366926, "totalPositionSize": 1.7998, "useFttCollateral": true, "username": "******"}, "success": true} success, error = await self._rest_api.get_account_info() if error: return None, error if not success["success"]: return None, "get_account_info error" data = success["result"] assets = {} total = float(data["collateral"]) free = float(data["freeCollateral"]) locked = total - free assets["USD"] = { "total": "%.8f" % total, "free": "%.8f" % free, "locked": "%.8f" % locked } if assets == self._assets: update = False else: update = True self._assets = assets timestamp = tools.get_cur_timestamp_ms() ast = Asset(self._platform, self._account, self._assets, timestamp, update) #因为ftx websocket接口里面没有资产通知,所以只能这样模拟 SingleTask.run(self.cb.on_asset_update_callback, ast) return ast, None
def parse(self): asset = Asset(**self.data) return asset
def parse(self): """ 解析self._data数据 """ asset = Asset(**self.data) return asset
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]