def get_last_available_price(self) -> PriceModel: """ Finds the last available price for security. Uses PriceDb. """ price_db = PriceDbApplication() symbol = SecuritySymbol(self.security.namespace, self.security.mnemonic) result = price_db.get_latest_price(symbol) return result
def get_latest_price(self, security: Commodity) -> PriceModel: """ Returns the latest available price for commodity """ assert isinstance(security, Commodity) symbol = SecuritySymbol(security.namespace, security.mnemonic) prices = PriceDbApplication() result = prices.get_latest_price(symbol) return result
def load_currency(self, mnemonic: str): """ load the latest rate for the given mnemonic; expressed in the base currency """ # , base_currency: str <= ignored for now. if self.rate and self.rate.currency == mnemonic: # Already loaded. return app = PriceDbApplication() # TODO use the base_currency parameter for the query #33 symbol = SecuritySymbol("CURRENCY", mnemonic) self.rate = app.get_latest_price(symbol) if not self.rate: raise ValueError(f"No rate found for {mnemonic}!")
def test_fetching_latest(session): """ Test fetching the latest out of two prices """ app = PriceDbApplication(session) symbol = SecuritySymbol("SOME", "SYMB") add_prices_for_yesterday_and_today(session, symbol) # Fetch the latest latest_price = app.get_latest_price(symbol) # Assert that it is for today assert latest_price is not None assert latest_price.value == Decimal("150.13") assert latest_price.symbol.mnemonic == symbol.mnemonic
def __load_latest_prices_from_pricedb(self, symbol: SecuritySymbol) -> PriceModel: """ Load security prices from PriceDb. Uses a separate price database that can be updated on (or from) Android. """ from pricedb import PriceDbApplication assert isinstance(symbol, SecuritySymbol) session = self.__get_pricedb_session() price_db = PriceDbApplication(session) latest_price = price_db.get_latest_price(symbol) return latest_price
def display(model: SecurityDetailsViewModel): """ Format and display the results """ # header #print(" security quantity ") #print("-------------------------------------------------------") print(f"{model.security.fullname}") #shares = agg.get_num_shares() print(f"{model.security.namespace}:{model.security.mnemonic}, shares: {model.quantity:,.2f}") # todo add all the info from the security details page in web ui, # prices, etc. # avg_price = agg.get_avg_price() #currency = agg.get_currency() currency = model.currency print(f"Average price: {model.average_price:.2f} {currency}") # last price prices_app = PriceDbApplication() sec_symbol = SecuritySymbol("", "") sec_symbol.parse(model.symbol) latest_price = prices_app.get_latest_price(sec_symbol) latest_price_date = latest_price.datum.to_iso_date_string() logging.debug(latest_price) print(f"Latest price: {latest_price.value:.2f} {latest_price.currency} on {latest_price_date}") print("") # Income print("Income") print(f"Total: {model.income:,.2f} {model.currency}, {model.income_perc:.2f}%") print(f"Last 12m: {model.income_last_12m:,.2f} {model.currency}, {model.income_perc_last_12m:.2f}%") # Return of Capital. if model.return_of_capital: print(f"Return of capital: {model.return_of_capital:,.2f}") print("") print("Holding Accounts:") print("-----------------") for account in model.accounts: balance = account.get_balance() if balance == 0: # Skip empty accounts continue value = balance * latest_price.value print(f"{account.fullname}, {balance:,.2f} units, {value:,.2f} {latest_price.currency}")
def last(symbol: str): """ displays last price, for symbol if provided """ app = PriceDbApplication() # convert to uppercase if symbol: symbol = symbol.upper() # extract namespace sec_symbol = SecuritySymbol("", "") sec_symbol.parse(symbol) latest = app.get_latest_price(sec_symbol) assert isinstance(latest, PriceModel) print(f"{latest}") else: # Show the latest prices available for all securities. latest = app.get_latest_prices() for price in latest: print(f"{price}")
def test_latest_date(session): """ Test fetching the latest price. The date is always today, even if the latest price is not from today! """ # Preparation add_price_for_yesterday(session) # Fetch the latest price for xy app = PriceDbApplication(session=session) symbol = SecuritySymbol("VANGUARD", "BOND") latest_price = app.get_latest_price(symbol) assert latest_price yesterday = Datum() yesterday.yesterday() yesterday.start_of_day() yesterday_str = yesterday.to_iso_date_string() assert latest_price.datum.to_iso_date_string() == yesterday_str