OPEN-SOURCE SCRIPT
Diupdate Stock Profit Calculator — Live Mode

## Overview
This Pine Script indicator calculates, in real time, the financial impact of a stock trade, including purchase/sale commissions, capital gains tax (CGT), and return on investment (ROI). It displays a compact table with key values and also calculates the breakeven price to see at what level the net P/L returns to zero.
---
## Inputs and customization
- **Number of shares:** `shares` defines the purchased quantity.
- **Purchase price:** `buyPrice` is the unit cost; the total purchase is calculated from this.
- **Live selling price:** `sellPrice = close` uses the last bar’s price for live valuation.
- **Fixed or percentage commissions:** `useFixedComm` selects the model.
- **Fixed:** `buyCommFixed`, `sellCommFixed`.
- **Percentage:** `buyCommPct`, `sellCommPct` (applied to notional value).
- **CGT rate:** `cgtRate` is the percentage rate, applied only in case of profit.
- **Table position:** `tablePosition` with predefined options.
- **Visual style:** `colTxt`, `colPos`, `colNeg`, `colBg`, `colHdr`, `colFrame` for text color, positive/negative P/L, background, header, and borders.
> Tip: if your broker uses minimum fees or composite fees, turn on “Use fixed commissions?” and enter the two fixed fees; otherwise, use the percentage model.
---
## Calculation logic
#### Purchase costs
- **Total purchase:**
\[
buyTotal = shares \cdot buyPrice
\]
- **Purchase commission:**
\[
buyComm =
\begin{cases}
buyCommFixed & \text{if } useFixedComm \\
buyTotal \cdot \frac{buyCommPct}{100} & \text{otherwise}
\end{cases}
\]
- **Net entry cost:**
\[
netBuy = buyTotal + buyComm
\]
#### Sale revenues
- **Total sale (with live price):**
\[
sellTotal = shares \cdot sellPrice
\]
- **Sale commission:**
\[
sellComm =
\begin{cases}
sellCommFixed & \text{if } useFixedComm \\
sellTotal \cdot \frac{sellCommPct}{100} & \text{otherwise}
\end{cases}
\]
- **Net exit revenue:**
\[
netSell = sellTotal - sellComm
\]
#### P/L and taxes
- **Gross P/L:**
\[
profitBeforeTax = netSell - netBuy
\]
- **CGT (only on positive P/L):**
\[
tax =
\begin{cases}
profitBeforeTax \cdot \frac{cgtRate}{100} & \text{if } profitBeforeTax > 0 \\
0 & \text{otherwise}
\end{cases}
\]
- **Net P/L:**
\[
profitAfterTax = profitBeforeTax - tax
\]
#### ROI
- **Percentage ROI on invested capital:**
\[
roiPct = \frac{profitAfterTax}{netBuy} \cdot 100
\]
#### Breakeven
- **Gross breakeven** shown in the table: the unit price that makes the net P/L exactly zero, including purchase cost and an estimate of the sale commission.
\[
breakevenPrice = \frac{netBuy + \text{(possible sellComm)}}{shares}
\]
In the script, if commissions are fixed it adds the fixed sale fee; if percentage-based, the sale component is not included in this row (conservative approximation).
- **Breakeven with tax** (calculated but not shown):
\[
breakevenWithTax = \frac{netBuy + \text{(possible fixed sellComm)}}{shares \cdot \left(1 - \frac{cgtRate}{100}\right)}
\]
Useful when you want the post-CGT result to be exactly zero. Not displayed in the table but ready for use.
> Note: CGT applies only on positive profits; near breakeven, the tax effect is null or only kicks in beyond a threshold. That’s why the script distinguishes between the “gross” and “with tax” versions.
---
## On-screen table
- **Displayed rows:**
- **Purchase:** total net entry cost (with commissions).
- **Sale:** total net exit revenue (with commissions).
- **Gross P/L:** difference between netSell and netBuy.
- **CGT:** estimated tax only if there’s a gain.
- **Net P/L:** P/L after taxes.
- **ROI (%):** percentage return on netBuy.
- **Breakeven:** gross unit breakeven price.
- **Conditional colors:**
- **P/L and ROI:** green for ≥ 0, red for < 0.
- **Headers and cells:** customizable via the color inputs.
- **Efficient refresh:** the table updates only on the last bar via `barstate.islast` to avoid unnecessary redraws.
---
## Behavior and performance
- **Overlay:** displayed on the price chart.
- **Persistent variable:** table is created once with `var table`.
- **Live price:** `sellPrice` follows the current `close`, making P/L, ROI, and breakeven dynamic.
---
## Limitations and suggestions
- **Commission model:** when using percentage commissions, the breakeven in the table doesn’t add the sale percentage fee in the “breakevenPrice” formula. For more precision, you could solve the equation including the percentage fee on exit.
- **Breakeven with tax:** `breakevenWithTax` is a linear estimate; near zero profit, tax may be null. You might choose to display it instead of, or alongside, the gross breakeven.
- **Precision and formatting:** values are shown with `format.mintick`. If the symbol has very small ticks, consider a custom format for better readability.
- **Edge cases:** ROI is undefined if `netBuy = 0` (unlikely in practice but good to note).
> Pro tip: if you want to show the breakeven with tax, add a “Breakeven (post-CGT)” row printing `breakevenWithTax`. If you prefer a single row, replace the shown value with the post-CGT one.
---
Catatan Rilis
---### 📊 **Vertical Portfolio Table
This Pine Script v5 indicator displays a **compact vertical portfolio dashboard** directly on your TradingView chart, without the extra “Metrics” column.
🔹 **Key Features**
* Monitor up to **two tickers simultaneously** (each can be enabled/disabled individually).
* **11 rows of live portfolio data** for each ticker:
* **Ticker symbol**
* **Current price**
* **Breakeven price** (including commissions)
* **Buy price**
* **Quantity held**
* **Total cost** (with entry commissions)
* **Market value** (after exit commissions)
* **Profit/Loss (gross)**
* **Capital Gains Tax (CGT)**
* **Profit/Loss (net)**
* **Net ROI (%)**
🔹 **Customization Options**
* Flexible **table positioning** (top, middle, or bottom; left, center, or right of chart).
* Fully customizable **colors** for headers, rows, backgrounds, borders, and text.
* Dynamic P/L and ROI coloring (green for profit, red for loss, yellow for breakeven).
* Define **entry and exit commissions** per ticker (fixed or percentage-based).
* Adjustable **capital gains tax (CGT %) input**.
🔹 **Why Use It**
This script provides a **real-time, easy-to-read performance overview** of your portfolio directly on the chart. It helps you quickly track:
* Gross vs. net P/L
* ROI after taxes and fees
* True breakeven price
Perfect for traders and investors who want a **clean portfolio dashboard** without extra clutter.
---
👉 Do you want me to also write a **shorter, catchy version** (like a one-paragraph description for the TradingView public library)?
Skrip open-source
Dengan semangat TradingView yang sesungguhnya, penulis skrip ini telah menjadikannya sumber terbuka, sehingga para trader dapat meninjau dan memverifikasi fungsinya. Hormat untuk penulisnya! Meskipun anda dapat menggunakannya secara gratis, ingatlah bahwa penerbitan ulang kode tersebut tunduk pada Tata Tertib kami.
Pernyataan Penyangkalan
Informasi dan publikasi tidak dimaksudkan untuk menjadi, dan bukan merupakan saran keuangan, investasi, perdagangan, atau rekomendasi lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Persyaratan Penggunaan.
Skrip open-source
Dengan semangat TradingView yang sesungguhnya, penulis skrip ini telah menjadikannya sumber terbuka, sehingga para trader dapat meninjau dan memverifikasi fungsinya. Hormat untuk penulisnya! Meskipun anda dapat menggunakannya secara gratis, ingatlah bahwa penerbitan ulang kode tersebut tunduk pada Tata Tertib kami.
Pernyataan Penyangkalan
Informasi dan publikasi tidak dimaksudkan untuk menjadi, dan bukan merupakan saran keuangan, investasi, perdagangan, atau rekomendasi lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Persyaratan Penggunaan.