示例#1
0
    def _handle_private_wallet(self, wallet):
        # Reason for update (in, out, fee, earned, spent, withdraw, deposit)
        reason = wallet["op"]
        info = wallet["info"]
        ref = wallet["ref"] # Reference code for bank transfer, if applicable.

        currency = wallet['balance']['currency']
        factor = currency_factor(currency)
        amount = Decimal(wallet['balance']['value_int']) / factor

        self.evt.emit('wallet_update', (currency, amount, reason, info, ref))
示例#2
0
    def _extract_order(self, order):
        oid = order['oid']
        coin = order['item']
        amount = Decimal(order['amount']['value_int']) / CURRENCY_FACTOR[coin]
        currency = order['currency']
        factor = currency_factor(currency)
        price = Decimal(order['price']['value_int']) / factor
        timestamp = int(order['date'])
        status = order['status']
        otype = order['type']

        return common.Order(oid, timestamp, otype, status, price, amount,
                (currency, coin))
示例#3
0
    def _handle_depth(self, depth):
        currency = depth["currency"]
        if currency != self.currency:
            # Ignore bid/ask in other currencies.
            return

        coin = depth["item"]
        dtype = depth["type_str"][0]
        volume = Decimal(depth["total_volume_int"]) / CURRENCY_FACTOR[coin]
        factor = currency_factor(currency)
        price = Decimal(depth["price_int"]) / factor

        self.evt.emit('depth', common.Depth(dtype, price, volume))
示例#4
0
    def _extract_trade(self, trade):
        tid = int(trade['tid'])
        currency = trade["price_currency"]
        if currency != self.currency or trade['primary'].lower() != 'y':
            # Ignore trades in different currency or that are not
            # primary.
            return common.Trade(tid, None, None, None, None)

        timestamp = int(trade['date'])
        ttype = trade['trade_type']
        if ttype:
            # Old trades always set trade_type to ''.
            # On newer trades, pick only the first letter from
            # (a)sk/(b)id.
            ttype = ttype[0]
        factor = currency_factor(currency)
        price = Decimal(trade['price_int']) / factor
        coin = trade['item']
        amount = Decimal(trade['amount_int']) / CURRENCY_FACTOR[coin]

        return common.Trade(tid, timestamp, ttype, price, amount)
示例#5
0
    def _handle_result(self, result, req_id, **kwargs):
        if req_id in self.pending_scall:
            query = self.pending_scall.pop(req_id)
            start = 1 if query['call'].startswith('/') else 0
            name = query['call'][start:]
        else:
            # Result from HTTP API
            name = req_id
            if result['result'] != 'success':
                # Call failed.
                result['params'] = kwargs
                result['params'].update(url=name)
                self._handle_remark(result)
                return
            result = result['data'] if 'data' in result else result['return']

        if name == 'idkey':
            self.evt.emit(name, result)
        elif name == 'orders':
            for order in result:
                self.evt.emit('userorder', self._extract_order(order))
        elif name == 'info':
            # Result from the info method.
            trade_fee = Decimal(str(result['Trade_Fee']))
            rights = result['Rights']
            self.evt.emit(name, (trade_fee, rights))
        elif name == 'wallet/history':
            # Result from the wallet_history method.
            self.evt.emit('wallet_history', result)
        elif name == 'order/add':
            # Result for the order_add method.
            self.evt.emit('order_added', (result, req_id))
        elif name == 'order/cancel':
            # Result for the order_cancel method.
            # Note: result['qid'] is being ignored for now.
            self.evt.emit('order_canceled', (result['oid'], req_id))

        elif name.endswith('/trades/fetch'):
            # Result from the load_trades_since method.
            for trade in result or []:
                trade = self._extract_trade(trade)
                if trade.price is None:
                    continue
                self.evt.emit('trade_fetch', trade)
            # Indicate end of fetch.
            self.evt.emit('trade_fetch', common.TRADE_EMPTY)
        elif name.endswith('/depth/fetch') or name.endswith('/depth/full'):
            # Result from depth_fetch or depth_full method.
            factor = currency_factor(self.currency)
            coin = CURRENCY_FACTOR['BTC'] # XXX
            for typ in ('bid', 'ask'):
                entry = '%ss' % typ
                for order in result[entry]:
                    price = Decimal(order['price_int']) / factor
                    amount = Decimal(order['amount_int']) / coin
                    depth = common.Depth(typ[0], price, amount)
                    self.evt.emit('depth_fetch', depth)
            # Indicate end of fetch.
            self.evt.emit('depth_fetch', common.DEPTH_EMPTY)
        elif name.endswith('/ticker'):
            self.evt.emit('ticker_fetch',
                    self._extract_ticker(result, restrict_currency=False))
        elif name.endswith('/currency'):
            self.evt.emit('currency_info', result)

        else:
            rtype = name.lstrip('/').replace('/', '_')
            if rtype[0] in ('1', '2') and rtype[1] == '_':
                # Assuming this is the result of an HTTP API call
                # and the version used is not interesting.
                rtype = rtype[2:]
            log.msg("Emitting result event for %s" % rtype)
            self.evt.emit('result', (rtype, result))
示例#6
0
 def order_add(self, otype, amount_dec, price_dec=0):
     amount_int = int(common.currency_factor(self.coin) *
             Decimal(amount_dec))
     price_int = int(common.currency_factor(self.currency) *
             Decimal(price_dec))
     return self._order_add(otype, amount_int, price_int)