def get(self): if not request.args: return create_error_response(400, "Query Error", 'Missing Query Parameter "symbol"') try: if request.args["symbol"]: ws = BitMEXWebsocket( endpoint="https://testnet.bitmex.com/api/v1", symbol=request.args["symbol"], api_key=None, api_secret=None) trades = [] trades = ws.recent_trades() for trade in trades: body = MasonControls(symbol=trade["symbol"], side=trade["side"], size=trade["size"], price=trade["price"]) body.add_control("buckets", href=api.url_for(BucketedPriceAction) + "?{timebucket}", title="Trades in time buckets") body.add_control("self", href=api.url_for(PriceAction) + "?symbol={}".format(trade["symbol"])) return Response(json.dumps(body), status=200, mimetype=MASON) except: print(traceback.format_exc()) return create_error_response(400, "Query Error", "Query Parameter doesn't exist")
def run(): if "BITMEX_API_KEY" not in os.environ: print("BITMEX_API_KEY env var must be set") exit(1) if "BITMEX_API_SECRET" not in os.environ: print("BITMEX_API_SECRET env var must be set") exit(1) bq = BigQuery( table="XBTUSD_trade_data", dataset="bitmex", schema=schema) logger = setup_logger() ws = BitMEXWebsocket( endpoint="https://www.bitmex.com/api/v1", symbol="XBTUSD", api_key=os.environ.get('BITMEX_API_KEY'), api_secret=os.environ.get('BITMEX_API_SECRET') ) while(ws.ws.sock.connected): if ws.api_key: rows_to_insert = ws.recent_trades() errors = bq.client.insert_rows(bq.tbl, rows_to_insert) if errors != []: print(errors)
def update_trades(): ws_trades = BitMEXWebsocket(endpoint="https://testnet.bitmex.com/api/v1", symbol="XBTUSD", api_key=None, api_secret=None) while (ws_trades.ws.sock.connected): trades=ws_trades.recent_trades() db.mkt.update_one({"_id":"trades"},{"$set":{"recent_trades": trades}}) logging.info("trades done") sleep(5)
def run(): logger = setup_logger() # Instantiating the WS will make it connect. Be sure to add your api_key/api_secret. ws = BitMEXWebsocket(endpoint="https://testnet.bitmex.com/api/v1", symbol="XBTUSD", api_key=None, api_secret=None) logger.info("Instrument data: %s" % ws.get_instrument()) # Run forever while(ws.ws.sock.connected): logger.info("Ticker: %s" % ws.get_ticker()) if ws.config['api_key']: logger.info("Funds: %s" % ws.funds()) logger.info("Market Depth: %s" % ws.market_depth()) logger.info("Recent Trades: %s\n\n" % ws.recent_trades()) sleep(10)
def run(): logger = setup_logger() # Instantiating the WS will make it connect. Be sure to add an auth method. You can use login/password # or api_key/api_secret. ws = BitMEXWebsocket(endpoint="https://testnet.bitmex.com/api/v1", symbol="XBT24H", login="******", password="******", api_key=None, api_secret=None) logger.info("Instrument data: %s" % ws.get_instrument()) # Run forever while(ws.ws.sock.connected): logger.info("Ticker: %s" % ws.get_ticker()) logger.info("Funds: %s" % ws.funds()) logger.info("Market Depth: %s" % ws.market_depth()) logger.info("Recent Trades: %s\n\n" % ws.recent_trades()) sleep(10)
def run(): logger = setup_logger() # Instantiating the WS will make it connect. Be sure to add your api_key/api_secret. ws = BitMEXWebsocket(endpoint="https://testnet.bitmex.com/api/v1", symbol="XBTUSD", api_key=None, api_secret=None) logger.info("Instrument data: %s" % ws.get_instrument()) # Run forever while(ws.ws.sock.connected): logger.info("Ticker: %s" % ws.get_ticker()) if ws.api_key: logger.info("Funds: %s" % ws.funds()) logger.info("Market Depth: %s" % ws.market_depth()) logger.info("Recent Trades: %s\n\n" % ws.recent_trades()) sleep(10)
def run(): logger = setup_logger() config = configparser.ConfigParser() config.read('config/config.ini') ws = BitMEXWebsocket(endpoint=config['bitmex']['endpoint'], symbol="XBTUSD", api_key=config['bitmex']['api_key'], api_secret=config['bitmex']['api_secret']) logger.info("Instrument data: %s" % ws.get_instrument()) # Run forever while(ws.ws.sock.connected): logger.info("Ticker: %s" % ws.get_ticker()) if ws.api_key: logger.info("Funds: %s" % ws.funds()) logger.info("Market Depth: %s" % ws.market_depth()) logger.info("Recent Trades: %s\n\n" % ws.recent_trades()) sleep(10)
def run(): logger = setup_logger() # Instantiating the WS will make it connect. Be sure to add your api_key/api_secret. ws = BitMEXWebsocket( endpoint="wss://testnet.bitmex.com/realtime", symbol="XBTUSD", api_key="wwvS30igJDo6Ksxa0h2EP1Eq", api_secret="-DOHRIUObpSQilqyr2y18YcTRi5NWFIV95du4i8rG4VveOBI") logger.info("Instrument data: {}".format(ws.get_instrument())) # Run forever while (ws.ws.sock.connected): logger.info("Ticker: %s" % ws.get_ticker()) if ws.config['api_key']: logger.info("Funds: %s" % ws.funds()) logger.info("Market Depth: %s" % ws.market_depth()) logger.info("Recent Trades: %s\n\n" % ws.recent_trades()) sleep(10)
def run(): logger = setup_logger() # Instantiating the WS will make it connect. Be sure to add an auth method. You can use login/password # or api_key/api_secret. ws = BitMEXWebsocket(endpoint="https://testnet.bitmex.com/api/v1", symbol="XBT24H", login="******", password="******", api_key=None, api_secret=None) logger.info("Instrument data: %s" % ws.get_instrument()) # Run forever while (ws.ws.sock.connected): logger.info("Ticker: %s" % ws.get_ticker()) logger.info("Funds: %s" % ws.funds()) logger.info("Market Depth: %s" % ws.market_depth()) logger.info("Recent Trades: %s\n\n" % ws.recent_trades()) sleep(10)
class BitMexWS(object): """ 适合订阅数据 """ def __init__(self, api_key=None, api_secret=None, symbol="XBTUSD"): endpoint = "https://testnet.bitmex.com/api/v1" if api_key: endpoint = "https://www.bitmex.com/api/v1" self.ws = BitMEXWebsocket(endpoint=endpoint, symbol=symbol, api_key=api_key, api_secret=api_secret) def run(self): logger = self.setup_logger() # Instantiating the WS will make it connect. Be sure to add your api_key/api_secret. logger.info("Instrument data: %s" % self.ws.get_instrument()) # Run forever while self.ws.ws.sock.connected: logger.info("Ticker: %s" % self.ws.get_ticker()) if self.ws.api_key: logger.info("Funds: %s" % self.ws.funds()) logger.info("Market Depth: %s" % self.ws.market_depth()) logger.info("Recent Trades: %s\n\n" % self.ws.recent_trades()) sleep(10) def setup_logger(self): # 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") # add formatter to ch ch.setFormatter(formatter) logger.addHandler(ch) return logger
def run(): logger = setup_logger() # Instantiating the WS will make it connect. Be sure to add your api_key/api_secret. ws = BitMEXWebsocket(endpoint="https://testnet.bitmex.com/api/v1", symbol="XBTUSD", api_key=API_KEY, api_secret=API_SECRET) logger.info("Instrument data: %s" % ws.get_instrument()) # Run forever while ws.ws.sock.connected: # logger.info("Ticker: %s" % ws.get_ticker()) # if ws.api_key: # logger.info("Funds: %s" % ws.funds()) # logger.info("Market Depth: %s" % ws.market_depth()) recent_trades = ws.recent_trades() print(f"Recent Trades: {recent_trades}") print(len(recent_trades)) sleep(1)
kukCajta = datetime.now() - lastRenew if (kukCajta.seconds / 60 > 15): # renew ws connection cause live feed lagging, renew every hour ws.exit() ws = BitMEXWebsocket(endpoint="https://www.bitmex.com/api/v1", symbol=symbol, api_key=apiKljuci['websocket']['key'], api_secret=apiKljuci['websocket']['secret']) lastRenew = datetime.now() if (ws.get_instrument()['volume'] < ws.get_instrument()['volume24h'] / 24): # avoid pumps/dumps d = dateutil.parser.parse(ws.get_instrument()['fundingTimestamp']) d = d.replace(tzinfo=None) razlika = d - datetime.utcnow() print(razlika.seconds / 60) if (razlika.seconds / 60 > 10): # check funding closing result1 = client.Order.Order_new(symbol=symbol, ordType='Limit', orderQty=amount, price=ws.recent_trades()[0]['price'] - offsetLong, execInst='ParticipateDoNotInitiate').result() while (True): # check if order succesful else try again if (result1[0]['ordStatus'] == 'New'): print('order LONG uspesen') break else: time.sleep(2) offsetLong += 0.05 result1 = client.Order.Order_new(symbol=symbol, ordType='Limit', orderQty=amount,price=ws.recent_trades()[0]['price'] - offsetLong,execInst='ParticipateDoNotInitiate').result() print('LONG retry') if (switchCounter > 5): deadManSwitch = client.Order.Order_cancelAllAfter(timeout=60000.0).result() switchCounter = 0 else: switchCounter += 1
for i in range(0, 9): bidsarr.append(bidsnew[(i +1 ) *-1]) asksarr.append(asksnew[i]) ts = calendar.timegm(time.gmtime()) result = {} result['bids'] = bidsarr result['asks'] = asksarr result['timeStamp'] = ts col = pydb["mex_order_book"] try: col.insert_one(result) except Exception as e: print(e) trades = (ws.recent_trades()) tradesnew = [] for t in trades: if t['trdMatchID'] not in trdMatchIDs: trdMatchIDs.append(t['trdMatchID']) tradesnew.append(t) #print(tradesnew) col = pydb["mex_trade"] try: col.insert_many(tradesnew) except Exception as e: print(e) sleep(1/2.5) except Exception as e: print(e)
### 'education is something you don't want to finish' ### # [email protected] # have fun, learn, give feedback # imports import pandas as pd import numpy as np from bitmex_websocket import BitMEXWebsocket import datetime import time from time import sleep apiKey = '' # input your api key apiSecret = '' # input your api secret ws = BitMEXWebsocket(endpoint="wss://www.bitmex.com/realtime", symbol='XBTUSD', api_key=apiKey, api_secret=apiSecret) # client # socket connections while (ws.ws.sock.connected): r = ws.recent_trades() recent = list(r[-1].items()) ts = recent[[0][0]][1] sym = recent[[1][0]][1] side = recent[[2][0]][1] size = recent[[3][0]][1] price = recent[[4][0]][1] ts, sym, side, size, price time.sleep(3)
class Connector(object): def __init__(self, symbol: str = "XBTUSD"): self._symbol = symbol self._rest_api = bitmex.bitmex(test=True, api_key=API_KEY, api_secret=API_SECRET) self._ws_api = BitMEXWebsocket( endpoint="https://testnet.bitmex.com/api/v1", symbol=self._symbol, api_key=API_KEY, api_secret=API_SECRET) self._static_data = pd.DataFrame() self._data = [] def run(self, start_time: datetime): # download data from S3 self._download_data_from_s3() # get static data historical_data = self._get_data_from_local(self._symbol, start_time) recent_data = self._get_data_from_rest_api( historical_data["timestamp"].max()) self._static_data = pd.concat([historical_data, recent_data]) # get realtime data threading.Thread(target=self._get_data_from_ws_api).start() def get_static_data(self): return self._static_data def get_data(self): return self._data @staticmethod def _download_data_from_s3(start: datetime = datetime(2014, 11, 22)): URL = "https://s3-eu-west-1.amazonaws.com/public.bitmex.com/data/trade/" if not os.path.exists(LOCAL_DATA_PATH): os.makedirs(LOCAL_DATA_PATH) for dt in [ start + timedelta(days=d) for d in range((datetime.now() - start).days) ]: file_name = f"{str(dt.year)}{str(dt.month).zfill(2)}{str(dt.day).zfill(2)}.csv.gz" file_path = os.path.join(LOCAL_DATA_PATH, file_name) if os.path.exists(file_path): continue try: logging.info(f"Downloading {file_name}") urlretrieve(os.path.join(URL, file_name), file_path) except Exception as e: logging.exception(e) break @staticmethod def _get_data_from_local(symbol: str, start_time: datetime): result = [] for dt in [ start_time + timedelta(days=d) for d in range((datetime.now() - start_time).days) ]: file_name = f"{str(dt.year)}{str(dt.month).zfill(2)}{str(dt.day).zfill(2)}.csv.gz" file_path = os.path.join(LOCAL_DATA_PATH, file_name) if os.path.exists(file_path): df = pd.read_csv(file_path, usecols=[ "timestamp", "symbol", "side", "size", "price", "trdMatchID" ]) df = df[df["symbol"] == symbol] result.append(df) else: break result = pd.concat(result) result["timestamp"] = pd.to_datetime(result["timestamp"], format="%Y-%m-%dD%H:%M:%S.%f") return result def _get_data_from_rest_api(self, start_time: datetime): delay = 0.5 count = 500 start = 0 result = [] while True: try: query = self._rest_api.Trade.Trade_get(symbol=self._symbol, startTime=start_time, start=start, count=count, reverse=False) response = query.result()[0] logging.info(f"Last timestamp: {response[-1]['timestamp']}") result.extend(response) except Exception as e: logging.error(e) break if len(response) < count: break start += count sleep(delay) result = pd.DataFrame(result) result = result[[ "timestamp", "symbol", "side", "size", "price", "trdMatchID" ]] result["timestamp"] = pd.to_datetime(result["timestamp"], format="%Y-%m-%dD%H:%M:%S.%f") return result def _get_data_from_ws_api(self): delay = 0.2 ids = set(self._static_data["trdMatchID"].tail(500)) while self._ws_api.ws.sock.connected: trades = self._ws_api.recent_trades() trades = [t for t in trades if t["trdMatchID"] not in ids] ids.update(t["trdMatchID"] for t in trades) self._data.extend(trades) sleep(delay) raise ConnectionError()
class Main(): def __init__(self): self.ws = BitMEXWebsocket( endpoint="wss://www.bitmex.com/realtime", symbol="XBTUSD", api_key='68_LZNeK1m7TEwnksVTe_7ig', api_secret='wKJXpjr6Y28BNDF4G97TBevkzLG0HVOurFDBn2dx42Sf_Aga') self.client = MongoClient('mongodb://localhost:27017/') self.db = self.client.main_database self.order_book = self.db.orderbook self.position = self.db.position self.balance = self.db.balance #uncomment these 3 methods to check out the db #to see orderbooks #self.check_db(self.find_all_orderbook()) #to see position #self.check_db(self.find_all_positions()) #to see balance #self.check_db(self.find_all_balance()) self.__on_open() self.fetch_data() def check_db(self, objects): for object in objects: self.logger(object) def logger(self, object): print(object) def find_all_positions(self): # Returns a list return self.position.find({}) def find_all_balance(self): # Returns a list return self.balance.find({}) def find_all_orderbook(self): # Returns a list return self.order_book.find({}) def list_db_collections(self): return self.db.list_collection_names() def __on_open(self): # Called when the WS opens. self.logger("Websocket Opened.") def __on_close(self): # Called on websocket close. self.logger('Websocket Closed') def fetch_data(self): # Balance funds = self.ws.funds() # Position positions = self.ws.recent_trades() # Order Book orderbook = self.ws.market_depth() self.__on_close(funds, positions, orderbook) def __on_open(self): '''Called when the WS opens.''' self.logger("Websocket Opened.") def __on_close(self, funds, positions, orderbook): '''Called on websocket close.''' # --- SAVE TO DB ---- # self.balance.insert_one(funds) for position in positions: self.position.insert_one(position) for trade in orderbook: self.order_book.insert_one(trade) # --- SAVE TO DB ---- # self.logger('Websocket Closed')
randID2 = ''.join(random.choice(string.ascii_lowercase) for i in range(15)) offsetLong = 0 offsetShort = 0 deadManSwitch = client.Order.Order_cancelAllAfter( timeout=60000.0).result() #dead man switch start switchCounter = 0 if (ws.get_instrument()['volume'] < ws.get_instrument()['volume24h'] / 24): result1 = client.Order.Order_new( symbol=symbol, ordType='Limit', orderQty=amount, price=ws.recent_trades()[0]['price'] - offsetLong, execInst='ParticipateDoNotInitiate').result() while (True): #check if order succesful else try again if (result1[0]['ordStatus'] == 'New'): print('order LONG uspesen') break else: offsetLong += 0.00000001 result1 = client.Order.Order_new( symbol=symbol, ordType='Limit', orderQty=amount, price=ws.recent_trades()[0]['price'] - offsetLong, execInst='ParticipateDoNotInitiate').result() print('LONG retry')