//+------------------------------------------------------------------+ //| CC-PercentBollinger.mq4 | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, Carlo Colombo" #property indicator_maximum 150 #property indicator_minimum -50 #property indicator_separate_window #property indicator_buffers 2 #property indicator_color1 DodgerBlue #property indicator_color2 Red //---- input parameters extern string PriceTypexPercentual = "1=Median 2=True-MID 3=WeightClose"; extern int PriceTypeApplied = 1; extern int BollingerPeriod = 80; extern double BollingerDeviation = 2; extern int BollingerShift = 0; extern string BollingerPriceTypes = "CL=0,OP=1,HI=2,LO=3,ME=4,TY=5,WE=6"; extern int BollingerPrice = 0 ; extern string SmoothingType = "0 =SMMA 1 = EMA"; extern int MAType = 1 ; extern int MAPeriod = 4; extern int Bars2Draw = 10000; //---- buffers double Buffer1[]; double Buffer2[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { string short_name; //---- indicator line 0 SetIndexStyle (0,DRAW_LINE); SetIndexBuffer (0,Buffer1); SetIndexLabel (0,"Buffer 0 - direct"); //---- indicator line 1 SetIndexStyle (1,DRAW_LINE); SetIndexBuffer (1,Buffer2); SetIndexLabel (1,"Buffer 1 - median smoothed"); SetLevelValue( 0, 100); SetLevelValue( 1, 50); SetLevelValue( 2, 0) ; if (MAType > 1 || MAType < 0) MAType = 1; if (MAPeriod < 1 ) MAPeriod = 1; if (BollingerPrice > 6 || BollingerPrice < 0) BollingerPrice = 0; if (PriceTypeApplied > 3 || PriceTypeApplied < 1) PriceTypeApplied = 1; short_name="CC-Percent Bollinger "+ BollingerPeriod+ "," + DoubleToStr(BollingerDeviation,2)+ "," + BollingerShift; IndicatorShortName(short_name); return(0); } //+------------------------------------------------------------------+ int start() { int i ; int counted_bars = IndicatorCounted(); if(Bars<=BollingerPeriod) return(0); //---- initial zero - azzero il buffer if( counted_bars < 1 ) { for(i=1;i<=Bars;i++) { Buffer1[Bars-i]=0.0; Buffer2[Bars-i]=0.0; } } //---- i = Bars - BollingerPeriod - 1 ; if ( counted_bars >= BollingerPeriod ) i = Bars - counted_bars - 1 ; while( i >= 0 ) { Buffer1[i]= PercentoSegnaleBollinger(i); i--; } for(i = 0; i < Bars2Draw; i++) { switch (MAType) { case 0: Buffer2[i] = SmaCC(Buffer1,MAPeriod,i); break; case 1: Buffer2[i] = EmaCC(Buffer1,MAPeriod,i); break; } } return(0); } double PercentoSegnaleBollinger (int i ) { double PercentoIstantaneo ; double AmpiezzaEnv ; double MinimoAttuale,MassimoAttuale ; double ValoreTop ; double Prezzo; //--------------------------------------------------------------------------------- // misura della distanza media attuale del prezzo dalla banda bassa di Bollinger double BandaBassa = iBands( NULL, 0, BollingerPeriod, BollingerDeviation, BollingerShift, BollingerPrice, MODE_LOWER ,i ); double BandaAlta = iBands( NULL, 0, BollingerPeriod, BollingerDeviation, BollingerShift, BollingerPrice, MODE_UPPER ,i ); // median if ( PriceTypeApplied == 1 ) Prezzo = (High [i] - Low [i]) / 2 + Low [i]; // true MID of open close if ( PriceTypeApplied == 2 ) Prezzo = (MathMax(Open [i],Close [i])-MathMin(Open[i],Close[i])) / 2 + MathMin(Open[i],Close[i]) ; // wclose if ( PriceTypeApplied == 3 ) Prezzo = (High[i]+Low[i]+Close[i]+Close[i])/4; //Distanza attuale AmpiezzaEnv = BandaAlta - BandaBassa; if (Prezzo >= ((BandaAlta-BandaBassa)/2)+BandaBassa) { Prezzo = High [i]; } else { Prezzo = Low [i]; } if (Prezzo >= BandaBassa && Prezzo <= BandaAlta) { // qui il prezzo medio è dentro le bande! Prezzo = (High[i]+Low[i]+Close[i]+Close[i])/4; ValoreTop = Prezzo - BandaBassa; if (ValoreTop==0)ValoreTop=Point; PercentoIstantaneo = (1 / (AmpiezzaEnv / ValoreTop ) ) * 100 ; } else { // qui il prezzo ha sforato sopra o sotto! if (Prezzo > BandaAlta) { Prezzo = High[i];//MathMax(Open[i],Close[i]); ValoreTop = Prezzo - BandaAlta; if (ValoreTop==0)ValoreTop=Point; PercentoIstantaneo = (1/(AmpiezzaEnv / ValoreTop) * 100) + 100 ; } else { // qui siamo sotto la banda bassa! Prezzo = Low[i];//MathMin(Open[i],Close[i]); ValoreTop = BandaBassa - Prezzo; if (ValoreTop==0)ValoreTop=Point; PercentoIstantaneo = -(1/(AmpiezzaEnv / ValoreTop) * 100) ; } } return (PercentoIstantaneo); } //+------------------------------------------------------------------+ double EmaCC (double ArrayIN[] , int Periodo , int Shift ) { double MediaUscita; double pr = 2.0 / ( Periodo + 1 ) ; double Medie[]; ArrayResize (Medie,Periodo * 2 + 2 ); int pos = Periodo * 2 -1; while ( pos >= Periodo ) { MediaUscita = MediaUscita + ArrayIN [ pos + Shift] ; pos-- ; } pos = Periodo-1; Medie[Periodo] = MediaUscita / Periodo; while ( pos >=0 ) { Medie[pos] = ( ArrayIN [ pos + Shift ] * pr ) + ( Medie [ pos + 1 ] * ( 1 - pr ) ) ; pos--; } return (Medie[0]); } double SmaCC (double ArrayIN[] , int Periodo , int Shift) { double MediaUscita; double Medie[]; ArrayResize (Medie, Periodo * 2 + 2 ); int pos = Periodo * 2 -1; while ( pos >= Periodo ) { MediaUscita = MediaUscita + ArrayIN [ pos + Shift] ; pos-- ; } pos = Periodo-1; Medie[Periodo] = MediaUscita / Periodo; while ( pos >=0 ) { MediaUscita = Medie [ pos + 1 ] * ( Periodo - 1 ) + ArrayIN [ pos + Shift] ; Medie[pos] = MediaUscita / Periodo; pos--; } return (Medie[0]); }