Merge branch 'feat/objectify-ccxt' into cxxt_obj_sellfix

This commit is contained in:
Matthias Voppichler
2018-04-21 22:39:22 +02:00
28 changed files with 254 additions and 406 deletions

View File

@@ -2,15 +2,15 @@
import gzip
import json
import logging
import os
from typing import Optional, List, Dict, Tuple
from freqtrade import misc
from freqtrade.exchange import get_ticker_history
from freqtrade.logger import Logger
from user_data.hyperopt_conf import hyperopt_optimize_conf
logger = Logger(name=__name__).get_logger()
logger = logging.getLogger(__name__)
def trim_tickerlist(tickerlist: List[Dict], timerange: Tuple[Tuple, int, int]) -> List[Dict]:

View File

@@ -3,6 +3,7 @@
"""
This module contains the backtesting logic
"""
import logging
from argparse import Namespace
from typing import Dict, Tuple, Any, List, Optional
@@ -15,11 +16,13 @@ from freqtrade import exchange
from freqtrade.analyze import Analyze
from freqtrade.arguments import Arguments
from freqtrade.configuration import Configuration
from freqtrade.logger import Logger
from freqtrade.misc import file_dump_json
from freqtrade.persistence import Trade
logger = logging.getLogger(__name__)
class Backtesting(object):
"""
Backtesting class, this class contains all the logic to run a backtest
@@ -29,10 +32,6 @@ class Backtesting(object):
backtesting.start()
"""
def __init__(self, config: Dict[str, Any]) -> None:
# Init the logger
self.logging = Logger(name=__name__, level=config['loglevel'])
self.logger = self.logging.get_logger()
self.config = config
self.analyze = None
self.ticker_interval = None
@@ -208,7 +207,7 @@ class Backtesting(object):
# For now export inside backtest(), maybe change so that backtest()
# returns a tuple like: (dataframe, records, logs, etc)
if record and record.find('trades') >= 0:
self.logger.info('Dumping backtest results')
logger.info('Dumping backtest results')
file_dump_json('backtest-result.json', records)
labels = ['currency', 'profit_percent', 'profit_BTC', 'duration']
return DataFrame.from_records(trades, columns=labels)
@@ -220,15 +219,15 @@ class Backtesting(object):
"""
data = {}
pairs = self.config['exchange']['pair_whitelist']
self.logger.info('Using stake_currency: %s ...', self.config['stake_currency'])
self.logger.info('Using stake_amount: %s ...', self.config['stake_amount'])
logger.info('Using stake_currency: %s ...', self.config['stake_currency'])
logger.info('Using stake_amount: %s ...', self.config['stake_amount'])
if self.config.get('live'):
self.logger.info('Downloading data for all pairs in whitelist ...')
logger.info('Downloading data for all pairs in whitelist ...')
for pair in pairs:
data[pair] = exchange.get_ticker_history(pair, self.ticker_interval)
else:
self.logger.info('Using local backtesting data (using whitelist in given config) ...')
logger.info('Using local backtesting data (using whitelist in given config) ...')
timerange = Arguments.parse_timerange(self.config.get('timerange'))
data = optimize.load_data(
@@ -243,14 +242,14 @@ class Backtesting(object):
if self.config.get('realistic_simulation', False):
max_open_trades = self.config['max_open_trades']
else:
self.logger.info('Ignoring max_open_trades (realistic_simulation not set) ...')
logger.info('Ignoring max_open_trades (realistic_simulation not set) ...')
max_open_trades = 0
preprocessed = self.tickerdata_to_dataframe(data)
# Print timeframe
min_date, max_date = self.get_timeframe(preprocessed)
self.logger.info(
logger.info(
'Measuring data from %s up to %s (%s days)..',
min_date.isoformat(),
max_date.isoformat(),
@@ -271,9 +270,7 @@ class Backtesting(object):
'record': self.config.get('export')
}
)
self.logging.set_format('%(message)s')
self.logger.info(
logger.info(
'\n==================================== '
'BACKTESTING REPORT'
' ====================================\n'
@@ -309,7 +306,6 @@ def start(args: Namespace) -> None:
"""
# Initialize logger
logger = Logger(name=__name__).get_logger()
logger.info('Starting freqtrade in Backtesting mode')
# Initialize configuration

View File

