stable/docs/includes/protections.md

230 lines
10 KiB
Markdown
Raw Normal View History

2020-10-14 05:40:44 +00:00
## Protections
2020-11-27 16:51:58 +00:00
!!! Warning "Beta feature"
This feature is still in it's testing phase. Should you notice something you think is wrong please let us know via Discord or via Github Issue.
2020-11-27 16:51:58 +00:00
2020-11-27 09:29:45 +00:00
Protections will protect your strategy from unexpected events and market conditions by temporarily stop trading for either one pair, or for all pairs.
2020-11-27 16:14:28 +00:00
All protection end times are rounded up to the next candle to avoid sudden, unexpected intra-candle buys.
2020-10-14 05:40:44 +00:00
2020-11-11 19:54:24 +00:00
!!! Note
Not all Protections will work for all strategies, and parameters will need to be tuned for your strategy to improve performance.
2020-11-11 19:54:24 +00:00
!!! Tip
Each Protection can be configured multiple times with different parameters, to allow different levels of protection (short-term / long-term).
2020-11-24 05:51:54 +00:00
!!! Note "Backtesting"
2020-12-02 06:42:39 +00:00
Protections are supported by backtesting and hyperopt, but must be explicitly enabled by using the `--enable-protections` flag.
2020-11-24 05:51:54 +00:00
2021-08-04 17:43:16 +00:00
!!! Warning "Setting protections from the configuration"
Setting protections from the configuration via `"protections": [],` key should be considered deprecated and will be removed in a future version.
It is also no longer guaranteed that your protections apply to the strategy in cases where the strategy defines [protections as property](hyperopt.md#optimizing-protections).
2020-12-07 07:22:12 +00:00
### Available Protections
2020-10-14 05:40:44 +00:00
2020-11-11 19:54:24 +00:00
* [`StoplossGuard`](#stoploss-guard) Stop trading if a certain amount of stoploss occurred within a certain time window.
* [`MaxDrawdown`](#maxdrawdown) Stop trading if max-drawdown is reached.
2020-11-11 19:54:24 +00:00
* [`LowProfitPairs`](#low-profit-pairs) Lock pairs with low profits
* [`CooldownPeriod`](#cooldown-period) Don't enter a trade right after selling a trade.
2022-09-29 11:13:01 +00:00
* [`ProfitLimit`](#profit-limit) Stop trading once a set profit limit is reached
2022-09-29 11:11:15 +00:00
2020-10-14 05:40:44 +00:00
2020-12-07 07:22:12 +00:00
### Common settings to all Protections
| Parameter| Description |
|------------|-------------|
2020-12-13 09:15:16 +00:00
| `method` | Protection name to use. <br> **Datatype:** String, selected from [available Protections](#available-protections)
| `stop_duration_candles` | For how many candles should the lock be set? <br> **Datatype:** Positive integer (in candles)
| `stop_duration` | how many minutes should protections be locked. <br>Cannot be used together with `stop_duration_candles`. <br> **Datatype:** Float (in minutes)
| `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).
2020-12-13 09:15:16 +00:00
| `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)
| `trade_limit` | Number of trades required at minimum (not used by all Protections). <br> **Datatype:** Positive integer
2020-12-07 07:22:12 +00:00
2020-12-07 09:45:35 +00:00
!!! Note "Durations"
Durations (`stop_duration*` and `lookback_period*` can be defined in either minutes or candles).
For more flexibility when testing different timeframes, all below examples will use the "candle" definition.
2020-10-14 05:40:44 +00:00
#### Stoploss Guard
`StoplossGuard` selects all trades within `lookback_period` in minutes (or in candles when using `lookback_period_candles`).
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`).
2020-11-25 10:11:55 +00:00
This applies across all pairs, unless `only_per_pair` is set to true, which will then only look at one pair at a time.
2020-10-14 05:40:44 +00:00
2022-04-24 12:38:23 +00:00
Similarly, this protection will by default look at all trades (long and short). For futures bots, setting `only_per_side` will make the bot only consider one side, and will then only lock this one side, allowing for example shorts to continue after a series of long stoplosses.
`required_profit` will determine the required relative profit (or loss) for stoplosses to consider. This should normally not be set and defaults to 0.0 - which means all losing stoplosses will be triggering a block.
2020-12-07 09:45:35 +00:00
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.
``` python
2021-08-04 17:43:16 +00:00
@property
def protections(self):
return [
{
"method": "StoplossGuard",
"lookback_period_candles": 24,
"trade_limit": 4,
"stop_duration_candles": 4,
"required_profit": 0.0,
"only_per_pair": False,
2022-04-24 12:38:23 +00:00
"only_per_side": False
2021-08-04 17:43:16 +00:00
}
]
2020-10-14 05:40:44 +00:00
```
!!! Note
`StoplossGuard` considers all trades with the results `"stop_loss"`, `"stoploss_on_exchange"` and `"trailing_stop_loss"` if the resulting profit was negative.
2020-11-11 19:54:24 +00:00
`trade_limit` and `lookback_period` will need to be tuned for your strategy.
2020-10-14 05:40:44 +00:00
#### MaxDrawdown
`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.
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.
``` python
2021-08-04 17:43:16 +00:00
@property
def protections(self):
return [
{
"method": "MaxDrawdown",
"lookback_period_candles": 48,
"trade_limit": 20,
"stop_duration_candles": 12,
"max_allowed_drawdown": 0.2
},
]
```
2020-11-11 06:49:30 +00:00
#### Low Profit Pairs
2021-01-06 08:45:21 +00:00
`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.
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`).
2020-11-11 06:49:30 +00:00
For futures bots, setting `only_per_side` will make the bot only consider one side, and will then only lock this one side, allowing for example shorts to continue after a series of long losses.
2020-12-07 09:45:35 +00:00
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.
``` python
2021-08-04 17:43:16 +00:00
@property
def protections(self):
return [
{
"method": "LowProfitPairs",
"lookback_period_candles": 6,
"trade_limit": 2,
"stop_duration": 60,
"required_profit": 0.02,
"only_per_pair": False,
2021-08-04 17:43:16 +00:00
}
]
2020-11-11 06:49:30 +00:00
```
2020-11-11 07:00:10 +00:00
#### Cooldown Period
2021-01-06 08:45:21 +00:00
`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.
2020-11-11 07:00:10 +00:00
2020-12-07 09:45:35 +00:00
The below example will stop trading a pair for 2 candles after closing a trade, allowing this pair to "cool down".
``` python
2021-08-04 17:43:16 +00:00
@property
def protections(self):
return [
{
"method": "CooldownPeriod",
"stop_duration_candles": 2
}
]
2020-11-11 07:00:10 +00:00
```
2020-12-07 07:22:12 +00:00
!!! Note
2020-11-11 07:00:10 +00:00
This Protection applies only at pair-level, and will never lock all pairs globally.
2020-12-07 07:22:12 +00:00
This Protection does not consider `lookback_period` as it only looks at the latest trade.
2020-11-11 07:00:10 +00:00
2022-09-29 11:11:15 +00:00
#### Profit Limit
`ProfitLimit` 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.
If that ratio is greater than `profit_limit`, all pairs will be locked for `stop_duration` in minutes (or in candles when using `stop_duration_candles`).
The below example will stop trading a pair for 180 minutes if the total profit from all trades reaches 1% (and a minimum of 3 trades) within the last 6 candles.
``` python
@property
def protections(self):
return [
{
"method": "ProfitLimit",
"lookback_period_candles": 6,
"trade_limit": 3,
"stop_duration": 180,
"profit_limit": 0.01
}
]
```
2020-10-14 05:40:44 +00:00
### Full example of Protections
2020-11-11 07:00:10 +00:00
All protections can be combined at will, also with different parameters, creating a increasing wall for under-performing pairs.
All protections are evaluated in the sequence they are defined.
2020-12-07 09:45:35 +00:00
The below example assumes a timeframe of 1 hour:
2020-11-11 07:00:10 +00:00
2020-12-07 09:45:35 +00:00
* Locks each pair after selling for an additional 5 candles (`CooldownPeriod`), giving other pairs a chance to get filled.
* 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`).
* Stops trading if more than 4 stoploss occur for all pairs within a 1 day (`24 * 1h candles`) limit (`StoplossGuard`).
* 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`).
* 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.
2022-09-29 11:11:15 +00:00
* Locks all pairs for 360 minutes once a profit of 0.02 (2%) is achieved within the last 800 candles , a minimum of 2 trades.
2020-10-14 05:40:44 +00:00
``` python
from freqtrade.strategy import IStrategy
class AwesomeStrategy(IStrategy)
timeframe = '1h'
2021-08-04 17:43:16 +00:00
@property
def protections(self):
return [
{
"method": "CooldownPeriod",
"stop_duration_candles": 5
},
{
"method": "MaxDrawdown",
"lookback_period_candles": 48,
"trade_limit": 20,
"stop_duration_candles": 4,
"max_allowed_drawdown": 0.2
},
{
"method": "StoplossGuard",
"lookback_period_candles": 24,
"trade_limit": 4,
"stop_duration_candles": 2,
"only_per_pair": False
},
{
"method": "LowProfitPairs",
"lookback_period_candles": 6,
"trade_limit": 2,
"stop_duration_candles": 60,
"required_profit": 0.02
},
{
"method": "LowProfitPairs",
"lookback_period_candles": 24,
"trade_limit": 4,
"stop_duration_candles": 2,
"required_profit": 0.01
2022-09-29 11:11:15 +00:00
},
{
"method": "ProfitLimit",
"lookback_period_candles": 800,
"trade_limit": 2,
"stop_duration": 360,
"profit_limit": 0.02
2021-08-04 17:43:16 +00:00
}
]
# ...
```