示例#1
0
def getTradeHistory(pair, connection=None, count=None):
    """Retrieve the trade history for the given pair.  Returns a list of
    Trade instances.  If count is not None, it should be an integer, and
    specifies the number of items from the trade history that will be
    processed and returned."""

    common.validatePair(pair)

    if connection is None:
        connection = common.BTCEConnection()

    history = connection.makeJSONRequest("/api/2/%s/trades" % pair)

    if type(history) is not list:
        raise TypeError("The response is a %r, not a list." % type(history))

    result = []

    # Limit the number of items returned if requested.
    if count is not None:
        history = history[:count]

    for h in history:
        h["pair"] = pair
        t = Trade(**h)
        result.append(t)
    return result
示例#2
0
    def _post(self, params, connection=None, raiseIfInvalidNonce=False):
        params["nonce"] = self.handler.getNextNonce(self.key)
        encoded_params = urllib.urlencode(params)

        # Hash the params string to produce the Sign header value
        H = hmac.new(self.secret, digestmod=hashlib.sha512)
        H.update(encoded_params)
        sign = H.hexdigest()

        if connection is None:
            connection = common.BTCEConnection()

        headers = {"Key": self.key, "Sign": sign}
        result = connection.makeJSONRequest("/tapi", headers, encoded_params)

        success = result.get(u'success')
        if not success:
            err_message = result.get(u'error')
            method = params.get("method", "[uknown method]")

            if "invalid nonce" in err_message:
                # If the nonce is out of sync, make one attempt to update to
                # the correct nonce.  This sometimes happens if a bot crashes
                # and the nonce file doesn't get saved, so it's reasonable to
                # attempt one correction.  If multiple threads/processes are
                # attempting to use the same key, this mechanism will
                # eventually fail and the InvalidNonce will be emitted so that
                # you'll end up here reading this comment. :)

                # The assumption is that the invalid nonce message looks like
                # "invalid nonce parameter; on key:4, you sent:3"
                s = err_message.split(",")
                expected = int(s[-2].split(":")[1].strip("'"))
                actual = int(s[-1].split(":")[1].strip("'"))
                if raiseIfInvalidNonce:
                    raise InvalidNonceException(method, expected, actual)

                warnings.warn("The nonce in the key file is out of date;"
                              " attempting to correct.")
                self.handler.setNextNonce(self.key, expected + 1000)
                return self._post(params, connection, True)
            elif "no orders" in err_message and method == "ActiveOrders":
                # ActiveOrders returns failure if there are no orders;
                # intercept this and return an empty dict.
                return {}
            elif "no trades" in err_message and method == "TradeHistory":
                # TradeHistory returns failure if there are no trades;
                # intercept this and return an empty dict.
                return {}

            raise Exception("%s call failed with error: %s"
                            % (method, err_message))

        if u'return' not in result:
            raise Exception("Response does not contain a 'return' item.")

        return result.get(u'return')
示例#3
0
def getTradeFee(pair, connection=None):
    """
    Retrieve the fee (in percent) associated with trades for a given pair.
    """

    common.validatePair(pair)

    if connection is None:
        connection = common.BTCEConnection()

    fees = connection.makeJSONRequest("/api/2/%s/fee" % pair)
    if type(fees) is not dict:
        raise TypeError("The response is not a dict.")

    trade_fee = fees.get(u'trade')
    if type(trade_fee) is not decimal.Decimal:
        raise TypeError("The response does not contain a trade fee")

    return trade_fee
示例#4
0
def getTicker(pair, connection=None):
    """Retrieve the ticker for the given pair.  Returns a Ticker instance."""

    # Commenting out the validate pair call since it seems to be old and is validating
    # against hard coded pairs in common.py which is clearly missing a lot of new pairs.
    #
    # common.validatePair(pair)

    if connection is None:
        connection = common.BTCEConnection()

    response = connection.makeJSONRequest("/api/2/%s/ticker" % pair)

    if type(response) is not dict:
        raise TypeError("The response is a %r, not a dict." % type(response))
    elif u'error' in response:
        print ("There is a error \"%s\" while obtaining ticker %s" % (response['error'], pair))
        ticker = None
    else:
        ticker = Ticker(**response[u'ticker'])

    return ticker
示例#5
0
def getDepth(pair, connection=None):
    """Retrieve the depth for the given pair.  Returns a tuple (asks, bids);
    each of these is a list of (price, volume) tuples."""

    common.validatePair(pair)

    if connection is None:
        connection = common.BTCEConnection()

    depth = connection.makeJSONRequest("/api/2/%s/depth" % pair)
    if type(depth) is not dict:
        raise TypeError("The response is not a dict.")

    asks = depth.get(u'asks')
    if type(asks) is not list:
        pp = pprint.PrettyPrinter(indent=4).pprint(depth)
        pp.pprint(depth)
        raise TypeError("The response does not contain an asks list.")

    bids = depth.get(u'bids')
    if type(bids) is not list:
        raise TypeError("The response does not contain a bids list.")

    return asks, bids
示例#6
0
def getInfo(connection=None):
    if connection is None:
        connection = common.BTCEConnection()

    return connection.makeJSONRequest("/api/3/info")