Compare commits
4 Commits
d635b6c4b9
...
3a96b50b68
Author | SHA1 | Date | |
---|---|---|---|
3a96b50b68 | |||
17d9a61bd4 | |||
aefece86d2 | |||
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):
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
__version__ = '0.1.34'
|
__version__ = '0.1.35'
|
||||||
|
|
|
@ -7,6 +7,7 @@ lint.ignore = [
|
||||||
'Q003', 'D205', # Format conflict
|
'Q003', 'D205', # Format conflict
|
||||||
|
|
||||||
'TCH', # Microoptimization at the cost of readability
|
'TCH', # Microoptimization at the cost of readability
|
||||||
|
'TID252', # I like relative imports
|
||||||
|
|
||||||
'D407', 'D413', # Weird documentation stuff
|
'D407', 'D413', # Weird documentation stuff
|
||||||
|
|
||||||
|
|
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'
|
|
@ -2,15 +2,18 @@ import pytest
|
||||||
|
|
||||||
import fin_defs
|
import fin_defs
|
||||||
|
|
||||||
BAD_TICKERS = ['TEST123', '123', 'TEST.EUR', 'TEST:EUR', 'EUR:TEST']
|
VALID_TICKERS = ['TEST123', '123', 'TEST.EUR']
|
||||||
|
BAD_TICKERS = ['TEST:EUR', 'EUR:TEST']
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('ticker', VALID_TICKERS)
|
||||||
|
def test_valid_tickers(ticker: str):
|
||||||
|
fin_defs.Stock(ticker,exchange=fin_defs.EXCHANGES_BY_IDS['NYSE'])
|
||||||
|
|
||||||
@pytest.mark.parametrize('ticker', BAD_TICKERS)
|
@pytest.mark.parametrize('ticker', BAD_TICKERS)
|
||||||
def test_bad_tickers(ticker):
|
def test_bad_tickers(ticker: str):
|
||||||
try:
|
with pytest.raises(ValueError):
|
||||||
fin_defs.Stock(ticker)
|
fin_defs.Stock(ticker,exchange=fin_defs.EXCHANGES_BY_IDS['NYSE'])
|
||||||
except Exception as e:
|
|
||||||
assert e
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('ticker', BAD_TICKERS)
|
@pytest.mark.parametrize('ticker', BAD_TICKERS)
|
||||||
|
@ -20,4 +23,4 @@ def test_crypto_tickers(ticker):
|
||||||
|
|
||||||
def test_str():
|
def test_str():
|
||||||
NVO = fin_defs.Stock('NVO', fin_defs.EXCHANGES_BY_IDS['NYSE'])
|
NVO = fin_defs.Stock('NVO', fin_defs.EXCHANGES_BY_IDS['NYSE'])
|
||||||
assert str(NVO) == 'NVO.XNYS'
|
assert str(NVO) == 'stock:NVO.XNYS'
|
||||||
|
|
|
@ -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