logo

#Trader PnL calculation

This doc describes how you can calculate a trader’s total P&L

TermDefinition
PnL currencyThe currency into which the entries are converted to.
CurrencyTrader income or outcome currency
Total CreditSum of incomes of the trader in the currency (without fees)
Total Credit FeesSum of fees applied to incomes of the trader in the currency
Total Credit Value(Total Credit + Total Credit Fees) estimated in pnl currency using the latest market price
Total DebitSum of outcomes of the trader in the currency (without fees)
Total Debit FeesSum of fees applied to outcomes of the trader in the currency
Total Debit Value(Total Debit + Total Debit Fees) estimated in pnl currency
Average Buy PriceTotal Credit Value / (Total Credit + Total Credit Fees)
Average Sell PriceTotal Debit Value / (Total Debit + Total Debit Fees)

Configuration

To enable the PnL calculation for traders you need to setup at least one destination currency in the PNL_CURRENCIES variable.

Environment VariableExampleDescription
PNL_CURRENCIESusd,btcList of pnl currencies
CONVERSION_PATHSusd/krw:_usdt/usd,usdt/krw this will convert usd to krw using the last price of markets usd/usdt (reversed) and usd/krwBy default conversions are made using the direct market (BTC to USD use latest market price of btc/usd). If a direct conversion market is missing you can specify a conversion path setting this variable. Several paths can be defined with semi-colon (;) separation.
PNL_EXCLUDE_ROLESmaker,brokerSkip PnL calculation for users with the following roles

Formulas

Calculate user Realized PNL

For one currency

Realized PNL Value = Total Debit Value * (Average Sell Price - Average Buy Price) / Average Sell Price

Calculate user Unrealized PNL

For one currency

Asset Current Value = Balance * Last Market Price
Asset Average Buy Value = Balance * Average Buy Price
Unrealized PNL Value = Asset Current Value - Asset Average Buy Value
Unrealized PNL Percentage = (100 * Unrealized PNL Value) / Asset Average Buy Value

Calculate user Total PNL

For one currency

Total PNL = Realized PNL + Unrealized PNL

Total user assets PNL


Total Asset Average Value = SUM(Asset Average Buy Value)
Total Asset Current Value = SUM(Asset Current Value)
Total PNL Value = Total Asset Current Value - Total Asset Average Value
Total PNL Percentage = (100 * Total PNL Value) / Total Asset Average Value

1) Initial State

CurrencyBalanceTotal CreditTotal Credit FeesTotal Credit ValueTotal DebitTotal Debit FeesTotal Debit ValueAverage Buy PriceAverage Sell PriceRealized PNLUnrealized PNLTotal PNLAverage PNL PriceTotal PNL Value
BTC00000000000000

2) Deposit 3 BTC, Portfolio Currency = ETH, Last Market Price (BTC/ETH) = 10 000

CurrencyBalanceTotal CreditTotal Credit FeesTotal Credit ValueTotal DebitTotal Debit FeesTotal Debit ValueAverage Buy PriceAverage Sell PriceRealized PNLUnrealized PNLTotal PNLAverage PNL PriceTotal PNL Value
BTC2.9940002.9940000.00600030 00000010 00000001000029940

Average Buy Price = 30000 / (2.994000 + 0.006000) = 10000
Total PNL Value = Total Credit Average Buy Price -Total Debit Value = 2.994000 10 000 - 0 = 29940
Average PNL Price = Total PNL Value / Balance = 29940 / 2.994000 = 10000

3) Sell 1 BTC, Portfolio Currency = ETH, Last Market Price (BTC/ETH) = 9000

CurrencyBalanceTotal CreditTotal Credit FeesTotal Credit ValueTotal DebitTotal Debit FeesTotal Debit ValueAverage Buy PriceAverage Sell PriceRealized PNLUnrealized PNLTotal PNLAverage PNL PriceTotal PNL Value
BTC1.9940002.9940000.00600030 00010900010 0009000-1000-1994-299410501.504513520940

Average Sell Price = 9000 / (1 + 0.006000) = 9000
Realized PNL = Total Debit Value (Average Sell Price - Average Buy Price) / Average Sell Price = 9000 (9000 - 10 000) / 9000 = -1000
Unrealized PNL = Balance Last Market Price (BTC/ETH) - Balance Average Buy Price = 1.994000 9000 - 1.994000 10 000 = - 1994
Total PNL = Realized PNL + Unrealized PNL = -1000 + (-1994) = -2994
Total PNL Value = Total Credit Average Buy Price - Total Debit Value = 2.994000 10 000 - 9000 = 20940
Average PNL Price = Total PNL Value / Balance = 20940 / 1.994000 = 10501.5045135