diff --git a/.dependabot/config.yml b/.dependabot/config.yml new file mode 100644 index 000000000..66b91e99f --- /dev/null +++ b/.dependabot/config.yml @@ -0,0 +1,17 @@ +version: 1 + +update_configs: + - package_manager: "python" + directory: "/" + update_schedule: "weekly" + allowed_updates: + - match: + update_type: "all" + target_branch: "develop" + + - package_manager: "docker" + directory: "/" + update_schedule: "daily" + allowed_updates: + - match: + update_type: "all" diff --git a/.pyup.yml b/.pyup.yml deleted file mode 100644 index b1b721113..000000000 --- a/.pyup.yml +++ /dev/null @@ -1,37 +0,0 @@ -# autogenerated pyup.io config file -# see https://pyup.io/docs/configuration/ for all available options - -# configure updates globally -# default: all -# allowed: all, insecure, False -update: all - -# configure dependency pinning globally -# default: True -# allowed: True, False -pin: True - -# update schedule -# default: empty -# allowed: "every day", "every week", .. -schedule: "every week" - - -search: False -# Specify requirement files by hand, default is empty -# default: empty -# allowed: list -requirements: - - requirements.txt - - requirements-dev.txt - - requirements-plot.txt - - requirements-common.txt - - -# configure the branch prefix the bot is using -# default: pyup- -branch_prefix: pyup/ - -# allow to close stale PRs -# default: True -close_prs: True diff --git a/Dockerfile b/Dockerfile index 7a0298719..8677b54de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.7.3-slim-stretch +FROM python:3.7.4-slim-stretch RUN apt-get update \ && apt-get -y install curl build-essential libssl-dev \ diff --git a/docs/bot-usage.md b/docs/bot-usage.md index 93ca5107e..4bec16913 100644 --- a/docs/bot-usage.md +++ b/docs/bot-usage.md @@ -2,7 +2,7 @@ This page explains the different parameters of the bot and how to run it. -!Note: +!!! Note: If you've used `setup.sh`, don't forget to activate your virtual environment (`source .env/bin/activate`) before running freqtrade commands. @@ -48,21 +48,24 @@ optional arguments: ``` -### How to use a different configuration file? +### How to specify which configuration file be used? -The bot allows you to select which configuration file you want to use. Per -default, the bot will load the file `./config.json` +The bot allows you to select which configuration file you want to use by means of +the `-c/--config` command line option: ```bash freqtrade -c path/far/far/away/config.json ``` +Per default, the bot loads the `config.json` configuration file from the current +working directory. + ### How to use multiple configuration files? The bot allows you to use multiple configuration files by specifying multiple -`-c/--config` configuration options in the command line. Configuration parameters -defined in the last configuration file override parameters with the same name -defined in the previous configuration file specified in the command line. +`-c/--config` options in the command line. Configuration parameters +defined in the latter configuration files override parameters with the same name +defined in the previous configuration files specified in the command line earlier. For example, you can make a separate configuration file with your key and secrete for the Exchange you use for trading, specify default configuration file with @@ -235,7 +238,7 @@ usage: freqtrade hyperopt [-h] [-i TICKER_INTERVAL] [--timerange TIMERANGE] [--customhyperopt NAME] [--hyperopt-path PATH] [--eps] [-e INT] [-s {all,buy,sell,roi,stoploss} [{all,buy,sell,roi,stoploss} ...]] - [--dmmp] [--print-all] [-j JOBS] + [--dmmp] [--print-all] [--no-color] [-j JOBS] [--random-state INT] [--min-trades INT] [--continue] [--hyperopt-loss NAME] @@ -271,6 +274,8 @@ optional arguments: (same as setting `max_open_trades` to a very high number). --print-all Print all results, not only the best ones. + --no-color Disable colorization of hyperopt results. May be + useful if you are redirecting output to a file. -j JOBS, --job-workers JOBS The number of concurrently running jobs for hyperoptimization (hyperopt worker processes). If -1 @@ -284,17 +289,18 @@ optional arguments: --continue Continue hyperopt from previous runs. By default, temporary files will be removed and hyperopt will start from scratch. - --hyperopt-loss NAME - Specify the class name of the hyperopt loss function + --hyperopt-loss NAME Specify the class name of the hyperopt loss function class (IHyperOptLoss). Different functions can generate completely different results, since the - target for optimization is different. (default: - `DefaultHyperOptLoss`). + target for optimization is different. Built-in + Hyperopt-loss-functions are: DefaultHyperOptLoss, + OnlyProfitHyperOptLoss, SharpeHyperOptLoss. + (default: `DefaultHyperOptLoss`). ``` ## Edge commands -To know your trade expectacny and winrate against historical data, you can use Edge. +To know your trade expectancy and winrate against historical data, you can use Edge. ``` usage: freqtrade edge [-h] [-i TICKER_INTERVAL] [--timerange TIMERANGE] diff --git a/docs/configuration.md b/docs/configuration.md index dfe191b10..2d21de942 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1,15 +1,34 @@ # Configure the bot -This page explains how to configure your `config.json` file. +This page explains how to configure the bot. -## Setup config.json +## The Freqtrade configuration file -We recommend to copy and use the `config.json.example` as a template +The bot uses a set of configuration parameters during its operation that all together conform the bot configuration. It normally reads its configuration from a file (Freqtrade configuration file). + +Per default, the bot loads configuration from the `config.json` file located in the current working directory. + +You can change the name of the configuration file used by the bot with the `-c/--config` command line option. + +In some advanced use cases, multiple configuration files can be specified and used by the bot or the bot can read its configuration parameters from the process standard input stream. + +If you used the [Quick start](installation.md/#quick-start) method for installing +the bot, the installation script should have already created the default configuration file (`config.json`) for you. + +If default configuration file is not created we recommend you to copy and use the `config.json.example` as a template for your bot configuration. -The table below will list all configuration parameters. +The Freqtrade configuration file is to be written in the JSON format. -Mandatory Parameters are marked as **Required**. +Additionally to the standard JSON syntax, you may use one-line `// ...` and multi-line `/* ... */` comments in your configuration files and trailing commas in the lists of parameters. + +Do not worry if you are not familiar with JSON format -- simply open the configuration file with an editor of your choice, make some changes to the parameters you need, save your changes and, finally, restart the bot or, if it was previously stopped, run it again with the changes you made to the configuration. The bot validates syntax of the configuration file at startup and will warn you if you made any errors editing it. + +## Configuration parameters + +The table below will list all configuration parameters available. + +Mandatory parameters are marked as **Required**. | Command | Default | Description | |----------|---------|-------------| @@ -53,6 +72,7 @@ Mandatory Parameters are marked as **Required**. | `experimental.use_sell_signal` | false | Use your sell strategy in addition of the `minimal_roi`. [Strategy Override](#parameters-in-the-strategy). | `experimental.sell_profit_only` | false | Waits until you have made a positive profit before taking a sell decision. [Strategy Override](#parameters-in-the-strategy). | `experimental.ignore_roi_if_buy_signal` | false | Does not sell if the buy-signal is still active. Takes preference over `minimal_roi` and `use_sell_signal`. [Strategy Override](#parameters-in-the-strategy). +| `experimental.block_bad_exchanges` | true | Block exchanges known to not work with freqtrade. Leave on default unless you want to test if that exchange works now. | `pairlist.method` | StaticPairList | Use static or dynamic volume-based pairlist. [More information below](#dynamic-pairlists). | `pairlist.config` | None | Additional configuration for dynamic pairlists. [More information below](#dynamic-pairlists). | `telegram.enabled` | true | **Required.** Enable or not the usage of Telegram. diff --git a/docs/data-analysis.md b/docs/data-analysis.md index 42ea42e9c..6d7d0eb00 100644 --- a/docs/data-analysis.md +++ b/docs/data-analysis.md @@ -31,6 +31,16 @@ df = load_trades_from_db("sqlite:///tradesv3.sqlite") df.groupby("pair")["sell_reason"].value_counts() ``` +### Load multiple configuration files + +This option can be usefull to inspect the results of passing in multiple configs in case of problems + +``` python +from freqtrade.configuration import Configuration +config = Configuration.from_files(["config1.json", "config2.json"]) +print(config) +``` + ## Strategy debugging example Debugging a strategy can be time-consuming. FreqTrade offers helper functions to visualize raw data. diff --git a/docs/developer.md b/docs/developer.md index 28369bb73..259bfafd8 100644 --- a/docs/developer.md +++ b/docs/developer.md @@ -17,6 +17,29 @@ Alternatively (if your system is not supported by the setup.sh script), follow t This will install all required tools for development, including `pytest`, `flake8`, `mypy`, and `coveralls`. +### Tests + +New code should be covered by basic unittests. Depending on the complexity of the feature, Reviewers may request more in-depth unittests. +If necessary, the Freqtrade team can assist and give guidance with writing good tests (however please don't expect anyone to write the tests for you). + +#### Checking log content in tests + +Freqtrade uses 2 main methods to check log content in tests, `log_has()` and `log_has_re()` (to check using regex, in case of dynamic log-messages). +These are available from `conftest.py` and can be imported in any test module. + +A sample check looks as follows: + +``` python +from freqtrade.tests.conftest import log_has, log_has_re + +def test_method_to_test(caplog): + method_to_test() + + assert log_has("This event happened", caplog) + # Check regex with trailing number ... + assert log_has_re(r"This dynamic event happened and produced \d+", caplog) +``` + ## Modules ### Dynamic Pairlist diff --git a/docs/docker.md b/docs/docker.md index 615d31796..923dec1e2 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -26,6 +26,10 @@ To update the image, simply run the above commands again and restart your runnin Should you require additional libraries, please [build the image yourself](#build-your-own-docker-image). +!!! Note Docker image update frequency + The official docker images with tags `master`, `develop` and `latest` are automatically rebuild once a week to keep the base image uptodate. + In addition to that, every merge to `develop` will trigger a rebuild for `develop` and `latest`. + ### Prepare the configuration files Even though you will use docker, you'll still need some files from the github repository. diff --git a/docs/hyperopt.md b/docs/hyperopt.md index 6ef68d82f..c1bf56a3d 100644 --- a/docs/hyperopt.md +++ b/docs/hyperopt.md @@ -164,7 +164,11 @@ By default, FreqTrade uses a loss function, which has been with freqtrade since A different loss function can be specified by using the `--hyperopt-loss ` argument. This class should be in its own file within the `user_data/hyperopts/` directory. -Currently, the following loss functions are builtin: `DefaultHyperOptLoss` (default legacy Freqtrade hyperoptimization loss function), `SharpeHyperOptLoss` (optimizes Sharpe Ratio calculated on the trade returns) and `OnlyProfitHyperOptLoss` (which takes only amount of profit into consideration). +Currently, the following loss functions are builtin: + +* `DefaultHyperOptLoss` (default legacy Freqtrade hyperoptimization loss function) +* `OnlyProfitHyperOptLoss` (which takes only amount of profit into consideration) +* `SharpeHyperOptLoss` (optimizes Sharpe Ratio calculated on the trade returns) ### Creating and using a custom loss function @@ -348,6 +352,10 @@ def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame: return dataframe ``` +By default, hyperopt prints colorized results -- epochs with positive profit are printed in the green color. This highlighting helps you find epochs that can be interesting for later analysis. Epochs with zero total profit or with negative profits (losses) are printed in the normal color. If you do not need colorization of results (for instance, when you are redirecting hyperopt output to a file) you can switch colorization off by specifying the `--no-color` option in the command line. + +You can use the `--print-all` command line option if you would like to see all results in the hyperopt output, not only the best ones. When `--print-all` is used, current best results are also colorized by default -- they are printed in bold (bright) style. This can also be switched off with the `--no-color` command line option. + ### Understand Hyperopt ROI results If you are optimizing ROI (i.e. if optimization search-space contains 'all' or 'roi'), your result will look as follows and include a ROI table: diff --git a/docs/strategy-customization.md b/docs/strategy-customization.md index 15f44955b..0d08bdd02 100644 --- a/docs/strategy-customization.md +++ b/docs/strategy-customization.md @@ -309,8 +309,10 @@ if self.dp: dataframe['best_bid'] = ob['bids'][0][0] dataframe['best_ask'] = ob['asks'][0][0] ``` -!Warning The order book is not part of the historic data which means backtesting and hyperopt will not work if this - method is used. + +!!! Warning + The order book is not part of the historic data which means backtesting and hyperopt will not work if this + method is used. #### Available Pairs diff --git a/freqtrade/configuration/__init__.py b/freqtrade/configuration/__init__.py index 548b508a7..7b476d173 100644 --- a/freqtrade/configuration/__init__.py +++ b/freqtrade/configuration/__init__.py @@ -1,2 +1,3 @@ -from freqtrade.configuration.arguments import Arguments, TimeRange # noqa: F401 +from freqtrade.configuration.arguments import Arguments # noqa: F401 +from freqtrade.configuration.timerange import TimeRange # noqa: F401 from freqtrade.configuration.configuration import Configuration # noqa: F401 diff --git a/freqtrade/configuration/arguments.py b/freqtrade/configuration/arguments.py index ef19d121e..083b05b40 100644 --- a/freqtrade/configuration/arguments.py +++ b/freqtrade/configuration/arguments.py @@ -2,10 +2,8 @@ This module contains the argument manager class """ import argparse -import re -from typing import List, NamedTuple, Optional +from typing import List, Optional -import arrow from freqtrade.configuration.cli_options import AVAILABLE_CLI_OPTIONS from freqtrade import constants @@ -23,7 +21,8 @@ ARGS_BACKTEST = ARGS_COMMON_OPTIMIZE + ["position_stacking", "use_max_market_pos ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path", "position_stacking", "epochs", "spaces", - "use_max_market_positions", "print_all", "hyperopt_jobs", + "use_max_market_positions", "print_all", + "print_colorized", "print_json", "hyperopt_jobs", "hyperopt_random_state", "hyperopt_min_trades", "hyperopt_continue", "hyperopt_loss"] @@ -44,18 +43,6 @@ ARGS_PLOT_PROFIT = (ARGS_COMMON + ARGS_STRATEGY + ["pairs", "timerange", "export", "exportfilename", "db_url", "trade_source"]) -class TimeRange(NamedTuple): - """ - NamedTuple defining timerange inputs. - [start/stop]type defines if [start/stop]ts shall be used. - if *type is None, don't use corresponding startvalue. - """ - starttype: Optional[str] = None - stoptype: Optional[str] = None - startts: int = 0 - stopts: int = 0 - - class Arguments(object): """ Arguments Class. Manage the arguments received by the cli @@ -138,45 +125,3 @@ class Arguments(object): ) list_exchanges_cmd.set_defaults(func=start_list_exchanges) self._build_args(optionlist=ARGS_LIST_EXCHANGES, parser=list_exchanges_cmd) - - @staticmethod - def parse_timerange(text: Optional[str]) -> TimeRange: - """ - Parse the value of the argument --timerange to determine what is the range desired - :param text: value from --timerange - :return: Start and End range period - """ - if text is None: - return TimeRange(None, None, 0, 0) - syntax = [(r'^-(\d{8})$', (None, 'date')), - (r'^(\d{8})-$', ('date', None)), - (r'^(\d{8})-(\d{8})$', ('date', 'date')), - (r'^-(\d{10})$', (None, 'date')), - (r'^(\d{10})-$', ('date', None)), - (r'^(\d{10})-(\d{10})$', ('date', 'date')), - (r'^(-\d+)$', (None, 'line')), - (r'^(\d+)-$', ('line', None)), - (r'^(\d+)-(\d+)$', ('index', 'index'))] - for rex, stype in syntax: - # Apply the regular expression to text - match = re.match(rex, text) - if match: # Regex has matched - rvals = match.groups() - index = 0 - start: int = 0 - stop: int = 0 - if stype[0]: - starts = rvals[index] - if stype[0] == 'date' and len(starts) == 8: - start = arrow.get(starts, 'YYYYMMDD').timestamp - else: - start = int(starts) - index += 1 - if stype[1]: - stops = rvals[index] - if stype[1] == 'date' and len(stops) == 8: - stop = arrow.get(stops, 'YYYYMMDD').timestamp - else: - stop = int(stops) - return TimeRange(stype[0], stype[1], start, stop) - raise Exception('Incorrect syntax for timerange "%s"' % text) diff --git a/freqtrade/configuration/check_exchange.py b/freqtrade/configuration/check_exchange.py index 8dae06f7a..70f4cfa33 100644 --- a/freqtrade/configuration/check_exchange.py +++ b/freqtrade/configuration/check_exchange.py @@ -2,9 +2,9 @@ import logging from typing import Any, Dict from freqtrade import OperationalException -from freqtrade.exchange import (is_exchange_bad, is_exchange_available, - is_exchange_officially_supported, available_exchanges) - +from freqtrade.exchange import (available_exchanges, get_exchange_bad_reason, + is_exchange_available, is_exchange_bad, + is_exchange_officially_supported) logger = logging.getLogger(__name__) @@ -31,9 +31,8 @@ def check_exchange(config: Dict[str, Any], check_for_bad: bool = True) -> bool: ) if check_for_bad and is_exchange_bad(exchange): - logger.warning(f'Exchange "{exchange}" is known to not work with the bot yet. ' - f'Use it only for development and testing purposes.') - return False + raise OperationalException(f'Exchange "{exchange}" is known to not work with the bot yet. ' + f'Reason: {get_exchange_bad_reason(exchange)}') if is_exchange_officially_supported(exchange): logger.info(f'Exchange "{exchange}" is officially supported ' diff --git a/freqtrade/configuration/cli_options.py b/freqtrade/configuration/cli_options.py index 53509c43b..685ce3273 100644 --- a/freqtrade/configuration/cli_options.py +++ b/freqtrade/configuration/cli_options.py @@ -196,6 +196,19 @@ AVAILABLE_CLI_OPTIONS = { action='store_true', default=False, ), + "print_colorized": Arg( + '--no-color', + help='Disable colorization of hyperopt results. May be useful if you are ' + 'redirecting output to a file.', + action='store_false', + default=True, + ), + "print_json": Arg( + '--print-json', + help='Print best result detailization in JSON format.', + action='store_true', + default=False, + ), "hyperopt_jobs": Arg( '-j', '--job-workers', help='The number of concurrently running jobs for hyperoptimization ' @@ -231,7 +244,9 @@ AVAILABLE_CLI_OPTIONS = { '--hyperopt-loss', help='Specify the class name of the hyperopt loss function class (IHyperOptLoss). ' 'Different functions can generate completely different results, ' - 'since the target for optimization is different. (default: `%(default)s`).', + 'since the target for optimization is different. Built-in Hyperopt-loss-functions are: ' + 'DefaultHyperOptLoss, OnlyProfitHyperOptLoss, SharpeHyperOptLoss.' + '(default: `%(default)s`).', metavar='NAME', default=constants.DEFAULT_HYPEROPT_LOSS, ), diff --git a/freqtrade/configuration/configuration.py b/freqtrade/configuration/configuration.py index 0d83ca0b5..7ed24b21f 100644 --- a/freqtrade/configuration/configuration.py +++ b/freqtrade/configuration/configuration.py @@ -5,11 +5,12 @@ import logging import warnings from argparse import Namespace from pathlib import Path -from typing import Any, Callable, Dict, Optional +from typing import Any, Callable, Dict, List, Optional from freqtrade import OperationalException, constants from freqtrade.configuration.check_exchange import check_exchange -from freqtrade.configuration.directory_operations import create_datadir, create_userdata_dir +from freqtrade.configuration.directory_operations import (create_datadir, + create_userdata_dir) from freqtrade.configuration.json_schema import validate_config_schema from freqtrade.configuration.load_config import load_config_file from freqtrade.loggers import setup_logging @@ -40,43 +41,43 @@ class Configuration(object): return self.config - def _load_config_files(self) -> Dict[str, Any]: + @staticmethod + def from_files(files: List[str]) -> Dict[str, Any]: """ - Iterate through the config files passed in the args, - loading all of them and merging their contents. + Iterate through the config files passed in, loading all of them + and merging their contents. + Files are loaded in sequence, parameters in later configuration files + override the same parameter from an earlier file (last definition wins). + :param files: List of file paths + :return: configuration dictionary """ + # Keep this method as staticmethod, so it can be used from interactive environments config: Dict[str, Any] = {} # We expect here a list of config filenames - for path in self.args.config: - logger.info('Using config: %s ...', path) + for path in files: + logger.info(f'Using config: {path} ...') # Merge config options, overwriting old values config = deep_merge_dicts(load_config_file(path), config) - return config - - def _normalize_config(self, config: Dict[str, Any]) -> None: - """ - Make config more canonical -- i.e. for example add missing parts that we expect - to be normally in it... - """ + # Normalize config if 'internals' not in config: config['internals'] = {} + # validate configuration before returning + logger.info('Validating configuration ...') + validate_config_schema(config) + + return config + def load_config(self) -> Dict[str, Any]: """ Extract information for sys.argv and load the bot configuration :return: Configuration dictionary """ # Load all configs - config: Dict[str, Any] = self._load_config_files() - - # Make resulting config more canonical - self._normalize_config(config) - - logger.info('Validating configuration ...') - validate_config_schema(config) + config: Dict[str, Any] = Configuration.from_files(self.args.config) self._validate_config_consistency(config) @@ -146,7 +147,7 @@ class Configuration(object): config['internals'].update({'sd_notify': True}) # Check if the exchange set by the user is supported - check_exchange(config) + check_exchange(config, config.get('experimental', {}).get('block_bad_exchanges', True)) def _process_datadir_options(self, config: Dict[str, Any]) -> None: """ @@ -244,6 +245,15 @@ class Configuration(object): self._args_to_config(config, argname='print_all', logstring='Parameter --print-all detected ...') + if 'print_colorized' in self.args and not self.args.print_colorized: + logger.info('Parameter --no-color detected ...') + config.update({'print_colorized': False}) + else: + config.update({'print_colorized': True}) + + self._args_to_config(config, argname='print_json', + logstring='Parameter --print-json detected ...') + self._args_to_config(config, argname='hyperopt_jobs', logstring='Parameter -j/--job-workers detected: {}') @@ -280,7 +290,7 @@ class Configuration(object): if not self.runmode: # Handle real mode, infer dry/live from config self.runmode = RunMode.DRY_RUN if config.get('dry_run', True) else RunMode.LIVE - logger.info("Runmode set to {self.runmode}.") + logger.info(f"Runmode set to {self.runmode}.") config.update({'runmode': self.runmode}) diff --git a/freqtrade/configuration/load_config.py b/freqtrade/configuration/load_config.py index 25504144f..7a3ca1798 100644 --- a/freqtrade/configuration/load_config.py +++ b/freqtrade/configuration/load_config.py @@ -1,7 +1,7 @@ """ This module contain functions to load the configuration file """ -import json +import rapidjson import logging import sys from typing import Any, Dict @@ -12,6 +12,9 @@ from freqtrade import OperationalException logger = logging.getLogger(__name__) +CONFIG_PARSE_MODE = rapidjson.PM_COMMENTS | rapidjson.PM_TRAILING_COMMAS + + def load_config_file(path: str) -> Dict[str, Any]: """ Loads a config file from the given path @@ -21,7 +24,7 @@ def load_config_file(path: str) -> Dict[str, Any]: try: # Read config from stdin if requested in the options with open(path) if path != '-' else sys.stdin as file: - config = json.load(file) + config = rapidjson.load(file, parse_mode=CONFIG_PARSE_MODE) except FileNotFoundError: raise OperationalException( f'Config file "{path}" not found!' diff --git a/freqtrade/configuration/timerange.py b/freqtrade/configuration/timerange.py new file mode 100644 index 000000000..f980b71ea --- /dev/null +++ b/freqtrade/configuration/timerange.py @@ -0,0 +1,70 @@ +""" +This module contains the argument manager class +""" +import re +from typing import Optional + +import arrow + + +class TimeRange(): + """ + object defining timerange inputs. + [start/stop]type defines if [start/stop]ts shall be used. + if *type is None, don't use corresponding startvalue. + """ + + def __init__(self, starttype: Optional[str] = None, stoptype: Optional[str] = None, + startts: int = 0, stopts: int = 0): + + self.starttype: Optional[str] = starttype + self.stoptype: Optional[str] = stoptype + self.startts: int = startts + self.stopts: int = stopts + + def __eq__(self, other): + """Override the default Equals behavior""" + return (self.starttype == other.starttype and self.stoptype == other.stoptype + and self.startts == other.startts and self.stopts == other.stopts) + + @staticmethod + def parse_timerange(text: Optional[str]): + """ + Parse the value of the argument --timerange to determine what is the range desired + :param text: value from --timerange + :return: Start and End range period + """ + if text is None: + return TimeRange(None, None, 0, 0) + syntax = [(r'^-(\d{8})$', (None, 'date')), + (r'^(\d{8})-$', ('date', None)), + (r'^(\d{8})-(\d{8})$', ('date', 'date')), + (r'^-(\d{10})$', (None, 'date')), + (r'^(\d{10})-$', ('date', None)), + (r'^(\d{10})-(\d{10})$', ('date', 'date')), + (r'^(-\d+)$', (None, 'line')), + (r'^(\d+)-$', ('line', None)), + (r'^(\d+)-(\d+)$', ('index', 'index'))] + for rex, stype in syntax: + # Apply the regular expression to text + match = re.match(rex, text) + if match: # Regex has matched + rvals = match.groups() + index = 0 + start: int = 0 + stop: int = 0 + if stype[0]: + starts = rvals[index] + if stype[0] == 'date' and len(starts) == 8: + start = arrow.get(starts, 'YYYYMMDD').timestamp + else: + start = int(starts) + index += 1 + if stype[1]: + stops = rvals[index] + if stype[1] == 'date' and len(stops) == 8: + stop = arrow.get(stops, 'YYYYMMDD').timestamp + else: + stop = int(stops) + return TimeRange(stype[0], stype[1], start, stop) + raise Exception('Incorrect syntax for timerange "%s"' % text) diff --git a/freqtrade/data/dataprovider.py b/freqtrade/data/dataprovider.py index b87589df7..5b71c21a8 100644 --- a/freqtrade/data/dataprovider.py +++ b/freqtrade/data/dataprovider.py @@ -44,36 +44,49 @@ class DataProvider(): def ohlcv(self, pair: str, ticker_interval: str = None, copy: bool = True) -> DataFrame: """ - get ohlcv data for the given pair as DataFrame - Please check `available_pairs` to verify which pairs are currently cached. + Get ohlcv data for the given pair as DataFrame + Please use the `available_pairs` method to verify which pairs are currently cached. :param pair: pair to get the data for - :param ticker_interval: ticker_interval to get pair for - :param copy: copy dataframe before returning. - Use false only for RO operations (where the dataframe is not modified) + :param ticker_interval: ticker interval to get data for + :param copy: copy dataframe before returning if True. + Use False only for read-only operations (where the dataframe is not modified) """ if self.runmode in (RunMode.DRY_RUN, RunMode.LIVE): - if ticker_interval: - pairtick = (pair, ticker_interval) - else: - pairtick = (pair, self._config['ticker_interval']) - - return self._exchange.klines(pairtick, copy=copy) + return self._exchange.klines((pair, ticker_interval or self._config['ticker_interval']), + copy=copy) else: return DataFrame() - def historic_ohlcv(self, pair: str, ticker_interval: str) -> DataFrame: + def historic_ohlcv(self, pair: str, ticker_interval: str = None) -> DataFrame: """ - get stored historic ohlcv data + Get stored historic ohlcv data :param pair: pair to get the data for - :param ticker_interval: ticker_interval to get pair for + :param ticker_interval: ticker interval to get data for """ return load_pair_history(pair=pair, - ticker_interval=ticker_interval, + ticker_interval=ticker_interval or self._config['ticker_interval'], refresh_pairs=False, datadir=Path(self._config['datadir']) if self._config.get( 'datadir') else None ) + def get_pair_dataframe(self, pair: str, ticker_interval: str = None) -> DataFrame: + """ + Return pair ohlcv data, either live or cached historical -- depending + on the runmode. + :param pair: pair to get the data for + :param ticker_interval: ticker interval to get data for + """ + if self.runmode in (RunMode.DRY_RUN, RunMode.LIVE): + # Get live ohlcv data. + data = self.ohlcv(pair=pair, ticker_interval=ticker_interval) + else: + # Get historic ohlcv data (cached on disk). + data = self.historic_ohlcv(pair=pair, ticker_interval=ticker_interval) + if len(data) == 0: + logger.warning(f"No data found for ({pair}, {ticker_interval}).") + return data + def ticker(self, pair: str): """ Return last ticker data diff --git a/freqtrade/data/history.py b/freqtrade/data/history.py index f600615df..899c6d0c8 100644 --- a/freqtrade/data/history.py +++ b/freqtrade/data/history.py @@ -252,10 +252,11 @@ def download_pair_history(datadir: Optional[Path], logger.debug("Current End: %s", misc.format_ms_time(data[-1][0]) if data else 'None') # Default since_ms to 30 days if nothing is given - new_data = exchange.get_history(pair=pair, ticker_interval=ticker_interval, - since_ms=since_ms if since_ms - else - int(arrow.utcnow().shift(days=-30).float_timestamp) * 1000) + new_data = exchange.get_historic_ohlcv(pair=pair, ticker_interval=ticker_interval, + since_ms=since_ms if since_ms + else + int(arrow.utcnow().shift( + days=-30).float_timestamp) * 1000) data.extend(new_data) logger.debug("New Start: %s", misc.format_ms_time(data[0][0])) diff --git a/freqtrade/edge/__init__.py b/freqtrade/edge/__init__.py index 7085663d6..2d3097ec4 100644 --- a/freqtrade/edge/__init__.py +++ b/freqtrade/edge/__init__.py @@ -10,7 +10,7 @@ import utils_find_1st as utf1st from pandas import DataFrame from freqtrade import constants, OperationalException -from freqtrade.configuration import Arguments, TimeRange +from freqtrade.configuration import TimeRange from freqtrade.data import history from freqtrade.strategy.interface import SellType @@ -75,7 +75,7 @@ class Edge(): self._stoploss_range_step ) - self._timerange: TimeRange = Arguments.parse_timerange("%s-" % arrow.now().shift( + self._timerange: TimeRange = TimeRange.parse_timerange("%s-" % arrow.now().shift( days=-1 * self._since_number_of_days).format('YYYYMMDD')) self.fee = self.exchange.get_fee() diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 5c58320f6..4037ca704 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -1,10 +1,13 @@ from freqtrade.exchange.exchange import Exchange # noqa: F401 -from freqtrade.exchange.exchange import (is_exchange_bad, # noqa: F401 +from freqtrade.exchange.exchange import (get_exchange_bad_reason, # noqa: F401 + is_exchange_bad, is_exchange_available, is_exchange_officially_supported, available_exchanges) from freqtrade.exchange.exchange import (timeframe_to_seconds, # noqa: F401 timeframe_to_minutes, - timeframe_to_msecs) + timeframe_to_msecs, + timeframe_to_next_date, + timeframe_to_prev_date) from freqtrade.exchange.kraken import Kraken # noqa: F401 from freqtrade.exchange.binance import Binance # noqa: F401 diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 657f382d8..5119e0fcd 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -6,7 +6,7 @@ import asyncio import inspect import logging from copy import deepcopy -from datetime import datetime +from datetime import datetime, timezone from math import ceil, floor from random import randint from typing import Any, Dict, List, Optional, Tuple @@ -25,6 +25,11 @@ logger = logging.getLogger(__name__) API_RETRY_COUNT = 4 +BAD_EXCHANGES = { + "bitmex": "Various reasons", + "bitstamp": "Does not provide history. " + "Details in https://github.com/freqtrade/freqtrade/issues/1983", + } def retrier_async(f): @@ -371,7 +376,7 @@ class Exchange(object): 'side': side, 'remaining': amount, 'datetime': arrow.utcnow().isoformat(), - 'status': "open", + 'status': "closed" if ordertype == "market" else "open", 'fee': None, "info": {} } @@ -541,19 +546,24 @@ class Exchange(object): logger.info("returning cached ticker-data for %s", pair) return self._cached_ticker[pair] - def get_history(self, pair: str, ticker_interval: str, - since_ms: int) -> List: + def get_historic_ohlcv(self, pair: str, ticker_interval: str, + since_ms: int) -> List: """ Gets candle history using asyncio and returns the list of candles. Handles all async doing. + Async over one pair, assuming we get `_ohlcv_candle_limit` candles per call. + :param pair: Pair to download + :param ticker_interval: Interval to get + :param since_ms: Timestamp in milliseconds to get history from + :returns List of tickers """ return asyncio.get_event_loop().run_until_complete( - self._async_get_history(pair=pair, ticker_interval=ticker_interval, - since_ms=since_ms)) + self._async_get_historic_ohlcv(pair=pair, ticker_interval=ticker_interval, + since_ms=since_ms)) - async def _async_get_history(self, pair: str, - ticker_interval: str, - since_ms: int) -> List: + async def _async_get_historic_ohlcv(self, pair: str, + ticker_interval: str, + since_ms: int) -> List: one_call = timeframe_to_msecs(ticker_interval) * self._ohlcv_candle_limit logger.debug( @@ -579,7 +589,10 @@ class Exchange(object): def refresh_latest_ohlcv(self, pair_list: List[Tuple[str, str]]) -> List[Tuple[str, List]]: """ - Refresh in-memory ohlcv asyncronously and set `_klines` with the result + Refresh in-memory ohlcv asynchronously and set `_klines` with the result + Loops asynchronously over pair_list and downloads all pairs async (semi-parallel). + :param pair_list: List of 2 element tuples containing pair, interval to refresh + :return: Returns a List of ticker-dataframes. """ logger.debug("Refreshing ohlcv data for %d pairs", len(pair_list)) @@ -627,7 +640,7 @@ class Exchange(object): async def _async_get_candle_history(self, pair: str, ticker_interval: str, since_ms: Optional[int] = None) -> Tuple[str, str, List]: """ - Asyncronously gets candle histories using fetch_ohlcv + Asynchronously gets candle histories using fetch_ohlcv returns tuple: (pair, ticker_interval, ohlcv_list) """ try: @@ -755,7 +768,11 @@ class Exchange(object): def is_exchange_bad(exchange: str) -> bool: - return exchange in ['bitmex', 'bitstamp'] + return exchange in BAD_EXCHANGES + + +def get_exchange_bad_reason(exchange: str) -> str: + return BAD_EXCHANGES.get(exchange, "") def is_exchange_available(exchange: str, ccxt_module=None) -> bool: @@ -781,13 +798,45 @@ def timeframe_to_seconds(ticker_interval: str) -> int: def timeframe_to_minutes(ticker_interval: str) -> int: """ - Same as above, but returns minutes. + Same as timeframe_to_seconds, but returns minutes. """ return ccxt.Exchange.parse_timeframe(ticker_interval) // 60 def timeframe_to_msecs(ticker_interval: str) -> int: """ - Same as above, but returns milliseconds. + Same as timeframe_to_seconds, but returns milliseconds. """ return ccxt.Exchange.parse_timeframe(ticker_interval) * 1000 + + +def timeframe_to_prev_date(timeframe: str, date: datetime = None) -> datetime: + """ + Use Timeframe and determine last possible candle. + :param timeframe: timeframe in string format (e.g. "5m") + :param date: date to use. Defaults to utcnow() + :returns: date of previous candle (with utc timezone) + """ + if not date: + date = datetime.now(timezone.utc) + timeframe_secs = timeframe_to_seconds(timeframe) + # Get offset based on timerame_secs + offset = date.timestamp() % timeframe_secs + # Subtract seconds passed since last offset + new_timestamp = date.timestamp() - offset + return datetime.fromtimestamp(new_timestamp, tz=timezone.utc) + + +def timeframe_to_next_date(timeframe: str, date: datetime = None) -> datetime: + """ + Use Timeframe and determine next candle. + :param timeframe: timeframe in string format (e.g. "5m") + :param date: date to use. Defaults to utcnow() + :returns: date of next candle (with utc timezone) + """ + prevdate = timeframe_to_prev_date(timeframe, date) + timeframe_secs = timeframe_to_seconds(timeframe) + + # Add one interval to previous candle + new_timestamp = prevdate.timestamp() + timeframe_secs + return datetime.fromtimestamp(new_timestamp, tz=timezone.utc) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index d52165e0a..68b45d96f 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -16,11 +16,11 @@ from freqtrade import (DependencyException, OperationalException, InvalidOrderEx from freqtrade.data.converter import order_book_to_dataframe from freqtrade.data.dataprovider import DataProvider from freqtrade.edge import Edge -from freqtrade.exchange import timeframe_to_minutes +from freqtrade.exchange import timeframe_to_minutes, timeframe_to_next_date from freqtrade.persistence import Trade from freqtrade.rpc import RPCManager, RPCMessageType from freqtrade.resolvers import ExchangeResolver, StrategyResolver, PairListResolver -from freqtrade.state import State +from freqtrade.state import State, RunMode from freqtrade.strategy.interface import SellType, IStrategy from freqtrade.wallets import Wallets @@ -75,6 +75,12 @@ class FreqtradeBot(object): persistence.init(self.config.get('db_url', None), clean_open_orders=self.config.get('dry_run', False)) + # Stoploss on exchange does not make sense, therefore we need to disable that. + if (self.dataprovider.runmode == RunMode.DRY_RUN and + self.strategy.order_types.get('stoploss_on_exchange', False)): + logger.info("Disabling stoploss_on_exchange during dry-run.") + self.strategy.order_types['stoploss_on_exchange'] = False + config['order_types']['stoploss_on_exchange'] = False # Set initial bot state from config initial_state = self.config.get('initial_state') self.state = State[initial_state.upper()] if initial_state else State.STOPPED @@ -99,13 +105,12 @@ class FreqtradeBot(object): # Adjust stoploss if it was changed Trade.stoploss_reinitialization(self.strategy.stoploss) - def process(self) -> bool: + def process(self) -> None: """ Queries the persistence layer for open trades and handles them, otherwise a new trade is created. :return: True if one or more trades has been created or closed, False otherwise """ - state_changed = False # Check whether markets have to be reloaded self.exchange._reload_markets() @@ -132,19 +137,17 @@ class FreqtradeBot(object): # First process current opened trades for trade in trades: - state_changed |= self.process_maybe_execute_sell(trade) + self.process_maybe_execute_sell(trade) # Then looking for buy opportunities if len(trades) < self.config['max_open_trades']: - state_changed = self.process_maybe_execute_buy() + self.process_maybe_execute_buy() if 'unfilledtimeout' in self.config: # Check and handle any timed out open orders self.check_handle_timedout() Trade.session.flush() - return state_changed - def _extend_whitelist_with_trades(self, whitelist: List[str], trades: List[Any]): """ Extend whitelist with pairs from open trades @@ -253,11 +256,12 @@ class FreqtradeBot(object): amount_reserve_percent = max(amount_reserve_percent, 0.5) return min(min_stake_amounts) / amount_reserve_percent - def create_trade(self) -> bool: + def create_trades(self) -> bool: """ - Checks the implemented trading indicator(s) for a randomly picked pair, - if one pair triggers the buy_signal a new trade record gets created - :return: True if a trade object has been created and persisted, False otherwise + Checks the implemented trading strategy for buy-signals, using the active pair whitelist. + If a pair triggers the buy_signal a new trade record gets created. + Checks pairs as long as the open trade count is below `max_open_trades`. + :return: True if at least one trade has been created. """ interval = self.strategy.ticker_interval whitelist = copy.deepcopy(self.active_pair_whitelist) @@ -276,15 +280,19 @@ class FreqtradeBot(object): logger.info("No currency pair in whitelist, but checking to sell open trades.") return False + buycount = 0 # running get_signal on historical data fetched for _pair in whitelist: + if self.strategy.is_pair_locked(_pair): + logger.info(f"Pair {_pair} is currently locked.") + continue (buy, sell) = self.strategy.get_signal( _pair, interval, self.dataprovider.ohlcv(_pair, self.strategy.ticker_interval)) - if buy and not sell: + if buy and not sell and len(Trade.get_open_trades()) < self.config['max_open_trades']: stake_amount = self._get_trade_stake_amount(_pair) if not stake_amount: - return False + continue logger.info(f"Buy signal found: about create a new trade with stake_amount: " f"{stake_amount} ...") @@ -294,12 +302,13 @@ class FreqtradeBot(object): if (bidstrat_check_depth_of_market.get('enabled', False)) and\ (bidstrat_check_depth_of_market.get('bids_to_ask_delta', 0) > 0): if self._check_depth_of_market_buy(_pair, bidstrat_check_depth_of_market): - return self.execute_buy(_pair, stake_amount) + buycount += self.execute_buy(_pair, stake_amount) else: - return False - return self.execute_buy(_pair, stake_amount) + continue - return False + buycount += self.execute_buy(_pair, stake_amount) + + return buycount > 0 def _check_depth_of_market_buy(self, pair: str, conf: Dict) -> bool: """ @@ -423,21 +432,17 @@ class FreqtradeBot(object): return True - def process_maybe_execute_buy(self) -> bool: + def process_maybe_execute_buy(self) -> None: """ Tries to execute a buy trade in a safe way :return: True if executed """ try: # Create entity and execute trade - if self.create_trade(): - return True - - logger.info('Found no buy signals for whitelisted currencies. Trying again..') - return False + if not self.create_trades(): + logger.info('Found no buy signals for whitelisted currencies. Trying again...') except DependencyException as exception: logger.warning('Unable to create trade: %s', exception) - return False def process_maybe_execute_sell(self, trade: Trade) -> bool: """ @@ -672,6 +677,9 @@ class FreqtradeBot(object): if stoploss_order and stoploss_order['status'] == 'closed': trade.sell_reason = SellType.STOPLOSS_ON_EXCHANGE.value trade.update(stoploss_order) + # Lock pair for one candle to prevent immediate rebuys + self.strategy.lock_pair(trade.pair, + timeframe_to_next_date(self.config['ticker_interval'])) self._notify_sell(trade) return True @@ -869,16 +877,23 @@ class FreqtradeBot(object): logger.exception(f"Could not cancel stoploss order {trade.stoploss_order_id}") # Execute sell and update trade record - order_id = self.exchange.sell(pair=str(trade.pair), - ordertype=self.strategy.order_types[sell_type], - amount=trade.amount, rate=limit, - time_in_force=self.strategy.order_time_in_force['sell'] - )['id'] + order = self.exchange.sell(pair=str(trade.pair), + ordertype=self.strategy.order_types[sell_type], + amount=trade.amount, rate=limit, + time_in_force=self.strategy.order_time_in_force['sell'] + ) - trade.open_order_id = order_id + trade.open_order_id = order['id'] trade.close_rate_requested = limit trade.sell_reason = sell_reason.value + # In case of market sell orders the order can be closed immediately + if order.get('status', 'unknown') == 'closed': + trade.update(order) Trade.session.flush() + + # Lock pair for one candle to prevent immediate rebuys + self.strategy.lock_pair(trade.pair, timeframe_to_next_date(self.config['ticker_interval'])) + self._notify_sell(trade) def _notify_sell(self, trade: Trade): diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 252175269..8f40a6582 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -12,7 +12,7 @@ from typing import Any, Dict, List, NamedTuple, Optional from pandas import DataFrame from freqtrade import OperationalException -from freqtrade.configuration import Arguments +from freqtrade.configuration import TimeRange from freqtrade.data import history from freqtrade.data.dataprovider import DataProvider from freqtrade.exchange import timeframe_to_minutes @@ -404,7 +404,7 @@ class Backtesting(object): logger.info('Using stake_currency: %s ...', self.config['stake_currency']) logger.info('Using stake_amount: %s ...', self.config['stake_amount']) - timerange = Arguments.parse_timerange(None if self.config.get( + timerange = TimeRange.parse_timerange(None if self.config.get( 'timerange') is None else str(self.config.get('timerange'))) data = history.load_data( datadir=Path(self.config['datadir']) if self.config.get('datadir') else None, diff --git a/freqtrade/optimize/default_hyperopt.py b/freqtrade/optimize/default_hyperopt.py index e05dfc95c..2554982ad 100644 --- a/freqtrade/optimize/default_hyperopt.py +++ b/freqtrade/optimize/default_hyperopt.py @@ -14,36 +14,48 @@ from freqtrade.optimize.hyperopt_interface import IHyperOpt class DefaultHyperOpts(IHyperOpt): """ Default hyperopt provided by the Freqtrade bot. - You can override it with your own hyperopt + You can override it with your own Hyperopt """ @staticmethod def populate_indicators(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Add several indicators needed for buy and sell strategies defined below. + """ + # ADX dataframe['adx'] = ta.ADX(dataframe) + # MACD macd = ta.MACD(dataframe) dataframe['macd'] = macd['macd'] dataframe['macdsignal'] = macd['macdsignal'] + # MFI dataframe['mfi'] = ta.MFI(dataframe) + # RSI dataframe['rsi'] = ta.RSI(dataframe) + # Stochastic Fast stoch_fast = ta.STOCHF(dataframe) dataframe['fastd'] = stoch_fast['fastd'] + # Minus-DI dataframe['minus_di'] = ta.MINUS_DI(dataframe) # Bollinger bands bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) dataframe['bb_lowerband'] = bollinger['lower'] dataframe['bb_upperband'] = bollinger['upper'] + # SAR dataframe['sar'] = ta.SAR(dataframe) + return dataframe @staticmethod def buy_strategy_generator(params: Dict[str, Any]) -> Callable: """ - Define the buy strategy parameters to be used by hyperopt + Define the buy strategy parameters to be used by Hyperopt. """ def populate_buy_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Buy strategy Hyperopt will build and use + Buy strategy Hyperopt will build and use. """ conditions = [] + # GUARDS AND TRENDS if 'mfi-enabled' in params and params['mfi-enabled']: conditions.append(dataframe['mfi'] < params['mfi-value']) @@ -79,7 +91,7 @@ class DefaultHyperOpts(IHyperOpt): @staticmethod def indicator_space() -> List[Dimension]: """ - Define your Hyperopt space for searching strategy parameters + Define your Hyperopt space for searching buy strategy parameters. """ return [ Integer(10, 25, name='mfi-value'), @@ -96,14 +108,14 @@ class DefaultHyperOpts(IHyperOpt): @staticmethod def sell_strategy_generator(params: Dict[str, Any]) -> Callable: """ - Define the sell strategy parameters to be used by hyperopt + Define the sell strategy parameters to be used by Hyperopt. """ def populate_sell_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Sell strategy Hyperopt will build and use + Sell strategy Hyperopt will build and use. """ - # print(params) conditions = [] + # GUARDS AND TRENDS if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']: conditions.append(dataframe['mfi'] > params['sell-mfi-value']) @@ -139,7 +151,7 @@ class DefaultHyperOpts(IHyperOpt): @staticmethod def sell_indicator_space() -> List[Dimension]: """ - Define your Hyperopt space for searching sell strategy parameters + Define your Hyperopt space for searching sell strategy parameters. """ return [ Integer(75, 100, name='sell-mfi-value'), @@ -157,9 +169,9 @@ class DefaultHyperOpts(IHyperOpt): def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Based on TA indicators. Should be a copy of from strategy - must align to populate_indicators in this file - Only used when --spaces does not include buy + Based on TA indicators. Should be a copy of same method from strategy. + Must align to populate_indicators in this file. + Only used when --spaces does not include buy space. """ dataframe.loc[ ( @@ -174,9 +186,9 @@ class DefaultHyperOpts(IHyperOpt): def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Based on TA indicators. Should be a copy of from strategy - must align to populate_indicators in this file - Only used when --spaces does not include sell + Based on TA indicators. Should be a copy of same method from strategy. + Must align to populate_indicators in this file. + Only used when --spaces does not include sell space. """ dataframe.loc[ ( @@ -186,4 +198,5 @@ class DefaultHyperOpts(IHyperOpt): (dataframe['fastd'] > 54) ), 'sell'] = 1 + return dataframe diff --git a/freqtrade/optimize/edge_cli.py b/freqtrade/optimize/edge_cli.py index 8d1fa381b..7e0d60843 100644 --- a/freqtrade/optimize/edge_cli.py +++ b/freqtrade/optimize/edge_cli.py @@ -9,7 +9,7 @@ from tabulate import tabulate from freqtrade import constants from freqtrade.edge import Edge -from freqtrade.configuration import Arguments +from freqtrade.configuration import TimeRange from freqtrade.exchange import Exchange from freqtrade.resolvers import StrategyResolver @@ -41,7 +41,7 @@ class EdgeCli(object): self.edge = Edge(config, self.exchange, self.strategy) self.edge._refresh_pairs = self.config.get('refresh_pairs', False) - self.timerange = Arguments.parse_timerange(None if self.config.get( + self.timerange = TimeRange.parse_timerange(None if self.config.get( 'timerange') is None else str(self.config.get('timerange'))) self.edge._timerange = self.timerange diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index 76891cbb5..cf4740f1c 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -7,17 +7,22 @@ This module contains the hyperopt logic import logging import sys +from collections import OrderedDict from operator import itemgetter from pathlib import Path from pprint import pprint from typing import Any, Dict, List, Optional +import rapidjson + +from colorama import init as colorama_init +from colorama import Fore, Style from joblib import Parallel, delayed, dump, load, wrap_non_picklable_objects, cpu_count from pandas import DataFrame from skopt import Optimizer from skopt.space import Dimension -from freqtrade.configuration import Arguments +from freqtrade.configuration import TimeRange from freqtrade.data.history import load_data, get_timeframe from freqtrade.optimize.backtesting import Backtesting # Import IHyperOptLoss to allow users import from this file @@ -136,30 +141,61 @@ class Hyperopt(Backtesting): results = sorted(self.trials, key=itemgetter('loss')) best_result = results[0] params = best_result['params'] - log_str = self.format_results_logstring(best_result) print(f"\nBest result:\n\n{log_str}\n") - if self.has_space('buy'): - print('Buy hyperspace params:') - pprint({p.name: params.get(p.name) for p in self.hyperopt_space('buy')}, - indent=4) - if self.has_space('sell'): - print('Sell hyperspace params:') - pprint({p.name: params.get(p.name) for p in self.hyperopt_space('sell')}, - indent=4) - if self.has_space('roi'): - print("ROI table:") - pprint(self.custom_hyperopt.generate_roi_table(params), indent=4) - if self.has_space('stoploss'): - print(f"Stoploss: {params.get('stoploss')}") + + if self.config.get('print_json'): + result_dict: Dict = {} + if self.has_space('buy') or self.has_space('sell'): + result_dict['params'] = {} + if self.has_space('buy'): + result_dict['params'].update({p.name: params.get(p.name) + for p in self.hyperopt_space('buy')}) + if self.has_space('sell'): + result_dict['params'].update({p.name: params.get(p.name) + for p in self.hyperopt_space('sell')}) + if self.has_space('roi'): + # Convert keys in min_roi dict to strings because + # rapidjson cannot dump dicts with integer keys... + # OrderedDict is used to keep the numeric order of the items + # in the dict. + result_dict['minimal_roi'] = OrderedDict( + (str(k), v) for k, v in self.custom_hyperopt.generate_roi_table(params).items() + ) + if self.has_space('stoploss'): + result_dict['stoploss'] = params.get('stoploss') + print(rapidjson.dumps(result_dict, default=str, number_mode=rapidjson.NM_NATIVE)) + else: + if self.has_space('buy'): + print('Buy hyperspace params:') + pprint({p.name: params.get(p.name) for p in self.hyperopt_space('buy')}, + indent=4) + if self.has_space('sell'): + print('Sell hyperspace params:') + pprint({p.name: params.get(p.name) for p in self.hyperopt_space('sell')}, + indent=4) + if self.has_space('roi'): + print("ROI table:") + pprint(self.custom_hyperopt.generate_roi_table(params), indent=4) + if self.has_space('stoploss'): + print(f"Stoploss: {params.get('stoploss')}") def log_results(self, results) -> None: """ Log results if it is better than any previous evaluation """ print_all = self.config.get('print_all', False) - if print_all or results['loss'] < self.current_best_loss: + is_best_loss = results['loss'] < self.current_best_loss + if print_all or is_best_loss: + if is_best_loss: + self.current_best_loss = results['loss'] log_str = self.format_results_logstring(results) + # Colorize output + if self.config.get('print_colorized', False): + if results['total_profit'] > 0: + log_str = Fore.GREEN + log_str + if print_all and is_best_loss: + log_str = Style.BRIGHT + log_str if print_all: print(log_str) else: @@ -174,7 +210,6 @@ class Hyperopt(Backtesting): total = self.total_epochs res = results['results_explanation'] loss = results['loss'] - self.current_best_loss = results['loss'] log_str = f'{current:5d}/{total}: {res} Objective: {loss:.5f}' log_str = f'*{log_str}' if results['is_initial_point'] else f' {log_str}' return log_str @@ -242,6 +277,7 @@ class Hyperopt(Backtesting): results_explanation = self.format_results(results) trade_count = len(results.index) + total_profit = results.profit_abs.sum() # If this evaluation contains too short amount of trades to be # interesting -- consider it as 'bad' (assigned max. loss value) @@ -252,6 +288,7 @@ class Hyperopt(Backtesting): 'loss': MAX_LOSS, 'params': params, 'results_explanation': results_explanation, + 'total_profit': total_profit, } loss = self.calculate_loss(results=results, trade_count=trade_count, @@ -261,6 +298,7 @@ class Hyperopt(Backtesting): 'loss': loss, 'params': params, 'results_explanation': results_explanation, + 'total_profit': total_profit, } def format_results(self, results: DataFrame) -> str: @@ -302,7 +340,7 @@ class Hyperopt(Backtesting): ) def start(self) -> None: - timerange = Arguments.parse_timerange(None if self.config.get( + timerange = TimeRange.parse_timerange(None if self.config.get( 'timerange') is None else str(self.config.get('timerange'))) data = load_data( datadir=Path(self.config['datadir']) if self.config.get('datadir') else None, @@ -344,6 +382,10 @@ class Hyperopt(Backtesting): logger.info(f'Number of parallel jobs set as: {config_jobs}') opt = self.get_optimizer(config_jobs) + + if self.config.get('print_colorized', False): + colorama_init(autoreset=True) + try: with Parallel(n_jobs=config_jobs) as parallel: jobs = parallel._effective_n_jobs() diff --git a/freqtrade/plot/plotting.py b/freqtrade/plot/plotting.py index e02a22a05..73af368e3 100644 --- a/freqtrade/plot/plotting.py +++ b/freqtrade/plot/plotting.py @@ -4,7 +4,7 @@ from typing import Dict, List, Optional import pandas as pd -from freqtrade.configuration import Arguments +from freqtrade.configuration import TimeRange from freqtrade.data import history from freqtrade.data.btanalysis import (combine_tickers_with_mean, create_cum_profit, load_trades) @@ -42,7 +42,7 @@ def init_plotscript(config): pairs = config["exchange"]["pair_whitelist"] # Set timerange to use - timerange = Arguments.parse_timerange(config.get("timerange")) + timerange = TimeRange.parse_timerange(config.get("timerange")) tickers = history.load_data( datadir=Path(str(config.get("datadir"))), diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index f77e0eddb..7b811cadc 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -10,7 +10,7 @@ from typing import Dict, Any, List, Optional import arrow import sqlalchemy as sql -from numpy import mean, nan_to_num, NAN +from numpy import mean, NAN from pandas import DataFrame from freqtrade import TemporaryError, DependencyException @@ -195,9 +195,9 @@ class RPC(object): trades = Trade.query.order_by(Trade.id).all() profit_all_coin = [] - profit_all_percent = [] + profit_all_perc = [] profit_closed_coin = [] - profit_closed_percent = [] + profit_closed_perc = [] durations = [] for trade in trades: @@ -211,7 +211,7 @@ class RPC(object): if not trade.is_open: profit_percent = trade.calc_profit_percent() profit_closed_coin.append(trade.calc_profit()) - profit_closed_percent.append(profit_percent) + profit_closed_perc.append(profit_percent) else: # Get current rate try: @@ -223,7 +223,7 @@ class RPC(object): profit_all_coin.append( trade.calc_profit(rate=Decimal(trade.close_rate or current_rate)) ) - profit_all_percent.append(profit_percent) + profit_all_perc.append(profit_percent) best_pair = Trade.session.query( Trade.pair, sql.func.sum(Trade.close_profit).label('profit_sum') @@ -238,7 +238,8 @@ class RPC(object): # Prepare data to display profit_closed_coin_sum = round(sum(profit_closed_coin), 8) - profit_closed_percent = round(nan_to_num(mean(profit_closed_percent)) * 100, 2) + profit_closed_percent = (round(mean(profit_closed_perc) * 100, 2) if profit_closed_perc + else 0.0) profit_closed_fiat = self._fiat_converter.convert_amount( profit_closed_coin_sum, stake_currency, @@ -246,7 +247,7 @@ class RPC(object): ) if self._fiat_converter else 0 profit_all_coin_sum = round(sum(profit_all_coin), 8) - profit_all_percent = round(nan_to_num(mean(profit_all_percent)) * 100, 2) + profit_all_percent = round(mean(profit_all_perc) * 100, 2) if profit_all_perc else 0.0 profit_all_fiat = self._fiat_converter.convert_amount( profit_all_coin_sum, stake_currency, diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 37aa97bb1..99f5f26de 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -4,7 +4,7 @@ This module defines the interface to apply for strategies """ import logging from abc import ABC, abstractmethod -from datetime import datetime +from datetime import datetime, timezone from enum import Enum from typing import Dict, List, NamedTuple, Optional, Tuple import warnings @@ -107,6 +107,7 @@ class IStrategy(ABC): self.config = config # Dict to determine if analysis is necessary self._last_candle_seen_per_pair: Dict[str, datetime] = {} + self._pair_locked_until: Dict[str, datetime] = {} @abstractmethod def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: @@ -154,6 +155,24 @@ class IStrategy(ABC): """ return self.__class__.__name__ + def lock_pair(self, pair: str, until: datetime) -> None: + """ + Locks pair until a given timestamp happens. + Locked pairs are not analyzed, and are prevented from opening new trades. + :param pair: Pair to lock + :param until: datetime in UTC until the pair should be blocked from opening new trades. + Needs to be timezone aware `datetime.now(timezone.utc)` + """ + self._pair_locked_until[pair] = until + + def is_pair_locked(self, pair: str) -> bool: + """ + Checks if a pair is currently locked + """ + if pair not in self._pair_locked_until: + return False + return self._pair_locked_until[pair] >= datetime.now(timezone.utc) + def analyze_ticker(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Parses the given ticker history and returns a populated DataFrame @@ -260,8 +279,8 @@ class IStrategy(ABC): sell: bool, low: float = None, high: float = None, force_stoploss: float = 0) -> SellCheckTuple: """ - This function evaluate if on the condition required to trigger a sell has been reached - if the threshold is reached and updates the trade record. + This function evaluates if one of the conditions required to trigger a sell + has been reached, which can either be a stop-loss, ROI or sell-signal. :param low: Only used during backtesting to simulate stoploss :param high: Only used during backtesting, to simulate ROI :param force_stoploss: Externally provided stoploss diff --git a/freqtrade/tests/config_test_comments.json b/freqtrade/tests/config_test_comments.json new file mode 100644 index 000000000..85becc3e8 --- /dev/null +++ b/freqtrade/tests/config_test_comments.json @@ -0,0 +1,133 @@ +{ + /* Single-line C-style comment */ + "max_open_trades": 3, + /* + * Multi-line C-style comment + */ + "stake_currency": "BTC", + "stake_amount": 0.05, + "fiat_display_currency": "USD", // C++-style comment + "amount_reserve_percent" : 0.05, // And more, tabs before this comment + "dry_run": false, + "ticker_interval": "5m", + "trailing_stop": false, + "trailing_stop_positive": 0.005, + "trailing_stop_positive_offset": 0.0051, + "trailing_only_offset_is_reached": false, + "minimal_roi": { + "40": 0.0, + "30": 0.01, + "20": 0.02, + "0": 0.04 + }, + "stoploss": -0.10, + "unfilledtimeout": { + "buy": 10, + "sell": 30, // Trailing comma should also be accepted now + }, + "bid_strategy": { + "use_order_book": false, + "ask_last_balance": 0.0, + "order_book_top": 1, + "check_depth_of_market": { + "enabled": false, + "bids_to_ask_delta": 1 + } + }, + "ask_strategy":{ + "use_order_book": false, + "order_book_min": 1, + "order_book_max": 9 + }, + "order_types": { + "buy": "limit", + "sell": "limit", + "stoploss": "market", + "stoploss_on_exchange": false, + "stoploss_on_exchange_interval": 60 + }, + "order_time_in_force": { + "buy": "gtc", + "sell": "gtc" + }, + "pairlist": { + "method": "VolumePairList", + "config": { + "number_assets": 20, + "sort_key": "quoteVolume", + "precision_filter": false + } + }, + "exchange": { + "name": "bittrex", + "sandbox": false, + "key": "your_exchange_key", + "secret": "your_exchange_secret", + "password": "", + "ccxt_config": {"enableRateLimit": true}, + "ccxt_async_config": { + "enableRateLimit": false, + "rateLimit": 500, + "aiohttp_trust_env": false + }, + "pair_whitelist": [ + "ETH/BTC", + "LTC/BTC", + "ETC/BTC", + "DASH/BTC", + "ZEC/BTC", + "XLM/BTC", + "NXT/BTC", + "POWR/BTC", + "ADA/BTC", + "XMR/BTC" + ], + "pair_blacklist": [ + "DOGE/BTC" + ], + "outdated_offset": 5, + "markets_refresh_interval": 60 + }, + "edge": { + "enabled": false, + "process_throttle_secs": 3600, + "calculate_since_number_of_days": 7, + "capital_available_percentage": 0.5, + "allowed_risk": 0.01, + "stoploss_range_min": -0.01, + "stoploss_range_max": -0.1, + "stoploss_range_step": -0.01, + "minimum_winrate": 0.60, + "minimum_expectancy": 0.20, + "min_trade_number": 10, + "max_trade_duration_minute": 1440, + "remove_pumps": false + }, + "experimental": { + "use_sell_signal": false, + "sell_profit_only": false, + "ignore_roi_if_buy_signal": false + }, + "telegram": { +// We can now comment out some settings +// "enabled": true, + "enabled": false, + "token": "your_telegram_token", + "chat_id": "your_telegram_chat_id" + }, + "api_server": { + "enabled": false, + "listen_ip_address": "127.0.0.1", + "listen_port": 8080, + "username": "freqtrader", + "password": "SuperSecurePassword" + }, + "db_url": "sqlite:///tradesv3.sqlite", + "initial_state": "running", + "forcebuy_enable": false, + "internals": { + "process_throttle_secs": 5 + }, + "strategy": "DefaultStrategy", + "strategy_path": "user_data/strategies/" +} diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index 6e3f40f54..654b959ca 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -10,6 +10,7 @@ from unittest.mock import MagicMock, PropertyMock import arrow import pytest +import numpy as np from telegram import Chat, Message, Update from freqtrade import constants, persistence @@ -25,17 +26,21 @@ from freqtrade.worker import Worker logging.getLogger('').setLevel(logging.INFO) +# Do not mask numpy errors as warnings that no one read, raise the exсeption +np.seterr(all='raise') + + def log_has(line, logs): # caplog mocker returns log as a tuple: ('freqtrade.something', logging.WARNING, 'foobar') # and we want to match line against foobar in the tuple return reduce(lambda a, b: a or b, - filter(lambda x: x[2] == line, logs), + filter(lambda x: x[2] == line, logs.record_tuples), False) def log_has_re(line, logs): return reduce(lambda a, b: a or b, - filter(lambda x: re.match(line, x[2]), logs), + filter(lambda x: re.match(line, x[2]), logs.record_tuples), False) diff --git a/freqtrade/tests/data/test_btanalysis.py b/freqtrade/tests/data/test_btanalysis.py index bad8db66f..cf8cae566 100644 --- a/freqtrade/tests/data/test_btanalysis.py +++ b/freqtrade/tests/data/test_btanalysis.py @@ -4,7 +4,7 @@ import pytest from arrow import Arrow from pandas import DataFrame, to_datetime -from freqtrade.configuration import Arguments, TimeRange +from freqtrade.configuration import TimeRange from freqtrade.data.btanalysis import (BT_DATA_COLUMNS, combine_tickers_with_mean, create_cum_profit, @@ -121,7 +121,7 @@ def test_combine_tickers_with_mean(): def test_create_cum_profit(): filename = make_testdata_path(None) / "backtest-result_test.json" bt_data = load_backtest_data(filename) - timerange = Arguments.parse_timerange("20180110-20180112") + timerange = TimeRange.parse_timerange("20180110-20180112") df = load_pair_history(pair="POWR/BTC", ticker_interval='5m', datadir=None, timerange=timerange) diff --git a/freqtrade/tests/data/test_converter.py b/freqtrade/tests/data/test_converter.py index f68224e0e..39462bdd8 100644 --- a/freqtrade/tests/data/test_converter.py +++ b/freqtrade/tests/data/test_converter.py @@ -18,7 +18,7 @@ def test_parse_ticker_dataframe(ticker_history_list, caplog): dataframe = parse_ticker_dataframe(ticker_history_list, '5m', pair="UNITTEST/BTC", fill_missing=True) assert dataframe.columns.tolist() == columns - assert log_has('Parsing tickerlist to dataframe', caplog.record_tuples) + assert log_has('Parsing tickerlist to dataframe', caplog) def test_ohlcv_fill_up_missing_data(caplog): @@ -34,8 +34,7 @@ def test_ohlcv_fill_up_missing_data(caplog): assert (data.columns == data2.columns).all() assert log_has(f"Missing data fillup for UNITTEST/BTC: before: " - f"{len(data)} - after: {len(data2)}", - caplog.record_tuples) + f"{len(data)} - after: {len(data2)}", caplog) # Test fillup actually fixes invalid backtest data min_date, max_date = get_timeframe({'UNITTEST/BTC': data}) @@ -97,8 +96,7 @@ def test_ohlcv_fill_up_missing_data2(caplog): assert (data.columns == data2.columns).all() assert log_has(f"Missing data fillup for UNITTEST/BTC: before: " - f"{len(data)} - after: {len(data2)}", - caplog.record_tuples) + f"{len(data)} - after: {len(data2)}", caplog) def test_ohlcv_drop_incomplete(caplog): @@ -140,11 +138,11 @@ def test_ohlcv_drop_incomplete(caplog): data = parse_ticker_dataframe(ticks, ticker_interval, pair="UNITTEST/BTC", fill_missing=False, drop_incomplete=False) assert len(data) == 4 - assert not log_has("Dropping last candle", caplog.record_tuples) + assert not log_has("Dropping last candle", caplog) # Drop last candle data = parse_ticker_dataframe(ticks, ticker_interval, pair="UNITTEST/BTC", fill_missing=False, drop_incomplete=True) assert len(data) == 3 - assert log_has("Dropping last candle", caplog.record_tuples) + assert log_has("Dropping last candle", caplog) diff --git a/freqtrade/tests/data/test_dataprovider.py b/freqtrade/tests/data/test_dataprovider.py index 993f0b59b..2272f69a3 100644 --- a/freqtrade/tests/data/test_dataprovider.py +++ b/freqtrade/tests/data/test_dataprovider.py @@ -13,6 +13,7 @@ def test_ohlcv(mocker, default_conf, ticker_history): exchange = get_patched_exchange(mocker, default_conf) exchange._klines[("XRP/BTC", ticker_interval)] = ticker_history exchange._klines[("UNITTEST/BTC", ticker_interval)] = ticker_history + dp = DataProvider(default_conf, exchange) assert dp.runmode == RunMode.DRY_RUN assert ticker_history.equals(dp.ohlcv("UNITTEST/BTC", ticker_interval)) @@ -37,11 +38,9 @@ def test_ohlcv(mocker, default_conf, ticker_history): def test_historic_ohlcv(mocker, default_conf, ticker_history): - historymock = MagicMock(return_value=ticker_history) mocker.patch("freqtrade.data.dataprovider.load_pair_history", historymock) - # exchange = get_patched_exchange(mocker, default_conf) dp = DataProvider(default_conf, None) data = dp.historic_ohlcv("UNITTEST/BTC", "5m") assert isinstance(data, DataFrame) @@ -51,14 +50,47 @@ def test_historic_ohlcv(mocker, default_conf, ticker_history): assert historymock.call_args_list[0][1]["ticker_interval"] == "5m" +def test_get_pair_dataframe(mocker, default_conf, ticker_history): + default_conf["runmode"] = RunMode.DRY_RUN + ticker_interval = default_conf["ticker_interval"] + exchange = get_patched_exchange(mocker, default_conf) + exchange._klines[("XRP/BTC", ticker_interval)] = ticker_history + exchange._klines[("UNITTEST/BTC", ticker_interval)] = ticker_history + + dp = DataProvider(default_conf, exchange) + assert dp.runmode == RunMode.DRY_RUN + assert ticker_history.equals(dp.get_pair_dataframe("UNITTEST/BTC", ticker_interval)) + assert isinstance(dp.get_pair_dataframe("UNITTEST/BTC", ticker_interval), DataFrame) + assert dp.get_pair_dataframe("UNITTEST/BTC", ticker_interval) is not ticker_history + assert not dp.get_pair_dataframe("UNITTEST/BTC", ticker_interval).empty + assert dp.get_pair_dataframe("NONESENSE/AAA", ticker_interval).empty + + # Test with and without parameter + assert dp.get_pair_dataframe("UNITTEST/BTC", + ticker_interval).equals(dp.get_pair_dataframe("UNITTEST/BTC")) + + default_conf["runmode"] = RunMode.LIVE + dp = DataProvider(default_conf, exchange) + assert dp.runmode == RunMode.LIVE + assert isinstance(dp.get_pair_dataframe("UNITTEST/BTC", ticker_interval), DataFrame) + assert dp.get_pair_dataframe("NONESENSE/AAA", ticker_interval).empty + + historymock = MagicMock(return_value=ticker_history) + mocker.patch("freqtrade.data.dataprovider.load_pair_history", historymock) + default_conf["runmode"] = RunMode.BACKTEST + dp = DataProvider(default_conf, exchange) + assert dp.runmode == RunMode.BACKTEST + assert isinstance(dp.get_pair_dataframe("UNITTEST/BTC", ticker_interval), DataFrame) + # assert dp.get_pair_dataframe("NONESENSE/AAA", ticker_interval).empty + + def test_available_pairs(mocker, default_conf, ticker_history): exchange = get_patched_exchange(mocker, default_conf) - ticker_interval = default_conf["ticker_interval"] exchange._klines[("XRP/BTC", ticker_interval)] = ticker_history exchange._klines[("UNITTEST/BTC", ticker_interval)] = ticker_history - dp = DataProvider(default_conf, exchange) + dp = DataProvider(default_conf, exchange) assert len(dp.available_pairs) == 2 assert dp.available_pairs == [ ("XRP/BTC", ticker_interval), diff --git a/freqtrade/tests/data/test_history.py b/freqtrade/tests/data/test_history.py index 424333e99..4ba65e470 100644 --- a/freqtrade/tests/data/test_history.py +++ b/freqtrade/tests/data/test_history.py @@ -64,8 +64,7 @@ def test_load_data_30min_ticker(mocker, caplog, default_conf) -> None: assert isinstance(ld, DataFrame) assert not log_has( 'Download history data for pair: "UNITTEST/BTC", interval: 30m ' - 'and store in None.', - caplog.record_tuples + 'and store in None.', caplog ) @@ -76,21 +75,19 @@ def test_load_data_7min_ticker(mocker, caplog, default_conf) -> None: assert log_has( 'No history data for pair: "UNITTEST/BTC", interval: 7m. ' 'Use --refresh-pairs-cached option or download_backtest_data.py ' - 'script to download the data', - caplog.record_tuples + 'script to download the data', caplog ) def test_load_data_1min_ticker(ticker_history, mocker, caplog) -> None: - mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history) + mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ticker_history) file = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'UNITTEST_BTC-1m.json') _backup_file(file, copy_file=True) history.load_data(datadir=None, ticker_interval='1m', pairs=['UNITTEST/BTC']) assert os.path.isfile(file) is True assert not log_has( 'Download history data for pair: "UNITTEST/BTC", interval: 1m ' - 'and store in None.', - caplog.record_tuples + 'and store in None.', caplog ) _clean_test_file(file) @@ -99,7 +96,7 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, defau """ Test load_pair_history() with 1 min ticker """ - mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history_list) + mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ticker_history_list) exchange = get_patched_exchange(mocker, default_conf) file = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-1m.json') @@ -113,8 +110,7 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, defau assert log_has( 'No history data for pair: "MEME/BTC", interval: 1m. ' 'Use --refresh-pairs-cached option or download_backtest_data.py ' - 'script to download the data', - caplog.record_tuples + 'script to download the data', caplog ) # download a new pair if refresh_pairs is set @@ -126,8 +122,7 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, defau assert os.path.isfile(file) is True assert log_has( 'Download history data for pair: "MEME/BTC", interval: 1m ' - 'and store in None.', - caplog.record_tuples + 'and store in None.', caplog ) with pytest.raises(OperationalException, match=r'Exchange needs to be initialized when.*'): history.load_pair_history(datadir=None, @@ -149,7 +144,7 @@ def test_load_data_live(default_conf, mocker, caplog) -> None: exchange=exchange) assert refresh_mock.call_count == 1 assert len(refresh_mock.call_args_list[0][0][0]) == 2 - assert log_has('Live: Downloading data for all defined pairs ...', caplog.record_tuples) + assert log_has('Live: Downloading data for all defined pairs ...', caplog) def test_load_data_live_noexchange(default_conf, mocker, caplog) -> None: @@ -271,7 +266,7 @@ def test_load_cached_data_for_updating(mocker) -> None: def test_download_pair_history(ticker_history_list, mocker, default_conf) -> None: - mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history_list) + mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ticker_history_list) exchange = get_patched_exchange(mocker, default_conf) file1_1 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-1m.json') file1_5 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-5m.json') @@ -324,7 +319,7 @@ def test_download_pair_history2(mocker, default_conf) -> None: [1509836580000, 0.00161, 0.00161, 0.00161, 0.00161, 82.390199] ] json_dump_mock = mocker.patch('freqtrade.misc.file_dump_json', return_value=None) - mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=tick) + mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=tick) exchange = get_patched_exchange(mocker, default_conf) download_pair_history(None, exchange, pair="UNITTEST/BTC", ticker_interval='1m') download_pair_history(None, exchange, pair="UNITTEST/BTC", ticker_interval='3m') @@ -332,7 +327,7 @@ def test_download_pair_history2(mocker, default_conf) -> None: def test_download_backtesting_data_exception(ticker_history, mocker, caplog, default_conf) -> None: - mocker.patch('freqtrade.exchange.Exchange.get_history', + mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', side_effect=Exception('File Error')) exchange = get_patched_exchange(mocker, default_conf) @@ -350,8 +345,7 @@ def test_download_backtesting_data_exception(ticker_history, mocker, caplog, def _clean_test_file(file1_5) assert log_has( 'Failed to download history data for pair: "MEME/BTC", interval: 1m. ' - 'Error: File Error', - caplog.record_tuples + 'Error: File Error', caplog ) @@ -380,7 +374,7 @@ def test_load_partial_missing(caplog) -> None: start_real = tickerdata['UNITTEST/BTC'].iloc[0, 0] assert log_has(f'Missing data at start for pair ' f'UNITTEST/BTC, data starts at {start_real.strftime("%Y-%m-%d %H:%M:%S")}', - caplog.record_tuples) + caplog) # Make sure we start fresh - test missing data at end caplog.clear() start = arrow.get('2018-01-10T00:00:00') @@ -396,7 +390,7 @@ def test_load_partial_missing(caplog) -> None: end_real = arrow.get(tickerdata['UNITTEST/BTC'].iloc[-1, 0]).shift(minutes=5) assert log_has(f'Missing data at end for pair ' f'UNITTEST/BTC, data ends at {end_real.strftime("%Y-%m-%d %H:%M:%S")}', - caplog.record_tuples) + caplog) def test_init(default_conf, mocker) -> None: @@ -560,7 +554,7 @@ def test_validate_backtest_data_warn(default_conf, mocker, caplog) -> None: assert len(caplog.record_tuples) == 1 assert log_has( "UNITTEST/BTC has missing frames: expected 14396, got 13680, that's 716 missing values", - caplog.record_tuples) + caplog) def test_validate_backtest_data(default_conf, mocker, caplog) -> None: diff --git a/freqtrade/tests/edge/test_edge.py b/freqtrade/tests/edge/test_edge.py index 45b8e609e..09fa1d93e 100644 --- a/freqtrade/tests/edge/test_edge.py +++ b/freqtrade/tests/edge/test_edge.py @@ -311,7 +311,7 @@ def test_edge_process_no_data(mocker, edge_conf, caplog): assert not edge.calculate() assert len(edge._cached_pairs) == 0 - assert log_has("No data found. Edge is stopped ...", caplog.record_tuples) + assert log_has("No data found. Edge is stopped ...", caplog) assert edge._last_updated == 0 @@ -326,7 +326,7 @@ def test_edge_process_no_trades(mocker, edge_conf, caplog): assert not edge.calculate() assert len(edge._cached_pairs) == 0 - assert log_has("No trades found.", caplog.record_tuples) + assert log_has("No trades found.", caplog) def test_edge_init_error(mocker, edge_conf,): diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index ebe5ad9df..6b833054d 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -14,7 +14,11 @@ from pandas import DataFrame from freqtrade import (DependencyException, InvalidOrderException, OperationalException, TemporaryError) from freqtrade.exchange import Binance, Exchange, Kraken -from freqtrade.exchange.exchange import API_RETRY_COUNT +from freqtrade.exchange.exchange import (API_RETRY_COUNT, timeframe_to_minutes, + timeframe_to_msecs, + timeframe_to_next_date, + timeframe_to_prev_date, + timeframe_to_seconds) from freqtrade.resolvers.exchange_resolver import ExchangeResolver from freqtrade.tests.conftest import get_patched_exchange, log_has, log_has_re @@ -62,7 +66,7 @@ async def async_ccxt_exception(mocker, default_conf, api_mock, fun, mock_ccxt_fu def test_init(default_conf, mocker, caplog): caplog.set_level(logging.INFO) get_patched_exchange(mocker, default_conf) - assert log_has('Instance is running with dry_run enabled', caplog.record_tuples) + assert log_has('Instance is running with dry_run enabled', caplog) def test_init_ccxt_kwargs(default_conf, mocker, caplog): @@ -71,8 +75,7 @@ def test_init_ccxt_kwargs(default_conf, mocker, caplog): conf = copy.deepcopy(default_conf) conf['exchange']['ccxt_async_config'] = {'aiohttp_trust_env': True} ex = Exchange(conf) - assert log_has("Applying additional ccxt config: {'aiohttp_trust_env': True}", - caplog.record_tuples) + assert log_has("Applying additional ccxt config: {'aiohttp_trust_env': True}", caplog) assert ex._api_async.aiohttp_trust_env assert not ex._api.aiohttp_trust_env @@ -81,20 +84,18 @@ def test_init_ccxt_kwargs(default_conf, mocker, caplog): conf = copy.deepcopy(default_conf) conf['exchange']['ccxt_config'] = {'TestKWARG': 11} ex = Exchange(conf) - assert not log_has("Applying additional ccxt config: {'aiohttp_trust_env': True}", - caplog.record_tuples) + assert not log_has("Applying additional ccxt config: {'aiohttp_trust_env': True}", caplog) assert not ex._api_async.aiohttp_trust_env assert hasattr(ex._api, 'TestKWARG') assert ex._api.TestKWARG == 11 assert not hasattr(ex._api_async, 'TestKWARG') - assert log_has("Applying additional ccxt config: {'TestKWARG': 11}", - caplog.record_tuples) + assert log_has("Applying additional ccxt config: {'TestKWARG': 11}", caplog) def test_destroy(default_conf, mocker, caplog): caplog.set_level(logging.DEBUG) get_patched_exchange(mocker, default_conf) - assert log_has('Exchange object destroyed, closing async loop', caplog.record_tuples) + assert log_has('Exchange object destroyed, closing async loop', caplog) def test_init_exception(default_conf, mocker): @@ -120,8 +121,7 @@ def test_exchange_resolver(default_conf, mocker, caplog): mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock()) exchange = ExchangeResolver('Bittrex', default_conf).exchange assert isinstance(exchange, Exchange) - assert log_has_re(r"No .* specific subclass found. Using the generic class instead.", - caplog.record_tuples) + assert log_has_re(r"No .* specific subclass found. Using the generic class instead.", caplog) caplog.clear() exchange = ExchangeResolver('kraken', default_conf).exchange @@ -129,7 +129,7 @@ def test_exchange_resolver(default_conf, mocker, caplog): assert isinstance(exchange, Kraken) assert not isinstance(exchange, Binance) assert not log_has_re(r"No .* specific subclass found. Using the generic class instead.", - caplog.record_tuples) + caplog) exchange = ExchangeResolver('binance', default_conf).exchange assert isinstance(exchange, Exchange) @@ -137,7 +137,7 @@ def test_exchange_resolver(default_conf, mocker, caplog): assert not isinstance(exchange, Kraken) assert not log_has_re(r"No .* specific subclass found. Using the generic class instead.", - caplog.record_tuples) + caplog) def test_validate_order_time_in_force(default_conf, mocker, caplog): @@ -249,8 +249,7 @@ def test__load_async_markets(default_conf, mocker, caplog): exchange._api_async.load_markets = Mock(side_effect=ccxt.BaseError("deadbeef")) exchange._load_async_markets() - assert log_has('Could not load async markets. Reason: deadbeef', - caplog.record_tuples) + assert log_has('Could not load async markets. Reason: deadbeef', caplog) def test__load_markets(default_conf, mocker, caplog): @@ -262,7 +261,7 @@ def test__load_markets(default_conf, mocker, caplog): mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock()) mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock()) Exchange(default_conf) - assert log_has('Unable to initialize markets. Reason: SomeError', caplog.record_tuples) + assert log_has('Unable to initialize markets. Reason: SomeError', caplog) expected_return = {'ETH/BTC': 'available'} api_mock = MagicMock() @@ -298,7 +297,7 @@ def test__reload_markets(default_conf, mocker, caplog): exchange._last_markets_refresh = arrow.utcnow().timestamp - 15 * 60 exchange._reload_markets() assert exchange.markets == updated_markets - assert log_has('Performing scheduled market reload..', caplog.record_tuples) + assert log_has('Performing scheduled market reload..', caplog) def test__reload_markets_exception(default_conf, mocker, caplog): @@ -312,7 +311,7 @@ def test__reload_markets_exception(default_conf, mocker, caplog): # less than 10 minutes have passed, no reload exchange._reload_markets() assert exchange._last_markets_refresh == 0 - assert log_has_re(r"Could not reload markets.*", caplog.record_tuples) + assert log_has_re(r"Could not reload markets.*", caplog) def test_validate_pairs(default_conf, mocker): # test exchange.validate_pairs directly @@ -357,8 +356,7 @@ def test_validate_pairs_exception(default_conf, mocker, caplog): mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value={})) Exchange(default_conf) - assert log_has('Unable to validate pairs (assuming they are correct).', - caplog.record_tuples) + assert log_has('Unable to validate pairs (assuming they are correct).', caplog) def test_validate_pairs_restricted(default_conf, mocker, caplog): @@ -374,8 +372,7 @@ def test_validate_pairs_restricted(default_conf, mocker, caplog): Exchange(default_conf) assert log_has(f"Pair XRP/BTC is restricted for some users on this exchange." f"Please check if you are impacted by this restriction " - f"on the exchange and eventually remove XRP/BTC from your whitelist.", - caplog.record_tuples) + f"on the exchange and eventually remove XRP/BTC from your whitelist.", caplog) def test_validate_timeframes(default_conf, mocker): @@ -1003,7 +1000,7 @@ def test_get_ticker(default_conf, mocker, exchange_name): @pytest.mark.parametrize("exchange_name", EXCHANGES) -def test_get_history(default_conf, mocker, caplog, exchange_name): +def test_get_historic_ohlcv(default_conf, mocker, caplog, exchange_name): exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) tick = [ [ @@ -1024,7 +1021,7 @@ def test_get_history(default_conf, mocker, caplog, exchange_name): # one_call calculation * 1.8 should do 2 calls since = 5 * 60 * 500 * 1.8 print(f"since = {since}") - ret = exchange.get_history(pair, "5m", int((arrow.utcnow().timestamp - since) * 1000)) + ret = exchange.get_historic_ohlcv(pair, "5m", int((arrow.utcnow().timestamp - since) * 1000)) assert exchange._async_get_candle_history.call_count == 2 # Returns twice the above tick @@ -1060,7 +1057,7 @@ def test_refresh_latest_ohlcv(mocker, default_conf, caplog) -> None: assert not exchange._klines exchange.refresh_latest_ohlcv(pairs) - assert log_has(f'Refreshing ohlcv data for {len(pairs)} pairs', caplog.record_tuples) + assert log_has(f'Refreshing ohlcv data for {len(pairs)} pairs', caplog) assert exchange._klines assert exchange._api_async.fetch_ohlcv.call_count == 2 for pair in pairs: @@ -1079,7 +1076,7 @@ def test_refresh_latest_ohlcv(mocker, default_conf, caplog) -> None: assert exchange._api_async.fetch_ohlcv.call_count == 2 assert log_has(f"Using cached ohlcv data for pair {pairs[0][0]}, interval {pairs[0][1]} ...", - caplog.record_tuples) + caplog) @pytest.mark.asyncio @@ -1109,7 +1106,7 @@ async def test__async_get_candle_history(default_conf, mocker, caplog, exchange_ assert res[1] == "5m" assert res[2] == tick assert exchange._api_async.fetch_ohlcv.call_count == 1 - assert not log_has(f"Using cached ohlcv data for {pair} ...", caplog.record_tuples) + assert not log_has(f"Using cached ohlcv data for {pair} ...", caplog) # exchange = Exchange(default_conf) await async_ccxt_exception(mocker, default_conf, MagicMock(), @@ -1168,8 +1165,8 @@ def test_refresh_latest_ohlcv_inv_result(default_conf, mocker, caplog): # Test that each is in list at least once as order is not guaranteed assert type(res[0]) is tuple or type(res[1]) is tuple assert type(res[0]) is TypeError or type(res[1]) is TypeError - assert log_has("Error loading ETH/BTC. Result was [[]].", caplog.record_tuples) - assert log_has("Async code raised an exception: TypeError", caplog.record_tuples) + assert log_has("Error loading ETH/BTC. Result was [[]].", caplog) + assert log_has("Async code raised an exception: TypeError", caplog) @pytest.mark.parametrize("exchange_name", EXCHANGES) @@ -1547,3 +1544,74 @@ def test_get_valid_pair_combination(default_conf, mocker, markets): assert ex.get_valid_pair_combination("BTC", "ETH") == "ETH/BTC" with pytest.raises(DependencyException, match=r"Could not combine.* to get a valid pair."): ex.get_valid_pair_combination("NOPAIR", "ETH") + + +def test_timeframe_to_minutes(): + assert timeframe_to_minutes("5m") == 5 + assert timeframe_to_minutes("10m") == 10 + assert timeframe_to_minutes("1h") == 60 + assert timeframe_to_minutes("1d") == 1440 + + +def test_timeframe_to_seconds(): + assert timeframe_to_seconds("5m") == 300 + assert timeframe_to_seconds("10m") == 600 + assert timeframe_to_seconds("1h") == 3600 + assert timeframe_to_seconds("1d") == 86400 + + +def test_timeframe_to_msecs(): + assert timeframe_to_msecs("5m") == 300000 + assert timeframe_to_msecs("10m") == 600000 + assert timeframe_to_msecs("1h") == 3600000 + assert timeframe_to_msecs("1d") == 86400000 + + +def test_timeframe_to_prev_date(): + # 2019-08-12 13:22:08 + date = datetime.fromtimestamp(1565616128, tz=timezone.utc) + + tf_list = [ + # 5m -> 2019-08-12 13:20:00 + ("5m", datetime(2019, 8, 12, 13, 20, 0, tzinfo=timezone.utc)), + # 10m -> 2019-08-12 13:20:00 + ("10m", datetime(2019, 8, 12, 13, 20, 0, tzinfo=timezone.utc)), + # 1h -> 2019-08-12 13:00:00 + ("1h", datetime(2019, 8, 12, 13, 00, 0, tzinfo=timezone.utc)), + # 2h -> 2019-08-12 12:00:00 + ("2h", datetime(2019, 8, 12, 12, 00, 0, tzinfo=timezone.utc)), + # 4h -> 2019-08-12 12:00:00 + ("4h", datetime(2019, 8, 12, 12, 00, 0, tzinfo=timezone.utc)), + # 1d -> 2019-08-12 00:00:00 + ("1d", datetime(2019, 8, 12, 00, 00, 0, tzinfo=timezone.utc)), + ] + for interval, result in tf_list: + assert timeframe_to_prev_date(interval, date) == result + + date = datetime.now(tz=timezone.utc) + assert timeframe_to_prev_date("5m", date) < date + + +def test_timeframe_to_next_date(): + # 2019-08-12 13:22:08 + date = datetime.fromtimestamp(1565616128, tz=timezone.utc) + tf_list = [ + # 5m -> 2019-08-12 13:25:00 + ("5m", datetime(2019, 8, 12, 13, 25, 0, tzinfo=timezone.utc)), + # 10m -> 2019-08-12 13:30:00 + ("10m", datetime(2019, 8, 12, 13, 30, 0, tzinfo=timezone.utc)), + # 1h -> 2019-08-12 14:00:00 + ("1h", datetime(2019, 8, 12, 14, 00, 0, tzinfo=timezone.utc)), + # 2h -> 2019-08-12 14:00:00 + ("2h", datetime(2019, 8, 12, 14, 00, 0, tzinfo=timezone.utc)), + # 4h -> 2019-08-12 14:00:00 + ("4h", datetime(2019, 8, 12, 16, 00, 0, tzinfo=timezone.utc)), + # 1d -> 2019-08-13 00:00:00 + ("1d", datetime(2019, 8, 13, 0, 0, 0, tzinfo=timezone.utc)), + ] + + for interval, result in tf_list: + assert timeframe_to_next_date(interval, date) == result + + date = datetime.now(tz=timezone.utc) + assert timeframe_to_next_date("5m", date) > date diff --git a/freqtrade/tests/optimize/test_backtesting.py b/freqtrade/tests/optimize/test_backtesting.py index 71d460621..9ed7e7296 100644 --- a/freqtrade/tests/optimize/test_backtesting.py +++ b/freqtrade/tests/optimize/test_backtesting.py @@ -181,21 +181,18 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> assert 'exchange' in config assert 'pair_whitelist' in config['exchange'] assert 'datadir' in config - assert log_has( - 'Using data directory: {} ...'.format(config['datadir']), - caplog.record_tuples - ) + assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog) assert 'ticker_interval' in config - assert not log_has_re('Parameter -i/--ticker-interval detected .*', caplog.record_tuples) + assert not log_has_re('Parameter -i/--ticker-interval detected .*', caplog) assert 'live' not in config - assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples) + assert not log_has('Parameter -l/--live detected ...', caplog) assert 'position_stacking' not in config - assert not log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples) + assert not log_has('Parameter --enable-position-stacking detected ...', caplog) assert 'refresh_pairs' not in config - assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples) + assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog) assert 'timerange' not in config assert 'export' not in config @@ -235,43 +232,31 @@ def test_setup_bt_configuration_with_arguments(mocker, default_conf, caplog) -> assert 'datadir' in config assert config['runmode'] == RunMode.BACKTEST - assert log_has( - 'Using data directory: {} ...'.format(config['datadir']), - caplog.record_tuples - ) + assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog) assert 'ticker_interval' in config assert log_has('Parameter -i/--ticker-interval detected ... Using ticker_interval: 1m ...', - caplog.record_tuples) + caplog) assert 'live' in config - assert log_has('Parameter -l/--live detected ...', caplog.record_tuples) + assert log_has('Parameter -l/--live detected ...', caplog) assert 'position_stacking' in config - assert log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples) + assert log_has('Parameter --enable-position-stacking detected ...', caplog) assert 'use_max_market_positions' in config - assert log_has('Parameter --disable-max-market-positions detected ...', caplog.record_tuples) - assert log_has('max_open_trades set to unlimited ...', caplog.record_tuples) + assert log_has('Parameter --disable-max-market-positions detected ...', caplog) + assert log_has('max_open_trades set to unlimited ...', caplog) assert 'refresh_pairs' in config - assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples) + assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog) assert 'timerange' in config - assert log_has( - 'Parameter --timerange detected: {} ...'.format(config['timerange']), - caplog.record_tuples - ) + assert log_has('Parameter --timerange detected: {} ...'.format(config['timerange']), caplog) assert 'export' in config - assert log_has( - 'Parameter --export detected: {} ...'.format(config['export']), - caplog.record_tuples - ) + assert log_has('Parameter --export detected: {} ...'.format(config['export']), caplog) assert 'exportfilename' in config - assert log_has( - 'Storing backtest results to {} ...'.format(config['exportfilename']), - caplog.record_tuples - ) + assert log_has('Storing backtest results to {} ...'.format(config['exportfilename']), caplog) def test_setup_configuration_unlimited_stake_amount(mocker, default_conf, caplog) -> None: @@ -303,10 +288,7 @@ def test_start(mocker, fee, default_conf, caplog) -> None: ] args = get_args(args) start_backtesting(args) - assert log_has( - 'Starting freqtrade in Backtesting mode', - caplog.record_tuples - ) + assert log_has('Starting freqtrade in Backtesting mode', caplog) assert start_mock.call_count == 1 @@ -360,7 +342,7 @@ def test_backtesting_init_no_ticker_interval(mocker, default_conf, caplog) -> No with pytest.raises(OperationalException): Backtesting(default_conf) log_has("Ticker-interval needs to be set in either configuration " - "or as cli argument `--ticker-interval 5m`", caplog.record_tuples) + "or as cli argument `--ticker-interval 5m`", caplog) def test_tickerdata_to_dataframe_bt(default_conf, mocker) -> None: @@ -511,7 +493,7 @@ def test_backtesting_start(default_conf, mocker, caplog) -> None: 'up to 2017-11-14T22:59:00+00:00 (0 days)..' ] for line in exists: - assert log_has(line, caplog.record_tuples) + assert log_has(line, caplog) def test_backtesting_start_no_data(default_conf, mocker, caplog) -> None: @@ -539,7 +521,7 @@ def test_backtesting_start_no_data(default_conf, mocker, caplog) -> None: backtesting.start() # check the logs, that will contain the backtest result - assert log_has('No data found. Terminating.', caplog.record_tuples) + assert log_has('No data found. Terminating.', caplog) def test_backtest(default_conf, fee, mocker) -> None: @@ -876,7 +858,7 @@ def test_backtest_start_live(default_conf, mocker, caplog): ] for line in exists: - assert log_has(line, caplog.record_tuples) + assert log_has(line, caplog) @pytest.mark.filterwarnings("ignore:DEPRECATED") @@ -936,4 +918,4 @@ def test_backtest_start_multi_strat(default_conf, mocker, caplog): ] for line in exists: - assert log_has(line, caplog.record_tuples) + assert log_has(line, caplog) diff --git a/freqtrade/tests/optimize/test_edge_cli.py b/freqtrade/tests/optimize/test_edge_cli.py index badaa5c45..cdc724db2 100644 --- a/freqtrade/tests/optimize/test_edge_cli.py +++ b/freqtrade/tests/optimize/test_edge_cli.py @@ -29,15 +29,12 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> assert 'exchange' in config assert 'pair_whitelist' in config['exchange'] assert 'datadir' in config - assert log_has( - 'Using data directory: {} ...'.format(config['datadir']), - caplog.record_tuples - ) + assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog) assert 'ticker_interval' in config - assert not log_has_re('Parameter -i/--ticker-interval detected .*', caplog.record_tuples) + assert not log_has_re('Parameter -i/--ticker-interval detected .*', caplog) assert 'refresh_pairs' not in config - assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples) + assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog) assert 'timerange' not in config assert 'stoploss_range' not in config @@ -69,21 +66,15 @@ def test_setup_edge_configuration_with_arguments(mocker, edge_conf, caplog) -> N assert 'pair_whitelist' in config['exchange'] assert 'datadir' in config assert config['runmode'] == RunMode.EDGE - assert log_has( - 'Using data directory: {} ...'.format(config['datadir']), - caplog.record_tuples - ) + assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog) assert 'ticker_interval' in config assert log_has('Parameter -i/--ticker-interval detected ... Using ticker_interval: 1m ...', - caplog.record_tuples) + caplog) assert 'refresh_pairs' in config - assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples) + assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog) assert 'timerange' in config - assert log_has( - 'Parameter --timerange detected: {} ...'.format(config['timerange']), - caplog.record_tuples - ) + assert log_has('Parameter --timerange detected: {} ...'.format(config['timerange']), caplog) def test_start(mocker, fee, edge_conf, caplog) -> None: @@ -100,10 +91,7 @@ def test_start(mocker, fee, edge_conf, caplog) -> None: ] args = get_args(args) start_edge(args) - assert log_has( - 'Starting freqtrade in Edge mode', - caplog.record_tuples - ) + assert log_has('Starting freqtrade in Edge mode', caplog) assert start_mock.call_count == 1 diff --git a/freqtrade/tests/optimize/test_hyperopt.py b/freqtrade/tests/optimize/test_hyperopt.py index d964e58a6..55ac6a739 100644 --- a/freqtrade/tests/optimize/test_hyperopt.py +++ b/freqtrade/tests/optimize/test_hyperopt.py @@ -82,21 +82,18 @@ def test_setup_hyperopt_configuration_without_arguments(mocker, default_conf, ca assert 'exchange' in config assert 'pair_whitelist' in config['exchange'] assert 'datadir' in config - assert log_has( - 'Using data directory: {} ...'.format(config['datadir']), - caplog.record_tuples - ) + assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog) assert 'ticker_interval' in config - assert not log_has_re('Parameter -i/--ticker-interval detected .*', caplog.record_tuples) + assert not log_has_re('Parameter -i/--ticker-interval detected .*', caplog) assert 'live' not in config - assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples) + assert not log_has('Parameter -l/--live detected ...', caplog) assert 'position_stacking' not in config - assert not log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples) + assert not log_has('Parameter --enable-position-stacking detected ...', caplog) assert 'refresh_pairs' not in config - assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples) + assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog) assert 'timerange' not in config assert 'runmode' in config @@ -133,41 +130,32 @@ def test_setup_hyperopt_configuration_with_arguments(mocker, default_conf, caplo assert 'datadir' in config assert config['runmode'] == RunMode.HYPEROPT - assert log_has( - 'Using data directory: {} ...'.format(config['datadir']), - caplog.record_tuples - ) + assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog) assert 'ticker_interval' in config assert log_has('Parameter -i/--ticker-interval detected ... Using ticker_interval: 1m ...', - caplog.record_tuples) + caplog) assert 'position_stacking' in config - assert log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples) + assert log_has('Parameter --enable-position-stacking detected ...', caplog) assert 'use_max_market_positions' in config - assert log_has('Parameter --disable-max-market-positions detected ...', caplog.record_tuples) - assert log_has('max_open_trades set to unlimited ...', caplog.record_tuples) + assert log_has('Parameter --disable-max-market-positions detected ...', caplog) + assert log_has('max_open_trades set to unlimited ...', caplog) assert 'refresh_pairs' in config - assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples) + assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog) assert 'timerange' in config - assert log_has( - 'Parameter --timerange detected: {} ...'.format(config['timerange']), - caplog.record_tuples - ) + assert log_has('Parameter --timerange detected: {} ...'.format(config['timerange']), caplog) assert 'epochs' in config assert log_has('Parameter --epochs detected ... Will run Hyperopt with for 1000 epochs ...', - caplog.record_tuples) + caplog) assert 'spaces' in config - assert log_has( - 'Parameter -s/--spaces detected: {}'.format(config['spaces']), - caplog.record_tuples - ) + assert log_has('Parameter -s/--spaces detected: {}'.format(config['spaces']), caplog) assert 'print_all' in config - assert log_has('Parameter --print-all detected ...', caplog.record_tuples) + assert log_has('Parameter --print-all detected ...', caplog) def test_hyperoptresolver(mocker, default_conf, caplog) -> None: @@ -184,9 +172,9 @@ def test_hyperoptresolver(mocker, default_conf, caplog) -> None: assert not hasattr(x, 'populate_buy_trend') assert not hasattr(x, 'populate_sell_trend') assert log_has("Custom Hyperopt does not provide populate_sell_trend. " - "Using populate_sell_trend from DefaultStrategy.", caplog.record_tuples) + "Using populate_sell_trend from DefaultStrategy.", caplog) assert log_has("Custom Hyperopt does not provide populate_buy_trend. " - "Using populate_buy_trend from DefaultStrategy.", caplog.record_tuples) + "Using populate_buy_trend from DefaultStrategy.", caplog) assert hasattr(x, "ticker_interval") @@ -232,10 +220,7 @@ def test_start(mocker, default_conf, caplog) -> None: import pprint pprint.pprint(caplog.record_tuples) - assert log_has( - 'Starting freqtrade in Hyperopt mode', - caplog.record_tuples - ) + assert log_has('Starting freqtrade in Hyperopt mode', caplog) assert start_mock.call_count == 1 @@ -260,7 +245,7 @@ def test_start_no_data(mocker, default_conf, caplog) -> None: import pprint pprint.pprint(caplog.record_tuples) - assert log_has('No data found. Terminating.', caplog.record_tuples) + assert log_has('No data found. Terminating.', caplog) def test_start_failure(mocker, default_conf, caplog) -> None: @@ -278,10 +263,7 @@ def test_start_failure(mocker, default_conf, caplog) -> None: args = get_args(args) with pytest.raises(DependencyException): start_hyperopt(args) - assert log_has( - "Please don't use --strategy for hyperopt.", - caplog.record_tuples - ) + assert log_has("Please don't use --strategy for hyperopt.", caplog) def test_start_filelock(mocker, default_conf, caplog) -> None: @@ -297,10 +279,7 @@ def test_start_filelock(mocker, default_conf, caplog) -> None: ] args = get_args(args) start_hyperopt(args) - assert log_has( - "Another running instance of freqtrade Hyperopt detected.", - caplog.record_tuples - ) + assert log_has("Another running instance of freqtrade Hyperopt detected.", caplog) def test_loss_calculation_prefer_correct_trade_count(default_conf, hyperopt_results) -> None: @@ -404,10 +383,7 @@ def test_save_trials_saves_trials(mocker, hyperopt, caplog) -> None: hyperopt.save_trials() trials_file = os.path.join('freqtrade', 'tests', 'optimize', 'ut_trials.pickle') - assert log_has( - 'Saving 1 evaluations to \'{}\''.format(trials_file), - caplog.record_tuples - ) + assert log_has('Saving 1 evaluations to \'{}\''.format(trials_file), caplog) mock_dump.assert_called_once() @@ -416,10 +392,7 @@ def test_read_trials_returns_trials_file(mocker, hyperopt, caplog) -> None: mock_load = mocker.patch('freqtrade.optimize.hyperopt.load', return_value=trials) hyperopt_trial = hyperopt.read_trials() trials_file = os.path.join('freqtrade', 'tests', 'optimize', 'ut_trials.pickle') - assert log_has( - 'Reading Trials from \'{}\''.format(trials_file), - caplog.record_tuples - ) + assert log_has('Reading Trials from \'{}\''.format(trials_file), caplog) assert hyperopt_trial == trials mock_load.assert_called_once() @@ -608,7 +581,8 @@ def test_generate_optimizer(mocker, default_conf) -> None: 'loss': 1.9840569076926293, 'results_explanation': ' 1 trades. Avg profit 2.31%. Total profit 0.00023300 BTC ' '( 2.31Σ%). Avg duration 100.0 mins.', - 'params': optimizer_param + 'params': optimizer_param, + 'total_profit': 0.00023300 } hyperopt = Hyperopt(default_conf) @@ -629,7 +603,7 @@ def test_clean_hyperopt(mocker, default_conf, caplog): h = Hyperopt(default_conf) assert unlinkmock.call_count == 2 - assert log_has(f"Removing `{h.tickerdata_pickle}`.", caplog.record_tuples) + assert log_has(f"Removing `{h.tickerdata_pickle}`.", caplog) def test_continue_hyperopt(mocker, default_conf, caplog): @@ -646,4 +620,78 @@ def test_continue_hyperopt(mocker, default_conf, caplog): Hyperopt(default_conf) assert unlinkmock.call_count == 0 - assert log_has(f"Continuing on previous hyperopt results.", caplog.record_tuples) + assert log_has(f"Continuing on previous hyperopt results.", caplog) + + +def test_print_json_spaces_all(mocker, default_conf, caplog, capsys) -> None: + dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) + mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock()) + mocker.patch( + 'freqtrade.optimize.hyperopt.get_timeframe', + MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13))) + ) + + parallel = mocker.patch( + 'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel', + MagicMock(return_value=[{'loss': 1, 'results_explanation': 'foo result', 'params': {}}]) + ) + patch_exchange(mocker) + + default_conf.update({'config': 'config.json.example', + 'epochs': 1, + 'timerange': None, + 'spaces': 'all', + 'hyperopt_jobs': 1, + 'print_json': True, + }) + + hyperopt = Hyperopt(default_conf) + hyperopt.strategy.tickerdata_to_dataframe = MagicMock() + hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) + + hyperopt.start() + + parallel.assert_called_once() + + out, err = capsys.readouterr() + assert '{"params":{"mfi-value":null,"fastd-value":null,"adx-value":null,"rsi-value":null,"mfi-enabled":null,"fastd-enabled":null,"adx-enabled":null,"rsi-enabled":null,"trigger":null,"sell-mfi-value":null,"sell-fastd-value":null,"sell-adx-value":null,"sell-rsi-value":null,"sell-mfi-enabled":null,"sell-fastd-enabled":null,"sell-adx-enabled":null,"sell-rsi-enabled":null,"sell-trigger":null},"minimal_roi":{},"stoploss":null}' in out # noqa: E501 + assert dumper.called + # Should be called twice, once for tickerdata, once to save evaluations + assert dumper.call_count == 2 + + +def test_print_json_spaces_roi_stoploss(mocker, default_conf, caplog, capsys) -> None: + dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) + mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock()) + mocker.patch( + 'freqtrade.optimize.hyperopt.get_timeframe', + MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13))) + ) + + parallel = mocker.patch( + 'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel', + MagicMock(return_value=[{'loss': 1, 'results_explanation': 'foo result', 'params': {}}]) + ) + patch_exchange(mocker) + + default_conf.update({'config': 'config.json.example', + 'epochs': 1, + 'timerange': None, + 'spaces': 'roi stoploss', + 'hyperopt_jobs': 1, + 'print_json': True, + }) + + hyperopt = Hyperopt(default_conf) + hyperopt.strategy.tickerdata_to_dataframe = MagicMock() + hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) + + hyperopt.start() + + parallel.assert_called_once() + + out, err = capsys.readouterr() + assert '{"minimal_roi":{},"stoploss":null}' in out + assert dumper.called + # Should be called twice, once for tickerdata, once to save evaluations + assert dumper.call_count == 2 diff --git a/freqtrade/tests/rpc/test_fiat_convert.py b/freqtrade/tests/rpc/test_fiat_convert.py index 66870efcc..1689ecac6 100644 --- a/freqtrade/tests/rpc/test_fiat_convert.py +++ b/freqtrade/tests/rpc/test_fiat_convert.py @@ -91,7 +91,7 @@ def test_fiat_convert_unsupported_crypto(mocker, caplog): mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._cryptomap', return_value=[]) fiat_convert = CryptoToFiatConverter() assert fiat_convert._find_price(crypto_symbol='CRYPTO_123', fiat_symbol='EUR') == 0.0 - assert log_has('unsupported crypto-symbol CRYPTO_123 - returning 0.0', caplog.record_tuples) + assert log_has('unsupported crypto-symbol CRYPTO_123 - returning 0.0', caplog) def test_fiat_convert_get_price(mocker): @@ -190,7 +190,7 @@ def test_fiat_invalid_response(mocker, caplog): length_cryptomap = len(fiat_convert._cryptomap) assert length_cryptomap == 0 assert log_has('Could not load FIAT Cryptocurrency map for the following problem: TypeError', - caplog.record_tuples) + caplog) def test_convert_amount(mocker): diff --git a/freqtrade/tests/rpc/test_rpc.py b/freqtrade/tests/rpc/test_rpc.py index d273244b0..5d3fb7920 100644 --- a/freqtrade/tests/rpc/test_rpc.py +++ b/freqtrade/tests/rpc/test_rpc.py @@ -44,7 +44,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, markets, mocker) -> None: with pytest.raises(RPCException, match=r'.*no active trade*'): rpc._rpc_trade_status() - freqtradebot.create_trade() + freqtradebot.create_trades() results = rpc._rpc_trade_status() assert { 'trade_id': 1, @@ -116,7 +116,7 @@ def test_rpc_status_table(default_conf, ticker, fee, markets, mocker) -> None: with pytest.raises(RPCException, match=r'.*no active order*'): rpc._rpc_status_table() - freqtradebot.create_trade() + freqtradebot.create_trades() result = rpc._rpc_status_table() assert 'instantly' in result['Since'].all() assert 'ETH/BTC' in result['Pair'].all() @@ -151,7 +151,7 @@ def test_rpc_daily_profit(default_conf, update, ticker, fee, rpc = RPC(freqtradebot) rpc._fiat_converter = CryptoToFiatConverter() # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.first() assert trade @@ -208,7 +208,7 @@ def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee, rpc._rpc_trade_statistics(stake_currency, fiat_display_currency) # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.first() # Simulate fulfilled LIMIT_BUY order for trade trade.update(limit_buy_order) @@ -222,7 +222,7 @@ def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee, trade.close_date = datetime.utcnow() trade.is_open = False - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.first() # Simulate fulfilled LIMIT_BUY order for trade trade.update(limit_buy_order) @@ -292,7 +292,7 @@ def test_rpc_trade_statistics_closed(mocker, default_conf, ticker, fee, markets, rpc = RPC(freqtradebot) # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.first() # Simulate fulfilled LIMIT_BUY order for trade trade.update(limit_buy_order) @@ -536,7 +536,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker, markets) -> None: msg = rpc._rpc_forcesell('all') assert msg == {'result': 'Created sell orders for all open trades.'} - freqtradebot.create_trade() + freqtradebot.create_trades() msg = rpc._rpc_forcesell('all') assert msg == {'result': 'Created sell orders for all open trades.'} @@ -570,7 +570,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker, markets) -> None: assert cancel_order_mock.call_count == 1 assert trade.amount == filled_amount - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.filter(Trade.id == '2').first() amount = trade.amount # make an limit-buy open trade, if there is no 'filled', don't sell it @@ -589,7 +589,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker, markets) -> None: assert cancel_order_mock.call_count == 2 assert trade.amount == amount - freqtradebot.create_trade() + freqtradebot.create_trades() # make an limit-sell open trade mocker.patch( 'freqtrade.exchange.Exchange.get_order', @@ -622,7 +622,7 @@ def test_performance_handle(default_conf, ticker, limit_buy_order, fee, rpc = RPC(freqtradebot) # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.first() assert trade @@ -660,7 +660,7 @@ def test_rpc_count(mocker, default_conf, ticker, fee, markets) -> None: assert counts["current"] == 0 # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() counts = rpc._rpc_count() assert counts["current"] == 1 diff --git a/freqtrade/tests/rpc/test_rpc_apiserver.py b/freqtrade/tests/rpc/test_rpc_apiserver.py index bd420ada6..794343a98 100644 --- a/freqtrade/tests/rpc/test_rpc_apiserver.py +++ b/freqtrade/tests/rpc/test_rpc_apiserver.py @@ -148,8 +148,8 @@ def test_api_run(default_conf, mocker, caplog): assert isinstance(server_mock.call_args_list[0][0][2], Flask) assert hasattr(apiserver, "srv") - assert log_has("Starting HTTP Server at 127.0.0.1:8080", caplog.record_tuples) - assert log_has("Starting Local Rest Server.", caplog.record_tuples) + assert log_has("Starting HTTP Server at 127.0.0.1:8080", caplog) + assert log_has("Starting Local Rest Server.", caplog) # Test binding to public caplog.clear() @@ -165,22 +165,20 @@ def test_api_run(default_conf, mocker, caplog): assert server_mock.call_args_list[0][0][0] == "0.0.0.0" assert server_mock.call_args_list[0][0][1] == "8089" assert isinstance(server_mock.call_args_list[0][0][2], Flask) - assert log_has("Starting HTTP Server at 0.0.0.0:8089", caplog.record_tuples) - assert log_has("Starting Local Rest Server.", caplog.record_tuples) + assert log_has("Starting HTTP Server at 0.0.0.0:8089", caplog) + assert log_has("Starting Local Rest Server.", caplog) assert log_has("SECURITY WARNING - Local Rest Server listening to external connections", - caplog.record_tuples) + caplog) assert log_has("SECURITY WARNING - This is insecure please set to your loopback," - "e.g 127.0.0.1 in config.json", - caplog.record_tuples) + "e.g 127.0.0.1 in config.json", caplog) assert log_has("SECURITY WARNING - No password for local REST Server defined. " - "Please make sure that this is intentional!", - caplog.record_tuples) + "Please make sure that this is intentional!", caplog) # Test crashing flask caplog.clear() mocker.patch('freqtrade.rpc.api_server.make_server', MagicMock(side_effect=Exception)) apiserver.run() - assert log_has("Api server failed to start.", caplog.record_tuples) + assert log_has("Api server failed to start.", caplog) def test_api_cleanup(default_conf, mocker, caplog): @@ -199,7 +197,7 @@ def test_api_cleanup(default_conf, mocker, caplog): apiserver.cleanup() assert stop_mock.shutdown.call_count == 1 - assert log_has("Stopping API Server", caplog.record_tuples) + assert log_has("Stopping API Server", caplog) def test_api_reloadconf(botclient): @@ -277,7 +275,7 @@ def test_api_count(botclient, mocker, ticker, fee, markets): assert rc.json["max"] == 1.0 # Create some test data - ftbot.create_trade() + ftbot.create_trades() rc = client_get(client, f"{BASE_URI}/count") assert_response(rc) assert rc.json["current"] == 1.0 @@ -331,7 +329,7 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, limit_buy_order, li assert len(rc.json) == 1 assert rc.json == {"error": "Error querying _profit: no closed trade"} - ftbot.create_trade() + ftbot.create_trades() trade = Trade.query.first() # Simulate fulfilled LIMIT_BUY order for trade @@ -420,7 +418,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets): assert_response(rc, 502) assert rc.json == {'error': 'Error querying _status: no active trade'} - ftbot.create_trade() + ftbot.create_trades() rc = client_get(client, f"{BASE_URI}/status") assert_response(rc) assert len(rc.json) == 1 @@ -550,7 +548,7 @@ def test_api_forcesell(botclient, mocker, ticker, fee, markets): assert_response(rc, 502) assert rc.json == {"error": "Error querying _forcesell: invalid argument"} - ftbot.create_trade() + ftbot.create_trades() rc = client_post(client, f"{BASE_URI}/forcesell", data='{"tradeid": "1"}') diff --git a/freqtrade/tests/rpc/test_rpc_manager.py b/freqtrade/tests/rpc/test_rpc_manager.py index 91fd2297f..468e3e8e5 100644 --- a/freqtrade/tests/rpc/test_rpc_manager.py +++ b/freqtrade/tests/rpc/test_rpc_manager.py @@ -19,7 +19,7 @@ def test_init_telegram_disabled(mocker, default_conf, caplog) -> None: default_conf['telegram']['enabled'] = False rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf)) - assert not log_has('Enabling rpc.telegram ...', caplog.record_tuples) + assert not log_has('Enabling rpc.telegram ...', caplog) assert rpc_manager.registered_modules == [] @@ -28,7 +28,7 @@ def test_init_telegram_enabled(mocker, default_conf, caplog) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf)) - assert log_has('Enabling rpc.telegram ...', caplog.record_tuples) + assert log_has('Enabling rpc.telegram ...', caplog) len_modules = len(rpc_manager.registered_modules) assert len_modules == 1 assert 'telegram' in [mod.name for mod in rpc_manager.registered_modules] @@ -43,7 +43,7 @@ def test_cleanup_telegram_disabled(mocker, default_conf, caplog) -> None: rpc_manager = RPCManager(freqtradebot) rpc_manager.cleanup() - assert not log_has('Cleaning up rpc.telegram ...', caplog.record_tuples) + assert not log_has('Cleaning up rpc.telegram ...', caplog) assert telegram_mock.call_count == 0 @@ -59,7 +59,7 @@ def test_cleanup_telegram_enabled(mocker, default_conf, caplog) -> None: assert 'telegram' in [mod.name for mod in rpc_manager.registered_modules] rpc_manager.cleanup() - assert log_has('Cleaning up rpc.telegram ...', caplog.record_tuples) + assert log_has('Cleaning up rpc.telegram ...', caplog) assert 'telegram' not in [mod.name for mod in rpc_manager.registered_modules] assert telegram_mock.call_count == 1 @@ -75,7 +75,7 @@ def test_send_msg_telegram_disabled(mocker, default_conf, caplog) -> None: 'status': 'test' }) - assert log_has("Sending rpc message: {'type': status, 'status': 'test'}", caplog.record_tuples) + assert log_has("Sending rpc message: {'type': status, 'status': 'test'}", caplog) assert telegram_mock.call_count == 0 @@ -90,7 +90,7 @@ def test_send_msg_telegram_enabled(mocker, default_conf, caplog) -> None: 'status': 'test' }) - assert log_has("Sending rpc message: {'type': status, 'status': 'test'}", caplog.record_tuples) + assert log_has("Sending rpc message: {'type': status, 'status': 'test'}", caplog) assert telegram_mock.call_count == 1 @@ -100,7 +100,7 @@ def test_init_webhook_disabled(mocker, default_conf, caplog) -> None: default_conf['webhook'] = {'enabled': False} rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf)) - assert not log_has('Enabling rpc.webhook ...', caplog.record_tuples) + assert not log_has('Enabling rpc.webhook ...', caplog) assert rpc_manager.registered_modules == [] @@ -110,7 +110,7 @@ def test_init_webhook_enabled(mocker, default_conf, caplog) -> None: default_conf['webhook'] = {'enabled': True, 'url': "https://DEADBEEF.com"} rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf)) - assert log_has('Enabling rpc.webhook ...', caplog.record_tuples) + assert log_has('Enabling rpc.webhook ...', caplog) assert len(rpc_manager.registered_modules) == 1 assert 'webhook' in [mod.name for mod in rpc_manager.registered_modules] @@ -144,7 +144,7 @@ def test_init_apiserver_disabled(mocker, default_conf, caplog) -> None: default_conf['telegram']['enabled'] = False rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf)) - assert not log_has('Enabling rpc.api_server', caplog.record_tuples) + assert not log_has('Enabling rpc.api_server', caplog) assert rpc_manager.registered_modules == [] assert run_mock.call_count == 0 @@ -160,7 +160,7 @@ def test_init_apiserver_enabled(mocker, default_conf, caplog) -> None: "listen_port": "8080"} rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf)) - assert log_has('Enabling rpc.api_server', caplog.record_tuples) + assert log_has('Enabling rpc.api_server', caplog) assert len(rpc_manager.registered_modules) == 1 assert 'apiserver' in [mod.name for mod in rpc_manager.registered_modules] assert run_mock.call_count == 1 diff --git a/freqtrade/tests/rpc/test_rpc_telegram.py b/freqtrade/tests/rpc/test_rpc_telegram.py index 1bee5bff3..2f469643f 100644 --- a/freqtrade/tests/rpc/test_rpc_telegram.py +++ b/freqtrade/tests/rpc/test_rpc_telegram.py @@ -76,7 +76,7 @@ def test_init(default_conf, mocker, caplog) -> None: "['performance'], ['daily'], ['count'], ['reload_conf'], " \ "['stopbuy'], ['whitelist'], ['blacklist'], ['edge'], ['help'], ['version']]" - assert log_has(message_str, caplog.record_tuples) + assert log_has(message_str, caplog) def test_cleanup(default_conf, mocker) -> None: @@ -102,18 +102,9 @@ def test_authorized_only(default_conf, mocker, caplog) -> None: dummy = DummyCls(bot) dummy.dummy_handler(bot=MagicMock(), update=update) assert dummy.state['called'] is True - assert log_has( - 'Executing handler: dummy_handler for chat_id: 0', - caplog.record_tuples - ) - assert not log_has( - 'Rejected unauthorized message from: 0', - caplog.record_tuples - ) - assert not log_has( - 'Exception occurred within Telegram module', - caplog.record_tuples - ) + assert log_has('Executing handler: dummy_handler for chat_id: 0', caplog) + assert not log_has('Rejected unauthorized message from: 0', caplog) + assert not log_has('Exception occurred within Telegram module', caplog) def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> None: @@ -128,18 +119,9 @@ def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> None: dummy = DummyCls(bot) dummy.dummy_handler(bot=MagicMock(), update=update) assert dummy.state['called'] is False - assert not log_has( - 'Executing handler: dummy_handler for chat_id: 3735928559', - caplog.record_tuples - ) - assert log_has( - 'Rejected unauthorized message from: 3735928559', - caplog.record_tuples - ) - assert not log_has( - 'Exception occurred within Telegram module', - caplog.record_tuples - ) + assert not log_has('Executing handler: dummy_handler for chat_id: 3735928559', caplog) + assert log_has('Rejected unauthorized message from: 3735928559', caplog) + assert not log_has('Exception occurred within Telegram module', caplog) def test_authorized_only_exception(default_conf, mocker, caplog) -> None: @@ -156,18 +138,9 @@ def test_authorized_only_exception(default_conf, mocker, caplog) -> None: dummy.dummy_exception(bot=MagicMock(), update=update) assert dummy.state['called'] is False - assert not log_has( - 'Executing handler: dummy_handler for chat_id: 0', - caplog.record_tuples - ) - assert not log_has( - 'Rejected unauthorized message from: 0', - caplog.record_tuples - ) - assert log_has( - 'Exception occurred within Telegram module', - caplog.record_tuples - ) + assert not log_has('Executing handler: dummy_handler for chat_id: 0', caplog) + assert not log_has('Rejected unauthorized message from: 0', caplog) + assert log_has('Exception occurred within Telegram module', caplog) def test_status(default_conf, update, mocker, fee, ticker, markets) -> None: @@ -219,7 +192,7 @@ def test_status(default_conf, update, mocker, fee, ticker, markets) -> None: # Create some test data for _ in range(3): - freqtradebot.create_trade() + freqtradebot.create_trades() telegram._status(bot=MagicMock(), update=update) assert msg_mock.call_count == 1 @@ -267,7 +240,7 @@ def test_status_handle(default_conf, update, ticker, fee, markets, mocker) -> No msg_mock.reset_mock() # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() # Trigger status while we have a fulfilled order for the open trade telegram._status(bot=MagicMock(), update=update) @@ -319,7 +292,7 @@ def test_status_table_handle(default_conf, update, ticker, fee, markets, mocker) msg_mock.reset_mock() # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() telegram._status_table(bot=MagicMock(), update=update) @@ -335,6 +308,7 @@ def test_status_table_handle(default_conf, update, ticker, fee, markets, mocker) def test_daily_handle(default_conf, update, ticker, limit_buy_order, fee, limit_sell_order, markets, mocker) -> None: patch_exchange(mocker) + default_conf['max_open_trades'] = 1 mocker.patch( 'freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0 @@ -358,7 +332,7 @@ def test_daily_handle(default_conf, update, ticker, limit_buy_order, fee, telegram = Telegram(freqtradebot) # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.first() assert trade @@ -384,9 +358,9 @@ def test_daily_handle(default_conf, update, ticker, limit_buy_order, fee, # Reset msg_mock msg_mock.reset_mock() + freqtradebot.config['max_open_trades'] = 2 # Add two other trades - freqtradebot.create_trade() - freqtradebot.create_trade() + freqtradebot.create_trades() trades = Trade.query.all() for trade in trades: @@ -465,7 +439,7 @@ def test_profit_handle(default_conf, update, ticker, ticker_sell_up, fee, msg_mock.reset_mock() # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.first() # Simulate fulfilled LIMIT_BUY order for trade @@ -760,7 +734,7 @@ def test_forcesell_handle(default_conf, update, ticker, fee, telegram = Telegram(freqtradebot) # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.first() assert trade @@ -811,7 +785,7 @@ def test_forcesell_down_handle(default_conf, update, ticker, fee, telegram = Telegram(freqtradebot) # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() # Decrease the price and sell it mocker.patch.multiple( @@ -859,14 +833,13 @@ def test_forcesell_all_handle(default_conf, update, ticker, fee, markets, mocker markets=PropertyMock(return_value=markets), validate_pairs=MagicMock(return_value={}) ) - + default_conf['max_open_trades'] = 4 freqtradebot = FreqtradeBot(default_conf) patch_get_signal(freqtradebot, (True, False)) telegram = Telegram(freqtradebot) # Create some test data - for _ in range(4): - freqtradebot.create_trade() + freqtradebot.create_trades() rpc_mock.reset_mock() update.message.text = '/forcesell all' @@ -1010,7 +983,7 @@ def test_performance_handle(default_conf, update, ticker, fee, telegram = Telegram(freqtradebot) # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() trade = Trade.query.first() assert trade @@ -1055,7 +1028,7 @@ def test_count_handle(default_conf, update, ticker, fee, markets, mocker) -> Non freqtradebot.state = State.RUNNING # Create some test data - freqtradebot.create_trade() + freqtradebot.create_trades() msg_mock.reset_mock() telegram._count(bot=MagicMock(), update=update) @@ -1440,7 +1413,4 @@ def test__send_msg_network_error(default_conf, mocker, caplog) -> None: # Bot should've tried to send it twice assert len(bot.method_calls) == 2 - assert log_has( - 'Telegram NetworkError: Oh snap! Trying one more time.', - caplog.record_tuples - ) + assert log_has('Telegram NetworkError: Oh snap! Trying one more time.', caplog) diff --git a/freqtrade/tests/rpc/test_rpc_webhook.py b/freqtrade/tests/rpc/test_rpc_webhook.py index a2dcd9b31..cc491d4dd 100644 --- a/freqtrade/tests/rpc/test_rpc_webhook.py +++ b/freqtrade/tests/rpc/test_rpc_webhook.py @@ -115,7 +115,7 @@ def test_exception_send_msg(default_conf, mocker, caplog): webhook = Webhook(get_patched_freqtradebot(mocker, default_conf)) webhook.send_msg({'type': RPCMessageType.BUY_NOTIFICATION}) assert log_has(f"Message type {RPCMessageType.BUY_NOTIFICATION} not configured for webhooks", - caplog.record_tuples) + caplog) default_conf["webhook"] = get_webhook_dict() default_conf["webhook"]["webhookbuy"]["value1"] = "{DEADBEEF:8f}" @@ -135,7 +135,7 @@ def test_exception_send_msg(default_conf, mocker, caplog): } webhook.send_msg(msg) assert log_has("Problem calling Webhook. Please check your webhook configuration. " - "Exception: 'DEADBEEF'", caplog.record_tuples) + "Exception: 'DEADBEEF'", caplog) msg_mock = MagicMock() mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) @@ -164,4 +164,4 @@ def test__send_msg(default_conf, mocker, caplog): post = MagicMock(side_effect=RequestException) mocker.patch("freqtrade.rpc.webhook.post", post) webhook._send_msg(msg) - assert log_has('Could not call webhook url. Exception: ', caplog.record_tuples) + assert log_has('Could not call webhook url. Exception: ', caplog) diff --git a/freqtrade/tests/strategy/test_interface.py b/freqtrade/tests/strategy/test_interface.py index 9f5ab70e3..36c9ffcd4 100644 --- a/freqtrade/tests/strategy/test_interface.py +++ b/freqtrade/tests/strategy/test_interface.py @@ -49,12 +49,12 @@ def test_returns_latest_sell_signal(mocker, default_conf, ticker_history): def test_get_signal_empty(default_conf, mocker, caplog): assert (False, False) == _STRATEGY.get_signal('foo', default_conf['ticker_interval'], DataFrame()) - assert log_has('Empty ticker history for pair foo', caplog.record_tuples) + assert log_has('Empty ticker history for pair foo', caplog) caplog.clear() assert (False, False) == _STRATEGY.get_signal('bar', default_conf['ticker_interval'], []) - assert log_has('Empty ticker history for pair bar', caplog.record_tuples) + assert log_has('Empty ticker history for pair bar', caplog) def test_get_signal_exception_valueerror(default_conf, mocker, caplog, ticker_history): @@ -65,7 +65,7 @@ def test_get_signal_exception_valueerror(default_conf, mocker, caplog, ticker_hi ) assert (False, False) == _STRATEGY.get_signal('foo', default_conf['ticker_interval'], ticker_history) - assert log_has('Unable to analyze ticker for pair foo: xyz', caplog.record_tuples) + assert log_has('Unable to analyze ticker for pair foo: xyz', caplog) def test_get_signal_empty_dataframe(default_conf, mocker, caplog, ticker_history): @@ -76,7 +76,7 @@ def test_get_signal_empty_dataframe(default_conf, mocker, caplog, ticker_history ) assert (False, False) == _STRATEGY.get_signal('xyz', default_conf['ticker_interval'], ticker_history) - assert log_has('Empty dataframe for pair xyz', caplog.record_tuples) + assert log_has('Empty dataframe for pair xyz', caplog) def test_get_signal_old_dataframe(default_conf, mocker, caplog, ticker_history): @@ -91,10 +91,7 @@ def test_get_signal_old_dataframe(default_conf, mocker, caplog, ticker_history): ) assert (False, False) == _STRATEGY.get_signal('xyz', default_conf['ticker_interval'], ticker_history) - assert log_has( - 'Outdated history for pair xyz. Last tick is 16 minutes old', - caplog.record_tuples - ) + assert log_has('Outdated history for pair xyz. Last tick is 16 minutes old', caplog) def test_get_signal_handles_exceptions(mocker, default_conf): @@ -237,9 +234,8 @@ def test_analyze_ticker_default(ticker_history, mocker, caplog) -> None: assert buy_mock.call_count == 1 assert buy_mock.call_count == 1 - assert log_has('TA Analysis Launched', caplog.record_tuples) - assert not log_has('Skipping TA Analysis for already analyzed candle', - caplog.record_tuples) + assert log_has('TA Analysis Launched', caplog) + assert not log_has('Skipping TA Analysis for already analyzed candle', caplog) caplog.clear() strategy.analyze_ticker(ticker_history, {'pair': 'ETH/BTC'}) @@ -247,9 +243,8 @@ def test_analyze_ticker_default(ticker_history, mocker, caplog) -> None: assert ind_mock.call_count == 2 assert buy_mock.call_count == 2 assert buy_mock.call_count == 2 - assert log_has('TA Analysis Launched', caplog.record_tuples) - assert not log_has('Skipping TA Analysis for already analyzed candle', - caplog.record_tuples) + assert log_has('TA Analysis Launched', caplog) + assert not log_has('Skipping TA Analysis for already analyzed candle', caplog) def test__analyze_ticker_internal_skip_analyze(ticker_history, mocker, caplog) -> None: @@ -275,9 +270,8 @@ def test__analyze_ticker_internal_skip_analyze(ticker_history, mocker, caplog) - assert ind_mock.call_count == 1 assert buy_mock.call_count == 1 assert buy_mock.call_count == 1 - assert log_has('TA Analysis Launched', caplog.record_tuples) - assert not log_has('Skipping TA Analysis for already analyzed candle', - caplog.record_tuples) + assert log_has('TA Analysis Launched', caplog) + assert not log_has('Skipping TA Analysis for already analyzed candle', caplog) caplog.clear() ret = strategy._analyze_ticker_internal(ticker_history, {'pair': 'ETH/BTC'}) @@ -290,6 +284,21 @@ def test__analyze_ticker_internal_skip_analyze(ticker_history, mocker, caplog) - assert 'sell' in ret.columns assert ret['buy'].sum() == 0 assert ret['sell'].sum() == 0 - assert not log_has('TA Analysis Launched', caplog.record_tuples) - assert log_has('Skipping TA Analysis for already analyzed candle', - caplog.record_tuples) + assert not log_has('TA Analysis Launched', caplog) + assert log_has('Skipping TA Analysis for already analyzed candle', caplog) + + +def test_is_pair_locked(default_conf): + strategy = DefaultStrategy(default_conf) + # dict should be empty + assert not strategy._pair_locked_until + + pair = 'ETH/BTC' + assert not strategy.is_pair_locked(pair) + strategy.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) + # ETH/BTC locked for 4 minutes + assert strategy.is_pair_locked(pair) + + # XRP/BTC should not be locked now + pair = 'XRP/BTC' + assert not strategy.is_pair_locked(pair) diff --git a/freqtrade/tests/strategy/test_strategy.py b/freqtrade/tests/strategy/test_strategy.py index c5fcd46d9..240b83b8b 100644 --- a/freqtrade/tests/strategy/test_strategy.py +++ b/freqtrade/tests/strategy/test_strategy.py @@ -15,7 +15,7 @@ from freqtrade.resolvers import StrategyResolver from freqtrade.strategy import import_strategy from freqtrade.strategy.default_strategy import DefaultStrategy from freqtrade.strategy.interface import IStrategy -from freqtrade.tests.conftest import log_has_re +from freqtrade.tests.conftest import log_has, log_has_re def test_import_strategy(caplog): @@ -35,12 +35,8 @@ def test_import_strategy(caplog): assert imported_strategy.__module__ == 'freqtrade.strategy' assert imported_strategy.some_method() == 42 - assert ( - 'freqtrade.strategy', - logging.DEBUG, - 'Imported strategy freqtrade.strategy.default_strategy.DefaultStrategy ' - 'as freqtrade.strategy.DefaultStrategy', - ) in caplog.record_tuples + assert log_has('Imported strategy freqtrade.strategy.default_strategy.DefaultStrategy ' + 'as freqtrade.strategy.DefaultStrategy', caplog) def test_search_strategy(): @@ -79,8 +75,7 @@ def test_load_strategy_base64(result, caplog, default_conf): assert 'adx' in resolver.strategy.advise_indicators(result, {'pair': 'ETH/BTC'}) # Make sure strategy was loaded from base64 (using temp directory)!! assert log_has_re(r"Using resolved strategy TestStrategy from '" - + tempfile.gettempdir() + r"/.*/TestStrategy\.py'\.\.\.", - caplog.record_tuples) + + tempfile.gettempdir() + r"/.*/TestStrategy\.py'\.\.\.", caplog) def test_load_strategy_invalid_directory(result, caplog, default_conf): @@ -88,7 +83,7 @@ def test_load_strategy_invalid_directory(result, caplog, default_conf): extra_dir = Path.cwd() / 'some/path' resolver._load_strategy('TestStrategy', config=default_conf, extra_dir=extra_dir) - assert log_has_re(r'Path .*' + r'some.*path.*' + r'.* does not exist', caplog.record_tuples) + assert log_has_re(r'Path .*' + r'some.*path.*' + r'.* does not exist', caplog) assert 'adx' in resolver.strategy.advise_indicators(result, {'pair': 'ETH/BTC'}) @@ -108,7 +103,7 @@ def test_load_staticmethod_importerror(mocker, caplog, default_conf): match=r"Impossible to load Strategy 'DefaultStrategy'. " r"This class does not exist or contains Python code errors."): StrategyResolver(default_conf) - assert log_has_re(r".*Error: can't pickle staticmethod objects", caplog.record_tuples) + assert log_has_re(r".*Error: can't pickle staticmethod objects", caplog) def test_strategy(result, default_conf): @@ -146,10 +141,7 @@ def test_strategy_override_minimal_roi(caplog, default_conf): resolver = StrategyResolver(default_conf) assert resolver.strategy.minimal_roi[0] == 0.5 - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'minimal_roi' with value in config file: {'0': 0.5}." - ) in caplog.record_tuples + assert log_has("Override strategy 'minimal_roi' with value in config file: {'0': 0.5}.", caplog) def test_strategy_override_stoploss(caplog, default_conf): @@ -161,10 +153,7 @@ def test_strategy_override_stoploss(caplog, default_conf): resolver = StrategyResolver(default_conf) assert resolver.strategy.stoploss == -0.5 - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'stoploss' with value in config file: -0.5." - ) in caplog.record_tuples + assert log_has("Override strategy 'stoploss' with value in config file: -0.5.", caplog) def test_strategy_override_trailing_stop(caplog, default_conf): @@ -177,10 +166,7 @@ def test_strategy_override_trailing_stop(caplog, default_conf): assert resolver.strategy.trailing_stop assert isinstance(resolver.strategy.trailing_stop, bool) - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'trailing_stop' with value in config file: True." - ) in caplog.record_tuples + assert log_has("Override strategy 'trailing_stop' with value in config file: True.", caplog) def test_strategy_override_trailing_stop_positive(caplog, default_conf): @@ -194,16 +180,12 @@ def test_strategy_override_trailing_stop_positive(caplog, default_conf): resolver = StrategyResolver(default_conf) assert resolver.strategy.trailing_stop_positive == -0.1 - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'trailing_stop_positive' with value in config file: -0.1." - ) in caplog.record_tuples + assert log_has("Override strategy 'trailing_stop_positive' with value in config file: -0.1.", + caplog) assert resolver.strategy.trailing_stop_positive_offset == -0.2 - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'trailing_stop_positive' with value in config file: -0.1." - ) in caplog.record_tuples + assert log_has("Override strategy 'trailing_stop_positive' with value in config file: -0.1.", + caplog) def test_strategy_override_ticker_interval(caplog, default_conf): @@ -218,10 +200,8 @@ def test_strategy_override_ticker_interval(caplog, default_conf): assert resolver.strategy.ticker_interval == 60 assert resolver.strategy.stake_currency == 'ETH' - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'ticker_interval' with value in config file: 60." - ) in caplog.record_tuples + assert log_has("Override strategy 'ticker_interval' with value in config file: 60.", + caplog) def test_strategy_override_process_only_new_candles(caplog, default_conf): @@ -234,10 +214,8 @@ def test_strategy_override_process_only_new_candles(caplog, default_conf): resolver = StrategyResolver(default_conf) assert resolver.strategy.process_only_new_candles - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'process_only_new_candles' with value in config file: True." - ) in caplog.record_tuples + assert log_has("Override strategy 'process_only_new_candles' with value in config file: True.", + caplog) def test_strategy_override_order_types(caplog, default_conf): @@ -259,12 +237,9 @@ def test_strategy_override_order_types(caplog, default_conf): for method in ['buy', 'sell', 'stoploss', 'stoploss_on_exchange']: assert resolver.strategy.order_types[method] == order_types[method] - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'order_types' with value in config file:" - " {'buy': 'market', 'sell': 'limit', 'stoploss': 'limit'," - " 'stoploss_on_exchange': True}." - ) in caplog.record_tuples + assert log_has("Override strategy 'order_types' with value in config file:" + " {'buy': 'market', 'sell': 'limit', 'stoploss': 'limit'," + " 'stoploss_on_exchange': True}.", caplog) default_conf.update({ 'strategy': 'DefaultStrategy', @@ -295,11 +270,8 @@ def test_strategy_override_order_tif(caplog, default_conf): for method in ['buy', 'sell']: assert resolver.strategy.order_time_in_force[method] == order_time_in_force[method] - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'order_time_in_force' with value in config file:" - " {'buy': 'fok', 'sell': 'gtc'}." - ) in caplog.record_tuples + assert log_has("Override strategy 'order_time_in_force' with value in config file:" + " {'buy': 'fok', 'sell': 'gtc'}.", caplog) default_conf.update({ 'strategy': 'DefaultStrategy', @@ -334,10 +306,7 @@ def test_strategy_override_use_sell_signal(caplog, default_conf): assert resolver.strategy.use_sell_signal assert isinstance(resolver.strategy.use_sell_signal, bool) - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'use_sell_signal' with value in config file: True." - ) in caplog.record_tuples + assert log_has("Override strategy 'use_sell_signal' with value in config file: True.", caplog) def test_strategy_override_use_sell_profit_only(caplog, default_conf): @@ -362,10 +331,7 @@ def test_strategy_override_use_sell_profit_only(caplog, default_conf): assert resolver.strategy.sell_profit_only assert isinstance(resolver.strategy.sell_profit_only, bool) - assert ('freqtrade.resolvers.strategy_resolver', - logging.INFO, - "Override strategy 'sell_profit_only' with value in config file: True." - ) in caplog.record_tuples + assert log_has("Override strategy 'sell_profit_only' with value in config file: True.", caplog) @pytest.mark.filterwarnings("ignore:deprecated") diff --git a/freqtrade/tests/test_arguments.py b/freqtrade/tests/test_arguments.py index bf744f72b..2cb7ff6d7 100644 --- a/freqtrade/tests/test_arguments.py +++ b/freqtrade/tests/test_arguments.py @@ -3,7 +3,7 @@ import argparse import pytest -from freqtrade.configuration import Arguments, TimeRange +from freqtrade.configuration import Arguments from freqtrade.configuration.arguments import ARGS_DOWNLOADER, ARGS_PLOT_DATAFRAME from freqtrade.configuration.cli_options import check_int_positive @@ -86,30 +86,6 @@ def test_parse_args_strategy_path_invalid() -> None: Arguments(['--strategy-path'], '').get_parsed_arg() -def test_parse_timerange_incorrect() -> None: - assert TimeRange(None, 'line', 0, -200) == Arguments.parse_timerange('-200') - assert TimeRange('line', None, 200, 0) == Arguments.parse_timerange('200-') - assert TimeRange('index', 'index', 200, 500) == Arguments.parse_timerange('200-500') - - assert TimeRange('date', None, 1274486400, 0) == Arguments.parse_timerange('20100522-') - assert TimeRange(None, 'date', 0, 1274486400) == Arguments.parse_timerange('-20100522') - timerange = Arguments.parse_timerange('20100522-20150730') - assert timerange == TimeRange('date', 'date', 1274486400, 1438214400) - - # Added test for unix timestamp - BTC genesis date - assert TimeRange('date', None, 1231006505, 0) == Arguments.parse_timerange('1231006505-') - assert TimeRange(None, 'date', 0, 1233360000) == Arguments.parse_timerange('-1233360000') - timerange = Arguments.parse_timerange('1231006505-1233360000') - assert TimeRange('date', 'date', 1231006505, 1233360000) == timerange - - # TODO: Find solution for the following case (passing timestamp in ms) - timerange = Arguments.parse_timerange('1231006505000-1233360000000') - assert TimeRange('date', 'date', 1231006505, 1233360000) != timerange - - with pytest.raises(Exception, match=r'Incorrect syntax.*'): - Arguments.parse_timerange('-') - - def test_parse_args_backtesting_invalid() -> None: with pytest.raises(SystemExit, match=r'2'): Arguments(['backtesting --ticker-interval'], '').get_parsed_arg() diff --git a/freqtrade/tests/test_configuration.py b/freqtrade/tests/test_configuration.py index 64843636e..083be3e6e 100644 --- a/freqtrade/tests/test_configuration.py +++ b/freqtrade/tests/test_configuration.py @@ -73,7 +73,7 @@ def test__args_to_config(caplog): # No warnings ... configuration._args_to_config(config, argname="strategy_path", logstring="DeadBeef") assert len(w) == 0 - assert log_has("DeadBeef", caplog.record_tuples) + assert log_has("DeadBeef", caplog) assert config['strategy_path'] == "TestTest" configuration = Configuration(args) @@ -85,7 +85,7 @@ def test__args_to_config(caplog): assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "DEPRECATED: Going away soon!" in str(w[-1].message) - assert log_has("DeadBeef", caplog.record_tuples) + assert log_has("DeadBeef", caplog) assert config['strategy_path'] == "TestTest" @@ -99,7 +99,7 @@ def test_load_config_max_open_trades_zero(default_conf, mocker, caplog) -> None: assert validated_conf['max_open_trades'] == 0 assert 'internals' in validated_conf - assert log_has('Validating configuration ...', caplog.record_tuples) + assert log_has('Validating configuration ...', caplog) def test_load_config_combine_dicts(default_conf, mocker, caplog) -> None: @@ -131,7 +131,36 @@ def test_load_config_combine_dicts(default_conf, mocker, caplog) -> None: assert validated_conf['exchange']['pair_whitelist'] == conf2['exchange']['pair_whitelist'] assert 'internals' in validated_conf - assert log_has('Validating configuration ...', caplog.record_tuples) + assert log_has('Validating configuration ...', caplog) + + +def test_from_config(default_conf, mocker, caplog) -> None: + conf1 = deepcopy(default_conf) + conf2 = deepcopy(default_conf) + del conf1['exchange']['key'] + del conf1['exchange']['secret'] + del conf2['exchange']['name'] + conf2['exchange']['pair_whitelist'] += ['NANO/BTC'] + conf2['fiat_display_currency'] = "EUR" + config_files = [conf1, conf2] + + configsmock = MagicMock(side_effect=config_files) + mocker.patch( + 'freqtrade.configuration.configuration.load_config_file', + configsmock + ) + + validated_conf = Configuration.from_files(['test_conf.json', 'test2_conf.json']) + + exchange_conf = default_conf['exchange'] + assert validated_conf['exchange']['name'] == exchange_conf['name'] + assert validated_conf['exchange']['key'] == exchange_conf['key'] + assert validated_conf['exchange']['secret'] == exchange_conf['secret'] + assert validated_conf['exchange']['pair_whitelist'] != conf1['exchange']['pair_whitelist'] + assert validated_conf['exchange']['pair_whitelist'] == conf2['exchange']['pair_whitelist'] + assert validated_conf['fiat_display_currency'] == "EUR" + assert 'internals' in validated_conf + assert log_has('Validating configuration ...', caplog) def test_load_config_max_open_trades_minus_one(default_conf, mocker, caplog) -> None: @@ -144,7 +173,7 @@ def test_load_config_max_open_trades_minus_one(default_conf, mocker, caplog) -> assert validated_conf['max_open_trades'] > 999999999 assert validated_conf['max_open_trades'] == float('inf') - assert log_has('Validating configuration ...', caplog.record_tuples) + assert log_has('Validating configuration ...', caplog) assert "runmode" in validated_conf assert validated_conf['runmode'] == RunMode.DRY_RUN @@ -281,8 +310,8 @@ def test_show_info(default_conf, mocker, caplog) -> None: configuration = Configuration(args) configuration.get_config() - assert log_has('Using DB: "sqlite:///tmp/testdb"', caplog.record_tuples) - assert log_has('Dry run is enabled', caplog.record_tuples) + assert log_has('Using DB: "sqlite:///tmp/testdb"', caplog) + assert log_has('Dry run is enabled', caplog) def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None: @@ -305,21 +334,18 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> assert 'pair_whitelist' in config['exchange'] assert 'datadir' in config assert 'user_data_dir' in config - assert log_has( - 'Using data directory: {} ...'.format(config['datadir']), - caplog.record_tuples - ) + assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog) assert 'ticker_interval' in config - assert not log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples) + assert not log_has('Parameter -i/--ticker-interval detected ...', caplog) assert 'live' not in config - assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples) + assert not log_has('Parameter -l/--live detected ...', caplog) assert 'position_stacking' not in config - assert not log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples) + assert not log_has('Parameter --enable-position-stacking detected ...', caplog) assert 'refresh_pairs' not in config - assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples) + assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog) assert 'timerange' not in config assert 'export' not in config @@ -361,43 +387,31 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non assert 'exchange' in config assert 'pair_whitelist' in config['exchange'] assert 'datadir' in config - assert log_has( - 'Using data directory: {} ...'.format("/foo/bar"), - caplog.record_tuples - ) - assert log_has( - 'Using user-data directory: {} ...'.format("/tmp/freqtrade"), - caplog.record_tuples - ) + assert log_has('Using data directory: {} ...'.format("/foo/bar"), caplog) + assert log_has('Using user-data directory: {} ...'.format("/tmp/freqtrade"), caplog) assert 'user_data_dir' in config assert 'ticker_interval' in config assert log_has('Parameter -i/--ticker-interval detected ... Using ticker_interval: 1m ...', - caplog.record_tuples) + caplog) assert 'live' in config - assert log_has('Parameter -l/--live detected ...', caplog.record_tuples) + assert log_has('Parameter -l/--live detected ...', caplog) assert 'position_stacking'in config - assert log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples) + assert log_has('Parameter --enable-position-stacking detected ...', caplog) assert 'use_max_market_positions' in config - assert log_has('Parameter --disable-max-market-positions detected ...', caplog.record_tuples) - assert log_has('max_open_trades set to unlimited ...', caplog.record_tuples) + assert log_has('Parameter --disable-max-market-positions detected ...', caplog) + assert log_has('max_open_trades set to unlimited ...', caplog) assert 'refresh_pairs'in config - assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples) + assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog) assert 'timerange' in config - assert log_has( - 'Parameter --timerange detected: {} ...'.format(config['timerange']), - caplog.record_tuples - ) + assert log_has('Parameter --timerange detected: {} ...'.format(config['timerange']), caplog) assert 'export' in config - assert log_has( - 'Parameter --export detected: {} ...'.format(config['export']), - caplog.record_tuples - ) + assert log_has('Parameter --export detected: {} ...'.format(config['export']), caplog) def test_setup_configuration_with_stratlist(mocker, default_conf, caplog) -> None: @@ -427,16 +441,13 @@ def test_setup_configuration_with_stratlist(mocker, default_conf, caplog) -> Non assert 'exchange' in config assert 'pair_whitelist' in config['exchange'] assert 'datadir' in config - assert log_has( - 'Using data directory: {} ...'.format(config['datadir']), - caplog.record_tuples - ) + assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog) assert 'ticker_interval' in config assert log_has('Parameter -i/--ticker-interval detected ... Using ticker_interval: 1m ...', - caplog.record_tuples) + caplog) assert 'strategy_list' in config - assert log_has('Using strategy list of 2 Strategies', caplog.record_tuples) + assert log_has('Using strategy list of 2 Strategies', caplog) assert 'position_stacking' not in config @@ -445,10 +456,7 @@ def test_setup_configuration_with_stratlist(mocker, default_conf, caplog) -> Non assert 'timerange' not in config assert 'export' in config - assert log_has( - 'Parameter --export detected: {} ...'.format(config['export']), - caplog.record_tuples - ) + assert log_has('Parameter --export detected: {} ...'.format(config['export']), caplog) def test_hyperopt_with_arguments(mocker, default_conf, caplog) -> None: @@ -467,11 +475,11 @@ def test_hyperopt_with_arguments(mocker, default_conf, caplog) -> None: assert 'epochs' in config assert int(config['epochs']) == 10 assert log_has('Parameter --epochs detected ... Will run Hyperopt with for 10 epochs ...', - caplog.record_tuples) + caplog) assert 'spaces' in config assert config['spaces'] == ['all'] - assert log_has('Parameter -s/--spaces detected: [\'all\']', caplog.record_tuples) + assert log_has('Parameter -s/--spaces detected: [\'all\']', caplog) assert "runmode" in config assert config['runmode'] == RunMode.HYPEROPT @@ -481,38 +489,35 @@ def test_check_exchange(default_conf, caplog) -> None: default_conf.get('exchange').update({'name': 'BITTREX'}) assert check_exchange(default_conf) assert log_has_re(r"Exchange .* is officially supported by the Freqtrade development team\.", - caplog.record_tuples) + caplog) caplog.clear() # Test an officially supported by Freqtrade team exchange default_conf.get('exchange').update({'name': 'binance'}) assert check_exchange(default_conf) assert log_has_re(r"Exchange .* is officially supported by the Freqtrade development team\.", - caplog.record_tuples) + caplog) caplog.clear() # Test an available exchange, supported by ccxt default_conf.get('exchange').update({'name': 'kraken'}) assert check_exchange(default_conf) assert log_has_re(r"Exchange .* is supported by ccxt and .* not officially supported " - r"by the Freqtrade development team\. .*", - caplog.record_tuples) + r"by the Freqtrade development team\. .*", caplog) caplog.clear() # Test a 'bad' exchange, which known to have serious problems default_conf.get('exchange').update({'name': 'bitmex'}) - assert not check_exchange(default_conf) - assert log_has_re(r"Exchange .* is known to not work with the bot yet\. " - r"Use it only for development and testing purposes\.", - caplog.record_tuples) + with pytest.raises(OperationalException, + match=r"Exchange .* is known to not work with the bot yet.*"): + check_exchange(default_conf) caplog.clear() # Test a 'bad' exchange with check_for_bad=False default_conf.get('exchange').update({'name': 'bitmex'}) assert check_exchange(default_conf, False) assert log_has_re(r"Exchange .* is supported by ccxt and .* not officially supported " - r"by the Freqtrade development team\. .*", - caplog.record_tuples) + r"by the Freqtrade development team\. .*", caplog) caplog.clear() # Test an invalid exchange @@ -538,7 +543,7 @@ def test_cli_verbose_with_params(default_conf, mocker, caplog) -> None: validated_conf = configuration.load_config() assert validated_conf.get('verbosity') == 3 - assert log_has('Verbosity set to 3', caplog.record_tuples) + assert log_has('Verbosity set to 3', caplog) def test_set_loggers() -> None: @@ -604,7 +609,7 @@ def test_load_config_warn_forcebuy(default_conf, mocker, caplog) -> None: validated_conf = configuration.load_config() assert validated_conf.get('forcebuy_enable') - assert log_has('`forcebuy` RPC message enabled.', caplog.record_tuples) + assert log_has('`forcebuy` RPC message enabled.', caplog) def test_validate_default_conf(default_conf) -> None: @@ -617,7 +622,7 @@ def test_create_datadir(mocker, default_conf, caplog) -> None: create_datadir(default_conf, '/foo/bar') assert md.call_args[1]['parents'] is True - assert log_has('Created data directory: /foo/bar', caplog.record_tuples) + assert log_has('Created data directory: /foo/bar', caplog) def test_create_userdata_dir(mocker, default_conf, caplog) -> None: @@ -675,6 +680,17 @@ def test_validate_tsl(default_conf): configuration._validate_config_consistency(default_conf) +def test_load_config_test_comments() -> None: + """ + Load config with comments + """ + config_file = Path(__file__).parents[0] / "config_test_comments.json" + print(config_file) + conf = load_config_file(str(config_file)) + + assert conf + + def test_load_config_default_exchange(all_conf) -> None: """ config['exchange'] subtree has required options in it diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 1a4c5159c..24d070d2d 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -16,7 +16,7 @@ from freqtrade.data.dataprovider import DataProvider from freqtrade.freqtradebot import FreqtradeBot from freqtrade.persistence import Trade from freqtrade.rpc import RPCMessageType -from freqtrade.state import State +from freqtrade.state import State, RunMode from freqtrade.strategy.interface import SellCheckTuple, SellType from freqtrade.tests.conftest import (get_patched_freqtradebot, get_patched_worker, log_has, log_has_re, @@ -63,7 +63,7 @@ def test_cleanup(mocker, default_conf, caplog) -> None: mocker.patch('freqtrade.persistence.cleanup', mock_cleanup) freqtrade = get_patched_freqtradebot(mocker, default_conf) freqtrade.cleanup() - assert log_has('Cleaning up modules ...', caplog.record_tuples) + assert log_has('Cleaning up modules ...', caplog) assert mock_cleanup.call_count == 1 @@ -76,7 +76,7 @@ def test_worker_running(mocker, default_conf, caplog) -> None: state = worker._worker(old_state=None) assert state is State.RUNNING - assert log_has('Changing state to: RUNNING', caplog.record_tuples) + assert log_has('Changing state to: RUNNING', caplog) assert mock_throttle.call_count == 1 # Check strategy is loaded, and received a dataprovider object assert worker.freqtrade.strategy @@ -93,7 +93,7 @@ def test_worker_stopped(mocker, default_conf, caplog) -> None: worker.state = State.STOPPED state = worker._worker(old_state=State.RUNNING) assert state is State.STOPPED - assert log_has('Changing state to: STOPPED', caplog.record_tuples) + assert log_has('Changing state to: STOPPED', caplog) assert mock_throttle.call_count == 0 assert mock_sleep.call_count == 1 @@ -111,7 +111,7 @@ def test_throttle(mocker, default_conf, caplog) -> None: assert result == 42 assert end - start > 0.1 - assert log_has('Throttling throttled_func for 0.10 seconds', caplog.record_tuples) + assert log_has('Throttling throttled_func for 0.10 seconds', caplog) result = worker._throttle(throttled_func, min_secs=-1) assert result == 42 @@ -130,7 +130,77 @@ def test_throttle_with_assets(mocker, default_conf) -> None: assert result == -1 -def test_get_trade_stake_amount(default_conf, ticker, limit_buy_order, fee, mocker) -> None: +def test_order_dict_dry_run(default_conf, mocker, caplog) -> None: + patch_RPCManager(mocker) + patch_exchange(mocker) + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + get_balance=MagicMock(return_value=default_conf['stake_amount'] * 2) + ) + conf = default_conf.copy() + conf['runmode'] = RunMode.DRY_RUN + conf['order_types'] = { + 'buy': 'market', + 'sell': 'limit', + 'stoploss': 'limit', + 'stoploss_on_exchange': True, + } + + freqtrade = FreqtradeBot(conf) + assert log_has("Disabling stoploss_on_exchange during dry-run.", caplog) + assert not freqtrade.strategy.order_types['stoploss_on_exchange'] + + caplog.clear() + # is left untouched + conf = default_conf.copy() + conf['runmode'] = RunMode.DRY_RUN + conf['order_types'] = { + 'buy': 'market', + 'sell': 'limit', + 'stoploss': 'limit', + 'stoploss_on_exchange': False, + } + freqtrade = FreqtradeBot(conf) + assert not freqtrade.strategy.order_types['stoploss_on_exchange'] + assert not log_has_re(".*stoploss_on_exchange .* dry-run", caplog) + + +def test_order_dict_live(default_conf, mocker, caplog) -> None: + patch_RPCManager(mocker) + patch_exchange(mocker) + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + get_balance=MagicMock(return_value=default_conf['stake_amount'] * 2) + ) + conf = default_conf.copy() + conf['runmode'] = RunMode.LIVE + conf['order_types'] = { + 'buy': 'market', + 'sell': 'limit', + 'stoploss': 'limit', + 'stoploss_on_exchange': True, + } + + freqtrade = FreqtradeBot(conf) + assert not log_has_re(".*stoploss_on_exchange .* dry-run", caplog) + assert freqtrade.strategy.order_types['stoploss_on_exchange'] + + caplog.clear() + # is left untouched + conf = default_conf.copy() + conf['runmode'] = RunMode.LIVE + conf['order_types'] = { + 'buy': 'market', + 'sell': 'limit', + 'stoploss': 'limit', + 'stoploss_on_exchange': False, + } + freqtrade = FreqtradeBot(conf) + assert not freqtrade.strategy.order_types['stoploss_on_exchange'] + assert not log_has_re(".*stoploss_on_exchange .* dry-run", caplog) + + +def test_get_trade_stake_amount(default_conf, ticker, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( @@ -183,13 +253,13 @@ def test_get_trade_stake_amount_unlimited_amount(default_conf, assert result == default_conf['stake_amount'] / conf['max_open_trades'] # create one trade, order amount should be 'balance / (max_open_trades - num_open_trades)' - freqtrade.create_trade() + freqtrade.execute_buy('ETH/BTC', result) result = freqtrade._get_trade_stake_amount('LTC/BTC') assert result == default_conf['stake_amount'] / (conf['max_open_trades'] - 1) # create 2 trades, order amount should be None - freqtrade.create_trade() + freqtrade.execute_buy('LTC/BTC', result) result = freqtrade._get_trade_stake_amount('XRP/BTC') assert result is None @@ -231,6 +301,7 @@ def test_edge_overrides_stoploss(limit_buy_order, fee, markets, caplog, mocker, patch_RPCManager(mocker) patch_exchange(mocker) patch_edge(mocker) + edge_conf['max_open_trades'] = float('inf') # Strategy stoploss is -0.1 but Edge imposes a stoploss at -0.2 # Thus, if price falls 21%, stoploss should be triggered @@ -255,14 +326,14 @@ def test_edge_overrides_stoploss(limit_buy_order, fee, markets, caplog, mocker, freqtrade.active_pair_whitelist = ['NEO/BTC'] patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) ############################################# # stoploss shoud be hit assert freqtrade.handle_trade(trade) is True - assert log_has('executed sell, reason: SellType.STOP_LOSS', caplog.record_tuples) + assert log_has('executed sell, reason: SellType.STOP_LOSS', caplog) assert trade.sell_reason == SellType.STOP_LOSS.value @@ -271,6 +342,7 @@ def test_edge_should_ignore_strategy_stoploss(limit_buy_order, fee, markets, patch_RPCManager(mocker) patch_exchange(mocker) patch_edge(mocker) + edge_conf['max_open_trades'] = float('inf') # Strategy stoploss is -0.1 but Edge imposes a stoploss at -0.2 # Thus, if price falls 15%, stoploss should not be triggered @@ -295,7 +367,7 @@ def test_edge_should_ignore_strategy_stoploss(limit_buy_order, fee, markets, freqtrade.active_pair_whitelist = ['NEO/BTC'] patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) ############################################# @@ -309,6 +381,7 @@ def test_total_open_trades_stakes(mocker, default_conf, ticker, patch_RPCManager(mocker) patch_exchange(mocker) default_conf['stake_amount'] = 0.0000098751 + default_conf['max_open_trades'] = 2 mocker.patch.multiple( 'freqtrade.exchange.Exchange', get_ticker=ticker, @@ -318,7 +391,7 @@ def test_total_open_trades_stakes(mocker, default_conf, ticker, ) freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade is not None @@ -326,7 +399,7 @@ def test_total_open_trades_stakes(mocker, default_conf, ticker, assert trade.is_open assert trade.open_date is not None - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.order_by(Trade.id.desc()).first() assert trade is not None @@ -449,7 +522,7 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None: assert result == min(8, 2 * 2) / 0.9 -def test_create_trade(default_conf, ticker, limit_buy_order, fee, markets, mocker) -> None: +def test_create_trades(default_conf, ticker, limit_buy_order, fee, markets, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( @@ -464,7 +537,7 @@ def test_create_trade(default_conf, ticker, limit_buy_order, fee, markets, mocke whitelist = deepcopy(default_conf['exchange']['pair_whitelist']) freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade is not None @@ -482,8 +555,8 @@ def test_create_trade(default_conf, ticker, limit_buy_order, fee, markets, mocke assert whitelist == default_conf['exchange']['pair_whitelist'] -def test_create_trade_no_stake_amount(default_conf, ticker, limit_buy_order, - fee, markets, mocker) -> None: +def test_create_trades_no_stake_amount(default_conf, ticker, limit_buy_order, + fee, markets, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) patch_wallet(mocker, free=default_conf['stake_amount'] * 0.5) @@ -498,11 +571,11 @@ def test_create_trade_no_stake_amount(default_conf, ticker, limit_buy_order, patch_get_signal(freqtrade) with pytest.raises(DependencyException, match=r'.*stake amount.*'): - freqtrade.create_trade() + freqtrade.create_trades() -def test_create_trade_minimal_amount(default_conf, ticker, limit_buy_order, - fee, markets, mocker) -> None: +def test_create_trades_minimal_amount(default_conf, ticker, limit_buy_order, + fee, markets, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) buy_mock = MagicMock(return_value={'id': limit_buy_order['id']}) @@ -517,13 +590,13 @@ def test_create_trade_minimal_amount(default_conf, ticker, limit_buy_order, freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() rate, amount = buy_mock.call_args[1]['rate'], buy_mock.call_args[1]['amount'] assert rate * amount >= default_conf['stake_amount'] -def test_create_trade_too_small_stake_amount(default_conf, ticker, limit_buy_order, - fee, markets, mocker) -> None: +def test_create_trades_too_small_stake_amount(default_conf, ticker, limit_buy_order, + fee, markets, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) buy_mock = MagicMock(return_value={'id': limit_buy_order['id']}) @@ -539,11 +612,11 @@ def test_create_trade_too_small_stake_amount(default_conf, ticker, limit_buy_ord freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - assert not freqtrade.create_trade() + assert not freqtrade.create_trades() -def test_create_trade_limit_reached(default_conf, ticker, limit_buy_order, - fee, markets, mocker) -> None: +def test_create_trades_limit_reached(default_conf, ticker, limit_buy_order, + fee, markets, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( @@ -560,12 +633,12 @@ def test_create_trade_limit_reached(default_conf, ticker, limit_buy_order, freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - assert not freqtrade.create_trade() + assert not freqtrade.create_trades() assert freqtrade._get_trade_stake_amount('ETH/BTC') is None -def test_create_trade_no_pairs_let(default_conf, ticker, limit_buy_order, fee, - markets, mocker, caplog) -> None: +def test_create_trades_no_pairs_let(default_conf, ticker, limit_buy_order, fee, + markets, mocker, caplog) -> None: patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( @@ -580,14 +653,13 @@ def test_create_trade_no_pairs_let(default_conf, ticker, limit_buy_order, fee, freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - assert freqtrade.create_trade() - assert not freqtrade.create_trade() - assert log_has("No currency pair in whitelist, but checking to sell open trades.", - caplog.record_tuples) + assert freqtrade.create_trades() + assert not freqtrade.create_trades() + assert log_has("No currency pair in whitelist, but checking to sell open trades.", caplog) -def test_create_trade_no_pairs_in_whitelist(default_conf, ticker, limit_buy_order, fee, - markets, mocker, caplog) -> None: +def test_create_trades_no_pairs_in_whitelist(default_conf, ticker, limit_buy_order, fee, + markets, mocker, caplog) -> None: patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( @@ -601,11 +673,11 @@ def test_create_trade_no_pairs_in_whitelist(default_conf, ticker, limit_buy_orde freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - assert not freqtrade.create_trade() - assert log_has("Whitelist is empty.", caplog.record_tuples) + assert not freqtrade.create_trades() + assert log_has("Whitelist is empty.", caplog) -def test_create_trade_no_signal(default_conf, fee, mocker) -> None: +def test_create_trades_no_signal(default_conf, fee, mocker) -> None: default_conf['dry_run'] = True patch_RPCManager(mocker) @@ -621,7 +693,56 @@ def test_create_trade_no_signal(default_conf, fee, mocker) -> None: Trade.query = MagicMock() Trade.query.filter = MagicMock() - assert not freqtrade.create_trade() + assert not freqtrade.create_trades() + + +@pytest.mark.parametrize("max_open", range(0, 5)) +def test_create_trades_multiple_trades(default_conf, ticker, + fee, markets, mocker, max_open) -> None: + patch_RPCManager(mocker) + patch_exchange(mocker) + default_conf['max_open_trades'] = max_open + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + get_ticker=ticker, + buy=MagicMock(return_value={'id': "12355555"}), + get_fee=fee, + markets=PropertyMock(return_value=markets) + ) + freqtrade = FreqtradeBot(default_conf) + patch_get_signal(freqtrade) + + freqtrade.create_trades() + + trades = Trade.get_open_trades() + assert len(trades) == max_open + + +def test_create_trades_preopen(default_conf, ticker, fee, markets, mocker) -> None: + patch_RPCManager(mocker) + patch_exchange(mocker) + default_conf['max_open_trades'] = 4 + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + get_ticker=ticker, + buy=MagicMock(return_value={'id': "12355555"}), + get_fee=fee, + markets=PropertyMock(return_value=markets) + ) + freqtrade = FreqtradeBot(default_conf) + patch_get_signal(freqtrade) + + # Create 2 existing trades + freqtrade.execute_buy('ETH/BTC', default_conf['stake_amount']) + freqtrade.execute_buy('NEO/BTC', default_conf['stake_amount']) + + assert len(Trade.get_open_trades()) == 2 + + # Create 2 new trades using create_trades + assert freqtrade.create_trades() + + trades = Trade.get_open_trades() + assert len(trades) == 4 def test_process_trade_creation(default_conf, ticker, limit_buy_order, @@ -642,8 +763,7 @@ def test_process_trade_creation(default_conf, ticker, limit_buy_order, trades = Trade.query.filter(Trade.is_open.is_(True)).all() assert not trades - result = freqtrade.process() - assert result is True + freqtrade.process() trades = Trade.query.filter(Trade.is_open.is_(True)).all() assert len(trades) == 1 @@ -657,8 +777,7 @@ def test_process_trade_creation(default_conf, ticker, limit_buy_order, assert trade.amount == 90.99181073703367 assert log_has( - 'Buy signal found: about create a new trade with stake_amount: 0.001 ...', - caplog.record_tuples + 'Buy signal found: about create a new trade with stake_amount: 0.001 ...', caplog ) @@ -676,8 +795,7 @@ def test_process_exchange_failures(default_conf, ticker, markets, mocker) -> Non worker = Worker(args=None, config=default_conf) patch_get_signal(worker.freqtrade) - result = worker._process() - assert result is False + worker._process() assert sleep_mock.has_calls() @@ -695,8 +813,7 @@ def test_process_operational_exception(default_conf, ticker, markets, mocker) -> assert worker.state == State.RUNNING - result = worker._process() - assert result is False + worker._process() assert worker.state == State.STOPPED assert 'OperationalException' in msg_mock.call_args_list[-1][0][0]['status'] @@ -718,13 +835,14 @@ def test_process_trade_handling( trades = Trade.query.filter(Trade.is_open.is_(True)).all() assert not trades - result = freqtrade.process() - assert result is True + freqtrade.process() + trades = Trade.query.filter(Trade.is_open.is_(True)).all() assert len(trades) == 1 - result = freqtrade.process() - assert result is False + # Nothing happened ... + freqtrade.process() + assert len(trades) == 1 def test_process_trade_no_whitelist_pair( @@ -766,11 +884,10 @@ def test_process_trade_no_whitelist_pair( )) assert pair not in freqtrade.active_pair_whitelist - result = freqtrade.process() + freqtrade.process() assert pair in freqtrade.active_pair_whitelist # Make sure each pair is only in the list once assert len(freqtrade.active_pair_whitelist) == len(set(freqtrade.active_pair_whitelist)) - assert result is True def test_process_informative_pairs_added(default_conf, ticker, markets, mocker) -> None: @@ -1010,7 +1127,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf, fee, caplog, # Fourth case: when stoploss is set and it is hit # should unset stoploss_order_id and return true # as a trade actually happened - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.is_open = True trade.open_order_id = None @@ -1025,7 +1142,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf, fee, caplog, }) mocker.patch('freqtrade.exchange.Exchange.get_order', stoploss_order_hit) assert freqtrade.handle_stoploss_on_exchange(trade) is True - assert log_has('STOP_LOSS_LIMIT is hit for {}.'.format(trade), caplog.record_tuples) + assert log_has('STOP_LOSS_LIMIT is hit for {}.'.format(trade), caplog) assert trade.stoploss_order_id is None assert trade.is_open is False @@ -1034,7 +1151,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf, fee, caplog, side_effect=DependencyException() ) freqtrade.handle_stoploss_on_exchange(trade) - assert log_has('Unable to place a stoploss order on exchange: ', caplog.record_tuples) + assert log_has('Unable to place a stoploss order on exchange: ', caplog) # Fifth case: get_order returns InvalidOrder # It should try to add stoploss order @@ -1085,7 +1202,7 @@ def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee, caplog, patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.is_open = True trade.open_order_id = None @@ -1175,7 +1292,7 @@ def test_handle_stoploss_on_exchange_trailing_error(mocker, default_conf, fee, c # setting stoploss_on_exchange_interval to 60 seconds freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 60 patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.is_open = True trade.open_order_id = None @@ -1196,8 +1313,7 @@ def test_handle_stoploss_on_exchange_trailing_error(mocker, default_conf, fee, c mocker.patch('freqtrade.exchange.Exchange.cancel_order', side_effect=InvalidOrderException()) mocker.patch('freqtrade.exchange.Exchange.get_order', stoploss_order_hanging) freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging) - assert log_has_re(r"Could not cancel stoploss order abcd for pair ETH/BTC.*", - caplog.record_tuples) + assert log_has_re(r"Could not cancel stoploss order abcd for pair ETH/BTC.*", caplog) # Still try to create order assert stoploss_limit.call_count == 1 @@ -1208,8 +1324,7 @@ def test_handle_stoploss_on_exchange_trailing_error(mocker, default_conf, fee, c mocker.patch("freqtrade.exchange.Exchange.stoploss_limit", side_effect=DependencyException()) freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging) assert cancel_mock.call_count == 1 - assert log_has_re(r"Could create trailing stoploss order for pair ETH/BTC\..*", - caplog.record_tuples) + assert log_has_re(r"Could create trailing stoploss order for pair ETH/BTC\..*", caplog) def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, caplog, @@ -1220,7 +1335,7 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, caplog, patch_RPCManager(mocker) patch_exchange(mocker) patch_edge(mocker) - + edge_conf['max_open_trades'] = float('inf') mocker.patch.multiple( 'freqtrade.exchange.Exchange', get_ticker=MagicMock(return_value={ @@ -1258,7 +1373,7 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, caplog, freqtrade.active_pair_whitelist = freqtrade.edge.adjust(freqtrade.active_pair_whitelist) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.is_open = True trade.open_order_id = None @@ -1322,25 +1437,23 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, caplog, stop_price=0.00002344 * 0.99) -def test_process_maybe_execute_buy(mocker, default_conf) -> None: +def test_process_maybe_execute_buy(mocker, default_conf, caplog) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf) - mocker.patch('freqtrade.freqtradebot.FreqtradeBot.create_trade', MagicMock(return_value=True)) - assert freqtrade.process_maybe_execute_buy() - - mocker.patch('freqtrade.freqtradebot.FreqtradeBot.create_trade', MagicMock(return_value=False)) - assert not freqtrade.process_maybe_execute_buy() + mocker.patch('freqtrade.freqtradebot.FreqtradeBot.create_trades', MagicMock(return_value=False)) + freqtrade.process_maybe_execute_buy() + assert log_has('Found no buy signals for whitelisted currencies. Trying again...', caplog) def test_process_maybe_execute_buy_exception(mocker, default_conf, caplog) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf) mocker.patch( - 'freqtrade.freqtradebot.FreqtradeBot.create_trade', + 'freqtrade.freqtradebot.FreqtradeBot.create_trades', MagicMock(side_effect=DependencyException) ) freqtrade.process_maybe_execute_buy() - log_has('Unable to create trade:', caplog.record_tuples) + assert log_has('Unable to create trade: ', caplog) def test_process_maybe_execute_sell(mocker, default_conf, limit_buy_order, caplog) -> None: @@ -1358,8 +1471,7 @@ def test_process_maybe_execute_sell(mocker, default_conf, limit_buy_order, caplo assert not freqtrade.process_maybe_execute_sell(trade) # Test amount not modified by fee-logic assert not log_has( - 'Applying fee to amount for Trade {} from 90.99181073 to 90.81'.format(trade), - caplog.record_tuples + 'Applying fee to amount for Trade {} from 90.99181073 to 90.81'.format(trade), caplog ) mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount', return_value=90.81) @@ -1382,7 +1494,7 @@ def test_process_maybe_execute_sell_exception(mocker, default_conf, side_effect=DependencyException() ) freqtrade.process_maybe_execute_sell(trade) - assert log_has('Unable to sell trade: ', caplog.record_tuples) + assert log_has('Unable to sell trade: ', caplog) def test_update_trade_state(mocker, default_conf, limit_buy_order, caplog) -> None: @@ -1401,7 +1513,7 @@ def test_update_trade_state(mocker, default_conf, limit_buy_order, caplog) -> No trade.open_fee = 0.001 freqtrade.update_trade_state(trade) # Test amount not modified by fee-logic - assert not log_has_re(r'Applying fee to .*', caplog.record_tuples) + assert not log_has_re(r'Applying fee to .*', caplog) assert trade.open_order_id is None assert trade.amount == limit_buy_order['amount'] @@ -1418,7 +1530,7 @@ def test_update_trade_state(mocker, default_conf, limit_buy_order, caplog) -> No # Assert we call handle_trade() if trade is feasible for execution freqtrade.update_trade_state(trade) - assert log_has_re('Found open order for.*', caplog.record_tuples) + assert log_has_re('Found open order for.*', caplog) def test_update_trade_state_withorderdict(default_conf, trades_for_order, limit_buy_order, mocker): @@ -1457,7 +1569,7 @@ def test_update_trade_state_exception(mocker, default_conf, side_effect=OperationalException() ) freqtrade.update_trade_state(trade) - assert log_has('Could not update trade amount: ', caplog.record_tuples) + assert log_has('Could not update trade amount: ', caplog) def test_update_trade_state_orderexception(mocker, default_conf, caplog) -> None: @@ -1473,7 +1585,7 @@ def test_update_trade_state_orderexception(mocker, default_conf, caplog) -> None grm_mock = mocker.patch("freqtrade.freqtradebot.FreqtradeBot.get_real_amount", MagicMock()) freqtrade.update_trade_state(trade) assert grm_mock.call_count == 0 - assert log_has(f'Unable to fetch order {trade.open_order_id}: ', caplog.record_tuples) + assert log_has(f'Unable to fetch order {trade.open_order_id}: ', caplog) def test_update_trade_state_sell(default_conf, trades_for_order, limit_sell_order, mocker): @@ -1524,7 +1636,7 @@ def test_handle_trade(default_conf, limit_buy_order, limit_sell_order, freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade @@ -1564,7 +1676,7 @@ def test_handle_overlpapping_signals(default_conf, ticker, limit_buy_order, patch_get_signal(freqtrade, value=(True, True)) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() # Buy and Sell triggering, so doing nothing ... trades = Trade.query.all() @@ -1573,7 +1685,7 @@ def test_handle_overlpapping_signals(default_conf, ticker, limit_buy_order, # Buy is triggering, so buying ... patch_get_signal(freqtrade, value=(True, False)) - freqtrade.create_trade() + freqtrade.create_trades() trades = Trade.query.all() nb_trades = len(trades) assert nb_trades == 1 @@ -1620,7 +1732,7 @@ def test_handle_trade_roi(default_conf, ticker, limit_buy_order, patch_get_signal(freqtrade, value=(True, False)) freqtrade.strategy.min_roi_reached = MagicMock(return_value=True) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.is_open = True @@ -1632,7 +1744,7 @@ def test_handle_trade_roi(default_conf, ticker, limit_buy_order, # if ROI is reached we must sell patch_get_signal(freqtrade, value=(False, True)) assert freqtrade.handle_trade(trade) - assert log_has('Required profit reached. Selling..', caplog.record_tuples) + assert log_has('Required profit reached. Selling..', caplog) def test_handle_trade_experimental( @@ -1652,7 +1764,7 @@ def test_handle_trade_experimental( freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.is_open = True @@ -1662,7 +1774,7 @@ def test_handle_trade_experimental( patch_get_signal(freqtrade, value=(False, True)) assert freqtrade.handle_trade(trade) - assert log_has('Sell signal received. Selling..', caplog.record_tuples) + assert log_has('Sell signal received. Selling..', caplog) def test_close_trade(default_conf, ticker, limit_buy_order, limit_sell_order, @@ -1680,7 +1792,7 @@ def test_close_trade(default_conf, ticker, limit_buy_order, limit_sell_order, patch_get_signal(freqtrade) # Create trade and sell it - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade @@ -1768,7 +1880,7 @@ def test_check_handle_cancelled_buy(default_conf, ticker, limit_buy_order_old, trades = Trade.query.filter(Trade.open_order_id.is_(trade_buy.open_order_id)).all() nb_trades = len(trades) assert nb_trades == 0 - assert log_has_re("Buy order canceled on Exchange for Trade.*", caplog.record_tuples) + assert log_has_re("Buy order canceled on Exchange for Trade.*", caplog) def test_check_handle_timedout_buy_exception(default_conf, ticker, limit_buy_order_old, @@ -1881,7 +1993,7 @@ def test_check_handle_cancelled_sell(default_conf, ticker, limit_sell_order_old, assert cancel_order_mock.call_count == 0 assert rpc_mock.call_count == 1 assert trade_sell.is_open is True - assert log_has_re("Sell order canceled on exchange for Trade.*", caplog.record_tuples) + assert log_has_re("Sell order canceled on exchange for Trade.*", caplog) def test_check_handle_timedout_partial(default_conf, ticker, limit_buy_order_old_partial, @@ -1959,7 +2071,7 @@ def test_check_handle_timedout_exception(default_conf, ticker, mocker, caplog) - freqtrade.check_handle_timedout() assert log_has_re(r'Cannot query order for Trade\(id=1, pair=ETH/BTC, amount=90.99181073, ' r'open_rate=0.00001099, open_since=10 hours ago\) due to Traceback \(most ' - r'recent call last\):\n.*', caplog.record_tuples) + r'recent call last\):\n.*', caplog) def test_handle_timedout_limit_buy(mocker, default_conf) -> None: @@ -2020,7 +2132,7 @@ def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, markets, moc patch_get_signal(freqtrade) # Create some test data - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade @@ -2066,7 +2178,7 @@ def test_execute_sell_down(default_conf, ticker, fee, ticker_sell_down, markets, patch_get_signal(freqtrade) # Create some test data - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade @@ -2115,7 +2227,7 @@ def test_execute_sell_down_stoploss_on_exchange_dry_run(default_conf, ticker, fe patch_get_signal(freqtrade) # Create some test data - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade @@ -2172,7 +2284,7 @@ def test_execute_sell_sloe_cancel_exception(mocker, default_conf, ticker, fee, freqtrade.strategy.order_types['stoploss_on_exchange'] = True patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() Trade.session = MagicMock() @@ -2183,7 +2295,7 @@ def test_execute_sell_sloe_cancel_exception(mocker, default_conf, ticker, fee, freqtrade.execute_sell(trade=trade, limit=1234, sell_reason=SellType.STOP_LOSS) assert sellmock.call_count == 1 - assert log_has('Could not cancel stoploss order abcd', caplog.record_tuples) + assert log_has('Could not cancel stoploss order abcd', caplog) def test_execute_sell_with_stoploss_on_exchange(default_conf, @@ -2219,7 +2331,7 @@ def test_execute_sell_with_stoploss_on_exchange(default_conf, patch_get_signal(freqtrade) # Create some test data - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade @@ -2271,7 +2383,7 @@ def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf, patch_get_signal(freqtrade) # Create some test data - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() freqtrade.process_maybe_execute_sell(trade) assert trade @@ -2309,8 +2421,8 @@ def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf, assert rpc_mock.call_count == 2 -def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee, - ticker_sell_up, markets, mocker) -> None: +def test_execute_sell_market_order(default_conf, ticker, fee, + ticker_sell_up, markets, mocker) -> None: rpc_mock = patch_RPCManager(mocker) mocker.patch.multiple( 'freqtrade.exchange.Exchange', @@ -2323,7 +2435,7 @@ def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee, patch_get_signal(freqtrade) # Create some test data - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade @@ -2333,10 +2445,13 @@ def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee, 'freqtrade.exchange.Exchange', get_ticker=ticker_sell_up ) - freqtrade.config = {} + freqtrade.config['order_types']['sell'] = 'market' freqtrade.execute_sell(trade=trade, limit=ticker_sell_up()['bid'], sell_reason=SellType.ROI) + assert not trade.is_open + assert trade.close_profit == 0.0611052 + assert rpc_mock.call_count == 2 last_msg = rpc_mock.call_args_list[-1][0][0] assert { @@ -2346,63 +2461,18 @@ def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee, 'gain': 'profit', 'limit': 1.172e-05, 'amount': 90.99181073703367, - 'order_type': 'limit', + 'order_type': 'market', 'open_rate': 1.099e-05, 'current_rate': 1.172e-05, 'profit_amount': 6.126e-05, 'profit_percent': 0.0611052, + 'stake_currency': 'BTC', + 'fiat_currency': 'USD', 'sell_reason': SellType.ROI.value } == last_msg -def test_execute_sell_without_conf_sell_down(default_conf, ticker, fee, - ticker_sell_down, markets, mocker) -> None: - rpc_mock = patch_RPCManager(mocker) - mocker.patch.multiple( - 'freqtrade.exchange.Exchange', - _load_markets=MagicMock(return_value={}), - get_ticker=ticker, - get_fee=fee, - markets=PropertyMock(return_value=markets) - ) - freqtrade = FreqtradeBot(default_conf) - patch_get_signal(freqtrade) - - # Create some test data - freqtrade.create_trade() - - trade = Trade.query.first() - assert trade - - # Decrease the price and sell it - mocker.patch.multiple( - 'freqtrade.exchange.Exchange', - get_ticker=ticker_sell_down - ) - - freqtrade.config = {} - freqtrade.execute_sell(trade=trade, limit=ticker_sell_down()['bid'], - sell_reason=SellType.STOP_LOSS) - - assert rpc_mock.call_count == 2 - last_msg = rpc_mock.call_args_list[-1][0][0] - assert { - 'type': RPCMessageType.SELL_NOTIFICATION, - 'exchange': 'Bittrex', - 'pair': 'ETH/BTC', - 'gain': 'loss', - 'limit': 1.044e-05, - 'amount': 90.99181073703367, - 'order_type': 'limit', - 'open_rate': 1.099e-05, - 'current_rate': 1.044e-05, - 'profit_amount': -5.492e-05, - 'profit_percent': -0.05478342, - 'sell_reason': SellType.STOP_LOSS.value - } == last_msg - - def test_sell_profit_only_enable_profit(default_conf, limit_buy_order, fee, markets, mocker) -> None: patch_RPCManager(mocker) @@ -2426,7 +2496,7 @@ def test_sell_profit_only_enable_profit(default_conf, limit_buy_order, patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) @@ -2457,7 +2527,7 @@ def test_sell_profit_only_disable_profit(default_conf, limit_buy_order, freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) @@ -2488,7 +2558,7 @@ def test_sell_profit_only_enable_loss(default_conf, limit_buy_order, fee, market patch_get_signal(freqtrade) freqtrade.strategy.stop_loss_reached = MagicMock(return_value=SellCheckTuple( sell_flag=False, sell_type=SellType.NONE)) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) @@ -2519,7 +2589,7 @@ def test_sell_profit_only_disable_loss(default_conf, limit_buy_order, fee, marke patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) @@ -2528,6 +2598,43 @@ def test_sell_profit_only_disable_loss(default_conf, limit_buy_order, fee, marke assert trade.sell_reason == SellType.SELL_SIGNAL.value +def test_locked_pairs(default_conf, ticker, fee, ticker_sell_down, markets, mocker, caplog) -> None: + patch_RPCManager(mocker) + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + _load_markets=MagicMock(return_value={}), + get_ticker=ticker, + get_fee=fee, + markets=PropertyMock(return_value=markets) + ) + freqtrade = FreqtradeBot(default_conf) + patch_get_signal(freqtrade) + + # Create some test data + freqtrade.create_trades() + + trade = Trade.query.first() + assert trade + + # Decrease the price and sell it + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + get_ticker=ticker_sell_down + ) + + freqtrade.execute_sell(trade=trade, limit=ticker_sell_down()['bid'], + sell_reason=SellType.STOP_LOSS) + trade.close(ticker_sell_down()['bid']) + assert trade.pair in freqtrade.strategy._pair_locked_until + assert freqtrade.strategy.is_pair_locked(trade.pair) + + # reinit - should buy other pair. + caplog.clear() + freqtrade.create_trades() + + assert log_has(f"Pair {trade.pair} is currently locked.", caplog) + + def test_ignore_roi_if_buy_signal(default_conf, limit_buy_order, fee, markets, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) @@ -2549,7 +2656,7 @@ def test_ignore_roi_if_buy_signal(default_conf, limit_buy_order, fee, markets, m patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=True) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) @@ -2581,7 +2688,7 @@ def test_trailing_stop_loss(default_conf, limit_buy_order, fee, markets, caplog, patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert freqtrade.handle_trade(trade) is False @@ -2609,7 +2716,7 @@ def test_trailing_stop_loss(default_conf, limit_buy_order, fee, markets, caplog, assert freqtrade.handle_trade(trade) is True assert log_has( f'HIT STOP: current price at 0.000012, stop loss is 0.000015, ' - f'initial stop loss was at 0.000010, trade opened at 0.000011', caplog.record_tuples) + f'initial stop loss was at 0.000010, trade opened at 0.000011', caplog) assert trade.sell_reason == SellType.TRAILING_STOP_LOSS.value @@ -2634,7 +2741,7 @@ def test_trailing_stop_loss_positive(default_conf, limit_buy_order, fee, markets freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) @@ -2651,9 +2758,8 @@ def test_trailing_stop_loss_positive(default_conf, limit_buy_order, fee, markets })) # stop-loss not reached, adjusted stoploss assert freqtrade.handle_trade(trade) is False - assert log_has(f'using positive stop loss: 0.01 offset: 0 profit: 0.2666%', - caplog.record_tuples) - assert log_has(f'adjusted stop loss', caplog.record_tuples) + assert log_has(f'using positive stop loss: 0.01 offset: 0 profit: 0.2666%', caplog) + assert log_has(f'adjusted stop loss', caplog) assert trade.stop_loss == 0.0000138501 mocker.patch('freqtrade.exchange.Exchange.get_ticker', @@ -2667,7 +2773,7 @@ def test_trailing_stop_loss_positive(default_conf, limit_buy_order, fee, markets assert log_has( f'HIT STOP: current price at {buy_price + 0.000002:.6f}, ' f'stop loss is {trade.stop_loss:.6f}, ' - f'initial stop loss was at 0.000010, trade opened at 0.000011', caplog.record_tuples) + f'initial stop loss was at 0.000010, trade opened at 0.000011', caplog) def test_trailing_stop_loss_offset(default_conf, limit_buy_order, fee, @@ -2693,7 +2799,7 @@ def test_trailing_stop_loss_offset(default_conf, limit_buy_order, fee, freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) @@ -2710,9 +2816,8 @@ def test_trailing_stop_loss_offset(default_conf, limit_buy_order, fee, })) # stop-loss not reached, adjusted stoploss assert freqtrade.handle_trade(trade) is False - assert log_has(f'using positive stop loss: 0.01 offset: 0.011 profit: 0.2666%', - caplog.record_tuples) - assert log_has(f'adjusted stop loss', caplog.record_tuples) + assert log_has(f'using positive stop loss: 0.01 offset: 0.011 profit: 0.2666%', caplog) + assert log_has(f'adjusted stop loss', caplog) assert trade.stop_loss == 0.0000138501 mocker.patch('freqtrade.exchange.Exchange.get_ticker', @@ -2726,7 +2831,7 @@ def test_trailing_stop_loss_offset(default_conf, limit_buy_order, fee, assert log_has( f'HIT STOP: current price at {buy_price + 0.000002:.6f}, ' f'stop loss is {trade.stop_loss:.6f}, ' - f'initial stop loss was at 0.000010, trade opened at 0.000011', caplog.record_tuples) + f'initial stop loss was at 0.000010, trade opened at 0.000011', caplog) assert trade.sell_reason == SellType.TRAILING_STOP_LOSS.value @@ -2757,7 +2862,7 @@ def test_tsl_only_offset_reached(default_conf, limit_buy_order, fee, freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) @@ -2777,7 +2882,7 @@ def test_tsl_only_offset_reached(default_conf, limit_buy_order, fee, # stop-loss should not be adjusted as offset is not reached yet assert freqtrade.handle_trade(trade) is False - assert not log_has(f'adjusted stop loss', caplog.record_tuples) + assert not log_has(f'adjusted stop loss', caplog) assert trade.stop_loss == 0.0000098910 # price rises above the offset (rises 12% when the offset is 5.5%) @@ -2789,9 +2894,8 @@ def test_tsl_only_offset_reached(default_conf, limit_buy_order, fee, })) assert freqtrade.handle_trade(trade) is False - assert log_has(f'using positive stop loss: 0.05 offset: 0.055 profit: 0.1218%', - caplog.record_tuples) - assert log_has(f'adjusted stop loss', caplog.record_tuples) + assert log_has(f'using positive stop loss: 0.05 offset: 0.055 profit: 0.1218%', caplog) + assert log_has(f'adjusted stop loss', caplog) assert trade.stop_loss == 0.0000117705 @@ -2817,7 +2921,7 @@ def test_disable_ignore_roi_if_buy_signal(default_conf, limit_buy_order, patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=True) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() trade.update(limit_buy_order) @@ -2850,7 +2954,7 @@ def test_get_real_amount_quote(default_conf, trades_for_order, buy_order_fee, ca assert freqtrade.get_real_amount(trade, buy_order_fee) == amount - (amount * 0.001) assert log_has('Applying fee on amount for Trade(id=None, pair=LTC/ETH, amount=8.00000000, ' 'open_rate=0.24544100, open_since=closed) (from 8.0 to 7.992) from Trades', - caplog.record_tuples) + caplog) def test_get_real_amount_no_trade(default_conf, buy_order_fee, caplog, mocker): @@ -2873,7 +2977,7 @@ def test_get_real_amount_no_trade(default_conf, buy_order_fee, caplog, mocker): assert freqtrade.get_real_amount(trade, buy_order_fee) == amount assert log_has('Applying fee on amount for Trade(id=None, pair=LTC/ETH, amount=8.00000000, ' 'open_rate=0.24544100, open_since=closed) failed: myTrade-Dict empty found', - caplog.record_tuples) + caplog) def test_get_real_amount_stake(default_conf, trades_for_order, buy_order_fee, mocker): @@ -2962,7 +3066,7 @@ def test_get_real_amount_multi(default_conf, trades_for_order2, buy_order_fee, c assert freqtrade.get_real_amount(trade, buy_order_fee) == amount - (amount * 0.001) assert log_has('Applying fee on amount for Trade(id=None, pair=LTC/ETH, amount=8.00000000, ' 'open_rate=0.24544100, open_since=closed) (from 8.0 to 7.992) from Trades', - caplog.record_tuples) + caplog) def test_get_real_amount_fromorder(default_conf, trades_for_order, buy_order_fee, caplog, mocker): @@ -2988,7 +3092,7 @@ def test_get_real_amount_fromorder(default_conf, trades_for_order, buy_order_fee assert freqtrade.get_real_amount(trade, limit_buy_order) == amount - 0.004 assert log_has('Applying fee on amount for Trade(id=None, pair=LTC/ETH, amount=8.00000000, ' 'open_rate=0.24544100, open_since=closed) (from 8.0 to 7.996) from Order', - caplog.record_tuples) + caplog) def test_get_real_amount_invalid_order(default_conf, trades_for_order, buy_order_fee, mocker): @@ -3074,7 +3178,7 @@ def test_order_book_depth_of_market(default_conf, ticker, limit_buy_order, fee, whitelist = deepcopy(default_conf['exchange']['pair_whitelist']) freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade is not None @@ -3108,7 +3212,7 @@ def test_order_book_depth_of_market_high_delta(default_conf, ticker, limit_buy_o # Save state of current whitelist freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade is None @@ -3214,7 +3318,7 @@ def test_order_book_ask_strategy(default_conf, limit_buy_order, limit_sell_order freqtrade = FreqtradeBot(default_conf) patch_get_signal(freqtrade) - freqtrade.create_trade() + freqtrade.create_trades() trade = Trade.query.first() assert trade diff --git a/freqtrade/tests/test_main.py b/freqtrade/tests/test_main.py index bcaad4d7d..d8ec532b0 100644 --- a/freqtrade/tests/test_main.py +++ b/freqtrade/tests/test_main.py @@ -60,8 +60,8 @@ def test_main_fatal_exception(mocker, default_conf, caplog) -> None: # Test Main + the KeyboardInterrupt exception with pytest.raises(SystemExit): main(args) - assert log_has('Using config: config.json.example ...', caplog.record_tuples) - assert log_has('Fatal exception!', caplog.record_tuples) + assert log_has('Using config: config.json.example ...', caplog) + assert log_has('Fatal exception!', caplog) def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None: @@ -77,8 +77,8 @@ def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None: # Test Main + the KeyboardInterrupt exception with pytest.raises(SystemExit): main(args) - assert log_has('Using config: config.json.example ...', caplog.record_tuples) - assert log_has('SIGINT received, aborting ...', caplog.record_tuples) + assert log_has('Using config: config.json.example ...', caplog) + assert log_has('SIGINT received, aborting ...', caplog) def test_main_operational_exception(mocker, default_conf, caplog) -> None: @@ -97,8 +97,8 @@ def test_main_operational_exception(mocker, default_conf, caplog) -> None: # Test Main + the KeyboardInterrupt exception with pytest.raises(SystemExit): main(args) - assert log_has('Using config: config.json.example ...', caplog.record_tuples) - assert log_has('Oh snap!', caplog.record_tuples) + assert log_has('Using config: config.json.example ...', caplog) + assert log_has('Oh snap!', caplog) def test_main_reload_conf(mocker, default_conf, caplog) -> None: @@ -121,7 +121,7 @@ def test_main_reload_conf(mocker, default_conf, caplog) -> None: with pytest.raises(SystemExit): main(['-c', 'config.json.example']) - assert log_has('Using config: config.json.example ...', caplog.record_tuples) + assert log_has('Using config: config.json.example ...', caplog) assert worker_mock.call_count == 4 assert reconfigure_mock.call_count == 1 assert isinstance(worker.freqtrade, FreqtradeBot) diff --git a/freqtrade/tests/test_persistence.py b/freqtrade/tests/test_persistence.py index 32425ef7b..c3ab7c128 100644 --- a/freqtrade/tests/test_persistence.py +++ b/freqtrade/tests/test_persistence.py @@ -151,7 +151,7 @@ def test_update_with_bittrex(limit_buy_order, limit_sell_order, fee, caplog): assert trade.close_date is None assert log_has("LIMIT_BUY has been fulfilled for Trade(id=2, " "pair=ETH/BTC, amount=90.99181073, open_rate=0.00001099, open_since=closed).", - caplog.record_tuples) + caplog) caplog.clear() trade.open_order_id = 'something' @@ -162,7 +162,7 @@ def test_update_with_bittrex(limit_buy_order, limit_sell_order, fee, caplog): assert trade.close_date is not None assert log_has("LIMIT_SELL has been fulfilled for Trade(id=2, " "pair=ETH/BTC, amount=90.99181073, open_rate=0.00001099, open_since=closed).", - caplog.record_tuples) + caplog) @pytest.mark.usefixtures("init_persistence") @@ -184,7 +184,7 @@ def test_update_market_order(market_buy_order, market_sell_order, fee, caplog): assert trade.close_date is None assert log_has("MARKET_BUY has been fulfilled for Trade(id=1, " "pair=ETH/BTC, amount=91.99181073, open_rate=0.00004099, open_since=closed).", - caplog.record_tuples) + caplog) caplog.clear() trade.open_order_id = 'something' @@ -195,7 +195,7 @@ def test_update_market_order(market_buy_order, market_sell_order, fee, caplog): assert trade.close_date is not None assert log_has("MARKET_SELL has been fulfilled for Trade(id=1, " "pair=ETH/BTC, amount=91.99181073, open_rate=0.00004099, open_since=closed).", - caplog.record_tuples) + caplog) @pytest.mark.usefixtures("init_persistence") @@ -558,10 +558,9 @@ def test_migrate_new(mocker, default_conf, fee, caplog): assert trade.ticker_interval is None assert trade.stoploss_order_id is None assert trade.stoploss_last_update is None - assert log_has("trying trades_bak1", caplog.record_tuples) - assert log_has("trying trades_bak2", caplog.record_tuples) - assert log_has("Running database migration - backup available as trades_bak2", - caplog.record_tuples) + assert log_has("trying trades_bak1", caplog) + assert log_has("trying trades_bak2", caplog) + assert log_has("Running database migration - backup available as trades_bak2", caplog) def test_migrate_mid_state(mocker, default_conf, fee, caplog): @@ -621,9 +620,8 @@ def test_migrate_mid_state(mocker, default_conf, fee, caplog): assert trade.max_rate == 0.0 assert trade.stop_loss == 0.0 assert trade.initial_stop_loss == 0.0 - assert log_has("trying trades_bak0", caplog.record_tuples) - assert log_has("Running database migration - backup available as trades_bak0", - caplog.record_tuples) + assert log_has("trying trades_bak0", caplog) + assert log_has("Running database migration - backup available as trades_bak0", caplog) def test_adjust_stop_loss(fee): diff --git a/freqtrade/tests/test_plotting.py b/freqtrade/tests/test_plotting.py index fa4e7e9da..27f34af65 100644 --- a/freqtrade/tests/test_plotting.py +++ b/freqtrade/tests/test_plotting.py @@ -6,7 +6,7 @@ from unittest.mock import MagicMock import plotly.graph_objects as go from plotly.subplots import make_subplots -from freqtrade.configuration import Arguments, TimeRange +from freqtrade.configuration import TimeRange from freqtrade.data import history from freqtrade.data.btanalysis import create_cum_profit, load_backtest_data from freqtrade.plot.plotting import (add_indicators, add_profit, @@ -88,7 +88,7 @@ def test_add_indicators(default_conf, caplog): # No indicator found fig3 = add_indicators(fig=deepcopy(fig), row=3, indicators=['no_indicator'], data=data) assert fig == fig3 - assert log_has_re(r'Indicator "no_indicator" ignored\..*', caplog.record_tuples) + assert log_has_re(r'Indicator "no_indicator" ignored\..*', caplog) def test_plot_trades(caplog): @@ -96,7 +96,7 @@ def test_plot_trades(caplog): # nothing happens when no trades are available fig = plot_trades(fig1, None) assert fig == fig1 - assert log_has("No trades found.", caplog.record_tuples) + assert log_has("No trades found.", caplog) pair = "ADA/BTC" filename = history.make_testdata_path(None) / "backtest-result_test.json" trades = load_backtest_data(filename) @@ -151,8 +151,8 @@ def test_generate_candlestick_graph_no_signals_no_trades(default_conf, mocker, c assert row_mock.call_count == 2 assert trades_mock.call_count == 1 - assert log_has("No buy-signals found.", caplog.record_tuples) - assert log_has("No sell-signals found.", caplog.record_tuples) + assert log_has("No buy-signals found.", caplog) + assert log_has("No sell-signals found.", caplog) def test_generate_candlestick_graph_no_trades(default_conf, mocker): @@ -218,13 +218,13 @@ def test_generate_plot_file(mocker, caplog): assert (plot_mock.call_args_list[0][1]['filename'] == "user_data/plots/freqtrade-plot-UNITTEST_BTC-5m.html") assert log_has("Stored plot as user_data/plots/freqtrade-plot-UNITTEST_BTC-5m.html", - caplog.record_tuples) + caplog) def test_add_profit(): filename = history.make_testdata_path(None) / "backtest-result_test.json" bt_data = load_backtest_data(filename) - timerange = Arguments.parse_timerange("20180110-20180112") + timerange = TimeRange.parse_timerange("20180110-20180112") df = history.load_pair_history(pair="POWR/BTC", ticker_interval='5m', datadir=None, timerange=timerange) @@ -244,7 +244,7 @@ def test_add_profit(): def test_generate_profit_graph(): filename = history.make_testdata_path(None) / "backtest-result_test.json" trades = load_backtest_data(filename) - timerange = Arguments.parse_timerange("20180110-20180112") + timerange = TimeRange.parse_timerange("20180110-20180112") pairs = ["POWR/BTC", "XLM/BTC"] tickers = history.load_data(datadir=None, diff --git a/freqtrade/tests/test_timerange.py b/freqtrade/tests/test_timerange.py new file mode 100644 index 000000000..6599472fb --- /dev/null +++ b/freqtrade/tests/test_timerange.py @@ -0,0 +1,28 @@ +# pragma pylint: disable=missing-docstring, C0103 +import pytest + +from freqtrade.configuration import TimeRange + + +def test_parse_timerange_incorrect() -> None: + assert TimeRange(None, 'line', 0, -200) == TimeRange.parse_timerange('-200') + assert TimeRange('line', None, 200, 0) == TimeRange.parse_timerange('200-') + assert TimeRange('index', 'index', 200, 500) == TimeRange.parse_timerange('200-500') + + assert TimeRange('date', None, 1274486400, 0) == TimeRange.parse_timerange('20100522-') + assert TimeRange(None, 'date', 0, 1274486400) == TimeRange.parse_timerange('-20100522') + timerange = TimeRange.parse_timerange('20100522-20150730') + assert timerange == TimeRange('date', 'date', 1274486400, 1438214400) + + # Added test for unix timestamp - BTC genesis date + assert TimeRange('date', None, 1231006505, 0) == TimeRange.parse_timerange('1231006505-') + assert TimeRange(None, 'date', 0, 1233360000) == TimeRange.parse_timerange('-1233360000') + timerange = TimeRange.parse_timerange('1231006505-1233360000') + assert TimeRange('date', 'date', 1231006505, 1233360000) == timerange + + # TODO: Find solution for the following case (passing timestamp in ms) + timerange = TimeRange.parse_timerange('1231006505000-1233360000000') + assert TimeRange('date', 'date', 1231006505, 1233360000) != timerange + + with pytest.raises(Exception, match=r'Incorrect syntax.*'): + TimeRange.parse_timerange('-') diff --git a/freqtrade/worker.py b/freqtrade/worker.py index db0dba0e8..df792e35e 100755 --- a/freqtrade/worker.py +++ b/freqtrade/worker.py @@ -127,11 +127,10 @@ class Worker(object): time.sleep(duration) return result - def _process(self) -> bool: + def _process(self) -> None: logger.debug("========================================") - state_changed = False try: - state_changed = self.freqtrade.process() + self.freqtrade.process() except TemporaryError as error: logger.warning(f"Error: {error}, retrying in {constants.RETRY_TIMEOUT} seconds...") time.sleep(constants.RETRY_TIMEOUT) @@ -144,10 +143,6 @@ class Worker(object): }) logger.exception('OperationalException. Stopping trader ...') self.freqtrade.state = State.STOPPED - # TODO: The return value of _process() is not used apart tests - # and should (could) be eliminated later. See PR #1689. -# state_changed = True - return state_changed def _reconfigure(self) -> None: """ diff --git a/requirements-common.txt b/requirements-common.txt index 1413539c3..4666fc053 100644 --- a/requirements-common.txt +++ b/requirements-common.txt @@ -1,9 +1,9 @@ # requirements without requirements installable via conda # mainly used for Raspberry pi installs -ccxt==1.18.1021 -SQLAlchemy==1.3.6 +ccxt==1.18.1063 +SQLAlchemy==1.3.7 python-telegram-bot==11.1.0 -arrow==0.14.4 +arrow==0.14.5 cachetools==3.1.1 requests==2.22.0 urllib3==1.25.3 @@ -23,10 +23,13 @@ filelock==3.0.12 py_find_1st==1.1.4 #Load ticker files 30% faster -python-rapidjson==0.7.2 +python-rapidjson==0.8.0 # Notify systemd sdnotify==0.3.2 # Api server flask==1.1.1 + +# Support for colorized terminal output +colorama==0.4.1 diff --git a/requirements-dev.txt b/requirements-dev.txt index 03b37417e..6436c60e4 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,7 +7,7 @@ flake8==3.7.8 flake8-type-annotations==0.1.0 flake8-tidy-imports==2.0.0 mypy==0.720 -pytest==5.0.1 +pytest==5.1.0 pytest-asyncio==0.10.0 pytest-cov==2.7.1 pytest-mock==1.10.4 diff --git a/requirements.txt b/requirements.txt index 6420c7879..9d558b5b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,4 @@ numpy==1.17.0 pandas==0.25.0 -scipy==1.3.0 +scipy==1.3.1 diff --git a/scripts/download_backtest_data.py b/scripts/download_backtest_data.py index 85635668d..36c1678ae 100755 --- a/scripts/download_backtest_data.py +++ b/scripts/download_backtest_data.py @@ -103,7 +103,7 @@ if not pairs or args.pairs_file: timerange = TimeRange() if args.days: time_since = arrow.utcnow().shift(days=-args.days).strftime("%Y%m%d") - timerange = arguments.parse_timerange(f'{time_since}-') + timerange = TimeRange.parse_timerange(f'{time_since}-') logger.info(f'About to download pairs: {pairs}, intervals: {timeframes} to {dl_path}') diff --git a/setup.py b/setup.py index 41e1b8f45..631c8b654 100644 --- a/setup.py +++ b/setup.py @@ -64,6 +64,7 @@ setup(name='freqtrade', 'py_find_1st', 'python-rapidjson', 'sdnotify', + 'colorama', # from requirements.txt 'numpy', 'pandas', diff --git a/user_data/hyperopts/sample_hyperopt.py b/user_data/hyperopts/sample_hyperopt.py index 1a3823afa..fabfdb23e 100644 --- a/user_data/hyperopts/sample_hyperopt.py +++ b/user_data/hyperopts/sample_hyperopt.py @@ -1,11 +1,10 @@ # pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement from functools import reduce -from math import exp from typing import Any, Callable, Dict, List from datetime import datetime -import numpy as np# noqa F401 +import numpy as np import talib.abstract as ta from pandas import DataFrame from skopt.space import Categorical, Dimension, Integer, Real @@ -16,7 +15,7 @@ from freqtrade.optimize.hyperopt_interface import IHyperOpt class SampleHyperOpts(IHyperOpt): """ - This is a sample hyperopt to inspire you. + This is a sample Hyperopt to inspire you. Feel free to customize it. More information in https://github.com/freqtrade/freqtrade/blob/develop/docs/hyperopt.md @@ -37,32 +36,44 @@ class SampleHyperOpts(IHyperOpt): """ @staticmethod def populate_indicators(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Add several indicators needed for buy and sell strategies defined below. + """ + # ADX dataframe['adx'] = ta.ADX(dataframe) + # MACD macd = ta.MACD(dataframe) dataframe['macd'] = macd['macd'] dataframe['macdsignal'] = macd['macdsignal'] + # MFI dataframe['mfi'] = ta.MFI(dataframe) + # RSI dataframe['rsi'] = ta.RSI(dataframe) + # Stochastic Fast stoch_fast = ta.STOCHF(dataframe) dataframe['fastd'] = stoch_fast['fastd'] + # Minus-DI dataframe['minus_di'] = ta.MINUS_DI(dataframe) # Bollinger bands bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) dataframe['bb_lowerband'] = bollinger['lower'] dataframe['bb_upperband'] = bollinger['upper'] + # SAR dataframe['sar'] = ta.SAR(dataframe) + return dataframe @staticmethod def buy_strategy_generator(params: Dict[str, Any]) -> Callable: """ - Define the buy strategy parameters to be used by hyperopt + Define the buy strategy parameters to be used by Hyperopt. """ def populate_buy_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Buy strategy Hyperopt will build and use + Buy strategy Hyperopt will build and use. """ conditions = [] + # GUARDS AND TRENDS if 'mfi-enabled' in params and params['mfi-enabled']: conditions.append(dataframe['mfi'] < params['mfi-value']) @@ -98,7 +109,7 @@ class SampleHyperOpts(IHyperOpt): @staticmethod def indicator_space() -> List[Dimension]: """ - Define your Hyperopt space for searching strategy parameters + Define your Hyperopt space for searching buy strategy parameters. """ return [ Integer(10, 25, name='mfi-value'), @@ -115,14 +126,14 @@ class SampleHyperOpts(IHyperOpt): @staticmethod def sell_strategy_generator(params: Dict[str, Any]) -> Callable: """ - Define the sell strategy parameters to be used by hyperopt + Define the sell strategy parameters to be used by Hyperopt. """ def populate_sell_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Sell strategy Hyperopt will build and use + Sell strategy Hyperopt will build and use. """ - # print(params) conditions = [] + # GUARDS AND TRENDS if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']: conditions.append(dataframe['mfi'] > params['sell-mfi-value']) @@ -158,7 +169,7 @@ class SampleHyperOpts(IHyperOpt): @staticmethod def sell_indicator_space() -> List[Dimension]: """ - Define your Hyperopt space for searching sell strategy parameters + Define your Hyperopt space for searching sell strategy parameters. """ return [ Integer(75, 100, name='sell-mfi-value'), @@ -176,9 +187,9 @@ class SampleHyperOpts(IHyperOpt): def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Based on TA indicators. Should be a copy of from strategy - must align to populate_indicators in this file - Only used when --spaces does not include buy + Based on TA indicators. Should be a copy of same method from strategy. + Must align to populate_indicators in this file. + Only used when --spaces does not include buy space. """ dataframe.loc[ ( @@ -193,9 +204,9 @@ class SampleHyperOpts(IHyperOpt): def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ - Based on TA indicators. Should be a copy of from strategy - must align to populate_indicators in this file - Only used when --spaces does not include sell + Based on TA indicators. Should be a copy of same method from strategy. + Must align to populate_indicators in this file. + Only used when --spaces does not include sell space. """ dataframe.loc[ ( @@ -205,4 +216,5 @@ class SampleHyperOpts(IHyperOpt): (dataframe['fastd'] > 54) ), 'sell'] = 1 + return dataframe