//+------------------------------------------------------------------+
//| |
//| Индикатор для автоматического построения уровней |
//| PPZ по трём фракталам |
//| _http://www.mql4.com/ru/users/Dserg |
//+------------------------------------------------------------------+
#property copyright "Dserg, 2010"
#property link "_http://www.mql4.com/ru/users/Dserg"
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 DeepSkyBlue
#property indicator_color2 HotPink
#property indicator_color3 Gold
#property indicator_color4 Tomato
//---- buffers
double B0[];
double B1[];
double B2[];
double B3[];
double Fr0[1000];
int Ind0[1000];
//Ширина окна, в котором ищутся уровни
extern int timeFrame = 0;
extern int Nbars = 150;
//Смещение правого бара для окна
extern int Nfirst = 0;
//Минимальное расстояние между уровнями в пунктах
extern double minDiff = 300.0;
//Минимальное расстояние между фракталами в барах
extern int minBars = 5;
static bool calculated;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 3additional buffers are used for counting.
//---- drawing settings
SetIndexStyle(0,DRAW_ARROW,0,4);
SetIndexBuffer(0, B0);
SetIndexArrow(0, 119);
SetIndexStyle(1,DRAW_ARROW,0,4);
SetIndexBuffer(1, B1);
SetIndexArrow(1, 119);
SetIndexStyle(2,DRAW_ARROW,0,4);
SetIndexBuffer(2, B2);
SetIndexArrow(2, 119);
SetIndexStyle(3,DRAW_ARROW,0,4);
SetIndexBuffer(3, B3);
SetIndexArrow(3, 119);
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
SetIndexEmptyValue(2,0.0);
SetIndexEmptyValue(3,0.0);
SetIndexLabel(0,"PPZ 0");
SetIndexLabel(1,"PPZ 1");
SetIndexLabel(2,"PPZ 2");
SetIndexLabel(3,"PPZ 3");
calculated = false;
return(0);
}
int deinit()
{
ObjectDelete("PPZ");
ObjectDelete("PPZ1");
ObjectDelete("PPZ2");
ObjectDelete("PPZ3");
ObjectDelete("V1");
ObjectDelete("V2");
Comment("");
return(0);
}
int start()
{
if (calculated) return(0);
if (Nbars>500) {
Comment("Too much bars to calulate, try < 500");
return(0);
}
int counted_bars = IndicatorCounted();
if (counted_bars < 0) return(-1);
if (counted_bars >= Bars) return(0);
Comment("Calculating PPZ..");
datetime t0 = TimeLocal();
int loopbegin = Nbars+Nfirst;
int i,j,k,l,imin,jmin,kmin,lmin,imin1,jmin1,kmin1,lmin1,imin2,jmin2,kmin2,lmin2,imin3,jmin3,kmin3,lmin3;
double f0,f1,f2,f3,p0,s0,pmin,smin,pmin1,smin1,pmin2,smin2,pmin3,smin3;
int Nmax;
pmin = 10.0;
smin = 10000.0;
pmin1 = 20.0;
smin1 = 20000.0;
pmin2 = 30.0;
smin2 = 30000.0;
pmin3 = 40.0;
smin3 = 40000.0;
//Ищем фракталы, складываем в массив
j=0;
for(i = loopbegin; i > Nfirst; i--) {
f0 = -1.0;
f0 = iFractals(NULL,timeFrame,MODE_UPPER,i);
if (f0<=0.0 ) f0 = iFractals(NULL,timeFrame,MODE_LOWER,i);
if (f0>0.0) {
Fr0[j]=f0;
Ind0[j]=i;
j++;
}
}
Nmax = j;
//ищем первую линию PPZ по 4-м фракталам
for(i = 0; i < Nmax; i++) {
f0 = Fr0;
for(j = 0; j < Nmax; j++) {
f1=Fr0[j];
if (i!=j && MathAbs(Ind0-Ind0[j])>minBars) {
for(k = 0; k < Nmax; k++) {
f2 = Fr0[k];
if (k!=i && k!=j && MathAbs(Ind0-Ind0[k])>minBars && MathAbs(Ind0[k]-Ind0[j])>minBars) {
for(l = 0; l < Nmax; l++) {
f3 = Fr0[l];
if ( l!=i && l!=j && l!=k && MathAbs(Ind0-Ind0[l])>minBars && MathAbs(Ind0[j]-Ind0[l])>minBars && MathAbs(Ind0[k]-Ind0[l])>minBars) {
p0 = (f0+f1+f2+f3)/4.0;
s0 = (f0-p0)*(f0-p0)+(f1-p0)*(f1-p0)+(f2-p0)*(f2-p0)+(f2-p0)*(f2-p0);
if (s0<smin) {
smin=s0;
pmin=p0;
imin=i;
jmin=j;
kmin=k;
lmin=l;
}
}
}
}
}
}
}
}
//ищем вторую линию PPZ по 4-м фракталам
for(i = 0; i < Nmax; i++) {
f0 = Fr0;
if (i!=imin && i!=jmin && i!=kmin && i!=lmin) {
for(j = 0; j < Nmax; j++) {
f1=Fr0[j];
if (i!=j && MathAbs(Ind0-Ind0[j])>minBars && j!=imin && j!=jmin && j!=kmin && j!=lmin) {
for(k = 0; k < Nmax; k++) {
f2 = Fr0[k];
if (k!=i && k!=j && MathAbs(Ind0-Ind0[k])>minBars && MathAbs(Ind0[k]-Ind0[j])>minBars && k!=imin && k!=jmin && k!=kmin && k!=lmin) {
for(l = 0; l < Nmax; l++) {
f3 = Fr0[l];
if ( l!=i && l!=j && l!=k && MathAbs(Ind0-Ind0[l])>minBars && MathAbs(Ind0[j]-Ind0[l])>minBars && MathAbs(Ind0[k]-Ind0[l])>minBars && l!=imin && l!=jmin && l!=kmin && l!=lmin) {
p0 = (f0+f1+f2+f3)/4.0;
s0 = (f0-p0)*(f0-p0)+(f1-p0)*(f1-p0)+(f2-p0)*(f2-p0)+(f2-p0)*(f2-p0);
if (s0<smin1 && MathAbs(p0-pmin)>minDiff*Point) {
smin1=s0;
pmin1=p0;
imin1=i;
jmin1=j;
kmin1=k;
lmin1=l;
}
}
}
}
}
}
}
}
}
//ищем третью линию PPZ по 4-м фракталам
for(i = 0; i < Nmax; i++) {
f0 = Fr0;
if (i!=imin && i!=jmin && i!=kmin && i!=lmin && i!=imin1 && i!=jmin1 && i!=kmin1 && i!=lmin1) {
for(j = 0; j < Nmax; j++) {
f1=Fr0[j];
if (i!=j && MathAbs(Ind0-Ind0[j])>minBars && j!=imin && j!=jmin && j!=kmin && j!=lmin && j!=imin1 && j!=jmin1 && j!=kmin1 && j!=lmin1) {
for(k = 0; k < Nmax; k++) {
f2 = Fr0[k];
if (k!=i && k!=j && MathAbs(Ind0-Ind0[k])>minBars && MathAbs(Ind0[k]-Ind0[j])>minBars && k!=imin && k!=jmin && k!=kmin && k!=lmin && k!=imin1 && k!=jmin1 && k!=kmin1 && k!=lmin1) {
for(l = 0; l < Nmax; l++) {
f3 = Fr0[l];
if ( l!=i && l!=j && l!=k && MathAbs(Ind0-Ind0[l])>minBars && MathAbs(Ind0[j]-Ind0[l])>minBars && MathAbs(Ind0[k]-Ind0[l])>minBars && l!=imin && l!=jmin && l!=kmin && l!=lmin && l!=imin1 && l!=jmin1 && l!=kmin1 && l!=lmin1) {
p0 = (f0+f1+f2+f3)/4.0;
s0 = (f0-p0)*(f0-p0)+(f1-p0)*(f1-p0)+(f2-p0)*(f2-p0)+(f2-p0)*(f2-p0);
if (s0<smin2 && MathAbs(p0-pmin)>minDiff*Point && MathAbs(p0-pmin1)>minDiff*Point) {
smin2=s0;
pmin2=p0;
imin2=i;
jmin2=j;
kmin2=k;
lmin2=l;
}
}
}
}
}
}
}
}
}
//ищем четвёртую линию PPZ по 4-м фракталам
for(i = 0; i < Nmax; i++) {
f0 = Fr0;
if (i!=imin && i!=jmin && i!=kmin && i!=lmin && i!=imin1 && i!=jmin1 && i!=kmin1 && i!=lmin1 && i!=imin2 && i!=jmin2 && i!=kmin2 && i!=lmin2) {
for(j = 0; j < Nmax; j++) {
f1=Fr0[j];
if (i!=j && MathAbs(Ind0-Ind0[j])>minBars && j!=imin && j!=jmin && j!=kmin && j!=lmin && j!=imin1 && j!=jmin1 && j!=kmin1 && j!=lmin1 && j!=imin2 && j!=jmin2 && j!=kmin2 && j!=lmin2) {
for(k = 0; k < Nmax; k++) {
f2 = Fr0[k];
if (k!=i && k!=j && MathAbs(Ind0-Ind0[k])>minBars && MathAbs(Ind0[k]-Ind0[j])>minBars && k!=imin && k!=jmin && k!=kmin && k!=lmin && k!=imin1 && k!=jmin1 && k!=kmin1 && k!=lmin1 && k!=imin2 && k!=jmin2 && k!=kmin2 && k!=lmin2) {
for(l = 0; l < Nmax; l++) {
f3 = Fr0[l];
if ( l!=i && l!=j && l!=k && MathAbs(Ind0-Ind0[l])>minBars && MathAbs(Ind0[j]-Ind0[l])>minBars && MathAbs(Ind0[k]-Ind0[l])>minBars && l!=imin && l!=jmin && l!=kmin && l!=lmin && l!=imin1 && l!=jmin1 && l!=kmin1 && l!=lmin1 && l!=imin2 && l!=jmin2 && l!=kmin2 && l!=lmin2) {
p0 = (f0+f1+f2+f3)/4.0;
s0 = (f0-p0)*(f0-p0)+(f1-p0)*(f1-p0)+(f2-p0)*(f2-p0)+(f2-p0)*(f2-p0);
if (s0<smin3 && MathAbs(p0-pmin)>minDiff*Point && MathAbs(p0-pmin1)>minDiff*Point && MathAbs(p0-pmin2)>minDiff*Point) {
smin3=s0;
pmin3=p0;
imin3=i;
jmin3=j;
kmin3=k;
lmin3=l;
}
}
}
}
}
}
}
}
}
//Рисуем результаты, отмечаем точки
B0[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[imin]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[imin]))];
B0[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[jmin]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[jmin]))];
B0[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[kmin]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[kmin]))];
B0[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[lmin]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[lmin]))];
B1[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[imin1]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[imin1]))];
B1[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[jmin1]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[jmin1]))];
B1[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[kmin1]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[kmin1]))];
B1[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[lmin1]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[lmin1]))];
B2[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[imin2]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[imin2]))];
B2[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[jmin2]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[jmin2]))];
B2[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[kmin2]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[kmin2]))];
B2[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[lmin2]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[lmin2]))];
B3[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[imin3]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[imin3]))];
B3[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[jmin3]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[jmin3]))];
B3[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[kmin3]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[kmin3]))];
B3[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[lmin3]))] = Open[iBarShift(NULL,0,iTime(NULL,timeFrame,Ind0[lmin3]))];
if(!ObjectCreate("PPZ",OBJ_HLINE,0,Time[0],NormalizeDouble(pmin,Digits))) {
Comment("ERROR");
return(0);
}
if(!ObjectCreate("PPZ1",OBJ_HLINE,0,Time[0],NormalizeDouble(pmin1,Digits))) {
Comment("ERROR");
return(0);
}
if(!ObjectCreate("PPZ2",OBJ_HLINE,0,Time[0],NormalizeDouble(pmin2,Digits))) {
Comment("ERROR");
return(0);
}
if(!ObjectCreate("PPZ3",OBJ_HLINE,0,Time[0],NormalizeDouble(pmin3,Digits))) {
Comment("ERROR");
return(0);
}
if(!ObjectCreate("V1",OBJ_VLINE,0,iTime(NULL,timeFrame,Nbars+Nfirst),Open[0])) {
Comment("ERROR");
return(0);
}
if(!ObjectCreate("V2",OBJ_VLINE,0,iTime(NULL,timeFrame,Nfirst),Open[0])) {
Comment("ERROR");
return(0);
}
ObjectSet("PPZ",OBJPROP_COLOR,DeepSkyBlue);
ObjectSet("PPZ",OBJPROP_STYLE,STYLE_DASH);
ObjectSet("PPZ1",OBJPROP_COLOR,DeepPink);
ObjectSet("PPZ1",OBJPROP_STYLE,STYLE_DASH);
ObjectSet("PPZ2",OBJPROP_COLOR,Gold);
ObjectSet("PPZ2",OBJPROP_STYLE,STYLE_DASH);
ObjectSet("PPZ3",OBJPROP_COLOR,Tomato);
ObjectSet("PPZ3",OBJPROP_STYLE,STYLE_DASH);
ObjectSet("V1",OBJPROP_COLOR,DarkKhaki);
ObjectSet("V2",OBJPROP_COLOR,DarkKhaki);
calculated = true;
datetime t1 = TimeLocal()-t0;
Comment("Calculation of PPZ done \n PPZ0 " + DoubleToStr(pmin,5) + "\n PPZ1 " + DoubleToStr(pmin1,5) + "\n PPZ2 " + DoubleToStr(pmin2,5) + "\n PPZ3 " + DoubleToStr(pmin3,5) + "\n Time " + t1 + " sec.");
return(0);
}
//+------------------------------------------------------------------+