如何在Python上通过Bitmex Websocket Api接收实时数据?

问题描述

我知道我可以使用“ while true”并调用“ get_ticker”方法获取产品的最后价格,但这是从python而不是市场本身驱动的。我想知道随着BitMEX网站的改变,是否有办法获得最后的价格。谢谢

解决方法

检查我的bitmex项目,是否有您的问题的解决方案:bitmex-supervisor

基本代码片段:

__init__()中:

self.last_price = 0
self._min_price = float('inf')
self._max_price = -1

self.initial_price = float('nan')

self.tracking = False

方法:

@property
def min_price(self):
    return self._min_price

@min_price.setter
def min_price(self,value):
    if value < self.initial_price:
        self.callback_price_decreased()  # here you can do some stuff
    self._min_price = value

@property
def max_price(self):
    return self._max_price

@max_price.setter
def max_price(self,value):
    if value > self.initial_price:
        self.callback_price_increased()  # here you can do some stuff
    self._max_price = value

def stop_trailing(self):
    self.tracking = False

def start_trailing(self,initial_price: float):
    """
    :param initial_price: the price after reaching which order will be moving
    """

    self._max_price = -1
    self._min_price = float('inf')
    self.initial_price = initial_price
    self.tracking = True

__on_message()中:

instrument = self.get_instrument(symbol=self.order.symbol)
    if instrument is not None:
        self.last_price = instrument['lastPrice']
        if self.tracking:
            if self.last_price > self.max_price and self.order.side == 'Sell':
                self.max_price = self.last_price
            elif self.last_price < self.min_price and self.order.side == 'Buy':
                self.min_price = self.last_price

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...