Home » Color Functions » Bgcolor Function In Pinescript

Bgcolor Function In Pinescript

Photo of author
Published on

Understanding the bgcolor() Function in Pine Script for Background Coloring

The bgcolor() function in Pine Script is a versatile tool for changing the background color of a script. This function is especially useful when the script is set to overlay mode (overlay = true), allowing it to color the chart’s background. Let’s dive deeper into how this function works and how it can be utilized in a script.

Function Syntax:

bgcolor(color, offset, editable, show_last, title) → void
  • color: This parameter accepts a “series color”, meaning it can be dynamically calculated in an expression.
  • offset: Determines the bar offset for the background color.
  • editable: Allows the background color to be editable in the script’s settings.
  • show_last: Specifies the number of last bars to apply the color to.
  • title: The title for the color setting in the script’s settings.

Dynamic Color Calculation and Transparency

To dynamically calculate the color, the color parameter can be set using expressions. If a specific transparency level is required, it can be generated using the color.new() function.

Example Script: Coloring Trading Sessions

The provided script colors the background of trading sessions, ideal for a 30-minute EURUSD chart.

//@version=5
indicator("Session backgrounds", overlay = true)

// Default color constants with transparency of 25.
BLUE_SHADE   = #0050FF40
PURPLE_SHADE = #0000FF40
PINK_SHADE   = #5000FF40
TRANSPARENT  = color(na)

// User-configurable colors.
earlyMarketColor  = input.color(BLUE_SHADE, "Pre-market")
mainSessionColor  = input.color(PURPLE_SHADE, "Regular Session")
lateMarketColor   = input.color(PINK_SHADE, "Post-market")

// Time range checker.
timeInRange(tf, session) =>
    time(tf, session) != 0

// Chart message printer.
printMessage(_text) =>
    var table _t = table.new(position.bottom_right, 1, 1)
    table.cell(_t, 0, 0, _text, bgcolor = color.yellow)

var is30MinChart = timeframe.isseconds or (timeframe.isintraday and timeframe.multiplier <= 30)
sessionShade = if is30MinChart
    switch
        timeInRange(timeframe.period, "0400-0930") => earlyMarketColor
        timeInRange(timeframe.period, "0930-1600") => mainSessionColor
        timeInRange(timeframe.period, "1600-2000") => lateMarketColor
        => TRANSPARENT
else
    printMessage("No background is displayed.\nChart timeframe must be <= 30min.")
    TRANSPARENT

bgcolor(sessionShade)
Bgcolor Function

Conclusion and Key Takeaways

  • Dynamic Background: The script dynamically changes the chart’s background color based on trading sessions.
  • User Customization: It provides user inputs for color customization, enhancing adaptability.
  • Timeframe Restriction: Includes checks for chart timeframe, ensuring the script’s functionality is limited to appropriate settings.
  • Visual Feedback: Offers feedback when the script is not applicable, guiding users effectively.
  • Versatile Usage: Demonstrates a versatile application of the bgcolor() function in Pine Script for session-based background coloring.

This script showcases the practical application of the bgcolor() function in Pine Script, enabling dynamic and user-interactive background coloring based on trading sessions.

Leave a Comment