-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
281 lines (232 loc) · 7.61 KB
/
index.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
# my packages
import sqlite
# system packages
import sqlite3, time
from sqlite3 import Error
def collect_index(file,
t1=(time.time() - (time.time() % 3600) - 604800), t2=(time.time())):
"""
Compose the index and set weights in the SQLite DB
:param t1: Earlier Unix Epoch
:type t1: Unix Epoch
:param t2: Later Unix Epoch
:type t2: Unix Epoch
"""
with sqlite.create_connection(file) as conn:
data = compose_index(conn, get_value(conn), t1, t2)
updates(conn, data)
def collect_index_value(file, time=time.time()):
"""
Value the index given a time
:param file: Database file
:type file: SQLite Database file
:param time: Unix Epoch
:type time: integer
"""
conn = sqlite.create_connection(file)
with conn:
value = value_index(conn, time)
print(value)
sqlite.insert_value(conn, time, value)
def update_weights(file, date=time.time()):
"""
Update the latest stock weights using the day's composition
:param file: Database file
:type file: SQLite Database file
:param time: Unix Epoch
:type time: integer
"""
conn = sqlite.create_connection(file)
comp = get_composition(conn, date - ((date - 14400) % 86400) + 9*3600)
for c in comp:
c["tim"] = date
updates(conn, comp)
def sort_by_popularity(conn, date):
"""
Calculate and sort stock popularity at date.
Returns a sorted list of dictionaries containing {id, time, popularity, price}
:param conn: SQLite Connection
:type conn: SQLite Connection
:param date: Unix Epoch
:type date: integer
"""
output = []
tim1 = date - (date % 3600)
tim2 = tim1 + 3600
query = "SELECT * FROM index_data WHERE tim BETWEEN {} AND {} ORDER BY popularity desc".format(tim1, tim2)
rows = sqlite.execute(conn, query)
for row in rows:
output.append({
"id": row[0],
"tim": row[1],
"popularity": row[2],
"price": row[3],
"weight": row[4]
})
return output
def sort_by_largest_change(conn, date1, date2):
"""
Calculate and sort the largest changes in popularity between date1 and date2.
Returns a dictionary such that "{id}" : {popularity}
:param conn: SQLite Connection
:type conn: SQLite Connection
:param date1: Earlier date
:type date1: Unix Epoch
:param date2: Later date
:type date2: Unix Epoch
"""
d1 = sort_by_popularity(conn, date1)
d2 = sort_by_popularity(conn, date2)
output = d2
for d in output:
try:
items = [item for item in d1 if item["id"] == d["id"]]
past_item = items[0]
d["change"] = d["popularity"] - past_item["popularity"]
d["tim1"] = past_item["tim"]
except IndexError:
d["change"] = 0
d["tim1"] = d["tim"]
return sorted(output, key=lambda item: item["change"], reverse=True)
def compose_index(conn, value, date1, date2):
"""
Composes the index stocks
Returns a dictionary such that "{id}" : {share_weight}
:param conn: SQLite Connection
:type conn: SQLite Connection
:param value: Total value of index
:type value: nonnegative float
:param date1: Earlier date
:type date1: String "YYYY-MM-DD"
:param date2: Later date
:type date2: String "YYYY-MM-DD"
"""
# number of total stocks in the index
num_stocks = 50
# price per stock
pps = value/num_stocks
# percentage of index based on movement
percent_change = 0.8
# percentage of index based on total popularity
percent_pop = 1 - percent_change
num_change = round(percent_change * num_stocks)
num_pop = num_stocks - num_change
composition = []
ids = []
most_popular = sort_by_popularity(conn, date2)
largest_change = sort_by_largest_change(conn, date1, date2)
for stk in largest_change:
if len(composition) >= num_change:
break
if not stk["id"] in ids:
stk["weight"] = pps / stk["price"]
composition.append(stk)
ids.append(stk["id"])
for stk in most_popular:
if len(composition) >= num_stocks:
break
if not stk["id"] in ids:
stk["weight"] = pps / stk["price"]
composition.append(stk)
ids.append(stk["id"])
return composition
def update(conn, data):
"""
Update the weights in the index_data table
:param conn: SQLite Connection
:type conn: SQLite Connection
:param data: Array of weights to be updated
:type data: Array of Dictionaries {id: _, tim: _, weight: _}
"""
for datum in data:
sqlite.index_update(conn, datum["id"], datum["tim"], datum["weight"])
def updates(conn, data):
if len(data) == 0:
print("No data to update.")
return
query = "UPDATE index_data SET weight = CASE "
tim = 0
for datum in data:
tim = datum['tim']
tim1 = tim - (tim % 3600)
tim2 = tim1 + 3600
string = "WHEN (id = '" + datum['id'] + "') THEN "
string += str(datum['weight']) + " "
query = ''.join([query, string])
string = "ELSE weight END WHERE tim BETWEEN {} AND {};".format(tim1, tim2)
query = ''.join([query, string])
sqlite.execute(conn, query)
def value_index(conn, date):
"""
Value the index at a given date, assuring no stock is repeated
:param conn: SQLite database Connection
:type conn: SQLite Connection
:param date: Unix Epcoh
:type date: integer
"""
tim1 = date - (date % 3600)
tim2 = tim1 + 3600
query = "SELECT * FROM index_data WHERE tim BETWEEN {} AND {} AND weight != 0".format(tim1, tim2)
data = sqlite.execute(conn, query)
value = 0
stocks = []
for datum in data:
if not datum[0] in stocks:
stocks.append(datum[0])
value += datum[3] * datum[4]
return value
def get_value(conn, date=None):
"""
Get the value of the index at a given time
:param conn: SQLite database Connection
:type conn: SQLite database connection
:param date: Unix Epoch
:type date: integer
"""
i = -1
query = "SELECT * FROM index_value"
if date is not None:
i = 0
tim1 = date - (date % 3600)
tim2 = tim1 + 3600
query = "SELECT * FROM index_value WHERE tim BETWEEN {} AND {}".format(tim1, tim2)
data = sqlite.execute(conn, query)
value = 100
if len(data) > 0:
value = data[i][1]
return value
def get_values(conn, tim1, tim2):
"""
Get all index values between tim1 and tim2
:param conn: SQLite database Connection
:type conn: SQLite database connection
:param tim1: Earlier Unix Epoch
:type tim1: integer
:param tim2: Earlier Unix Epoch
:type tim2: integer
"""
query = "SELECT * FROM index_value WHERE tim BETWEEN {} AND {}".format(tim1, tim2)
data = sqlite.execute(conn, query)
return data
def get_composition(conn, date):
"""
Return the index composition at a time
:param conn: SQLite database Connection
:type conn: SQLite database connection
:param date: Unix Epoch
:type date: integer
"""
tim1 = date - (date % 3600)
tim2 = tim1 + 3600
query = "SELECT * FROM index_data WHERE tim BETWEEN {} AND {} AND NOT weight = 0".format(tim1, tim2)
data = sqlite.execute(conn, query)
results = []
for row in data:
results.append({
"id": row[0],
"tim": row[1],
"popularity": row[2],
"price": row[3],
"weight": row[4]
})
return results