-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice_indices.py
238 lines (193 loc) · 10.3 KB
/
price_indices.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
import csv
from typing import List
import matplotlib.pyplot as plt
import numpy
import pandas
from common.currency import Currency
from common.currency_handler import CurrencyHandler
class PriceIndices:
currency_handler = CurrencyHandler.Instance()
def __init__(self):
self.counter = 0
self.results = dict()
all_currency_names = self.currency_handler.get_all_currency_names()
self.all_currencies: List[Currency] = list(
map(lambda x: self.currency_handler.get_currency(x), all_currency_names))
self.bitcoin: Currency = self.currency_handler.get_currency("bitcoin")
def run(self):
fig, ax = plt.subplots()
# fig, ax = self.calculates_weighted_index("market_cap", 1, 0, fig, ax)
# fig, ax = self.calculates_weighted_index("volume", 1, 0, fig, ax)
# fig, ax = self.calculates_weighted_index("market_cap", .2, 10000, fig, ax)
# fig, ax = self.calculates_weighted_index("market_cap", .2, 100000, fig, ax)
# fig, ax = self.calculates_weighted_index("market_cap", .2, 1000000, fig, ax)
# fig, ax = self.calculates_weighted_index("market_cap", .2, 10000000, fig, ax)
# fig, ax = self.calculates_weighted_index("volume", .2, 10000, fig, ax)
# fig, ax = self.calculates_weighted_index("volume", .2, 100000, fig, ax)
# fig, ax = self.calculates_weighted_index("volume", .2, 1000000, fig, ax)
# fig, ax = self.calculates_weighted_index("volume", .2, 10000000, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("market_cap", 1, 0, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("volume", 1, 0, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("market_cap", .2, 10000, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("market_cap", .2, 100000, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("market_cap", .2, 1000000, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("market_cap", .2, 10000000, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("volume", .2, 10000, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("volume", .2, 100000, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("volume", .2, 1000000, fig, ax)
fig, ax = self.calculates_weighted_index_30_day_adjusted("volume", .2, 10000000, fig, ax)
# fig, ax = self.calculates_weighted_index_30_day_adjusted("market_cap", .1, 1000000, fig, ax)
# fig, ax = self.calculates_weighted_index_30_day_adjusted("market_cap", .1, 100000, fig, ax)
# fig, ax = self.calculates_weighted_index_30_day_adjusted("volume", .1, 10000000, fig, ax)
# fig, ax = self.calculates_weighted_index_30_day_adjusted("volume", .1, 1000000, fig, ax)
self.export()
ax.set_yscale("log")
plt.legend()
plt.show()
def calculates_weighted_index(self, weight_name, max_weight, min_volume, fig, ax):
print("Counter at " + str(self.counter))
self.counter += 1
# if weight_name == "both":
weighted_return = dict()
calculation_name = "Daily balanced: " + weight_name + "-weighted (max_weight=" + str(
max_weight) + ", min_volume=" + str(min_volume) + ")"
self.results["timestamp"] = self.bitcoin.data.index
self.results["bitcoin"] = list(self.bitcoin.data.usd / self.bitcoin.data.usd[self.bitcoin.data.index[0]] * 100)
# total = Currency("total")
for index in self.bitcoin.relative_data.index:
print(index)
for_weight_calculation = dict()
for currency in self.all_currencies + [self.bitcoin]:
for_weight_calculation[currency.currency] = 0
if index in currency.data.index:
if currency.data.volume[index] >= min_volume:
for_weight_calculation[currency.currency] = currency.data[weight_name][index]
weights_to_use = self.calculate_weights(for_weight_calculation, max_weight)
value = 0
# print(index)
index_next_day = index + 1000 * 3600 * 24
for currency in self.all_currencies + [self.bitcoin]:
# if index_next_day in currency.relative_data.index and numpy.isfinite(
# currency.relative_data.usd[index_next_day]):
# value += currency.relative_data.usd[index_next_day] * weights_to_use[currency.currency]
#
# weighted_return[index_next_day] = value
if index in currency.relative_data.index and numpy.isfinite(
currency.relative_data.usd[index]):
value += currency.relative_data.usd[index] * weights_to_use[currency.currency]
weighted_return[index] = value
# print(weighted_return)
current = 100
chart_index = dict()
for key in sorted(weighted_return.keys()):
current = current * (1 + weighted_return[key])
chart_index[key] = current
pandas.Series(list(chart_index.values())).plot(ax=ax,
label=calculation_name)
self.results[calculation_name] = list(chart_index.values())
return fig, ax
def calculates_weighted_index_30_day_adjusted(self, weight_name, max_weight, min_volume, fig, ax):
print("Counter at " + str(self.counter))
self.counter += 1
# if weight_name == "both":
weighted_return = dict()
calculation_name = "30 day balanced: " + weight_name + "-weighted (max_weight=" + str(
max_weight) + ", min_volume=" + str(min_volume) + ")"
self.results["timestamp"] = self.bitcoin.data.index
self.results["bitcoin"] = list(self.bitcoin.data.usd / self.bitcoin.data.usd[self.bitcoin.data.index[0]] * 100)
# total = Currency("total")
counter = 0
currencies_to_use = list()
for index in self.bitcoin.relative_data.index:
# print(index)
if counter % 30 == 0:
currencies_to_use = list()
for currency in self.all_currencies + [self.bitcoin]:
start = counter - 30
if start < 0:
start = 0
average_volume = sum(currency.data.volume[start: counter]) / 30
if average_volume > min_volume:
currencies_to_use.append(currency)
print(len(currencies_to_use))
for_weight_calculation = dict()
for currency in currencies_to_use:
for_weight_calculation[currency.currency] = 0
if index in currency.data.index:
for_weight_calculation[currency.currency] = currency.data[weight_name][index]
if numpy.isnan(for_weight_calculation[currency.currency]):
for_weight_calculation[currency.currency] = 0
weights_to_use = self.calculate_weights(for_weight_calculation, max_weight)
value = 0
for currency in currencies_to_use:
if index in currency.relative_data.index and numpy.isfinite(currency.relative_data.usd[index]):
value += currency.relative_data.usd[index] * weights_to_use[currency.currency]
weighted_return[index] = value
counter += 1
current = 100
chart_index = dict()
for key in sorted(weighted_return.keys()):
current = current * (1 + weighted_return[key])
chart_index[key] = current
pandas.Series(list(chart_index.values())).plot(ax=ax, label=calculation_name)
self.results[calculation_name] = list(chart_index.values())
return fig, ax
def calculate_weights(self, slots: dict, max_weight):
# print(slots)
weights = dict()
total = sum(list(slots.values()))
# In case there are no weights at all
if total == 0:
return slots
rest = 0
exclude = list()
for slot in slots:
weights[slot] = slots[slot] / total
if weights[slot] > max_weight:
rest += (weights[slot] - max_weight) * total
weights[slot] = max_weight
exclude.append(slot)
percentage_left = rest / total
# In case all weights are nan or 0
if rest == total:
return weights
while percentage_left > 0.0001:
rest = 0
current_total = 0
for slot in slots:
if slot not in exclude:
current_total += slots[slot]
if current_total == 0:
multiplier = 1 / (len(exclude) * max_weight)
for weight in weights:
weights[weight] *= multiplier
return weights
for slot in slots:
if slot not in exclude:
if weights[slot] + percentage_left * slots[slot] / current_total <= max_weight:
weights[slot] += percentage_left * slots[slot] / current_total
else:
exclude.append(slot)
rest += total * ((weights[slot] + percentage_left * slots[slot] / current_total) - max_weight)
weights[slot] = max_weight
percentage_left = rest / total
if not sum(weights.values()) <= 1.0001:
print(weights)
print(slots)
assert (sum(weights.values()) <= 1.0001)
return weights
def export(self):
with open("final-indices-2017-12-03-30-day-weighting-without-top-ten.csv", "w") as file:
writer = csv.writer(file, delimiter=",", lineterminator="\n")
columns = sorted(self.results.keys())
columns.remove("timestamp")
writer.writerow(["timestamp"] + columns)
for index in list(range(len(self.results["timestamp"]))):
print(index)
line = list()
columns = sorted(self.results.keys())
columns.remove("timestamp")
for key in ["timestamp"] + columns:
line.append(self.results[key][index])
writer.writerow(line)
PriceIndices().run()