Place a pending order when another pending order turns into a market order

 
I will send 1 buy market order (No1) and 01 pending sell order (No2) at the same time.
TP1 = 20 pips
SL1 = 40 pips
Openprice1 = Ask
coment = "No1"
Openprice2 = Ask- (SL1 / 2) * point
TP2 = Ask-SL1 * point
SL2 = Ask + TP1 * poin
coment = "No2"
There are 2 scenarios:
- The price goes up and touches TP1 and No2 is still a pending order => delete the pending order No2.
- The price goes down and starts the pending order into a market order (sellstop becomes a sell), then immediately opens a pending order (No3).
Openprice3 = Openprice1
TP = 20 pips
SL = 40 pips.
I am in the process of writing my own code, trying to understand each piece of code and I have the code for the above problem with the code below. I don't know how to place multiple orders with its own condition on the same candle.
Here, initially I placed 2 orders at the same time on 1 candle at TF H4. When the price goes down, it makes a pending order into a market order and has not finished the candle H4. Therefore, it cannot place No3 order, but has to wait until a new candle to enter.
Can anyone help me solve this problem.
We hope to be taught by you.

Thank you so much!

//+------------------------------------------------------------------+
//|                                                      V_EA_12.mq4 |
//|                                     Copyright 2020, Tran Hoai Vu 
//|                        
//+------------------------------------------------------------------+

extern string Setup="TP SL TrailingStop";
extern double   TP=20; //
extern double   SL=40;
extern double   Lot=0.1;
extern int TLS = 30; // 
// Số lệnh cho phép vào cùng một khoảng thời gian
extern string Solenh="So lenh duoc phep mo de max drawdown an toan, nen <=20";
extern int MaxLenh=20; 
// MagicNumber
extern int MagicNumber=2805; 

extern int      Slip=30; // Do truot gia khi vao lenh  
datetime t; 
int init(){ t = Time[0]; return(0);}                                                     

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+

int start()
  {
  
      
      double MyPoint=Point;
      if(Digits==3 || Digits==5) MyPoint=Point*10;
  double Gia1, Gia2;
  datetime Last1, Last2;
  double LoiNhuan1, LoiNhuan2;
  int Type1, Type2;
  int Order1, Order2;
  int ticket1, ticket2;
  int total; 
  int Magic;
  int t1;
 
  double TheStopLoss=0;
  double TheTakeProfit=0;
  double Lots1, Lots2, Lots3;
  double TPF1, TPF2, TPF3;
  string cmt;  
  
 if(Condition&&NewBar())
 {
// Entry No1
         TheStopLoss=0;
         TheTakeProfit=0;
         Magic=MagicNumber;
         cmt="No1";
         if(TP>0) TheTakeProfit=Ask+TP*MyPoint;
         if(SL>0) TheStopLoss=Ask-SL*MyPoint;          
         OrderSend(Symbol(),OP_BUY,Lot,Ask,Slip,NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),cmt,Magic,0,clrAliceBlue); 
        
//Entry No2
         TheStopLoss=0;
         TheTakeProfit=0;
         Magic=MagicNumber;
         cmt="No2";
         if(TP>0) TheTakeProfit=Ask-(SL-3)*MyPoint;
         if(SL>0) TheStopLoss=Ask+TP*MyPoint; 
         OrderSend(Symbol(),OP_SELLSTOP,3*Lot,NormalizeDouble(Ask-(SL/2)*MyPoint,Digits),Slip,NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),cmt,Magic,0,clrAliceBlue);
     
       
         
 } 
 
     
 // Entry No3
        bool isNewBar = t != Time[0]; t=Time[0];
         for(int cnt= OrdersTotal()-1;cnt>=0;  cnt--)
                { 
    
                OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); 
  
                if(OrderSymbol()==Symbol()&&OrderType()==OP_SELL&&OrderMagicNumber()==MagicNumber&&OrderComment()=="No2") 
                {
                Last1=OrderOpenTime();
                Gia1=OrderOpenPrice();
                Type1=OrderType();
                Lots1=OrderLots();
         	//if(TimeCurrent()<=Last1+120)
                {
                total=OrdersTotal();  
                if(OrdersTotal()>total+1) t=Time[0]; 
                if(isNewBar==false)  continue;
                else
                {
                TheStopLoss=0;
                TheTakeProfit=0;
                cmt="No3";
                if(TP>0) TheTakeProfit=Gia1+SL*MyPoint;
                if(SL>0) TheStopLoss=Gia1-TP*MyPoint; 
                OrderSend(Symbol(),OP_BUYSTOP,2*Lots1,NormalizeDouble(Gia1+(SL/2)*MyPoint,Digits),Slip,NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),cmt,MagicNumber,0,clrAliceBlue);// ok  
                t=Time[0];
                }
                }
                }
                }  
 // Delete pending order   
  
   for(int i= OrdersTotal()-1;i>=0;  i--)
    {            
                OrderSelect(i, SELECT_BY_POS, MODE_TRADES); Type2 = OrderType(); Order2=OrderTicket();
                if(OrderSymbol() == Symbol())
                { 
                OrderSelect(i-1,SELECT_BY_POS, MODE_TRADES);
      Gia1=OrderOpenPrice(); 
      Last1=OrderOpenTime();
      TPF1=OrderTakeProfit();
      LoiNhuan1=OrderProfit();
      Type1=OrderType();
      Order1=OrderTicket();
      Close1=OrderCloseTime();
      Lots1=OrderLots();
              if((Type2 == OP_SELLSTOP&&Type1==OP_BUY&&LoiNhuan1>=TPF1)||(Type2 == OP_BUYSTOP&&Type1==OP_SELL&&LoiNhuan1>=TPF1))  
              {
            OrderDelete(Order2);
               
         }
      
      }
   }    
return(0);   
  }
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain information...
Files:
mlq5.jpg  448 kb