| 1 | ## Protections |
| 2 | |
| 3 | !!! Warning "Beta feature" |
| 4 | This feature is still in it's testing phase. Should you notice something you think is wrong please let us know via Discord, Slack or via Github Issue. |
| 5 | |
| 6 | Protections will protect your strategy from unexpected events and market conditions by temporarily stop trading for either one pair, or for all pairs. |
| 7 | All protection end times are rounded up to the next candle to avoid sudden, unexpected intra-candle buys. |
| 8 | |
| 9 | !!! Note |
| 10 | Not all Protections will work for all strategies, and parameters will need to be tuned for your strategy to improve performance. |
| 11 | To align your protection with your strategy, you can define protections in the strategy. |
| 12 | |
| 13 | !!! Tip |
| 14 | Each Protection can be configured multiple times with different parameters, to allow different levels of protection (short-term / long-term). |
| 15 | |
| 16 | !!! Note "Backtesting" |
| 17 | Protections are supported by backtesting and hyperopt, but must be explicitly enabled by using the `--enable-protections` flag. |
| 18 | |
| 19 | ### Available Protections |
| 20 | |
| 21 | * [`StoplossGuard`](#stoploss-guard) Stop trading if a certain amount of stoploss occurred within a certain time window. |
| 22 | * [`MaxDrawdown`](#maxdrawdown) Stop trading if max-drawdown is reached. |
| 23 | * [`LowProfitPairs`](#low-profit-pairs) Lock pairs with low profits |
| 24 | * [`CooldownPeriod`](#cooldown-period) Don't enter a trade right after selling a trade. |
| 25 | |
| 26 | ### Common settings to all Protections |
| 27 | |
| 28 | | Parameter| Description | |
| 29 | |------------|-------------| |
| 30 | | `method` | Protection name to use. <br> **Datatype:** String, selected from [available Protections](#available-protections) |
| 31 | | `stop_duration_candles` | For how many candles should the lock be set? <br> **Datatype:** Positive integer (in candles) |
| 32 | | `stop_duration` | how many minutes should protections be locked. <br>Cannot be used together with `stop_duration_candles`. <br> **Datatype:** Float (in minutes) |
| 33 | | `lookback_period_candles` | Only trades that completed within the last `lookback_period_candles` candles will be considered. This setting may be ignored by some Protections. <br> **Datatype:** Positive integer (in candles). |
| 34 | | `lookback_period` | Only trades that completed after `current_time - lookback_period` will be considered. <br>Cannot be used together with `lookback_period_candles`. <br>This setting may be ignored by some Protections. <br> **Datatype:** Float (in minutes) |
| 35 | | `trade_limit` | Number of trades required at minimum (not used by all Protections). <br> **Datatype:** Positive integer |
| 36 | |
| 37 | !!! Note "Durations" |
| 38 | Durations (`stop_duration*` and `lookback_period*` can be defined in either minutes or candles). |
| 39 | For more flexibility when testing different timeframes, all below examples will use the "candle" definition. |
| 40 | |
| 41 | #### Stoploss Guard |
| 42 | |
| 43 | `StoplossGuard` selects all trades within `lookback_period` in minutes (or in candles when using `lookback_period_candles`). |
| 44 | If `trade_limit` or more trades resulted in stoploss, trading will stop for `stop_duration` in minutes (or in candles when using `stop_duration_candles`). |
| 45 | |
| 46 | This applies across all pairs, unless `only_per_pair` is set to true, which will then only look at one pair at a time. |
| 47 | |
| 48 | The below example stops trading for all pairs for 4 candles after the last trade if the bot hit stoploss 4 times within the last 24 candles. |
| 49 | |
| 50 | ```json |
| 51 | "protections": [ |
| 52 | { |
| 53 | "method": "StoplossGuard", |
| 54 | "lookback_period_candles": 24, |
| 55 | "trade_limit": 4, |
| 56 | "stop_duration_candles": 4, |
| 57 | "only_per_pair": false |
| 58 | } |
| 59 | ], |
| 60 | ``` |
| 61 | |
| 62 | !!! Note |
| 63 | `StoplossGuard` considers all trades with the results `"stop_loss"`, `"stoploss_on_exchange"` and `"trailing_stop_loss"` if the resulting profit was negative. |
| 64 | `trade_limit` and `lookback_period` will need to be tuned for your strategy. |
| 65 | |
| 66 | #### MaxDrawdown |
| 67 | |
| 68 | `MaxDrawdown` uses all trades within `lookback_period` in minutes (or in candles when using `lookback_period_candles`) to determine the maximum drawdown. If the drawdown is below `max_allowed_drawdown`, trading will stop for `stop_duration` in minutes (or in candles when using `stop_duration_candles`) after the last trade - assuming that the bot needs some time to let markets recover. |
| 69 | |
| 70 | The below sample stops trading for 12 candles if max-drawdown is > 20% considering all pairs - with a minimum of `trade_limit` trades - within the last 48 candles. If desired, `lookback_period` and/or `stop_duration` can be used. |
| 71 | |
| 72 | ```json |
| 73 | "protections": [ |
| 74 | { |
| 75 | "method": "MaxDrawdown", |
| 76 | "lookback_period_candles": 48, |
| 77 | "trade_limit": 20, |
| 78 | "stop_duration_candles": 12, |
| 79 | "max_allowed_drawdown": 0.2 |
| 80 | }, |
| 81 | ], |
| 82 | ``` |
| 83 | |
| 84 | #### Low Profit Pairs |
| 85 | |
| 86 | `LowProfitPairs` uses all trades for a pair within `lookback_period` in minutes (or in candles when using `lookback_period_candles`) to determine the overall profit ratio. |
| 87 | If that ratio is below `required_profit`, that pair will be locked for `stop_duration` in minutes (or in candles when using `stop_duration_candles`). |
| 88 | |
| 89 | The below example will stop trading a pair for 60 minutes if the pair does not have a required profit of 2% (and a minimum of 2 trades) within the last 6 candles. |
| 90 | |
| 91 | ```json |
| 92 | "protections": [ |
| 93 | { |
| 94 | "method": "LowProfitPairs", |
| 95 | "lookback_period_candles": 6, |
| 96 | "trade_limit": 2, |
| 97 | "stop_duration": 60, |
| 98 | "required_profit": 0.02 |
| 99 | } |
| 100 | ], |
| 101 | ``` |
| 102 | |
| 103 | #### Cooldown Period |
| 104 | |
| 105 | `CooldownPeriod` locks a pair for `stop_duration` in minutes (or in candles when using `stop_duration_candles`) after selling, avoiding a re-entry for this pair for `stop_duration` minutes. |
| 106 | |
| 107 | The below example will stop trading a pair for 2 candles after closing a trade, allowing this pair to "cool down". |
| 108 | |
| 109 | ```json |
| 110 | "protections": [ |
| 111 | { |
| 112 | "method": "CooldownPeriod", |
| 113 | "stop_duration_candles": 2 |
| 114 | } |
| 115 | ], |
| 116 | ``` |
| 117 | |
| 118 | !!! Note |
| 119 | This Protection applies only at pair-level, and will never lock all pairs globally. |
| 120 | This Protection does not consider `lookback_period` as it only looks at the latest trade. |
| 121 | |
| 122 | ### Full example of Protections |
| 123 | |
| 124 | All protections can be combined at will, also with different parameters, creating a increasing wall for under-performing pairs. |
| 125 | All protections are evaluated in the sequence they are defined. |
| 126 | |
| 127 | The below example assumes a timeframe of 1 hour: |
| 128 | |
| 129 | * Locks each pair after selling for an additional 5 candles (`CooldownPeriod`), giving other pairs a chance to get filled. |
| 130 | * Stops trading for 4 hours (`4 * 1h candles`) if the last 2 days (`48 * 1h candles`) had 20 trades, which caused a max-drawdown of more than 20%. (`MaxDrawdown`). |
| 131 | * Stops trading if more than 4 stoploss occur for all pairs within a 1 day (`24 * 1h candles`) limit (`StoplossGuard`). |
| 132 | * Locks all pairs that had 4 Trades within the last 6 hours (`6 * 1h candles`) with a combined profit ratio of below 0.02 (<2%) (`LowProfitPairs`). |
| 133 | * Locks all pairs for 2 candles that had a profit of below 0.01 (<1%) within the last 24h (`24 * 1h candles`), a minimum of 4 trades. |
| 134 | |
| 135 | ```json |
| 136 | "timeframe": "1h", |
| 137 | "protections": [ |
| 138 | { |
| 139 | "method": "CooldownPeriod", |
| 140 | "stop_duration_candles": 5 |
| 141 | }, |
| 142 | { |
| 143 | "method": "MaxDrawdown", |
| 144 | "lookback_period_candles": 48, |
| 145 | "trade_limit": 20, |
| 146 | "stop_duration_candles": 4, |
| 147 | "max_allowed_drawdown": 0.2 |
| 148 | }, |
| 149 | { |
| 150 | "method": "StoplossGuard", |
| 151 | "lookback_period_candles": 24, |
| 152 | "trade_limit": 4, |
| 153 | "stop_duration_candles": 2, |
| 154 | "only_per_pair": false |
| 155 | }, |
| 156 | { |
| 157 | "method": "LowProfitPairs", |
| 158 | "lookback_period_candles": 6, |
| 159 | "trade_limit": 2, |
| 160 | "stop_duration_candles": 60, |
| 161 | "required_profit": 0.02 |
| 162 | }, |
| 163 | { |
| 164 | "method": "LowProfitPairs", |
| 165 | "lookback_period_candles": 24, |
| 166 | "trade_limit": 4, |
| 167 | "stop_duration_candles": 2, |
| 168 | "required_profit": 0.01 |
| 169 | } |
| 170 | ], |
| 171 | ``` |
| 172 | |
| 173 | You can use the same in your strategy, the syntax is only slightly different: |
| 174 | |
| 175 | ``` python |
| 176 | from freqtrade.strategy import IStrategy |
| 177 | |
| 178 | class AwesomeStrategy(IStrategy) |
| 179 | timeframe = '1h' |
| 180 | protections = [ |
| 181 | { |
| 182 | "method": "CooldownPeriod", |
| 183 | "stop_duration_candles": 5 |
| 184 | }, |
| 185 | { |
| 186 | "method": "MaxDrawdown", |
| 187 | "lookback_period_candles": 48, |
| 188 | "trade_limit": 20, |
| 189 | "stop_duration_candles": 4, |
| 190 | "max_allowed_drawdown": 0.2 |
| 191 | }, |
| 192 | { |
| 193 | "method": "StoplossGuard", |
| 194 | "lookback_period_candles": 24, |
| 195 | "trade_limit": 4, |
| 196 | "stop_duration_candles": 2, |
| 197 | "only_per_pair": False |
| 198 | }, |
| 199 | { |
| 200 | "method": "LowProfitPairs", |
| 201 | "lookback_period_candles": 6, |
| 202 | "trade_limit": 2, |
| 203 | "stop_duration_candles": 60, |
| 204 | "required_profit": 0.02 |
| 205 | }, |
| 206 | { |
| 207 | "method": "LowProfitPairs", |
| 208 | "lookback_period_candles": 24, |
| 209 | "trade_limit": 4, |
| 210 | "stop_duration_candles": 2, |
| 211 | "required_profit": 0.01 |
| 212 | } |
| 213 | ] |
| 214 | # ... |
| 215 | ``` |