mql4 to mql5 how convert orderprofit?

 

Hi guys i have this part of code (is free and is not mine )

/**
* Create the info-string for opened and closed order arrows.
*/
string formatOrderArrowInfo() {
   // order is already selected
   return(StringConcatenate(
             OrderComment(),
             " (",
             OrderMagicNumber(),
             ")\nP/L: ",
             DoubleToStr(OrderProfit() + OrderSwap() + OrderCommission(), 2),
             " ",
             AccountCurrency(),
             " (",
             DoubleToStr(MathAbs(OrderOpenPrice() - OrderClosePrice()) / (MarketInfo(OrderSymbol(), MODE_POINT) * pointsPerPip()), 2),
             " pips)"
          ));
}
order profit in mql4  exist , in mql5  i find this oreder_calc_profit but for call this function i must pass many things but i not  have in this function how can  doit ?  exist somthing similar o library that do this ? thanks
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

There is no direct equivalent to OrderProfit() in MQL5 that would give you the profit instantly like in MQL4. Here is a rewritten MQL5 function does the same by collecting the required parameters.


double calculateOrderProfit(int orderTicket) {
    double openPrice = 0, closePrice = 0, volume = 0;
    int orderType;
    
    if (HistorySelect(OrderGetInteger(ORDER_TIME_SETUP), TimeCurrent())) {
        if (OrderSelect(orderTicket)) {
            openPrice = OrderGetDouble(ORDER_PRICE_OPEN);
            closePrice = OrderGetDouble(ORDER_PRICE_CURRENT); 
            volume = OrderGetDouble(ORDER_VOLUME);
            orderType = OrderGetInteger(ORDER_TYPE);

            double profit = OrderCalcProfit(
                orderType,
                OrderGetString(ORDER_SYMBOL),
                volume,
                openPrice,
                closePrice
            );
            return profit;
        }
    }
    return 0;
}

string formatOrderArrowInfo(int orderTicket) {
    double profit = calculateOrderProfit(orderTicket);
    
    return(StringConcatenate(
             OrderGetString(ORDER_COMMENT),
             " (",
             OrderGetInteger(ORDER_MAGIC),
             ")\nP/L: ",
             DoubleToStr(profit + OrderGetDouble(ORDER_SWAP) + OrderGetDouble(ORDER_COMMISSION), 2),
             " ",
             AccountInfoString(ACCOUNT_CURRENCY),
             " (",
             DoubleToStr(MathAbs(OrderGetDouble(ORDER_PRICE_OPEN) - OrderGetDouble(ORDER_PRICE_CLOSE)) / (SymbolInfoDouble(OrderGetString(ORDER_SYMBOL), SYMBOL_POINT) * pointsPerPip()), 2),
             " pips)"
          ));
}
 
Nasimul Haque Choudhury #:

There is no direct equivalent to OrderProfit() in MQL5 that would give you the profit instantly like in MQL4. Here is a rewritten MQL5 function does the same by collecting the required parameters.


thanks so much woow  i hope in next update  metatrader  introduce the  funcion thanks

 

Hi

Orders MT5 are different things than in MT4 (https://www.mql5.com/en/articles/211) so when you want to get orderProfit from orders it’s always “zero” since those are only pending orders.

I believe you might be looking for profit from positions, so opened market trades(buy/sell) then you can get those values by using PositionSelect and PositionGetDouble (POSITION_PROFIT)

Have a nice day👍📊

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • www.mql5.com
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.