5

Rhino | 如何兼容不同交易所的差异性

 2 years ago
source link: https://benpaodewoniu.github.io/2022/12/25/tomoon57/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

虽然,我们从交易所获取的数据类型都大致相同,比如 tickerdepthtrade等。

交易所返回格式不一样

比如,binance 的 深度信息是

{
"lastUpdateId": 1027024,
"E": 1589436922972, // 消息时间
"T": 1589436922959, // 撮合引擎时间
"bids": [ // 买单
[
"4.00000000", // 价格
"431.00000000" // 数量
]
],
"asks": [ // 卖单
[
"4.00000200", // 价格
"12.00000000" // 数量
]
]
}

而其他交易所可能不是这个组织形式。

所以,我们需要自定义自己的各类型数据结构,比如,我的 trade 数据为

@dataclass
class RhinoTrade(BaseInfo):
price: float = 0
amount: float = 0
time: int = 0
direction: Union[OrderDirection, PositionDirection] = OrderDirection.BUY.value

limit: int = 1000
trade_start_time: int = 0
trade_end_time: int = 0

def __str__(self):
return f"trades: {self.chain}_{self.cex_exchange_sub}_{self.dex_exchange}_{self.real_pair}"

这样,不同的交易所数据到来之后,我们根据交易所实际数据存放到 RhinoTrade 里面然后抛出就好了。这样,最后你拿到交易所传过来的数据后,就不需要考虑交易所之间的差异了。

不同交易所 API

之前我在对接 mexc 的时候,那个时候还是 V2,现在部分已经升级到 V3 了。

V2 升级到 V3 是非常阵痛的,因为,当时在设计的时候就没有考虑这种情况。比如,查询 order 信息。最开始是这样写的

def get_order(self,order_id);

这里面就一个参数 order_id,但是,升级到 V3 后,单一的传参满足不了更复杂的场景了,比如,它让你传 symbol、时间等。所以,如果持续添加参数,对之前的逻辑维护起来代价非常大。

所以,传参我最后也采用了自定义数据结构,比如

def get_order(self,rhino_order);

其中 rhino_order 是和上面 RhinoTrade 类似的自定义对象,这样的话,需要什么参数,就在对象里添加新的属性就好了,不会对之前的逻辑造成影响。

</div


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK