Repository: freqtrade/freqtrade

Issue:
Regarding trailing and process_only_new_candles
Hi,

I'm running a swing strategy that runs on either the `1d` or `3d`  `timeframe`. 
Three things:

1) The backtests show that the strategy will not buy for a certain period, however, as soon as the bot started, it will buy, these signals cannot be found on the plot.

Does the `process_only_new_candles` affect this in any way?

2) The `trailing_stop_positive` of the strategy is about ~2%, I'm unaware of the strategy running at a `1d` or `3d` timeframe will take this into consideration when doing a `Hyperopt`. That is, does the Hyperopt will look into these price changes? 

3) For some reason the `avg duration` of the trades is showing as 0:00:00 for a lot of my backtests....is this normal?

 
![image](https://user-images.githubusercontent.com/53630064/99047712-8d0f9a00-25cf-11eb-901e-d45761a481f3.png)

Regarding trailing and process_only_new_candles
Hi,

I'm running a swing strategy that runs on either the `1d` or `3d`  `timeframe`. 
Three things:

1) The backtests show that the strategy will not buy for a certain period, however, as soon as the bot started, it will buy, these signals cannot be found on the plot.

Does the `process_only_new_candles` affect this in any way?

2) The `trailing_stop_positive` of the strategy is about ~2%, I'm unaware of the strategy running at a `1d` or `3d` timeframe will take this into consideration when doing a `Hyperopt`. That is, does the Hyperopt will look into these price changes? 

3) For some reason the `avg duration` of the trades is showing as 0:00:00 for a lot of my backtests....is this normal?

 
![image](https://user-images.githubusercontent.com/53630064/99047712-8d0f9a00-25cf-11eb-901e-d45761a481f3.png)



Discussion:
I went tru' the "assumptions made by backtesting", but still had those doubts, specially the odd thing regarding the 0:00:00 duration.

Is it possible to just adjust the trailing_space() in hyperopt_interface.py to just force the optimizer to look for higher values? 
You can use the `AdvancedSampleHyperOpt` (generated automatically when creating a user directory) as sample, it includes a custom trailing space method.

the 0:00 is clear when using 2% with long timeframes - as it'll sell at the same candle.
your trailing parameters should be > than the average candle size - on 3d candles, 2% swings will almost always happen - so your backtest is basically worthless, as you'll not know what intra-candle movements the pair took.


Also, i'm sure that the signals will be there when the bot starts - maybe your plot_dataframe starts too late.
I don't think plotting considers startup-candles, as you'll very rarely be able to plot the full timerange you're backtesting anyway (although that assumption changes when you're using 1d candles).
Adding that support would be rather simple - but it's currently never come up.

I went tru' the "assumptions made by backtesting", but still had those doubts, specially the odd thing regarding the 0:00:00 duration.

Is it possible to just adjust the trailing_space() in hyperopt_interface.py to just force the optimizer to look for higher values? 
You can use the `AdvancedSampleHyperOpt` (generated automatically when creating a user directory) as sample, it includes a custom trailing space method.

the 0:00 is clear when using 2% with long timeframes - as it'll sell at the same candle.
your trailing parameters should be > than the average candle size - on 3d candles, 2% swings will almost always happen - so your backtest is basically worthless, as you'll not know what intra-candle movements the pair took.


Also, i'm sure that the signals will be there when the bot starts - maybe your plot_dataframe starts too late.
I don't think plotting considers startup-candles, as you'll very rarely be able to plot the full timerange you're backtesting anyway (although that assumption changes when you're using 1d candles).
Adding that support would be rather simple - but it's currently never come up.


Resolving patch:
diff --git a/freqtrade/plot/plotting.py b/freqtrade/plot/plotting.py
--- a/freqtrade/plot/plotting.py
+++ b/freqtrade/plot/plotting.py
@@ -9,9 +9,9 @@
                                        create_cum_profit, extract_trades_of_period, load_trades)
 from freqtrade.data.converter import trim_dataframe
 from freqtrade.data.dataprovider import DataProvider
-from freqtrade.data.history import load_data
+from freqtrade.data.history import get_timerange, load_data
 from freqtrade.exceptions import OperationalException
-from freqtrade.exchange import timeframe_to_prev_date
+from freqtrade.exchange import timeframe_to_prev_date, timeframe_to_seconds
 from freqtrade.misc import pair_to_filename
 from freqtrade.resolvers import ExchangeResolver, StrategyResolver
 from freqtrade.strategy import IStrategy
@@ -29,7 +29,7 @@
     exit(1)
 
 
-def init_plotscript(config):
+def init_plotscript(config, startup_candles: int = 0):
     """
     Initialize objects needed for plotting
     :return: Dict with candle (OHLCV) data, trades and pairs
@@ -48,9 +48,16 @@ def init_plotscript(config):
         pairs=pairs,
         timeframe=config.get('timeframe', '5m'),
         timerange=timerange,
+        startup_candles=startup_candles,
         data_format=config.get('dataformat_ohlcv', 'json'),
     )
 
+    if startup_candles:
+        min_date, max_date = get_timerange(data)
+        logger.info(f"Loading data from {min_date} to {max_date}")
+        timerange.adjust_start_if_necessary(timeframe_to_seconds(config.get('timeframe', '5m')),
+                                            startup_candles, min_date)
+
     no_trades = False
     filename = config.get('exportfilename')
     if config.get('no_trades', False):
@@ -72,6 +79,7 @@ def init_plotscript(config):
     return {"ohlcv": data,
             "trades": trades,
             "pairs": pairs,
+            "timerange": timerange,
             }
 
 
@@ -474,7 +482,8 @@ def load_and_plot_trades(config: Dict[str, Any]):
 
     exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config)
     IStrategy.dp = DataProvider(config, exchange)
-    plot_elements = init_plotscript(config)
+    plot_elements = init_plotscript(config, strategy.startup_candle_count)
+    timerange = plot_elements['timerange']
     trades = plot_elements['trades']
     pair_counter = 0
     for pair, data in plot_elements["ohlcv"].items():
@@ -482,6 +491,7 @@ def load_and_plot_trades(config: Dict[str, Any]):
         logger.info("analyse pair %s", pair)
 
         df_analyzed = strategy.analyze_ticker(data, {'pair': pair})
+        df_analyzed = trim_dataframe(df_analyzed, timerange)
         trades_pair = trades.loc[trades['pair'] == pair]
         trades_pair = extract_trades_of_period(df_analyzed, trades_pair)
 
diff --git a/freqtrade/templates/sample_strategy.py b/freqtrade/templates/sample_strategy.py
--- a/freqtrade/templates/sample_strategy.py
+++ b/freqtrade/templates/sample_strategy.py
@@ -64,7 +64,7 @@ class SampleStrategy(IStrategy):
     ignore_roi_if_buy_signal = False
 
     # Number of candles the strategy requires before producing valid signals
-    startup_candle_count: int = 20
+    startup_candle_count: int = 30
 
     # Optional order type mapping.
     order_types = {


Test patch:
diff --git a/tests/test_plotting.py b/tests/test_plotting.py
--- a/tests/test_plotting.py
+++ b/tests/test_plotting.py
@@ -51,9 +51,10 @@ def test_init_plotscript(default_conf, mocker, testdatadir):
     assert "ohlcv" in ret
     assert "trades" in ret
     assert "pairs" in ret
+    assert 'timerange' in ret
 
     default_conf['pairs'] = ["TRX/BTC", "ADA/BTC"]
-    ret = init_plotscript(default_conf)
+    ret = init_plotscript(default_conf, 20)
     assert "ohlcv" in ret
     assert "TRX/BTC" in ret["ohlcv"]
     assert "ADA/BTC" in ret["ohlcv"]