1

I'd like to extract the "col" color value from this function to be used to paint plots or candle colors. But everything I try creates one error or another. I checked the Script Reference. Shouldn't there be some way to "return" a value, as is usually the case with most functions?

lset(l,x1,y1,x2,y2,col)=>
    line.set_xy1(l,x1,y1)
    line.set_xy2(l,x2,y2)
    line.set_width(l,5)
    line.set_style(l, line.style_solid)
    line.set_color(l,y2 > y1 ? #ff1100 : #39ff14)  //red : green
    temp = line.get_price(l,bar_index) // another value to extract
Paul Townsend
  • 687
  • 1
  • 6
  • 10

1 Answers1

0

The documentation is showing it like this:

line.new(x1, y1, x2, y2, xloc, extend, color, style, width) → series line

So in your code it's looking differently and also the "new" is missing. Scrolling a bit up on the linked page shows that there exist indeed methods to retrieve some properties of the line object:

Lines are managed using built-in functions in the line namespace. They include:

line.new() to create them.
line.set_*() functions to modify the properties of an line.
line.get_*() functions to read the properties of an existing line.
line.copy() to clone them.
line.delete() to delete them.
The line.all array which always contains the IDs of all
    the visible lines on the chart. The array’s size will depend on 
    the maximum line count for your script and how many of those you
    have drawn. aray.size(line.all) will return the array’s size.

The most simple usage is to instantiate a line object with the correct values directly, like shown here:

//@version=5
indicator("Price path projection", "PPP", true, max_lines_count = 100)
qtyOfLinesInput = input.int(10, minval = 1)

y2Increment = (close - open) / qtyOfLinesInput
// Starting point of the fan in y.
lineY1 = math.avg(close[1], open[1])
// Loop creating the fan of lines on each bar.
for i = 0 to qtyOfLinesInput
    // End point in y if line stopped at current bar.
    lineY2 = open + (y2Increment * i)
    // Extrapolate necessary y position to the next bar because we extend lines one bar in the future.
    lineY2 := lineY2 + (lineY2 - lineY1)
    lineColor = lineY2 > lineY1 ? color.lime : color.fuchsia
    line.new(bar_index - 1, lineY1, bar_index + 1, lineY2, color = lineColor)

Getting the line color from outside is difficult or impossible though as there never exists a method to retrieve it while for other properties those methods exist. So the most simple way is to create the same funcionality, to get the color that exists inside the line-object, outside too, or only outside.

currentLineColor = y2 > y1 ? #ff1100 : #39ff14

You could try to extend the line-object somehow like this:

line.prototype.get_color = function() {
  return this.color;
};
console.log(line.get_color())

I'm not sure if the approach with the prototype is working but it's worth it to try if you need it.

David
  • 5,882
  • 3
  • 33
  • 44