2024-02-15 16:40:19 +00:00
|
|
|
import csv
|
|
|
|
import json
|
2024-07-20 16:42:19 +00:00
|
|
|
import pandas as pd
|
|
|
|
import re
|
2024-02-15 16:40:19 +00:00
|
|
|
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()
|
|
|
|
|
2024-07-20 16:42:19 +00:00
|
|
|
dividend_expression_re = re.compile(r'(\d+\.?\d*?) ([A-Z]{3})')
|
|
|
|
|
2024-02-15 16:40:19 +00:00
|
|
|
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
|
2024-07-20 16:42:19 +00:00
|
|
|
},
|
|
|
|
"NVDA": {
|
|
|
|
"2024-06-10 00:00:00 UTC": 0.1
|
2024-02-15 16:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol_translations = {
|
|
|
|
"NOVO B": "NOVO-B.CO",
|
2024-07-20 16:42:19 +00:00
|
|
|
"NOVO B.OLD": "NOVO-B.CO",
|
|
|
|
"Xtrackers MSCI Eur CDisc ESG Scr ETF 1C": "XZEC.DE"
|
2024-02-15 16:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Date, Code, DataSource, Currency, Price, Quantity, Action, Free, Note
|
2024-07-20 16:42:19 +00:00
|
|
|
with open(args.input_filename, newline='', encoding="utf16") as csvfile:
|
|
|
|
df = pd.read_csv(csvfile, sep='\t', encoding='utf16', index_col=0)
|
2024-02-15 16:40:19 +00:00
|
|
|
|
2024-07-20 16:42:19 +00:00
|
|
|
for index, row in df.iterrows():
|
2024-02-15 16:40:19 +00:00
|
|
|
date = None
|
|
|
|
t_type = None
|
|
|
|
price = 0
|
|
|
|
|
|
|
|
try:
|
2024-07-20 16:42:19 +00:00
|
|
|
date = datetime.strptime(row['Handelsdag'], format)
|
2024-02-15 16:40:19 +00:00
|
|
|
except ValueError:
|
|
|
|
data = None
|
|
|
|
print("Failed parsing date")
|
|
|
|
|
2024-07-20 16:42:19 +00:00
|
|
|
price = str(row['Kurs']).replace('.', '')
|
2024-02-15 16:40:19 +00:00
|
|
|
price = price.replace(',', '.')
|
2024-07-20 16:42:19 +00:00
|
|
|
quantity = row['Antal']
|
|
|
|
fee = float(str(row['Samlede afgifter']).replace(',', '.'))
|
|
|
|
symbol = row['Værdipapirer']
|
|
|
|
currency = row['Valuta.3']
|
2024-02-15 16:40:19 +00:00
|
|
|
|
2024-07-20 16:42:19 +00:00
|
|
|
match row['Transaktionstype']:
|
2024-02-15 16:40:19 +00:00
|
|
|
case "KØBT":
|
|
|
|
t_type = "BUY"
|
|
|
|
case "SOLGT":
|
|
|
|
t_type = "SELL"
|
|
|
|
case "UDB.":
|
|
|
|
t_type = "DIVIDEND"
|
|
|
|
fee = 0
|
2024-07-20 16:42:19 +00:00
|
|
|
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.":
|
2024-02-15 16:40:19 +00:00
|
|
|
key = list(splits[symbol].keys())[0]
|
|
|
|
if date.isoformat() < key:
|
|
|
|
ratio = splits[symbol][key]
|
|
|
|
price = float(price) * ratio;
|
|
|
|
quantity = float(quantity) * (1 / ratio);
|
|
|
|
|
2024-07-20 16:42:19 +00:00
|
|
|
if row['Valuta'] != row['Valuta.3'] and row['Transaktionstype'] != "UDB.":
|
|
|
|
fee = float(fee) / float(str(row['Vekslingskurs']).replace(',', '.'))
|
|
|
|
|
2024-02-15 16:40:19 +00:00
|
|
|
if symbol in symbol_translations:
|
|
|
|
symbol = symbol_translations[symbol]
|
|
|
|
|
|
|
|
data['activities'].append({
|
|
|
|
'accountId': NORDNET_ACCOUNT_ID,
|
2024-07-20 16:42:19 +00:00
|
|
|
'fee': fee,
|
2024-02-15 16:40:19 +00:00
|
|
|
'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()
|