Search code examples
pine-scriptpine-script-v5

how to draw a Horizontal line according to formula calculation result


if is it possible to have any small example for what I'm going to ask it's simple but since I'm new to this code language I could not figure it out yet

i have a calculation formula on excel sheet i want to apply it as simple indicator just a Horizontal line to be appeared according to the result of the formula calculation the formula is " Close * 0.31 / 10 + Close " i want to draw line according to the result

i tried to some ways but the line will not be same position as the Formula

thank you in advance


Solution

  • Built-in hline() function does not accept series argument, to dynamically plot a horizontal line you should use the line.new() function with the equal y1 and y2 argument values, as is shown below:

    //@version=5
    indicator("hline example", overlay = true)
    float y = close * 0.31 / 10 + close
    if barstate.islast or barstate.isrealtime
        line.new(bar_index, y, bar_index +1, y, extend = extend.both)