Optimized Keltner Channels SL/TP Strategy for BTC
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chanu_lev10k

//@version=4
strategy(title="Optimized Keltner Channels SL/TP Strategy for BTC", overlay=true)
length = input(9, minval=1)
mult = input(1.0, "Multiplier")
src = input(close, title="Source")
exp = input(true, "Use Exponential MA")
BandsStyle = input("Average True Range", options = ["Average True Range", "True Range", "Range"], title="Bands Style")
atrlength = input(19, "ATR Length")
sl = input(defval=20, minval=0, type=input.float, step=0.1, title="Stop Loss (%)")
tp = input(defval=20.3, minval=0, type=input.float, step=0.1, title="Take Profit (%)")

esma(source, length)=>
	s = sma(source, length)
	e = ema(source, length)
	exp ? e : s
ma = esma(src, length)
rangema = BandsStyle == "True Range" ? rma(tr(true), length) : BandsStyle == "Average True Range" ? atr(atrlength) : rma(high - low, length)
upper = ma + rangema * mult
lower = ma - rangema * mult
c = color.blue
u = plot(upper, color=color.green, title="Upper")
plot(ma, color=#0094FF, title="Basis")
l = plot(lower, color=color.red, title="Lower")
fill(u, l, color.new(color=#0094FF, transp=95), title="Background")
crossUpper = crossover(src, upper)
crossLower = crossunder(src, lower)
bprice = 0.0
bprice := crossUpper ? close+syminfo.mintick : nz(bprice[1])
sprice = 0.0
sprice := crossLower ? close-syminfo.mintick : nz(sprice[1])
crossBcond = false
crossBcond := crossUpper ? true
     : na(crossBcond[1]) ? false : crossBcond[1]
crossScond = false
crossScond := crossLower ? true
     : na(crossScond[1]) ? false : crossScond[1]
cancelBcond = crossBcond and (src < ma or high >= bprice )
cancelScond = crossScond and (src > ma or low <= sprice )
if (cancelBcond)
	strategy.cancel("KltChLE")
if (crossUpper)
	strategy.entry("KltChLE", strategy.long, stop=bprice, comment="Long")
if (cancelScond)
	strategy.cancel("KltChSE")
if (crossLower)
	strategy.entry("KltChSE", strategy.short, stop=sprice, comment="Short")

strategy.exit("Long exit", "KltChLE", profit = close * tp * 0.01 / syminfo.mintick, loss = close * sl * 0.01 / syminfo.mintick)
strategy.exit("Short exit", "KltChSE", profit = close * tp * 0.01 / syminfo.mintick, loss = close * sl * 0.01 / syminfo.mintick)

plot(bprice, color=color.green)
plot(sprice, color=color.red)