My code won't execute any SELL trade - MQL4

 

Hi Folks,

I've been working on this EA. the idea is to follow the trend and enter at pull backs. The problem is when the trend stalls and reverse, we don't know whether it's for real or just another pull back. So I programed the EA to enter an opposite trade with larger position with the hope that it will breakeven. 

I don't intent to discuss the strategy itself, but rather, I have a technical problem. That is, my trade (market order) should only open in opposite direction of the last trade. So I did this in my Buy Trade function


   

 double lb=lastbuy(); double ls = lastsell(); 

    int lastOrderType = 10;

    if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES)==True)

    {

      lastOrderType = OrderType();

    };

    if(lastOrderType == 0)return;

    if(ls>Bid) return;

It works well. What puzzles me is that I did the same for my Sell Trade function, but it won't execute any Sell Trade. The code goes like this


  

 double ls=lastsell(); double lb=lastbuy(); 

   int lastOrderType = 10;

   if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES)==True)

   {

      lastOrderType = OrderType();

   };

   if(lastOrderType == 1)return;

   if(lb<Ask) return;


What did I do wrong? Any help is much appreciated.

 

Use the SRC button to post your code.


 
Jing Wang:

Hi Folks,

I've been working on this EA. the idea is to follow the trend and enter at pull backs. The problem is when the trend stalls and reverse, we don't know whether it's for real or just another pull back. So I programed the EA to enter an opposite trade with larger position with the hope that it will breakeven. 

I don't intent to discuss the strategy itself, but rather, I have a technical problem. That is, my trade (market order) should only open in opposite direction of the last trade. So I did this in my Buy Trade function


   

It works well. What puzzles me is that I did the same for my Sell Trade function, but it won't execute any Sell Trade. The code goes like this


  


What did I do wrong? Any help is much appreciated.

Check your lastbuy() function
 
Mladen Rakic:
Check your lastbuy() function

Thanks for the suggestion. I've indeed checked the lastbuy() many times. Here it is.


double   lastbuy(void)
{
 double priceBought=0;
  
 orders_total=OrdersTotal();

 for(int pos=orders_total-1; pos>=0; pos--) {
  if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol()!=Symbol())continue;
  if(OrderMagicNumber()!=MagicNumber) continue; // not our order
   if(OrderType()==OP_BUY)
    {
     priceBought=OrderOpenPrice();
     break;
    }
   else continue;
  }  
//---
 return(priceBought);
}
 

Hi Folks,


I've found the bug. It was my lastsell()... As the function returns 0 value when there is no open sell trade, the sell == 0 which is always less than the current Bid price.


thanks for your kind reply.

Reason: