/* https://www.mql5.com/zh/code/21869 */ //+------------------------------------------------------------------ #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 4 #property indicator_plots 3 #property indicator_label1 "No trend zone" #property indicator_type1 DRAW_FILLING #property indicator_color1 clrDimGray #property indicator_label2 "Trend direction and force" #property indicator_type2 DRAW_COLOR_LINE #property indicator_color2 clrDarkGray,clrRed,clrLime #property indicator_width2 3 // //--- input parameters // input int trendPeriod = 34; // Trend period input double TriggerUp = 0.05; // Trigger up level input double TriggerDown = -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 = "TREND"; // 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 = "TrendDirectionForce"; // 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[]; //+------------------------------------------------------------------------------------------------------------------+ //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_FILLING); PlotIndexSetInteger(1, 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; i0) ? workTrend[i-1][_MMA]+_alpha*(close[i]-workTrend[i-1][_MMA]) : close[i]; workTrend[i][_SMMA] = (i>0) ? workTrend[i-1][_SMMA]+_alpha*(workTrend[i][_MMA]-workTrend[i-1][_SMMA]) : workTrend[i][_MMA]; double impetmma = (i>0) ? workTrend[i][_MMA] - workTrend[i-1][_MMA] : 0; double impetsmma = (i>0) ? workTrend[i][_SMMA] - workTrend[i-1][_SMMA] : 0; double divma = MathAbs(workTrend[i][_MMA]-workTrend[i][_SMMA])/_Point; double averimpet = (impetmma+impetsmma)/(2*_Point); workTrend[i][_TDF] = divma*MathPow(averimpet,3); // //--- // double absValue = 0; for (int k=0; k=0; k++) absValue = MathMax(absValue,MathAbs(workTrend[i-k][_TDF])); val[i] = (absValue > 0) ? workTrend[i][_TDF]/absValue : 0; levup[i] = TriggerUp; levdn[i] = TriggerDown; valc[i] = (val[i] > levup[i]) ? 2 : (val[i] < levdn[i]) ? 1 : 0; } return (i); } //+------------------------------------------------------------------+