-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpad.py
184 lines (148 loc) · 6.57 KB
/
pad.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- coding: utf-8 -*-
"""
PORTFOLIO TRACKER - PAD MODULE
For BNPP Singapore PAD requirements
Created on Sat Sep 26 13:38:19 2020
@author: Wilson Leong
"""
from setup import *
import pandas as pd
import datetime
# This function generates the list of transactions based on input start and end dates - to be pasted into PAD declaration form.
def GetTransactionList(DateFrom, DateTo):
#DateFrom = datetime.datetime(2017,4,1)
#DateTo = datetime.datetime(2017,9,30)
db = ConnectToMongoDB()
coll = db['Transactions']
df = pd.DataFrame(list(coll.find({'Date': {'$gte': DateFrom, '$lt': DateTo},
'Type': {'$in': ['Buy', 'Sell']},
'Platform': {'$in': ['FSM HK', 'FSM SG', 'TD UK']}
}
,{'Platform':1,
'BBGCode':1,
'Date':1,
'NoOfUnits':1,
'Type':1
}
)))
df.drop(['_id'], axis=1, inplace=True)
df.sort_values(['Platform','Date'], inplace=True)
# loop through the list and enrich columns
for i in range(len(df)):
row = df.loc[i]
# get security name
df.loc[i, 'SecurityName'] = _GetSecurityName(row.BBGCode)
# buy/sell
if row.Type=='Buy':
df.loc[i, 'NoOfUnitsBought'] = row.NoOfUnits
df.loc[i, 'NoOfUnitsSold'] = None
elif row.Type=='Sell':
df.loc[i, 'NoOfUnitsBought'] = None
df.loc[i, 'NoOfUnitsSold'] = -row.NoOfUnits
# get balance b/f
df.loc[i,'BalanceBF'] = _GetBalBF(row.Platform, row.BBGCode, DateFrom)
# get balance c/f
df.loc[i,'BalanceCF'] = _GetBalCF(row.Platform, row.BBGCode, DateTo)
# rearrange and export
df = df[['Platform', 'BalanceBF', 'Date', 'SecurityName', 'NoOfUnitsBought', 'NoOfUnitsSold', 'BalanceCF']]
return df
# get the security name from BBG Code
def _GetSecurityName(BBGCode):
db = ConnectToMongoDB()
coll = db['Security']
df = pd.DataFrame(list(coll.find({'BBGCode': BBGCode})))
if len(df) > 0:
name = df.iloc[0].Name
else:
name = None
return name
# get the balance brought forward (opening balance) for a security on a given date
def _GetBalBF(Platform, BBGCode, Date):
#Platform = 'FSM SG'
#BBGCode = 'ESJDASH LX'
#Date = datetime.datetime(2017,4,1)
db = ConnectToMongoDB()
coll = db['Transactions']
df = pd.DataFrame(list(coll.find({'Date': {'$lt': Date},
'BBGCode': BBGCode,
'Platform': Platform,
'Type': {'$in': ['Buy', 'Sell']}
})))
if len(df)>0:
bal_bf = round(df.NoOfUnits.sum(), 4)
else:
bal_bf = 0
return bal_bf
# get the balance carried forward (closing balance) for a security on a given date
def _GetBalCF(Platform, BBGCode, Date):
#BBGCode = 'ESJDASH LX'
#Date = datetime.datetime(2017,9,20)
db = ConnectToMongoDB()
coll = db['Transactions']
df = pd.DataFrame(list(coll.find({'Date': {'$lte': Date},
'BBGCode': BBGCode,
'Platform': Platform,
'Type': {'$in': ['Buy', 'Sell']}
})))
if len(df)>0:
bal_cf = round(df.NoOfUnits.sum(), 4)
else:
bal_cf = 0
return bal_cf
# This function generates the list of portfolio holdings including balance b/f and balance c/f - to be pasted into the PAD declaration form.
def GetHoldingsSummary(DateFrom, DateTo):
#DateFrom = datetime.datetime(2017,4,1)
#DateTo = datetime.datetime(2017,9,30)
db = ConnectToMongoDB()
coll = db['Transactions']
df = pd.DataFrame(list(coll.find({'Date': {'$gte': DateFrom, '$lt': DateTo},
'Type': {'$in': ['Buy', 'Sell']},
'Platform': {'$in': ['FSM HK', 'FSM SG', 'TD UK']}
}
,{'Platform':1,
'BBGCode':1,
'Date':1,
'NoOfUnits':1,
'Type':1
}
)))
df.drop(['_id'], axis=1, inplace=True)
df.sort_values(['Platform','Date'], inplace=True)
# sum up buy and sell
for i in range(len(df)):
row = df.loc[i]
# buy/sell
if row.Type=='Buy':
df.loc[i, 'NoOfUnitsBought'] = row.NoOfUnits
df.loc[i, 'NoOfUnitsSold'] = None
elif row.Type=='Sell':
df.loc[i, 'NoOfUnitsBought'] = None
df.loc[i, 'NoOfUnitsSold'] = -row.NoOfUnits
a = df.groupby(['Platform', 'BBGCode']).agg({'NoOfUnitsBought': sum, 'NoOfUnitsSold': sum})
a = a.reset_index()
# loop through the list and enrich columns
for i in range(len(a)):
row = a.loc[i]
# get security name
a.loc[i, 'SecurityName'] = _GetSecurityName(row.BBGCode)
# # buy/sell
# if row.Type=='Buy':
# a.loc[i, 'NoOfUnitsBought'] = row.NoOfUnits
# a.loc[i, 'NoOfUnitsSold'] = None
#
# elif row.Type=='Sell':
# a.loc[i, 'NoOfUnitsBought'] = None
# a.loc[i, 'NoOfUnitsSold'] = -row.NoOfUnits
# get balance b/f
a.loc[i,'BalanceBF'] = _GetBalBF(row.Platform, row.BBGCode, DateFrom)
# get balance c/f
a.loc[i,'BalanceCF'] = _GetBalCF(row.Platform, row.BBGCode, DateTo)
# rearrange and export
a['TradeDate'] = '-'
a = a[['Platform', 'BalanceBF', 'TradeDate', 'SecurityName', 'NoOfUnitsBought', 'NoOfUnitsSold', 'BalanceCF']]
return a
# # REPORTING for Singapore PAD (not a requirement for HK)
# DateFrom = datetime.datetime(2017, 4,1)
# DateTo = datetime.datetime(2017,9,30)
# Report_TransactionsList = GetTransactionList(DateFrom, DateTo)
# Report_HoldingsSummary = GetHoldingsSummary(DateFrom, DateTo)