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

 
Vitaly Gorbunov:

The fact that you have identified somewhere the number of orders you need is good, but in this block you run through all the orders again and check them!

Break down the code properly and you will see at once!

You can of course recalculate it again. But it doesn't make the difference. The if() operator does not perform "otherwise". this is just one case, and there were others.
 
for(int h = OrdersTotal()-1; h >= 0; h--)
    {
     if(OrderSelect(h, SELECT_BY_POS))
      {
       if((cnt_OO >= 2))
        {
       if((OrderMagicNumber() == Magic)&&(OrderLots() <= Lots/Prikup - Dplus))
        {
         Nextstep  = NextStep;
         BaseNext  = OrderOpenPrice();
         LotsNext  = NormalizeDouble(OrderLots()*K,lotDigit);
         if(NewPB > 0)
          PBcloseON = true;
         Alert ("Pospedny Order NEXT  ",OrderTicket());
         Alert ("Otkritih orderov  ",cnt_OO);
//         break;
        }
       break;
       }
         LotsNext    = NormalizeDouble(Lots*Prikup,lotDigit); 
         Alert ("Otkritih orderov NEXT net ");
        Alert ("Otkritih orderov  ",cnt_OO);
         break;
     
        }}

Try it like this!

I don't see anything else in your code!

Throw other cases, let's see what's wrong there :)

After a careful study of logic, I reverse-engineered your code.

It turned out something like this

if(cnt_OO>0) //Если нет ордеров то и не надо ни чего делать
{
  for(int h = OrdersTotal()-1; h >= 0; h--)
   {
    if(OrderSelect(h, SELECT_BY_POS))
     {
      if(cnt_OO==1)
         {
          //Если ордер один проверяем тот ли ордер (майджик и прочее) и что то там делаем
         }
      else
         {
          //Если ордеров больше чем 1 проверяем те ли ордера (майджик и прочее) и что то там делаем
         }
     }
   }
   
}
 

We can do it that way. But the tutorial says that if the condition is not fulfilled, the commands after the curly bracket closing the block to process the condition of the if() operator are processed. It does not happen.

There was one more glitch:

     if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))&&(OrderMagicNumber() == Magic)&&(OrderLotsOld==zLots))
      {
       

If I added one more condition to the first if operator - the next one - it didn't work

I have two suggestions

1. Stupid strategy tester. Because these situations occur at the debugging stage of the program, you have to be a complete idiot to check it on a real account. And on a demo account, too, because it is difficult to reproduce the restart situation. The fact that the tester is an idiot tells us that the order of closing of several orders in the MT4 window often doesn't correspond to the real one if the closing takes place at one time. This can be clearly seen if you recalculate the last orders in the program. This error made me spend a week trying to find out who was nuts.

The very logic of the tester's operation, even on tick history, is far from real life. It is very critical for my algorithm.

2.As a guess. A question to especially advanced gurus not only in MQL/MT4, but also to system specialists.

- I have two identical EAs on the same pair in different windows. When I run the EAs for example on Monday after the computer is off for the weekend, they both start working at the same time when the first tick appears. I hoped that one would work first to restore the state and then the other. Whoever was lucky, worked first.

In fact the data recovery messages are mixed. In other words, program execution is interrupted by some condition, for example system timer and then continues. Interesting situations occur when e.g. switch to another account to sort of check how it's going. Conditions for checking the account are at the beginning of the program and at the moment of returning to the initial account, the program is in the middle and does not give a shit what account is there now.

I found a way out - at the beginning of each block I check the account number. I'm not sure wherever it's supposed to be.

 

Amon1953 did you look at the first version I fixed? Does it work? Exactly as it is written in the if() manual and it works for many years of using it. The problem is in your code you put a break in a wrong block.

if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))&&(OrderMagicNumber() == Magic)&&(OrderLotsOld==zLots)) you must check what is assigned to variables.

On the second point - both owls will be running in parallel, so messages from them will be mixed. In order to do as you describe you need to implement semaphoring between copies of the owl. And it's very interesting that something weird happens when I change the account. I would very much like to see OnInit and OnDeinit. Most likely the problem is there.

 
Vitaly Gorbunov:

Amon1953 did you look at the first version I fixed? Does it work? Exactly as it is written in the if() manual and it works for many years of using it. The problem is in your code you put a break in a wrong block.

