-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBuyOnRecoveryAfterDropDuringGrowthTrendStrategy.py
74 lines (63 loc) · 2.67 KB
/
BuyOnRecoveryAfterDropDuringGrowthTrendStrategy.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
""" bot buy strategy file """
from lib.bot import Bot
from lib.coin import Coin
from lib.helpers import c_from_timestamp, logging, percent
class Strategy(Bot):
"""BuyOnRecoveryAfterDropDuringGrowthTrendStrategy"""
def buy_strategy(self, coin: Coin) -> bool:
"""BuyOnRecoveryAfterDropDuringGrowthTrendStrategy buy_strategy
This strategy looks for coins that have gone up by
KLINES_SLICE_PERCENTAGE_CHANGE in each slice (m,h,d) of the
KLINES_TREND_PERIOD.
Then it checkous that the current price for those is
lower than the BUY_AT_PERCENTAGE over the maximum price recorded.
if it is, then mark the coin as TARGET_DIP
and buy it as soon we're over the dip by TRAIL_RECOVERY_PERCENTAGE.
"""
unit = str(coin.klines_trend_period[-1:]).lower()
klines_trend_period = int(coin.klines_trend_period[:-1])
last_period = list(coin.averages[unit])[-klines_trend_period:]
# we need at least a full period of klines before we can
# make a buy decision
if len(last_period) < klines_trend_period:
return False
last_period_slice = last_period[0][1]
# if the price keeps going down, skip it
# we want to make sure the price has increased over n slices of the
# klines_trend_period (m, h, d) by klines_slice_percentage_change
# each time.
for _, n in last_period[1:]:
if (
percent(
100 + coin.klines_slice_percentage_change,
last_period_slice,
)
> n
):
return False
last_period_slice = n
# check if the maximum price recorded is now lower than the
# BUY_AT_PERCENTAGE
if (
coin.price < percent(coin.buy_at_percentage, coin.max)
and coin.status == ""
and not coin.naughty
):
coin.dip = coin.price
logging.info(
f"{c_from_timestamp(coin.date)}: {coin.symbol} [{coin.status}] "
+ f"-> [TARGET_DIP] ({coin.price})"
)
coin.status = "TARGET_DIP"
return False
if coin.status != "TARGET_DIP":
return False
# do some gimmicks, and don't buy the coin straight away
# but only buy it when the price is now higher than the last
# price recorded. This way we ensure that we got the dip
self.log_debug_coin(coin)
if coin.price > coin.last:
if coin.price > percent(coin.trail_recovery_percentage, coin.dip):
self.buy_coin(coin)
return True
return False