220 lines
No EOL
7.3 KiB
MQL4
220 lines
No EOL
7.3 KiB
MQL4
//+------------------------------------------------------------------+
|
|
//| KG SD Level V.1.1.mq4 |
|
|
//| Copyright © 2008|
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright © 2008"
|
|
#property link "www.freedom.net"
|
|
//----
|
|
#property indicator_chart_window
|
|
#property indicator_buffers 8
|
|
#property indicator_color1 DarkSlateGray
|
|
#property indicator_color2 DarkSlateGray
|
|
#property indicator_color3 DarkSlateGray
|
|
#property indicator_color4 DarkSlateGray
|
|
#property indicator_color5 DarkSlateGray
|
|
#property indicator_color6 DarkSlateGray
|
|
#property indicator_color7 DarkSlateGray
|
|
#property indicator_color8 DarkSlateGray
|
|
|
|
//---- buffers
|
|
double P1Buffer[];
|
|
double P2Buffer[];
|
|
double P3Buffer[];
|
|
double P4Buffer[];
|
|
double P5Buffer[];
|
|
double P6Buffer[];
|
|
double P7Buffer[];
|
|
double P8Buffer[];
|
|
//----
|
|
extern int myPeriod = PERIOD_D1;
|
|
extern double SD_1 = 0.5;
|
|
extern double SD_2 = 1.0;
|
|
extern double SD_3 = 1.5;
|
|
extern double SD_4 = 2.0;
|
|
//----
|
|
double H1, L1, H2, L2, H3, L3, H4, L4, Q;
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int init()
|
|
{
|
|
SetIndexBuffer(0, P1Buffer);
|
|
SetIndexBuffer(1, P2Buffer);
|
|
SetIndexBuffer(2, P3Buffer);
|
|
SetIndexBuffer(3, P4Buffer);
|
|
SetIndexBuffer(4, P5Buffer);
|
|
SetIndexBuffer(5, P6Buffer);
|
|
SetIndexBuffer(6, P7Buffer);
|
|
SetIndexBuffer(7, P8Buffer);
|
|
//----
|
|
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID);
|
|
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID);
|
|
SetIndexStyle(2, DRAW_LINE, STYLE_SOLID);
|
|
SetIndexStyle(3, DRAW_LINE, STYLE_SOLID);
|
|
SetIndexStyle(4, DRAW_LINE, STYLE_SOLID);
|
|
SetIndexStyle(5, DRAW_LINE, STYLE_SOLID);
|
|
SetIndexStyle(6, DRAW_LINE, STYLE_SOLID);
|
|
SetIndexStyle(7, DRAW_LINE, STYLE_SOLID);
|
|
//----
|
|
SetIndexLabel(0, "Dev +" + SD_1);
|
|
SetIndexLabel(1, "Dev -" + SD_1);
|
|
SetIndexLabel(2, "Dev +" + SD_2);
|
|
SetIndexLabel(3, "Dev -" + SD_2);
|
|
SetIndexLabel(4, "Dev +" + SD_3);
|
|
SetIndexLabel(5, "Dev -" + SD_3);
|
|
SetIndexLabel(6, "Dev +" + SD_4);
|
|
SetIndexLabel(7, "Dev -" + SD_4);
|
|
return(0);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Custor indicator deinitialization function |
|
|
//+------------------------------------------------------------------+
|
|
int deinit()
|
|
{
|
|
ObjectDelete("HSD_0.5");
|
|
ObjectDelete("HSD_1.0");
|
|
ObjectDelete("HSD_1.5");
|
|
ObjectDelete("HSD_2.0");
|
|
|
|
ObjectDelete("LSD_0.5");
|
|
ObjectDelete("LSD_1.0");
|
|
ObjectDelete("LSD_1.5");
|
|
ObjectDelete("LSD_2.0");
|
|
|
|
ObjectDelete("txtLSD_0.5");
|
|
ObjectDelete("txtLSD_1.0");
|
|
ObjectDelete("txtLSD_1.5");
|
|
ObjectDelete("txtLSD_2.0");
|
|
|
|
ObjectDelete("txtHSD_0.5");
|
|
ObjectDelete("txtHSD_1.0");
|
|
ObjectDelete("txtHSD_1.5");
|
|
ObjectDelete("txtHSD_2.0");
|
|
Comment("Kg Standard Deviation Level");
|
|
return(0);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator iteration function |
|
|
//+------------------------------------------------------------------+
|
|
int start()
|
|
{
|
|
int i, dayi, counted_bars = IndicatorCounted();
|
|
double L,H,O,C,S,P2,P3,P4,P5,PA;
|
|
//---- check for possible errors
|
|
if(counted_bars < 0)
|
|
return(-1);
|
|
//---- last counted bar will be recounted
|
|
if(counted_bars > 0)
|
|
counted_bars--;
|
|
int limit = Bars - counted_bars;
|
|
//----
|
|
for(i = limit - 1; i >= 0; i--)
|
|
{
|
|
dayi = iBarShift(Symbol(), myPeriod, Time[i], false);
|
|
Q = (iHigh(Symbol(), myPeriod,dayi+1) - iLow(Symbol(),myPeriod,dayi+1));
|
|
|
|
L = iLow(NULL,myPeriod,dayi+1);
|
|
H = iHigh(NULL,myPeriod,dayi+1);
|
|
O = iOpen(NULL,myPeriod,dayi+1);
|
|
C = iClose(NULL,myPeriod,dayi+1);
|
|
|
|
// Deviations
|
|
S = MathPow(C-((C+C+L+H)/4),2)+MathPow(C-((C+C+L+H)/4),2)+MathPow(L-((C+C+L+H)/4),2)+MathPow(H-((C+C+L+H)/4),2);
|
|
//sum+=S*S;
|
|
P5=SD_4*MathSqrt(S/3);
|
|
P4=SD_3*MathSqrt(S/3);
|
|
P3=SD_2*MathSqrt(S/3);
|
|
P2=SD_1*MathSqrt(S/3);
|
|
|
|
PA = (C+C+L+H)/4;
|
|
|
|
|
|
//----
|
|
H1 = PA + P2;
|
|
L1 = PA - P2;
|
|
//----
|
|
H2 = PA + P3;
|
|
L2 = PA - P3;
|
|
//----
|
|
H3 = PA + P4;
|
|
L3 = PA - P4;
|
|
//----
|
|
H4 = PA + P5;
|
|
L4 = PA - P5;
|
|
//----
|
|
P1Buffer[i] = H1;
|
|
SetPrice("HSD_0.5", Time[i], H1, DarkSlateGray);
|
|
SetText("txtHSD_0.5", DoubleToStr(SD_1,1), Time[i], H1, DarkSlateGray);
|
|
//----
|
|
P2Buffer[i] = L1;
|
|
SetPrice("LSD_0.5", Time[i], L1, DarkSlateGray);
|
|
SetText("txtLSD_0.5", DoubleToStr(SD_1,1), Time[i], L1, DarkSlateGray);
|
|
//----
|
|
P3Buffer[i] = H2;
|
|
SetPrice("HSD_1.0", Time[i], H2, DarkSlateGray);
|
|
SetText("txtHSD_1.0", DoubleToStr(SD_2,1), Time[i], H2, DarkSlateGray);
|
|
//----
|
|
P4Buffer[i] = L2;
|
|
SetPrice("LSD_1.0",Time[i],L2,DarkSlateGray);
|
|
SetText("txtLSD_1.0",DoubleToStr(SD_2,1),Time[i],L2,DarkSlateGray);
|
|
//----
|
|
P5Buffer[i] = H3;
|
|
SetPrice("HSD_1.5", Time[i], H3, DarkSlateGray);
|
|
SetText("txtHSD_1.5", DoubleToStr(SD_3,1), Time[i], H3, DarkSlateGray);
|
|
//----
|
|
P6Buffer[i] = L3;
|
|
SetPrice("LSD_1.5", Time[i], L3, DarkSlateGray);
|
|
SetText("txtLSD_1.5", DoubleToStr(SD_3,1), Time[i], L3, DarkSlateGray);
|
|
//----
|
|
P7Buffer[i] = H4;
|
|
SetPrice("HSD_2.0", Time[i], H4, DarkSlateGray);
|
|
SetText("txtHSD_2.0", DoubleToStr(SD_4,1), Time[i], H4, DarkSlateGray);
|
|
//----
|
|
P8Buffer[i] = L4;
|
|
SetPrice("LSD_2.0", Time[i], L4, DarkSlateGray);
|
|
SetText("txtLSD_2.0", DoubleToStr(SD_4,1), Time[i], L4, DarkSlateGray);
|
|
|
|
}
|
|
//----
|
|
return(0);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void SetPrice(string name, datetime Tm, double Prc, color clr)
|
|
{
|
|
if(ObjectFind(name) == -1)
|
|
{
|
|
ObjectCreate(name, OBJ_ARROW, 0, Tm, Prc);
|
|
ObjectSet(name, OBJPROP_COLOR, clr);
|
|
ObjectSet(name, OBJPROP_WIDTH, 1);
|
|
ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
|
|
}
|
|
else
|
|
{
|
|
ObjectSet(name, OBJPROP_TIME1, Tm);
|
|
ObjectSet(name, OBJPROP_PRICE1, Prc);
|
|
ObjectSet(name, OBJPROP_COLOR, clr);
|
|
ObjectSet(name, OBJPROP_WIDTH, 1);
|
|
ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void SetText(string name, string txt, datetime Tm, double Prc, color clr)
|
|
{
|
|
if(ObjectFind(name) == -1)
|
|
{
|
|
ObjectCreate(name, OBJ_TEXT, 0, Tm, Prc);
|
|
ObjectSetText(name, txt, 7, "Times New Roman", clr);
|
|
}
|
|
else
|
|
{
|
|
ObjectSet(name, OBJPROP_TIME1, Tm);
|
|
ObjectSet(name, OBJPROP_PRICE1, Prc);
|
|
ObjectSetText(name, txt, 7, "Times New Roman", clr);
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+ |