#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 White
#property indicator_color2 Yellow
#property indicator_color3 Yellow
#property indicator_color4 Yellow
#property indicator_color5 Yellow
#property indicator_color6 Yellow
//---- input parameters
extern int BBI_Ma1 = 3;
extern int BBI_Ma2 = 6;
extern int BBI_Ma3 = 12;
extern int BBI_Ma4 = 24;
extern int Boll_Ma = 9;
extern int Boll_P = 3;
extern int Ma_Method = 0;//=0:Simple moving average,=1:Exponential moving average,=2:Smoothed moving average,=3:Linear weighted moving average
//---- indicator buffers
double up[];
double lower[];
double BBI[];
double Flag[];
double BBI_std[];//standard deviation
double BBI_av[];//average
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- first positions skipped when drawing
SetIndexDrawBegin(0,BBI_Ma4+Boll_Ma*2);
SetIndexDrawBegin(1,BBI_Ma4+Boll_Ma*2);
SetIndexDrawBegin(2,BBI_Ma4+Boll_Ma*2);
SetIndexDrawBegin(3,BBI_Ma4+Boll_Ma*2);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,BBI);
SetIndexBuffer(1,up);
SetIndexBuffer(2,lower);
SetIndexBuffer(3,Flag);
SetIndexBuffer(4,BBI_std);
SetIndexBuffer(5,BBI_av);
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
SetIndexStyle(3,DRAW_ARROW);
SetIndexStyle(4,DRAW_NONE);
SetIndexStyle(5,DRAW_NONE);
SetIndexArrow(3,108);
//---- index labels
SetIndexLabel(0,"BBI");
SetIndexLabel(1,"BBI UPR");
SetIndexLabel(2,"BBI DWR");
SetIndexLabel(3,"BBI Flag");
IndicatorShortName("BBIboll band");
SetIndexEmptyValue(0,0);
SetIndexEmptyValue(1,0);
SetIndexEmptyValue(2,0);
SetIndexEmptyValue(3,0);
SetIndexEmptyValue(4,0);
SetIndexEmptyValue(5,0);
//---- initialization done
return(0);
}
int start()
{
int limit,i,ii;
int counted_bars=IndicatorCounted();
double ma1,ma2,ma3,ma4,std;
if(Bars<BBI_Ma4+Boll_Ma*2)return(-1);
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;//+BBI_Ma4+Boll_Ma;
//---- main loop
while(limit>-1)
{
ma1 = iMA(NULL,0,BBI_Ma1,0,Ma_Method,PRICE_WEIGHTED,limit);
ma2 = iMA(NULL,0,BBI_Ma2,0,Ma_Method,PRICE_WEIGHTED,limit);
ma3 = iMA(NULL,0,BBI_Ma3,0,Ma_Method,PRICE_WEIGHTED,limit);
ma4 = iMA(NULL,0,BBI_Ma4,0,Ma_Method,PRICE_WEIGHTED,limit);
BBI[limit] = 0.25 * (ma1 + ma2 + ma3 + ma4);
limit --;
}
limit=Bars-counted_bars;//+BBI_Ma4+Boll_Ma;
while(limit>-1)
{
BBI_av[limit] = iMAOnArray(BBI,0,Boll_Ma,0,Ma_Method,limit);
std = 0;
for (i =0;i<Boll_Ma;i++){
ii = limit + i;
double d = (BBI_av[limit] - BBI[ii]);
std += d*d;
}
BBI_std[limit] = MathSqrt(std/Boll_Ma);
up[limit] = BBI[limit] + Boll_P * BBI_std[limit];
lower[limit] = BBI[limit] - Boll_P * BBI_std[limit];
// std = iStdDevOnArray(BBI,0,Boll_Ma,Ma_Method,0,limit);
// up[limit] = BBI[limit] + Boll_P * std;
// lower[limit] = BBI[limit] - Boll_P * std;
if(up[limit]<High[limit]&&lower[limit]>Low[limit])Flag[limit]=High[limit]+5*Point;
limit --;
}
Comment("BBI: ",BBI[0]," ",up[0]," ",lower[0]);
//---- done
return(0);
}
//+------------------------------------------------------------------+
|