Help in coding.

To add comments, please log in or register
tt.aa
17
tt.aa  
Hi everyone.

Could anyone one please teach me how to control the number of the opened trades in my EA ? (please have a look on my code below)

It is a 2 moving averages (not crossover) (it is above/below) using candle number 0, so when the fast MA is above the slow MA it opens a buy order straight away, and the opposite for sell.

I know it might open a lot of trades as I'm using candle number 0, but I'm only trying to learn how to control the number of opened trades that's all.

The problem is that the EA open trades on every tick,,,,,
but what I would like is for the EA to open only 1 trade for every time the conditions for buy/sell orders are met.

For example:
If the EA opened a 1 buy trade,
then the conditions are met for a sell trade then the EA should open 1 sell trade,
and if the conditions are met again for buy I would like the EA to add another buy trade, and so on.

I would appreciate any help.

void OnTick()
  {
  double fastMA = iMA (NULL,60,5,0,0,0,0);
  double slowMA = iMA (NULL,60,30,0,0,0,0);
  
  if (fastMA>slowMA)
     if (Orderscnt()==0) //for first buy order
     int ticket_1 = OrderSend (Symbol(),OP_BUY,lot,Ask,30,0,0,NULL,magicno,0,clrGreen);
      
  if (fastMA<slowMA)
     if (Orderscnt()==0) // for first sell order 
     int ticket_2 = OrderSend (Symbol(),OP_SELL,lot,Bid,30,0,0,NULL,magicno,0,clrRed);
        
  if (fastMA>slowMA)
     if (Orderscnt()>0) // for any future buy order
     int ticket_1 = OrderSend (Symbol(),OP_BUY,lot,Ask,30,0,0,NULL,magicno,0,clrGreen);
      
  if (fastMA<slowMA)
     if (Orderscnt()>0) // for any future sell order 
     int ticket_2 = OrderSend (Symbol(),OP_SELL,lot,Bid,30,0,0,NULL,magicno,0,clrRed); 
  }
//+------------------------------------------------------------------+
int Orderscnt(int type=-1)
{
 int cnt=0;
 for(int i=0;i<OrdersTotal();i++)
 {
  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  {
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magicno)
   {
    if(OrderType()==type||type==-1)
    {
     cnt++;
    }
   } 
  }
 }
 return(cnt);
}  
William Roeder
20207
William Roeder  
tt.aa: Could anyone one please teach me how to control the number of the opened trades in my EA ? (please have a look on my code below)
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Your first if/if says fast above slow and no open orders, open a buy. The third if/if says fast above slow and at least one open order, open another buy. Stop opening orders if there is already one open. Delete the third and fourth.
To add comments, please log in or register