if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))&&(OrderMagicNumber() == Magic)&&(OrderLotsOld==zLots)) you must check what is assigned to variables.

On the second point - both owls will run in parallel, so messages from them will be mixed. In order to do as you describe you need to organize semaphoring between copies of the owl. And it's very interesting that something weird happens when I change the account. I would very much like to see OnInit and OnDeinit. Most likely the problem is there.

I have not checked it because we need to exit the loop by the last order (it is the first in the list).

I'm not an experienced programmer and I do not fully understand operation of OnInit and OnDeinit. This is why they are not used in my code, but they seem to allow the program to run without interruption in the middle.

I don't understand semaphore too, advisors are installed in different windows and have different majors

 
My advice to you, if you have only recently started programming, is to take another look at the basic programming basics. No offence, but you are very difficult to communicate with.
 
Vitaly Gorbunov:
My advice to you, if you have only recently started programming, is to study the basic programming basics again. No offense, but you are very difficult to communicate with.
Thank you. This section is for beginners. Even this kind of communication has benefited me. It is difficult to do both EA algorithm and write program code (moreover, the programming language is quite new to me)
 
Amon1953:
Thank you. This is a section for beginners. Even this kind of communication has benefited me. It's hard to do both the EA algorithm and the program code (especially since the programming language is quite new to me)
It looks like you need to improve the basics! Since I don't quite understand from your code what kind of logic you want to implement, try to explain in words what you want to do. And I will try to explain where you have an error.
 
Vitaly Gorbunov:
Looks like you need to tighten up the base! Since I don't quite understand from your code what logic you want to implement, try to explain in words what you want to do. And I'll try to explain where you have a mistake.

I have already explained what I need before. I will try to clarify the details.

When you restart the EA, you need to restore the previous state, because the algorithm is a chain of orders. The first order is the basic one, and we can calculate parameters of next orders in the chain from it. For example, the volume of the second order is 50% of the base, the third 75%, and so on. When restarting an EA, we need to know the volume of baseline and last order, since the volume of the next order will be calculated from the last one. For example, there are 3 open orders. To calculate the next (fourth) order, we need to find the volume of the last open order.

If there is only one order, it means it is the base one and in this case, we are not interested in it, it is handled by another unit.

The algorithm is very simple. But it only works with two if() operators.

 
Amon1953:

I have already explained what I need before. I will try to clarify the details.

When you restart the EA, you need to restore the previous state, because the algorithm is a chain of orders. The first order is the basic one, and we can calculate parameters of next orders in the chain from it. For example, the volume of the second order is 50% of the base, the third 75%, and so on. When restarting an EA, we need to know the volume of baseline and last order, since the volume of the next order will be calculated from the last one. For example, there are 3 open orders. To calculate the next (fourth) order, we need to find the volume of the last opened order.

If there is only one order, it means it is the base one and in this case, we are not interested in it, it is handled by another unit.

The algorithm is very simple. But it only works with two if() operators.

Amon1953:

I haven't checked it because we need exit from the loop by the last order (it is the first in the list).

I'm not an experienced programmer and do not quite understand operation of OnInit and OnDeinit. This is why they are not used in my code, but they seem to allow the program to run without interruption in the middle.

About the semaphore, too, it is not clear. The Expert Advisors are installed in different windows and have different majors.

Please read the documentation:

OnInit

The OnInit() function is the Init event handler. Itcan be ofvoid or inttype , has no parameters:

voidOnInit();

Init events are generated immediately after loading an Expert Advisor or indicator. The OnInit() function is used for initialization. If OnInit() has the int return value, the non-zero return code means unsuccessful initialization, and generates the event Deinit with the code of deinitialization reasonREASON_INITFAILED.

Also sort out the visibility of the variables.

События клиентского терминала - Программы MQL4 - Справочник MQL4
События клиентского терминала - Программы MQL4 - Справочник MQL4
  • docs.mql4.com
Сразу же после того, как клиентский терминал загрузит программу (эксперт или пользовательский индикатор) и запустит процесс инициализации глобальных переменных, будет послано событие Init, которое обрабатывается функцией OnInit(), если она есть. Это событие также генерируется после смены финансового инструмента и/или периода графика, после...