need help for Stochastic every Cross

 

hi all 

my new in programming

make exprit tow Stochastic the first Stochastic with trend

the second  Stochastic with price dispersion

 but i have problem open only one order sell or buy need fix problem every Cross Stochastic open order 

can anyone help me Thank

//+------------------------------------------------------------------+
//|                                                      Sto pro.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//#property strict
extern bool TrendTrade=true;
extern string ATR="------ATR--------";
extern int  KperiodATR=5;
extern int  DperiodATR=3;
extern int  slowingATR=3;
extern int ATRPRATR=14;
extern int ATRTMATR=0;
extern double ATRLivelATR=0.0016;
extern int StochBuyLineATR=20;
extern int StochSellLineATR=80;             
extern int sall_bayATR=1; 
input int  TakeprofitATR=0, 
           StoplossATR=0;
extern int TrailingStopATR=0;
extern int TrailingProfitATR=0;
extern int TrailingStepATR=0;
extern int HalfLotsCloseBuyATR=0;
extern int HalfLotsCloseSellATR=0; 
extern int MagicNumberATR=25555;                        
extern string Stochastic="-----------Stochastic1-----------";
extern bool StochTrade=true;
extern int  Kperiod=5;
extern int  Dperiod=3;
extern int  slowing=3;
extern int   ATRPR=14;
extern int   ATRTM=0;
extern double ATRLivel=0.0016;
extern int  StochBuyLine=20;
extern int  StochSellLine=80;
extern bool ClaseSignal=false;
extern int  sall_bay=1;
input int Takeprofit=0, 
             Stoploss=0;         
input double Lots=0.1,
            RiskPercent=0;
extern int TrailingStop=0;
extern int TrailingProfit=0;
extern int TrailingStep=0;
extern int HalfLotsCloseBuy=0;
extern int HalfLotsCloseSell=0;  
           
extern int MagicNumber=2533;
double point; 
int    Q,Lot_Decimal;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
 if(MarketInfo(Symbol(),MODE_MINLOT)<0.1)Lot_Decimal=2;else Lot_Decimal=1;  
 if(_Digits==5||_Digits==3)Q=10;else Q=1;                             
 if(_Digits<4)point=0.01;else point=0.0001; 
 if(!IsExpertEnabled())Alert("Must Enable AutoTrading"); 
 if(!IsTradeAllowed())Alert("Must Enable ALLOW LIVE TRADING"); 
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(MarketInfo(Symbol(),MODE_MINLOT)<0.1)Lot_Decimal=2;else Lot_Decimal=1;  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  HideTestIndicators(TRUE);
  //----------
  double  ATRR=iATR(NULL,ATRTM,ATRPR,1);
  double  St1 =iStochastic(Symbol(),0,Kperiod,Dperiod,slowing,0,0,0,1);
  double  St2 =iStochastic(Symbol(),0,Kperiod,Dperiod,slowing,0,0,1,1);
  double  St12 =iStochastic(Symbol(),0,Kperiod,Dperiod,slowing,0,0,0,2);
  double  St22 =iStochastic(Symbol(),0,Kperiod,Dperiod,slowing,0,0,1,2);
  double  ATRRATR=iATR(NULL,ATRTMATR,ATRPRATR,1);
  double  St1ATR =iStochastic(Symbol(),0,KperiodATR,DperiodATR,slowingATR,0,0,0,1);
  double  St2ATR =iStochastic(Symbol(),0,KperiodATR,DperiodATR,slowingATR,0,0,1,1);
  double  St12ATR =iStochastic(Symbol(),0,KperiodATR,DperiodATR,slowingATR,0,0,0,2);
  double  St22ATR =iStochastic(Symbol(),0,KperiodATR,DperiodATR,slowingATR,0,0,1,2);
   //-----------------
  if(StochTrade){
  if(sall_bay>=1&&ATRR<=ATRLivel&&St1>St2&&St12<=St22&&St1<=StochBuyLine&&Orderscnt(OP_BUY)==0&&CountOrdersTime()==0&&(LastOrder("Type")==OP_SELL)){BuyOrder();} else
  if(sall_bay<1&&ATRR<=ATRLivel&&St1>St2&&St12<=St22&&St1<=StochBuyLine&&Orderscnt(OP_BUY)==0&&CountOrdersTime()==0){BuyOrder();}
  if(sall_bay>=1&&ATRR<=ATRLivel&&St1<St2&&St12>=St22&&St1>=StochSellLine&&Orderscnt(OP_SELL)==10&&CountOrdersTime()==0&&(LastOrder("Type")==OP_BUY||LastOrder("Type")==0)){SellOrder();}else
  if(sall_bay<1&&ATRR<=ATRLivel&&St1<St2&&St12>=St22&&St1>=StochSellLine&&Orderscnt(OP_SELL)==0&&CountOrdersTime()==0){SellOrder();}}
  //-----------------ATR-----------
  if(TrendTrade){
  if(sall_bayATR>=1&&ATRRATR>=ATRLivelATR&&St1ATR>St2ATR&&St12ATR<=St22ATR&&St1ATR<=StochBuyLineATR&&OrderscntATR(OP_BUY)==0&&CountOrdersTimeATR()==0&&(LastOrderATR("Type")==OP_SELL)){BuyOrderATR();}else
  if(sall_bayATR<1&&ATRRATR>=ATRLivelATR&&St1ATR>St2ATR&&St12ATR<=St22ATR&&St1ATR<=StochBuyLineATR&&OrderscntATR(OP_BUY)==0&&CountOrdersTimeATR()==0){BuyOrderATR();}
  if(sall_bayATR>=1&&ATRRATR>=ATRLivelATR&&St1ATR<St2ATR&&St12ATR>=St22ATR&&St1ATR>=StochSellLineATR&&OrderscntATR(OP_SELL)==0&&CountOrdersTimeATR()==0&&(LastOrderATR("Type")==OP_BUY||LastOrderATR("Type")==0)){SellOrderATR();}else
  if(sall_bayATR<1&&ATRRATR>=ATRLivelATR&&St1ATR<St2ATR&&St12ATR>=St22ATR&&St1ATR>=StochSellLineATR&&OrderscntATR(OP_SELL)==0&&CountOrdersTimeATR()==0){SellOrderATR();}}
  //---------------------------------------

  if(TrailingStop>0){MoveTrailingStop();}
  if(HalfLotsCloseBuy>0){CloseOrdersH(OP_BUY);}
  if(HalfLotsCloseSell>0){CloseOrdersH(OP_SELL);}
  //-----------------------------------
  if(TrailingStopATR>0){MoveTrailingStopATR();}
  if(HalfLotsCloseBuyATR>0){CloseOrdersHATR(OP_BUY);}
  if(HalfLotsCloseSellATR>0){CloseOrdersHATR(OP_SELL);}
