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 |//+------------------------------------------------------------------+voidOnTradeTransaction(constMqlTradeTransaction &trans,
constMqlTradeRequest &request,
constMqlTradeResult &result)
{
//--- get transaction type as enumeration valueENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in historyif(type==TRADE_TRANSACTION_DEAL_ADD)
{
// Select the deal from the historyif(HistoryDealSelect(trans.deal))
{
// Create variables to store deal informationdouble entryPrice, stopLossPrice, takeProfitPrice;
// Get the deal's entry price, stop loss, take profit, and typeif (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());
}
} elseif((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());
}
} elseif((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());
}
}
}
}
}
}
}
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
Thank you