the best way to get PNL

 

 hi, i'm new here creating a new topic, but i search a lot of things. In special this problem if can we help me


void RangeBreakout::TotalProfit() {
    floatingPNL = 0;
    int total_positions = PositionsTotal();
    for (int i = total_positions - 1; i >= 0; i--) {
        ulong ticket = PositionGetTicket(i);
        if (!PositionSelectByTicket(ticket)) {
            if (inputs.InpDebug) Print("Failed to select position by ticket");
            continue; // Continuar con la próxima iteración del bucle
        }
        ulong magicNumber;
        if (!PositionGetInteger(POSITION_MAGIC, magicNumber)){
            if (inputs.InpDebug) Print("Failed to get position magicnumber");
            return;
        }
        if (inputs.InpMagicNumber == magicNumber) {
             floatingPNL += PositionGetDouble(POSITION_PROFIT);
        }
    }
}

this is mi function to get current PNL, but i think is not the best way because the functions is called in the tick function, does exist the better way?, searching i think found a possible solution.

in this thread https://www.mql5.com/en/forum/364683#comment_21260716

thx for yours reply's

Formula for calculating profits
Formula for calculating profits
  • 2021.03.11
  • Oleksnadr Myronchuk
  • www.mql5.com
I made a purchase at a price of 0.9615 and sell it when the price was 0.9586...
 
wiredlain:
ulong ticket = PositionGetTicket(i);         if (!PositionSelectByTicket(ticket)) {

You do not need PositionSelectByTicket. PositionGetTicket selects the position already.

wiredlain:
if (!PositionGetInteger(POSITION_MAGIC, magicNumber)){

PositionGetInteger return integer type not boolean type. Study the documentation.

wiredlain:
floatingPNL += PositionGetDouble(POSITION_PROFIT);

You need deals' commision and POSITION_SWAP for a better estimate of unrealized profit.

 
Yashar Seyyedin #:

You do not need PositionSelectByTicket. PositionGetTicket selects the position already.

PositionGetInteger return integer type not boolean type. Study the documentation.

You need deals' commision and POSITION_SWAP for a better estimate of unrealized profit.

i try with the comments, but the function it's called every tick ( ihmo is not good for the perfomance )
 
wiredlain #:
i try with the comments, but the function it's called every tick ( ihmo is not good for the perfomance )

Cycling all trades every tick is not a problem as long as you don't work with a lot of opened trades at the same time.

Anyway you can choose to use the function to check only once per bar, or only if the amount of opened trades changes, but you will not be able to get the instant profit/loss of each one of your trades.

 
There's always the option to check when a trade is opened/close by handling the TradeTransaction event. By doing this, you can create a struct and store the entry price, tp, sl, position ticket, etc., all in an object of that struct. Then you don't need to call a few of these functions (like PositionsTotal, PositionGetInteger to get the magic number, etc.), and you'll also loop through less positions, since you'll only store positions created by that EA/symbol.
 
wiredlain #:
i try with the comments, but the function it's called every tick ( ihmo is not good for the perfomance )
Nowadays processing power is abundant enough not to worry about a dozen of iterations per tick. Don't bother.