-
Notifications
You must be signed in to change notification settings - Fork 7
/
coinanalyze.py
392 lines (318 loc) · 10.7 KB
/
coinanalyze.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import time
from datetime import datetime, date, timedelta
import urllib3
from coinmarketcap import coinmarketcap
from binance import binance
from coin import coin
from trade import trade
import numpy as np
import importlib
import sys
importlib.reload(sys)
#reload(sys)
#### Send To Coin ANalyzer Channel on Telegram
def sendMessageTelegram(msg):
#We replace %n by %0A for Telegram
txt = msg.replace("\n", "%0A")
url = "https://api.telegram.org/bot123456789:XXXXXXXXXXXXXXXXx/sendMessage?parse_mode=Markdown&chat_id=-123456789&text=" + txt
urllib3.disable_warnings()
http = urllib3.PoolManager()
r = http.request('GET', url)
return 0
#### Return text for market trend
def printcheckmarket(market, checkmarket):
res = ""
res = "* Total marketcap : " + str(market.get_marketcap()) + "*\n"
if checkmarket == 0:
res = res + "* Normal Market : (" + str(market.get_trend()) + "%)*"
else:
if checkmarket == -1:
if market.get_trend() > 3:
res = res + "* /!\ /!\ REALLY BULLISH Market /!\ /!\ >> (" + str(market.get_trend()) + "%)*"
else:
res = res + "* BULLISH Market : (" + str(market.get_trend()) + "%)*"
else:
if checkmarket == 1:
if market.get_trend() < -3:
res = res + "* /!\ /!\ REALLY BEARISH Market /!\ /!\ >> (" + str(market.get_trend()) + "%)*"
else:
res = res + "* BEARISH Market : (" + str(market.get_trend()) + "%)*"
return res
#### Return text for BTC domination
def printbtcdominance(market, checkmarket):
res = ""
if checkmarket == 0:
res = "* BTC dominance isn't moving : " + str(market.get_bitcoin_percentage_of_market_cap()) + "%*"
else:
if checkmarket == 1:
res = "* BTC dominance on fire : +" + str(market.get_bitcoin_percentage_of_market_cap()) + "%*"
else:
if checkmarket == -1:
res = "* BTC dominance dumping : " + str(market.get_bitcoin_percentage_of_market_cap()) + "%*"
return res
#### Return Coin with 1h price change >5%
def printPumpDump(pump,dump):
msg = ""
if len(pump) != 0:
msg = "* PUMP price (> 5%) : *\n"
for v in pump:
if float(v['percent_change_1h']) > 0:
msg = msg + "\t* " + v['symbol'] + ' : +' + v['percent_change_1h'] + "%*\n"
if len(dump) != 0:
if msg != "":
msg = msg + "\n"
msg = msg + "* DUMP price (> 5%) :*\n"
for v in dump:
if float(v['percent_change_1h']) < 0:
msg = msg + "\t* " + v['symbol'] + ' : ' + v['percent_change_1h'] + "%*\n"
return msg
#### Return Coin with Rank change
## TODO : previous RANK
def printRankChange(rank,oldrank):
msg = ""
if len(rank) != 0:
msg = "* RANK up :*\n"
for v in rank:
msg = msg + "\t" + v['symbol'] + ' - ' + v['rank'] + "\n"
#msg = msg + "\t" + v[0] + ' - ' + v[2] + " >> " + v[1] + "\n"
return msg
#### Return message for new coin on exchange
def printNewSymbol(symbol):
msg = ""
if len(symbol) != 0:
msg = "* New Symbol on Binance :*\n"
for s in symbol:
msg = msg + "\t" + s + "\n"
return msg
#Return MACD Alert msg
def printMACDAlert(symbol, macd, period, price):
msg = "* MACD Signal (" + period +")*\n"
if macd == 1:
msg = msg + symbol + " Bullish signal on Binance at " + price + " sat\n"
else:
msg = msg + symbol + " Bearish signal on Binance at " + price + " sat\n"
return msg
#Return RSI Alert msg
def printRSIAlert(symbol, rsi, period):
msg = "* RSI Signal (" + period +")*\n"
if rsi == 1:
msg = msg + symbol + " Time to BUY on Binance\n"
else:
msg = msg + symbol + " Time to SELL on Binance\n"
return msg
#Return RSI Divergence Alert msg
def printRSIDIVAlert(symbol, rsidiv, period, price):
msg = "* RSI Divergence Signal (" + period +")*\n"
if rsidiv == -1:
msg = msg + symbol + " SELL Signal on Binance at " + price + " sat \n"
return msg
### Return nightou alert
def printNightouAlert(symbol, period, price):
msg = "* /!\\ Nightou Alert /!\\ (" + period +")* \n"
msg = msg + "\t" + symbol + " Good entry at " + price + " sat ! \n"
return msg
#Return Alert msg
def printAlert(strategy, symbol, ema, period, price):
msg = "* " + strategy + " Signal (" + period +")*\n"
if ema == 1:
msg = msg + symbol + " BUY Signal on Binance at " + price + " sat \n"
else:
msg = msg + symbol + " SELL Signal on Binance at " + price + " sat \n"
return msg
######## MAIN #####
def main():
try:
print("\n>> CoinAnalyze v0.7 by cloud <<\n\n")
sendMessageTelegram("Coin Analyzer Bot v0.7 Started")
##### TRADE MODULE#######
trading = 0
tr = trade()
if trading == 1:
print("Trading module : On")
sendMessageTelegram("Trading module : On")
end = 0
else :
print("Trading module : Off")
sendMessageTelegram("Trading module : Off")
#########################
#We initialize the different timer
timer1hnext = (datetime.now() + timedelta(seconds=3600)).hour
timer20mnnext = (datetime.now() + timedelta(seconds=60))
while (int(str(timer20mnnext.minute)) % 20) != 0:
timer20mnnext = timer20mnnext + timedelta(seconds=60)
market = coinmarketcap()
binancecoin = binance()
listcoin = []
ticker = {}
ticker = binancecoin.checkTicker()
symbol = binancecoin.getListSymbol(ticker)
#We create an object by coin
for c in symbol:
coinC = coin(c)
listcoin.append(coinC)
while True:
time.sleep(1)
timer = datetime.now()
####### EVERY 1H ###############
if timer.hour == timer1hnext:
print(timer)
if trading == 1:
msg = tr.summaryTrade()
print(msg)
sendMessageTelegram(msg)
timer1hnext = (datetime.now() + timedelta(seconds=3600)).hour
### We check General coinmarketcap every 60mn
market.checkGlobal()
checkmarket = market.analyze_total_market_cap_eur()
#Analyze Market
msg = ""
msg = msg + printcheckmarket(market, checkmarket)
msg = msg + "\n"
checkmarket = market.analyze_bitcoin_percentage_of_market_cap()
msg = msg + printbtcdominance(market, checkmarket)
print(msg)
sendMessageTelegram(msg)
msg = ""
####### EVERY 20mn ###############
## New Symbol check
timer20mn = datetime.now()
period = ['1h', '4h', '1d']
if ((int(timer20mn.minute) % 20) == 0) and (timer20mnnext.minute == timer20mn.minute):
#if 1 == 1:
if trading == 1:
period = ['1h', '4h', '1d']
timer20mnnext = (datetime.now() + timedelta(seconds=12000))
####### ACTION ###################
for p in period:
msg = ""
#On récupère les infos de chaque coin sur une période de 252 et on les ajoute à chaque coin pour gérer les alertes
listAllValues = []
listAllValues = binancecoin.checkAllInfoValues(p)
for c in listAllValues:
for x in listcoin:
end, msg = tr.checkEndTrade()
if end == 1:
print(msg)
sendMessageTelegram(msg)
trading = 0
period = ['4h', '1d']
if c['symbol'] == x.getSymbol():
x.setValues(c)
if trading == 1:
stoploss = tr.stopLoss(x)
if stoploss == 1:
msg = "STOP LOSS (5%) for " + x.getSymbol()
print(msg)
sendMessageTelegram(msg)
if p != '1h':
#RSI must be the first test to set RSI for the PRS test
#RSI alert check
rsi = x.rsiAlert(p)
if rsi != 0:
msg = printRSIAlert(x.getSymbol(), rsi, p)
print(msg)
#sendMessageTelegram(msg)
msg = ""
#RSI DIV Alert
rsidiv, price = x.rsiDivAlert(p)
if rsidiv != 0:
msg = msg + printRSIDIVAlert(x.getSymbol(), rsidiv, p, price)
print(msg)
sendMessageTelegram(msg)
msg = ""
#if trading == 1:
# restrade,msg = tr.buysell(rsidiv, x.getSymbol(), price)
# print msg
# sendMessageTelegram(msg)
#MACD alert check
macd, price = x.macdAlert(p)
if macd != 0:
msg = printMACDAlert(x.getSymbol(), macd, p, price)
print(msg)
#sendMessageTelegram(msg)
msg = ""
#PRS Alert
prs, price = x.prsAlert(p)
if prs != 0:
msg = printAlert("PRS", x.getSymbol(), prs, p, price)
print(msg)
#sendMessageTelegram(msg)
msg = ""
#if trading == 1:
# restrade,msg = tr.buysell(prs, x.getSymbol(), price)
# print msg
# sendMessageTelegram(msg)
#Nightou Alert
nightou, price = x.nightouAlert(p)
if nightou != 0:
msg = msg + printNightouAlert(x.getSymbol(), p, price)
print(msg)
sendMessageTelegram(msg)
msg = ""
if trading == 1:
restrade,msg = tr.buysell(nightou, x.getSymbol(), price)
print(msg)
sendMessageTelegram(msg)
#ADX Alert
adx, price = x.adxAlert(p)
if adx != 0:
msg = msg + printAlert("ADX", x.getSymbol(), adx, p, price)
print(msg)
#sendMessageTelegram(msg)
msg = ""
#if trading == 1:
# restrade,msg = tr.buysell(adx, x.getSymbol(), price)
# print msg
# sendMessageTelegram(msg)
#CCI Alert
cci, price = x.cciAlert(p)
if cci != 0:
msg = msg + printAlert("CCI", x.getSymbol(), cci, p, price)
print(msg)
#sendMessageTelegram(msg)
msg = ""
#if trading == 1:
# restrade,msg = tr.buysell(cci, x.getSymbol(), price)
# print msg
# sendMessageTelegram(msg)
#CLOUD Alert
cloud, price = x.cloudStrategy(p)
if cloud != 0:
msg = msg + printAlert("CLOUD", x.getSymbol(), cloud, p, price)
print(msg)
sendMessageTelegram(msg)
msg = ""
if trading == 1:
restrade,msg = tr.buysell(cloud, x.getSymbol(), price)
print(msg)
sendMessageTelegram(msg)
#EMA Alert
ema, price = x.emaStrategy(p)
if ema != 0:
msg = msg + printAlert("EMA", x.getSymbol(), ema, p, price)
print(msg)
sendMessageTelegram(msg)
msg = ""
if trading == 1:
restrade,msg = tr.buysell(ema, x.getSymbol(), price)
print(msg)
sendMessageTelegram(msg)
####### NEW SYMBOL #########
ticker = binancecoin.checkTicker()
newsymbol = []
newsymbol = binancecoin.getNewSymbol()
for c in newsymbol:
coinC = coin(c)
listcoin.append(coinC)
msg = printNewSymbol(newsymbol)
if msg != "":
sendMessageTelegram(msg)
print(msg)
except (KeyboardInterrupt, SystemExit):
sendMessageTelegram("Maintenance, I'll be back soon")
return 0
if __name__ == '__main__':
main()