def get_net_worth(self, epoch_s: float, register: SymbolDayRegister) -> float: """ Provide instantaneous net worth of trader. The net worth is the sum of cash held as well as the value of all shares held at the close price of the moment. """ net_worth = self.cash for stock, n_shares in self.holdings.items(): moment = register.get(stock, epoch_s) if moment is not None: net_worth += moment.close * n_shares return net_worth
class GetFromRegisterTestCase(unittest.TestCase): def setUp(self): self.symbol_day_register = SymbolDayRegister() def test_get(self): moments = list() for symbol in ('AAPL', 'BYND'): _chunk = [Moment.from_dict(x) for x in STOCKS_DAILY[symbol]['records']] for moment in _chunk: moment.symbol = symbol moments += _chunk self.symbol_day_register.add_moments(moments) for symbol in ('AAPL', 'BYND'): for _ in range(3): i = random.randint(0, len(STOCKS_DAILY[symbol]['records']) - 1) moment = Moment.from_dict(STOCKS_DAILY[symbol]['records'][i]) moment.symbol = symbol self.assertEqual(moment, self.symbol_day_register.get(symbol, moment.epoch_s))