import datetime as dt from BybitWebsocket import BybitWebsocket import time import discord from discord.ext import commands from dotenv import load_dotenv import os import asyncio import requests import json ws = BybitWebsocket(wsURL="wss://stream.bytick.com/realtime", api_key="", api_secret="") ws.subscribe_trade() class pricewatch(): async def watch(self, bot): while (1): eth_data = ws.get_data("trade.ETHUSD") btc_data = ws.get_data("trade.BTCUSD") if eth_data: print("-") for trade in eth_data: ethprice = trade["price"] ethprice = round(float(ethprice), 2) ethprice = "${:,.2f} USD".format(float(ethprice)) if btc_data: print("-")
# Prints logger info to terminal logger = logging.getLogger() logger.setLevel(logging.INFO) # Change this to DEBUG if you want a lot more info ch = logging.StreamHandler() # create formatter formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") ch.setFormatter(formatter) logger.addHandler(ch) return logger if __name__ == "__main__": logger = setup_logger() # inverse perpetual ws = BybitWebsocket(wsURL="wss://stream.bybit.com/realtime", api_key="", api_secret="" ) ws.subscribe_orderBookL2("BTCUSD") # ws.subscribe_kline("BTCUSD", '1') # ws.subscribe_instrument_info('BTCUSD') # ws.subscribe_trade("BTCUSD") # ws.subscribe_order() # ws.subscribe_stop_order() # ws.subscribe_execution() # ws.subscribe_position() # ws.subscribe_insurance() # only support inverse perpetual # ws.subscribe_wallet() # only support USDT perpetual while(1): logger.info(ws.get_data("orderBookL2_25.BTCUSD")) # logger.info(ws.get_data('candle.1.BTCUSD'))
# create formatter formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") ch.setFormatter(formatter) logger.addHandler(ch) return logger def my_callback(data): 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"))
def Connect(restart): global ws if restart: ws.exit() # Exit original ws if attempting to reopen. ws = BybitWebsocket(wsURL="wss://stream.bybit.com/realtime", api_key=apikey, api_secret=apisecret)
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)
def open_bybit_socket(self): print('Opening Bybit Socket') self.ws = BybitWebsocket(wsURL="wss://stream.bybit.com/realtime", api_key=None, api_secret=None)
from BybitWebsocket import BybitWebsocket ws = BybitWebsocket(wsURL='wss://stream-testnet.bybit.com/realtime', api_key='AYLFUOcvXIoZzGMv1C', api_secret='0vvUOZAOK6EoZ8PuqjvrLKcNZWyU3iF90p97')