[Kpt-Ahab] PnL-calculator

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.
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.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Untuk akses pantas pada carta, tambah skrip ini kepada kegemaran anda — ketahui lebih lanjut di sini.
Penafian
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Untuk akses pantas pada carta, tambah skrip ini kepada kegemaran anda — ketahui lebih lanjut di sini.