-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_xlsx.py
More file actions
195 lines (170 loc) · 8.76 KB
/
make_xlsx.py
File metadata and controls
195 lines (170 loc) · 8.76 KB
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
import sys
import os
import xlsxwriter
import pymysql.cursors
from datetime import datetime
from pathlib import Path
import mysql_config
from start_timestamps import start_timestamps
import granularities
from consts import months_json, month_days
if sys.version_info >= (3, 0):
import configparser as ConfigParser
else:
import ConfigParser
def load_config(defaults):
_settings_dir = "."
config_file = os.path.join(_settings_dir, "config.ini")
if os.path.exists(config_file):
try:
config = ConfigParser.SafeConfigParser()
config.read(config_file)
if config.has_section("analyzing"):
config_items = dict(config.items("analyzing"))
defaults['analyzing'] = config_items
except ConfigParser.Error as e:
print("\nError parsing config file: " + config_file)
print(str(e))
exit(1)
return defaults
def create_excel(coin, granularity, rows):
highlight_up = int(config_params['analyzing']['highlight_up'])
highlight_up_color = str(config_params['analyzing']['highlight_up_color'])
highlight_down = int(config_params['analyzing']['highlight_down'])
highlight_down_color = str(config_params['analyzing']['highlight_down_color'])
Path("./output/{}".format(coin)).mkdir(parents=True, exist_ok=True)
prev_year = ''
prev_month = ''
workbook = None
sheet = None
datetime_format = None
r_index = 0
for row in rows:
timestamp = row['datetime']
date = row['datetime'][:10]
open = row['open']
low = row['low']
high = row['high']
up_percent = row['upPercent']
up_price = row['upPrice']
down_percent = row['downPercent']
down_price = row['downPrice']
year = timestamp[:4]
month = timestamp[5:7]
day = timestamp[8:10]
if prev_year != year:
if workbook is not None:
workbook.close()
filename = "./output/{}/{}_{}.xlsx".format(coin, granularity, year)
workbook = xlsxwriter.Workbook(filename)
header_format = workbook.add_format({"bold": True, "align": "center", "bg_color": "#dbdbdb", "top": 5, "right": 1, "bottom": 1, "left": 1})
header_format_l = workbook.add_format({"bold": True, "align": "center", "bg_color": "#dbdbdb", "top": 5, "right": 1, "bottom": 1, "left": 5})
header_format_r = workbook.add_format({"bold": True, "align": "center", "bg_color": "#dbdbdb", "top": 5, "right": 5, "bottom": 1, "left": 1})
datetime_format = workbook.add_format({"num_format": "d mmmm yyyy", "bg_color": "#dbdbdb", "top": 1, "right": 1, "bottom": 1, "left": 5})
datetime_format_b = workbook.add_format({"num_format": "d mmmm yyyy", "bg_color": "#dbdbdb", "top": 1, "right": 1, "bottom": 5, "left": 5})
number_format = workbook.add_format({"num_format": "#,##0.00", "bg_color": "#dbdbdb", "border": 1})
number_format_b = workbook.add_format({"num_format": "#,##0.00", "bg_color": "#dbdbdb", "top": 1, "right": 1, "bottom": 5, "left": 1})
down_format = workbook.add_format({"num_format": "#,##0.00", "font_color": "#ff0000", "bg_color": "#dbdbdb", "border": 1})
down_highlight_format = workbook.add_format({"num_format": "#,##0.00", "font_color": "#ff0000", "bg_color": "#" + highlight_down_color, "border": 1})
down_format_b = workbook.add_format({"num_format": "#,##0.00", "font_color": "#ff0000", "bg_color": "#dbdbdb", "top": 1, "right": 1, "bottom": 5, "left": 1})
down_highlight_format_b = workbook.add_format({"num_format": "#,##0.00", "font_color": "#ff0000", "bg_color": "#" + highlight_down_color, "top": 1, "right": 1, "bottom": 5, "left": 1})
open_format = workbook.add_format({"num_format": "#,##0.00", "font_color": "#0070c0", "bg_color": "#dbdbdb", "border": 1})
open_format_b = workbook.add_format({"num_format": "#,##0.00", "font_color": "#0070c0", "bg_color": "#dbdbdb", "top": 1, "right": 1, "bottom": 5, "left": 1})
up_format = workbook.add_format({"num_format": "#,##0.00", "font_color": "#00b050", "bg_color": "#dbdbdb", "top": 1, "right": 1, "bottom": 1, "left": 1})
up_highlight_format = workbook.add_format({"num_format": "#,##0.00", "font_color": "#00b050", "bg_color": "#" + highlight_up_color, "top": 1, "right": 1, "bottom": 1, "left": 1})
up_format_r = workbook.add_format({"num_format": "#,##0.00", "font_color": "#00b050", "bg_color": "#dbdbdb", "top": 1, "right": 5, "bottom": 1, "left": 1})
up_highlight_format_r = workbook.add_format({"num_format": "#,##0.00", "font_color": "#00b050", "bg_color": "#" + highlight_up_color, "top": 1, "right": 5, "bottom": 1, "left": 1})
up_format_b = workbook.add_format({"num_format": "#,##0.00", "font_color": "#00b050", "bg_color": "#dbdbdb", "top": 1, "right": 1, "bottom": 5, "left": 1})
up_format_rb = workbook.add_format({"num_format": "#,##0.00", "font_color": "#00b050", "bg_color": "#dbdbdb", "top": 1, "right": 5, "bottom": 5, "left": 1})
up_highlight_format_rb = workbook.add_format({"num_format": "#,##0.00", "font_color": "#00b050", "bg_color": "#" + highlight_up_color, "top": 1, "right": 5, "bottom": 5, "left": 1})
if prev_month != month:
sheetname = months_json[month]
sheet = workbook.add_worksheet(sheetname)
sheet.set_column("A:A", 20)
sheet.set_column("B:H", 10)
sheet.write("A1", "Date", header_format_l)
sheet.write("B1", "Down $", header_format)
sheet.write("C1", "Down %", header_format)
sheet.write("D1", "Low", header_format)
sheet.write("E1", "Open", header_format)
sheet.write("F1", "High", header_format)
sheet.write("G1", "Up $", header_format)
sheet.write("H1", "Up %", header_format_r)
r_index = 2
prev_year = year
prev_month = month
days = month_days[month]
i_year = int(year)
i_month = int(month)
if i_month == 2 and (i_year % 4 == 0 and i_year % 100 != 0 or i_year % 400 == 0):
days = 29
if r_index - 1 == days:
sheet.write_datetime("A{}".format(r_index), datetime.fromisoformat(timestamp), datetime_format_b)
sheet.write_number("B{}".format(r_index), down_price, down_format_b)
if down_percent >= highlight_down:
sheet.write_number("C{}".format(r_index), down_percent, down_highlight_format_b)
else:
sheet.write_number("C{}".format(r_index), down_percent, down_format_b)
sheet.write_number("D{}".format(r_index), low, number_format_b)
sheet.write_number("E{}".format(r_index), open, open_format_b)
sheet.write_number("F{}".format(r_index), high, number_format_b)
sheet.write_number("G{}".format(r_index), up_price, up_format_b)
if up_percent >= highlight_up:
sheet.write_number("H{}".format(r_index), up_percent, up_highlight_format_rb)
else:
sheet.write_number("H{}".format(r_index), up_percent, up_format_rb)
else:
sheet.write_datetime("A{}".format(r_index), datetime.fromisoformat(timestamp), datetime_format)
sheet.write_number("B{}".format(r_index), down_price, down_format)
if down_percent >= highlight_down:
sheet.write_number("C{}".format(r_index), down_percent, down_highlight_format)
else:
sheet.write_number("C{}".format(r_index), down_percent, down_format)
sheet.write_number("D{}".format(r_index), low, number_format)
sheet.write_number("E{}".format(r_index), open, open_format)
sheet.write_number("F{}".format(r_index), high, number_format)
sheet.write_number("G{}".format(r_index), up_price, up_format)
if up_percent >= highlight_up:
sheet.write_number("H{}".format(r_index), up_percent, up_highlight_format_r)
else:
sheet.write_number("H{}".format(r_index), up_percent, up_format_r)
r_index += 1
workbook.close()
return
def create_file(coin, granularity):
conn = pymysql.connect(host=mysql_config.host,
user=mysql_config.user,
password=mysql_config.password,
db=mysql_config.db,
charset=mysql_config.charset,
cursorclass=pymysql.cursors.DictCursor)
try:
with conn.cursor() as cursor:
sql = "SELECT * FROM `up_down_view_{}_{}` ORDER BY `timestamp`;".format(coin, granularity)
cursor.execute(sql)
rows = cursor.fetchall()
create_excel(coin, "day" if granularity == granularities.day else "", rows)
except Exception as inst:
print(type(inst), inst.args, inst)
conn.close()
return {
"statusCode": 500,
"step": "Create File",
"coin": coin,
"granularity": granularity
}
conn.close()
return {
"statusCode": 200,
"step": "Create File",
"coin": coin,
"granularity": granularity
}
defaults = {}
config_params = load_config(defaults)
if __name__ == "__main__":
coins = start_timestamps.keys()
intervals = [granularities.day]
for coin in coins:
for interval in intervals:
create_file(coin, interval)