Bug on the timer on the EA

 

Hi,

i'm new to EA, i tried to code an EA that take position when stochastics are above or below 80-20 zones and when H4 candles cross moving average...

but there is a bug on the code, it doesn't take any positions. I think, it's the part with all the loops and the orderselect


extern double lots=0.02;


void OnTick()
  {
   string signal = "";
   
   
   //define stochastics and moving average 20
   
   double K1=iStochastic(NULL,PERIOD_D1, 5,3,3,MODE_SMA,0,MODE_MAIN,1);
   double D1=iStochastic(NULL,PERIOD_D1, 5,3,3,MODE_SMA,0,MODE_SIGNAL, 1);
   
   double SMA= iMA(NULL,PERIOD_H4,20,0,MODE_SMA,PRICE_MEDIAN,1);
   
   
   
   
  
  //sell signal (if stochastics are above 80 zone, the EA checks if price cross SMA during 90 candles)
  if ((K1 > 80)&&(D1 > 80))
   {
   for (int j=0;j<=90;j++)
   if (iClose(_Symbol, PERIOD_H4,2) > SMA && iClose(_Symbol, PERIOD_H4,1) < SMA)
      {
         for(int z=OrdersHistoryTotal() - 1; z >= 0; z--)
         {
          OrderSelect(z,SELECT_BY_POS, MODE_HISTORY);
            if (TimeCurrent()- OrderCloseTime() >= 20*4*60*60)
            {
               signal="sell";
               continue;
            }
         }
      }
   } 
   
   //sell 
   if (signal=="sell" && OrdersTotal()==0)
   {
   OrderSend(_Symbol,OP_SELL,lots,Bid,3,Bid*1.0075,Bid/1.005,NULL,0,0, Green);
   }
   
   
  
  
  
   
   //buy signal (if stochastics are below 20 zone, the EA checks if price cross SMA during 90 candles)
  if ((K1 < 20) && (D1 < 20))
   {
   for (int i=0;i<=90;i++)
   if (iClose(_Symbol, PERIOD_H4,2) < SMA && iClose(_Symbol, PERIOD_H4,1) > SMA) 
      {
         for(int y=OrdersHistoryTotal() - 1; y >= 0; y--)
         {
          OrderSelect(y,SELECT_BY_POS, MODE_HISTORY);
            if (TimeCurrent()- OrderCloseTime() >= 20*4*60*60)
            {
               signal="buy";
               continue;
            }
         }
      }
   } 
   
   
   
   //buy
   if (signal=="buy" && OrdersTotal()==0)
   {
   OrderSend(_Symbol,OP_BUY,lots,Ask,3,Ask/1.0075,Ask*1.005,NULL,0,0, Green);
   }
   
 
  }

also i would try to add some lines to close and modify orders when it gets to a certain ammount of pips based on the orderopenprice; i coded this that i initially added to the code but it didn't work either:

(initially i didn't put  TP on the ordersend, so the bug don't come from that)
for (int z= OrdersTotal()-1;z>=0;z++)
   {
      if (OrderSelect(z,SELECT_BY_POS,MODE_TRADES))
      {
         if (Ask<= OrderOpenPrice()/1.005  && OrderType()==OP_SELL)
         OrderClose(OrderTicket(),lots/2,Ask,3,Blue);
         OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-20,OrderOpenPrice()/1.0075,0,Blue); 
   
         if (Bid>= OrderOpenPrice()*1.005  && OrderType()==OP_BUY)
         OrderClose(OrderTicket(),lots/2,Ask,3,Blue);
         OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+20,OrderOpenPrice()*1.0075,0,Blue);
      }
   }


thanks for your help by advance

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.