-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
140 lines (120 loc) · 4.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import csv
import os
from datetime import datetime
from dateutil import parser
from transaction import Transaction
# Define the input folder path
input_folder = "/Users/jakub/Development/portfolio-tracker/input"
us_stocks = ["AAPL", "MRNA", "BB", "GME", "BYND", "KODK"]
print("---------------- NUTMEG ------------------")
all_transactions = []
csv_files = [
filename
for filename in os.listdir(input_folder)
if filename.startswith("NUTMEG_In") and filename.endswith(".csv")
]
# Iterate through the CSV files and append their transactions to the list
for csv_file in csv_files:
file_path = os.path.join(input_folder, csv_file)
with open(file_path, newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
time = row["Date"]
asset = (
"$CASH"
if row["Description"].lower()
in ["dividend", "deposit", "interest on cash", "withdrawal"]
else row["Investment"]
)
try:
share_price = float(row["Share Price"])
num_shares = float(row["No. Shares"])
except ValueError:
share_price = 0.0
num_shares = 0.0
transaction = Transaction(
date=time,
type=row["Description"],
ticker=asset,
share_price=share_price,
share_amount=num_shares,
source_currency="GBP",
target_currency="GBP",
total=row["Total Value"],
)
all_transactions.append(transaction)
csv_files = [
filename
for filename in os.listdir(input_folder)
if filename.startswith("NUTMEG_Tr") and filename.endswith(".csv")
]
for csv_file in csv_files:
file_path = os.path.join(input_folder, csv_file)
with open(file_path, newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
time = row["Date"]
asset = "$CASH"
transaction = Transaction(
date=time,
type=row["Description"],
ticker=asset,
source_currency="GBP",
target_currency="GBP",
total=row["Amount"],
)
all_transactions.append(transaction)
print(f"{len(all_transactions)} total Nutmeg transactions")
# Define the output CSV file path
yahoo_nutmeg_csv_file = (
"/Users/jakub/Development/portfolio-tracker/output/yahoo_nutmeg.csv"
)
GBP_stocks = ["UESD", "GIL5", "DHYG", "JPSG"]
with open(yahoo_nutmeg_csv_file, "w", newline="") as csvfile:
fieldnames = [
"Symbol",
"Date",
"Time",
"Trade Date",
"Purchase Price",
"Quantity",
"Comment",
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for transaction in all_transactions:
if not transaction.type in ["BUY", "SELL"]:
continue
item = {}
if transaction.asset == "$CASH":
item["Symbol"] = "$$CASH"
item["Purchase Price"] = 1
item["Quantity"] = transaction.total
elif (
not transaction.source_currency == "GBP"
or transaction.target_currency == "GBP"
) and transaction.asset not in us_stocks:
item["Symbol"] = transaction.asset + ".L"
item["Purchase Price"] = (
round(float(transaction.share_price) * 100, 2)
if transaction.asset not in GBP_stocks
else round(float(transaction.share_price), 2)
)
if transaction.type == "BUY":
item["Quantity"] = transaction.amount
elif transaction.type == "SELL":
item["Quantity"] = -transaction.amount
else:
item["Symbol"] = transaction.asset
item["Purchase Price"] = transaction.share_price
if transaction.type == "BUY":
item["Quantity"] = transaction.amount
elif transaction.type == "SELL":
item["Quantity"] = -transaction.amount
item["Date"] = datetime.fromisoformat(transaction.time).strftime("%d/%m/%Y")
item["Time"] = (
datetime.fromisoformat(transaction.time).strftime("%H:%M") + " BST"
)
item["Trade Date"] = datetime.fromisoformat(transaction.time).strftime("%Y%m%d")
item["Comment"] = transaction.transaction
writer.writerow(item)