async def on_trade_update_callback(self, trade: Trade): """ 市场最新成交更新 """ if self.native_to_system: trade.symbol = self.native_to_system[ trade.symbol] #'交易所原始符号'转换成'量化平台通用符号' await self._original_on_trade_update_callback(trade)
async def process_trade(self, data): symbol = data.get("instrument_id") if symbol not in self._symbols: return action = ORDER_ACTION_BUY if data[ "side"] == "buy" else ORDER_ACTION_SELL price = float(data["price"]) quantity = float(data["size"]) timestamp = tools.utctime_str_to_mts(data["timestamp"]) info = { "platform": self._platform, "symbol": symbol, "action": action, "price": price, "quantity": quantity, "timestamp": timestamp } trade = Trade(**info) SingleTask.run(self.cb.on_trade_update_callback, trade)
async def feed(self, row): """ 通过历史数据驱动策略进行回测 """ drive_type = row["drive_type"] #数据驱动方式 if drive_type == "kline" and self.cb.on_kline_update_callback: kw = row.to_dict() del kw["drive_type"] del kw["gw"] del kw["dt"] kw["platform"] = self._platform kw["timestamp"] = int(kw["begin_dt"]) kw["kline_type"] = MARKET_TYPE_KLINE kline = Kline(**kw) await self.cb.on_kline_update_callback(kline) elif drive_type == "trade" and self.cb.on_trade_update_callback: kw = { "platform": self._platform, "symbol": row["symbol"], "action": row["direction"], "price": row["tradeprice"], "quantity": row["volume"], "timestamp": int(row["tradedt"]) } trade = Trade(**kw) await self.cb.on_trade_update_callback(trade) elif drive_type == "orderbook" and self.cb.on_orderbook_update_callback: asks = [] bids = [] for i in range(1, 20 + 1): asks.append([row[f'askprice{i}'], row[f'asksize{i}']]) bids.append([row[f'bidprice{i}'], row[f'bidsize{i}']]) kw = { "platform": self._platform, "symbol": row["symbol"], "asks": asks, "bids": bids, "timestamp": int(row["pubdt"]) } ob = Orderbook(**kw) await self.cb.on_orderbook_update_callback(ob)
def _update_trades(self, trades_info): """ trades update. Args: trades_info: trades information. Returns: """ #{"channel": "trades", "market": "BTC-PERP", "type": "update", "data": [{"id": 2616562, "price": 9333.25, "size": 0.2143, "side": "sell", "liquidation": false, "time": "2019-11-06T05:19:51.187372+00:00"}]} for t in trades_info["data"]: ts = tools.utctime_str_to_mts(t["time"], "%Y-%m-%dT%H:%M:%S.%f+00:00") p = { "platform": self._platform, "symbol": trades_info["market"], "action": ORDER_ACTION_BUY if t["side"] == "buy" else ORDER_ACTION_SELL, "price": t["price"], "quantity": t["size"], "timestamp": ts } trade = Trade(**p) SingleTask.run(self.cb.on_trade_update_callback, trade)
def parse(self): trade = Trade(**self.data) return trade
def parse(self): """ 解析self._data数据 """ trade = Trade(**self.data) return trade