Questions from Beginners MQL5 MT5 MetaTrader 5 - page 548

 
edutak:

I'd hate to show the code, as the first version proved to be very survivable. It survived for 10 years without optimisation. Now I want to make it a bit more complex and profitable.

The code is very simple.

Still, orders may be opened on every tick because of an error in the logic.

Check before opening a position to see if it is already open or not.
 
Vitalii Ananev:
Check before you open a position to see if it is already open or not.
Right?
for(int pos=0; pos<OrdersTotal(); pos++)
     {
      OrderSelect(pos,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magik)
         kolpos++;
     }
 

Before closing is like this.

 if(OrdersTotal()!=0)
         if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES))
            if(OrderType()==OP_BUY)
 
edutak:
Right?
   for(int pos=OrdersTotal()-1; pos>=0; pos--)         
   {
      if(!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magik)  kolpos++;
   }

It goes something like this.

It's better to make it into a function.

int CountOrder(string Smb,int Magik)
{
   int Total = OrdersTotal();
   if (Total==0) return(0);
   int kolpos = 0;
   for(int pos=Total-1; pos>=0; pos--)         
   {
      if(!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()==Smb && OrderMagicNumber()==Magik)  kolpos++;
   }
   return(kolpos);
}
 
edutak:

Before closing is like this.

You don't have a check for magik and symbol before closing. Can close trades of another EA or trades opened manually.
 
Vitalii Ananev:
It goes something like this.

No, the problem has not been solved. it is not in this unit.

Can you please explain the difference between my option and yours?

 
edutak:

No, the problem has not been solved. it is not in this unit.

Can you please explain the difference between my option and yours?

Then you are doing something wrong, work out your code.

Mine is better :)

...

This way you won't miss any order. In your version, if at that moment some position is closed or an order is deleted, you can skip any of the remaining ones.

 
Vitalii Ananev:

So you're doing something wrong, deal with your code.

Mine is better :)

...

This way you won't miss any order. If your version shows that if at that moment some position is closed or an order is deleted, then you can skip some of the remaining ones.

I declared it as a global variable, but it gives me an error.

int CountOrder;
 
edutak:

I declared it as a global variable, but it gives an error.

What I have written to you is a function.

Here's an example of how to use it.

int Count = CountOrder(Symbol(),12345);

if (Count==0)
{
//можно открывать
}
 
Vitalii Ananev:

What I have written to you is a function.

Here is an example of how to use it.

Confused