forked from TBMoonwalker/3cqsbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signals.py
264 lines (232 loc) · 9.27 KB
/
signals.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import math
import re
from functools import lru_cache, wraps
from time import monotonic_ns, sleep
from babel.numbers import format_currency
from dateutil.relativedelta import relativedelta as rd
from pycoingecko import CoinGeckoAPI
from tenacity import retry, wait_fixed
class Signals:
def __init__(self, logging):
self.logging = logging
# Credits goes to https://gist.github.com/Morreski/c1d08a3afa4040815eafd3891e16b945
def timed_lru_cache(
_func=None, *, seconds: int = 600, maxsize: int = 128, typed: bool = False
):
"""Extension of functools lru_cache with a timeout
Parameters:
seconds (int): Timeout in seconds to clear the WHOLE cache, default = 10 minutes
maxsize (int): Maximum Size of the Cache
typed (bool): Same value of different type will be a different entry
"""
def wrapper_cache(f):
f = lru_cache(maxsize=maxsize, typed=typed)(f)
f.delta = seconds * 10**9
f.expiration = monotonic_ns() + f.delta
@wraps(f)
def wrapped_f(*args, **kwargs):
if monotonic_ns() >= f.expiration:
f.cache_clear()
f.expiration = monotonic_ns() + f.delta
return f(*args, **kwargs)
wrapped_f.cache_info = f.cache_info
wrapped_f.cache_clear = f.cache_clear
return wrapped_f
# To allow decorator to be used without arguments
if _func is None:
return wrapper_cache
else:
return wrapper_cache(_func)
@staticmethod
@timed_lru_cache(seconds=10800, maxsize=None)
@retry(wait=wait_fixed(5))
def cgexchanges(exchange, id):
cg = CoinGeckoAPI()
try:
exchange = cg.get_exchanges_tickers_by_id(id=exchange, coin_ids=id)
except Exception as e:
raise IOError("Coingecko API error:" + e)
return exchange
@staticmethod
@timed_lru_cache(seconds=10800, maxsize=None)
def cgvalues(rank):
cg = CoinGeckoAPI()
market = []
if rank <= 250:
pages = 1
else:
pages = math.ceil(rank / 250)
for page in range(1, pages + 1):
page = cg.get_coins_markets(vs_currency="usd", page=page, per_page=250)
for entry in page:
market.append(entry)
return market
def topvolume(self, id, volume, exchange, market):
# Check if topcoin has enough volume
volume_btc = 0
if volume > 0:
volume_target = False
exchange = self.cgexchanges(exchange, id)
self.logging.debug(self.cgexchanges.cache_info())
for target in exchange["tickers"]:
converted_btc = format_currency(
target["converted_volume"]["btc"], "", locale="en_US"
)
converted_usd = format_currency(
target["converted_volume"]["usd"], "USD", locale="en_US"
)
btc_price = (
target["converted_volume"]["usd"]
/ target["converted_volume"]["btc"]
)
configured_usd = format_currency(
(volume * btc_price),
"USD",
locale="en_US",
)
if (
target["target"] == market
and target["converted_volume"]["btc"] >= volume
):
volume_target = True
volume_btc = target["converted_volume"]["btc"]
self.logging.info(
market
+ "_"
+ str(target["base"])
+ " daily trading volume is "
+ converted_btc
+ " BTC ("
+ converted_usd
+ ") and over the configured value of "
+ str(volume)
+ " BTC ("
+ configured_usd
+ ") on "
+ exchange["name"]
)
break
elif (
target["target"] == market
and target["converted_volume"]["btc"] < volume
):
volume_target = False
volume_btc = target["converted_volume"]["btc"]
self.logging.info(
market
+ "_"
+ str(target["base"])
+ " daily trading volume is "
+ converted_btc
+ " BTC ("
+ converted_usd
+ ") NOT passing the minimum daily BTC volume of "
+ str(volume)
+ " BTC ("
+ configured_usd
+ ") on "
+ exchange["name"]
)
break
else:
volume_target = False
volume_btc = 0
if volume_btc == 0:
if exchange["tickers"]:
self.logging.info(
market
+ "_"
+ target["base"]
+ " is not traded on "
+ exchange["name"]
)
else:
self.logging.info("Pair is not traded on " + exchange["name"])
else:
volume_target = True
return volume_target, volume_btc
def topcoin(self, pairs, rank, volume, exchange, trademarket, first_time):
market = self.cgvalues(rank)
self.logging.debug(self.cgvalues.cache_info())
self.logging.info(
"Applying CG's top coin filter settings: marketcap <= "
+ str(rank)
+ " with daily BTC volume >= "
+ str(volume)
+ " on "
+ str(exchange)
)
if isinstance(pairs, list):
self.logging.info(
str(len(pairs))
+ " symrank pair(s) BEFORE top coin filter: "
+ str(pairs)
)
pairlist = []
for pair in pairs:
for symbol in market:
coin = pair
if (
coin.lower() == symbol["symbol"]
and int(symbol["market_cap_rank"]) <= rank
):
self.logging.info(
str(pair)
+ " is ranked #"
+ str(symbol["market_cap_rank"])
+ " and has passed marketcap filter limit of top #"
+ str(rank)
)
# Prevent from being block for 30sec from too many API requests
if first_time:
sleep(2.2)
# Check if topcoin has enough volume
enough_volume, volume_btc = self.topvolume(
symbol["id"], volume, exchange, trademarket
)
if enough_volume:
pairlist.append((pair, volume_btc))
break
else:
pairlist = ""
coin = re.search("(\w+)_(\w+)", pairs).group(2)
for symbol in market:
if (
coin.lower() == symbol["symbol"]
and int(symbol["market_cap_rank"]) <= rank
):
self.logging.info(
str(pairs)
+ " is ranked #"
+ str(symbol["market_cap_rank"])
+ " and has passed marketcap filter limit of top #"
+ str(rank)
)
# Check if topcoin has enough volume
enough_volume, volume_btc = self.topvolume(
symbol["id"], volume, exchange, trademarket
)
if enough_volume:
pairlist = tuple([coin, volume_btc])
break
pairtuple_sorted = []
if not pairlist:
self.logging.info(str(pairs) + " not matching the topcoin filter criteria")
else:
if isinstance(pairlist, tuple):
pairtuple_sorted = pairlist
pairlist = trademarket + "_" + pairlist[0]
self.logging.info(str(pairlist) + " matching top coin filter criteria")
else:
pairtuple_sorted = sorted(pairlist, key=lambda x: x[1], reverse=True)
self.logging.info(
str(len(pairtuple_sorted))
+ " BTC volume sorted "
+ trademarket
+ " symrank pair(s) AFTER top coin filter: "
+ str(pairtuple_sorted)
)
pairlist = []
for i in range(len(pairtuple_sorted)):
pairlist.append(pairtuple_sorted[i][0])
return pairlist, pairtuple_sorted