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 test_generate_order_fills_report(self): # Arrange report_provider = ReportProvider() order1 = self.order_factory.limit(AUDUSD_FXCM, OrderSide.BUY, Quantity(1500000), Price(0.80010, 5)) order2 = self.order_factory.limit(AUDUSD_FXCM, OrderSide.SELL, Quantity(1500000), Price(0.80000, 5)) event = OrderFilled(self.account_id, order1.id, ExecutionId('SOME_EXEC_ID_1'), PositionIdBroker('SOME_EXEC_TICKET_1'), order1.symbol, order1.side, order1.quantity, Price(0.80011, 5), Currency.AUD, UNIX_EPOCH, GUID(uuid.uuid4()), UNIX_EPOCH) order1.apply(event) orders = {order1.id: order1, order2.id: order2} # Act report = report_provider.generate_order_fills_report(orders) # Assert # print(report.iloc[0]) self.assertEqual(1, len(report)) self.assertEqual('order_id', report.index.name) self.assertEqual(order1.id.value, report.index[0]) self.assertEqual('AUDUSD', report.iloc[0]['symbol']) self.assertEqual('BUY', report.iloc[0]['side']) self.assertEqual('LIMIT', report.iloc[0]['type']) self.assertEqual(1500000, report.iloc[0]['quantity']) self.assertAlmostEqual(0.80011, report.iloc[0]['avg_price']) self.assertEqual(0.00001, report.iloc[0]['slippage'])
def test_generate_trades_report(self): # Arrange report_provider = ReportProvider() position1 = TestStubs.position_which_is_closed(PositionId("P-1")) position2 = TestStubs.position_which_is_closed(PositionId("P-2")) positions = [position1, position2] # Act report = report_provider.generate_positions_report(positions) # Assert self.assertEqual(2, len(report)) self.assertEqual("position_id", report.index.name) self.assertEqual(position1.id.value, report.index[0]) self.assertEqual("AUD/USD", report.iloc[0]["symbol"]) self.assertEqual("SELL", report.iloc[0]["entry"]) self.assertEqual(100000, report.iloc[0]["peak_quantity"]) self.assertEqual(1.0001, report.iloc[0]["avg_open_price"]) self.assertEqual(1.0001, report.iloc[0]["avg_close_price"]) self.assertEqual(UNIX_EPOCH + timedelta(minutes=5), report.iloc[0]["opened_time"]) self.assertEqual(UNIX_EPOCH + timedelta(minutes=5), report.iloc[0]["closed_time"]) self.assertEqual(0.0, report.iloc[0]["realized_points"]) self.assertEqual(0.0, report.iloc[0]["realized_return"])
def test_generate_trades_report(self): # Arrange report_provider = ReportProvider() position1 = TestStubs.position_which_is_closed(number=1) position2 = TestStubs.position_which_is_closed(number=2) positions = {position1.id: position1, position2.id: position2} # Act report = report_provider.generate_positions_report(positions) # Assert # print(report.iloc[0]) self.assertEqual(2, len(report)) self.assertEqual('position_id', report.index.name) self.assertEqual(position1.id.value, report.index[0]) self.assertEqual('AUDUSD', report.iloc[0]['symbol']) self.assertEqual('BUY', report.iloc[0]['direction']) self.assertEqual(100000, report.iloc[0]['peak_quantity']) self.assertEqual(1.00000, report.iloc[0]['avg_open_price']) self.assertEqual(1.0001, report.iloc[0]['avg_close_price']) self.assertEqual(UNIX_EPOCH, report.iloc[0]['opened_time']) self.assertEqual(UNIX_EPOCH + timedelta(minutes=5), report.iloc[0]['closed_time']) self.assertEqual(9.999999999998899e-05, report.iloc[0]['realized_points']) self.assertEqual(9.999999999998899e-05, report.iloc[0]['realized_return'])
def test_generate_orders_fills_report_with_no_order_returns_emtpy_dataframe(self): # Arrange report_provider = ReportProvider() # Act report = report_provider.generate_order_fills_report([]) # Assert self.assertTrue(report.empty)
def test_generate_positions_report(self): # Arrange report_provider = ReportProvider() order1 = self.order_factory.market( AUDUSD_SIM.symbol, OrderSide.BUY, Quantity(100000), ) order2 = self.order_factory.market( AUDUSD_SIM.symbol, OrderSide.SELL, Quantity(100000), ) fill1 = TestStubs.event_order_filled( order1, instrument=AUDUSD_SIM, position_id=PositionId("P-123456"), strategy_id=StrategyId("S", "001"), fill_price=Price("1.00010"), ) fill2 = TestStubs.event_order_filled( order2, instrument=AUDUSD_SIM, position_id=PositionId("P-123457"), strategy_id=StrategyId("S", "001"), fill_price=Price("1.00010"), ) position1 = Position(fill1) position1.apply(fill2) position2 = Position(fill1) position2.apply(fill2) positions = [position1, position2] # Act report = report_provider.generate_positions_report(positions) # Assert self.assertEqual(2, len(report)) self.assertEqual("position_id", report.index.name) self.assertEqual(position1.id.value, report.index[0]) self.assertEqual("AUD/USD", report.iloc[0]["symbol"]) self.assertEqual("BUY", report.iloc[0]["entry"]) self.assertEqual(100000, report.iloc[0]["peak_quantity"]) self.assertEqual(1.0001, report.iloc[0]["avg_open"]) self.assertEqual(1.0001, report.iloc[0]["avg_close"]) self.assertEqual(UNIX_EPOCH, report.iloc[0]["opened_time"]) self.assertEqual(UNIX_EPOCH, report.iloc[0]["closed_time"]) self.assertEqual(0, report.iloc[0]["realized_points"]) self.assertEqual(0, report.iloc[0]["realized_return"])
def test_generate_order_fills_report(self): # Arrange report_provider = ReportProvider() order1 = self.order_factory.limit( AUDUSD_SIM.symbol, OrderSide.BUY, Quantity(1500000), Price("0.80010"), ) order1.apply(TestStubs.event_order_submitted(order1)) order1.apply(TestStubs.event_order_accepted(order1)) order1.apply(TestStubs.event_order_working(order1)) order2 = self.order_factory.limit( AUDUSD_SIM.symbol, OrderSide.SELL, Quantity(1500000), Price("0.80000"), ) submitted2 = TestStubs.event_order_submitted(order2) accepted2 = TestStubs.event_order_accepted(order2) working2 = TestStubs.event_order_working(order2) order2.apply(submitted2) order2.apply(accepted2) order2.apply(working2) filled = TestStubs.event_order_filled( order1, instrument=AUDUSD_SIM, position_id=PositionId("P-1"), strategy_id=StrategyId("S", "1"), fill_price=Price("0.80011"), ) order1.apply(filled) orders = [order1, order2] # Act report = report_provider.generate_order_fills_report(orders) # Assert self.assertEqual(1, len(report)) self.assertEqual("cl_ord_id", report.index.name) self.assertEqual(order1.cl_ord_id.value, report.index[0]) self.assertEqual("AUD/USD", report.iloc[0]["symbol"]) self.assertEqual("BUY", report.iloc[0]["side"]) self.assertEqual("LIMIT", report.iloc[0]["type"]) self.assertEqual(1500000, report.iloc[0]["quantity"]) self.assertEqual(0.80011, report.iloc[0]["avg_price"]) self.assertEqual(0.00001, report.iloc[0]["slippage"])
def test_generate_orders_report(self): # Arrange report_provider = ReportProvider() order1 = self.order_factory.limit( AUDUSD_SIM.id, OrderSide.BUY, Quantity(1500000), Price("0.80010"), ) order1.apply(TestStubs.event_order_submitted(order1)) order1.apply(TestStubs.event_order_accepted(order1)) order2 = self.order_factory.limit( AUDUSD_SIM.id, OrderSide.SELL, Quantity(1500000), Price("0.80000"), ) order2.apply(TestStubs.event_order_submitted(order2)) order2.apply(TestStubs.event_order_accepted(order2)) event = TestStubs.event_order_filled( order1, instrument=AUDUSD_SIM, position_id=PositionId("P-1"), fill_price=Price("0.80011"), ) order1.apply(event) orders = [order1, order2] # Act report = report_provider.generate_orders_report(orders) # Assert self.assertEqual(2, len(report)) self.assertEqual("cl_ord_id", report.index.name) self.assertEqual(order1.cl_ord_id.value, report.index[0]) self.assertEqual("AUD/USD.SIM", report.iloc[0]["instrument_id"]) self.assertEqual("BUY", report.iloc[0]["side"]) self.assertEqual("LIMIT", report.iloc[0]["type"]) self.assertEqual(1500000, report.iloc[0]["quantity"]) self.assertEqual(0.80011, report.iloc[0]["avg_price"]) self.assertEqual(0.00001, report.iloc[0]["slippage"]) self.assertEqual("None", report.iloc[1]["avg_price"])
def test_generate_positions_report_with_no_positions_returns_emtpy_dataframe( self): # Arrange, Act report = ReportProvider.generate_positions_report([]) # Assert assert report.empty
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_event=0, ts_init=0, ) account = MarginAccount(state) # Act report = ReportProvider.generate_account_report(account) # Assert assert len(report) == 1
def test_generate_positions_report(self): # Arrange order1 = self.order_factory.market( AUDUSD_SIM.id, OrderSide.BUY, Quantity.from_int(100000), ) order2 = self.order_factory.market( AUDUSD_SIM.id, OrderSide.SELL, Quantity.from_int(100000), ) fill1 = TestStubs.event_order_filled( order1, instrument=AUDUSD_SIM, position_id=PositionId("P-123456"), strategy_id=StrategyId("S-001"), last_px=Price.from_str("1.00010"), ) fill2 = TestStubs.event_order_filled( order2, instrument=AUDUSD_SIM, position_id=PositionId("P-123457"), strategy_id=StrategyId("S-001"), last_px=Price.from_str("1.00010"), ) position1 = Position(instrument=AUDUSD_SIM, fill=fill1) position1.apply(fill2) position2 = Position(instrument=AUDUSD_SIM, fill=fill1) position2.apply(fill2) positions = [position1, position2] # Act report = ReportProvider.generate_positions_report(positions) # Assert assert len(report) == 2 assert report.index.name == "position_id" assert report.index[0] == position1.id.value assert report.iloc[0]["instrument_id"] == "AUD/USD.SIM" assert report.iloc[0]["entry"] == "BUY" assert report.iloc[0]["side"] == "FLAT" assert report.iloc[0]["peak_qty"] == "100000" assert report.iloc[0]["avg_px_open"] == "1.00010" assert report.iloc[0]["avg_px_close"] == "1.00010" assert report.iloc[0]["ts_opened"] == UNIX_EPOCH assert report.iloc[0]["ts_closed"] == UNIX_EPOCH assert report.iloc[0]["realized_points"] == "0.00000" assert report.iloc[0]["realized_return"] == "0.00000"
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_generate_order_fills_report(self): # Arrange order1 = self.order_factory.limit( AUDUSD_SIM.id, OrderSide.BUY, Quantity.from_int(1500000), Price.from_str("0.80010"), ) order1.apply(TestStubs.event_order_submitted(order1)) order1.apply(TestStubs.event_order_accepted(order1)) order2 = self.order_factory.limit( AUDUSD_SIM.id, OrderSide.SELL, Quantity.from_int(1500000), Price.from_str("0.80000"), ) order2.apply(TestStubs.event_order_submitted(order2)) order2.apply(TestStubs.event_order_accepted(order2)) filled = TestStubs.event_order_filled( order1, instrument=AUDUSD_SIM, position_id=PositionId("P-1"), strategy_id=StrategyId("S-1"), last_px=Price.from_str("0.80011"), ) order1.apply(filled) orders = [order1, order2] # Act report = ReportProvider.generate_order_fills_report(orders) # Assert assert len(report) == 1 assert report.index.name == "client_order_id" assert report.index[0] == order1.client_order_id.value assert report.iloc[0]["instrument_id"] == "AUD/USD.SIM" assert report.iloc[0]["side"] == "BUY" assert report.iloc[0]["type"] == "LIMIT" assert report.iloc[0]["quantity"] == "1500000" assert report.iloc[0]["avg_px"] == "0.80011" assert report.iloc[0]["slippage"] == "0.00001"