OPEN-SOURCE SCRIPT

Stock Profit Calculator — Live Mode

59

## 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.

---

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.