-
Notifications
You must be signed in to change notification settings - Fork 3
/
bci-comparison.py
176 lines (140 loc) · 7.46 KB
/
bci-comparison.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
import logging
import sys
import json
import argparse
import matplotlib.pyplot as plt
from BCI import BCI
logger = logging.getLogger('matplotlib')
logger.setLevel(logging.WARN)
logger.addHandler(logging.StreamHandler(sys.stdout))
logger = logging.getLogger('BCI')
logger.setLevel(logging.WARN)
logger.addHandler(logging.StreamHandler(sys.stdout))
logger = logging.getLogger('__main__')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(sys.stdout))
LOG = logging.getLogger(__name__)
GRAPH = False
EXPORT_CSV_RESULTS = False
EXPORT_FULL_RESULTS = False
FEE = 0.001
def parse_args() -> dict:
parser = argparse.ArgumentParser(description='Bitpanda Crypto Index Simulator')
parser.add_argument('--index', help = 'Size of the index', default = 5, type = int)
return vars(parser.parse_args())
if __name__ == "__main__":
args = parse_args()
indices = [args['index']]
rebalancings = [0, 60]
primary_volume_filters = [600000, 1000000, 1500000]
secondary_volume_filters = [1000000, 1500000, 2000000]
max_allocations = [0.2, 0.3, 0.35, 0.45, 0.5]
running_avg_volume_periods = [30]
primary_candidates = [3, 5, 8, 15]
offsets = [0, 3, 6, 9, 12, 15, 20, 30]
start_dt = "2017-07-01"
end_dt = "2020-11-01"
#indices = [10]
#rebalancings = [0]
#primary_volume_filters = [300000]
#secondary_volume_filters = [1000000]
#max_allocations = [0.5]
#running_avg_volume_periods = [30]
#primary_candidates = [3]
#offsets = [35]
#start_dt = "2018-06-01"
#end_dt = "2020-11-01"
with open("input_data_160101_201231.json", 'r') as file:
input_data = json.loads(file.read())
data = None
dates = None
data_by_coin = None
results = []
for index in indices:
for rebalancing in rebalancings:
for primary_volume_filter in primary_volume_filters:
for secondary_volume_filter in secondary_volume_filters:
if primary_volume_filter >= secondary_volume_filter:
continue
for max_allocation in max_allocations:
for running_avg_volume_period in running_avg_volume_periods:
for primary_candidate in primary_candidates:
if primary_candidate > index:
continue
for offset in offsets:
LOG.debug(f"Index: {index}, "
f"rebalancing: {rebalancing}, "
f"primary_volume_filter: {primary_volume_filter}, "
f"secondary_volume_filter: {secondary_volume_filter}, "
f"max_allocation: {max_allocation}, "
f"running_avg_volume_period: {running_avg_volume_period}, "
f"primary_candidate: {primary_candidate}, "
f"offset: {offset}")
try:
bci = BCI(
index_size = index,
rebalancing_period = rebalancing,
primary_usd_filtering = primary_volume_filter,
secondary_usd_filtering = secondary_volume_filter,
max_asset_allocation = max_allocation,
fee = FEE,
running_avg_volume_period = running_avg_volume_period,
index_candidate_size = index * 2,
primary_candidate_size = min(primary_candidate, index),
secondary_candidate_size = min(primary_candidate, index) + 5,
initial_funds = 1000,
offset = offset,
start_dt = start_dt,
end_dt = end_dt
)
# use previously calculated data to save initialization time
if data_by_coin is None:
bci.set_input_data(input_data)
else:
bci.data = data
bci.dates = dates
bci.data_by_coin = data_by_coin
[dates, baseline_values, index_values, fees] = bci.run()
results.append([
index,
rebalancing,
primary_volume_filter,
secondary_volume_filter,
max_allocation,
running_avg_volume_period,
primary_candidate,
offset,
dates, index_values, fees, baseline_values])
if data_by_coin is None:
data = bci.data
dates = bci.dates
data_by_coin = bci.data_by_coin
except Exception as e:
LOG.debug(e)
LOG.info(f"Best performing index configurations:")
for data in sorted(results, key = lambda x: x[9][-1]-x[10], reverse = True):
LOG.info(f"{start_dt}:{end_dt}:{data[:8]}:{data[9][-1]:,.2f}:{data[11][-1]:,.2f}:{data[10]:,.2f}:{data[9][-1]-data[10]:,.2f}:{max(data[9])-data[10]:,.2f}")
#LOG.info(f"Best performing baseline configurations:")
#for data in sorted(results, key = lambda x: x[11][-1], reverse = True):
# LOG.info(f"{data[:8]}:{data[9][-1]:.2f}:{data[11][-1]:.2f}:{data[10]:.2f}")
if EXPORT_FULL_RESULTS is True:
with open(f"results_{args['index']}_{start_dt}_{end_dt}.json", 'w') as file:
file.write(json.dumps(results))
if EXPORT_CSV_RESULTS is True:
with open(f"results_{args['index']}_{start_dt}_{end_dt}.csv", 'w') as file:
for data in results:
file.write(f"{';'.join(map(str, data[:8]))};{data[9][-1]};{data[11][-1]};{data[10]}\n")
if GRAPH is True:
dates = None
for data in results[-10:]:
dates = data[8]
plt.plot(data[8], data[9], label = str(data[:8]), linewidth = 0.7)
plt.xlabel('Date')
plt.xticks(list(filter(lambda x: x.split('-')[2] == '01' and int(x.split('-')[1]) % 3 == 0, dates)), rotation = 45, fontsize = 6)
plt.ylabel('Value (USD)')
plt.title(f'BCI {start_dt} - {end_dt}')
plt.grid(linestyle = '--', linewidth = 0.5)
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
fancybox=True, shadow=True, ncol=5)
plt.savefig(f"index_comparison_{args['index']}_{start_dt}_{end_dt}.svg", format = "svg")
#plt.show()