예제 #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 Exception("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
파일: trade.py 프로젝트: nsandman/btce-api
    def _post(self, params, connection=None, raiseIfInvalidNonce=False):
        self.nonce = int(time.time())
        params["nonce"] = self.nonce
        encoded_params = urllib.parse.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('success')
        if not success:
            err_message = result.get('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 + 1)"""
                self.nonce = int(time.time())
                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 'return' not in result:
            raise Exception("Response does not contain a 'return' item.")

        return result.get('return')
예제 #3
0
파일: public.py 프로젝트: zm801/btce-api
def getTicker(pair, connection=None):
    '''Retrieve the ticker for the given pair.  Returns a Ticker instance.'''

    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))

    return Ticker(**response[u'ticker'])
예제 #4
0
def getTradeFee(pair, connection=None):
    '''Retrieve the fee 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 Exception("The response is not a dict.")

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

    return trade_fee
예제 #5
0
 def _attemptSell(self, price, amount):
     conn = common.BTCEConnection()
     info = self.api.getInfo(conn)
     curr1, curr2 = self.pair.split("_")
     
     # Limit order to what we have available to sell.
     available = getattr(info, "balance_" + curr1)
     sell_amount = min(available, amount) * self.fee_adjustment
     if sell_amount >= common.min_orders[self.pair]:
         print "attempting to sell %s %s at %s for %s %s" % (sell_amount,
             curr1.upper(), price, sell_amount*price, curr2.upper())
         if self.live_trades:
             r = self.api.trade(self.pair, "sell", price, sell_amount, conn)
             print "\tReceived %s %s" % (r.received, curr2.upper())
             # If the order didn't fill completely, cancel the remaining order
             if r.order_id != 0:
                 print "\tCanceling unfilled portion of order"
                 self.api.cancelOrder(r.order_id, conn)
예제 #6
0
def getTicker(pair, connection=None):
    """Retrieve the ticker for the given pair.  Returns a Ticker instance."""

    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 'error' in response:
        print(("There is a error \"%s\" while obtaining ticker %s" % (response['error'], pair))) 
        ticker = None
    else:
        ticker = Ticker(**response['ticker'])

    return ticker
예제 #7
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 Exception("The response is not a dict.")

    asks = depth.get(u'asks')
    if type(asks) is not list:
        raise Exception("The response does not contain an asks list.")

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

    return asks, bids