Problem in pending orders

 

Hello guys


I tried to code an EA, but i have a problem in pending orders.

when signal called and pending order did set, if pending order get not reach and open, EA dose not delete that and dose not set new pending order for new signal.

What should i do now?



Most part of my EA

input string StartTime      = "00:00";
input string EndTime        = "23:00"; 
input int    Diff           = 150;   
input int    TakeProfit     = 150;   
input int    Stoploss       = 150;
input int    AddPips        = 50;
input double Lotsize        = 0.1; 
input double Multiply       = 2; 
input int    Max_Cycle      = 3;
input bool   BrokerIsECN    = false;
input int    Magic_Number   = 12345;

input string             com1       = "****** Indicator Settings ******";
input int                Shift = 20;

string gs_comment = "SSA";
datetime gd_close_time = TimeCurrent();
bool gd_close_all;
datetime time_bar;
int gd_signal;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--    
  int gd_last_tp=-1; 
  if(!gd_close_all && Hit_TP(gd_last_tp,gd_close_time)) {
  gd_close_all = true; 
  }
  
  if(gd_close_all) {
     CloseAll();
     if(orderCount(-1,Magic_Number)<1) gd_close_all = false;
     return;
  }
       
  double buy_lots=0, sell_lots=0;  
  double buy_sl=0, sell_sl=0;  
  int cnt_trade=0, cnt_buy=0, cnt_sell=0, cnt_bstop=0, cnt_sstop=0;
  datetime last_time=0;
  int last_type =-1;
  double buy_pip_sl=0, sell_pip_sl=0;  
  double buy_pip_tp=0, sell_pip_tp=0;  
  int last_level=0;
  for(int cnt=0;cnt<OrdersTotal();cnt++)  {
   int select =  OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if(OrderSymbol()!=Symbol()) continue;
   if(OrderMagicNumber() == Magic_Number) {
      cnt_trade++;
      if(OrderType()<2&&OrderOpenTime() > last_time )  {
        last_time = OrderOpenTime() ;
        last_type =OrderType();
      }
      if(OrderType()==OP_BUY) {
      cnt_buy++;
      buy_lots = OrderLots();
      buy_sl = OrderStopLoss();
      buy_pip_sl = OrderOpenPrice() - OrderStopLoss();
      buy_pip_tp = OrderTakeProfit() - OrderOpenPrice();
      }
      if(OrderType()==OP_SELL) {
      cnt_sell++;
      sell_lots = OrderLots();
      sell_sl = OrderStopLoss();
      sell_pip_tp = OrderOpenPrice() - OrderTakeProfit();
      sell_pip_sl = OrderStopLoss() - OrderOpenPrice();
      }
      if(OrderType()==OP_BUYSTOP) cnt_bstop++;
      if(OrderType()==OP_SELLSTOP) cnt_sstop++;
      last_level = get_number(OrderComment());
      }
  }
  
  double gd_lotsize = CheckLots(Multiply*MathMax(buy_lots,sell_lots));
  
  if(last_level>=Max_Cycle) return;
  
  if(last_type==1 && cnt_bstop < 1) {
  int send = PendingOrder(Symbol(),OP_BUYSTOP,gd_lotsize,sell_sl,sell_sl-sell_pip_sl-AddPips*Point,sell_sl+sell_pip_tp+AddPips*Point,gs_comment+(string)(last_level+1),Magic_Number);
  
  }
  
  if(last_type==0 && cnt_sstop <1 )   {
  int send = PendingOrder(Symbol(),OP_SELLSTOP,gd_lotsize,buy_sl,buy_sl+buy_pip_sl+AddPips*Point,buy_sl-buy_pip_tp-AddPips*Point,gs_comment+(string)(last_level+1),Magic_Number);
  }
  
   if(Time[0]>time_bar && WorkingHour(StartTime,EndTime) ) {
     if(cnt_trade>0) { time_bar = Time[0]; gd_signal =0; }
     else {
     gd_signal = Cal_Signal();
     if(gd_signal>0) time_bar = Time[0];
     }
  }
  
 if(AccountEquity()<Lotsize*MarketInfo(Symbol(),MODE_MARGINREQUIRED)) return;

  if( gd_signal==1 && cnt_trade <1 ) {
     double price_stop;
     if(Highest(10)-High[1]<=Diff*Point) price_stop =High[1]+Diff*Point+Shift*Point; else price_stop = High[1]+Shift*Point;
     double sl =0, tp =0;
     if(Stoploss>0) sl = price_stop - Stoploss*Point;
     if(TakeProfit>0) tp = price_stop + TakeProfit*Point;
     int send = PendingOrder(Symbol(),OP_BUYSTOP,Lotsize,price_stop,sl,tp,gs_comment+"1",Magic_Number);
     if( send >0 ) gd_signal =0;
  }

  if( gd_signal==2 && cnt_trade <1 ) {
     double price_stop;
     if((High[1]-Lowest(10)>=Diff*Point) && (High[1]-Lowest(10)<11*Point)) price_stop =Low[1]-Diff*Point-Shift*Point; else price_stop = Low[1]-Shift*Point;
     double sl =0, tp =0;
     if(Stoploss>0) sl = price_stop + Stoploss*Point;
     if(TakeProfit>0) tp = price_stop - TakeProfit*Point;
     int send = PendingOrder(Symbol(),OP_SELLSTOP,Lotsize,price_stop,sl,tp,gs_comment+"1",Magic_Number);
     if( send >0 ) gd_signal =0;
  }  
  }
