Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 671

 
Artyom Trishkin:

Nothing complicated.

Thanks, I'll be at my computer tonight and I'll get it for myself... nothing complicated, but I'm starved for time ))))

 
Hello! Please give me an example of code to create a button on a chart and have it perform at least one operation. At least in the comment output any sign. Thank you.
 
inter78:
Hello! Please give me an example of code to create a button on a chart and have it perform at least one operation. At least in the comment output any sign. Thank you.

Here's a code like this:

#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
kn();
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectDelete("close");
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
if(ObjectGetInteger(0,"close",OBJPROP_STATE)==true)
 {
  ObjectSetString(0,"close",OBJPROP_TEXT,"Открыть ВСЕ"); 
  Comment("Открыть всё!");
 }
else
 {
  ObjectSetString(0,"close",OBJPROP_TEXT,"Закрыть ВСЕ"); 
  Comment("Закрыть всё!");
 }
return;
}

//==============================================================
void kn()
{
 if (ObjectFind("close")==-1)
  {
   ObjectCreate(0,"close",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"close",OBJPROP_XDISTANCE,30); 
   ObjectSetInteger(0,"close",OBJPROP_YDISTANCE,30); 
   //--- установим размер кнопки 
   ObjectSetInteger(0,"close",OBJPROP_XSIZE,100); 
   ObjectSetInteger(0,"close",OBJPROP_YSIZE,30); 
   //--- установим угол графика, относительно которого будут определяться координаты точки 
   ObjectSetInteger(0,"close",OBJPROP_CORNER,CORNER_LEFT_UPPER); 
   //--- установим текст 
   ObjectSetString(0,"close",OBJPROP_TEXT,"Закрыть ВСЕ"); 
   //--- установим цвет фона 
   ObjectSetInteger(0,"close",OBJPROP_BGCOLOR,clrRed); 
   //--- установим цвет текста 
   ObjectSetInteger(0,"close",OBJPROP_COLOR,clrWhite);
  }
}
 
Valerius:

This is about the size of the code:

The comments don't come out.
 
inter78:
Comments do not come out.

After starting the programme, press the button and the corresponding inscription appears in the upper left corner. Press the button again and the message will change. Do not forget to press the auto-trade button or allow trading in the EA settings. There should be a smiling face in the upper right corner. Everything works in the tester.

 
Valerius:

After starting the programme, press the button and the corresponding inscription appears in the upper left corner. Press the button again and the message will change. Do not forget to press the auto-trade button or allow trading in the EA settings. There should be a smiling face in the upper right corner. Everything works in the tester.

How will the indicator work ?
 
inter78:
How will the indicator work?

It's made for an EA, not for an indicator.

 
That's a pity. I need an indicator.
 
inter78:
That's a pity. I need an indicator.

There's not much difference, is there?

//==============================================================
void kn()
{
 if (ObjectFind("close")==-1)
  {
   ObjectCreate(0,"close",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"close",OBJPROP_XDISTANCE,30); 
   ObjectSetInteger(0,"close",OBJPROP_YDISTANCE,30); 
   //--- установим размер кнопки 
   ObjectSetInteger(0,"close",OBJPROP_XSIZE,100); 
   ObjectSetInteger(0,"close",OBJPROP_YSIZE,30); 
   //--- установим угол графика, относительно которого будут определяться координаты точки 
   ObjectSetInteger(0,"close",OBJPROP_CORNER,CORNER_LEFT_UPPER); 
   //--- установим текст 
   ObjectSetString(0,"close",OBJPROP_TEXT,"Закрыть ВСЕ"); 
   //--- установим цвет фона 
   ObjectSetInteger(0,"close",OBJPROP_BGCOLOR,clrRed); 
   //--- установим цвет текста 
   ObjectSetInteger(0,"close",OBJPROP_COLOR,clrWhite);
  }
}

This is code for creating a button.

The button is created in the initialization function

int OnInit()
{
kn();
return(INIT_SUCCEEDED);
}

And this is

