Pine Script, the programming language utilized by the TradingView platform, contains a vast range of functions and capabilities to enable traders to create powerful custom indicators. One such function is hline
, which renders a horizontal line at a given fixed price level. This blog post aims to deliver a comprehensive guide on the hline
function, explaining its syntax, parameters, and offering a unique use case example.
The Syntax of hline Function
The hline
function in Pine Script follows this syntax:
hline(price, title, color, linestyle, linewidth, editable, display) → hline
Arguments Explained
Here’s what each argument in the hline
function represents:
price (input int/float): This is a required argument and represents the price value at which the object will be rendered.
title (const string): This is the title of the object. This title can be used to identify your horizontal line in the chart.
color (input color): This is the color of the rendered line. This value must be a constant and not an expression. This is an optional argument.
linestyle (input hline_style): This defines the style of the rendered line. Possible values are hline.style_solid
, hline.style_dotted
, hline.style_dashed
. This is an optional argument.
linewidth (input int): This represents the width of the rendered line. The default value is 1.
editable (const bool): If set to true, the hline style will be editable in the Format dialog. The default value is true.
display (input plot_simple_display): This argument controls where the hline is displayed. The possible values are display.none
, display.all
. The default is display.all
.
A Unique Use Case: Filling the Background between Two hlines
Now let’s delve into a unique use case of the hline
function, where we fill the background between any two horizontal lines using the fill()
function.
//@version=5 indicator("input.hline", overlay=true) h1 = hline(20) h2 = hline(10) fill(h1, h2, color=color.new(color.green, 90))
This script begins by declaring a new indicator overlay and two hline
functions at different price levels (20 and 10). We then use the fill
function to fill the background between these two lines.
Here, h1
and h2
are horizontal lines at price levels 20 and 10, respectively. fill(h1, h2, color=color.new(color.green, 90))
fills the area between h1
and h2
with a green color at 90% transparency.
Key Takeaway
The hline
function in Pine Script is a handy tool to visualize specific price levels on your TradingView chart. Whether you’re looking to highlight areas of support and resistance, fill the background between different price levels, or simply better understand your data, the hline
function offers a range of possibilities. Remember, while price is the only required argument, customizing your horizontal lines through additional parameters can provide a more comprehensive and visually-appealing analysis.
Conclusion
Mastering the hline
function can open up new opportunities for your trading strategies and market analysis. The function is versatile and easy to use, with an array of customizable parameters. The more you understand and experiment with hline
, the more you can leverage its full potential. Happy trading!