Why 's my EA do many order ? please help!

 
#include <stdlib.mqh>
int start()
  {
//----
double L=iLow(NULL,PERIOD_D1,1);
double H=iHigh(NULL,PERIOD_D1,1);
double H_0=iHigh(NULL,PERIOD_D1,0);
double C=iClose(NULL,PERIOD_D1,0);
double O=iOpen(NULL,PERIOD_D1,0);
double L5=iLow(NULL,PERIOD_M5,0);
double SL = 50*Point; // StopLoss
double TP = 100*Point; // TakeProfit
double Spread=MarketInfo(Symbol(),MODE_SPREAD)*Point;
int ticket,err,q;
double P  = 10*Point; // Breakout points
double x1=0;
double x2=0;
double a=1;
double b=50*Point;
double Check1=H-C;
double Check2=C-L;
//----

 //----
for( q=0;q<OrdersTotal();q++)
 {
  if (OrderSelect(q, SELECT_BY_POS, MODE_TRADES)==true && OrderSymbol()==Symbol())
   {
// checking positions, if there are some opended orders, lets check them with the indicator
   if (OrderType()==OP_BUYSTOP)
     {
      
       return(0); 
     }
   if (OrderType()==OP_SELLSTOP)
     {
      
       return(0); 
     }
     
   }  
}
//----*/             
             
if(a==1)
{
      if(O < H )
                   {
                       if(C-H >= P) 
                        {
                          x1=1;
                        }
                   }
          
    
          

         
      if(x1==1 && H_0 - C >=50*Point)
                           {
                              x2=1;
                           }
             
}               
    if(Hour()==0 && Minute()==0 && Seconds()==0  )
         {
            a=1;
         }    
       
//----
if(x2==1 && a==1)   
             
        {
          OrderSend(Symbol(),OP_BUYSTOP,0.1,H_0+P+Spread,0,H_0+P-SL+Spread,H_0+P+TP+Spread,NULL,0,iTime( Symbol(), PERIOD_D1, 0 ) + 86400);
          {
           a=2; 
            Print("a=",a);     
          }   
    
          if (ticket==-1)
            {
              err=GetLastError();
              Print("error(",err,")");
            }
        }
      
    
//----
   return(0);
  }
//+------------------------------------------------------------------+

I do not know why my EA do many order ? i just want to do 1 per day!

please show me what I wrong! 

 
toi10005doi:

I do not know why my EA do many order ? i just want to do 1 per day!

please show me what I wrong! 

When your pending order gets activated your code will place another pending order,  there is nothing stopping it . . .  if you want only one order per day you have to find if a currently open order was placed today or a closed order was placed today,  if there has then you don't open a new order . . today.

Also this will not work,  ticket is always 0 . . .

if (ticket==-1)
            {
              err=GetLastError();
              Print("error(",err,")");
            }

 is that all you need to know if there is an error ?  just the error number ?  wouldn't it help to know the Bid, Ask, opening price, SL, TP, etc, etc ?

 
RaptorUK:

When your pending order gets activated your code will place another pending order,  there is nothing stopping it . . .  if you want only one order per day you have to find if a currently open order was placed today or a closed order was placed today,  if there has then you don't open a new order . . today.

Also this will not work,  ticket is always 0 . . .

 is that all you need to know if there is an error ?  just the error number ?  wouldn't it help to know the Bid, Ask, opening price, SL, TP, etc, etc ?


Basically it doesn't matter his ticket has value 0   and doesn't get another value  
 
deVries:

Basically it doesn't matter his ticket has value 0   and doesn't get another value  
Sure,  but when he fixes the update to the ticket variable the error reporting will still be virtually useless . . . 
 
Thank you. But in my code, there is a number "a", i think when the first order activated, "a" would change, and there are no more orders untill the day end.
 
toi10005doi:
Thank you. But in my code, there is a number "a", i think when the first order activated, "a" would change, and there are no more orders untill the day end.


At Start();

you are given a value 1  every new tick....

So your thinking is wrong 

 
deVries:


At Start();

you are given a value 1  every new tick....

So your thinking is wrong 


Thank so much!! can you show me how to fix it?

 
toi10005doi:


Thank so much!! can you show me how to fix it?

 


Do an attempt yourself if you wanna learn

