Pending and Market execution orders are being executed different than a specified price.

 

Hello

I am quite new in mql4 programming and recently I build my own EA which should execute pending and market orders at a specified price.

So, this is the code I wrote and yeah there is so much junk, but it works. The only problem I am facing is when I look at the results after running the EA I see that, for some days EA opens and closes the orders at the exact order like X.XXXX56, then it changes this to X.XXXX71 or less, I never changed the opening and closing pending orders a bit in the code, I wonder does market opening and closing times matter in this case? 


My algorithm is simple (  the code i shared is too messy, i'm sorry for that )

TP 30 pips


BUY 0.05 LOT

Range 10 pips

SELL 0.07 LOT


SL 40 pips

it should keep going if it keeps consolidating with increased number of lots, but, if it hits TP or SL in any case everything should close and I start with another buy order. Buy while testing I see that it works for some time, then fails to open buy stop order changing the x.xxxx16 to x.xxxx35, I don't just understand why it's not doing this always and making EA fail only in 3-4 times a year?


Please help


#include <Events.mq4>

extern int MagicNumber = 0;
const int StopLoss = 400;
const int TakeProfit = 300;

int OnInit()
  {

   BuyOrder(0.05);
   double SellStopNumber1 = SellStopOrder(0.07, Bid - 100*_Point);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

  }
  
void OnTick()
  {
  double Lot = 1;
  CheckEvents(MagicNumber);
  

      //3 Talik gacha, Buy Sell Buy Algoritm Tayyor
      for (int i = OrdersTotal()-1; i > 0; i--){
         if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true){
            if(eventSellStopOpened) {
               switch(OrdersTotal()){
                  case 2://Comment ( "Birinchi Sell stop urdi");
                     BuyStopOrder(0.05, Ask + 100*_Point);
                     break;
                  default :
                     
                     break;
               }
            }
            if(eventBuyStopOpened) {
               switch(OrdersTotal()){
                  case 2:
                     SellStopOrder(0.07, Bid - 100*_Point);
                     break;
                  default:
                     break;
               }
            }
           
         
         }
      }
   
   //AsosiyAlgoritm(Lot);
   Comment("Number of Buy positions : ", CountBuyPositions(), "\nNumber of Sell Positions : ", CountSellPositions());
   if(OrdersTotal()==3){
      if(CountBuyPositions()==2 && CountSellPositions()==1){ //Buy Sell Buy ochilganda 2 talik
           SellStopOrder(0.07, Bid - 100*_Point);
           }
   }
   if(OrdersTotal()==4){
      if(CountBuyPositions()==2 && CountSellPositions()==2){  //Buy Sell Buy Sell ochilganda 2 talik to'liq
           BuyStopOrder(0.09, Ask + 100*_Point);
   }
   }
   
   if(OrdersTotal()==5){
      if(CountBuyPositions()==3 && CountSellPositions()==2){ //Buy Sell Buy Sell Buy ochilganda 3 talik
           SellStopOrder(0.12, Bid - 100*_Point);
           }
   }
   
   if(OrdersTotal()==6){
      if(CountBuyPositions()==3 && CountSellPositions()==3){ //Buy Sell Buy Sell Buy Sell ochilganda 3 talik to'liq
           BuyStopOrder(0.16, Ask + 100*_Point);
           }
   }
   
   if(OrdersTotal()==7){
      if(CountBuyPositions()==4 && CountSellPositions()==3){ //Buy Sell Buy Sell Buy Sell Buy ochilganda 4 talik
           SellStopOrder(0.21, Bid - 100*_Point);
           }
   }
   
   if(OrdersTotal()==8){
      if(CountBuyPositions()==4 && CountSellPositions()==4){ //Buy Sell Buy Sell Buy Sell Buy Sell ochilganda  4 talik to'liq
           BuyStopOrder(0.28, Ask + 100*_Point);
      }
   }
   
   if(OrdersTotal()==9){
      if(CountBuyPositions()==5 && CountSellPositions()==4){ //Buy Sell Buy Sell Buy Sell Buy Sell Buy ochilganda 5 talik
           SellStopOrder(0.37, Bid - 100*_Point);
           }
   }
   
   if(OrdersTotal()==10){
      if(CountBuyPositions()==5 && CountSellPositions()==5){ //Buy Sell Buy Sell Buy Sell Buy Sell Buy Sellochilganda 5 talik to'liq
           BuyStopOrder(0.5, Ask + 100*_Point);
      }   
   }
   
   if(OrdersTotal()==11){
      if(CountBuyPositions()==6 && CountSellPositions()==5){ //Buy Sell Buy Sell Buy Sell Buy Sell Buy Sell Buy ochilganda 6 talik
           SellStopOrder(0.66, Bid - 100*_Point);
      }
   }
   
   if(OrdersTotal()==12){
      if(CountBuyPositions()==6 && CountSellPositions()==6){ //Buy Sell Buy Sell Buy Sell Buy Sell Buy Sell Buy Sell ochilganda 6 talik to'liq
           BuyStopOrder(0.88, Ask + 100*_Point);
      }
   }
   
   if(OrdersTotal()==13){
      if(CountBuyPositions()==7 && CountSellPositions()==6){ //7 talik
         SellStopOrder(1.17, Bid - 100*_Point);
      }
   }
   
   if(OrdersTotal()==14){
      if(CountBuyPositions()==7 && CountSellPositions()==7){ //7 talik to'liq
         BuyStopOrder(1.55, Ask + 100*_Point);
      }
   }
   
   if(OrdersTotal()==15){
      if(CountBuyPositions()==8 && CountSellPositions()==7){ //8 talik
         SellStopOrder(2.07, Bid - 100*_Point);
      }
   }
   
   if(OrdersTotal()==16){
      if(CountBuyPositions()==8 && CountSellPositions()==8){ // 8 talik to'liq
         BuyStopOrder(2.75, Ask + 100*_Point);
      }
   }
   
   if(OrdersTotal()==17){
      if(CountBuyPositions()==9 && CountSellPositions()==8){ //9 talik
         SellStopOrder(3.66, Bid - 100*_Point);
      }
   }
   
   if(OrdersTotal()==18){
      if(CountBuyPositions()==9 && CountSellPositions()==9){ //9 talik to'liq
         BuyStopOrder(4.87, Ask + 100*_Point);
      }
   }
   if(OrdersTotal()==19){
      if(CountBuyPositions()==10 && CountSellPositions()==9){ //10 talik
         SellStopOrder(6.47, Bid - 100*_Point);
      }
   }
   if(OrdersTotal()==20){
      if(CountBuyPositions()==10 && CountSellPositions()==10){ //10 talik to'liq
         BuyStopOrder(8.61, Ask + 100*_Point);
      }
   }
   if(OrdersTotal()==21){
      if(CountBuyPositions()==11 && CountSellPositions()==10){ //11 talik
         SellStopOrder(11.45, Bid - 100*_Point);      
      }
   }
   if(OrdersTotal()==22){
      if(CountBuyPositions()==11 && CountSellPositions()==11){ //11 talik to'liq
         BuyStopOrder(15.24, Ask + 100*_Point);
      }
   }
   if(OrdersTotal()==23){
      if(CountBuyPositions()==12 && CountSellPositions()==11){ //12 talik
         SellStopOrder(20.26, Bid - 100*_Point);
      }
   }    
   if(OrdersTotal()==24){
      if(CountBuyPositions()==12 && CountSellPositions()==12){ //12 talik to'liq
         BuyStopOrder(26.95, Ask + 100*_Point);
      }
   }
    //Buy TP bilan yopilganda nima ochadi

   
   if(eventSellClosed_TP){
      for (int f=1; f<=OrdersTotal(); f++)
     {                                        
        if(OrderSelect(f-1,SELECT_BY_POS)==true)
        {
            
            if(OrderType()==OP_BUYSTOP){
               int ordTicket = OrderTicket();
               OrderDelete(ordTicket);
            }  
        }
     }
   }

   if(eventBuyClosed_TP){
      for (int p=1; p<=OrdersTotal(); p++)
     {                                        
        if(OrderSelect(p-1,SELECT_BY_POS)==true)
        {
            if(OrderType()==OP_SELLSTOP){
               int ordTicket1 = OrderTicket();
               OrderDelete(ordTicket1);
            }

        }
     }
   }
   
   
   //Davomiy order ochib ketish
   if(OrdersTotal()==0){
      BuyOrder(0.05);
      SellStopOrder(0.07, Bid - 100*_Point);
   }

   CreateButtons();

  }
