import csv import json import pandas as pd import re from datetime import datetime from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument("-i", "--input", dest="input_filename", help="Nordnet source file", metavar="INPUT") parser.add_argument("-o", "--output", dest="output_filename", help="Ghostfolio destination file", metavar="OUTPUT") args = parser.parse_args() dividend_expression_re = re.compile(r'(\d+\.?\d*?) ([A-Z]{3})') NORDNET_ACCOUNT_ID="cae3d45f-53bc-4dee-83a4-4448b409f8b2" format = "%Y-%m-%d" data = { 'activities': [] } splits = { "NOVO B": { "2023-09-20 00:00:00 UTC": 0.5 }, "NVDA": { "2024-06-10 00:00:00 UTC": 0.1 } } symbol_translations = { "NOVO B": "NOVO-B.CO", "NOVO B.OLD": "NOVO-B.CO", "Xtrackers MSCI Eur CDisc ESG Scr ETF 1C": "XZEC.DE" } # Date, Code, DataSource, Currency, Price, Quantity, Action, Free, Note with open(args.input_filename, newline='', encoding="utf16") as csvfile: df = pd.read_csv(csvfile, sep='\t', encoding='utf16', index_col=0) for index, row in df.iterrows(): date = None t_type = None price = 0 try: date = datetime.strptime(row['Handelsdag'], format) except ValueError: data = None print("Failed parsing date") price = str(row['Kurs']).replace('.', '') price = price.replace(',', '.') quantity = row['Antal'] fee = float(str(row['Samlede afgifter']).replace(',', '.')) symbol = row['Værdipapirer'] currency = row['Valuta.3'] match row['Transaktionstype']: case "KØBT": t_type = "BUY" case "SOLGT": t_type = "SELL" case "UDB.": t_type = "DIVIDEND" fee = 0 div = dividend_expression_re.findall(row['Transaktionstekst']) price = div[0][0] price = price.replace(',', '.') currency = div[0][1] case _: continue if symbol in splits and row['Transaktionstype'] != "UDB.": key = list(splits[symbol].keys())[0] if date.isoformat() < key: ratio = splits[symbol][key] price = float(price) * ratio; quantity = float(quantity) * (1 / ratio); if row['Valuta'] != row['Valuta.3'] and row['Transaktionstype'] != "UDB.": fee = float(fee) / float(str(row['Vekslingskurs']).replace(',', '.')) if symbol in symbol_translations: symbol = symbol_translations[symbol] data['activities'].append({ 'accountId': NORDNET_ACCOUNT_ID, 'fee': fee, 'quantity': int(quantity), 'type': t_type, 'unitPrice': float(price), 'currency': currency, 'dataSource': 'YAHOO', 'date': date.isoformat(), 'symbol': symbol, }) json_data = json.dumps(data) f = open(args.output_filename, "w") f.write(json_data) f.close()