From bdff29a472b6493df34af8a622cb49cc5878fcc9 Mon Sep 17 00:00:00 2001 From: gcarq Date: Fri, 17 Nov 2017 17:54:31 +0100 Subject: [PATCH] remove code duplicates --- freqtrade/main.py | 11 +++-------- freqtrade/misc.py | 19 ++++++++++++++++++- freqtrade/tests/test_backtesting.py | 25 +++++++++++-------------- freqtrade/tests/test_hyperopt.py | 3 +++ 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/freqtrade/main.py b/freqtrade/main.py index 4a14703c1..a47b7e0cb 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -11,14 +11,14 @@ from typing import Dict, Optional, List import requests from cachetools import cached, TTLCache -from jsonschema import validate from freqtrade import __version__, exchange, persistence from freqtrade.analyze import get_signal, SignalType -from freqtrade.misc import CONF_SCHEMA, State, get_state, update_state, parse_args, throttle from freqtrade.misc import ( FreqtradeException ) +from freqtrade.misc import State, get_state, update_state, parse_args, throttle, \ + load_config from freqtrade.persistence import Trade from freqtrade.rpc import telegram @@ -335,12 +335,7 @@ def main(): ) # Load and validate configuration - with open(args.config) as file: - _CONF = json.load(file) - if 'internals' not in _CONF: - _CONF['internals'] = {} - logger.info('Validating configuration ...') - validate(_CONF, CONF_SCHEMA) + _CONF = load_config(args.config) # Initialize all modules and start main loop if args.dynamic_whitelist: diff --git a/freqtrade/misc.py b/freqtrade/misc.py index ab06a85de..7cde85813 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -1,10 +1,12 @@ import argparse import enum +import json import logging import os import time -from typing import Any, Callable, List +from typing import Any, Callable, List, Dict +from jsonschema import validate from wrapt import synchronized from freqtrade import __version__ @@ -45,6 +47,21 @@ def get_state() -> State: return _STATE +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 ...') + validate(conf, CONF_SCHEMA) + return conf + + def throttle(func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any: """ Throttles the given callable that it diff --git a/freqtrade/tests/test_backtesting.py b/freqtrade/tests/test_backtesting.py index 0af8da8f3..78eeb1f72 100644 --- a/freqtrade/tests/test_backtesting.py +++ b/freqtrade/tests/test_backtesting.py @@ -1,13 +1,12 @@ # pragma pylint: disable=missing-docstring -import json + import logging import os from typing import Tuple, Dict import arrow import pytest -from arrow import Arrow from pandas import DataFrame from tabulate import tabulate @@ -16,6 +15,7 @@ from freqtrade.analyze import parse_ticker_dataframe, populate_indicators, \ populate_buy_trend, populate_sell_trend from freqtrade.exchange import Bittrex from freqtrade.main import min_roi_reached +from freqtrade.misc import load_config from freqtrade.persistence import Trade from freqtrade.tests import load_backtesting_data @@ -32,11 +32,6 @@ def format_results(results: DataFrame): ) -def print_pair_results(pair: str, results: DataFrame): - print('For currency {}:'.format(pair)) - print(format_results(results[results.currency == pair])) - - def preprocess(backdata) -> Dict[str, DataFrame]: processed = {} for pair, pair_data in backdata.items(): @@ -44,7 +39,7 @@ def preprocess(backdata) -> Dict[str, DataFrame]: return processed -def get_timeframe(data: Dict[str, Dict]) -> Tuple[Arrow, Arrow]: +def get_timeframe(data: Dict[str, Dict]) -> Tuple[arrow.Arrow, arrow.Arrow]: """ Get the maximum timeframe for the given backtest data :param data: dictionary with backtesting data @@ -118,38 +113,40 @@ def backtest(backtest_conf, processed, mocker): @pytest.mark.skipif(not os.environ.get('BACKTEST'), reason="BACKTEST not set") def test_backtest(backtest_conf, mocker): print('') + exchange._API = Bittrex({'key': '', 'secret': ''}) - config = None + # Load configuration file based on env variable conf_path = os.environ.get('BACKTEST_CONFIG') if conf_path: print('Using config: {} ...'.format(conf_path)) - with open(conf_path, 'r') as conf_file: - config = json.load(conf_file) + config = load_config(conf_path) + else: + config = backtest_conf + # Parse ticker interval ticker_interval = int(os.environ.get('BACKTEST_TICKER_INTERVAL') or 5) print('Using ticker_interval: {} ...'.format(ticker_interval)) data = {} if os.environ.get('BACKTEST_LIVE'): print('Downloading data for all pairs in whitelist ...') - exchange._API = Bittrex({'key': '', 'secret': ''}) for pair in config['exchange']['pair_whitelist']: data[pair] = exchange.get_ticker_history(pair, ticker_interval) else: print('Using local backtesting data (ignoring whitelist in given config)...') data = load_backtesting_data(ticker_interval) - config = config or backtest_conf - print('Using stake_currency: {} ...\nUsing stake_amount: {} ...'.format( config['stake_currency'], config['stake_amount'] )) + # Print timeframe min_date, max_date = get_timeframe(data) print('Measuring data from {} up to {} ...'.format( min_date.isoformat(), max_date.isoformat() )) + # Execute backtest and print results results = backtest(config, preprocess(data), mocker) print('====================== BACKTESTING REPORT ======================================\n\n' 'NOTE: This Report doesn\'t respect the limits of max_open_trades, \n' diff --git a/freqtrade/tests/test_hyperopt.py b/freqtrade/tests/test_hyperopt.py index e2a2a1513..329f6964a 100644 --- a/freqtrade/tests/test_hyperopt.py +++ b/freqtrade/tests/test_hyperopt.py @@ -9,6 +9,8 @@ import pytest from hyperopt import fmin, tpe, hp, Trials, STATUS_OK from pandas import DataFrame +from freqtrade import exchange +from freqtrade.exchange import Bittrex from freqtrade.tests.test_backtesting import backtest, format_results from freqtrade.tests.test_backtesting import preprocess from freqtrade.vendor.qtpylib.indicators import crossed_above @@ -70,6 +72,7 @@ def buy_strategy_generator(params): def test_hyperopt(backtest_conf, backdata, mocker): mocked_buy_trend = mocker.patch('freqtrade.tests.test_backtesting.populate_buy_trend') processed = preprocess(backdata) + exchange._API = Bittrex({'key': '', 'secret': ''}) def optimizer(params): mocked_buy_trend.side_effect = buy_strategy_generator(params)