Generic formula to calculate position profit from price, pips, lots

 
Dear experts,

I read 3,4 form threads about position profit calculation but still can not figure out the correct formula to find
profit from the price difference and SYMBOL values. 

My account currency is USD and here are the values from my broker for an EURUSD position:

For EURUSD:
-----------------------

POSITION_PRICE_CURRENT: 1.07720
POSITION_PRICE_OPEN: 1.07716
Difference: 0.00004
POSITION_PROFIT shows in MT5 screen: 12.0$
POSITION_VOLUME: 3.0 LOTS
SYMBOL_TRADE_TICK_SIZE: 0.00001
SYMBOL_POINT:0.00001
SYMBOL_TRADE_TICK_VALUE: 1.0

And my formula for profit is :

dbPointValue = SYMBOL_TRADE_TICK_VALUE * SYMBOL_POINT / SYMBOL_TRADE_TICK_SIZE
dbPointValue = 1.0 * 0.00001 / 0.00001
dbPointValue = 1.0
and
Position Profit= Difference * POSITION_VOLUME * dbPointValue  / SYMBOL_POINT
Position Profit= 0.00004 * 3.0 * 1.0 / 0.00001
Position Profit= 12.0$ (matches to the profit shown my MT5) 

Or my reduced formula for profit is which still gives 20$
Position Profit= Difference * POSITION_VOLUME * SYMBOL_TRADE_TICK_VALUE / SYMBOL_TRADE_TICK_SIZE 
   = 0.00004 * 3.0 * 1.0 / 0.00001
   = 12$ (matches to the position profit shown by MT5)   


And here I apply the same formula for XAUUSD:
-----------------------

POSITION_PRICE_CURRENT: 1966.97
POSITION_PRICE_OPEN: 1966.91
Difference: 0.06
POSITION_PROFIT shows in MT5 screen: 30.0$
POSITION_VOLUME: 5.0 LOTS
SYMBOL_TRADE_TICK_SIZE: 0.00001
SYMBOL_POINT:0.001
SYMBOL_TRADE_TICK_VALUE: 1.0
SYMBOL_TRADE_CONTRACT_SIZE: 100000.0

And my calculation for profit is :

dbPointValue = SYMBOL_TRADE_TICK_VALUE * SYMBOL_POINT / SYMBOL_TRADE_TICK_SIZE
dbPointValue = 1.0 * 0.001 / 0.00001
dbPointValue = 100.0
and
Position Profit= Difference * POSITION_VOLUME * dbPointValue  / SYMBOL_POINT
Position Profit= 0.06 * 5.0 * 100.0 / 0.001
Position Profit= 30.000$ ????  ( Does not match to the position profit 30.$ shown by MT5)   

I am not able to calculate correct profit of 30$ from this formula.
Where is my mistake ? What is the correct generic formula I can apply for all Symbols in a generic way

thank you very much
Robobiee

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Please don't post randomly in any section. Your topic has been moved to the section: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 

I did not read and check your entire post, but I am left wondering if you are perhaps using the enumerations directly in your calculations, instead of using them in the functions?

For example, in your post ...

dbPointValue = SYMBOL_TRADE_TICK_VALUE * SYMBOL_POINT / SYMBOL_TRADE_TICK_SIZE

Are you doing like this?

Forum on trading, automated trading systems and testing trading strategies

Symbol Point Value

Fernando Carreiro, 2022.05.18 21:05

double
   dbTickSize   = SymbolInfoDouble( _symbol, SYMBOL_TRADE_TICK_SIZE  ), // Tick size
   dbTickValue  = SymbolInfoDouble( _symbol, SYMBOL_TRADE_TICK_VALUE ), // Tick value
   dbPointSize  = SymbolInfoDouble( _symbol, SYMBOL_POINT ),            // Point size
   dbPointValue = dbTickValue * dbPointSize / dbTickSize;               // Point value
Remember, it's best to use tick size and tick value in your calculations, instead of point size and its value.
 

Also, consider simplifying your code and simply use OrderCalcProfit instead.

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 17:41

You can! These are the steps I take. I supply the function with a lot size equal to the “Max Lot Size” allowed for the symbol in question, then calculate the ratio needed to achieve the fractional risk that I wish to apply, to get the correct volume for the order. I then align that with the “Lot Step” and finally check it against both the maximum and minimum allowed lots for the symbol.

The reason I use the “maximum” lots instead of just “1.0” lots as a reference value is because there is no guarantee that the value of 1.0 is within the minimum and maximum values allowed. Given that using 1.0, or the maximum, gives equivalent results anyway (by using the ratio method), I choose to use the “max lots” as the reference point which also offers the most precision for the calculation.

