iWavelet
Download scriptThis MetaTrader script is a sophisticated visual analysis tool designed to decompose price action into different scales of volatility and movement. Instead of relying on traditional moving averages or simple oscillators, it utilizes a mathematical concept known as "Wavelet Analysis" to break down the market data.
Core Logic and Methodology
The primary goal of this script is to separate "noise" from "trend." It achieves this by processing price data through multiple layers of recursive calculations. Think of it like looking at a complex image through different filters: one filter might show you the broad shapes, while another reveals the tiny textures.
1. Multi-Scale Decomposition The script looks at the market through various "levels" (defined by the user input N). Each level represents a different frequency of price movement. Lower levels capture rapid, short-term fluctuations (micro-movements), while higher levels capture slower, broader shifts in price (macro-trends).
2. Recursive Averaging To create these levels, the script uses a recursive mathematical approach. It takes a base price point (such as the closing price) and compares it against data points that are progressively further apart in time. By doing this, it can isolate specific types of price behavior at each distinct scale without losing the underlying integrity of the original data.
3. Dynamic Visual Representation Unlike most indicators that simply draw a line on your chart, this script creates a visual field of rectangles.
- Spatial Mapping: It populates a designated area on your screen with these shapes, where each shape represents a specific calculation at a specific point in history.
- The Color Gradient: The most unique feature is the dynamic coloring system. The script calculates the "intensity" or magnitude of the price movements at every level. It then maps these values to a color gradient.
- One end of the spectrum represents one extreme (e.g., "High" pressure).
- The other end represents the opposite extreme (e.g., "Low" pressure).
- The colors transition smoothly between these two extremes based on the actual strength of the market movement at that moment.
4. Real-Time Adaptation The script is designed to be "live." As new price data arrives, it automatically shifts the existing visual objects forward in time and recalculates the color intensities. This ensures that the visual representation remains aligned with current market conditions without needing to redraw the entire history every time a new tick occurs.
Summary for the User
In simple terms, this tool takes messy market data and "unpacks" it into different layers of importance. It then paints those layers onto your screen using a heat-map style color system. By observing where colors shift or cluster, you are essentially seeing a visual map of where significant energy is moving in the market across various timeframes simultaneously.
//+------------------------------------------------------------------+
//| iWavelet.mq4 |
//| Erics |
//| erics.fx@tut.by |
//+------------------------------------------------------------------+
#property copyright "Erics"
#property link "erics.fx@tut.by"
#property indicator_separate_window
#define MIN 0.
#define MAX 1.
#property indicator_minimum MIN
#property indicator_maximum MAX
extern int N=8;
extern int Len=1000;
extern bool Symmetric=true;
extern bool VRescale=true;
extern int OHLC=3;
extern color HighColor=Red;
extern color LowColor =DarkBlue;
double c1[];
double c2[];
double c3[];
double c4[];
double c5[];
double c6[];
double c7[];
double c8[];
double w[][9];
int colr[256];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
if(N>8) N=8; else if(N<1) N=1;
ArrayResize(w, Len);
CreateColorTable();
IndicatorBuffers(N);
//IndicatorShortName("iWavelet ("+N+")");
//IndicatorDigits(0);
SetIndexBuffer(0, c1); SetIndexStyle(0, DRAW_NONE); if(N==1) return(0);
SetIndexBuffer(1, c2); SetIndexStyle(1, DRAW_NONE); if(N==2) return(0);
SetIndexBuffer(2, c3); SetIndexStyle(2, DRAW_NONE); if(N==3) return(0);
SetIndexBuffer(3, c4); SetIndexStyle(3, DRAW_NONE); if(N==4) return(0);
SetIndexBuffer(4, c5); SetIndexStyle(4, DRAW_NONE); if(N==5) return(0);
SetIndexBuffer(5, c6); SetIndexStyle(5, DRAW_NONE); if(N==6) return(0);
SetIndexBuffer(6, c7); SetIndexStyle(6, DRAW_NONE); if(N==7) return(0);
SetIndexBuffer(7, c8); SetIndexStyle(7, DRAW_NONE);
}
int CreateObjects()
{
string nm;
int shift=0;
double pr1, pr2;
for(int iw=1; iw<=N; iw++)
{
if(Symmetric) shift = 1<<(iw-1)-1;
for(int bar=0; bar<Len; bar++)
{
nm = "R"+iw+"."+bar;
if(VRescale) {
pr1 = MAX - (MAX-MIN)*MathSqrt(1.* (N-iw+1)/N);
pr2 = MAX - (MAX-MIN)*MathSqrt(1.* (N-iw) /N);
//pr2 = MIN + (MAX-MIN)*MathSqrt(1.*(1<<iw-1)/(1<<N-1));
} else {
pr1 = MIN + (MAX-MIN)*(iw-1)/N;
pr2 = MIN + (MAX-MIN)*iw/N;
}
ObjectCreate(nm, OBJ_RECTANGLE, 1, Time[bar+shift], pr1, Time[bar+shift+1], pr2);
ObjectSet(nm, OBJPROP_BACK, true);
ObjectSet(nm, OBJPROP_COLOR, HighColor);
}
}
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
DeleteObjects();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
static int ltime=0;
int i, counted_bars = IndicatorCounted();
if(Bars<=N) return(0);
i = Bars-N-1;
if(counted_bars>=N) i=Bars-counted_bars-1;
while(i>=0)
{
getC(N, i);
i--;
}
if(Time[0]>ltime) {
// íîâûé áàð
if(ltime==0) CreateObjects(); // ïåðâûé áàð
else ShiftObjects();
RedrawObjects();
ltime = Time[0];
}
}
//+------------------------------------------------------------------+
int RedrawObjects()
{
string nm;
double mx=-9999999;
double mn= 9999999;
for(int bar=0; bar<Len; bar++)
for(int iw=1; iw<=N; iw++)
{
w[bar][iw] = getC(iw-1, bar) - getC(iw, bar); // âåéâëåò-êîýôôèöèåíòû
w[bar][iw] /= MathSqrt(1<<(iw-1));
//Print("w[",bar,"][",iw,"]= ",w[bar][iw]);
if(mx<w[bar][iw]) mx = w[bar][iw];
if(mn>w[bar][iw]) mn = w[bar][iw];
}
if(mx<=0.) mx = 0.0001;
if(mn>=0.) mn = -0.0001;
if(mx<-mn) mx=-mn;
int ic;
for(bar=0; bar<Len; bar++)
for(iw=1; iw<=N; iw++)
{
nm = "R"+iw+"."+bar;
ic = 127 + w[bar][iw]/mx*127;
ObjectSet(nm, OBJPROP_COLOR, colr[ic]);
}
}
double getC(int j, int l)
{
int n;
if(j>0) n = 1<<(j-1); else {
switch(OHLC) {
case 3: return(Close[l]);
case 2: return(Low[l]);
case 1: return(High[l]);
case 0: return(Open[l]);
}
}
switch(j)
{
case 1: if(c1[l]==EMPTY_VALUE) c1[l] = 0.5*(getC(j-1, l+n) + getC(j-1, l)); return(c1[l]);
case 2: if(c2[l]==EMPTY_VALUE) c2[l] = 0.5*(getC(j-1, l+n) + getC(j-1, l)); return(c2[l]);
case 3: if(c3[l]==EMPTY_VALUE) c3[l] = 0.5*(getC(j-1, l+n) + getC(j-1, l)); return(c3[l]);
case 4: if(c4[l]==EMPTY_VALUE) c4[l] = 0.5*(getC(j-1, l+n) + getC(j-1, l)); return(c4[l]);
case 5: if(c5[l]==EMPTY_VALUE) c5[l] = 0.5*(getC(j-1, l+n) + getC(j-1, l)); return(c5[l]);
case 6: if(c6[l]==EMPTY_VALUE) c6[l] = 0.5*(getC(j-1, l+n) + getC(j-1, l)); return(c6[l]);
case 7: if(c7[l]==EMPTY_VALUE) c7[l] = 0.5*(getC(j-1, l+n) + getC(j-1, l)); return(c7[l]);
case 8: if(c8[l]==EMPTY_VALUE) c8[l] = 0.5*(getC(j-1, l+n) + getC(j-1, l)); return(c8[l]);
}
}
int ShiftObjects()
{
string nm;
int shift=0;
for(int iw=1; iw<=N; iw++)
{
if(Symmetric) shift = 1<<(iw-1)-1;
for(int bar=0; bar<Len; bar++)
{
nm = "R"+iw+"."+bar;
ObjectSet(nm, OBJPROP_TIME1, Time[bar+shift]);
ObjectSet(nm, OBJPROP_TIME2, Time[bar+shift+1]);
}
}
}
int DeleteObjects()
{
string nm;
for(int bar=0; bar<Len; bar++)
for(int iw=1; iw<=N; iw++)
{
nm = "R"+iw+"."+bar;
ObjectDelete(nm);
}
}
int CreateColorTable()
{
int r, g, b, ri, gi, bi;
r = HighColor & 0xFF;
g = (HighColor & 0xFF00)>>8;
b = (HighColor & 0xFF0000)>>16;
for(int i=127; i<256; i++)
{
ri = 0xFF - (255-r)*(i-127)/128;
gi = 0xFF - (255-g)*(i-127)/128;
bi = 0xFF - (255-b)*(i-127)/128;
colr[i] = ri + gi<<8 + bi<<16;
}
r = LowColor & 0xFF;
g = (LowColor & 0xFF00)>>8;
b = (LowColor & 0xFF0000)>>16;
for(i=0; i<127; i++)
{
ri = 0xFF - (255-r)*(127-i)/128;
gi = 0xFF - (255-g)*(127-i)/128;
bi = 0xFF - (255-b)*(127-i)/128;
colr[i] = ri + gi<<8 + bi<<16;
}
/*
for(i=0; i<256; i++)
{
r = colr[i] & 0xFF;
g = (colr[i] & 0xFF00)>>8;
b = (colr[i] & 0xFF0000)>>16;
Print("colr[",i,"]=",colr[i]," (",r,", ",g,", ",b,")");
}
*/
}
Comments