Nordnet supports basic stocks now. (Works for me!)
All checks were successful
Test Python / Test (push) Successful in 20s
All checks were successful
Test Python / Test (push) Successful in 20s
Index funds are still WIP
This commit is contained in:
parent
515475233a
commit
1870de8109
|
@ -15,7 +15,7 @@ from decimal import Decimal
|
||||||
|
|
||||||
import fin_defs
|
import fin_defs
|
||||||
|
|
||||||
from .data import Depo, DepoSingle
|
from .data import Depo, DepoSingle, DepoGroup
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -31,6 +31,13 @@ API_ACCOUNT_LEDGERS = API_ROOT + '/accounts/{accid}/ledgers'
|
||||||
API_ACCOUNT_POSITIONS = API_ROOT + '/accounts/{accid}/positions'
|
API_ACCOUNT_POSITIONS = API_ROOT + '/accounts/{accid}/positions'
|
||||||
|
|
||||||
|
|
||||||
|
def asset_from_instrument_json(json) -> fin_defs.Asset | None:
|
||||||
|
if json['instrument_group_type'] == 'FND':
|
||||||
|
return None
|
||||||
|
symbol = json['symbol']
|
||||||
|
exchange_id = json['tradables'][0]['mic']
|
||||||
|
return fin_defs.Stock(symbol, fin_defs.EXCHANGES_BY_IDS[exchange_id])
|
||||||
|
|
||||||
class NordnetDepoFetcher:
|
class NordnetDepoFetcher:
|
||||||
def __init__(self, session, username: str, password: str):
|
def __init__(self, session, username: str, password: str):
|
||||||
assert session is not None, 'missing session'
|
assert session is not None, 'missing session'
|
||||||
|
@ -39,7 +46,7 @@ class NordnetDepoFetcher:
|
||||||
self.password = password
|
self.password = password
|
||||||
self.is_logged_in = False
|
self.is_logged_in = False
|
||||||
|
|
||||||
def get_json(self, url: str, params: dict[str, str | int] = {}) -> object:
|
def get_json(self, url: str, params: dict[str, str | int] = {}) -> dict:
|
||||||
if not url.startswith(API_ROOT):
|
if not url.startswith(API_ROOT):
|
||||||
msg = f'Given url must be located below API ROOT: {url}'
|
msg = f'Given url must be located below API ROOT: {url}'
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
|
@ -77,26 +84,42 @@ class NordnetDepoFetcher:
|
||||||
|
|
||||||
def get_depo(self) -> Depo:
|
def get_depo(self) -> Depo:
|
||||||
self.login()
|
self.login()
|
||||||
|
now = datetime.datetime.now(tz=datetime.UTC) # TODO: Use info from json requests
|
||||||
|
|
||||||
accounts = self.get_json(API_ACCOUNTS)
|
json_accounts = self.get_json(API_ACCOUNTS)
|
||||||
print(accounts)
|
|
||||||
|
|
||||||
|
nested = []
|
||||||
|
for json_account in json_accounts:
|
||||||
|
account_id = json_account['accid']
|
||||||
|
|
||||||
|
assets: dict[fin_defs.Asset, Decimal] = {
|
||||||
|
}
|
||||||
|
|
||||||
# Old
|
# Determine amount of usable currency stored on Nordnet
|
||||||
now = datetime.datetime.now(tz=datetime.UTC)
|
if True:
|
||||||
result = self.session.query_private('Balance')
|
json_account_info = self.get_json(API_ACCOUNT_INFO.format(accid = account_id))
|
||||||
assets: dict[fin_defs.Asset, Decimal] = {}
|
asset = fin_defs.FiatCurrency(json_account_info[0]['account_sum']['currency'])
|
||||||
for account, balance_str in result['result'].items():
|
amount = Decimal(json_account_info[0]['account_sum']['value'])
|
||||||
account = account.removesuffix('.HOLD')
|
assets[asset] = amount
|
||||||
asset = fin_defs.WELL_KNOWN_SYMBOLS[account]
|
del asset, amount
|
||||||
balance = Decimal(balance_str)
|
|
||||||
if balance != 0:
|
|
||||||
assets[asset] = assets.get(asset, Decimal(0)) + balance
|
|
||||||
del account, balance_str, asset, balance
|
|
||||||
|
|
||||||
return DepoSingle(
|
# Determine positions on Nordnet
|
||||||
name='nordnet',
|
json_account_positions = self.get_json(API_ACCOUNT_POSITIONS.format(accid = account_id))
|
||||||
_assets=assets,
|
for pos in json_account_positions:
|
||||||
|
asset = asset_from_instrument_json(pos['instrument'])
|
||||||
|
if asset is None: continue
|
||||||
|
amount = Decimal(pos['qty'])
|
||||||
|
assets[asset] = amount
|
||||||
|
del asset, amount
|
||||||
|
|
||||||
|
nested.append(DepoSingle(
|
||||||
|
name='{} {}'.format(json_account['type'], json_account['alias']),
|
||||||
|
_assets=assets,
|
||||||
|
updated_time=now,
|
||||||
|
))
|
||||||
|
|
||||||
|
return DepoGroup(
|
||||||
|
name=f'Nordnet',
|
||||||
updated_time=now,
|
updated_time=now,
|
||||||
|
nested=nested,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user