-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade_analyzer.py
204 lines (192 loc) · 7.76 KB
/
trade_analyzer.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import os
import json
import pandas as pd
import numpy as np
class TradeAnalyzer:
"""A class to analyze trade activities"""
def analyzeData(self):
self.__asset_allocation()
def __init__(self, config_file="config.json"):
self.config = {}
# read self.config
with open(config_file, 'r') as f:
self.config = json.load(f)
self.data_directory = os.path.abspath(
os.getcwd() + self.config['market_data_folder'])
self.trades_directory = os.path.abspath(
os.getcwd() + self.config['trade_activities_folder'])
# market data checks
if not os.path.exists(self.data_directory):
os.makedirs(self.data_directory)
if not os.path.exists(os.path.join(
self.data_directory,
self.config['market_data_export']['history'])):
raise FileNotFoundError('No market dividend data found.')
if not os.path.exists(os.path.join(
self.data_directory,
self.config['market_data_export']['dividend'])):
raise FileNotFoundError('No market history data found.')
# trade activities checks
if not os.path.exists(self.trades_directory):
os.makedirs(self.trades_directory)
if not os.path.exists(os.path.join(
self.data_directory,
self.config['trade_activities_inputs']['trade_activities_excel'])):
raise FileNotFoundError('No trade activities found.')
if not os.path.exists(os.path.join(
self.data_directory,
self.config['trade_activities_inputs']['ticker_category_csv'])):
raise FileNotFoundError('No ticker category defined.')
#
# read files and build basic tables
#
# read history df
self.history_df = pd.read_csv(os.path.join(
self.data_directory,
self.config['market_data_export']['history']))
# process ticker
self.history_df['Ticker'] = self.history_df['Ticker'].map(
lambda x: x.lstrip('').rstrip('.TO'))
# read activities and ticker category
activities_file = os.path.join(
self.trades_directory,
self.config['trade_activities_inputs']['trade_activities_excel'])
activities_excel = pd.ExcelFile(activities_file, engine='openpyxl')
activities_excel_list = []
for i in activities_excel.sheet_names:
activities_excel_list.append(
pd.read_excel(
activities_file,
sheet_name=i,
engine='openpyxl')
)
self.activities_df = pd.concat(activities_excel_list)
self.activities_df = self.activities_df.sort_values(by=['Date'])
# add additional dates
self.activities_df['Date'] = self.activities_df['Date'].dt.strftime(
"%Y-%m-%d")
self.ticker_cat_df = pd.read_csv(os.path.join(
self.trades_directory,
self.config['trade_activities_inputs']['ticker_category_csv']))
# # get full trade df
# self.fulltrade_df = self.ticker_cat_df\
# .merge(self.activities_df, on='Ticker')\
# .merge(self.history_df, on=['Ticker', 'Date'])
# read dividend df
self.dividend_df = pd.read_csv(os.path.join(
self.data_directory,
self.config['market_data_export']['dividend']))
def __asset_allocation(self):
"""Analyze allocation of asset"""
# asset over time
assetovertime_df = self.activities_df[['Account', 'Date', 'Ticker', 'Quantity']]\
.merge(self.ticker_cat_df, on='Ticker')\
.merge(self.history_df, on=['Ticker', 'Date'])
# asset cumulative quantity over time by account
assetacctime_df = pd.pivot_table(
assetovertime_df,
values='Quantity',
index='Date',
columns=['Account', 'Ticker'],
aggfunc=np.sum)\
.fillna(0)\
.aggregate(np.cumsum)\
.stack(level=[0, 1])\
.reset_index()\
.rename(columns={0: 'Quantity'})\
.merge(
self.history_df,
on=['Ticker', 'Date'])
# calculate market value of asset over time
assetacctime_df['Market_Value'] = assetacctime_df['Close'] * \
assetacctime_df['Quantity']
# generate pivot table of market asset value
assetacctime_pivot_df = pd.pivot_table(
assetacctime_df,
values='Market_Value',
index='Date',
columns='Account',
aggfunc=np.sum)\
.fillna(0)\
.reset_index()
assetacctime_pivot_df['Date'] = assetacctime_pivot_df['Date']\
.astype('datetime64[ns]')
# asset cumulative quantity over time by category
assetcattime_df = pd.pivot_table(
assetovertime_df,
values='Quantity',
index='Date',
columns=['Category', 'Ticker'],
aggfunc=np.sum)\
.fillna(0)\
.aggregate(np.cumsum)\
.stack(level=[0, 1])\
.reset_index()\
.rename(columns={0: 'Quantity'})\
.merge(
self.history_df,
on=['Ticker', 'Date'])
# calculate market value of asset over time
assetcattime_df['Market_Value'] = assetcattime_df['Close'] * \
assetcattime_df['Quantity']
# generate pivot table of market asset value
assetcattime_pivot_df = pd.pivot_table(
assetcattime_df,
values='Market_Value',
index='Date',
columns='Category',
aggfunc=np.sum)\
.fillna(0)\
.reset_index()
assetcattime_pivot_df['Date'] = assetcattime_pivot_df['Date']\
.astype('datetime64[ns]')
# generate pivot table of market asset allocation
asset_pct_df = pd.pivot_table(
assetcattime_df,
values='Market_Value',
index=['Date', 'Category'],
aggfunc=np.sum).fillna(0)\
.groupby(level=0)\
.apply(lambda x: 100 * x / float(x.sum()))\
.reset_index()
asset_pct_pivot_df = pd.pivot_table(
asset_pct_df,
values='Market_Value',
index='Date',
columns='Category',
aggfunc=np.sum)\
.fillna(0)\
.round(2)\
.reset_index()
asset_pct_pivot_df['Date'] = asset_pct_pivot_df['Date']\
.astype('datetime64[ns]')
# write to excel
with pd.ExcelWriter(self.config['analysis_output_excel'], engine='xlsxwriter') as writer: # pylint: disable=abstract-class-instantiated
assetacctime_pivot_df.to_excel(
writer,
sheet_name='Asset_Accounts',
index=False)
asset_pct_pivot_df.to_excel(
writer,
sheet_name='Asset_Allocation',
index=False)
assetcattime_pivot_df.to_excel(
writer,
sheet_name='Asset_Values',
index=False)
# write raw data to csv
assetacctime_pivot_df.to_csv(
self.config['analysis_output_excel'].replace(
".xlsx", "_asset_acct.csv"),
index=False)
asset_pct_pivot_df.to_csv(
self.config['analysis_output_excel'].replace(
".xlsx", "_asset_allo.csv"),
index=False)
assetcattime_pivot_df.to_csv(
self.config['analysis_output_excel'].replace(
".xlsx", "_asset_val.csv"),
index=False)
def __income_by_asset(self):
"""Analyze income by asset"""
pass