No deal profit available

 

Hello,

when open a position and close i can see the profit of this transaction in the deal history along with profit.

Not so from within mql: 

 

 

 

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
{

    if(trans.deal > 0)
                Print(DoubleToString(HistoryDealGetDouble(trans.deal,DEAL_PROFIT),6));    
}

 This returns always zero. However, in the history you can see the profit for same deal #.

Anyone knows how to get proft from Transaction event ? 

 

 

asd 

 
chinaski:

Hello,

when open a position and close i can see the profit of this transaction in the deal history along with profit.

Not so from within mql: 

 This returns always zero. However, in the history you can see the profit for same deal #.

Anyone knows how to get proft from Transaction event ? 

 

asd 

Learn the code from here SendNotification with P&L
 

Hello Phi,

HistoryDealSelect

does not work as well (as well as using CDealInfo).

So a transaction happened, a deal number available; how to get profit please ?

Thank you 

 

 

Phi,

You are right. 

HistoryDealSelect

is what solves it. 

(Why at all, if HistoryDealGetDouble expects a deal # ?)

 

CDealInfo does not work reliable:

 

 

void OnTradeTransaction(const MqlTradeTransaction& t,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
//---
        if(t.deal > 0)
        {
             CDealInfo di;
             di.Ticket(t.deal);
             Print("s1=",di.Symbol());

           
            HistoryDealSelect(t.deal);
            Print("s2=",HistoryDealGetString(t.deal,DEAL_SYMBOL));
        }
        
        
  }

 

s1 is emtpy. s2 not.

 
the same mt4, i have script close at profit. you can see here. from mt4, you can wirte for mt5, i think same
Files:
 

Hello,

it seems that  at least DEAL_PRICE and DEAL_PROFIT does always return 0 in backtest (MqlTradeTransaction has zeros as well).

 

 

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {

        if(trans.symbol == Symbol() && trans.deal > 0)
        {
                if(HistoryDealSelect(trans.deal) == true)
                {
                        int dealtype=HistoryDealGetInteger(trans.deal,DEAL_TYPE);
                        //if(trans.deal_type == (int) DEAL_ENTRY_OUT)
                        {
                                double price=HistoryDealGetDouble(trans.deal,DEAL_PRICE);
                                double profit=HistoryDealGetDouble(trans.deal,DEAL_PROFIT);
                                Print(StringFormat("deal price=%.5f profit=%.2f",price,profit)); /// zero zero
                                
                                
                        }
                }
                else
                        Print("SELECT FAILED");
                
                
        }
  }

 

Anyone knows what i do wrong or a workaround.

This applies only for backtesting.

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties - Documentation on MQL5