Stochastic oscillator strategy
On ES futures, the stochastic oscillator strategy lost $99,521 after costs and ran a $100,000 account down to its margin on December 4, 2018. The run stops there.
Mean reversionIntraday5-minute barsBacktested Nov 6, 2026
Stochastic oscillator backtest, the result
Contract
E-mini S&P 500
-
−$99.5K
Net after costs
After $66,346 in fees and slippage
-
0 of 3
Years that made money
January 2016 to December 2018
-
−$100K
Largest drawdown
Peak to trough, 1 contract
-
−$43
Average trade
2,338 trades
Net after costs by month, January 2016 to December 2018.
Stochastic oscillator rules
A 0 to 100 reading of where price closed in its recent range: buy as it turns up from under 20, sell as it turns down from over 80.
A drawing of the rules on a made-up day, not a result.
- Raw %K is 100 x (close - lowest low of 14 bars) / (highest high of 14 bars - lowest low), and %K its 3-bar mean, on every five-minute close of the Globex session.
- When %K crosses up through 20 the strategy goes long, and when it crosses down through 80 it goes short, turning round on the opposite signal.
- New positions are taken only on bars that close in the day session before 15:45 New York.
- Whatever is open is closed at 15:45 New York.
- Decides on
- 5-minute bars
- Holds
- Nothing overnight
- Parameters
- oversold 20, overbought 80, open minute 570, exit minute 945, bar minutes 5
Results by year
It made money in 0 of 3 years on ES. Its best year was 2017, −$27,696, and its worst 2016, −$33,086. The account was closed out on December 4, 2018, so the years stop there.
| Year | Net | Trades | Win rate | Drawdown | Buy and hold |
|---|---|---|---|---|---|
| 2016 | −$33,086 | 803 | 49% | −$33,826 | +$11,497 |
| 2017 | −$27,696 | 769 | 43% | −$27,791 | +$21,936 |
| 2018, Jan to Dec | −$38,739 | 766 | 52% | −$38,739 | −$8,589 |
Stochastic oscillator backtest results
On ES it lost $33,175 before costs, then paid $66,346 in fees and slippage.
- Net = before costs − fees − slippage
- ES: −$33,175 − $7,896 − $58,450 = −$99,521
The whole interval is under zero: after costs, the average trade lost money.
Stochastic oscillator code and Pine Script
The strategy as the backtest ran it, and the Pine Script our compiler wrote from it for TradingView.
1from atlantive import BUY, NEW_YORK, SELL, Bars, Rolling, RollingMinMax, Session, Strategy2 3 4class StochasticOscillator(Strategy):5 """Slow stochastic %K (14, 3) on five-minute bars, traded in the New York day session.6 7 Raw %K is 100 x (close - lowest low of 14 bars) / (highest high of 14 bars - lowest low), and8 %K its 3-bar mean, on every five-minute close of the Globex session. When %K crosses up through9 20 the strategy goes long, and when it crosses down through 80 it goes short, turning round on10 the opposite signal. New positions are taken only on bars that close in the day session before11 15:45, and whatever is open at 15:45 is closed. A bar whose 14-bar range is zero leaves %K12 where it was.13 """14 15 data = "bars"16 17 class Params:18 oversold: float = 20.019 overbought: float = 80.020 open_minute: int = 57021 exit_minute: int = 94522 bar_minutes: int = 523 clip: int = 124 25 class State:26 clock: Session(zone=NEW_YORK)27 bars: Bars(interval_ns=300_000_000_000)28 highs: RollingMinMax(window=14)29 lows: RollingMinMax(window=14)30 smooth: Rolling(window=3)31 k: float32 last_k: float33 has_k: bool34 has_last: bool35 36 def on_event(self, view, ev, orders):37 self.clock.update(ev)38 if not self.bars.update(ev):39 return40 minute = self.clock.prev_minute41 high = float(self.bars.prev_high)42 low = float(self.bars.prev_low)43 close = float(self.bars.prev_close)44 self.highs.update(high)45 self.lows.update(low)46 self.has_last = self.has_k47 self.last_k = self.k48 if self.highs.ready == 1 and self.lows.ready == 1:49 span = self.highs.max() - self.lows.min()50 if span != 0:51 raw = 100.0 * (close - self.lows.min()) / span52 self.smooth.update(raw)53 if self.smooth.ready == 1:54 self.k = self.smooth.mean()55 self.has_k = True56 ends = minute + self.p.bar_minutes57 in_day = minute >= self.p.open_minute and ends < self.p.exit_minute58 up = self.has_last and self.has_k and self.last_k < self.p.oversold and self.k >= self.p.oversold59 down = self.has_last and self.has_k and self.last_k > self.p.overbought and self.k <= self.p.overbought60 if view.position != 0 and not in_day:61 if view.position > 0:62 orders.ioc(side=SELL, qty=abs(view.position))63 else:64 orders.ioc(side=BUY, qty=abs(view.position))65 return66 if not in_day:67 return68 if up:69 if view.position < 0:70 orders.ioc(side=BUY, qty=abs(view.position) + self.p.clip)71 elif view.position == 0:72 orders.ioc(side=BUY, qty=self.p.clip)73 elif down:74 if view.position > 0:75 orders.ioc(side=SELL, qty=abs(view.position) + self.p.clip)76 elif view.position == 0:77 orders.ioc(side=SELL, qty=self.p.clip) 1//@version=62// StochasticOscillator, exported from Atlantive.3// Chart simulation: decisions use completed bars; market orders fill at the next bar open.4// Native fills, fees, data gaps and session boundaries can differ. Re-test on the target symbol.5// Use standard time-based candles and the strategy's timeframe, not synthetic chart prices.6// Pine v6 trims old trade records at its order limit; the strategy keeps calculating.7// Margin is explicitly disabled to retain the native signal simulation's sizing.8strategy("StochasticOscillator", overlay=true, initial_capital=100000.0, commission_type=strategy.commission.cash_per_contract, commission_value=1.68, default_qty_type=strategy.fixed, default_qty_value=1, calc_on_every_tick=false, process_orders_on_close=false, slippage=0, margin_long=0, margin_short=0)9 10if timeframe.in_seconds() != 30011 runtime.error("StochasticOscillator was exported for the 5 minute timeframe; set the chart to 5.")12 13oversold = input.float(20.0, "oversold")14overbought = input.float(80.0, "overbought")15open_minute = input.int(570, "open_minute", minval=-9007199254740991, maxval=9007199254740991)16exit_minute = input.int(945, "exit_minute", minval=-9007199254740991, maxval=9007199254740991)17bar_minutes = input.int(5, "bar_minutes", minval=-9007199254740991, maxval=9007199254740991)18clip = input.int(1, "clip", minval=-9007199254740991, maxval=9007199254740991)19 20_q_safe_int(x) =>21 if math.abs(x) > 900719925474099122 runtime.error("an integer left the exact chart range; this run cannot be compared faithfully")23 int(x)24_q_int(x) => _q_safe_int(x < 0 ? math.ceil(x) : math.floor(x))25_q_iadd(a, b) => _q_safe_int(a + b)26 27_q_high = _q_int(math.round(high * 1000000000.0))28_q_low = _q_int(math.round(low * 1000000000.0))29_q_close = _q_int(math.round(close * 1000000000.0))30 31var float k = 0.032var float last_k = 0.033var bool has_k = false34var bool has_last = false35// clock: completed chart bar's opening timestamp (America/New_York).36_q_clock_prev_minute = hour(time, "America/New_York") * 60 + minute(time, "America/New_York")37_q_clock_prev_second = _q_clock_prev_minute * 60 + second(time, "America/New_York")38_q_clock_prev_weekday = (dayofweek(time, "America/New_York") + 5) % 739_q_clock_prev_day = int(timestamp("UTC", year(time, "America/New_York"), month(time, "America/New_York"), dayofmonth(time, "America/New_York"), 0, 0) / 86400000)40// Current clock: this chart bar's scheduled close, known without a future bar.41// A native next event after a data gap can carry a later time.42_q_clock_minute = hour(time_close, "America/New_York") * 60 + minute(time_close, "America/New_York")43_q_clock_second = _q_clock_minute * 60 + second(time_close, "America/New_York")44_q_clock_weekday = (dayofweek(time_close, "America/New_York") + 5) % 745_q_clock_day = int(timestamp("UTC", year(time_close, "America/New_York"), month(time_close, "America/New_York"), dayofmonth(time_close, "America/New_York"), 0, 0) / 86400000)46_q_clock_seen = 147var _q_highs_buf = array.new_float(14, 0.0)48var int _q_highs_idx = 049var int _q_highs_count = 050var int _q_highs_skipped = 051var _q_highs_hiq = array.new_int(14, 0)52var int _q_highs_hih = 053var int _q_highs_hin = 054var _q_highs_loq = array.new_int(14, 0)55var int _q_highs_loh = 056var int _q_highs_lon = 057var _q_lows_buf = array.new_float(14, 0.0)58var int _q_lows_idx = 059var int _q_lows_count = 060var int _q_lows_skipped = 061var _q_lows_hiq = array.new_int(14, 0)62var int _q_lows_hih = 063var int _q_lows_hin = 064var _q_lows_loq = array.new_int(14, 0)65var int _q_lows_loh = 066var int _q_lows_lon = 067var _q_smooth_buf = array.new_float(3, 0.0)68var int _q_smooth_idx = 069var int _q_smooth_count = 070var float _q_smooth_off = 0.071var float _q_smooth_sum = 0.072var float _q_smooth_sumsq = 0.073var int _q_smooth_skipped = 074int minute_l = na75float high_l = na76float low_l = na77float close_l = na78float span = na79float raw = na80int ends = na81bool in_day = false82bool up = false83bool down = false84 85bool _q_running = true86minute_l := _q_int(_q_clock_prev_minute)87high_l := _q_high * 1.088low_l := _q_low * 1.089close_l := _q_close * 1.090_q_sample1 = high_l91if na(_q_sample1) or math.abs(_q_sample1) > 1.7976931348623157e30892 _q_highs_skipped := _q_highs_skipped + 193else94 if _q_highs_count >= 14 and _q_highs_hin > 0 and array.get(_q_highs_hiq, _q_highs_hih) == _q_highs_idx95 _q_highs_hih := (_q_highs_hih + 1) % 1496 _q_highs_hin := _q_highs_hin - 197 if _q_highs_count >= 14 and _q_highs_lon > 0 and array.get(_q_highs_loq, _q_highs_loh) == _q_highs_idx98 _q_highs_loh := (_q_highs_loh + 1) % 1499 _q_highs_lon := _q_highs_lon - 1100 array.set(_q_highs_buf, _q_highs_idx, _q_sample1)101 while _q_highs_hin > 0102 _q_back1_hi = array.get(_q_highs_hiq, (_q_highs_hih + _q_highs_hin - 1) % 14)103 if array.get(_q_highs_buf, _q_back1_hi) <= _q_sample1104 _q_highs_hin := _q_highs_hin - 1105 else106 break107 array.set(_q_highs_hiq, (_q_highs_hih + _q_highs_hin) % 14, _q_highs_idx)108 _q_highs_hin := _q_highs_hin + 1109 while _q_highs_lon > 0110 _q_back1_lo = array.get(_q_highs_loq, (_q_highs_loh + _q_highs_lon - 1) % 14)111 if array.get(_q_highs_buf, _q_back1_lo) >= _q_sample1112 _q_highs_lon := _q_highs_lon - 1113 else114 break115 array.set(_q_highs_loq, (_q_highs_loh + _q_highs_lon) % 14, _q_highs_idx)116 _q_highs_lon := _q_highs_lon + 1117 _q_highs_idx := (_q_highs_idx + 1) % 14118 _q_highs_count := math.min(_q_highs_count + 1, 14)119_q_sample2 = low_l120if na(_q_sample2) or math.abs(_q_sample2) > 1.7976931348623157e308121 _q_lows_skipped := _q_lows_skipped + 1122else123 if _q_lows_count >= 14 and _q_lows_hin > 0 and array.get(_q_lows_hiq, _q_lows_hih) == _q_lows_idx124 _q_lows_hih := (_q_lows_hih + 1) % 14125 _q_lows_hin := _q_lows_hin - 1126 if _q_lows_count >= 14 and _q_lows_lon > 0 and array.get(_q_lows_loq, _q_lows_loh) == _q_lows_idx127 _q_lows_loh := (_q_lows_loh + 1) % 14128 _q_lows_lon := _q_lows_lon - 1129 array.set(_q_lows_buf, _q_lows_idx, _q_sample2)130 while _q_lows_hin > 0131 _q_back2_hi = array.get(_q_lows_hiq, (_q_lows_hih + _q_lows_hin - 1) % 14)132 if array.get(_q_lows_buf, _q_back2_hi) <= _q_sample2133 _q_lows_hin := _q_lows_hin - 1134 else135 break136 array.set(_q_lows_hiq, (_q_lows_hih + _q_lows_hin) % 14, _q_lows_idx)137 _q_lows_hin := _q_lows_hin + 1138 while _q_lows_lon > 0139 _q_back2_lo = array.get(_q_lows_loq, (_q_lows_loh + _q_lows_lon - 1) % 14)140 if array.get(_q_lows_buf, _q_back2_lo) >= _q_sample2141 _q_lows_lon := _q_lows_lon - 1142 else143 break144 array.set(_q_lows_loq, (_q_lows_loh + _q_lows_lon) % 14, _q_lows_idx)145 _q_lows_lon := _q_lows_lon + 1146 _q_lows_idx := (_q_lows_idx + 1) % 14147 _q_lows_count := math.min(_q_lows_count + 1, 14)148has_last := has_k149last_k := k150if (_q_highs_count >= 14 ? 1 : 0) == 1 and (_q_lows_count >= 14 ? 1 : 0) == 1151 span := (_q_highs_count == 0 ? 0.0 : array.get(_q_highs_buf, array.get(_q_highs_hiq, _q_highs_hih))) - (_q_lows_count == 0 ? 0.0 : array.get(_q_lows_buf, array.get(_q_lows_loq, _q_lows_loh)))152 if span != 0153 raw := (100.0 * (close_l - (_q_lows_count == 0 ? 0.0 : array.get(_q_lows_buf, array.get(_q_lows_loq, _q_lows_loh)))) * 1.0) / span154 _q_sample3 = raw155 if na(_q_sample3) or math.abs(_q_sample3) > 1.7976931348623157e308156 _q_smooth_skipped := _q_smooth_skipped + 1157 else158 if _q_smooth_count == 0159 _q_smooth_off := _q_sample3160 if _q_smooth_count >= 3161 _q_old4 = array.get(_q_smooth_buf, _q_smooth_idx) - _q_smooth_off162 _q_smooth_sum := _q_smooth_sum - _q_old4163 _q_smooth_sumsq := _q_smooth_sumsq - _q_old4 * _q_old4164 else165 _q_smooth_count := _q_smooth_count + 1166 _q_dev4 = _q_sample3 - _q_smooth_off167 array.set(_q_smooth_buf, _q_smooth_idx, _q_sample3)168 _q_smooth_sum := _q_smooth_sum + _q_dev4169 _q_smooth_sumsq := _q_smooth_sumsq + _q_dev4 * _q_dev4170 _q_smooth_idx := (_q_smooth_idx + 1) % 3171 if _q_smooth_idx == 0 and _q_smooth_count >= 3172 _q_origin4 = _q_smooth_off + _q_smooth_sum / _q_smooth_count173 _q_total4 = 0.0174 _q_totalsq4 = 0.0175 for _q_j4 = 0 to 2176 _q_e4 = array.get(_q_smooth_buf, _q_j4) - _q_origin4177 _q_total4 := _q_total4 + _q_e4178 _q_totalsq4 := _q_totalsq4 + _q_e4 * _q_e4179 _q_smooth_off := _q_origin4180 _q_smooth_sum := _q_total4181 _q_smooth_sumsq := _q_totalsq4182 if (_q_smooth_count >= 3 ? 1 : 0) == 1183 k := (_q_smooth_count == 0 ? 0.0 : _q_smooth_off + _q_smooth_sum / _q_smooth_count)184 has_k := true185ends := _q_int(_q_iadd(minute_l, bar_minutes))186in_day := minute_l >= open_minute and ends < exit_minute187up := has_last and has_k and last_k < oversold and k >= oversold188down := has_last and has_k and last_k > overbought and k <= overbought189if _q_int(strategy.position_size) != 0 and not in_day190 if _q_int(strategy.position_size) > 0191 _q_qty5 = _q_int(math.abs(_q_int(strategy.position_size)))192 if _q_qty5 <= 0193 runtime.error("market order quantity must be a positive whole unit")194 _q_pos5 = _q_int(strategy.position_size)195 if _q_pos5 < 0 or _q_pos5 > 0 and _q_qty5 < _q_pos5196 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")197 strategy.order("q1", strategy.short, _q_qty5)198 else199 _q_qty6 = _q_int(math.abs(_q_int(strategy.position_size)))200 if _q_qty6 <= 0201 runtime.error("market order quantity must be a positive whole unit")202 _q_pos6 = _q_int(strategy.position_size)203 if _q_pos6 > 0 or _q_pos6 < 0 and _q_qty6 < -_q_pos6204 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")205 strategy.order("q2", strategy.long, _q_qty6)206 _q_running := false207if _q_running208 if not (in_day)209 _q_running := false210if _q_running211 if up212 if _q_int(strategy.position_size) < 0213 _q_qty7 = _q_int(_q_iadd(math.abs(_q_int(strategy.position_size)), clip))214 if _q_qty7 <= 0215 runtime.error("market order quantity must be a positive whole unit")216 _q_pos7 = _q_int(strategy.position_size)217 if _q_pos7 > 0 or _q_pos7 < 0 and _q_qty7 < -_q_pos7218 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")219 strategy.order("q3", strategy.long, _q_qty7)220 else if _q_int(strategy.position_size) == 0221 _q_qty8 = _q_int(clip)222 if _q_qty8 <= 0223 runtime.error("market order quantity must be a positive whole unit")224 _q_pos8 = _q_int(strategy.position_size)225 if _q_pos8 > 0 or _q_pos8 < 0 and _q_qty8 < -_q_pos8226 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")227 strategy.order("q4", strategy.long, _q_qty8)228 else if down229 if _q_int(strategy.position_size) > 0230 _q_qty9 = _q_int(_q_iadd(math.abs(_q_int(strategy.position_size)), clip))231 if _q_qty9 <= 0232 runtime.error("market order quantity must be a positive whole unit")233 _q_pos9 = _q_int(strategy.position_size)234 if _q_pos9 < 0 or _q_pos9 > 0 and _q_qty9 < _q_pos9235 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")236 strategy.order("q5", strategy.short, _q_qty9)237 else if _q_int(strategy.position_size) == 0238 _q_qty10 = _q_int(clip)239 if _q_qty10 <= 0240 runtime.error("market order quantity must be a positive whole unit")241 _q_pos10 = _q_int(strategy.position_size)242 if _q_pos10 < 0 or _q_pos10 > 0 and _q_qty10 < _q_pos10243 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")244 strategy.order("q6", strategy.short, _q_qty10) How it was tested
The published rules, never tuned on the data, with real costs on every fill. Every strategy here is tested the same way.
- Market data
- CME Globex 5-minute bars, Jan 2, 2016 to Jul 31, 2026
- Rules
- As published, with the published parameters, never tuned on this data
- Size
- 1 contract on every trade
- Contract
- The most traded one, rolled as the market moved
- Fees
- The broker's $0.50 a side, plus CME's and the NFA's fees when the window opens
- Slippage
- 1 tick a side on ES, 2 in NQ's regular session and 3 outside it, 1 on other markets, where it was not measured
- Stops and entries
- Tested on 1-second closes and filled at the next bar
- Contracts traded
- ES: 43 contracts, ESH6 to ESU6NQ: 43 contracts, NQH6 to NQU6
- Costs paid
- ES: $7,896 in fees, $58,450 in slippageNQ: $10,524 in fees, $62,410 in slippage
- Warm-up
- None: each contract reads only its own bars
- Flagged days
- ES: NoneNQ: None Days the data holds too few bars for. Flagged, not removed.
Could you pass
a prop firm challenge
with the stochastic oscillator strategy?
A backtest says whether a strategy makes money. Our AI runs its trades through the rules of every prop firm account, then tells you which one it would pass and at what size.
No card needed. Free credits every month.
- Most money: Blue Guardian Futures Direct 50K, $5,084
- Best payout odds: Funded Futures Family Straight to Funded 50K, 98%
- Fastest pass: Phidias Express to Live 50K, 11 days
$5,084
Median net across 2,000 simulated runs
- Evaluation
- None
- First payout
- 97%
- Fees paid
- $1,521
Median net across 2,000 simulated runs
Questions about the stochastic oscillator strategy
What is the stochastic oscillator strategy?
A 0 to 100 reading of where price closed in its recent range: buy as it turns up from under 20, sell as it turns down from over 80. The rules follow George C. Lane, Lane's Stochastics, Technical Analysis of Stocks & Commodities V.2:3 (1984). Atlantive backtested it on CME futures from January 2016 to July 2026 after costs.
Is the stochastic oscillator strategy profitable on ES futures?
Not in this backtest: on ES futures from January 2016 to December 2018 it lost $99,521 after costs, over 2,338 trades, positive in 0 of 3 years; on NQ it lost $99,024. Buying and holding one ES contract over the same window made $236,212. The average trade was −$43, its 95% interval −$58 to −$27. These are hypothetical results of a backtest, not of trading.
What are the stochastic oscillator strategy's rules?
Raw %K is 100 x (close - lowest low of 14 bars) / (highest high of 14 bars - lowest low), and %K its 3-bar mean, on every five-minute close of the Globex session. When %K crosses up through 20 the strategy goes long, and when it crosses down through 80 it goes short, turning round on the opposite signal. New positions are taken only on bars that close in the day session before 15:45 New York. Whatever is open is closed at 15:45 New York. The rules follow George C. Lane, Lane's Stochastics, Technical Analysis of Stocks & Commodities V.2:3 (1984). Its parameters are the published ones, never tuned on the data it was tested on.
How was the stochastic oscillator strategy backtested?
On CME Globex data from January 2016 to July 2026, one contract, on 5-minute bars, rolled to the most traded contract as the market moved, with the broker's and CME's fees and the slippage measured on each market. Stops and entries were tested on 1-second closes and filled at the next bar. Nothing was fitted to the data.
Can I run the stochastic oscillator strategy in TradingView?
Yes. The page carries its Pine Script, written by Atlantive's compiler from the same code the backtest ran, for TradingView's strategy tester. A chart's fills and fees can differ from the backtest's, so test it again on the symbol you trade.
Can I change the stochastic oscillator strategy and test it again?
Yes. Rerun it in Atlantive: a new chat opens with the strategy's rules in the box, for you to read, change and send. The agent writes the code, backtests it on the market and window you name, and reports the result with its costs.