EA enters in wrong positions in mt4

 

Hi all

I faced an issue with mql4 when I tried to write a simple code triggering a signal for entering into a position based on specific set of rules (the simplified code is below). the problem is that despite the set rules EA enters in wrong positions (it opens one candle after the rules become true) and trades where it is not supposed to . another problem is that it seems that after a while EA becomes inactive and does not enter into a trade and I need to drop the EA to the graph again.

it would be so nice if anyone could help me figure out the problem

here is the code

//+------------------------------------------------------------------+
//|                                                                  |
//|                                                                  |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright  "SM"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//-------------------------------------------------------------------+
double SAR0=iSAR(NULL,0,0.02,0.2,0);
double SAR1=iSAR(NULL,0,0.02,0.2,1);
double SL_Buy=Bid-200*Point()*10;          
double TP_Buy=Bid+50*Point()*10;          
double SL_Sell=Ask+200*Point()*10;
double TP_Sell=Ask-50*Point()*10;
double Buyprice=Ask;
double Sellprice=Bid;
int ticket;
double Lot=(AccountBalance()/10000000);    //provides dynamic Lot value based on account balance
int total;
bool email;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
  
//--------------------------------------BUY BLOCK-------------------------------------------------------------
if((Open[0]>SAR0)&&(Open[1]>SAR1)&&(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)>0))
{
ticket=OrderSend(Symbol(),OP_BUY,ND(Lot),ND(Buyprice),3*10,ND(SL_Buy),ND(TP_Buy),"my order",0,0,clrRed);
             
if(ticket<0)
  {
   // OrderSend error.
   Print("OrderSend failed with error #",GetLastError()); 
  }else
  {
                               Alert("buy order has been placed");
                              
             }
}
//----------------------------------------SELL BLOCK-------------------------------------------------------------
if((Open[0]<SAR0)&&(Open[1]<SAR1)&&(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)<0))
{
ticket=OrderSend(Symbol(),OP_SELL,ND(Lot),ND(Sellprice),3*10,ND(SL_Sell),ND(TP_Sell),"my order",0,0,clrBlue);
if(ticket<0)
  {
   // OrderSend error.
   Print("OrderSend failed with error #",GetLastError()); 
  }else
  {
                                Alert("Sell order has been placed");
                                
                                     
     }

}
  // making the EA exit after making an order
  if(ticket>0)
 {
 OrderSelect(ticket, SELECT_BY_TICKET);
      
      string direction;
      if(OrderType() == 0) 
      {
         direction = "Buy";
      } 
      else
      {
         direction = "Sell";
      }
  email=SendMail("Order placed",  "Order #"      + string(ticket)  + " Opened Succesfully\n" 
                                            +"Instrument: " + OrderSymbol()             + "\n"
                                            +"Type:  " + direction                 + "\n"
                                            +"Open Price: " + string(OrderOpenPrice())  + "\n"
                                            +"StopLoss: "   + string(OrderStopLoss())   + "\n"
                                            +"TakeProfit: " + string(OrderTakeProfit()) + "\n"
                                            +"total orders: " + string(OrdersTotal())   + "\n"                                           
                                            );
 ExpertRemove();
 }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//ND function normalizes ask, bid, take profit and stopiloss values:
double ND(double val)
{
   return(NormalizeDouble(val, Digits));
}
 
yazdforex: it would be so nice if anyone could help me figure out the problem
double SAR0=iSAR(NULL,0,0.02,0.2,0);
double SAR1=iSAR(NULL,0,0.02,0.2,1);
double SL_Buy=Bid-200*Point()*10;          
double TP_Buy=Bid+50*Point()*10;          
double SL_Sell=Ask+200*Point()*10;
double TP_Sell=Ask-50*Point()*10;
double Buyprice=Ask;
double Sellprice=Bid;
double Lot=(AccountBalance()/10000000);  
Those are constant. Assign them in OnTick.