I have a question about the KC strategy, a strategy that is built into the trading view.
I would like to edit the script so that short orders cannot be placed with this strategy.
(I would like to edit it to only place long orders).
//@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)
u = plot(upper, color=#2962FF, title="Upper")
l = plot(lower, color=#2962FF, title="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 )
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")
I tried deleting the last two lines as a test, but the long order is no longer closing.
You can remove the lines:
if (cancelScond)
strategy.cancel("KltChSE")
if (crossLower)
strategy.entry("KltChSE", strategy.short, stop=sprice, comment="KltChSE")
But if you do so, you need an exit strategy with either strategy.exit()
or strategy.close()
. Otherwise, it will always be stuck in the very first long position.