Home » Color Functions » Introduction to Line Fills in Pine Script

Introduction to Line Fills in Pine Script

Photo of author
Published on

In this tutorial, we delve into the concept of line fills in Pine Script, a powerful feature that enhances the visual analysis of charts. Pine Script is a domain-specific language used in TradingView to develop custom indicators and strategies. We will explore how to use the linefill.new() function to create visually impactful areas between two lines on a chart.

Understanding the linefill.new() Function

The linefill.new() function in Pine Script is used for filling the space between two line drawings. This function is particularly useful for highlighting areas between trend lines or indicating channels. Its basic syntax is:

linefill.new(line1, line2, color) → series linefill

Here, line1 and line2 refer to the IDs of the two lines you want to fill between, and color is the color of the fill. This function returns the ID of the linefill object, which can be stored in a variable for later modifications, such as changing its color.

Key Characteristics

  1. Unique Linefill per Line Pair: For any pair of lines, only one linefill can exist. Repeated calls to linefill.new() with the same pair of lines will replace the previous fill.
  2. Dependent on Line Coordinates: The position and extension of a linefill are directly tied to the lines it connects. It’s crucial to ensure the lines are extended correctly for the linefill to behave as expected.

Example: Creating a Channel Indicator

In this example, we demonstrate how to use line fills to create a channel indicator, which can help in visualizing the movement between two pivot points on a chart.

Code Explanation

indicator("Channel", overlay = true)

// Length parameters for pivot calculation

// Calculation of pivot points
pH = ta.pivothigh(LEN_LEFT, LEN_RIGHT)
pL = ta.pivotlow(LEN_LEFT, LEN_RIGHT)

// Determining bar indices for pivot points
pH_x1 = ta.valuewhen(pH, bar_index, 1) - LEN_RIGHT
pH_x2 = ta.valuewhen(pH, bar_index, 0) - LEN_RIGHT
pL_x1 = ta.valuewhen(pL, bar_index, 1) - LEN_RIGHT
pL_x2 = ta.valuewhen(pL, bar_index, 0) - LEN_RIGHT

// Obtaining price values for pivot points
pH_y1 = ta.valuewhen(pH, pH, 1)
pH_y2 = ta.valuewhen(pH, pH, 0)
pL_y1 = ta.valuewhen(pL, pL, 1)
pL_y2 = ta.valuewhen(pL, pL, 0)

// Drawing lines and filling the area between them
if barstate.islastconfirmedhistory
    lH = line.new(pH_x1, pH_y1, pH_x2, pH_y2, extend = extend.right)
    lL = line.new(pL_x1, pL_y1, pL_x2, pL_y2, extend = extend.right)
    // Determining fill color based on pivot movement
    fillColor = switch
        pH_y2 > pH_y1 and pL_y2 > pL_y1 => color.green
        pH_y2 < pH_y1 and pL_y2 < pL_y1 => color.red
        => color.silver
    // Applying the fill
    linefill.new(lH, lL, color.new(fillColor, 90))
Creating a Channel Indicator

Breakdown of the Code:

  • Pivot Calculation: We use ta.pivothigh and ta.pivotlow to find pivot points, which are significant highs and lows on the chart.
  • Line Drawing: Two lines (lH and lL) are drawn connecting these pivot points.
  • Line Extension: The lines are extended to the right to indicate future projections.
  • Color Determination: The fill color changes based on the relative movement of the pivots.
  • Line Fill Application: linefill.new() is used to fill the space between the two lines, enhancing the visual representation of the channel.

Key Features and Takeaways

  • Function Usability: linefill.new() is crucial for visual representation in trading scripts, allowing for clear demarcation of areas between trends.
  • Syntax and Application: Understanding the syntax of linefill.new(line1, line2, color) is key. It connects two lines and fills the area between them with a specified color.
  • Dependence on Line Coordinates: The behavior and appearance of linefills are directly linked to the lines they are attached to. This requires careful plotting of the lines for accurate representation.

By integrating line fills into your Pine Script indicators, you enhance the visual effectiveness of your charts, making it easier to identify patterns and trends. This feature is invaluable for traders and analysts looking to make informed decisions based on chart analysis.

Leave a Comment