//+------------------------------------------------------------------+

/*
Sell Limit tepadegi Sell uchun, Sell Stop pasdegi sell uchun
*/

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
  {
//Istalgan pozitsiya bilan trade ni boshlash
   if(id==CHARTEVENT_OBJECT_CLICK)
     {

      //Agar BUY ga analiz qilsak BUY
      if(sparam=="BuyButton")
        {
         //Buy order ochiladi, SL = 80 pips, TP = 60 pips
         BuyOrder(1);
         //Sell Stop ochiladi
         SellStopOrder(1.4, Bid - 200*_Point);
         Comment("Buy bosildi");

         //Endi shunde funksiya kereki SellStop ishga tushgandan keyin BuyLimit ochadigan, Tekshirishdi ikki xil yo'li bor, 1. Ochilgan pozitsiyalar soni 2 ta bo'sa yoki Sell order ochilgan bo'sa.
        }

      //Agar SELL ga analiz qilsak SELL
      if(sparam=="SellButton")
        {
         Comment("Sell bosildi");
         SellOrder(1);
        }

     }

  }

void CreateButtons(){
//Buy Sell tugmalar dizayni

//Buy tugma 
   ObjectCreate(_Symbol, "BuyButton", OBJ_BUTTON, 0, 0, 0);

   ObjectSetInteger(_Symbol, "BuyButton", OBJPROP_XDISTANCE, 400);
   ObjectSetInteger(_Symbol, "BuyButton", OBJPROP_XSIZE, 50);
   ObjectSetInteger(_Symbol, "BuyButton", OBJPROP_YDISTANCE, 0);
   ObjectSetInteger(_Symbol, "BuyButton", OBJPROP_YSIZE, 50);
   ObjectSetInteger(_Symbol, "BuyButton", OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetString(_Symbol, "BuyButton", OBJPROP_TEXT, "BUY");


//Sell tugma
   ObjectCreate(_Symbol, "SellButton", OBJ_BUTTON, 0, 0, 0);

   ObjectSetInteger(_Symbol, "SellButton", OBJPROP_XDISTANCE, 450);
   ObjectSetInteger(_Symbol, "SellButton", OBJPROP_XSIZE, 50);
   ObjectSetInteger(_Symbol, "SellButton", OBJPROP_YDISTANCE, 0);
   ObjectSetInteger(_Symbol, "SellButton", OBJPROP_YSIZE, 50);
   ObjectSetInteger(_Symbol, "SellButton", OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetString(_Symbol, "SellButton", OBJPROP_TEXT, "SELL");
}


//Buy order funksiyalari
void BuyOrder(double LotSize)
  {
   OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, Ask - 400*_Point, Ask + 300*_Point, NULL, 0, 0, Green);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyLimitOrder(double Lotsize, double Price)
  {
   OrderSend(Symbol(), OP_BUYLIMIT, Lotsize, Price, 3, Ask - 400*_Point, Ask + 300*_Point, NULL, 0, 0, Green);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double BuyStopOrder(double Lotsize, double Price)
  {
   OrderSend(Symbol(), OP_BUYSTOP, Lotsize, Price, 3, Ask - 300*_Point, Ask + 400*_Point, NULL, 0, 0, Green);
  }

//Sell order funksiyalari
void SellOrder(double LotSize)
  {
   OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, Bid + 400*_Point, Bid - 300*_Point, NULL, 0, 0, Green);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellLimitOrder(double Lotsize, double Price)
  {
   OrderSend(Symbol(), OP_SELLLIMIT, Lotsize, Price, 3, Bid + 400*_Point, Bid - 300*_Point, NULL, 0, 0, Green);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double SellStopOrder(double Lotsize, double Price)
  {
   OrderSend(Symbol(), OP_SELLSTOP, Lotsize, Price, 3, Bid + 300*_Point, Bid - 400*_Point, NULL, 0, 0, Green);
  }
//+------------------------------------------------------------------+


void AsosiyAlgoritm(double Lot){
   //Events for closed orders
   if(eventBuyClosed_SL){
      Comment("Buy Closed with SL");
   }
   if(eventBuyClosed_TP){
      Comment("Buy Closed with TP");
      SellOrder(1);
   }
   if(eventSellClosed_SL){
      Comment("Sell Closed with SL");
   }
   if(eventSellClosed_TP){
      Comment("Sell Closed with TP");
      BuyOrder(1);
   }
   
   //Events for Opened orders
   if(eventBuyStopOpened){
   
   }
   if(eventBuyLimitOpened){
   
   }
   if(eventSellStopOpened){
      OrderSend(Symbol(), OP_BUYSTOP, Lot, Ask + 200*_Point, 3, Ask - 600*_Point, Ask + 800*_Point, NULL, 0, 0, Green);
   }
   if(eventBuyStopOpened){
   
   }
}

int CountBuyPositions(){
   int Numberofbuypositions = 0;
   for( int k=1; k<=OrdersTotal(); k++){
      if(OrderSelect(k-1, SELECT_BY_POS, MODE_TRADES)==true){
         if(_Symbol==OrderSymbol()){
            if(OrderType()==OP_BUY){
               Numberofbuypositions=Numberofbuypositions+1;
            }
         }
      }
      
   }
   return Numberofbuypositions;
}

int CountSellPositions(){
   int Numberofsellpositions = 0;
   for ( int y=1; y<=OrdersTotal(); y++){
      if(OrderSelect(y-1, SELECT_BY_POS, MODE_TRADES)==true){
         if(_Symbol==OrderSymbol()){
            if(OrderType()==OP_SELL){
               Numberofsellpositions=Numberofsellpositions+1;
            }
         }
      }
   }
   return Numberofsellpositions;
}
Reason: