KDJ背离预警 MT4外汇指标
#property copyright "Copyright? 2010,Sin.Petric"
#property indicator_separate_window #property indicator_buffers 6 #property indicator_color1 Red #property indicator_color2 Yellow #property indicator_color3 Green #property indicator_color4 Black #property indicator_color5 Red #property indicator_color6 Blue #property indicator_level1 0 #property indicator_level2 20 #property indicator_level3 80 #property indicator_level4 100 #property indicator_width5 2 #property indicator_width6 2
//---- input parameters
extern int nPeriod=20; extern double factor_1=0.6666666; extern double factor_2=0.3333333; //---- buffers
double percentK[];
double percentD[];
double percentJ[];
double RSV[];
double UpperFr[];
double LowerFr[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//IndicatorBuffers(4);
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,percentK);
SetIndexLabel(0, "%K");
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,percentD);
SetIndexLabel(1, "%D");
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,percentJ);
SetIndexLabel(2, "%J");
SetIndexBuffer(3,RSV);
SetIndexBuffer(4, UpperFr);
SetIndexBuffer(5, LowerFr);
//
SetIndexEmptyValue(4, 0);
SetIndexEmptyValue(5, 0);
//
SetIndexStyle(4, DRAW_ARROW);
SetIndexArrow(4, 241);
//
SetIndexStyle(5, DRAW_ARROW);
SetIndexArrow(5, 242);
//----
return(0);
}
//+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start()
{
int counted_bars=IndicatorCounted();
//----
int i, k, num;
double Ln, Hn, Cn;
//----
i = Bars - counted_bars - 1;
num = Bars - nPeriod;
while(i>=0)
{
Cn = iClose(NULL,0,i); Ln = iClose(NULL,0,i); Hn = iClose(NULL,0,i);
for(k=0; k
iLow(NULL,0,i+k)) Ln = iLow(NULL,0,i+k);
if (Hn < iHigh(NULL,0,i+k)) Hn = iHigh(NULL,0,i+k);
}
if (Hn-Ln != 0) RSV[i] = (Cn-Ln)/(Hn-Ln)*100; else RSV[i] = 50;
if (i >= num) {
percentK[i] = factor_1 * 50 + factor_2 * RSV[i];
percentD[i] = factor_1 * 50 + factor_2 * percentK[i];
} else {
percentK[i] = factor_1 * percentK[i+1] + factor_2 * RSV[i];
percentD[i] = factor_1 * percentD[i+1] + factor_2 * percentK[i];
}
percentJ[i] = 3 * percentD[i] - 2 * percentK[i];
if(percentJ[i]>percentK[i]&&percentJ[i+1]<=percentK[i+1]&&percentJ[i+1]-percentJ
[i]<10)
{
LowerFr[i]=percentJ[i];
Alert(TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS),",",Symbol(),",周
期:",period_pros(Period()),",KDJ背离,","卖出");
}
if(percentJ[i]=percentK[i+1]&&percentJ[i]-percentJ[i
+1]<10)
{
UpperFr[i]=percentJ[i];
Alert(TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS),",",Symbol(),",周
期:",period_pros(Period()),",KDJ背离,","买入");
}
i--;
}
return(0);
}
string period_pros(int per_t)
{
string alart_string="";
switch(per_t)
{
case 1:alart_string="M1";break;
case 5:alart_string="M5";break;
case 15:alart_string="M15";break;
case 30:alart_string="M30";break;
case 60:alart_string="H1";break;
case 240:alart_string="H4";break;
case 1440:alart_string="D1";break;
case 10080:alart_string="W1";break;
case 43200:alart_string="MN";break;
default:break;
}
return(alart_string);
}