// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // ©️ ashishchauhan //@version=5 strategy(title="Keltner Channels Strategy", overlay=true) length = input.int(20, minval=1) mult = input.float(2.0, "Multiplier") src = input(close, title="Source") exp = input(true, "Use Exponential MA") BandsStyle = input.string("Average True Range", options = ["Average True Range", "True Range", "Range"], title="Bands Style") atrlength = input(10, "ATR Length") esma(source, length)=> s = ta.sma(source, length) e = ta.ema(source, length) exp ? e : s ma = esma(src, length) rangema = BandsStyle == "True Range" ? ta.tr(true) : BandsStyle == "Average True Range" ? ta.atr(atrlength) : ta.rma(high - low, length) upper = ma + rangema * mult lower = ma - rangema * mult crossUpper = ta.crossover(src, upper) crossLower = ta.crossunder(src, lower) bprice = 0.0 bprice := crossUpper ? high+syminfo.mintick : nz(bprice[1]) sprice = 0.0 sprice := crossLower ? low -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 ) ut=input(defval=false,title="USE TARGET") us=input(defval=false,title="USE STOPLOSS") tar=input(defval=10.0,title="TARGET IN %") stop=input(defval=7.0,title="STOP LOSS IN %") tar:=tar/100*close/syminfo.mintick stop:=stop/100*close/syminfo.mintick if (cancelBcond) strategy.cancel("KltChLE") if (crossUpper) strategy.entry("KltChLE", strategy.long, stop=bprice, comment="KltChLE") if (cancelScond) strategy.cancel("KltChSE") if (crossLower) strategy.entry("KltChSE", strategy.short, stop=sprice, comment="KltChSE") if(ut==true and us==false) strategy.exit(id="LX",from_entry="KltChLE",profit=tar,comment="TYPE:LX") strategy.exit(id="SX",from_entry="KltChSE",profit=tar,comment="TYPE:SX") if(us==true and ut==false) strategy.exit(id="LX",from_entry="KltChLE",loss=stop,comment="TYPE:LX") strategy.exit(id="SX",from_entry="KltChSE",loss=stop,comment="TYPE:SX") if(ut==true and us==true) strategy.exit(id="LX",from_entry="KltChLE",profit=tar,loss=stop,comment="TYPE:LX") strategy.exit(id="SX",from_entry="KltChSE",profit=tar,loss=stop,comment="TYPE:SX")