Skip to content

Commit

Permalink
fix(tests): remove value from account and holding. rename equity to n…
Browse files Browse the repository at this point in the history
…av, and redefine equity
  • Loading branch information
MetzkerLior committed Jul 1, 2024
1 parent 23b195d commit 2d3312a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
40 changes: 20 additions & 20 deletions tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -170,16 +170,16 @@ 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)

@staticmethod
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
Expand Down
13 changes: 8 additions & 5 deletions tests/test_holdings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import bktest

import datetime

class HoldingTest(unittest.TestCase):

Expand All @@ -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)
Expand Down

0 comments on commit 2d3312a

Please sign in to comment.