OrdersTotal() Function not working properly

 

Hi everyone. I am trying to create a simple moving average cross-over EA. I have a little bit of a problem. What I want to do is to first check the total number of orders before opening a buy/sell order. If the total number of orders is zero, only then will the EA opens a buy/sell order depending on the signal provided by the moving averages. I am using the OrdersTotal() Function for counting the total number of opened orders. The problem is whenever I try to test the EA in the strategy tester, the EA continually opens buy orders simultaneously even though the total number of orders returned by the OrdersTotal() Function is already greater than zero. Will someone please help.


Here is my code

extern int TakeProfit = 50;
extern int StopLoss = 25;
extern int FastMA = 5;
extern int FastMaShift = 0;
extern int FastMaMethod = 0;
extern int FastMaAppliedTo = 0;
extern int SlowMA = 21;
extern int SlowMaShift = 0;
extern int SlowMaMethod = 0;
extern int SlowMaAppliedTo = 0;
extern double LotSize = 0.01;
extern int MagicNumber = 1234;
int ordersTotal;


void OnTick()
  {

      ordersTotal = OrdersTotal();
      double PreviousFast = iMA(NULL, 0, FastMA, FastMaShift, FastMaMethod, FastMaAppliedTo, 2);
      double CurrentFast = iMA(NULL, 0, FastMA, FastMaShift, FastMaMethod, FastMaAppliedTo, 1);
      double PreviousSlow = iMA(NULL, 0, SlowMA, SlowMaShift, SlowMaMethod, SlowMaAppliedTo, 2);
      double CurrentSlow = iMA(NULL, 0, SlowMA, SlowMaShift, SlowMaMethod, SlowMaAppliedTo, 1);
      
      
      if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow )
      {
         
         
         if(OrdersTotal()==0)
         {
            OrderSend(_Symbol, OP_BUY, 0.01, Ask, 3, Ask-StopLoss*_Point, Ask+TakeProfit*_Point, NULL, 0, 0, Green);
         }
            
         
            
         

      }
      else if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow)
      {
         
         if(OrdersTotal()==0)
         {
            OrderSend(_Symbol, OP_SELL, 0.01, Bid, 3, Bid+StopLoss*_Point, Bid-TakeProfit*_Point, NULL, 0, 0, Red);
         }
            
         
            
         
      }
      
      Comment("Number of Orders = " + ordersTotal);
  }
 

This code is working fine. Tested on MT4 build 1170.

You should just check the value returned by OrderSend(), but that's not related to your issue I suppose.

Always 1 order at a time :


 
Alain Verleyen:

This code is working fine. Tested on MT4 build 1170.

You should just check the value returned by OrderSend(), but that's not related to your issue I suppose.

Always 1 order at a time :


Hello. Thanks for the help. I figured it out. It seems my stop loss is too small. That's why it was always hit. I made my stop loss 1000, and now I can confirm that there is really nothing wrong with the code but with the stoploss that I had set.

This is what I see in the visual mode of strategy tester before I had set the stoploss to 1000.


 
floraMerano:
if(OrdersTotal()==0)
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
Reason: