[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 422

 

Good afternoon. My question is as follows. In the MQL4 book found at MQL4.community, in the "Standard Functions" section of the "String Functions and Data Conversion Functions" section, the following example of the strings.mq4 Expert Advisor is given (given in full from the book)

//--------------------------------------------------------------------
// strings.mq4
// Предназначен для использования в качестве примера в учебнике MQL4.
//--------------------------------------------------------------- 1 --
extern int Quant_Bars=100;             // Количество баров
datetime   Time_On;
string     Prefix    ="Paint_";
//--------------------------------------------------------------- 2 --
int init()                             // Спец. функция init()
  {
   int Ind_Bar;                        // Индекс бара
   Time_On=Time [Quant_Bars];          // Время первого раскрашенного
   for(Ind_Bar=Quant_Bars-1; Ind_Bar>=0; Ind_Bar--)// Цикл по барам
     {
      Create(Ind_Bar,1);               // Нарисуем тонкую линию
      Create(Ind_Bar,2);               // Нарисуем толстую линию
     }
   WindowRedraw();                     // Перерисовка изображения 
   return;                             // Выход из init()
  }
//--------------------------------------------------------------- 3 --
int start()                            // Спец. функция start
  {
   datetime T1, T2;                    // 1 и 2 координаты времени
   int Error,Ind_Bar;                  // Код ошибки и индекс бара
   double P1, P2;                      // 1 и 2 координата цены
   color Col;                          // Цвет созданного объекта
//--------------------------------------------------------------- 4 --
   for(int Line=1; Line<=2; Line++)    // Цикл по видам линий
     {
      string Nom_Lin =Line + "_";      // Строка с номером линии
      //    string Nom_Lin  = DoubleToStr(Line,0)+"_";// Можно и так
      for(Ind_Bar=0; ;Ind_Bar++)       // Цикл по барам
        {
//--------------------------------------------------------------- 5 --
         datetime T_Bar= Time[Ind_Bar];// Время открытия бара
         if (T_Bar < Time_On) break;   // Заграничные не раскрашиваем
         string Str_Time=TimeToStr(T_Bar);       // Строка со временем
         string His_Name=Prefix+Nom_Lin+Str_Time;// Имя объекта
//--------------------------------------------------------------- 6 --
         T1=ObjectGet(His_Name,OBJPROP_TIME1);// Запрос коорд. t1
         Error=GetLastError();         // Получение кода ошибки
         if (Error==4202)              // Если объекта нет :(
           {
            Create(Ind_Bar,Line);      // Вызов ф-ии создания объекта
            continue;                  // На следующую итерацию
           }
//--------------------------------------------------------------- 7 --
         T2 =ObjectGet(His_Name,OBJPROP_TIME2); // Запрос коорд. t2
         P1 =ObjectGet(His_Name,OBJPROP_PRICE1);// Запрос коорд. p1
         P2 =ObjectGet(His_Name,OBJPROP_PRICE2);// Запрос коорд. p1
         Col=ObjectGet(His_Name,OBJPROP_COLOR); // Запрос цвета
         if(T1!=T_Bar || T2!=T_Bar || // Не те координаты или цвет:
            (Line==1 && (P1!=High[Ind_Bar] || P2!=  Low[Ind_Bar])) ||
            (Line==2 && (P1!=Open[Ind_Bar] || P2!=Close[Ind_Bar])) ||
            (Open[Ind_Bar] Close[Ind_Bar] && Col!=Red)  ||
            (Open[Ind_Bar]==Close[Ind_Bar] && Col!=Green)  )
           {
            ObjectDelete(His_Name);    // Удаляем объект
            Create(Ind_Bar,Line);      // Создаём правильный объект
           }
//--------------------------------------------------------------- 8 --
        }
     }
   WindowRedraw();                     // Перерисовка изображения 
   return;                             // Выход из start()
  }
//--------------------------------------------------------------- 9 --
int deinit()                           // Спец. функция deinit()
  {
   string Name_Del[1];                 // Объявление массива
   int Quant_Del=0;                    // Количество удаляемых объектов
   int Quant_Objects=ObjectsTotal();   // Cтолько всего ВСЕХ объектов
   ArrayResize(Name_Del,Quant_Objects);// Необходимый размер массива
   for(int k=0; k<=Quant_Del; i++)     // Удаляем объекты с именами,.. 
      ObjectDelete(Name_Del[i]);       // .. имеющимися в массиве
   return;                             // Выход из deinit()
  }
//-------------------------------------------------------------- 10 --
int Create(int Ind_Bar, int Line)      // Пользовательская функция..
  {                                    // ..создания объекта
   color Color;                        // Цвет объекта
   datetime T_Bar=Time [Ind_Bar];      // Время открытия бара
   double   O_Bar=Open [Ind_Bar];      // Цена открытия бара
   double   C_Bar=Close[Ind_Bar];      // Цена закрытия бара
   double   H_Bar=High [Ind_Bar];      // Максимальная цена бара
   double   L_Bar=Low  [Ind_Bar];      // Минимальная цена бара
 
   string Nom_Lin =Line + "_";         // Строка - номер линии
   // string Nom_Lin  = DoubleToStr(Line,0)+"_";// Можно и так
   string Str_Time=TimeToStr(T_Bar);   // Строка - время откр.     
   string His_Name=Prefix+Nom_Lin+Str_Time;// Имя созаваемого объект
   if (O_Bar < C_Bar) Color=Blue;      // Выбор цвета в зависимости..
   if (O_Bar >C_Bar) Color=Red;        // .. от характеристик бара
   if (O_Bar ==C_Bar) Color=Green;
 
   switch(Line)                        // Тонкая или толстая линия
     {
      case 1:                          // Тонкая линия
         ObjectCreate(His_Name,OBJ_TREND,0,T_Bar,H_Bar,T_Bar,L_Bar);
         break;                        // Выход из switch
      case 2:                          // Толстая линия
         ObjectCreate(His_Name,OBJ_TREND,0,T_Bar,O_Bar,T_Bar,C_Bar);
         ObjectSet(   His_Name, OBJPROP_WIDTH, 3);// Стиль     
     }
   ObjectSet(    His_Name, OBJPROP_COLOR, Color); // Цвет
   ObjectSet(    His_Name, OBJPROP_RAY,   false); // Луч
   ObjectSetText(His_Name,"Объект создан экспертом",10);// Описание
   return;                             // Выход из польз. ф-ии
  }
//-------------------------------------------------------------- 11 –
 

... continued...

At the end of subsection StringSubstr Function in brackets is the following phrase: "separately we must note that in the first for loop (my note: it must be deinit() as there is no other way to delete objects) we cannot delete objects since after every deletion the number of objects and their numbering will change and some object names will be skipped". But in the cited EA there is no second for loop at all. Moreover, the variable Quant_Del defined in deinit() function before the for loop is initialized with zero, while the variable i was not defined anywhere before the for loop.

   for(int k=0; k<=Quant_Del; i++)     // Удаляем объекты с именами,.. 
         ObjectDelete(Name_Del[i]);       // .. имеющимися в массиве

it turns out that the above code just trivially omits several program lines.

So the 1st question is as follows: Was it really a trivial omission of several lines or is it such a tricky program that a novice cannot grasp its meaning without outside help?

Further... In the same chapter of the same section, there is a link to the same Expert Advisor (in the attached file) before the cited text that, when clicked, opens the same Expert Advisor in MetaElitor. But the text of this Expert Advisor is different: the deinit() function looks like this

//--------------------------------------------------------------- 9 --
int deinit()                           // Спец. функция deinit()
  {
   string Name_Del[1];                 // Объявление массива
   int Quant_Del=0;                    // Количество удаляемых объекто
   int Quant_Objects=ObjectsTotal();   // Cтолько всего ВСЕХ объектов
   ArrayResize(Name_Del,Quant_Objects);// Необходимый размер массива
   for(int k=0; k<Quant_Objects; k++)  // По количеству объектов 
     {
      string Obj_Name=ObjectName(k);   // Запрашиваем имя объекта
      string Head=StringSubstr(Obj_Name,0,6);// Извлекаем первые 6 сим
      if (Head==Prefix)                // Найден объект, ..
        {                              // .. начинающийся с Paint_
         Quant_Del=Quant_Del+1;        // Колич имён к удалению
         Name_Del[Quant_Del-1]=Obj_Name;//Запоминаем имя удаляемого
        }
     }
   for(int i=0; i<=Quant_Del; i++)     // Удаляем объекты с именами,.. 
      ObjectDelete(Name_Del[i]);       // .. имеющимися в массиве
   return;                             // Выход из deinit()
  }
//-------------------------------------------------------------- 10 --

Then, if "combine" the phrase "Separately, please note that you cannot delete objects in the first for loop, because in this case the total number of objects and their numbering will change after each deletion, and some object names will be skipped as a result" AND text deinit() function of the last variant, I have the 2nd question:

Question. Why cannot I delete objects in the first for loop by placing the ObjectDelete(Obj_Name) function inside the if statement...? Something like:

int deinit()                           // Спец. функция deinit()
  {
   int Quant_Objects=ObjectsTotal();   // Cтолько всего ВСЕХ объектов
   for(int k=0; k<Quant_Objects; k++)  // По количеству объектов 
     {
      string Obj_Name=ObjectName(k);   // Запрашиваем имя объекта
      string Head=StringSubstr(Obj_Name,0,6);// Извлекаем первые 6 сим
      if (Head==Prefix)                // Если найден объект, …
        {                              // ... начинающийся с Paint_,
         ObjectDelete(Obj_Name);       // … то его и удаляем
        }                              //конец if
     }                                 //конец for
   return;                             // Выход из deinit()
  }                                    //конец deinit
//-------------------------------------------------------------- 10 --

Why after each deletion "... The total number of objects will change"...? Because in this case, the Quant_Objects variable, which determines the total number of objects, gets its value equal to ObjectsTotal(), FORWARD the for loop?

P.S. Thanks in advance for the answer, in order not to litter the forum.

Files:
strings_1.mq4  7 kb
 

Good afternoon, I made a block to calculate magic buy open positions, but the problem is that if the block works on different currency pairs, then the block only produces results for one currency pair (the last open position). How can I fix the code (without changing the magic) so that the block gives results for each currency pair?

int OpenMagicOrderCount_buy() 
{
   int OpenBuyOrdersCount = 0;
   int OpenOrders = 0;
   int cnt = 0;
   OpenOrders=OrdersTotal();
   for(cnt=0;cnt<OpenOrders;cnt++)   // scan all orders and positions. ..
   {
     OrderSelect(cnt, SELECT_BY_POS);
          if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic &&  OrderType() == OP_BUY )
          {                             
                  OpenBuyOrdersCount ++;
          }
   } 
   Print("+++++++++++++++++++++++OpenBuyOrdersCount: " + OpenBuyOrdersCount,"   OrderSymbol()= ", OrderSymbol());
   return(OpenBuyOrdersCount); 
}
 
