Color input for PineScript (TradingView)

Color input in TradinView PineScript

Unfortunately, as many other things, also color inputs are not yet available in TradingView PineScript (v4) indicators or strategies. 

So, if you want to have some kind of input for color to use as a variable in indicator or strategy, then the only nearest approach seems to have dropdown of colors, like:

Code:


chosenColor(c_)=> c_ == "aqua" ? color.aqua : c_ == "black" ? color.black : c_ == "blue" ? color.blue : c_ == "fuchsia" ? color.fuchsia : c_ == "gray" ? color.gray : c_ == "green" ? color.green : c_ == "lime" ? color.lime : c_ == "maroon" ? color.maroon : c_ == "navy" ? color.navy : c_ == "olive" ? color.olive : c_ == "orange" ? color.orange : c_ == "purple" ? color.purple : c_ == "red" ? color.red : c_ == "silver" ? color.silver : c_ == "teal" ? color.teal : c_ == "white" ? color.white : c_ == "yellow" ? color.yellow : color.black

my_color_1 = input(title="Color", defval="green", options=["aqua", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "orange", "purple", "red", "silver", "teal", "white", "yellow"])
my_color_2 = input(title="Color", defval="red", options=["aqua", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "orange", "purple", "red", "silver", "teal", "white", "yellow"])

plot(low, color=chosenColor(my_color_1) )
plot(high, color=chosenColor(my_color_2) )

P.S. If you want to have Transparent/Opacity inputs for color, view article how to create color inputs with opacity for drawing elements.

Leave a Reply

Your email address will not be published. Required fields are marked *