Hint :  give a value 1 at beginning new day 

 
#include <stdlib.mqh>
int start()
  {
//----
double L=iLow(NULL,PERIOD_D1,1);
double H=iHigh(NULL,PERIOD_D1,1);
double H_0=iHigh(NULL,PERIOD_D1,0);
double C=iClose(NULL,PERIOD_D1,0);
double O=iOpen(NULL,PERIOD_D1,0);
double L5=iLow(NULL,PERIOD_M5,0);
double SL = 50*Point; // StopLoss
double TP = 100*Point; // TakeProfit
double Spread=MarketInfo(Symbol(),MODE_SPREAD)*Point;
int ticket,err,q;
double P  = 10*Point; // Breakout points
double x1;
double x2;
bool a=false;
double b=50*Point;
double Check1=H-C;
double Check2=C-L;
int first_order = 0;
int last_order_day = -1;
int current_day = 0;
int exp = iTime( Symbol(), PERIOD_D1, 0 ) + 86400;
datetime ax;
//----

 //----
for( q=0;q<OrdersTotal();q++)
 {
  if (OrderSelect(q, SELECT_BY_POS, MODE_TRADES)==true && OrderSymbol()==Symbol())
   {
// checking positions, if there are some opended orders, lets check them with the indicator
   if (OrderType()==OP_BUYSTOP)
     {
      
       return(0); 
     }
   if (OrderType()==OP_SELLSTOP)
     {
      
       return(0); 
     }
     
   }  
}
//----*/  


           
             

      if(O < H )
                   {
                       if(C-H >= P) 
                        {
                          x1=1;
                        }
                   }
          
    
          

         
      if(x1==1 && (H_0 - C >=(50*Point)))
                           {
                              x2=1;
                             
                              current_day = Day();
                              if (current_day != last_order_day) 
                              {
                                 first_order = 0;
                                 last_order_day = current_day;
                              }
                           }
             
                  
       
//----
if(x2==1 && first_order == 0)   
             
        {
          ticket=OrderSend(Symbol(),OP_BUYSTOP,0.1,H_0+P+Spread,0,H_0+P-SL+Spread,H_0+P+TP+Spread,NULL,0,exp);
         
          first_order = 1;
          if (ticket==-1)
            {
              err=GetLastError();
              Print("error(",err,")");
            }
        }
      
    
//----
   return(0);
  }
//+------------------------------------------------------------------+
I write new code and have the same result. Haizzz
 
toi10005doi:
I write new code and have the same result. Haizzz
Your firstorder and last_order_day  variables are being declared for each and every tick,  so for each tick they are reset,  make then static int  or declare them globally ( before start() )
 
#include <stdlib.mqh>
int start()
  {
//----
double L=iLow(NULL,PERIOD_D1,1);
double H=iHigh(NULL,PERIOD_D1,1);
double H_0=iHigh(NULL,PERIOD_D1,0);
double C=iClose(NULL,PERIOD_D1,0);
double O=iOpen(NULL,PERIOD_D1,0);
double L5=iLow(NULL,PERIOD_M5,0);
double SL = 50*Point; // StopLoss
double TP = 100*Point; // TakeProfit
double Spread=MarketInfo(Symbol(),MODE_SPREAD)*Point;
int ticket,err,q;
double P  = 10*Point; // Breakout points
double x1=0;
double x2=0;
double a=1;
double b=50*Point;
double Check1=H-C;
double Check2=C-L;
//----

 //----
for( q=0;q<OrdersTotal();q++)
 {
  if (OrderSelect(q, SELECT_BY_POS, MODE_TRADES)==true && OrderSymbol()==Symbol())    //Check OrderMagicNumber() place it inside the EA
   {
// checking positions, if there are some opended orders, lets check them with the indicator
//  check Also if OP_BUY   or OP_SELL
//  check OrderOpenTime() trades of your EA
   if (OrderType()==OP_BUYSTOP)
     {
      
       return(0); 
     }
   if (OrderType()==OP_SELLSTOP)
     {
      
       return(0); 
     }
     
   }  
}
/*
When your pending order gets activated your code will place another pending order,  there is nothing stopping it . . .  
if you want only one order per day you have to find if a currently open order was placed today or a closed order was placed today,  
if there has then you don't open a new order . . today.
*/             
             
if(a==1)
{
      if(O < H )
                   {
                       if(C-H >= P) 
                        {
                          x1=1;
                        }
                   }
          
    
          

         
      if(x1==1 && H_0 - C >=50*Point)
                           {
                              x2=1;
                           }
             
}               
    if(Hour()==0 && Minute()==0 && Seconds()==0  )
         {
            a=1;
         }    
       
//----
if(x2==1 && a==1)   
             
        {
          OrderSend(Symbol(),OP_BUYSTOP,0.1,H_0+P+Spread,0,H_0+P-SL+Spread,H_0+P+TP+Spread,NULL,0,iTime( Symbol(), PERIOD_D1, 0 ) + 86400);
          {
           a=2; 
            Print("a=",a);     
          }   
    
          if (ticket==-1)
            {
              err=GetLastError();
              Print("error(",err,")");
            }
        }
      
    
//----
   return(0);
  }
//+------------------------------------------------------------------+

Moderator
8393

RaptorUK 2013.03.28 20:05

When your pending order gets activated your code will place another pending order,  there is nothing stopping it . . .  if you want only one order per day you have to find if a currently open order was placed today or a closed order was placed today,  if there has then you don't open a new order . . today.

.

So first correct checking if you placed today already....

So you have to check if there are Orders opened today with using OrderOpenTime( )  in OrdersTotal( )  and OrdersHistoryTotal( )....

Reason: