HowTo get ticket number and status for open trades.

 

I need to know is if there are any existing open buy or sell transactions when my EA starts.


Studying MQL4 language specs, the best i could do to get the details as required was as in the following function.



int findopenbuysell(string buyorsell){
for (int j = 0; j < OrdersTotal(); j++) {
OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
Comment("j=",j,"Ordertype=",OrderType()) ;
if (buyorsell=="buy" ){if (OrderType()==0){return(OrderType());break;} else {return(-1);break;} }
if (buyorsell=="sell"){if (OrderType()==1){return(OrderType());break;} else {return(-1);break;} }
return(-1);
}

}



where buyorsell can be either "buy" or "sell"

All the function does is if you call

findopenbuysell(buy)

It returns "0" if there is at least one open buy and -1 if none.

Similarly

findopenbuysell(sell)

returns "1" if there is at least one open sell and -1 if none.


It unfortunately does not work because OrderType() is seemingly always 0 in my function.



Anyone have soime advice how I can correct this?



Also if anyone know how I can get the ticketnumber for an existing open buy or sell I would appreciate it.

Somehow I cannot figure that out.

 
nutzergruppe wrote >>

I need to know is if there are any existing open buy or sell transactions when my EA starts.


Studying MQL4 language specs, the best i could do to get the details as required was as in the following function.


int findopenbuysell(string buyorsell){
for (int j = 0; j < OrdersTotal(); j++) {
OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
Comment("j=",j,"Ordertype=",OrderType()) ;
if (buyorsell=="buy" ){if (OrderType()==0){return(OrderType());break;} else {return(-1);break;} }
if (buyorsell=="sell"){if (OrderType()==1){return(OrderType());break;} else {return(-1);break;} }
return(-1);
}

}


where buyorsell can be either "buy" or "sell"

All the function does is if you call

findopenbuysell(buy)

It returns "0" if there is at least one open buy and -1 if none.

Similarly

findopenbuysell(sell)

returns "1" if there is at least one open sell and -1 if none.

It unfortunately does not work because OrderType() is seemingly always 0 in my function.


Anyone have soime advice how I can correct this?


Also if anyone know how I can get the ticketnumber for an existing open buy or sell I would appreciate it.

Somehow I cannot figure that out.

you was trying if (OrderType()==OP_BUY)?

 

Actually, the code is good, just one mistake:

int findopenbuysell(string buyorsell){
for (int j = 0; j < OrdersTotal(); j++) {
OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
Comment("j=",j,"Ordertype=",OrderType()) ;
if (buyorsell=="buy" ){if (OrderType()==0){return(OrderType());break;} else {return(-1);break;} }
if (buyorsell=="sell"){if (OrderType()==1){return(OrderType());break;} else {return(-1);break;} } 

}
return(-1);
}

 

Try that :

This works only if you have only 1 buy and/or 1 sell. If your EA works with more orders,currencies or whatever add search criterias as mn, symbol,profit, opentime, etc....

Dam.

ordtot=OrdersTotal();
for ( i=0; i<ordtot; i++)
   {
    OrderSelect(i,SELECT_BY_POS);
    if ( OrderType()==OP_BUY )
      ltn=OrderTicket();
    if ( OrderType()==OP_SELL );
      stn=OrderTicket();  
   }  


 

EADeveloper,


Yes I did try ==OP_BUY, as the manual prescribes, but it did not work. I then added a comment line which printed out the values of OrderType (see my code).

It then printed "0" rather than OP_BUY and "1" rather than "OP_SELL", so I reverted to what it actually outputs which was "0" and "1".




 
nutzergruppe:

EADeveloper,


Yes I did try ==OP_BUY, as the manual prescribes, but it did not work. I then added a comment line which printed out the values of OrderType (see my code).

It then printed "0" rather than OP_BUY and "1" rather than "OP_SELL", so I reverted to what it actually outputs which was "0" and "1".




Sorry cannot help you more than that because i hardly understand your code..... Do simple AND organize you code with line jumps and spaces.

 
nutzergruppe wrote >>

EADeveloper,


Yes I did try ==OP_BUY, as the manual prescribes, but it did not work. I then added a comment line which printed out the values of OrderType (see my code).

It then printed "0" rather than OP_BUY and "1" rather than "OP_SELL", so I reverted to what it actually outputs which was "0" and "1".

Hi, ich mach es immer so, vielleicht hilft es Dir oder du kannst es in Deine funktion übernehmen:

int hasbuy=0;
int hassell=0;


for(int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol()&&OrderMagicNumber() == MagicN)
{

if (OrderType()==OP_BUY)
{
hasbuy++;
}
if (OrderType()==OP_SELL)
{
hassell++;
}
}
}//for (i=0..

Reason: