Esempio n. 1
0
class PoloniexClientWrapper:
    MAX_TRIES = 3
    SLEEP_TRIES = 1
    API_KEY = EXCHANGE_DATA["poloniex"]["api_key"]
    API_SECRET = EXCHANGE_DATA["poloniex"]["api_secret"]

    def __init__(self):
        self._client = Client(self.API_KEY, self.API_SECRET)
        self._cache = dict()

    def get_orderbook(self, symbol="BTC_USDT", limit=4612):
        operation = PoloniexOperation.GET_ORDERBOOK
        params = {"symbol": self._invert_symbol(symbol), "limit": limit}
        return self._request(operation, params)

    def get_orderbook_ticker(self, symbol="BTC_USDT"):
        operation = PoloniexOperation.GET_ORDERBOOK_TICKER
        params = {"symbol": self._invert_symbol(symbol)}
        return self._request(operation, params)

    def get_orderbook_tickers(self):
        operation = PoloniexOperation.GET_ORDERBOOK_TICKERS
        return self._request(operation)

    def get_recent_trades(self, symbol="BTC_USDT", limit=200):
        operation = PoloniexOperation.GET_RECENT_TRADES
        params = {"symbol": self._invert_symbol(symbol), "limit": limit}
        return self._request(operation, params)

    def get_coins(self):
        operation = PoloniexOperation.GET_COINS
        return self._request(operation)

    def _invert_symbol(self, symbol):
        coin_1, coin_2 = symbol.split("_")
        symbol = coin_2 + "_" + coin_1
        return symbol

    def _request(self, operation, params=dict()):
        current_time = time.time()

        # Create cache key.
        key = str(operation)
        for p in params:
            key += str(params[p])

        if key in self._cache and \
                abs(current_time - self._cache[key]["time"]) < 60:
            return self._cache[key]["database"]
        else:
            i = 0
            while i < self.MAX_TRIES:
                try:
                    if operation == PoloniexOperation.GET_ORDERBOOK:
                        response = self._client.returnOrderBook(
                            currencyPair=params["symbol"],
                            depth=params["limit"])
                    elif operation == PoloniexOperation.GET_ORDERBOOK_TICKER:
                        response = self._client.returnOrderBook(
                            currencyPair=params["symbol"], depth=1)
                    elif operation == PoloniexOperation.GET_ORDERBOOK_TICKERS:
                        response = self._client.returnOrderBook(
                            currencyPair="all", depth=1)
                    elif operation == PoloniexOperation.GET_RECENT_TRADES:
                        response = self._client.returnTradeHistoryPublic(
                            currencyPair=params["symbol"])[:params["limit"]]
                    elif operation == PoloniexOperation.GET_COINS:
                        response = self._client._private('returnBalances')
                    break
                except Exception as e:
                    print(e)
                    if i < self.MAX_TRIES:
                        time.sleep(self.SLEEP_TRIES)
                        self._client = Client(self.API_KEY, self.API_SECRET)
                        current_time = time.time()
                        i += 1
                    else:
                        return None

        if key not in self._cache:
            self._cache[key] = dict()

        # Update cache.
        self._cache[key]["time"] = current_time
        self._cache[key]["database"] = response

        return response