-
Notifications
You must be signed in to change notification settings - Fork 82
/
ClucCrypROI.py
292 lines (237 loc) · 9.99 KB
/
ClucCrypROI.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
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy as np
from functools import reduce
import talib.abstract as ta
from freqtrade.strategy.interface import IStrategy
from freqtrade.strategy import merge_informative_pair
from datetime import datetime
from freqtrade.persistence import Trade
from pandas import DataFrame, Series
class ClucCrypROI(IStrategy):
# Used for "informative pairs"
fiat = 'USD'
startup_candle_count: int = 48
def informative_pairs(self):
"""
Add informative pairs as follows
coin to fiat @ same candle as base strategy
stake to fiat @ same candle as base strategy
"""
pairs = self.dp.current_whitelist()
informative_pairs = []
for pair in pairs:
coin, stake = pair.split('/')
coin_fiat = f"{coin}/{self.fiat}"
informative_pairs += [(coin_fiat, self.timeframe)]
stake_fiat = f"{stake}/{self.fiat}"
informative_pairs += [(stake_fiat, self.timeframe)]
return informative_pairs
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# Set Up Bollinger Bands
upper_bb1, mid_bb1, lower_bb1 = ta.BBANDS(dataframe['close'], timeperiod=36)
upper_bb2, mid_bb2, lower_bb2 = ta.BBANDS(qtpylib.typical_price(dataframe), timeperiod=12)
# Only putting some bands into dataframe as the others are not used elsewhere in the strategy
dataframe['lower-bb1'] = lower_bb1
dataframe['lower-bb2'] = lower_bb2
dataframe['mid-bb2'] = mid_bb2
dataframe['bb1-delta'] = (mid_bb1 - dataframe['lower-bb1']).abs()
dataframe['closedelta'] = (dataframe['close'] - dataframe['close'].shift()).abs()
dataframe['tail'] = (dataframe['close'] - dataframe['low']).abs()
# Additional indicators
dataframe['ema_fast'] = ta.EMA(dataframe['close'], timeperiod=6)
dataframe['ema_slow'] = ta.EMA(dataframe['close'], timeperiod=48)
dataframe['volume_mean_slow'] = dataframe['volume'].rolling(window=24).mean()
dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
# Inverse Fisher transform on RSI: values [-1.0, 1.0] (https://goo.gl/2JGGoy)
rsi = 0.1 * (dataframe['rsi'] - 50)
dataframe['fisher-rsi'] = (np.exp(2 * rsi) - 1) / (np.exp(2 * rsi) + 1)
# Informative Pair Indicators
coin, stake = metadata['pair'].split('/')
fiat = self.fiat
stake_fiat = f"{stake}/{self.fiat}"
coin_fiat = f"{coin}/{self.fiat}"
coin_fiat_inf = self.dp.get_pair_dataframe(pair=f"{coin}/{fiat}", timeframe=self.timeframe)
dataframe['coin-fiat-adx'] = ta.ADX(coin_fiat_inf, timeperiod=21)
coin_aroon = ta.AROON(coin_fiat_inf, timeperiod=25)
dataframe['coin-fiat-aroon-down'] = coin_aroon['aroondown']
dataframe['coin-fiat-aroon-up'] = coin_aroon['aroonup']
stake_fiat_inf = self.dp.get_pair_dataframe(pair=f"{stake}/{fiat}", timeframe=self.timeframe)
dataframe['stake-fiat-adx'] = ta.ADX(stake_fiat_inf, timeperiod=21)
stake_aroon = ta.AROON(stake_fiat_inf, timeperiod=25)
dataframe['stake-fiat-aroon-down'] = stake_aroon['aroondown']
dataframe['stake-fiat-aroon-up'] = stake_aroon['aroonup']
# These indicators are used to persist a buy signal in live trading only
# They dramatically slow backtesting down
if self.config['runmode'].value in ('live', 'dry_run'):
dataframe['sar'] = ta.SAR(dataframe)
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
params = self.buy_params
active_trade = False
if self.config['runmode'].value in ('live', 'dry_run'):
active_trade = Trade.get_trades([Trade.pair == metadata['pair'], Trade.is_open.is_(True),]).all()
conditions = []
"""
If this is a fresh buy, apple additional conditions.
Idea is to leverage "ignore_roi_if_buy_signal = True" functionality by using certain
indicators for active trades while applying additional protections to new trades.
"""
if not active_trade:
if 'stake-fiat-adx' in dataframe.columns and 'coin-fiat-adx' in dataframe.columns:
conditions.append(
((
(dataframe['stake-fiat-adx'] > params['adx']) &
(dataframe['stake-fiat-aroon-down'] > params['aroon'])
) | (
(dataframe['stake-fiat-adx'] < params['adx'])
)) & ((
(dataframe['coin-fiat-adx'] > params['adx']) &
(dataframe['coin-fiat-aroon-up'] > params['aroon'])
) | (
(dataframe['coin-fiat-adx'] < params['adx'])
))
)
conditions.append(
(
dataframe['bb1-delta'].gt(dataframe['close'] * params['bbdelta-close']) &
dataframe['closedelta'].gt(dataframe['close'] * params['closedelta-close']) &
dataframe['tail'].lt(dataframe['bb1-delta'] * params['bbdelta-tail']) &
dataframe['close'].lt(dataframe['lower-bb1'].shift()) &
dataframe['close'].le(dataframe['close'].shift())
) |
(
(dataframe['close'] < dataframe['ema_slow']) &
(dataframe['close'] < params['close-bblower'] * dataframe['lower-bb2']) &
(dataframe['volume'] < (dataframe['volume_mean_slow'].shift(1) * params['volume']))
)
)
else:
conditions.append(dataframe['close'] > dataframe['close'].shift())
conditions.append(dataframe['close'] > dataframe['sar'])
conditions.append(dataframe['volume'].gt(0))
if conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
'buy'] = 1
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
params = self.sell_params
conditions = []
if 'stake-fiat-adx' in dataframe.columns and 'coin-fiat-adx' in dataframe.columns:
conditions.append(
((
(dataframe['stake-fiat-adx'] > params['sell-adx']) &
(dataframe['stake-fiat-aroon-up'] > params['sell-aroon'])
) | (
(dataframe['stake-fiat-adx'] < params['sell-adx'])
)) & ((
(dataframe['coin-fiat-adx'] > params['sell-adx']) &
(dataframe['coin-fiat-aroon-down'] > params['sell-aroon'])
) | (
(dataframe['coin-fiat-adx'] < params['sell-adx'])
))
)
conditions.append((dataframe['close'] * params['sell-bbmiddle-close']) > dataframe['mid-bb2'])
conditions.append(dataframe['ema_fast'].gt(dataframe['close']))
conditions.append(dataframe['volume'].gt(0))
if conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
'sell'] = 1
return dataframe
"""
https://www.freqtrade.io/en/latest/strategy-advanced/
Custom Order Timeouts
"""
def check_buy_timeout(self, pair: str, trade: Trade, order: dict, **kwargs) -> bool:
ob = self.dp.orderbook(pair, 1)
current_price = ob['bids'][0][0]
# Cancel buy order if price is more than 1% above the order.
if current_price > order['price'] * 1.01:
return True
return False
def check_sell_timeout(self, pair: str, trade: Trade, order: dict, **kwargs) -> bool:
ob = self.dp.orderbook(pair, 1)
current_price = ob['asks'][0][0]
# Cancel sell order if price is more than 1% below the order.
if current_price < order['price'] * 0.99:
return True
return False
# Overriding strategy with optimized values when ETH is the stake currency
class ClucCrypROI_ETH(ClucCrypROI):
timeframe = '15m'
use_sell_signal = True
sell_profit_only = False
ignore_roi_if_buy_signal = True
# Buy hyperspace params:
buy_params = {
'adx': 46,
'aroon': 51,
'bbdelta-close': 0.01775,
'bbdelta-tail': 0.84013,
'close-bblower': 0.01096,
'closedelta-close': 0.01068,
'volume': 15
}
# Sell hyperspace params:
sell_params = {
'sell-adx': 39,
'sell-aroon': 65,
'sell-bbmiddle-close': 0.98656
}
# ROI table:
minimal_roi = {
"0": 0.11487,
"4": 0.10783,
"5": 0.07209,
"15": 0.03179,
"60": 0.01044,
"112": 0.00541,
"270": 0
}
# Stoploss:
stoploss = -0.32238
# Trailing stop:
trailing_stop = True
trailing_stop_positive = 0.0102
trailing_stop_positive_offset = 0.02872
trailing_only_offset_is_reached = False
# Overriding strategy with optimized values when BTC is the stake currency
class ClucCrypROI_BTC(ClucCrypROI):
timeframe = '15m'
use_sell_signal = True
sell_profit_only = False
ignore_roi_if_buy_signal = True
# Buy hyperspace params:
buy_params = {
'adx': 47,
'aroon': 34,
'bbdelta-close': 0.01957,
'bbdelta-tail': 0.86961,
'close-bblower': 0.00257,
'closedelta-close': 0.01381,
'volume': 27
}
# Sell hyperspace params:
sell_params = {
'sell-adx': 20,
'sell-aroon': 63,
'sell-bbmiddle-close': 0.95563
}
# ROI table:
minimal_roi = {
"0": 0.08449,
"7": 0.04432,
"28": 0.0387,
"30": 0.0137,
"145": 0.0086,
"318": 0.00344,
"600": 0
}
# Stoploss:
stoploss = -0.33807
# Trailing stop:
trailing_stop = True
trailing_stop_positive = 0.01001
trailing_stop_positive_offset = 0.02495
trailing_only_offset_is_reached = False