Skip to content

Commit

Permalink
Merge pull request freqtrade#1037 from freqtrade/fix/backtest-comment
Browse files Browse the repository at this point in the history
replace --realistic with 2 separate flags
  • Loading branch information
vertti authored Jul 19, 2018
2 parents 6070d81 + 71100a6 commit 0cc1b66
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 57 deletions.
8 changes: 4 additions & 4 deletions docs/backtesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ The backtesting is very easy with freqtrade.
#### With 5 min tickers (Per default)

```bash
python3 ./freqtrade/main.py backtesting --realistic-simulation
python3 ./freqtrade/main.py backtesting
```

#### With 1 min tickers

```bash
python3 ./freqtrade/main.py backtesting --realistic-simulation --ticker-interval 1m
python3 ./freqtrade/main.py backtesting --ticker-interval 1m
```

#### Update cached pairs with the latest data

```bash
python3 ./freqtrade/main.py backtesting --realistic-simulation --refresh-pairs-cached
python3 ./freqtrade/main.py backtesting --refresh-pairs-cached
```

#### With live data (do not alter your testdata files)

```bash
python3 ./freqtrade/main.py backtesting --realistic-simulation --live
python3 ./freqtrade/main.py backtesting --live
```

#### Using a different on-disk ticker-data source
Expand Down
45 changes: 28 additions & 17 deletions docs/bot-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,21 @@ python3 ./freqtrade/main.py -c config.json --db-url sqlite:///tradesv3.dry_run.s
Backtesting also uses the config specified via `-c/--config`.

```
usage: main.py backtesting [-h] [-i TICKER_INTERVAL] [--realistic-simulation]
[--timerange TIMERANGE] [-l] [-r] [--export EXPORT]
[--export-filename EXPORTFILENAME]
usage: main.py backtesting [-h] [-i TICKER_INTERVAL] [--eps] [--dmmp]
[--timerange TIMERANGE] [-l] [-r]
[--export EXPORT] [--export-filename PATH]
optional arguments:
-h, --help show this help message and exit
-i TICKER_INTERVAL, --ticker-interval TICKER_INTERVAL
specify ticker interval (1m, 5m, 30m, 1h, 1d)
--realistic-simulation
uses max_open_trades from config to simulate real
world limitations
--eps, --enable-position-stacking
Allow buying the same pair multiple times (position
stacking)
--dmmp, --disable-max-market-positions
Disable applying `max_open_trades` during backtest
(same as setting `max_open_trades` to a very high
number)
--timerange TIMERANGE
specify what timerange of data to use.
-l, --live using live data
Expand All @@ -138,11 +141,13 @@ optional arguments:
run your backtesting with up-to-date data.
--export EXPORT export backtest results, argument are: trades Example
--export=trades
--export-filename EXPORTFILENAME
--export-filename PATH
Save backtest results to this filename requires
--export to be set as well Example --export-
filename=backtest_today.json (default: backtest-
result.json
filename=user_data/backtest_data/backtest_today.json
(default: user_data/backtest_data/backtest-
result.json)
```

### How to use --refresh-pairs-cached parameter?
Expand All @@ -164,22 +169,28 @@ To optimize your strategy, you can use hyperopt parameter hyperoptimization
to find optimal parameter values for your stategy.

```
usage: main.py hyperopt [-h] [-i TICKER_INTERVAL] [--realistic-simulation]
[--timerange TIMERANGE] [-e INT]
[-s {all,buy,roi,stoploss} [{all,buy,roi,stoploss} ...]]
usage: freqtrade hyperopt [-h] [-i TICKER_INTERVAL] [--eps] [--dmmp]
[--timerange TIMERANGE] [-e INT]
[-s {all,buy,roi,stoploss} [{all,buy,roi,stoploss} ...]]
optional arguments:
-h, --help show this help message and exit
-i TICKER_INTERVAL, --ticker-interval TICKER_INTERVAL
specify ticker interval (1m, 5m, 30m, 1h, 1d)
--realistic-simulation
uses max_open_trades from config to simulate real
world limitations
--timerange TIMERANGE specify what timerange of data to use.
--eps, --enable-position-stacking
Allow buying the same pair multiple times (position
stacking)
--dmmp, --disable-max-market-positions
Disable applying `max_open_trades` during backtest
(same as setting `max_open_trades` to a very high
number)
--timerange TIMERANGE
specify what timerange of data to use.
-e INT, --epochs INT specify number of epochs (default: 100)
-s {all,buy,roi,stoploss} [{all,buy,roi,stoploss} ...], --spaces {all,buy,roi,stoploss} [{all,buy,roi,stoploss} ...]
Specify which parameters to hyperopt. Space separate
list. Default: all
```

