def fulldepth_thread(): """Request the full market depth, initialize the order book and then terminate. This is called in a separate thread after the streaming API has been connected.""" querystring = "?pair=%s" % self.pair # self.debug("### requesting full depth") json_depth = http_request("%s://%s/0/public/Depth%s" % ( self.proto, HTTP_HOST, querystring )) if json_depth and not self._terminating: try: fulldepth = json.loads(json_depth) depth = {} depth['error'] = fulldepth['error'] # depth['data'] = fulldepth['result'] depth['data'] = {'asks': [], 'bids': []} for ask in fulldepth['result'][self.pair]['asks']: depth['data']['asks'].append({ 'price': float(ask[0]), 'amount': float(ask[1]) }) for bid in reversed(fulldepth['result'][self.pair]['bids']): depth['data']['bids'].append({ 'price': float(bid[0]), 'amount': float(bid[1]) }) if depth: self.signal_fulldepth(self, (depth)) except Exception as exc: self.debug("### exception in fulldepth_thread:", exc)
def http_signed_call(self, api_endpoint, params): """send a signed request to the HTTP API V2""" if (not self.secret) or (not self.secret.know_secret()): self.debug("### don't know secret, cannot call %s" % api_endpoint) return key = self.secret.key sec = self.secret.secret params["nonce"] = self.get_unique_mirotime() post = urlencode(params) # prefix = api_endpoint # sign = hmac.new(base64.b64decode(sec), prefix + post, hashlib.sha512).digest() sign = hmac.new(sec, post, hashlib.sha512).hexdigest() headers = { 'Key': key, 'Sign': base64.b64encode(sign) } url = "%s://%s/%s" % ( self.proto, HTTP_HOST, api_endpoint ) # self.debug("### (%s) calling %s" % (self.proto, url)) try: result = json.loads(http_request(url, post, headers)) return result except ValueError as exc: self.debug("### exception in http_signed_call:", exc)
def ticker_thread(): querystring = "?pair=%s" % self.pair json_ticker = http_request("%s://%s/0/public/Ticker%s" % ( self.proto, HTTP_HOST, querystring )) if not self._terminating: try: answer = json.loads(json_ticker) # self.debug("TICK %s" % answer) if not answer["error"]: bid = float(answer['result'][self.pair]['b'][0]) ask = float(answer['result'][self.pair]['a'][0]) self.signal_ticker(self, (bid, ask)) except Exception as exc: self.debug("### exception in ticker_thread:", exc)
def lag_thread(): json_time = http_request("%s://%s/0/public/Time" % ( self.proto, HTTP_HOST )) if not self._terminating: try: answer = json.loads(json_time) if not answer["error"]: lag = time.time() - answer['result']['unixtime'] result = { 'lag': lag * 1000, 'lag_text': "%0.3f s" % lag } translated = { "op": "result", "result": result, "id": "order_lag" } self.signal_recv(self, (json.dumps(translated))) except Exception as exc: self.debug("### exception in lag_thread:", exc)
def fulldepth_thread(): """request the full market depth, initialize the order book and then terminate. This is called in a separate thread after the streaming API has been connected.""" # self.debug("### requesting full depth") json_depth = http_request("%s://%s/public?command=returnOrderBook¤cyPair=%s&depth=500" % ( self.proto, HTTP_HOST, self.pair )) if json_depth and not self._terminating: try: fulldepth = json.loads(json_depth) # self.debug("Depth: %s" % fulldepth) depth = {} depth['error'] = {} if 'error' in fulldepth: depth['error'] = fulldepth['error'] depth['data'] = {'asks': [], 'bids': []} for ask in fulldepth['asks']: depth['data']['asks'].append({ 'price': float(ask[0]), 'amount': float(ask[1]) }) for bid in reversed(fulldepth['bids']): depth['data']['bids'].append({ 'price': float(bid[0]), 'amount': float(bid[1]) }) self.signal_fulldepth(self, depth) except Exception as exc: self.debug("### exception in fulldepth_thread:", exc)
def history_thread(): """request trading history""" querystring = "?pair=%s" % self.pair if not self.history_last_candle: querystring += "&since=%i" % ((time.time() - 172800) * 1e9) # self.debug("Requesting history since: %s" % time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time() - 172800))) else: querystring += "&since=%i" % (self.history_last_candle * 1e9) # self.debug("Last candle: %s" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.history_last_candle))) # self.debug("### requesting history") json_hist = http_request("%s://%s/0/public/Trades%s" % ( self.proto, HTTP_HOST, querystring )) if json_hist and not self._terminating: try: raw_history = json.loads(json_hist) if raw_history['error']: self.debug("Error in history: %s" % raw_history['error']) return # self.debug("History: %s" % raw_history) history = [] for h in raw_history["result"][self.pair]: history.append({ 'price': float(h[0]), 'amount': float(h[1]), 'date': h[2] }) if history: self.signal_fullhistory(self, history) except Exception as exc: self.debug("### exception in history_thread:", exc)
def history_thread(): """request trading history""" if not self.history_last_candle: querystring = "&start=%i&end=%i" % ((time.time() - 172800), (time.time() - 86400)) # self.debug("### requesting 2d history since %s" % time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time() - 172800))) else: querystring = "&start=%i" % (self.history_last_candle - 14400) # self.debug("Last candle: %s" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.history_last_candle - 14400))) json_hist = http_request("%s://%s/public?command=returnTradeHistory¤cyPair=%s%s" % ( self.proto, HTTP_HOST, self.pair, querystring )) if json_hist and not self._terminating: try: raw_history = json.loads(json_hist) # self.debug("History: %s" % raw_history) history = [] for h in reversed(raw_history): history.append({ 'price': float(h['rate']), 'amount': float(h['amount']), 'date': time.mktime(time.strptime(h['date'], "%Y-%m-%d %H:%M:%S")) - 480 }) # self.debug("History: %s" % history) if history and not self._terminating: self.signal_fullhistory(self, history) except Exception as exc: self.debug("### exception in history_thread:", exc)