Skip to content

Commit fca454b

Browse files
authored
Merge pull request freqtrade#11708 from hippocritical/develop
lookahead-analysis docs changed to better explain the output table
2 parents 8fb4446 + 3dc1adf commit fca454b

File tree

1 file changed

+71
-38
lines changed

1 file changed

+71
-38
lines changed

docs/lookahead-analysis.md

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,105 @@
11
# Lookahead analysis
22

3-
This page explains how to validate your strategy in terms of look ahead bias.
3+
This page explains how to validate your strategy in terms of lookahead bias.
44

5-
Checking look ahead bias is the bane of any strategy since it is sometimes very easy to introduce backtest bias -
6-
but very hard to detect.
5+
Lookahead bias is the bane of any strategy since it is sometimes very easy to introduce this bias, but can be very hard to detect.
76

8-
Backtesting initializes all timestamps at once and calculates all indicators in the beginning.
9-
This means that if your indicators or entry/exit signals could look into future candles and falsify your backtest.
7+
Backtesting initializes all timestamps (loads the whole dataframe into memory) and calculates all indicators at once.
8+
This means that if your indicators or entry/exit signals look into future candles, this will falsify your backtest.
109

11-
Lookahead-analysis requires historic data to be available.
10+
The `lookahead-analysis` command requires historic data to be available.
1211
To learn how to get data for the pairs and exchange you're interested in,
1312
head over to the [Data Downloading](data-download.md) section of the documentation.
13+
`lookahead-analysis` also supports freqai strategies.
1414

15-
This command is built upon backtesting since it internally chains backtests and pokes at the strategy to provoke it to show look ahead bias.
16-
This is done by not looking at the strategy itself - but at the results it returned.
17-
The results are things like changed indicator-values and moved entries/exits compared to the full backtest.
15+
This command internally chains backtests and pokes at the strategy to provoke it to show lookahead bias.
16+
This is done by not looking at the strategy code itself, but at changed indicator values and moved entries/exits compared to the full backtest.
1817

19-
You can use commands of [Backtesting](backtesting.md).
20-
It also supports the lookahead-analysis of freqai strategies.
18+
`lookahead-analysis` can use the typical options of [Backtesting](backtesting.md), but forces the following options:
2119

2220
- `--cache` is forced to "none".
2321
- `--max-open-trades` is forced to be at least equal to the number of pairs.
2422
- `--dry-run-wallet` is forced to be basically infinite (1 billion).
2523
- `--stake-amount` is forced to be a static 10000 (10k).
2624
- `--enable-protections` is forced to be off.
2725

28-
Those are set to avoid users accidentally generating false positives.
26+
These are set to avoid users accidentally generating false positives.
2927

3028
## Lookahead-analysis command reference
3129

3230
--8<-- "commands/lookahead-analysis.md"
3331

34-
!!! Note ""
35-
The above Output was reduced to options `lookahead-analysis` adds on top of regular backtesting commands.
36-
37-
### Summary
38-
39-
Checks a given strategy for look ahead bias via lookahead-analysis
40-
Look ahead bias means that the backtest uses data from future candles thereby not making it viable beyond backtesting
41-
and producing false hopes for the one backtesting.
32+
!!! Note
33+
The above output was reduced to options that `lookahead-analysis` adds on top of regular backtesting commands.
4234

4335
### Introduction
4436

45-
Many strategies - without the programmer knowing - have fallen prey to look ahead bias.
37+
Many strategies, without the programmer knowing, have fallen prey to lookahead bias.
38+
This typically makes the strategy backtest look profitable, sometimes to extremes, but this is not realistic as the strategy is "cheating" by looking at data it would not have in dry or live modes.
4639

47-
Any backtest will populate the full dataframe including all time stamps at the beginning.
48-
If the programmer is not careful or oblivious how things work internally
49-
(which sometimes can be really hard to find out) then it will just look into the future making the strategy amazing
50-
but not realistic.
40+
The reason why strategies can "cheat" is because the freqtrade backtesting process populates the full dataframe including all candle timestamps at the outset.
41+
If the programmer is not careful or oblivious how things work internally
42+
(which sometimes can be really hard to find out) then the strategy will look into the future.
5143

52-
This command is made to try to verify the validity in the form of the aforementioned look ahead bias.
44+
This command is made to try to verify the validity in the form of the aforementioned lookahead bias.
5345

5446
### How does the command work?
5547

