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

 
Valeriy Yastremskiy:

I understand that, but mql5 is difficult to understand unfortunately. I agree with fxsaber, I think it is easier to use 4 and for optimization and other stuff we should use 5. In general, I'd like to know if there is anybody who has mastered mql5 without OOP practice? Of course, after C++ 4 is not relevant, although I may be wrong.

The two languages are absolutely identical. Neither more nor less. And OOP (which everyone has been intimidated by) is exactly the same in both. You didn't know? Have you read horror stories on the Internet and watched bloggers on YouTube who "sow good, wise and eternal"? Who cleverly talk nonsense, deliberately scaring people away from MQL5.

I hasten to disappoint - both languages are exactly the same. Both in ease of understanding, and in features and presence of OOP.
But MQL5 has more possibilities.

 
Alexey Viktorov:

I sent the Spaniard to you there - well, scare him. Make him an indicator - I don't have time. It's in English. I'll explain if there's anything. But... you'll figure it out.

 
Alexey Viktorov:

I don't remember Artem's answer, and I'm not going to search for it. All of the codes in his articles are either multi-terminal or two versions, for mql5 and mql4. I have checked these versions on mql4. Everything works properly and detects closing by stop or take and makes no mistakes.

Yes, these articles are difficult to understand, but it's worth it if you are no older than me. And I, for a programmer, am obscenely old.

The other variant is more simple, but it will be much slower. Write the order ticket into an array, and by this array, select the order and check the time of order closing. If it is higher than zero, it means that the order is closed. If the comment of a closed order contains the letters "sl", it means that the order is closed using a stop. If the order is closed, it is deleted from the array. Or, after the array is completed, it is refilled with remaining open orders. Imagination is flying in general.

Which option you choose is up to you. And read some of my posts on Kovalev's textbook. It is the textbook that suggests writing int start(), while the newer mql4 requires void OnTick().

For indicators and scripts, please refer to documentation. And by the way, it's much easier to write indicators in updated mql4 than it was when Sergey wrote this tutorial.

Thank you very much. Your information was very valuable for me. Especially about the start() function.

 
Alexey Viktorov:

I don't remember Artem's answer, and I won't look for it, I can guess what he might have answered. ...

Here is my answer. With a clear indication of what needs to be done:

Forum on trading, automated trading systems and trading strategy testing

Any questions from newbies on MQL4 and MQL5, help and discussion on algorithms and codes

Artyom Trishkin, 2020.04.28 08:22

This is a simple explanation: you need to monitor the number of orders and positions, and compare them with the previous state. If we have 12 positions on the previous tick, and 8 positions on the current tick, we have a change of 4 positions. Accordingly, we should take the last four (by time of closing) positions to understand what happened to them.

But, judging by the attached code, in which "miracles" are written, you really don't need an explanation in words. But it's here to help, not to write for/for.


 
ANDREY:

Thank you very much. Your information was very valuable to me. Especially about the start() function

The good thing is to create in the editor what we need, EA, script, indicator and the editor will create a template with the right fields.
 
Artyom Trishkin:

And what does your code do besides that it opens two sell positions, each at a strictly specified time, and prints the time of opening the last position when it is allowed? That's it. Using uninitialized variables, which can lead to "wonders" in the code's behavior, and very, very old start() handler, which was pulled from a dusty shelf with years of cobwebs, and in the market (someday you want to sell something) with ancient handlers will never miss validator - it will say that the wrong program type.

Thanks for the input. On the GBP/USD pair I found one pattern in price behaviour when testing. This pattern with small corrections, as well as with small drawdowns, balance chart constantly leads upwards since 2008 and up to the current moment. As I said, balance chart corrections and drawdowns are not big, but they can last for several months.
I started to test different variants of this pattern and resorted to optimization for this purpose. But as it turned out, optimization using several parameters at once takes a huge amount of time. In my case it took about a year. A genetic algorithm is not good for me.
Then I came to the idea that testing and optimization can be performed faster with the help of properly formulated code, the Pront() function and Excel table.

The code below is just an attempt to write such a code. I will improve this code further. But I encountered a problem I lacked knowledge to solve.
Please don't judge me and my code very strictly.... I am a beginner who only just began to understand the basics of coding and little more. But I take criticism calmly and do not take offence..... especially to constructive criticism.

Thank you for your help.

int s1,Mn,a,CH;
double Hay,Lou=Bid,Nm_PL[700]={0},PrS,DL=0.0030,X;
int start()
{
int ot = OrdersTotal();                                         
int Ht = OrdersHistoryTotal();
////******************************************************************
if (OrderSelect (Ht-1,SELECT_BY_POS, MODE_HISTORY))                                             
if (X!=OrderTicket( ))
{
Mn=OrderMagicNumber();CH=TimeHour(OrderOpenTime());PrS=OrderProfit( );Nm_PL[CH+Mn*24]=Nm_PL[CH+Mn*24]+PrS;
Print("----------------- ПРОФИТ-----------------------=",CH,"    Ном орд.(1-26)   ",Mn," ТП    ",PrS," ФИН.РЕЗУЛЬТ.   ",Nm_PL[CH+Mn*24]," НОМ. ИНДЕКСА   ",Mn*24);X=OrderTicket();
}
////******************************************************************
if (Bid < Lou)
{
Lou=Bid;
}
//=======================
if (Bid > Hay)
{
Hay=Bid;
}
////=======================
if (Bid - Lou > DL&& Lou!=0)
{
for(int c=0; c<=500;c+=20) 
{
a++;
OrderSend(Symbol(),OP_SELL,0.1,Bid, 3,Ask+0.0030+c*Point,Ask-0.0010-c*Point,"300",a );
}
Lou=Bid;
Hay=Bid;
a=0;
s1=0;
}
return(0);
}

