stable/docs/plotting.md

380 lines
15 KiB
Markdown
Raw Normal View History

2018-01-12 10:49:50 +00:00
# Plotting
2019-05-27 17:42:12 +00:00
This page explains how to plot prices, indicators and profits.
2018-01-12 10:49:50 +00:00
2019-08-24 13:09:33 +00:00
## Installation / Setup
2018-01-28 10:53:52 +00:00
2019-08-24 13:09:33 +00:00
Plotting modules use the Plotly library. You can install / upgrade this by running the following command:
2018-01-28 10:53:52 +00:00
2019-05-27 17:42:12 +00:00
``` bash
pip install -U -r requirements-plot.txt
2018-01-28 10:53:52 +00:00
```
2018-01-12 10:49:50 +00:00
## Plot price and indicators
2019-05-27 17:42:12 +00:00
2019-08-30 18:42:14 +00:00
The `freqtrade plot-dataframe` subcommand shows an interactive graph with three subplots:
2019-08-24 13:09:33 +00:00
* Main plot with candlestics and indicators following price (sma/ema)
* Volume bars
* Additional indicators as specified by `--indicators2`
![plot-dataframe](assets/plot-dataframe.png)
Possible arguments:
2019-08-22 15:16:33 +00:00
```
2020-03-14 22:55:13 +00:00
usage: freqtrade plot-dataframe [-h] [-v] [--logfile FILE] [-V] [-c PATH]
[-d PATH] [--userdir PATH] [-s NAME]
[--strategy-path PATH] [-p PAIRS [PAIRS ...]]
[--indicators1 INDICATORS1 [INDICATORS1 ...]]
[--indicators2 INDICATORS2 [INDICATORS2 ...]]
[--plot-limit INT] [--db-url PATH]
[--trade-source {DB,file}] [--export EXPORT]
[--export-filename PATH]
[--timerange TIMERANGE] [-i TIMEFRAME]
[--no-trades]
2019-08-22 15:16:33 +00:00
optional arguments:
-h, --help show this help message and exit
-p PAIRS [PAIRS ...], --pairs PAIRS [PAIRS ...]
2021-04-17 08:53:03 +00:00
Limit command to these pairs. Pairs are space-
2020-03-14 22:55:13 +00:00
separated.
2019-08-22 15:16:33 +00:00
--indicators1 INDICATORS1 [INDICATORS1 ...]
2020-03-14 22:55:13 +00:00
Set indicators from your strategy you want in the
first row of the graph. Space-separated list. Example:
2019-08-22 15:16:33 +00:00
`ema3 ema5`. Default: `['sma', 'ema3', 'ema5']`.
--indicators2 INDICATORS2 [INDICATORS2 ...]
2020-03-14 22:55:13 +00:00
Set indicators from your strategy you want in the
third row of the graph. Space-separated list. Example:
2019-08-22 15:16:33 +00:00
`fastd fastk`. Default: `['macd', 'macdsignal']`.
2020-03-14 22:55:13 +00:00
--plot-limit INT Specify tick limit for plotting. Notice: too high
values cause huge files. Default: 750.
--db-url PATH Override trades database URL, this is useful in custom
deployments (default: `sqlite:///tradesv3.sqlite` for
Live Run mode, `sqlite:///tradesv3.dryrun.sqlite` for
Dry Run).
2019-08-22 15:16:33 +00:00
--trade-source {DB,file}
2020-03-14 22:55:13 +00:00
Specify the source for trades (Can be DB or file
(backtest file)) Default: file
--export EXPORT Export backtest results, argument are: trades.
Example: `--export=trades`
2019-08-22 15:16:33 +00:00
--export-filename PATH
2020-03-14 22:55:13 +00:00
Save backtest results to the file with this filename.
Requires `--export` to be set as well. Example:
`--export-filename=user_data/backtest_results/backtest
_today.json`
2019-08-22 15:16:33 +00:00
--timerange TIMERANGE
Specify what timerange of data to use.
-i TIMEFRAME, --timeframe TIMEFRAME, --ticker-interval TIMEFRAME
2021-04-03 14:54:47 +00:00
Specify timeframe (`1m`, `5m`, `30m`, `1h`, `1d`).
2020-03-15 20:20:32 +00:00
--no-trades Skip using trades from backtesting file and DB.
Common arguments:
-v, --verbose Verbose mode (-vv for more, -vvv to get all messages).
2020-03-14 22:55:13 +00:00
--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
2020-03-14 22:55:13 +00:00
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.
Strategy arguments:
-s NAME, --strategy NAME
2020-03-14 22:55:13 +00:00
Specify strategy class name which will be used by the
bot.
--strategy-path PATH Specify additional strategy lookup path.
2021-04-17 08:53:03 +00:00
2018-01-28 10:53:52 +00:00
```
2018-01-12 10:49:50 +00:00
2019-08-24 13:09:33 +00:00
Example:
2019-05-27 17:42:12 +00:00
``` bash
2019-08-22 15:16:33 +00:00
freqtrade plot-dataframe -p BTC/ETH
2018-01-12 10:49:50 +00:00
```
2019-08-30 18:42:14 +00:00
The `-p/--pairs` argument can be used to specify pairs you would like to plot.
2019-08-22 15:16:33 +00:00
!!! Note
2019-08-30 18:42:14 +00:00
The `freqtrade plot-dataframe` subcommand generates one plot-file per pair.
Specify custom indicators.
Use `--indicators1` for the main plot and `--indicators2` for the subplot below (if values are in a different range than prices).
!!! Tip
2019-08-31 13:31:47 +00:00
You will almost certainly want to specify a custom strategy! This can be done by adding `-s Classname` / `--strategy ClassName` to the command.
2019-08-24 13:09:33 +00:00
``` bash
freqtrade plot-dataframe --strategy AwesomeStrategy -p BTC/ETH --indicators1 sma ema --indicators2 macd
```
2018-01-12 10:49:50 +00:00
2019-08-24 13:09:33 +00:00
### Further usage examples
2019-08-24 13:09:33 +00:00
To plot multiple pairs, separate them with a space:
2019-05-27 17:42:12 +00:00
``` bash
freqtrade plot-dataframe --strategy AwesomeStrategy -p BTC/ETH XRP/ETH
```
2019-08-24 13:09:33 +00:00
To plot a timerange (to zoom in)
2019-05-27 17:42:12 +00:00
``` bash
freqtrade plot-dataframe --strategy AwesomeStrategy -p BTC/ETH --timerange=20180801-20180805
```
2019-05-27 17:42:12 +00:00
2019-08-24 13:09:33 +00:00
To plot trades stored in a database use `--db-url` in combination with `--trade-source DB`:
2019-05-27 17:42:12 +00:00
``` bash
freqtrade plot-dataframe --strategy AwesomeStrategy --db-url sqlite:///tradesv3.dry_run.sqlite -p BTC/ETH --trade-source DB
```
2019-05-27 17:42:12 +00:00
To plot trades from a backtesting result, use `--export-filename <filename>`
2019-05-27 17:42:12 +00:00
``` bash
freqtrade plot-dataframe --strategy AwesomeStrategy --export-filename user_data/backtest_results/backtest-result.json -p BTC/ETH
```
2020-01-06 11:55:12 +00:00
### Plot dataframe basics
2020-01-07 06:16:31 +00:00
![plot-dataframe2](assets/plot-dataframe2.png)
2020-01-06 11:55:12 +00:00
2020-01-13 03:36:05 +00:00
The `plot-dataframe` subcommand requires backtesting data, a strategy and either a backtesting-results file or a database, containing trades corresponding to the strategy.
2020-01-06 11:55:12 +00:00
The resulting plot will have the following elements:
2020-01-13 03:31:15 +00:00
* Green triangles: Buy signals from the strategy. (Note: not every buy signal generates a trade, compare to cyan circles.)
* Red triangles: Sell signals from the strategy. (Also, not every sell signal terminates a trade, compare to red and green squares.)
* Cyan circles: Trade entry points.
* Red squares: Trade exit points for trades with loss or 0% profit.
* Green squares: Trade exit points for profitable trades.
* Indicators with values corresponding to the candle scale (e.g. SMA/EMA), as specified with `--indicators1`.
* Volume (bar chart at the bottom of the main chart).
* Indicators with values in different scales (e.g. MACD, RSI) below the volume bars, as specified with `--indicators2`.
2020-01-06 11:55:12 +00:00
2020-01-06 11:59:17 +00:00
!!! Note "Bollinger Bands"
2020-01-13 03:31:15 +00:00
Bollinger bands are automatically added to the plot if the columns `bb_lowerband` and `bb_upperband` exist, and are painted as a light blue area spanning from the lower band to the upper band.
2020-01-06 11:59:17 +00:00
2020-01-13 03:31:15 +00:00
#### Advanced plot configuration
2020-01-06 11:55:12 +00:00
2020-01-13 03:31:15 +00:00
An advanced plot configuration can be specified in the strategy in the `plot_config` parameter.
2020-01-06 11:55:12 +00:00
Additional features when using `plot_config` include:
2020-01-06 11:55:12 +00:00
* Specify colors per indicator
* Specify additional subplots
* Specify indicator pairs to fill area in between
2020-01-06 11:55:12 +00:00
The sample plot configuration below specifies fixed colors for the indicators. Otherwise, consecutive plots may produce different color schemes each time, making comparisons difficult.
2020-01-13 03:31:15 +00:00
It also allows multiple subplots to display both MACD and RSI at the same time.
Plot type can be configured using `type` key. Possible types are:
* `scatter` corresponding to `plotly.graph_objects.Scatter` class (default).
* `bar` corresponding to `plotly.graph_objects.Bar` class.
Extra parameters to `plotly.graph_objects.*` constructor can be specified in `plotly` dict.
2020-01-06 11:55:12 +00:00
Sample configuration with inline comments explaining the process:
``` python
@property
def plot_config(self):
"""
There are a lot of solutions how to build the return dictionary.
The only important point is the return value.
Example:
plot_config = {'main_plot': {}, 'subplots': {}}
"""
plot_config = {}
plot_config['main_plot'] = {
# Configuration for main plot indicators.
# Assumes 2 parameters, emashort and emalong to be specified.
f'ema_{self.emashort.value}': {'color': 'red'},
f'ema_{self.emalong.value}': {'color': '#CCCCCC'},
# By omitting color, a random color is selected.
'sar': {},
# fill area between senkou_a and senkou_b
'senkou_a': {
'color': 'green', #optional
'fill_to': 'senkou_b',
'fill_label': 'Ichimoku Cloud', #optional
'fill_color': 'rgba(255,76,46,0.2)', #optional
2020-01-06 11:55:12 +00:00
},
# plot senkou_b, too. Not only the area to it.
'senkou_b': {}
}
plot_config['subplots'] = {
# Create subplot MACD
"MACD": {
'macd': {'color': 'blue', 'fill_to': 'macdhist'},
'macdsignal': {'color': 'orange'},
'macdhist': {'type': 'bar', 'plotly': {'opacity': 0.9}}
},
# Additional subplot RSI
"RSI": {
'rsi': {'color': 'red'}
}
2020-01-06 11:55:12 +00:00
}
return plot_config
```
??? Note "As attribute (former method)"
Assigning plot_config is also possible as Attribute (this used to be the default way).
This has the disadvantage that strategy parameters are not available, preventing certain configurations from working.
``` python
plot_config = {
'main_plot': {
# Configuration for main plot indicators.
# Specifies `ema10` to be red, and `ema50` to be a shade of gray
'ema10': {'color': 'red'},
'ema50': {'color': '#CCCCCC'},
# By omitting color, a random color is selected.
'sar': {},
# fill area between senkou_a and senkou_b
'senkou_a': {
'color': 'green', #optional
'fill_to': 'senkou_b',
'fill_label': 'Ichimoku Cloud', #optional
'fill_color': 'rgba(255,76,46,0.2)', #optional
},
# plot senkou_b, too. Not only the area to it.
'senkou_b': {}
},
'subplots': {
# Create subplot MACD
"MACD": {
'macd': {'color': 'blue', 'fill_to': 'macdhist'},
'macdsignal': {'color': 'orange'},
'macdhist': {'type': 'bar', 'plotly': {'opacity': 0.9}}
},
# Additional subplot RSI
"RSI": {
'rsi': {'color': 'red'}
}
}
}
```
2020-01-06 11:55:12 +00:00
!!! Note
2020-12-19 19:47:25 +00:00
The above configuration assumes that `ema10`, `ema50`, `senkou_a`, `senkou_b`,
`macd`, `macdsignal`, `macdhist` and `rsi` are columns in the DataFrame created by the strategy.
2020-01-06 11:55:12 +00:00
!!! Warning
`plotly` arguments are only supported with plotly library and will not work with freq-ui.
2021-12-26 14:34:37 +00:00
!!! Note "Trade position adjustments"
If `position_adjustment_enable` / `adjust_trade_position()` is used, the trade initial buy price is averaged over multiple orders and the trade start price will most likely appear outside the candle range.
2018-01-12 09:55:49 +00:00
## Plot profit
2019-08-24 13:09:33 +00:00
![plot-profit](assets/plot-profit.png)
2020-01-13 03:36:05 +00:00
The `plot-profit` subcommand shows an interactive graph with three plots:
2019-05-27 17:42:12 +00:00
2020-01-13 03:36:05 +00:00
* Average closing price for all pairs.
* The summarized profit made by backtesting.
Note that this is not the real-world profit, but more of an estimate.
* Profit for each individual pair.
* Parallelism of trades.
2022-01-01 15:40:18 +00:00
* Underwater (Periods of drawdown).
2018-01-12 09:55:49 +00:00
2019-08-22 15:16:33 +00:00
The first graph is good to get a grip of how the overall market progresses.
2018-01-12 09:55:49 +00:00
2019-08-24 13:09:33 +00:00
The second graph will show if your algorithm works or doesn't.
Perhaps you want an algorithm that steadily makes small profits, or one that acts less often, but makes big swings.
2020-03-03 19:18:38 +00:00
This graph will also highlight the start (and end) of the Max drawdown period.
2018-01-12 09:55:49 +00:00
2019-08-24 13:09:33 +00:00
The third graph can be useful to spot outliers, events in pairs that cause profit spikes.
2018-01-12 09:55:49 +00:00
The forth graph can help you analyze trade parallelism, showing how often max_open_trades have been maxed out.
2019-08-30 18:42:14 +00:00
Possible options for the `freqtrade plot-profit` subcommand:
2018-01-28 10:53:52 +00:00
2019-08-22 15:16:33 +00:00
```
usage: freqtrade plot-profit [-h] [-v] [--logfile FILE] [-V] [-c PATH]
[-d PATH] [--userdir PATH] [-s NAME]
[--strategy-path PATH] [-p PAIRS [PAIRS ...]]
2019-08-22 15:16:33 +00:00
[--timerange TIMERANGE] [--export EXPORT]
[--export-filename PATH] [--db-url PATH]
[--trade-source {DB,file}] [-i TIMEFRAME]
2019-08-22 15:16:33 +00:00
optional arguments:
-h, --help show this help message and exit
-p PAIRS [PAIRS ...], --pairs PAIRS [PAIRS ...]
2021-04-17 08:53:03 +00:00
Limit command to these pairs. Pairs are space-
2019-08-22 15:16:33 +00:00
separated.
--timerange TIMERANGE
Specify what timerange of data to use.
--export EXPORT Export backtest results, argument are: trades.
Example: `--export=trades`
--export-filename PATH
Save backtest results to the file with this filename.
Requires `--export` to be set as well. Example:
`--export-filename=user_data/backtest_results/backtest
_today.json`
2019-08-22 15:16:33 +00:00
--db-url PATH Override trades database URL, this is useful in custom
deployments (default: `sqlite:///tradesv3.sqlite` for
Live Run mode, `sqlite:///tradesv3.dryrun.sqlite` for
Dry Run).
2019-08-22 15:16:33 +00:00
--trade-source {DB,file}
Specify the source for trades (Can be DB or file
(backtest file)) Default: file
-i TIMEFRAME, --timeframe TIMEFRAME, --ticker-interval TIMEFRAME
2021-04-03 14:54:47 +00:00
Specify timeframe (`1m`, `5m`, `30m`, `1h`, `1d`).
--auto-open Automatically open generated plot.
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.
2019-08-22 15:16:33 +00:00
Strategy arguments:
-s NAME, --strategy NAME
Specify strategy class name which will be used by the
bot.
--strategy-path PATH Specify additional strategy lookup path.
2021-04-17 08:53:03 +00:00
2018-01-28 10:53:52 +00:00
```
2018-01-12 09:55:49 +00:00
2019-08-30 18:42:14 +00:00
The `-p/--pairs` argument, can be used to limit the pairs that are considered for this calculation.
2018-01-12 09:55:49 +00:00
2019-08-24 13:09:33 +00:00
Examples:
Use custom backtest-export file
``` bash
freqtrade plot-profit -p LTC/BTC --export-filename user_data/backtest_results/backtest-result.json
2019-08-24 13:09:33 +00:00
```
Use custom database
``` bash
freqtrade plot-profit -p LTC/BTC --db-url sqlite:///tradesv3.sqlite --trade-source DB
```
2019-05-27 17:42:12 +00:00
``` bash
2019-10-05 07:56:01 +00:00
freqtrade --datadir user_data/data/binance_save/ plot-profit -p LTC/BTC
2018-01-12 09:55:49 +00:00
```