//+------------------------------------------------------------------+ //| EMA(EMA(Stoc,80),3),5).mq4 | //+------------------------------------------------------------------+ #property indicator_separate_window #property indicator_buffers 4 #property indicator_color1 Black #property indicator_width1 2 #property indicator_style1 1 #property indicator_color2 Green #property indicator_width2 2 #property indicator_style2 1 #property indicator_color3 Red #property indicator_width3 2 #property indicator_style3 1 #property indicator_color4 Blue #property indicator_width4 2 #property indicator_style4 1 #property indicator_minimum 0 #property indicator_maximum 100 #property indicator_level1 10 #property indicator_level2 20 #property indicator_level3 80 #property indicator_level4 90 //---- indicator parameters extern int Sto_Black = 80; extern int Sto_Green = 40; extern int Sto_Red = 20; extern int Sto_Blue = 10; extern int ema_1 = 3; extern int ema_2 = 5; //---- indicator buffers double Sto_b[]; double Sto_g[]; double Sto_r[]; double Sto_bl[]; //---- int draw_begin, pBars, mcnt_bars; int TimeFrame = 0; int SmoothMode = 1; int SignalMode = 1; int Price = 0; string TF, fast_name, slow_name, Price_; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- drawing settings SetIndexStyle(0,DRAW_LINE); SetIndexStyle(1,DRAW_LINE); SetIndexStyle(2,DRAW_LINE); SetIndexStyle(3,DRAW_LINE); draw_begin=Sto_Black+ema_1+ema_2; //---- indicator name fast_name ="EMA"; slow_name="EMA"; if(Price == 0) Price_ = "Close"; TF = "Current"; IndicatorShortName("AllStochastics["+TF+"] ("+Price_+","+Sto_Black+","+fast_name+"("+ema_1+"),"+slow_name+"("+ema_2+"))"); SetIndexDrawBegin(0,draw_begin); SetIndexDrawBegin(1,draw_begin); SetIndexDrawBegin(2,draw_begin); SetIndexDrawBegin(3,draw_begin); SetIndexLabel(0,"Black"); SetIndexLabel(1,"Green"); SetIndexLabel(2,"Red"); SetIndexLabel(3,"Blue"); //---- indicator buffers mapping SetIndexBuffer(0,Sto_b); SetIndexBuffer(1,Sto_g); SetIndexBuffer(2,Sto_r); SetIndexBuffer(3,Sto_bl); //---- initialization done if(TimeFrame == 0 || TimeFrame < Period()) TimeFrame = Period(); return(0); } //+------------------------------------------------------------------+ //| AllStochastics_v2 | //+------------------------------------------------------------------+ int start() { int limit, y, i, shift, cnt_bars=IndicatorCounted(); double K_B[], K_G[], K_R[], K_BL[]; double mSto_b[], mSto_g[], mSto_r[], mSto_bl[]; double mSto_b1[],mSto_g1[], mSto_r1[], mSto_bl1[]; int mBars = iBars(NULL,TimeFrame); if(mBars != pBars) { ArrayResize(K_B,mBars); ArrayResize(K_G,mBars); ArrayResize(K_R,mBars); ArrayResize(K_BL,mBars); ArrayResize(mSto_b,mBars); ArrayResize(mSto_g,mBars); ArrayResize(mSto_r,mBars); ArrayResize(mSto_bl,mBars); ArrayResize(mSto_b1,mBars); ArrayResize(mSto_g1,mBars); ArrayResize(mSto_r1,mBars); ArrayResize(mSto_bl1,mBars); pBars = mBars; } //Alert(cnt_bars); if(cnt_bars<1) { for(i=1;i<=draw_begin;i++) { Sto_b[Bars-i]=0; Sto_g[Bars-i]=0; Sto_r[Bars-i]=0; Sto_bl[Bars-i]=0; } mcnt_bars = 1; } if(mcnt_bars > 1) { mcnt_bars = mcnt_bars - 1; } //----------------------------------------------------- //calcolo la prima EMA dello stoc Black for(y = mcnt_bars-1; y <=mBars; y++) { //shift = mBars-y-1; int shift_b = mBars-y-1; double BPrice = iMA(NULL,TimeFrame,1,0,0,0,shift_b); double up_B = 0; double dn_B = 10000000000; for(i=0;i 0) { K_B[y] = 100*(BPrice - dn_B)/(up_B - dn_B); } else { K_B[y] = 0; } mSto_b[y] = EMA(K_B[y],mSto_b,ema_1,y); mSto_b1[y] = EMA(mSto_b[y],mSto_b1,ema_2,y); if(TimeFrame == Period()) { Sto_b[shift_b] = mSto_b1[y]; } } //-------------------------------------------------------- //Calcolo la seconda EMA dello Stoc Green for(y = mcnt_bars - 1; y < mBars; y++) { int shift_g = mBars-y-1; double GPrice = iMA(NULL,TimeFrame,1,0,0,0,shift_g); double up_G = 0; double dn_G = 10000000000; for(i=0;i 0) { K_G[y] = 100*(GPrice - dn_G)/(up_G - dn_G); } else { K_G[y] = 0; } mSto_g[y] = EMA(K_G[y],mSto_g,ema_1,y); mSto_g1[y] = EMA(mSto_g[y],mSto_g1,ema_2,y); if(TimeFrame == Period()) { Sto_g[shift_g] = mSto_g1[y]; } } //-------------------------------------------------------- //Calcolo la seconda EMA dello Stoc Red for(y = mcnt_bars - 1; y < mBars; y++) { int shift_r = mBars-y-1; double RPrice = iMA(NULL,TimeFrame,1,0,0,0,shift_r); double up_R = 0; double dn_R = 10000000000; for(i=0;i 0) { K_R[y] = 100*(RPrice - dn_R)/(up_R - dn_R); } else { K_R[y] = 0; } mSto_r[y] = EMA(K_R[y],mSto_r,ema_1,y); mSto_r1[y] = EMA(mSto_r[y],mSto_r1,ema_2,y); if(TimeFrame == Period()) { Sto_r[shift_r] = mSto_r1[y]; } } //-------------------------------------------------------- //Calcolo la seconda EMA dello Stoc Blue for(y = mcnt_bars - 1; y < mBars; y++) { int shift_bl = mBars-y-1; double BlPrice = iMA(NULL,TimeFrame,1,0,0,0,shift_bl); double up_Bl = 0; double dn_Bl = 10000000000; for(i=0;i 0) { K_BL[y] = 100*(BlPrice - dn_Bl)/(up_Bl - dn_Bl); } else { K_BL[y] = 0; } mSto_bl[y] = EMA(K_BL[y],mSto_bl,ema_1,y); mSto_bl1[y] = EMA(mSto_bl[y],mSto_bl1,ema_2,y); if(TimeFrame == Period()) { Sto_bl[shift_bl] = mSto_bl1[y]; } } mcnt_bars = mBars-1; //---- done return(0); } //------------------------------------------------------ // MA_Method=1: EMA - Exponential Moving Average double EMA(double price,double array[],int per,int bar) { if(bar == 2) { double ema = price; } else { if(bar > 2) { ema = array[bar-1] + 2.0/(1+per)*(price - array[bar-1]); } } return(ema); } //------------------------------------------------------