From 2d3312a41ba9965df6db7809137b444fb6915936 Mon Sep 17 00:00:00 2001 From: MetzkerLior Date: Mon, 1 Jul 2024 20:10:06 +0300 Subject: [PATCH] fix(tests): remove value from account and holding. rename equity to nav, and redefine equity --- tests/test_account.py | 40 ++++++++++++++++++++-------------------- tests/test_holdings.py | 13 ++++++++----- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/tests/test_account.py b/tests/test_account.py index 44fa061..81ab7dc 100755 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -4,11 +4,11 @@ import bktest -invalid = bktest.Order(None, 1, 1, 1 * 1) -aapl = bktest.Order("AAPL", 42, 1, 42 * 1) -aapl_hold = bktest.Order("AAPL", 0, 1, 0 * 1) -aapl_short = bktest.Order("AAPL", -42, 1, - 42 * 1) -tsla = bktest.Order("TSLA", 15, 2, 15 * 2) +invalid = bktest.Order(None, 1, 1) +aapl = bktest.Order("AAPL", 42, 1) +aapl_hold = bktest.Order("AAPL", 0, 1) +aapl_short = bktest.Order("AAPL", -42, 1) +tsla = bktest.Order("TSLA", 15, 2) cash = 1000000 rfr = 4.17 today = datetime.date.today() @@ -27,25 +27,25 @@ def test_place_order(self): result = account.place_order(aapl, today) self.assertTrue(result.success) self.assertEqual(1, len(account.holdings)) - self.assertEqual(account.cash + aapl.value, account.equity) + self.assertEqual(account.cash + aapl.value, account.nav) result = account.place_order(aapl, today) self.assertTrue(result.success) self.assertEqual(1, len(account.holdings)) - self.assertEqual(account.cash + aapl.value * 2, account.equity) + self.assertEqual(account.cash + aapl.value * 2, account.nav) result = account.place_order(tsla, today) self.assertTrue(result.success) self.assertEqual(2, len(account.holdings)) - self.assertEqual(account.cash + tsla.value + aapl.value * 2, account.equity) + self.assertEqual(account.cash + tsla.value + aapl.value * 2, account.nav) result = account.place_order(aapl_short, today) self.assertEqual(2, len(account.holdings)) - self.assertEqual(account.cash + tsla.value + aapl.value, account.equity) + self.assertEqual(account.cash + tsla.value + aapl.value, account.nav) result = account.place_order(aapl_short, today) self.assertEqual(1, len(account.holdings)) - self.assertEqual(account.cash + tsla.value, account.equity) + self.assertEqual(account.cash + tsla.value, account.nav) def test_order_position(self): account = bktest.Account() @@ -96,7 +96,7 @@ def test_close_position(self): result = account.place_order(aapl, today) self.assertTrue(result.success) self.assertEqual(1, len(account.holdings)) - self.assertEqual(account.cash + aapl.value, account.equity) + self.assertEqual(account.cash + aapl.value, account.nav) result = account.close_position(tsla.symbol) self.assertTrue(result.missing) @@ -105,17 +105,17 @@ def test_close_position(self): result = account.close_position(aapl.symbol) self.assertTrue(result.success) self.assertEqual(0, len(account.holdings)) - self.assertEqual(account.initial_cash, account.equity) + self.assertEqual(account.initial_cash, account.nav) - def test_value(self): + def test_equity(self): account, aapl, tsla = AccountTest._create_dummy() - self.assertEqual(aapl.market_price + tsla.market_price, account.value) + self.assertEqual(aapl.market_price + tsla.market_price, account.equity) - def test_equity(self): + def test_nav(self): account, aapl, tsla = AccountTest._create_dummy() - self.assertEqual(account.initial_cash + aapl.market_price + tsla.market_price, account.equity) + self.assertEqual(account.initial_cash + aapl.market_price + tsla.market_price, account.nav) def test_symbols(self): account, aapl, tsla = AccountTest._create_dummy() @@ -154,7 +154,7 @@ def test_to_relative_order(self): relative = account.to_relative_order(aapl_short, today) self.assertEqual(aapl_short.quantity * 2, relative.quantity) - order = bktest.Order("AAPL", 44, 1, 44 * 1) + order = bktest.Order("AAPL", 44, 1) relative = account.to_relative_order(order, today) self.assertEqual(2, relative.quantity) @@ -170,7 +170,7 @@ def test_to_relative_order(self): relative = account.to_relative_order(aapl, today) self.assertEqual(aapl.quantity * 2, relative.quantity) - order = bktest.Order("AAPL", -44, 1, -44 * 1) + order = bktest.Order("AAPL", -44, 1) relative = account.to_relative_order(order, today) self.assertEqual(-2, relative.quantity) @@ -178,8 +178,8 @@ def test_to_relative_order(self): def _create_dummy(add=True) -> typing.Tuple[bktest.Account, bktest.Holding, bktest.Holding]: account = bktest.Account() - aapl = bktest.Holding("AAPL", 15, 2, 15 * 2) - tsla = bktest.Holding("TSLA", 30, 4, 30 * 4) + aapl = bktest.Holding("AAPL", 15, 2) + tsla = bktest.Holding("TSLA", 30, 4) for holding in [aapl, tsla]: account._holdings[holding.symbol] = holding diff --git a/tests/test_holdings.py b/tests/test_holdings.py index 719a9fc..5b8d9be 100644 --- a/tests/test_holdings.py +++ b/tests/test_holdings.py @@ -2,6 +2,7 @@ import bktest +import datetime class HoldingTest(unittest.TestCase): @@ -11,18 +12,20 @@ def test_market_price(self): self.assertEqual(15 * 2, holding.market_price) def test_merge(self): - holding = bktest.Holding("AAPL", 15, 2, 15 * 2, False) - order = bktest.Order("AAPL", 30, 4, 30 * 4) + today = datetime.date.today() + price = 2 + holding = bktest.Holding("AAPL", 15, price, today) + order = bktest.Order("AAPL", 30, price) expected_quantity = holding.quantity + order.quantity - expected_value = holding.value + order._value + expected_value = holding.market_price + order.price * order.quantity holding.merge(order) self.assertEqual(expected_quantity, holding.quantity) - self.assertEqual(expected_value, holding.value) + self.assertEqual(expected_value, holding.market_price) self.assertEqual(order.price, holding.price) - self.assertTrue(holding.up_to_date) + self.assertEqual(holding.last_date_updated, today) def test_str(self): holding = bktest.Holding("AAPL", 15, 2, None)