2017-11-08 21:43:47 +00:00
|
|
|
import argparse
|
2017-09-08 22:31:40 +00:00
|
|
|
import enum
|
2017-11-17 16:54:31 +00:00
|
|
|
import json
|
2017-11-08 21:43:47 +00:00
|
|
|
import logging
|
2017-11-16 15:39:06 +00:00
|
|
|
import time
|
2018-01-11 11:58:06 +00:00
|
|
|
import os
|
2018-01-15 21:25:02 +00:00
|
|
|
import re
|
2018-01-10 07:51:36 +00:00
|
|
|
from typing import Any, Callable, Dict, List
|
2017-09-08 22:31:40 +00:00
|
|
|
|
2018-01-10 07:51:36 +00:00
|
|
|
from jsonschema import Draft4Validator, validate
|
|
|
|
from jsonschema.exceptions import ValidationError, best_match
|
2017-09-08 22:31:40 +00:00
|
|
|
from wrapt import synchronized
|
|
|
|
|
2017-11-08 21:43:47 +00:00
|
|
|
from freqtrade import __version__
|
|
|
|
|
2017-11-11 15:47:19 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-09-08 22:31:40 +00:00
|
|
|
|
2018-01-13 16:39:36 +00:00
|
|
|
def file_dump_json(filename, data):
|
|
|
|
with open(filename, 'w') as fp:
|
|
|
|
json.dump(data, fp)
|
|
|
|
|
|
|
|
|
2017-09-08 22:31:40 +00:00
|
|
|
class State(enum.Enum):
|
|
|
|
RUNNING = 0
|
|
|
|
STOPPED = 1
|
|
|
|
|
|
|
|
|
|
|
|
# Current application state
|
|
|
|
_STATE = State.STOPPED
|
|
|
|
|
|
|
|
|
|
|
|
@synchronized
|
|
|
|
def update_state(state: State) -> None:
|
|
|
|
"""
|
|
|
|
Updates the application state
|
|
|
|
:param state: new state
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
global _STATE
|
|
|
|
_STATE = state
|
|
|
|
|
|
|
|
|
|
|
|
@synchronized
|
|
|
|
def get_state() -> State:
|
|
|
|
"""
|
|
|
|
Gets the current application state
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return _STATE
|
|
|
|
|
2017-05-12 17:11:56 +00:00
|
|
|
|
2017-11-17 16:54:31 +00:00
|
|
|
def load_config(path: str) -> Dict:
|
|
|
|
"""
|
|
|
|
Loads a config file from the given path
|
|
|
|
:param path: path as str
|
|
|
|
:return: configuration as dictionary
|
|
|
|
"""
|
|
|
|
with open(path) as file:
|
|
|
|
conf = json.load(file)
|
|
|
|
if 'internals' not in conf:
|
|
|
|
conf['internals'] = {}
|
|
|
|
logger.info('Validating configuration ...')
|
2017-11-20 18:37:25 +00:00
|
|
|
try:
|
|
|
|
validate(conf, CONF_SCHEMA)
|
|
|
|
return conf
|
2018-01-09 11:06:28 +00:00
|
|
|
except ValidationError as exception:
|
|
|
|
logger.fatal('Invalid configuration. See config.json.example. Reason: %s', exception)
|
2017-11-20 18:37:25 +00:00
|
|
|
raise ValidationError(
|
|
|
|
best_match(Draft4Validator(CONF_SCHEMA).iter_errors(conf)).message
|
|
|
|
)
|
2017-11-17 16:54:31 +00:00
|
|
|
|
|
|
|
|
2017-11-11 15:47:19 +00:00
|
|
|
def throttle(func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any:
|
|
|
|
"""
|
|
|
|
Throttles the given callable that it
|
|
|
|
takes at least `min_secs` to finish execution.
|
|
|
|
:param func: Any callable
|
|
|
|
:param min_secs: minimum execution time in seconds
|
|
|
|
:return: Any
|
|
|
|
"""
|
|
|
|
start = time.time()
|
|
|
|
result = func(*args, **kwargs)
|
|
|
|
end = time.time()
|
|
|
|
duration = max(min_secs - (end - start), 0.0)
|
|
|
|
logger.debug('Throttling %s for %.2f seconds', func.__name__, duration)
|
|
|
|
time.sleep(duration)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2018-01-09 11:06:28 +00:00
|
|
|
def common_args_parser(description: str):
|
2017-11-17 16:18:31 +00:00
|
|
|
"""
|
2018-01-06 06:39:05 +00:00
|
|
|
Parses given common arguments and returns them as a parsed object.
|
2017-11-17 16:18:31 +00:00
|
|
|
"""
|
2017-11-08 21:43:47 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
2018-01-06 06:39:05 +00:00
|
|
|
description=description
|
2017-11-08 21:43:47 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-v', '--verbose',
|
|
|
|
help='be verbose',
|
|
|
|
action='store_const',
|
|
|
|
dest='loglevel',
|
|
|
|
const=logging.DEBUG,
|
|
|
|
default=logging.INFO,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--version',
|
|
|
|
action='version',
|
|
|
|
version='%(prog)s {}'.format(__version__),
|
|
|
|
)
|
2017-11-11 18:20:53 +00:00
|
|
|
parser.add_argument(
|
2018-01-06 06:39:05 +00:00
|
|
|
'-c', '--config',
|
|
|
|
help='specify configuration file (default: config.json)',
|
|
|
|
dest='config',
|
|
|
|
default='config.json',
|
|
|
|
type=str,
|
|
|
|
metavar='PATH',
|
2017-11-11 18:20:53 +00:00
|
|
|
)
|
2018-01-12 09:52:44 +00:00
|
|
|
parser.add_argument(
|
2018-01-12 18:18:31 +00:00
|
|
|
'--datadir',
|
2018-01-19 06:17:47 +00:00
|
|
|
help='path to backtest data (default freqdata/tests/testdata)',
|
2018-01-12 09:52:44 +00:00
|
|
|
dest='datadir',
|
|
|
|
default=os.path.join('freqtrade', 'tests', 'testdata'),
|
|
|
|
type=str,
|
|
|
|
metavar='PATH',
|
|
|
|
)
|
2018-01-15 08:35:11 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-s', '--strategy',
|
|
|
|
help='specify strategy file (default: freqtrade/strategy/default_strategy.py)',
|
|
|
|
dest='strategy',
|
|
|
|
default='.default_strategy',
|
|
|
|
type=str,
|
|
|
|
metavar='PATH',
|
|
|
|
)
|
2018-01-06 06:39:05 +00:00
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args(args: List[str], description: str):
|
|
|
|
"""
|
|
|
|
Parses given arguments and returns an argparse Namespace instance.
|
|
|
|
Returns None if a sub command has been selected and executed.
|
|
|
|
"""
|
2018-01-09 11:06:28 +00:00
|
|
|
parser = common_args_parser(description)
|
2017-12-14 14:10:11 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--dry-run-db',
|
2018-01-09 11:06:28 +00:00
|
|
|
help='Force dry run to use a local DB "tradesv3.dry_run.sqlite" \
|
|
|
|
instead of memory DB. Work only if dry_run is enabled.',
|
2017-12-14 14:10:11 +00:00
|
|
|
action='store_true',
|
|
|
|
dest='dry_run_db',
|
|
|
|
)
|
2018-01-06 06:39:05 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--dynamic-whitelist',
|
2018-01-09 11:06:28 +00:00
|
|
|
help='dynamically generate and update whitelist \
|
|
|
|
based on 24h BaseVolume (Default 20 currencies)', # noqa
|
2018-01-06 06:39:05 +00:00
|
|
|
dest='dynamic_whitelist',
|
|
|
|
const=20,
|
|
|
|
type=int,
|
|
|
|
metavar='INT',
|
|
|
|
nargs='?',
|
|
|
|
)
|
2018-01-06 22:24:35 +00:00
|
|
|
|
2017-11-14 21:15:24 +00:00
|
|
|
build_subcommands(parser)
|
2018-01-06 10:21:09 +00:00
|
|
|
return parser.parse_args(args)
|
2017-11-08 21:43:47 +00:00
|
|
|
|
|
|
|
|
2018-01-12 09:52:44 +00:00
|
|
|
def backtesting_options(parser: argparse.ArgumentParser) -> None:
|
|
|
|
parser.add_argument(
|
2017-11-14 21:15:24 +00:00
|
|
|
'-l', '--live',
|
|
|
|
action='store_true',
|
|
|
|
dest='live',
|
|
|
|
help='using live data',
|
|
|
|
)
|
2018-01-12 09:52:44 +00:00
|
|
|
parser.add_argument(
|
2017-11-14 21:37:30 +00:00
|
|
|
'-i', '--ticker-interval',
|
2018-01-13 07:55:45 +00:00
|
|
|
help='specify ticker interval in minutes (1, 5, 30, 60, 1440)',
|
2017-11-14 21:37:30 +00:00
|
|
|
dest='ticker_interval',
|
|
|
|
default=5,
|
|
|
|
type=int,
|
|
|
|
metavar='INT',
|
|
|
|
)
|
2018-01-12 09:52:44 +00:00
|
|
|
parser.add_argument(
|
2017-11-24 20:09:44 +00:00
|
|
|
'--realistic-simulation',
|
2017-11-22 23:25:06 +00:00
|
|
|
help='uses max_open_trades from config to simulate real world limitations',
|
|
|
|
action='store_true',
|
2017-11-24 20:09:44 +00:00
|
|
|
dest='realistic_simulation',
|
2017-11-22 23:25:06 +00:00
|
|
|
)
|
2018-01-12 09:52:44 +00:00
|
|
|
parser.add_argument(
|
2017-12-16 14:42:28 +00:00
|
|
|
'-r', '--refresh-pairs-cached',
|
2017-12-18 16:36:00 +00:00
|
|
|
help='refresh the pairs files in tests/testdata with the latest data from Bittrex. \
|
|
|
|
Use it if you want to run your backtesting with up-to-date data.',
|
2017-12-16 14:42:28 +00:00
|
|
|
action='store_true',
|
|
|
|
dest='refresh_pairs',
|
|
|
|
)
|
2018-01-20 07:33:28 +00:00
|
|
|
parser.add_argument(
|
2018-01-11 14:45:39 +00:00
|
|
|
'--export',
|
|
|
|
help='Export backtest results, argument are: trades\
|
2018-01-20 07:33:28 +00:00
|
|
|
Example --export=trades',
|
2018-01-11 14:45:39 +00:00
|
|
|
type=str,
|
|
|
|
default=None,
|
|
|
|
dest='export',
|
|
|
|
)
|
2018-01-19 06:17:47 +00:00
|
|
|
parser.add_argument(
|
2018-01-15 20:49:06 +00:00
|
|
|
'--timerange',
|
2018-01-15 21:25:02 +00:00
|
|
|
help='Specify what timerange of data to use.',
|
2018-01-10 22:03:05 +00:00
|
|
|
default=None,
|
2018-01-15 21:25:02 +00:00
|
|
|
type=str,
|
2018-01-15 20:49:06 +00:00
|
|
|
dest='timerange',
|
2018-01-10 22:03:05 +00:00
|
|
|
)
|
2017-11-14 21:15:24 +00:00
|
|
|
|
2018-01-12 09:52:44 +00:00
|
|
|
|
|
|
|
def hyperopt_options(parser: argparse.ArgumentParser) -> None:
|
|
|
|
parser.add_argument(
|
2017-11-25 00:12:44 +00:00
|
|
|
'-e', '--epochs',
|
|
|
|
help='specify number of epochs (default: 100)',
|
|
|
|
dest='epochs',
|
|
|
|
default=100,
|
|
|
|
type=int,
|
|
|
|
metavar='INT',
|
|
|
|
)
|
2018-01-12 09:52:44 +00:00
|
|
|
parser.add_argument(
|
2017-11-25 01:04:37 +00:00
|
|
|
'--use-mongodb',
|
|
|
|
help='parallelize evaluations with mongodb (requires mongod in PATH)',
|
|
|
|
dest='mongodb',
|
|
|
|
action='store_true',
|
|
|
|
)
|
2018-01-12 09:52:44 +00:00
|
|
|
parser.add_argument(
|
2017-12-17 10:34:26 +00:00
|
|
|
'-i', '--ticker-interval',
|
|
|
|
help='specify ticker interval in minutes (default: 5)',
|
|
|
|
dest='ticker_interval',
|
|
|
|
default=5,
|
|
|
|
type=int,
|
|
|
|
metavar='INT',
|
|
|
|
)
|
2018-01-19 06:17:47 +00:00
|
|
|
parser.add_argument(
|
2018-01-15 21:25:02 +00:00
|
|
|
'--timerange',
|
|
|
|
help='Specify what timerange of data to use.',
|
2018-01-10 22:03:05 +00:00
|
|
|
default=None,
|
2018-01-15 21:25:02 +00:00
|
|
|
type=str,
|
2018-01-15 20:49:06 +00:00
|
|
|
dest='timerange',
|
2018-01-10 22:03:05 +00:00
|
|
|
)
|
2017-11-25 01:04:37 +00:00
|
|
|
|
2017-11-14 21:15:24 +00:00
|
|
|
|
2018-01-15 21:25:02 +00:00
|
|
|
def parse_timerange(text):
|
|
|
|
if text is None:
|
|
|
|
return None
|
|
|
|
syntax = [('^-(\d{8})$', (None, 'date')),
|
|
|
|
('^(\d{8})-$', ('date', None)),
|
|
|
|
('^(\d{8})-(\d{8})$', ('date', 'date')),
|
|
|
|
('^(-\d+)$', (None, 'line')),
|
|
|
|
('^(\d+)-$', ('line', None)),
|
|
|
|
('^(\d+)-(\d+)$', ('index', 'index'))]
|
|
|
|
for rex, stype in syntax:
|
|
|
|
# Apply the regular expression to text
|
|
|
|
m = re.match(rex, text)
|
|
|
|
if m: # Regex has matched
|
|
|
|
rvals = m.groups()
|
|
|
|
n = 0
|
|
|
|
start = None
|
|
|
|
stop = None
|
|
|
|
if stype[0]:
|
|
|
|
start = rvals[n]
|
|
|
|
if stype[0] != 'date':
|
|
|
|
start = int(start)
|
|
|
|
n += 1
|
|
|
|
if stype[1]:
|
|
|
|
stop = rvals[n]
|
|
|
|
if stype[1] != 'date':
|
|
|
|
stop = int(stop)
|
|
|
|
return (stype, start, stop)
|
|
|
|
raise Exception('Incorrect syntax for timerange "%s"' % text)
|
2017-11-25 01:04:37 +00:00
|
|
|
|
2017-11-14 21:15:24 +00:00
|
|
|
|
2018-01-12 09:52:44 +00:00
|
|
|
def build_subcommands(parser: argparse.ArgumentParser) -> None:
|
|
|
|
""" Builds and attaches all subcommands """
|
|
|
|
from freqtrade.optimize import backtesting, hyperopt
|
|
|
|
|
|
|
|
subparsers = parser.add_subparsers(dest='subparser')
|
|
|
|
|
|
|
|
# Add backtesting subcommand
|
|
|
|
backtesting_cmd = subparsers.add_parser('backtesting', help='backtesting module')
|
|
|
|
backtesting_cmd.set_defaults(func=backtesting.start)
|
|
|
|
backtesting_options(backtesting_cmd)
|
|
|
|
|
|
|
|
# Add hyperopt subcommand
|
|
|
|
hyperopt_cmd = subparsers.add_parser('hyperopt', help='hyperopt module')
|
|
|
|
hyperopt_cmd.set_defaults(func=hyperopt.start)
|
|
|
|
hyperopt_options(hyperopt_cmd)
|
2017-11-25 01:04:37 +00:00
|
|
|
|
2017-11-14 21:15:24 +00:00
|
|
|
|
2017-09-01 23:10:21 +00:00
|
|
|
# Required json-schema for user specified config
|
2017-09-08 21:10:22 +00:00
|
|
|
CONF_SCHEMA = {
|
2017-09-01 23:10:21 +00:00
|
|
|
'type': 'object',
|
|
|
|
'properties': {
|
2017-09-08 05:49:50 +00:00
|
|
|
'max_open_trades': {'type': 'integer', 'minimum': 1},
|
2018-01-22 08:39:26 +00:00
|
|
|
'ticker_interval': {'type': 'integer', 'enum': [1, 5, 30, 60, 1440]},
|
2017-09-08 05:51:10 +00:00
|
|
|
'stake_currency': {'type': 'string', 'enum': ['BTC', 'ETH', 'USDT']},
|
2017-09-08 05:45:27 +00:00
|
|
|
'stake_amount': {'type': 'number', 'minimum': 0.0005},
|
2018-01-08 07:51:04 +00:00
|
|
|
'fiat_display_currency': {'type': 'string', 'enum': ['AUD', 'BRL', 'CAD', 'CHF',
|
|
|
|
'CLP', 'CNY', 'CZK', 'DKK',
|
|
|
|
'EUR', 'GBP', 'HKD', 'HUF',
|
|
|
|
'IDR', 'ILS', 'INR', 'JPY',
|
|
|
|
'KRW', 'MXN', 'MYR', 'NOK',
|
|
|
|
'NZD', 'PHP', 'PKR', 'PLN',
|
|
|
|
'RUB', 'SEK', 'SGD', 'THB',
|
|
|
|
'TRY', 'TWD', 'ZAR', 'USD']},
|
2017-09-01 23:10:21 +00:00
|
|
|
'dry_run': {'type': 'boolean'},
|
|
|
|
'minimal_roi': {
|
|
|
|
'type': 'object',
|
|
|
|
'patternProperties': {
|
|
|
|
'^[0-9.]+$': {'type': 'number'}
|
|
|
|
},
|
|
|
|
'minProperties': 1
|
|
|
|
},
|
2017-09-07 14:31:55 +00:00
|
|
|
'stoploss': {'type': 'number', 'maximum': 0, 'exclusiveMaximum': True},
|
2018-01-03 23:35:57 +00:00
|
|
|
'unfilledtimeout': {'type': 'integer', 'minimum': 0},
|
2017-09-17 19:37:46 +00:00
|
|
|
'bid_strategy': {
|
|
|
|
'type': 'object',
|
|
|
|
'properties': {
|
|
|
|
'ask_last_balance': {
|
|
|
|
'type': 'number',
|
|
|
|
'minimum': 0,
|
|
|
|
'maximum': 1,
|
|
|
|
'exclusiveMaximum': False
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'required': ['ask_last_balance']
|
|
|
|
},
|
2017-10-06 10:22:04 +00:00
|
|
|
'exchange': {'$ref': '#/definitions/exchange'},
|
2017-11-24 20:58:00 +00:00
|
|
|
'experimental': {
|
|
|
|
'type': 'object',
|
|
|
|
'properties': {
|
2017-12-22 03:58:06 +00:00
|
|
|
'use_sell_signal': {'type': 'boolean'},
|
|
|
|
'sell_profit_only': {'type': 'boolean'}
|
2017-11-24 20:58:00 +00:00
|
|
|
}
|
|
|
|
},
|
2017-09-01 23:10:21 +00:00
|
|
|
'telegram': {
|
|
|
|
'type': 'object',
|
|
|
|
'properties': {
|
|
|
|
'enabled': {'type': 'boolean'},
|
|
|
|
'token': {'type': 'string'},
|
|
|
|
'chat_id': {'type': 'string'},
|
|
|
|
},
|
|
|
|
'required': ['enabled', 'token', 'chat_id']
|
2017-09-08 22:31:40 +00:00
|
|
|
},
|
|
|
|
'initial_state': {'type': 'string', 'enum': ['running', 'stopped']},
|
2017-11-11 15:47:19 +00:00
|
|
|
'internals': {
|
|
|
|
'type': 'object',
|
|
|
|
'properties': {
|
2018-01-12 16:02:35 +00:00
|
|
|
'process_throttle_secs': {'type': 'number'},
|
|
|
|
'interval': {'type': 'integer'}
|
2017-11-11 15:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-01 23:10:21 +00:00
|
|
|
},
|
|
|
|
'definitions': {
|
|
|
|
'exchange': {
|
|
|
|
'type': 'object',
|
|
|
|
'properties': {
|
2017-10-06 10:22:04 +00:00
|
|
|
'name': {'type': 'string'},
|
2017-09-01 23:10:21 +00:00
|
|
|
'key': {'type': 'string'},
|
|
|
|
'secret': {'type': 'string'},
|
|
|
|
'pair_whitelist': {
|
|
|
|
'type': 'array',
|
2017-11-20 18:37:25 +00:00
|
|
|
'items': {
|
|
|
|
'type': 'string',
|
|
|
|
'pattern': '^[0-9A-Z]+_[0-9A-Z]+$'
|
|
|
|
},
|
2017-09-01 23:10:21 +00:00
|
|
|
'uniqueItems': True
|
2017-12-30 13:15:07 +00:00
|
|
|
},
|
|
|
|
'pair_blacklist': {
|
|
|
|
'type': 'array',
|
|
|
|
'items': {
|
|
|
|
'type': 'string',
|
|
|
|
'pattern': '^[0-9A-Z]+_[0-9A-Z]+$'
|
|
|
|
},
|
|
|
|
'uniqueItems': True
|
2017-09-01 23:10:21 +00:00
|
|
|
}
|
|
|
|
},
|
2017-10-07 15:36:48 +00:00
|
|
|
'required': ['name', 'key', 'secret', 'pair_whitelist']
|
2017-09-01 23:10:21 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'anyOf': [
|
2017-10-06 10:22:04 +00:00
|
|
|
{'required': ['exchange']}
|
2017-09-01 23:10:21 +00:00
|
|
|
],
|
|
|
|
'required': [
|
|
|
|
'max_open_trades',
|
2018-01-12 16:02:35 +00:00
|
|
|
'ticker_interval',
|
2017-09-01 23:10:21 +00:00
|
|
|
'stake_currency',
|
|
|
|
'stake_amount',
|
2017-12-30 03:52:55 +00:00
|
|
|
'fiat_display_currency',
|
2017-09-01 23:10:21 +00:00
|
|
|
'dry_run',
|
2017-09-17 19:37:46 +00:00
|
|
|
'bid_strategy',
|
2017-09-01 23:10:21 +00:00
|
|
|
'telegram'
|
|
|
|
]
|
|
|
|
}
|