-
Notifications
You must be signed in to change notification settings - Fork 18
/
FTX_Ichimoku_Scanner_With_Chikou_MultiThreaded_MultiTimeFrame.py
724 lines (578 loc) · 34.6 KB
/
FTX_Ichimoku_Scanner_With_Chikou_MultiThreaded_MultiTimeFrame.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
import glob, os
from datetime import datetime
from datetime import timedelta
from binance.client import Client
from binance.enums import HistoricalKlinesType
import binance
import pandas as pd
import requests
import threading
import time
import ta
import math
import ftx
from enum import Enum
class ScanType(Enum):
UP = 0
DOWN = 1
client = ftx.FtxClient(
api_key='',
api_secret='',
subaccount_name=''
)
# Set this variable to ScanType.UP for scanning uptrend assets or to ScanType.DOWN for scanning downtrend assets
scan_type = ScanType.UP
# Set this variable to False to scan in spot mode
scan_futures = False
scan_spot = True
def log_to_results(str_to_log):
fr = open("results.txt", "a")
fr.write(str_to_log + "\n")
fr.close()
def log_to_errors(str_to_log):
fr = open("errors.txt", "a")
fr.write(str_to_log + "\n")
fr.close()
def log_to_trades(str_to_log):
fr = open("trades.txt", "a")
fr.write(str_to_log + "\n")
fr.close()
def log_to_evol(str_to_log):
fr = open("evol.txt", "a")
fr.write(str_to_log + "\n")
fr.close()
if os.path.exists("results.txt"):
os.remove("results.txt")
if os.path.exists("errors.txt"):
os.remove("errors.txt")
if os.path.exists("trades.txt"):
os.remove("trades.txt")
if os.path.exists("evol.txt"):
os.remove("evol.txt")
for fg in glob.glob("CS_*.txt"):
os.remove(fg)
print("Scanning type =", scan_type.name)
# log_to_results("Scanning type = " + scan_type.name)
results_count = 0
stop_thread = False
list_results = []
array_futures = []
# Set the timeframe to scan on the following line
interval_for_klinesT = Client.KLINE_INTERVAL_15MINUTE
print("Scanning timeframe =", str(interval_for_klinesT))
days_ago_for_klinest = "80 day ago UTC" # for daily download by default
if interval_for_klinesT == Client.KLINE_INTERVAL_1MINUTE:
days_ago_for_klinest = "80 minute ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_3MINUTE:
days_ago_for_klinest = "240 minute ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_5MINUTE:
days_ago_for_klinest = "400 minute ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_15MINUTE:
days_ago_for_klinest = "1200 minute ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_30MINUTE:
days_ago_for_klinest = "2400 minute ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_1HOUR:
days_ago_for_klinest = "80 hour ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_2HOUR:
days_ago_for_klinest = "160 hour ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_4HOUR:
days_ago_for_klinest = "320 hour ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_6HOUR:
days_ago_for_klinest = "480 hour ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_8HOUR:
days_ago_for_klinest = "640 hour ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_12HOUR:
days_ago_for_klinest = "960 hour ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_1DAY:
days_ago_for_klinest = "80 day ago UTC"
elif interval_for_klinesT == Client.KLINE_INTERVAL_3DAY:
days_ago_for_klinest = "240 day ago UTC"
dict_evol = {}
new_results_found = False
def execute_code(symbol):
global results_count, dict_evol
global new_results_found
symbol_type = "n/a"
HISTORY_RESOLUTION_MINUTE = 60
HISTORY_RESOLUTION_3MINUTE = 60 * 3
HISTORY_RESOLUTION_5MINUTE = 60 * 5
HISTORY_RESOLUTION_15MINUTE = 60 * 15
HISTORY_RESOLUTION_30MINUTE = 60 * 30
HISTORY_RESOLUTION_HOUR = 60 * 60
HISTORY_RESOLUTION_4HOUR = 60 * 60 * 4
HISTORY_RESOLUTION_DAY = 60 * 60 * 24
if interval_for_klinesT == Client.KLINE_INTERVAL_1MINUTE:
history_resolution = HISTORY_RESOLUTION_MINUTE
elif interval_for_klinesT == Client.KLINE_INTERVAL_5MINUTE:
history_resolution = HISTORY_RESOLUTION_5MINUTE
elif interval_for_klinesT == Client.KLINE_INTERVAL_15MINUTE:
history_resolution = HISTORY_RESOLUTION_15MINUTE
elif interval_for_klinesT == Client.KLINE_INTERVAL_1HOUR:
history_resolution = HISTORY_RESOLUTION_HOUR
elif interval_for_klinesT == Client.KLINE_INTERVAL_4HOUR:
history_resolution = HISTORY_RESOLUTION_4HOUR
elif interval_for_klinesT == Client.KLINE_INTERVAL_1DAY:
history_resolution = HISTORY_RESOLUTION_DAY
# history_resolution = history_resolution # define the resolution used for the scan here
delta_time = 0
if history_resolution == HISTORY_RESOLUTION_MINUTE: # using this resolution seems not ok, must be improved
delta_time = 1
if history_resolution == HISTORY_RESOLUTION_3MINUTE: # using this resolution seems not ok, must be improved
delta_time = 3
elif history_resolution == HISTORY_RESOLUTION_5MINUTE: # using this resolution seems not ok, must be improved
delta_time = 5
elif history_resolution == HISTORY_RESOLUTION_15MINUTE:
delta_time = 15
elif history_resolution == HISTORY_RESOLUTION_HOUR:
delta_time = 60
elif history_resolution == HISTORY_RESOLUTION_4HOUR:
delta_time = 60 * 4
elif history_resolution == HISTORY_RESOLUTION_DAY:
delta_time = 60 * 24
try:
data = client.get_historical_data(market_name=symbol, resolution=history_resolution, # 60min * 60sec = 3600 sec
limit=1000000, start_time=float(round(time.time())) - delta_time * 5000,
# 1000*3600 for resolution=3600*24 (daily) # 3600*3 for resolution=60*5 (5min) # 3600*3*15 for 60*15 # 3600 * 3 * 15 * 2 for 60*60
end_time=float(round(time.time())))
#if interval_for_klinesT != Client.KLINE_INTERVAL_1DAY:
dataDaily = client.get_historical_data(market_name=symbol, resolution=HISTORY_RESOLUTION_DAY * 7,
# 60min * 60sec = 3600 sec
limit=1000000,
start_time=float(round(time.time())) - 60 * 24 * 5000,
# 1000*3600 for resolution=3600*24 (daily) # 3600*3 for resolution=60*5 (5min) # 3600*3*15 for 60*15 # 3600 * 3 * 15 * 2 for 60*60
end_time=float(round(time.time())))
dataWeekly = client.get_historical_data(market_name=symbol, resolution=HISTORY_RESOLUTION_DAY * 7,
# 60min * 60sec = 3600 sec
limit=1000000,
start_time=float(round(time.time())) - 60 * 24 * 7 * 5000,
# 1000*3600 for resolution=3600*24 (daily) # 3600*3 for resolution=60*5 (5min) # 3600*3*15 for 60*15 # 3600 * 3 * 15 * 2 for 60*60
end_time=float(round(time.time())))
# print(" (ok)")
dframe = pd.DataFrame(data)
dframeDaily = pd.DataFrame(dataDaily)
dframeWeekly = pd.DataFrame(dataWeekly)
try:
dframe['close'] = pd.to_numeric(dframe['close'])
dframeDaily['close'] = pd.to_numeric(dframeDaily['close'])
dframeWeekly['close'] = pd.to_numeric(dframeWeekly['close'])
except:
print(symbol, "ERREUR DFRAME CLOSE")
# print(dframe)
return
try:
dframe['high'] = pd.to_numeric(dframe['high'])
dframeDaily['high'] = pd.to_numeric(dframeDaily['high'])
dframeWeekly['high'] = pd.to_numeric(dframeWeekly['high'])
except:
# print(symbol, "ERREUR DFRAME HIGH")
return
try:
dframe['low'] = pd.to_numeric(dframe['low'])
dframeDaily['low'] = pd.to_numeric(dframeDaily['low'])
dframeWeekly['low'] = pd.to_numeric(dframeWeekly['low'])
except:
# print(symbol, "ERREUR DFRAME LOW")
return
try:
dframe['open'] = pd.to_numeric(dframe['open'])
dframeDaily['open'] = pd.to_numeric(dframeDaily['open'])
dframeWeekly['open'] = pd.to_numeric(dframeWeekly['open'])
except:
# print(symbol, "ERREUR DFRAME OPEN")
return
dframe = dframe.set_index(dframe['startTime'])
dframeDaily = dframeDaily.set_index(dframeDaily['startTime'])
dframeWeekly = dframeWeekly.set_index(dframeWeekly['startTime'])
# dframe.index = pd.to_datetime(dframe.index, unit='ms')
except requests.exceptions.HTTPError:
print("Erreur (HTTPError) tentative obtention données historiques pour " + symbol)
log_to_errors("Erreur (HTTPError) tentative obtention données historiques pour " + symbol)
return
except requests.exceptions.ConnectionError:
print("Erreur (ConnectionError) tentative obtention données historiques pour " + symbol)
log_to_errors("Erreur (ConnectionError) tentative obtention données historiques pour " + symbol)
return
except binance.exceptions.BinanceAPIException:
# in case the symbol does not exist in futures then this exception is thrown
print(symbol + " Does not exist")
return
# a = time.time()
# my_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(a))
# my_time_2 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(a - delta_time))
# dframe = pd.DataFrame(data)
# dframe['time'] = pd.to_datetime(dframe['time'], unit='ms')
# print(dframe)
try:
dframe['ICH_SSA'] = ta.trend.ichimoku_a(dframe['high'], dframe['low'], window1=9, window2=26).shift(25)
dframe['ICH_SSB'] = ta.trend.ichimoku_b(dframe['high'], dframe['low'], window2=26, window3=52).shift(25)
dframe['ICH_KS'] = ta.trend.ichimoku_base_line(dframe['high'], dframe['low'])
dframe['ICH_TS'] = ta.trend.ichimoku_conversion_line(dframe['high'], dframe['low'])
dframe['ICH_CS'] = dframe['close'].shift(-25)
dframeDaily['ICH_SSA'] = ta.trend.ichimoku_a(dframeDaily['high'], dframeDaily['low'], window1=9,window2=26).shift(25)
dframeDaily['ICH_SSB'] = ta.trend.ichimoku_b(dframeDaily['high'], dframeDaily['low'], window2=26,window3=52).shift(25)
dframeDaily['ICH_KS'] = ta.trend.ichimoku_base_line(dframeDaily['high'], dframeDaily['low'])
dframeDaily['ICH_TS'] = ta.trend.ichimoku_conversion_line(dframeDaily['high'], dframeDaily['low'])
dframeDaily['ICH_CS'] = dframeDaily['close'].shift(-25)
dframeWeekly['ICH_SSA'] = ta.trend.ichimoku_a(dframeWeekly['high'], dframeWeekly['low'], window1=9,window2=26).shift(25)
dframeWeekly['ICH_SSB'] = ta.trend.ichimoku_b(dframeWeekly['high'], dframeWeekly['low'], window2=26,window3=52).shift(25)
dframeWeekly['ICH_KS'] = ta.trend.ichimoku_base_line(dframeWeekly['high'], dframeWeekly['low'])
dframeWeekly['ICH_TS'] = ta.trend.ichimoku_conversion_line(dframeWeekly['high'], dframeWeekly['low'])
dframeWeekly['ICH_CS'] = dframeWeekly['close'].shift(-25)
except KeyError as err:
print("ERR001", err)
return
for indexdf, rowdf in dframeDaily.iterrows():
ssaDaily = dframeDaily['ICH_SSA'].iloc[-1]
ssbDaily = dframeDaily['ICH_SSB'].iloc[-1]
#print(symbol, "ssaDaily, ssbDaily", ssaDaily, ssbDaily)
ksDaily = rowdf['ICH_KS']
tsDaily = rowdf['ICH_TS']
for indexdf, rowdf in dframeWeekly.iterrows():
ssaWeekly = dframeWeekly['ICH_SSA'].iloc[-1]
ssbWeekly = dframeWeekly['ICH_SSB'].iloc[-1]
#print(symbol, "ssaDaily, ssbDaily", ssaDaily, ssbDaily)
ksWeekly = rowdf['ICH_KS']
tsWeekly = rowdf['ICH_TS']
for indexdf, rowdf in dframe.iterrows():
openp = rowdf['open']
high = rowdf['high']
low = rowdf['low']
close = rowdf['close']
ssa = rowdf['ICH_SSA']
ssb = rowdf['ICH_SSB']
ks = rowdf['ICH_KS']
ts = rowdf['ICH_TS']
# cs = rowdf['ICH_CS']
try:
ssa = dframe['ICH_SSA'].iloc[-1] # bougie n-1 car bougie 0 donne nan ?
ssb = dframe['ICH_SSB'].iloc[-1] # bougie n-1 car bougie 0 donne nan ?
ssa2 = dframe['ICH_SSA'].iloc[-2] # bougie n-1 car bougie 0 donne nan ?
ssa3 = dframe['ICH_SSA'].iloc[-3] # bougie n-1 car bougie 0 donne nan ?
ssa4 = dframe['ICH_SSA'].iloc[-4] # bougie n-1 car bougie 0 donne nan ?
ssa5 = dframe['ICH_SSA'].iloc[-5] # bougie n-1 car bougie 0 donne nan ?
ssb2 = dframe['ICH_SSB'].iloc[-2] # bougie n-1 car bougie 0 donne nan ?
ssb3 = dframe['ICH_SSB'].iloc[-3] # bougie n-1 car bougie 0 donne nan ?
ssb4 = dframe['ICH_SSB'].iloc[-4] # bougie n-1 car bougie 0 donne nan ?
ssb5 = dframe['ICH_SSB'].iloc[-5] # bougie n-1 car bougie 0 donne nan ?
# print(ssa, ssb, ssa2, ssb2)
cs = dframe['ICH_CS'].iloc[-26] # cs bougie n en cours
cs2 = dframe['ICH_CS'].iloc[-27] # cs bougie n-1
cs3 = dframe['ICH_CS'].iloc[-28] # cs bougie n en cours
# print(cs, cs2)
ssbchikou = dframe['ICH_SSB'].iloc[-26]
ssbchikou2 = dframe['ICH_SSB'].iloc[-27]
ssbchikou3 = dframe['ICH_SSB'].iloc[-28]
ssbchikou4 = dframe['ICH_SSB'].iloc[-29]
ssbchikou5 = dframe['ICH_SSB'].iloc[-30]
ssbchikou6 = dframe['ICH_SSB'].iloc[-31]
# print(ssbchikou, ssbchikou2, ssbchikou3, ssbchikou4, ssbchikou5, ssbchikou6)
ssachikou = dframe['ICH_SSA'].iloc[-26]
ssachikou2 = dframe['ICH_SSA'].iloc[-27]
ssachikou3 = dframe['ICH_SSA'].iloc[-28]
# print(ssachikou, ssachikou2, ssachikou3)
closechikou = dframe['close'].iloc[-26]
closechikou2 = dframe['close'].iloc[-27]
# print(closechikou, closechikou2)
openchikou = dframe['open'].iloc[-26]
openchikou2 = dframe['open'].iloc[-27]
lowchikou = dframe['low'].iloc[-26]
lowchikou2 = dframe['low'].iloc[-27]
highchikou = dframe['high'].iloc[-26]
highchikou2 = dframe['high'].iloc[-27]
kijunchikou = dframe['ICH_KS'].iloc[-26]
kijunchikou2 = dframe['ICH_KS'].iloc[-27]
kijunchikou3 = dframe['ICH_KS'].iloc[-28]
# print(kijunchikou, kijunchikou2, kijunchikou3)
tenkanchikou = dframe['ICH_TS'].iloc[-26]
tenkanchikou2 = dframe['ICH_TS'].iloc[-27]
tenkanchikou3 = dframe['ICH_TS'].iloc[-28]
# print(tenkanchikou, tenkanchikou2, tenkanchikou3)
timestamp = pd.to_datetime(rowdf['time'], unit='ms')
# print(timestamp, openp, close, ssa, ssb)
except IndexError as error:
print("ERR002", symbol + " EXCEPTION " + str(error))
log_to_errors(symbol + " EXCEPTION " + str(error) + '\n')
# quit(0)
break
timestamp = pd.to_datetime(rowdf['time'], unit='ms')
# timestamp = pd.to_datetime(rowdf['timestamp'], unit='ms')
error_nan_values = False
# To check the values of Ichimoku data (use TradingView with Ichimoku Cloud to compare them)
# log_to_results(str(timestamp) + " " + symbol + " closecs=" + str(closechikou) + " closecs2=" + str(closechikou2) + " CS=" + str(cs) + " CS2=" + str(cs2) + " SSBCS=" + str(ssbchikou) + " SSBCS2=" + str(ssbchikou2) + " SSBCS3=" + str(ssbchikou3) + " KSCS=" + str(kijunchikou)+ " KSCS2=" + str(kijunchikou2)+ " KSCS3=" + str(kijunchikou3) + " TSCS=" + str(tenkanchikou)+ " TSCS2=" + str(tenkanchikou2)+ " TSCS3=" + str(tenkanchikou3) + " SSACS=" + str(ssachikou) + " SSACS2=" + str(ssachikou2) + " SSACS3=" + str(ssachikou3) + " SSA=" + str(ssa) + " SSB=" + str(ssb))
# exit()
if math.isnan(ssa) or math.isnan(ssb) or math.isnan(closechikou) or math.isnan(closechikou2) or math.isnan(
cs) or math.isnan(cs2) or math.isnan(ssbchikou) or math.isnan(ssbchikou2) or math.isnan(
ssbchikou3) or math.isnan(kijunchikou) or math.isnan(kijunchikou2) or math.isnan(
kijunchikou3) or math.isnan(tenkanchikou) or math.isnan(tenkanchikou2) or math.isnan(
tenkanchikou3) or math.isnan(ssachikou) or math.isnan(ssbchikou2) or math.isnan(ssachikou3):
print(symbol + " THERE ARE NAN VALUES IN ICHIMOKU DATA")
log_to_errors(symbol + " THERE ARE NAN VALUES IN ICHIMOKU DATA" + '\n')
error_nan_values = True
# quit(0)
if error_nan_values:
break
filename = "CS_" + symbol.replace('/', '_') + ".txt"
if os.path.exists(filename):
os.remove(filename)
# now_cs = datetime.datetime_result_min() - timedelta(hours=4 * 26)
# # print("now_cs=" + str(now_cs))
# # quit(0)
# if timestamp.year == now_cs.year and timestamp.month == now_cs.year and timestamp.day == now_cs.day and timestamp.hour == now_cs.hour:
# print(str(cs))
data_minute = timestamp.minute
data_hour = timestamp.hour
data_day = timestamp.day
data_month = timestamp.month
data_year = timestamp.year
if interval_for_klinesT == Client.KLINE_INTERVAL_1MINUTE:
datetime_result_min = datetime.now() - timedelta(minutes=1)
elif interval_for_klinesT == Client.KLINE_INTERVAL_3MINUTE:
# datetime_result_min = datetime.now() - timedelta(minutes=15)
datetime_result_min = datetime.now() - timedelta(minutes=3)
elif interval_for_klinesT == Client.KLINE_INTERVAL_5MINUTE:
# datetime_result_min = datetime.now() - timedelta(minutes=15)
datetime_result_min = datetime.now() - timedelta(minutes=5)
elif interval_for_klinesT == Client.KLINE_INTERVAL_15MINUTE:
# datetime_result_min = datetime.now() - timedelta(hours=1)
datetime_result_min = datetime.now() - timedelta(minutes=15)
elif interval_for_klinesT == Client.KLINE_INTERVAL_30MINUTE:
# datetime_result_min = datetime.now() - timedelta(hours=1)
datetime_result_min = datetime.now() - timedelta(minutes=30)
elif interval_for_klinesT == Client.KLINE_INTERVAL_1HOUR:
datetime_result_min = datetime.now() - timedelta(hours=1)
elif interval_for_klinesT == Client.KLINE_INTERVAL_2HOUR:
datetime_result_min = datetime.now() - timedelta(hours=2)
elif interval_for_klinesT == Client.KLINE_INTERVAL_4HOUR:
datetime_result_min = datetime.now() - timedelta(hours=4)
elif interval_for_klinesT == Client.KLINE_INTERVAL_6HOUR:
datetime_result_min = datetime.now() - timedelta(hours=6)
elif interval_for_klinesT == Client.KLINE_INTERVAL_8HOUR:
datetime_result_min = datetime.now() - timedelta(hours=8)
elif interval_for_klinesT == Client.KLINE_INTERVAL_12HOUR:
datetime_result_min = datetime.now() - timedelta(hours=12)
elif interval_for_klinesT == Client.KLINE_INTERVAL_1DAY:
datetime_result_min = datetime.now() - timedelta(hours=timestamp.hour)
elif interval_for_klinesT == Client.KLINE_INTERVAL_3DAY:
datetime_result_min = datetime.now() - timedelta(hours=72)
else:
datetime_result_min = datetime.now() - timedelta(hours=1) # We should never get here
datetime_result_min_minute = datetime_result_min.minute
datetime_result_min_hour = datetime_result_min.hour
datetime_result_min_day = datetime_result_min.day
datetime_result_min_month = datetime_result_min.month
datetime_result_min_year = datetime_result_min.year
# if math.isnan(ssa):
# print(symbol, "ssa is null")
#
# if math.isnan(ssb):
# print(symbol, "ssb is null")
evol_co = round(((close - openp) / openp) * 100, 4)
scan = True
if interval_for_klinesT == Client.KLINE_INTERVAL_1MINUTE:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour == datetime_result_min_hour and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_3MINUTE:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour == datetime_result_min_hour and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_5MINUTE:
# print("comparing : " + str(data_hour) + " " + str(data_minute) + " to " + str(datetime_result_min_hour) + " " + str(datetime_result_min_minute))
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour == datetime_result_min_hour and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_15MINUTE:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour == datetime_result_min_hour and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_30MINUTE:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour == datetime_result_min_hour and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_1HOUR:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour > datetime_result_min_hour # and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_2HOUR:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour > datetime_result_min_hour # and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_4HOUR:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour > datetime_result_min_hour # and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_6HOUR:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour > datetime_result_min_hour # and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_8HOUR:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour > datetime_result_min_hour # and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_12HOUR:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour > datetime_result_min_hour # and data_minute >= datetime_result_min_minute
elif interval_for_klinesT == Client.KLINE_INTERVAL_1DAY:
result_ok = data_day >= datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year # and data_hour >= datetime_result_min_hour
elif interval_for_klinesT == Client.KLINE_INTERVAL_3DAY:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year # and data_hour >= datetime_result_min_hour
else:
result_ok = data_day == datetime_result_min_day and data_month == datetime_result_min_month and data_year == datetime_result_min_year and data_hour >= datetime_result_min_hour
# if symbol == "ETHUSDT":
# print ("ETHUSDT SSACHIKOU = " + str(ssachikou))
# print ("ETHUSDT SSBCHIKOU = " + str(ssbchikou))
if scan:
if result_ok:
# print("result ok")
# if openp < ssb < close or openp > ssb and close > ssb:
# Define your own criterias for filtering assets on the line below
if scan_type == ScanType.UP:
#condition_is_satisfied = openp < ksDaily and close > ksDaily
#condition_is_satisfied = condition_is_satisfied or (openp < tsDaily and close > tsDaily)
#condition_is_satisfied = condition_is_satisfied or (openp < ssbDaily and close > ssbDaily)
#condition_is_satisfied = condition_is_satisfied or (openp < ssaDaily and close > ssaDaily)
condition1 = openp <= ssbWeekly and close >= ssbWeekly
condition2 = False#openp >= ssbWeekly and close <= ssbWeekly
condition3 = openp <= ksWeekly and close >= ksWeekly
condition4 = False#openp >= ksWeekly and close <= ksWeekly
condition5 = openp <= tsWeekly and close >= tsWeekly
condition6 = False#openp >= tsWeekly and close <= tsWeekly
condition7 = openp <= tsDaily and close >= tsDaily
condition8 = openp <= ssbDaily and close >= ssbDaily
condition_is_satisfied = condition1 or condition2 or condition3 or condition4 or condition5 or condition6 or condition7 or condition8
if condition_is_satisfied:
#print(symbol, "ssaW, ssbW", ssaWeekly, ssbWeekly)
tf = str(interval_for_klinesT)
str_conditions = ""
if condition1 is True:
str_condition = symbol + " " + "openp (" + tf + ") <= ssbWeekly and close (" + tf + ") >= ssbWeekly"
str_conditions = str_conditions + "\n" + str_condition + "\n"
#log_to_results(str_condition)
if condition2 is True:
str_condition = symbol + " " + "openp (" + tf + ") >= ssbWeekly and close (" + tf + ") <= ssbWeekly"
str_conditions = str_conditions + "\n" + str_condition + "\n"
#log_to_results(str_condition)
if condition3 is True:
str_condition = symbol + " " + "openp (" + tf + ") <= ksWeekly and close (" + tf + ") >= ksWeekly"
str_conditions = str_conditions + "\n" + str_condition + "\n"
#log_to_results(str_condition)
if condition4 is True:
str_condition = symbol + " " + "openp (" + tf + ") >= ksWeekly and close (" + tf + ") <= ksWeekly"
str_conditions = str_conditions + "\n" + str_condition + "\n"
#log_to_results(str_condition)
if condition5 is True:
str_condition = symbol + " " + "openp (" + tf + ") <= tsWeekly and close (" + tf + ") >= tsWeekly"
str_conditions = str_conditions + "\n" + str_condition + "\n"
#log_to_results(str_condition)
if condition6 is True:
str_condition = symbol + " " + "openp (" + tf + ") >= tsWeekly and close (" + tf + ") <= tsWeekly"
str_conditions = str_conditions + "\n" + str_condition + "\n"
#log_to_results(str_condition)
if condition7 is True:
str_condition = symbol + " " + "openp (" + tf + ") <= tsDaily and close (" + tf + ") >= tsDaily"
str_conditions = str_conditions + "\n" + str_condition + "\n"
#log_to_results(str_condition)
if condition8 is True:
str_condition = symbol + " " + "openp (" + tf + ") <= ssbDaily and close (" + tf + ") >= ssbDaily"
str_conditions = str_conditions + "\n" + str_condition + "\n"
#log_to_results(str_condition)
elif scan_type == ScanType.DOWN:
condition_is_satisfied = openp < ks and close < ks and close < ts and close < openp and close < ssa and close < ssb and cs < lowchikou and cs < kijunchikou and cs < ssbchikou and cs < ssachikou and cs < tenkanchikou
#log_to_results(symbol + " ssa daily = " + str(ssaDaily) + " ssb daily = " + str(ssbDaily) + " ks daily = " + str(ksDaily) + " ts daily = " + str(tsDaily))
#log_to_results(symbol + " ssa w = " + str(ssaWeekly) + " ssb w = " + str(ssbWeekly) + " ks w = " + str(ksWeekly) + " ts w = " + str(tsWeekly))
#log_to_results(symbol + " ssa w = " + str(ssaWeekly) + " ssb w = " + str(ssbWeekly) + " ks w = " + str(ksWeekly) + " ts w = " + str(tsWeekly))
if condition_is_satisfied:
cs_results = ""
str_result = str(timestamp) + " " + symbol + " " + symbol_type + " SSA=" + str(ssa) + " SSB=" + str(
ssb) + " KS=" + str(ks) + " TS=" + str(ts) + " O=" + str(
openp) + " H=" + str(high) + " L=" + str(low) + " SSBCS=" + str(
ssbchikou) + str_conditions # + " C=" + str(close) + " CS=" + str(cs) + " EVOL%=" + str(evol_co) # We don't concatenate the variable parts (for comparisons in list_results)
if not (str_result in list_results):
if not new_results_found:
new_results_found = True
results_count = results_count + 1
list_results.append(str_result)
#log_to_results(str_result)
str_result = cs_results + "\n" + str(results_count) + " " + str_result + " C=" + str(
close) + " CS=" + str(cs) + " EVOL(C/O)%=" + str(
evol_co) # We add the data with variable parts
if scan_futures:
str_result += "\nhttps://tradingview.com/chart/?symbol=FTX%3A" + symbol.replace("-", "")
else:
str_result += "\nhttps://tradingview.com/chart/?symbol=FTX%3A" + symbol.replace("/", "")
print(str_result + "\n")
log_to_results(str(datetime.now()) + ":" + str_result + "\n")
dict_evol[symbol] = evol_co
else:
# if result_ok:
print(timestamp, symbol, "O", openp, "H", high, "L", low, "C", close, "SSA", ssa, "SSB", ssb, "KS", ks,
"TS", ts, "CS", cs, "SSB CS", ssbchikou)
str_result = str(timestamp) + " " + symbol + " O=" + str(openp) + " H=" + str(high) + " L=" + str(
low) + " C=" + str(close) + " SSA=" + str(
ssa) + " SSB=" + str(ssb) + " KS=" + str(ks) + " TS=" + str(ts) + " CS=" + str(cs) + " SSB CS=" + str(
ssbchikou) + " EVOL%(C/O)=" + str(evol_co)
log_to_results(str(datetime.now()) + ":" + str_result)
# Set loop_scan to True to scan in loop
loop_scan = True
maxthreads = 75
threadLimiter = threading.BoundedSemaphore(maxthreads)
def scan_one(symbol):
threadLimiter.acquire()
try:
execute_code(symbol)
finally:
threadLimiter.release()
threads = []
def main_thread(name):
global client, list_results, results_count, stop_thread, interval_for_klinesT
global new_results_found
log_to_evol(str(datetime.now()))
while not stop_thread:
# dict_evol = {}
# new_results_found = False
# info_binance = Client().get_all_tickers()
# print(info_binance)
# exit()
markets = requests.get('https://ftx.com/api/markets').json()
df = pd.DataFrame(markets['result'])
df.set_index('name')
# df = pd.DataFrame(info_binance)
# df.set_index('symbol')
for index, row in df.iterrows():
# symbol = row['symbol']
# symbol_type = "n/a" #row['type']
symbol = row['name']
symbol_type = row['type']
if scan_futures is True and not (symbol_type == "future"):
# print("this is not future, this is ", symbol_type)
continue
if scan_spot is True and not (symbol_type == "spot"):
# print("this is not future, this is ", symbol_type)
continue
# print(symbol, symbol_type)
# filtering symbols to scan here
# if not symbol.endswith('USDT') or symbol.endswith("DOWNUSDT") or symbol.endswith("UPUSDT"):
# continue
# if symbol != 'BTC-PERP':
# continue
# else:
# print(symbol, "found")
# if scan_futures:
# print(symbol, "trying to scan in futures", end=" ")
# print(symbol, "trying to scan in futures")
# else:
# print(symbol, "trying to scan", end=" ")
# print(symbol, "trying to scan")
if symbol.endswith("BEAR/USD") or symbol.endswith("BULL/USD") or symbol.endswith("HEDGE/USD"):
continue
if symbol.endswith("BEAR/USDT") or symbol.endswith("BULL/USDT") or symbol.endswith("HEDGE/USDT"):
continue
if symbol.endswith("HALF/USDT") or symbol.endswith("HALF/USD"):
continue
disable = False
if disable == False:
try:
t = threading.Thread(target=scan_one, args=(symbol,))
threads.append(t)
t.start()
except requests.exceptions.ConnectionError:
continue
for tt in threads:
tt.join()
print(str(datetime.now()) + " All threads finished.")
log_to_results(str(datetime.now()) + " All threads finished.")
time.sleep(1)
if loop_scan is False:
stop_thread = True
else:
stop_thread = False
#####
if new_results_found:
log_to_results(100 * '*' + "\n")
new_dict = sorted(dict_evol.items(), key=lambda kv: (kv[1], kv[0]))
if new_dict:
print(str(datetime.now()) + " " + str(new_dict))
log_to_evol(str(datetime.now()) + " " + str(new_dict))
# Remove the line below to scan in loop
# stop_thread = True
x = threading.Thread(target=main_thread, args=(1,))
x.start()