unable to use plot command from within while loop

Viewed 26

I am trying to adapt an existing study to suit intraday trading, where in the buy/sell signals has to generate after session open and exit at Session close. I have defined session

sess = input.session('0915-1525', title='Regular Session Time')
ta = time(timeframe.period, sess + ':1234567')
in_session = not na(ta)

i am using a while loop determining the session is live or not and to execute the codes.

while in_session
    var int a
    plot (a)
    ........
    break()

however when i am making plot, fill, plotshape commands the below error is getting generated

line 84: Cannot use 'plot' in local scope line 89: Cannot use 'plot' in local scope line 90: Cannot use 'fill' in local scope line 91: Cannot use 'fill' in local scope line 93: Cannot use 'plotshape' in local scope line 94: Cannot use 'plotshape' in local scope

please advice how to overcome this?

1 Answers

I see zero reasons why you need a while loop.

That being said, as the error message tells you, you cannot use plot functions in a local scope.

If you really need to plot something in a local scope, you should use line.new() instead.

Alternatively, you can apply your condition to the series argument of your plot() function. This way, you can use the plot functions in the global scope.

Something like this:

plot_cond = ...

plot(in_session and plot_cond ? plot_value : na)

This would only plot something if it is in the session and your plot condition is true. Otherwise, it won't plot anything due to na.

Related