0

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")
Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42

1 Answers1

0

According to your description of the strategy you have to have two exit orders for each entry. The first one should describe how to exit the first 50% and the second one should describe the other half. The difference is that the other half should have only the stop loss, while the first one should have both take profit and stop loss. Try to replace lines 68-69 with

strategy.entry("Short", strategy.short)
strategy.exit("S exit1", "Short", from_entry = "Short", qty_percent = 50, limit = S_target, stop = S_stop)
strategy.exit("S exit2", "Short", from_entry = "Short", stop = S_stop)

and lines 91-92 with

strategy.entry("Long", strategy.long)
strategy.exit("L exit1", "Long", from_entry = "Long", qty_percent = 50, limit = L_target, stop = L_stop)
strategy.exit("L exit2", "Long", from_entry = "Long", stop = L_stop)

Notice that qty_percent = 50 will make exit order "S exit1" to exit only half of the initial "Short" order!

Also it would be good to set pyramiding = 0 on your strategy for debug purposes so you have only one active order to deal with.

To set your stop loss for exit2 orders to breakeven you need to know if the take profit order was activated. You can achieve that by monitoring the strategy.position_size

e.g something like

var longTakeProfitExecuted = false
...

longTakeProfitExecuted := strategy.position_size > 0 and (longTakeProfitExecuted[1] or strategy.position_size < strategy.position_size[1] or (strategy.position_size[1] == 0 and high >= L_target))

Then you can use this info to set the correct price for stop Loss e.g.

strategy.exit("L exit2", "Long", from_entry = "Long", stop = longTakeProfitExecuted ? Breakeven_stop : L_stop)
Jason
  • 97
  • 7