PlotVarColor CC - plot() with a variable color input (like on TradingView)
stableDescription
When converting from PineScript to HaasScript, it's pretty common to find colors changing on some condition.
This function imitates TV's variable color plotting (including the weird color extension backward when a new colored datapoint comes in).
Also has built-in PlotCircle and PlotBars capability as an optional parameter.
PlotHistogram may require some tinkering.
Accepts LineOptions() with a variable color inside.
Best when used inside an OptimizedForInterval function, which will significantly speed up the calculation time.
HaasScript
DefineCommand("PlotVarColor", "Used like a regular plot command but you have a variable color")
local palette, varmodif, varcolor, varLO, is_LO, clr, LineOptsID, LineName, x, a, b, t_modif, chartid, name, values, color_or_LO
chartid = DefineParameter(NumberType, "Chart ID", "", true, 1)
name = DefineParameter(StringType, "Line Name", "", true, "Line")
values = DefineParameter(ListDynamicType, "Values", "", true, {100, 100})
color_or_LO = DefineParameter(ListDynamicType, "Color or LineOptions object", "", true, Blue)
varmodif = DefineParameter(StringType, "Modifier", "Specify to add a modifier to the plot (Circles, Histogram, Bars)", false, "None")
OptimizedForInterval(999999990, function()
palette = {}
palette= {
["rgba(0, 255, 255, 1.00)"] = " Aqua",
["rgba(0, 0, 0, 1.00)"] = " Black",
["rgba(0, 0, 204, 1.00)"] = " Blue",
["rgba(0, 204, 204, 1.00)"] = " Cyan",
["rgba(128, 128, 128, 1.00)"] = " DarkGray",
["rgba(0, 128, 0, 1.00)"] = " DarkGreen",
["rgba(255, 0, 255, 1.00)"] = " Fuchsia",
["rgba(204, 153, 0, 1.00)"] = " Gold",
["rgba(192, 192, 192, 1.00)"] = " Gray",
["rgba(0, 255, 0, 1.00)"] = " Green",
["rgba(128, 0, 0, 1.00)"] = " Maroon",
["rgba(128, 128, 0, 1.00)"] = " Olive",
["rgba(255, 128, 0, 1.00)"] = " Orange",
["rgba(255, 26, 140, 1.00)"] = " Purple",
["rgba(255, 0, 0, 1.00)"] = " Red",
["rgba(51, 204, 255, 1.00)"] = " SkyBlue",
["rgba(0, 128, 128, 1.00)"] = " Teal",
["rgba(255, 255, 255, 1.00)"] = " White",
["rgba(255, 255, 0, 1.00)"] = " Yellow"
}
if varmodif ~= "None" then
t_modif = {}
t_modif.Circles = PlotCircle
-- t_modif.Histogram = PlotHistogram
t_modif.Bars = PlotBars
t_modif.None = ArrayContains
else
t_modif = {}
t_modif.Circles = ArrayContains
t_modif.Histogram = ArrayContains
t_modif.Bars = ArrayContains
t_modif.None = ArrayContains
end
end)
OptimizedForInterval(CurrentInterval(), function()
if (ArrayContains(color_or_LO, true)) or (ArrayContains(color_or_LO, false)) then
varcolor = color_or_LO["color"]
varLO = color_or_LO
is_LO = true
else
varcolor = color_or_LO
end
clr = Load("clr", varcolor)
LineOptsID = Load("LOID", "zzz")
LineName = Load("linename", name)
x = Load("x", 0)
if clr ~= varcolor then
x = x + 1
clr = varcolor
LineName = name..palette[Parse(varcolor, StringType)]
LineOptsID = x..LineName
end
if is_LO then
color_or_LO.color = clr
color_or_LO.id = LineOptsID
a = Plot(chartid, LineName, values, color_or_LO)
color_or_LO.offset = -1
b = Plot(chartid, LineName, values[2], color_or_LO)
else
a = Plot(chartid, LineName, values, LineOptions({color = clr, id = LineOptsID}) )
b = Plot(chartid, LineName, values[2], LineOptions({color = clr, id = LineOptsID, offset = -1}))
end
t_modif[varmodif](a)
Save("clr", clr)
Save("x", x)
Save("linename", LineName)
Save("LOID", LineOptsID)
end)
DefineOutput(VoidType)
1 Comment
Sign in to leave a comment.
Cool !