OrderTotal Help ?

 
   int x=1;  //Global Variable


   if(OrdersTotal()<=x)                                     ////////////////SELL
   if(CODE)
   {
   ticket= OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,0,0,NULL,0,0,Red);
   
   }
   
   
   for(i=0;i<OrdersTotal();i++)
      { 
      select= OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      
   if(OrderType()==OP_SELL &&OrderSymbol()==Symbol())
   if(CODE)
   {
   closeticket=OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
   }
   
     }
     
   if(OrdersTotal()<=x)                                     ////////////////BUY
   if(CODE)
   {
   ticket= OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,0,0,NULL,0,0,Red);
   }
   
   
   for(i=0;i<OrdersTotal();i++)
      { 
      select= OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      
   if(OrderType()==OP_BUY &&OrderSymbol()==Symbol())
   if(CODE)
   {
   closeticket=OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
   }
 

Hi

I struggled with making my EA do 2 trades max I mean I did It but I need it to do one SELL and if it needs to it can make one BUY so total equals 2 trades but never do the same type twice

(I mean 2 SELL or 2 BUY) I tried OrderType() <=x but it doesn't work any idea how to do it ?

Thanks 

 

Explain what your problem is.

What is the variable CODE and where does it get its value from? 

 
Keith Watford:

Explain what your problem is.

What is the variable CODE and where does it get its value from?  

My problem is with the code 

if(OrdersTotal()<=x)

It can make 2 trades Maximum but I don't need it to do 2 Sells or 2 buys so my problem is I need it to make 2 trades Maximum(if it needs to) only if its 1Buy and 1SELL .

 
themasterx7:

My problem is with the code 

It can make 2 trades Maximum but I don't need it to do 2 Sells or 2 buys so my problem is I need it to make 2 trades Maximum(if it needs to) only if its 1Buy and 1SELL .

you will able to help ?

 
themasterx7:

you will able to help ?

You just want to allow 1 buy and 1 sell trade ?

Why don't you declare 2 global variables, like :

int buy_trade=0,sell_trade=0;

Then you test them :

if (buy_trade>0)
{
        //close buy
        //set buy_trade to 0 if closeorder right
} else
{
        //open buy
        //set buy_trade to 1 if sendorder right
}

No ?

 

OrdersTotal() already includes buy and sell so it will be 2 trades max independent of the order type buy or sell.

If you want it to be 1 buy and 1 sell then you have to check the two orders and compare the two order types if they differ it's a buy/sell or sell/buy, and if they are equal its either a buy/buy or a sell/sell.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of...
 
remcous:

You just want to allow 1 buy and 1 sell trade ?

Why don't you declare 2 global variables, like :

Then you test them :

No ?

Hi sir

First of all thank you for your answer really appreciated, so you mean like this :

if (buy_trade==0)
{
        //Open Buy Trade
}
 if (buy_trade==1)
{
        //Close the Trade
        
}
else 
{
//print error
}
 
themasterx7:

Hi sir

First of all thank your answer really appreciated, so you mean like this :

Can't I do it like this ?

if (OrdersTotal()==0)
{
        //Open Buy Trade
}
 if (OrdersTotal()==1)
{
        //Close the Trade
        
}
else 
{
//print error
}
 
themasterx7:

Can't I do it like this ?

Of course, but how you detect trade type (buy or sell) ?

Maybe you can do a function who tell you is a trade exist ?

Like this :

bool is_exist_trade(int order_operation)
{

  int total=OrdersTotal(); 
  int order_type=0;

  for(int pos=0;pos<total;pos++) 
    { 
     if(OrderSelect(pos,SELECT_BY_POS)==true) 
     {
         order_type=OrderType(); 
         if (order_type==order_operation) return true;
     } 
    } 
   return false;   
}
  

And now, you can do :

if (is_exist_trade(OP_BUY))
{
        //close your buy trade
} else
{       
        //open your buy trade
}

if (is_exist_trade(OP_SELL))
{
        //close your sell trade
} else
{       
        //open your sell trade
}
 
remcous:

Of course, but how you detect trade type (buy or sell) ?

Maybe you can do a function who tell you is a trade exist ?

Like this :

And now, you can do :

I think your code is better, I have this one I don't know if it will work:

int OrderType;

if
(OrdersTotal()==0) { OrderType=OrderType();         //Open Buy Trade } if (OrdersTotal()==1) { //OrderType=OrderType();         //Close the Trade          } else { //print error }

I will try it all and see what works.

Reason: