Amount prefix
This commit is contained in:
parent
d635b6c4b9
commit
74895720d6
|
@ -258,6 +258,7 @@ Commodity.COFFEE = Commodity('COFFEE')
|
||||||
DKK = FiatCurrency('DKK')
|
DKK = FiatCurrency('DKK')
|
||||||
USD = FiatCurrency('USD')
|
USD = FiatCurrency('USD')
|
||||||
EUR = FiatCurrency('EUR')
|
EUR = FiatCurrency('EUR')
|
||||||
|
GBP = FiatCurrency('GBP')
|
||||||
BTC = CryptoCurrency('BTC', coingecko_id='bitcoin')
|
BTC = CryptoCurrency('BTC', coingecko_id='bitcoin')
|
||||||
MPC = CryptoCurrency('MPC', coingecko_id='partisia-blockchain')
|
MPC = CryptoCurrency('MPC', coingecko_id='partisia-blockchain')
|
||||||
SPX = Index('SPX')
|
SPX = Index('SPX')
|
||||||
|
@ -316,6 +317,13 @@ WELL_KNOWN_SYMBOLS = (
|
||||||
| {'SPX500': SPX, 'SP500': SPX, 'Nasdaq 100': NDX}
|
| {'SPX500': SPX, 'SP500': SPX, 'Nasdaq 100': NDX}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ASSET_PREFIX: dict[Asset, str] = {
|
||||||
|
USD: '$',
|
||||||
|
EUR: '€',
|
||||||
|
GBP: '£',
|
||||||
|
BTC: '₿',
|
||||||
|
}
|
||||||
|
|
||||||
NYSE = StockExchange(
|
NYSE = StockExchange(
|
||||||
name='New York Stock Exchange',
|
name='New York Stock Exchange',
|
||||||
mic='XNYS',
|
mic='XNYS',
|
||||||
|
@ -430,8 +438,14 @@ class AssetAmount:
|
||||||
amount: Decimal
|
amount: Decimal
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
return self.human_readable_str()
|
||||||
|
|
||||||
|
def human_readable_str(self):
|
||||||
specificity = '2' if self.amount >= 0.10 else '3'
|
specificity = '2' if self.amount >= 0.10 else '3'
|
||||||
return ('{:.' + specificity + 'f} {}').format(self.amount, self.asset)
|
prefix = ASSET_PREFIX.get(self.asset, '')
|
||||||
|
return ('{}{:.' + specificity + 'f} {}').format(
|
||||||
|
prefix, self.amount, self.asset.raw_short_name()
|
||||||
|
)
|
||||||
|
|
||||||
def __mul__(self, other: Decimal):
|
def __mul__(self, other: Decimal):
|
||||||
if not isinstance(other, Decimal):
|
if not isinstance(other, Decimal):
|
||||||
|
|
8
test/test_asset_amount.py
Normal file
8
test/test_asset_amount.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
import fin_defs
|
||||||
|
|
||||||
|
|
||||||
|
def test_str():
|
||||||
|
amount = fin_defs.AssetAmount(fin_defs.USD, Decimal(10))
|
||||||
|
assert str(amount) == '$10.00 USD'
|
|
@ -7,10 +7,8 @@ BAD_TICKERS = ['TEST123', '123', 'TEST.EUR', 'TEST:EUR', 'EUR:TEST']
|
||||||
|
|
||||||
@pytest.mark.parametrize('ticker', BAD_TICKERS)
|
@pytest.mark.parametrize('ticker', BAD_TICKERS)
|
||||||
def test_bad_tickers(ticker):
|
def test_bad_tickers(ticker):
|
||||||
try:
|
with pytest.raises(Exception) as e:
|
||||||
fin_defs.Stock(ticker)
|
fin_defs.Stock(ticker)
|
||||||
except Exception as e:
|
|
||||||
assert e
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('ticker', BAD_TICKERS)
|
@pytest.mark.parametrize('ticker', BAD_TICKERS)
|
||||||
|
|
|
@ -8,9 +8,6 @@ def test_to_from_string_id(asset: fin_defs.Asset):
|
||||||
assert fin_defs.Asset.from_string_id(asset.to_string_id()) == asset
|
assert fin_defs.Asset.from_string_id(asset.to_string_id()) == asset
|
||||||
|
|
||||||
|
|
||||||
import fin_defs
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
|
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
|
||||||
def test_to_from_polygon_id(asset: fin_defs.Asset):
|
def test_to_from_polygon_id(asset: fin_defs.Asset):
|
||||||
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset
|
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset
|
||||||
|
|
Loading…
Reference in New Issue
Block a user