int OpenMagicOrderCount_buy() 
{
   int OpenBuyOrdersCount = 0;
   int OpenOrders = 0;
   int cnt = 0;
   OpenOrders=OrdersTotal();
   for(cnt=0;cnt<OpenOrders;cnt++)   // scan all orders and positions. ..
   {
     OrderSelect(cnt, SELECT_BY_POS);
          if (OrderMagicNumber()==magic &&  OrderType() == OP_BUY )
          {                             
                  OpenBuyOrdersCount ++;
          }
   } 
   Print("+++++++++++++++++++++++OpenBuyOrdersCount: " + OpenBuyOrdersCount,"   OrderSymbol()= ", OrderSymbol());
   return(OpenBuyOrdersCount); 
}
так все " OP_BUY" magicians will count
 
7777877:

... continued...

At the end of subsection StringSubstr Function in brackets is the following phrase: "separately we must note that in the first for loop (my note: it must be deinit() as there is no other way to delete objects) we cannot delete objects since after every deletion the number of objects and their numbering will change and some object names will be skipped". But in the cited EA there is no second for loop at all. Moreover, the variable Quant_Del defined in the function deinit() before the for loop is initialized with zero, while the variable i was not defined anywhere before the for loop.

It turns out that the above code just trivially omits several program lines.

