Pine script for plotting horizontal lines in TradingView
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Currently I have to enter my horizontal line in tradingview using the horizontal line drawing tool. Very time consuming over multiple charts.

I need a pine script where I can run the script and a dialogue box opens with 21 input tabs that can be check or unchecked if I only have a few lines to enter. I want to be able to add my horizontal line levels in the each text box. The first 10 line need to be bright purple, the eleventh needs to be orange and the last 10 need to be bright 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 display the 21 lines on the chart or for example 6 lines if only 6 checkboxes are selected.

See below example of how I plot lines

https://www.tradingview.com/chart/AAPL/FMsaRbAA-Apple/

awarded to ocanal

Crowdsource coding tasks.

1 Solution

Winning solution

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

//@version=4
study("Horizontal Lines", overlay=true)
hline(0, title='h1', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h2', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h3', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h4', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h5', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h6', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h7', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h8', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h9', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h10', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h11', color=color.orange, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h12', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h13', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h14', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h15', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h16', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h17', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h18', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h19', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h20', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(0, title='h21', color=color.green, linestyle=hline.style_solid, linewidth=1)
plot(close)

https://imgur.com/a/9fHur94

UPDATE:

1- Hi when I run the script it flattens out the candle sticks so impossible to view the chart

It's because initial value for each line is zero, so when you run script it adds the line to 0 point. I searched for if it is possible to use last price for initial value but it seems to be not possible, so I added an input for base - initial value.

2- Also when I add the line a to a chart and go to the next chart is it possible to be able to fill levels for each chart individually?

If you work on fully-featured chart and change the current stock from top-left search input, lines should remains its positions. I created a video to show that https://drive.google.com/file/d/1d3y0wxSnKdTEiqfCXvJXsavYLIDfdFLL/view

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

//@version=4
study("Horizontal Lines", overlay=true)
base = input(100, title = "base", confirm = true)
hline(base, title='h1', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h2', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h3', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h4', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h5', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h6', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h7', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h8', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h9', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h10', color=color.purple, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h11', color=color.orange, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h12', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h13', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h14', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h15', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h16', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h17', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h18', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h19', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h20', color=color.green, linestyle=hline.style_solid, linewidth=1)
hline(base, title='h21', color=color.green, linestyle=hline.style_solid, linewidth=1)
plot(close)
Hi when I run the script it flattens out the candle sticks so impossible to view the chart Also when I add the line a to a chart and go to the next chart is it possible to be able to fill levels for each chart individually?
Osteocruncher 1 month ago
Hey I answered your questions on latest update.
ocanal 1 month ago
Hi thanks for the update is it possible to input levels for different charts so I can plot different horizontal lines on each chart? So if i have aapl 300,305,308 and change to adobe chart I can then input new value but when I go back to Apple the levels I inputted are still there?
Osteocruncher 1 month ago
Or do I need to save a pine script for each stock?
Osteocruncher 1 month ago
I couldn't find a way to handle it individually without saving multiple layouts. If autoave is enabled and you change some values, it also keeps same values on other stock charts. So it seems that the only way is to save script for each stock.
ocanal 1 month ago