Blankman
Blankman

Reputation: 267230

Looping array and multiplying input float value and using hline to create horizontal lines

I have a simple indicator where I want to draw some lines above the current input float price. The lines will be 110%, 120% and 130% above the inputted price.

I am getting an error (see below):

//@version=5
indicator("test123", overlay=true)

aHigh = input(title="aHigh", defval=750.00)
aLow = input(title="aLow", defval=620.33)


var upswing = array.new_float(10)
array.set(upswing, 0, 1.0)
array.set(upswing, 1, 1.1)
array.set(upswing, 2, 1.2)

for _i = 1 to 10
    var abc = aHigh * array.get(upswing, (_i - 1))
    hline(abc, "abc", color.red, hline.style_solid)

I am getting the error:

Cannot call 'hline' with argument 'price'='abc'. An argument of 'series float' type was used but a 'input float' is expected

Upvotes: 0

Views: 741

Answers (1)

e2e4
e2e4

Reputation: 3828

The built-in hline() function supports only a constant/input value and should be called only from a global scope of the script.

If you would like to plot from a local scope, use the line.new() function instead. The script example below demonstrates how using the array of lines lets you plot the horizontal lines at the 110%, 120% and 130% percent of the given input value:

//@version=5
indicator("test123", overlay=true)

aHigh = input(title="aHigh", defval=750.00)

var line[] lines = array.new<line>()

if barstate.islastconfirmedhistory
    for i = 1 to 3
        perc = aHigh + aHigh * i/10.
        array.unshift(lines, line.new(bar_index, perc, bar_index +1, perc, extend = extend.both))

Upvotes: 0

Related Questions