This tutorial will guide you through the syntax, usage, and key considerations of the linefill.new()
function in Pine Script, ensuring you can effectively apply it to your trading strategies.
Syntax
The linefill.new()
function is used to create a new linefill object, which visually fills the space between two specified lines on a chart. The syntax for this function is as follows:
linefill.new(lineA, lineB, fillColor) → series linefill
Arguments
lineA
(series line
): This is the first line object that defines one boundary of the fill area.lineB
(series line
): The second line object that defines the other boundary of the fill area.fillColor
(series color
): Specifies the color used to fill the space betweenlineA
andlineB
.
Returns
- The function returns the ID of a linefill object. This ID can be used with other
linefill.*()
functions to modify or query the linefill object.
Remarks
- Dynamic Behavior: If either
lineA
orlineB
is deleted from the chart, the associated linefill object is also deleted. Moreover, if the lines are moved, the linefill object adjusts accordingly, maintaining the fill between the two lines. - Extension Handling: If both lines are extended in the same direction (for instance, both lines have
extend.right
), the space between these extensions is also filled. This is particularly useful for visualizing areas of interest that extend into the future of the chart.
Example
Let’s walk through a practical example of using the linefill.new()
function to highlight a specific area on a chart. Imagine you want to fill the space between a dynamic support line and a resistance line to visualize a trading channel.
//@version=5 indicator("Line Fill Example", overlay=true) // Define the dynamic support as a 20-period moving average dynamicSupportValue = ta.sma(close, 20) // Define the static resistance level (arbitrarily chosen for this example) staticResistanceValue = 150 // Plot the dynamic support line dynamicSupportLine = line.new(x1=bar_index[20], y1=dynamicSupportValue[20], x2=bar_index, y2=dynamicSupportValue, width=2, color=color.blue) // Plot the static resistance line staticResistanceLine = line.new(x1=bar_index[20], y1=staticResistanceValue, x2=bar_index, y2=staticResistanceValue, width=2, color=color.red) // Fill the area between the dynamic support and the static resistance fillColor = color.new(color.green, 90) // Semi-transparent green color channelFill = linefill.new(dynamicSupportLine, staticResistanceLine, fillColor)
Walkthrough of Code
- Indicator Declaration: The script starts with
//@version=5
, specifying that it uses version 5 of Pine Script. Theindicator
function declares a new indicator named “Line Fill Example” withoverlay=true
, indicating that the indicator will be drawn over the price chart. - Dynamic Support Calculation: It calculates the dynamic support level as a 20-period simple moving average (SMA) of the
close
prices. This means the dynamic support level adjusts with price changes over time, using the average closing price of the last 20 bars. - Static Resistance Definition: It sets a static resistance level at a value of 150. Unlike dynamic support, this resistance level does not change with price movements and is arbitrarily chosen for this example.
- Dynamic Support Line Plotting: A line representing the dynamic support is drawn using
line.new
. This line starts from the 20th previous bar (x1=bar_index[20]
) at the dynamic support value at that point (y1=dynamicSupportValue[20]
), extending to the current bar (x2=bar_index
) at the current dynamic support value (y2=dynamicSupportValue
). The line is styled with a width of 2 and colored blue. - Static Resistance Line Plotting: Similarly, a line for the static resistance is drawn using
line.new
, starting from the 20th previous bar to the current bar, at the y-level ofstaticResistanceValue
(150). This line is also styled with a width of 2 and colored red. - Area Fill Between Lines: The script then creates a semi-transparent green fill between the dynamic support and static resistance lines using
linefill.new
. It takes the previously defined lines as arguments (dynamicSupportLine
,staticResistanceLine
) and fills the area between them with a green color (color.green
) at 90% transparency.
Key Features and Takeaways
- Function Usability: The
linefill.new()
function is invaluable for visually representing relationships between two lines on a chart, such as support and resistance levels, making it easier to identify potential trading channels or areas of interest. - Syntax and Application: Understanding the syntax and application of
linefill.new()
allows for dynamic visualizations in trading strategies, adapting as lines move or are extended. - Visual Enhancement: This function significantly enhances the visual analysis capability within Pine Script, allowing traders and analysts to highlight key areas on a chart for better decision-making.
By incorporating the linefill.new()
function into your Pine Script strategies, you can leverage visual cues to enhance your trading decisions, making complex strategies more intuitive and visually accessible.