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 --externint 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) // Тонкая или толстая линия
{
case1: // Тонкая линияObjectCreate(His_Name,OBJ_TREND,0,T_Bar,H_Bar,T_Bar,L_Bar);
break; // Выход из switchcase2: // Толстая линия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 –
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" ANDtextdeinit() 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
} //конец forreturn; // Выход из 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.
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);
}
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" ANDtextdeinit() 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
} //конец forreturn; // Выход из deinit()
} //конец deinit//-------------------------------------------------------------- 10 --
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.
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 )
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 пипсов выше предпоследней свечи
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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)
... 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.
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
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:
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.
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?
... 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.
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:
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.
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:
To:
How do I replace the stop loss in pips in the EA, with a stop loss above the last candle?