def test_instantiated_accounts_basic_properties(self): # Arrange event = AccountState( account_id=AccountId("SIM", "001"), account_type=AccountType.CASH, base_currency=USD, reported=True, balances=[ AccountBalance( USD, Money(1_000_000, USD), Money(0, USD), Money(1_000_000, USD), ), ], info={}, event_id=uuid4(), ts_updated_ns=0, timestamp_ns=0, ) # Act account = Account(event) # Prepare components account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Assert self.assertEqual(AccountId("SIM", "001"), account.id) self.assertEqual("Account(id=SIM-001)", str(account)) self.assertEqual("Account(id=SIM-001)", repr(account)) self.assertEqual(int, type(hash(account))) self.assertTrue(account == account) self.assertFalse(account != account)
def test_unrealized_pnl_with_single_asset_account_when_no_open_positions_returns_zero( self, ): # Arrange event = AccountState( account_id=AccountId("SIM", "001"), account_type=AccountType.CASH, base_currency=USD, reported=True, balances=[ AccountBalance( USD, Money(1_000_000, USD), Money(0, USD), Money(1_000_000, USD), ), ], info={}, event_id=uuid4(), ts_updated_ns=0, timestamp_ns=0, ) account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Act result = account.unrealized_pnl() # Assert self.assertEqual(Money(0, USD), result)
def test_equity_with_single_asset_account_returns_expected_money(self): # Arrange event = AccountState( account_id=AccountId("SIM", "001"), account_type=AccountType.CASH, base_currency=USD, reported=True, balances=[ AccountBalance( USD, Money(100000.00, USD), Money(0.00, USD), Money(100000.00, USD), ), ], info={}, event_id=uuid4(), ts_updated_ns=0, timestamp_ns=0, ) account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Act result = account.equity() # Assert self.assertEqual(Money(100000.00, USD), result)
def test_instantiated_accounts_basic_properties(self): # Arrange event = AccountState( AccountId("SIM", "001"), [Money(1_000_000, USD)], [Money(1_000_000, USD)], [Money(0, USD)], info={"default_currency": "USD"}, # Set the default currency event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) # Act account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Assert self.assertEqual(AccountId("SIM", "001"), account.id) self.assertEqual("Account(id=SIM-001)", str(account)) self.assertEqual("Account(id=SIM-001)", repr(account)) self.assertEqual(int, type(hash(account))) self.assertTrue(account == account) self.assertFalse(account != account)
def test_equity_with_multi_asset_account_returns_expected_money(self): # Arrange event = AccountState( AccountId("SIM", "001"), [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("0.00000000", BTC), Money("0.00000000", ETH)], info={}, # No default currency set event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Act result = account.equity(BTC) # Assert self.assertEqual(Money("10.00000000", BTC), result)
def test_unrealized_pnl_with_multi_asset_account_when_no_open_positions_returns_zero( self): # Arrange event = AccountState( AccountId("SIM", "001"), [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("0.00000000", BTC), Money("0.00000000", ETH)], info={}, # No default currency set event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Act result = account.unrealized_pnl(BTC) # Assert self.assertEqual(result, Money("0.00000000", BTC))
def test_update_maint_margin(self): # Arrange event = AccountState( AccountId("SIM", "001"), [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("0.00000000", BTC), Money("0.00000000", ETH)], info={}, # No default currency set event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) # Act account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) margin = Money("0.00050000", BTC) # Act account.update_maint_margin(margin) # Assert self.assertEqual(margin, account.maint_margin(BTC)) self.assertEqual({BTC: margin}, account.maint_margins())
def setUp(self): # Fixture Setup clock = TestClock() logger = TestLogger(clock) self.order_factory = OrderFactory( trader_id=TraderId("TESTER", "000"), strategy_id=StrategyId("S", "001"), clock=TestClock(), ) state = AccountState( account_id=AccountId("BINANCE", "1513111"), balances=[Money("10.00000000", BTC)], balances_free=[Money("0.00000000", BTC)], balances_locked=[Money("0.00000000", BTC)], info={}, event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) self.data_cache = DataCache(logger) self.account = Account(state) self.portfolio = Portfolio(clock, logger) self.portfolio.register_account(self.account) self.portfolio.register_cache(self.data_cache) self.data_cache.add_instrument(AUDUSD_SIM) self.data_cache.add_instrument(GBPUSD_SIM) self.data_cache.add_instrument(BTCUSDT_BINANCE) self.data_cache.add_instrument(BTCUSD_BITMEX) self.data_cache.add_instrument(ETHUSD_BITMEX)
def test_generate_accounts_report_with_initial_account_state_returns_expected(self): # Arrange state = AccountState( account_id=AccountId("BITMEX", "1513111"), account_type=AccountType.MARGIN, base_currency=BTC, reported=True, balances=[ AccountBalance( currency=BTC, total=Money(10.00000000, BTC), free=Money(10.00000000, BTC), locked=Money(0.00000000, BTC), ) ], info={}, event_id=uuid4(), ts_updated_ns=0, timestamp_ns=0, ) account = Account(state) report_provider = ReportProvider() # Act report = report_provider.generate_account_report(account) # Assert self.assertEqual(1, len(report))
def setUp(self): # Fixture setup state = AccountState(AccountId.from_string("BITMEX-1513111-SIMULATED"), BTC, Money(10., BTC), Money(0., BTC), Money(0., BTC), uuid4(), UNIX_EPOCH) self.account = Account(state)
def setUp(self): # Fixture Setup self.clock = TestClock() uuid_factor = TestUUIDFactory() logger = TestLogger(self.clock) self.order_factory = OrderFactory( strategy_id=StrategyId("S", "001"), id_tag_trader=IdTag("001"), id_tag_strategy=IdTag("001"), clock=TestClock(), ) state = AccountState( AccountId.from_string("BITMEX-1513111-SIMULATED"), BTC, Money(10., BTC), Money(0., BTC), Money(0., BTC), uuid4(), UNIX_EPOCH ) self.account = Account(state) self.portfolio = Portfolio(self.clock, uuid_factor, logger) self.portfolio.register_account(self.account)
def test_market_value_when_insufficient_data_for_xrate_returns_none(self): # Arrange state = AccountState( account_id=AccountId("BITMEX", "01234"), balances=[Money("10.00000000", BTC), Money("10.00000000", ETH)], balances_free=[ Money("10.00000000", BTC), Money("10.00000000", ETH) ], balances_locked=[ Money("0.00000000", BTC), Money("0.00000000", ETH) ], info={}, event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) account = Account(state) self.portfolio.register_account(account) order = self.order_factory.market( ETHUSD_BITMEX.symbol, OrderSide.BUY, Quantity(100), ) fill = TestStubs.event_order_filled( order=order, instrument=ETHUSD_BITMEX, position_id=PositionId("P-123456"), strategy_id=StrategyId("S", "001"), fill_price=Price("376.05"), ) last_ethusd = QuoteTick( ETHUSD_BITMEX.symbol, Price("376.05"), Price("377.10"), Quantity("16"), Quantity("25"), UNIX_EPOCH, ) position = Position(fill) self.portfolio.update_position( TestStubs.event_position_opened(position)) self.data_cache.add_quote_tick(last_ethusd) self.portfolio.update_tick(last_ethusd) # Act result = self.portfolio.market_values(BITMEX) # Assert # TODO: Currently no Quanto thus no xrate required self.assertEqual({ETH: Money('0.02659221', ETH)}, result)
def test_load_accounts_cache_when_one_account_in_database(self): # Arrange event = TestStubs.event_account_state() account = Account(event) self.database.add_account(account) # Act # Assert assert self.database.load_accounts() == {account.id: account}
def test_add_account(self): # Arrange initial = TestStubs.event_account_state() account = Account(initial) # Act self.cache.add_account(account) # Assert self.assertEqual(account, self.cache.load_account(account.id))
def test_add_account(self): # Arrange event = TestStubs.event_account_state() account = Account(event) # Act self.database.add_account(account) # Assert assert self.database.load_account(account.id) == account
def test_load_account_when_no_account_in_database_returns_none(self): # Arrange event = TestStubs.event_account_state() account = Account(event) # Act result = self.database.load_account(account.id) # Assert assert result is None
def test_update_account(self): # Arrange event = TestStubs.event_account_state() account = Account(event) self.database.add_account(account) # Act self.database.update_account(account) # Assert self.assertEqual(account, self.database.load_account(account.id))
def test_load_account_when_account_in_database_returns_account(self): # Arrange event = TestStubs.event_account_state() account = Account(event) self.database.add_account(account) # Act result = self.database.load_account(account.id) # Assert self.assertEqual(account, result)
def test_update_account(self): # Arrange event = TestStubs.event_account_state() account = Account(event) self.cache.add_account(account) # Act self.cache.update_account(account) # Assert self.assertTrue(True) # No exceptions raised
def test_unrealized_pnl_with_multi_asset_account_when_no_open_positions_returns_zero( self, ): # Arrange event = AccountState( account_id=AccountId("SIM", "001"), account_type=AccountType.CASH, base_currency=None, # Multi-currency reported=True, balances=[ AccountBalance( BTC, Money(10.00000000, BTC), Money(0.00000000, BTC), Money(10.00000000, BTC), ), AccountBalance( ETH, Money(20.00000000, ETH), Money(0.00000000, ETH), Money(20.00000000, ETH), ), ], info={}, # No default currency set event_id=uuid4(), updated_ns=0, timestamp_ns=0, ) account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Act result = account.unrealized_pnl(BTC) # Assert self.assertEqual(Money(0.00000000, BTC), result)
def test_unrealized_pnl_with_single_asset_account_when_no_open_positions_returns_zero(self): # Arrange event = AccountState( AccountId("SIM", "001"), balances=[Money(1_000_000, USD)], balances_free=[Money(1_000_000, USD)], balances_locked=[Money(0, USD)], info={"default_currency": "USD"}, # No default currency set event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Act result = account.unrealized_pnl() # Assert self.assertEqual(Money(0, USD), result)
def test_equity_with_single_asset_account_no_default_returns_none(self): # Arrange event = AccountState( AccountId("SIM", "001"), [Money("100000.00", USD)], [Money("0.00", USD)], [Money("0.00", USD)], info={}, # No default currency set event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Act result = account.equity(BTC) # Assert self.assertIsNone(result)
def test_equity_with_single_asset_account_returns_expected_money(self): # Arrange event = AccountState( AccountId("SIM", "001"), [Money("100000.00", USD)], [Money("0.00", USD)], [Money("0.00", USD)], info={"default_currency": "USD"}, # No default currency set event_id=uuid4(), timestamp_ns=0, ) account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Act result = account.equity() # Assert self.assertEqual(Money("100000.00", USD), result)
def test_unrealized_pnl_when_insufficient_data_for_xrate_returns_none( self): # Arrange state = AccountState( account_id=AccountId("BITMEX", "01234"), balances=[Money("10.00000000", BTC), Money("10.00000000", ETH)], balances_free=[ Money("10.00000000", BTC), Money("10.00000000", ETH) ], balances_locked=[ Money("0.00000000", BTC), Money("0.00000000", ETH) ], info={}, event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) account = Account(state) self.portfolio.register_account(account) order = self.order_factory.market( ETHUSD_BITMEX.symbol, OrderSide.BUY, Quantity(100), ) fill = TestStubs.event_order_filled( order=order, instrument=ETHUSD_BITMEX, position_id=PositionId("P-123456"), strategy_id=StrategyId("S", "001"), fill_price=Price("376.05"), ) position = Position(fill) self.portfolio.update_position( TestStubs.event_position_opened(position)) # Act result = self.portfolio.unrealized_pnls(BITMEX) # # Assert self.assertIsNone(result)
async def execution_client(betfair_client, account_id, exec_engine, clock, live_logger, betfair_account_state) -> BetfairExecutionClient: client = BetfairExecutionClient( client=betfair_client, account_id=account_id, base_currency=AUD, engine=exec_engine, clock=clock, logger=live_logger, market_filter={}, load_instruments=False, ) client.instrument_provider().load_all() exec_engine.register_client(client) exec_engine.cache.add_account(account=Account(betfair_account_state)) return client
def test_update_maint_margin(self): # Arrange event = AccountState( account_id=AccountId("SIM", "001"), account_type=AccountType.CASH, base_currency=None, # Multi-currency reported=True, balances=[ AccountBalance( BTC, Money(10.00000000, BTC), Money(0.00000000, BTC), Money(10.00000000, BTC), ), AccountBalance( ETH, Money(20.00000000, ETH), Money(0.00000000, ETH), Money(20.00000000, ETH), ), ], info={}, # No default currency set event_id=uuid4(), ts_updated_ns=0, timestamp_ns=0, ) # Act account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) margin = Money(0.00050000, BTC) # Act account.update_maint_margin(margin) # Assert self.assertEqual(margin, account.maint_margin(BTC)) self.assertEqual({BTC: margin}, account.maint_margins())
def test_generate_accounts_report_with_initial_account_state_returns_expected( self): # Arrange state = AccountState( account_id=AccountId("BITMEX", "1513111"), balances=[Money("10.00000000", BTC)], balances_free=[Money("10.00000000", BTC)], balances_locked=[Money("0.00000000", BTC)], info={}, event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) account = Account(state) report_provider = ReportProvider() # Act report = report_provider.generate_account_report(account) # Assert self.assertEqual(1, len(report))
def test_instantiate_multi_asset_account(self): # Arrange event = AccountState( AccountId("SIM", "001"), [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("0.00000000", BTC), Money("0.00000000", ETH)], info={}, # No default currency set event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) # Act account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Assert self.assertEqual(AccountId("SIM", "001"), account.id) self.assertEqual(None, account.default_currency) self.assertEqual(event, account.last_event) self.assertEqual([event], account.events) self.assertEqual(1, account.event_count) self.assertEqual(Money("10.00000000", BTC), account.balance(BTC)) self.assertEqual(Money("20.00000000", ETH), account.balance(ETH)) self.assertEqual(Money("10.00000000", BTC), account.balance_free(BTC)) self.assertEqual(Money("20.00000000", ETH), account.balance_free(ETH)) self.assertEqual(Money("0.00000000", BTC), account.balance_locked(BTC)) self.assertEqual(Money("0.00000000", ETH), account.balance_locked(ETH)) self.assertEqual( { BTC: Money("10.00000000", BTC), ETH: Money("20.00000000", ETH) }, account.balances()) self.assertEqual( { BTC: Money("10.00000000", BTC), ETH: Money("20.00000000", ETH) }, account.balances_free()) self.assertEqual( { BTC: Money("0.00000000", BTC), ETH: Money("0.00000000", ETH) }, account.balances_locked()) self.assertEqual(Money("0.00000000", BTC), account.unrealized_pnl(BTC)) self.assertEqual(Money("0.00000000", ETH), account.unrealized_pnl(ETH)) self.assertEqual(Money("10.00000000", BTC), account.equity(BTC)) self.assertEqual(Money("20.00000000", ETH), account.equity(ETH)) self.assertEqual({}, account.init_margins()) self.assertEqual({}, account.maint_margins()) self.assertEqual(None, account.init_margin(BTC)) self.assertEqual(None, account.init_margin(ETH)) self.assertEqual(None, account.maint_margin(BTC)) self.assertEqual(None, account.maint_margin(ETH))
def test_instantiate_single_asset_account(self): # Arrange event = AccountState( AccountId("SIM", "001"), [Money(1_000_000, USD)], [Money(1_000_000, USD)], [Money(0, USD)], info={"default_currency": "USD"}, # Set the default currency event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) # Act account = Account(event) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) # Assert self.assertEqual(AccountId("SIM", "001"), account.id) self.assertEqual(USD, account.default_currency) self.assertEqual(event, account.last_event) self.assertEqual([event], account.events) self.assertEqual(1, account.event_count) self.assertEqual(Money(1_000_000, USD), account.balance()) self.assertEqual(Money(1_000_000, USD), account.balance_free()) self.assertEqual(Money(0, USD), account.balance_locked()) self.assertEqual({USD: Money(1_000_000, USD)}, account.balances()) self.assertEqual({USD: Money(1_000_000, USD)}, account.balances_free()) self.assertEqual({USD: Money(0, USD)}, account.balances_locked()) self.assertEqual(Money(0, USD), account.unrealized_pnl()) self.assertEqual(Money(1_000_000, USD), account.equity()) self.assertEqual({}, account.init_margins()) self.assertEqual({}, account.maint_margins()) self.assertEqual(None, account.init_margin()) self.assertEqual(None, account.maint_margin())
def test_apply_given_new_state_event_updates_correctly(self): # Arrange event1 = AccountState( AccountId("SIM", "001"), [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("10.00000000", BTC), Money("20.00000000", ETH)], [Money("0.00000000", BTC), Money("0.00000000", ETH)], info={}, # No default currency set event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) # Act account = Account(event1) # Wire up account to portfolio account.register_portfolio(self.portfolio) self.portfolio.register_account(account) event2 = AccountState( AccountId("SIM", "001"), [Money("9.00000000", BTC), Money("20.00000000", ETH)], [Money("8.50000000", BTC), Money("20.00000000", ETH)], [Money("0.50000000", BTC), Money("0.00000000", ETH)], info={}, # No default currency set event_id=uuid4(), event_timestamp=UNIX_EPOCH, ) # Act account.apply(event2) # Assert self.assertEqual(event2, account.last_event) self.assertEqual([event1, event2], account.events) self.assertEqual(2, account.event_count) self.assertEqual(Money("9.00000000", BTC), account.balance(BTC)) self.assertEqual(Money("8.50000000", BTC), account.balance_free(BTC)) self.assertEqual(Money("0.50000000", BTC), account.balance_locked(BTC)) self.assertEqual(Money("20.00000000", ETH), account.balance(ETH)) self.assertEqual(Money("20.00000000", ETH), account.balance_free(ETH)) self.assertEqual(Money("0.00000000", ETH), account.balance_locked(ETH))