I was coding this YouTube strategy. I did most of them but could cot figure out how to code "stop to breakeven" so the strategy has only one entry and one exit. can any one help me? I want to take 50% position out at first TP (1 x atr) and put breakeven stop at the entry and exit the remaining position when ssl channel crosses oposite direciton This is the strategy video
//@version=4
strategy("for jcolina1 v2",overlay=true)
//Inputs------------------------------------------------------------------------
startDate = input(timestamp("2020-07-30T00:00:00"), type = input.time, group="Back-testing range")
finishDate = input(timestamp("2022-09-09T00:00:00"), type = input.time, group="Back-testing range")
profit_multiplier= input(1,title="profit multiplier", type=input.float)
stop_multiplier = input(1.5, title="stop loss multiplier", type=input.float)
level_offset = input(5, type=input.integer, title="Length", group="levels")
bearish_levels = input(color.new(color.red,0), type=input.color,title="Bearish", inline ="0",group="levels" )
bearish_L_text = input(color.new(color.purple,0), type=input.color,title="text", inline="0", group="levels")
Bullish_levels = input(color.new(color.green,0), type=input.color,title="Bullish", inline ="00",group="levels" )
Bullish_L_text = input(color.new(color.yellow,0), type=input.color,title="text", inline="00", group="levels")
ssl_len = input(title="ssl length", defval=10,inline="1",group="Base indicators")
en_ssl = input(true,title=" ", type=input.bool,inline="1",group="Base indicators")
Up = input(#66ff00, type=input.color,title="Up", inline="1",group="Base indicators")
Dn = input(#ff0000, type=input.color, title="Dn", inline="1",group="Base indicators")
kiju_len = input(26, minval=1, title="Kijun-Sen",type=input.integer,inline="2",group="Base indicators")
en_ki = input(true,title=" ", type=input.bool,inline="2",group="Base indicators")
col_kijun = input(color.white, type=input.color, title=" ", inline="2", group="Base indicators")
Sensitivity = input(150, title="Sensitivity", type=input.integer,group="Waddah attar explosion")
Fast_Length = input(20 , title="Fast Length", type=input.integer,group="Waddah attar explosion")
Slow_Length = input(40 , title="Slow Length", type=input.integer,group="Waddah attar explosion")
Channel_Length = input(20 , title="Channel Length",type=input.integer,group="Waddah attar explosion")
Multi = input(2.0, title="BB Multi",type=input.float,group="Waddah attar explosion")
//Computing required calculations-----------------------------------------------
smaHigh = sma(high, ssl_len)
smaLow = sma(low, ssl_len)
Hlv = int(na)
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh : smaLow //output
sslUp = Hlv < 0 ? smaLow : smaHigh // output
donchian(d_len) =>
avg(lowest(d_len), highest(d_len))
baseLine = donchian(kiju_len)
Calc_Macd(Source, Fast_Length, Slow_Length) =>
Fast_Ma = ema(Source, Fast_Length)
Slow_Ma = ema(Source, Slow_Length)
Fast_Ma - Slow_Ma
Calc_Upper(Source, length, Multi) =>
Basis = sma(Source, length)
Dev = Multi * stdev(Source, length)
Basis + Dev
Calc_Lower(Source, length, Multi) =>
Basis = sma(Source, length)
Dev = Multi * stdev(Source, length)
Basis - Dev
t1 = (Calc_Macd(close, Fast_Length, Slow_Length) - Calc_Macd(close[1], Fast_Length, Slow_Length)) * Sensitivity
e1 = Calc_Upper(close, Channel_Length, Multi) - Calc_Lower(close, Channel_Length, Multi)
trendUp = t1 >= 0 ? t1 : 0
trendDown = t1 < 0 ? -1 * t1 : 0
DEAD_ZONE = nz(rma(tr(true),100)) * 3.7
atr = atr(14)
time_cond = time >= startDate and time <= finishDate
// entry logic------------------------------------------------------------------
ssl_bul = crossover(sslUp,sslDown) , ssl_bear = crossunder(sslUp,sslDown)
kij_bul = (close > baseLine) , kij_bear = (close < baseLine)
wae_filter = ((trendUp > e1) or (trendDown > e1))
bullish = ((ssl_bul and kij_bul) and wae_filter)
bearish = ((ssl_bear and kij_bear) and wae_filter)
// computing required dynamic data for short
var float S_stop = na
var float S_entry = na
var float S_target = na
if ((bearish and barstate.isconfirmed) and time_cond)
S_entry := close
S_stop := (close + (atr * stop_multiplier))
S_target := (close - (atr * profit_multiplier ))
strategy.entry("Short", strategy.short)
strategy.exit("S exit", "Short",limit = S_target, stop = S_stop)
tt = (time - time[1]) * level_offset
//stop
line.new(x1=time, y1=S_stop,
x2=time + tt , y2=S_stop , color=bearish_levels,
xloc=xloc.bar_time,width=2)
label.new(x=time +(time - time[1]), y=S_stop, text=tostring(S_stop)+" short SL",color=color.new(color.black,100), style=label.style_label_lower_left,textcolor=bearish_L_text,xloc=xloc.bar_time,textalign= text.align_left)
//target
line.new(x1=time, y1=S_target,
x2=time + tt , y2=S_target , color=bearish_levels,
xloc=xloc.bar_time,width=2)
label.new(x=time +(time - time[1]) , y=S_target, text=tostring(S_target)+" short TP",color=color.new(color.black,100), style=label.style_label_upper_left,textcolor=bearish_L_text,xloc=xloc.bar_time,textalign= text.align_left)
// computing required dynamic data for long
var float L_stop = na
var float L_entry = na
var float L_target = na
if ((bullish and barstate.isconfirmed) and time_cond)
L_entry := close
L_stop := (close - (atr * stop_multiplier))
L_target := (close + (atr * profit_multiplier ))
strategy.entry("Long", strategy.long)
strategy.exit("L exit", "Long",limit = L_target, stop = L_stop)
tt = (time - time[1]) * level_offset
//stop
line.new(x1=time, y1=L_stop,
x2=time + tt , y2=L_stop , color=Bullish_levels,
xloc=xloc.bar_time,width=2)
label.new(x=time +(time - time[1]), y=L_stop, text=tostring(L_stop)+" Long SL",color=color.new(color.black,100), style=label.style_label_upper_left,textcolor=Bullish_L_text,xloc=xloc.bar_time,textalign= text.align_left)
//target
line.new(x1=time, y1=L_target,
x2=time + tt , y2=L_target , color=Bullish_levels,
xloc=xloc.bar_time,width=2)
label.new(x=time +(time - time[1]) , y=L_target, text=tostring(L_target)+" Lomg TP",color=color.new(color.black,100), style=label.style_label_lower_left,textcolor=Bullish_L_text,xloc=xloc.bar_time,textalign= text.align_left)
// signals -------------------------------------------------
if (bullish and time_cond)
lbl = label.new(bar_index, low,text = "Buy" ,textcolor = color.white)
label.set_color(lbl,color.green)
label.set_yloc(lbl,yloc.belowbar)
label.set_style(lbl,label.style_label_up)
if (bearish and time_cond)
lbl = label.new(bar_index, low,text ="Sell",textcolor = color.white)
label.set_color(lbl,color.red)
label.set_yloc(lbl,yloc.abovebar)
label.set_style(lbl,label.style_label_down)
//Ploting the data------------------------------------------------------------------------
plot(en_ssl ? sslDown : na, linewidth=2, color=Up)
plot(en_ssl ? sslUp : na, linewidth=2, color=Dn)
plot(en_ki ? baseLine : na, color=col_kijun, title="Kijun-Sen")