Repository: freqtrade/freqtrade

Issue:
Strong deviations between Hyperopt and Backtest Results
## Step 1: Have you search for this issue before posting it?

If you have discovered a bug in the bot, please [search our issue tracker](https://github.com/freqtrade/freqtrade/issues?q=is%3Aissue). 
If it hasn't been reported, please create a new issue.

## Step 2: Describe your environment

  * Python Version: 3.7.3
  * CCXT version: pip freeze | grep ccxt
  * Branch: Develop
  * Last Commit ID: d055dc0c6e0aa24297e5309eee7ea7e511bf9e51

  
## Step 3: Describe the problem:

Strong deviations between Hyperopt and Backtest Results, **number of trades and total profit**

### Steps to reproduce:

  1. strategy (MACD and CCI based) coded in Hyperopt to find the best parameters for the CCI buy / sell, stoploss and ROIs
  2. run Hyperopt
  3. put the paramters in the strategy and backtested over the same timeframe as hyperopt was running
  
### Observed Results:

Results should be similar, correct?

### Relevant code exceptions or logs:

hyperopt results

  ```
2019-07-09 07:01:39,815 - freqtrade.exchange.exchange - INFO - downloaded LINK/BTC with length 1.
2019-07-09 07:01:39,816 - freqtrade.misc - INFO - dumping json to "user_data/data/binance/LINK_BTC-5m.json"
2019-07-09 07:01:40,851 - freqtrade.data.converter - INFO - Missing data fillup for LINK/BTC: before: 71711 - after: 71999
2019-07-09 07:01:40,884 - freqtrade.optimize.hyperopt - INFO - Hyperopting with data from 2018-11-01T05:05:00+00:00 up to 2019-07-09T04:55:00+00:00 (249 days)..

2019-07-09 09:43:45,460 - freqtrade.optimize.hyperopt - INFO - Best result:
   604 trades. Avg profit  0.53%. Total profit  0.48081498 BTC ( 320.22Σ%). Avg duration 1292.9 mins.
with values:

{   'buy-cci-value': -12,
    'roi_p1': 0.014714316102334512,
    'roi_p2': 0.06416378274370114,
    'roi_p3': 0.12198411469836923,
    'roi_t1': 114,
    'roi_t2': 43,
    'roi_t3': 33,
    'sell-cci-value': 158,
    'stoploss': -0.4700606834872094}


  ```

Backtest results

  ```

2019-07-09 16:59:11,178 - freqtrade.optimize.backtesting - INFO - Backtesting with data from 2018-11-01T00:00:00+00:00 up to 2019-07-08T23:55:00+00:00 (249 days)..
2019-07-09 16:59:11,178 - freqtrade.optimize.backtesting - INFO - Running backtesting for Strategy MACDStrategy1
Result for strategy MACDStrategy1
========================================================= BACKTESTING REPORT ========================================================
| pair     |   buy count |   avg profit % |   cum profit % |   tot profit BTC |   tot profit % | avg duration    |   profit |   loss |
|:---------|------------:|---------------:|---------------:|-----------------:|---------------:|:----------------|---------:|-------:|
| LINK/BTC |          88 |           0.63 |          55.61 |       0.08349766 |          18.54 | 2 days, 6:31:00 |       33 |      1 |
| TOTAL    |          88 |           0.63 |          55.61 |       0.08349766 |          18.54 | 2 days, 6:31:00 |       33 |      1 |
========================================================= SELL REASON STATS =========================================================
| Sell Reason   |   Count |
|:--------------|--------:|
| roi           |      87 |
| force_sell    |       1 |
====================================================== LEFT OPEN TRADES REPORT ======================================================
| pair     |   buy count |   avg profit % |   cum profit % |   tot profit BTC |   tot profit % | avg duration   |   profit |   loss |
|:---------|------------:|---------------:|---------------:|-----------------:|---------------:|:---------------|---------:|-------:|
| LINK/BTC |           1 |          -5.67 |          -5.67 |      -0.00851834 |          -1.89 | 11:40:00       |        0 |      1 |
| TOTAL    |           1 |          -5.67 |          -5.67 |      -0.00851834 |          -1.89 | 11:40:00       |        0 |      1 |


  ```

Strong deviations between Hyperopt and Backtest Results
## Step 1: Have you search for this issue before posting it?

If you have discovered a bug in the bot, please [search our issue tracker](https://github.com/freqtrade/freqtrade/issues?q=is%3Aissue). 
If it hasn't been reported, please create a new issue.

## Step 2: Describe your environment

  * Python Version: 3.7.3
  * CCXT version: pip freeze | grep ccxt
  * Branch: Develop
  * Last Commit ID: d055dc0c6e0aa24297e5309eee7ea7e511bf9e51

  
## Step 3: Describe the problem:

Strong deviations between Hyperopt and Backtest Results, **number of trades and total profit**

### Steps to reproduce:

  1. strategy (MACD and CCI based) coded in Hyperopt to find the best parameters for the CCI buy / sell, stoploss and ROIs
  2. run Hyperopt
  3. put the paramters in the strategy and backtested over the same timeframe as hyperopt was running
  
### Observed Results:

Results should be similar, correct?

### Relevant code exceptions or logs:

hyperopt results

  ```
2019-07-09 07:01:39,815 - freqtrade.exchange.exchange - INFO - downloaded LINK/BTC with length 1.
2019-07-09 07:01:39,816 - freqtrade.misc - INFO - dumping json to "user_data/data/binance/LINK_BTC-5m.json"
2019-07-09 07:01:40,851 - freqtrade.data.converter - INFO - Missing data fillup for LINK/BTC: before: 71711 - after: 71999
2019-07-09 07:01:40,884 - freqtrade.optimize.hyperopt - INFO - Hyperopting with data from 2018-11-01T05:05:00+00:00 up to 2019-07-09T04:55:00+00:00 (249 days)..

2019-07-09 09:43:45,460 - freqtrade.optimize.hyperopt - INFO - Best result:
   604 trades. Avg profit  0.53%. Total profit  0.48081498 BTC ( 320.22Σ%). Avg duration 1292.9 mins.
with values:

{   'buy-cci-value': -12,
    'roi_p1': 0.014714316102334512,
    'roi_p2': 0.06416378274370114,
    'roi_p3': 0.12198411469836923,
    'roi_t1': 114,
    'roi_t2': 43,
    'roi_t3': 33,
    'sell-cci-value': 158,
    'stoploss': -0.4700606834872094}


  ```

Backtest results

  ```

2019-07-09 16:59:11,178 - freqtrade.optimize.backtesting - INFO - Backtesting with data from 2018-11-01T00:00:00+00:00 up to 2019-07-08T23:55:00+00:00 (249 days)..
2019-07-09 16:59:11,178 - freqtrade.optimize.backtesting - INFO - Running backtesting for Strategy MACDStrategy1
Result for strategy MACDStrategy1
========================================================= BACKTESTING REPORT ========================================================
| pair     |   buy count |   avg profit % |   cum profit % |   tot profit BTC |   tot profit % | avg duration    |   profit |   loss |
|:---------|------------:|---------------:|---------------:|-----------------:|---------------:|:----------------|---------:|-------:|
| LINK/BTC |          88 |           0.63 |          55.61 |       0.08349766 |          18.54 | 2 days, 6:31:00 |       33 |      1 |
| TOTAL    |          88 |           0.63 |          55.61 |       0.08349766 |          18.54 | 2 days, 6:31:00 |       33 |      1 |
========================================================= SELL REASON STATS =========================================================
| Sell Reason   |   Count |
|:--------------|--------:|
| roi           |      87 |
| force_sell    |       1 |
====================================================== LEFT OPEN TRADES REPORT ======================================================
| pair     |   buy count |   avg profit % |   cum profit % |   tot profit BTC |   tot profit % | avg duration   |   profit |   loss |
|:---------|------------:|---------------:|---------------:|-----------------:|---------------:|:---------------|---------:|-------:|
| LINK/BTC |           1 |          -5.67 |          -5.67 |      -0.00851834 |          -1.89 | 11:40:00       |        0 |      1 |
| TOTAL    |           1 |          -5.67 |          -5.67 |      -0.00851834 |          -1.89 | 11:40:00       |        0 |      1 |


  ```



Discussion:

Resolving patch:
diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py
--- a/freqtrade/optimize/backtesting.py
+++ b/freqtrade/optimize/backtesting.py
@@ -319,6 +319,9 @@ def backtest(self, args: Dict) -> DataFrame:
             position_stacking: do we allow position stacking? (default: False)
         :return: DataFrame
         """
+        # Arguments are long and noisy, so this is commented out.
+        # Uncomment if you need to debug the backtest() method.
+#        logger.debug(f"Start backtest, args: {args}")
         processed = args['processed']
         stake_amount = args['stake_amount']
         max_open_trades = args.get('max_open_trades', 0)
diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py
--- a/freqtrade/optimize/hyperopt.py
+++ b/freqtrade/optimize/hyperopt.py
@@ -190,12 +190,22 @@ def generate_optimizer(self, _params: Dict) -> Dict:
             self.strategy.stoploss = params['stoploss']
 
         processed = load(TICKERDATA_PICKLE)
+
+        # Use max_open_trades for hyperopt as well, 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.debug('Ignoring max_open_trades (--disable-max-market-positions was used) ...')
+            max_open_trades = 0
+
         min_date, max_date = get_timeframe(processed)
+
         results = self.backtest(
             {
                 'stake_amount': self.config['stake_amount'],
                 'processed': processed,
-                'position_stacking': self.config.get('position_stacking', True),
+                'max_open_trades': max_open_trades,
+                'position_stacking': self.config.get('position_stacking', False),
                 'start_date': min_date,
                 'end_date': max_date,
             }


Test patch: