forked from enarjord/passivbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspect_opt_results.py
More file actions
executable file
·217 lines (203 loc) · 8.79 KB
/
inspect_opt_results.py
File metadata and controls
executable file
·217 lines (203 loc) · 8.79 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
if "NOJIT" not in os.environ:
os.environ["NOJIT"] = "true"
import json
import pprint
import numpy as np
import argparse
import hjson
from procedures import load_live_config, dump_live_config, make_get_filepath
from pure_funcs import config_pretty_str, candidate_to_live_config
def main():
parser = argparse.ArgumentParser(prog="view conf", description="inspect conf")
parser.add_argument("results_fpath", type=str, help="path to results file")
parser.add_argument(
"-p",
"--PAD",
"--pad",
dest="PAD_max",
type=float,
required=False,
default=None,
help="max pa dist",
)
parser.add_argument(
"-l",
"--profit_loss_ratio",
dest="profit_loss_ratio_max",
type=float,
required=False,
default=None,
help="max profit loss ratio",
)
parser.add_argument(
"-i", "--index", dest="index", type=int, required=False, default=1, help="best conf index"
)
parser.add_argument(
"-sf",
dest="score_formula",
type=str,
required=False,
default="adgrealizedPADstd",
help="choices: [adgPADstd, adg_mean, adg_min, adgPADmean, adgrealizedPADmean, adgrealizedPADstd]",
)
parser.add_argument(
"-d",
"--dump_live_config",
action="store_true",
help="dump config",
)
args = parser.parse_args()
if args.PAD_max is None:
opt_config = hjson.load(open("configs/optimize/harmony_search.hjson"))
PAD_max_long = opt_config["maximum_pa_distance_std_long"]
PAD_max_short = opt_config["maximum_pa_distance_std_short"]
else:
PAD_max_long = args.PAD_max
PAD_max_short = args.PAD_max
if args.profit_loss_ratio_max is None:
opt_config = hjson.load(open("configs/optimize/harmony_search.hjson"))
maximum_loss_profit_ratio_long = opt_config["maximum_loss_profit_ratio_long"]
maximum_loss_profit_ratio_short = opt_config["maximum_loss_profit_ratio_short"]
else:
maximum_loss_profit_ratio_long = args.profit_loss_ratio_max
maximum_loss_profit_ratio_short = args.profit_loss_ratio_max
keys_ = [
"adg",
"adg_realized_per_exposure",
"pa_distance_mean",
"pa_distance_std",
"loss_profit_ratio",
"profit_sum",
"loss_sum",
]
keys = [k + "_long" for k in keys_] + [k + "_short" for k in keys_]
with open(args.results_fpath) as f:
results = []
for x in f.readlines():
j = json.loads(x)
rsl = {
"results": {
s: {k: j["results"][s][k] for k in keys if k in j["results"][s]}
for s in j["results"]
if s != "config_no"
}
}
rsl["results"]["config_no"] = j["results"]["config_no"]
rsl["config"] = j["config"]
results.append(rsl)
# results = [{k:json.loads(x)} for x in f.readlines()]
print(
"n results",
len(results),
f"score formula: {args.score_formula}, PAD max:",
[PAD_max_long, PAD_max_short],
f"loss_profit_ratio max:",
[maximum_loss_profit_ratio_long, maximum_loss_profit_ratio_short],
)
best_config = {}
adgs_sides = {"long": {}, "short": {}}
PAD_max = {"long": PAD_max_long, "short": PAD_max_short}
maximum_loss_profit_ratio = {
"long": maximum_loss_profit_ratio_long,
"short": maximum_loss_profit_ratio_short,
}
for side in ["long", "short"]:
stats = []
for r in results:
adgs, adgs_realized, PAD_stds, PAD_means, loss_profit_ratios = [], [], [], [], []
for s in (rs := r["results"]):
try:
adgs.append(rs[s][f"adg_{side}"])
adgs_realized.append(rs[s][f"adg_realized_per_exposure_{side}"])
PAD_stds.append(rs[s][f"pa_distance_std_{side}"])
PAD_means.append(rs[s][f"pa_distance_mean_{side}"])
if f"loss_profit_ratio_{side}" in rs[s]:
loss_profit_ratios.append(rs[s][f"loss_profit_ratio_{side}"])
else:
# for backwards compatibility
lpr = (
(abs(rs[s][f"loss_sum_{side}"]) / rs[s][f"profit_sum_{side}"])
if rs[s][f"profit_sum_{side}"]
else 1.0
)
rs[s][f"loss_profit_ratio_{side}"] = lpr
loss_profit_ratios.append(lpr)
except Exception as e:
pass
adg_mean = np.mean(adgs)
adg_realized_mean = np.mean(adgs_realized)
PAD_std_mean_raw = np.mean(PAD_stds)
PAD_std_mean = np.mean([max(PAD_max[side], x) for x in PAD_stds])
PAD_mean_mean_raw = np.mean(PAD_means)
PAD_mean_mean = np.mean([max(PAD_max[side], x) for x in PAD_means])
loss_profit_ratio_mean = np.mean(
[max(maximum_loss_profit_ratio[side], x) for x in loss_profit_ratios]
)
loss_profit_ratio_mean_raw = np.mean(loss_profit_ratios)
if args.score_formula.lower() == "adgpadstd":
score = adg_mean / max(PAD_max[side], PAD_std_mean)
elif args.score_formula.lower() == "adg_mean":
score = adg_mean
elif args.score_formula.lower() == "adg_min":
score = min(adgs)
elif args.score_formula.lower() == "adgpadmean":
score = adg_mean * min(1, PAD_max[side] / PAD_mean_mean)
elif args.score_formula.lower() == "adgrealizedpadmean":
score = adg_realized_mean / max(PAD_max[side], PAD_mean_mean)
elif args.score_formula.lower() == "adgrealizedpadstd":
score = adg_realized_mean / max(PAD_max[side], PAD_std_mean)
else:
raise Exception("unknown score formula")
score *= maximum_loss_profit_ratio[side] / max(
loss_profit_ratio_mean, maximum_loss_profit_ratio[side]
)
stats.append(
{
"config": r["config"],
"adg_mean": adg_mean,
"adg_realized_mean": adg_realized_mean,
"PAD_std_mean": PAD_std_mean,
"PAD_std_mean_raw": PAD_std_mean_raw,
"PAD_mean_mean": PAD_mean_mean,
"PAD_mean_mean_raw": PAD_mean_mean_raw,
"loss_profit_ratio_mean_raw": loss_profit_ratio_mean_raw,
"score": score,
"config_no": r["results"]["config_no"],
}
)
ss = sorted(stats, key=lambda x: x["score"])
bc = ss[-args.index]
best_config[side] = bc["config"][side]
for r in results:
if r["results"]["config_no"] == bc["config_no"]:
rs = r["results"]
syms = [s for s in rs if "config" not in s]
print(f"results {side} best config no {bc['config_no']}")
print("symbol adg PADmean PADstd lp_ratio score")
for s in sorted(syms, key=lambda x: rs[x][f"adg_realized_per_exposure_{side}"]):
adgs_sides[side][s] = rs[s][f"adg_realized_per_exposure_{side}"]
print(
f"{s: <20} {rs[s][f'adg_realized_per_exposure_{side}']:.6f} "
+ f"{rs[s][f'pa_distance_std_{side}']:.6f} {rs[s][f'pa_distance_mean_{side}']:.6f} "
+ f"{rs[s][f'loss_profit_ratio_{side}']:.6f} "
+ f"{bc['score']:.6f}"
)
print(
f"{'means': <20} {bc['adg_realized_mean']:.6f} "
+ f"{bc['PAD_std_mean_raw']:.6f} "
+ f"{bc['PAD_mean_mean_raw']:.6f} "
+ f"{bc['loss_profit_ratio_mean_raw']:.6f} "
)
live_config = candidate_to_live_config(best_config)
if args.dump_live_config:
lc_fpath = make_get_filepath(f"{args.results_fpath.replace('.txt', '_best_config.json')}")
print(f"dump_live_config {lc_fpath}")
dump_live_config(live_config, lc_fpath)
adgs_sums = {s: adgs_sides["long"][s] + adgs_sides["short"][s] for s in adgs_sides["long"]}
print("\nsum adgs")
for k, v in sorted(adgs_sums.items(), key=lambda x: x[1]):
print(f"{k: <12} {v:.6f}")
print(config_pretty_str(live_config))
if __name__ == "__main__":
main()