Concept 37 / 52·Momentum·Oscillator reading
‹ Opening Range BreakoutParabolic SAR ›

Overbought & Oversold in Pine Script

OB/OS · overbought · oversold · oscillator extremes · bounded extremes

The reading of a bounded oscillator at the edge of its scale, and what it does and does not say about the next move, especially when the market is trending.

Reads best on: Ranging markets on any timeframe, and pullbacks with the trend once the trend is defined. In strong trends the readings describe strength, not a turn.
On this page: the Pine v6 script, checked, drawn on real NVDA bars. Backtest it free, costs charged, every trade on the chart.
01

Definition

Overbought and oversold are readings, not events. A bounded oscillator such as RSI, the stochastic, Williams %R or the money flow index maps recent price behaviour onto a fixed scale. The top and bottom slices of that scale are given names: overbought above 70 or 80, oversold below 30 or 20. The words describe where the oscillator is. That in turn describes how one-sided recent closes have been, or where the close sits inside the recent range. They belong to the oscillator-reading family alongside divergence, because they are ways of interpreting a line rather than tools in themselves.

The reading has value because one-sided price change tends to pause. A run of up closes uses up the buyers who were ready to act at those prices, and the next bars are more often sideways or down than another run. So an extreme reading raises the odds of a pause. What it does not do is raise the odds of a reversal by much, and it says nothing about the size of what follows. A pause inside a trend is usually a shallow pullback that leaves the oscillator in the middle and the trend intact. That is the reason fading overbought readings in a trend loses.

The standard lines are 70 and 30 for RSI, 80 and 20 for the stochastic and MFI, and −20 and −80 for Williams %R, all on 14 bars. Those numbers came from daily stock and commodity charts and mark zones the oscillator reaches a few times a month. Traders adjust them to the regime: 80 and 20 for RSI in trends so that only real overshoots register, or 60 and 40 to turn a range oscillator into a pullback trigger. The lines are conventions, not thresholds with a statistical meaning of their own.

On the chart an oscillator enters a zone quickly and leaves it in one of two ways. In a range it turns and leaves from the same side within a few bars, and the cross back out is the trigger. In a trend it stays in the zone for many bars, sometimes weeks, while price keeps going, and the eventual exit is a pause. The library's scripts all mark the exit rather than the entry. RSI, the stochastic, Williams %R and MFI each print Long when the line climbs back out of oversold and Short when it drops back out of overbought.

02

Reading the chart

The reading is easy to see; the work is deciding which regime it is in.

  1. Choose one bounded oscillator and its standard lines, for example RSI 14 with 70 and 30 or a 14, 3, 3 stochastic with 80 and 20. Do not stack several; they measure nearly the same thing and agree by construction.
  2. Note the reading and how long it has held. A first touch of the zone after weeks in the middle is different from the twentieth bar inside it; the second describes a trend, not a stretch.
  3. Decide the regime from the price chart or a higher timeframe. Higher highs and higher lows with a rising average mean overbought readings are normal; a flat range with clear edges means they are actionable.
  4. Wait for the exit from the zone. The cross back above the oversold line or below the overbought line is the cross every library script marks; the entry into the zone is the setup.
  5. Look for a second reason. A zone exit at a support level, at a prior day's low, on a divergence or with a reversal candle carries several reasons. A zone exit on its own in the middle of nowhere is a statistic.
  6. Read the range shift. If pullbacks stop reaching oversold and stall near 40 or 50 on RSI, the market has shifted into a bull range, and the oversold line should move up with it.
03

The maths

The zones are fixed slices of a bounded scale; each oscillator maps price onto that scale in its own way.

