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

 
trader781:

Once you've cleaned up your code, it will be more readable to you, not to mention others who need to understand your logic. That's where all the bugs will be seen.

OK, is that better?
Now delete unnecessary paired curly brackets and arrange the rest normally in blocks, and you will see where you have faults in logic.
 
Artyom Trishkin:
Now remove the extra paired curly brackets and arrange the rest normally in blocks, and you'll see where your logic is flawed.
What are the extra ones?
 
trader781:
Which ones are redundant?
You have a lot of unnecessary brackets in your code - use a styling tool and you will immediately see the unnecessary empty paired curly brackets.
 
Artyom Trishkin:
You've got a lot of unnecessary braces there in your code - treat the code with a styler, and you'll immediately see the extra empty paired curly brackets.
Done
Files:
 
trader781:
Done
I'll look at it later - it'll take at least four hours...
 
trader781:
Made by
1.count++; // count ticks from program start

Better way: if(count<=20) count++; - why count further if you only need to 21?

2.
   if(count>20) //  если количество тиков больше начинаем работу ... и дальше код эксперта
     {
      if(Bars<801 || (IsTradeAllowed()==false)) //--- Проверим достаточна ли в истории баров для анализа и разрешение торговли
         Print("Нет достаточного количества баров или торговля на текущем инструменте запрещена");
      return;
     }

And here is the wigwam. This will only check for the number of bars and print whencount>20, the rest of the code will work whencount<=20.

3.

if(OrderSymbol()==Symbol() && OrderType()<2)
            continue;

If you only need to consider the market ones (and this is what you expect from further code), you don't needcontinue at all.

4.

         if(y==true && (OrderType()==0)) //+-----покупка
           {
            dummy=(OrderClose(OrderTicket(),OrderLots(),Bid,0,White));

              {
               if((dummy==true) && ((OrderSelect(i,SELECT_BY_POS,MODE_TRADES))==false))

                  PlaySound("music");
               Sleep(20000);
               PlaySound("music");
               Sleep(20000);
               PlaySound("music");
               Sleep(20000);
               dummy=false;
               ExpertRemove();
              }
           }

It closes one order and eliminates itself? And what if there are more of them? And it sleeps for a whole minute.

I have not looked at the code above, there is no place to test it yet.

 

1 ok, I'll fix it

2 it is not ok, the following blocks should not work atcount<20

3 OK, I will correct it

4 Yes, it is a minute but it should be started only if there is no more market orders for the current symbol. I tried to implement it through a negative result of order selection, that's why we should set return somewhere, but the return in void OnTick() doesn't look so good. And again, OrdersTotal() will give the wrong result if we have a lot of orders for all symbols.

 
trader781:

2 is not OK, the following blocks should not work whencount<20

Then we need to add else return after the block;

4 Yes, it is a minute but it should be started only if there are no more market orders for the current symbol. I have tried to implement it through a negative result of orders selection, that's why we should set return somewhere, while the return in void OnTick() doesn't look very good. And again, OrdersTotal() will give the wrong result if we have a lot of orders over all symbols.

Why do we need three sounds?

Well, we can do everything in two stages: in the first loop, you close the orders and in the next one you recalculate all the market orders to check if there are any left, and if there are none, the fanfares play.

But I still do not understand what the trick is with music after orders are closed. Well, you can make a print to the journal or send a message to the mail or notification to your smartphone, but why would you turn an EA into a music box?
 
Vitalie Postolache:

Then you should add a return after the else return block;

Why do we need three times the sound?

But you can do everything in two steps: in the first loop you close the orders, and in the next one you recalculate all market orders to check if there are any not closed, and if there are none, the fanfare plays.

I still do not understand what the trick is with the music when orders have been closed. Well, we can print it to the journal, send a message to the mail or notify to the smartphone, but why should we turn our EA into a music box?

How do you separate the cycles?

How do I bind the sound to the last closed order? Because if there are no orders, the Expert Advisor would not trigger a sound.

And one last thing: I'm happy with the music for now

 
trader781:

How do I separate the cycles?

How to bind the sound to the last closed one? because if there are no orders, the Expert Advisor will not work

And lastly, I'm happy with the music for now.

What do you mean by "separate"? We don't have to separate anything. We just need two loops (they are almost the same) but one has OrderClose() and the other has a counter of orders. The order selection criterion is the same. If the counter is=0, all orders have been closed and we can play music.

I also noticed that the condition

         if(Uslovie1==true) //Bid+ma6
           {
            if((Bid>=ma1-X*Point && Bid<ma1) || (Bid<=ma1+X*Point && Bid>ma1))
              {
                 {y=true;}
              }
           }

It doesn't seem to be related to a certain order, so what is it for in the loop?

I would check it before the closing loop.

Reason: