Maintain value for drawdown after changing input

 

I have this code, which I'm must trying to show the daily drawdown. It generally works, but if the user goes in to change an input value, it resets the drawdown value.

How can I get it to maintain the drawdown value?


//+------------------------------------------------------------------+
//|                                                      dd Test.mq4 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//===Choose to show or hide Menu
#define SHOW 0
#define HIDE 1
enum  SH { Show,Hide };
//---Expert Proprities
extern SH     ShowMenu    = SHOW;
extern color  MenuColor   = clrBlack;
extern color  FrameColor  = clrWhite;
enum pos {
   tl=CORNER_LEFT_UPPER,     // Top Left
   tr=CORNER_RIGHT_UPPER,    // Top Right
   bl=CORNER_LEFT_LOWER,     // Botom Left
   br=CORNER_RIGHT_LOWER,    // Bottom Right
};
extern pos position=tl;  // Starting Corner
extern int hOffset=0;    // Horizontal Offset
extern int vOffset=0;     // Vertical Offset
extern double acctBal=0; // Account Start Balance

//---initialize global variables
double dayBal = AccountBalance();
double dayLow;
double currProfit;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   DrawMenu();
//   }
//----
   return(0);
}
//+------------------------------------------------------------------+y
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
   if(Hour()==00 && Minute()==00) {
      dayBal = AccountBalance();
      dayLow = 0;
   }

   currProfit = AccountProfit();
   
   if (currProfit < 0.0 && currProfit < dayLow) {
      dayLow = currProfit;
   }

//---Show or hide Menu

   ReDrawMenu();

   return(0);
}
//+------------------------------------------------------------------+
//| DRAW MENU OBJECTS FUNCTIONS                                      |
//+------------------------------------------------------------------+

