//+------------------------------------------------------------------+ //| KeltnerChannel.mq4 | //+------------------------------------------------------------------+ #property copyright "" #property link "" #property indicator_chart_window #property indicator_buffers 8 #property indicator_color1 Linen #property indicator_color2 LimeGreen #property indicator_color3 LimeGreen #property indicator_color4 Red #property indicator_color5 Red #property indicator_color6 DarkGray #property indicator_color7 DarkGray #property indicator_color8 DarkGray #property indicator_width1 2 #property indicator_width2 2 #property indicator_width3 2 #property indicator_width4 2 #property indicator_width5 2 #property indicator_style6 STYLE_DASH #property indicator_style7 STYLE_DASH #property indicator_style8 STYLE_DASH // // // // // // extern string TimeFrame = "current time frame"; extern double Length = 75; extern int Price = 0; extern double Phase = 0; extern bool Double = false; extern double FilterDeviations = 1.0; extern int FilterPeriod = 20; extern int Ma_Period = 34; extern int Ma_Mode = 3; extern int Atr_Period = 20; extern double K = 1.0; extern bool Atr_Mode = true; extern bool Interpolate = true; // // // // // double jur[]; double jurUpa[]; double jurUpb[]; double jurDna[]; double jurDnb[]; double upper[]; double ma[]; double lower[]; // // // // // string IndicatorFileName; int timeFrame; bool calculateKcn; bool returningBars; //+------------------------------------------------------------------+ //| //+------------------------------------------------------------------+ // // // // // int init() { IndicatorBuffers(8); SetIndexBuffer(0,jur); SetIndexBuffer(1,jurUpa); SetIndexBuffer(2,jurUpb); SetIndexBuffer(3,jurDna); SetIndexBuffer(4,jurDnb); SetIndexBuffer(5,upper); SetIndexBuffer(6,ma); SetIndexBuffer(7,lower); // // // // // IndicatorFileName = WindowExpertName(); calculateKcn = (TimeFrame=="calculateKcn"); returningBars = (TimeFrame=="returnBars"); timeFrame = stringToTimeFrame(TimeFrame); return(0); } int deinit() { return(0); } //+------------------------------------------------------------------+ //| //+------------------------------------------------------------------+ // // // // // double work[][4]; #define _lastLo 0 #define _lastHi 1 #define _trend 2 #define _trenc 3 int start() { int counted_bars=IndicatorCounted(); int i,k,r,limit; if(counted_bars<0) return(-1); if(counted_bars>0) counted_bars--; limit=MathMin(Bars-counted_bars,Bars-1); if (returningBars) { jur[0] = limit; return(0); } if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars); // // // // // if (calculateKcn || timeFrame==Period()) { if (work[Bars-limit-1][_trenc]== 1 && work[Bars-limit-2][_trend] == 1) CleanPoint(limit,jurUpa,jurUpb); if (work[Bars-limit-1][_trenc]==-1 && work[Bars-limit-2][_trend] == -1) CleanPoint(limit,jurDna,jurDnb); for(i=limit, r=Bars-i-1; i>=0; i--,r++) { jur[i] = iDSmooth(iMA(NULL,0,1,0,MODE_SMA,Price,i),Length,Phase,Double,i); jurUpa[i] = EMPTY_VALUE; jurUpb[i] = EMPTY_VALUE; jurDna[i] = EMPTY_VALUE; jurDnb[i] = EMPTY_VALUE; // // // // // double stddev = 0; if (FilterDeviations>0) { double avgdiff = 0; for (k=0; k filter) { work[r][_trend] = 1; if (work[r-1][_trend] != 1) work[r][_lastLo] = jur[i+1]; } if (jur[i+1]-jur[i] > filter) { work[r][_trend] = -1; if (work[r-1][_trend] != -1) work[r][_lastHi] = jur[i+1]; } if (work[r][_trend] == 1 && jur[i]>jur[i+1] && (jur[i]-work[r][_lastLo])>filter && work[r][_lastLo]>0) work[r][_trenc] = 1; if (work[r][_trend] ==-1 && jur[i]filter && work[r][_lastHi]>0) work[r][_trenc] = -1; if (!calculateKcn) { if (work[r][_trenc] == 1 && work[r-1][_trend] == 1) PlotPoint(i,jurUpa,jurUpb,jur); if (work[r][_trenc] == -1 && work[r-1][_trend] == -1) PlotPoint(i,jurDna,jurDnb,jur); } // // // // // ma[i] = iMA(NULL,0,Ma_Period,0,Ma_Mode,Price,i); if (Atr_Mode) double avg = iATR(NULL,0,Atr_Period,i); else { double sum=0; for (int x=0; x Period()) limit = MathMax(limit,MathMin(Bars,iCustom(NULL,timeFrame,IndicatorFileName,"returnBars",0,0)*timeFrame/Period())); if (work[Bars-limit-1][_trenc]== 1 && work[Bars-limit-2][_trend] == 1) CleanPoint(limit,jurUpa,jurUpb); if (work[Bars-limit-1][_trenc]==-1 && work[Bars-limit-2][_trend] == -1) CleanPoint(limit,jurDna,jurDnb); for(i=limit, r=Bars-i-1; i>=0; i--,r++) { int y = iBarShift(NULL,timeFrame,Time[i]); jur[i] = iCustom(NULL,timeFrame,IndicatorFileName,"CalculateKcn",Length,Price,Phase,Double,FilterDeviations,FilterPeriod,Ma_Period,Ma_Mode,Atr_Period,K,Atr_Mode,0,y); upper[i] = iCustom(NULL,timeFrame,IndicatorFileName,"CalculateKcn",Length,Price,Phase,Double,FilterDeviations,FilterPeriod,Ma_Period,Ma_Mode,Atr_Period,K,Atr_Mode,5,y); ma[i] = iCustom(NULL,timeFrame,IndicatorFileName,"CalculateKcn",Length,Price,Phase,Double,FilterDeviations,FilterPeriod,Ma_Period,Ma_Mode,Atr_Period,K,Atr_Mode,6,y); lower[i] = iCustom(NULL,timeFrame,IndicatorFileName,"CalculateKcn",Length,Price,Phase,Double,FilterDeviations,FilterPeriod,Ma_Period,Ma_Mode,Atr_Period,K,Atr_Mode,7,y); work[r][_trend] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateKcn",Length,Price,Phase,Double,FilterDeviations,FilterPeriod,Ma_Period,Ma_Mode,Atr_Period,K,Atr_Mode,8,y); work[r][_trenc] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateKcn",Length,Price,Phase,Double,FilterDeviations,FilterPeriod,Ma_Period,Ma_Mode,Atr_Period,K,Atr_Mode,9,y); jurUpa[i] = EMPTY_VALUE; jurUpb[i] = EMPTY_VALUE; jurDna[i] = EMPTY_VALUE; jurDnb[i] = EMPTY_VALUE; // // // // // if (!Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) continue; // // // // // datetime time = iTime(NULL,timeFrame,y); for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue; for(int j = 1; j < n; j++) { jur[i+j] = jur[i] + (jur[i+n] - jur[i] ) * j/n; upper[i+j] = upper[i] + (upper[i+n] - upper[i]) * j/n; ma[i+j] = ma[i] + (ma[i+n] - ma[i]) * j/n; lower[i+j] = lower[i] + (lower[i+n] - lower[i]) * j/n; } } for(i=limit, r=Bars-i-1; i>=0; i--,r++) { work[r][_trenc] = 0; work[r][_trend] = work[r-1][_trend]; work[r][_lastLo] = work[r-1][_lastLo]; work[r][_lastHi] = work[r-1][_lastHi]; if (jur[i]-jur[i+1] > filter) { work[r][_trend] = 1; if (work[r-1][_trend] != 1) work[r][_lastLo] = jur[i+1]; } if (jur[i+1]-jur[i] > filter) { work[r][_trend] = -1; if (work[r-1][_trend] != -1) work[r][_lastHi] = jur[i+1]; } if (work[r][_trend] == 1 && jur[i]>jur[i+1] && (jur[i]-work[r][_lastLo])>filter && work[r][_lastLo]>0) work[r][_trenc] = 1; if (work[r][_trend] ==-1 && jur[i]filter && work[r][_lastHi]>0) work[r][_trenc] = -1; if (work[r][_trenc] == 1 && work[r-1][_trend] == 1) PlotPoint(i,jurUpa,jurUpb,jur); if (work[r][_trenc] == -1 && work[r-1][_trend] == -1) PlotPoint(i,jurDna,jurDnb,jur); } return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // string sTfTable[] = {"M1","M5","M10","M15","M30","H1","H4","D1","W1","MN"}; int iTfTable[] = {1,5,10,15,30,60,240,1440,10080,43200}; // // // // // int stringToTimeFrame(string tfs) { tfs = StringUpperCase(tfs); for (int i=ArraySize(iTfTable)-1; i>=0; i--) if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period())); return(Period()); } // // // // // string StringUpperCase(string str) { string s = str; for (int length=StringLen(str)-1; length>=0; length--) { int tchar = StringGetChar(s, length); if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256)) s = StringSetChar(s, length, tchar - 32); else if(tchar > -33 && tchar < 0) s = StringSetChar(s, length, tchar + 224); } return(s); } // // // // // double wrk[][20]; #define bsmax 5 #define bsmin 6 #define volty 7 #define vsum 8 #define avolty 9 // // // // // double iDSmooth(double price, double length, double phase, bool isDouble, int i, int s=0) { if (isDouble) return (iSmooth(iSmooth(price,MathSqrt(length),phase,i,s),MathSqrt(length),phase,i,s+10)); else return (iSmooth(price,length,phase,i,s)); } // // // // // double iSmooth(double price, double length, double phase, int i, int s=0) { if (length <=1) return(price); if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars); int r = Bars-i-1; if (r==0) { for(int k=0; k<7; k++) wrk[r][k+s]=price; for(; k<10; k++) wrk[r][k+s]=0; return(price); } // // // // // double len1 = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0); double pow1 = MathMax(len1-2.0,0.5); double del1 = price - wrk[r-1][bsmax+s]; double del2 = price - wrk[r-1][bsmin+s]; double div = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100); int forBar = MathMin(r,10); wrk[r][volty+s] = 0; if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty+s] = MathAbs(del1); if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty+s] = MathAbs(del2); wrk[r][vsum+s] = wrk[r-1][vsum+s] + (wrk[r][volty+s]-wrk[r-forBar][volty+s])*div; // // // // // wrk[r][avolty+s] = wrk[r-1][avolty+s]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum+s]-wrk[r-1][avolty+s]); if (wrk[r][avolty+s] > 0) double dVolty = wrk[r][volty+s]/wrk[r][avolty+s]; else dVolty = 0; if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1); if (dVolty < 1) dVolty = 1.0; // // // // // double pow2 = MathPow(dVolty, pow1); double len2 = MathSqrt(0.5*(length-1))*len1; double Kv = MathPow(len2/(len2+1), MathSqrt(pow2)); if (del1 > 0) wrk[r][bsmax+s] = price; else wrk[r][bsmax+s] = price - Kv*del1; if (del2 < 0) wrk[r][bsmin+s] = price; else wrk[r][bsmin+s] = price - Kv*del2; // // // // // double R = MathMax(MathMin(phase,100),-100)/100.0 + 1.5; double beta = 0.45*(length-1)/(0.45*(length-1)+2); double alpha = MathPow(beta,pow2); wrk[r][0+s] = price + alpha*(wrk[r-1][0+s]-price); wrk[r][1+s] = (price - wrk[r][0+s])*(1-beta) + beta*wrk[r-1][1+s]; wrk[r][2+s] = (wrk[r][0+s] + R*wrk[r][1+s]); wrk[r][3+s] = (wrk[r][2+s] - wrk[r-1][4+s])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3+s]; wrk[r][4+s] = (wrk[r-1][4+s] + wrk[r][3+s]); // // // // // return(wrk[r][4+s]); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // void CleanPoint(int i,double& first[],double& second[]) { if ((second[i] != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE)) second[i+1] = EMPTY_VALUE; else if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE)) first[i+1] = EMPTY_VALUE; } // // // // // void PlotPoint(int i,double& first[],double& second[],double& from[]) { if (first[i+1] == EMPTY_VALUE) { if (first[i+2] == EMPTY_VALUE) { first[i] = from[i]; first[i+1] = from[i+1]; second[i] = EMPTY_VALUE; } else { second[i] = from[i]; second[i+1] = from[i+1]; first[i] = EMPTY_VALUE; } } else { first[i] = from[i]; second[i] = EMPTY_VALUE; } }