// https://www.mql5.com/en/code/23756 // https://forex-station.com/viewtopic.php?p=1295468075#p1295468075 //------------------------------------------------------------------ #property copyright "© mladen, 2018" #property link "mladenfx@gmail.com" #property description "AMA filtered" //+------------------------------------------------------------------ #property indicator_chart_window #property indicator_buffers 3 #property indicator_plots 1 #property indicator_label1 "AMA" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 clrDarkGray,clrMediumSeaGreen,clrOrangeRed #property indicator_width1 2 // // // input int inpPeriod = 17; // Period input int inpFastPeriod = 3; // Fast end period input int inpSlowPeriod = 34; // Slow end period input double inpFilter = 5; // Filter size (<=0 for no filter) input ENUM_APPLIED_PRICE inpPrice = PRICE_CLOSE; // Price // //--- // //Forex-Station copy & paste code; Button code start 11 input string button_note1 = "------------------------------"; input int btn_Subwindow = 0; // What window to put the button on input ENUM_BASE_CORNER btn_corner = CORNER_LEFT_UPPER; // button corner on chart for anchoring input string btn_text = "AMA"; // a button name input string btn_Font = "Arial"; // button font name input int btn_FontSize = 9; // button font size input color btn_text_ON_color = clrLime; // ON color when the button is turned on input color btn_text_OFF_color = clrRed; // OFF color when the button is turned off input color btn_background_color = clrDimGray; // background color of the button input color btn_border_color = clrBlack; // border color the button input int button_x = 20; // x coordinate of the button input int button_y = 25; // y coordinate of the button input int btn_Width = 80; // button width input int btn_Height = 20; // button height input string soundBT = "tick.wav"; // sound file when the button is pressed input string UniqueButtonID = "AMAfiltered"; // Unique ID for each button input string button_note2 = "------------------------------"; bool show_data = true, recalc = true; string IndicatorName, IndicatorObjPrefix, buttonId; //Forex-Station copy & paste code; Button code end 11 double val[],valc[],ama[],g_fastEnd,g_slowEnd; int g_hlPeriod; //+------------------------------------------------------------------+ //Forex-Station copy & paste code; Button code start 12 string GenerateIndicatorName(const string target) { string name = target; int try = 2; while(ChartWindowFind(0, name) != -1) { name = target + " #" + IntegerToString(try++); } return name; } //+------------------------------------------------------------------+ int OnInit(void) { IndicatorName = GenerateIndicatorName(btn_text); IndicatorObjPrefix = "__" + IndicatorName + "__"; IndicatorSetString(INDICATOR_SHORTNAME, IndicatorName); IndicatorSetInteger(INDICATOR_DIGITS, _Digits); double ForexVal; if(GlobalVariableGet(IndicatorName + "_visibility", ForexVal)) show_data = ForexVal != 0; ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1); buttonId = IndicatorObjPrefix + UniqueButtonID + btn_text; createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color); ObjectSetInteger(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y); ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x); Init2(); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(ChartID(), IndicatorObjPrefix, -1, -1); } //+------------------------------------------------------------------+ void createButton(string buttonID, string buttonText, int width, int height, string font, int fontSize, color bgColor, color borderColor, color txtColor) { ObjectDelete (ChartID(), buttonID); ObjectCreate (ChartID(), buttonID, OBJ_BUTTON, btn_Subwindow, 0, 0); ObjectSetInteger(ChartID(), buttonID, OBJPROP_COLOR, txtColor); ObjectSetInteger(ChartID(), buttonID, OBJPROP_BGCOLOR, bgColor); ObjectSetInteger(ChartID(), buttonID, OBJPROP_BORDER_COLOR, borderColor); ObjectSetInteger(ChartID(), buttonID, OBJPROP_BORDER_TYPE, BORDER_RAISED); ObjectSetInteger(ChartID(), buttonID, OBJPROP_XSIZE, width); ObjectSetInteger(ChartID(), buttonID, OBJPROP_YSIZE, height); ObjectSetString (ChartID(), buttonID, OBJPROP_FONT, font); ObjectSetString (ChartID(), buttonID, OBJPROP_TEXT, buttonText); ObjectSetInteger(ChartID(), buttonID, OBJPROP_FONTSIZE, fontSize); ObjectSetInteger(ChartID(), buttonID, OBJPROP_SELECTABLE, 0); ObjectSetInteger(ChartID(), buttonID, OBJPROP_CORNER, btn_corner); ObjectSetInteger(ChartID(), buttonID, OBJPROP_HIDDEN, 1); ObjectSetInteger(ChartID(), buttonID, OBJPROP_XDISTANCE, 9999); ObjectSetInteger(ChartID(), buttonID, OBJPROP_YDISTANCE, 9999); } //+------------------------------------------------------------------------------------------------------------------+ void handleButtonClicks() { if(ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE)) { ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false); show_data = !show_data; GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0); recalc = true; } } //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { handleButtonClicks(); bool ForexStation = ObjectGetInteger(ChartID(),sparam,OBJPROP_TYPE); if (id==CHARTEVENT_OBJECT_CLICK && ForexStation==OBJ_BUTTON) { if (soundBT!="") PlaySound(soundBT); } if (show_data) { ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color); PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_LINE); } else { ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color); PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_NONE); } } //+------------------------------------------------------------------+ //Forex-Station copy & paste code; Button code end 12 //------------------------------------------------------------------ // Custom indicator initialization function //------------------------------------------------------------------ // // // int Init2() { // //--- indicator buffers mapping // SetIndexBuffer(0,val ,INDICATOR_DATA); SetIndexBuffer(1,valc,INDICATOR_COLOR_INDEX); SetIndexBuffer(2,ama ,INDICATOR_CALCULATIONS); g_fastEnd = 2.0/(inpFastPeriod+1.0); g_slowEnd = 2.0/(inpSlowPeriod+1.0); g_hlPeriod = inpPeriod+1; iFilter.init(inpPeriod,inpFilter); return (INIT_SUCCEEDED); } //------------------------------------------------------------------ // Custom indicator iteration function //------------------------------------------------------------------ // //--- // #define _setPrice(_priceType,_target,_index) \ { \ switch(_priceType) \ { \ case PRICE_CLOSE: _target = close[_index]; break; \ case PRICE_OPEN: _target = open[_index]; break; \ case PRICE_HIGH: _target = high[_index]; break; \ case PRICE_LOW: _target = low[_index]; break; \ case PRICE_MEDIAN: _target = (high[_index]+low[_index])/2.0; break; \ case PRICE_TYPICAL: _target = (high[_index]+low[_index]+close[_index])/3.0; break; \ case PRICE_WEIGHTED: _target = (high[_index]+low[_index]+close[_index]+close[_index])/4.0; break; \ default : _target = 0; \ }} // //--- // int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { static int prev_i=-1; static double prev_max,prev_min; // // // int i= prev_calculated-1; if (i<0) i=0; for (; i prev_max) ? high[i] : prev_max; double min = (low[i] < prev_min) ? low[i] : prev_min; // //--- // double mltp = (max!=min) ? ((_price-min)-(max-_price))/(max-min) : 1; if (mltp<0) mltp *= -1; double ssc = mltp * ( g_fastEnd-g_slowEnd) + g_slowEnd; // //--- // ama[i] = (i>0) ? ama[i-1]+(ssc*ssc)*(_price-ama[i-1]) : _price; val[i] = iFilter.calculate(ama[i],i); valc[i]=(i>0) ?(val[i]>val[i-1]) ? 1 :(val[i]1) ? period : 1; m_filter = (filter>=0) ? filter : 0; m_arraySize = m_period + 32; if (ArrayResize(m_array,m_arraySize)!=m_arraySize) return(false); return(true); } double calculate(double value, int i) { int _indC = (i )%m_arraySize; int _indP = (i-1)%m_arraySize; // // // m_array[_indC].value = value; double _change = (i>0) ? m_array[_indC].value-m_array[_indP].value : 0; m_array[_indC].change = (_change>0) ? _change : - _change; if (i>m_period) { int _indF = (i-m_period)%m_arraySize; #define _power(_val) ((_val)*(_val)) m_array[_indC].summc = m_array[_indP].summc +m_array[_indC].change-m_array[_indF].change; m_array[_indC].power = _power(m_array[_indC].change-m_array[_indC].summc/(double)m_period); m_array[_indC].summp = m_array[_indP].summp+m_array[_indC].power-m_array[_indF].power; } else { m_array[_indC].summc = m_array[_indC].summp = 0; for(int k=0; k=k; k++) m_array[_indC].summc += m_array[_indC-k].change; m_array[_indC].power = _power(m_array[_indC].change-m_array[_indC].summc/(double)m_period); for(int k=0; k=k; k++) m_array[_indC].summp += m_array[_indC-k].power; #undef _power } if (i>0 && m_filter>0 && m_array[_indC].change