strategy · own pane · open source
Stochastic crossover strategy (14, 3, 3)
Stochastic %K/%D crossover: buy a bullish cross below 20, exit when %K rises above 80. Plain-English rules, copyable code and a backtest with costs included. Deepwick Script runs a subset of Pine Script.
What the Stochastic Crossover strategy does
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
Lengthto filter noise; lower it for snappier reversals. - Pair with an RSI divergence or volume confirmation for cleaner entries.
Entry and exit rules
Language reference: Deepwick Script.
Default inputs
| Input | Default | Min | Max |
|---|---|---|---|
%K Length | 14 | 5 | 50 |
%K Smoothing | 3 | 1 | 10 |
%D Smoothing | 3 | 1 | 10 |
Oversold | 20 | 5 | 45 |
Stop Loss % | 2 | 0.5 | 10 |
Take Profit % | 6 | 0 | 20 |
How Deepwick fills this strategy
- Market orders fill at the next bar's open.
- Stops and limits follow the bar's path: open → nearer extreme → farther extreme → close.
- Gaps fill at the open.
- Commission is charged on every fill, and slippage applies to market and stop fills, as set in
strategy().
Source code
Deepwick Script, a subset of Pine Script · read-only
//@version=6
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)
colorLine = input.color("color.blue", "Color Line")
colorMid = input.color("color.orange", "Color Mid")
colorUp = input.color("color.green", "Color Up")
colorDown = input.color("color.red", "Color Down")
colorNeutral = input.color("color.gray", "Color Neutral")
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=colorNeutral, linestyle=hline.style_dashed)
hline(20, "Oversold", color=colorNeutral, linestyle=hline.style_dashed)
p__K = plot(k, "%K", color=colorLine, linewidth=2)
p__D = plot(d, "%D", color=colorMid)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=colorUp, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=colorDown, text="X")
alertcondition(longSignal, "Stoch long entry", "Stochastic crossed up inside oversold")
alertcondition(exitSignal, "Stoch exit", "Stochastic left overbought")
Run it in Deepwick
- Open a chart of any crypto pair or CFD.
- Click “Scripts” and add this strategy.
- Press Run and read the trades in the Strategy Tester.
FAQ
Is this the slow or the fast Stochastic?
It is the slow Stochastic: %K is smoothed with a 3-period SMA, and %D is a 3-period SMA of that.
Is this the Stochastic RSI strategy?
No. Stochastic RSI applies the stochastic formula to RSI values. See the Stochastic RSI indicator page; a Stoch RSI strategy page is not published yet.
Why only crosses below 20?
Requiring the cross inside oversold filters out mid-range crosses, which are the noisiest. Raise the Oversold input to get more trades.
No comments yet.
Sign in to comment.