Exemplo n.º 1
0
    def get_balance(self, btype='total'):
        """
        :param str btype: The balance types to include
        """
        try:
            stampbal = json.loads(self.submit_request('balance', {}, True))
            if 'btc_balance' not in stampbal or 'usd_balance' not in stampbal:
                raise ExchangeError(
                    exchange='bitstamp',
                    message="Bitstamp balance information unavailable")
        except ValueError as e:
            raise ExchangeError(
                'bitfinex', '%s %s while sending to bitfinex get_open_orders' %
                (type(e), str(e)))

        if btype == 'total':
            total = MultiMoney(Money(stampbal['btc_balance']),
                               Money(stampbal['usd_balance'], currency='USD'))
            return total
        elif btype == 'available':
            available = MultiMoney(
                Money(stampbal['btc_available']),
                Money(stampbal['usd_available'], currency='USD'))
            return available
        else:
            total = MultiMoney(Money(stampbal['btc_balance']),
                               Money(stampbal['usd_balance'], currency='USD'))
            # TODO this isn't correct
            available = MultiMoney(
                Money(stampbal['btc_available']),
                Money(stampbal['usd_available'], currency='USD'))
            return total, available
Exemplo n.º 2
0
    def get_balance(self, btype='total'):
        ainfo = self.account_info()
        total = MultiMoney()
        available = MultiMoney()
        if ainfo is None:
            if btype == 'all':
                return total, available
            else:
                return total
        # XXX
        if ('btc' not in ainfo['balance']
                or 'amount' not in ainfo['balance']['btc']
                or ainfo['balance']['btc']['amount'] is None):
            ainfo['balance']['btc']['amount'] = 0
        if ainfo['balance']['cny']['amount'] is None:
            ainfo['balance']['cny']['amount'] = 0
        if ainfo['frozen']['btc']['amount'] is None:
            ainfo['frozen']['btc']['amount'] = 0
        if ainfo['frozen']['cny']['amount'] is None:
            ainfo['frozen']['cny']['amount'] = 0
        available += Money(amount=ainfo['balance']['btc']['amount'])
        total += Money(amount=ainfo['balance']['btc']['amount'])
        total += Money(amount=ainfo['frozen']['btc']['amount'])
        available += Money(amount=ainfo['balance']['cny']['amount'],
                           currency='CNY')
        total += Money(amount=ainfo['balance']['cny']['amount'],
                       currency='CNY')
        total += Money(amount=ainfo['frozen']['cny']['amount'], currency='CNY')

        if btype == 'total':
            return total
        elif btype == 'available':
            return available
        else:
            return total, available
    def test_sub(self):
        piggy = copy.copy(self.one_mixed_fortune)
        piggy -= MultiMoney(self.one_million_bucks)
        assert piggy == MultiMoney(self.one_thousand_bitcoins)

        piggy -= self.one_thousand_bitcoins
        assert piggy == MultiMoney()

        piggy -= self.one_million_bucks
        assert piggy == MultiMoney(self.one_million_bucks * -1)
Exemplo n.º 4
0
 def get_balance(self, btype='total'):
     data = self.huobi_request('get_account_info')
     avail = MultiMoney(Money(data['available_btc_display']),
                        Money(data['available_cny_display'], 'CNY'))
     frozen = MultiMoney(Money(data['frozen_btc_display']),
                         Money(data['frozen_cny_display'], 'CNY'))
     if btype == 'total':
         return avail + frozen
     elif btype == 'available':
         return avail
     return avail + frozen, avail
    def test_add(self):
        piggy = MultiMoney()
        piggy += MultiMoney(self.one_million_bucks)
        assert piggy == MultiMoney(self.one_million_bucks)

        piggy += self.one_million_bucks
        assert piggy == MultiMoney(self.one_million_bucks * 2)

        piggy += self.one_thousand_bitcoins
        assert piggy == MultiMoney(self.one_million_bucks * 2,
                                   self.one_thousand_bitcoins)
    def setup_method(self, method):
        self.one_million_decimal = Decimal('1000000')
        self.USD = CURRENCIES['USD']
        self.one_million_bucks = Money(amount=self.one_million_decimal,
                                       currency=self.USD)

        self.one_thousand_decimal = Decimal('1000')
        self.BTC = CURRENCIES['BTC']
        self.one_thousand_bitcoins = Money(amount=self.one_thousand_decimal,
                                           currency=self.BTC)

        self.one_mixed_fortune = MultiMoney(self.one_million_bucks,
                                            self.one_thousand_bitcoins)
Exemplo n.º 7
0
    def get_balance(self, btype='total'):
        data = polo.returnCompleteBalances()

        # filter balances for btc and dash, report totals for both together
        btc_bal = data['BTC']
        usdt_bal = data['USDT']
        available = MultiMoney(Money(btc_bal['available'], currency='BTC'), Money(usdt_bal['available'], currency='USD'))
        onOrders = MultiMoney(Money(btc_bal['onOrders'], currency='BTC'), Money(usdt_bal['onOrders'], currency='USD'))
        if btype == 'total':
            return available + onOrders
        elif btype == 'available':
            return available
        return available + onOrders, available
Exemplo n.º 8
0
    def get_balance(self, btype='total'):
        data = self.okcoin_request('userinfo.do')

        free = MultiMoney()
        freeze = MultiMoney()
        for cur, amount in data['info']['funds']['free'].iteritems():
            free += Money(amount, currency=cur)
            freeze += Money(data['info']['funds']['freezed'][cur],
                            currency=cur)
        if btype == 'total':
            return free + freeze
        elif btype == 'available':
            return free
        return freeze + free, free
    def test_lt(self):
        one_buck_piggy = MultiMoney(Money(amount=1, currency=self.USD))
        assert one_buck_piggy < MultiMoney(self.one_million_bucks)
        assert one_buck_piggy < self.one_mixed_fortune
        one_buck_thousand_bitcoin_piggy = one_buck_piggy + self.one_thousand_bitcoins
        assert one_buck_thousand_bitcoin_piggy < self.one_mixed_fortune

        assert not MultiMoney(Money(currency='USD')) < MultiMoney()
        assert not self.one_mixed_fortune < MultiMoney()
        assert not self.one_mixed_fortune < one_buck_thousand_bitcoin_piggy
        assert not MultiMoney() < MultiMoney()
        assert not MultiMoney() < MultiMoney(Money())
Exemplo n.º 10
0
    def test_ne(self):
        piggy = MultiMoney()
        assert piggy != MultiMoney(Money(amount=1))
        assert piggy != Money(amount=1)
        assert piggy != 1

        piggy += Money(amount=1, currency='BTC')
        assert piggy != MultiMoney(Money(amount=2))
        assert piggy != Money(amount=2)
        assert piggy != 2

        piggy += Money(amount=1, currency='USD')
        assert piggy != MultiMoney(Money(amount=1),
                                   Money(amount=2, currency='USD'))
        assert piggy != Money(amount=1)
        assert piggy != 1
Exemplo n.º 11
0
    def test_eq(self):
        piggy = MultiMoney()
        assert piggy == MultiMoney(Money(amount=0))
        assert piggy == Money(amount=0)
        assert piggy == 0

        piggy += Money(amount=1, currency='BTC')
        assert piggy == MultiMoney(Money(amount=1))
        assert piggy == Money(amount=1)
        assert piggy == 1

        piggy += Money(amount=1, currency='USD')
        assert piggy == MultiMoney(Money(amount=1),
                                   Money(amount=1, currency='USD'))
        assert piggy != Money(amount=1)
        assert piggy != 1
Exemplo n.º 12
0
 def _get_unavailable_balance(self):
     orders = self.get_open_orders()
     unavailable = MultiMoney()
     for o in orders:
         if o.side == 'ask':
             unavailable += o.amount
         else:
             unavailable += o.price * o.amount.amount
     return unavailable
Exemplo n.º 13
0
    def get_balance(self, btype='total'):
        try:
            data = self.bitfinex_request('/v1/balances').json()
        except ValueError as e:
            raise ExchangeError('bitfinex', '%s %s while sending to bitfinex get_open_orders' % (type(e), str(e)))
        if 'message' in data:
            raise ExchangeError(exchange='bitfinex', message=data['message'])
        relevant = filter(lambda x: x['currency'] in ('usd', 'btc'), data)

        if btype == 'total':
            total = MultiMoney(*map(lambda x: Money(x['amount'], x['currency'].upper()), relevant))
            return total
        elif btype == 'available':
            available = MultiMoney(*map(lambda x: Money(x['available'], x['currency'].upper()), relevant))
            return available
        else:
            total = MultiMoney(*map(lambda x: Money(x['amount'], x['currency'].upper()), relevant))
            available = MultiMoney(*map(lambda x: Money(x['available'], x['currency'].upper()), relevant))
            return total, available
Exemplo n.º 14
0
 def get_balance(self, btype='total'):
     data = self.lakebtc_request('getAccountInfo')
     balance = MultiMoney()
     for cur, amount in data['balance'].iteritems():
         balance += Money(data['balance'][cur], currency=cur)
     if btype == 'total':
         return balance
     available = balance - self._get_unavailable_balance()
     if btype == 'available':
         return available
     return balance, available
Exemplo n.º 15
0
 def get_balance_in_open_orders(self):
     bal = MultiMoney()
     try:
         olist = self.order_list()
     except ExchangeError as ee:
         if ee.error == "no orders":
             return bal
         else:
             raise ee
     for oNum in olist:
         if olist[oNum]['pair'] == 'btc_usd':
             if olist[oNum]['type'] == 'buy':
                 bal += (
                     Money(amount=olist[oNum]['amount'], currency='USD') *
                     Money(amount=olist[oNum]['rate'], currency='USD'))
             elif olist[oNum]['type'] == 'sell':
                 bal += Money(amount=Decimal(olist[oNum]['amount']))
     return bal
Exemplo n.º 16
0
class TestMultiMoney:
    def setup_method(self, method):
        self.one_million_decimal = Decimal('1000000')
        self.USD = CURRENCIES['USD']
        self.one_million_bucks = Money(amount=self.one_million_decimal,
                                       currency=self.USD)

        self.one_thousand_decimal = Decimal('1000')
        self.BTC = CURRENCIES['BTC']
        self.one_thousand_bitcoins = Money(amount=self.one_thousand_decimal,
                                           currency=self.BTC)

        self.one_mixed_fortune = MultiMoney(self.one_million_bucks,
                                            self.one_thousand_bitcoins)

    def test_init(self):
        assert self.one_mixed_fortune.getMoneys(
            'USD') == self.one_million_bucks
        assert self.one_mixed_fortune.getMoneys(
            'BTC') == self.one_thousand_bitcoins

    def test_init_empty(self):
        one_empty_piggy = MultiMoney()
        assert one_empty_piggy == 0
        assert one_empty_piggy == MultiMoney(Money())

    # def test_repr(self):
    #     assert repr(self.one_million_bucks) == '1000000 USD'
    #     assert repr(Money(Decimal('2.000'), 'PLN')) == '2 PLN'

    # def test_str(self):
    #     assert str(self.one_million_bucks) == 'US$1,000,000.00'

    def test_add(self):
        piggy = MultiMoney()
        piggy += MultiMoney(self.one_million_bucks)
        assert piggy == MultiMoney(self.one_million_bucks)

        piggy += self.one_million_bucks
        assert piggy == MultiMoney(self.one_million_bucks * 2)

        piggy += self.one_thousand_bitcoins
        assert piggy == MultiMoney(self.one_million_bucks * 2,
                                   self.one_thousand_bitcoins)

    def test_add_non_money(self):
        with pytest.raises(TypeError):
            MultiMoney() + 123

    def test_sub(self):
        piggy = copy.copy(self.one_mixed_fortune)
        piggy -= MultiMoney(self.one_million_bucks)
        assert piggy == MultiMoney(self.one_thousand_bitcoins)

        piggy -= self.one_thousand_bitcoins
        assert piggy == MultiMoney()

        piggy -= self.one_million_bucks
        assert piggy == MultiMoney(self.one_million_bucks * -1)

    def test_sub_non_money(self):
        with pytest.raises(TypeError):
            MultiMoney() - 123

    def test_mul(self):
        assert 3 * self.one_mixed_fortune == MultiMoney(
            self.one_million_bucks * 3, self.one_thousand_bitcoins * 3)

    def test_div(self):
        assert self.one_mixed_fortune / 3 == MultiMoney(
            self.one_million_bucks / 3, self.one_thousand_bitcoins / 3)

    def test_eq(self):
        piggy = MultiMoney()
        assert piggy == MultiMoney(Money(amount=0))
        assert piggy == Money(amount=0)
        assert piggy == 0

        piggy += Money(amount=1, currency='BTC')
        assert piggy == MultiMoney(Money(amount=1))
        assert piggy == Money(amount=1)
        assert piggy == 1

        piggy += Money(amount=1, currency='USD')
        assert piggy == MultiMoney(Money(amount=1),
                                   Money(amount=1, currency='USD'))
        assert piggy != Money(amount=1)
        assert piggy != 1

    def test_ne(self):
        piggy = MultiMoney()
        assert piggy != MultiMoney(Money(amount=1))
        assert piggy != Money(amount=1)
        assert piggy != 1

        piggy += Money(amount=1, currency='BTC')
        assert piggy != MultiMoney(Money(amount=2))
        assert piggy != Money(amount=2)
        assert piggy != 2

        piggy += Money(amount=1, currency='USD')
        assert piggy != MultiMoney(Money(amount=1),
                                   Money(amount=2, currency='USD'))
        assert piggy != Money(amount=1)
        assert piggy != 1

    def test_lt(self):
        one_buck_piggy = MultiMoney(Money(amount=1, currency=self.USD))
        assert one_buck_piggy < MultiMoney(self.one_million_bucks)
        assert one_buck_piggy < self.one_mixed_fortune
        one_buck_thousand_bitcoin_piggy = one_buck_piggy + self.one_thousand_bitcoins
        assert one_buck_thousand_bitcoin_piggy < self.one_mixed_fortune

        assert not MultiMoney(Money(currency='USD')) < MultiMoney()
        assert not self.one_mixed_fortune < MultiMoney()
        assert not self.one_mixed_fortune < one_buck_thousand_bitcoin_piggy
        assert not MultiMoney() < MultiMoney()
        assert not MultiMoney() < MultiMoney(Money())

    def test_gt(self):
        x = MultiMoney(Money(1, 'BTC'), Money(-1, 'USD'))
        y = MultiMoney(Money(0.5, 'BTC'))
        assert not x > y

        one_buck_piggy = MultiMoney(Money(amount=1, currency=self.USD))
        assert MultiMoney(self.one_million_bucks) > one_buck_piggy
        assert self.one_mixed_fortune > one_buck_piggy
        one_buck_thousand_bitcoin_piggy = one_buck_piggy + self.one_thousand_bitcoins
        assert self.one_mixed_fortune > one_buck_thousand_bitcoin_piggy

        assert not MultiMoney(Money(currency='USD')) > MultiMoney()
        assert not MultiMoney() > self.one_mixed_fortune
        assert not one_buck_thousand_bitcoin_piggy > self.one_mixed_fortune
        assert not MultiMoney() > MultiMoney()
        assert not MultiMoney() > MultiMoney(Money())