So the 1st question is as follows: Was it really a trivial omission of several lines or is it such a tricky program that a novice cannot grasp its meaning without outside help?

Further... In the same chapter of the same section, there is a link to the same Expert Advisor (in the attached file) before the cited text that, when clicked, opens the same Expert Advisor in MetaElitor. But the text of this Expert Advisor is different: the deinit()function here looks like this

Then, if "combine" the phrase "Separately, please note that you cannot delete objects in the first for loop, because in this case the total number of objects and their numbering will change after each deletion, and some object names will be skipped as a result" ANDtext deinit() function of the last variant, I have the 2nd question:

Question. Why cannot I delete objects in the first for loop by placing the ObjectDelete(Obj_Name) function inside the if statement?

Why after each deletion "... The total number of objects will change"...? Because in this case, the Quant_Objects variable, which determines the total number of objects, gets its value equal to ObjectsTotal(), FORWARD the for loop?

P.S. Thanks in advance for the answer, in order not to litter the forum.

Turn the cycle backwards and delete as much as you like
 
FAQ:
Expand the loop backwards and delete as much as you can

Did I understand you correctly? You should think of the deinit function (and the loop in particular) as follows:

int deinit()                           // Спец. функция deinit()
  {
   int Quant_Objects=ObjectsTotal();   // Cтолько всего ВСЕХ объектов
   for(int k=Quant_Objects; k>=0; k--) // По количеству объектов 
     {
      string Obj_Name=ObjectName(k);   // Запрашиваем имя объекта
      string Head=StringSubstr(Obj_Name,0,6);// Извлекаем первые 6 сим
      if (Head==Prefix)                // Если найден объект, …
        {                              // ... начинающийся с Paint_,
         ObjectDelete(Obj_Name);       // … то его и удаляем
        }                              //конец if
     }                                 //конец for
   return;                             // Выход из deinit()
  }                                    //конец deinit
//-------------------------------------------------------------- 10 --
 
Elektronik:


This data is needed to calculate the difference between buy and sell orders.

let's assume one buy order with the volume of 0.09 lots and two sell orders with the total volume of 0.11 lots were opened, the difference between them will be 0.09-0.11=-0.02, or if possible the difference between their values.


Well then the function OrderLots.
 
abeiks:

Good afternoon, I made a block to calculate magic buy open positions, but the problem is that if the block works on different currency pairs, then the block only produces results for one currency pair (the last open position). How can I fix the code (without changing magic) so the block will calculate for all currency pairs?

Change:

if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic &&  OrderType() == OP_BUY )

To:

if (OrderMagicNumber()==magic &&  OrderType() == OP_BUY )
 
How do I replace the stop loss in pips in the EA, with a stop loss above the last candle?
 
Neo777:
How do I replace the stop loss in pips in the EA, with a stop loss above the last candle?
double x = ... // Задаем на сколько пипсов выше
double sl = NormalizeDouble(High[1] + x * Point, Digits); // Задаем значение стоплосс на x пипсов выше предпоследней свечи
Reason: