//+------------------------------------------------------------------+ //| test.mq4 | //| Copyright 2017, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2017, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict #define Name WindowExpertName() int CheckSeconds = 1; // or whatever double equityhigh; double equitylow; double meqh, meql; int maxdd = 10; // 10% //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- set GVs if (GlobalVariableCheck("eqhigh") == false) GlobalVariableSet("eqhigh", AccountEquity()); equityhigh = GlobalVariableGet("eqhigh"); if (GlobalVariableCheck("eqlow") == false) GlobalVariableSet("eqlow", AccountEquity()); equitylow = GlobalVariableGet("eqlow"); if (GlobalVariableCheck("meqh") == false) GlobalVariableSet("meqh", equityhigh); meqh = GlobalVariableGet("meqh"); if (GlobalVariableCheck("meql") == false) GlobalVariableSet("meql", equitylow); meql = GlobalVariableGet("meql"); //--- create timer EventSetTimer(60); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(0, Name); //--- destroy timer EventKillTimer(); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { bool new_check = false; static datetime recheck_time = 0; if (TimeCurrent() >= recheck_time + (CheckSeconds)) { new_check = true; recheck_time = TimeCurrent(); } if (new_check) { if (OrdersTotal() == 0) { equityhigh = AccountBalance(); GlobalVariableSet("eqlow", equityhigh); equitylow = AccountBalance(); GlobalVariableSet("eqhigh", equitylow); ObjectsDeleteAll(0, Name + "mx"); } else if (OrdersTotal() > 0) { if (AccountEquity() > equityhigh) { equityhigh = AccountEquity(); GlobalVariableSet("eqhigh", equityhigh); } if (AccountEquity() < equitylow) { equitylow = AccountEquity(); GlobalVariableSet("eqlow", equitylow); } if (AccountEquity() > meqh) { meqh = AccountEquity(); GlobalVariableSet("meqh", meqh); } if (AccountEquity() < meql) { meql = AccountEquity(); GlobalVariableSet("meql", meql); } GlobalVariablesFlush(); new_check = false; } } eqshow(); tradecheck(); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { //--- } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { {//Reset if (id == CHARTEVENT_KEYDOWN) { if (lparam == StringGetChar("R", 0)) { equityhigh = AccountBalance(); GlobalVariableDel("eqhigh"); GlobalVariableSet("eqhigh", equityhigh); equitylow = AccountBalance(); GlobalVariableDel("eqlow"); GlobalVariableSet("eqlow", equitylow); GlobalVariablesFlush(); } if (lparam == StringGetChar("M", 0)) { string obname; obname = Name + "meqh"; LabelMake(obname, 1, 200, 110, "Max Equity: " + DoubleToStr(GlobalVariableGet("meqh"), 2), 9, clrGreen); obname = Name + "meql"; LabelMake(obname, 1, 200, 120, "Min Equity: " + DoubleToStr(GlobalVariableGet("meql"), 2), 9, clrMagenta); } if (lparam == StringGetChar("N", 0)) { ObjectsDeleteAll(0, Name + "meq"); } } } //--- } //+------------------------------------------------------------------+ //+TEST SHOW LABELS--------------------------------------------------+ void eqshow() { string obname; obname = Name + "eqh"; LabelMake(obname, 1, 200, 15, "Cur Equity max: " + DoubleToStr(GlobalVariableGet("eqhigh"), 2), 8, clrBlue); obname = Name + "bal"; LabelMake(obname, 1, 200, 25, "Balance: " + DoubleToStr(AccountBalance(), 2), 8, clrBlack); obname = Name + "eqm"; LabelMake(obname, 1, 200, 35, "Cur Equity mid: " + DoubleToStr((GlobalVariableGet("eqhigh") + GlobalVariableGet("eqlow")) / 2, 2), 8, clrWhite); obname = Name + "eql"; LabelMake(obname, 1, 200, 45, "Cur Equity min: " + DoubleToStr(GlobalVariableGet("eqlow"), 2), 8, clrRed); if (OrdersTotal() > 0) obname = Name + "mxp"; LabelMake(obname, 1, 200, 60, "Max Profit: " + DoubleToStr(GlobalVariableGet("eqhigh") - AccountBalance(), 2), 8, clrBlue); if (OrdersTotal() > 0) obname = Name + "mxl"; LabelMake(obname, 1, 200, 70, "Max Loss: " + DoubleToStr(AccountBalance() - GlobalVariableGet("eqlow"), 2), 8, clrRed); obname = Name + "mdd"; LabelMake(obname, 1, 200, 85, "Max DD all: " + DoubleToStr((equityhigh * maxdd) / 100, 2), 9, clrRed); obname = Name + "tpl"; LabelMake(obname, 1, 200, 95, "Total P/L: " + DoubleToStr(totalpl(), 2), 9, clrBlue); if (totalpl() < 0) ObjectSetInteger(0, obname, OBJPROP_COLOR, clrRed); } //+------------------------------------------------------------------+ //+SL WATCHDOG-------------------------------------------------------+ void tradecheck() { bool opentrades = false; double maxloss = -((equityhigh * maxdd) / 100); if (totalpl() <= maxloss) Print("CLOSE!"); } //+------------------------------------------------------------------+ //+P/L CALC----------------------------------------------------------+ double totalpl() { double total = 0; for (int i = OrdersTotal(); i >= 0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { total += OrderProfit() + OrderSwap() + OrderCommission(); } return (total); } //+------------------------------------------------------------------+ //+LABELMAKE FUNCTION------------------------------------------------+ void LabelMake(const string name, const int corner, const int x, const int y, const string label, const int FSize, const color FCol) { if (ObjectFind(0, name) < 0) if (!ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0)) { Print("error: can't create label_object! code #", GetLastError()); } ObjectSetInteger(0, name, OBJPROP_CORNER, corner); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y); ObjectSetText(name, label, FSize, "Arial", FCol); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_READONLY, true); ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); ObjectSetString(0, name, OBJPROP_TOOLTIP, "\n"); } //Create live labels for each call - refresh is automatic depending on oninit / oncalculate / timer //+------------------------------------------------------------------+