how to detect the order is belong to which currency?

 

Hi i am new to mql,

I am trying to create an EA to trade automatic. The EA must only can have only one trade on each currency. which means that if i attach the EA to the chart, the EA must detect is there an Real Order running OR any pending order available for the currency that attached.

If there is any Real Order or Pending order, it must be discard and wait for no real order or pending order in the terminal..

The problem is..... even when i have a real trade order OR pending ticket in terminal, the code execute what i wanted, but it cannot detect whether it is same to the currency which attach with this EA or not.

below is my code,anyone who can help me, pls advise....



/////// My code ////////

//+------------------------------------------------------------------+
//| test_order.mq4 |
//| xxx |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "xxx"
#property link "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Alert("EA attached"); //Test
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
string Symb=Symbol();
int total=OrdersTotal();
if (total>0){
Alert("Got trade/pending ticket in interminal..."); //Test

if (OrderSymbol()!= Symb){
Alert("Got trade/pending ticket in terminal, but not same currency"); //Test
}else{
Alert("Got trade/pending ticket in terminal which is same to the currency attach with this EA"); //Test
}
}else{
Alert("Got trade/pending ticket in interminal..."); //Test
}
//----
return(0);
}
//+------------------------------------------------------------------+

 

J

Basic examples of counting open orders and counting pending ones

int OpenTradesForMNandPair(int iMN, string sSymbol)
{
int icnt, itotal, retval;

retval=0;
itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++)
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
       if(OrderMagicNumber() == iMN)
        if (OrderSymbol() ==sSymbol)
         if(OrderType() <= OP_SELL)
           {
             retval++;
           }

     }

return(retval);
}


int PendingTradesForMNandPair(int iMN, string sSymbol)
{
int icnt, itotal, retval;

retval=0;
itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++)
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
       if(OrderMagicNumber() == iMN)
        if (OrderSymbol() ==sSymbol)
         if(OrderType() > OP_SELL)
           {
             retval++;
           }

     }

return(retval);
}

-BB-

 

Yes, you have to use the OrderSelect function. The rest is correct though.

If you don't know the ticket number of the order then you should use a for loop to cycle through all the open trades.

Chris

 
BarrowBoy:

J

Basic examples of counting open orders and counting pending ones

-BB-


-BB-, i dont have the magic number since we dont know anyone that use the EA at the first place still running trade or not. if earlier stage i do open trade or pending trade that not dont with this EA, the EA should wait for the current trade or pending trade to close then create a Boolean signal. How without the magic number, pls advise.
 
chrisbenjy:

Yes, you have to use the OrderSelect function. The rest is correct though.

If you don't know the ticket number of the order then you should use a for loop to cycle through all the open trades.

Chris


OrderSelect function? Mind you kindly give me a full example since i am learning from scratch. Thanks.
 
BarrowBoy:

J

Basic examples of counting open orders and counting pending ones

-BB-

-BB-...

is it like this.....? .......................as below?

//+------------------------------------------------------------------+
//| test_order.mq4 |
//| Joel Loh |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "xxx"
#property link "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Alert("EA attached"); //Test
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int iMN = 0;
string sSymbol = Symbol();
int opentrade = OpenTradesForMNandPair(int iMN, string sSymbol);
int pendingtrade = PendingTradesForMNandPair(int iMN, string sSymbol);
Alert(opentrade);
Alert(pendingtrade);
//----
return(0);
}
//+------------------------------------------------------------------+

int OpenTradesForMNandPair(int iMN, string sSymbol)
{
int icnt, itotal, retval;

retval=0;
itotal=OrdersTotal();

for(icnt=0;icnt<itotal;icnt++)
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if(OrderMagicNumber() == iMN)
if (OrderSymbol() ==sSymbol)
if(OrderType() <= OP_SELL)
{
retval++;
}

}

return(retval);
}

int PendingTradesForMNandPair(int iMN, string sSymbol)
{
int icnt, itotal, retval;

retval=0;
itotal=OrdersTotal();

for(icnt=0;icnt<itotal;icnt++)
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if(OrderMagicNumber() == iMN)
if (OrderSymbol() ==sSymbol)
if(OrderType() > OP_SELL)
{
retval++;
}

}

return(retval);
}
 
joelloh:

-BB-, i dont have the magic number since we dont know anyone that use the EA at the first place still running trade or not. if earlier stage i do open trade or pending trade that not dont with this EA, the EA should wait for the current trade or pending trade to close then create a Boolean signal. How without the magic number, pls advise.

Thanks alot -BB-, it works.
 
chrisbenjy:

Yes, you have to use the OrderSelect function. The rest is correct though.

If you don't know the ticket number of the order then you should use a for loop to cycle through all the open trades.

Chris


chrisbenjy, thanks alot..i got it.
 
joelloh:
How without the magic number, pls advise.

If no magic number was allocated by script or EA, the default of zero will be found

-BB-