Something like this ...

// This code will not compile. It is only a example reference

if( OrderCalcProfit( eOrderType, _Symbol, dbLotsMax, dbPriceOpen, dbPriceStopLoss, dbProfit ) )
{
   dbOrderLots = fmin( fmax( round( dbRiskMax * dbLotsMax / ( -dbProfit * dbLotsStep ) )
               * dbLotsStep, dbLotsMin ), dbLotsMax ); 
      
   // the rest of the code ...
};
Documentation on MQL5: Trade Functions / OrderCalcProfit
Documentation on MQL5: Trade Functions / OrderCalcProfit
  • www.mql5.com
OrderCalcProfit - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

I did not read and check your entire post, but I am left wondering if you are perhaps using the enumerations directly in your calculations, instead of using them in the functions?

For example, in your post ...

dbPointValue = SYMBOL_TRADE_TICK_VALUE * SYMBOL_POINT / SYMBOL_TRADE_TICK_SIZE

Are you doing like this?

hi Fernando,

yes I start like that and I had taken that formula from one of you posts . But then I enhance it with :

Position Profit= Difference * POSITION_VOLUME * dbPointValue  / SYMBOL_POINT

and find the EURUSD profit correctly as MT5 displays. But same formula did not work for XAUUSD. Will appreciate if you can point me what is wrong in my logic.
And also I am aware of functions like OrderCalcProfit but I am behind the formula to calculate position profit my by self ( my own curiosity)

thank you

 

Forum on trading, automated trading systems and testing trading strategies

How to calculate take profit from currency

Fernando Carreiro, 2022.09.05 17:00

These are all the same equation written in different ways ...

[Volume]      = [Money Value] * [Tick Size] / ( [Tick Value] * [Stop Size] )
[Stop Size]   = [Money Value] * [Tick Size] / ( [Tick Value] * [Volume]    )
[Money Value] = [Volume]      * [Stop Size] * [Tick Value]   / [Tick Size]

[Volume] in lots
[Stop Size] in quote price change
[Money Value] in account currency
 
Fernando Carreiro #:

hi Fernando,

I am not able to find the same Profit MT5 shows with these formulas e.g.

 [Money Value] = [Volume]      * [Stop Size] * [Tick Value]   / [Tick Size]
                        =  3 lot    *  [Stop Size]   *   1.0        / 100000.0
gives something  wrong where profit should be 12$


thank you
Robobiee

 
Robobiee #: I am not able to find the same Profit MT5 shows with these formulas e.g.
[Money Value] = [Volume] * [Stop Size]           * [Tick Value] / [Tick Size]
              =    3     * ( 1.07720 - 1.07716 ) *     1.0      /   0.00001
              =    3     *  0.00004              *     1.0      /   0.00001
              = 12
 
Fernando Carreiro #:

hi Fernando,

Thank you, I understand what you mean by Stop Size now. Now this is exactly the same way I use to calculate EURSUD correctly above.

BUT when I use this for XUAAUSD:

[Money Value] = [Volume] * [Stop Size]           * [Tick Value] / [Tick Size]
              =    5     * ( 1966.97 - 1966.91 ) *     1.0      /   0.00001
              =    5     *  0.06                  *     1.0      /   0.00001
              =    30,000$   where it should be 30$ actually


So what is the generic formula again ?

thank you

 

Robobiee #: BUT when I use this for XUAAUSD:

[Money Value] = [Volume] * [Stop Size]           * [Tick Value] / [Tick Size]
              =    5     * ( 1966.97 - 1966.91 ) *     1.0      /   0.00001
              =    5     *  0.06                  *     1.0      /   0.00001
              =    30,000$   where it should be 30$ actually

So what is the generic formula again ?

Your tick size is incorrect for XAUUSD.

If your prices are with two decimal places (digits), then the point and tick size are probably 0.01 and not 0.00001.

In your first post, you have shown ..
SYMBOL_TRADE_TICK_SIZE: 0.00001
SYMBOL_POINT:0.001

But I doubt the tick size. Either you are reading it incorrectly in your code or the broker is reporting it incorrectly.

 
Fernando Carreiro #:

Your tick size is incorrect for XAUUSD.

If your prices are with two decimal places (digits), then the point and tick size are probably 0.01 and not 0.00001.

In your first post, you have shown ..
SYMBOL_TRADE_TICK_SIZE: 0.00001
SYMBOL_POINT:0.001

But I doubt the tick size. Either you are reading it incorrectly in your code or the broker is reporting it incorrectly.

Thank you Fernando, your support is very much appreciated !

Robobiee