def bitstamp_message(self, *args, **kwargs): try: gc.collect() response = ast.literal_eval(args[0]) #log_function('Bitstamp received! :)') trade_id = response['id'] unix_time = int(response['timestamp']) price = float(response['price']) size_volume = float( response['amount']) if not response['type'] else -float( response['amount']) created_at = datetime.utcnow() vwap = price * abs(size_volume) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: log_function("Bitstamp error! :(") log_function(str(e))
def on_message(self, ws, message): try: # log_function("Poloniex received! :)") gc.collect() responses = ast.literal_eval(message) if len(responses) > 1: for response in responses[2]: if response[0] == 't': trade_id = response[1] unix_time = int(response[5]) price = float(response[3]) size_volume = float( response[4]) if response[2] else -float( response[4]) created_at = datetime.utcnow() vwap = price * abs(size_volume) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: log_function("Poloniex error! :(") log_function(str(e))
def on_message(self, ws, message): try: gc.collect() response = ast.literal_eval( gzip.decompress(message).decode('utf-8')) # log_function('Houbi received! :)') if 'tick' in response: for transaction in response["tick"]["data"]: trade_id = transaction["id"] unix_time = int(transaction["ts"] / 1000) price = float(transaction["price"]) size_volume = float( transaction["amount"] ) if transaction['direction'] == 'buy' else -float( transaction["amount"]) created_at = datetime.utcnow() vwap = price * abs(size_volume) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: log_function("Huobi error! :(") log_function(str(e))
def on_message(self, ws, message): try: gc.collect() response = json.loads(message) # log_function("Hitbtc received! :)") if 'method' in response and 'updateTrades' in response["method"]: trade_id = response["params"]["data"][0]["id"] unix_time = int( calendar.timegm( datetime.strptime( response["params"]["data"][0]["timestamp"][:19], "%Y-%m-%dT%X").timetuple())) price = float(response["params"]["data"][0]["price"]) size_volume = float( response["params"]["data"][0]["quantity"]) if response[ "params"]["data"][0]["side"] == 'buy' else -float( response["params"]["data"][0]["quantity"]) created_at = datetime.utcnow() vwap = price * abs(size_volume) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: log_function("Hitbtc error! :(") log_function(str(e))
def on_message(self, message): try: gc.collect() if 'product_id' in message and 'trade_id' in message and 'time' in message and 'price' in message and 'side' in message: # log_function('Coinbase received! :)') exchange = 'Coinbase' pair = message['product_id'].replace('-', '') trade_id = message['trade_id'] if 'trade_id' in message else "-" unix_time = int( time.mktime( datetime.strptime( message['time'], "%Y-%m-%dT%H:%M:%S.%fZ").timetuple())) - 25200 price = float(message['price']) size_volume = float( message['last_size'] ) if message['side'] == 'buy' else -float(message['last_size']) created_at = datetime.utcnow() vwap = price * abs(size_volume) write_db(self.cursor, self.cnx, exchange=exchange, pair=pair, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: log_function("Coinbase error! :(") log_function(str(e))
def run(self): while True: gc.collect() current_unix = int(time.time()) while current_unix % 60 > 0: current_unix = int(time.time()) time.sleep(1) lower_unix = current_unix - 60 result = read_db_result_null(self.cursor, pair=self.pair, current_unix=current_unix, lower_unix=lower_unix) payload = { 'pair': self.pair, 'unix_open': lower_unix, 'unix_close': current_unix, 'created_at': datetime.utcnow() } if result[0][0] is None: payload.update({ 'open': 0, 'high': 0, 'low': 0, 'close': 0, 'volume': 0, 'typical_price': 0, }) else: open_payload, close_payload, result_open, result_close = read_db_result_not_null( self.cursor, pair=self.pair, result=result) open = round(open_payload / len(result_open), 2) high = round(result[0][3], 2) low = round(result[0][4], 2) close = round(close_payload / len(result_close), 2) volume = round(result[0][0], 4) typical_price = round( (open_payload / len(result_open) + result[0][3] + result[0][4] + close_payload / len(result_close)) / 4, 2) payload.update({ 'open': open, 'high': high, 'low': low, 'close': close, 'volume': volume, 'typical_price': typical_price, }) write_db_result(self.cursor, self.cnx, **payload) log_function('OHLCVT saved! :)') time.sleep(50)
def run(self): """ Kraken does not have socket so Rest is implemented. """ trade_id = '-' kraken_start = krakenex.API() while True: gc.collect() try: responses = kraken_start.query_public( 'Trades', {"pair": CRYPTO_SYMBOLS['kraken'][self.pair]}) # log_function("Kreken received! :)") unix_time_s = responses['result']['last'][:10] unix_time_ms = responses['result']['last'][10:] unix_time_set = (unix_time_s, unix_time_ms) self.unix_time_g_kraken = round(float('.'.join(unix_time_set)), 4) for response in responses['result'][ CRYPTO_SYMBOLS['kraken_sub'][self.pair]]: res_time = round(float(response[2]), 4) if res_time > self.unix_time_kraken: unix_time = int(response[2]) price = float(response[0]) size_volume = float( response[1]) if response[3] == 'b' else -float( response[1]) created_at = datetime.utcnow() vwap = price * abs(size_volume) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) self.unix_time_kraken = self.unix_time_g_kraken time.sleep(45) except Exception as e: log_function("Kraken error! :(") log_function(str(e)) time.sleep(45)
def on_message(self, ws, message): try: gc.collect() response = ast.literal_eval(message) if 'eventId' in response and 'timestamp' in response and 'events' in response: # log_function('Gemini received! :)') trade_id = response['eventId'] unix_time = int(response['timestamp']) price = float(response['events'][0]['price']) size_volume = float(response['events'][0]['amount']) if response['events'][0][ 'makerSide'] == 'ask' else -float( response['events'][0]['amount']) created_at = datetime.utcnow() vwap = price * abs(size_volume) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: log_function("Gemini error! :(") log_function(str(e))
def on_message(self, ws, message): try: gc.collect() # log_function('Binance received! :)') response = json.loads(message) response = response['data'] pair = "BCHUSD" if str(response['s'])[:-1] == "BCCUSD" else str(response['s'])[:-1] if pair == "BCCUSD": pair = "BCHUSD" trade_id = response['t'] unix_time = int(response['T'] / 1000) price = float(response['p']) size_volume = float(response['q']) if response['m'] else -float(response['q']) created_at = datetime.utcnow() vwap = price * abs(size_volume) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: log_function('Binance error! :(') log_function(str(e))
def run(self): unix_ms = 0 while True: gc.collect() try: r = requests.get("{market}{pair}".format( market=self.market, pair=CRYPTO_SYMBOLS['kucoin'][self.pair])).json() # log_function("Kucoin received! :)") if 'data' in r: for response in r['data']: if response[0] > unix_ms: trade_id = '-' unix_time = int(response[0] / 1000) price = float(response[2]) size_volume = float( response[3] ) if response[1] == 'BUY' else -float(response[3]) created_at = datetime.utcnow() vwap = price * abs(size_volume) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) unix_ms = response[0] time.sleep(45) except Exception as e: self.cnx.reconnect(attempts=1, delay=0) log_function("Kucoin error! :(") log_function(str(e))
def run(self): """ hb - heart beat is returned when there is no action on market te - message which mimics the current behavior, use it if speed is important to you tu - message which will be delayed by 1-2 seconds and include the tradeId, use if the tradeId is important to you """ while True: gc.collect() try: # log_function('Bitfinex received! :)') try: tu_btc = self.ticker_btc.get() if tu_btc[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_btc) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair_btc, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_eth = self.ticker_eth.get() if tu_eth[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_eth) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair_eth, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_etc = self.ticker_etc.get() if tu_etc[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_etc) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair_etc, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_ltc = self.ticker_ltc.get() if tu_ltc[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_ltc) write_db(self.cursor, self.cnx, exchange=self.exchange, pair=self.pair_ltc, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_dsh = self.ticker_dsh.get() if tu_dsh[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_dsh) pair_dsh = 'DASHUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_dsh, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_bch = self.ticker_bch.get() if tu_bch[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_bch) pair_bch = 'BCHUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_bch, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_btg = self.ticker_btg.get() if tu_btg[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_btg) pair_btg = 'BTGUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_btg, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_eos = self.ticker_eos.get() if tu_eos[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_eos) pair_eos = 'EOSUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_eos, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_trx = self.ticker_trx.get() if tu_trx[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_trx) pair_trx = 'TRXUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_trx, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_xmr = self.ticker_xmr.get() if tu_xmr[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_xmr) pair_xmr = 'XMRUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_xmr, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_vet = self.ticker_vet.get() if tu_vet[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_vet) pair_vet = 'VETUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_vet, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_iot = self.ticker_iot.get() if tu_iot[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_iot) pair_iot = 'IOTAUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_iot, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_zec = self.ticker_zec.get() if tu_zec[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_zec) pair_zec = 'ZECUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_zec, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass try: tu_neo = self.ticker_neo.get() if tu_neo[0][0] == "tu": trade_id, unix_time, price, size_volume, created_at, vwap = self._extract_data( tu_neo) pair_neo = 'NEOUSD' write_db(self.cursor, self.cnx, exchange=self.exchange, pair=pair_neo, trade_id=trade_id, unix_time=unix_time, price=price, size_volume=size_volume, created_at=created_at, vwap=vwap) except Exception as e: pass except Exception as e: log_function("Bitfinex error") log_function(str(e))