pinescript-mcp 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. pinescript_mcp/__init__.py +7 -0
  2. pinescript_mcp/__main__.py +6 -0
  3. pinescript_mcp/docs/LLM_MANIFEST.md +96 -0
  4. pinescript_mcp/docs/concepts/colors_and_display.md +574 -0
  5. pinescript_mcp/docs/concepts/common_errors.md +550 -0
  6. pinescript_mcp/docs/concepts/execution_model.md +1059 -0
  7. pinescript_mcp/docs/concepts/methods.md +543 -0
  8. pinescript_mcp/docs/concepts/objects.md +349 -0
  9. pinescript_mcp/docs/concepts/timeframes.md +73 -0
  10. pinescript_mcp/docs/pine_v6_functions.json +488 -0
  11. pinescript_mcp/docs/reference/annotations.md +229 -0
  12. pinescript_mcp/docs/reference/constants.md +1694 -0
  13. pinescript_mcp/docs/reference/functions/collections.md +2793 -0
  14. pinescript_mcp/docs/reference/functions/drawing.md +1082 -0
  15. pinescript_mcp/docs/reference/functions/general.md +1892 -0
  16. pinescript_mcp/docs/reference/functions/request.md +232 -0
  17. pinescript_mcp/docs/reference/functions/strategy.md +1517 -0
  18. pinescript_mcp/docs/reference/functions/ta.md +1225 -0
  19. pinescript_mcp/docs/reference/keywords.md +487 -0
  20. pinescript_mcp/docs/reference/operators.md +311 -0
  21. pinescript_mcp/docs/reference/pine_v6_cheatsheet.md +183 -0
  22. pinescript_mcp/docs/reference/types.md +405 -0
  23. pinescript_mcp/docs/reference/variables.md +2272 -0
  24. pinescript_mcp/docs/visuals/backgrounds.md +110 -0
  25. pinescript_mcp/docs/visuals/bar_coloring.md +35 -0
  26. pinescript_mcp/docs/visuals/bar_plotting.md +93 -0
  27. pinescript_mcp/docs/visuals/colors.md +574 -0
  28. pinescript_mcp/docs/visuals/fills.md +497 -0
  29. pinescript_mcp/docs/visuals/levels.md +91 -0
  30. pinescript_mcp/docs/visuals/lines_and_boxes.md +1252 -0
  31. pinescript_mcp/docs/visuals/overview.md +563 -0
  32. pinescript_mcp/docs/visuals/plots.md +390 -0
  33. pinescript_mcp/docs/visuals/tables.md +207 -0
  34. pinescript_mcp/docs/visuals/texts_and_shapes.md +862 -0
  35. pinescript_mcp/docs/writing_scripts/debugging.md +1943 -0
  36. pinescript_mcp/docs/writing_scripts/limitations.md +457 -0
  37. pinescript_mcp/docs/writing_scripts/profiling_and_optimization.md +2016 -0
  38. pinescript_mcp/docs/writing_scripts/publishing_scripts.md +327 -0
  39. pinescript_mcp/docs/writing_scripts/style_guide.md +245 -0
  40. pinescript_mcp/server.py +570 -0
  41. pinescript_mcp-0.1.0.dist-info/METADATA +121 -0
  42. pinescript_mcp-0.1.0.dist-info/RECORD +45 -0
  43. pinescript_mcp-0.1.0.dist-info/WHEEL +4 -0
  44. pinescript_mcp-0.1.0.dist-info/entry_points.txt +2 -0
  45. pinescript_mcp-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,574 @@
