-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_grid.py
1873 lines (1512 loc) · 73.6 KB
/
test_grid.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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import numpy as np
import requests
import pandas as pd
from datetime import datetime
import time
import schedule
from threading import Thread
from pathlib import Path
import constant as cons
import botfunction as func
import indicator as ind
from botclass import BinanceCandlePrice as candleClass
from botclass import BinanceSymbol as symbolClass
from botclass import BinanceBookTicker as tickerClass
from botclass import Trade as tradeClass
from botclass import TickerData
from botclass import BinanceOrderBook as orderBookClass
import notify as tlg
import logging
import winsound
SIDE_BUY = 'BUY'
SIDE_SELL = 'SELL'
STATUS_BUY = 0
STATUS_SELL = 1
STATUS_STOP = 2
BTC_CANDLE_INTERVAL = "30m"
BTC_CANDLE_COUNT = 11
BTC_SLOPE_PERIOD = 10
BTC_LSMA_PERIOD = 6
BTC_STD_DEV_UP = float(2.5)
BTC_STD_DEV_DOWN = float(2.5)
BTC_SIGNAL = False
BTC_INNER_SIGNAL = False
BTC_RED_ALERT = False
BTC_GREEN_ALERT = False
BTC_GREEN_THRESHOLD = float(1.5)
BTC_RED_THRESHOLD = float(1.5)
BTC_SLOPE = float(0.0)
CANDLE_INTERVAL = "30m" # Okunacak mum verisi periyodu
CANDLE_COUNT = 33 # Okunacak mum verisi adeti
LR_PERIOD = 18
STOCH_PERIOD = 18
RSI_PERIOD = 18
ROC_PERIOD = 18
MACD_FAST = 7
MACD_SLOW = 18
MACD_SIGNAL = 9
VWMA_PERIOD = 18
LSMA_PERIOD = 6
LR_STD_UP_FACTOR = float(2.3)
LR_STD_DOWN_FACTOR = float(2.8)
LR_UP_HEIGHT = float(2.3)
LR_DOWN_HEIGHT = float(3.0)
AVERAGE_CANDLE_COUNT = 18 # Ortalaması hesaplanacak mum adeti
ATR_PERIOD = 6
ATR_STOP_FACTOR = float(0.5)
MAX_AMOUNT_LIMIT = float(100)
LIMIT_FACTOR = float(5)
COMMISSION_RATE = float(0.00075) # %0.075
VOLUME_FACTOR = float(2.0)
FIRST_PROFIT_TARGET = float(1.5) # 2.0
PROFIT_TARGET = float(1.5) # 2.0
PROFIT_STOP_TRAIL = float(0.7) # 0.8
PROFIT_STRATEGY_17 = float(2.0)
IS_LOG = True
IS_PRINT = True
IS_ALARM = False
TEST_NAME = "STRATEJI GRID"
glbExplanation = ""
logging.basicConfig(filename="debug_test_grid", level=logging.INFO, format='(%(threadName)-5s) %(message)s', )
def log(msg=None):
if IS_LOG is True:
logging.info(msg=msg)
if IS_PRINT is True:
print(msg, flush=True)
if IS_ALARM is True:
alarm()
def alarm():
frequency = 2000
duration = 500
winsound.Beep(frequency, duration)
def readSummary(dbCursor=None):
trade = tradeClass()
res = trade.readProfitSummary(dbCursor=dbCursor)
return res
def convert_dataframe(bars=None):
"""
Mum verilerinin ilk 6 kolonu "date", "open", "high", "low", "close", "volume" muhafaza edilir.
Diğer kolonlar silinir.
DataFrame e çevrilen data geri dönülür.
"""
for line in bars:
del line[6:]
df = pd.DataFrame(bars, columns=['date', 'open', 'high', 'low', 'close', 'volume'])
return df
def calculate_average(data=None):
dataLength = len(data)
total = sum(data)
average = total / dataLength
return average
def get_candle_data(dbCursor=None, session=None, url=None, symbol=None, interval=None, limit=None):
candle = candleClass()
candle.dbCursor = dbCursor
bars = candle.getDataWithSession(session=session, url=url, symbol=symbol, candleInterval=interval, limit=limit)
return bars
def get_ticker_info(session=None, url=None, symbol=None):
status = False
bidPrice = None
bidQty = None
askPrice = None
askQty = None
ticker = tickerClass()
ticker.symbols = [symbol]
tickerRows = ticker.getDataWithSession(session=session, url=url)
if tickerRows is None:
status = False
return status, bidPrice, bidQty, askPrice, askQty
for tickerRow in tickerRows:
status = True
bidPrice = float(tickerRow["bidPrice"])
bidQty = float(tickerRow["bidQty"])
askPrice = float(tickerRow["askPrice"])
askQty = float(tickerRow["askQty"])
return status, bidPrice, bidQty, askPrice, askQty
def position_control(dbCursor=None, symbol=None, status=None):
trade = tradeClass()
row = trade.readTrade(dbCursor=dbCursor, symbol=symbol, status=status)
if (row is None) or (len(row) <= 0):
return False, row
return True, row
def insert_trade_table(dbCursor=None, symbol=None, buySell=None, price=None, buyLot=None, buyAmount=None,
buyCommission=None, stopPrice=None, stopType=None, stopHeight=None,
sellTarget=None, period=None, periodTime=None, currentPeriodTime=None,
signalName=None, explanation=None, profitTarget=None, strategy=None):
trade = tradeClass()
trade.symbol = symbol
trade.period = period
trade.period_time = periodTime
trade.current_period_time = currentPeriodTime
trade.explanation = explanation
trade.buy_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
trade.buy_price = price
trade.buy_lot = buyLot
trade.buy_amount = buyAmount
trade.buy_commission = buyCommission
trade.stop_price = stopPrice
trade.stop_type = stopType
trade.stop_height = stopHeight
trade.stop_change_count = 0
trade.buy_signal_name = signalName
trade.sell_target = sellTarget
trade.status = STATUS_BUY
trade.profit_target = profitTarget
trade.strategy = strategy
trade.btc_signal = int(BTC_SIGNAL)
trade.btc_inner_signal = int(BTC_INNER_SIGNAL)
trade.btc_red_alert = int(BTC_RED_ALERT)
trade.btc_green_alert = int(BTC_GREEN_ALERT)
trade.addTrade(dbCursor=dbCursor)
def update_trade_table(dbCursor=None, symbol=None, buySell=None, price=None, sellLot=None, sellAmount=None,
sellCommission=None, signalName=None, oldStatus=None, newStatus=None):
trade = tradeClass()
trade.symbol = symbol
if buySell == SIDE_BUY:
trade.buy_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
trade.buy_price = price
trade.buy_signal_name = signalName
if buySell == SIDE_SELL:
trade.sell_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
trade.sell_price = price
trade.sell_signal_name = signalName
trade.sell_lot = sellLot
trade.sell_amount = sellAmount
trade.sell_commission = sellCommission
trade.updateTrade(dbCursor=dbCursor, symbol=symbol, oldStatus=oldStatus, newStatus=newStatus)
def update_stop_price(dbCursor=None, symbol=None, stopPrice=None, stopChangeCount=None):
trade = tradeClass()
trade.updateStopPrice(dbCursor=dbCursor, symbol=symbol, stopPrice=stopPrice,
stopChangeCount=stopChangeCount,
status=STATUS_BUY)
def update_stop_price_and_current_period(dbCursor=None, symbol=None, stopPrice=None,
currentPeriodTime=None, stopChangeCount=None):
trade = tradeClass()
trade.updateStopPriceAndCurrentPeriod(dbCursor=dbCursor, symbol=symbol, stopPrice=stopPrice,
currentPeriodTime=currentPeriodTime, stopChangeCount=stopChangeCount,
status=STATUS_BUY)
def update_profit_target(dbCursor=None, symbol=None, profitTarget=None):
trade = tradeClass()
trade.updateProfitTarget(dbCursor=dbCursor, symbol=symbol, profitTarget=profitTarget, status=STATUS_BUY)
def stop_control(currentPrice=None, stopPrice=None) -> (bool):
if currentPrice < stopPrice:
log(f" STOP StopPrice: {stopPrice} CurrentPrice: {currentPrice}")
return True
return False
def lr_stop_control(coinTicker=None, stopPrice=None, buyPrice=None, tickSize=None) -> (bool):
up, center, down, slope = ind.get_linear_regression(prices=coinTicker.closes, period=LR_PERIOD,
standard_deviation_up_factor=2.0,
standard_deviation_down_factor=2.0)
if slope > 0.0:
return False
closePrice = coinTicker.closes[-1]
upPrice = up[-1]
if closePrice >= upPrice:
log(f" Price >= LR Up: {coinTicker.closes[-1]} > {up[-1]}")
return True
return False
def trailing_stop_control(stopHeight=None, currentPrice=None, stopPrice=None, tickSize=None) -> (bool, float):
stopChange = False
newStopPrice = None
if currentPrice > stopPrice:
difference = currentPrice - stopPrice
difference = func.round_tick_size(price=difference, tick_size=tickSize)
if difference > stopHeight:
newStopPrice = currentPrice - stopHeight
newStopPrice = func.round_tick_size(price=newStopPrice, tick_size=tickSize)
stopChange = True
return stopChange, newStopPrice
def profit_control(coinTicker=None, currentPrice=None, buyingPrice=None, strategy=None) -> (bool):
# BTC_RED_ALERT = True ise alınmış olan coinler o anki fiyattan satılır.
# if BTC_RED_ALERT == True:
# log(f" ****** BTC_RED_ALERT: {BTC_RED_ALERT} ******")
# return True
# BTC_INNER_SIGNAL = False ise coin alış fiyatına göre belli bir (örn: %0.6)
# fiyatın üzerine çıkmış ise satılır.
# if BTC_INNER_SIGNAL is True:
# return False
# curColor = ind.get_candle_color(open=coinTicker.opens[-1], close=coinTicker.closes[-1])
# if curColor == cons.CANDLE_GREEN:
# return False
if strategy == 17:
if currentPrice > buyingPrice:
profit = ((currentPrice - buyingPrice) * 100) / buyingPrice
# up, center, down, slope = ind.get_linear_regression(prices=coinTicker.closes, period=LR_PERIOD,
# standard_deviation_up_factor=1.8,
# standard_deviation_down_factor=2.3)
# if slope < 0:
# target = 0.90
# else:
# target = PROFIT_STRATEGY_17
if profit >= PROFIT_STRATEGY_17: # target:
log(f" BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL}")
log(f" PROFIT STOP BuyingPrice: {buyingPrice} CurrentPrice: {currentPrice}")
return True
return False
def profit_target_control(currentPrice=None, buyingPrice=None, profitTarget=None, strategy=None) -> (bool, float):
profitTargetLevel = PROFIT_TARGET
newProfitTarget = profitTarget
profit = (currentPrice - buyingPrice)*100/buyingPrice
if profit > profitTarget:
newProfitTarget += profitTargetLevel
while profit >= newProfitTarget:
newProfitTarget += profitTargetLevel
return True, newProfitTarget
return False, newProfitTarget
def get_stop_price(stopType=None, coinTicker=None, tickSize=None):
stopPrice = None
stopHeight = None
if stopType == cons.STOP_TYPE_LR:
stopPrice = coinTicker.lows[-2]
if stopPrice > coinTicker.lows[-1]:
stopPrice = coinTicker.lows[-1]
if stopType == cons.STOP_TYPE_PREVIOUS_LOW:
stopPrice = coinTicker.lows[-2]
if stopPrice > coinTicker.lows[-1]:
stopPrice = coinTicker.lows[-1]
if stopType == cons.STOP_TYPE_PROFIT:
stopPrice = coinTicker.lows[-2]
if stopPrice > coinTicker.lows[-1]:
stopPrice = coinTicker.lows[-1]
if stopType == cons.STOP_TYPE_VOLUME:
stopPrice = coinTicker.lows[-1]
if stopType == cons.STOP_TYPE_TRAILING:
atr = ind.get_atr(highPrices=coinTicker.highs, lowPrices=coinTicker.lows,
closePrices=coinTicker.closes, period=ATR_PERIOD)
atrIndicator = func.round_tick_size(price=atr[-2], tick_size=tickSize)
stopPrice = coinTicker.lows[-2]
if ATR_STOP_FACTOR > 0:
stopPrice = stopPrice - (atrIndicator * ATR_STOP_FACTOR)
else:
stopPrice = stopPrice - atrIndicator
"""
Stop fiyatının coin in minumum artım miktarına (tick size) göre ayarlanır.
Minimum artım miktarından fazla olan digitler silinir.
Örnek: Stop: 0.2346328 ise ve tick_size: 0.0001 ise stop: 0.2346 yapılır.
"""
stopPrice = func.round_tick_size(price=stopPrice, tick_size=tickSize)
stopHeight = coinTicker.lows[-2] - stopPrice
stopHeight = func.round_tick_size(price=stopHeight, tick_size=tickSize)
log(f" Stop Price: {stopPrice} Stop Height: {stopHeight} Under Low: %{round((stopHeight/stopPrice)*100, 3)}")
return stopPrice, stopHeight
def get_stop_price_under_target(currentPrice=None, buyingPrice=None, profitTarget=None,
tickSize=None, strategy=None) -> (float):
log(f" currentPrice:{currentPrice} buyingPrice:{buyingPrice} profitTarget:{profitTarget} tickSize:{tickSize}")
stopProfitLevel = profitTarget - PROFIT_TARGET
stopProfitLevel = stopProfitLevel - PROFIT_STOP_TRAIL
log(f" profitTarget: {profitTarget} stopProfitLevel: {stopProfitLevel} ")
newStop = buyingPrice + (stopProfitLevel * buyingPrice)/100
newStop = func.round_tick_size(price=newStop, tick_size=tickSize)
log(f" newStop: {newStop}")
return newStop
def control_btc(symbol=None, coinTicker=None, tickSize=None):
global BTC_SIGNAL
global BTC_INNER_SIGNAL
global BTC_GREEN_ALERT
global BTC_RED_ALERT
global BTC_SLOPE
curColor = ind.get_candle_color(open=coinTicker.opens[-1], close=coinTicker.closes[-1])
prev1Color = ind.get_candle_color(open=coinTicker.opens[-2], close=coinTicker.closes[-2])
prev2Color = ind.get_candle_color(open=coinTicker.opens[-3], close=coinTicker.closes[-3])
curClose = coinTicker.closes[-1]
curLow = coinTicker.lows[-1]
curHigh = coinTicker.highs[-1]
lsma = ind.get_lsma(data=coinTicker.closes, period=BTC_LSMA_PERIOD)
curLSMA = func.round_tick_size(price=lsma[-1], tick_size=tickSize)
prev1LSMA = func.round_tick_size(price=lsma[-2], tick_size=tickSize)
prev2LSMA = func.round_tick_size(price=lsma[-3], tick_size=tickSize)
ups, center, downs, slope = ind.get_linear_regression(prices=coinTicker.closes, period=BTC_SLOPE_PERIOD,
standard_deviation_up_factor=BTC_STD_DEV_UP,
standard_deviation_down_factor=BTC_STD_DEV_DOWN)
BTC_SLOPE = slope
if slope < 0:
BTC_SIGNAL = False
else:
BTC_SIGNAL = True
u, c, d, innerSlope = ind.get_linear_regression(prices=coinTicker.closes, period=3,
standard_deviation_up_factor=BTC_STD_DEV_UP,
standard_deviation_down_factor=BTC_STD_DEV_DOWN)
if innerSlope < 0:
BTC_INNER_SIGNAL = False
else:
BTC_INNER_SIGNAL = True
"""
Mumun boyu bir yöne eşik değerini geçmiş ise ALARM (RED veya GREEN) aktif olur.
Mumun renge terse dönünceye kadar ALARM aktif olarak kalır.
"""
threshold = ((curHigh - curLow) * 100) / curLow
if curColor == cons.CANDLE_GREEN:
if threshold >= BTC_GREEN_THRESHOLD:
BTC_GREEN_ALERT = True
BTC_RED_ALERT = False
if curColor == cons.CANDLE_RED:
if threshold >= BTC_RED_THRESHOLD:
BTC_GREEN_ALERT = False
BTC_RED_ALERT = True
# Mumun rengi terse dönünceye kadar ALARM aktif olarak kalır.
if BTC_GREEN_ALERT is True:
if curColor == cons.CANDLE_GREEN:
BTC_GREEN_ALERT = True
else:
BTC_GREEN_ALERT = False
if BTC_RED_ALERT is True:
if curColor == cons.CANDLE_RED:
BTC_RED_ALERT = True
else:
BTC_RED_ALERT = False
def control_strategy_1(symbol=None, coinTicker=None, tickSize=None) -> (bool):
global glbExplanation
glbExplanation = ""
if BTC_RED_ALERT is True:
return False
if BTC_SIGNAL is False and BTC_INNER_SIGNAL is False:
return False
curClose = coinTicker.closes[-1]
prevHigh = coinTicker.highs[-2]
curColor = ind.get_candle_color(open=coinTicker.opens[-1], close=coinTicker.closes[-1])
if curColor == cons.CANDLE_RED:
return False
rsi = ind.get_rsi(prices=coinTicker.closes, timePeriod=10)
curRSI = rsi[-1]
prev1RSI = rsi[-2]
prev2RSI = rsi[-3]
if (curRSI < 20) and (prev1RSI < 20) and (prev2RSI < 20):
if (prev2RSI > prev1RSI < curRSI):
if (curClose <= prevHigh):
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} *S1")
log(f" curRSI, prev1RSI, prev2RSI < 20: {curRSI}, {prev1RSI}, {prev2RSI}")
log(f" prev2RSI > prev1RSI < curRSI: {prev2RSI} > {prev1RSI} < {curRSI} ")
log(f" +++ curClose <= prevHigh: {curClose} < {prevHigh}")
glbExplanation = f" curRSI, prev1RSI, prev2RSI < 20: {curRSI}, {prev1RSI}, {prev2RSI} prev2RSI > prev1RSI < curRSI: {prev2RSI} > {prev1RSI} < {curRSI}"
return True
return False
def control_strategy_2(symbol=None, coinTicker=None, tickSize=None) -> (bool):
global glbExplanation
glbExplanation = ""
if BTC_RED_ALERT is True:
return False
if BTC_SIGNAL is False and BTC_INNER_SIGNAL is False:
return False
up, center, down, slope = ind.get_linear_regression(prices=coinTicker.closes, period=LR_PERIOD,
standard_deviation_up_factor=2.0,
standard_deviation_down_factor=2.2)
curClose = coinTicker.closes[-1]
curOpen = coinTicker.opens[-1]
prev1Close = coinTicker.closes[-2]
prev1Open = coinTicker.opens[-2]
prev1High = coinTicker.highs[-1]
curCandleColor = ind.get_candle_color(open=curOpen, close=curClose)
prev1CandleColor = ind.get_candle_color(open=prev1Open, close=prev1Close)
if slope < 0:
if (BTC_SIGNAL is True) and (BTC_INNER_SIGNAL is True):
rsi = ind.get_rsi(prices=coinTicker.closes, timePeriod=10)
curRSI = rsi[-1]
prevRSI = rsi[-2]
rsiEMA = ind.get_ema(data=rsi, period=10)
curRSIEMA = rsiEMA[-1]
if (prevRSI < 15) and (prev1CandleColor == cons.CANDLE_RED) and (curCandleColor == cons.CANDLE_GREEN):
if (curRSI > prevRSI) and (curRSI > curRSIEMA):
if curClose < prev1High:
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} *S2")
log(f" prevRSI < 15: {prevRSI} slope:{slope} ")
log(f" curRSI > curRSIEMA: {curRSI} > {curRSIEMA} ")
log(f" curRSI > prevRSI: {curRSI} > {prevRSI} ")
log(f" +++ curClose < prev1High: {curClose} < {prev1High}")
glbExplanation = f" prevRSI:{prevRSI} curRSI > curRSIEMA: {curRSI} > {curRSIEMA} slope:{slope}"
return True
return False
def control_strategy_3(symbol=None, coinTicker=None, tickSize=None) -> (bool):
global glbExplanation
glbExplanation = ""
curOpen = coinTicker.opens[-1]
curClose = coinTicker.closes[-1]
curCandleColor = ind.get_candle_color(open=curOpen, close=curClose)
if curCandleColor == cons.CANDLE_RED:
return False
start = CANDLE_COUNT - AVERAGE_CANDLE_COUNT - 1
end = CANDLE_COUNT - 1
avgVolume = coinTicker.volumes[start:end].mean()
curVolume = coinTicker.volumes[-1]
if curVolume < avgVolume * VOLUME_FACTOR:
return False
fastST, slowST = ind.get_stochastic(high=coinTicker.highs, low=coinTicker.lows, close=coinTicker.closes,
timePeriod=LR_PERIOD, slowKPeriod=3, slowDPeriod=3)
if fastST[-1] <= slowST[-1]:
return False
if (fastST[-1] < 25) and (slowST[-1] < 25):
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} *S3")
log(f" curVolume > avgVolume*VOLUME_FACTOR: {curVolume} > {avgVolume * VOLUME_FACTOR}")
log(f" fastST > slowST: {fastST[-1]} > {slowST[-1]}")
glbExplanation = f" {symbol} curVolume > avgVolume*VOLUME_FACTOR: {curVolume} > {avgVolume*VOLUME_FACTOR} fastST > slowST: {fastST[-1]} > {slowST[-1]} "
return True
return False
def control_strategy_4(symbol=None, coinTicker=None, tickSize=None) -> (bool):
global glbExplanation
glbExplanation = ""
if BTC_RED_ALERT is True:
return False
if BTC_SIGNAL is False and BTC_INNER_SIGNAL is False:
return False
curClose = coinTicker.closes[-1]
prev1High = coinTicker.highs[-2]
rsi = ind.get_rsi(prices=coinTicker.closes, timePeriod=10)
curRSI = rsi[-1]
prev1RSI = rsi[-2]
prev2RSI = rsi[-3]
rsiEMA = ind.get_ema(data=rsi, period=10)
curRSIEMA = rsiEMA[-1]
lsma = ind.get_lsma(data=coinTicker.closes, period=LSMA_PERIOD)
curLSMA = func.round_tick_size(price=lsma[-1], tick_size=tickSize)
prev1LSMA = func.round_tick_size(price=lsma[-2], tick_size=tickSize)
prev2LSMA = func.round_tick_size(price=lsma[-3], tick_size=tickSize)
prev3LSMA = func.round_tick_size(price=lsma[-4], tick_size=tickSize)
if (BTC_SIGNAL is True) and (BTC_INNER_SIGNAL is True):
if (curLSMA > prev1LSMA < prev2LSMA): # and (slope > 0):
if (curRSI > curRSIEMA) and (curRSI > prev1RSI) :
if (prev1RSI < 20) or (prev2RSI < 20):
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} *S4")
log(f" LSMA: {curLSMA} > {prev1LSMA} < {prev2LSMA}")
log(f" curRSI > curRSIEMA : {curRSI} > {curRSIEMA} curRSI > prev1RSI:{curRSI} > {prev1RSI}")
log(f" prev1RSI OR prev2RSI < 20: {prev1RSI} {prev2RSI}")
if curClose <= prev1High:
log(f" +++ curClose <= prev1High : {curClose} <= {prev1High} ")
glbExplanation = f" LSMA: {curLSMA} > {prev1LSMA} < {prev2LSMA} curRSI > prev1RSI: {curRSI} > {prev2RSI} "
return True
return False
def control_strategy_5(symbol=None, coinTicker=None, tickSize=None) -> (bool):
global glbExplanation
glbExplanation = ""
if BTC_RED_ALERT is True:
return False
if BTC_SIGNAL is False and BTC_INNER_SIGNAL is False:
return False
curClose = coinTicker.closes[-1]
prev1High = coinTicker.highs[-2]
rsi = ind.get_rsi(prices=coinTicker.closes, timePeriod=10)
curRSI = rsi[-1]
prev1RSI = rsi[-2]
prev2RSI = rsi[-3]
rsiEMA = ind.get_ema(data=rsi, period=10)
curRSIEMA = rsiEMA[-1]
lsma = ind.get_lsma(data=coinTicker.closes, period=LSMA_PERIOD)
curLSMA = func.round_tick_size(price=lsma[-1], tick_size=tickSize)
prev1LSMA = func.round_tick_size(price=lsma[-2], tick_size=tickSize)
prev2LSMA = func.round_tick_size(price=lsma[-3], tick_size=tickSize)
prev3LSMA = func.round_tick_size(price=lsma[-4], tick_size=tickSize)
if (BTC_SIGNAL is True) and (BTC_INNER_SIGNAL is False):
if curLSMA > prev1LSMA < prev2LSMA:
if (curRSI > curRSIEMA) and (curRSI > prev1RSI):
if (prev1RSI < 20) or (prev2RSI < 20):
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} *S5")
log(f" LSMA: {curLSMA} > {prev1LSMA} < {prev2LSMA}")
log(f" curRSI > curRSIEMA : {curRSI} > {curRSIEMA} curRSI > prev1RSI:{curRSI} > {prev1RSI}")
log(f" prev1RSI OR prev2RSI < 20: {prev1RSI} {prev2RSI}")
if curClose <= prev1High:
log(f" +++ curClose <= prev1High : {curClose} <= {prev1High} ")
glbExplanation = f" LSMA: {curLSMA} > {prev1LSMA} < {prev2LSMA} prev1RSI or prev2RSI < 20: {prev1RSI} {prev2RSI} "
return True
return False
def control_strategy_6(symbol=None, coinTicker=None, tickSize=None) -> (bool):
global glbExplanation
glbExplanation = ""
curOpen = coinTicker.opens[-1]
curClose = coinTicker.closes[-1]
curCandleColor = ind.get_candle_color(open=curOpen, close=curClose)
if curCandleColor == cons.CANDLE_RED:
return False
start = CANDLE_COUNT - AVERAGE_CANDLE_COUNT - 1
end = CANDLE_COUNT - 1
avgVolume = coinTicker.volumes[start:end].mean()
avgVolume = round(avgVolume, 1)
curVolume = coinTicker.volumes[-1]
volRatio = round(curVolume/avgVolume, 1)
if volRatio < 3.0:
return False
prevHigh = coinTicker.highs[-2]
if curClose > prevHigh:
return False
up, center, down, slope = ind.get_linear_regression(prices=coinTicker.closes, period=LR_PERIOD,
standard_deviation_up_factor=LR_STD_UP_FACTOR,
standard_deviation_down_factor=LR_STD_DOWN_FACTOR)
curLRUp = func.round_tick_size(price=up[-1], tick_size=tickSize)
if curClose > curLRUp:
return False
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} *S6")
log(f" curVolume: {curVolume} avgVolume: {avgVolume} volRatio: {volRatio}")
log(f" curClose: {curClose} prevHigh: {prevHigh} curLRUp: {curLRUp}")
glbExplanation = f" curVolume: {curVolume} avgVolume: {avgVolume} volRatio: {volRatio} prevHigh {prevHigh} curLRUp: {curLRUp} "
return True
def control_strategy_7(symbol=None, coinTicker=None, tickSize=None) -> (bool):
global glbExplanation
glbExplanation = ""
if BTC_RED_ALERT is True:
return False
if BTC_SIGNAL is False and BTC_INNER_SIGNAL is False:
return False
curColor = ind.get_candle_color(open=coinTicker.opens[-1], close=coinTicker.closes[-1])
prevColor = ind.get_candle_color(open=coinTicker.opens[-2], close=coinTicker.closes[-2])
curClose = coinTicker.closes[-1]
prevHigh = coinTicker.highs[-2]
prevOpen = coinTicker.opens[-2]
rsi = ind.get_rsi(prices=coinTicker.closes, timePeriod=10)
curRSI = rsi[-1]
prev1RSI = rsi[-2]
prev2RSI = rsi[-3]
prev3RSI = rsi[-4]
rsiMA = ind.get_ema(data=rsi, period=10)
curRSIMA = rsiMA[-1]
prev1RSIMA = rsiMA[-2]
prev2RSIMA = rsiMA[-3]
if prevColor == cons.CANDLE_GREEN:
priceLevel = prevHigh
else:
priceLevel = (prevHigh + prevOpen) / 2
if curColor == cons.CANDLE_GREEN:
if curRSI < 20:
if prev1RSI < prev1RSIMA and curRSI > curRSIMA:
if curRSI > prev1RSI and curRSIMA > prev1RSIMA:
if curClose <= priceLevel:
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} *S7")
log(f" curRSI < 40:{curRSI} ")
log(f" prev1RSI < prev1RSIMA and curRSI > curRSIMA : {prev1RSI} < {prev1RSIMA} AND {curRSI} > {curRSIMA} ")
log(f" curRSI > prev1RSI and curRSIMA > prev1RSIMA: {curRSI} > {prev1RSI} AND {curRSIMA} > {prev1RSIMA}")
log(f" +++ curClose <= priceLevel: {curClose} <= {priceLevel} ")
glbExplanation = f" {symbol} prev1RSI < prev1RSIMA and curRSI > curRSIMA : {prev1RSI} < {prev1RSIMA} AND {curRSI} > {curRSIMA} "
return True
return False
def control_strategy_8(symbol=None, coinTicker=None, tickSize=None) -> (bool):
global glbExplanation
glbExplanation = ""
if BTC_RED_ALERT is True:
return False
if BTC_SIGNAL is False and BTC_INNER_SIGNAL is False:
return False
curColor = ind.get_candle_color(open=coinTicker.opens[-1], close=coinTicker.closes[-1])
prevColor = ind.get_candle_color(open=coinTicker.opens[-2], close=coinTicker.closes[-2])
curClose = coinTicker.closes[-1]
prevHigh = coinTicker.highs[-2]
prevOpen = coinTicker.opens[-2]
rsi = ind.get_rsi(prices=coinTicker.closes, timePeriod=10)
curRSI = rsi[-1]
prev1RSI = rsi[-2]
prev2RSI = rsi[-3]
prev3RSI = rsi[-4]
rsiMA = ind.get_ema(data=rsi, period=10)
curRSIMA = rsiMA[-1]
prev1RSIMA = rsiMA[-2]
prev2RSIMA = rsiMA[-3]
if prevColor == cons.CANDLE_GREEN:
priceLevel = prevHigh
else:
priceLevel = (prevHigh + prevOpen) / 2
if curColor == cons.CANDLE_GREEN:
if curRSI < 30:
if prev1RSI < prev1RSIMA and curRSI > curRSIMA:
if curRSI > prev1RSI and curRSIMA > prev1RSIMA:
if curClose <= priceLevel:
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} *S8")
log(f" curRSI < 30:{curRSI} ")
log(f" prev1RSI < prev1RSIMA and curRSI > curRSIMA : {prev1RSI} < {prev1RSIMA} AND {curRSI} > {curRSIMA} ")
log(f" curRSI > prev1RSI and curRSIMA > prev1RSIMA: {curRSI} > {prev1RSI} AND {curRSIMA} > {prev1RSIMA}")
log(f" +++ curClose <= priceLevel: {curClose} <= {priceLevel} ")
glbExplanation = f" {symbol} prev1RSI < prev1RSIMA and curRSI > curRSIMA : {prev1RSI} < {prev1RSIMA} AND {curRSI} > {curRSIMA} "
return True
return False
def control_strategy_9(symbol=None, coinTicker=None, tickSize=None) -> (bool):
"""
STOCH hızlı yavaşı yukarı keser
hızlı eğim yukarı
yavaş eğim yukarı
hızlı veya yavaş eşik değerinin altında
RSI hızlı yavaşı yukarı keser
hızlı eğim yukarı
yavaş eğim yukarı
hızlı veya yavaş eşik değerinin altında
VWMA Fiyat(low) VWMA nın eşik değeri kadar altında
"""
global glbExplanation
glbExplanation = ""
if BTC_RED_ALERT == True:
return False
if (BTC_SIGNAL == False) and (BTC_INNER_SIGNAL == False):
return False
curColor = ind.get_candle_color(open=float(coinTicker.opens[-1]), close=float(coinTicker.closes[-1]))
if (curColor == cons.CANDLE_RED):
return False
# LR Kontrolleri
up, center, down, slope = ind.get_linear_regression(prices=coinTicker.closes, period=LR_PERIOD,
standard_deviation_up_factor=LR_STD_UP_FACTOR,
standard_deviation_down_factor=LR_STD_DOWN_FACTOR)
# LR alt ve üst bandı arasındaki yüksekliğin belli bir oranın üstünde olması aranır.
hFactor = LR_UP_HEIGHT
if slope < 0:
hFactor = LR_DOWN_HEIGHT
heightLR = func.calculate_ratio(float(up[-1]), float(down[-1]))
heightLRCond = heightLR > hFactor
if heightLRCond == False:
return False
# STOCHASTIC Kontrolleri
fastST, slowST = ind.get_stochastic(high=coinTicker.highs, low=coinTicker.lows, close=coinTicker.closes,
timePeriod=STOCH_PERIOD, slowKPeriod=3, slowDPeriod=3)
stochThreshold = 10.0
stochBelowThreshold = (fastST[-1] < stochThreshold) or (slowST[-1] < stochThreshold)
if (stochBelowThreshold == False):
return False
stochCrossOver = func.cross_over(fastST, slowST )
if (stochCrossOver == False):
return False
stochFastSlopeUp = fastST[-1] > fastST[-2]
if (stochFastSlopeUp == False):
return False
stochSlowSlopeUp = slowST[-1] > slowST[-2]
if (stochSlowSlopeUp == False):
return False
# RSI Kontrolleri
rsiFast = ind.get_rsi(prices=coinTicker.closes, timePeriod=RSI_PERIOD)
rsiSlow = ind.get_ema(data=rsiFast, period=RSI_PERIOD)
rsiThreshold = 50.0
rsiBelowThreshold = rsiFast[-1] < rsiThreshold
if (rsiBelowThreshold == False):
return False
rsiCrossOver = func.cross_over(rsiFast, rsiSlow)
if (rsiCrossOver == False):
return False
rsiFastSlopeUp = rsiFast[-1] > rsiFast[-2]
if (rsiFastSlopeUp == False):
return False
rsiSlowSlopeUp = rsiSlow[-1] > rsiSlow[-2]
if (rsiSlowSlopeUp == False):
return False
# VWMA Kontrolleri
vwap = ind.get_vwma(data=coinTicker.closes, volume=coinTicker.volumes, period=VWMA_PERIOD)
vwapThreshold = float(1.0)
if slope < 0:
vwapThreshold = float(3.0)
vwapHeight = func.calculate_ratio(vwap[-2], coinTicker.lows[-2])
priceUnderVWAPThreshold = vwapHeight > vwapThreshold
if (priceUnderVWAPThreshold == False):
return False
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} *S9")
log(f" STOCH stochBelowThreshold: {stochBelowThreshold} stochCrossUp: {stochCrossOver} stochFastSlopeUp: {stochFastSlopeUp} stochSlowSlopeUp: {stochSlowSlopeUp}")
log(f" RSI rsiBelowThreshold: {rsiBelowThreshold} rsiCrossUp: {rsiCrossOver} rsiFastSlopeUp: {rsiFastSlopeUp} rsiSlowSlopeUp: {rsiSlowSlopeUp}")
log(f" VWAP priceUnderVWAPThreshold: {priceUnderVWAPThreshold}")
glbExplanation = f" {symbol} vwapThreshold: {vwapThreshold} vwHeight: {vwapHeight} stochBelowThreshold: {stochBelowThreshold} rsiBelowThreshold: {rsiBelowThreshold} "
return True
def control_strategy_10(symbol=None, coinTicker=None, tickSize=None) -> (bool):
"""
VWMA Fiyat VWMA nın eşik değeri kadar altında
STOCH Hızlı veya yavaş eşik değerinin altında
Hızlı yavaşı yukarı keser
BB Bant genişliği eşik değerinin üzerinde
VWAP Fiyat(low), VWAP ın eşik değeri kadar altında
Fiyat(low ve high), VWAP çizgisinin altında
"""
global glbExplanation
glbExplanation = ""
if BTC_RED_ALERT == True:
return False
if (BTC_SIGNAL == False) and (BTC_INNER_SIGNAL == False):
return False
curColor = ind.get_candle_color(open=float(coinTicker.opens[-1]), close=float(coinTicker.closes[-1]))
if curColor == cons.CANDLE_RED:
return False
# LR Kontrolleri
up, center, down, slope = ind.get_linear_regression(prices=coinTicker.closes, period=LR_PERIOD,
standard_deviation_up_factor=LR_STD_UP_FACTOR,
standard_deviation_down_factor=LR_STD_DOWN_FACTOR)
# LR alt ve üst bandı arasındaki yüksekliğin belli bir oranın üstünde olması aranır.
hFactor = LR_UP_HEIGHT
if slope < 0:
hFactor = LR_DOWN_HEIGHT
heightLR = func.calculate_ratio(float(up[-1]), float(down[-1]))
heightLRCond = heightLR > hFactor
if heightLRCond == False:
return False
# VWMA Kontrolleri
vwap = ind.get_vwma(data=coinTicker.closes, volume=coinTicker.volumes, period=VWMA_PERIOD)
priceUnderVWAP = (vwap[-2] > float(coinTicker.highs[-2])) and (vwap[-1] > float(coinTicker.closes[-1]))
if (priceUnderVWAP == False):
return False
vwapThreshold = float(1.5)
if slope < 0:
vwapThreshold = float(3.0)
vwapHeight = func.calculate_ratio(vwap[-2], coinTicker.lows[-2])
priceUnderVWAPThreshold = vwapHeight > vwapThreshold
if (priceUnderVWAPThreshold == False):
return False
# STOCHASTIC Kontrolleri
fastST, slowST = ind.get_stochastic(high=coinTicker.highs, low=coinTicker.lows, close=coinTicker.closes,
timePeriod=STOCH_PERIOD, slowKPeriod=3, slowDPeriod=3)
stochThreshold = 15.0
stochBelowThreshold = (fastST[-1] < stochThreshold) or (slowST[-1] < stochThreshold)
if (stochBelowThreshold == False):
return False
stochCrossUp = func.cross_over(fastST, slowST)
if (stochCrossUp == False):
return False
log(f" {symbol} BTC_SIGNAL: {BTC_SIGNAL} BTC_INNER_SIGNAL: {BTC_INNER_SIGNAL} * S10 *")
log(f" heightLRCond: {heightLRCond} priceUnderVWAPThreshold: {priceUnderVWAPThreshold} stochCrossUp: {stochCrossUp}")
glbExplanation = f" {symbol} heightLRCond: {heightLRCond} priceUnderVWAPThreshold: {priceUnderVWAPThreshold} stochCrossUp: {stochCrossUp}"
return True
def control_strategy_11(symbol=None, coinTicker=None, tickSize=None) -> (bool):
# Fiyat, VWMA çizgisini yukarı kesiyor.
# STOCH hızlı, yavaşı yukarı kesiyor.
# RSI hzılı, yavaşı yukarı kesiyor.
global glbExplanation
glbExplanation = ""
if BTC_RED_ALERT is True:
return False
if BTC_SIGNAL is False and BTC_INNER_SIGNAL is False:
return False
curColor = ind.get_candle_color(open=coinTicker.opens[-1], close=coinTicker.closes[-1])
if curColor == cons.CANDLE_RED:
return False
curClose = coinTicker.closes[-1]
curLow = coinTicker.lows[-1]
curHigh = coinTicker.highs[-1]
prevHigh = coinTicker.highs[-2]
prevOpen = coinTicker.opens[-2]
prevLow = coinTicker.lows[-2]
vwap = ind.get_vwma(data=coinTicker.closes, volume=coinTicker.volumes, period=VWMA_PERIOD)
curVWAP = func.round_tick_size(price=vwap[-1], tick_size=tickSize)
prevVWAP = func.round_tick_size(price=vwap[-2], tick_size=tickSize)
vwSignal = False
# Fiyat VWAP çizgisini yukarı kesiyor
if curLow < curVWAP < curHigh:
if prevHigh < prevVWAP:
vwSignal = True
if vwSignal is False:
return False
rsi = ind.get_rsi(prices=coinTicker.closes, timePeriod=15)
curRSIFast = rsi[-1]
prevRSIFast = rsi[-2]
rsiMA = ind.get_ema(data=rsi, period=15)
curRSISlow = rsiMA[-1]
prevRSISlow = rsiMA[-2]
rsiSignal = False
# RSI hızlı, yavaşın üzerinde
# RSI slow eğim yukarı doğru
if curRSIFast > curRSISlow:
if curRSISlow > prevRSISlow:
rsiSignal = True
if rsiSignal is False:
return False
fast, slow = ind.get_stochastic(high=coinTicker.highs, low=coinTicker.lows, close=coinTicker.closes,
timePeriod=15, slowKPeriod=3, slowDPeriod=3)
curSTOCHFast = fast[-1]
curSTOCHSlow = slow[-1]
stochSignal = False
# STOCHASTIC hızlı, yavaşın üstünde
if curSTOCHFast > curSTOCHSlow:
stochSignal = True