//+------------------------------------------------------------------+

int MarketOrder(string a_symbol_0, int a_cmd_8, double a_lots_12, double a_stoploss_28, double a_takeprofit_36,string a_comment, int a_magic) {
   int l_error_64; int l_ticket_60; int l_slippage_68 = 8;
   double a_point = MarketInfo(a_symbol_0,MODE_POINT);
   double a_stoplevel = MarketInfo(a_symbol_0,MODE_STOPLEVEL); 
   double a_price_20 = MarketInfo(a_symbol_0,MODE_ASK);
   color l_color_92 = clrGreen;
   double sl = 0, tp  = 0;
   if( a_stoploss_28 > 0 ) sl  = MathMin(a_price_20 - a_stoploss_28*a_point,a_price_20 - a_stoplevel*a_point);
   if( a_takeprofit_36 > 0 ) tp  = MathMax(a_price_20 + a_takeprofit_36*a_point,a_price_20 + a_stoplevel*a_point);
   if (a_cmd_8 == OP_SELL) { 
   l_color_92 = clrRed; 
   a_price_20 = MarketInfo(a_symbol_0,MODE_BID); 
   if( a_stoploss_28 > 0 ) sl = MathMax(a_price_20+a_stoploss_28*a_point,a_price_20 + a_stoplevel*a_point);
   if( a_takeprofit_36 > 0 ) tp  = MathMin(a_price_20 - a_takeprofit_36*a_point,a_price_20 - a_stoplevel*a_point);
   }   
   if (BrokerIsECN) {
   l_ticket_60 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, a_price_20, l_slippage_68, 0, 0, a_comment, a_magic, 0, l_color_92);
   if (l_ticket_60 > 0 && (sl != 0 || tp !=0 )) int gi_modify = OrderModify(l_ticket_60, OrderOpenPrice(), sl, tp , 0,l_color_92);
   } else {
   l_ticket_60 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, a_price_20, l_slippage_68, sl, tp, a_comment, a_magic, 0, l_color_92);
   }
   if (l_ticket_60 <= 0) {
      l_error_64 = GetLastError();
      return (0);
   }
   return (l_ticket_60);}

bool WorkingHour(string a_starthour , string a_endhour) {
  
  datetime gi_time_01 = StrToTime(TimeToStr(iTime(NULL,PERIOD_D1,0), TIME_DATE) + " " + a_starthour);
  datetime gi_time_02 = StrToTime(TimeToStr(iTime(NULL,PERIOD_D1,0), TIME_DATE) + " " + a_endhour);
 
  if( gi_time_01 > gi_time_02 && TimeCurrent() < gi_time_02 )
  gi_time_01 = StrToTime(TimeToStr(iTime(NULL,PERIOD_D1,1), TIME_DATE) + " " + a_starthour);
    
  datetime datetime_0 = TimeCurrent();
  if ( gi_time_01 < gi_time_02 && gi_time_01 <= datetime_0 && datetime_0 <= gi_time_02 ) return (true);
  if ( gi_time_01 > gi_time_02 && datetime_0 >= gi_time_01 ) return (true);
  
  return (false);
}




int PendingOrder( string a_symbol, int a_type, double a_lots, double a_price, double a_stoploss, double a_takeprofit,string a_comment, int a_magic ) {
  double a_ask = MarketInfo(a_symbol,MODE_ASK);
  double a_bid = MarketInfo(a_symbol,MODE_BID);
  double a_point = MarketInfo(a_symbol,MODE_POINT);
  int a_stoplevel = (int)MarketInfo(a_symbol,MODE_STOPLEVEL);
  datetime expirationTime = 0;
  color  a_color = clrBlue;
  if(a_type==OP_SELLSTOP||a_type==OP_SELLLIMIT ) {
     a_color = clrRed;
  }  
  int ticket;
  ticket=OrderSend(a_symbol,a_type,a_lots,a_price,0,a_stoploss,a_takeprofit,a_comment,a_magic,expirationTime,clrNONE);
  if(ticket==-1) Print("Error " + (string) GetLastError() +" when sending order.");
  return (ticket);
}


Please help me.

Thanks