#property copyright "KK" #property strict #property indicator_chart_window #property indicator_buffers 6 #property indicator_color1 Yellow // SSL Felső/Alsó váltó (pirosas jelleg az eredetiben) #property indicator_color2 Aqua // SSL Alsó/Felső váltó (zöldes jelleg az eredetiben) #property indicator_color3 White // Középvonal (EMA) #property indicator_style3 STYLE_DOT //+------------------------------------------------------------------+ //| INPUTS | [cite: 20] //+------------------------------------------------------------------+ extern int Price = PRICE_MEDIAN; extern double Deviations = 1.0; extern string ___UI___ = "=== PERIOD BUTTON UI ==="; extern int Button_X_Offset = 20; // [cite: 22] extern int Button_Y_Offset = 40; // [cite: 22] extern int Button_W = 110; // [cite: 23] extern int Button_H = 25; // [cite: 24] //+------------------------------------------------------------------+ //| GLOBALS | [cite: 25] //+------------------------------------------------------------------+ double buffer1[]; // Dinamikus SSL vonal 1 double buffer2[]; // Dinamikus SSL vonal 2 double buffer3[]; // Középvonal (EMA) [cite: 26] double ema1[]; // Munka buffer az iEDev-hez double ema2[]; // Munka buffer az iEDev-hez double Hlv[]; // SSL trend állapot buffer [cite: 4, 5] string ButtonName = "KK_EMA_Period_Btn"; string GVarName = "KK_EMA_Period_"; int PeriodOptions[3] = {5, 20, 50}; // [cite: 27] int LastPeriod = -1; bool ForceRecalc = false; // [cite: 28] //+------------------------------------------------------------------+ //| HELPERS | [cite: 29] //+------------------------------------------------------------------+ int GetCurrentPeriod() { if(!GlobalVariableCheck(GVarName)) GlobalVariableSet(GVarName, 5); int stored = (int)GlobalVariableGet(GVarName); for(int i = 0; i < 3; i++) // [cite: 31] if(stored == PeriodOptions[i]) return stored; GlobalVariableSet(GVarName, 5); return 5; // [cite: 32] } int NextPeriod(int current) { for(int i = 0; i < 3; i++) if(PeriodOptions[i] == current) return PeriodOptions[(i + 1) % 3]; return 5; // [cite: 33] } //+------------------------------------------------------------------+ //| UI FUNKCIÓK | [cite: 34] //+------------------------------------------------------------------+ void CreateButton() { if(ObjectFind(ButtonName) < 0) ObjectCreate(ButtonName, OBJ_BUTTON, 0, 0, 0); ObjectSetInteger(0, ButtonName, OBJPROP_XDISTANCE, Button_X_Offset); // [cite: 35] ObjectSetInteger(0, ButtonName, OBJPROP_YDISTANCE, Button_Y_Offset); ObjectSetInteger(0, ButtonName, OBJPROP_XSIZE, Button_W); ObjectSetInteger(0, ButtonName, OBJPROP_YSIZE, Button_H); // [cite: 36] ObjectSetInteger(0, ButtonName, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, ButtonName, OBJPROP_FONTSIZE, 9); // [cite: 37] ObjectSetInteger(0, ButtonName, OBJPROP_SELECTABLE, false); ObjectSetInteger(0, ButtonName, OBJPROP_STATE, false); UpdateButtonUI(); } void UpdateButtonUI() { int p = GetCurrentPeriod(); color bg; if(p == 5) bg = clrDodgerBlue; // [cite: 39] else if(p == 20) bg = clrDarkOrange; else bg = clrMediumSeaGreen; // [cite: 40] ObjectSetString(0, ButtonName, OBJPROP_TEXT, "Period: " + IntegerToString(p)); // [cite: 41] ObjectSetInteger(0, ButtonName, OBJPROP_BGCOLOR, bg); ObjectSetInteger(0, ButtonName, OBJPROP_COLOR, clrWhite); ChartRedraw(); // [cite: 42] } void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id == CHARTEVENT_OBJECT_CLICK && sparam == ButtonName) { int current = GetCurrentPeriod(); int next = NextPeriod(current); // [cite: 43] GlobalVariableSet(GVarName, next); UpdateButtonUI(); ObjectSetInteger(0, ButtonName, OBJPROP_STATE, false); ForceRecalc = true; LastPeriod = -1; // [cite: 44] IndicatorShortName("EMA SSL Dev [P=" + IntegerToString(next) + "]"); start(); // [cite: 45] } } //+------------------------------------------------------------------+ //| init | [cite: 46] //+------------------------------------------------------------------+ int init() { IndicatorBuffers(6); SetIndexBuffer(0, buffer1); SetIndexStyle(0, DRAW_LINE); SetIndexBuffer(1, buffer2); SetIndexStyle(1, DRAW_LINE); SetIndexBuffer(2, buffer3); SetIndexStyle(2, DRAW_LINE); SetIndexBuffer(3, ema1); SetIndexStyle(3, DRAW_NONE); SetIndexBuffer(4, ema2); SetIndexStyle(4, DRAW_NONE); SetIndexBuffer(5, Hlv); SetIndexStyle(5, DRAW_NONE); // SSL állapot buffer [cite: 6] GVarName += Symbol(); if(!GlobalVariableCheck(GVarName)) GlobalVariableSet(GVarName, 5); int p = GetCurrentPeriod(); IndicatorShortName("EMA SSL Dev [P=" + IntegerToString(p) + "]"); // [cite: 48] CreateButton(); // [cite: 49] return(0); } int deinit() { ObjectDelete(ButtonName); return(0); } // [cite: 50] //+------------------------------------------------------------------+ //| start | [cite: 51] //+------------------------------------------------------------------+ int start() { int Periods = GetCurrentPeriod(); if(Periods != LastPeriod || ForceRecalc) { LastPeriod = Periods; ForceRecalc = false; // [cite: 52] ArrayInitialize(buffer1, EMPTY_VALUE); ArrayInitialize(buffer2, EMPTY_VALUE); ArrayInitialize(buffer3, EMPTY_VALUE); ArrayInitialize(ema1, 0); ArrayInitialize(ema2, 0); ArrayInitialize(Hlv, 0); // [cite: 53] } int counted_bars = IndicatorCounted(); if(counted_bars < 0) return(-1); bool needFullCalc = (ema1[Bars - 1] == 0 && Bars > 1); // [cite: 54] int limit; if(needFullCalc || counted_bars == 0) limit = Bars - 2; // [cite: 55] else limit = Bars - counted_bars; // [cite: 56] if(limit >= Bars) limit = Bars - 2; double alpha = 2.0 / (1.0 + (double)Periods); // [cite: 57] for(int i = limit; i >= 0; i--) // [cite: 58] { double price = iMA(NULL, 0, 1, 0, MODE_SMA, Price, i); // Ártípus kezelése [cite: 7] // EMA és Deviáció számítás (v2 stílusú optimalizált iEDev) ema1[i] = ema1[i+1] + alpha * (price - ema1[i+1]); // [cite: 61] ema2[i] = ema2[i+1] + alpha * (price * price - ema2[i+1]); // [cite: 62] buffer3[i] = ema1[i]; // Középvonal az EMA [cite: 7] double var = (double)Periods * (ema2[i] - ema1[i] * ema1[i]) / ((double)Periods - 1.0); // [cite: 63] double dev = (var > 0.0) ? MathSqrt(var) : 0.0; // [cite: 64] double upperBand = buffer3[i] + Deviations * dev; // [cite: 8] double lowerBand = buffer3[i] - Deviations * dev; // [cite: 8] // --- SSL LOGIKA (v1-ből átemelve) --- [cite: 9] Hlv[i] = Hlv[i+1]; if(Close[i] > upperBand) Hlv[i] = 1; // [cite: 10] if(Close[i] < lowerBand) Hlv[i] = -1; // [cite: 10] if(Hlv[i] == -1) // Medve trend [cite: 11] { buffer1[i] = upperBand; // Sárga = Felső [cite: 12] buffer2[i] = lowerBand; // Aqua = Alsó [cite: 13] } else // Bika trend { buffer1[i] = lowerBand; // Sárga = Alsó [cite: 14] buffer2[i] = upperBand; // Aqua = Felső [cite: 15] } } return(0); }