Writing an mql5 function that calculates the profit and loss of a position in cash

I assume your account is in dollars, which is why it only works for dollars. If it’s not a symbol containing USD, you need to use the base currency’s symbol with USD price to convert the number into USD.

But your stop loss won’t be perfect because as the USD moves, the value of your stop loss will too.

Spread betting is much easier to do all of this because a pip has a fixed price in your account currency. And I can do this without an EA