I have a very odd problem: OrdersTotal() return 1 when starting my EA

 
Hi,

I have a very odd problem:
sometimes after starting my EA again (in Backtesting mode) i see that i have already open trades !
OrdersTotal() return 1 open trade
and it looks like the EA in skipping some code lines ...

what can be my problem ?

Thanks,

Avi
 
I think you're going to have to post your code for someone to offer feedback.
 
I think you're going to have to post your code for someone to offer feedback.


OK, i inserted my code , i hope it will help:

//+------------------EA1.mq4------------------------------------------------+
#property copyright "Ferman"
#property link      "http://www.metaquotes.net"
#include <stdlib.mqh>

int      ticket=0; 
extern double Lots=0.1;
extern int    Slippage=2;
extern bool   Skip = false;
//+------------------------------------------------------------------+
int init()
  {
   Print ("--- Init happened ---");  
   return(0);
  }
//+------------------------------------------------------------------+
int deinit()
  {

   Print("--- DE-Init happened --- ");
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
   double  Stoc=0;
   double  Stoc_pre_1_bar=0;
   double  Stoc_pre_2_bar=0;
   int     gle=0;

   
   Stoc=iStochastic(NULL,0,8,5,3,MODE_SMA,1,MODE_MAIN,0);
   Stoc_pre_1_bar=iStochastic(NULL,0,8,5,3,MODE_SMA,1,MODE_MAIN,1);
   Stoc_pre_2_bar=iStochastic(NULL,0,8,5,3,MODE_SMA,1,MODE_MAIN,2);
   
   Print("-----------",TimeToStr(iTime(NULL,PERIOD_H1,0),TIME_DATE|TIME_SECONDS),"-------------");
   Print("stoc = ", Stoc);
   Print("Stoc_pre_1_bar = ", Stoc_pre_1_bar);
   Print(OrdersTotal());
   
   if (OrdersTotal() < 1) 
    {
       //LONG TREN?
      if (Stoc >= 30 && Stoc < 35 && Stoc_pre_1_bar < Stoc )  // GO LONG !
      {
         
         if (!Skip)// Are we in long trend already ?, yes - lets skip it and wait for the next break
         {
            ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,NULL,0,White);
            gle=GetLastError();
            if(gle==0)
               Print("BUY  Ask=",Ask, " OrderTicket = ",ticket);
            else 
               Print("BUY-ERROR! Ask=",Ask," Stoc=",Stoc," gle=",gle);
                 
         }
      Skip = false; //wait for the next break
      return(0); 
      }
      return(0);

    }//if
   else // OredersTotal > 1 - Manage Open Orders
      {
         //Take Profit  or stop lost    
         if ((Stoc >= 90 ) || Stoc <= 10 )
         {
            Print("need to close orderticket: ",ticket); 
            if (OrderClose(ticket,Lots,Ask,5,Red))
               Print("Close  Ask=",Ask);
            else 
               Print ("Close  -----ERROR----- Ask=",Ask," Stoc=",Stoc," error desc: ",ErrorDescription(GetLastError()));
       
            return(0);
          }
       }
   
   return(0);
   }
//+------------------------------------------------------------------+



my problems are:

1) when starting my EA in backtesting mode (H1, EUR/USD, open price only), sometimes i see that i have already open orders (i am printing the OrdersTotal() in the start() func.)

2) i see sometimes that the program not starting in the begining ( i see prints that comes from the middle of the program, its like skipping ome lines in the start() ...

3) in the results i see orders, and in the journal we see zero orders (OrdersTotal()=0) all the period of testing ...

4) the main() func. dont start every 1 hour, why is that ? (we can see the prints)

i inserts the beginnings of the results :

2005.11.14 14:10:20	2005.11.04 04:00  EA1 EURUSD,H1: 0
2005.11.14 14:10:20	2005.11.04 04:00  EA1 EURUSD,H1: Stoc_pre_1_bar = 26.4706
2005.11.14 14:10:19	2005.09.22 08:00  EA1 EURUSD,H1: Stoc_pre_1_bar = 13.3333
2005.11.14 14:10:19	2005.09.22 08:00  EA1 EURUSD,H1: stoc = 20.098
2005.11.14 14:10:19	2005.09.22 08:00  EA1 EURUSD,H1: -----------2005.09.22 08:00:00-------------
2005.11.14 14:10:19	2005.09.22 07:00  EA1 EURUSD,H1: 0
2005.11.14 14:10:19	2005.09.22 07:00  EA1 EURUSD,H1: Stoc_pre_1_bar = 3.2258
2005.11.14 14:10:19	2005.09.22 07:00  EA1 EURUSD,H1: stoc = 3.0612
2005.11.14 14:10:19	2005.09.22 07:00  EA1 EURUSD,H1: -----------2005.09.22 07:00:00------------- [b] we jump over many days !!! [/b]
2005.11.14 14:10:19	2005.09.22 06:00  EA1 EURUSD,H1: 0
2005.11.14 14:10:18	2005.08.08 08:00  EA1 EURUSD,H1: stoc = 3.5088
2005.11.14 14:10:18	2005.08.08 08:00  EA1 EURUSD,H1: -----------2005.08.08 08:00:00-------------
2005.11.14 14:10:18	2005.08.08 07:00  EA1 EURUSD,H1: 0 [b] zero orders all the testing time [/b]
2005.11.14 14:10:18	2005.08.08 07:00  EA1 EURUSD,H1: Stoc_pre_1_bar = 12.3457
2005.11.14 14:10:18	2005.08.08 07:00  EA1 EURUSD,H1: stoc = 14.9254
2005.11.14 14:10:18	2005.08.08 07:00  EA1 EURUSD,H1: -----------2005.08.08 07:00:00-------------
2005.11.14 14:10:18	2005.08.08 06:00  EA1 EURUSD,H1: 0
2005.11.14 14:10:18	2005.08.08 06:00  EA1 EURUSD,H1: Stoc_pre_1_bar = 16.0494     [b]<-- here we see skiping over some code lines [/b]



i hope we have all the details for understanding my problems, thank you guys you are doing a great job !

Ferman

Reason: