From ef7646417bac614403ff624ad86023ac4761db58 Mon Sep 17 00:00:00 2001 From: Gerald Lonlas Date: Sun, 10 Dec 2017 23:56:03 -0800 Subject: [PATCH] Allow to change the number of currencies used by dynamic-whitelist --- README.md | 35 ++++++++++++++++++++++++++++------- freqtrade/main.py | 12 +++++++++--- freqtrade/misc.py | 8 ++++++-- freqtrade/tests/test_misc.py | 13 +++++++++++-- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 28cba0855..4efbb76d4 100644 --- a/README.md +++ b/README.md @@ -146,19 +146,40 @@ Simple High Frequency Trading Bot for crypto currencies positional arguments: {backtesting} - backtesting backtesting module + backtesting backtesting module optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit -c PATH, --config PATH - specify configuration file (default: config.json) - -v, --verbose be verbose - --version show program's version number and exit - --dynamic-whitelist dynamically generate and update whitelist based on 24h - BaseVolume + specify configuration file (default: config.json) + -v, --verbose be verbose + --version show program's version number and exit + --dynamic-whitelist INT dynamically generate and update whitelist based on 24h + BaseVolume (Default 20 currencies) ``` +#### Dynamic whitelist example +Per default `--dynamic-whitelist` will retrieve the 20 currencies based +on BaseVolume. This value can be changed when you run the script. + +**By Default** +Get the 20 currencies based on BaseVolume. +```bash +freqtrade --dynamic-whitelist +``` + +**Customize the number of currencies to retrieve** +Get the 30 currencies based on BaseVolume. +```bash +freqtrade --dynamic-whitelist 30 +``` + +**Exception** +`--dynamic-whitelist` must be greater than 0. If you enter 0 or a +negative value (e.g -2), `--dynamic-whitelist` will use the default +value (20). + ### Backtesting Backtesting also uses the config specified via `-c/--config`. diff --git a/freqtrade/main.py b/freqtrade/main.py index 6fd1ac197..385bb46ac 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import argparse import copy import json import logging @@ -49,7 +50,7 @@ def refresh_whitelist(whitelist: Optional[List[str]] = None) -> None: _CONF['exchange']['pair_whitelist'] = sanitized_whitelist -def _process(dynamic_whitelist: Optional[bool] = False) -> bool: +def _process(dynamic_whitelist: Optional[int] = 0) -> bool: """ Queries the persistence layer for open trades and handles them, otherwise a new trade is created. @@ -60,7 +61,7 @@ def _process(dynamic_whitelist: Optional[bool] = False) -> bool: try: # Refresh whitelist based on wallet maintenance refresh_whitelist( - gen_pair_whitelist(_CONF['stake_currency']) if dynamic_whitelist else None + gen_pair_whitelist(_CONF['stake_currency'], topn = dynamic_whitelist) if dynamic_whitelist else None ) # Query trades from persistence layer trades = Trade.query.filter(Trade.is_open.is_(True)).all() @@ -266,7 +267,7 @@ def gen_pair_whitelist(base_currency: str, topn: int = 20, key: str = 'BaseVolum """ Updates the whitelist with with a dynamically generated list :param base_currency: base currency as str - :param topn: maximum number of returned results + :param topn: maximum number of returned results, must be greater than 0 :param key: sort key (defaults to 'BaseVolume') :return: List of pairs """ @@ -275,6 +276,11 @@ def gen_pair_whitelist(base_currency: str, topn: int = 20, key: str = 'BaseVolum key=lambda s: s.get(key) or 0.0, reverse=True ) + + # topn must be greater than 0 + if not topn > 0: + topn = 20 + return [s['MarketName'].replace('-', '_') for s in summaries[:topn]] diff --git a/freqtrade/misc.py b/freqtrade/misc.py index dcae19ac4..8e27a4282 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -112,8 +112,12 @@ def parse_args(args: List[str]): ) parser.add_argument( '--dynamic-whitelist', - help='dynamically generate and update whitelist based on 24h BaseVolume', - action='store_true', + help='dynamically generate and update whitelist based on 24h BaseVolume (Default 20 currencies)', + dest='dynamic_whitelist', + const=20, + type=int, + metavar='INT', + nargs='?', ) build_subcommands(parser) parsed_args = parser.parse_args(args) diff --git a/freqtrade/tests/test_misc.py b/freqtrade/tests/test_misc.py index 6f52b44b8..ca3e69264 100644 --- a/freqtrade/tests/test_misc.py +++ b/freqtrade/tests/test_misc.py @@ -30,7 +30,7 @@ def test_parse_args_defaults(): args = parse_args([]) assert args is not None assert args.config == 'config.json' - assert args.dynamic_whitelist is False + assert args.dynamic_whitelist is None assert args.loglevel == 20 @@ -58,7 +58,16 @@ def test_parse_args_verbose(): def test_parse_args_dynamic_whitelist(): args = parse_args(['--dynamic-whitelist']) assert args is not None - assert args.dynamic_whitelist is True + assert args.dynamic_whitelist is 20 + +def test_parse_args_dynamic_whitelist_10(): + args = parse_args(['--dynamic-whitelist', '10']) + assert args is not None + assert args.dynamic_whitelist is 10 + +def test_parse_args_dynamic_whitelist_invalid_values(): + with pytest.raises(SystemExit, match=r'2'): + parse_args(['--dynamic-whitelist', 'abc']) def test_parse_args_backtesting(mocker):