stable/freqtrade/tests/test_backtesting.py

155 lines
5.7 KiB
Python
Raw Normal View History

# pragma pylint: disable=missing-docstring,W0212
2017-11-14 21:15:24 +00:00
2017-11-17 16:54:31 +00:00
import logging
2017-09-28 21:26:28 +00:00
import os
2017-11-14 21:15:24 +00:00
from typing import Tuple, Dict
2017-09-28 21:26:28 +00:00
import arrow
2017-11-14 21:15:24 +00:00
import pytest
from pandas import DataFrame
from tabulate import tabulate
2017-09-28 21:26:28 +00:00
from freqtrade import exchange
from freqtrade.analyze import parse_ticker_dataframe, populate_indicators, \
populate_buy_trend, populate_sell_trend
from freqtrade.exchange import Bittrex
2017-11-16 05:49:06 +00:00
from freqtrade.main import min_roi_reached
2017-11-17 16:54:31 +00:00
from freqtrade.misc import load_config
2017-09-28 21:26:28 +00:00
from freqtrade.persistence import Trade
from freqtrade.tests import load_backtesting_data
2017-09-28 21:26:28 +00:00
2017-11-14 21:15:24 +00:00
logger = logging.getLogger(__name__)
2017-10-30 23:36:35 +00:00
2017-11-14 21:15:24 +00:00
def format_results(results: DataFrame):
return ('Made {} buys. Average profit {:.2f}%. '
'Total profit was {:.3f}. Average duration {:.1f} mins.').format(
len(results.index),
results.profit.mean() * 100.0,
results.profit.sum(),
results.duration.mean() * 5,
)
2017-10-30 23:36:35 +00:00
def preprocess(backdata) -> Dict[str, DataFrame]:
processed = {}
for pair, pair_data in backdata.items():
processed[pair] = populate_indicators(parse_ticker_dataframe(pair_data))
return processed
2017-11-17 16:54:31 +00:00
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
:return: tuple containing min_date, max_date
"""
2017-11-14 21:15:24 +00:00
min_date, max_date = None, None
for values in data.values():
sorted_values = sorted(values, key=lambda d: arrow.get(d['T']))
if not min_date or sorted_values[0]['T'] < min_date:
min_date = sorted_values[0]['T']
if not max_date or sorted_values[-1]['T'] > max_date:
max_date = sorted_values[-1]['T']
2017-11-14 21:15:24 +00:00
return arrow.get(min_date), arrow.get(max_date)
2017-11-14 22:46:48 +00:00
def generate_text_table(data: Dict[str, Dict], results: DataFrame, stake_currency) -> str:
"""
Generates and returns a text table for the given backtest data and the results dataframe
:return: pretty printed table with tabulate as str
"""
tabular_data = []
headers = ['pair', 'buy count', 'avg profit', 'total profit', 'avg duration']
for pair in data:
result = results[results.currency == pair]
tabular_data.append([
pair,
len(result.index),
'{:.2f}%'.format(result.profit.mean() * 100.0),
2017-11-14 22:46:48 +00:00
'{:.08f} {}'.format(result.profit.sum(), stake_currency),
'{:.2f}'.format(result.duration.mean() * 5),
])
# Append Total
tabular_data.append([
'TOTAL',
len(results.index),
'{:.2f}%'.format(results.profit.mean() * 100.0),
2017-11-14 22:46:48 +00:00
'{:.08f} {}'.format(results.profit.sum(), stake_currency),
'{:.2f}'.format(results.duration.mean() * 5),
])
return tabulate(tabular_data, headers=headers)
def backtest(backtest_conf, processed, mocker):
trades = []
exchange._API = Bittrex({'key': '', 'secret': ''})
2017-11-07 19:12:56 +00:00
mocker.patch.dict('freqtrade.main._CONF', backtest_conf)
for pair, pair_data in processed.items():
pair_data['buy'] = 0
pair_data['sell'] = 0
ticker = populate_sell_trend(populate_buy_trend(pair_data))
# for each buy point
for row in ticker[ticker.buy == 1].itertuples(index=True):
trade = Trade(
open_rate=row.close,
open_date=row.date,
2017-11-14 22:46:48 +00:00
amount=backtest_conf['stake_amount'],
fee=exchange.get_fee() * 2
)
# calculate win/lose forwards from buy point
for row2 in ticker[row.Index:].itertuples(index=True):
2017-11-16 05:49:06 +00:00
if min_roi_reached(trade, row2.close, row2.date) or row2.sell == 1:
current_profit = trade.calc_profit(row2.close)
trades.append((pair, current_profit, row2.Index - row.Index))
break
labels = ['currency', 'profit', 'duration']
2017-11-17 06:02:06 +00:00
return DataFrame.from_records(trades, columns=labels)
2017-10-30 23:36:35 +00:00
2017-11-14 21:15:24 +00:00
@pytest.mark.skipif(not os.environ.get('BACKTEST'), reason="BACKTEST not set")
def test_backtest(backtest_conf, mocker):
2017-11-14 21:15:24 +00:00
print('')
2017-11-17 16:54:31 +00:00
exchange._API = Bittrex({'key': '', 'secret': ''})
2017-11-14 21:15:24 +00:00
2017-11-17 16:54:31 +00:00
# Load configuration file based on env variable
2017-11-14 21:15:24 +00:00
conf_path = os.environ.get('BACKTEST_CONFIG')
if conf_path:
print('Using config: {} ...'.format(conf_path))
2017-11-17 16:54:31 +00:00
config = load_config(conf_path)
else:
config = backtest_conf
2017-11-14 21:15:24 +00:00
2017-11-17 16:54:31 +00:00
# Parse ticker interval
2017-11-14 21:37:30 +00:00
ticker_interval = int(os.environ.get('BACKTEST_TICKER_INTERVAL') or 5)
print('Using ticker_interval: {} ...'.format(ticker_interval))
2017-11-14 21:37:30 +00:00
data = {}
2017-11-14 21:15:24 +00:00
if os.environ.get('BACKTEST_LIVE'):
print('Downloading data for all pairs in whitelist ...')
2017-11-14 21:15:24 +00:00
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)
2017-11-14 21:15:24 +00:00
2017-11-14 22:46:48 +00:00
print('Using stake_currency: {} ...\nUsing stake_amount: {} ...'.format(
config['stake_currency'], config['stake_amount']
))
2017-11-17 16:54:31 +00:00
# Print timeframe
2017-11-14 21:15:24 +00:00
min_date, max_date = get_timeframe(data)
print('Measuring data from {} up to {} ...'.format(
min_date.isoformat(), max_date.isoformat()
))
2017-11-17 16:54:31 +00:00
# Execute backtest and print results
2017-11-14 21:15:24 +00:00
results = backtest(config, preprocess(data), mocker)
2017-11-14 22:46:48 +00:00
print('====================== BACKTESTING REPORT ======================================\n\n'
'NOTE: This Report doesn\'t respect the limits of max_open_trades, \n'
' so the projected values should be taken with a grain of salt.\n')
print(generate_text_table(data, results, config['stake_currency']))