Reputation: 11
I am trying to create an indicator to plot swing points on the chart.
Basically after 3 bullish bars and 3 bearish bars I want the indicator to look back to the last 6 bars and plot a point figure on the highest bar to illustrate the Swing High and that after 3 bearish bars and 3 bullish bars the indicator must look back to the last 6 bars and plot a point figure on the lowest bar to illustrate the Swing low.
I use the heikin-ashi candles.
this is what I am doing:
SH = open[5] < close[5] and open[4] < close[4] and open[3] < close[3] and open[2] > close[2] and open[1] > close[1] and open[0] > close[0]
SL = open[5] > close[5] and open[4] > close[4] and open[3] > close[3] and open[2] < close[2] and open[1] < close[1] and open[0] < close[0]
plotchar(SH, title='Swing High', char='•', location=location.abovebar, color=color.blue, offset = highestbars(6))
plotchar(SL, title='Swing Low', char='•', location=location.belowbar, color=color.red, offset = lowestbars(6))
For some reason sometimes it does not plot the • on the correct bar. As it shows on the chart below the red or blue point should be where the arrow is pointing. Thanks in advance
Upvotes: 1
Views: 342
Reputation: 21342
plotchar()
is no good for your application because your offset will be dynamic.
Use label
instead.
//@version=4
study("My script", overlay=true, max_labels_count = 500)
SH = open[5] < close[5] and open[4] < close[4] and open[3] < close[3] and open[2] > close[2] and open[1] > close[1] and open[0] > close[0]
SL = open[5] > close[5] and open[4] > close[4] and open[3] > close[3] and open[2] < close[2] and open[1] < close[1] and open[0] < close[0]
SH_offset = -highestbars(6)
SL_offset = -lowestbars(6)
if (SH)
label.new(bar_index - SH_offset, high, "SH", yloc=yloc.abovebar, color=color.blue, style=label.style_none, textcolor=color.blue)
if (SL)
label.new(bar_index - SL_offset, low, "SL", yloc=yloc.belowbar, color=color.red, style=label.style_none, textcolor=color.red)
Upvotes: 1