Code for alternating long and short trade

 

Greetings all,

I need a code that will only allow my expert to go long if the last trade was short and only go short if the last trade was long.

Thanks...........

 

Depends on what you mean by "Last trade". Last Trade Opened Last trade closed is EA concerned only with its own trades or does it include the trades of other EA's

extern int BuyMode = OP_BUY ;
extern int magicnumber=1111111 ;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  int iMyOrder = -1 ;
  
  //Restarting EA so check for current active Order
  iMyOrder = ActiveOrder( magicnumber,Symbol() ) ; 
  if ( iMyOrder >= 0 )
     {
      OrderSelect(iMyOrder, SELECT_BY_POS, MODE_TRADES ) ;
      if(OrderType() == OP_BUY)  BuyMode = OP_BUY ;
      if(OrderType() == OP_SELL) BuyMode = OP_SELL ;
      //other order types if needed
     }
   if ( iMyOrder<0 ) 
   {
    //Do a orders history search which is beyond scope of this answer
   } 
  Print("initilised") ;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  Print("Deinitialised") ;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
string sSymbol=OrderSymbol() ;

if ( ActiveOrder( magicnumber, Symbol() )< 0 ) //Then no active orders on symbol 
    {
        if ( BuyMode==OP_BUY )
           {
           //some code deciding to make buy
           //OrderClose(....etc....)
           BuyMode=OP_SELL ;
           } 
        if ( BuyMode==OP_SELL )
           {
           //some code deciding to make sell
           //OrderClose(....etc....)
           BuyMode=OP_BUY ;
           } 
            
     return(0);
}
//+------------------------------------------------------------------+
///Functions
//+-------
int ActiveOrder( int MN, string sSymbol )  //Return index number of my order or -1 for none
   { 
    int total = OrdersTotal();
    for(int i=total-1;i>=0;i--)
      {
       if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
          { 
           if (OrderMagicNumber()==MN && OrderSymbol()== sSymbol)  // Magic Number Block
             {
              return(i) ; //Assumes only one order open with this magicnumber
             }
          }
      }
    return(-1) ; //No active Orders
   }
Appologies for the messy code structure as I am New to MT4 coding
 

Hi Ickyrus,

Thanks for reply, I need it to be for last order closed.

I may be able to change MODE_TRADES to MODE_HISTORY in your code.

It only trades from 1 EA

Thanks again......

 

Example of using the history search https://www.mql5.com/en/forum/125067.
There are problems with the history search as it only searches what the MT4 account tab displays.
-
Instead of starting at zero go backwards with
for(int i=OrdersHistoryTotal()-1;i>=0;i--)

However you also need to be sure that it is the LAST order that you are getting perhaps a full search and look for the greatist OrderCloseTime() && magicnumber to be sure you are getting the correct LAST
providing ordertotal() is already zero.

Reason: