def test_locked_balance(): wallet = Wallet(exchange, 10000 * USD) wallet.deposit( quantity=Quantity(USD, 500, path_id=path_id), reason="test" ) wallet.deposit( quantity=Quantity(USD, 700, path_id=other_id), reason="test" ) assert wallet.locked_balance == 1200 * USD
def test_invalid_isub(): # Add to balance with locked path_id wallet = Wallet(exchange, 10000 * USD) wallet.deposit(quantity=Quantity(USD, 500, path_id=path_id), reason="test") wallet.deposit(quantity=Quantity(USD, 700, path_id=other_id), reason="test") with pytest.raises(InsufficientFunds): wallet.withdraw(quantity=11000 * USD, reason="test") with pytest.raises(InsufficientFunds): wallet.withdraw(quantity=Quantity(USD, 750, path_id), reason="test") with pytest.raises(InsufficientFunds): wallet.withdraw(quantity=Quantity(USD, 750, path_id), reason="test") with pytest.raises(IncompatibleInstrumentOperation): wallet.withdraw(quantity=500 * BTC, reason="test")
def test_valid_isub(): # Add to remove from unlocked balance wallet = Wallet(exchange, 10000 * USD) wallet.withdraw(quantity=500 * USD, reason="test") assert wallet.balance == 9500 * USD assert len(wallet.locked) == 0 # Add to balance with locked path_id wallet = Wallet(exchange, 10000 * USD) wallet.deposit(quantity=Quantity(USD, 750, path_id=path_id), reason="test") wallet.deposit(quantity=Quantity(USD, 1000, path_id=other_id), reason="test") wallet.withdraw(quantity=Quantity(USD, 500, path_id=path_id), reason="test") assert wallet.balance == 10000 * USD assert wallet.locked[path_id] == 250 * USD wallet.withdraw(quantity=Quantity(USD, 500, path_id=other_id), reason="test") assert wallet.balance == 10000 * USD assert wallet.locked[other_id] == 500 * USD
def test_exchange_with_wallets_feed(): ex1 = Exchange("bitfinex", service=execute_order)( Stream.source([7000, 7500, 8300], dtype="float").rename("USD-BTC"), Stream.source([200, 212, 400], dtype="float").rename("USD-ETH")) ex2 = Exchange("binance", service=execute_order)( Stream.source([7005, 7600, 8200], dtype="float").rename("USD-BTC"), Stream.source([201, 208, 402], dtype="float").rename("USD-ETH"), Stream.source([56, 52, 60], dtype="float").rename("USD-LTC")) wallet_btc = Wallet(ex1, 10 * BTC) wallet_btc_ds = _create_wallet_source(wallet_btc) wallet_usd = Wallet(ex2, 1000 * USD) wallet_usd.withdraw(quantity=400 * USD, reason="test") wallet_usd.deposit(quantity=Quantity(USD, 400, path_id="fake_id"), reason="test") wallet_usd_ds = _create_wallet_source(wallet_usd, include_worth=False) streams = ex1.streams() + ex2.streams() + wallet_btc_ds + wallet_usd_ds feed = DataFeed(streams) assert feed.next() == { "bitfinex:/USD-BTC": 7000, "bitfinex:/USD-ETH": 200, "bitfinex:/BTC:/free": 10, "bitfinex:/BTC:/locked": 0, "bitfinex:/BTC:/total": 10, "bitfinex:/BTC:/worth": 70000, "binance:/USD-BTC": 7005, "binance:/USD-ETH": 201, "binance:/USD-LTC": 56, "binance:/USD:/free": 600, "binance:/USD:/locked": 400, "binance:/USD:/total": 1000 }
def test_valid_deposit(): # Add to free unlocked balance wallet = Wallet(exchange, 10000 * USD) wallet.deposit(500 * USD, reason="test") assert wallet.balance == 10500 * USD assert len(wallet.locked) == 0 # Add to balance with locked path_id wallet = Wallet(exchange, 10000 * USD) wallet.deposit(Quantity(USD, 500, path_id=path_id), reason="test") assert wallet.balance == 10000 * USD assert wallet.locked[path_id] == 500 * USD # Add to more balance with locked path_id wallet.deposit(Quantity(USD, 500, path_id=path_id), reason="test") assert wallet.balance == 10000 * USD assert wallet.locked[path_id] == 1000 * USD # Add to balance that has another locked path_id wallet.deposit(Quantity(USD, 500, path_id=other_id), reason="test") assert wallet.balance == 10000 * USD assert wallet.locked[path_id] == 1000 * USD assert wallet.locked[other_id] == 500 * USD