strategy · own pane · open source

Stochastic Cross

Deepwick · @deepwickv1Updated 2 Oct 2026▲ 0 users
Running…
Loading chart…

About

Classic Stochastic %K/%D crossover long-only: enter when %K crosses above %D inside the oversold zone, exit when %K leaves the overbought zone.

  • 14/3/3 with 20/80 thresholds is the textbook setting.
  • Raise Length to filter noise; lower it for snappier reversals.
  • Pair with an RSI divergence or volume confirmation for cleaner entries.

Source code

//@version=5
strategy("Stochastic Cross", overlay=false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
len = input.int(14, "%K Length", minval=5, maxval=50, step=1)
kSmooth = input.int(3, "%K Smoothing", minval=1, maxval=10, step=1)
dSmooth = input.int(3, "%D Smoothing", minval=1, maxval=10, step=1)
oversold = input.int(20, "Oversold", minval=5, maxval=45, step=1)
stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
hh = ta.highest(high, len)
ll = ta.lowest(low, len)
kRaw = 100.0 * (close - ll) / (hh - ll)
k = ta.sma(kRaw, kSmooth)
d = ta.sma(k, dSmooth)
longSignal = strategy.position_size == 0 and ta.crossover(k, d) and k < oversold
exitSignal = strategy.position_size > 0 and k > 80
if longSignal
    strategy.entry("Long", strategy.long)
if strategy.position_size > 0
    strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
if exitSignal
    strategy.close("Long", comment="Stoch exit")
hline(80, "Overbought", color=color.gray, linestyle=hline.style_dashed)
hline(20, "Oversold", color=color.gray, linestyle=hline.style_dashed)
p__K = plot(k, "%K", color=color.blue, linewidth=2)
p__D = plot(d, "%D", color=color.orange)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=color.green, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=color.red, text="X")
alertcondition(longSignal, "Stoch long entry", "Stochastic crossed up inside oversold")
alertcondition(exitSignal, "Stoch exit", "Stochastic left overbought")

Comments

No comments yet.

Sign in to comment.