5648
It will start with a backtest of all pairs to generate a baseline for indicators and entries/exits.
57-
After the backtest ran, it will look if the `minimum-trade-amount` is met
58-
and if not cancel the lookahead-analysis for this strategy.
59-
60-
After setting the baseline it will then do additional runs for every entry and exit separately.
61-
When a verification-backtest is done, it will compare the indicators as the signal (either entry or exit) and report the bias.
62-
After all signals have been verified or falsified a result-table will be generated for the user to see.
49+
After this initial backtest runs, it will look if the `minimum-trade-amount` is met and if not cancel the lookahead-analysis for this strategy.
50+
If this happens, use a wider timerange to get more trades for the analysis, or use a timerange where more trades occur.
51+
52+
After setting the baseline it will then do additional backtest runs for every entry and exit separately.
53+
When these verification backtests complete, it will compare the indicators at the signal candles (both entry or exit)
54+
and report the bias.
55+
After all signals have been verified or falsified a result table will be generated for the user to see.
56+
57+
### How to find and remove bias? How can I salvage a biased strategy?
58+
59+
If you found a biased strategy online and want to have the same results, just without bias,
60+
then you will be out of luck most of the time.
61+
Usually the bias in the strategy is THE driving factor for "too good to be true" profits.
62+
Removing conditions or indicators that push the profits up from bias will usually make the strategy significantly worse.
63+
You might be able to salvage it partially if the biased indicators or conditions are not the core of the strategy, or there
64+
are other entry and exit signals that are not biased.
65+
66+
### Examples of lookahead-bias
67+
68+
- `shift(-10)` looks 10 candles into the future.
69+
- Using `iloc[]` in populate_* functions to access a specific row in the dataframe.
70+
- For-loops are prone to introduce lookahead bias if you don't tightly control which numbers are looped through.
71+
- Aggregation functions like `.mean()`, `.min()` and `.max()`, without a rolling window,
72+
will calculate the value over the **whole** dataframe, so the signal candle will "see" a value including future candles.
73+
A non-biased example would be to look back candles using `rolling()` instead:
74+
e.g. `dataframe['volume_mean_12'] = dataframe['volume'].rolling(12).mean()`
75+
- `ta.MACD(dataframe, 12, 26, 1)` will introduce bias with a signalperiod of 1.
76+
77+
### What do the columns in the results table mean?
78+
79+
- `filename`: name of the checked strategy file
80+
- `strategy`: checked strategy class name
81+
- `has_bias`: result of the lookahead-analysis. `No` would be good, `Yes` would be bad.
82+
- `total_signals`: number of checked signals (default is 20)
83+
- `biased_entry_signals`: found bias in that many entries
84+
- `biased_exit_signals`: found bias in that many exits
85+
- `biased_indicators`: shows you the indicators themselves that are defined in populate_indicators
86+
87+
You might get false positives in the `biased_exit_signals` if you have biased entry signals paired with those exits.
88+
However, a biased entry will usually result in a biased exit too,
89+
even if the exit itself does not produce the bias -
90+
especially if your entry and exit conditions use the same biased indicator.
91+
92+
**Address the bias in the entries first, then address the exits.**
6393

6494
### Caveats
6595

6696
- `lookahead-analysis` can only verify / falsify the trades it calculated and verified.
67-
If the strategy has many different signals / signal types, it's up to you to select appropriate parameters to ensure that all signals have triggered at least once. Not triggered signals will not have been verified.
68-
This could lead to a false-negative (the strategy will then be reported as non-biased).
69-
- `lookahead-analysis` has access to everything that backtesting has too.
70-
Please don't provoke any configs like enabling position stacking.
71-
If you decide to do so, then make doubly sure that you won't ever run out of `max_open_trades` amount and neither leftover money in your wallet.
72-
- In the results table, the `biased_indicators` column will falsely flag FreqAI target indicators defined in `set_freqai_targets()` as biased. These are not biased and can safely be ignored.
97+
If the strategy has many different signals / signal types, it's up to you to select appropriate parameters to ensure that all signals have triggered at least once. Signals that are not triggered will not have been verified.
98+
This would lead to a false-negative, i.e. the strategy will be reported as non-biased.
99+
- `lookahead-analysis` has access to the same backtesting options and this can introduce problems.
100+
Please don't use any options like enabling position stacking as this will distort the number of checked signals.
101+
If you decide to do so, then make doubly sure that you won't ever run out of `max_open_trades` slots,
102+
and that you have enough capital in the backtest wallet configuration.
103+
- In the results table, the `biased_indicators` column
104+
will falsely flag FreqAI target indicators defined in `set_freqai_targets()` as biased.
105+
**These are not biased and can safely be ignored.**

0 commit comments

Comments
 (0)