Elffuss BraidHub

kikocis/freqtrade public

develop · 7.8 KiBRawPermalink
1# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
2
3from functools import reduce
4from typing import Any, Callable, Dict, List
5
6import talib.abstract as ta
7from pandas import DataFrame
8from skopt.space import Categorical, Dimension, Integer
9
10import freqtrade.vendor.qtpylib.indicators as qtpylib
11from freqtrade.optimize.hyperopt_interface import IHyperOpt
12
13
14class DefaultHyperOpt(IHyperOpt):
15 """
16 Default hyperopt provided by the Freqtrade bot.
17 You can override it with your own Hyperopt
18 """
19 @staticmethod
20 def populate_indicators(dataframe: DataFrame, metadata: dict) -> DataFrame:
21 """
22 Add several indicators needed for buy and sell strategies defined below.
23 """
24 # ADX
25 dataframe['adx'] = ta.ADX(dataframe)
26 # MACD
27 macd = ta.MACD(dataframe)
28 dataframe['macd'] = macd['macd']
29 dataframe['macdsignal'] = macd['macdsignal']
30 # MFI
31 dataframe['mfi'] = ta.MFI(dataframe)
32 # RSI
33 dataframe['rsi'] = ta.RSI(dataframe)
34 # Stochastic Fast
35 stoch_fast = ta.STOCHF(dataframe)
36 dataframe['fastd'] = stoch_fast['fastd']
37 # Minus-DI
38 dataframe['minus_di'] = ta.MINUS_DI(dataframe)
39 # Bollinger bands
40 bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
41 dataframe['bb_lowerband'] = bollinger['lower']
42 dataframe['bb_upperband'] = bollinger['upper']
43 # SAR
44 dataframe['sar'] = ta.SAR(dataframe)
45
46 return dataframe
47
48 @staticmethod
49 def buy_strategy_generator(params: Dict[str, Any]) -> Callable:
50 """
51 Define the buy strategy parameters to be used by Hyperopt.
52 """
53 def populate_buy_trend(dataframe: DataFrame, metadata: dict) -> DataFrame:
54 """
55 Buy strategy Hyperopt will build and use.
56 """
57 conditions = []
58
59 # GUARDS AND TRENDS
60 if 'mfi-enabled' in params and params['mfi-enabled']:
61 conditions.append(dataframe['mfi'] < params['mfi-value'])
62 if 'fastd-enabled' in params and params['fastd-enabled']:
63 conditions.append(dataframe['fastd'] < params['fastd-value'])
64 if 'adx-enabled' in params and params['adx-enabled']:
65 conditions.append(dataframe['adx'] > params['adx-value'])
66 if 'rsi-enabled' in params and params['rsi-enabled']:
67 conditions.append(dataframe['rsi'] < params['rsi-value'])
68
69 # TRIGGERS
70 if 'trigger' in params:
71 if params['trigger'] == 'bb_lower':
72 conditions.append(dataframe['close'] < dataframe['bb_lowerband'])
73 if params['trigger'] == 'macd_cross_signal':
74 conditions.append(qtpylib.crossed_above(
75 dataframe['macd'], dataframe['macdsignal']
76 ))
77 if params['trigger'] == 'sar_reversal':
78 conditions.append(qtpylib.crossed_above(
79 dataframe['close'], dataframe['sar']
80 ))
81
82 if conditions:
83 dataframe.loc[
84 reduce(lambda x, y: x & y, conditions),
85 'buy'] = 1
86
87 return dataframe
88
89 return populate_buy_trend
90
91 @staticmethod
92 def indicator_space() -> List[Dimension]:
93 """
94 Define your Hyperopt space for searching buy strategy parameters.
95 """
96 return [
97 Integer(10, 25, name='mfi-value'),
98 Integer(15, 45, name='fastd-value'),
99 Integer(20, 50, name='adx-value'),
100 Integer(20, 40, name='rsi-value'),
101 Categorical([True, False], name='mfi-enabled'),
102 Categorical([True, False], name='fastd-enabled'),
103 Categorical([True, False], name='adx-enabled'),
104 Categorical([True, False], name='rsi-enabled'),
105 Categorical(['bb_lower', 'macd_cross_signal', 'sar_reversal'], name='trigger')
106 ]
107
108 @staticmethod
109 def sell_strategy_generator(params: Dict[str, Any]) -> Callable:
110 """
111 Define the sell strategy parameters to be used by Hyperopt.
112 """
113 def populate_sell_trend(dataframe: DataFrame, metadata: dict) -> DataFrame:
114 """
115 Sell strategy Hyperopt will build and use.
116 """
117 conditions = []
118
119 # GUARDS AND TRENDS
120 if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']:
121 conditions.append(dataframe['mfi'] > params['sell-mfi-value'])
122 if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']:
123 conditions.append(dataframe['fastd'] > params['sell-fastd-value'])
124 if 'sell-adx-enabled' in params and params['sell-adx-enabled']:
125 conditions.append(dataframe['adx'] < params['sell-adx-value'])
126 if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']:
127 conditions.append(dataframe['rsi'] > params['sell-rsi-value'])
128
129 # TRIGGERS
130 if 'sell-trigger' in params:
131 if params['sell-trigger'] == 'sell-bb_upper':
132 conditions.append(dataframe['close'] > dataframe['bb_upperband'])
133 if params['sell-trigger'] == 'sell-macd_cross_signal':
134 conditions.append(qtpylib.crossed_above(
135 dataframe['macdsignal'], dataframe['macd']
136 ))
137 if params['sell-trigger'] == 'sell-sar_reversal':
138 conditions.append(qtpylib.crossed_above(
139 dataframe['sar'], dataframe['close']
140 ))
141
142 if conditions:
143 dataframe.loc[
144 reduce(lambda x, y: x & y, conditions),
145 'sell'] = 1
146
147 return dataframe
148
149 return populate_sell_trend
150
151 @staticmethod
152 def sell_indicator_space() -> List[Dimension]:
153 """
154 Define your Hyperopt space for searching sell strategy parameters.
155 """
156 return [
157 Integer(75, 100, name='sell-mfi-value'),
158 Integer(50, 100, name='sell-fastd-value'),
159 Integer(50, 100, name='sell-adx-value'),
160 Integer(60, 100, name='sell-rsi-value'),
161 Categorical([True, False], name='sell-mfi-enabled'),
162 Categorical([True, False], name='sell-fastd-enabled'),
163 Categorical([True, False], name='sell-adx-enabled'),
164 Categorical([True, False], name='sell-rsi-enabled'),
165 Categorical(['sell-bb_upper',
166 'sell-macd_cross_signal',
167 'sell-sar_reversal'], name='sell-trigger')
168 ]
169
170 def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
171 """
172 Based on TA indicators. Should be a copy of same method from strategy.
173 Must align to populate_indicators in this file.
174 Only used when --spaces does not include buy space.
175 """
176 dataframe.loc[
177 (
178 (dataframe['close'] < dataframe['bb_lowerband']) &
179 (dataframe['mfi'] < 16) &
180 (dataframe['adx'] > 25) &
181 (dataframe['rsi'] < 21)
182 ),
183 'buy'] = 1
184
185 return dataframe
186
187 def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
188 """
189 Based on TA indicators. Should be a copy of same method from strategy.
190 Must align to populate_indicators in this file.
191 Only used when --spaces does not include sell space.
192 """
193 dataframe.loc[
194 (
195 (qtpylib.crossed_above(
196 dataframe['macdsignal'], dataframe['macd']
197 )) &
198 (dataframe['fastd'] > 54)
199 ),
200 'sell'] = 1
201
202 return dataframe