move backtesting to freqtrade.optimize.backtesting
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
# pragma pylint: disable=missing-docstring
|
||||
import json
|
||||
import os
|
||||
from typing import Optional, List
|
||||
|
||||
|
||||
def load_backtesting_data(ticker_interval: int = 5):
|
||||
def load_backtesting_data(ticker_interval: int = 5, pairs: Optional[List[str]] = None):
|
||||
path = os.path.abspath(os.path.dirname(__file__))
|
||||
result = {}
|
||||
pairs = [
|
||||
_pairs = pairs or [
|
||||
'BTC_BCC', 'BTC_ETH', 'BTC_DASH', 'BTC_POWR', 'BTC_ETC',
|
||||
'BTC_VTC', 'BTC_WAVES', 'BTC_LSK', 'BTC_XLM', 'BTC_OK',
|
||||
]
|
||||
for pair in pairs:
|
||||
for pair in _pairs:
|
||||
with open('{abspath}/testdata/{pair}-{ticker_interval}.json'.format(
|
||||
abspath=path,
|
||||
pair=pair,
|
||||
|
@@ -1,5 +1,6 @@
|
||||
# pragma pylint: disable=missing-docstring,W0621
|
||||
import json
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import arrow
|
||||
import pytest
|
||||
@@ -35,20 +36,30 @@ def test_populates_sell_trend(result):
|
||||
|
||||
|
||||
def test_returns_latest_buy_signal(mocker):
|
||||
buydf = DataFrame([{'buy': 1, 'date': arrow.utcnow()}])
|
||||
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=buydf)
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
|
||||
mocker.patch(
|
||||
'freqtrade.analyze.analyze_ticker',
|
||||
return_value=DataFrame([{'buy': 1, 'date': arrow.utcnow()}])
|
||||
)
|
||||
assert get_signal('BTC-ETH', SignalType.BUY)
|
||||
|
||||
buydf = DataFrame([{'buy': 0, 'date': arrow.utcnow()}])
|
||||
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=buydf)
|
||||
mocker.patch(
|
||||
'freqtrade.analyze.analyze_ticker',
|
||||
return_value=DataFrame([{'buy': 0, 'date': arrow.utcnow()}])
|
||||
)
|
||||
assert not get_signal('BTC-ETH', SignalType.BUY)
|
||||
|
||||
|
||||
def test_returns_latest_sell_signal(mocker):
|
||||
selldf = DataFrame([{'sell': 1, 'date': arrow.utcnow()}])
|
||||
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=selldf)
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
|
||||
mocker.patch(
|
||||
'freqtrade.analyze.analyze_ticker',
|
||||
return_value=DataFrame([{'sell': 1, 'date': arrow.utcnow()}])
|
||||
)
|
||||
assert get_signal('BTC-ETH', SignalType.SELL)
|
||||
|
||||
selldf = DataFrame([{'sell': 0, 'date': arrow.utcnow()}])
|
||||
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=selldf)
|
||||
mocker.patch(
|
||||
'freqtrade.analyze.analyze_ticker',
|
||||
return_value=DataFrame([{'sell': 0, 'date': arrow.utcnow()}])
|
||||
)
|
||||
assert not get_signal('BTC-ETH', SignalType.SELL)
|
||||
|
@@ -1,188 +0,0 @@
|
||||
# pragma pylint: disable=missing-docstring,W0212
|
||||
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Tuple, Dict
|
||||
|
||||
import arrow
|
||||
import pytest
|
||||
from pandas import DataFrame
|
||||
from tabulate import tabulate
|
||||
|
||||
from freqtrade import exchange
|
||||
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
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def format_results(results: DataFrame):
|
||||
return ('Made {:6d} buys. Average profit {: 5.2f}%. '
|
||||
'Total profit was {: 7.3f}. Average duration {:5.1f} mins.').format(
|
||||
len(results.index),
|
||||
results.profit.mean() * 100.0,
|
||||
results.profit.sum(),
|
||||
results.duration.mean() * 5,
|
||||
)
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
"""
|
||||
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']
|
||||
return arrow.get(min_date), arrow.get(max_date)
|
||||
|
||||
|
||||
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),
|
||||
'{:.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),
|
||||
'{:.08f} {}'.format(results.profit.sum(), stake_currency),
|
||||
'{:.2f}'.format(results.duration.mean() * 5),
|
||||
])
|
||||
return tabulate(tabular_data, headers=headers)
|
||||
|
||||
|
||||
def backtest(config: Dict, processed, mocker, max_open_trades=0, realistic=True):
|
||||
"""
|
||||
Implements backtesting functionality
|
||||
:param config: config to use
|
||||
:param processed: a processed dictionary with format {pair, data}
|
||||
:param mocker: mocker instance
|
||||
:param max_open_trades: maximum number of concurrent trades (default: 0, disabled)
|
||||
:param realistic: do we try to simulate realistic trades? (default: True)
|
||||
:return: DataFrame
|
||||
"""
|
||||
trades = []
|
||||
trade_count_lock = {}
|
||||
exchange._API = Bittrex({'key': '', 'secret': ''})
|
||||
mocker.patch.dict('freqtrade.main._CONF', config)
|
||||
for pair, pair_data in processed.items():
|
||||
pair_data['buy'], pair_data['sell'] = 0, 0
|
||||
ticker = populate_sell_trend(populate_buy_trend(pair_data))
|
||||
# for each buy point
|
||||
lock_pair_until = None
|
||||
for row in ticker[ticker.buy == 1].itertuples(index=True):
|
||||
if realistic:
|
||||
if lock_pair_until is not None and row.Index <= lock_pair_until:
|
||||
continue
|
||||
if max_open_trades > 0:
|
||||
# Check if max_open_trades has already been reached for the given date
|
||||
if not trade_count_lock.get(row.date, 0) < max_open_trades:
|
||||
continue
|
||||
|
||||
if max_open_trades > 0:
|
||||
# Increase lock
|
||||
trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1
|
||||
|
||||
trade = Trade(
|
||||
open_rate=row.close,
|
||||
open_date=row.date,
|
||||
amount=config['stake_amount'],
|
||||
fee=exchange.get_fee() * 2
|
||||
)
|
||||
|
||||
# calculate win/lose forwards from buy point
|
||||
for row2 in ticker[row.Index + 1:].itertuples(index=True):
|
||||
if max_open_trades > 0:
|
||||
# Increase trade_count_lock for every iteration
|
||||
trade_count_lock[row2.date] = trade_count_lock.get(row2.date, 0) + 1
|
||||
|
||||
if min_roi_reached(trade, row2.close, row2.date) or row2.sell == 1:
|
||||
current_profit = trade.calc_profit(row2.close)
|
||||
lock_pair_until = row2.Index
|
||||
|
||||
trades.append((pair, current_profit, row2.Index - row.Index))
|
||||
break
|
||||
labels = ['currency', 'profit', 'duration']
|
||||
return DataFrame.from_records(trades, columns=labels)
|
||||
|
||||
|
||||
def get_max_open_trades(config):
|
||||
if not os.environ.get('BACKTEST_REALISTIC_SIMULATION'):
|
||||
return 0
|
||||
print('Using max_open_trades: {} ...'.format(config['max_open_trades']))
|
||||
return config['max_open_trades']
|
||||
|
||||
|
||||
@pytest.mark.skipif(not os.environ.get('BACKTEST'), reason="BACKTEST not set")
|
||||
def test_backtest(backtest_conf, mocker):
|
||||
print('')
|
||||
exchange._API = Bittrex({'key': '', 'secret': ''})
|
||||
|
||||
# Load configuration file based on env variable
|
||||
conf_path = os.environ.get('BACKTEST_CONFIG')
|
||||
if conf_path:
|
||||
print('Using config: {} ...'.format(conf_path))
|
||||
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 ...')
|
||||
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)
|
||||
|
||||
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
|
||||
realistic = os.environ.get('BACKTEST_REALISTIC_SIMULATION')
|
||||
results = backtest(config, preprocess(data), mocker, get_max_open_trades(config), realistic)
|
||||
print('====================== BACKTESTING REPORT ======================================\n\n')
|
||||
print(generate_text_table(data, results, config['stake_currency']))
|
@@ -11,9 +11,9 @@ from pandas import DataFrame
|
||||
|
||||
from freqtrade import exchange
|
||||
from freqtrade.exchange import Bittrex
|
||||
from freqtrade.optimize.backtesting import backtest, format_results
|
||||
from freqtrade.optimize.backtesting import preprocess
|
||||
from freqtrade.tests import load_backtesting_data
|
||||
from freqtrade.tests.test_backtesting import backtest, format_results
|
||||
from freqtrade.tests.test_backtesting import preprocess
|
||||
from freqtrade.vendor.qtpylib.indicators import crossed_above
|
||||
|
||||
logging.disable(logging.DEBUG) # disable debug logs that slow backtesting a lot
|
||||
|
@@ -1,15 +1,13 @@
|
||||
# pragma pylint: disable=missing-docstring,C0103
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from argparse import Namespace
|
||||
from copy import deepcopy
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from jsonschema import ValidationError
|
||||
|
||||
from freqtrade.misc import throttle, parse_args, start_backtesting, load_config
|
||||
from freqtrade.misc import throttle, parse_args, load_config
|
||||
|
||||
|
||||
def test_throttle():
|
||||
@@ -64,7 +62,7 @@ def test_parse_args_dynamic_whitelist():
|
||||
|
||||
|
||||
def test_parse_args_backtesting(mocker):
|
||||
backtesting_mock = mocker.patch('freqtrade.misc.start_backtesting', MagicMock())
|
||||
backtesting_mock = mocker.patch('freqtrade.optimize.backtesting.start', MagicMock())
|
||||
args = parse_args(['backtesting'])
|
||||
assert args is None
|
||||
assert backtesting_mock.call_count == 1
|
||||
@@ -87,7 +85,7 @@ def test_parse_args_backtesting_invalid():
|
||||
|
||||
|
||||
def test_parse_args_backtesting_custom(mocker):
|
||||
backtesting_mock = mocker.patch('freqtrade.misc.start_backtesting', MagicMock())
|
||||
backtesting_mock = mocker.patch('freqtrade.optimize.backtesting.start', MagicMock())
|
||||
args = parse_args(['-c', 'test_conf.json', 'backtesting', '--live', '--ticker-interval', '1'])
|
||||
assert args is None
|
||||
assert backtesting_mock.call_count == 1
|
||||
@@ -101,31 +99,6 @@ def test_parse_args_backtesting_custom(mocker):
|
||||
assert call_args.ticker_interval == 1
|
||||
|
||||
|
||||
def test_start_backtesting(mocker):
|
||||
pytest_mock = mocker.patch('pytest.main', MagicMock())
|
||||
env_mock = mocker.patch('os.environ', {})
|
||||
args = Namespace(
|
||||
config='config.json',
|
||||
live=True,
|
||||
loglevel=20,
|
||||
ticker_interval=1,
|
||||
realistic_simulation=True,
|
||||
)
|
||||
start_backtesting(args)
|
||||
assert env_mock == {
|
||||
'BACKTEST': 'true',
|
||||
'BACKTEST_LIVE': 'true',
|
||||
'BACKTEST_CONFIG': 'config.json',
|
||||
'BACKTEST_TICKER_INTERVAL': '1',
|
||||
'BACKTEST_REALISTIC_SIMULATION': 'true',
|
||||
}
|
||||
assert pytest_mock.call_count == 1
|
||||
|
||||
main_call_args = pytest_mock.call_args[0][0]
|
||||
assert main_call_args[0] == '-s'
|
||||
assert main_call_args[1].endswith(os.path.join('freqtrade', 'tests', 'test_backtesting.py'))
|
||||
|
||||
|
||||
def test_load_config(default_conf, mocker):
|
||||
file_mock = mocker.patch('freqtrade.misc.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
|
18
freqtrade/tests/test_optimize_backtesting.py
Normal file
18
freqtrade/tests/test_optimize_backtesting.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# pragma pylint: disable=missing-docstring,W0212
|
||||
|
||||
|
||||
from freqtrade import exchange
|
||||
from freqtrade.exchange import Bittrex
|
||||
from freqtrade.optimize.backtesting import backtest, preprocess
|
||||
from freqtrade.tests import load_backtesting_data
|
||||
|
||||
|
||||
def test_backtest(backtest_conf, mocker):
|
||||
mocker.patch.dict('freqtrade.main._CONF', backtest_conf)
|
||||
exchange._API = Bittrex({'key': '', 'secret': ''})
|
||||
|
||||
data = load_backtesting_data(ticker_interval=5, pairs=['BTC_ETH'])
|
||||
results = backtest(backtest_conf, preprocess(data), 10, True)
|
||||
num_resutls = len(results)
|
||||
assert num_resutls > 0
|
||||
|
Reference in New Issue
Block a user