RSIt=100−1001+AvgUtAvgDt\mathrm{RSI}_t = 100 - \frac{100}{1 + \frac{\mathrm{AvgU}_t}{\mathrm{AvgD}_t}}
%Kt=100 Ct−LnHn−Ln\%K_t = 100 \, \frac{C_t - L_n}{H_n - L_n}
%Rt=−100 Hn−CtHn−Ln\%R_t = -100 \, \frac{H_n - C_t}{H_n - L_n}
Overboughtt={1Ot>U0otherwise\mathrm{Overbought}_t = \begin{cases} 1 & O_t > U \\ 0 & \text{otherwise} \end{cases}
Oversoldt={1Ot<L0otherwise\mathrm{Oversold}_t = \begin{cases} 1 & O_t < L \\ 0 & \text{otherwise} \end{cases}
AvgUt\mathrm{AvgU}_tthe smoothed average up move over the RSI length
AvgDt\mathrm{AvgD}_tthe smoothed average down move over the RSI length
CtC_tclose of the current bar
HnH_nhighest high of the last n bars
LnL_nlowest low of the last n bars
nnthe oscillator length, 14 by default
OtO_tany bounded oscillator at bar t
UUthe upper line: 70, 80 or −20 depending on the oscillator
LLthe lower line: 30, 20 or −80 depending on the oscillator
RSIt\mathrm{RSI}_tthe relative strength index at bar t
%Kt\%K_tthe raw stochastic at bar t
%Rt\%R_tWilliams %R at bar t
Overboughtt,Oversoldt\mathrm{Overbought}_t, \mathrm{Oversold}_ttrue when the oscillator is beyond the upper or lower line on bar t

RSI compares average gains with average losses, so it is smoothed and slow; the stochastic and Williams %R read the close's place in the range and are fast.

The money flow index is RSI computed on typical price times volume, so volume weights each bar's contribution and the zones sit at 80 and 20.

A mark in the library's scripts is a cross of the line back through U or L on a confirmed bar; the reading inside the zone is not itself a mark.

Unbounded oscillators such as MACD, CCI and rate of change have no fixed zones; their extremes must be set per instrument from past readings.

04

Inputs that matter

InputTypicalWhat changing it does
Overbought line70 (RSI), 80 (stochastic, MFI), −20 (Williams %R)Raising it makes the zone rarer and cuts the short marks in an uptrend; lowering it turns the zone into a pullback marker.
Oversold line30 (RSI), 20 (stochastic, MFI), −80 (Williams %R)Lowering it waits for deeper washouts; raising it registers ordinary dips inside a trend.
Length14Shorter lengths visit the zones on nearly every swing; longer ones reserve them for large moves and make the reading a regime statement.
05

In practice

  • As a range entry: inside a defined range, the rule goes long on the cross back out of oversold and short on the cross back out of overbought. That is what the RSI, stochastic, Williams %R and MFI scripts mark as Long and Short.
  • As a pullback trigger in a trend: a trend filter keeps only the oversold exits in an uptrend and only the overbought exits in a downtrend, ignoring the readings against the trend.
  • As a warning to stop adding: an overbought reading in a trend does not mean a short, but it does say late; position rules use it to stop adding and to trail the stop rather than to reverse.
  • As a regime read: an oscillator that stays in the overbought zone for many bars is describing strength. The first exit after a long stay is the moment to expect a deeper pullback than the recent ones.
  • As a confluence input: a zone exit that lines up with a level, a divergence or a reversal candle carries several reasons; the reading alone is a weak one.
  • As a scan filter: screening a watchlist for oscillators leaving oversold gives a short list of names to look at on the price chart, not a list of entries.
06

Where it goes wrong

  • Shorting overbought in an uptrend. The oscillator can stay above 70 for weeks; fades belong to ranges, and in trends the reading is for trailing stops.
  • Trading the entry into the zone. The line can keep going; wait for the cross back out, which is what every library script marks.
  • Stacking oscillators. RSI, the stochastic and %R agree by construction, so three overbought readings are one reading; add a level or a trend filter instead.
  • Keeping the lines fixed across regimes. In a bull range RSI bottoms near 40; if the oversold line stays at 30 the tool never fires when it should.
07

Compared with

Mean Reversion. Mean reversion is the statistical claim that price returns to an average; overbought and oversold are one way of spotting when it might. A z-score or a Bollinger %B measures the stretch in price; a bounded oscillator measures the stretch in momentum. Both need a regime where reversion is the norm.

Divergence. Divergence compares the oscillator's swings with price's swings; overbought and oversold read the oscillator's level alone. A reading at 75 says nothing about the previous peak; a lower peak at 75 after one at 85 does. Traders use the zones to know where to look and divergence to decide.

Bollinger Bands. Bollinger Bands mark the stretch in price on the chart: a close outside the band is two standard deviations from the average. An oscillator marks the stretch in closes or momentum in a pane. Price at the upper band with RSI at 70 says the same thing twice; price at the band with RSI at 55 is a trend.

Volatility Squeeze. A squeeze is the opposite reading: it flags when the market has done nothing for a while and is due to move. Overbought and oversold flag when it has done too much of one thing. The squeeze sets up breakouts; the zones set up pauses.

08

Build it in Quant

Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.

Open Overbought & Oversold on the workbench chart
Drawn on real data; then audit, backtest and refine in conversation.
Open in Quant →
09

Questions

  • Does overbought mean the price will fall?

    No. It means the recent closes have been mostly up, or the close sits near the top of the recent range, which is what an uptrend looks like on most bars. Overbought raises the odds of a pause; it says little about a reversal. In a range the pause often becomes a turn. In a trend it becomes a shallow pullback and the trend resumes.

  • Which oscillator is best for overbought and oversold?

    They agree more than they differ. RSI is smoothest and holds a reading, so it suits the regime read. The stochastic and Williams %R are fast and suit timing inside a range, and MFI adds volume and flags stretched moves on heavy volume. Pick one, learn how it behaves on your market and timeframe, and add a level or a trend filter rather than a second oscillator.

  • Why does the oscillator stay oversold in a crash?

    Because every new down close keeps the reading pinned. RSI's average gain stays near zero until a run of up closes arrives; the stochastic keeps reading the close at the bottom of a falling range. The reading describes what has happened, and in a crash what has happened keeps happening. That is why a long on the entry into the zone fails and the cross back out is the trigger.

  • Should I change the levels in a trend?

    Yes, or change how you read them. In an uptrend RSI tends to hold between 40 and 80, so 30 rarely prints and 70 prints constantly. Traders either move the lines to 80 and 40 for that regime, or keep 70 and 30 and simply refuse to short the overbought readings. The library's scripts take the levels as inputs, so 80 and 40 is one change.

  • What is the difference between overbought and a divergence?

    Overbought is a level: the oscillator is above its upper line. Divergence is a comparison: price made a higher high while the oscillator made a lower high. A market can be overbought without divergence, which is common in trends, and can show divergence below the overbought line. Divergence carries more information because it says momentum is thinning; the level says only that momentum has been high.

  • How do the library's scripts use the zones?

    All four bounded oscillators, RSI, the stochastic, Williams %R and MFI, mark the exit from a zone, not the entry. RSI, %R and MFI print Long when the line crosses back above the oversold level and Short when it crosses back below the overbought level. The stochastic requires a %K and %D cross inside the zone. Every mark waits for the bar to close.