iVAR
Download scriptThis script is a sophisticated financial analysis tool designed to help traders understand the underlying nature of price movements. Rather than simply looking at whether the price is going up or down, it analyzes the "texture" of the market to determine if the current trend is stable, volatile, or behaving randomly.
Core Concept: Fractal Analysis
The logic is based on a mathematical approach known as fractal analysis. It looks at the price data across different scales simultaneously. By examining how price changes over various timeframes (specifically using powers of two), the tool can identify patterns that are often invisible to the naked eye.
How it Works
The indicator processes the market data through several layers of calculation:
- Multi-Scale Observation: It breaks down the price history into different intervals. It compares very short-term price movements against longer-term movements.
- Variation Tracking: For each of these intervals, the script calculates the difference between the highest and lowest prices. This provides a raw measurement of "volatility" or "spread" at every specific scale.
- Logarithmic Mapping: To ensure that small movements are weighted fairly against large ones, the data is converted into a logarithmic scale. This allows for a more accurate comparison of price dynamics across different time horizons.
- Trend Identification (Regression): The script applies a mathematical formula (Least Squares Regression) to these various scales. It looks for a consistent relationship between the timeframe size and the price variation.
What the Results Mean
The final output is a numerical value that represents the "Variation Index." This index serves as a diagnostic tool for the market's current behavior:
- Trend Components: If the index indicates a dominant trend, it means the price movement is organized and following a specific direction.
- Flat/Rangebound Components: It can identify periods where the market is moving but lacks a clear direction, staying within a specific horizontal range.
- Random Walk: Most importantly, it can detect "random" behavior. In a perfectly random market (like white noise), the variation index will reflect that lack of organization, warning the trader that traditional trend-following strategies may not be effective at that moment.
Practical Use
By monitoring this index, a user can distinguish between a "healthy" trending market and a "noisy" market. It provides a deeper level of market context, helping to identify whether the current price action is governed by underlying structural trends or if it is merely fluctuating randomly.
//+------------------------------------------------------------------+
//| iVAR.mq4 |
//| (C)opyright © 2008, Ilnur |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
// Èíäèêàòîð îòîáðàæàåò èíäåêñ âàðèàöèè öåíîâîãî ðÿäà, âû÷èñëåííîãî
// íà ìèíèìàëüíîì ïðåäøåñòâóþùåì èíòåðâàëå äëèíû 2^n. Èíäåêñ âàðèàöèè
// ïîêàçûâàåò, ÷òî ïðåîáëàäàåò âî âðåìåííîì ðÿäó òðåíäîâàÿ èëè ôëåòîâàÿ
// ñîñòàâëÿþùàÿ, èëè æå ðÿä âåäåò ñåáÿ ñëó÷àéíî.
// Ì.Ì. Äóáîâèêîâ è äð. - Ðàçìåðíîñòü ìèíèìàëüíîãî ïîêðûòèÿ è ëîêàëüíûé
// àíàëèç ôðàêòàëüíûõ âðåìåííûõ ðÿäîâ.
#property copyright "(C)opyright © 2008, Ilnur"
#property link "http://www.metaquotes.net"
//---- íàñòðîéêè èíäèêàòîðà
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_level1 0.5
//---- âõîäíûå ïàðàìåòðû
extern int n = 5;
//---- áóôåð èíäèêàòîðà
double ibuffer[];
//+------------------------------------------------------------------+
//| Ôóíêöèÿ èíèöèàëèçàöèè èíäèêàòîðà |
//+------------------------------------------------------------------+
int init()
{
//---- íàñòðîéêà ïàðàìåòðîâ îòðèñîâêè
SetIndexBuffer(0,ibuffer);
SetIndexStyle(0,DRAW_LINE);
SetIndexDrawBegin(0,MathPow(2,n));
SetIndexLabel(0,"iVAR");
//---- "êîðîòêîå èìÿ" îòîáðàæàåìîå â îêíå èíäèêàòîðà
IndicatorShortName("iVAR("+n+")");
return(0);
}
//+------------------------------------------------------------------+
//| Îñíîâíàÿ ôóíêöèÿ èíäèêàòîðà |
//+------------------------------------------------------------------+
int start()
{
int i, j, k, nTotal, nCountedBars = IndicatorCounted();
int ihigh, ilow, nInterval, nWindow = MathPow(2,n);
double Delta, Xñ, Yñ, Sx, Sy, Sxx, Sxy;
//---- ïîñëåäíèé ïîñ÷èòàííûé áàð áóäåò ïåðåñ÷èòàí
nTotal = Bars-nWindow;
if(nCountedBars>nWindow) nTotal = Bars-nCountedBars-1;
//---- îñíîâíîé öèêë èíäèêàòîðà
for(j=nTotal; j>=0; j--)
{
Sx = 0; Sy = 0; Sxx = 0; Sxy = 0;
for(i=0; i<=n; i++)
{
nInterval = MathPow(2,n-i);
//---- ñóììèðóåì ðàçíèöû ìàêñèìàëüíîé è ìèíèìàëüíîé öåí íà èíòåðâàëå
for(Delta=0, k=0; k<MathPow(2,i); k++)
{
ihigh = iHighest(Symbol(),0,MODE_HIGH,nInterval,nInterval*k+j);
ilow = iLowest(Symbol(),0,MODE_LOW,nInterval,nInterval*k+j);
Delta += High[ihigh]-Low[ilow];
}
//---- âû÷èñëÿåì êîîðäèíàòû âàðèàöèè â äâîéíîì ëîãàðèôìè÷åñêîì ìàñøòàáå
Xñ = (n-i)*MathLog(2.0);
Yñ = MathLog(Delta);
//---- íàêàïëèâàåì äàííûå äëÿ íàõîæäåíèÿ êîýôôèöèåíòîâ ëèíèè ðåãðåññèè ñ ïîìîùüþ ÌÍÊ
Sx += Xñ;
Sy += Yñ;
Sxx += Xñ*Xñ;
Sxy += Xñ*Yñ;
}
//---- âû÷èñëÿåì èíäåêñ âàðèàöèè (êîýôôèöèåíò íàêëîíà ëèíèè ðåãðåññèè)
ibuffer[j] = -(Sx*Sy-(n+1)*Sxy)/(Sx*Sx-(n+1)*Sxx);
}
return(0);
}
Comments