If two or more orders are closed by stop or take on the same tick, the program prints Print() only after the last order closed on this tick. I need Print() after each order is closed. I don't know how to achieve this. But if I see the necessary code, I will immediately understand and remember it.

Below is an example of my problem.


Генетические алгоритмы - это просто!
Генетические алгоритмы - это просто!
  • www.mql5.com
Введение Генетический алгоритм (ГА) относится к эвристическим алгоритмам (ЭА), который даёт приемлемое решение задачи в большинстве практически значимых случаев, однако при этом правильность решения математически не доказана и применяют чаще всего для задач, аналитическое решение которых весьма затруднительно или вовсе невозможно. Классическим...
 
Valeriy Yastremskiy:
The good thing is to create in the editor what we need, an EA, a script, an indicator and the editor will create a template with the right fields.

Yes. I noticed these correct fields, i.e. new functions instead of START. But I did not pay much attention to them and used START for testing as before.
I don't know where I can read more details about the latest updates of MQL4. I like Kovalev's book very much, because all information is logically structured and easily understood. And it is detailed enough to understand all the necessary details. I wonder why he hasn't written the same textbook for MQL5.

 
Artyom Trishkin:

Two absolutely identical languages. Neither more nor less. And OOP (which everyone has been intimidated by) is exactly the same in both. You didn't know? Have you read horror stories on the Internet and watched bloggers on YouTube who "sow good, wise and eternal"? Who cleverly talk nonsense, deliberately scaring people away from MQL5.

I hasten to disappoint - both languages are exactly the same. Both in ease of understanding, and in features and presence of OOP.
But MQL5 has more possibilities.

Yes in general I agree, and OOP in 4ka glad))) but in the access of beginners in my experience textbooks Kovalev, Zhdan on 4ka and incomprehensible Mishin on 5ka and OOP. I don't know how to understand it. No matter how many times you repeat encapsulation, polyformism understanding will not come. And you don't understand which articles to read first. And where to look for what. That's why so far in codobase in ontik the exit from the start function is found )))))
 
ANDREY:

Yes. I noticed those correct fields, i.e. new functions instead of START. But I didn't pay much attention to them, so I used START for testing as before.
I don't know where I can read more details about the latest updates of MQL4. I like Kovalev's book very much, because all information is logically structured and easily understood. And it is detailed enough to understand all the necessary details. I wonder why he hasn't written the same textbook for MQL5.

AsAlexey Viktorov said:Understand the simple truth, mql5 differs from mql4 only in OrderSend() and some other functions, which I do not remember. Another important difference is the direction of indexingof indicator buffers. However, it can be solved very quickly, but you'd better get used to the new features. But getting of indicator values remains the same.
OOP articles for beginners have helped. And the editor's help.
 
ANDREY:

Thank you for your participation.

...

The code below is just an attempt to write such a code. I will refine this code in the future. But I encountered a problem I lacked knowledge to solve.
Please don't judge me and my code very severely.... I am a beginner who only just began to understand the basics of coding and little more. But I take criticism calmly and do not take offence..... especially to constructive criticism.

Thank you for your help.

If two or more orders are closed by stop or take on the same tick, the program prints Print() only after the last order has closed on this tick. I need Print() after each order is closed. I don't know how to achieve this. But if I see the necessary code, I will immediately understand and remember it.

Below is an example of my problem.


I have already replied to you:

The explanation in words: you need to monitor the number of orders and positions and compare them with the previous state. If we had 12 positions on the previous tick and 8 on the current tick, we have a change of 4 positions. Accordingly, we should take the last four (by time of closing) positions to see what happened to them.

You take the last order in the list:

int Ht = OrdersHistoryTotal();
////******************************************************************
if (OrderSelect (Ht-1,SELECT_BY_POS, MODE_HISTORY))

And you first need to find out how much the state of the order list has changed. You used to have 12, now you have 8 - this is a change of 4 orders. Accordingly, you need to print all four orders. And you always print only the latest order in the list.

How do I know how much has changed? We need to write the number of orders into a variable, for example int last_total, when we run the EA. Then, on each tick, compare OrdersTotal() with last_total. If they are NOT equal, that is a change. Write the difference between OrdersTotal() and last_total into a variable, for example, num_changes and save the new state of OrdersTotal() to last_total.
There may be some subtleties here, for example, when a pending order is triggered. But for now, you should do what you need to do first.
Knowing the number of pending orders, you can look them up in the history. However, you can also limit the monitoring of orders in the history list - do all of the above for the list of historical orders, and not for the list of market orders. The difference between what was and what has become - this is the amount of orders you need to analyze. You always analyze only the last order in the list.

However, this cannot guarantee that the last order in the history list will be the last closed order. This is also a nuance that must be considered. But then.