## Thursday, April 15, 2010

### Self-similarity and a measure of it

Following an exchange via emails with a fellow trader, John Last, in which he made some remarks about the interest of some kind of self-similarity, I came to conceive a new indicator, which can be used to detect some convergence of behaviour between different timescales, in the sense outlined below.
____________________________________________________________________________________

I-General Remarks

Self-similarity is a well-known feature of fractals, it may however be useful to precise a few things about this concept.
The classic examples of fractals such as the Cantor Dust, the Von Koch Curve or the Sierpinski Gasket display an obvious self-similarity that is the direct consequence of their recursive mode of construction. However, when one comes to consider real-life applications of fractals, one should not expect to find such a perfect self-similarity.

Furthermore, when it comes to random fractals and their applications, which is the case of financial markets, self-similarity should not be taken as meaning a repetition of the exact same pattern, not even as a repetition of a pattern close enough to the original to warrant the use of "repetition".
Rather, what is meant in the case of financial price variations by self-similarity is really a "statistical self-similarity", which is more of a similarity of behavior between different timescales.

What should be compared therefore in order to measure the level of self-similarity at a given time, is not how the price curves at different timescales are "similar" to each others, but rather whether their behaviour, and particularly their volatility displays a level of self-similarity across timescales.

II-Dispersion of the Fractal Dimension across various timeframes

To measure this statistical similarity, I will only consider the dispersion between the FGDI of various timeframes around the FGDI of the longest timeframe considered.
For instance, considering the TimeFrame of 1hr, whose FGDI is fgdi(60), and the other shorter timeframes of 5mn (fgdi(5)), 15mn(fgdi(15)) and 30mn(fgdi(30)). The dispersion will be given by:

$D=\sqrt{\frac{(fgdi(5)-fgdi(60))^{2}+(fgdi(15)-fgdi(60))^{2}+(fgdi(30)-fgdi(60))^{2}}{3}}\;\;\; \; \; (1)$

Which is basically the formula of the standard deviation around the value of the longest timeframe.
This calculation seems rather straightforward, except that we must take care of a little technical problem: if, at the present instant, the value of the FGDI is actual for all the timeframes, the value of FGDI 30 bars ago in the 5mn timeframe is not corresponding to the value of the FGDI 30 bars ago in the 15mn timeframe. Indeed, the 30th bar back from now on the 5mn TF corresponds to 150mn ago, which on the 15mn timeframe corresponds to the 10th bar in the past.
Clearly therefore, to re-establish a correspondance that makes sense in equation (1) above, one must apply a change of index, whose general equation between a given timeframe (TF) and the reference timeframe (TFref) has the following form:

$newpos=pos*\frac{TF_{ref}}{TF}$

'newpos' is the new value of the index to be considered in the shorter timeframe in relation to the index 'pos' from the reference timeframe. Notice that in MQL4, the further a bar is in the past the higher is its index, in order to have the past index available, the main loop should be a decreasing one in terms of index, starting from as far as necessary in the past and calculating the bars towards the present.

That being said, I am aware, that the simplicity of this transformation does not ensure a perfect match between the different timeframes and an error of a few bars is still possible in the past, but the complexity of implementing a full check in order to ensure a perfect match is not warranted given that it will modify the final calculation in a negligible manner.

III-Implementation in MT4

I then wrote this indicator as MTF_FractalDispersion11.mq4, the script is available here, and here is what it looks like on a EUR/USD chart, in orange, in the bottom window:

For clarity purposes, the value of the dispersion is multiplied by 10, a low value is indicative of a high self-similarity, between the different timeframes.
For instance, in case of a trendy market (FGDI below 1.5), a low dispersion (corresponding to a high self-similarity) is a positive indicator to enter a trade in the sense of the trend, provided the said trend is in the same direction in all the timeframes considered.
The available timeframes are 5mn, 15mn, 30mn, 1hr, 4hr and 1 day. Each timeframe can be weighted as desired (but by an integer value).

Important remark:
From an excellent remark by John Last, I came to realise that the graphical representation of the Fractal dispersion is only aligned temporally to the prices graph (and therefore also to the Fractal Dimension graph) for the reference TimeFrame (the longest TF selected, with a weight above 0). On all the shorter TF, this representation will appear as contracted towards the right (proportionally to the distance we are looking at in the past from the present, rather than the Fractal Dispersion being contracted, it is the price, and therefore also the Fractal Dimension, that are dilated, taking more values than in the longer TF, within the same time interval), and the movements of the FractalDispersion will therefore appear to have taken place at a time later than at which they really did happen (the correct time will be the one displayed on the reference TF, i.e. the longest one selected).
The only time all the graphs will coincide on all TF is the present. Any analysis of the past should therefore take this into account.
This effect of contraction/dilatation is particularly well-illustrated on the following graph, sent to me, by John:

Here, by comparing the two lower windows (ignore the difference in numerical values, they are due to another mistake I did in the first version and that I also corrected), we see that the yellow curve MTF_FracDisp11 is nothing else than the contraction towards the left (with the present as the fixed point) of the green curve MTF_FracDisp.
Incidentally on this graph, the correct representation, except for a multiplicative factor of √2, is given by MTF_FracDisp, this is however exceptional and solely due to a specific setup, the 15mn TF is actually not the reference TF.
In all cases, MTF_FracDisp11 gives the proper value of the fractal dispersion and does coincide with the timescale only for the graph on the reference TF. And this indicator should only be used as such.

Notice that this indicator needs to access FGDI.mq4 on your PC, and that this one should therefore be present and compiled properly.

The parameters of MTF_FractalDispersion11.mq4 are:
e_period (integer): This is the period considered for calculating the fractal dimension, default is 30.
e_type_data (0,1,2,3,4,5 or 6): This is the type of price the indicator will consider (0=CLOSE, 1=OPEN, 2=HIGH, 3=LOW, 4=MEDIAN, 5=TYPICAL, 6=WEIGHTED), default is 0.
M5w (integer): This is the weight to be applied to the 5mn timeframe, default is 1.
M15w (integer): This is the weight to be applied to the 15mn timeframe, default is 1.
M30w (integer): This is the weight to be applied to the 30mn timeframe, default is 1.
M60w (integer): This is the weight to be applied to the 1hr timeframe, default is 1.
M240w (integer): This is the weight to be applied to the 4hr timeframe, default is 0.
M1440w (integer): This is the weight to be applied to the 1day timeframe, default is 0.