Ejemplo n.º 1
0
    def _private_api(self, method, path, extra=None):
        debugger.debug('_private_api, [{}], [{}], [{}]'.format(
            method, path, extra))
        if extra is None:
            extra = dict()

        extra.update({
            'access_token': self._key,
            'nonce': int(time.time() * 1000)
        })

        payload = self.get_encoded_payload(extra)
        signature = self.get_signature(payload)

        headers = {
            'Content-type': 'application/json',
            'X-COINONE-PAYLOAD': payload,
            'X-COINONE-SIGNATURE': signature,
        }

        url = self._base_url + path

        rq = requests.post(url, method, payload, headers=headers)

        if rq.status_code != 200:
            debugger.debug('request fail, [{}]'.format(rq.text))
            return dict()

        result = rq.json()

        return result
Ejemplo n.º 2
0
    def get_account_id(self):
        for _ in range(3):
            result_object = self.api_request('GET', Urls.GET_ACCOUNT)

            if result_object.success:
                self.account_id = result_object.data[0]['id']
                break
            debugger.debug(result_object.message)
        else:
            raise
Ejemplo n.º 3
0
    def buy(self, amount, coin, price=None, is_limit=True):
        params = dict()
        if is_limit is True:
            params.update(dict(price=price, type='buy-limit'))
        else:
            params.update(dict(type='buy-market'))

        params.update({
            'account-id': str(self.account_id),
            'symbol': coin,
            'amount': '{}'.format(amount).strip()
        })
        debugger.debug("params = {}.".format(params))

        return self.api_request('POST', Urls.TRADE, params)
Ejemplo n.º 4
0
    async def get_market_overview_forever(self):
        for _ in range(10):
            try:
                await self.huobi_client.ws_connect()
                break
            except Exception as e:
                debugger.exception(
                    'unexpected error : {}\ntry to reconnect to Huobi Websocket Server'
                    .format(e))

        self.huobi_client.subscribe_market_overview()

        while True:
            msg = await self.huobi_client.get_market_overview()
            if msg is None:
                self.huobi_client.ws_close()
                break

            with gzip.open(BytesIO(msg), 'rb') as f:
                raw_msg = f.read().decode('utf-8')
                msg = json.loads(raw_msg)

                # pong should be sent back to the Huobi Server with the same value in ping
                # otherwise the connection to the server closes
                if 'ping' in msg:
                    ping = msg['ping']
                    pong = json.dumps(dict(pong=ping))

                    self.huobi_client.ws_send(pong)
                # subscribe success messeage is not needed
                elif 'subbed' in msg:
                    pass
                else:
                    market_overview = msg['data']
                    self.market_overview_queue.put(market_overview)

        debugger.debug('trying to reconnect to Huobi Websocket Server')
        self.get_market_overview_forever()
Ejemplo n.º 5
0
 def run_huobi_websocket_client(self):
     self.huobi_client = HuobiClientRunner(self.market_overview_queue)
     debugger.debug('started : Huobi Websocket Client')
Ejemplo n.º 6
0
 def run_market_overview_push_thread(self):
     self.market_overview_thread = MarketOverviewPushThread(
         self.market_overview_queue, self.stopped, self.loop)
     self.market_overview_thread.start()
     debugger.debug('started : {}'.format(self.market_overview_thread.name))
Ejemplo n.º 7
0
 def on_close(self):
     debugger.debug("Tornado Websocket Connection Closed")
     self.remove_connection()
Ejemplo n.º 8
0
 def open(self):
     debugger.debug("Tornado Websocket Connection Opened")
     self.add_connection()
Ejemplo n.º 9
0
 def ws_close(self):
     self.ws.close()
     debugger.debug('Huobi Websocket connection closed')
Ejemplo n.º 10
0
 def subscribe_market_overview(self):
     market_overview_topic = json.dumps(SubscribeTopics.MARKET_OVERVIEW)
     self.ws.write_message(market_overview_topic)
     debugger.debug('subscribed market overview topic')
Ejemplo n.º 11
0
 async def ws_connect(self):
     self.ws = await websocket_connect(self.url)
     debugger.debug('connected to Huobi Websocket Server')
     return self.ws