-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_random_universes.py
125 lines (99 loc) · 3.03 KB
/
create_random_universes.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
from typing import List
from trading_gym.envs.data_loader import CryptoData
import numpy as np
from datetime import datetime
from tqdm import tqdm
def get_random_universes(
n_symbols: int, n_universes: int, start: datetime, end: datetime
) -> List[List[str]]:
"""
Get random universes for evaluating the agents.
Parameters
----------
n_symbols: int
Number of symbols in each universe.
n_universes: int
Number of universes to generate.
start: datetime
Start date.
end: datetime
End date.
Returns
-------
random_universes: List[List[str]]
List of random universes.
"""
cd = CryptoData()
supported_symbols = cd.supported_symbols
symbols_filtered = [
symbol
for symbol in supported_symbols
if (
symbol.endswith("USDT")
or symbol.endswith("BUSD")
or symbol.endswith("USDC")
)
and not symbol.startswith("USDT")
and not symbol.startswith("USDS")
and not symbol.startswith("BUSD")
and not symbol.startswith("USDC")
and not symbol.startswith("TUSD")
and not symbol.startswith("DAI")
and not symbol.startswith("PAX")
and not symbol.startswith("EUR")
and not symbol.startswith("MDX")
and symbol
not in [
"UNIDOWNUSDT",
"LTCDOWNUSDT",
"SUSHIDOWNUSDT",
"ETHDOWNUSDT",
"BNBDOWNUSDT",
"XLMDOWNUSDT",
"SXPDOWNUSDT",
"TRXDOWNUSDT",
"YFIDOWNUSDT",
"XTZDOWNUSDT",
"AAVEDOWNUSDT",
"DOTDOWNUSDT",
"LINKDOWNUSDT",
"ADADOWNUSDT",
"XRPDOWNUSDT",
"COCOSUSDT",
]
]
symbols = {}
for symbol in symbols_filtered:
if "USDT" in symbol:
quote = "USDT"
elif "BUSD" in symbol:
quote = "BUSD"
elif "USDC" in symbol:
quote = "USDC"
else:
continue
s = symbol.replace(quote, "")
if s in symbols:
continue
else:
symbols[s] = symbol
symbols = list(symbols.values())
print("Downloading data")
assets_data = cd.get_data(start=start, end=end, universe=symbols)
symbols = list(filter(lambda s: s in assets_data["close"].columns, symbols))
random_universes = []
print("Creating universes")
for _ in tqdm(range(n_universes)):
universe = list(np.random.choice(symbols, size=n_symbols, replace=False))
universe_data = {}
for k in assets_data:
universe_data[k] = assets_data[k].loc[:, universe].dropna(how="all", axis=1)
random_universes.append(universe_data)
print("Done")
return random_universes
if __name__ == "__main__":
import pickle
start = datetime(2019, 5, 1)
end = datetime(2021, 9, 30)
random_universes = get_random_universes(50, 100, start, end)
pickle.dump(random_universes, open("./random_universes.pickle", "wb"))