-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyfe_parser.py
121 lines (99 loc) · 2.79 KB
/
syfe_parser.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
# -*- coding: utf-8 -*-
"""
Created on Sat May 16 00:14:55 2020
Updated script to handle certain bugs:
- "<0.01" issue. Value will be set to 0.00 instead.
- ValueError: could not convert string to float: "1,081.62" (due to the comma)
@author: KPO
"""
import numpy as np
import pandas as pd
from helper import reit_lookup
filename = "transactions.txt"
special = [
"TRANSFER_IN",
"BONUS_IN",
"MANAGEMENT_FEE",
"Funds added",
"Bonus",
"Management fee",
"Adjustment (in)",
"Portfolio transfer (in)",
]
list_date = []
list_type = []
list_details = []
list_amount = []
list_units = []
fh = open(filename)
counter = 1
with open(filename) as f:
content = f.readlines()
content = [x.strip() for x in content]
for line in content:
if line.strip() != "":
if counter == 1:
list_date.append(line)
counter += 1
elif counter == 2:
list_type.append(line)
if line in special:
list_details.append("")
counter += 2
else:
counter += 1
elif counter == 3:
list_details.append(line)
counter += 1
elif counter == 4:
list_amount.append(line)
counter += 1
elif counter == 5:
if line == "<0.01":
line = 0.00
list_units.append(line)
counter = 1
else:
continue
df = pd.DataFrame(
list(zip(list_date, list_type, list_details, list_amount, list_units)),
columns=["Date", "Type", "Details", "Amount", "Units"],
)
# Remove commas in numbers
df["Amount"] = df["Amount"].str.replace(",", "").astype(float)
df["Units"] = df["Units"].str.replace(",", "").astype(float)
# Compute price of stock
df["Computed_Price"] = df["Amount"] / df["Units"]
# Reformat datetime column
df["Date"] = pd.to_datetime(df["Date"])
# Set default exchange and currency
df["Exchange"] = "SGX"
df["Currency"] = "SGD"
# Map stock name to ticker symbol
df["Symbol"] = df["Details"].map(reit_lookup)
# Rename transaction type
df["Type"].replace(
{"Bought": "BUY", "Sold": "SELL", "Management fee": "FEES"}, inplace=True
)
# Drop transactions of fund addition
df = df[df["Type"] != "Funds added"]
df = df[df["Type"] != "Dividend received"]
df.loc[df["Type"] == "FEES", "Units"] = np.NaN
df.loc[df["Type"] == "FEES", "Computed_Price"] = np.NaN
df.loc[df["Type"] == "FEES", "Symbol"] = np.NaN
# Add remarks
df["Remarks"] = ""
df = df[
[
"Type",
"Exchange",
"Symbol",
"Units",
"Currency",
"Computed_Price",
"Date",
"Amount",
"Remarks",
]
]
df.to_csv("syfe_transactions.csv", index=False, header=False)