//+------------------------------------------------------------------+
//| 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