# P&L calculation formula for different traded assets

Hello,

I'm making a post because I can't find an answer to my question: I'm looking to calculate the P&L of past or current trades for different types of traded assets.

Context:

I am developing my algorithmic trading framework. Currently I can retrieve data from different platforms and place trades on demo or real accounts for which I have developed connectors to different brokers. My time loop for trade passing is working without any problem.

I am now tackling the backtesting part. I am creating a mock broker that will calculate the P&L of my trades. Unfortunately I'm stuck on how to calculate this P&L. My goal is to be able to calculate the past and current P&L for any asset as a real broker would.

So I would like to have the formula to calculate the P&L for different types of traded assets (not the underlyings), and also to know the "units" of each term (is a volume/contract size dimensionless or in one currency?).

Currently I am at this stage (without really knowing the "units" of all the terms) :
• Stocks : P&L = (Sell price - Buy price) * Number of shares - Transaction fee.
• Bonds: P&L = (Sell price - Buy price) * Face value * Exchange rate - Transaction costs
• Commodities: P&L = (Sell Price - Buy Price) * Contract Size * Contract Price * Exchange Rate - Transaction Costs
• Currencies: P&L = (Sell Price - Buy Price) * Contract Size * Exchange Rate - Transaction Costs
• Derivatives: P&L = Contract size * (Closing price - Opening price) * Exchange rate - Transaction fees
• Exchange Traded Funds (ETFs): P&L = (Sale price - Purchase price) * Number of units * Exchange rate - Transaction fees
Regarding the types of assets traded, I am not absolutely sure that they are exhaustive or well defined. For example: are CFDs well covered by the "derivatives" category?

When I was looking at currency trading, I was faced with the problem of the currency in which the trading account is. Indeed, if I have an account in EUR and I want to trade the NOKSEK pair (Norwegian Kroner to Swedish Kronor): how are my EUR converted into NOK or SEK when I take a position, and then in the calculation of my P&L in past or current EUR?

To summarize :
• are the assets "stocks", "bonds", "commodities", "currencies", "derivatives", "ETFs" exhaustive and do they concretely represent the "objects" traded?
• Are the above formulas correct? If not, what would be the correct formulas?
• how to take into account the currency of the trading account?
• what are the "units" of the terms appearing in the formulas?

I have a scientific background, very good in applied mathematics and software development, but limited on hard trading concepts. A formula will be fine for me, I will do the conversion into code behind :-).

Thanks for your help !

I suggest reading up on the following two MQL5 trade functions ...

 OrderCalcMargin Calculates the margin required for the specified order type, in the deposit currency OrderCalcProfit Calculates the profit based on the parameters passed, in the deposit currency

They calculate the margin and the profit/loss respectively, adjusting automatically for the asset type, making it unnecessary to calculate it manually using the methods you have described.

They do however, not include Commission or Swaps. Those are only provided after the fact and are accessible via other functionality — Documentation on MQL5: Trade Functions

This post and the link on codebase contains necessary equations for forex.

https://www.mql5.com/en/forum/441959/page4#comment_45165158
SYMBOL_TRADE_TICK_VALUE_LOSS vs SYMBOL_TRADE_TICK_VALUE_PROFIT - My broker used the ASK instead of the bid to convert profits/losses; This is what you need to know about your broker
• 2023.02.22
• www.mql5.com
As soon as you imagine that it's your broker buying or selling from you instead of you buying or selling from them in order to convert profits/losses, you now need to use the ask instead of the bid and vice versa