

We use the ? : ternary operator to write our conditional statement. We define a c_ma color variable that is assigned one of our two colors, depending on the value of the maRising boolean.We define an maRising boolean variable which will hold true when the moving average is higher on the current bar than it was on the last.We provide users of our script a selection of colors for our bull/bear colors.color c_ma = maRising ? i_c_maBull : i_c_maBear plot ( ma, "MA", c_ma, 2 ) bool maRising = rising ( ma, 1 ) // Build our color. Build new colors dynamically, by calculating them as the script executes bar to bar, to implement color gradations, for study ( "Conditional colors", "", true ) int i_length = input ( 20, "Length", minval = 2 ) color i_c_maBull = input ( een, "Bull" ) color i_c_maBear = input ( color.maroon, "Bear" ) float ma = sma ( close, i_length ) // Define our states.Use conditional statements to select colors from a few pre-determined base colors.For those cases, Pine programmers have two options: Sometimes, however, colors need to be created as the script executes on each bar because they depend on conditions that are unknown at compile time, or when the script begins execution on bar zero. The colors in the previous script do not vary as the script executes bar to bar. This is also true for other functions with a transp parameter. Additionally, if you use a color argument that already includes transparency information, as is done in the next three plot() calls, any argument used for the transp parameter would have no effect. Using the transp parameter to define transparency is not as flexible because it requires an argument of input integer type, which entails it must be known before the script is executed, and so cannot be calculated dynamically, as your script executes bar to bar. This use should be avoided as the transp is slated for deprecation in Pine v5. The first two plot() calls specify transparency using the transp parameter. All five methods are functionally equivalent:

In the following script, all plots use the same color.olive color with a transparency of 40, but expressed in different ways. This table lists their names, hexadecimal equivalent, and RGB values as arguments to color.rgb(): Name Pine has built-in colors such as een, as well as functions like color.rgb() which allow you to dynamically generate any color in the RGBA color space.

The only exception to this rule is that a pane indicator can color chart bars or candles.

The color of bars or the body of candles appearing on a chart.Ī script can only color the elements it places in its own visual space.The background of a script’s visual space, whether the script is running in its own pane, or in overlay mode on the chart.Any element you can plot or draw in an indicator’s visual space, be it lines, fills, text or candles.The 4,294,967,296 possible assemblies of color and transparency available in Pine can be applied to: Using colors in Pine can be as simple as you want, or as involved as your concept requires. Good visual designs establish a visual hierarchy that allows the more important information to stand out, and the less important one to not get in the way. Well-designed plots and drawings make indicators easier to use and understand. Script visuals can play a critical role in the usability of the indicators we write in Pine.
