Persistent Variable After Input CHange

 

I have an indicator that tracks max drawdown during the day.

I have it displayed on the chart with a text object.

Problem is, that if the user changes an input in the indicator (to move the text to a different corner), I have to delete the text object and then it loses it's value.

How can I create a variable that will maintain it's value thru an input change?

 
rwh0965:

I have an indicator that tracks max drawdown during the day.

I have it displayed on the chart with a text object.

Problem is, that if the user changes an input in the indicator (to move the text to a different corner), I have to delete the text object and then it loses it's value.

How can I create a variable that will maintain it's value thru an input change?

You don't have to delete the text object.
 
Alain Verleyen #:
You don't have to delete the text object.

When I didn't delete the text object in the deinit function, the text is all blank after changing the input.


int OnInit()
{
   ObjectCreate("asb_label", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("asb_label","Account Starting Balance",9, "Consolas", Black);
   ObjectSet("asb_label", OBJPROP_CORNER, 0);
   ObjectSet("asb_label", OBJPROP_XDISTANCE, horizontal_offset+10);
   ObjectSet("asb_label", OBJPROP_YDISTANCE, vertical_offset+10);
   ObjectCreate("asb_val", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("asb_val", rightAlign(starting_balance, 9, 2), 9, "Consolas", Black);
   ObjectSet("asb_val", OBJPROP_CORNER, 0);
   ObjectSet("asb_val", OBJPROP_XDISTANCE, horizontal_offset+335);
   ObjectSet("asb_val", OBJPROP_YDISTANCE, vertical_offset+10);
}
 
//+------------------------------------------------------------------+
//|                                                   daily_info.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

#property strict
#property indicator_chart_window

#include <ChartObjects\ChartObjectsTxtControls.mqh>

//--- input parameters
enum pos
  {
   tl=0,     // Top Left
   tr=1,     // Top Right
   bl=2,     // Botom Left
   br=3,     // Bottom Right
  };
input pos position=tl;         // Starting Corner
input int horizontal_offset=0; // Horizontal Offset
input int vertical_offset=0;   // Vertical Offset
input int starting_balance=0;  // Account Start Balance

double DayLowFloatingProfit;

class MyRectLabel : public CChartObjectRectLabel
{
   CChartObjectLabel m_label;
public:
   bool Create(long chart, const string name, const int window, 
               const int X, const int Y, const int sizeX, const int sizeY)
   {
      if(!CChartObjectRectLabel::Create(chart,name,window,X,Y,sizeX,sizeY))
         return false;
      return m_label.Create(chart, name + "_", window, X + 8, Y + 12);
   }
   bool Color(const color clr){
      return m_label.Color(clr);
   }
   bool Description(const string text){
      return m_label.Description(text);
   }
   bool FontSize(const int size){
      return m_label.FontSize(size);
   }
   bool Font(const string text){
      return m_label.Font(text);
   }
   bool ToolTip(const string text){
      return (this.ToolTip(text) && m_label.Tooltip(text));
   }
};

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);
  }

MyRectLabel rect_box;
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{      
   if(!rect_box.Create(0, "rbox", 0, horizontal_offset, vertical_offset, 500, 400)
      || !rect_box.BackColor(clrWhiteSmoke)
      || !rect_box.Description("")
      || !rect_box.Color(clrBlack)
      || !rect_box.FontSize(9)
   )
      return INIT_FAILED;

   ObjectCreate("dd_label", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("dd_label","Max Daily Drawdon",9, "Consolas", Black);
   ObjectSet("dd_label", OBJPROP_CORNER, 0);
   ObjectSet("dd_label", OBJPROP_XDISTANCE, horizontal_offset+10);
   ObjectSet("dd_label", OBJPROP_YDISTANCE, vertical_offset+160);
   ObjectCreate("dd_val", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("dd_val", rightAlign(DayLowFloatingProfit, 9, 2), 9, "Consolas", Black);
   ObjectSet("dd_val", OBJPROP_CORNER, 0);
   ObjectSet("dd_val", OBJPROP_XDISTANCE, horizontal_offset+335);
   ObjectSet("dd_val", OBJPROP_YDISTANCE, vertical_offset+160);

   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

   CurrentFloatingProfit = AccountProfit();   

   if (CurrentFloatingProfit < 0.0 && CurrentFloatingProfit < DayLowFloatingProfit) {
      DayLowFloatingProfit = CurrentFloatingProfit;
   }
  
   ObjectSetText("dd_val", rightAlign(DayLowFloatingProfit, 9, 2), 9, "Consolas", Black);

   return(0);
  }
Alain Verleyen #:

I have no idea what your code is doing. All I can say is there is no need to delete the object after a change in the inputs.

Show your code if you need coding help.

 
Ok, posted the code above.
 
rwh0965 #:

So you "blank" the text and you are surprised it is blanked ?

If you don't delete in OnDeinit() of course you don't have to create and initialize it in OnInit() if it already exists.

 
Alain Verleyen #:

So you "blank" the text and you are surprised it is blanked ?

If you don't delete in OnDeinit() of course you don't have to create and initialize it in OnInit() if it already exists.

I guess I don't understand your comment. Where do I "blank" the text?

Sorry, brand new to MQL4...

 
rwh0965 #:

I guess I don't understand your comment. Where do I "blank" the text?

Sorry, brand new to MQL4...

   ObjectSetText("dd_val", rightAlign(DayLowFloatingProfit, 9, 2), 9, "Consolas", Black);

On each change of the inputs an indicator is reinitialized.

You need to check if the object already exists and create/initialize it only if it doesn't.

And in OnDeinit() you check the "reason" and delete the object only if it's not a change in the input.

 
Alain Verleyen #:

On each change of the inputs an indicator is reinitialized.

You need to check if the object already exists and create/initialize it only if it doesn't.

And in OnDeinit() you check the "reason" and delete the object only if it's not a change in the input.


Ok, so how can I create the object if it doesn't exist?

 
rwh0965 #: Ok, so how can I create the object if it doesn't exist?

You already posted code that creates objects.

 

But you said I need to check if it exists.

Tried this, but it doesn't work:

   if(!ObjectCreate("dd_label", OBJ_LABEL, 0, 0, 0)) {
      ObjectCreate("add_label", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("add_label","Max Daily Drawdown",9, "Consolas", Black);
      ObjectSet("dd_label", OBJPROP_CORNER, 0);
      ObjectSet("dd_label", OBJPROP_XDISTANCE, horizontal_offset+10);
      ObjectSet("dd_label", OBJPROP_YDISTANCE, vertical_offset+10);
   }

   if(!ObjectCreate("dd_val", OBJ_LABEL, 0, 0, 0)) {
      ObjectCreate("dd_val", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("dd_val", rightAlign(DayLowFloatingProfit, 9, 2), 9, "Consolas", Black);
      ObjectSet("dd_val", OBJPROP_CORNER, 0);
      ObjectSet("dd_val", OBJPROP_XDISTANCE, horizontal_offset+335);
      ObjectSet("dd_val", OBJPROP_YDISTANCE, vertical_offset+10);
   }