-
Notifications
You must be signed in to change notification settings - Fork 17
/
CombinedBinHAndClucV2.py
161 lines (132 loc) · 5.98 KB
/
CombinedBinHAndClucV2.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
# --- Do not remove these libs ---
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy as np
# --------------------------------
import talib.abstract as ta
from freqtrade.strategy import IStrategy, merge_informative_pair
from pandas import DataFrame
# The main idea is to buy only when overall uptrend in higher informative
# but in local dip cause BinCluc some king of pullback strategy.
def bollinger_bands(stock_price, window_size, num_of_std):
rolling_mean = stock_price.rolling(window=window_size).mean()
rolling_std = stock_price.rolling(window=window_size).std()
lower_band = rolling_mean - (rolling_std * num_of_std)
return np.nan_to_num(rolling_mean), np.nan_to_num(lower_band)
def SSLChannels(dataframe, length = 7):
df = dataframe.copy()
df['ATR'] = ta.ATR(df, timeperiod=14)
df['smaHigh'] = df['high'].rolling(length).mean() + df['ATR']
df['smaLow'] = df['low'].rolling(length).mean() - df['ATR']
df['hlv'] = np.where(df['close'] > df['smaHigh'], 1, np.where(df['close'] < df['smaLow'], -1, np.NAN))
df['hlv'] = df['hlv'].ffill()
df['sslDown'] = np.where(df['hlv'] < 0, df['smaHigh'], df['smaLow'])
df['sslUp'] = np.where(df['hlv'] < 0, df['smaLow'], df['smaHigh'])
return df['sslDown'], df['sslUp']
class CombinedBinHAndClucV2(IStrategy):
minimal_roi = {
'120': 0.01,
'60': 0.02,
'30': 0.05,
'0': 0.1
}
informative_timeframe = '1h'
timeframe = '5m'
stoploss = -0.05
use_sell_signal = True
sell_profit_only = False
ignore_roi_if_buy_signal = True
protections = [
{
"method": "StoplossGuard",
"lookback_period_candles": 24,
"trade_limit": 2,
"stop_duration_candles": 12,
"only_per_pair": True
}
]
startup_candle_count = 200
def informative_pairs(self):
pairs = self.dp.current_whitelist()
informative_pairs = [(pair, self.informative_timeframe) for pair in pairs]
return informative_pairs
@staticmethod
def get_informative_indicators(dataframe: DataFrame, metadata: dict):
ssl_down, ssl_up = SSLChannels(dataframe, 25)
dataframe['ssl_down'] = ssl_down
dataframe['ssl_up'] = ssl_up
dataframe['ssl_high'] = (ssl_up > ssl_down).astype('int') * 3
dataframe['mfi'] = ta.MFI(dataframe, timeperiod=30)
stoch = ta.STOCHRSI(dataframe, 30, 35, 2, 2)
dataframe['srsi_fk'] = stoch['fastk']
dataframe['srsi_fd'] = stoch['fastd']
# dataframe['rsi'] = ta.RSI(dataframe, timeperiod=3)
dataframe['go_long'] = (
(dataframe['ssl_high'] > 0)
&
(dataframe['mfi'].shift().rolling(3).mean() > dataframe['mfi'])
&
(dataframe['srsi_fk'].shift().rolling(3).mean() > dataframe['srsi_fk'])
).astype('int') * 4
return dataframe
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
if not self.dp:
return dataframe
informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=self.informative_timeframe)
informative = self.get_informative_indicators(informative.copy(), metadata)
dataframe = merge_informative_pair(dataframe, informative, self.timeframe, self.informative_timeframe,
ffill=True)
# don't overwrite the base dataframe's HLCV information
skip_columns = [(s + "_" + self.informative_timeframe) for s in
['date', 'open', 'high', 'low', 'close', 'volume']]
dataframe.rename(
columns=lambda s: s.replace("_{}".format(self.informative_timeframe), "") if (not s in skip_columns) else s,
inplace=True)
typical_price = qtpylib.typical_price(dataframe)
# strategy BinHV45
bollinger_b = qtpylib.bollinger_bands(typical_price, window=40, stds=2)
mid = bollinger_b['mid']
lower = bollinger_b['lower']
dataframe['lower'] = lower
dataframe['bbdelta'] = (mid - dataframe['lower']).abs()
dataframe['closedelta'] = (dataframe['close'] - dataframe['close'].shift()).abs()
dataframe['tail'] = (dataframe['close'] - dataframe['low']).abs()
# strategy ClucMay72018
bollinger_c = qtpylib.bollinger_bands(typical_price, window=20, stds=2)
dataframe['bb_lowerband'] = bollinger_c['lower']
dataframe['bb_middleband'] = bollinger_c['mid']
dataframe['bb_upperband'] = bollinger_c['upper']
dataframe['ema_slow'] = ta.EMA(dataframe, timeperiod=50)
dataframe['volume_mean_slow'] = dataframe['volume'].rolling(window=30).mean()
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[
(dataframe['go_long'] > 0)
&
(
( # strategy BinHV45
dataframe['lower'].shift().gt(0) &
dataframe['bbdelta'].gt(dataframe['close'] * 0.008) &
dataframe['closedelta'].gt(dataframe['close'] * 0.0175) &
dataframe['tail'].lt(dataframe['bbdelta'] * 0.25) &
dataframe['close'].lt(dataframe['lower'].shift()) &
dataframe['close'].le(dataframe['close'].shift())
) |
( # strategy ClucMay72018
(dataframe['close'] < dataframe['ema_slow']) &
(dataframe['close'] < 0.985 * dataframe['bb_lowerband']) &
(dataframe['volume'] < (dataframe['volume_mean_slow'].shift(1) * 20))
)
),
'buy'
] = 1
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
"""
dataframe.loc[
(
(qtpylib.crossed_below(dataframe['close'], dataframe['bb_upperband']))
),
'sell'
] = 1
return dataframe