## A parameter missing in the configuration?
Expand Down
17 changes: 14 additions & 3 deletions freqtrade/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,22 @@ def optimizer_shared_options(parser: argparse.ArgumentParser) -> None:
type=str,
)
parser.add_argument(
'--realistic-simulation',
help='uses max_open_trades from config to simulate real world limitations',
'--eps', '--enable-position-stacking',
help='Allow buying the same pair multiple times (position stacking)',
action='store_true',
dest='realistic_simulation',
dest='position_stacking',
default=False
)

parser.add_argument(
'--dmmp', '--disable-max-market-positions',
help='Disable applying `max_open_trades` during backtest '
'(same as setting `max_open_trades` to a very high number)',
action='store_false',
dest='use_max_market_positions',
default=True
)

parser.add_argument(
'--timerange',
help='specify what timerange of data to use.',
Expand Down
19 changes: 13 additions & 6 deletions freqtrade/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,18 @@ def _load_backtesting_config(self, config: Dict[str, Any]) -> Dict[str, Any]:
config.update({'live': True})
logger.info('Parameter -l/--live detected ...')

# If --realistic-simulation is used we add it to the configuration
if 'realistic_simulation' in self.args and self.args.realistic_simulation:
config.update({'realistic_simulation': True})
logger.info('Parameter --realistic-simulation detected ...')
logger.info('Using max_open_trades: %s ...', config.get('max_open_trades'))
# If --enable-position-stacking is used we add it to the configuration
if 'position_stacking' in self.args and self.args.position_stacking:
config.update({'position_stacking': True})
logger.info('Parameter --enable-position-stacking detected ...')

# If --disable-max-market-positions is used we add it to the configuration
if 'use_max_market_positions' in self.args and not self.args.use_max_market_positions:
config.update({'use_max_market_positions': False})
logger.info('Parameter --disable-max-market-positions detected ...')
logger.info('max_open_trades set to unlimited ...')
else:
logger.info('Using max_open_trades: %s ...', config.get('max_open_trades'))

# If --timerange is used we add it to the configuration
if 'timerange' in self.args and self.args.timerange:
Expand Down Expand Up @@ -195,7 +202,7 @@ def _load_hyperopt_config(self, config: Dict[str, Any]) -> Dict[str, Any]:
Extract information for sys.argv and load Hyperopt configuration
:return: configuration as dictionary
"""
# If --realistic-simulation is used we add it to the configuration
# If --epochs is used we add it to the configuration
if 'epochs' in self.args and self.args.epochs:
config.update({'epochs': self.args.epochs})
logger.info('Parameter --epochs detected ...')
Expand Down
14 changes: 7 additions & 7 deletions freqtrade/optimize/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ def backtest(self, args: Dict) -> DataFrame:
stake_amount: btc amount to use for each trade
processed: a processed dictionary with format {pair, data}
max_open_trades: maximum number of concurrent trades (default: 0, disabled)
realistic: do we try to simulate realistic trades? (default: True)
position_stacking: do we allow position stacking? (default: False)
:return: DataFrame
"""
headers = ['date', 'buy', 'open', 'close', 'sell']
processed = args['processed']
max_open_trades = args.get('max_open_trades', 0)
realistic = args.get('realistic', False)
position_stacking = args.get('position_stacking', False)
trades = []
trade_count_lock: Dict = {}
for pair, pair_data in processed.items():
Expand All @@ -232,7 +232,7 @@ def backtest(self, args: Dict) -> DataFrame:
if row.buy == 0 or row.sell == 1:
continue # skip rows where no buy signal or that would immediately sell off

if realistic:
if not position_stacking:
if lock_pair_until is not None and row.date <= lock_pair_until:
continue
if max_open_trades > 0:
Expand Down Expand Up @@ -286,11 +286,11 @@ def start(self) -> None:
if not data:
logger.critical("No data found. Terminating.")
return
# Ignore max_open_trades in backtesting, except realistic flag was passed
if self.config.get('realistic_simulation', False):
# Use max_open_trades in backtesting, except --disable-max-market-positions is set
if self.config.get('use_max_market_positions', True):
max_open_trades = self.config['max_open_trades']
else:
logger.info('Ignoring max_open_trades (realistic_simulation not set) ...')
logger.info('Ignoring max_open_trades (--disable-max-market-positions was used) ...')
max_open_trades = 0

preprocessed = self.tickerdata_to_dataframe(data)
Expand All @@ -310,7 +310,7 @@ def start(self) -> None:
'stake_amount': self.config.get('stake_amount'),
'processed': preprocessed,
'max_open_trades': max_open_trades,
'realistic': self.config.get('realistic_simulation', False),
'position_stacking': self.config.get('position_stacking', False),
}
)

