-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollect.py
More file actions
85 lines (64 loc) · 2.26 KB
/
collect.py
File metadata and controls
85 lines (64 loc) · 2.26 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
import argparse
import os
import sys
import time
import brain
from alpha_db import AlphaDB
def fetch_results(db: AlphaDB, cli: brain.Client):
simulations = db.simulations()
alphas = db.alphas()
succ, fail, wait_sec = 0, 0, 1.0
def print_info(msg: str, file=sys.stdout):
print(
f"[\33[0;32m{succ:0>4}\033[0m|\33[0;31m{fail:0>4}\033[0m] {msg}", file=file
)
while True:
is_empty = True
for row in simulations.filter(status="SIMULATING"):
try:
is_empty = False
alpha = cli.simulation_result(row["simulation_id"]).wait().detail()
alphas.save(alpha)
simulations.complete(row["id"], alpha["id"])
succ += 1
print_info(f"New alpha: {alpha['id']}")
except brain.BrainError as e:
simulations.error(row["id"])
fail += 1
print_info(
f"Simulation: {row['simulation_id']}, Error: {str(e)}", sys.stderr
)
if is_empty:
wait_sec = wait_sec * 2 if wait_sec < 5.0 else wait_sec
else:
wait_sec = wait_sec / 3.0 if wait_sec > 1.0 else 1.0
print_info(f"Rescan after {wait_sec:.2f} secs.")
time.sleep(wait_sec)
print_info("Rescan...")
def main():
parser = argparse.ArgumentParser(description="Get simulated alphas from Brain API.")
parser.add_argument(
"--user",
default=os.environ.get("WQB_USER"),
help="Brain API user. use env WQB_USER if not given.",
)
parser.add_argument(
"--password",
default=os.environ.get("WQB_PASS"),
help="Brain API password. use env WQB_PASS if not given.",
)
parser.add_argument(
"--db", default="alpha.db", help="sqlite db that store all alphas."
)
parser.add_argument(
"--limit", default=0, type=int, help="max number of alphas to get."
)
args = parser.parse_args(sys.argv[1:])
if not args.user or not args.password:
print("no user or password found.", file=sys.stderr)
sys.exit(1)
cli = brain.Client(args.user, args.password)
with AlphaDB(args.db) as db:
fetch_results(db, cli)
if __name__ == "__main__":
main()