Download MetaTrader 5

Searching for opened orders my magic number

To add comments, please log in or register
Why to sell products in MetaTrader AppStore? Find that out from the article!
Stan4o1
118
Stan4o1 2015.11.27 14:30 

Hi, I made some changes to one of my EAs it is about searching for already opened orders by their magic nbr. I have set different magic nbr for the ea, when i attached it to different currencies, but still  order opened stops the other EAs from trading with different currencies. Can you help me ? This is the function i created, and  in the ontick function i have set an if (Arethereorders() ==  1 || AreThereOrders() == 2) then it checks for buy/sell signals

 )

int AreThereOrders ()
{
if (OrdersTotal()<1)
{return 1;}
else
{
int i;
for (i=0;i<OrdersTotal();i++)
   {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
   continue;
   if(OrderMagicNumber() != magic)
   {return 2;}
   break;
   }
   return 0;
   }
   }
Carl Schreiber
6593
Carl Schreiber 2015.11.27 15:22  

Probably you wanted to write this:

for (i=0;i<OrdersTotal();i++)
   {
   if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false ) 
   continue;
..
Stan4o1
118
Stan4o1 2015.11.27 15:51  

You were right! Thanks But after opening different orders for different currencies, it opened too many orders on the same currency, How can i fix it?

Carl Schreiber
6593
Carl Schreiber 2015.11.27 17:59  
Stan4o1:

You were right! Thanks But after opening different orders for different currencies, it opened too many orders on the same currency, How can i fix it?

If it was only a temporary mistake in your code close'em by hand?

Stan4o1
118
Stan4o1 2015.11.27 19:06  
It is not temporary, as soon as i attach the EA to a second chart it starts opening many many orders, i dont know whats wrong i even added Refreshates but it wouldn`t help.If someone could help me i would be very gratefull
int AreThereOrders ()
{
if (OrdersTotal()<1)
{return 1;}
else
{
int i;
for (i=0;i<OrdersTotal();i++)
   {
   if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   continue;
   if(OrderMagicNumber() != magic)
   {return 2;}
   break;
   }
   return 0;
   }
   RefreshRates();
   }




And then i use




 if (AreThereOrders() == 1 || AreThereOrders() ==2)
     {    

and there follows the buy or sell condition
 
Carl Schreiber
6593
Carl Schreiber 2015.11.27 20:04  

You probably wanted to write:

if(OrderMagicNumber() == magic) return 0; // no further order
...
//and at the end
return 2; // no matching open order found 
Stan4o1
118
Stan4o1 2015.11.27 20:21  
Im sorry i tried it but it still doesnt work - The EA opens many orders
Stan4o1
118
Stan4o1 2015.11.27 20:26  
int AreThereOrders ()
{
if (OrdersTotal()<1)
{return 1;}
else
{
int i;
for (i=0; i<OrdersTotal() ; i++)
   {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && ( OrderMagicNumber()== magic)) return 0;
   break;
   }
   
   }
   return 2 ;
   }

This is what you meant right?
Keith Watford
Moderator
9624
Keith Watford 2015.11.27 21:39  
for (i=0; i<OrdersTotal() ; i++)
   {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && ( OrderMagicNumber()== magic)) return 0;
   break;
   }
The break means that only 1 order is checked, remove it.
Stan4o1
118
Stan4o1 2015.11.28 21:10  

Thanks

Stan4o1
118
Stan4o1 2015.11.30 11:19  
int AreThereOrders ()
{
if (OrdersTotal()<1)
{
return 1;
}
else if (OrdersTotal()>0)
{

int i;

for (i=OrdersTotal()-1; i>=0; i--)
   {
   if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   continue;
   if( OrderMagicNumber()!= magic && OrderSymbol()!=Symbol()) 
   {
   return 2;
   }
 
  
  }
  
 
    }
   return 0 ;   
   }
This is what i made but ,i dont know why, it still keeps opening too many orders, not like i wanted just 1 per currency/magic number. I dont` know what to do .
12
To add comments, please log in or register