if(ObjectGetInteger(0,"close",OBJPROP_STATE)==true)
 {
  ObjectSetString(0,"close",OBJPROP_TEXT,"Открыть ВСЕ"); 
  Comment("Открыть всё!");
 }
else
 {
  ObjectSetString(0,"close",OBJPROP_TEXT,"Закрыть ВСЕ"); 
  Comment("Закрыть всё!");
 }

Button control code.

 
Alexey Viktorov:

There's not much difference, is there?

This is code for creating a button.

The button is created in the initialization function

And this is

is the code for controlling the button.

Actually, I did, but I don't like the long response time

int start()
 {
  string    Direktor="ID";     // Имя каталога 
  string    Fail_ID=Symbol();      // Имя файла 
  int file_ID;
  file_ID=FileOpen(Direktor+"//"+Fail_ID,FILE_READ|FILE_WRITE|FILE_TXT); 
  FileWrite(file_ID,ChartID()); 
  FileClose(file_ID); 

  int total_GLOB=GlobalVariablesTotal();
  string name;
  string text;
  int kol=0;
  int x=1000;
  string strGl;
  string strGl_2;
  string strGl_3;
  double strGl_4;
  double strGl_5;
  for(int g=0;g<total_GLOB;g++)
    {
     name= GlobalVariableName(g);
     string g_2=IntegerToString(g+1,2,'0');
     text=g_2+" .  "+name+"   "+DoubleToString(GlobalVariableGet(name),4);
     kol++;
     strGl=IntegerToString(MathAbs(GlobalVariableGet(name)*10000),15,'0');
     strGl_2=StringSubstr(strGl,11,2);
     strGl_3=StringSubstr(strGl,13,2);
     strGl_4=StringToDouble(strGl_2);
     strGl_5=StringToDouble(strGl_3);
     ObjectCreate(name,OBJ_BUTTON,0,0,0);
         if(ObjectGet(name,OBJPROP_STATE)==true)
         {
          string Fail_ID_2=name;
          int file_ID_2 =FileOpen(Direktor+"//"+Fail_ID_2,FILE_READ|FILE_WRITE|FILE_TXT);
          string id =FileReadString(file_ID_2);
          long id2=StringToInteger(id);
          ChartSetInteger(id2,CHART_BRING_TO_TOP,true);
          FileClose(file_ID_2); 
         }
     if(strGl_4==strGl_5)
     {
      ObjectSet(name,6,clrDarkSlateGray);
      ObjectSet(name,OBJPROP_BGCOLOR,clrAquamarine);
      }
     if(strGl_4!=strGl_5)
     {
      ObjectSet(name,6,clrDarkSlateGray);
      ObjectSet(name,OBJPROP_BGCOLOR,clrSnow);
      }
     ObjectSet(name,102,x);
     ObjectSet(name,OBJPROP_SELECTED,false);
     ObjectSetText(name,text,10,"Arial",clrLightCyan);
     ObjectSet(name,103,40+(kol*20));
     if(GlobalVariableGet(name)<0) ObjectSet(name,6,Red);
     if(GlobalVariableGet(name)>0) ObjectSet(name,6,clrMediumBlue);
     if(GlobalVariableGet(name)==0) ObjectDelete(name);
     ObjectSetInteger(0,name,OBJPROP_XSIZE,180); 

//--- переведем кнопку в заданное состояние 
     ObjectSetInteger(0,name,OBJPROP_STATE,false); 

     if(Symbol()==name)
      {
       ObjectSet(name,6,clrMaroon);
       ObjectSet(name,103,25);
       ObjectSet(name,OBJPROP_FONTSIZE,13);
       ObjectSetInteger(0,name,OBJPROP_YSIZE,25); 
       ObjectSetInteger(0,name,OBJPROP_XSIZE,200); 
      }
    }

  return(0);
 }
void OnDeinit(const int reason)
  {
  GlobalVariablesDeleteAll();
ObjectsDeleteAll(0,0,OBJ_BUTTON);

  }
  
Reason: