Search code examples
plotscriptingpine-scripttradingarrows

Plotted arrows aren't displayed below or above bar graph in Pine


I have the next code.

//@version=4
study(title="MACD", shorttitle="MACD")

// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=11)
slow_length = input(title="Slow Length", type=input.integer, defval=22)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)

// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00

// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal

plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )
plot(macd, title="MACD", color=col_macd, transp=0)
plot(signal, title="Signal", color=col_signal, transp=0)

currMacd = hist[0]
prevMacd = hist[1]

buy = currMacd > 0 and prevMacd <= 0
sell = currMacd < 0 and prevMacd >= 0

timetobuy = buy==1
timetosell = sell==1

tup = barssince(timetobuy[1])
tdn = barssince(timetosell[1])

long    = tup>tdn?1:na
short   = tdn>tup?1:na


plotshape(long==1?buy:na, color=#26A69A, location=location.abovebar, style=shape.arrowup, title="Buy Arrow", transp=0)
plotshape(short==1?sell:na, color=#EF5350, location=location.belowbar, style=shape.arrowdown, title="Sell Arrow", transp=0)

As you can see, it is the macd histogram code with some plot code to graph arrows near the histogram. I configured their location as above and below. However, they are displayed far from the graph.

macd histogram

How can I plot the arrows above and below the macdh bars?


Solution

  • By using location=location.abovebar the function is putting the level of the symbol's price in play, which ruins your indicator's scale. The solution is to use location=location.top instead:

    plotshape(long==1?buy:na, color=#26A69A, location=location.top, style=shape.arrowup, title="Buy Arrow", transp=0)
    plotshape(short==1?sell:na, color=#EF5350, location=location.bottom, style=shape.arrowdown, title="Sell Arrow", transp=0)
    

    enter image description here