Exemplo n.º 17
0
 def test_init_empty(self):
     one_empty_piggy = MultiMoney()
     assert one_empty_piggy == 0
     assert one_empty_piggy == MultiMoney(Money())
Exemplo n.º 18
0
 def test_add_non_money(self):
     with pytest.raises(TypeError):
         MultiMoney() + 123
Exemplo n.º 19
0
 def get_total_balance(self):
     info = self.get_info()
     funds = info['funds']
     return (MultiMoney() + Money(amount=funds['btc']) +
             Money(amount=funds['usd'], currency='USD'))
Exemplo n.º 20
0
 def test_sub_non_money(self):
     with pytest.raises(TypeError):
         MultiMoney() - 123
Exemplo n.º 21
0
 def test_mul(self):
     assert 3 * self.one_mixed_fortune == MultiMoney(
         self.one_million_bucks * 3, self.one_thousand_bitcoins * 3)
Exemplo n.º 22
0
    def test_gt(self):
        x = MultiMoney(Money(1, 'BTC'), Money(-1, 'USD'))
        y = MultiMoney(Money(0.5, 'BTC'))
        assert not x > y

        one_buck_piggy = MultiMoney(Money(amount=1, currency=self.USD))
        assert MultiMoney(self.one_million_bucks) > one_buck_piggy
        assert self.one_mixed_fortune > one_buck_piggy
        one_buck_thousand_bitcoin_piggy = one_buck_piggy + self.one_thousand_bitcoins
        assert self.one_mixed_fortune > one_buck_thousand_bitcoin_piggy

        assert not MultiMoney(Money(currency='USD')) > MultiMoney()
        assert not MultiMoney() > self.one_mixed_fortune
        assert not one_buck_thousand_bitcoin_piggy > self.one_mixed_fortune
        assert not MultiMoney() > MultiMoney()
        assert not MultiMoney() > MultiMoney(Money())
Exemplo n.º 23
0
 def test_div(self):
     assert self.one_mixed_fortune / 3 == MultiMoney(
         self.one_million_bucks / 3, self.one_thousand_bitcoins / 3)