Below is the pinescript.
import TradingView/ta/5
source = hl2
length = 50
show_SR = input.bool(title='Draw Support, Mean and Resistance Zones', defval=true)
draw_targets = input.bool(title='Draw Target Labels', defval=false)
draw_sl = input.bool(title='Draw Stoploss Labels', defval=false)
sl_pct = input.float(title='Stoploss Percentage', defval=15.0)
sl_pct_formatted = 1+(sl_pct / 100)
innermult = 2
outermult = 3
var pi = 2 * math.asin(1)
var mult = pi * innermult
var mult2 = pi * outermult
var gradsize = 0.5
SAK_smoothing(_type, _src, _length) =>
c0 = 1.0
c1 = 0.0
b0 = 1.0
b1 = 0.0
b2 = 0.0
a1 = 0.0
a2 = 0.0
alpha = 0.0
beta = 0.0
gamma = 0.0
cycle = 2 * pi / _length
alpha := 1 / _length
b0 := alpha
a1 := 1 - alpha
_Input = _src
_Output = 0.0
_Output := c0 * (b0 * _Input + b1 * nz(_Input[1]) + b2 * nz(_Input[2])) + a1 * nz(_Output[1]) + a2 * nz(_Output[2]) - c1 * nz(_Input[_length])
_Output
supersmoother(_src, _length) =>
s_a1 = math.exp(-math.sqrt(2) * pi / _length)
s_b1 = 2 * s_a1 * math.cos(math.sqrt(2) * pi / _length)
s_c3 = -math.pow(s_a1, 2)
s_c2 = s_b1
s_c1 = 1 - s_c2 - s_c3
ss = 0.0
ss := s_c1 * _src + s_c2 * nz(ss[1], _src[1]) + s_c3 * nz(ss[2], _src[2])
ss
//-----------------------
// Mean Reversion Channel Function
//-----------------------
get_mrc() =>
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(ta.tr, length)
v_meanline := SAK_smoothing('RMA', source, length)
v_upband1 = v_meanline + v_meanrange * mult
v_loband1 = v_meanline - v_meanrange * mult
v_upband2 = v_meanline + v_meanrange * mult2
v_loband2 = v_meanline - v_meanrange * mult2
//-- Check Condition
if close > v_meanline
v_upband2_1 = v_upband2 + v_meanrange * gradsize * 4
v_upband2_9 = v_upband2 + v_meanrange * gradsize * -4
if high >= v_upband2_9 and high < v_upband2
v_condition := 1
v_condition
else if high >= v_upband2 and high < v_upband2_1
v_condition := 2
v_condition
else if high >= v_upband2_1
v_condition := 3
v_condition
else if close <= v_meanline + v_meanrange
v_condition := 4
v_condition
else
v_condition := 5
v_condition
if close < v_meanline
v_loband2_1 = v_loband2 - v_meanrange * gradsize * 4
v_loband2_9 = v_loband2 - v_meanrange * gradsize * -4
if low <= v_loband2_9 and low > v_loband2
v_condition := -1
v_condition
else if low <= v_loband2 and low > v_loband2_1
v_condition := -2
v_condition
else if low <= v_loband2_1
v_condition := -3
v_condition
else if close >= v_meanline + v_meanrange
v_condition := -4
v_condition
else
v_condition := -5
v_condition
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
//************************************************************************************************************
// Trade Signals
//************************************************************************************************************
candle_color = close>open?#089981:#f23645
if close > upband2
candle_color := color.blue
barcolor(candle_color)
var state = 'idle'
var sl_price = 0.0
// SHORT ENTRY
if close[1] > upband1[1] and close < upband1 and close < open//(close - open != 0.0)// Short Signal
label.new(bar_index,high,'Short',color=color.purple, textcolor = color.white,yloc = yloc.abovebar, style = label.style_label_down, size=size.small)
state := 'Position'
sl_price := close * sl_pct_formatted
if close > sl_price and state != 'idle'
if draw_sl
if state == 'Position'
label.new(bar_index,high,'Hard SL',color=color.rgb(177, 130, 0), textcolor = color.white,yloc = yloc.abovebar, style = label.style_label_down, size=size.small)
if state == 'Target 1'
label.new(bar_index,high,'Soft SL',color=color.rgb(245, 230, 20), textcolor = color.black,yloc = yloc.abovebar, style = label.style_label_down, size=size.small)
state := 'idle'
// SHORT TARGETS
if draw_targets
if state == 'Position' and (close <= meanline) //and close < position_price
label.new(bar_index,high,'T1',color=color.rgb(16, 156, 199), textcolor = color.white,yloc = yloc.abovebar, style = label.style_label_down, size=size.small)
state := 'Target 1'
if state == 'Target 1' and (close <= loband1) //and close < position_price
label.new(bar_index,high,'T2',color=color.rgb(16, 156, 199), textcolor = color.white,yloc = yloc.abovebar, style = label.style_label_down, size=size.small)
state := 'idle'
//************************************************************************************************************
// Plots
//************************************************************************************************************
box resistance_box = na
box median_box = na
box support_box = na
if barstate.islast and show_SR
res_top_left = chart.point.new(time[10], bar_index[10], upband2)
res_bottom_right = chart.point.new(time+30, bar_index+30, upband1)
resistance_box := box.new(res_top_left, res_bottom_right, border_width = 1, bgcolor = color.rgb(255, 9, 9, 86), border_color = color.rgb(255, 58, 58, 42), extend = extend.right)
// Median
full_range = math.abs(upband1 - loband1)
median_spread = full_range / 15
med_top_left = chart.point.new(time[10], bar_index[10], meanline+median_spread)
med_bottom_right = chart.point.new(time+30, bar_index+30, meanline-median_spread)
median_box := box.new(med_top_left, med_bottom_right, border_width = 1, bgcolor = color.rgb(9, 95, 255, 86), border_color = color.rgb(58, 110, 255, 42), extend = extend.right)
sup_top_left = chart.point.new(time[10], bar_index[10], loband2)
sup_bottom_right = chart.point.new(time+30, bar_index+30, loband1)
support_box := box.new(sup_top_left, sup_bottom_right, border_width = 1, bgcolor = color.rgb(9, 255, 29, 86), border_color = color.rgb(15, 214, 41, 42), extend = extend.right)
box.delete(resistance_box[1])
box.delete(median_box[1])
box.delete(support_box[1])