Volatility Breakout Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dicargo_Beam

//@version=5
strategy("Volatility Breakout Strategy", overlay=true, default_qty_type= strategy.percent_of_equity, default_qty_value=100)

k = input.float(0.6)
f_security(_sym, _res, _src) => 
    request.security(_sym, _res, _src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
o = f_security(syminfo.tickerid,"D",open)
h = f_security(syminfo.tickerid,"D",high)
l = f_security(syminfo.tickerid,"D",low)
c = f_security(syminfo.tickerid,"D",close)


var lp = 0.0
var sl = 0.0
var sp = 0.0
var sl2 = 0.0

if hour[1]-hour>1
    lp := c+(h-l)*k
    sl := (l+lp)/2
    sp := c-(h-l)*k
    sl2 := (h+sp)/2

longcond = lp < high
exit = hour[1]-hour>1 or  close < sl

plot(lp,"Long Entry",color=color.yellow)
plot(sl,"Long StopLoss",color=color.red)

 
exit2 = hour[1]-hour>1 or close > sl2

shortcond = sp > low

shortopen = input.bool(false,"Short Postion Open??")

plot(shortopen? sp : na,"Short Entry",color=color.new(color.yellow,50))
plot(shortopen? sl2 : na,"Short StopLoss",color=color.new(color.red,50))




trend = input.bool(false,"Trend Following?")

longtrend = trend? lp/sl > sl2/sp : true
shorttrend = trend? lp/sl < sl2/sp : true


strategy.entry("Long", strategy.long,comment = "Long", when = longcond and strategy.opentrades == 0 and longtrend)
strategy.close("Long", comment="Long Exit", when = exit)

if shortopen
    strategy.entry("Short", strategy.short,comment = "Short", when = shortcond and strategy.opentrades == 0 and shorttrend)
    strategy.close("Short", comment="Short Exit", when = exit2)
    

var bg = 0
if hour[1]-hour>1
    bg := bg + 1


bgcolor(bg/2== math.floor(bg/2) ? color.new(color.blue,95):na)




