-
Notifications
You must be signed in to change notification settings - Fork 5
/
gecko.py
182 lines (139 loc) · 4.3 KB
/
gecko.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
import json
import logging
import os
import pandas as pd
import requests
from requests.adapters import HTTPAdapter, Retry
import cache_db
API_ROOT = "https://pro-api.coingecko.com/api/v3"
CG_KEY = os.environ["CG_KEY"]
GT_ROOT = "https://api.geckoterminal.com/api/v2/"
GT_KEY = os.environ["GT_KEY"]
SESSION: requests.Session = None
def init():
global SESSION
SESSION = requests.Session()
SESSION.mount(
"http://",
HTTPAdapter(
max_retries=Retry(
total=5,
backoff_factor=0.1,
)
),
)
def get(*args, params: dict = {}):
path = "/".join(args)
def fetch():
url = "/".join((API_ROOT, path))
logging.info("%s %s", url, json.dumps(params))
return SESSION.get(
url,
params={**params, "x_cg_pro_api_key": CG_KEY},
timeout=10,
).json()
return cache_db.try_cache(path, params, fetch)
def get_gt(*args, params: dict = {}):
path = "/".join(args)
def fetch():
url = "/".join((GT_ROOT, path))
logging.info("%s %s", url, json.dumps(params))
return SESSION.get(
url,
params={**params, "partner_api_key": CG_KEY},
timeout=10,
).json()
return cache_db.try_cache(path, params, fetch)
def exchanges(dex):
data = [
{
"pair": ticker["coin_id"] + "<>" + ticker["target_coin_id"],
"volume": ticker["converted_volume"]["usd"],
}
for ticker in get("exchanges", dex)["tickers"]
]
df = pd.DataFrame(data)
df.set_index("pair", inplace=True)
df.index.name = "pair"
return df
def exchanges_multi(dex, n_item=2):
df = exchanges(dex)
if n_item > 1:
for i in range(n_item):
tmp_df = exchanges(dex)
for j in tmp_df.index:
if j not in df.index:
df.loc[j, "volume"] = tmp_df.loc[j, "volume"]
return df
def filter_tickers(ticker, dex):
result = get("coins", ticker, "tickers", params={"vs_currency": "usd"})
in_dex = False
for i in result["tickers"]:
if dex == i["market"]["identifier"]:
return True
return in_dex
def top_gainers():
result = get("coins", "top_gainers_losers", params={"vs_currency": "usd"})
df = pd.DataFrame()
gainers = result["top_gainers"]
for i in gainers:
df.loc[i["id"], "price"] = i["usd"]
return df
def new_listing():
result = get("coins", "list","new")
df = pd.DataFrame()
for i in result:
df.loc[i["id"], "price"] = 1
df.loc[i["id"], "activated_at"] = i["activated_at"]
return df
def market_chart(coin, *, days):
assert days in (1, 100)
chart = get(
"coins", coin, "market_chart", params={"vs_currency": "usd", "days": days}
)
if chart == {"error": "coin not found"}:
logging.info("coin not found for %s", coin)
chart = {
"prices": [],
"market_caps": [],
"total_volumes": [],
}
pr = pd.DataFrame(chart["prices"], columns=["ts", "price"])
mc = pd.DataFrame(chart["market_caps"], columns=["ts", "market_caps"])
tv = pd.DataFrame(chart["total_volumes"], columns=["ts", "total_volumes"])
for df in [pr, mc, tv]:
df["ts"] = pd.to_datetime(df["ts"], unit="ms")
df.set_index("ts", inplace=True)
df = pd.concat([pr, mc, tv], axis=1)
return df
def coin_return_intraday(coin, lag):
df = market_chart(coin, days=1)
current = df.index[-1]
prback = df["price"].asof(current - pd.Timedelta(hours=lag))
prcurrent = df["price"].iloc[-1]
return (prcurrent - prback) / prback
def price(coin):
return market_chart(coin, days=1)["price"][-1]
def query_coin(coin):
return get("coins", coin)
def query_coins_markets(coins):
return get(
"coins",
"markets",
params={
"ids": ",".join(sorted(coins)),
"vs_currency": "usd",
},
)
def simple_price_1d(coins):
return get(
"simple",
"price",
params={
"ids": ",".join(sorted(coins)),
"vs_currencies": "usd",
"include_24hr_change": "true",
},
)
def networks_tokens_pools(chain, contract_addr):
return get_gt("networks", chain, "tokens", contract_addr, "pools")