//--- Function to draw Menu for the first time
bool DrawMenu() {
   string  MenuFont = "Consolas";
   int     MenuFontSize = 9;

//--- Create Labels Cells
   ObjectCreate("FrameLabel",OBJ_RECTANGLE_LABEL,0,0,0,0,0,0);
   ObjectCreate("acctBalLabel",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("dayBalLabel",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("dayLowLabel",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("currProfitLabel",OBJ_LABEL,0,0,0,0,0);

//---Modify Label Cells

//****
   
   ObjectSet("FrameLabel",OBJPROP_BGCOLOR,FrameColor);
   ObjectSet("FrameLabel",OBJPROP_CORNER,0);
//   ObjectSet("FrameLabel",OBJPROP_BACK,true);
   ObjectSet("FrameLabel",OBJPROP_XDISTANCE,hOffset);
   ObjectSet("FrameLabel",OBJPROP_YDISTANCE,vOffset);
   ObjectSet("FrameLabel",OBJPROP_XSIZE,500);
   ObjectSet("FrameLabel",OBJPROP_YSIZE,400);
      
//***
     
   ObjectSetText(     "acctBalLabel", "Account Balance" , MenuFontSize, MenuFont, MenuColor);
   ObjectSet("acctBalLabel",OBJPROP_XDISTANCE,hOffset+10);     
   ObjectSet("acctBalLabel",OBJPROP_YDISTANCE,vOffset);
   ObjectSet("acctBalLabel",OBJPROP_CORNER,0);
          
   ObjectSetText(     "dayBalLabel", "Day Balance" , MenuFontSize, MenuFont, MenuColor);
   ObjectSet("dayBalLabel",OBJPROP_XDISTANCE,hOffset+10);     
   ObjectSet("dayBalLabel",OBJPROP_YDISTANCE,vOffset+30);
   ObjectSet("dayBalLabel",OBJPROP_CORNER,0);
          
   ObjectSetText(     "dayLowLabel", "Day Low" , MenuFontSize, MenuFont, MenuColor);
   ObjectSet("dayLowLabel",OBJPROP_XDISTANCE,hOffset+10);     
   ObjectSet("dayLowLabel",OBJPROP_YDISTANCE,vOffset+120);
   ObjectSet("dayLowLabel",OBJPROP_CORNER,0);
          
   ObjectSetText(     "currProfitLabel", "Profit" , MenuFontSize, MenuFont, MenuColor);
   ObjectSet("currProfitLabel",OBJPROP_XDISTANCE,hOffset+10);     
   ObjectSet("currProfitLabel",OBJPROP_YDISTANCE,vOffset+180);
   ObjectSet("currProfitLabel",OBJPROP_CORNER,0);
          
//--- Create Numerical Value Cells  
   if(ObjectFind(0,"acctBalVal") < 0)
      ObjectCreate("acctBalVal",OBJ_LABEL,0,0,0,0,0);
   if(ObjectFind(0,"dayBalVal") < 0)
      ObjectCreate("dayBalVal",OBJ_LABEL,0,0,0,0,0);
   if(ObjectFind(0,"dayLowVal") < 0)
      ObjectCreate("dayLowVal",OBJ_LABEL,0,0,0,0,0);
   if(ObjectFind(0,"currProfitVal") < 0)
      ObjectCreate("currProfitVal",OBJ_LABEL,0,0,0,0,0);

//--- Modify numerical Value Cells                   
//   ObjectSetText("acctBalVal",rightAlign(acctBal,9,2), MenuFontSize, MenuFont,MenuColor);
   ObjectSet("acctBalVal",OBJPROP_XDISTANCE, hOffset+340);     
   ObjectSet("acctBalVal",OBJPROP_YDISTANCE,vOffset);
   ObjectSet("acctBalVal",OBJPROP_CORNER,0);
     
//   ObjectSetText("dayBalVal",rightAlign(dayBal,9,2), MenuFontSize, MenuFont,MenuColor);
   ObjectSet("dayBalVal",OBJPROP_XDISTANCE,hOffset+340);     
   ObjectSet("dayBalVal",OBJPROP_YDISTANCE,vOffset+30);
   ObjectSet("dayBalVal",OBJPROP_CORNER,0);
              
//   ObjectSetText("dayLowVal",rightAlign(dayLow,9,2), MenuFontSize, MenuFont,MenuColor);
   ObjectSet("dayLowVal",OBJPROP_XDISTANCE,hOffset+340);     
   ObjectSet("dayLowVal",OBJPROP_YDISTANCE,vOffset+120);
   ObjectSet("dayLowVal",OBJPROP_CORNER,0);
    
//   ObjectSetText("currProfitVal",rightAlign(currProfit,9,2), MenuFontSize, MenuFont,MenuColor);
   ObjectSet("currProfitVal",OBJPROP_XDISTANCE,hOffset+340);     
   ObjectSet("currProfitVal",OBJPROP_YDISTANCE,vOffset+180);
   ObjectSet("currProfitVal",OBJPROP_CORNER,0);

   return(0);
}

//--- Function to redraw Menu on each tick
bool ReDrawMenu() {
   string  MenuFont = "Consolas";
   int     MenuFontSize = 9;
            
   ObjectSetText("acctBalVal",rightAlign(acctBal,9,2), MenuFontSize, MenuFont,MenuColor);
   ObjectSetText("dayBalVal",rightAlign(dayBal,9,2), MenuFontSize, MenuFont,MenuColor);
   ObjectSetText("dayLowVal",rightAlign(dayLow,9,2), MenuFontSize, MenuFont,MenuColor);
   ObjectSetText("currProfitVal",rightAlign(currProfit,9,2), MenuFontSize, MenuFont,MenuColor);

   return(0);
}

string formatDouble(double number, int precision, string pcomma=",", string ppoint=".") {
   string snum   = DoubleToStr(number,precision);
   int    decp   = StringFind(snum,".",0);
   string sright = StringSubstr(snum,decp+1,precision);
   string sleft  = StringSubstr(snum,0,decp);
   string formated = "";
   string comma    = "";
   
      while (StringLen(sleft)>3)
      {
         int    length = StringLen(sleft);
         string part   = StringSubstr(sleft,length-3,0);
              formated = part+comma+formated;
              comma    = pcomma;
              sleft    = StringSubstr(sleft,0,length-3);
      }
      if (sleft=="-")  comma=""; // this line missing previously
      if (sleft!="")   formated = sleft+comma+formated;
      if (precision>0) formated = formated+ppoint+sright;
   return(formated);
} 

string rightAlign (double varToAlign, int numChar, int numDec) {
   string textOut=formatDouble(varToAlign, numDec);
   string blanks="";
   for(int f=0;f<=numChar-StringLen(textOut);f++) blanks=" " + blanks;
   textOut=blanks+textOut;         
   return (textOut);
}
 
rwh0965: I have this code, which I'm must trying to show the daily drawdown. It generally works, but if the user goes in to change an input value, it resets the drawdown value. How can I get it to maintain the drawdown value?
To maintain any persistent variables that need to survive an indicator or EA restart, save them to Global Terminal Variables or save (and restore) the data to (from) a file.
Documentation on MQL5: Global Variables of the Terminal
Documentation on MQL5: Global Variables of the Terminal
  • www.mql5.com
Global Variables of the Terminal - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:
To maintain any persistent variables that need to survive an indicator or EA restart, save them to Global Terminal Variables or save (and restore) the data to (from) a file.

That's exactly what I needed. 

Thanks!