Indicator · Pine Script v4 · Own pane

Return on Investment (ROI)

by everget

Calculates the percentage return on investment measured from a user-defined start date or the chart's historical beginning.

View on GitHub

GitHub stars948on the repository
Pine versionv4older, still runs
Lines of code59
Settings6inputs
LicenceGPL-3.0
Last update10 days ago18 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

59 lines of Pine Script v4 from statistics/roi_return_on_investment.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

roi_return_on_investment.pine
//@version=4
// Copyright (c) 2020-present, Alex Orekhov (everget)
// Return on Investment (ROI) script may be freely distributed under the terms of GPL-3.0 license.
study("Return on Investment (ROI)", shorttitle="ROI", precision=2)

// mode = input(title="Display Mode", defval="Regular", options=["Regular", "Annualized"])

useDate = input(title="Use Specific Start Date ?", defval=true)
yearStart = input(title="Start Year", defval=2019)
monthStart = input(title="Start Month", minval=1, maxval=12, defval=1)
dayStart = input(title="Start Day", minval=1, maxval=31, defval=1)

tp = input(title="Profit Level, %", type=input.integer, defval=0, minval=0)
sl = input(title="Loss Level, %", type=input.integer, defval=0, maxval=0)

startTime = timestamp(yearStart, monthStart, dayStart, 0, 0)

index = float(na)
index := na(index[1]) ? valuewhen(time(timeframe.period) >= startTime, bar_index, 0) : index[1]

initialCond = useDate ? bar_index == index : bar_index == 0
initialRegular = valuewhen(initialCond, open, 0)
// initialAnnualized = valuewhen(initialCond, close, 0)

roiRegular = 100 * (close - initialRegular) / initialRegular

// bars = bar_index - index
// power = 1 / ((useDate ? (bars != 0 ? bars : 1) : bar_index) / 365)
// roiAnnualized = 100 * (pow(close / initialAnnualized, power) - 1)

roi = roiRegular //mode == "Regular" ? roiRegular : roiAnnualized
roiPlot = plot(roi, title="ROI, %", color=color.black)

transparent = color.new(color.white, 100)
roiColor = roi < 0 ? color.red : #09b71e
labelColor = #f9ce02
levelColor = #674ea7

bgcolor(year != year[1] ? #f57f17 : transparent, title="Year Separator", transp=60)

zeroPlot = plot(0, title="", style=plot.style_circles, linewidth=0, color=labelColor, editable=false)
hline(0, title="Zero Level", linestyle=hline.style_dotted, color=levelColor)

fill(roiPlot, zeroPlot, title="Base Background", color=roiColor, transp=80)

tpPlot = plot(tp, title="Profit", style=plot.style_circles, linewidth=0, color=labelColor, editable=false)
hline(tp, title="Profit Level", linestyle=hline.style_dotted, color=levelColor)

tpFillColor = tp > 0 and roi > tp ? #09b71e : transparent
fill(tpPlot, roiPlot, title="Profit Background", color=tpFillColor, transp=60)

slPlot = plot(sl, title="Loss", style=plot.style_circles, linewidth=0, color=labelColor, editable=false)
hline(sl, title="Loss Level", linestyle=hline.style_dotted, color=levelColor)

slFillColor = sl < 0 and roi < sl ? color.red : transparent
fill(slPlot, roiPlot, title="Loss Background", color=slFillColor, transp=60)

alertcondition(crossover(roi, tp), title="ROI Profit Level Crossover", message="ROI Profit Level Crossover")
alertcondition(crossunder(roi, sl), title="ROI Loss Level Crossunder", message="ROI Loss Level Crossunder")

Overview

The script measures price performance relative to the initial bar's open price and plots the resulting percentage curve in a separate pane. Users can define custom profit and loss percentage target levels, which are displayed as reference lines with shaded background fills when thresholds are breached. Configurable alert conditions trigger when ROI crosses above the profit target or drops below the loss limit.

Details

Published by

everget on GitHub

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinepinescripttechnical-indicatorsindicatorsoscillatorbandsaveragetradingviewindicatormoving-average

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Use Specific Start Date ?booltrueuseDate
Start Yearint2019yearStart
Start Monthint1monthStart
Start Dayint1dayStart
Profit Level, %int0tp
Loss Level, %int0sl
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
549 stars

Range Volume Change compares volume inside a chosen session window with the same window on the previous day and plots that change as a percentage. It accumulates volume during the window and, when enabled, volume outside it, drawing growing columns in a separa

Indicator v5 VolumeSessions & time Repaints
Updated 10 months ago · None stated GitHub repository of Range Volume Change

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

Mojito v3 Strategy

by namuan
318 stars

The Mojito v3 Strategy indicator evaluates volatility term structure by calculating the ratio between front-month and second-month VIX futures contracts. It smooths this ratio using a configurable median filter and compares the result against user-defined uppe

Indicator v5 Volatility & bands
Updated 2 months ago · MIT GitHub repository of Mojito v3 Strategy

Color Triangular Gradient

by ricardosantos79
261 stars

This script defines a helper function to generate colors via triangular interpolation using TradingView's color.from_gradient. The function blends between minimum, mid, and maximum color points based on an input value. It applies the function to the RSI indica

Indicator v5 Support & resistance
Updated 4 years ago · None stated GitHub repository of Color Triangular Gradient

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers