OPEN-SOURCE SCRIPT
Updated

[Kpt-Ahab] PnL-calculator

291
The PnL-Cal shows how much you’re up or down in your own currency, based on the current exchange rate.

Let’s say your home currency is EUR.
On October 10, 2022, you bought 10 Tesla stocks at $219 apiece.
Back then, with an exchange rate of 0.9701, you spent €2,257.40.

If you sold the 10 Tesla shares on April 17, 2025 for $241.37 each, that’s around a 10% gain in USD.
But if you converted the USD back to EUR on the same day at an exchange rate of 1.1398, you’d actually end up with an overall loss of about 6.2%.

Right now, only a single entry point is supported.
If you bought shares on different days with different exchange rates, you’ll unfortunately have to enter an average for now.

For viewing on a phone, the table can be simplified.
Release Notes
Small fixes and more user-friendly table headings.
Release Notes
Improved color contrast
Release Notes
Display corrections
Release Notes
FX-only gain/loss formulas updated — now factoring in more than just FX rates.
Release Notes
Description and Functionality

The script combines price and currency changes to calculate the actual performance of an investment, including the effect of exchange rate fluctuations.

1. User Inputs
Purchase date, purchase price, and quantity are defined via input fields.
Input of the base currency (e.g., CHF, EUR) and the data provider (e.g., FX_IDC).
Selection of a color scheme for the lines and tables displayed in the chart.

2. Exchange Rate Calculation
The script loads the current exchange rate (syminfo.currency → base currency).
The exchange rate at the time of purchase is stored.
This enables the calculation of the impact of currency fluctuations on the investment performance later on.

3. Chart Displays
Vertical line: Marks the purchase date on the chart.
Horizontal line: Marks the purchase price.

4. Performance Calculation
The script calculates:
Price change (absolute and percentage) without currency effects.
Initial investment (adjusted for the exchange rate at the time of purchase).
Current value (based on the current exchange rate).
Overall performance including exchange rate effects.
Isolated impact of exchange rate changes on performance.

5. Table Display
A dynamic table is shown on the chart.
The display can be either:
Full table (desktop view, detailed information)
Compact table (mobile/small view)

Term Explanation
PnL excl. FX Profit/Loss from price movement only (excluding FX).
PnL incl. FX Actual profit/loss including exchange rate impact.
FX only Portion of the profit/loss caused solely by exchange rate changes.
Corrected invest Adjusted purchase and current value in the base currency.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.