forked from AlexCryptoKing/freqailstm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTankAiRevivalFutures.py
1479 lines (1236 loc) · 66.1 KB
/
TankAiRevivalFutures.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 logging
from functools import reduce
import datetime
import talib.abstract as ta
import pandas_ta as pta
import numpy as np
import pandas as pd
import time
import freqtrade.vendor.qtpylib.indicators as qtpylib
from technical import qtpylib
from datetime import timedelta, datetime, timezone
from pandas import DataFrame, Series
from typing import Optional
from freqtrade.strategy.interface import IStrategy
from technical.pivots_points import pivots_points
from freqtrade.exchange import timeframe_to_prev_date, timeframe_to_minutes
from freqtrade.persistence import Trade
from freqtrade.strategy import (IStrategy, BooleanParameter, CategoricalParameter, DecimalParameter,
IntParameter, RealParameter, merge_informative_pair, stoploss_from_open,
stoploss_from_absolute, informative)
from scipy.signal import argrelextrema
from sklearn.feature_selection import VarianceThreshold
import gc
logger = logging.getLogger(__name__)
class TankAiRevival(IStrategy):
INTERFACE_VERSION = 3
def version(self) -> str:
return "v1.4.f"
exit_profit_only = True ### No selling at a loss
use_custom_stoploss = True
trailing_stop = False
position_adjustment_enable = True
ignore_roi_if_entry_signal = True
position_adjustment_enable = True
max_entry_position_adjustment = 2
max_dca_multiplier = 2.5
process_only_new_candles = True
can_short = True
use_exit_signal = True
startup_candle_count = 200
stoploss = -0.99
timeframe = '30m'
leverage_value = 3.0
locked_stoploss = {}
minimal_roi = {}
plot_config = {
"main_plot": {},
"subplots": {
"extrema": {
"&s-extrema": {
"color": "#f53580",
"type": "line"
},
"&s-minima_sort_threshold": {
"color": "#4ae747",
"type": "line"
},
"&s-maxima_sort_threshold": {
"color": "#5b5e4b",
"type": "line"
}
},
"min_max": {
"maxima": {
"color": "#a29db9",
"type": "line"
},
"minima": {
"color": "#ac7fc",
"type": "line"
},
"maxima_check": {
"color": "#a29db9",
"type": "line"
},
"minima_check": {
"color": "#ac7fc",
"type": "line"
}
}
}
}
# DCA
position_adjustment_enable = True
max_epa = IntParameter(0, 6, default = 1 ,space='buy', optimize=True, load=True) # of additional buys.
max_dca_multiplier = DecimalParameter(low=1.0, high=15, default=3, decimals=1 ,space='buy', optimize=True, load=True)
use_static = BooleanParameter(default=True, space="buy", optimize=True, load=True)
filldelay = IntParameter(100, 300, default = 100 ,space='buy', optimize=True, load=True)
max_entry_position_adjustment = max_epa.value
# Stake size adjustments
stake0 = DecimalParameter(low=0.7, high=1.5, default=1.0, decimals=1 ,space='buy', optimize=True, load=True)
stake1 = DecimalParameter(low=0.7, high=1.5, default=1.0, decimals=1 ,space='buy', optimize=True, load=True)
stake2 = DecimalParameter(low=0.7, high=1.5, default=1.0, decimals=1 ,space='buy', optimize=True, load=True)
stake3 = DecimalParameter(low=0.7, high=1.5, default=1.0, decimals=1 ,space='buy', optimize=True, load=True)
### Custom Functions
# Threshold and Limits 30m/15m = 2x multiplier, 30/5 = 6x multiplier
u_window_size = IntParameter(100, 160, default=100, space='buy', optimize=True)
l_window_size = IntParameter(5, 40, default=20, space='buy', optimize=True)
dc_x = DecimalParameter(low=3.0, high=5.0, default=4.5, decimals=1 ,space='buy', optimize=True, load=True)
# Custom Entry
increment = DecimalParameter(low=1.0005, high=1.002, default=1.001, decimals=4 ,space='buy', optimize=True, load=True)
last_entry_price = None
# protections
cooldown_lookback = IntParameter(24, 48, default=12, space="protection", optimize=True)
stop_duration = IntParameter(12, 200, default=5, space="protection", optimize=True)
use_stop_protection = BooleanParameter(default=True, space="protection", optimize=True)
# negative stoploss
use_stop1 = BooleanParameter(default=False, space="protection", optimize=True, load=True)
use_stop2 = BooleanParameter(default=False, space="protection", optimize=True, load=True)
use_stop3 = BooleanParameter(default=False, space="protection", optimize=True, load=True)
use_stop4 = BooleanParameter(default=False, space="protection", optimize=True, load=True)
# roi
time0 = IntParameter(low=1440, high=2600, default=1440, space='sell', optimize=True, load=True)
time1 = IntParameter(low=1440, high=2600, default=2000, space='sell', optimize=True, load=True)
time2 = IntParameter(low=2600, high=4000, default=3200, space='sell', optimize=True, load=True)
time3 = IntParameter(low=2500, high=5000, default=4500, space='sell', optimize=True, load=True)
# block specific exit timer
time4 = IntParameter(low=240, high=480, default=240, space='sell', optimize=True, load=True)
# Logic Selection
use0 = BooleanParameter(default=False, space="sell", optimize=True, load=True)
use1 = BooleanParameter(default=False, space="sell", optimize=True, load=True)
use2 = BooleanParameter(default=True, space="sell", optimize=True, load=True)
# use3 = BooleanParameter(default=True, space="buy", optimize=True, load=True)
# use4 = BooleanParameter(default=True, space="buy", optimize=True, load=True)
# use5 = BooleanParameter(default=True, space="buy", optimize=True, load=True)
# use6 = BooleanParameter(default=True, space="buy", optimize=True, load=True)
# use7 = BooleanParameter(default=True, space="buy", optimize=True, load=True)
# use8 = BooleanParameter(default=True, space="sell", optimize=True, load=True)
# use9 = BooleanParameter(default=True, space="sell", optimize=True, load=True)
# use10 = BooleanParameter(default=True, space="sell", optimize=True, load=True)
# use11 = BooleanParameter(default=True, space="sell", optimize=True, load=True)
# use12 = BooleanParameter(default=True, space="sell", optimize=True, load=True)
# use13 = BooleanParameter(default=True, space="sell", optimize=True, load=True)
def __init__(self, config: dict) -> None:
super().__init__(config) # Initialize the base class
self.cp = 80
self.h0 = 40
self.h1 = 27
self.h2 = 20
### protections ###
@property
def protections(self):
prot = []
prot.append({
"method": "CooldownPeriod",
"stop_duration_candles": self.cooldown_lookback.value
})
if self.use_stop_protection.value:
prot.append({
"method": "StoplossGuard",
"lookback_period_candles": 24 * 3,
"trade_limit": 2,
"stop_duration_candles": self.stop_duration.value,
"only_per_pair": False
})
return prot
### Dollar Cost Averaging ###
### Custom Functions ###
# This is called when placing the initial order (opening trade)
# Let unlimited stakes leave funds open for DCA orders
def custom_stake_amount(
self, pair: str, current_time: datetime, current_rate: float,
proposed_stake: float, min_stake: Optional[float], max_stake: float,
leverage: float, entry_tag: Optional[str], side: str,
**kwargs
) -> float:
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
current_candle = dataframe.iloc[-1].squeeze()
pair_tag = f'_gen_{pair}_{self.timeframe}'
pair_tag = pair_tag.replace(':USDT', 'USDT')
#EP0 = current_candle[f'lower_envelope{pair_tag}']
#EP1 = current_candle[f'lower_envelope_h0{pair_tag}']
#EP2 = current_candle[f'lower_envelope_h1{pair_tag}']
#EP3 = current_candle[f'lower_envelope_h2{pair_tag}']
if side == 'long':
EP0 = current_candle[f'%%-lower_envelope{pair_tag}']
EP1 = current_candle[f'%%-lower_envelope_h0{pair_tag}']
EP2 = current_candle[f'%%-lower_envelope_h1{pair_tag}']
EP3 = current_candle[f'%%-lower_envelope_h2{pair_tag}']
elif side == 'short':
EP0 = current_candle[f'%%-upper_envelope{pair_tag}']
EP1 = current_candle[f'%%-upper_envelope_h0{pair_tag}']
EP2 = current_candle[f'%%-upper_envelope_h1{pair_tag}']
EP3 = current_candle[f'%%-upper_envelope_h2{pair_tag}']
# Define balance thresholds and stake multipliers
#balance = self.wallets.get_total_balance()
balance = self.wallets.get_total_stake_amount()
if balance < 3000:
balance_multiplier = 0.8
self.config['max_open_trades'] = 3
elif balance < 7500:
balance_multiplier = 0.9 # Increase stake by 20%
self.config['max_open_trades'] = 6
elif balance < 10000:
balance_multiplier = 1.0 # No increase
self.config['max_open_trades'] = 10
elif balance < 15000:
balance_multiplier = 1.2 # Increase stake by 20%
self.config['max_open_trades'] = 12
elif balance < 20000:
balance_multiplier = 1.5 # Increase stake by 50%
self.config['max_open_trades'] = 15
else:
balance_multiplier = 2.0 # Double stake for larger balances
# Static or calculated staking
if self.use_static.value == 'True':
return (self.wallets.get_total_stake_amount() / self.config["max_open_trades"]) * balance_multiplier
num_tr = self.config['max_open_trades']
if side == 'long':
# Adjust stake based on DCA and balance multiplier
if current_rate < EP0:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * balance_multiplier
elif EP0 <= current_rate < EP1:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * self.stake3.value * balance_multiplier
elif EP1 <= current_rate < EP2:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * self.stake2.value * balance_multiplier
elif EP2 <= current_rate < EP3:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * self.stake1.value * balance_multiplier
else:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * self.stake0.value * balance_multiplier
logger.info(f'{pair} using {calculated_stake} instead of {proposed_stake} | Trade Slots: {num_tr} Balance X:{balance_multiplier}')
self.dp.send_msg(f'{pair} using {calculated_stake} instead of {proposed_stake} | Trade Slots: {num_tr} Balance X:{balance_multiplier}')
elif side == 'short':
# Adjust stake based on DCA and balance multiplier
if current_rate > EP0:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * balance_multiplier
elif EP0 <= current_rate > EP1:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * self.stake3.value * balance_multiplier
elif EP1 <= current_rate > EP2:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * self.stake2.value * balance_multiplier
elif EP2 <= current_rate > EP3:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * self.stake1.value * balance_multiplier
else:
calculated_stake = (proposed_stake / self.max_dca_multiplier.value) * self.stake0.value * balance_multiplier
logger.info(f'{pair} using {calculated_stake} instead of {proposed_stake} | Trade Slots: {num_tr} Balance X:{balance_multiplier}')
self.dp.send_msg(f'{pair} using {calculated_stake} instead of {proposed_stake} | Trade Slots: {num_tr} Balance X:{balance_multiplier}')
return min(max(calculated_stake, min_stake or 0), max_stake)
def adjust_trade_position(self, trade: Trade, current_time: datetime,
current_rate: float, current_profit: float,
min_stake: Optional[float], max_stake: float,
current_entry_rate: float, current_exit_rate: float,
current_entry_profit: float, current_exit_profit: float,
**kwargs) -> Optional[float]:
dataframe, _ = self.dp.get_analyzed_dataframe(trade.pair, self.timeframe)
filled_entries = trade.select_filled_orders(trade.entry_side)
count_of_entries = trade.nr_of_successful_entries
trade_duration = (current_time - trade.open_date_utc).seconds / 60
last_fill = (current_time - trade.date_last_filled_utc).seconds / 60
if dataframe is not None:
current_candle = dataframe.iloc[-1].squeeze()
pair_tag = f'_gen_{trade.pair}_{self.timeframe}'
pair_tag = pair_tag.replace(':USDT', 'USDT')
#print([col for col in dataframe.columns if '%%-h2_move_mean' in col])
TP0 = current_candle[f'%%-h2_move_mean{pair_tag}']
TP1 = current_candle[f'%%-h1_move_mean{pair_tag}']
TP2 = current_candle[f'%%-h0_move_mean{pair_tag}']
TP3 = current_candle[f'%%-cycle_move_mean{pair_tag}']
display_profit = current_profit * 100
if current_candle['enter_long'] is not None:
signal = current_candle['enter_long']
if current_profit is not None:
logger.info(f"{trade.pair} - Current Profit: {display_profit:.3}%% # of Entries: {trade.nr_of_successful_entries}")
# Take Profit if m00n
if current_profit > TP2 and trade.nr_of_successful_exits == 0:
# Take quarter of the profit at next fib%%
return -(trade.stake_amount / 2) # modified from 4 to 2
if current_profit > TP3 and trade.nr_of_successful_exits == 1:
# Take half of the profit at last fib%%
return -(trade.stake_amount / 4)
if current_profit > (TP3 * 1.5) and trade.nr_of_successful_exits == 2:
# Take half of the profit at last fib%%
return -(trade.stake_amount / 2)
if current_profit > (TP3 * 2.0) and trade.nr_of_successful_exits == 3:
# Take half of the profit at last fib%%
return -(trade.stake_amount)
if trade.nr_of_successful_entries == self.max_epa.value + 1:
return None
# Block concurrent buys
if current_profit > -TP1:
return None
try:
# This returns first order stake size
# Modify the following parameters to enable more levels or different buy size:
# max_entry_position_adjustment = 3
# max_dca_multiplier = 3.5
stake_amount = filled_entries[0].cost
# This then calculates current safety order size
if (last_fill > self.filldelay.value):
if (signal == 1 and current_profit < -TP1):
if count_of_entries >= 1:
stake_amount = stake_amount * 2
else:
stake_amount = stake_amount
return stake_amount
# This accommadates a one shot at buying the dip on a big wick with one
# large buy if the funds are available...
if (last_fill > self.filldelay.value):
if (current_profit < -TP3):
if count_of_entries == 1:
stake_amount = stake_amount * 4
else:
stake_amount = stake_amount
return stake_amount
except Exception as exception:
return None
return None
### Trailing Stop ###
def custom_stoploss(self, pair: str, trade: Trade, current_time: datetime, current_rate: float,
current_profit: float, after_fill: bool, length: int = 14, multiplier: float = 1.5, **kwargs) -> float:
if after_fill:
return stoploss_from_open(self.stoploss, current_profit, is_short=trade.is_short, leverage=trade.leverage)
dataframe, _ = self.dp.get_analyzed_dataframe(pair=pair, timeframe=self.timeframe)
current_candle = dataframe.iloc[-1].squeeze()
entry_time = timeframe_to_prev_date(self.timeframe, trade.open_date_utc)
timeframe_minutes = timeframe_to_minutes(self.timeframe)
signal_time = entry_time - timedelta(minutes=int(timeframe_minutes))
signal_candle = dataframe.loc[dataframe['date'] == signal_time]
if not signal_candle.empty:
signal_candle = signal_candle.iloc[-1].squeeze() # Only squeeze if it's not empty
else:
signal_candle = None # Handle the case where there's no matching candle
trade_duration = (current_time - trade.open_date_utc).seconds / 60
pair_tag = f'_gen_{pair}_{self.timeframe}'
pair_tag = pair_tag.replace(':USDT', 'USDT')
SLT0 = current_candle[f'%%-h2_move_mean{pair_tag}'] * trade.leverage
SLT1 = current_candle[f'%%-h1_move_mean{pair_tag}'] * trade.leverage
SLT2 = current_candle[f'%%-h0_move_mean{pair_tag}'] * trade.leverage
SLT3 = current_candle[f'%%-cycle_move_mean{pair_tag}'] * trade.leverage
display_profit = current_profit * 100
if current_profit < -0.01:
if pair in self.locked_stoploss:
del self.locked_stoploss[pair]
self.dp.send_msg(f'*** {pair} *** Stoploss reset.')
logger.info(f'*** {pair} *** Stoploss reset.')
return stoploss_from_open(self.stoploss, current_profit, is_short=trade.is_short, leverage=trade.leverage)
new_stoploss = None
if SLT3 is not None and current_profit > SLT3:
new_stoploss = (SLT3 - SLT2)
level = 4
elif SLT2 is not None and current_profit > SLT2:
new_stoploss = (SLT2 - SLT1)
level = 3
# in the future toggle these on certain conditions with indicators.
elif SLT1 is not None and current_profit > SLT1 and self.use1.value == True:
new_stoploss = (SLT1 - SLT0)
level = 2
elif SLT0 is not None and current_profit > SLT0 and self.use0.value == True:
new_stoploss = (SLT1 - SLT0)
level = 1
if new_stoploss is not None:
if pair not in self.locked_stoploss or new_stoploss > self.locked_stoploss[pair]:
self.locked_stoploss[pair] = new_stoploss
self.dp.send_msg(f'*** {pair} *** Profit {level} {display_profit:.3f}%% - New stoploss: {new_stoploss:.4f} activated')
logger.info(f'*** {pair} *** Profit {level} {display_profit:.3f}%% - New stoploss: {new_stoploss:.4f} activated')
return stoploss_from_open(self.locked_stoploss[pair], current_profit, is_short=trade.is_short, leverage=trade.leverage)
return None
def custom_entry_price(self, pair: str, trade: Optional['Trade'], current_time: datetime, proposed_rate: float,
entry_tag: Optional[str], side: str, **kwargs) -> float:
dataframe, last_updated = self.dp.get_analyzed_dataframe(pair=pair,
timeframe=self.timeframe)
entry_price = (dataframe['close'].iat[-1] + dataframe['open'].iat[-1] + proposed_rate + proposed_rate) / 4
logger.info(f"{pair} Using Entry Price: {entry_price} | close: {dataframe['close'].iat[-1]} open: {dataframe['open'].iat[-1]} proposed_rate: {proposed_rate}")
if entry_tag == 'enter_long':
if self.last_entry_price is not None and abs(entry_price - self.last_entry_price) < 0.0001: # Tolerance for floating-point comparison
entry_price *= self.increment.value # Increment by 0.2%
logger.info(f'{pair} Incremented entry price: {entry_price} based on previous entry price : {self.last_entry_price}.')
elif entry_tag == 'enter_short':
# Check if there is a stored last entry price and if it matches the proposed entry price
if self.last_entry_price is not None and abs(entry_price - self.last_entry_price) < 0.0001: # Tolerance for floating-point comparison
entry_price /= self.increment.value # Increment by 0.2%
logger.info(f'{pair} Incremented entry price: {entry_price} based on previous entry price : {self.last_entry_price}.')
# Update the last entry price
self.last_entry_price = entry_price
return entry_price
# Custom_Exits
def custom_exit(self, pair: str, trade: 'Trade', current_time: 'datetime', current_rate: float, current_profit: float, **kwargs):
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
last_candle = dataframe.iloc[-1].squeeze()
filled_entries = trade.select_filled_orders(trade.entry_side)
count_of_entries = trade.nr_of_successful_entries
trade_duration = (current_time - trade.open_date_utc).seconds / 60
last_fill = (current_time - trade.date_last_filled_utc).seconds / 60
current_candle = dataframe.iloc[-1].squeeze()
pair_tag = f'_gen_{pair}_{self.timeframe}'
pair_tag = pair_tag.replace(':USDT', 'USDT')
TP0 = current_candle[f'%%-h2_move_mean{pair_tag}'] / trade.leverage
TP1 = current_candle[f'%%-h1_move_mean{pair_tag}'] / trade.leverage
TP2 = current_candle[f'%%-h0_move_mean{pair_tag}'] / trade.leverage
TP3 = current_candle[f'%%-cycle_move_mean{pair_tag}'] / trade.leverage
### roi ###
if current_profit > TP3 and trade_duration > self.time0.value:
return 'Roi 0 - Easy $$$'
if current_profit > TP2 and trade_duration > self.time1.value:
return 'Roi 1 - ol reliable'
if current_profit > TP1 and trade_duration > self.time2.value:
return 'Roi 2 - Avg Joe'
if current_profit > TP0 and trade_duration > self.time3.value:
return 'Roi 3 - Better than Nothing'
### negative stoploss ###
if current_profit < -TP3 and self.use_stop1.value == True:
return 'Failsafe 3 - REKTd'
if current_profit < -TP2 and self.use_stop2.value == True:
return 'Failsafe 2 - Ooo that hurts'
if current_profit < -TP1 and self.use_stop3.value == True:
return 'Failsafe 1 - Leverage is risky'
if current_profit < -TP0 and self.use_stop4.value == True:
return 'Failsafe 0 - Wasnt a good idea...'
return False
def confirm_trade_exit(self, pair: str, trade: Trade, order_type: str, amount: float,
rate: float, time_in_force: str, exit_reason: str,
current_time: datetime, **kwargs) -> bool:
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
last_candle = dataframe.iloc[-1].squeeze()
trade_duration = (current_time - trade.open_date_utc).seconds / 60
last_candle = dataframe.iloc[-1].squeeze()
pair_tag = f'_gen_{pair}_{self.timeframe}'
pair_tag = pair_tag.replace(':USDT', 'USDT')
hodl = last_candle[f'%%-frac_HODL{pair_tag}']
# Handle freak events
if hodl == 1:
logger.info(f"{trade.pair} HODL!!!")
return False
if exit_reason.startswith('roi') and trade.calc_profit_ratio(rate) < 0.003:
logger.info(f"{trade.pair} ROI is below 0")
# self.dp.send_msg(f'{trade.pair} ROI is below 0')
return False
if exit_reason.startswith('partial_exit') and trade.calc_profit_ratio(rate) < 0:
logger.info(f"{trade.pair} partial exit is below 0")
# self.dp.send_msg(f'{trade.pair} partial exit is below 0')
return False
if exit_reason.startswith('trailing_stop_loss') and trade.calc_profit_ratio(rate) < 0:
logger.info(f"{trade.pair} trailing stop price is below 0")
# self.dp.send_msg(f'{trade.pair} trailing stop price is below 0')
return False
# Block quick extrema for a mimimum duration of trades...
# Trust the model for a 2 tap entry at most and 12h - 7d hold time (optimum cook time 12-36hrs)
if exit_reason.startswith('Extrema M') and trade_duration > self.time4.value: # Extrema Minima... or Extrema Maxima...
logger.info(f"{trade.pair} blocking small extrema!!!")
# self.dp.send_msg(f'{trade.pair} trailing stop price is below 0')
return False
if exit_reason.endswith('Full Send') and trade_duration > self.time4.value: #...Full Send
logger.info(f"{trade.pair} blocking small extrema!!!")
# self.dp.send_msg(f'{trade.pair} trailing stop price is below 0')
return False
return True
def feature_engineering_expand_all(self, dataframe, period, **kwargs):
# We do this in expand basic using Hurst Cycle Theory and FFT
return dataframe
def feature_engineering_expand_basic(self, dataframe, metadata, **kwargs):
#selector = VarianceThreshold(threshold=1e-8)
start_time = time.time()
pair = metadata['pair']
dataframe["%%-raw_volume"] = dataframe["volume"]
dataframe["%%-obv"] = ta.OBV(dataframe['close'], dataframe['volume'])
# Williams R%%
dataframe['%%-willr14'] = pta.willr(dataframe['high'], dataframe['low'], dataframe['close'])
# VWAP
vwap_low, vwap, vwap_high = VWAPB(dataframe, 20, 1)
dataframe['%%-vwap_upperband'] = vwap_high
dataframe['%%-vwap_middleband'] = vwap
dataframe['%%-vwap_lowerband'] = vwap_low
dataframe['%%-vwap_width'] = ((dataframe['%%-vwap_upperband'] -
dataframe['%%-vwap_lowerband']) / dataframe['%%-vwap_middleband']) * 100
#dataframe = dataframe.copy() ### dataframe isolation mode
dataframe['%%-dist_to_vwap_upperband'] = get_distance(dataframe['close'], dataframe['%%-vwap_upperband'])
dataframe['%%-dist_to_vwap_middleband'] = get_distance(dataframe['close'], dataframe['%%-vwap_middleband'])
dataframe['%%-dist_to_vwap_lowerband'] = get_distance(dataframe['close'], dataframe['%%-vwap_lowerband'])
dataframe['%%-tail'] = (dataframe['close'] - dataframe['low']).abs()
dataframe['%%-wick'] = (dataframe['high'] - dataframe['close']).abs()
dataframe['%%-raw_close'] = dataframe['close']
dataframe["%%-pct-change"] = dataframe["close"].pct_change()
dataframe["%%-raw_volume"] = dataframe["volume"]
dataframe["%%-raw_price"] = dataframe["close"]
dataframe["%%-raw_open"] = dataframe["open"]
dataframe["%%-raw_low"] = dataframe["low"]
dataframe["%%-raw_high"] = dataframe["high"]
heikinashi = qtpylib.heikinashi(dataframe)
dataframe['%%-ha_open'] = heikinashi['open']
dataframe['%%-ha_close'] = heikinashi['close']
dataframe['%%-ha_high'] = heikinashi['high']
dataframe['%%-ha_low'] = heikinashi['low']
dataframe['%%-ha_closedelta'] = (heikinashi['close'] - heikinashi['close'].shift())
dataframe['%%-ha_tail'] = (heikinashi['close'] - heikinashi['low'])
dataframe['%%-ha_wick'] = (heikinashi['high'] - heikinashi['close'])
dataframe['%%-ha_HLC3'] = (heikinashi['high'] + heikinashi['low'] + heikinashi['close'])/3
# Initialize Hurst Cycles for startup errors
cycle_period = 80
harmonics = [0, 0, 0]
harmonics[0] = 40
harmonics[1] = 27
harmonics[2] = 20
if len(dataframe) < self.u_window_size.value:
raise ValueError(f"Insufficient data points for FFT: {len(dataframe)}. Need at least {self.u_window_size.value} data points.")
# Perform FFT to identify cycles with a rolling window
freq, power = perform_fft(dataframe['%%-ha_close'], window_size=self.u_window_size.value)
if len(freq) == 0 or len(power) == 0:
raise ValueError("FFT resulted in zero or invalid frequencies. Check the data or the FFT implementation.")
# Filter out the zero-frequency component and limit the frequency to below 500
# positive_mask = (freq > 0) & (1 / freq < self.u_window_size.value)
positive_mask = (1 / freq > self.l_window_size.value) & (1 / freq < self.u_window_size.value)
positive_freqs = freq[positive_mask]
positive_power = power[positive_mask]
# Convert frequencies to periods
cycle_periods = 1 / positive_freqs
# Set a threshold to filter out insignificant cycles based on power
power_threshold = 0.01 * np.max(positive_power)
significant_indices = positive_power > power_threshold
significant_periods = cycle_periods[significant_indices]
significant_power = positive_power[significant_indices]
# Identify the dominant cycle
dominant_freq_index = np.argmax(significant_power)
dominant_freq = positive_freqs[dominant_freq_index]
logger.info(f'{pair} Hurst Exponent: {dominant_freq}')
cycle_period = int(np.abs(1 / dominant_freq)) if dominant_freq != 0 else 100
if cycle_period == np.inf:
raise ValueError("No dominant frequency found. Check the data or the method used.")
# Calculate harmonics for the dominant cycle
harmonics = [cycle_period / (i + 1) for i in range(1, 4)]
# print(cycle_period, harmonics)
self.cp = int(cycle_period)
self.h0 = int(harmonics[0])
self.h1 = int(harmonics[1])
self.h2 = int(harmonics[2])
dataframe['%%-dc_EWM'] = dataframe['%%-ha_close'].ewm(span=int(cycle_period)).mean()
dataframe['%%-dc_1/2'] = dataframe['%%-ha_close'].ewm(span=int(harmonics[0])).mean()
dataframe['%%-dc_1/3'] = dataframe['%%-ha_close'].ewm(span=int(harmonics[1])).mean()
dataframe['%%-dc_1/4'] = dataframe['%%-ha_close'].ewm(span=int(harmonics[2])).mean()
# Fractional Brownian Motion (fBm)
n = len(dataframe['%%-dc_EWM'])
h = dominant_freq #0.5
t = 1
dt = t / n
fBm = np.zeros(n)
for i in range(1, n):
fBm[i] = fBm[i-1] + np.sqrt(dt) * np.random.normal(0, 1)
dataframe['%%-fBm'] = fBm * (dt ** h)
# Fractional differentiation
dataframe['%%-frac_diff'] = np.gradient(dataframe['%%-ha_close'])
dataframe['%%-fBm_mean'] = np.mean(dataframe['%%-fBm']) + 2 * np.std(dataframe['%%-fBm'])
dataframe['%%-frac_diff_norm'] = indicator_normalization(dataframe, col='%%-frac_diff', length=int(cycle_period), norm_range='minus_one_to_one')
dataframe['%%-frac_sma_dc'] = dataframe['%%-frac_diff'].ewm(span=int(harmonics[1])).mean()
dataframe['%%-signal_UP_dc'] = np.where(dataframe['%%-frac_sma_dc'] > 0, dataframe['%%-frac_sma_dc'], 0)
dataframe['%%-signal_DN_dc'] = np.where(dataframe['%%-frac_sma_dc'] < 0, dataframe['%%-frac_sma_dc'], 0)
dataframe['%%-signal_UP_dc'] = dataframe['%%-signal_UP_dc'].ffill()
dataframe['%%-signal_DN_dc'] = dataframe['%%-signal_DN_dc'].ffill()
if self.dp.runmode.value in ('live', 'dry_run'):
dataframe['%%-signal_MEAN_UP_dc'] = dataframe['%%-signal_UP_dc'].rolling(int(cycle_period)).mean() * self.dc_x.value
dataframe['%%-signal_MEAN_DN_dc'] = dataframe['%%-signal_DN_dc'].rolling(int(cycle_period)).mean() * self.dc_x.value
else:
# this step is for hyperopting and backtesting to simulate the rolling window of the exchange in live mode.
# this needs to be a value specific for YOUR exchange and/or timeframe.
dataframe['%%-signal_MEAN_UP_dc'] = dataframe['%%-signal_UP_dc'].rolling(700).mean() * self.dc_x.value
dataframe['%%-signal_MEAN_DN_dc'] = dataframe['%%-signal_DN_dc'].rolling(700).mean() * self.dc_x.value
dataframe['%%-signal_buy'] = np.where((dataframe['%%-frac_sma_dc'] > 0) & (dataframe['%%-frac_sma_dc'].shift() < 0), 1, 0)
dataframe['%%-signal_sell'] = np.where((dataframe['%%-frac_sma_dc'] < 0) & (dataframe['%%-frac_sma_dc'].shift() > 0), -1, 0)
dataframe['%%-frac_HODL'] = np.where(dataframe['%%-frac_sma_dc'] > dataframe['%%-signal_MEAN_UP_dc'], 1, 0)
dataframe['%%-frac_y33t'] = np.where(dataframe['%%-frac_sma_dc'] < dataframe['%%-signal_MEAN_DN_dc'], -1, 0)
# Apply rolling window operation to the 'OHLC4' column
rolling_windowc = dataframe['%%-ha_close'].rolling(cycle_period)
rolling_windowh0 = dataframe['%%-ha_close'].rolling(int(harmonics[0]))
rolling_windowh1 = dataframe['%%-ha_close'].rolling(int(harmonics[1]))
rolling_windowh2 = dataframe['%%-ha_close'].rolling(int(harmonics[2]))
# Calculate the peak-to-peak value on the resulting rolling window data
ptp_valuec = rolling_windowc.apply(lambda x: np.ptp(x))
ptp_valueh0 = rolling_windowh0.apply(lambda x: np.ptp(x))
ptp_valueh1 = rolling_windowh1.apply(lambda x: np.ptp(x))
ptp_valueh2 = rolling_windowh2.apply(lambda x: np.ptp(x))
# Assign the calculated peak-to-peak value to the DataFrame column
dataframe['cycle_move'] = ptp_valuec / dataframe['%%-ha_close']
dataframe['h0_move'] = ptp_valueh0 / dataframe['%%-ha_close']
dataframe['h1_move'] = ptp_valueh1 / dataframe['%%-ha_close']
dataframe['h2_move'] = ptp_valueh2 / dataframe['%%-ha_close']
dataframe['%%-cycle_move'] = dataframe['cycle_move']
dataframe['%%-h0_move'] = dataframe['h0_move']
dataframe['%%-h1_move'] = dataframe['h1_move']
dataframe['%%-h2_move'] = dataframe['h2_move']
if self.dp.runmode.value in ('live', 'dry_run'):
dataframe['cycle_move_mean'] = dataframe['cycle_move'].mean()
dataframe['h0_move_mean'] = dataframe['h0_move'].mean()
dataframe['h1_move_mean'] = dataframe['h1_move'].mean()
dataframe['h2_move_mean'] = dataframe['h2_move'].mean()
else:
# this step is for hyperopting and backtesting to simulate the rolling window of the exchange in live mode.
# this needs to be a value specific for YOUR exchange and/or timeframe.
dataframe['cycle_move_mean'] = dataframe['cycle_move'].rolling(700).mean()
dataframe['h0_move_mean'] = dataframe['h0_move'].rolling(700).mean()
dataframe['h1_move_mean'] = dataframe['h1_move'].rolling(700).mean()
dataframe['h2_move_mean'] = dataframe['h2_move'].rolling(700).mean()
dataframe['%%-cycle_move_mean'] = dataframe['cycle_move_mean']
dataframe['%%-h0_move_mean'] = dataframe['h0_move_mean']
dataframe['%%-h1_move_mean'] = dataframe['h1_move_mean']
dataframe['%%-h2_move_mean'] = dataframe['h2_move_mean']
###########
# Add envelopes for the dominant cycle
dataframe['%%-upper_envelope'] = dataframe['%%-dc_EWM'] * (1 + dataframe['%%-cycle_move_mean'])
dataframe['%%-lower_envelope'] = dataframe['%%-dc_EWM'] * (1 - dataframe['%%-cycle_move_mean'])
dataframe['%%-upper_envelope_h0'] = dataframe['%%-dc_EWM'] * (1 + dataframe['%%-h0_move_mean'])
dataframe['%%-lower_envelope_h0'] = dataframe['%%-dc_EWM'] * (1 - dataframe['%%-h0_move_mean'])
dataframe['%%-upper_envelope_h1'] = dataframe['%%-dc_EWM'] * (1 + dataframe['%%-h1_move_mean'])
dataframe['%%-lower_envelope_h1'] = dataframe['%%-dc_EWM'] * (1 - dataframe['%%-h1_move_mean'])
dataframe['%%-upper_envelope_h2'] = dataframe['%%-dc_EWM'] * (1 + dataframe['%%-h2_move_mean'])
dataframe['%%-lower_envelope_h2'] = dataframe['%%-dc_EWM'] * (1 - dataframe['%%-h2_move_mean'])
############
dataframe['%%-candle_size'] = abs((dataframe['high'] - dataframe['low']) / dataframe['low'])
dataframe['%%-candle_size_lower'] = dataframe['%%-candle_size'].rolling(window=int(harmonics[2])).mean()
dataframe['%%-candle_size_upper'] = (dataframe['%%-candle_size'].rolling(window=int(harmonics[2])).mean()) * 2
dataframe["%%-rsi"] = ta.RSI(dataframe, timeperiod=int(harmonics[2]))
dataframe["%%-mfi-period"] = ta.MFI(dataframe, timeperiod=int(cycle_period))
dataframe["%%-rocr-period"] = ta.ROCR(dataframe, timeperiod=int(harmonics[2]))
dataframe["%%-cmf-period"] = chaikin_mf(dataframe, periods=int(cycle_period))
dataframe["%%-chop-period"] = qtpylib.chopiness(dataframe, int(harmonics[2]))
dataframe["%%-linear-period"] = ta.LINEARREG_ANGLE(
dataframe['close'], timeperiod=int(harmonics[2]))
dataframe["%%-atr-period"] = ta.ATR(dataframe, timeperiod=int(harmonics[2]))
dataframe["%%-atr-periodp"] = dataframe["%%-atr-period"] / \
dataframe['close'] * 1000
# Calculate the percentage change between the high and open prices for each 30-minute candle
dataframe['%%-perc_change'] = (dataframe['high'] / dataframe['open'] - 1) * 100
# Create a custom indicator that checks if any of the past 30-minute candles' high price is 3%% or more above the open price
dataframe['%%-candle_1perc_50'] = dataframe['%%-perc_change'].rolling(50).apply(lambda x: np.where(x >= 1, 1, 0).sum()).shift()
dataframe['%%-candle_2perc_50'] = dataframe['%%-perc_change'].rolling(50).apply(lambda x: np.where(x >= 2, 1, 0).sum()).shift()
dataframe['%%-candle_3perc_50'] = dataframe['%%-perc_change'].rolling(50).apply(lambda x: np.where(x >= 3, 1, 0).sum()).shift()
dataframe['%%-candle_5perc_50'] = dataframe['%%-perc_change'].rolling(50).apply(lambda x: np.where(x >= 5, 1, 0).sum()).shift()
dataframe['%%-candle_-1perc_50'] = dataframe['%%-perc_change'].rolling(50).apply(lambda x: np.where(x <= -1, -1, 0).sum()).shift()
dataframe['%%-candle_-2perc_50'] = dataframe['%%-perc_change'].rolling(50).apply(lambda x: np.where(x <= -2, -1, 0).sum()).shift()
dataframe['%%-candle_-3perc_50'] = dataframe['%%-perc_change'].rolling(50).apply(lambda x: np.where(x <= -3, -1, 0).sum()).shift()
dataframe['%%-candle_-5perc_50'] = dataframe['%%-perc_change'].rolling(50).apply(lambda x: np.where(x <= -5, -1, 0).sum()).shift()
# Calculate the percentage of the current candle's range where the close price is
dataframe['%%-close_percentage'] = (dataframe['close'] - dataframe['low']) / (dataframe['high'] - dataframe['low'])
dataframe['%%-body_size'] = abs(dataframe['open'] - dataframe['close'])
dataframe['%%-range_size'] = dataframe['high'] - dataframe['low']
dataframe['%%-body_range_ratio'] = dataframe['%%-body_size'] / dataframe['%%-range_size']
dataframe['%%-upper_wick_size'] = dataframe['high'] - dataframe[['open', 'close']].max(axis=1)
dataframe['%%-upper_wick_range_ratio'] = dataframe['%%-upper_wick_size'] / dataframe['%%-range_size']
#lookback_period = 10
dataframe['%%-max_high'] = dataframe['high'].rolling(50).max()
dataframe['%%-min_low'] = dataframe['low'].rolling(50).min()
dataframe['%%-close_position'] = (dataframe['close'] - dataframe['%%-min_low']) / (dataframe['%%-max_high'] - dataframe['%%-min_low'])
dataframe['%%-current_candle_perc_change'] = (dataframe['high'] / dataframe['open'] - 1) * 100
# Lazy Bear Impulse Macd
dataframe['%%-hi'] = ta.SMA(dataframe['high'], timeperiod = 28)
dataframe['%%-lo'] = ta.SMA(dataframe['low'], timeperiod = 28)
dataframe['%%-ema1'] = ta.EMA(dataframe['%%-ha_HLC3'], timeperiod = 28)
dataframe['%%-ema2'] = ta.EMA(dataframe['%%-ema1'], timeperiod = 28)
dataframe['%%-d'] = dataframe['%%-ema1'] - dataframe['%%-ema2']
dataframe['%%-mi'] = dataframe['%%-ema1'] + dataframe['%%-d']
dataframe['%%-md'] = np.where(dataframe['%%-mi'] > dataframe['%%-hi'],
dataframe['%%-mi'] - dataframe['%%-hi'],
np.where(dataframe['%%-mi'] < dataframe['%%-lo'],
dataframe['%%-mi'] - dataframe['%%-lo'], 0))
dataframe['%%-sb'] = ta.SMA(dataframe['%%-md'], timeperiod = 8)
dataframe['%%-sh'] = dataframe['%%-md'] - dataframe['%%-sb']
########### Avoid fragmentation
dataframe = dataframe.copy() ### dataframe isolation mode
###########
# WaveTrend using OHLC4 or HA close - 3/21
ap = dataframe['%%-ha_HLC3']
dataframe['esa'] = ta.EMA(ap, timeperiod = 9)
dataframe['d'] = ta.EMA(abs(ap - dataframe['esa']), timeperiod = 9)
dataframe['%%-wave_ci'] = (ap-dataframe['esa']) / (0.015 * dataframe['d'])
dataframe['%%-wave_t1'] = ta.EMA(dataframe['%%-wave_ci'], timeperiod = 12)
dataframe['%%-wave_t2'] = ta.SMA(dataframe['%%-wave_t1'], timeperiod = 4)
# 200 SMA and distance
dataframe['%%-200sma'] = ta.SMA(dataframe, timeperiod = 200)
dataframe['%%-200sma_dist'] = get_distance(heikinashi["close"], dataframe['%%-200sma'])
h = int(harmonics[2])
r = 8.0
x_0 = int(cycle_period)
smoothColors = False
lag = 0
# nadaraya_watson
dataframe = nadaraya_watson(dataframe, h, r, x_0, smoothColors, lag, mult = 2.5)
#calculate_pnf_with_percentage
box_size_percentage = dataframe['h2_move_mean'].iloc[-1]
reversal_size = box_size_percentage * 0.5
dataframe = calculate_pnf_with_percentage(dataframe, box_size_percentage, reversal_size)
# More appropiate data fix
#dataframe.fillna(method='ffill', inplace=True)
#dataframe.fillna(method='bfill', inplace=True)
if not self.dp.runmode.value in ("backtest", "plot", "hyperopt"):
logger.info(f'{pair} - DC: {cycle_period:.2f} | 1/2: {harmonics[0]:.2f} | 1/3: {harmonics[1]:.2f} | 1/4: {harmonics[2]:.2f}')
end_time = time.time()
logger.info(f"Indicators done for {pair} in {end_time - start_time:.2f} secs")
return dataframe
def feature_engineering_standard(self, dataframe, **kwargs):
dataframe["%%-day_of_week"] = (dataframe["date"].dt.dayofweek + 1) / 7
dataframe["%%-hour_of_day"] = (dataframe["date"].dt.hour + 1) / 25
return dataframe
def set_freqai_targets(self, dataframe, **kwargs):
cycle_period = self.cp
dataframe["&s-extrema"] = 0
min_peaks = argrelextrema(
dataframe["close"].values, np.less,
order=cycle_period
)
max_peaks = argrelextrema(
dataframe["close"].values, np.greater,
order=cycle_period
)
for mp in min_peaks[0]:
dataframe.at[mp, "&s-extrema"] = -1
for mp in max_peaks[0]:
dataframe.at[mp, "&s-extrema"] = 1
dataframe['&s-extrema'] = dataframe['&s-extrema'].rolling(window=5, win_type='gaussian', center=True).mean(std=0.5)
# predict the expected range
dataframe['&-s_max'] = dataframe["close"].shift(-cycle_period).rolling(cycle_period).max()/dataframe["close"] - 1
dataframe['&-s_min'] = dataframe["close"].shift(-cycle_period).rolling(cycle_period).min()/dataframe["close"] - 1
return dataframe
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
cycle_period = self.cp
dataframe = self.freqai.start(dataframe, metadata, self)
dataframe["minima"] = 0
dataframe["maxima"] = 0
min_peaks = argrelextrema(
dataframe["&s-extrema"].values, np.less,
order=cycle_period
)
max_peaks = argrelextrema(
dataframe["&s-extrema"].values, np.greater,
order=cycle_period
)
for mp in min_peaks[0]:
dataframe.at[mp, "minima"] = 1
for mp in max_peaks[0]:
dataframe.at[mp, "maxima"] = 1
dataframe["DI_catch"] = np.where(
dataframe["DI_values"] > dataframe["DI_cutoff"], 0, 1,
)
dataframe["minima_sort_threshold"] = dataframe["&s-minima_sort_threshold"]
dataframe["maxima_sort_threshold"] = dataframe["&s-maxima_sort_threshold"]
dataframe["min_sort_threshold"] = dataframe["&-s_min"].mean()
dataframe["max_sort_threshold"] = dataframe["&-s_max"].mean()
dataframe['maxima_check'] = dataframe['maxima'].rolling(cycle_period).apply(lambda x: int((x != 1).all()), raw=True).fillna(0)
dataframe['minima_check'] = dataframe['minima'].rolling(cycle_period).apply(lambda x: int((x != 1).all()), raw=True).fillna(0)
dataframe["uptrend"] = np.where(dataframe["&s-extrema"] < 0, 1, -1)
dataframe["extrema_cross_up"] = np.where(((dataframe["&s-extrema"].shift() < 0) & (dataframe["&s-extrema"] >= 0)), 1, 0)
dataframe["extrema_cross_dn"] = np.where(((dataframe["&s-extrema"].shift() > 0) & (dataframe["&s-extrema"] <= 0)), -1, 0)
dataframe['BOP'] = (ta.SMA(dataframe['close'], cycle_period) - ta.SMA(dataframe['open'], cycle_period)) / (ta.SMA(dataframe['high'], cycle_period) - ta.SMA(dataframe['low'], cycle_period))
dataframe['zero'] = 0
gc.collect()
return dataframe
def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
pair = metadata['pair']
current_df, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
if not current_df.empty:
current_candle = current_df.iloc[-1].squeeze()
pair_tag = f'_gen_{pair}_{self.timeframe}'
pair_tag = pair_tag.replace(':USDT', 'USDT')
##### LONG
df.loc[
(
(df["do_predict"] == 1) &
(df["DI_catch"] == 1) &
(df["&s-extrema"] < df["minima_sort_threshold"]) &
(df['volume'] > 0) # Make sure Volume is not 0
),
['enter_long', 'enter_tag']] = (1, 'Extrema Minima')
df.loc[
(
(df["do_predict"] == 0) &
# (df["DI_catch"] == 1) &
# (df["extrema_cross"] == 1) &
(df["&s-extrema"] < df["minima_sort_threshold"]) &
# (df["minima"].shift(1) == 1) &
(df['volume'] > 0) # Make sure Volume is not 0
),
['enter_long', 'enter_tag']] = (1, 'Extrema Minima Full Send')
df.loc[
(
(df["do_predict"] == 1) &
(df["DI_catch"] == 1) &
# (df["maxima_check"] == 1) &
# (df["&s-extrema"] < df["minima_sort_threshold"]) &
(df["minima"].shift(1) == 1) &
(df['volume'] > 0) # Make sure Volume is not 0
),
['enter_long', 'enter_tag']] = (1, 'Minima')
df.loc[
(
(df["do_predict"] == 0) &
# (df["DI_catch"] == 1) &
# (df["maxima_check"] == 1) &
# (df["&s-extrema"] < df["minima_sort_threshold"]) &
# (df["&s-extrema"] < df["&-s_min"]) &
(df["minima"].shift(1) == 1) &
(df['volume'] > 0) # Make sure Volume is not 0
),
['enter_long', 'enter_tag']] = (1, 'Minima Full Send')
###### SHORT
df.loc[
(
(df["do_predict"] == 1) &
(df["DI_catch"] == 1) &
(df["&s-extrema"] > df["maxima_sort_threshold"]) &
(df['volume'] > 0) # Make sure Volume is not 0
),
['enter_short', 'enter_tag']] = (1, 'Extrema Maxima')
df.loc[
(
(df["do_predict"] == 0) &
# (df["DI_catch"] == 1) &
# (df["extrema_cross"] == 1) &
(df["&s-extrema"] > df["maxima_sort_threshold"]) &
# (df["maxima"].shift(1) == 1) &
(df['volume'] > 0) # Make sure Volume is not 0
),
['enter_short', 'enter_tag']] = (1, 'Extrema Maxima Full Send')
df.loc[
(
(df["do_predict"] == 1) &
(df["DI_catch"] == 1) &
# (df["minima_check"] == 1) &
# (df["&s-extrema"] > df["maxima_sort_threshold"]) &
(df["maxima"].shift(1) == 1) &
(df['volume'] > 0) # Make sure Volume is not 0
),
['enter_short', 'enter_tag']] = (1, 'Maxima')
df.loc[
(
(df["do_predict"] == 0) &
#(df["DI_catch"] == 1) &
# (df["minima_check"] == 1) &
# (df["&s-extrema"] > df["maxima_sort_threshold"]) &