-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodo.py
281 lines (228 loc) · 9.09 KB
/
dodo.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from os import path, listdir, makedirs
import re, codecs
import datetime as dt
import calendar as cal
import pandas as pd
import string
import numpy as np
from datamanager.envs import *
from datamanager.load import *
from datamanager.adjust import calc_adj_close
from datamanager.utils import last_month_end
import datamanager.transforms as transf
fields = marketdata_fields()
# paths
mergein_old = MASTER_DATA_PATH
mergein_new = CONVERT_PATH
index_src_path = path.join(DL_PATH, 'Indices.xlsx')
closepath = path.join(MERGED_PATH, "Close.csv")
divpath = path.join(MERGED_PATH, "Dividend Ex Date.csv")
bookvaluepath = path.join(MERGED_PATH, "Book Value per Share.csv")
def get_all_equities():
new_all, _, _, _ = get_all_equities_from_data(MERGED_PATH, CONVERT_PATH, 'Close')
return new_all
def get_current_listed():
new_all, current, newly_listed, delisted = get_all_equities_from_data(MERGED_PATH, CONVERT_PATH, 'Close')
return current_set
def convert_data(task):
'''
'''
name = task.name.split(':')[1]
fp = path.join(DL_PATH, name + '.xlsx')
new_data = load_inetbfa_ts_data(fp)
# drop all data for current month
dropix = new_data.index[new_data.index.values.astype('datetime64[D]') > np.datetime64(last_month_end())]
new_data.drop(dropix).sort_index(axis = 1).to_csv(task.targets[0])
def convert_indices(task):
new_data = load_inetbfa_ts_data(index_src_path)
dropix = new_data.index[new_data.index.values.astype('datetime64[D]') > np.datetime64(last_month_end())]
new_data.drop(dropix).sort_index(axis = 1).to_csv(task.targets[0])
def merge_index(task):
new = load_ts(path.join(CONVERT_PATH, 'Indices.csv'))
old = load_ts(path.join(MERGED_PATH, 'Indices.csv'))
merged = empty_dataframe(old.columns, enddate = last_month_end())
merged.update(old)
merged.update(new)
merged.sort_index(axis = 1).to_csv(task.targets[0])
def merge_data(task):
name = task.name.split(':')[1]
new = load_ts(path.join(CONVERT_PATH, name + '.csv'))
old = load_ts(path.join(MERGED_PATH, name + '.csv'))
merged = empty_dataframe(get_all_equities(), enddate = last_month_end())
merged.update(old)
merged.update(new)
merged.sort_index(axis = 1).to_csv(task.targets[0])
def calc_adjusted_close(dependencies, targets):
all_equities = get_all_equities()
# Import closing price data
close = load_field_ts(MERGED_PATH, field = "Close")
# Import dividend ex date data
divs = load_field_ts(MERGED_PATH, field = "Dividend Ex Date")
adj_close = calc_adj_close(close, divs, all_equities, enddate = last_month_end())
adj_close.sort_index(axis = 1).to_csv(targets[0])
def booktomarket(dependencies, targets):
# Import closing price data
close = load_field_ts(MERGED_PATH, field = "Close")
# Import book value per share data
bookvalue = load_field_ts(MERGED_PATH, field = "Book Value per Share")
b2m = transf.calc_booktomarket(close, bookvalue)
b2m.sort_index(axis = 1).to_csv(targets[0])
def resample_monthly(task):
name = task.name.split(':')[1]
data = load_field_ts(MASTER_DATA_PATH, field = name)
write = True
out = pd.DataFrame()
if name == 'Close':
out = transf.resample_monthly(data, how = 'last')
elif name == 'Adjusted Close':
out = transf.resample_monthly(data, how = 'last')
elif name == 'Open':
out = transf.resample_monthly(data, how = 'first')
elif name == 'High':
out = transf.resample_monthly(data, how = 'max')
elif name == 'Low':
out = transf.resample_monthly(data, how = 'min')
elif name == 'DY':
out = transf.resample_monthly(data, how = 'last')
elif name == 'EY':
out = transf.resample_monthly(data, how = 'last')
elif name == 'PE':
out = transf.resample_monthly(data, how = 'last')
elif name == 'Book-to-Market':
out = transf.resample_monthly(data, how = 'last')
elif name == 'Volume':
out = transf.resample_monthly(data, how = 'sum')
elif name == 'Total Number Of Shares':
out = transf.resample_monthly(data, how = 'last')
elif name == 'Number Of Trades':
out = transf.resample_monthly(data, how = 'sum')
elif name == 'Market Cap':
out = transf.resample_monthly(data, how = 'last')
else:
write = False
if (write):
out.sort_index(axis = 1).to_csv(path.join(MASTER_DATA_PATH, name + '-monthly.csv'))
def monthly_avg_momentum(task):
# load the daily close
close = load_field_ts(MASTER_DATA_PATH, field = "Close")
# resample to monthly average data
close_m = transf.resample_monthly(close, how = 'mean')
# calculate the momentum
mom = transf.momentum_monthly(close_m, 12, 1)
mom.sort_index(axis = 1).to_csv(path.join(MASTER_DATA_PATH, "Monthly-Avg-Momentum.csv"))
def monthly_close_momentum(task):
# load the daily close
close = load_field_ts(MASTER_DATA_PATH, field = "Close")
# resample to monthly close data
close_m = transf.resample_monthly(close, how = 'last')
# calculate the momentum
mom = transf.momentum_monthly(close_m, 12, 1)
mom.sort_index(axis = 1).to_csv(path.join(MASTER_DATA_PATH, "Monthly-Close-Momentum.csv"))
def calc_log_returns(task):
close = load_field_ts(MASTER_DATA_PATH, field = "Close")
logret = transf.log_returns(close)
logret.sort_index(axis = 1).to_csv(path.join(MASTER_DATA_PATH, "Log-Returns.csv"))
def calc_pead_momentum(task):
# load close
close = load_field_ts(MASTER_DATA_PATH, field = "Close")
# load dividend decl date
announcements = load_field_ts(MASTER_DATA_PATH, field = "Dividend Declaration Date")
pead = transf.pead_momentum(announcements, close)
pead.sort_index(axis = 1).to_csv(path.join(MASTER_DATA_PATH, "Normalized-PEAD-Momentum.csv"))
def swapaxes(dependencies, targets):
temp = {}
for d in dependencies:
field = d.split("\\")[-1].split(".")[0]
temp[field] = pd.read_csv(d, sep = ',', index_col = 0, parse_dates=True)
# now create panel
panel = pd.Panel(temp)
out = panel.swapaxes(0, 2)
for ticker in out.items:
out[ticker].dropna(how='all').sort_index(axis = 1).to_csv(path.join(CONVERT_PATH, "tickers", ticker + '.csv'), index_label = "Date")
##########################################################################################
# DOIT tasks
##########################################################################################
# 1
def task_convert():
for f in fields:
yield {
'name':f,
'actions':[convert_data],
'targets':[path.join(CONVERT_PATH, f+ '.csv')],
'file_dep':[path.join(DL_PATH, f + '.xlsx')],
}
def task_convert_index():
return {
'actions':[convert_indices],
'file_dep': [path.join(DL_PATH, 'Indices.xlsx')],
'targets':[path.join(CONVERT_PATH, "Indices.csv")]
}
def task_merge_index():
return {
'actions':[merge_index],
'targets':[path.join(MERGED_PATH, "Indices.csv")],
'file_dep':[path.join(CONVERT_PATH, "Indices.csv")]
}
# 2
def task_merge():
for f in fields:
yield {
'name':f,
'actions':[merge_data],
'targets':[path.join(MERGED_PATH, f + '.csv')],
'file_dep':[path.join(mergein_new, f + '.csv'), path.join(mergein_old, f + '.csv')]
}
# 3
def task_adjusted_close():
return {
'actions':[calc_adjusted_close],
'file_dep': [closepath,
divpath],
'targets':[path.join(MERGED_PATH, "Adjusted Close.csv")]
}
# 5
def task_book2market():
return {
'actions':[booktomarket],
'file_dep': [closepath, bookvaluepath],
'targets':[path.join(MERGED_PATH, "Book-to-Market.csv")]
}
# 6
def task_data_per_ticker():
files = [path.join(CONVERT_PATH, f + '.csv') for f in fields]
return {
'actions':[swapaxes],
'file_dep': files,
'targets':[path.join(CONVERT_PATH, "tickers")]
}
def task_resample_monthly():
expanded = fields + ['Book-to-Market', 'Adjusted Close']
for f in fields:
yield {
'name':f,
'actions':[resample_monthly],
'targets':[path.join(MASTER_DATA_PATH, f + '-monthly.csv')],
'file_dep':[path.join(MASTER_DATA_PATH, f + '.csv')],
}
def task_monthly_close_momentum():
return {
'actions':[monthly_close_momentum],
'file_dep':[path.join(MASTER_DATA_PATH, 'Close.csv')],
}
def task_monthly_avg_momentum():
return {
'actions':[monthly_avg_momentum],
'file_dep':[path.join(MASTER_DATA_PATH, 'Close.csv')],
'targets':[path.join(MASTER_DATA_PATH, "Monthly-Avg-Momentum.csv")]
}
def task_log_returns():
return {
'actions':[calc_log_returns],
'file_dep':[path.join(MASTER_DATA_PATH, 'Close.csv')],
}
def task_pead_momentum():
return {
'actions':[calc_pead_momentum],
'file_dep':[path.join(MASTER_DATA_PATH, 'Close.csv'), path.join(MASTER_DATA_PATH, 'Dividend Declaration Date.csv')],
'targets':[path.join(MASTER_DATA_PATH, "Normalized-PEAD-Momentum.csv")]
}