Using OnTradeTransaction to manage position replacement

 

Hello All, please I am trying to use the OnTradeTransaction function to manage my positions such that I want to replace a buy position that closes due to take profit with a buy limit order and replace a buy position that closes due to stop loss with a buy stop order. Also vice-versa for sell positions. Please take note that the program I am working on places hedged trades.

I know I definitely have my entire logic coded wrongly as seen in my code below so please help me correct the code because the code does not function as expected. For instance when a buy position is opened first followed by a sell trade, I now have 2 trades running and if the buy position hits stop loss, I am supposed to have the program place a new buy stop order using the price, sl, and tp of the closed buy position but this is not so as my code below only detects the last opened position which is a sell position and tries to use its price, sl, and tp parameters to place a buy stop which is totally wrong.

Apparently, I don't know how to select the last closed buy position from the total deals and then pass the trade.BuyStop function to it. Please help me correct this in the code below. T

//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
{
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
          // Select the deal from the history
          if(HistoryDealSelect(trans.deal))
          {
             // Create variables to store deal information
             double entryPrice, stopLossPrice, takeProfitPrice;
             
             // Get the deal's entry price, stop loss, take profit, and type
             if (HistoryDealGetDouble(trans.deal, DEAL_SL, stopLossPrice) &&
             HistoryDealGetDouble(trans.deal, DEAL_TP, takeProfitPrice) &&
             HistoryDealGetDouble(trans.deal, DEAL_PRICE, entryPrice))
             {
                  deal.Ticket(trans.deal);
                 
                 if(deal.DealType() == DEAL_TYPE_SELL && 
                    (deal.Entry() == DEAL_ENTRY_OUT || deal.Entry() == DEAL_ENTRY_INOUT))
                 {
                     long deal_reason = -1;
                     if(deal.InfoInteger(DEAL_REASON, deal_reason))
                     {
                         if((ENUM_DEAL_REASON)deal_reason == DEAL_REASON_TP)
                         {
                             if(trade.BuyLimit(lotSize,entryPrice,_Symbol,stopLossPrice,takeProfitPrice))
                             {
                                 Print("Buy trade closed due to take profit and replaced with new buy limit");
                             }
                             else
                             {
                                 Print("Failed to place pending order. Error :", GetLastError());
                             }
                         } else
                         
                         if((ENUM_DEAL_REASON)deal_reason == DEAL_REASON_SL)
                         {
                             if(trade.BuyStop(lotSize,entryPrice,_Symbol,stopLossPrice,takeProfitPrice))
                             {
                                 Print("Buy trade closed due to stop loss and replaced with new buy stop");
                             }
                             else
                          {
                              Print("Failed to place pending order. Error :", GetLastError());
                          }
                         }
                     }
                 }
             }
            
              
              if(deal.DealType() == DEAL_TYPE_BUY && 
                 (deal.Entry() == DEAL_ENTRY_OUT || deal.Entry() == DEAL_ENTRY_INOUT))
              {
                  long deal_reason = -1;
                  if(deal.InfoInteger(DEAL_REASON, deal_reason))
                  {
                      if((ENUM_DEAL_REASON)deal_reason == DEAL_REASON_TP)
                      {
                          if(trade.SellLimit(lotSize,entryPrice,_Symbol,stopLossPrice,takeProfitPrice))
                          {
                              Print("Sell trade closed due to take profit and replaced with new sell limit");
                          }
                          else
                          {
                              Print("Failed to place pending order. Error :", GetLastError());
                          }
                          
                      } else
                      if((ENUM_DEAL_REASON)deal_reason == DEAL_REASON_SL)
                      {
                          if(trade.SellStop(lotSize,entryPrice,_Symbol,stopLossPrice,takeProfitPrice))
                          {
                              Print("Sell trade closed due to take profit and replaced with new sell stop");
                          }
                          else
                          {
                              Print("Failed to place pending order. Error :", GetLastError());
                          }
                      }
                  }
              }            
          }
     }
}


Thank you

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
The MQL5 language provides processing of some predefined events . Functions for handling these events must be defined in a MQL5 program; function...