//---
   
  }

//-----------------------------------------

int Orderscnt(int type=-1) 
{ 
 int cnt=0; 
 for(int i=0;i<OrdersTotal();i++) 
 { 
  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
  { 
   if(OrderSymbol()==Symbol()&&(OrderMagicNumber()==MagicNumber)) 
   { 
    if(OrderType()==type||type==-1) 
    { 
     cnt++; 
    } 
   }  
  } 
 } 
 return(cnt); 
} 
//---------------
int OrderscntATR(int type=-1) 
{ 
 int cnt=0; 
 for(int i=0;i<OrdersTotal();i++) 
 { 
  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
  { 
   if(OrderSymbol()==Symbol()&&(OrderMagicNumber()==MagicNumberATR)) 
   { 
    if(OrderType()==type||type==-1) 
    { 
     cnt++; 
    } 
   }  
  } 
 } 
 return(cnt); 
} 
//----------------
double LotManage()
{
 double lot=MathFloor(AccountBalance()*RiskPercent/1000)/100; 
 if(lot<MarketInfo(Symbol(),MODE_MINLOT))lot=MarketInfo(Symbol(),MODE_MINLOT);
 if(lot>MarketInfo(Symbol(),MODE_MAXLOT))lot=MarketInfo(Symbol(),MODE_MAXLOT);
 return(NormalizeDouble(lot,Lot_Decimal));
} 
//-----------------
void BuyOrder() 
{ 
 double NewLots,TP,SL; 
 if(RiskPercent>0)NewLots=NormalizeDouble(LotManage(),Lot_Decimal);
 else NewLots=Lots;
 if(Stoploss!=0){SL=Ask-Stoploss*point;}else SL=0; 
 if(Takeprofit!=0){TP=Ask+Takeprofit*point;}else TP=0; 
bool  tk=OrderSend(Symbol(),OP_BUY,NewLots,NormalizeDouble(Ask,Digits),5*Q,SL,TP,"Moh",MagicNumber,0,Blue); 
}  
//------------------------------
void SellOrder() 
{ 
 double NewLots,TP,SL; 
 if(RiskPercent>0)NewLots=NormalizeDouble(LotManage(),Lot_Decimal);
 else NewLots=Lots;
 if(Stoploss!=0){SL=Bid+Stoploss*point;}else SL=0; 
 if(Takeprofit!=0){TP=Bid-Takeprofit*point;}else TP=0; 
 bool tk=OrderSend(Symbol(),OP_SELL,NewLots,NormalizeDouble(Bid,Digits),5*Q,SL,TP,"Moh",MagicNumber,0,Red); 
} 
//------------------
void BuyOrderATR() 
{
 double NewLots,TP,SL; 
 if(RiskPercent>0)NewLots=NormalizeDouble(LotManage(),Lot_Decimal);
 else NewLots=Lots;
 if(Stoploss!=0){SL=Ask-StoplossATR*point;}else SL=0; 
 if(Takeprofit!=0){TP=Ask+TakeprofitATR*point;}else TP=0; 
bool  tk=OrderSend(Symbol(),OP_BUY,NewLots,NormalizeDouble(Ask,Digits),5*Q,SL,TP,"MohATR",MagicNumberATR,0,Blue); 
}  
//------------------------------
void SellOrderATR() 
{ 
 double NewLots,TP,SL; 
 if(RiskPercent>0)NewLots=NormalizeDouble(LotManage(),Lot_Decimal);
 else NewLots=Lots;
 if(Stoploss!=0){SL=Bid+StoplossATR*point;}else SL=0; 
 if(Takeprofit!=0){TP=Bid-TakeprofitATR*point;}else TP=0; 
 bool tk=OrderSend(Symbol(),OP_SELL,NewLots,NormalizeDouble(Bid,Digits),5*Q,SL,TP,"MohATR",MagicNumberATR,0,Red); 
} 
//----------------
double LastOrder(string info,int type=-1) 
{ 
 for(int i=OrdersHistoryTotal()-1;i>=0;i--) 
 { 
  bool select=OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); 
  string   sy=OrderSymbol(), 
           mm=OrderComment(); 
  int      mn=OrderMagicNumber(), 
           ty=OrderType(); 
  double   lo=OrderLots(), 
           op=OrderOpenPrice(), 
           cp=OrderClosePrice(), 
           pt=OrderProfit(), 
           tp=OrderTakeProfit(), 
           sl=OrderStopLoss();   
  datetime om=OrderOpenTime(), 
           cm=OrderCloseTime(); 
                     
  if(sy==Symbol()&&(mn==MagicNumber)) 
  { 
   if(ty==type||type==-1) 
   { 
    if(info=="Lots")return(lo); 
    else if(info=="OpenPrice")return(op); 
    else if(info=="ClosePrice")return(cp); 
    else if(info=="Profit")return(pt); 
    else if(info=="Type")return(ty); 
    else if(info=="TP")return(tp); 
    else if(info=="SL")return(sl);  
   } 
  }  
 } 
 return(0); 
} 
//-----------------
//----------------
double LastOrderATR(string info,int type=-1) 
{ 
 for(int i=OrdersHistoryTotal()-1;i>=0;i--) 
 { 
  bool select=OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); 
  string   sy=OrderSymbol(), 
           mm=OrderComment(); 
  int      mn=OrderMagicNumber(), 
           ty=OrderType(); 
  double   lo=OrderLots(), 
           op=OrderOpenPrice(), 
           cp=OrderClosePrice(), 
           pt=OrderProfit(), 
           tp=OrderTakeProfit(), 
           sl=OrderStopLoss();   
  datetime om=OrderOpenTime(), 
           cm=OrderCloseTime(); 
                     
  if(sy==Symbol()&&(mn==MagicNumberATR)) 
  { 
   if(ty==type||type==-1) 
   { 
    if(info=="Lots")return(lo); 
    else if(info=="OpenPrice")return(op); 
    else if(info=="ClosePrice")return(cp); 
    else if(info=="Profit")return(pt); 
    else if(info=="Type")return(ty); 
    else if(info=="TP")return(tp); 
    else if(info=="SL")return(sl); 
 
   } 
  }  
 } 
 return(0); 
} 
//--------------
void MoveTrailingStop()
{
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
      if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol()&&(OrderMagicNumber()==MagicNumber))
      {
         if(OrderType()==OP_BUY)
         {
            if(TrailingStop>0&&NormalizeDouble(Ask-TrailingStep*point,Digits)>NormalizeDouble(OrderOpenPrice()+TrailingProfit*point,Digits))  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-TrailingStop*point,Digits))||(OrderStopLoss()==0))
               {
                 bool t= OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-TrailingStop*point,Digits),OrderTakeProfit(),0,Blue);
                }
            }
         }
         else 
         {
            if(TrailingStop>0&&NormalizeDouble(Bid+TrailingStep*point,Digits)<NormalizeDouble(OrderOpenPrice()-TrailingProfit*point,Digits))  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)>(NormalizeDouble(Ask+TrailingStop*point,Digits)))||(OrderStopLoss()==0))
               {
                 bool tt=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailingStop*point,Digits),OrderTakeProfit(),0,Red);
               }
            }
         }
      }
   }
   } 
 //---------
   int CountOrdersTime()
{
 int cnt=0;
 for(int i=0;i<OrdersHistoryTotal();i++) 
 {
  bool select=OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
  if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
  {
   if(OrderOpenTime()>=Time[0])cnt++;
  }
 }
 return(cnt);
}
//----------
void CloseOrdersH(int type=-1) 
{ 
 bool select,close; 
 for(int ii=OrdersTotal()-1;ii>=0;ii--) 
 { 
  select=OrderSelect(ii,SELECT_BY_POS,MODE_TRADES); 
  if(OrdersTotal()>0&&OrderSymbol()==Symbol()&&(OrderMagicNumber()==MagicNumber)&&(OrderLots()== Lots)) 
  { 
   if(OrderType()==type||type==-1) 
   { 
   if(OrderType()==OP_BUY&&(Bid>(OrderOpenPrice()+HalfLotsCloseBuy*point))){close=OrderClose(OrderTicket(),OrderLots()/2,NormalizeDouble(Bid,Digits),5);}
   if(OrderType()==OP_SELL&&(Ask<(OrderOpenPrice()-HalfLotsCloseSell*point))){close=OrderClose(OrderTicket(),OrderLots()/2,NormalizeDouble(Ask,Digits),5);} 
    
   }  
  } 
 } 
} 
 
