Indicator · Pine Script v4 · Own pane

Year-to-Date (YTD) % Return

by everget

The script calculates and plots the year-to-date percentage return based on the difference between the current close and the year's open.

View on GitHub

GitHub stars948on the repository
Pine versionv4older, still runs
Lines of code68
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

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

ytd_year_to_date_percent_return.pine
//@version=4
// Copyright (c) 2019-present, Alex Orekhov (everget)
// Year-to-Date (YTD) % Return script may be freely distributed under the terms of GPL-3.0 license.
study("Year-to-Date (YTD) % Return", shorttitle="YTD % Return", precision=2)

level1 = input(title="Level #1", type=input.integer, defval=0)
level2 = input(title="Level #2", type=input.integer, defval=0)
level3 = input(title="Level #3", type=input.integer, defval=0)
level4 = input(title="Level #4", type=input.integer, defval=0)
level5 = input(title="Level #5", type=input.integer, defval=0)
level6 = input(title="Level #6", type=input.integer, defval=0)

_ytd(yearTarget) =>
    yearCond = year != year[1]
    yearOpen = valuewhen(yearCond, open, yearTarget)
    ytd = 100 * (close - yearOpen) / yearOpen
    ytd

labelColor = #f9ce02
levelColor = #674ea7
transparent = color.new(color.white, 100)

ytd = _ytd(0)
ytdColor = ytd < 0 ? color.red : #09b71e
ytdPlot = plot(ytd, title="YTD, %", color=color.black)

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(ytdPlot, zeroPlot, color=ytdColor, transp=80)

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

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

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

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

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

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

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

alertcondition(crossover(ytd, level1), title="Level #1 Crossover", message="Level #1 Crossover!")
alertcondition(crossunder(ytd, level1), title="Level #1 Crossunder", message="Level #1 Crossunder!")

alertcondition(crossover(ytd, level2), title="Level #2 Crossover", message="Level #2 Crossover!")
alertcondition(crossunder(ytd, level2), title="Level #2 Crossunder", message="Level #2 Crossunder!")

alertcondition(crossover(ytd, level3), title="Level #3 Crossover", message="Level #3 Crossover!")
alertcondition(crossunder(ytd, level3), title="Level #3 Crossunder", message="Level #3 Crossunder!")

alertcondition(crossover(ytd, level4), title="Level #4 Crossover", message="Level #4 Crossover!")
alertcondition(crossunder(ytd, level4), title="Level #4 Crossunder", message="Level #4 Crossunder!")

alertcondition(crossover(ytd, level5), title="Level #5 Crossover", message="Level #5 Crossover!")
alertcondition(crossunder(ytd, level5), title="Level #5 Crossunder", message="Level #5 Crossunder!")

alertcondition(crossover(ytd, level6), title="Level #6 Crossover", message="Level #6 Crossover!")
alertcondition(crossunder(ytd, level6), title="Level #6 Crossunder", message="Level #6 Crossunder!")

Overview

This plot appears in a separate pane with a black line, colored green for positive returns and red for negative ones. Horizontal dotted lines with circles represent user-defined levels (1 through 6) that serve as alert triggers. The indicator emits alerts for both crossovers and crossunders at each level. Background color changes to orange during the current year and becomes transparent otherwise, helping to highlight the current year's performance. Settings for these levels enable customization of the tracking points for percentage milestones.

Details

Published by

everget on GitHub

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Category

Support & resistance

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
Level #1int0level1
Level #2int0level2
Level #3int0level3
Level #4int0level4
Level #5int0level5
Level #6int0level6
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

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
114 stars

This indicator combines a scaled Forecast Oscillator with a Point of Force state detector. The Forecast Oscillator calculates the percentage deviation of price from its linear regression curve and normalizes the output between 0 and 1, plotting a line in a sep

Indicator v4 Oscillators & momentumSupport & resistance Repaints

Rolling Minimum Index

by mihakralj
99 stars

Rolling Minimum Index calculates and plots the bar offset to the lowest closing price over a user-defined lookback window using Pine Script's built-in lowestbars function. Plotted in a separate indicator pane, it outputs a negative integer representing how man

Indicator v5 Support & resistance
Updated 4 weeks ago · Apache-2.0 GitHub repository of Rolling Minimum Index

CCI DC

by be-thomas
65 stars

This indicator plots the Commodity Channel Index in a separate pane and surrounds it with a Donchian channel whose lookback length changes with volatility. It measures volatility as a ratio of short and long ATR values, then ranks that ratio and shortens or le

Indicator v5 Oscillators & momentumVolatility & bands
Updated 4 days ago · GPL-3.0 GitHub repository of CCI DC

BTC Multiples

by readysetcryptocodes
43 stars

BTC Multiples calculates and displays Bitcoin macro valuation metrics in a separate pane, including the Mayer Multiple, Stock-to-Flow Multiple, Difficulty Multiple, and Puell Multiple. Each valuation multiple features configurable overvalued and undervalued th

Indicator v4 Moving averagesSupport & resistance
Updated 2 years ago · None stated GitHub repository of BTC Multiples

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