@@ -25,12 +25,14 @@ from pandas import DataFrame
import freqtrade.vendor.qtpylib.indicators as qtpylib
from freqtrade.arguments import Arguments
from freqtrade.configuration import Configuration
from freqtrade.logger import Logger
from freqtrade.optimize import load_data
from freqtrade.optimize.backtesting import Backtesting
from user_data.hyperopt_conf import hyperopt_optimize_conf
logger = logging.getLogger(__name__)
class Hyperopt(Backtesting):
"""
Hyperopt class, this class contains all the logic to run a hyperopt simulation
@@ -42,11 +44,6 @@ class Hyperopt(Backtesting):
def __init__(self, config: Dict[str, Any]) -> None:
super().__init__(config)
# Rename the logging to display Hyperopt file instead of Backtesting
self.logging = Logger(name=__name__, level=config['loglevel'])
self.logger = self.logging.get_logger()
# set TARGET_TRADES to suit your number concurrent trades so its realistic
# to the number of days
self.target_trades = 600
@@ -194,14 +191,14 @@ class Hyperopt(Backtesting):
"""
Save hyperopt trials to file
"""
self.logger.info('Saving Trials to \'%s\'', self.trials_file)
logger.info('Saving Trials to \'%s\'', self.trials_file)
pickle.dump(self.trials, open(self.trials_file, 'wb'))
def read_trials(self) -> Trials:
"""
Read hyperopt trials file
"""
self.logger.info('Reading Trials from \'%s\'', self.trials_file)
logger.info('Reading Trials from \'%s\'', self.trials_file)
trials = pickle.load(open(self.trials_file, 'rb'))
os.remove(self.trials_file)
return trials
@@ -212,7 +209,7 @@ class Hyperopt(Backtesting):
"""
vals = json.dumps(self.trials.best_trial['misc']['vals'], indent=4)
results = self.trials.best_trial['result']['result']
self.logger.info('Best result:\n%s\nwith values:\n%s', results, vals)
logger.info('Best result:\n%s\nwith values:\n%s', results, vals)
def log_results(self, results) -> None:
"""
@@ -220,13 +217,13 @@ class Hyperopt(Backtesting):
"""
if results['loss'] < self.current_best_loss:
self.current_best_loss = results['loss']
log_msg = '{:5d}/{}: {}. Loss {:.5f}'.format(
log_msg = '\n{:5d}/{}: {}. Loss {:.5f}'.format(
results['current_tries'],
results['total_tries'],
results['result'],
results['loss']
)
self.logger.info(log_msg)
print(log_msg)
else:
print('.', end='')
sys.stdout.flush()
@@ -511,8 +508,8 @@ class Hyperopt(Backtesting):
self.processed = self.tickerdata_to_dataframe(data)
if self.config.get('mongodb'):
self.logger.info('Using mongodb ...')
self.logger.info(
logger.info('Using mongodb ...')
logger.info(
'Start scripts/start-mongodb.sh and start-hyperopt-worker.sh manually!'
)
@@ -522,7 +519,7 @@ class Hyperopt(Backtesting):
exp_key='exp1'
)
else:
self.logger.info('Preparing Trials..')
logger.info('Preparing Trials..')
signal.signal(signal.SIGINT, self.signal_handler)
# read trials file if we have one
if os.path.exists(self.trials_file) and os.path.getsize(self.trials_file) > 0:
@@ -530,16 +527,13 @@ class Hyperopt(Backtesting):
self.current_tries = len(self.trials.results)
self.total_tries += self.current_tries
self.logger.info(
logger.info(
'Continuing with trials. Current: %d, Total: %d',
self.current_tries,
self.total_tries
)
try:
# change the Logging format
self.logging.set_format('\n%(message)s')
best_parameters = fmin(
fn=self.generate_optimizer,
space=self.hyperopt_space(),
@@ -563,11 +557,11 @@ class Hyperopt(Backtesting):
best_parameters
)
self.logger.info('Best parameters:\n%s', json.dumps(best_parameters, indent=4))
logger.info('Best parameters:\n%s', json.dumps(best_parameters, indent=4))
if 'roi_t1' in best_parameters:
self.logger.info('ROI table:\n%s', self.generate_roi_table(best_parameters))
logger.info('ROI table:\n%s', self.generate_roi_table(best_parameters))
self.logger.info('Best Result:\n%s', best_result)
logger.info('Best Result:\n%s', best_result)
# Store trials result to file to resume next time
self.save_trials()
@@ -576,7 +570,7 @@ class Hyperopt(Backtesting):
"""
Hyperopt SIGINT handler
"""
self.logger.info(
logger.info(
'Hyperopt received %s',
signal.Signals(sig).name
)
@@ -597,8 +591,6 @@ def start(args: Namespace) -> None:
logging.getLogger('hyperopt.mongoexp').setLevel(logging.WARNING)
logging.getLogger('hyperopt.tpe').setLevel(logging.WARNING)
# Initialize logger
logger = Logger(name=__name__).get_logger()
logger.info('Starting freqtrade in Hyperopt mode')
# Initialize configuration