1
+
2
+ # [Colors](https://www.tradingview.com/pine-script-docs/visuals/colors/#colors)
3
+
4
+ ## [Introduction](https://www.tradingview.com/pine-script-docs/visuals/colors/#introduction)
5
+
6
+ Script visuals can play a critical role in the usability of the indicators we write in Pine Script®. Well-designed plots and drawings make indicators easier to use and understand. 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.
7
+
8
+ Using colors in Pine can be as simple as you want, or as involved as your concept requires. The 4,294,967,296 possible assemblies of color and transparency available in Pine Script can be applied to:
9
+
10
+ - Any element you can plot or draw in an indicator’s visual space, be it lines, fills, text or candles.
11
+ - The background of a script’s visual space, whether the script is running in its own pane, or in overlay mode on the chart.
12
+ - The color of bars or the body of candles appearing on a chart.
13
+
14
+ A script can only color the elements it places in its own visual space. The only exception to this rule is that a pane indicator can color chart bars or candles.
15
+
16
+ Pine Script has built-in colors such as [color.green](https://www.tradingview.com/pine-script-reference/v6/#const_color.green), as well as functions like [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb) which allow you to dynamically generate any color in the RGBA color space.
17
+
18
+ ### [Transparency](https://www.tradingview.com/pine-script-docs/visuals/colors/#transparency)
19
+
20
+ Each color in Pine Script is defined by four values:
21
+
22
+ - Its red, green and blue components (0-255), following the [RGB color model](https://en.wikipedia.org/wiki/RGB_color_space).
23
+ - Its transparency (0-100), often referred to as the Alpha channel outside Pine, as defined in the [RGBA color model](https://en.wikipedia.org/wiki/RGBA_color_model). Even though transparency is expressed in the 0-100 range, its value can be a “float” when used in functions, which gives you access to the 256 underlying values of the alpha channel.
24
+
25
+ The transparency of a color defines how opaque it is: zero is fully opaque, 100 makes the color — whichever it is — invisible. Modulating transparency can be crucial in more involved color visuals or when using backgrounds, to control which colors dominate the others, and how they mix together when superimposed.
26
+
27
+ ## [Constant colors](https://www.tradingview.com/pine-script-docs/visuals/colors/#constant-colors)
28
+
29
+ There are 17 built-in colors in Pine Script. This table lists their names, hexadecimal equivalent, and RGB values as arguments to [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb):
30
+
31
+ Name
32
+
33
+ Hex
34
+
35
+ RGB values
36
+
37
+ color.aqua
38
+
39
+ #00BCD4
40
+
41
+ color.rgb(0, 188, 212)
42
+
43
+ color.black
44
+
45
+ #363A45
46
+
47
+ color.rgb(54, 58, 69)
48
+
49
+ color.blue
50
+
51
+ #2196F3
52
+
53
+ color.rgb(33, 150, 243)
54
+
55
+ color.fuchsia
56
+
57
+ #E040FB
58
+
59
+ color.rgb(224, 64, 251)
60
+
61
+ color.gray
62
+
63
+ #787B86
64
+
65
+ color.rgb(120, 123, 134)
66
+
67
+ color.green
68
+
69
+ #4CAF50
70
+
71
+ color.rgb(76, 175, 80)
72
+
73
+ color.lime
74
+
75
+ #00E676
76
+
77
+ color.rgb(0, 230, 118)
78
+
79
+ color.maroon
80
+
81
+ #880E4F
82
+
83
+ color.rgb(136, 14, 79)
84
+
85
+ color.navy
86
+
87
+ #311B92
88
+
89
+ color.rgb(49, 27, 146)
90
+
91
+ color.olive
92
+
93
+ #808000
94
+
95
+ color.rgb(128, 128, 0)
96
+
97
+ color.orange
98
+
99
+ #FF9800
100
+
101
+ color.rgb(255, 152, 0)
102
+
103
+ color.purple
104
+
105
+ #9C27B0
106
+
107
+ color.rgb(156, 39, 176)
108
+
109
+ color.red
110
+
111
+ #F23645
112
+
113
+ color.rgb(242, 54, 69)
114
+
115
+ color.silver
116
+
117
+ #B2B5BE
118
+
119
+ color.rgb(178, 181, 190)
120
+
121
+ color.teal
122
+
123
+ #089981
124
+
125
+ color.rgb(8, 153, 129)
126
+
127
+ color.white
128
+
129
+ #FFFFFF
130
+
131
+ color.rgb(255, 255, 255)
132
+
133
+ color.yellow
134
+
135
+ #FDD835
136
+
137
+ color.rgb(253, 216, 53)
138
+
139
+ The following script shows three different ways to express [color.olive](https://www.tradingview.com/pine-script-reference/v6/#const_color.olive) with 40% transparency. All three methods are functionally equivalent:
140
+
141
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-Constant-colors-1.CnlC-u-k_1ibe2x.webp)
142
+
143
+ [Pine Script®](https://tradingview.com/pine-script-docs)
144
+
145
+ Copied
146
+
147
+ ``//@version=6
148
+ indicator("Constant colors demo", overlay = true)
149
+
150
+ // Create a plot using the hex color code equivalent for `color.olive` with `99` as the alpha value (60% opacity).
151
+ plot(ta.sma(close, 10), "10-bar SMA", #80800099, 3)
152
+ // Create a plot using `color.new()` to modify `color.olive` with 40% transparency.
153
+ plot(ta.sma(close, 30), "30-bar SMA", color.new(color.olive, 40), 3)
154
+ // Create a plot using `color.rgb()` with the `r`, `g`, and `b` components of `color.olive` and 40% transparency.
155
+ plot(ta.sma(close, 50), "50-bar SMA", color.rgb(128, 128, 0, 40), 3)
156
+ ``
157
+
158
+ Note that:
159
+
160
+ - An alpha value of `99` in a hexadecimal color code is equivalent to 60% opacity, meaning the resulting color is 40% transparent.
161
+ - Transparency does _not_ affect plot outputs in the status line, price scale, or Data Window. All these locations show the color with 0% transparency.
162
+
163
+ The colors in the previous script do not vary as the script executes bar to bar. 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. For those cases, programmers have two options:
164
+
165
+ 1. Use conditional statements to select colors from a few pre-determined base colors.
166
+ 2. Build new colors dynamically, by calculating them as the script executes bar to bar, to implement color gradients, for example.
167
+
168
+ ## [Conditional coloring](https://www.tradingview.com/pine-script-docs/visuals/colors/#conditional-coloring)
169
+
170
+ Let’s say you want to color a moving average in different colors, depending on some conditions you define. To do so, you can use a conditional statement that will select a different color for each of your states. Let’s start by coloring a moving average in a bull color when it’s rising, and in a bear color when it’s not:
171
+
172
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-ConditionalColors-1.D1QaF3Y-_ZumNWi.webp)
173
+
174
+ [Pine Script®](https://tradingview.com/pine-script-docs)
175
+
176
+ Copied
177
+
178
+ `//@version=6
179
+ indicator("Conditional colors", "", true)
180
+ int lengthInput = input.int(20, "Length", minval = 2)
181
+ color maBullColorInput = input.color(color.green, "Bull")
182
+ color maBearColorInput = input.color(color.maroon, "Bear")
183
+ float ma = ta.sma(close, lengthInput)
184
+ // Define our states.
185
+ bool maRising = ta.rising(ma, 1)
186
+ // Build our color.
187
+ color maColor = maRising ? maBullColorInput : maBearColorInput
188
+ plot(ma, "MA", maColor, 2)
189
+ `
190
+
191
+ Note that:
192
+
193
+ - We provide users of our script a selection of colors for our bull/bear colors.
194
+ - 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.
195
+ - We define an `maColor` variable that is assigned one of our two colors, depending on the value of the `maRising` variable. We use the [ternary operator](https://www.tradingview.com/pine-script-docs/language/operators/#-ternary-operator) to define our conditional expression.
196
+
197
+ You can also use conditional colors to avoid plotting under certain conditions. Here, we plot high and low pivots using a line, but we do not want to plot anything when a new pivot comes in, to avoid the joints that would otherwise appear in pivot transitions. To do so, we test for pivot changes and use [na](https://www.tradingview.com/pine-script-reference/v6/#var_na) as the color value when a change is detected, so that no line is plotted on that bar:
198
+
199
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-ConditionalColors-2.erY98c4P_Z1OwyIO.webp)
200
+
201
+ [Pine Script®](https://tradingview.com/pine-script-docs)
202
+
203
+ Copied
204
+
205
+ `//@version=6
206
+ indicator("Conditional colors", "", true)
207
+ int legsInput = input.int(5, "Pivot Legs", minval = 1)
208
+ color pHiColorInput = input.color(color.olive, "High pivots")
209
+ color pLoColorInput = input.color(color.orange, "Low pivots")
210
+ // Intialize the pivot level variables.
211
+ var float pHi = na
212
+ var float pLo = na
213
+ // When a new pivot is detected, save its value.
214
+ pHi := nz(ta.pivothigh(legsInput, legsInput), pHi)
215
+ pLo := nz(ta.pivotlow( legsInput, legsInput), pLo)
216
+ // When a new pivot is detected, do not plot a color.
217
+ plot(pHi, "High", ta.change(pHi) != 0 ? na : pHiColorInput, 2, plot.style_line)
218
+ plot(pLo, "Low", ta.change(pLo) != 0 ? na : pLoColorInput, 2, plot.style_line)
219
+ `
220
+
221
+ To undertand how this code works, one must first know that [ta.pivothigh()](https://www.tradingview.com/pine-script-reference/v6/#fun_ta.pivothigh) and [ta.pivotlow()](https://www.tradingview.com/pine-script-reference/v6/#fun_ta.pivotlow), used as they are here without an argument to the `source` parameter, will return a value when they find a [high](https://www.tradingview.com/pine-script-reference/v6/#var_high)/[low](https://www.tradingview.com/pine-script-reference/v6/#var_low) pivot, otherwise they return [na](https://www.tradingview.com/pine-script-reference/v6/#var_na).
222
+
223
+ When we test the value returned by the pivot function for [na](https://www.tradingview.com/pine-script-reference/v6/#var_na) using the [nz()](https://www.tradingview.com/pine-script-reference/v6/#fun_nz) function, we allow the value returned to be assigned to the `pHi` or `pLo` variables only when it is not [na](https://www.tradingview.com/pine-script-reference/v6/#var_na), otherwise the previous value of the variable is simply reassigned to it, which has no impact on its value. Keep in mind that previous values of `pHi` and `pLo` are preserved bar to bar because we use the [var](https://www.tradingview.com/pine-script-reference/v6/#kw_var) keyword when initializing them, which causes the initialization to only occur on the first bar.
224
+
225
+ All that’s left to do next is, when we plot our lines, to insert a ternary conditional statement that will yield [na](https://www.tradingview.com/pine-script-reference/v6/#var_na) for the color when the pivot value changes, or the color selected in the script’s inputs when the pivot level does not change.
226
+
227
+ ## [Calculated colors](https://www.tradingview.com/pine-script-docs/visuals/colors/#calculated-colors)
228
+
229
+ Using functions like [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new), [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb) and [color.from_gradient()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.from_gradient), one can build colors on the fly, as the script executes bar to bar.
230
+
231
+ [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) is most useful when you need to generate different transparency levels from a base color.
232
+
233
+ [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb) is useful when you need to build colors dynamically from red, green, blue, or tranparency components. While [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb) creates a color, its sister functions [color.r()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.r), [color.g()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.g), [color.b()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.b) and [color.t()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.t) can be used to extract the red, green, blue or transparency values from a color, which can in turn be used to generate a variant.
234
+
235
+ [color.from_gradient()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.from_gradient) is useful to create linear gradients between two base colors. It determines which intermediary color to use by evaluating a source value against minimum and maximum values.
236
+
237
+ ### [color.new()](https://www.tradingview.com/pine-script-docs/visuals/colors/#colornew)
238
+
239
+ Let’s put [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) to use to create different transparencies for volume columns using one of two bull/bear base colors:
240
+
241
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-CalculatingColors-1.BtEyXAO2_Z2dfpcb.webp)
242
+
243
+ [Pine Script®](https://tradingview.com/pine-script-docs)
244
+
245
+ Copied
246
+
247
+ ``//@version=6
248
+ indicator("Volume")
249
+ // We name our color constants to make them more readable.
250
+ var color GOLD_COLOR = #CCCC00ff
251
+ var color VIOLET_COLOR = #AA00FFff
252
+ color bullColorInput = input.color(GOLD_COLOR, "Bull")
253
+ color bearColorInput = input.color(VIOLET_COLOR, "Bear")
254
+ int levelsInput = input.int(10, "Gradient levels", minval = 1)
255
+ // We initialize only once on bar zero with `var`, otherwise the count would reset to zero on each bar.
256
+ var float riseFallCnt = 0
257
+ // Count the rises/falls, clamping the range to: 1 to `i_levels`.
258
+ riseFallCnt := math.max(1, math.min(levelsInput, riseFallCnt + math.sign(volume - nz(volume[1]))))
259
+ // Rescale the count on a scale of 80, reverse it and cap transparency to <80 so that colors remains visible.
260
+ float transparency = 80 - math.abs(80 * riseFallCnt / levelsInput)
261
+ // Build the correct transparency of either the bull or bear color.
262
+ color volumeColor = color.new(close > open ? bullColorInput : bearColorInput, transparency)
263
+ plot(volume, "Volume", volumeColor, 1, plot.style_columns)
264
+ ``
265
+
266
+ Note that:
267
+
268
+ - In the next to last line of our script, we dynamically calculate the column color by varying both the base color used, depending on whether the bar is up or down, **and** the transparency level, which is calculated from the cumulative rises or falls of volume.
269
+ - We offer the script user control over not only the base bull/bear colors used, but also on the number of brightness levels we use. We use this value to determine the maximum number of rises or falls we will track. Giving users the possiblity to manage this value allows them to adapt the indicator’s visuals to the timeframe or market they use.
270
+ - We take care to control the maximum level of transparency we use so that it never goes higher than 80. This ensures our colors always retain some visibility.
271
+ - We also set the minimum value for the number of levels to 1 in the inputs. When the user selects 1, the volume columns will be either in bull or bear color of maximum brightness — or transparency zero.
272
+
273
+ ### [color.rgb()](https://www.tradingview.com/pine-script-docs/visuals/colors/#colorrgb)
274
+
275
+ In our next example we use [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb) to build colors from RGBA values. We use the result in a holiday season gift for our friends, so they can bring their TradingView charts to parties:
276
+
277
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-CalculatingColors-2.B7PJa61g_ZI19Ev.webp)
278
+
279
+ [Pine Script®](https://tradingview.com/pine-script-docs)
280
+
281
+ Copied
282
+
283
+ `//@version=6
284
+ indicator("Holiday candles", "", true)
285
+ float r = math.random(0, 255)
286
+ float g = math.random(0, 255)
287
+ float b = math.random(0, 255)
288
+ float t = math.random(0, 100)
289
+ color holidayColor = color.rgb(r, g, b, t)
290
+ plotcandle(open, high, low, close, color = holidayColor, wickcolor = holidayColor, bordercolor = holidayColor)
291
+ `
292
+
293
+ Note that:
294
+
295
+ - We generate values in the zero to 255 range for the red, green and blue channels, and in the zero to 100 range for transparency. Also note that because [math.random()](https://www.tradingview.com/pine-script-reference/v6/#fun_math.random) returns float values, the float 0.0-100.0 range provides access to the full 0-255 transparency values of the underlying alpha channel.
296
+ - We use the [math.random(min, max, seed)](https://www.tradingview.com/pine-script-reference/v6/#fun_math.random) function to generate pseudo-random values. We do not use an argument for the third parameter of the function: `seed`. Using it is handy when you want to ensure the repeatability of the function’s results. Called with the same seed, it will produce the same sequence of values.
297
+
298
+ ### [color.from_gradient()](https://www.tradingview.com/pine-script-docs/visuals/colors/#colorfrom_gradient)
299
+
300
+ Our last examples of color calculations will use [color.from_gradient()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.from_gradient). Let’s first use it in its simplest form, to color a CCI signal in a version of the indicator that otherwise looks like the built-in:
301
+
302
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-CalculatingColors-3.BWe7BFsC_Zg2kjC.webp)
303
+
304
+ [Pine Script®](https://tradingview.com/pine-script-docs)
305
+
306
+ Copied
307
+
308
+ `//@version=6
309
+ indicator(title="CCI line gradient", precision=2, timeframe="")
310
+ var color GOLD_COLOR = #CCCC00
311
+ var color VIOLET_COLOR = #AA00FF
312
+ var color BEIGE_COLOR = #9C6E1B
313
+ float srcInput = input.source(close, title="Source")
314
+ int lenInput = input.int(20, "Length", minval = 5)
315
+ color bullColorInput = input.color(GOLD_COLOR, "Bull")
316
+ color bearColorInput = input.color(BEIGE_COLOR, "Bear")
317
+ float signal = ta.cci(srcInput, lenInput)
318
+ color signalColor = color.from_gradient(signal, -200, 200, bearColorInput, bullColorInput)
319
+ plot(signal, "CCI", signalColor)
320
+ bandTopPlotID = hline(100, "Upper Band", color.silver, hline.style_dashed)
321
+ bandBotPlotID = hline(-100, "Lower Band", color.silver, hline.style_dashed)
322
+ fill(bandTopPlotID, bandBotPlotID, color.new(BEIGE_COLOR, 90), "Background")
323
+ `
324
+
325
+ Note that:
326
+
327
+ - To calculate the gradient, [color.from_gradient()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.from_gradient) requires minimum and maximum values against which the argument used for the `value` parameter will be compared. The fact that we want a gradient for an unbounded signal like CCI (i.e., without fixed boundaries such as RSI, which always oscillates between 0-100), does not entail we cannot use [color.from_gradient()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.from_gradient). Here, we solve our conundrum by providing values of -200 and 200 as arguments. They do not represent the real minimum and maximum values for CCI, but they are at levels from which we do not mind the colors no longer changing, as whenever the series is outside the `bottom_value` and `top_value` limits, the colors used for `bottom_color` and `top_color` will apply.
328
+ - The color progression calculated by [color.from_gradient()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.from_gradient) is linear. If the value of the series is halfway between the `bottom_value` and `top_value` arguments, the generated color’s RGBA components will also be halfway between those of `bottom_color` and `top_color`.
329
+ - Many common indicator calculations are available in Pine Script as built-in functions. Here we use [ta.cci()](https://www.tradingview.com/pine-script-reference/v6/#fun_ta.cci) instead of calculating it the long way.
330
+
331
+ The argument used for `value` in [color.from_gradient()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.from_gradient) does not necessarily have to be the value of the line we are calculating. Anything we want can be used, as long as arguments for `bottom_value` and `top_value` can be supplied. Here, we enhance our CCI indicator by coloring the band using the number of bars since the signal has been above/below the centerline:
332
+
333
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-CalculatingColors-4.-U0l6lwc_Zd5X5K.webp)
334
+
335
+ [Pine Script®](https://tradingview.com/pine-script-docs)
336
+
337
+ Copied
338
+
339
+ ``//@version=6
340
+ indicator(title="CCI line gradient", precision=2, timeframe="")
341
+ var color GOLD_COLOR = #CCCC00
342
+ var color VIOLET_COLOR = #AA00FF
343
+ var color GREEN_BG_COLOR = color.new(color.green, 70)
344
+ var color RED_BG_COLOR = color.new(color.maroon, 70)
345
+ float srcInput = input.source(close, "Source")
346
+ int lenInput = input.int(20, "Length", minval = 5)
347
+ int stepsInput = input.int(50, "Gradient levels", minval = 1)
348
+ color bullColorInput = input.color(GOLD_COLOR, "Line: Bull", inline = "11")
349
+ color bearColorInput = input.color(VIOLET_COLOR, "Bear", inline = "11")
350
+ color bullBgColorInput = input.color(GREEN_BG_COLOR, "Background: Bull", inline = "12")
351
+ color bearBgColorInput = input.color(RED_BG_COLOR, "Bear", inline = "12")
352
+
353
+ // Plot colored signal line.
354
+ float signal = ta.cci(srcInput, lenInput)
355
+ color signalColor = color.from_gradient(signal, -200, 200, color.new(bearColorInput, 0), color.new(bullColorInput, 0))
356
+ plot(signal, "CCI", signalColor, 2)
357
+
358
+ // Detect crosses of the centerline.
359
+ bool signalX = ta.cross(signal, 0)
360
+ // Count no of bars since cross. Capping it to the no of steps from inputs.
361
+ int gradientStep = math.min(stepsInput, nz(ta.barssince(signalX)))
362
+ // Choose bull/bear end color for the gradient.
363
+ color endColor = signal > 0 ? bullBgColorInput : bearBgColorInput
364
+ // Get color from gradient going from no color to `endColor`
365
+ color bandColor = color.from_gradient(gradientStep, 0, stepsInput, na, endColor)
366
+ bandTopPlotID = hline(100, "Upper Band", color.silver, hline.style_dashed)
367
+ bandBotPlotID = hline(-100, "Lower Band", color.silver, hline.style_dashed)
368
+ fill(bandTopPlotID, bandBotPlotID, bandColor, title = "Band")
369
+ ``
370
+
371
+ Note that:
372
+
373
+ - The signal plot uses the same base colors and gradient as in our previous example. We have however increased the width of the line from the default 1 to 2. It is the most important component of our visuals; increasing its width is a way to give it more prominence, and ensure users are not distracted by the band, which has become busier than it was in its original, flat beige color.
374
+ - The fill must remain unobtrusive for two reasons. First, it is of secondary importance to the visuals, as it provides complementary information, i.e., the duration for which the signal has been in bull/bear territory. Second, since fills have a greater z-index than plots, the fill will cover the signal plot. For these reasons, we make the fill’s base colors fairly transparent, at 70, so they do not mask the plots. The gradient used for the band starts with no color at all (see the [na](https://www.tradingview.com/pine-script-reference/v6/#var_na) used as the argument to `bottom_color` in the [color.from_gradient()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.from_gradient) call), and goes to the base bull/bear colors from the inputs, which the conditional `endColor` variable contains.
375
+ - We provide users with distinct bull/bear color selections for the line and the band.
376
+ - When we calculate the `gradientStep` variable, we use [nz()](https://www.tradingview.com/pine-script-reference/v6/#fun_nz) on [ta.barssince()](https://www.tradingview.com/pine-script-reference/v6/#fun_ta.barssince) because in early bars of the dataset, when the condition tested has not occurred yet, [ta.barssince()](https://www.tradingview.com/pine-script-reference/v6/#fun_ta.barssince) will return [na](https://www.tradingview.com/pine-script-reference/v6/#var_na). Because we use [nz()](https://www.tradingview.com/pine-script-reference/v6/#fun_nz), the value returned is replaced with zero in those cases.
377
+
378
+ ## [Mixing transparencies](https://www.tradingview.com/pine-script-docs/visuals/colors/#mixing-transparencies)
379
+
380
+ In this example we take our CCI indicator in another direction. We will build dynamically adjusting extremes zone buffers using a Donchian Channel (historical highs/lows) calculated from the CCI. We build the top/bottom bands by making them 1/4 the height of the DC. We will use a dynamically adjusting lookback to calculate the DC. To modulate the lookback, we will calculate a simple measure of volatility by keeping a ratio of a short-period ATR to a long one. When that ratio is higher than 50 of its last 100 values, we consider the volatility high. When the volatility is high/low, we decrease/increase the lookback.
381
+
382
+ Our aim is to provide users of our indicator with:
383
+
384
+ - The CCI line colored using a bull/bear gradient, as we illustrated in our most recent examples.
385
+ - The top and bottom bands of the Donchian Channel, filled in such a way that their color darkens as a historical high/low becomes older and older.
386
+ - A way to appreciate the state of our volatility measure, which we will do by painting the background with one color whose intensity increases when volatility increases.
387
+
388
+ This is what our indicator looks like using the light theme:
389
+
390
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-MixingTransparencies-1.DJ-yTBxm_1VcSbJ.webp)
391
+
392
+ And with the dark theme:
393
+
394
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-MixingTransparencies-2.BJOvmETq_Z1CqQBv.webp)
395
+
396
+ [Pine Script®](https://tradingview.com/pine-script-docs)
397
+
398
+ Copied
399
+
400
+ ``//@version=6
401
+ indicator("CCI DC", precision = 6)
402
+ color GOLD_COLOR = #CCCC00ff
403
+ color VIOLET_COLOR = #AA00FFff
404
+ int lengthInput = input.int(20, "Length", minval = 5)
405
+ color bullColorInput = input.color(GOLD_COLOR, "Bull")
406
+ color bearColorInput = input.color(VIOLET_COLOR, "Bear")
407
+
408
+ // ————— Function clamps `val` between `min` and `max`.
409
+ clamp(val, min, max) =>
410
+ math.max(min, math.min(max, val))
411
+
412
+ // ————— Volatility expressed as 0-100 value.
413
+ float v = ta.atr(lengthInput / 5) / ta.atr(lengthInput * 5)
414
+ float vPct = ta.percentrank(v, lengthInput * 5)
415
+
416
+ // ————— Calculate dynamic lookback for DC. It increases/decreases on low/high volatility.
417
+ bool highVolatility = vPct > 50
418
+ var int lookBackMin = lengthInput * 2
419
+ var int lookBackMax = lengthInput * 10
420
+ var float lookBack = math.avg(lookBackMin, lookBackMax)
421
+ lookBack += highVolatility ? -2 : 2
422
+ lookBack := clamp(lookBack, lookBackMin, lookBackMax)
423
+
424
+ // ————— Dynamic lookback length Donchian channel of signal.
425
+ float signal = ta.cci(close, lengthInput)
426
+ // `lookBack` is a float; need to cast it to int to be used a length.
427
+ float hiTop = ta.highest(signal, int(lookBack))
428
+ float loBot = ta.lowest( signal, int(lookBack))
429
+ // Get margin of 25% of the DC height to build high and low bands.
430
+ float margin = (hiTop - loBot) / 4
431
+ float hiBot = hiTop - margin
432
+ float loTop = loBot + margin
433
+ // Center of DC.
434
+ float center = math.avg(hiTop, loBot)
435
+
436
+ // ————— Create colors.
437
+ color signalColor = color.from_gradient(signal, -200, 200, bearColorInput, bullColorInput)
438
+ // Bands: Calculate transparencies so the longer since the hi/lo has changed,
439
+ // the darker the color becomes. Cap highest transparency to 90.
440
+ float hiTransp = clamp(100 - (100 * math.max(1, nz(ta.barssince(ta.change(hiTop) != 0) + 1)) / 255), 60, 90)
441
+ float loTransp = clamp(100 - (100 * math.max(1, nz(ta.barssince(ta.change(loBot) != 0) + 1)) / 255), 60, 90)
442
+ color hiColor = color.new(bullColorInput, hiTransp)
443
+ color loColor = color.new(bearColorInput, loTransp)
444
+ // Background: Rescale the 0-100 range of `vPct` to 0-25 to create 75-100 transparencies.
445
+ color bgColor = color.new(color.gray, 100 - (vPct / 4))
446
+
447
+ // ————— Plots
448
+ // Invisible lines for band fills.
449
+ hiTopPlotID = plot(hiTop, color = na)
450
+ hiBotPlotID = plot(hiBot, color = na)
451
+ loTopPlotID = plot(loTop, color = na)
452
+ loBotPlotID = plot(loBot, color = na)
453
+ // Plot signal and centerline.
454
+ p_signal = plot(signal, "CCI", signalColor, 2)
455
+ plot(center, "Centerline", color.silver, 1)
456
+
457
+ // Fill the bands.
458
+ fill(hiTopPlotID, hiBotPlotID, hiColor)
459
+ fill(loTopPlotID, loBotPlotID, loColor)
460
+
461
+ // ————— Background.
462
+ bgcolor(bgColor)
463
+ ``
464
+
465
+ Note that:
466
+
467
+ - We clamp the transparency of the background to a 100-75 range so that it doesn’t overwhelm. We also use a neutral color that will not distract too much. The darker the background is, the higher our measure of volatility.
468
+ - We also clamp the transparency values for the band fills between 60 and 90. We use 90 so that when a new high/low is found and the gradient resets, the starting transparency makes the color somewhat visible. We do not use a transparency lower than 60 because we don’t want those bands to hide the signal line.
469
+ - We use the very handy [ta.percentrank()](https://www.tradingview.com/pine-script-reference/v6/#fun_ta.percentrank) function to generate a 0-100 value from our ATR ratio measuring volatility. It is useful to convert values whose scale is unknown into known values that can be used to produce transparencies.
470
+ - Because we must clamp values three times in our script, we wrote an `f_clamp()` function, instead of explicitly coding the logic three times.
471
+
472
+ ## [Tips](https://www.tradingview.com/pine-script-docs/visuals/colors/#tips)
473
+
474
+ ### [Maintaining automatic color selectors](https://www.tradingview.com/pine-script-docs/visuals/colors/#maintaining-automatic-color-selectors)
475
+
476
+ Under certain conditions, Pine Script can automatically display all of the colors used in a script’s plots in the “Settings/Style” menu. These plots are graphics created by all `plot*()` functions, [barcolor()](https://www.tradingview.com/pine-script-reference/v6/#fun_barcolor), and [bgcolor()](https://www.tradingview.com/pine-script-reference/v6/#fun_bgcolor). The user can change the colors using a color picker. This feature allows colors in scripts to be customized without any extra code.
477
+
478
+ For example, this simple script has a [plot()](https://www.tradingview.com/pine-script-reference/v6/#fun_plot) that is colored either teal or red, depending on the relationship between the bar’s [close](https://www.tradingview.com/pine-script-reference/v6/#var_close) and [open](https://www.tradingview.com/pine-script-reference/v6/#var_open). The script does not specify that these colors should be editable, nor does it create any color-related inputs. Nevertheless, Pine Script automatically displays the colors in the “Settings/Style” menu and allows the user to change them, along with the style of the plot:
479
+
480
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-MaintainAutomaticColorSelection-1.BHISUteP_LNVpG.webp)
481
+
482
+ [Pine Script®](https://tradingview.com/pine-script-docs)
483
+
484
+ Copied
485
+
486
+ `//@version=6
487
+ indicator("Color picker showcase")
488
+ plotColor = close > open ? color.teal : color.red
489
+ plot(close, color = plotColor)
490
+ `
491
+
492
+ TipTo prevent the user from changing the color or the type of a plot from a script’s “Settings/Style” tab, include `editable = false` in the [plot()](https://www.tradingview.com/pine-script-reference/v6/#fun_plot) call.
493
+
494
+ The colors in the above script can be automatically displayed in this way because they are _not dynamically calculated_ and are known as soon as the script has finished compiling. All colors of the [“const”](https://www.tradingview.com/pine-script-docs/language/type-system/#const) type, and all colors of type [“input”](https://www.tradingview.com/pine-script-docs/language/type-system/#input) that are _not modified_ via the [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) or [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb) functions can be automatically displayed like this.
495
+
496
+ NoteIf [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) and [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb) functions use a “const color” and other “const” parameters, color modifications are calculated during the script’s compilation, not at its runtime. As a result, they are available when the script finishes compiling and can be displayed in the “Settings/Style” tab.
497
+
498
+ However, if _even a single calculated color_ is of type “simple color” or “series color”, or if an “input color” is passed to [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) or [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb), _all_ colors are calculated in the script’s runtime, and no color pickers are available in the “Style” section.
499
+
500
+ In practice, the creation of [“simple”](https://www.tradingview.com/pine-script-docs/language/type-system/#simple) or [“series”](https://www.tradingview.com/pine-script-docs/language/type-system/#series) colors is also most often due to using [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) and [color.rgb()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.rgb) functions. The qualifier of the color that these functions return is the strongest qualifier of the values passed to them. If each call to these functions passes only “const” values, the resulting colors are also “const”, and the script _does_ display them in the “Style” menu.
501
+
502
+ NoticeThe [color.from_gradient()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.from_gradient) function always returns a “series color” value, regardless of the parameters passed to it. If it’s used in a script, all of the script’s colors are calculated at runtime.
503
+
504
+ For example, let’s try to make the plots in the script above semi-transparent by adding a transparency of `50` to its colors via [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new). The easiest way to do this is to wrap the `plotColor` variable with [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new), like in the example below:
505
+
506
+ [Pine Script®](https://tradingview.com/pine-script-docs)
507
+
508
+ Copied
509
+
510
+ `//@version=6
511
+ indicator("Color picker showcase")
512
+ plotColor = color.new(close > open ? color.teal : color.red, 50)
513
+ plot(close, color = plotColor)
514
+ `
515
+
516
+ Unfortunately, with these changes the “Style” tab does not display a color picker any longer. This is because we use the “series bool” condition `close > open` to decide the color, and then pass the result of this expression to a single [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) call. The qualified type of the calculated color that it returns is “series color”.
517
+
518
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-MaintainAutomaticColorSelection-2.BO3w97fq_14utEv.webp)
519
+
520
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-MaintainAutomaticColorSelection-3.B0TUMw_p_Z2hnihX.webp)
521
+
522
+ To avoid this, we can ensure that every calculated color created by [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) is a “const color”. Below, we wrap [color.teal](https://www.tradingview.com/pine-script-reference/v6/#const_color.teal) and [color.red](https://www.tradingview.com/pine-script-reference/v6/#const_color.red) separately with [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) — creating two constant calculated colors in the process — and then decide which one to assign to `plotColor` based on the condition. And while the `plotColor` variable is a “series color”, each [color.new()](https://www.tradingview.com/pine-script-reference/v6/#fun_color.new) call returns a constant color, so the script displays a color picker in the “Style” tab:
523
+
524
+ [Pine Script®](https://tradingview.com/pine-script-docs)
525
+
526
+ Copied
527
+
528
+ `//@version=6
529
+ indicator("Color picker showcase")
530
+ plotColor = close > open ? color.new(color.teal, 50) : color.new(color.red, 50)
531
+ plot(close, color = plotColor)
532
+ `
533
+
534
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-MaintainAutomaticColorSelection-4.B7QSrDzd_18UfnH.webp)
535
+
536
+ To calculate the colors at runtime, create custom color inputs for all of the colors that are to be editable. This approach requires more effort, but allows significantly more control over what the user can affect. Learn more about creating color inputs on the [Inputs page](https://www.tradingview.com/pine-script-docs/concepts/inputs/#color-input).
537
+
538
+ ### [Designing usable colors schemes](https://www.tradingview.com/pine-script-docs/visuals/colors/#designing-usable-colors-schemes)
539
+
540
+ If you write scripts intended for other traders, try to avoid colors that will not work well in some environments, whether it be for plots, labels, tables or fills. At a minimum, test your visuals to ensure they perform satisfactorily with both the light and dark TradingView themes; they are the most commonly used. Colors such as black and white, for example, should be avoided.
541
+
542
+ Build the appropriate inputs to provide script users the flexibility to adapt your script’s visuals to their particular environments.
543
+
544
+ Take care to build a visual hierarchy of the colors you use that matches the relative importance of your script’s visual components. Good designers understand how to achieve the optimal balance of color and weight so the eye is naturally drawn to the most important elements of the design. When you make everything stand out, nothing does. Make room for some elements to stand out by toning down the visuals surrounding it.
545
+
546
+ Providing a selection of color presets in your inputs — rather than a single color that can be changed — can help color-challenged users. Our [Technical Ratings](https://www.tradingview.com/script/Jdw7wW2g-Technical-Ratings/) demonstrates one way of achieving this.
547
+
548
+ ### [Plot crisp lines](https://www.tradingview.com/pine-script-docs/visuals/colors/#plot-crisp-lines)
549
+
550
+ It is best to use zero transparency to plot the important lines in your visuals, to keep them crisp. This way, they will show through fills more precisely. Keep in mind that fills have a higher z-index than plots, so they are placed on top of them. A slight increase of a line’s width can also go a long way in making it stand out.
551
+
552
+ If you want a special plot to stand out, you can also give it more importance by using multiple plots for the same line. These are examples where we modulate the successive width and transparency of plots to achieve this:
553
+
554
+ ![image](https://www.tradingview.com/pine-script-docs/_astro/Colors-PlotCrispLines-1.CJkrlPd__Z1Me4S0.webp)
555
+
556
+ [Pine Script®](https://tradingview.com/pine-script-docs)
557
+
558
+ Copied
559
+
560
+ `//@version=6
561
+ indicator("")
562
+ plot(high, "", color.new(color.orange, 80), 8)
563
+ plot(high, "", color.new(color.orange, 60), 4)
564
+ plot(high, "", color.new(color.orange, 00), 1)
565
+
566
+ plot(hl2, "", color.new(color.orange, 60), 4)
567
+ plot(hl2, "", color.new(color.orange, 00), 1)
568
+
569
+ plot(low, "", color.new(color.orange, 0), 1)
570
+ `
571
+
572
+ ### [Customize gradients](https://www.tradingview.com/pine-script-docs/visuals/colors/#customize-gradients)
573
+
574
+ When building gradients, adapt them to the visuals they apply to. If you are using a gradient to color candles, for example, it is usually best to limit the number of steps in the gradient to ten or less, as it is more difficult for the eye to perceive intensity variations of discrete objects. As we did in our examples, cap minimum and maximum transparency levels so your visual elements remain visible and do not overwhelm when it’s not necessary.