//------------------
void MoveTrailingStopATR()
{
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
      if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol()&&(OrderMagicNumber()==MagicNumberATR))
      {
         if(OrderType()==OP_BUY)
         {
            if(TrailingStopATR>0&&NormalizeDouble(Ask-TrailingStepATR*point,Digits)>NormalizeDouble(OrderOpenPrice()+TrailingProfitATR*point,Digits))  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-TrailingStopATR*point,Digits))||(OrderStopLoss()==0))
               {
                 bool t= OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-TrailingStopATR*point,Digits),OrderTakeProfit(),0,Blue);
                }
            }
         }
         else 
         {
            if(TrailingStopATR>0&&NormalizeDouble(Bid+TrailingStepATR*point,Digits)<NormalizeDouble(OrderOpenPrice()-TrailingProfitATR*point,Digits))  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)>(NormalizeDouble(Ask+TrailingStopATR*point,Digits)))||(OrderStopLoss()==0))
               {
                 bool tt=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailingStopATR*point,Digits),OrderTakeProfit(),0,Red);
               }
            }
         }
      }
   }
   } 
   //---------
   //---------
   int CountOrdersTimeATR()
  {
  int cnt=0;
  for(int i=0;i<OrdersHistoryTotal();i++) 
  {
  bool select=OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
  if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumberATR)
  {
   if(OrderOpenTime()>=Time[0])cnt++;
  }
 }
 return(cnt);
}
//----------
//----------
void CloseOrdersHATR(int type=-1) 
{ 
 bool select,close; 
 for(int ii=OrdersTotal()-1;ii>=0;ii--) 
 { 
  select=OrderSelect(ii,SELECT_BY_POS,MODE_TRADES); 
  if(OrdersTotal()>0&&OrderSymbol()==Symbol()&&(OrderMagicNumber()==MagicNumberATR)&&(OrderLots()== Lots)) 
  { 
   if(OrderType()==type||type==-1) 
   { 
   if(OrderType()==OP_BUY&&(Bid>(OrderOpenPrice()+HalfLotsCloseBuyATR*point))){close=OrderClose(OrderTicket(),OrderLots()/2,NormalizeDouble(Bid,Digits),5);}
   if(OrderType()==OP_SELL&&(Ask<(OrderOpenPrice()-HalfLotsCloseSellATR*point))){close=OrderClose(OrderTicket(),OrderLots()/2,NormalizeDouble(Ask,Digits),5);} 
    
   }  
  } 
 } 
} 
//-------------
double LastOrderCurrent(string info,int type=-1) 
{ 
 for(int i=OrdersTotal()-1;i>=0;i--) 
 { 
  bool select=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); 
  string   sy=OrderSymbol(), 
           mm=OrderComment(); 
  int      mn=OrderMagicNumber(), 
           ty=OrderType(); 
  double   lo=OrderLots(), 
           op=OrderOpenPrice(), 
           pt=OrderProfit(), 
           tp=OrderTakeProfit(), 
           sl=OrderStopLoss();   
  datetime om=OrderOpenTime(); 
                     
  if(sy==Symbol()&&mn==MagicNumber) 
  { 
   if(ty==type||type==-1) 
   { 
    if(info=="Lots")return(lo); 
    else if(info=="OpenPrice")return(op); 
    else if(info=="Profit")return(pt); 
    else if(info=="Type")return(ty); 
    else if(info=="TP")return(tp); 
    else if(info=="SL")return(sl); 
  //  else if(info=="OpenTime")return(om); 
   // else if(info=="Comment")return(mm); 
   } 
  }  
 } 
 return(0); 
} 
//---------------
//---------------
Reason: