How to get the close price of a manually closed trade?

 

Hi guys,

if I closed a trade before the take profit or the stoploss, how can I get this price?

I only find the order open price: HistoryOrderGetDouble(ticket,ORDER_PRICE_OPEN);

But there is no property double for the close price.

Can anyone help please?

 

An example of an MQL5 Expert Advisor. The EA "catches" transaction type 'TRADE_TRANSACTION_DEAL_ADD' (Adding a deal to the history. The action is performed as a result of an order execution or performing operations with an account balance.)

After that, it is checked whether there is a DEAL (NOT an ORDER!!!) in the trading history. If a DEAL is found in the trading history, we get its price.

#include <Trade\DealInfo.mqh>
//---
CDealInfo      m_deal;                       // object of CDealInfo class
***
//+------------------------------------------------------------------+
//| 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)
     {
      ResetLastError();
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryDealSelect(",trans.deal,") error: ",GetLastError());
         return;
        }
      double deal_price=m_deal.Price();
     }
  }
 
Vladimir Karputov #:

An example of an MQL5 Expert Advisor. The EA "catches" transaction type 'TRADE_TRANSACTION_DEAL_ADD' (Adding a deal to the history. The action is performed as a result of an order execution or performing operations with an account balance.)

After that, it is checked whether there is a DEAL (NOT an ORDER!!!) in the trading history. If a DEAL is found in the trading history, we get its price.

Hello Vladimir,

so the order open price can be found within the orders and the closing price can only be found with the deals? Very complicated...

I only use a script and this script has to access to the OnTradeTransaction function. Is there any way I can find the "closing deal price" from the ticket number?

Or do I have to search in the whole deal-history for that closing price?


This is my code so far but I need the real order closing price instead of the tp.

void OnStart() {
   ulong ticket=0;
   if (HistoryOrderSelect(ticket)) {
      double op=HistoryOrderGetDouble(ticket,ORDER_PRICE_OPEN);
      double sl=HistoryOrderGetDouble(ticket,ORDER_SL);
      double tp=HistoryOrderGetDouble(ticket,ORDER_TP);
      datetime t1=(datetime)HistoryOrderGetInteger(ticket,ORDER_TIME_SETUP);
      
      ObjectCreate(0,"win",OBJ_RECTANGLE,0,t1,tp,iTime(_Symbol,PERIOD_CURRENT,0),op);
      ObjectSetInteger(0,"win",OBJPROP_COLOR,clrSpringGreen);
      ObjectSetInteger(0,"win",OBJPROP_FILL,true);
      ObjectCreate(0,"loss",OBJ_RECTANGLE,0,t1,op,iTime(_Symbol,PERIOD_CURRENT,0),sl);
      ObjectSetInteger(0,"loss",OBJPROP_COLOR,clrRed);
      ObjectSetInteger(0,"loss",OBJPROP_FILL,true);
   }
}
Reason: