Repository: freqtrade/freqtrade

Issue:
Plot-scripts require --datadir
## Describe your environment

  * Python Version: 3.7
  * Branch: Develop
  * Last Commit ID:  962d487edb0d28f95d6395c09189a333c436fd20
  
## Describe the problem:

Currently, `freqtrade plot-dataframe` does require either a valid configuration (`--config` or `config.json` in cwd - or `--datadir user_data/data/bittrex` to find the backtest data.
This is because without one of these, the exchange is not known, which is a requirement to find the data in the datadir.

## Possible fixes

* Error and point out that one of the 2 conditions have to be met
* add `--exchange` parameter as alternative (including the above)

* ... other ideas?


Discussion:
My idea was (is) to make behavior of all commands same -- if `-c` is not specified in cli, then the default config is used. The command will take the exchange name from the config and make anything it needs with it -- in the case it will form the path to the data-dir.

If default config is not loaded for some commands (`plot-*` in this case) as now, it's not `--data-dir` which is needed. It's actually `--exchange`. With the same behavior as mentioned above -- the command will use it to make the path to the data. The `--data-dir` is only needed if it points to another location (can also be specified, but not required).


Resolving patch:
diff --git a/freqtrade/configuration/arguments.py b/freqtrade/configuration/arguments.py
--- a/freqtrade/configuration/arguments.py
+++ b/freqtrade/configuration/arguments.py
@@ -3,6 +3,7 @@
 """
 import argparse
 from typing import List, Optional
+from pathlib import Path
 
 from freqtrade.configuration.cli_options import AVAILABLE_CLI_OPTIONS
 from freqtrade import constants
@@ -73,11 +74,13 @@ def _parse_args(self) -> argparse.Namespace:
         """
         parsed_arg = self.parser.parse_args(self.args)
 
+        # When no config is provided, but a config exists, use that configuration!
+
         # Workaround issue in argparse with action='append' and default value
         # (see https://bugs.python.org/issue16399)
         # Allow no-config for certain commands (like downloading / plotting)
-        if (parsed_arg.config is None
-                and not ('subparser' in parsed_arg and parsed_arg.subparser in NO_CONF_REQURIED)):
+        if (parsed_arg.config is None and ((Path.cwd() / constants.DEFAULT_CONFIG).is_file() or
+           not ('subparser' in parsed_arg and parsed_arg.subparser in NO_CONF_REQURIED))):
             parsed_arg.config = [constants.DEFAULT_CONFIG]
 
         return parsed_arg
diff --git a/freqtrade/plot/plot_utils.py b/freqtrade/plot/plot_utils.py
--- a/freqtrade/plot/plot_utils.py
+++ b/freqtrade/plot/plot_utils.py
@@ -1,15 +1,24 @@
 from argparse import Namespace
-
+from freqtrade import OperationalException
 from freqtrade.state import RunMode
 from freqtrade.utils import setup_utils_configuration
 
 
+def validate_plot_args(args: Namespace):
+    args_tmp = vars(args)
+    if not args_tmp.get('datadir') and not args_tmp.get('config'):
+        raise OperationalException(
+            "You need to specify either `--datadir` or `--config` "
+            "for plot-profit and plot-dataframe.")
+
+
 def start_plot_dataframe(args: Namespace) -> None:
     """
     Entrypoint for dataframe plotting
     """
     # Import here to avoid errors if plot-dependencies are not installed.
     from freqtrade.plot.plotting import analyse_and_plot_pairs
+    validate_plot_args(args)
     config = setup_utils_configuration(args, RunMode.PLOT)
 
     analyse_and_plot_pairs(config)
@@ -21,6 +30,7 @@ def start_plot_profit(args: Namespace) -> None:
     """
     # Import here to avoid errors if plot-dependencies are not installed.
     from freqtrade.plot.plotting import plot_profit
+    validate_plot_args(args)
     config = setup_utils_configuration(args, RunMode.PLOT)
 
     plot_profit(config)


Test patch:
diff --git a/freqtrade/tests/test_configuration.py b/freqtrade/tests/test_configuration.py
--- a/freqtrade/tests/test_configuration.py
+++ b/freqtrade/tests/test_configuration.py
@@ -871,6 +871,8 @@ def test_pairlist_resolving_fallback(mocker):
     ]
 
     args = Arguments(arglist).get_parsed_arg()
+    # Fix flaky tests if config.json exists
+    args.config = None
 
     configuration = Configuration(args)
     config = configuration.get_config()
diff --git a/freqtrade/tests/test_plotting.py b/freqtrade/tests/test_plotting.py
--- a/freqtrade/tests/test_plotting.py
+++ b/freqtrade/tests/test_plotting.py
@@ -4,8 +4,10 @@
 from unittest.mock import MagicMock
 
 import plotly.graph_objects as go
+import pytest
 from plotly.subplots import make_subplots
 
+from freqtrade import OperationalException
 from freqtrade.configuration import TimeRange
 from freqtrade.data import history
 from freqtrade.data.btanalysis import create_cum_profit, load_backtest_data
@@ -335,6 +337,15 @@ def test_start_plot_profit(mocker):
     assert called_config['pairs'] == ["ETH/BTC"]
 
 
+def test_start_plot_profit_error(mocker):
+    args = [
+        "plot-profit",
+        "--pairs", "ETH/BTC"
+    ]
+    with pytest.raises(OperationalException):
+        start_plot_profit(get_args(args))
+
+
 def test_plot_profit(default_conf, mocker, caplog):
     default_conf['trade_source'] = 'file'
     default_conf["datadir"] = history.make_testdata_path(None)