Indicator · Pine Script v5 · On the chart

Chart CAGR

by ivansoft· code credited to TradingView

An indicator that reports the growth rate and compound annual growth rate between the first and last visible bars of the chart.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code75
Settings5inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago1 Mar 2025

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

75 lines of Pine Script v5 from edu_std/cagr.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

cagr.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradingView

//@version=5
indicator("Chart CAGR", overlay = true)

// Chart CAGR
// v1, 2022.10.09

// This code was written using the recommendations from the Pine Script™ User Manual's Style Guide:
//   https://www.tradingview.com/pine-script-docs/en/v5/writing/Style_guide.html



import PineCoders/VisibleChart/1 as chart
import TradingView/ta/2 as ta



//#region ———————————————————— Constants and Inputs


// ————— Constants
color   GRAY    = color.silver
color   NOCOLOR = #FFFFFF00

// ————— Inputs
string  infoBoxSizeInput     = input.string("normal", "Size ", inline = "display", options = ["tiny", "small", "normal", "large", "huge", "auto"])
string  infoBoxYPosInput     = input.string("bottom", "↕",     inline = "display", options = ["top", "middle", "bottom"])
string  infoBoxXPosInput     = input.string("right",  "↔",     inline = "display", options = ["left", "center", "right"])
color   infoBoxColorInput    = input.color(NOCOLOR,   "",      inline = "display")
color   infoBoxTxtColorInput = input.color(GRAY,      "T",     inline = "display")
//#endregion



//#region ———————————————————— Functions


//@function         Calculates the growth rate as a percent from the `exitPrice` relative to the `entryPrice`.    
//@param entryPrice (series float) The entry price.     
//@param exitPrice  (series float) The exit price.         
//@returns          (float) The percent growth rate. 
growthRate(series float entryPrice, series float exitPrice) =>
    float result = (exitPrice / entryPrice - 1) * 100
//#endregion



//#region ———————————————————— Calculations


float firstChartBarOpen = chart.open()
float lastChartBarClose = chart.close()
int   firstChartBarTime = chart.left_visible_bar_time
int   lastChartBarTime  = chart.right_visible_bar_time
float cagr              = ta.cagr(firstChartBarTime, firstChartBarOpen, lastChartBarTime, lastChartBarClose)
float gr                = growthRate(firstChartBarOpen, lastChartBarClose)
//#endregion



//#region ———————————————————— Visuals


var table infoBox = table.new(infoBoxYPosInput + "_" + infoBoxXPosInput, 2, 3)
if barstate.isfirst
    table.cell(infoBox, 0, 0, "Growth rate:", text_color = infoBoxTxtColorInput, text_size = infoBoxSizeInput, bgcolor = infoBoxColorInput)
    table.cell(infoBox, 0, 1, "CAGR:",        text_color = infoBoxTxtColorInput, text_size = infoBoxSizeInput, bgcolor = infoBoxColorInput)
    table.cell(infoBox, 1, 0, "",             text_color = infoBoxTxtColorInput, text_size = infoBoxSizeInput, bgcolor = infoBoxColorInput)
    table.cell(infoBox, 1, 1, "",             text_color = infoBoxTxtColorInput, text_size = infoBoxSizeInput, bgcolor = infoBoxColorInput)
else if barstate.islast
    table.cell_set_text(infoBox, 1, 0, str.tostring(gr,   format.percent))
    table.cell_set_text(infoBox, 1, 1, str.tostring(cagr, format.percent))
//#endregion

Overview

Chart CAGR uses the open of the leftmost visible bar and the close of the rightmost visible bar, then shows both values as percentages in a small table on the price chart. Settings control the table text size, corner position, background colour and text colour. The figures change when the visible range changes.

Details

Published by

ivansoft on GitHub

Code credit

TradingView, as written in the script's header

Draws

On the price 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

pinescripttradingvew

Settings

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

SettingTypeDefaultVariable
Size stringnormalinfoBoxSizeInput
↕stringbottominfoBoxYPosInput
↔stringrightinfoBoxXPosInput
infoBoxColorInputcolorNOCOLORinfoBoxColorInput
TcolorGRAYinfoBoxTxtColorInput
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

Symbol Info Helper

by everget
947 stars

Symbol Info Helper is an informational utility script that displays metadata about the currently selected instrument directly on the main price chart. It gathers built-in symbol properties including ticker identifier, description, instrument type, point value,

Indicator v4 Screeners & dashboards
Updated 11 days ago · GPL-3.0 GitHub repository of Symbol Info Helper
575 stars

The indicator plots ICT displacement candles on the price chart and can switch to retracement scanning. Displacement requires a move larger than a set share of the 14-bar average high-low range, lasting at least a chosen number of bars, with optional close, pr

Indicator v6 SMC / ICT / price actionCandlestick patterns
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Displacement Candles Indicator
330 stars

This indicator monitors multiple cryptocurrency symbols for trading signals. It calculates and plots entry prices, take profit, and stop loss levels for each symbol including BTCUSDT, ETHUSDT, BNBUSDT and others. Price crossovers trigger buy, sell or close ale

Indicator v5 Support & resistanceScreeners & dashboards
Updated 2 months ago · None stated GitHub repository of Multi Alert Long/Short [Alorse]

Pricing Table

by namuan
318 stars

This indicator calculates theoretical options contract values and in-the-money probabilities using historical realized volatility across multiple standard deviations. It displays these metrics in a table overlay directly on the price chart. Users can adjust th

Indicator v4 Volatility & bandsScreeners & dashboards
Updated 2 months ago · MIT GitHub repository of Pricing Table

3938 Tracker

by Allen1212
303 stars

3938 Tracker visualizes historical buying and selling activity from ARK Invest funds directly on the main price chart. It plots labels at specific timestamps corresponding to trade dates, indicating buy or sell actions along with the count of participating ETF

Indicator v4 Screeners & dashboards
Updated 2 years ago · None stated GitHub repository of 3938 Tracker

More Screeners & dashboards scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers