Some help regarding Break even function please
Posted: Mon Dec 04, 2023 4:39 pm
This strategy is designed to set the trade to break even at tp1 - and close at tp2 to entry once tp1 is hit. This issue i am having is once price gets to tp1, if there is an opposing signal, the trade will close at that point and enter the new one. Can anyone advise on how to stop this please?
//@strategy_alert_message {{strategy.order.alert_message}}
//@version=5
strategy("Supertrend MACD strategy 2.2 BTC", overlay = true, process_orders_on_close = true)
//////
pc_id = input.string(title='License ID', defval='', group='MT4/5 Settings', tooltip='This is your license ID')
pc_risk = input.float(title='Risk', defval=0.1, step=0.1, minval=0, group='MT4/5 Settings', tooltip='Risk')
pc_prefix = input.string(title='MetaTrader Symbol', defval='', group='MT4/5 Settings', tooltip='This is your broker\'s MetaTrader symbol ')
var symbol = pc_prefix
closelonga = pc_id + ',closelong,' + symbol + ''
closeshorta = pc_id + ',closeshort,' + symbol + ''
Pips() =>
syminfo.mintick * (syminfo.type == "forex" ? 100 : 1)
session = input.session("0700-1700",title="Trading Session ",inline = "tx")
tx = input.string("GMT+1","",inline = "tx")
t=time(timeframe.period,session,tx)
bgcolor(not na(t) ? color.new(color.green,90) : na,title = "Trading Session" )
session2 = input.session("0700-1700",title="Trading Session2 ",inline = "tx2")
tx2 = input.string("GMT+1","",inline = "tx2")
t2=time(timeframe.period,session2,tx2)
bgcolor(not na(t2) ? color.new(color.green,90) : na,title = "Trading Session2" )
sps = strategy.position_size
grp1 = "Supertrend"
atrPeriod = input.int(11, "ATR Length", minval = 1,group = grp1)
factor = input.float(2.0, "Factor", minval = 0.01, step = 0.01,group = grp1)
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
supertrend := barstate.isfirst ? na : supertrend
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style = plot.style_linebr)
downTrend = plot(direction < 0 ? na : supertrend, "Down Trend", color = color.red, style = plot.style_linebr)
bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle",display = display.none)
fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps = false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps = false)
st_b = direction < 0//direction[1] > direction
st_s = direction > 0//direction[1] < direction
grp2 ='MACD Crossover'
fastLength = input.int(13, minval=1,group = grp2)
slowLength = input.int(26, minval=1,group = grp2)
signalLength = input.int(11, minval=1,group = grp2)
// hline(0, color=color.purple, linestyle=hline.style_dashed)
fastMA = ta.ema(close, fastLength)
slowMA = ta.ema(close, slowLength)
macd = fastMA - slowMA
signal = ta.sma(macd, signalLength)
pos = 0.00
iff_1 = signal > macd ? -1 : nz(pos[1], 0)
pos := signal < macd ? 1 : iff_1
barcolor(pos == -1 ? color.red : pos == 1 ? color.green : color.blue)
// plot(signal, color=color.new(color.red, 0), title='SIGNAL')
// plot(macd, color=color.new(color.blue, 0), title='MACD')
macd_b = macd>signal
macd_s = macd<signal
ema_on = input.bool(true,"Enable EMA",group="EMA")
ema_src = input.source(title="EMA Src", defval=close,inline="1",group="EMA")
ema_len = input.int(title="Len", defval=200,inline="1",group="EMA")
ema = ta.ema(ema_src,ema_len)
plot( ema , title='EMA', color=color.blue, linewidth=3, style=plot.style_line )
ema_b = ema_on ? close>ema :true
ema_s = ema_on ? close<ema :true
slbuff = input.float(200,"SL Buffer(Pips)")*Pips()
rr1 = input.float(1,"R:R 1")
rr2 = input.float(2,"R:R 2")
beon = input.bool(true,"Enable Breakeven")
var float l_tp1 = 0
var float l_tp2 = 0
var float l_sl = 0
var float l_ep = 0
var float s_tp1 = 0
var float s_tp2 = 0
var float s_sl = 0
var float s_ep = 0
in_session = (not(na(t))) or (not(na(t2)))
f_b = (macd_b and st_b) and (not((macd_b and st_b)[1])) and (s_sl==s_ep and sps<0?sps<0:sps==0) and ema_b and in_session
f_s = (macd_s and st_s)and (not((macd_s and st_s)[1])) and (l_sl==l_ep and sps>0?sps>0:sps==0) and ema_s and in_session
if f_b
l_sl := supertrend-slbuff
if sps<0
alert(closeshorta,alert.freq_once_per_bar_close)
l_ep := close
l_tp1 := l_ep + rr1*(l_ep-l_sl)
l_tp2 := l_ep + rr2*(l_ep-l_sl)
longa = pc_id + ',buy,' + symbol + ',risk=' + str.tostring(pc_risk, '#.##')+ ',tp=' + str.tostring(l_tp2, '#.####')+ ',sl=' + str.tostring(l_sl, '#.####')
strategy.entry("Long",strategy.long,alert_message = longa )
if sps>0 and high>=l_tp1 and beon
l_sl := l_ep+slbuff
strategy.exit("Ex Long","Long",stop = l_sl,limit = l_tp2,alert_message = closelonga )
if f_s
if sps>0
alert(closelonga,alert.freq_once_per_bar_close)
s_ep := close
s_sl := supertrend+ slbuff
s_tp1 := s_ep - rr1*(s_sl-s_ep)
s_tp2 := s_ep - rr2*(s_sl-s_ep)
shorta = pc_id + ',sell,' + symbol + ',risk=' + str.tostring(pc_risk, '#.##')+ ',tp=' + str.tostring(s_tp2, '#.####')+ ',sl=' + str.tostring(s_sl, '#.####')
strategy.entry("Short",strategy.short,alert_message = shorta )
if sps<0 and low<=s_tp1 and beon
s_sl := s_ep+slbuff
strategy.exit("Ex Short","Short",stop = s_sl,limit = s_tp2,alert_message = closeshorta )
plot(sps<0 ? s_tp2 : na , title='Short TP', color=color.new(color.orange, 0), linewidth=2, style=plot.style_linebr )
plot(sps<0 ? s_tp1 : na , title='Short BE', color=color.new(color.orange, 0), linewidth=2, style=plot.style_linebr )
plot(sps<0 ? s_sl : na , title='Short SL', color=color.new(color.maroon, 0), linewidth=2, style=plot.style_linebr )
plot(sps>0 ? l_tp2 : na , title='Long TP', color=color.new(color.orange, 0), linewidth=2, style=plot.style_linebr )
plot(sps>0 ? l_tp1 : na , title='Long BE', color=color.new(color.orange, 0), linewidth=2, style=plot.style_linebr )
plot(sps>0 ? l_sl : na , title='Long SL', color=color.new(color.maroon, 0), linewidth=2, style=plot.style_linebr )
if (na(t)) and (not(na(t)))[1]
// strategy.cancel_all()
strategy.close("Long",alert_message = closelonga,comment = "Session Close")
strategy.close("Short",alert_message = closeshorta,comment = "Session Close")
if (na(t2)) and (not(na(t2)))[1]
// strategy.cancel_all()
strategy.close("Long",alert_message = closelonga,comment = "Session Close")
strategy.close("Short",alert_message = closeshorta,comment = "Session Close")
//@strategy_alert_message {{strategy.order.alert_message}}
//@version=5
strategy("Supertrend MACD strategy 2.2 BTC", overlay = true, process_orders_on_close = true)
//////
pc_id = input.string(title='License ID', defval='', group='MT4/5 Settings', tooltip='This is your license ID')
pc_risk = input.float(title='Risk', defval=0.1, step=0.1, minval=0, group='MT4/5 Settings', tooltip='Risk')
pc_prefix = input.string(title='MetaTrader Symbol', defval='', group='MT4/5 Settings', tooltip='This is your broker\'s MetaTrader symbol ')
var symbol = pc_prefix
closelonga = pc_id + ',closelong,' + symbol + ''
closeshorta = pc_id + ',closeshort,' + symbol + ''
Pips() =>
syminfo.mintick * (syminfo.type == "forex" ? 100 : 1)
session = input.session("0700-1700",title="Trading Session ",inline = "tx")
tx = input.string("GMT+1","",inline = "tx")
t=time(timeframe.period,session,tx)
bgcolor(not na(t) ? color.new(color.green,90) : na,title = "Trading Session" )
session2 = input.session("0700-1700",title="Trading Session2 ",inline = "tx2")
tx2 = input.string("GMT+1","",inline = "tx2")
t2=time(timeframe.period,session2,tx2)
bgcolor(not na(t2) ? color.new(color.green,90) : na,title = "Trading Session2" )
sps = strategy.position_size
grp1 = "Supertrend"
atrPeriod = input.int(11, "ATR Length", minval = 1,group = grp1)
factor = input.float(2.0, "Factor", minval = 0.01, step = 0.01,group = grp1)
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
supertrend := barstate.isfirst ? na : supertrend
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style = plot.style_linebr)
downTrend = plot(direction < 0 ? na : supertrend, "Down Trend", color = color.red, style = plot.style_linebr)
bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle",display = display.none)
fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps = false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps = false)
st_b = direction < 0//direction[1] > direction
st_s = direction > 0//direction[1] < direction
grp2 ='MACD Crossover'
fastLength = input.int(13, minval=1,group = grp2)
slowLength = input.int(26, minval=1,group = grp2)
signalLength = input.int(11, minval=1,group = grp2)
// hline(0, color=color.purple, linestyle=hline.style_dashed)
fastMA = ta.ema(close, fastLength)
slowMA = ta.ema(close, slowLength)
macd = fastMA - slowMA
signal = ta.sma(macd, signalLength)
pos = 0.00
iff_1 = signal > macd ? -1 : nz(pos[1], 0)
pos := signal < macd ? 1 : iff_1
barcolor(pos == -1 ? color.red : pos == 1 ? color.green : color.blue)
// plot(signal, color=color.new(color.red, 0), title='SIGNAL')
// plot(macd, color=color.new(color.blue, 0), title='MACD')
macd_b = macd>signal
macd_s = macd<signal
ema_on = input.bool(true,"Enable EMA",group="EMA")
ema_src = input.source(title="EMA Src", defval=close,inline="1",group="EMA")
ema_len = input.int(title="Len", defval=200,inline="1",group="EMA")
ema = ta.ema(ema_src,ema_len)
plot( ema , title='EMA', color=color.blue, linewidth=3, style=plot.style_line )
ema_b = ema_on ? close>ema :true
ema_s = ema_on ? close<ema :true
slbuff = input.float(200,"SL Buffer(Pips)")*Pips()
rr1 = input.float(1,"R:R 1")
rr2 = input.float(2,"R:R 2")
beon = input.bool(true,"Enable Breakeven")
var float l_tp1 = 0
var float l_tp2 = 0
var float l_sl = 0
var float l_ep = 0
var float s_tp1 = 0
var float s_tp2 = 0
var float s_sl = 0
var float s_ep = 0
in_session = (not(na(t))) or (not(na(t2)))
f_b = (macd_b and st_b) and (not((macd_b and st_b)[1])) and (s_sl==s_ep and sps<0?sps<0:sps==0) and ema_b and in_session
f_s = (macd_s and st_s)and (not((macd_s and st_s)[1])) and (l_sl==l_ep and sps>0?sps>0:sps==0) and ema_s and in_session
if f_b
l_sl := supertrend-slbuff
if sps<0
alert(closeshorta,alert.freq_once_per_bar_close)
l_ep := close
l_tp1 := l_ep + rr1*(l_ep-l_sl)
l_tp2 := l_ep + rr2*(l_ep-l_sl)
longa = pc_id + ',buy,' + symbol + ',risk=' + str.tostring(pc_risk, '#.##')+ ',tp=' + str.tostring(l_tp2, '#.####')+ ',sl=' + str.tostring(l_sl, '#.####')
strategy.entry("Long",strategy.long,alert_message = longa )
if sps>0 and high>=l_tp1 and beon
l_sl := l_ep+slbuff
strategy.exit("Ex Long","Long",stop = l_sl,limit = l_tp2,alert_message = closelonga )
if f_s
if sps>0
alert(closelonga,alert.freq_once_per_bar_close)
s_ep := close
s_sl := supertrend+ slbuff
s_tp1 := s_ep - rr1*(s_sl-s_ep)
s_tp2 := s_ep - rr2*(s_sl-s_ep)
shorta = pc_id + ',sell,' + symbol + ',risk=' + str.tostring(pc_risk, '#.##')+ ',tp=' + str.tostring(s_tp2, '#.####')+ ',sl=' + str.tostring(s_sl, '#.####')
strategy.entry("Short",strategy.short,alert_message = shorta )
if sps<0 and low<=s_tp1 and beon
s_sl := s_ep+slbuff
strategy.exit("Ex Short","Short",stop = s_sl,limit = s_tp2,alert_message = closeshorta )
plot(sps<0 ? s_tp2 : na , title='Short TP', color=color.new(color.orange, 0), linewidth=2, style=plot.style_linebr )
plot(sps<0 ? s_tp1 : na , title='Short BE', color=color.new(color.orange, 0), linewidth=2, style=plot.style_linebr )
plot(sps<0 ? s_sl : na , title='Short SL', color=color.new(color.maroon, 0), linewidth=2, style=plot.style_linebr )
plot(sps>0 ? l_tp2 : na , title='Long TP', color=color.new(color.orange, 0), linewidth=2, style=plot.style_linebr )
plot(sps>0 ? l_tp1 : na , title='Long BE', color=color.new(color.orange, 0), linewidth=2, style=plot.style_linebr )
plot(sps>0 ? l_sl : na , title='Long SL', color=color.new(color.maroon, 0), linewidth=2, style=plot.style_linebr )
if (na(t)) and (not(na(t)))[1]
// strategy.cancel_all()
strategy.close("Long",alert_message = closelonga,comment = "Session Close")
strategy.close("Short",alert_message = closeshorta,comment = "Session Close")
if (na(t2)) and (not(na(t2)))[1]
// strategy.cancel_all()
strategy.close("Long",alert_message = closelonga,comment = "Session Close")
strategy.close("Short",alert_message = closeshorta,comment = "Session Close")