//@version=4 study("manue1134 - With Label Adjustment and Text","manue1134",overlay=true) // Main parameters length = input(100, "Length") mult = input(10, "ATR Multiplier") src = input(close, "Source") label_offset_mult = input(0.1, "Label Distance Adjustment", minval=0.01, maxval=1, step=0.01) show_text = input(true, "Show Simple Text") //---- Main calculation ---- ts = 0., os = 0 diff = src - nz(ts[1],src[1]) atr = atr(length) os := rising(src,length) ? 1 : falling(src,length) ? -1 : os[1] rise = change(os) == 2 and diff[1] < 0 fall = change(os) == -2 and diff[1] > 0 up = crossover(diff,0) dn = crossunder(diff,0) val = valuewhen(up or dn or rise or fall,atr/length,0) bars = barssince(up or dn or rise or fall) ts := up ? nz(ts[1],src) - atr*mult : dn ? nz(ts[1],src) + atr*mult : rise ? src - atr*mult : fall ? src + atr*mult : nz(ts[1],src) + sign(diff)*val*bars //-------------------------------------- // Label position calculation label_pos = up ? ts + (atr * mult * label_offset_mult) : dn ? ts - (atr * mult * label_offset_mult) : rise ? ts + (atr * mult * label_offset_mult) : fall ? ts - (atr * mult * label_offset_mult) : na css = diff > 0 ? #2196f3 : #ff1100 plotshape(iff(crossover(diff,0), label_pos, na), "Buy Label", shape.labelup, location.absolute, #2196f3, 0, text="Buy", textcolor=color.white, size=size.tiny) plotshape(iff(crossunder(diff,0), label_pos, na), "Sell Label", shape.labeldown, location.absolute, #ff1100, 0, text="Sell", textcolor=color.white, size=size.tiny) // Simple text displayed in upper right corner if show_text var label txtLabel = na txtLabel := label.new( bar_index, na, "manue1134 \nCurrent ATR: " + tostring(atr, format.mintick) + "\nTrend: " + (diff > 0 ? "Up" : "Down"), color=color.new(#bd57b8, 18), style=label.style_label_right, textcolor=color.white, size=size.normal, yloc=yloc.price) // Position the label in top right corner label.set_xy( txtLabel, bar_index + 10, highest(high, 20)[1] * 1.01) label.delete(txtLabel[1])