[HELP] my EA opens too many unwanted one direction pending orders

 

Hi, could somebody please tell me what's wrong?

My EA should only open 2 pending orders: 1 buy stop and 1 sell stop. 

But when i test it in strategy tester SOME TIMES it opens too many unwanted sell stops (or buy stops). I mean when it comes to X candle it does open only 1 order but on other test the X candle opens up to 30 orders! There's no error 148 message at all at journal. 

Is it the problem of my code's, or the strategy tester's? 

Is it possible to code to avoid that such thing?

Thx in advance.

happy trading  :)

 

with respect, is not tester. we all look elsewhere at such times.

look at your code with cpu eyes - what code is executed each time start() called?

are there variables which are scoped local to a function eg, start() that should be at source file global scope - so having persistent memory instead of transient memory?

eg, if code relies on some state value from a previous start() call, yet always gets the same value....

just idea.

nobody can be sure - no code is there - only some English description - only MQL4 is relevant, yes?

 

Thx fbj for your quick respons.. :)

below is my whole code (still a two days scratch).. 


//+------------------------------------------------------------------+
//|                                                  GAJAH SUBUH.mq4 |
//|                                   Copyright © 2009, Siluman Cumi |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Siluman Cumi"
#property link      ""

extern double Target1 =bla bla,
              Open1   =bla bla,
              StopLoss=bla bla,
              Open2   =bla bla,
              Target2 =bla bla;
extern double    Lots=0.2;
extern int       Slippage=0;
extern int       Offset=3;
extern bool   DailyExpired=True;
//extern bool   DoubleOpositeWhenLoss=True;
           
extern string txComment="GAJAH SUBUH";
extern int MagicNumber=10000;

int tiketBuy, tiketSell;
datetime bartime=0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  { 
    /////////fibonacci//////////////
    double Hi=High[1], Lo=Low[1];    
    /////////   A     /////////
    double Delta=Hi-Lo,
           aOPSell=NormalizeDouble(Hi-(Delta)*Open1,Digits),     
           aTPSell=NormalizeDouble(Hi-(Delta)*Target1,Digits)+Offset*pointt(),  
           aOPBuy =Hi,    
           aTPBuy =NormalizeDouble(Hi-(Delta)*Target2,Digits)-Offset*pointt(),
           aSL    =NormalizeDouble(Hi-(Delta)*StopLoss,Digits);
    /////////   B     /////////    
    double bOPSell=Lo,   
           bTPSell=NormalizeDouble(Lo+(Delta)*Target2,Digits)+Offset*pointt(),   
           bOPBuy =NormalizeDouble(Lo+(Delta)*Open1,Digits),     
           bTPBuy =NormalizeDouble(Lo+(Delta)*Target1,Digits)-Offset*pointt(),
           bSL    =NormalizeDouble(Lo+(Delta)*StopLoss,Digits);
    //////// A or B ? ////////
    if (Open[0]<aOPBuy && Open[0]>aOPSell) {string ZoneCheck="A";}
    if (Open[0]<bOPBuy && Open[0]>bOPSell) {ZoneCheck="B";}
    
   /////////check trades/////////
    
    
   ///////////open position/////////
   
   if (Open[0]>0 && bartime != Time[0])
      { if (ZoneCheck=="A")
           {
            tiketBuy = OrderSend(Symbol(),OP_BUYSTOP,Lots,aOPBuy,Slippage,aSL,aTPBuy,txComment,MagicNumber,0,Green); 
            tiketSell= OrderSend(Symbol(),OP_SELLSTOP,Lots,aOPSell,Slippage,aSL,aTPSell,txComment,MagicNumber,0,Red);
            if(tiketBuy>0 && tiketSell>0) bartime = Time[0];
            }
        if (ZoneCheck=="B")
           {
            tiketBuy = OrderSend(Symbol(),OP_BUYSTOP,Lots,bOPBuy,Slippage,bSL,bTPBuy,txComment,MagicNumber,0,Green); 
            tiketSell= OrderSend(Symbol(),OP_SELLSTOP,Lots,bOPSell,Slippage,bSL,bTPSell,txComment,MagicNumber,0,Red);
            if(tiketBuy>0 && tiketSell>0) bartime = Time[0];
            }
           
       }   
   ////////// OCO ///////////
   for (int i = 0; i < OrdersTotal(); i++)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
          {
            if(OrderType()==OP_BUY && Ask>=OrderTakeProfit())
            {OrderDelete(tiketSell);
             return(0);}        
         
            if(OrderType()==OP_SELL && Bid<=OrderTakeProfit())
            {OrderDelete(tiketBuy);
             return(0);}          
                    
            
            if (TimeCurrent()-OrderOpenTime()>=86340 && DailyExpired==True)
            {OrderDelete(tiketBuy);
             OrderDelete(tiketSell);
             return(0);}
          
          }
       }
   ////////
   
   
   //////////////
   return(0);
  }
//+------------------------------------------------------------------+
double pointt()
{
   double pts;
   if(Digits==5 || Digits==3) pts = 10*(MarketInfo(Symbol(),MODE_POINT));
   else pts = MarketInfo(Symbol(),MODE_POINT);
   return (pts);
}  
Reason: