Price Data Components
Indicators Used
Miscellaneous
0
Views
0
Downloads
0
Favorites
aWilliamsBand
//+------------------------------------------------------------------+
//| !WilliamsBand.mq4 |
//| Copyright 2012, Melniciuc Alexei |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, Melniciuc Alexei"
#property link ""
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Green
#property indicator_color3 Red
//--- external
extern bool alert=true;
extern bool ShowDispersia=true;
extern bool Buffer=false;
extern bool IfHour=true;
extern bool IfHour4=false;
extern bool IfDay=false;
extern bool IfWeek=false;
extern bool IfMonth=false;
extern color ColorBandUp=Blue;
extern color ColorCenter=Green;
extern color ColorBandDw=Red;
//--- constant
#define EMTYVAL -1
//--- buffer
double BandUp[];
double Center[];
double BandDw[];
//--- vars
int period, obj_period, p;
string P, StrBandUp, StrCenter, StrBandDw;
bool visibility;
int width;
string PP;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
PP="MN1";
switch(Period())
{
case PERIOD_M1 : PP="M1"; break;
case PERIOD_M5 : PP="M5"; break;
case PERIOD_M15 : PP="M15"; break;
case PERIOD_M30 : PP="M30"; break;
case PERIOD_H1 : PP="H1"; break;
case PERIOD_H4 : PP="H4"; break;
case PERIOD_D1 : PP="D1"; break;
case PERIOD_W1 : PP="W1";
}
visibility=true;
if(IfWeek)switch(Period()) // visibility M1 - W1
{
case PERIOD_MN1 :visibility=false;
}
if(IfDay)switch(Period()) // visibility M1 - D1
{
case PERIOD_W1 : visibility=false; break;
case PERIOD_MN1 : visibility=false;
}
if(IfHour4)switch(Period()) // visibility M1 - H1
{
case PERIOD_D1 : visibility=false; break;
case PERIOD_W1 : visibility=false; break;
case PERIOD_MN1 : visibility=false;
}
if(IfHour)switch(Period()) // visibility M1 - H1
{
case PERIOD_H4 : visibility=false; break;
case PERIOD_D1 : visibility=false; break;
case PERIOD_W1 : visibility=false; break;
case PERIOD_MN1 : visibility=false;
}
if(!visibility)return(0);
p=1;
P="H1";
width=1;
period=PERIOD_H1;
obj_period=OBJ_PERIOD_M1|OBJ_PERIOD_M5|OBJ_PERIOD_M15|OBJ_PERIOD_M30|OBJ_PERIOD_H1;
if(IfHour4){P="H4"; p=4; width=1; period=PERIOD_H4; obj_period=obj_period|OBJ_PERIOD_H4;}
if(IfDay){P="D1"; p=24; width=2; period=PERIOD_D1; obj_period=obj_period|OBJ_PERIOD_H4|OBJ_PERIOD_D1;}
if(IfWeek){P="W1"; p=7*24; width=3; period=PERIOD_W1; obj_period=obj_period|OBJ_PERIOD_H4|OBJ_PERIOD_D1|OBJ_PERIOD_W1;}
if(IfMonth){P="MN1"; p=31*24; width=4; period=PERIOD_MN1; obj_period=OBJ_ALL_PERIODS;}
StrBandUp="BandUp "+P;
StrCenter="Center "+P;
StrBandDw="BandDw "+P;
SetIndexStyle(0,DRAW_SECTION,STYLE_SOLID,width,ColorBandUp);
SetIndexBuffer(0,BandUp);
SetIndexStyle(1,DRAW_SECTION,STYLE_SOLID,width,ColorCenter);
SetIndexBuffer(1,Center);
SetIndexStyle(2,DRAW_SECTION,STYLE_SOLID,width,ColorBandDw);
SetIndexBuffer(2,BandDw);
IndicatorShortName("!WilliamsBand"+P);
SetIndexLabel(0,StrBandUp);
SetIndexLabel(1,StrCenter);
SetIndexLabel(2,StrBandDw);
SetIndexEmptyValue(0,EMTYVAL);
SetIndexEmptyValue(1,EMTYVAL);
SetIndexEmptyValue(2,EMTYVAL);
SetIndexDrawBegin(0,3);
SetIndexDrawBegin(1,3);
SetIndexDrawBegin(2,3);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
if(!visibility)return(0);
ObjectDelete(StrBandUp+" Dispersia+");
ObjectDelete(StrBandUp);
ObjectDelete(StrBandUp+" Dispersia-");
ObjectDelete(StrCenter);
ObjectDelete(StrBandDw+" Dispersia+");
ObjectDelete(StrBandDw);
ObjectDelete(StrBandDw+" Dispersia-");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if(!visibility)return(0);
int counted_bars=IndicatorCounted();
int Limit=Bars-counted_bars;
int bars=Bars/period*Period();
bars=Bars;
if(Limit>bars)Limit=bars;
//----
for(int i=Limit; i>=0; i--){
BandUp[iBarShift(NULL,0,iTime(NULL,period,i+1))]=iMA(NULL,period,3,0,MODE_SMA,PRICE_HIGH,i+1);
Center[iBarShift(NULL,0,iTime(NULL,period,i))]=iMA(NULL,period,3,0,MODE_SMA,PRICE_MEDIAN,i);
BandDw[iBarShift(NULL,0,iTime(NULL,period,i+1))]=iMA(NULL,period,3,0,MODE_SMA,PRICE_LOW,i+1);
}
double center=Center[iBarShift(NULL,0,iTime(NULL,period,0))];
double Mean, Dispersia;
int t;
if(IfMonth){ //month
t=TimeMonth(iTime(NULL,period,0));
}else if(IfWeek){ //week
t=TimeDay(iTime(NULL,period,0));
}
else if(IfDay){ //day
t=TimeDayOfWeek(iTime(NULL,period,0));
}
else if(IfHour4){ //hour
t=TimeHour(iTime(NULL,period,0));
}
else if(IfHour){ //hour
t=TimeHour(iTime(NULL,period,0));
}
Mean=MeanHL(t)/2;
Dispersia=DispersiaHL(t)/2;
double a=(Center[iBarShift(NULL,0,iTime(NULL,period,1))]-center)/(iTime(NULL,period,1)-iTime(NULL,period,0));
double b=(center*iTime(NULL,period,1)-Center[iBarShift(NULL,0,iTime(NULL,period,1))]*iTime(NULL,period,0))/(iTime(NULL,period,1)-iTime(NULL,period,0));
datetime CenterX=iTime(NULL,period,0)+p*3600;
double Center=a*CenterX+b;
double aUp=(Center+Mean-BandUp[iBarShift(NULL,0,iTime(NULL,period,1))])/(CenterX-iTime(NULL,period,1));
double bUp=BandUp[iBarShift(NULL,0,iTime(NULL,period,1))]-aUp*iTime(NULL,period,1);
double yUp=aUp*Time[0]+bUp;
double aDw=(Center-Mean-BandDw[iBarShift(NULL,0,iTime(NULL,period,1))])/(CenterX-iTime(NULL,period,1));
double bDw=BandDw[iBarShift(NULL,0,iTime(NULL,period,1))]-aDw*iTime(NULL,period,1);
double yDw=aDw*Time[0]+bDw;
if(Buffer){
for(i=iBarShift(NULL,0,iTime(NULL,period,1))-1; i>0; i--){
BandUp[i]=EMTYVAL;
BandDw[i]=EMTYVAL;
}
BandUp[0]=yUp;
BandDw[0]=yDw;
}
bool flag=false;
if(alert && Close[0]>=yUp){
flag=true;
Alert(Symbol()+" ["+P+" Band] "+PP+" Dw "+DoubleToStr((Close[0]-yUp)/Point,0)+"p "+DoubleToStr(100*(Close[0]-yUp)/Mean/2,0)+"%");
//ObjectDelete("ARROWDW "+P+" "+MathRand());
ObjectCreate("ARROWDW "+P+" "+MathRand(),OBJ_ARROW,0,Time[0],yUp);
ObjectSet("ARROWDW "+P+" "+MathRand(),OBJPROP_COLOR,ColorBandDw);
ObjectSet("ARROWDW "+P+" "+MathRand(),OBJPROP_ARROWCODE,SYMBOL_ARROWDOWN);
}
if(alert && Close[0]<=yDw){
flag=true;
Alert(Symbol()+" ["+P+" Band] "+PP+" Up "+DoubleToStr((yDw-Close[0])/Point,0)+"p "+DoubleToStr(100*(yDw-Close[0])/Mean/2,0)+"%");
//ObjectDelete("ARROWUP "+P+" "+MathRand());
ObjectCreate("ARROWUP "+P+" "+MathRand(),OBJ_ARROW,0,Time[0],yDw);
ObjectSet("ARROWUP "+P+" "+MathRand(),OBJPROP_COLOR,ColorBandUp);
ObjectSet("ARROWUP "+P+" "+MathRand(),OBJPROP_ARROWCODE,SYMBOL_ARROWUP);
}
if(flag)alert=false;
static double OldCenter=-1;
if(OldCenter==center)return(0);
OldCenter=center;
ObjectDelete(StrBandUp+" Dispersia+");
ObjectDelete(StrBandUp);
ObjectDelete(StrBandUp+" Dispersia-");
ObjectDelete(StrCenter);
ObjectDelete(StrBandDw+" Dispersia+");
ObjectDelete(StrBandDw);
ObjectDelete(StrBandDw+" Dispersia-");
ObjectCreate(StrBandUp,OBJ_TREND,0,iTime(NULL,period,1),BandUp[iBarShift(NULL,0,iTime(NULL,period,1))],CenterX,Center+Mean);
ObjectSet(StrBandUp,OBJPROP_COLOR,ColorBandUp);
ObjectSet(StrBandUp,OBJPROP_RAY,false);
ObjectSet(StrBandUp,OBJPROP_TIMEFRAMES,obj_period);
ObjectSet(StrBandUp,OBJPROP_STYLE,STYLE_DOT);
ObjectSetText(StrBandUp,P+" Up",6,"Arial Black",ColorBandUp);
ObjectCreate(StrCenter,OBJ_TREND,0,iTime(NULL,period,0),center,CenterX,Center);
ObjectSet(StrCenter,OBJPROP_COLOR,ColorCenter);
ObjectSet(StrCenter,OBJPROP_RAY,false);
ObjectSet(StrCenter,OBJPROP_TIMEFRAMES,obj_period);
ObjectSet(StrCenter,OBJPROP_STYLE,STYLE_DOT);
ObjectSetText(StrCenter,P+" Center",6,"Arial Black",ColorCenter);
ObjectCreate(StrBandDw,OBJ_TREND,0,iTime(NULL,period,1),BandDw[iBarShift(NULL,0,iTime(NULL,period,1))],CenterX,Center-Mean);
ObjectSet(StrBandDw,OBJPROP_COLOR,ColorBandDw);
ObjectSet(StrBandDw,OBJPROP_RAY,false);
ObjectSet(StrBandDw,OBJPROP_TIMEFRAMES,obj_period);
ObjectSet(StrBandDw,OBJPROP_STYLE,STYLE_DOT);
ObjectSetText(StrBandDw,P+" Dw",6,"Arial Black",ColorBandDw);
if(!ShowDispersia)return(0);
ObjectCreate(StrBandUp+" Dispersia+",OBJ_TREND,0,iTime(NULL,period,1),BandUp[iBarShift(NULL,0,iTime(NULL,period,1))],CenterX,Center+Mean+Dispersia);
ObjectSet(StrBandUp+" Dispersia+",OBJPROP_COLOR,ColorBandUp);
ObjectSet(StrBandUp+" Dispersia+",OBJPROP_RAY,false);
ObjectSet(StrBandUp+" Dispersia+",OBJPROP_TIMEFRAMES,obj_period);
ObjectSet(StrBandUp+" Dispersia+",OBJPROP_STYLE,STYLE_DASHDOTDOT);
//ObjectSetText(StrBandUp+" Dispersia+",P+" Up",6,"Arial Black",ColorBandUp);
ObjectCreate(StrBandUp+" Dispersia-",OBJ_TREND,0,iTime(NULL,period,1),BandUp[iBarShift(NULL,0,iTime(NULL,period,1))],CenterX,Center+Mean-Dispersia);
ObjectSet(StrBandUp+" Dispersia-",OBJPROP_COLOR,ColorBandUp);
ObjectSet(StrBandUp+" Dispersia-",OBJPROP_RAY,false);
ObjectSet(StrBandUp+" Dispersia-",OBJPROP_TIMEFRAMES,obj_period);
ObjectSet(StrBandUp+" Dispersia-",OBJPROP_STYLE,STYLE_DASHDOTDOT);
//ObjectSetText(StrBandUp+" Dispersia-",P+" Up",6,"Arial Black",ColorBandUp);
ObjectCreate(StrBandDw+" Dispersia+",OBJ_TREND,0,iTime(NULL,period,1),BandDw[iBarShift(NULL,0,iTime(NULL,period,1))],CenterX,Center-Mean+Dispersia);
ObjectSet(StrBandDw+" Dispersia+",OBJPROP_COLOR,ColorBandDw);
ObjectSet(StrBandDw+" Dispersia+",OBJPROP_RAY,false);
ObjectSet(StrBandDw+" Dispersia+",OBJPROP_TIMEFRAMES,obj_period);
ObjectSet(StrBandDw+" Dispersia+",OBJPROP_STYLE,STYLE_DASHDOTDOT);
//ObjectSetText(StrBandDw+" Dispersia+",P+" Dw",6,"Arial Black",ColorBandDw);
ObjectCreate(StrBandDw+" Dispersia-",OBJ_TREND,0,iTime(NULL,period,1),BandDw[iBarShift(NULL,0,iTime(NULL,period,1))],CenterX,Center-Mean-Dispersia);
ObjectSet(StrBandDw+" Dispersia-",OBJPROP_COLOR,ColorBandDw);
ObjectSet(StrBandDw+" Dispersia-",OBJPROP_RAY,false);
ObjectSet(StrBandDw+" Dispersia-",OBJPROP_TIMEFRAMES,obj_period);
ObjectSet(StrBandDw+" Dispersia-",OBJPROP_STYLE,STYLE_DASHDOTDOT);
//ObjectSetText(StrBandDw+" Dispersia-",P+" Dw",6,"Arial Black",ColorBandDw);
//----
return(0);
}
//+------------------------------------------------------------------+
double MeanHL(int h){
static double mean;
static int Oldh=-1;
if(Oldh==h)return(mean);
Oldh=h;
mean=0;
int n=0;
for(int i=iBars(NULL,period); i>0; i--){
if(IfMonth){if(TimeMonth(iTime(NULL,period,i))!=h)continue;} //month
else if(IfWeek){if(TimeDay(iTime(NULL,period,i))!=h)continue;} //week
else if(IfDay){if(TimeDayOfWeek(iTime(NULL,period,i))!=h)continue;} //day
else if(IfHour4){if(TimeHour(iTime(NULL,period,i))!=h)continue;} //hour4
else if(IfHour){if(TimeHour(iTime(NULL,period,i))!=h)continue;} //hour
mean+=iHigh(NULL,period,i)-iLow(NULL,period,i);
n++;
}
mean=mean/n;
return(mean);
}
//+------------------------------------------------------------------+
double DispersiaHL(int h){
static double dispersia;
static int Oldh=-1;
if(Oldh==h)return(dispersia);
Oldh=h;
double mean=MeanHL(h);
int n=0;
for(int i=iBars(NULL,period); i>0; i--){
if(IfMonth){if(TimeMonth(iTime(NULL,period,i))!=h)continue;} //month
else if(IfWeek){if(TimeDay(iTime(NULL,period,i))!=h)continue;} //week
else if(IfDay){if(TimeDayOfWeek(iTime(NULL,period,i))!=h)continue;} //day
else if(IfHour4){if(TimeHour(iTime(NULL,period,i))!=h)continue;} //hour4
else if(IfHour){if(TimeHour(iTime(NULL,period,i))!=h)continue;} //hour
dispersia+=MathAbs(iHigh(NULL,period,i)-iLow(NULL,period,i)-mean);
n++;
}
dispersia=dispersia/n;
return(dispersia);
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---