Merge branch 'develop' into feature_keyval_storage
This commit is contained in:
commit
0009b987e4
@ -22,50 +22,79 @@ DataFrame of the candles that resulted in buy signals. Depending on how many buy
|
|||||||
makes, this file may get quite large, so periodically check your `user_data/backtest_results`
|
makes, this file may get quite large, so periodically check your `user_data/backtest_results`
|
||||||
folder to delete old exports.
|
folder to delete old exports.
|
||||||
|
|
||||||
To analyze the buy tags, we need to use the `buy_reasons.py` script from
|
|
||||||
[froggleston's repo](https://github.com/froggleston/freqtrade-buyreasons). Follow the instructions
|
|
||||||
in their README to copy the script into your `freqtrade/scripts/` folder.
|
|
||||||
|
|
||||||
Before running your next backtest, make sure you either delete your old backtest results or run
|
Before running your next backtest, make sure you either delete your old backtest results or run
|
||||||
backtesting with the `--cache none` option to make sure no cached results are used.
|
backtesting with the `--cache none` option to make sure no cached results are used.
|
||||||
|
|
||||||
If all goes well, you should now see a `backtest-result-{timestamp}_signals.pkl` file in the
|
If all goes well, you should now see a `backtest-result-{timestamp}_signals.pkl` file in the
|
||||||
`user_data/backtest_results` folder.
|
`user_data/backtest_results` folder.
|
||||||
|
|
||||||
Now run the `buy_reasons.py` script, supplying a few options:
|
To analyze the entry/exit tags, we now need to use the `freqtrade backtesting-analysis` command
|
||||||
|
with `--analysis-groups` option provided with space-separated arguments (default `0 1 2`):
|
||||||
|
|
||||||
``` bash
|
``` bash
|
||||||
python3 scripts/buy_reasons.py -c <config.json> -s <strategy_name> -t <timerange> -g0,1,2,3,4
|
freqtrade backtesting-analysis -c <config.json> --analysis-groups 0 1 2 3 4
|
||||||
```
|
```
|
||||||
|
|
||||||
The `-g` option is used to specify the various tabular outputs, ranging from the simplest (0)
|
This command will read from the last backtesting results. The `--analysis-groups` option is
|
||||||
to the most detailed per pair, per buy and per sell tag (4). More options are available by
|
used to specify the various tabular outputs showing the profit fo each group or trade,
|
||||||
running with the `-h` option.
|
ranging from the simplest (0) to the most detailed per pair, per buy and per sell tag (4):
|
||||||
|
|
||||||
|
* 1: profit summaries grouped by enter_tag
|
||||||
|
* 2: profit summaries grouped by enter_tag and exit_tag
|
||||||
|
* 3: profit summaries grouped by pair and enter_tag
|
||||||
|
* 4: profit summaries grouped by pair, enter_ and exit_tag (this can get quite large)
|
||||||
|
|
||||||
|
More options are available by running with the `-h` option.
|
||||||
|
|
||||||
|
### Using export-filename
|
||||||
|
|
||||||
|
Normally, `backtesting-analysis` uses the latest backtest results, but if you wanted to go
|
||||||
|
back to a previous backtest output, you need to supply the `--export-filename` option.
|
||||||
|
You can supply the same parameter to `backtest-analysis` with the name of the final backtest
|
||||||
|
output file. This allows you to keep historical versions of backtest results and re-analyse
|
||||||
|
them at a later date:
|
||||||
|
|
||||||
|
``` bash
|
||||||
|
freqtrade backtesting -c <config.json> --timeframe <tf> --strategy <strategy_name> --timerange=<timerange> --export=signals --export-filename=/tmp/mystrat_backtest.json
|
||||||
|
```
|
||||||
|
|
||||||
|
You should see some output similar to below in the logs with the name of the timestamped
|
||||||
|
filename that was exported:
|
||||||
|
|
||||||
|
```
|
||||||
|
2022-06-14 16:28:32,698 - freqtrade.misc - INFO - dumping json to "/tmp/mystrat_backtest-2022-06-14_16-28-32.json"
|
||||||
|
```
|
||||||
|
|
||||||
|
You can then use that filename in `backtesting-analysis`:
|
||||||
|
|
||||||
|
```
|
||||||
|
freqtrade backtesting-analysis -c <config.json> --export-filename=/tmp/mystrat_backtest-2022-06-14_16-28-32.json
|
||||||
|
```
|
||||||
|
|
||||||
### Tuning the buy tags and sell tags to display
|
### Tuning the buy tags and sell tags to display
|
||||||
|
|
||||||
To show only certain buy and sell tags in the displayed output, use the following two options:
|
To show only certain buy and sell tags in the displayed output, use the following two options:
|
||||||
|
|
||||||
```
|
```
|
||||||
--enter_reason_list : Comma separated list of enter signals to analyse. Default: "all"
|
--enter-reason-list : Space-separated list of enter signals to analyse. Default: "all"
|
||||||
--exit_reason_list : Comma separated list of exit signals to analyse. Default: "stop_loss,trailing_stop_loss"
|
--exit-reason-list : Space-separated list of exit signals to analyse. Default: "all"
|
||||||
```
|
```
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python3 scripts/buy_reasons.py -c <config.json> -s <strategy_name> -t <timerange> -g0,1,2,3,4 --enter_reason_list "enter_tag_a,enter_tag_b" --exit_reason_list "roi,custom_exit_tag_a,stop_loss"
|
freqtrade backtesting-analysis -c <config.json> --analysis-groups 0 2 --enter-reason-list enter_tag_a enter_tag_b --exit-reason-list roi custom_exit_tag_a stop_loss
|
||||||
```
|
```
|
||||||
|
|
||||||
### Outputting signal candle indicators
|
### Outputting signal candle indicators
|
||||||
|
|
||||||
The real power of the buy_reasons.py script comes from the ability to print out the indicator
|
The real power of `freqtrade backtesting-analysis` comes from the ability to print out the indicator
|
||||||
values present on signal candles to allow fine-grained investigation and tuning of buy signal
|
values present on signal candles to allow fine-grained investigation and tuning of buy signal
|
||||||
indicators. To print out a column for a given set of indicators, use the `--indicator-list`
|
indicators. To print out a column for a given set of indicators, use the `--indicator-list`
|
||||||
option:
|
option:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python3 scripts/buy_reasons.py -c <config.json> -s <strategy_name> -t <timerange> -g0,1,2,3,4 --enter_reason_list "enter_tag_a,enter_tag_b" --exit_reason_list "roi,custom_exit_tag_a,stop_loss" --indicator_list "rsi,rsi_1h,bb_lowerband,ema_9,macd,macdsignal"
|
freqtrade backtesting-analysis -c <config.json> --analysis-groups 0 2 --enter-reason-list enter_tag_a enter_tag_b --exit-reason-list roi custom_exit_tag_a stop_loss --indicator-list rsi rsi_1h bb_lowerband ema_9 macd macdsignal
|
||||||
```
|
```
|
||||||
|
|
||||||
The indicators have to be present in your strategy's main DataFrame (either for your main
|
The indicators have to be present in your strategy's main DataFrame (either for your main
|
||||||
|
@ -300,6 +300,7 @@ A backtesting result will look like that:
|
|||||||
| Absolute profit | 0.00762792 BTC |
|
| Absolute profit | 0.00762792 BTC |
|
||||||
| Total profit % | 76.2% |
|
| Total profit % | 76.2% |
|
||||||
| CAGR % | 460.87% |
|
| CAGR % | 460.87% |
|
||||||
|
| Profit factor | 1.11 |
|
||||||
| Avg. stake amount | 0.001 BTC |
|
| Avg. stake amount | 0.001 BTC |
|
||||||
| Total trade volume | 0.429 BTC |
|
| Total trade volume | 0.429 BTC |
|
||||||
| | |
|
| | |
|
||||||
@ -399,6 +400,7 @@ It contains some useful key metrics about performance of your strategy on backte
|
|||||||
| Absolute profit | 0.00762792 BTC |
|
| Absolute profit | 0.00762792 BTC |
|
||||||
| Total profit % | 76.2% |
|
| Total profit % | 76.2% |
|
||||||
| CAGR % | 460.87% |
|
| CAGR % | 460.87% |
|
||||||
|
| Profit factor | 1.11 |
|
||||||
| Avg. stake amount | 0.001 BTC |
|
| Avg. stake amount | 0.001 BTC |
|
||||||
| Total trade volume | 0.429 BTC |
|
| Total trade volume | 0.429 BTC |
|
||||||
| | |
|
| | |
|
||||||
@ -444,6 +446,8 @@ It contains some useful key metrics about performance of your strategy on backte
|
|||||||
- `Final balance`: Final balance - starting balance + absolute profit.
|
- `Final balance`: Final balance - starting balance + absolute profit.
|
||||||
- `Absolute profit`: Profit made in stake currency.
|
- `Absolute profit`: Profit made in stake currency.
|
||||||
- `Total profit %`: Total profit. Aligned to the `TOTAL` row's `Tot Profit %` from the first table. Calculated as `(End capital − Starting capital) / Starting capital`.
|
- `Total profit %`: Total profit. Aligned to the `TOTAL` row's `Tot Profit %` from the first table. Calculated as `(End capital − Starting capital) / Starting capital`.
|
||||||
|
- `CAGR %`: Compound annual growth rate.
|
||||||
|
- `Profit factor`: profit / loss.
|
||||||
- `Avg. stake amount`: Average stake amount, either `stake_amount` or the average when using dynamic stake amount.
|
- `Avg. stake amount`: Average stake amount, either `stake_amount` or the average when using dynamic stake amount.
|
||||||
- `Total trade volume`: Volume generated on the exchange to reach the above profit.
|
- `Total trade volume`: Volume generated on the exchange to reach the above profit.
|
||||||
- `Best Pair` / `Worst Pair`: Best and worst performing pair, and it's corresponding `Cum Profit %`.
|
- `Best Pair` / `Worst Pair`: Best and worst performing pair, and it's corresponding `Cum Profit %`.
|
||||||
|
@ -64,7 +64,10 @@ You will also have to pick a "margin mode" (explanation below) - with freqtrade
|
|||||||
|
|
||||||
### Margin mode
|
### Margin mode
|
||||||
|
|
||||||
The possible values are: `isolated`, or `cross`(*currently unavailable*)
|
On top of `trading_mode` - you will also have to configure your `margin_mode`.
|
||||||
|
While freqtrade currently only supports one margin mode, this will change, and by configuring it now you're all set for future updates.
|
||||||
|
|
||||||
|
The possible values are: `isolated`, or `cross`(*currently unavailable*).
|
||||||
|
|
||||||
#### Isolated margin mode
|
#### Isolated margin mode
|
||||||
|
|
||||||
@ -82,6 +85,16 @@ One account is used to share collateral between markets (trading pairs). Margin
|
|||||||
"margin_mode": "cross"
|
"margin_mode": "cross"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Set leverage to use
|
||||||
|
|
||||||
|
Different strategies and risk profiles will require different levels of leverage.
|
||||||
|
While you could configure one static leverage value - freqtrade offers you the flexibility to adjust this via [strategy leverage callback](strategy-callbacks.md#leverage-callback) - which allows you to use different leverages by pair, or based on some other factor benefitting your strategy result.
|
||||||
|
|
||||||
|
If not implemented, leverage defaults to 1x (no leverage).
|
||||||
|
|
||||||
|
!!! Warning
|
||||||
|
Higher leverage also equals higher risk - be sure you fully understand the implications of using leverage!
|
||||||
|
|
||||||
## Understand `liquidation_buffer`
|
## Understand `liquidation_buffer`
|
||||||
|
|
||||||
*Defaults to `0.05`*
|
*Defaults to `0.05`*
|
||||||
|
@ -191,6 +191,19 @@ For example, simplified math:
|
|||||||
!!! Tip
|
!!! Tip
|
||||||
Make sure to have this value (`trailing_stop_positive_offset`) lower than minimal ROI, otherwise minimal ROI will apply first and sell the trade.
|
Make sure to have this value (`trailing_stop_positive_offset`) lower than minimal ROI, otherwise minimal ROI will apply first and sell the trade.
|
||||||
|
|
||||||
|
## Stoploss and Leverage
|
||||||
|
|
||||||
|
Stoploss should be thought of as "risk on this trade" - so a stoploss of 10% on a 100$ trade means you are willing to lose 10$ (10%) on this trade - which would trigger if the price moves 10% to the downside.
|
||||||
|
|
||||||
|
When using leverage, the same principle is applied - with stoploss defining the risk on the trade (the amount you are willing to lose).
|
||||||
|
|
||||||
|
Therefore, a stoploss of 10% on a 10x trade would trigger on a 1% price move.
|
||||||
|
If your stake amount (own capital) was 100$ - this trade would be 1000$ at 10x (after leverage).
|
||||||
|
If price moves 1% - you've lost 10$ of your own capital - therfore stoploss will trigger in this case.
|
||||||
|
|
||||||
|
Make sure to be aware of this, and avoid using too tight stoploss (at 10x leverage, 10% risk may be too little to allow the trade to "breath" a little).
|
||||||
|
|
||||||
|
|
||||||
## Changing stoploss on open trades
|
## Changing stoploss on open trades
|
||||||
|
|
||||||
A stoploss on an open trade can be changed by changing the value in the configuration or strategy and use the `/reload_config` command (alternatively, completely stopping and restarting the bot also works).
|
A stoploss on an open trade can be changed by changing the value in the configuration or strategy and use the `/reload_config` command (alternatively, completely stopping and restarting the bot also works).
|
||||||
|
@ -823,3 +823,6 @@ class AwesomeStrategy(IStrategy):
|
|||||||
"""
|
"""
|
||||||
return 1.0
|
return 1.0
|
||||||
```
|
```
|
||||||
|
|
||||||
|
All profit calculations include leverage. Stoploss / ROI also include leverage in their calculation.
|
||||||
|
Defining a stoploss of 10% at 10x leverage would trigger the stoploss with a 1% move to the downside.
|
||||||
|
@ -171,8 +171,8 @@ official commands. You can ask at any moment for help with `/help`.
|
|||||||
| `/locks` | Show currently locked pairs.
|
| `/locks` | Show currently locked pairs.
|
||||||
| `/unlock <pair or lock_id>` | Remove the lock for this pair (or for this lock id).
|
| `/unlock <pair or lock_id>` | Remove the lock for this pair (or for this lock id).
|
||||||
| `/profit [<n>]` | Display a summary of your profit/loss from close trades and some stats about your performance, over the last n days (all trades by default)
|
| `/profit [<n>]` | Display a summary of your profit/loss from close trades and some stats about your performance, over the last n days (all trades by default)
|
||||||
| `/forceexit <trade_id>` | Instantly exits the given trade (Ignoring `minimum_roi`).
|
| `/forceexit <trade_id> | /fx <tradeid>` | Instantly exits the given trade (Ignoring `minimum_roi`).
|
||||||
| `/forceexit all` | Instantly exits all open trades (Ignoring `minimum_roi`).
|
| `/forceexit all | /fx all` | Instantly exits all open trades (Ignoring `minimum_roi`).
|
||||||
| `/fx` | alias for `/forceexit`
|
| `/fx` | alias for `/forceexit`
|
||||||
| `/forcelong <pair> [rate]` | Instantly buys the given pair. Rate is optional and only applies to limit orders. (`force_entry_enable` must be set to True)
|
| `/forcelong <pair> [rate]` | Instantly buys the given pair. Rate is optional and only applies to limit orders. (`force_entry_enable` must be set to True)
|
||||||
| `/forceshort <pair> [rate]` | Instantly shorts the given pair. Rate is optional and only applies to limit orders. This will only work on non-spot markets. (`force_entry_enable` must be set to True)
|
| `/forceshort <pair> [rate]` | Instantly shorts the given pair. Rate is optional and only applies to limit orders. This will only work on non-spot markets. (`force_entry_enable` must be set to True)
|
||||||
@ -270,10 +270,15 @@ Return a summary of your profit/loss and performance.
|
|||||||
> **Latest Trade opened:** `2 minutes ago`
|
> **Latest Trade opened:** `2 minutes ago`
|
||||||
> **Avg. Duration:** `2:33:45`
|
> **Avg. Duration:** `2:33:45`
|
||||||
> **Best Performing:** `PAY/BTC: 50.23%`
|
> **Best Performing:** `PAY/BTC: 50.23%`
|
||||||
|
> **Trading volume:** `0.5 BTC`
|
||||||
|
> **Profit factor:** `1.04`
|
||||||
|
> **Max Drawdown:** `9.23% (0.01255 BTC)`
|
||||||
|
|
||||||
The relative profit of `1.2%` is the average profit per trade.
|
The relative profit of `1.2%` is the average profit per trade.
|
||||||
The relative profit of `15.2 Σ%` is be based on the starting capital - so in this case, the starting capital was `0.00485701 * 1.152 = 0.00738 BTC`.
|
The relative profit of `15.2 Σ%` is be based on the starting capital - so in this case, the starting capital was `0.00485701 * 1.152 = 0.00738 BTC`.
|
||||||
Starting capital is either taken from the `available_capital` setting, or calculated by using current wallet size - profits.
|
Starting capital is either taken from the `available_capital` setting, or calculated by using current wallet size - profits.
|
||||||
|
Profit Factor is calculated as gross profits / gross losses - and should serve as an overall metric for the strategy.
|
||||||
|
Max drawdown corresponds to the backtesting metric `Absolute Drawdown (Account)` - calculated as `(Absolute Drawdown) / (DrawdownHigh + startingBalance)`.
|
||||||
|
|
||||||
### /forceexit <trade_id>
|
### /forceexit <trade_id>
|
||||||
|
|
||||||
@ -281,6 +286,7 @@ Starting capital is either taken from the `available_capital` setting, or calcul
|
|||||||
|
|
||||||
!!! Tip
|
!!! Tip
|
||||||
You can get a list of all open trades by calling `/forceexit` without parameter, which will show a list of buttons to simply exit a trade.
|
You can get a list of all open trades by calling `/forceexit` without parameter, which will show a list of buttons to simply exit a trade.
|
||||||
|
This command has an alias in `/fx` - which has the same capabilities, but is faster to type in "emergency" situations.
|
||||||
|
|
||||||
### /forcelong <pair> [rate] | /forceshort <pair> [rate]
|
### /forcelong <pair> [rate] | /forceshort <pair> [rate]
|
||||||
|
|
||||||
|
@ -651,6 +651,61 @@ Common arguments:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Detailed backtest analysis
|
||||||
|
|
||||||
|
Advanced backtest result analysis.
|
||||||
|
|
||||||
|
More details in the [Backtesting analysis](advanced-backtesting.md#analyze-the-buyentry-and-sellexit-tags) Section.
|
||||||
|
|
||||||
|
```
|
||||||
|
usage: freqtrade backtesting-analysis [-h] [-v] [--logfile FILE] [-V]
|
||||||
|
[-c PATH] [-d PATH] [--userdir PATH]
|
||||||
|
[--export-filename PATH]
|
||||||
|
[--analysis-groups {0,1,2,3,4} [{0,1,2,3,4} ...]]
|
||||||
|
[--enter-reason-list ENTER_REASON_LIST [ENTER_REASON_LIST ...]]
|
||||||
|
[--exit-reason-list EXIT_REASON_LIST [EXIT_REASON_LIST ...]]
|
||||||
|
[--indicator-list INDICATOR_LIST [INDICATOR_LIST ...]]
|
||||||
|
|
||||||
|
optional arguments:
|
||||||
|
-h, --help show this help message and exit
|
||||||
|
--export-filename PATH, --backtest-filename PATH
|
||||||
|
Use this filename for backtest results.Requires
|
||||||
|
`--export` to be set as well. Example: `--export-filen
|
||||||
|
ame=user_data/backtest_results/backtest_today.json`
|
||||||
|
--analysis-groups {0,1,2,3,4} [{0,1,2,3,4} ...]
|
||||||
|
grouping output - 0: simple wins/losses by enter tag,
|
||||||
|
1: by enter_tag, 2: by enter_tag and exit_tag, 3: by
|
||||||
|
pair and enter_tag, 4: by pair, enter_ and exit_tag
|
||||||
|
(this can get quite large)
|
||||||
|
--enter-reason-list ENTER_REASON_LIST [ENTER_REASON_LIST ...]
|
||||||
|
Comma separated list of entry signals to analyse.
|
||||||
|
Default: all. e.g. 'entry_tag_a,entry_tag_b'
|
||||||
|
--exit-reason-list EXIT_REASON_LIST [EXIT_REASON_LIST ...]
|
||||||
|
Comma separated list of exit signals to analyse.
|
||||||
|
Default: all. e.g.
|
||||||
|
'exit_tag_a,roi,stop_loss,trailing_stop_loss'
|
||||||
|
--indicator-list INDICATOR_LIST [INDICATOR_LIST ...]
|
||||||
|
Comma separated list of indicators to analyse. e.g.
|
||||||
|
'close,rsi,bb_lowerband,profit_abs'
|
||||||
|
|
||||||
|
Common arguments:
|
||||||
|
-v, --verbose Verbose mode (-vv for more, -vvv to get all messages).
|
||||||
|
--logfile FILE Log to the file specified. Special values are:
|
||||||
|
'syslog', 'journald'. See the documentation for more
|
||||||
|
details.
|
||||||
|
-V, --version show program's version number and exit
|
||||||
|
-c PATH, --config PATH
|
||||||
|
Specify configuration file (default:
|
||||||
|
`userdir/config.json` or `config.json` whichever
|
||||||
|
exists). Multiple --config options may be used. Can be
|
||||||
|
set to `-` to read config from stdin.
|
||||||
|
-d PATH, --datadir PATH
|
||||||
|
Path to directory with historical backtesting data.
|
||||||
|
--userdir PATH, --user-data-dir PATH
|
||||||
|
Path to userdata directory.
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
## List Hyperopt results
|
## List Hyperopt results
|
||||||
|
|
||||||
You can list the hyperoptimization epochs the Hyperopt module evaluated previously with the `hyperopt-list` sub-command.
|
You can list the hyperoptimization epochs the Hyperopt module evaluated previously with the `hyperopt-list` sub-command.
|
||||||
|
@ -6,6 +6,7 @@ Contains all start-commands, subcommands and CLI Interface creation.
|
|||||||
Note: Be careful with file-scoped imports in these subfiles.
|
Note: Be careful with file-scoped imports in these subfiles.
|
||||||
as they are parsed on startup, nothing containing optional modules should be loaded.
|
as they are parsed on startup, nothing containing optional modules should be loaded.
|
||||||
"""
|
"""
|
||||||
|
from freqtrade.commands.analyze_commands import start_analysis_entries_exits
|
||||||
from freqtrade.commands.arguments import Arguments
|
from freqtrade.commands.arguments import Arguments
|
||||||
from freqtrade.commands.build_config_commands import start_new_config
|
from freqtrade.commands.build_config_commands import start_new_config
|
||||||
from freqtrade.commands.data_commands import (start_convert_data, start_convert_trades,
|
from freqtrade.commands.data_commands import (start_convert_data, start_convert_trades,
|
||||||
|
69
freqtrade/commands/analyze_commands.py
Executable file
69
freqtrade/commands/analyze_commands.py
Executable file
@ -0,0 +1,69 @@
|
|||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from freqtrade.configuration import setup_utils_configuration
|
||||||
|
from freqtrade.enums import RunMode
|
||||||
|
from freqtrade.exceptions import OperationalException
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def setup_analyze_configuration(args: Dict[str, Any], method: RunMode) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Prepare the configuration for the entry/exit reason analysis module
|
||||||
|
:param args: Cli args from Arguments()
|
||||||
|
:param method: Bot running mode
|
||||||
|
:return: Configuration
|
||||||
|
"""
|
||||||
|
config = setup_utils_configuration(args, method)
|
||||||
|
|
||||||
|
no_unlimited_runmodes = {
|
||||||
|
RunMode.BACKTEST: 'backtesting',
|
||||||
|
}
|
||||||
|
if method in no_unlimited_runmodes.keys():
|
||||||
|
from freqtrade.data.btanalysis import get_latest_backtest_filename
|
||||||
|
|
||||||
|
if 'exportfilename' in config:
|
||||||
|
if config['exportfilename'].is_dir():
|
||||||
|
btfile = Path(get_latest_backtest_filename(config['exportfilename']))
|
||||||
|
signals_file = f"{config['exportfilename']}/{btfile.stem}_signals.pkl"
|
||||||
|
else:
|
||||||
|
if config['exportfilename'].exists():
|
||||||
|
btfile = Path(config['exportfilename'])
|
||||||
|
signals_file = f"{btfile.parent}/{btfile.stem}_signals.pkl"
|
||||||
|
else:
|
||||||
|
raise OperationalException(f"{config['exportfilename']} does not exist.")
|
||||||
|
else:
|
||||||
|
raise OperationalException('exportfilename not in config.')
|
||||||
|
|
||||||
|
if (not Path(signals_file).exists()):
|
||||||
|
raise OperationalException(
|
||||||
|
(f"Cannot find latest backtest signals file: {signals_file}."
|
||||||
|
"Run backtesting with `--export signals`.")
|
||||||
|
)
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def start_analysis_entries_exits(args: Dict[str, Any]) -> None:
|
||||||
|
"""
|
||||||
|
Start analysis script
|
||||||
|
:param args: Cli args from Arguments()
|
||||||
|
:return: None
|
||||||
|
"""
|
||||||
|
from freqtrade.data.entryexitanalysis import process_entry_exit_reasons
|
||||||
|
|
||||||
|
# Initialize configuration
|
||||||
|
config = setup_analyze_configuration(args, RunMode.BACKTEST)
|
||||||
|
|
||||||
|
logger.info('Starting freqtrade in analysis mode')
|
||||||
|
|
||||||
|
process_entry_exit_reasons(config['exportfilename'],
|
||||||
|
config['exchange']['pair_whitelist'],
|
||||||
|
config['analysis_groups'],
|
||||||
|
config['enter_reason_list'],
|
||||||
|
config['exit_reason_list'],
|
||||||
|
config['indicator_list']
|
||||||
|
)
|
@ -101,6 +101,9 @@ ARGS_HYPEROPT_SHOW = ["hyperopt_list_best", "hyperopt_list_profitable", "hyperop
|
|||||||
"print_json", "hyperoptexportfilename", "hyperopt_show_no_header",
|
"print_json", "hyperoptexportfilename", "hyperopt_show_no_header",
|
||||||
"disableparamexport", "backtest_breakdown"]
|
"disableparamexport", "backtest_breakdown"]
|
||||||
|
|
||||||
|
ARGS_ANALYZE_ENTRIES_EXITS = ["exportfilename", "analysis_groups", "enter_reason_list",
|
||||||
|
"exit_reason_list", "indicator_list"]
|
||||||
|
|
||||||
NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes",
|
NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes",
|
||||||
"list-markets", "list-pairs", "list-strategies", "list-data",
|
"list-markets", "list-pairs", "list-strategies", "list-data",
|
||||||
"hyperopt-list", "hyperopt-show", "backtest-filter",
|
"hyperopt-list", "hyperopt-show", "backtest-filter",
|
||||||
@ -182,8 +185,9 @@ class Arguments:
|
|||||||
self.parser = argparse.ArgumentParser(description='Free, open source crypto trading bot')
|
self.parser = argparse.ArgumentParser(description='Free, open source crypto trading bot')
|
||||||
self._build_args(optionlist=['version'], parser=self.parser)
|
self._build_args(optionlist=['version'], parser=self.parser)
|
||||||
|
|
||||||
from freqtrade.commands import (start_backtesting, start_backtesting_show,
|
from freqtrade.commands import (start_analysis_entries_exits, start_backtesting,
|
||||||
start_convert_data, start_convert_db, start_convert_trades,
|
start_backtesting_show, start_convert_data,
|
||||||
|
start_convert_db, start_convert_trades,
|
||||||
start_create_userdir, start_download_data, start_edge,
|
start_create_userdir, start_download_data, start_edge,
|
||||||
start_hyperopt, start_hyperopt_list, start_hyperopt_show,
|
start_hyperopt, start_hyperopt_list, start_hyperopt_show,
|
||||||
start_install_ui, start_list_data, start_list_exchanges,
|
start_install_ui, start_list_data, start_list_exchanges,
|
||||||
@ -283,6 +287,13 @@ class Arguments:
|
|||||||
backtesting_show_cmd.set_defaults(func=start_backtesting_show)
|
backtesting_show_cmd.set_defaults(func=start_backtesting_show)
|
||||||
self._build_args(optionlist=ARGS_BACKTEST_SHOW, parser=backtesting_show_cmd)
|
self._build_args(optionlist=ARGS_BACKTEST_SHOW, parser=backtesting_show_cmd)
|
||||||
|
|
||||||
|
# Add backtesting analysis subcommand
|
||||||
|
analysis_cmd = subparsers.add_parser('backtesting-analysis',
|
||||||
|
help='Backtest Analysis module.',
|
||||||
|
parents=[_common_parser])
|
||||||
|
analysis_cmd.set_defaults(func=start_analysis_entries_exits)
|
||||||
|
self._build_args(optionlist=ARGS_ANALYZE_ENTRIES_EXITS, parser=analysis_cmd)
|
||||||
|
|
||||||
# Add edge subcommand
|
# Add edge subcommand
|
||||||
edge_cmd = subparsers.add_parser('edge', help='Edge module.',
|
edge_cmd = subparsers.add_parser('edge', help='Edge module.',
|
||||||
parents=[_common_parser, _strategy_parser])
|
parents=[_common_parser, _strategy_parser])
|
||||||
|
@ -614,4 +614,37 @@ AVAILABLE_CLI_OPTIONS = {
|
|||||||
"that do not contain any parameters."),
|
"that do not contain any parameters."),
|
||||||
action="store_true",
|
action="store_true",
|
||||||
),
|
),
|
||||||
|
"analysis_groups": Arg(
|
||||||
|
"--analysis-groups",
|
||||||
|
help=("grouping output - "
|
||||||
|
"0: simple wins/losses by enter tag, "
|
||||||
|
"1: by enter_tag, "
|
||||||
|
"2: by enter_tag and exit_tag, "
|
||||||
|
"3: by pair and enter_tag, "
|
||||||
|
"4: by pair, enter_ and exit_tag (this can get quite large)"),
|
||||||
|
nargs='+',
|
||||||
|
default=['0', '1', '2'],
|
||||||
|
choices=['0', '1', '2', '3', '4'],
|
||||||
|
),
|
||||||
|
"enter_reason_list": Arg(
|
||||||
|
"--enter-reason-list",
|
||||||
|
help=("Comma separated list of entry signals to analyse. Default: all. "
|
||||||
|
"e.g. 'entry_tag_a,entry_tag_b'"),
|
||||||
|
nargs='+',
|
||||||
|
default=['all'],
|
||||||
|
),
|
||||||
|
"exit_reason_list": Arg(
|
||||||
|
"--exit-reason-list",
|
||||||
|
help=("Comma separated list of exit signals to analyse. Default: all. "
|
||||||
|
"e.g. 'exit_tag_a,roi,stop_loss,trailing_stop_loss'"),
|
||||||
|
nargs='+',
|
||||||
|
default=['all'],
|
||||||
|
),
|
||||||
|
"indicator_list": Arg(
|
||||||
|
"--indicator-list",
|
||||||
|
help=("Comma separated list of indicators to analyse. "
|
||||||
|
"e.g. 'close,rsi,bb_lowerband,profit_abs'"),
|
||||||
|
nargs='+',
|
||||||
|
default=[],
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
@ -95,6 +95,8 @@ class Configuration:
|
|||||||
|
|
||||||
self._process_data_options(config)
|
self._process_data_options(config)
|
||||||
|
|
||||||
|
self._process_analyze_options(config)
|
||||||
|
|
||||||
# Check if the exchange set by the user is supported
|
# Check if the exchange set by the user is supported
|
||||||
check_exchange(config, config.get('experimental', {}).get('block_bad_exchanges', True))
|
check_exchange(config, config.get('experimental', {}).get('block_bad_exchanges', True))
|
||||||
|
|
||||||
@ -433,6 +435,19 @@ class Configuration:
|
|||||||
self._args_to_config(config, argname='candle_types',
|
self._args_to_config(config, argname='candle_types',
|
||||||
logstring='Detected --candle-types: {}')
|
logstring='Detected --candle-types: {}')
|
||||||
|
|
||||||
|
def _process_analyze_options(self, config: Dict[str, Any]) -> None:
|
||||||
|
self._args_to_config(config, argname='analysis_groups',
|
||||||
|
logstring='Analysis reason groups: {}')
|
||||||
|
|
||||||
|
self._args_to_config(config, argname='enter_reason_list',
|
||||||
|
logstring='Analysis enter tag list: {}')
|
||||||
|
|
||||||
|
self._args_to_config(config, argname='exit_reason_list',
|
||||||
|
logstring='Analysis exit tag list: {}')
|
||||||
|
|
||||||
|
self._args_to_config(config, argname='indicator_list',
|
||||||
|
logstring='Analysis indicator list: {}')
|
||||||
|
|
||||||
def _process_runmode(self, config: Dict[str, Any]) -> None:
|
def _process_runmode(self, config: Dict[str, Any]) -> None:
|
||||||
|
|
||||||
self._args_to_config(config, argname='dry_run',
|
self._args_to_config(config, argname='dry_run',
|
||||||
|
227
freqtrade/data/entryexitanalysis.py
Executable file
227
freqtrade/data/entryexitanalysis.py
Executable file
@ -0,0 +1,227 @@
|
|||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
import joblib
|
||||||
|
import pandas as pd
|
||||||
|
from tabulate import tabulate
|
||||||
|
|
||||||
|
from freqtrade.data.btanalysis import (get_latest_backtest_filename, load_backtest_data,
|
||||||
|
load_backtest_stats)
|
||||||
|
from freqtrade.exceptions import OperationalException
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _load_signal_candles(backtest_dir: Path):
|
||||||
|
if backtest_dir.is_dir():
|
||||||
|
scpf = Path(backtest_dir,
|
||||||
|
Path(get_latest_backtest_filename(backtest_dir)).stem + "_signals.pkl"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
scpf = Path(backtest_dir.parent / f"{backtest_dir.stem}_signals.pkl")
|
||||||
|
|
||||||
|
try:
|
||||||
|
scp = open(scpf, "rb")
|
||||||
|
signal_candles = joblib.load(scp)
|
||||||
|
logger.info(f"Loaded signal candles: {str(scpf)}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Cannot load signal candles from pickled results: ", e)
|
||||||
|
|
||||||
|
return signal_candles
|
||||||
|
|
||||||
|
|
||||||
|
def _process_candles_and_indicators(pairlist, strategy_name, trades, signal_candles):
|
||||||
|
analysed_trades_dict = {}
|
||||||
|
analysed_trades_dict[strategy_name] = {}
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.info(f"Processing {strategy_name} : {len(pairlist)} pairs")
|
||||||
|
|
||||||
|
for pair in pairlist:
|
||||||
|
if pair in signal_candles[strategy_name]:
|
||||||
|
analysed_trades_dict[strategy_name][pair] = _analyze_candles_and_indicators(
|
||||||
|
pair,
|
||||||
|
trades,
|
||||||
|
signal_candles[strategy_name][pair])
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Cannot process entry/exit reasons for {strategy_name}: ", e)
|
||||||
|
|
||||||
|
return analysed_trades_dict
|
||||||
|
|
||||||
|
|
||||||
|
def _analyze_candles_and_indicators(pair, trades, signal_candles):
|
||||||
|
buyf = signal_candles
|
||||||
|
|
||||||
|
if len(buyf) > 0:
|
||||||
|
buyf = buyf.set_index('date', drop=False)
|
||||||
|
trades_red = trades.loc[trades['pair'] == pair].copy()
|
||||||
|
|
||||||
|
trades_inds = pd.DataFrame()
|
||||||
|
|
||||||
|
if trades_red.shape[0] > 0 and buyf.shape[0] > 0:
|
||||||
|
for t, v in trades_red.open_date.items():
|
||||||
|
allinds = buyf.loc[(buyf['date'] < v)]
|
||||||
|
if allinds.shape[0] > 0:
|
||||||
|
tmp_inds = allinds.iloc[[-1]]
|
||||||
|
|
||||||
|
trades_red.loc[t, 'signal_date'] = tmp_inds['date'].values[0]
|
||||||
|
trades_red.loc[t, 'enter_reason'] = trades_red.loc[t, 'enter_tag']
|
||||||
|
tmp_inds.index.rename('signal_date', inplace=True)
|
||||||
|
trades_inds = pd.concat([trades_inds, tmp_inds])
|
||||||
|
|
||||||
|
if 'signal_date' in trades_red:
|
||||||
|
trades_red['signal_date'] = pd.to_datetime(trades_red['signal_date'], utc=True)
|
||||||
|
trades_red.set_index('signal_date', inplace=True)
|
||||||
|
|
||||||
|
try:
|
||||||
|
trades_red = pd.merge(trades_red, trades_inds, on='signal_date', how='outer')
|
||||||
|
except Exception as e:
|
||||||
|
raise e
|
||||||
|
return trades_red
|
||||||
|
else:
|
||||||
|
return pd.DataFrame()
|
||||||
|
|
||||||
|
|
||||||
|
def _do_group_table_output(bigdf, glist):
|
||||||
|
for g in glist:
|
||||||
|
# 0: summary wins/losses grouped by enter tag
|
||||||
|
if g == "0":
|
||||||
|
group_mask = ['enter_reason']
|
||||||
|
wins = bigdf.loc[bigdf['profit_abs'] >= 0] \
|
||||||
|
.groupby(group_mask) \
|
||||||
|
.agg({'profit_abs': ['sum']})
|
||||||
|
|
||||||
|
wins.columns = ['profit_abs_wins']
|
||||||
|
loss = bigdf.loc[bigdf['profit_abs'] < 0] \
|
||||||
|
.groupby(group_mask) \
|
||||||
|
.agg({'profit_abs': ['sum']})
|
||||||
|
loss.columns = ['profit_abs_loss']
|
||||||
|
|
||||||
|
new = bigdf.groupby(group_mask).agg({'profit_abs': [
|
||||||
|
'count',
|
||||||
|
lambda x: sum(x > 0),
|
||||||
|
lambda x: sum(x <= 0)]})
|
||||||
|
new = pd.concat([new, wins, loss], axis=1).fillna(0)
|
||||||
|
|
||||||
|
new['profit_tot'] = new['profit_abs_wins'] - abs(new['profit_abs_loss'])
|
||||||
|
new['wl_ratio_pct'] = (new.iloc[:, 1] / new.iloc[:, 0] * 100).fillna(0)
|
||||||
|
new['avg_win'] = (new['profit_abs_wins'] / new.iloc[:, 1]).fillna(0)
|
||||||
|
new['avg_loss'] = (new['profit_abs_loss'] / new.iloc[:, 2]).fillna(0)
|
||||||
|
|
||||||
|
new.columns = ['total_num_buys', 'wins', 'losses', 'profit_abs_wins', 'profit_abs_loss',
|
||||||
|
'profit_tot', 'wl_ratio_pct', 'avg_win', 'avg_loss']
|
||||||
|
|
||||||
|
sortcols = ['total_num_buys']
|
||||||
|
|
||||||
|
_print_table(new, sortcols, show_index=True)
|
||||||
|
|
||||||
|
else:
|
||||||
|
agg_mask = {'profit_abs': ['count', 'sum', 'median', 'mean'],
|
||||||
|
'profit_ratio': ['sum', 'median', 'mean']}
|
||||||
|
agg_cols = ['num_buys', 'profit_abs_sum', 'profit_abs_median',
|
||||||
|
'profit_abs_mean', 'median_profit_pct', 'mean_profit_pct',
|
||||||
|
'total_profit_pct']
|
||||||
|
sortcols = ['profit_abs_sum', 'enter_reason']
|
||||||
|
|
||||||
|
# 1: profit summaries grouped by enter_tag
|
||||||
|
if g == "1":
|
||||||
|
group_mask = ['enter_reason']
|
||||||
|
|
||||||
|
# 2: profit summaries grouped by enter_tag and exit_tag
|
||||||
|
if g == "2":
|
||||||
|
group_mask = ['enter_reason', 'exit_reason']
|
||||||
|
|
||||||
|
# 3: profit summaries grouped by pair and enter_tag
|
||||||
|
if g == "3":
|
||||||
|
group_mask = ['pair', 'enter_reason']
|
||||||
|
|
||||||
|
# 4: profit summaries grouped by pair, enter_ and exit_tag (this can get quite large)
|
||||||
|
if g == "4":
|
||||||
|
group_mask = ['pair', 'enter_reason', 'exit_reason']
|
||||||
|
if group_mask:
|
||||||
|
new = bigdf.groupby(group_mask).agg(agg_mask).reset_index()
|
||||||
|
new.columns = group_mask + agg_cols
|
||||||
|
new['median_profit_pct'] = new['median_profit_pct'] * 100
|
||||||
|
new['mean_profit_pct'] = new['mean_profit_pct'] * 100
|
||||||
|
new['total_profit_pct'] = new['total_profit_pct'] * 100
|
||||||
|
|
||||||
|
_print_table(new, sortcols)
|
||||||
|
else:
|
||||||
|
logger.warning("Invalid group mask specified.")
|
||||||
|
|
||||||
|
|
||||||
|
def _print_results(analysed_trades, stratname, analysis_groups,
|
||||||
|
enter_reason_list, exit_reason_list,
|
||||||
|
indicator_list, columns=None):
|
||||||
|
if columns is None:
|
||||||
|
columns = ['pair', 'open_date', 'close_date', 'profit_abs', 'enter_reason', 'exit_reason']
|
||||||
|
|
||||||
|
bigdf = pd.DataFrame()
|
||||||
|
for pair, trades in analysed_trades[stratname].items():
|
||||||
|
bigdf = pd.concat([bigdf, trades], ignore_index=True)
|
||||||
|
|
||||||
|
if bigdf.shape[0] > 0 and ('enter_reason' in bigdf.columns):
|
||||||
|
if analysis_groups:
|
||||||
|
_do_group_table_output(bigdf, analysis_groups)
|
||||||
|
|
||||||
|
if enter_reason_list and "all" not in enter_reason_list:
|
||||||
|
bigdf = bigdf.loc[(bigdf['enter_reason'].isin(enter_reason_list))]
|
||||||
|
|
||||||
|
if exit_reason_list and "all" not in exit_reason_list:
|
||||||
|
bigdf = bigdf.loc[(bigdf['exit_reason'].isin(exit_reason_list))]
|
||||||
|
|
||||||
|
if "all" in indicator_list:
|
||||||
|
print(bigdf)
|
||||||
|
elif indicator_list is not None:
|
||||||
|
available_inds = []
|
||||||
|
for ind in indicator_list:
|
||||||
|
if ind in bigdf:
|
||||||
|
available_inds.append(ind)
|
||||||
|
ilist = ["pair", "enter_reason", "exit_reason"] + available_inds
|
||||||
|
_print_table(bigdf[ilist], sortcols=['exit_reason'], show_index=False)
|
||||||
|
else:
|
||||||
|
print("\\_ No trades to show")
|
||||||
|
|
||||||
|
|
||||||
|
def _print_table(df, sortcols=None, show_index=False):
|
||||||
|
if (sortcols is not None):
|
||||||
|
data = df.sort_values(sortcols)
|
||||||
|
else:
|
||||||
|
data = df
|
||||||
|
|
||||||
|
print(
|
||||||
|
tabulate(
|
||||||
|
data,
|
||||||
|
headers='keys',
|
||||||
|
tablefmt='psql',
|
||||||
|
showindex=show_index
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def process_entry_exit_reasons(backtest_dir: Path,
|
||||||
|
pairlist: List[str],
|
||||||
|
analysis_groups: Optional[List[str]] = ["0", "1", "2"],
|
||||||
|
enter_reason_list: Optional[List[str]] = ["all"],
|
||||||
|
exit_reason_list: Optional[List[str]] = ["all"],
|
||||||
|
indicator_list: Optional[List[str]] = []):
|
||||||
|
try:
|
||||||
|
backtest_stats = load_backtest_stats(backtest_dir)
|
||||||
|
for strategy_name, results in backtest_stats['strategy'].items():
|
||||||
|
trades = load_backtest_data(backtest_dir, strategy_name)
|
||||||
|
|
||||||
|
if not trades.empty:
|
||||||
|
signal_candles = _load_signal_candles(backtest_dir)
|
||||||
|
analysed_trades_dict = _process_candles_and_indicators(pairlist, strategy_name,
|
||||||
|
trades, signal_candles)
|
||||||
|
_print_results(analysed_trades_dict,
|
||||||
|
strategy_name,
|
||||||
|
analysis_groups,
|
||||||
|
enter_reason_list,
|
||||||
|
exit_reason_list,
|
||||||
|
indicator_list)
|
||||||
|
|
||||||
|
except ValueError as e:
|
||||||
|
raise OperationalException(e) from e
|
@ -93,7 +93,7 @@ class Exchange:
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
self._api: ccxt.Exchange
|
self._api: ccxt.Exchange
|
||||||
self._api_async: ccxt_async.Exchange
|
self._api_async: ccxt_async.Exchange = None
|
||||||
self._markets: Dict = {}
|
self._markets: Dict = {}
|
||||||
self._trading_fees: Dict[str, Any] = {}
|
self._trading_fees: Dict[str, Any] = {}
|
||||||
self._leverage_tiers: Dict[str, List[Dict]] = {}
|
self._leverage_tiers: Dict[str, List[Dict]] = {}
|
||||||
|
@ -3,6 +3,7 @@ import logging
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Dict, List, Optional, Tuple
|
from typing import Dict, List, Optional, Tuple
|
||||||
|
|
||||||
|
from freqtrade.constants import BuySell
|
||||||
from freqtrade.enums import MarginMode, TradingMode
|
from freqtrade.enums import MarginMode, TradingMode
|
||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
from freqtrade.exchange import Exchange
|
from freqtrade.exchange import Exchange
|
||||||
@ -24,6 +25,8 @@ class Gateio(Exchange):
|
|||||||
_ft_has: Dict = {
|
_ft_has: Dict = {
|
||||||
"ohlcv_candle_limit": 1000,
|
"ohlcv_candle_limit": 1000,
|
||||||
"ohlcv_volume_currency": "quote",
|
"ohlcv_volume_currency": "quote",
|
||||||
|
"time_in_force_parameter": "timeInForce",
|
||||||
|
"order_time_in_force": ['gtc', 'ioc'],
|
||||||
"stoploss_order_types": {"limit": "limit"},
|
"stoploss_order_types": {"limit": "limit"},
|
||||||
"stoploss_on_exchange": True,
|
"stoploss_on_exchange": True,
|
||||||
}
|
}
|
||||||
@ -40,13 +43,33 @@ class Gateio(Exchange):
|
|||||||
]
|
]
|
||||||
|
|
||||||
def validate_ordertypes(self, order_types: Dict) -> None:
|
def validate_ordertypes(self, order_types: Dict) -> None:
|
||||||
super().validate_ordertypes(order_types)
|
|
||||||
|
|
||||||
if self.trading_mode != TradingMode.FUTURES:
|
if self.trading_mode != TradingMode.FUTURES:
|
||||||
if any(v == 'market' for k, v in order_types.items()):
|
if any(v == 'market' for k, v in order_types.items()):
|
||||||
raise OperationalException(
|
raise OperationalException(
|
||||||
f'Exchange {self.name} does not support market orders.')
|
f'Exchange {self.name} does not support market orders.')
|
||||||
|
|
||||||
|
def _get_params(
|
||||||
|
self,
|
||||||
|
side: BuySell,
|
||||||
|
ordertype: str,
|
||||||
|
leverage: float,
|
||||||
|
reduceOnly: bool,
|
||||||
|
time_in_force: str = 'gtc',
|
||||||
|
) -> Dict:
|
||||||
|
params = super()._get_params(
|
||||||
|
side=side,
|
||||||
|
ordertype=ordertype,
|
||||||
|
leverage=leverage,
|
||||||
|
reduceOnly=reduceOnly,
|
||||||
|
time_in_force=time_in_force,
|
||||||
|
)
|
||||||
|
if ordertype == 'market' and self.trading_mode == TradingMode.FUTURES:
|
||||||
|
params['type'] = 'market'
|
||||||
|
param = self._ft_has.get('time_in_force_parameter', '')
|
||||||
|
params.update({param: 'ioc'})
|
||||||
|
return params
|
||||||
|
|
||||||
def get_trades_for_order(self, order_id: str, pair: str, since: datetime,
|
def get_trades_for_order(self, order_id: str, pair: str, since: datetime,
|
||||||
params: Optional[Dict] = None) -> List:
|
params: Optional[Dict] = None) -> List:
|
||||||
trades = super().get_trades_for_order(order_id, pair, since, params)
|
trades = super().get_trades_for_order(order_id, pair, since, params)
|
||||||
@ -61,7 +84,8 @@ class Gateio(Exchange):
|
|||||||
pair_fees = self._trading_fees.get(pair, {})
|
pair_fees = self._trading_fees.get(pair, {})
|
||||||
if pair_fees:
|
if pair_fees:
|
||||||
for idx, trade in enumerate(trades):
|
for idx, trade in enumerate(trades):
|
||||||
if trade.get('fee', {}).get('cost') is None:
|
fee = trade.get('fee', {})
|
||||||
|
if fee and fee.get('cost') is None:
|
||||||
takerOrMaker = trade.get('takerOrMaker', 'taker')
|
takerOrMaker = trade.get('takerOrMaker', 'taker')
|
||||||
if pair_fees.get(takerOrMaker) is not None:
|
if pair_fees.get(takerOrMaker) is not None:
|
||||||
trades[idx]['fee'] = {
|
trades[idx]['fee'] = {
|
||||||
|
@ -73,8 +73,6 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
|
|
||||||
PairLocks.timeframe = self.config['timeframe']
|
PairLocks.timeframe = self.config['timeframe']
|
||||||
|
|
||||||
self.protections = ProtectionManager(self.config, self.strategy.protections)
|
|
||||||
|
|
||||||
# RPC runs in separate threads, can start handling external commands just after
|
# RPC runs in separate threads, can start handling external commands just after
|
||||||
# initialization, even before Freqtradebot has a chance to start its throttling,
|
# initialization, even before Freqtradebot has a chance to start its throttling,
|
||||||
# so anything in the Freqtradebot instance should be ready (initialized), including
|
# so anything in the Freqtradebot instance should be ready (initialized), including
|
||||||
@ -124,6 +122,8 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
self.last_process = datetime(1970, 1, 1, tzinfo=timezone.utc)
|
self.last_process = datetime(1970, 1, 1, tzinfo=timezone.utc)
|
||||||
|
|
||||||
self.strategy.ft_bot_start()
|
self.strategy.ft_bot_start()
|
||||||
|
# Initialize protections AFTER bot start - otherwise parameters are not loaded.
|
||||||
|
self.protections = ProtectionManager(self.config, self.strategy.protections)
|
||||||
|
|
||||||
def notify_status(self, msg: str) -> None:
|
def notify_status(self, msg: str) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -1058,6 +1058,7 @@ class Backtesting:
|
|||||||
# Close trade
|
# Close trade
|
||||||
open_trade_count -= 1
|
open_trade_count -= 1
|
||||||
open_trades[pair].remove(t)
|
open_trades[pair].remove(t)
|
||||||
|
LocalTrade.trades_open.remove(t)
|
||||||
self.wallets.update()
|
self.wallets.update()
|
||||||
|
|
||||||
# 2. Process entries.
|
# 2. Process entries.
|
||||||
@ -1081,6 +1082,8 @@ class Backtesting:
|
|||||||
open_trade_count += 1
|
open_trade_count += 1
|
||||||
# logger.debug(f"{pair} - Emulate creation of new trade: {trade}.")
|
# logger.debug(f"{pair} - Emulate creation of new trade: {trade}.")
|
||||||
open_trades[pair].append(trade)
|
open_trades[pair].append(trade)
|
||||||
|
LocalTrade.add_bt_trade(trade)
|
||||||
|
self.wallets.update()
|
||||||
|
|
||||||
for trade in list(open_trades[pair]):
|
for trade in list(open_trades[pair]):
|
||||||
# 3. Process entry orders.
|
# 3. Process entry orders.
|
||||||
@ -1088,7 +1091,6 @@ class Backtesting:
|
|||||||
if order and self._get_order_filled(order.price, row):
|
if order and self._get_order_filled(order.price, row):
|
||||||
order.close_bt_order(current_time, trade)
|
order.close_bt_order(current_time, trade)
|
||||||
trade.open_order_id = None
|
trade.open_order_id = None
|
||||||
LocalTrade.add_bt_trade(trade)
|
|
||||||
self.wallets.update()
|
self.wallets.update()
|
||||||
|
|
||||||
# 4. Create exit orders (if any)
|
# 4. Create exit orders (if any)
|
||||||
@ -1267,13 +1269,14 @@ class Backtesting:
|
|||||||
self.results['strategy_comparison'].extend(results['strategy_comparison'])
|
self.results['strategy_comparison'].extend(results['strategy_comparison'])
|
||||||
else:
|
else:
|
||||||
self.results = results
|
self.results = results
|
||||||
|
dt_appendix = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
if self.config.get('export', 'none') in ('trades', 'signals'):
|
if self.config.get('export', 'none') in ('trades', 'signals'):
|
||||||
store_backtest_stats(self.config['exportfilename'], self.results)
|
store_backtest_stats(self.config['exportfilename'], self.results, dt_appendix)
|
||||||
|
|
||||||
if (self.config.get('export', 'none') == 'signals' and
|
if (self.config.get('export', 'none') == 'signals' and
|
||||||
self.dataprovider.runmode == RunMode.BACKTEST):
|
self.dataprovider.runmode == RunMode.BACKTEST):
|
||||||
store_backtest_signal_candles(self.config['exportfilename'], self.processed_dfs)
|
store_backtest_signal_candles(
|
||||||
|
self.config['exportfilename'], self.processed_dfs, dt_appendix)
|
||||||
|
|
||||||
# Results may be mixed up now. Sort them so they follow --strategy-list order.
|
# Results may be mixed up now. Sort them so they follow --strategy-list order.
|
||||||
if 'strategy_list' in self.config and len(self.results) > 0:
|
if 'strategy_list' in self.config and len(self.results) > 0:
|
||||||
|
@ -17,21 +17,21 @@ from freqtrade.optimize.backtest_caching import get_backtest_metadata_filename
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def store_backtest_stats(recordfilename: Path, stats: Dict[str, DataFrame]) -> None:
|
def store_backtest_stats(
|
||||||
|
recordfilename: Path, stats: Dict[str, DataFrame], dtappendix: str) -> None:
|
||||||
"""
|
"""
|
||||||
Stores backtest results
|
Stores backtest results
|
||||||
:param recordfilename: Path object, which can either be a filename or a directory.
|
:param recordfilename: Path object, which can either be a filename or a directory.
|
||||||
Filenames will be appended with a timestamp right before the suffix
|
Filenames will be appended with a timestamp right before the suffix
|
||||||
while for directories, <directory>/backtest-result-<datetime>.json will be used as filename
|
while for directories, <directory>/backtest-result-<datetime>.json will be used as filename
|
||||||
:param stats: Dataframe containing the backtesting statistics
|
:param stats: Dataframe containing the backtesting statistics
|
||||||
|
:param dtappendix: Datetime to use for the filename
|
||||||
"""
|
"""
|
||||||
if recordfilename.is_dir():
|
if recordfilename.is_dir():
|
||||||
filename = (recordfilename /
|
filename = (recordfilename / f'backtest-result-{dtappendix}.json')
|
||||||
f'backtest-result-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.json')
|
|
||||||
else:
|
else:
|
||||||
filename = Path.joinpath(
|
filename = Path.joinpath(
|
||||||
recordfilename.parent,
|
recordfilename.parent, f'{recordfilename.stem}-{dtappendix}'
|
||||||
f'{recordfilename.stem}-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}'
|
|
||||||
).with_suffix(recordfilename.suffix)
|
).with_suffix(recordfilename.suffix)
|
||||||
|
|
||||||
# Store metadata separately.
|
# Store metadata separately.
|
||||||
@ -44,7 +44,8 @@ def store_backtest_stats(recordfilename: Path, stats: Dict[str, DataFrame]) -> N
|
|||||||
file_dump_json(latest_filename, {'latest_backtest': str(filename.name)})
|
file_dump_json(latest_filename, {'latest_backtest': str(filename.name)})
|
||||||
|
|
||||||
|
|
||||||
def store_backtest_signal_candles(recordfilename: Path, candles: Dict[str, Dict]) -> Path:
|
def store_backtest_signal_candles(
|
||||||
|
recordfilename: Path, candles: Dict[str, Dict], dtappendix: str) -> Path:
|
||||||
"""
|
"""
|
||||||
Stores backtest trade signal candles
|
Stores backtest trade signal candles
|
||||||
:param recordfilename: Path object, which can either be a filename or a directory.
|
:param recordfilename: Path object, which can either be a filename or a directory.
|
||||||
@ -52,14 +53,13 @@ def store_backtest_signal_candles(recordfilename: Path, candles: Dict[str, Dict]
|
|||||||
while for directories, <directory>/backtest-result-<datetime>_signals.pkl will be used
|
while for directories, <directory>/backtest-result-<datetime>_signals.pkl will be used
|
||||||
as filename
|
as filename
|
||||||
:param stats: Dict containing the backtesting signal candles
|
:param stats: Dict containing the backtesting signal candles
|
||||||
|
:param dtappendix: Datetime to use for the filename
|
||||||
"""
|
"""
|
||||||
if recordfilename.is_dir():
|
if recordfilename.is_dir():
|
||||||
filename = (recordfilename /
|
filename = (recordfilename / f'backtest-result-{dtappendix}_signals.pkl')
|
||||||
f'backtest-result-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}_signals.pkl')
|
|
||||||
else:
|
else:
|
||||||
filename = Path.joinpath(
|
filename = Path.joinpath(
|
||||||
recordfilename.parent,
|
recordfilename.parent, f'{recordfilename.stem}-{dtappendix}_signals.pkl'
|
||||||
f'{recordfilename.stem}-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}_signals.pkl'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
file_dump_joblib(filename, candles)
|
file_dump_joblib(filename, candles)
|
||||||
@ -416,6 +416,9 @@ def generate_strategy_stats(pairlist: List[str],
|
|||||||
key=lambda x: x['profit_sum']) if len(pair_results) > 1 else None
|
key=lambda x: x['profit_sum']) if len(pair_results) > 1 else None
|
||||||
worst_pair = min([pair for pair in pair_results if pair['key'] != 'TOTAL'],
|
worst_pair = min([pair for pair in pair_results if pair['key'] != 'TOTAL'],
|
||||||
key=lambda x: x['profit_sum']) if len(pair_results) > 1 else None
|
key=lambda x: x['profit_sum']) if len(pair_results) > 1 else None
|
||||||
|
winning_profit = results.loc[results['profit_abs'] > 0, 'profit_abs'].sum()
|
||||||
|
losing_profit = results.loc[results['profit_abs'] < 0, 'profit_abs'].sum()
|
||||||
|
profit_factor = winning_profit / abs(losing_profit) if losing_profit else 0.0
|
||||||
|
|
||||||
backtest_days = (max_date - min_date).days or 1
|
backtest_days = (max_date - min_date).days or 1
|
||||||
strat_stats = {
|
strat_stats = {
|
||||||
@ -443,6 +446,7 @@ def generate_strategy_stats(pairlist: List[str],
|
|||||||
'profit_total_long_abs': results.loc[~results['is_short'], 'profit_abs'].sum(),
|
'profit_total_long_abs': results.loc[~results['is_short'], 'profit_abs'].sum(),
|
||||||
'profit_total_short_abs': results.loc[results['is_short'], 'profit_abs'].sum(),
|
'profit_total_short_abs': results.loc[results['is_short'], 'profit_abs'].sum(),
|
||||||
'cagr': calculate_cagr(backtest_days, start_balance, content['final_balance']),
|
'cagr': calculate_cagr(backtest_days, start_balance, content['final_balance']),
|
||||||
|
'profit_factor': profit_factor,
|
||||||
'backtest_start': min_date.strftime(DATETIME_PRINT_FORMAT),
|
'backtest_start': min_date.strftime(DATETIME_PRINT_FORMAT),
|
||||||
'backtest_start_ts': int(min_date.timestamp() * 1000),
|
'backtest_start_ts': int(min_date.timestamp() * 1000),
|
||||||
'backtest_end': max_date.strftime(DATETIME_PRINT_FORMAT),
|
'backtest_end': max_date.strftime(DATETIME_PRINT_FORMAT),
|
||||||
@ -497,8 +501,10 @@ def generate_strategy_stats(pairlist: List[str],
|
|||||||
(drawdown_abs, drawdown_start, drawdown_end, high_val, low_val,
|
(drawdown_abs, drawdown_start, drawdown_end, high_val, low_val,
|
||||||
max_drawdown) = calculate_max_drawdown(
|
max_drawdown) = calculate_max_drawdown(
|
||||||
results, value_col='profit_abs', starting_balance=start_balance)
|
results, value_col='profit_abs', starting_balance=start_balance)
|
||||||
|
# max_relative_drawdown = Underwater
|
||||||
(_, _, _, _, _, max_relative_drawdown) = calculate_max_drawdown(
|
(_, _, _, _, _, max_relative_drawdown) = calculate_max_drawdown(
|
||||||
results, value_col='profit_abs', starting_balance=start_balance, relative=True)
|
results, value_col='profit_abs', starting_balance=start_balance, relative=True)
|
||||||
|
|
||||||
strat_stats.update({
|
strat_stats.update({
|
||||||
'max_drawdown': max_drawdown_legacy, # Deprecated - do not use
|
'max_drawdown': max_drawdown_legacy, # Deprecated - do not use
|
||||||
'max_drawdown_account': max_drawdown,
|
'max_drawdown_account': max_drawdown,
|
||||||
@ -777,6 +783,8 @@ def text_table_add_metrics(strat_results: Dict) -> str:
|
|||||||
strat_results['stake_currency'])),
|
strat_results['stake_currency'])),
|
||||||
('Total profit %', f"{strat_results['profit_total']:.2%}"),
|
('Total profit %', f"{strat_results['profit_total']:.2%}"),
|
||||||
('CAGR %', f"{strat_results['cagr']:.2%}" if 'cagr' in strat_results else 'N/A'),
|
('CAGR %', f"{strat_results['cagr']:.2%}" if 'cagr' in strat_results else 'N/A'),
|
||||||
|
('Profit factor', f'{strat_results["profit_factor"]:.2f}' if 'profit_factor'
|
||||||
|
in strat_results else 'N/A'),
|
||||||
('Trades per day', strat_results['trades_per_day']),
|
('Trades per day', strat_results['trades_per_day']),
|
||||||
('Avg. daily profit %',
|
('Avg. daily profit %',
|
||||||
f"{(strat_results['profit_total'] / strat_results['backtest_days']):.2%}"),
|
f"{(strat_results['profit_total'] / strat_results['backtest_days']):.2%}"),
|
||||||
|
@ -627,8 +627,8 @@ class LocalTrade():
|
|||||||
"""
|
"""
|
||||||
self.close_rate = rate
|
self.close_rate = rate
|
||||||
self.close_date = self.close_date or datetime.utcnow()
|
self.close_date = self.close_date or datetime.utcnow()
|
||||||
self.close_profit = self.calc_profit_ratio()
|
self.close_profit = self.calc_profit_ratio(rate)
|
||||||
self.close_profit_abs = self.calc_profit()
|
self.close_profit_abs = self.calc_profit(rate)
|
||||||
self.is_open = False
|
self.is_open = False
|
||||||
self.exit_order_status = 'closed'
|
self.exit_order_status = 'closed'
|
||||||
self.open_order_id = None
|
self.open_order_id = None
|
||||||
@ -696,10 +696,9 @@ class LocalTrade():
|
|||||||
"""
|
"""
|
||||||
self.open_trade_value = self._calc_open_trade_value()
|
self.open_trade_value = self._calc_open_trade_value()
|
||||||
|
|
||||||
def calculate_interest(self, interest_rate: Optional[float] = None) -> Decimal:
|
def calculate_interest(self) -> Decimal:
|
||||||
"""
|
"""
|
||||||
:param interest_rate: interest_charge for borrowing this coin(optional).
|
Calculate interest for this trade. Only applicable for Margin trading.
|
||||||
If interest_rate is not set self.interest_rate will be used
|
|
||||||
"""
|
"""
|
||||||
zero = Decimal(0.0)
|
zero = Decimal(0.0)
|
||||||
# If nothing was borrowed
|
# If nothing was borrowed
|
||||||
@ -712,34 +711,26 @@ class LocalTrade():
|
|||||||
total_seconds = Decimal((now - open_date).total_seconds())
|
total_seconds = Decimal((now - open_date).total_seconds())
|
||||||
hours = total_seconds / sec_per_hour or zero
|
hours = total_seconds / sec_per_hour or zero
|
||||||
|
|
||||||
rate = Decimal(interest_rate or self.interest_rate)
|
rate = Decimal(self.interest_rate)
|
||||||
borrowed = Decimal(self.borrowed)
|
borrowed = Decimal(self.borrowed)
|
||||||
|
|
||||||
return interest(exchange_name=self.exchange, borrowed=borrowed, rate=rate, hours=hours)
|
return interest(exchange_name=self.exchange, borrowed=borrowed, rate=rate, hours=hours)
|
||||||
|
|
||||||
def _calc_base_close(self, amount: Decimal, rate: Optional[float] = None,
|
def _calc_base_close(self, amount: Decimal, rate: float, fee: float) -> Decimal:
|
||||||
fee: Optional[float] = None) -> Decimal:
|
|
||||||
|
|
||||||
close_trade = Decimal(amount) * Decimal(rate or self.close_rate) # type: ignore
|
close_trade = amount * Decimal(rate)
|
||||||
fees = close_trade * Decimal(fee or self.fee_close)
|
fees = close_trade * Decimal(fee)
|
||||||
|
|
||||||
if self.is_short:
|
if self.is_short:
|
||||||
return close_trade + fees
|
return close_trade + fees
|
||||||
else:
|
else:
|
||||||
return close_trade - fees
|
return close_trade - fees
|
||||||
|
|
||||||
def calc_close_trade_value(self, rate: Optional[float] = None,
|
def calc_close_trade_value(self, rate: float) -> float:
|
||||||
fee: Optional[float] = None,
|
|
||||||
interest_rate: Optional[float] = None) -> float:
|
|
||||||
"""
|
"""
|
||||||
Calculate the close_rate including fee
|
Calculate the Trade's close value including fees
|
||||||
:param fee: fee to use on the close rate (optional).
|
:param rate: rate to compare with.
|
||||||
If rate is not set self.fee will be used
|
:return: value in stake currency of the open trade
|
||||||
:param rate: rate to compare with (optional).
|
|
||||||
If rate is not set self.close_rate will be used
|
|
||||||
:param interest_rate: interest_charge for borrowing this coin (optional).
|
|
||||||
If interest_rate is not set self.interest_rate will be used
|
|
||||||
:return: Price in BTC of the open trade
|
|
||||||
"""
|
"""
|
||||||
if rate is None and not self.close_rate:
|
if rate is None and not self.close_rate:
|
||||||
return 0.0
|
return 0.0
|
||||||
@ -748,49 +739,38 @@ class LocalTrade():
|
|||||||
trading_mode = self.trading_mode or TradingMode.SPOT
|
trading_mode = self.trading_mode or TradingMode.SPOT
|
||||||
|
|
||||||
if trading_mode == TradingMode.SPOT:
|
if trading_mode == TradingMode.SPOT:
|
||||||
return float(self._calc_base_close(amount, rate, fee))
|
return float(self._calc_base_close(amount, rate, self.fee_close))
|
||||||
|
|
||||||
elif (trading_mode == TradingMode.MARGIN):
|
elif (trading_mode == TradingMode.MARGIN):
|
||||||
|
|
||||||
total_interest = self.calculate_interest(interest_rate)
|
total_interest = self.calculate_interest()
|
||||||
|
|
||||||
if self.is_short:
|
if self.is_short:
|
||||||
amount = amount + total_interest
|
amount = amount + total_interest
|
||||||
return float(self._calc_base_close(amount, rate, fee))
|
return float(self._calc_base_close(amount, rate, self.fee_close))
|
||||||
else:
|
else:
|
||||||
# Currency already owned for longs, no need to purchase
|
# Currency already owned for longs, no need to purchase
|
||||||
return float(self._calc_base_close(amount, rate, fee) - total_interest)
|
return float(self._calc_base_close(amount, rate, self.fee_close) - total_interest)
|
||||||
|
|
||||||
elif (trading_mode == TradingMode.FUTURES):
|
elif (trading_mode == TradingMode.FUTURES):
|
||||||
funding_fees = self.funding_fees or 0.0
|
funding_fees = self.funding_fees or 0.0
|
||||||
# Positive funding_fees -> Trade has gained from fees.
|
# Positive funding_fees -> Trade has gained from fees.
|
||||||
# Negative funding_fees -> Trade had to pay the fees.
|
# Negative funding_fees -> Trade had to pay the fees.
|
||||||
if self.is_short:
|
if self.is_short:
|
||||||
return float(self._calc_base_close(amount, rate, fee)) - funding_fees
|
return float(self._calc_base_close(amount, rate, self.fee_close)) - funding_fees
|
||||||
else:
|
else:
|
||||||
return float(self._calc_base_close(amount, rate, fee)) + funding_fees
|
return float(self._calc_base_close(amount, rate, self.fee_close)) + funding_fees
|
||||||
else:
|
else:
|
||||||
raise OperationalException(
|
raise OperationalException(
|
||||||
f"{self.trading_mode.value} trading is not yet available using freqtrade")
|
f"{self.trading_mode.value} trading is not yet available using freqtrade")
|
||||||
|
|
||||||
def calc_profit(self, rate: Optional[float] = None,
|
def calc_profit(self, rate: float) -> float:
|
||||||
fee: Optional[float] = None,
|
|
||||||
interest_rate: Optional[float] = None) -> float:
|
|
||||||
"""
|
"""
|
||||||
Calculate the absolute profit in stake currency between Close and Open trade
|
Calculate the absolute profit in stake currency between Close and Open trade
|
||||||
:param fee: fee to use on the close rate (optional).
|
:param rate: close rate to compare with.
|
||||||
If fee is not set self.fee will be used
|
|
||||||
:param rate: close rate to compare with (optional).
|
|
||||||
If rate is not set self.close_rate will be used
|
|
||||||
:param interest_rate: interest_charge for borrowing this coin (optional).
|
|
||||||
If interest_rate is not set self.interest_rate will be used
|
|
||||||
:return: profit in stake currency as float
|
:return: profit in stake currency as float
|
||||||
"""
|
"""
|
||||||
close_trade_value = self.calc_close_trade_value(
|
close_trade_value = self.calc_close_trade_value(rate)
|
||||||
rate=(rate or self.close_rate),
|
|
||||||
fee=(fee or self.fee_close),
|
|
||||||
interest_rate=(interest_rate or self.interest_rate)
|
|
||||||
)
|
|
||||||
|
|
||||||
if self.is_short:
|
if self.is_short:
|
||||||
profit = self.open_trade_value - close_trade_value
|
profit = self.open_trade_value - close_trade_value
|
||||||
@ -798,23 +778,13 @@ class LocalTrade():
|
|||||||
profit = close_trade_value - self.open_trade_value
|
profit = close_trade_value - self.open_trade_value
|
||||||
return float(f"{profit:.8f}")
|
return float(f"{profit:.8f}")
|
||||||
|
|
||||||
def calc_profit_ratio(self, rate: Optional[float] = None,
|
def calc_profit_ratio(self, rate: float) -> float:
|
||||||
fee: Optional[float] = None,
|
|
||||||
interest_rate: Optional[float] = None) -> float:
|
|
||||||
"""
|
"""
|
||||||
Calculates the profit as ratio (including fee).
|
Calculates the profit as ratio (including fee).
|
||||||
:param rate: rate to compare with (optional).
|
:param rate: rate to compare with.
|
||||||
If rate is not set self.close_rate will be used
|
|
||||||
:param fee: fee to use on the close rate (optional).
|
|
||||||
:param interest_rate: interest_charge for borrowing this coin (optional).
|
|
||||||
If interest_rate is not set self.interest_rate will be used
|
|
||||||
:return: profit ratio as float
|
:return: profit ratio as float
|
||||||
"""
|
"""
|
||||||
close_trade_value = self.calc_close_trade_value(
|
close_trade_value = self.calc_close_trade_value(rate)
|
||||||
rate=(rate or self.close_rate),
|
|
||||||
fee=(fee or self.fee_close),
|
|
||||||
interest_rate=(interest_rate or self.interest_rate)
|
|
||||||
)
|
|
||||||
|
|
||||||
short_close_zero = (self.is_short and close_trade_value == 0.0)
|
short_close_zero = (self.is_short and close_trade_value == 0.0)
|
||||||
long_close_zero = (not self.is_short and self.open_trade_value == 0.0)
|
long_close_zero = (not self.is_short and self.open_trade_value == 0.0)
|
||||||
@ -831,14 +801,6 @@ class LocalTrade():
|
|||||||
return float(f"{profit_ratio:.8f}")
|
return float(f"{profit_ratio:.8f}")
|
||||||
|
|
||||||
def recalc_trade_from_orders(self):
|
def recalc_trade_from_orders(self):
|
||||||
# We need at least 2 entry orders for averaging amounts and rates.
|
|
||||||
# TODO: this condition could probably be removed
|
|
||||||
if len(self.select_filled_orders(self.entry_side)) < 2:
|
|
||||||
self.stake_amount = self.amount * self.open_rate / self.leverage
|
|
||||||
|
|
||||||
# Just in case, still recalc open trade value
|
|
||||||
self.recalc_open_trade_value()
|
|
||||||
return
|
|
||||||
|
|
||||||
total_amount = 0.0
|
total_amount = 0.0
|
||||||
total_stake = 0.0
|
total_stake = 0.0
|
||||||
@ -1410,3 +1372,18 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
|
|
||||||
def get_kvals(self, key: Optional[str]) -> List[KeyValue]:
|
def get_kvals(self, key: Optional[str]) -> List[KeyValue]:
|
||||||
return super().get_kvals(key=key)
|
return super().get_kvals(key=key)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_trading_volume(start_date: datetime = datetime.fromtimestamp(0)) -> float:
|
||||||
|
"""
|
||||||
|
Get Trade volume based on Orders
|
||||||
|
NOTE: Not supported in Backtesting.
|
||||||
|
:returns: Tuple containing (pair, profit_sum)
|
||||||
|
"""
|
||||||
|
trading_volume = Order.query.with_entities(
|
||||||
|
func.sum(Order.cost).label('volume')
|
||||||
|
).filter(
|
||||||
|
Order.order_filled_date >= start_date,
|
||||||
|
Order.status == 'closed'
|
||||||
|
).scalar()
|
||||||
|
return trading_volume
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
from datetime import datetime
|
||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
from fastapi import APIRouter, BackgroundTasks, Depends
|
from fastapi import APIRouter, BackgroundTasks, Depends
|
||||||
@ -102,7 +103,10 @@ async def api_start_backtest(bt_settings: BacktestRequest, background_tasks: Bac
|
|||||||
min_date=min_date, max_date=max_date)
|
min_date=min_date, max_date=max_date)
|
||||||
|
|
||||||
if btconfig.get('export', 'none') == 'trades':
|
if btconfig.get('export', 'none') == 'trades':
|
||||||
store_backtest_stats(btconfig['exportfilename'], ApiServer._bt.results)
|
store_backtest_stats(
|
||||||
|
btconfig['exportfilename'], ApiServer._bt.results,
|
||||||
|
datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
|
)
|
||||||
|
|
||||||
logger.info("Backtest finished.")
|
logger.info("Backtest finished.")
|
||||||
|
|
||||||
|
@ -104,6 +104,10 @@ class Profit(BaseModel):
|
|||||||
best_pair_profit_ratio: float
|
best_pair_profit_ratio: float
|
||||||
winning_trades: int
|
winning_trades: int
|
||||||
losing_trades: int
|
losing_trades: int
|
||||||
|
profit_factor: float
|
||||||
|
max_drawdown: float
|
||||||
|
max_drawdown_abs: float
|
||||||
|
trading_volume: Optional[float]
|
||||||
|
|
||||||
|
|
||||||
class SellReason(BaseModel):
|
class SellReason(BaseModel):
|
||||||
|
@ -18,6 +18,7 @@ from freqtrade import __version__
|
|||||||
from freqtrade.configuration.timerange import TimeRange
|
from freqtrade.configuration.timerange import TimeRange
|
||||||
from freqtrade.constants import CANCEL_REASON, DATETIME_PRINT_FORMAT
|
from freqtrade.constants import CANCEL_REASON, DATETIME_PRINT_FORMAT
|
||||||
from freqtrade.data.history import load_data
|
from freqtrade.data.history import load_data
|
||||||
|
from freqtrade.data.metrics import calculate_max_drawdown
|
||||||
from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, SignalDirection, State,
|
from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, SignalDirection, State,
|
||||||
TradingMode)
|
TradingMode)
|
||||||
from freqtrade.exceptions import ExchangeError, PricingError
|
from freqtrade.exceptions import ExchangeError, PricingError
|
||||||
@ -415,6 +416,8 @@ class RPC:
|
|||||||
durations = []
|
durations = []
|
||||||
winning_trades = 0
|
winning_trades = 0
|
||||||
losing_trades = 0
|
losing_trades = 0
|
||||||
|
winning_profit = 0.0
|
||||||
|
losing_profit = 0.0
|
||||||
|
|
||||||
for trade in trades:
|
for trade in trades:
|
||||||
current_rate: float = 0.0
|
current_rate: float = 0.0
|
||||||
@ -430,8 +433,10 @@ class RPC:
|
|||||||
profit_closed_ratio.append(profit_ratio)
|
profit_closed_ratio.append(profit_ratio)
|
||||||
if trade.close_profit >= 0:
|
if trade.close_profit >= 0:
|
||||||
winning_trades += 1
|
winning_trades += 1
|
||||||
|
winning_profit += trade.close_profit_abs
|
||||||
else:
|
else:
|
||||||
losing_trades += 1
|
losing_trades += 1
|
||||||
|
losing_profit += trade.close_profit_abs
|
||||||
else:
|
else:
|
||||||
# Get current rate
|
# Get current rate
|
||||||
try:
|
try:
|
||||||
@ -447,6 +452,7 @@ class RPC:
|
|||||||
profit_all_ratio.append(profit_ratio)
|
profit_all_ratio.append(profit_ratio)
|
||||||
|
|
||||||
best_pair = Trade.get_best_pair(start_date)
|
best_pair = Trade.get_best_pair(start_date)
|
||||||
|
trading_volume = Trade.get_trading_volume(start_date)
|
||||||
|
|
||||||
# Prepare data to display
|
# Prepare data to display
|
||||||
profit_closed_coin_sum = round(sum(profit_closed_coin), 8)
|
profit_closed_coin_sum = round(sum(profit_closed_coin), 8)
|
||||||
@ -470,6 +476,21 @@ class RPC:
|
|||||||
profit_closed_ratio_fromstart = profit_closed_coin_sum / starting_balance
|
profit_closed_ratio_fromstart = profit_closed_coin_sum / starting_balance
|
||||||
profit_all_ratio_fromstart = profit_all_coin_sum / starting_balance
|
profit_all_ratio_fromstart = profit_all_coin_sum / starting_balance
|
||||||
|
|
||||||
|
profit_factor = winning_profit / abs(losing_profit) if losing_profit else float('inf')
|
||||||
|
|
||||||
|
trades_df = DataFrame([{'close_date': trade.close_date.strftime(DATETIME_PRINT_FORMAT),
|
||||||
|
'profit_abs': trade.close_profit_abs}
|
||||||
|
for trade in trades if not trade.is_open])
|
||||||
|
max_drawdown_abs = 0.0
|
||||||
|
max_drawdown = 0.0
|
||||||
|
if len(trades_df) > 0:
|
||||||
|
try:
|
||||||
|
(max_drawdown_abs, _, _, _, _, max_drawdown) = calculate_max_drawdown(
|
||||||
|
trades_df, value_col='profit_abs', starting_balance=starting_balance)
|
||||||
|
except ValueError:
|
||||||
|
# ValueError if no losing trade.
|
||||||
|
pass
|
||||||
|
|
||||||
profit_all_fiat = self._fiat_converter.convert_amount(
|
profit_all_fiat = self._fiat_converter.convert_amount(
|
||||||
profit_all_coin_sum,
|
profit_all_coin_sum,
|
||||||
stake_currency,
|
stake_currency,
|
||||||
@ -508,11 +529,15 @@ class RPC:
|
|||||||
'best_pair_profit_ratio': best_pair[1] if best_pair else 0,
|
'best_pair_profit_ratio': best_pair[1] if best_pair else 0,
|
||||||
'winning_trades': winning_trades,
|
'winning_trades': winning_trades,
|
||||||
'losing_trades': losing_trades,
|
'losing_trades': losing_trades,
|
||||||
|
'profit_factor': profit_factor,
|
||||||
|
'max_drawdown': max_drawdown,
|
||||||
|
'max_drawdown_abs': max_drawdown_abs,
|
||||||
|
'trading_volume': trading_volume,
|
||||||
}
|
}
|
||||||
|
|
||||||
def _rpc_balance(self, stake_currency: str, fiat_display_currency: str) -> Dict:
|
def _rpc_balance(self, stake_currency: str, fiat_display_currency: str) -> Dict:
|
||||||
""" Returns current account balance per crypto """
|
""" Returns current account balance per crypto """
|
||||||
currencies = []
|
currencies: List[Dict] = []
|
||||||
total = 0.0
|
total = 0.0
|
||||||
try:
|
try:
|
||||||
tickers = self._freqtrade.exchange.get_tickers(cached=True)
|
tickers = self._freqtrade.exchange.get_tickers(cached=True)
|
||||||
@ -547,13 +572,12 @@ class RPC:
|
|||||||
except (ExchangeError):
|
except (ExchangeError):
|
||||||
logger.warning(f" Could not get rate for pair {coin}.")
|
logger.warning(f" Could not get rate for pair {coin}.")
|
||||||
continue
|
continue
|
||||||
total = total + (est_stake or 0)
|
total = total + est_stake
|
||||||
currencies.append({
|
currencies.append({
|
||||||
'currency': coin,
|
'currency': coin,
|
||||||
# TODO: The below can be simplified if we don't assign None to values.
|
'free': balance.free,
|
||||||
'free': balance.free if balance.free is not None else 0,
|
'balance': balance.total,
|
||||||
'balance': balance.total if balance.total is not None else 0,
|
'used': balance.used,
|
||||||
'used': balance.used if balance.used is not None else 0,
|
|
||||||
'est_stake': est_stake or 0,
|
'est_stake': est_stake or 0,
|
||||||
'stake': stake_currency,
|
'stake': stake_currency,
|
||||||
'side': 'long',
|
'side': 'long',
|
||||||
@ -583,7 +607,6 @@ class RPC:
|
|||||||
total, stake_currency, fiat_display_currency) if self._fiat_converter else 0
|
total, stake_currency, fiat_display_currency) if self._fiat_converter else 0
|
||||||
|
|
||||||
trade_count = len(Trade.get_trades_proxy())
|
trade_count = len(Trade.get_trades_proxy())
|
||||||
starting_capital_ratio = 0.0
|
|
||||||
starting_capital_ratio = (total / starting_capital) - 1 if starting_capital else 0.0
|
starting_capital_ratio = (total / starting_capital) - 1 if starting_capital else 0.0
|
||||||
starting_cap_fiat_ratio = (value / starting_cap_fiat) - 1 if starting_cap_fiat else 0.0
|
starting_cap_fiat_ratio = (value / starting_cap_fiat) - 1 if starting_cap_fiat else 0.0
|
||||||
|
|
||||||
|
@ -236,6 +236,14 @@ class Telegram(RPCHandler):
|
|||||||
# This can take up to `timeout` from the call to `start_polling`.
|
# This can take up to `timeout` from the call to `start_polling`.
|
||||||
self._updater.stop()
|
self._updater.stop()
|
||||||
|
|
||||||
|
def _exchange_from_msg(self, msg: Dict[str, Any]) -> str:
|
||||||
|
"""
|
||||||
|
Extracts the exchange name from the given message.
|
||||||
|
:param msg: The message to extract the exchange name from.
|
||||||
|
:return: The exchange name.
|
||||||
|
"""
|
||||||
|
return f"{msg['exchange']}{' (dry)' if self._config['dry_run'] else ''}"
|
||||||
|
|
||||||
def _format_entry_msg(self, msg: Dict[str, Any]) -> str:
|
def _format_entry_msg(self, msg: Dict[str, Any]) -> str:
|
||||||
if self._rpc._fiat_converter:
|
if self._rpc._fiat_converter:
|
||||||
msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount(
|
msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount(
|
||||||
@ -248,7 +256,7 @@ class Telegram(RPCHandler):
|
|||||||
entry_side = ({'enter': 'Long', 'entered': 'Longed'} if msg['direction'] == 'Long'
|
entry_side = ({'enter': 'Long', 'entered': 'Longed'} if msg['direction'] == 'Long'
|
||||||
else {'enter': 'Short', 'entered': 'Shorted'})
|
else {'enter': 'Short', 'entered': 'Shorted'})
|
||||||
message = (
|
message = (
|
||||||
f"{emoji} *{msg['exchange']}:*"
|
f"{emoji} *{self._exchange_from_msg(msg)}:*"
|
||||||
f" {entry_side['entered'] if is_fill else entry_side['enter']} {msg['pair']}"
|
f" {entry_side['entered'] if is_fill else entry_side['enter']} {msg['pair']}"
|
||||||
f" (#{msg['trade_id']})\n"
|
f" (#{msg['trade_id']})\n"
|
||||||
)
|
)
|
||||||
@ -297,7 +305,7 @@ class Telegram(RPCHandler):
|
|||||||
msg['profit_extra'] = ''
|
msg['profit_extra'] = ''
|
||||||
is_fill = msg['type'] == RPCMessageType.EXIT_FILL
|
is_fill = msg['type'] == RPCMessageType.EXIT_FILL
|
||||||
message = (
|
message = (
|
||||||
f"{msg['emoji']} *{msg['exchange']}:* "
|
f"{msg['emoji']} *{self._exchange_from_msg(msg)}:* "
|
||||||
f"{'Exited' if is_fill else 'Exiting'} {msg['pair']} (#{msg['trade_id']})\n"
|
f"{'Exited' if is_fill else 'Exiting'} {msg['pair']} (#{msg['trade_id']})\n"
|
||||||
f"*{'Profit' if is_fill else 'Unrealized Profit'}:* "
|
f"*{'Profit' if is_fill else 'Unrealized Profit'}:* "
|
||||||
f"`{msg['profit_ratio']:.2%}{msg['profit_extra']}`\n"
|
f"`{msg['profit_ratio']:.2%}{msg['profit_extra']}`\n"
|
||||||
@ -327,33 +335,33 @@ class Telegram(RPCHandler):
|
|||||||
|
|
||||||
elif msg_type in (RPCMessageType.ENTRY_CANCEL, RPCMessageType.EXIT_CANCEL):
|
elif msg_type in (RPCMessageType.ENTRY_CANCEL, RPCMessageType.EXIT_CANCEL):
|
||||||
msg['message_side'] = 'enter' if msg_type in [RPCMessageType.ENTRY_CANCEL] else 'exit'
|
msg['message_side'] = 'enter' if msg_type in [RPCMessageType.ENTRY_CANCEL] else 'exit'
|
||||||
message = ("\N{WARNING SIGN} *{exchange}:* "
|
message = (f"\N{WARNING SIGN} *{self._exchange_from_msg(msg)}:* "
|
||||||
"Cancelling {message_side} Order for {pair} (#{trade_id}). "
|
f"Cancelling {msg['message_side']} Order for {msg['pair']} "
|
||||||
"Reason: {reason}.".format(**msg))
|
f"(#{msg['trade_id']}). Reason: {msg['reason']}.")
|
||||||
|
|
||||||
elif msg_type == RPCMessageType.PROTECTION_TRIGGER:
|
elif msg_type == RPCMessageType.PROTECTION_TRIGGER:
|
||||||
message = (
|
message = (
|
||||||
"*Protection* triggered due to {reason}. "
|
f"*Protection* triggered due to {msg['reason']}. "
|
||||||
"`{pair}` will be locked until `{lock_end_time}`."
|
f"`{msg['pair']}` will be locked until `{msg['lock_end_time']}`."
|
||||||
).format(**msg)
|
)
|
||||||
|
|
||||||
elif msg_type == RPCMessageType.PROTECTION_TRIGGER_GLOBAL:
|
elif msg_type == RPCMessageType.PROTECTION_TRIGGER_GLOBAL:
|
||||||
message = (
|
message = (
|
||||||
"*Protection* triggered due to {reason}. "
|
f"*Protection* triggered due to {msg['reason']}. "
|
||||||
"*All pairs* will be locked until `{lock_end_time}`."
|
f"*All pairs* will be locked until `{msg['lock_end_time']}`."
|
||||||
).format(**msg)
|
)
|
||||||
|
|
||||||
elif msg_type == RPCMessageType.STATUS:
|
elif msg_type == RPCMessageType.STATUS:
|
||||||
message = '*Status:* `{status}`'.format(**msg)
|
message = f"*Status:* `{msg['status']}`"
|
||||||
|
|
||||||
elif msg_type == RPCMessageType.WARNING:
|
elif msg_type == RPCMessageType.WARNING:
|
||||||
message = '\N{WARNING SIGN} *Warning:* `{status}`'.format(**msg)
|
message = f"\N{WARNING SIGN} *Warning:* `{msg['status']}`"
|
||||||
|
|
||||||
elif msg_type == RPCMessageType.STARTUP:
|
elif msg_type == RPCMessageType.STARTUP:
|
||||||
message = '{status}'.format(**msg)
|
message = f"{msg['status']}"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError('Unknown message type: {}'.format(msg_type))
|
raise NotImplementedError(f"Unknown message type: {msg_type}")
|
||||||
return message
|
return message
|
||||||
|
|
||||||
def send_msg(self, msg: Dict[str, Any]) -> None:
|
def send_msg(self, msg: Dict[str, Any]) -> None:
|
||||||
@ -723,12 +731,18 @@ class Telegram(RPCHandler):
|
|||||||
f"*Total Trade Count:* `{trade_count}`\n"
|
f"*Total Trade Count:* `{trade_count}`\n"
|
||||||
f"*{'First Trade opened' if not timescale else 'Showing Profit since'}:* "
|
f"*{'First Trade opened' if not timescale else 'Showing Profit since'}:* "
|
||||||
f"`{first_trade_date}`\n"
|
f"`{first_trade_date}`\n"
|
||||||
f"*Latest Trade opened:* `{latest_trade_date}\n`"
|
f"*Latest Trade opened:* `{latest_trade_date}`\n"
|
||||||
f"*Win / Loss:* `{stats['winning_trades']} / {stats['losing_trades']}`"
|
f"*Win / Loss:* `{stats['winning_trades']} / {stats['losing_trades']}`"
|
||||||
)
|
)
|
||||||
if stats['closed_trade_count'] > 0:
|
if stats['closed_trade_count'] > 0:
|
||||||
markdown_msg += (f"\n*Avg. Duration:* `{avg_duration}`\n"
|
markdown_msg += (
|
||||||
f"*Best Performing:* `{best_pair}: {best_pair_profit_ratio:.2%}`")
|
f"\n*Avg. Duration:* `{avg_duration}`\n"
|
||||||
|
f"*Best Performing:* `{best_pair}: {best_pair_profit_ratio:.2%}`\n"
|
||||||
|
f"*Trading volume:* `{round_coin_value(stats['trading_volume'], stake_cur)}`\n"
|
||||||
|
f"*Profit factor:* `{stats['profit_factor']:.2f}`\n"
|
||||||
|
f"*Max Drawdown:* `{stats['max_drawdown']:.2%} "
|
||||||
|
f"({round_coin_value(stats['max_drawdown_abs'], stake_cur)})`"
|
||||||
|
)
|
||||||
self._send_msg(markdown_msg, reload_able=True, callback_path="update_profit",
|
self._send_msg(markdown_msg, reload_able=True, callback_path="update_profit",
|
||||||
query=update.callback_query)
|
query=update.callback_query)
|
||||||
|
|
||||||
@ -868,7 +882,7 @@ class Telegram(RPCHandler):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
msg = self._rpc._rpc_start()
|
msg = self._rpc._rpc_start()
|
||||||
self._send_msg('Status: `{status}`'.format(**msg))
|
self._send_msg(f"Status: `{msg['status']}`")
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _stop(self, update: Update, context: CallbackContext) -> None:
|
def _stop(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -880,7 +894,7 @@ class Telegram(RPCHandler):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
msg = self._rpc._rpc_stop()
|
msg = self._rpc._rpc_stop()
|
||||||
self._send_msg('Status: `{status}`'.format(**msg))
|
self._send_msg(f"Status: `{msg['status']}`")
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _reload_config(self, update: Update, context: CallbackContext) -> None:
|
def _reload_config(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -892,7 +906,7 @@ class Telegram(RPCHandler):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
msg = self._rpc._rpc_reload_config()
|
msg = self._rpc._rpc_reload_config()
|
||||||
self._send_msg('Status: `{status}`'.format(**msg))
|
self._send_msg(f"Status: `{msg['status']}`")
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _stopbuy(self, update: Update, context: CallbackContext) -> None:
|
def _stopbuy(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -904,7 +918,7 @@ class Telegram(RPCHandler):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
msg = self._rpc._rpc_stopbuy()
|
msg = self._rpc._rpc_stopbuy()
|
||||||
self._send_msg('Status: `{status}`'.format(**msg))
|
self._send_msg(f"Status: `{msg['status']}`")
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _force_exit(self, update: Update, context: CallbackContext) -> None:
|
def _force_exit(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1066,9 +1080,9 @@ class Telegram(RPCHandler):
|
|||||||
trade_id = int(context.args[0])
|
trade_id = int(context.args[0])
|
||||||
msg = self._rpc._rpc_delete(trade_id)
|
msg = self._rpc._rpc_delete(trade_id)
|
||||||
self._send_msg((
|
self._send_msg((
|
||||||
'`{result_msg}`\n'
|
f"`{msg['result_msg']}`\n"
|
||||||
'Please make sure to take care of this asset on the exchange manually.'
|
'Please make sure to take care of this asset on the exchange manually.'
|
||||||
).format(**msg))
|
))
|
||||||
|
|
||||||
except RPCException as e:
|
except RPCException as e:
|
||||||
self._send_msg(str(e))
|
self._send_msg(str(e))
|
||||||
@ -1396,7 +1410,7 @@ class Telegram(RPCHandler):
|
|||||||
"*/stopbuy:* `Stops buying, but handles open trades gracefully` \n"
|
"*/stopbuy:* `Stops buying, but handles open trades gracefully` \n"
|
||||||
"*/forceexit <trade_id>|all:* `Instantly exits the given trade or all trades, "
|
"*/forceexit <trade_id>|all:* `Instantly exits the given trade or all trades, "
|
||||||
"regardless of profit`\n"
|
"regardless of profit`\n"
|
||||||
"*/fe <trade_id>|all:* `Alias to /forceexit`\n"
|
"*/fx <trade_id>|all:* `Alias to /forceexit`\n"
|
||||||
f"{force_enter_text if self._config.get('force_entry_enable', False) else ''}"
|
f"{force_enter_text if self._config.get('force_entry_enable', False) else ''}"
|
||||||
"*/delete <trade_id>:* `Instantly delete the given trade in the database`\n"
|
"*/delete <trade_id>:* `Instantly delete the given trade in the database`\n"
|
||||||
"*/whitelist:* `Show current whitelist` \n"
|
"*/whitelist:* `Show current whitelist` \n"
|
||||||
|
@ -29,6 +29,7 @@ def mock_order_1(is_short: bool):
|
|||||||
'average': 0.123,
|
'average': 0.123,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
|
'cost': 15.129,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +66,7 @@ def mock_order_2(is_short: bool):
|
|||||||
'price': 0.123,
|
'price': 0.123,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
|
'cost': 15.129,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,6 +81,7 @@ def mock_order_2_sell(is_short: bool):
|
|||||||
'price': 0.128,
|
'price': 0.128,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
|
'cost': 15.129,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,6 +129,7 @@ def mock_order_3(is_short: bool):
|
|||||||
'price': 0.05,
|
'price': 0.05,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
|
'cost': 15.129,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,6 +145,7 @@ def mock_order_3_sell(is_short: bool):
|
|||||||
'average': 0.06,
|
'average': 0.06,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
|
'cost': 15.129,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,6 +191,7 @@ def mock_order_4(is_short: bool):
|
|||||||
'price': 0.123,
|
'price': 0.123,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 0.0,
|
'filled': 0.0,
|
||||||
|
'cost': 15.129,
|
||||||
'remaining': 123.0,
|
'remaining': 123.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,6 +231,7 @@ def mock_order_5(is_short: bool):
|
|||||||
'price': 0.123,
|
'price': 0.123,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
|
'cost': 15.129,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,6 +246,7 @@ def mock_order_5_stoploss(is_short: bool):
|
|||||||
'price': 0.123,
|
'price': 0.123,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 0.0,
|
'filled': 0.0,
|
||||||
|
'cost': 0.0,
|
||||||
'remaining': 123.0,
|
'remaining': 123.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -281,6 +289,7 @@ def mock_order_6(is_short: bool):
|
|||||||
'price': 0.15,
|
'price': 0.15,
|
||||||
'amount': 2.0,
|
'amount': 2.0,
|
||||||
'filled': 2.0,
|
'filled': 2.0,
|
||||||
|
'cost': 0.3,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,6 +304,7 @@ def mock_order_6_sell(is_short: bool):
|
|||||||
'price': 0.15 if is_short else 0.20,
|
'price': 0.15 if is_short else 0.20,
|
||||||
'amount': 2.0,
|
'amount': 2.0,
|
||||||
'filled': 0.0,
|
'filled': 0.0,
|
||||||
|
'cost': 0.0,
|
||||||
'remaining': 2.0,
|
'remaining': 2.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,6 +347,7 @@ def short_order():
|
|||||||
'price': 0.123,
|
'price': 0.123,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
|
'cost': 15.129,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -351,6 +362,7 @@ def exit_short_order():
|
|||||||
'price': 0.128,
|
'price': 0.128,
|
||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
|
'cost': 15.744,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -424,6 +436,7 @@ def leverage_order():
|
|||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
|
'cost': 15.129,
|
||||||
'leverage': 5.0
|
'leverage': 5.0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -439,6 +452,7 @@ def leverage_order_sell():
|
|||||||
'amount': 123.0,
|
'amount': 123.0,
|
||||||
'filled': 123.0,
|
'filled': 123.0,
|
||||||
'remaining': 0.0,
|
'remaining': 0.0,
|
||||||
|
'cost': 15.744,
|
||||||
'leverage': 5.0
|
'leverage': 5.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
191
tests/data/test_entryexitanalysis.py
Executable file
191
tests/data/test_entryexitanalysis.py
Executable file
@ -0,0 +1,191 @@
|
|||||||
|
import logging
|
||||||
|
from unittest.mock import MagicMock, PropertyMock
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from freqtrade.commands.analyze_commands import start_analysis_entries_exits
|
||||||
|
from freqtrade.commands.optimize_commands import start_backtesting
|
||||||
|
from freqtrade.enums import ExitType
|
||||||
|
from freqtrade.optimize.backtesting import Backtesting
|
||||||
|
from tests.conftest import get_args, patch_exchange, patched_configuration_load_config_file
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def entryexitanalysis_cleanup() -> None:
|
||||||
|
yield None
|
||||||
|
|
||||||
|
Backtesting.cleanup()
|
||||||
|
|
||||||
|
|
||||||
|
def test_backtest_analysis_nomock(default_conf, mocker, caplog, testdatadir, tmpdir, capsys):
|
||||||
|
caplog.set_level(logging.INFO)
|
||||||
|
|
||||||
|
default_conf.update({
|
||||||
|
"use_exit_signal": True,
|
||||||
|
"exit_profit_only": False,
|
||||||
|
"exit_profit_offset": 0.0,
|
||||||
|
"ignore_roi_if_entry_signal": False,
|
||||||
|
})
|
||||||
|
patch_exchange(mocker)
|
||||||
|
result1 = pd.DataFrame({'pair': ['ETH/BTC', 'LTC/BTC', 'ETH/BTC', 'LTC/BTC'],
|
||||||
|
'profit_ratio': [0.025, 0.05, -0.1, -0.05],
|
||||||
|
'profit_abs': [0.5, 2.0, -4.0, -2.0],
|
||||||
|
'open_date': pd.to_datetime(['2018-01-29 18:40:00',
|
||||||
|
'2018-01-30 03:30:00',
|
||||||
|
'2018-01-30 08:10:00',
|
||||||
|
'2018-01-31 13:30:00', ], utc=True
|
||||||
|
),
|
||||||
|
'close_date': pd.to_datetime(['2018-01-29 20:45:00',
|
||||||
|
'2018-01-30 05:35:00',
|
||||||
|
'2018-01-30 09:10:00',
|
||||||
|
'2018-01-31 15:00:00', ], utc=True),
|
||||||
|
'trade_duration': [235, 40, 60, 90],
|
||||||
|
'is_open': [False, False, False, False],
|
||||||
|
'stake_amount': [0.01, 0.01, 0.01, 0.01],
|
||||||
|
'open_rate': [0.104445, 0.10302485, 0.10302485, 0.10302485],
|
||||||
|
'close_rate': [0.104969, 0.103541, 0.102041, 0.102541],
|
||||||
|
"is_short": [False, False, False, False],
|
||||||
|
'enter_tag': ["enter_tag_long_a",
|
||||||
|
"enter_tag_long_b",
|
||||||
|
"enter_tag_long_a",
|
||||||
|
"enter_tag_long_b"],
|
||||||
|
'exit_reason': [ExitType.ROI,
|
||||||
|
ExitType.EXIT_SIGNAL,
|
||||||
|
ExitType.STOP_LOSS,
|
||||||
|
ExitType.TRAILING_STOP_LOSS]
|
||||||
|
})
|
||||||
|
|
||||||
|
backtestmock = MagicMock(side_effect=[
|
||||||
|
{
|
||||||
|
'results': result1,
|
||||||
|
'config': default_conf,
|
||||||
|
'locks': [],
|
||||||
|
'rejected_signals': 20,
|
||||||
|
'timedout_entry_orders': 0,
|
||||||
|
'timedout_exit_orders': 0,
|
||||||
|
'canceled_trade_entries': 0,
|
||||||
|
'canceled_entry_orders': 0,
|
||||||
|
'replaced_entry_orders': 0,
|
||||||
|
'final_balance': 1000,
|
||||||
|
}
|
||||||
|
])
|
||||||
|
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
|
||||||
|
PropertyMock(return_value=['ETH/BTC', 'LTC/BTC', 'DASH/BTC']))
|
||||||
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest', backtestmock)
|
||||||
|
|
||||||
|
patched_configuration_load_config_file(mocker, default_conf)
|
||||||
|
|
||||||
|
args = [
|
||||||
|
'backtesting',
|
||||||
|
'--config', 'config.json',
|
||||||
|
'--datadir', str(testdatadir),
|
||||||
|
'--user-data-dir', str(tmpdir),
|
||||||
|
'--timeframe', '5m',
|
||||||
|
'--timerange', '1515560100-1517287800',
|
||||||
|
'--export', 'signals',
|
||||||
|
'--cache', 'none',
|
||||||
|
]
|
||||||
|
args = get_args(args)
|
||||||
|
start_backtesting(args)
|
||||||
|
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert 'BACKTESTING REPORT' in captured.out
|
||||||
|
assert 'EXIT REASON STATS' in captured.out
|
||||||
|
assert 'LEFT OPEN TRADES REPORT' in captured.out
|
||||||
|
|
||||||
|
base_args = [
|
||||||
|
'backtesting-analysis',
|
||||||
|
'--config', 'config.json',
|
||||||
|
'--datadir', str(testdatadir),
|
||||||
|
'--user-data-dir', str(tmpdir),
|
||||||
|
]
|
||||||
|
|
||||||
|
# test group 0 and indicator list
|
||||||
|
args = get_args(base_args +
|
||||||
|
['--analysis-groups', "0",
|
||||||
|
'--indicator-list', "close", "rsi", "profit_abs"]
|
||||||
|
)
|
||||||
|
start_analysis_entries_exits(args)
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert 'LTC/BTC' in captured.out
|
||||||
|
assert 'ETH/BTC' in captured.out
|
||||||
|
assert 'enter_tag_long_a' in captured.out
|
||||||
|
assert 'enter_tag_long_b' in captured.out
|
||||||
|
assert 'exit_signal' in captured.out
|
||||||
|
assert 'roi' in captured.out
|
||||||
|
assert 'stop_loss' in captured.out
|
||||||
|
assert 'trailing_stop_loss' in captured.out
|
||||||
|
assert '0.5' in captured.out
|
||||||
|
assert '-4' in captured.out
|
||||||
|
assert '-2' in captured.out
|
||||||
|
assert '-3.5' in captured.out
|
||||||
|
assert '50' in captured.out
|
||||||
|
assert '0' in captured.out
|
||||||
|
assert '0.01616' in captured.out
|
||||||
|
assert '34.049' in captured.out
|
||||||
|
assert '0.104104' in captured.out
|
||||||
|
assert '47.0996' in captured.out
|
||||||
|
|
||||||
|
# test group 1
|
||||||
|
args = get_args(base_args + ['--analysis-groups', "1"])
|
||||||
|
start_analysis_entries_exits(args)
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert 'enter_tag_long_a' in captured.out
|
||||||
|
assert 'enter_tag_long_b' in captured.out
|
||||||
|
assert 'total_profit_pct' in captured.out
|
||||||
|
assert '-3.5' in captured.out
|
||||||
|
assert '-1.75' in captured.out
|
||||||
|
assert '-7.5' in captured.out
|
||||||
|
assert '-3.75' in captured.out
|
||||||
|
assert '0' in captured.out
|
||||||
|
|
||||||
|
# test group 2
|
||||||
|
args = get_args(base_args + ['--analysis-groups', "2"])
|
||||||
|
start_analysis_entries_exits(args)
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert 'enter_tag_long_a' in captured.out
|
||||||
|
assert 'enter_tag_long_b' in captured.out
|
||||||
|
assert 'exit_signal' in captured.out
|
||||||
|
assert 'roi' in captured.out
|
||||||
|
assert 'stop_loss' in captured.out
|
||||||
|
assert 'trailing_stop_loss' in captured.out
|
||||||
|
assert 'total_profit_pct' in captured.out
|
||||||
|
assert '-10' in captured.out
|
||||||
|
assert '-5' in captured.out
|
||||||
|
assert '2.5' in captured.out
|
||||||
|
|
||||||
|
# test group 3
|
||||||
|
args = get_args(base_args + ['--analysis-groups', "3"])
|
||||||
|
start_analysis_entries_exits(args)
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert 'LTC/BTC' in captured.out
|
||||||
|
assert 'ETH/BTC' in captured.out
|
||||||
|
assert 'enter_tag_long_a' in captured.out
|
||||||
|
assert 'enter_tag_long_b' in captured.out
|
||||||
|
assert 'total_profit_pct' in captured.out
|
||||||
|
assert '-7.5' in captured.out
|
||||||
|
assert '-3.75' in captured.out
|
||||||
|
assert '-1.75' in captured.out
|
||||||
|
assert '0' in captured.out
|
||||||
|
assert '2' in captured.out
|
||||||
|
|
||||||
|
# test group 4
|
||||||
|
args = get_args(base_args + ['--analysis-groups', "4"])
|
||||||
|
start_analysis_entries_exits(args)
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert 'LTC/BTC' in captured.out
|
||||||
|
assert 'ETH/BTC' in captured.out
|
||||||
|
assert 'enter_tag_long_a' in captured.out
|
||||||
|
assert 'enter_tag_long_b' in captured.out
|
||||||
|
assert 'exit_signal' in captured.out
|
||||||
|
assert 'roi' in captured.out
|
||||||
|
assert 'stop_loss' in captured.out
|
||||||
|
assert 'trailing_stop_loss' in captured.out
|
||||||
|
assert 'total_profit_pct' in captured.out
|
||||||
|
assert '-10' in captured.out
|
||||||
|
assert '-5' in captured.out
|
||||||
|
assert '-4' in captured.out
|
||||||
|
assert '0.5' in captured.out
|
||||||
|
assert '1' in captured.out
|
||||||
|
assert '2.5' in captured.out
|
@ -33,6 +33,12 @@ def test_validate_order_types_gateio(default_conf, mocker):
|
|||||||
match=r'Exchange .* does not support market orders.'):
|
match=r'Exchange .* does not support market orders.'):
|
||||||
ExchangeResolver.load_exchange('gateio', default_conf, True)
|
ExchangeResolver.load_exchange('gateio', default_conf, True)
|
||||||
|
|
||||||
|
# market-orders supported on futures markets.
|
||||||
|
default_conf['trading_mode'] = 'futures'
|
||||||
|
default_conf['margin_mode'] = 'isolated'
|
||||||
|
ex = ExchangeResolver.load_exchange('gateio', default_conf, True)
|
||||||
|
assert ex
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("init_persistence")
|
@pytest.mark.usefixtures("init_persistence")
|
||||||
def test_fetch_stoploss_order_gateio(default_conf, mocker):
|
def test_fetch_stoploss_order_gateio(default_conf, mocker):
|
||||||
|
@ -7,6 +7,7 @@ import pytest
|
|||||||
from freqtrade.data.history import get_timerange
|
from freqtrade.data.history import get_timerange
|
||||||
from freqtrade.enums import ExitType
|
from freqtrade.enums import ExitType
|
||||||
from freqtrade.optimize.backtesting import Backtesting
|
from freqtrade.optimize.backtesting import Backtesting
|
||||||
|
from freqtrade.persistence.trade_model import LocalTrade
|
||||||
from tests.conftest import patch_exchange
|
from tests.conftest import patch_exchange
|
||||||
from tests.optimize import (BTContainer, BTrade, _build_backtest_dataframe,
|
from tests.optimize import (BTContainer, BTrade, _build_backtest_dataframe,
|
||||||
_get_frame_time_from_offset, tests_timeframe)
|
_get_frame_time_from_offset, tests_timeframe)
|
||||||
@ -964,5 +965,7 @@ def test_backtest_results(default_conf, fee, mocker, caplog, data: BTContainer)
|
|||||||
assert res.open_date == _get_frame_time_from_offset(trade.open_tick)
|
assert res.open_date == _get_frame_time_from_offset(trade.open_tick)
|
||||||
assert res.close_date == _get_frame_time_from_offset(trade.close_tick)
|
assert res.close_date == _get_frame_time_from_offset(trade.close_tick)
|
||||||
assert res.is_short == trade.is_short
|
assert res.is_short == trade.is_short
|
||||||
|
assert len(LocalTrade.trades) == len(data.trades)
|
||||||
|
assert len(LocalTrade.trades_open) == 0
|
||||||
backtesting.cleanup()
|
backtesting.cleanup()
|
||||||
del backtesting
|
del backtesting
|
||||||
|
@ -171,7 +171,7 @@ def test_generate_backtest_stats(default_conf, testdatadir, tmpdir):
|
|||||||
_backup_file(filename_last, copy_file=True)
|
_backup_file(filename_last, copy_file=True)
|
||||||
assert not filename.is_file()
|
assert not filename.is_file()
|
||||||
|
|
||||||
store_backtest_stats(filename, stats)
|
store_backtest_stats(filename, stats, '2022_01_01_15_05_13')
|
||||||
|
|
||||||
# get real Filename (it's btresult-<date>.json)
|
# get real Filename (it's btresult-<date>.json)
|
||||||
last_fn = get_latest_backtest_filename(filename_last.parent)
|
last_fn = get_latest_backtest_filename(filename_last.parent)
|
||||||
@ -194,7 +194,7 @@ def test_store_backtest_stats(testdatadir, mocker):
|
|||||||
|
|
||||||
dump_mock = mocker.patch('freqtrade.optimize.optimize_reports.file_dump_json')
|
dump_mock = mocker.patch('freqtrade.optimize.optimize_reports.file_dump_json')
|
||||||
|
|
||||||
store_backtest_stats(testdatadir, {'metadata': {}})
|
store_backtest_stats(testdatadir, {'metadata': {}}, '2022_01_01_15_05_13')
|
||||||
|
|
||||||
assert dump_mock.call_count == 3
|
assert dump_mock.call_count == 3
|
||||||
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
||||||
@ -202,7 +202,7 @@ def test_store_backtest_stats(testdatadir, mocker):
|
|||||||
|
|
||||||
dump_mock.reset_mock()
|
dump_mock.reset_mock()
|
||||||
filename = testdatadir / 'testresult.json'
|
filename = testdatadir / 'testresult.json'
|
||||||
store_backtest_stats(filename, {'metadata': {}})
|
store_backtest_stats(filename, {'metadata': {}}, '2022_01_01_15_05_13')
|
||||||
assert dump_mock.call_count == 3
|
assert dump_mock.call_count == 3
|
||||||
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
||||||
# result will be testdatadir / testresult-<timestamp>.json
|
# result will be testdatadir / testresult-<timestamp>.json
|
||||||
@ -216,7 +216,7 @@ def test_store_backtest_candles(testdatadir, mocker):
|
|||||||
candle_dict = {'DefStrat': {'UNITTEST/BTC': pd.DataFrame()}}
|
candle_dict = {'DefStrat': {'UNITTEST/BTC': pd.DataFrame()}}
|
||||||
|
|
||||||
# mock directory exporting
|
# mock directory exporting
|
||||||
store_backtest_signal_candles(testdatadir, candle_dict)
|
store_backtest_signal_candles(testdatadir, candle_dict, '2022_01_01_15_05_13')
|
||||||
|
|
||||||
assert dump_mock.call_count == 1
|
assert dump_mock.call_count == 1
|
||||||
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
||||||
@ -225,7 +225,7 @@ def test_store_backtest_candles(testdatadir, mocker):
|
|||||||
dump_mock.reset_mock()
|
dump_mock.reset_mock()
|
||||||
# mock file exporting
|
# mock file exporting
|
||||||
filename = Path(testdatadir / 'testresult')
|
filename = Path(testdatadir / 'testresult')
|
||||||
store_backtest_signal_candles(filename, candle_dict)
|
store_backtest_signal_candles(filename, candle_dict, '2022_01_01_15_05_13')
|
||||||
assert dump_mock.call_count == 1
|
assert dump_mock.call_count == 1
|
||||||
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
||||||
# result will be testdatadir / testresult-<timestamp>_signals.pkl
|
# result will be testdatadir / testresult-<timestamp>_signals.pkl
|
||||||
@ -238,7 +238,7 @@ def test_write_read_backtest_candles(tmpdir):
|
|||||||
candle_dict = {'DefStrat': {'UNITTEST/BTC': pd.DataFrame()}}
|
candle_dict = {'DefStrat': {'UNITTEST/BTC': pd.DataFrame()}}
|
||||||
|
|
||||||
# test directory exporting
|
# test directory exporting
|
||||||
stored_file = store_backtest_signal_candles(Path(tmpdir), candle_dict)
|
stored_file = store_backtest_signal_candles(Path(tmpdir), candle_dict, '2022_01_01_15_05_13')
|
||||||
scp = open(stored_file, "rb")
|
scp = open(stored_file, "rb")
|
||||||
pickled_signal_candles = joblib.load(scp)
|
pickled_signal_candles = joblib.load(scp)
|
||||||
scp.close()
|
scp.close()
|
||||||
@ -252,7 +252,7 @@ def test_write_read_backtest_candles(tmpdir):
|
|||||||
|
|
||||||
# test file exporting
|
# test file exporting
|
||||||
filename = Path(tmpdir / 'testresult')
|
filename = Path(tmpdir / 'testresult')
|
||||||
stored_file = store_backtest_signal_candles(filename, candle_dict)
|
stored_file = store_backtest_signal_candles(filename, candle_dict, '2022_01_01_15_05_13')
|
||||||
scp = open(stored_file, "rb")
|
scp = open(stored_file, "rb")
|
||||||
pickled_signal_candles = joblib.load(scp)
|
pickled_signal_candles = joblib.load(scp)
|
||||||
scp.close()
|
scp.close()
|
||||||
|
@ -724,7 +724,9 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
|
|||||||
'profit_closed_fiat': -83.19455985, 'profit_closed_ratio_mean': -0.0075,
|
'profit_closed_fiat': -83.19455985, 'profit_closed_ratio_mean': -0.0075,
|
||||||
'profit_closed_percent_mean': -0.75, 'profit_closed_ratio_sum': -0.015,
|
'profit_closed_percent_mean': -0.75, 'profit_closed_ratio_sum': -0.015,
|
||||||
'profit_closed_percent_sum': -1.5, 'profit_closed_ratio': -6.739057628404269e-06,
|
'profit_closed_percent_sum': -1.5, 'profit_closed_ratio': -6.739057628404269e-06,
|
||||||
'profit_closed_percent': -0.0, 'winning_trades': 0, 'losing_trades': 2}
|
'profit_closed_percent': -0.0, 'winning_trades': 0, 'losing_trades': 2,
|
||||||
|
'profit_factor': 0.0, 'trading_volume': 91.074,
|
||||||
|
}
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
False,
|
False,
|
||||||
@ -737,7 +739,9 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
|
|||||||
'profit_closed_fiat': 9.124559849999999, 'profit_closed_ratio_mean': 0.0075,
|
'profit_closed_fiat': 9.124559849999999, 'profit_closed_ratio_mean': 0.0075,
|
||||||
'profit_closed_percent_mean': 0.75, 'profit_closed_ratio_sum': 0.015,
|
'profit_closed_percent_mean': 0.75, 'profit_closed_ratio_sum': 0.015,
|
||||||
'profit_closed_percent_sum': 1.5, 'profit_closed_ratio': 7.391275897987988e-07,
|
'profit_closed_percent_sum': 1.5, 'profit_closed_ratio': 7.391275897987988e-07,
|
||||||
'profit_closed_percent': 0.0, 'winning_trades': 2, 'losing_trades': 0}
|
'profit_closed_percent': 0.0, 'winning_trades': 2, 'losing_trades': 0,
|
||||||
|
'profit_factor': None, 'trading_volume': 91.074,
|
||||||
|
}
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
None,
|
None,
|
||||||
@ -750,7 +754,9 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
|
|||||||
'profit_closed_fiat': -67.02260985, 'profit_closed_ratio_mean': 0.0025,
|
'profit_closed_fiat': -67.02260985, 'profit_closed_ratio_mean': 0.0025,
|
||||||
'profit_closed_percent_mean': 0.25, 'profit_closed_ratio_sum': 0.005,
|
'profit_closed_percent_mean': 0.25, 'profit_closed_ratio_sum': 0.005,
|
||||||
'profit_closed_percent_sum': 0.5, 'profit_closed_ratio': -5.429078808526421e-06,
|
'profit_closed_percent_sum': 0.5, 'profit_closed_ratio': -5.429078808526421e-06,
|
||||||
'profit_closed_percent': -0.0, 'winning_trades': 1, 'losing_trades': 1}
|
'profit_closed_percent': -0.0, 'winning_trades': 1, 'losing_trades': 1,
|
||||||
|
'profit_factor': 0.02775724835771106, 'trading_volume': 91.074,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
])
|
])
|
||||||
def test_api_profit(botclient, mocker, ticker, fee, markets, is_short, expected):
|
def test_api_profit(botclient, mocker, ticker, fee, markets, is_short, expected):
|
||||||
@ -803,6 +809,10 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, is_short, expected)
|
|||||||
'closed_trade_count': 2,
|
'closed_trade_count': 2,
|
||||||
'winning_trades': expected['winning_trades'],
|
'winning_trades': expected['winning_trades'],
|
||||||
'losing_trades': expected['losing_trades'],
|
'losing_trades': expected['losing_trades'],
|
||||||
|
'profit_factor': expected['profit_factor'],
|
||||||
|
'max_drawdown': ANY,
|
||||||
|
'max_drawdown_abs': ANY,
|
||||||
|
'trading_volume': expected['trading_volume'],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -852,8 +862,8 @@ def test_api_performance(botclient, fee):
|
|||||||
close_rate=0.265441,
|
close_rate=0.265441,
|
||||||
|
|
||||||
)
|
)
|
||||||
trade.close_profit = trade.calc_profit_ratio()
|
trade.close_profit = trade.calc_profit_ratio(trade.close_rate)
|
||||||
trade.close_profit_abs = trade.calc_profit()
|
trade.close_profit_abs = trade.calc_profit(trade.close_rate)
|
||||||
Trade.query.session.add(trade)
|
Trade.query.session.add(trade)
|
||||||
|
|
||||||
trade = Trade(
|
trade = Trade(
|
||||||
@ -868,8 +878,8 @@ def test_api_performance(botclient, fee):
|
|||||||
fee_open=fee.return_value,
|
fee_open=fee.return_value,
|
||||||
close_rate=0.391
|
close_rate=0.391
|
||||||
)
|
)
|
||||||
trade.close_profit = trade.calc_profit_ratio()
|
trade.close_profit = trade.calc_profit_ratio(trade.close_rate)
|
||||||
trade.close_profit_abs = trade.calc_profit()
|
trade.close_profit_abs = trade.calc_profit(trade.close_rate)
|
||||||
|
|
||||||
Trade.query.session.add(trade)
|
Trade.query.session.add(trade)
|
||||||
Trade.commit()
|
Trade.commit()
|
||||||
@ -1384,12 +1394,14 @@ def test_api_strategies(botclient):
|
|||||||
rc = client_get(client, f"{BASE_URI}/strategies")
|
rc = client_get(client, f"{BASE_URI}/strategies")
|
||||||
|
|
||||||
assert_response(rc)
|
assert_response(rc)
|
||||||
|
|
||||||
assert rc.json() == {'strategies': [
|
assert rc.json() == {'strategies': [
|
||||||
'HyperoptableStrategy',
|
'HyperoptableStrategy',
|
||||||
'InformativeDecoratorTest',
|
'InformativeDecoratorTest',
|
||||||
'StrategyTestV2',
|
'StrategyTestV2',
|
||||||
'StrategyTestV3',
|
'StrategyTestV3',
|
||||||
'StrategyTestV3Futures',
|
'StrategyTestV3Analysis',
|
||||||
|
'StrategyTestV3Futures'
|
||||||
]}
|
]}
|
||||||
|
|
||||||
|
|
||||||
|
@ -704,11 +704,13 @@ def test_profit_handle(default_conf_usdt, update, ticker_usdt, ticker_sell_up, f
|
|||||||
assert '∙ `6.253 USD`' in msg_mock.call_args_list[-1][0][0]
|
assert '∙ `6.253 USD`' in msg_mock.call_args_list[-1][0][0]
|
||||||
|
|
||||||
assert '*Best Performing:* `ETH/USDT: 9.45%`' in msg_mock.call_args_list[-1][0][0]
|
assert '*Best Performing:* `ETH/USDT: 9.45%`' in msg_mock.call_args_list[-1][0][0]
|
||||||
|
assert '*Max Drawdown:*' in msg_mock.call_args_list[-1][0][0]
|
||||||
|
assert '*Profit factor:*' in msg_mock.call_args_list[-1][0][0]
|
||||||
|
assert '*Trading volume:* `60 USDT`' in msg_mock.call_args_list[-1][0][0]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('is_short', [True, False])
|
@pytest.mark.parametrize('is_short', [True, False])
|
||||||
def test_telegram_stats(default_conf, update, ticker, ticker_sell_up, fee,
|
def test_telegram_stats(default_conf, update, ticker, fee, mocker, is_short) -> None:
|
||||||
limit_buy_order, limit_sell_order, mocker, is_short) -> None:
|
|
||||||
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
||||||
mocker.patch.multiple(
|
mocker.patch.multiple(
|
||||||
'freqtrade.exchange.Exchange',
|
'freqtrade.exchange.Exchange',
|
||||||
@ -1680,7 +1682,7 @@ def test_send_msg_buy_notification(default_conf, mocker, caplog, message_type,
|
|||||||
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
||||||
|
|
||||||
assert msg_mock.call_args[0][0] == (
|
assert msg_mock.call_args[0][0] == (
|
||||||
f'\N{LARGE BLUE CIRCLE} *Binance:* {enter} ETH/BTC (#1)\n'
|
f'\N{LARGE BLUE CIRCLE} *Binance (dry):* {enter} ETH/BTC (#1)\n'
|
||||||
f'*Enter Tag:* `{enter_signal}`\n'
|
f'*Enter Tag:* `{enter_signal}`\n'
|
||||||
'*Amount:* `1333.33333333`\n'
|
'*Amount:* `1333.33333333`\n'
|
||||||
f'{leverage_text}'
|
f'{leverage_text}'
|
||||||
@ -1720,7 +1722,7 @@ def test_send_msg_buy_cancel_notification(default_conf, mocker, message_type, en
|
|||||||
'pair': 'ETH/BTC',
|
'pair': 'ETH/BTC',
|
||||||
'reason': CANCEL_REASON['TIMEOUT']
|
'reason': CANCEL_REASON['TIMEOUT']
|
||||||
})
|
})
|
||||||
assert (msg_mock.call_args[0][0] == '\N{WARNING SIGN} *Binance:* '
|
assert (msg_mock.call_args[0][0] == '\N{WARNING SIGN} *Binance (dry):* '
|
||||||
'Cancelling enter Order for ETH/BTC (#1). '
|
'Cancelling enter Order for ETH/BTC (#1). '
|
||||||
'Reason: cancelled due to timeout.')
|
'Reason: cancelled due to timeout.')
|
||||||
|
|
||||||
@ -1782,7 +1784,7 @@ def test_send_msg_entry_fill_notification(default_conf, mocker, message_type, en
|
|||||||
})
|
})
|
||||||
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage != 1.0 else ''
|
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage != 1.0 else ''
|
||||||
assert msg_mock.call_args[0][0] == (
|
assert msg_mock.call_args[0][0] == (
|
||||||
f'\N{CHECK MARK} *Binance:* {entered}ed ETH/BTC (#1)\n'
|
f'\N{CHECK MARK} *Binance (dry):* {entered}ed ETH/BTC (#1)\n'
|
||||||
f'*Enter Tag:* `{enter_signal}`\n'
|
f'*Enter Tag:* `{enter_signal}`\n'
|
||||||
'*Amount:* `1333.33333333`\n'
|
'*Amount:* `1333.33333333`\n'
|
||||||
f"{leverage_text}"
|
f"{leverage_text}"
|
||||||
@ -1820,7 +1822,7 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None:
|
|||||||
'close_date': arrow.utcnow(),
|
'close_date': arrow.utcnow(),
|
||||||
})
|
})
|
||||||
assert msg_mock.call_args[0][0] == (
|
assert msg_mock.call_args[0][0] == (
|
||||||
'\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n'
|
'\N{WARNING SIGN} *Binance (dry):* Exiting KEY/ETH (#1)\n'
|
||||||
'*Unrealized Profit:* `-57.41% (loss: -0.05746268 ETH / -24.812 USD)`\n'
|
'*Unrealized Profit:* `-57.41% (loss: -0.05746268 ETH / -24.812 USD)`\n'
|
||||||
'*Enter Tag:* `buy_signal1`\n'
|
'*Enter Tag:* `buy_signal1`\n'
|
||||||
'*Exit Reason:* `stop_loss`\n'
|
'*Exit Reason:* `stop_loss`\n'
|
||||||
@ -1854,7 +1856,7 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None:
|
|||||||
'close_date': arrow.utcnow(),
|
'close_date': arrow.utcnow(),
|
||||||
})
|
})
|
||||||
assert msg_mock.call_args[0][0] == (
|
assert msg_mock.call_args[0][0] == (
|
||||||
'\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n'
|
'\N{WARNING SIGN} *Binance (dry):* Exiting KEY/ETH (#1)\n'
|
||||||
'*Unrealized Profit:* `-57.41%`\n'
|
'*Unrealized Profit:* `-57.41%`\n'
|
||||||
'*Enter Tag:* `buy_signal1`\n'
|
'*Enter Tag:* `buy_signal1`\n'
|
||||||
'*Exit Reason:* `stop_loss`\n'
|
'*Exit Reason:* `stop_loss`\n'
|
||||||
@ -1883,10 +1885,12 @@ def test_send_msg_sell_cancel_notification(default_conf, mocker) -> None:
|
|||||||
'reason': 'Cancelled on exchange'
|
'reason': 'Cancelled on exchange'
|
||||||
})
|
})
|
||||||
assert msg_mock.call_args[0][0] == (
|
assert msg_mock.call_args[0][0] == (
|
||||||
'\N{WARNING SIGN} *Binance:* Cancelling exit Order for KEY/ETH (#1).'
|
'\N{WARNING SIGN} *Binance (dry):* Cancelling exit Order for KEY/ETH (#1).'
|
||||||
' Reason: Cancelled on exchange.')
|
' Reason: Cancelled on exchange.')
|
||||||
|
|
||||||
msg_mock.reset_mock()
|
msg_mock.reset_mock()
|
||||||
|
# Test with live mode (no dry appendix)
|
||||||
|
telegram._config['dry_run'] = False
|
||||||
telegram.send_msg({
|
telegram.send_msg({
|
||||||
'type': RPCMessageType.EXIT_CANCEL,
|
'type': RPCMessageType.EXIT_CANCEL,
|
||||||
'trade_id': 1,
|
'trade_id': 1,
|
||||||
@ -1935,7 +1939,7 @@ def test_send_msg_sell_fill_notification(default_conf, mocker, direction,
|
|||||||
|
|
||||||
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
||||||
assert msg_mock.call_args[0][0] == (
|
assert msg_mock.call_args[0][0] == (
|
||||||
'\N{WARNING SIGN} *Binance:* Exited KEY/ETH (#1)\n'
|
'\N{WARNING SIGN} *Binance (dry):* Exited KEY/ETH (#1)\n'
|
||||||
'*Profit:* `-57.41%`\n'
|
'*Profit:* `-57.41%`\n'
|
||||||
f'*Enter Tag:* `{enter_signal}`\n'
|
f'*Enter Tag:* `{enter_signal}`\n'
|
||||||
'*Exit Reason:* `stop_loss`\n'
|
'*Exit Reason:* `stop_loss`\n'
|
||||||
@ -1991,6 +1995,7 @@ def test_send_msg_unknown_type(default_conf, mocker) -> None:
|
|||||||
def test_send_msg_buy_notification_no_fiat(
|
def test_send_msg_buy_notification_no_fiat(
|
||||||
default_conf, mocker, message_type, enter, enter_signal, leverage) -> None:
|
default_conf, mocker, message_type, enter, enter_signal, leverage) -> None:
|
||||||
del default_conf['fiat_display_currency']
|
del default_conf['fiat_display_currency']
|
||||||
|
default_conf['dry_run'] = False
|
||||||
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
||||||
|
|
||||||
telegram.send_msg({
|
telegram.send_msg({
|
||||||
@ -2060,7 +2065,7 @@ def test_send_msg_sell_notification_no_fiat(
|
|||||||
|
|
||||||
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
||||||
assert msg_mock.call_args[0][0] == (
|
assert msg_mock.call_args[0][0] == (
|
||||||
'\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n'
|
'\N{WARNING SIGN} *Binance (dry):* Exiting KEY/ETH (#1)\n'
|
||||||
'*Unrealized Profit:* `-57.41%`\n'
|
'*Unrealized Profit:* `-57.41%`\n'
|
||||||
f'*Enter Tag:* `{enter_signal}`\n'
|
f'*Enter Tag:* `{enter_signal}`\n'
|
||||||
'*Exit Reason:* `stop_loss`\n'
|
'*Exit Reason:* `stop_loss`\n'
|
||||||
|
175
tests/strategy/strats/strategy_test_v3_analysis.py
Normal file
175
tests/strategy/strats/strategy_test_v3_analysis.py
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
|
||||||
|
|
||||||
|
import talib.abstract as ta
|
||||||
|
from pandas import DataFrame
|
||||||
|
|
||||||
|
import freqtrade.vendor.qtpylib.indicators as qtpylib
|
||||||
|
from freqtrade.strategy import (BooleanParameter, DecimalParameter, IntParameter, IStrategy,
|
||||||
|
RealParameter)
|
||||||
|
|
||||||
|
|
||||||
|
class StrategyTestV3Analysis(IStrategy):
|
||||||
|
"""
|
||||||
|
Strategy used by tests freqtrade bot.
|
||||||
|
Please do not modify this strategy, it's intended for internal use only.
|
||||||
|
Please look at the SampleStrategy in the user_data/strategy directory
|
||||||
|
or strategy repository https://github.com/freqtrade/freqtrade-strategies
|
||||||
|
for samples and inspiration.
|
||||||
|
"""
|
||||||
|
INTERFACE_VERSION = 3
|
||||||
|
|
||||||
|
# Minimal ROI designed for the strategy
|
||||||
|
minimal_roi = {
|
||||||
|
"40": 0.0,
|
||||||
|
"30": 0.01,
|
||||||
|
"20": 0.02,
|
||||||
|
"0": 0.04
|
||||||
|
}
|
||||||
|
|
||||||
|
# Optimal stoploss designed for the strategy
|
||||||
|
stoploss = -0.10
|
||||||
|
|
||||||
|
# Optimal timeframe for the strategy
|
||||||
|
timeframe = '5m'
|
||||||
|
|
||||||
|
# Optional order type mapping
|
||||||
|
order_types = {
|
||||||
|
'entry': 'limit',
|
||||||
|
'exit': 'limit',
|
||||||
|
'stoploss': 'limit',
|
||||||
|
'stoploss_on_exchange': False
|
||||||
|
}
|
||||||
|
|
||||||
|
# Number of candles the strategy requires before producing valid signals
|
||||||
|
startup_candle_count: int = 20
|
||||||
|
|
||||||
|
# Optional time in force for orders
|
||||||
|
order_time_in_force = {
|
||||||
|
'entry': 'gtc',
|
||||||
|
'exit': 'gtc',
|
||||||
|
}
|
||||||
|
|
||||||
|
buy_params = {
|
||||||
|
'buy_rsi': 35,
|
||||||
|
# Intentionally not specified, so "default" is tested
|
||||||
|
# 'buy_plusdi': 0.4
|
||||||
|
}
|
||||||
|
|
||||||
|
sell_params = {
|
||||||
|
'sell_rsi': 74,
|
||||||
|
'sell_minusdi': 0.4
|
||||||
|
}
|
||||||
|
|
||||||
|
buy_rsi = IntParameter([0, 50], default=30, space='buy')
|
||||||
|
buy_plusdi = RealParameter(low=0, high=1, default=0.5, space='buy')
|
||||||
|
sell_rsi = IntParameter(low=50, high=100, default=70, space='sell')
|
||||||
|
sell_minusdi = DecimalParameter(low=0, high=1, default=0.5001, decimals=3, space='sell',
|
||||||
|
load=False)
|
||||||
|
protection_enabled = BooleanParameter(default=True)
|
||||||
|
protection_cooldown_lookback = IntParameter([0, 50], default=30)
|
||||||
|
|
||||||
|
# TODO: Can this work with protection tests? (replace HyperoptableStrategy implicitly ... )
|
||||||
|
# @property
|
||||||
|
# def protections(self):
|
||||||
|
# prot = []
|
||||||
|
# if self.protection_enabled.value:
|
||||||
|
# prot.append({
|
||||||
|
# "method": "CooldownPeriod",
|
||||||
|
# "stop_duration_candles": self.protection_cooldown_lookback.value
|
||||||
|
# })
|
||||||
|
# return prot
|
||||||
|
|
||||||
|
bot_started = False
|
||||||
|
|
||||||
|
def bot_start(self):
|
||||||
|
self.bot_started = True
|
||||||
|
|
||||||
|
def informative_pairs(self):
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||||
|
|
||||||
|
# Momentum Indicator
|
||||||
|
# ------------------------------------
|
||||||
|
|
||||||
|
# ADX
|
||||||
|
dataframe['adx'] = ta.ADX(dataframe)
|
||||||
|
|
||||||
|
# MACD
|
||||||
|
macd = ta.MACD(dataframe)
|
||||||
|
dataframe['macd'] = macd['macd']
|
||||||
|
dataframe['macdsignal'] = macd['macdsignal']
|
||||||
|
dataframe['macdhist'] = macd['macdhist']
|
||||||
|
|
||||||
|
# Minus Directional Indicator / Movement
|
||||||
|
dataframe['minus_di'] = ta.MINUS_DI(dataframe)
|
||||||
|
|
||||||
|
# Plus Directional Indicator / Movement
|
||||||
|
dataframe['plus_di'] = ta.PLUS_DI(dataframe)
|
||||||
|
|
||||||
|
# RSI
|
||||||
|
dataframe['rsi'] = ta.RSI(dataframe)
|
||||||
|
|
||||||
|
# Stoch fast
|
||||||
|
stoch_fast = ta.STOCHF(dataframe)
|
||||||
|
dataframe['fastd'] = stoch_fast['fastd']
|
||||||
|
dataframe['fastk'] = stoch_fast['fastk']
|
||||||
|
|
||||||
|
# Bollinger bands
|
||||||
|
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
|
||||||
|
dataframe['bb_lowerband'] = bollinger['lower']
|
||||||
|
dataframe['bb_middleband'] = bollinger['mid']
|
||||||
|
dataframe['bb_upperband'] = bollinger['upper']
|
||||||
|
|
||||||
|
# EMA - Exponential Moving Average
|
||||||
|
dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
|
||||||
|
|
||||||
|
return dataframe
|
||||||
|
|
||||||
|
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||||
|
|
||||||
|
dataframe.loc[
|
||||||
|
(
|
||||||
|
(dataframe['rsi'] < self.buy_rsi.value) &
|
||||||
|
(dataframe['fastd'] < 35) &
|
||||||
|
(dataframe['adx'] > 30) &
|
||||||
|
(dataframe['plus_di'] > self.buy_plusdi.value)
|
||||||
|
) |
|
||||||
|
(
|
||||||
|
(dataframe['adx'] > 65) &
|
||||||
|
(dataframe['plus_di'] > self.buy_plusdi.value)
|
||||||
|
),
|
||||||
|
['enter_long', 'enter_tag']] = 1, 'enter_tag_long'
|
||||||
|
|
||||||
|
dataframe.loc[
|
||||||
|
(
|
||||||
|
qtpylib.crossed_below(dataframe['rsi'], self.sell_rsi.value)
|
||||||
|
),
|
||||||
|
['enter_short', 'enter_tag']] = 1, 'enter_tag_short'
|
||||||
|
|
||||||
|
return dataframe
|
||||||
|
|
||||||
|
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||||
|
dataframe.loc[
|
||||||
|
(
|
||||||
|
(
|
||||||
|
(qtpylib.crossed_above(dataframe['rsi'], self.sell_rsi.value)) |
|
||||||
|
(qtpylib.crossed_above(dataframe['fastd'], 70))
|
||||||
|
) &
|
||||||
|
(dataframe['adx'] > 10) &
|
||||||
|
(dataframe['minus_di'] > 0)
|
||||||
|
) |
|
||||||
|
(
|
||||||
|
(dataframe['adx'] > 70) &
|
||||||
|
(dataframe['minus_di'] > self.sell_minusdi.value)
|
||||||
|
),
|
||||||
|
['exit_long', 'exit_tag']] = 1, 'exit_tag_long'
|
||||||
|
|
||||||
|
dataframe.loc[
|
||||||
|
(
|
||||||
|
qtpylib.crossed_above(dataframe['rsi'], self.buy_rsi.value)
|
||||||
|
),
|
||||||
|
['exit_long', 'exit_tag']] = 1, 'exit_tag_short'
|
||||||
|
|
||||||
|
return dataframe
|
@ -34,7 +34,7 @@ def test_search_all_strategies_no_failed():
|
|||||||
directory = Path(__file__).parent / "strats"
|
directory = Path(__file__).parent / "strats"
|
||||||
strategies = StrategyResolver.search_all_objects(directory, enum_failed=False)
|
strategies = StrategyResolver.search_all_objects(directory, enum_failed=False)
|
||||||
assert isinstance(strategies, list)
|
assert isinstance(strategies, list)
|
||||||
assert len(strategies) == 5
|
assert len(strategies) == 6
|
||||||
assert isinstance(strategies[0], dict)
|
assert isinstance(strategies[0], dict)
|
||||||
|
|
||||||
|
|
||||||
@ -42,10 +42,10 @@ def test_search_all_strategies_with_failed():
|
|||||||
directory = Path(__file__).parent / "strats"
|
directory = Path(__file__).parent / "strats"
|
||||||
strategies = StrategyResolver.search_all_objects(directory, enum_failed=True)
|
strategies = StrategyResolver.search_all_objects(directory, enum_failed=True)
|
||||||
assert isinstance(strategies, list)
|
assert isinstance(strategies, list)
|
||||||
assert len(strategies) == 6
|
assert len(strategies) == 7
|
||||||
# with enum_failed=True search_all_objects() shall find 2 good strategies
|
# with enum_failed=True search_all_objects() shall find 2 good strategies
|
||||||
# and 1 which fails to load
|
# and 1 which fails to load
|
||||||
assert len([x for x in strategies if x['class'] is not None]) == 5
|
assert len([x for x in strategies if x['class'] is not None]) == 6
|
||||||
assert len([x for x in strategies if x['class'] is None]) == 1
|
assert len([x for x in strategies if x['class'] is None]) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@ -2151,7 +2151,7 @@ def test_handle_trade(
|
|||||||
|
|
||||||
assert trade.close_rate == 2.0 if is_short else 2.2
|
assert trade.close_rate == 2.0 if is_short else 2.2
|
||||||
assert trade.close_profit == close_profit
|
assert trade.close_profit == close_profit
|
||||||
assert trade.calc_profit() == 5.685
|
assert trade.calc_profit(trade.close_rate) == 5.685
|
||||||
assert trade.close_date is not None
|
assert trade.close_date is not None
|
||||||
assert trade.exit_reason == 'sell_signal1'
|
assert trade.exit_reason == 'sell_signal1'
|
||||||
|
|
||||||
|
@ -606,9 +606,9 @@ def test_calc_open_close_trade_price(
|
|||||||
trade.close_rate = 2.2
|
trade.close_rate = 2.2
|
||||||
trade.recalc_open_trade_value()
|
trade.recalc_open_trade_value()
|
||||||
assert isclose(trade._calc_open_trade_value(), open_value)
|
assert isclose(trade._calc_open_trade_value(), open_value)
|
||||||
assert isclose(trade.calc_close_trade_value(), close_value)
|
assert isclose(trade.calc_close_trade_value(trade.close_rate), close_value)
|
||||||
assert isclose(trade.calc_profit(), round(profit, 8))
|
assert isclose(trade.calc_profit(trade.close_rate), round(profit, 8))
|
||||||
assert pytest.approx(trade.calc_profit_ratio()) == profit_ratio
|
assert pytest.approx(trade.calc_profit_ratio(trade.close_rate)) == profit_ratio
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("init_persistence")
|
@pytest.mark.usefixtures("init_persistence")
|
||||||
@ -660,7 +660,7 @@ def test_calc_close_trade_price_exception(limit_buy_order_usdt, fee):
|
|||||||
trade.open_order_id = 'something'
|
trade.open_order_id = 'something'
|
||||||
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
|
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
|
||||||
trade.update_trade(oobj)
|
trade.update_trade(oobj)
|
||||||
assert trade.calc_close_trade_value() == 0.0
|
assert trade.calc_close_trade_value(trade.close_rate) == 0.0
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("init_persistence")
|
@pytest.mark.usefixtures("init_persistence")
|
||||||
@ -813,7 +813,7 @@ def test_calc_close_trade_price(
|
|||||||
funding_fees=funding_fees
|
funding_fees=funding_fees
|
||||||
)
|
)
|
||||||
trade.open_order_id = 'close_trade'
|
trade.open_order_id = 'close_trade'
|
||||||
assert round(trade.calc_close_trade_value(rate=close_rate, fee=fee_rate), 8) == result
|
assert round(trade.calc_close_trade_value(rate=close_rate), 8) == result
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -884,6 +884,17 @@ def test_calc_close_trade_price(
|
|||||||
('binance', False, 3, 2.2, 0.0025, 4.684999, 0.23366583, futures, -1),
|
('binance', False, 3, 2.2, 0.0025, 4.684999, 0.23366583, futures, -1),
|
||||||
('binance', True, 1, 2.2, 0.0025, -7.315, -0.12222222, futures, -1),
|
('binance', True, 1, 2.2, 0.0025, -7.315, -0.12222222, futures, -1),
|
||||||
('binance', True, 3, 2.2, 0.0025, -7.315, -0.36666666, futures, -1),
|
('binance', True, 3, 2.2, 0.0025, -7.315, -0.36666666, futures, -1),
|
||||||
|
|
||||||
|
# FUTURES, funding_fee=0
|
||||||
|
('binance', False, 1, 2.1, 0.0025, 2.6925, 0.04476309, futures, 0),
|
||||||
|
('binance', False, 3, 2.1, 0.0025, 2.6925, 0.13428928, futures, 0),
|
||||||
|
('binance', True, 1, 2.1, 0.0025, -3.3074999, -0.05526316, futures, 0),
|
||||||
|
('binance', True, 3, 2.1, 0.0025, -3.3074999, -0.16578947, futures, 0),
|
||||||
|
|
||||||
|
('binance', False, 1, 1.9, 0.0025, -3.2925, -0.05473815, futures, 0),
|
||||||
|
('binance', False, 3, 1.9, 0.0025, -3.2925, -0.16421446, futures, 0),
|
||||||
|
('binance', True, 1, 1.9, 0.0025, 2.7075, 0.0452381, futures, 0),
|
||||||
|
('binance', True, 3, 1.9, 0.0025, 2.7075, 0.13571429, futures, 0),
|
||||||
])
|
])
|
||||||
@pytest.mark.usefixtures("init_persistence")
|
@pytest.mark.usefixtures("init_persistence")
|
||||||
def test_calc_profit(
|
def test_calc_profit(
|
||||||
@ -2258,6 +2269,7 @@ def test_Trade_object_idem():
|
|||||||
'get_exit_reason_performance',
|
'get_exit_reason_performance',
|
||||||
'get_enter_tag_performance',
|
'get_enter_tag_performance',
|
||||||
'get_mix_tag_performance',
|
'get_mix_tag_performance',
|
||||||
|
'get_trading_volume',
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user