/* https://www.mql5.com/zh/code/22976 */ //+------------------------------------------------------------------ #property copyright "© mladen, 2018" #property link "mladenfx@gmail.com" #property version "1.00" #property description "Trend direction and force" //+------------------------------------------------------------------ #property indicator_separate_window #property indicator_height 120 #property indicator_buffers 7 #property indicator_plots 1 #property indicator_label1 "Trend direction and force" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 clrDarkGray,clrRed,clrLime #property indicator_width1 3 // //--- input parameters // input int inpTrendPeriod = 34; // Trend period input double inpSmooth = 3; // Smoothing period input double inpTriggerUp = 0.05; // Trigger up level input double inpTriggerDown = -0.05; // Trigger down level //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 = "TRENDdsema"; // a button name input string btn_Font = "Arial"; // button font name input int btn_FontSize = 8; // 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 UniqueButtonID = "TrendDSEMA"; // Unique ID for each button input string button_note2 = "------------------------------"; bool show_data = true; string IndicatorName, IndicatorObjPrefix, buttonId; //Forex-Station copy & paste code; Button code end 11 // //--- buffers and global variables declarations // double val[],valc[],levup[],levdn[],mma[],smma[],tdf[],tdfa[],val1[],ª_alpha,ª_alpha2; int ª_maxPeriod; //+------------------------------------------------------------------------------------------------------------------+ //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 + "__"; IndicatorSetInteger(INDICATOR_DIGITS, _Digits); ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, 1); buttonId = IndicatorObjPrefix + UniqueButtonID; createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color); ObjectSetInteger(0, buttonId, OBJPROP_YDISTANCE, button_y); ObjectSetInteger(0, buttonId, OBJPROP_XDISTANCE, button_x); double ForexVal; if(GlobalVariableGet(buttonId + "_visibility", ForexVal)) show_data = ForexVal != 0; OnInit2(); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Custom indicator de-initialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(0, buttonId, -1, -1); } //+------------------------------------------------------------------------------------------------------------------+ void createButton(string buttonID, string buttonText, int width, int height, string font, int fontSize, color bgColor, color borderColor, color txtColor) { ObjectDelete (0, buttonID); ObjectCreate (0, buttonID, OBJ_BUTTON, btn_Subwindow, 0, 0); ObjectSetInteger(0, buttonID, OBJPROP_COLOR, txtColor); ObjectSetInteger(0, buttonID, OBJPROP_BGCOLOR, bgColor); ObjectSetInteger(0, buttonID, OBJPROP_BORDER_COLOR, borderColor); ObjectSetInteger(0, buttonID, OBJPROP_BORDER_TYPE, BORDER_RAISED); ObjectSetInteger(0, buttonID, OBJPROP_XSIZE, width); ObjectSetInteger(0, buttonID, OBJPROP_YSIZE, height); ObjectSetString (0, buttonID, OBJPROP_FONT, font); ObjectSetString (0, buttonID, OBJPROP_TEXT, buttonText); ObjectSetInteger(0, buttonID, OBJPROP_FONTSIZE, fontSize); ObjectSetInteger(0, buttonID, OBJPROP_SELECTABLE, 0); ObjectSetInteger(0, buttonID, OBJPROP_CORNER, btn_corner); ObjectSetInteger(0, buttonID, OBJPROP_HIDDEN, 1); ObjectSetInteger(0, buttonID, OBJPROP_XDISTANCE, 9999); ObjectSetInteger(0, buttonID, OBJPROP_YDISTANCE, 9999); } //+------------------------------------------------------------------------------------------------------------------+ void handleButtonClicks() { if(ObjectGetInteger(0, buttonId, OBJPROP_STATE)) { ObjectSetInteger(0, buttonId, OBJPROP_STATE, false); show_data = !show_data; GlobalVariableSet(buttonId + "_visibility", show_data ? 1.0 : 0.0); } } //+------------------------------------------------------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { handleButtonClicks(); if (GlobalVariableGet(buttonId + "_visibility") == 1.0) { ObjectSetInteger(0,buttonId,OBJPROP_COLOR,btn_text_ON_color); PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_LINE); show_data = true; Comment(""); //this line has to be here; otherwise this indicator doesn't work; so weird } else { ObjectSetInteger(0,buttonId,OBJPROP_COLOR,btn_text_OFF_color); for (int i=0; i1?inpSmooth:1)); ª_maxPeriod = 3*inpTrendPeriod; IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,inpTriggerUp); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,inpTriggerDown); // //--- // IndicatorSetString(INDICATOR_SHORTNAME,"Trend direction and force ("+(string)inpTrendPeriod+","+(string)(inpSmooth>1?inpSmooth:1)+")"); return (INIT_SUCCEEDED); } //------------------------------------------------------------------ // Custom indicator iteration function //------------------------------------------------------------------ // //--- // 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[]) { int i = (prev_calculated>0 ? prev_calculated-1 : 0); for (; i0) ? mma [i-1]+ª_alpha*(close[i]-mma [i-1]) : close[i]; smma[i] = (i>0) ? smma[i-1]+ª_alpha*(mma[i] -smma[i-1]) : mma[i]; double impetmma = (i>0) ? mma[i] - mma[i-1] : 0; double impetsmma = (i>0) ? smma[i] - smma[i-1] : 0; double divma = (mma[i]-smma[i]); if (divma<0) divma = -divma; double averimpet = (impetmma+impetsmma)/2.0; tdf[i] = divma*averimpet*averimpet*averimpet; tdfa[i] = tdf[i]>0 ? tdf[i] : -tdf[i]; // //--- // int _start = i-ª_maxPeriod+1; if (_start<0) _start=0; double _absValue = tdfa[ArrayMaximum(tdfa,_start,ª_maxPeriod)]; double _val1 = (_absValue>0) ? tdf[i]/_absValue : 0; val1[i] = (i>0) ? val1[i-1]+ª_alpha2*(_val1 -val1[i-1]) : 0; val[i] = (i>0) ? val[i-1] +ª_alpha2*(val1[i]-val [i-1]) : 0; valc[i] = (val[i] >inpTriggerUp) ? 2 : (val[i]