Pine Script for TradingView that Draws Horizontal Lines
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I need a script written for TradingView.com (a site that displays stock and cryptocurrency trading charts). They have their own simple scripting language called Pine Script and an editor within the main interface to enter the code:

https://www.tradingview.com/

https://www.tradingview.com/study-script-reference/

The script should calculate and display horizontal lines at 5 specific intervals based on an entered value. These intervals should be [initial value] and 5%,10%,15%, and 20% above the initial value. So let's say my buy price is $100. I would like a line drawn at $100 in blue, and lines drawn at $105, $110, $115, and $120 in green. Pine Script has functionality for a popup to enter values. To keep it simple, choosing and running the script would initiate a popup dialog to enter the initial value. Entering a value and clicking OK would close the dialog and draw the 5 horizontal lines.

awarded to CyteBode

Crowdsource coding tasks.

2 Solutions

Winning solution

study("Relative Lines", overlay=true)

initialValue = input(100, confirm = true)

line0  = plot(initialValue, color = blue)
line5  = plot(initialValue * 1.05, color = green)
line10 = plot(initialValue * 1.10, color = green)
line15 = plot(initialValue * 1.15, color = green)
line20 = plot(initialValue * 1.20, color = green)

Edit: Added confirm for popup on add.

Thank you for your submission. For some reason, this is plotting lines at the absolute values 100, 105, 110, 115, and 120 every time. It appears maybe initialValue is 100 every time, regardless of what is entered?
moiremusic 2 months ago
You're right, this is weird. However, it works if you change the value by clicking on the gear, next to the name of the indicator on the chart. Let me look at the documentation to see what's wrong.
CyteBode 2 months ago
From a quick look in the documentation, I couldn't find the problem. The problem is present in Wuddrum's solution as well. You could just remove the confirm = true and set the value manually with the gear I guess?
CyteBode 2 months ago
Just setting the value each time in the script is sufficient for this small bounty. Thanks!
moiremusic 2 months ago
Thanks for awarding me the bounty! I found this issue on the TradingView feature request forum, so the confirm = true feature is clearly broken. Also, if you want to increase the visibility of the lines, you may want to add the line fill(line0, line20, color = green) at the end of the script, which will add a transparent green background.
CyteBode 2 months ago

In case you want it as a separate indicator over a specific input:

//@version=3
study("Horizontal Lines")
src = input(close)
base = input(100.0, title="Base Value", confirm=true)

hline(base, title='Base', color=blue, linestyle=solid, linewidth=1)
hline(base * 1.05, title='5%', color=green, linestyle=solid, linewidth=1)
hline(base * 1.1, title='10%', color=green, linestyle=solid, linewidth=1)
hline(base * 1.15, title='15%', color=green, linestyle=solid, linewidth=1)
hline(base * 1.2, title='20%', color=green, linestyle=solid, linewidth=1)

plot(src)
View Timeline