Expand Down
2 changes: 1 addition & 1 deletion freqtrade/optimize/hyperopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def generate_optimizer(self, _params) -> Dict:
{
'stake_amount': self.config['stake_amount'],
'processed': processed,
'realistic': self.config.get('realistic_simulation', False),
'position_stacking': self.config.get('position_stacking', True),
}
)
result_explanation = self.format_results(results)
Expand Down
31 changes: 18 additions & 13 deletions freqtrade/tests/optimize/test_backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def simple_backtest(config, contour, num_results, mocker) -> None:
'stake_amount': config['stake_amount'],
'processed': processed,
'max_open_trades': 1,
'realistic': True
'position_stacking': False
}
)
# results :: <class 'pandas.core.frame.DataFrame'>
Expand Down Expand Up @@ -127,7 +127,7 @@ def _make_backtest_conf(mocker, conf=None, pair='UNITTEST/BTC', record=None):
'stake_amount': conf['stake_amount'],
'processed': backtesting.tickerdata_to_dataframe(data),
'max_open_trades': 10,
'realistic': True,
'position_stacking': False,
'record': record
}

Expand Down Expand Up @@ -193,8 +193,8 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) ->
assert 'live' not in config
assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples)

assert 'realistic_simulation' not in config
assert not log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
assert 'position_stacking' not in config
assert not log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples)

assert 'refresh_pairs' not in config
assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
Expand All @@ -218,7 +218,8 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non
'backtesting',
'--ticker-interval', '1m',
'--live',
'--realistic-simulation',
'--enable-position-stacking',
'--disable-max-market-positions',
'--refresh-pairs-cached',
'--timerange', ':100',
'--export', '/bar/foo',
Expand Down Expand Up @@ -246,9 +247,12 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non
assert 'live' in config
assert log_has('Parameter -l/--live detected ...', caplog.record_tuples)

assert 'realistic_simulation' in config
assert log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
assert log_has('Using max_open_trades: 1 ...', caplog.record_tuples)
assert 'position_stacking' in config
assert log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples)

assert 'use_max_market_positions' in config
assert log_has('Parameter --disable-max-market-positions detected ...', caplog.record_tuples)
assert log_has('max_open_trades set to unlimited ...', caplog.record_tuples)

assert 'refresh_pairs' in config
assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
Expand Down Expand Up @@ -491,7 +495,7 @@ def test_backtest(default_conf, fee, mocker) -> None:
'stake_amount': default_conf['stake_amount'],
'processed': data_processed,
'max_open_trades': 10,
'realistic': True
'position_stacking': False
}
)
assert not results.empty
Expand Down Expand Up @@ -539,7 +543,7 @@ def test_backtest_1min_ticker_interval(default_conf, fee, mocker) -> None:
'stake_amount': default_conf['stake_amount'],
'processed': backtesting.tickerdata_to_dataframe(data),
'max_open_trades': 1,
'realistic': True
'position_stacking': False
}
)
assert not results.empty
Expand Down Expand Up @@ -714,7 +718,8 @@ def test_backtest_start_live(default_conf, mocker, caplog):
'--ticker-interval', '1m',
'--live',
'--timerange', '-100',
'--realistic-simulation'
'--enable-position-stacking',
'--disable-max-market-positions'
]
args = get_args(args)
start(args)
Expand All @@ -723,14 +728,14 @@ def test_backtest_start_live(default_conf, mocker, caplog):
'Parameter -i/--ticker-interval detected ...',
'Using ticker_interval: 1m ...',
'Parameter -l/--live detected ...',
'Using max_open_trades: 1 ...',
'Ignoring max_open_trades (--disable-max-market-positions was used) ...',
'Parameter --timerange detected: -100 ...',
'Using data folder: freqtrade/tests/testdata ...',
'Using stake_currency: BTC ...',
'Using stake_amount: 0.001 ...',
'Downloading data for all pairs in whitelist ...',
'Measuring data from 2017-11-14T19:31:00+00:00 up to 2017-11-14T22:58:00+00:00 (0 days)..',
'Parameter --realistic-simulation detected ...'
'Parameter --enable-position-stacking detected ...'
]

for line in exists:
Expand Down
16 changes: 10 additions & 6 deletions freqtrade/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) ->
assert 'live' not in config
assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples)

assert 'realistic_simulation' not in config
assert not log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
assert 'position_stacking' not in config
assert not log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples)

assert 'refresh_pairs' not in config
assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
Expand All @@ -301,7 +301,8 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non
'backtesting',
'--ticker-interval', '1m',
'--live',
'--realistic-simulation',
'--enable-position-stacking',
'--disable-max-market-positions',
'--refresh-pairs-cached',
'--timerange', ':100',
'--export', '/bar/foo'
Expand Down Expand Up @@ -331,9 +332,12 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non
assert 'live' in config
assert log_has('Parameter -l/--live detected ...', caplog.record_tuples)

assert 'realistic_simulation'in config
assert log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
assert log_has('Using max_open_trades: 1 ...', caplog.record_tuples)
assert 'position_stacking'in config
assert log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples)

assert 'use_max_market_positions' in config
assert log_has('Parameter --disable-max-market-positions detected ...', caplog.record_tuples)
assert log_has('max_open_trades set to unlimited ...', caplog.record_tuples)

assert 'refresh_pairs'in config
assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
Expand Down

0 comments on commit 0cc1b66

Please sign in to comment.