Search code examples
mql4

How to trigger event on TakeProfit


I am working on this problem for several weeks now and I just can't find a proper way to solve it. I am looking for help.

I want the following to happen:

  1. Place 2 orders, n and n+1. n has a TP & SL level, n+1 has only SL level. SL levels are the same for both orders.

  2. If SL is hit, fine. Both orders are terminated.

  3. If TP on order n is hit, modify order n+1 to have its SL moved to the price where order n hit TP.

  4. After that I want to execute trailing stop loss and I have already programmed that successfully.

How is this done? I tried with magic numbers, iterating over HistoryDealsTotal, HistoryOrdersTotal and a few more approaches but I just never get to the point where I am able to track an order inbetween many open orders and check if it just hit TP, then modify the other order that was initially created right after the first order.

I would appreciate any help towards a solution, thanks!


Solution

  • After doing some research, I came up with a solution to the problem above. here is a breakdown.

    I looped through my order history and recorded the following;

    • OrderOpenTime();
    • OrderSymbol();
    • OrderType();
    • OrderLots();

    Note: the data you record is up to you.

    I then loop through the OrdersTotal with the data I have got for possible matches and then modify the match.

    Hence;

    datetime OrderDateTime=0; // variable to store the OrderOpenTime
       string orderSymbol=""; 
       int orderType=0 ;
       double orderLot=0.0;
       int TotalHistoryOrders = OrdersHistoryTotal(),TotalOrders=OrdersTotal();
    
       for(int h = TotalHistoryOrders - 1; h >= 0; h--)
         {
          if(OrderSelect(h, SELECT_BY_POS, MODE_HISTORY))  // checking through orderHistory 
            {
             if(OrderSymbol() == _Symbol)
               {
                if(OrderType() == OP_SELL || OrderType() == OP_BUY)
                  {
                   if(OrderProfit() >= 0) // checking if the closed trade was in profit 
                     {
                      //saving the needed data from the history data
                      OrderDateTime = OrderOpenTime();
                      orderSymbol = OrderSymbol();
                      orderType = OrderType();
                      orderLot = OrderLots();
                     }
                   for(int i = TotalOrders - 1; i >= 0; i--)
                     {
                        //checking opened trades for a possible match
                      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
                         if(OrderOpenTime() == OrderDateTime|| OrderOpenTime() == OrderDateTime+1) // +1 just incase the second order opens a second later 
                           {
                            if(OrderSymbol() == orderSymbol)
                              {
                               if(OrderType() == orderType)
                                 {
                                  if(OrderLots() == orderLot)
                                    {
    
                                     // CHEKING IF STOP LOSS IS BELOW ONBAR_STOPLOSS
                                     if(OrderStopLoss() < Onbar_stoploss(OP_BUY)) // Onbar_stoploss is my stoploss function for Buy orders 
                                       {
                                        double stop_loss = Onbar_stoploss(OP_BUY); // setting the stoploss
    
                                        //MODIFYING STOP LOSS
                                        bool ORDER_MORDIFY1 = OrderModify(OrderTicket(),OrderOpenPrice(), stop_loss, OrderTakeProfit(),0,clrNONE);
    
                                       }
                                     else
                                        if(OrderStopLoss() > Onbar_stoploss(OP_SELL))// Onbar_stoploss is my stoploss function  for sell orders
                                          {
                                           double stop_loss = Onbar_stoploss(OP_SELL); // setting the stoploss
    
                                           //MODIFYING STOP LOSS
                                           bool ORDER_MORDIFY1 = OrderModify(OrderTicket(),OrderOpenPrice(), stop_loss, OrderTakeProfit(),0,clrNONE);
    
                                          }
                                    }
                                 }
                              }
                           }
                     }
                  }
               }
            }
         }

    I hope this helps.