Automateyourself Well
How to determine if an indicator is suitable for Expert Advisors
Most Expert Advisors use one or more indicators. MetaTrader platform delivers a set of build‑in indicators and additionally a possibility to write own indicators (Custom Indicators). Unfortunately, a lot of indicators that can be found in the Internet can't be used by Expert Advisors, because for example they are badly implemented or they only draw objects and can't be used in a standard way. The other common reason for disqualifying an indicator is the usage of data taken from the future in calculations (not only current and past candles). For example to calculate the value of 5‑period simple moving average for a bar at 2008.01.01 12:00 on a H1 chart, we need bars from 8:00, 9:00, 10:00, 11:00 and 12:00. Now, imagine that certain indicator uses bars from 8:00 to 14:00 to display value at 2008.01.01 12:00. So it uses 2 bars taken from the future. How is it possible? It is possible only on the basis of historical data. In live trading these 2 future bars of course won't be available. That's why this indicator will look different on a historical data and after some time in live trading. Sometimes such indicators change values not only for the most recent bar. We call such behaviour "redrawing". Generally, such indicators aren't suitable for an Expert Advisors. In rare cases, some of indicators that redraw can be used, but it must be fully conscious. A good example of an indicator, that redraws but can be used (but very consciously) in Expert Advisors, is ZigZag, which is provided with MetaTrader terminal.
Below there are 3 steps which will help you test indicators and check if they are suitable for an Expert Advisors.
Step 1 – Data Window
The Data Window can be turned on in MetaTrader terminal by pressing Ctrl + D
or via main menu View -> Data Window
. In the Data Window quotations and values of all used buffers (in this example all buffers are visualized as lines) of all attached indicators are displayed: Moving Average, Stochastic Oscillator and ADX. Buffer is a series of values that can be visualized in a few different ways: lines, dots, histogram. Moving Average has one buffer, Stochastic Oscillator two (main line and signal line) and ADX has three buffers (ADX, +DI and -DI).
Also Custom Indicators should be displayed in the Data Window. Make sure that option Show in the Data Window
is selected in Visualization Tab in indicators' properties.
If, in spite of above setting, an indicator is not displayed in the Data Window, it is generally not suitable for Expert Advisors. Of course, to every rule there is an exception. If your indicator only draws objects (for example Pivot levels for only current day), it won't be displayed in the Data Window. An Expert Advisor can retrieve values from objects but backtesting of such Expert Advisor will be impossible or very limited. In fact, it will be possible only in visual mode after manual attaching this indicator to a chart. Also Optimization won't be possible. That's why such indicators aren't recommended for Expert Advisors. However, they can be successfully used in a manual trading.
Step 2 – Historical view vs. Current view
If analysed indicator passes Step 1, it is time for the next test. Every properly written indicator must look identical after some time of running and just after attaching to a chart. See example:
If we attached this indicator at 2008.08.13, it would look like in the lower chart. But if we attached this indicator at 2008.08.08 and let it run for 3 days, it would look like in the upper chart. The difference is obvious. In live trading this indicator gives only a few signals in spite of many perfect signals on a historical quotations. For a properly implemented indicator, both charts must be identical.
How to conduct such test? Very simple:
- Open any chart (lower timeframes are better, because you will see any problems with indicator sooner)
- Place vertical line on the latest bar to remember where the beginning of a test is
- Attach analysed indicator to a chart
- Wait a few hours (this time depends on selected timeframe; the more candles, the better)
- Once again attach analysed indicator
- Compare how they look; you can use the Data Window to compare all values precisely
For indicators, that are displayed on a main chart just after conducting the whole test, open a new chart and compare both views by switching from one chart to the other.
You don't have to wait a few hours or days for the result. To speed up examination you can use Visual mode in backtester – for example any Expert Advisor, although the best will be one that does absolutely nothing. Download VisualTester that only places vertical line at the beginning (VisualTester.ex4).
Make sure that Visual mode option is selected. Move scrollbar to the left to have time to attach the indicator. You don't have to use Every tick model; for most cases Control points will be sufficient. Click on the Start button and follow points 3 to 6. In point 4 just move scrollbar to the right and after some time move it back to the left.
Step 3 – IndicatorTester
This is the final step. It enables us to eliminate most of wrong indicators that have positively passed two previous steps. IndicatorTester should be used wisely. It simulates the usage of an indicator in an Expert Advisor. With every tick, it compares all indicator's buffers to their previous values and checks how many indicator's values (expressed in bars) have been changed. For most proper indicators the result (called Redraw Factor) will be equal to 1 or 0. Redraw Factor is the maximum amount of changed bars for whole test and all buffers. Redraw Factor = 1 means that the indicator changes only its most recent value. Values of completed bars are not changed. Redraw Factor = 0 means that the indicator never changes its values. A good example of such indicator is the moving average based on open prices (not close prices). Because open price remains unchanged, the result of calculations is also the same.
You can find indicators that are implemented correctly, but their Redraw Factor is equal to 2. These indicators are visualized as one line, but in two colours. For example, one colour corresponds to the rising part of the line and the other colour to the descending part. This is because each colour of this indicator requires separate buffer – this is the only way to implement such visualization in MetaTrader. However, to avoid any ambiguities, such indicators should be visualized as dots in two colours, not lines. Additionally, lines can overlap on each other, which can lead to seemingly strange behaviour of an Expert Advisor. Overlapping of lines can be detected via the Data Window – more than one buffer will be active at the same time. Also indicators, that are based on other indicators from higher timeframes, can have Redraw Factor higher than 1. For example, if an indicator on H1 chart uses other indicator from H4 timeframe, it can have Redraw Factor equal to 4 instead of 1 (there are 4 H1 candles for one H4 candle).
The usage of IndicatorTester needs some explanation. File IndicatorTester.ex4 should be placed as other Expert Advisors in experts subdirectory. As it is a kind of Expert Advisor, which doesn't trade but checks indicators, it also has its own parameters.
IndicatorName
is the name of analysed indicator. It is simply the name of indicator's file without extension. Examples: ZigZag, Heiken Ashi. Remember to write indicator's name very carefully. A typo totally prevents from working. You can also test build‑in indicators. The full list of build‑in indicators can be found here docs.mql4.com/indicators. These indicators' names start from lower‑case letteri
. From obvious reasons indicatorsiXxxxxOnArray
can't be analysed.IndicatorParameters
is also very important. Each Custom Indicator or build‑in indicator can have a different number of its own parameters. That's why these parameters are passed in a little bit different way. Simply, parameters are separated by|
(pipe sign, broken vertical bar). The order of parameters is very important. Any value, which is not a string, can be passed via this mechanism.True
values should be replaced by1
andFalse
by0
. Examples: Parabolic SAR indicator has two parameters Step and Maximum, so IdicatorParameters field should look like this:0.02|0.2
. For build‑in 14‑period simple moving average from close prices this parameters should look like this:14|0|0|0
. The first 0 means shift equal to zero, the second 0 means that it is a simple moving average, not exponential or something alike, the third 0 means that it is calculated from close prices. The order of parameters and values of methods and prices are taken from the documentation docs.mql4.com/indicators/iMA. If you want IndicatorTester to use default parameters of the indicator, just leave this field empty. Also if the indicator has any string parameter, then you will have to use default parameters, so leave this field empty.RangeBars
parameter defines how many latest bars from each buffer are stored in IndicatorTester memory. Bigger value means more comparisons and slower performance, but if potential change occurred beyond RangeBars, it wouldn't be detected.
IndicatorTester should be run for at least 100 bars. The more, the better. It can be used both in live trading and backtester (Visual mode).
Examples:
Note: Analysed indicator is drawn on a chart automatically after the end of a backtest (after clicking on the Stop button). If you want to observe the indicator during test, you have to attach it to the chart by yourself.
- 14‑period Simple Moving Average from open prices
IndicatorName:iMA
IndicatorParameters:14|0|0|1
RangeBars:100
According to the theory, moving average from open prices never changes even current value. So Redraw Factor is equal to 0. The name of indicator is yellow, which means that IndicatorTester tests build‑in indicator. - Parabolic SAR with parameters 0.02 and 0.2
IndicatorName:iSAR
IndicatorParameters:0.02|02
RangeBars:100
Just after starting the test Redraw Factor is equal to 0, which means that once placed dot doesn't change its position. But when price crosses the most recent dot, Parabolic SAR moves the dot to the opposite side and Redraw Factor changes its value to 1. - ZigZag indicator with default parameters: 12, 5, 3
IndicatorName:ZigZag
IndicatorParameters:12|5|3
RangeBars:100
The high value of Redraw Factor doesn't surprise. ZigZag indicator can change its swings even after a huge amount of bars. It can be used in Expert Advisors but very consciously, because the most recent or even the previous swing can disappear or ZigZag can place a new swing between them. With every tick current configurations of swings should be read. - Build‑in Fractals indicator
IndicatorName:iFractals
IndicatorParameters: (empty field)
RangeBars:100
This indicator doesn't have any parameters. Despite the fact that it is a build‑in indicator, its Redraw Factor is equal to 3. Moreover, values of all buffers for the most recent bar (B#0, B#1, ... B#7) are always equal to 0. What does it mean? It means that all arrows are placed not when the most recent candle is being formed, but after 2 or 3 candles. So in fact, these perfect signals aren't so perfect, because they are drawn with 2 or 3‑bar delay. It doesn't mean that this indicator is useless. Simply, it can't be used in the most intuitive way – as buy and sell signals. For example, it can be used as the detector of swings that will be used for further calculations.
Summary
- Step 1 – Indicator's buffers must be visible in the Data Window
- Step 2 – Historical and current views must be identical (VisualTester.ex4)
- Step 3 – Redraw Factor should be equal to 0 or 1 (IndicatorTester.ex4)