Ejemplo n.º 1
0
    logger.info(data)


if __name__ == "__main__":
    logger = setup_logger()
    ws = BybitWebsocket(wsURL="wss://stream-testnet.bybit.com/realtime",
                        api_key=None,
                        api_secret=None)

    ws.subscribe_orderBookL2("BTCUSD")
    ws.subscribe_kline("BTCUSD", '1m')
    ws.subscribe_klineV2('1', "ETHUSD")
    ws.subscribe_order()
    ws.subscribe_execution()
    ws.subscribe_position()
    ws.subscribe_instrument_info('BTCUSD')
    ws.subscribe_insurance()

    ws.add_callback('klineV2.1.ETHUSD', my_callback)

    while (1):
        logger.info(ws.get_data("orderBookL2_25.BTCUSD"))
        logger.info(ws.get_data('kline.BTCUSD.1m'))
        logger.info(ws.get_data('order'))
        logger.info(ws.get_data("execution"))
        logger.info(ws.get_data("position"))
        logger.info(ws.get_data("instrument_info.100ms.BTCUSD"))
        logger.info(ws.get_data('insurance.BTC'))
        logger.info(ws.get_data('insurance.EOS'))
        sleep(1)
class Recording:

    def __init__(self):
        self.ws = None
        self.curr_price = None
        # these are to keep track of the current candle's
        # open, high, low, & close price's
        self.thirty_min_candle = Candle()
        self.one_hour_candle = Candle()
        self.four_hour_candle = Candle()
        self.one_day_candle = Candle()
        self.clock = gmtime()

    def open_bybit_socket(self):
        print('Opening Bybit Socket')
        self.ws = BybitWebsocket(wsURL="wss://stream.bybit.com/realtime",
                                 api_key=None, api_secret=None)

    def subscribe_to_socket(self):
        print('Subscribing to BTCUSD')
        self.ws.subscribe_instrument_info('BTCUSD')

    def start(self):
        print('Starting recording')
        # subscribe to a current
        self.open_bybit_socket()
        self.subscribe_to_socket()

        # timer
        print('Listening')
        while True:
            # grab price data out of the web socket flow
            data = self.ws.get_data("instrument_info.100ms.BTCUSD")
            if data:
                # check if price data has updated
                if 'update' in data.keys():
                    data = data['update'][0]
                    for k, v in data.items():
                        # last traded price update
                        if k == 'last_price_e4':
                            self.curr_price = v

                    # keep track of time
                    tick = gmtime()
                    # TODO: put logic to handle candle stick data
                    # 30 minute open
                    if tick.tm_min is not self.clock.tm_min and tick.tm_min is 30:
                        strftime("%a, %d %b %Y %H:%M:%S +0000", self.clock)
                        print('30 min candle opening')
                    # hourly open
                    if tick.tm_hour is not self.clock.tm_hour:
                        strftime("%a, %d %b %Y %H:%M:%S +0000", self.clock)
                        print('30 min candle opening')
                        print('1 hr candle opening')
                        # 4 hour open
                        if tick.tm_hour % 4 is 0:
                            print('4 hr candle opening')
                    # daily open
                    if tick.tm_mday is not self.clock.tm_mday:
                        strftime("%a, %d %b %Y %H:%M:%S +0000", self.clock)
                        print('daily open')
                    # update clock
                    self.clock = tick
                    strftime("%a, %d %b %Y %H:%M:%S +0000", self.clock)

                sleep(1)