def test_instantiate_single_asset_cash_account(self): # Arrange event = AccountState( account_id=AccountId("SIM", "000"), account_type=AccountType.BETTING, base_currency=GBP, reported=True, balances=[ AccountBalance( GBP, Money(1_000_000, GBP), Money(0, GBP), Money(1_000_000, GBP), ), ], info={}, event_id=UUID4(), ts_event=0, ts_init=0, ) # Act account = BettingAccount(event) # Assert assert account.base_currency == GBP assert account.last_event == event assert account.events == [event] assert account.event_count == 1 assert account.balance_total() == Money(1_000_000, GBP) assert account.balance_free() == Money(1_000_000, GBP) assert account.balance_locked() == Money(0, GBP) assert account.balances_total() == {GBP: Money(1_000_000, GBP)} assert account.balances_free() == {GBP: Money(1_000_000, GBP)} assert account.balances_locked() == {GBP: Money(0, GBP)}
def test_apply_given_new_state_event_updates_correctly(self): # Arrange event1 = AccountState( account_id=AccountId("SIM", "001"), account_type=AccountType.BETTING, base_currency=None, # Multi-currency reported=True, balances=[ AccountBalance( GBP, Money(10.00000000, GBP), Money(0.00000000, GBP), Money(10.00000000, GBP), ), ], info={}, # No default currency set event_id=UUID4(), ts_event=0, ts_init=0, ) # Act account = BettingAccount(event1) event2 = AccountState( account_id=AccountId("SIM", "001"), account_type=AccountType.BETTING, base_currency=None, # Multi-currency reported=True, balances=[ AccountBalance( GBP, Money(9.00000000, GBP), Money(0.50000000, GBP), Money(8.50000000, GBP), ), ], info={}, # No default currency set event_id=UUID4(), ts_event=0, ts_init=0, ) # Act account.apply(event=event2) # Assert assert account.last_event == event2 assert account.events == [event1, event2] assert account.event_count == 2 assert account.balance_total(GBP) == Money(9.00000000, GBP) assert account.balance_free(GBP) == Money(8.50000000, GBP) assert account.balance_locked(GBP) == Money(0.50000000, GBP)
def test_calculate_pnls_for_single_currency_cash_account(self): # Arrange event = self._make_account_state(starting_balance=1000.0) account = BettingAccount(event) fill = self._make_fill(price="0.8", volume=100) position = Position(self.instrument, fill) # Act result = account.calculate_pnls( instrument=self.instrument, position=position, fill=fill, ) # Assert assert result == [Money("-80.00", GBP)]
def test_calculate_balance_locked(self, price, quantity, side, locked_balance): # Arrange event = self._make_account_state(starting_balance=1000.0) account = BettingAccount(event) # Act result = account.calculate_balance_locked( instrument=self.instrument, side=OrderSideParser.from_str_py(side), quantity=Quantity.from_int(quantity), price=Price.from_str(price), ) # Assert assert result == Money(Price.from_str(locked_balance), GBP)
def test_calculate_pnls_partially_closed(self): # Arrange event = self._make_account_state(starting_balance=1000.0) account = BettingAccount(event) fill1 = self._make_fill(price="0.50", volume=100, side="BUY") fill2 = self._make_fill(price="0.80", volume=100, side="SELL") position = Position(self.instrument, fill1) position.apply(fill2) # Act result = account.calculate_pnls( instrument=self.instrument, position=position, fill=fill2, ) # Assert # TODO - this should really be 75 GBP given the position (but we are currently not using position?) # assert result == [Money("75.00", GBP)] assert result == [Money("80.00", GBP)]