Indicator · Pine Script v6 · Own pane

Pretty Good Oscillator

by mihakralj

The Pretty Good Oscillator calculates the distance between the selected price source and its simple moving average, normalized by the Average True Range over a specified period.

View on GitHub

GitHub stars47on the repository
Pine versionv6current
Lines of code78
Settings2inputs
LicenceMIT
Last update8 months ago28 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

78 lines of Pine Script v6 from indicators/oscillators/pgo.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

pgo.pine
// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Pretty Good Oscillator", "PGO", overlay=false)

//@function Calculate Pretty Good Oscillator (PGO)
//@param source Price data to analyze
//@param period Number of bars for SMA and ATR calculation
//@returns PGO value normalized by ATR
pgo(series float source, simple int period) =>
    if period <= 0
        runtime.error("Period must be greater than 0")
    if period > 5000
        runtime.error("Period exceeds maximum of 5000")
    
    var array<float> sma_buffer = array.new_float(period, na)
    var int sma_head = 0
    var float sma_sum = 0.0
    var int valid_count = 0
    
    float oldest = array.get(sma_buffer, sma_head)
    if not na(oldest)
        sma_sum -= oldest
        valid_count -= 1
    
    if not na(source)
        sma_sum += source
        valid_count += 1
    
    array.set(sma_buffer, sma_head, source)
    sma_head := (sma_head + 1) % period
    
    float sma_value = nz(sma_sum / valid_count, source)
    
    float prevClose = nz(close[1], close)
    float tr1 = high - low
    float tr2 = math.abs(high - prevClose)
    float tr3 = math.abs(low - prevClose)
    float tr = math.max(tr1, math.max(tr2, tr3))
    
    float a = 1.0 / float(period)
    float beta = 1.0 - a
    var bool warmup = true
    var float e = 1.0
    var float ema = 0.0
    var float atr = nz(tr)
    
    ema := a * (nz(tr) - ema) + ema
    
    if warmup
        e *= beta
        float c = 1.0 / (1.0 - e)
        atr := c * ema
        warmup := e > 1e-10
    else
        atr := ema
    
    float pgo_value = atr > 0 ? (source - sma_value) / atr : na
    
    pgo_value

// ---------- Main loop ----------

// Inputs
i_period = input.int(14, "Period", minval=1, maxval=500, tooltip="Number of bars for SMA and ATR calculation")
i_source = input.source(close, "Source")

// Calculation
result = pgo(i_source, i_period)

// Plot
plot(result, "PGO", color=color.yellow, linewidth=2)
hline(0, "Zero Line", color=color.gray, linestyle=hline.style_solid)
hline(3, "Overbought", color=color.red, linestyle=hline.style_dashed)
hline(-3, "Oversold", color=color.green, linestyle=hline.style_dashed)

// Background coloring for extreme zones
bgcolor(not na(result) and result > 3 ? color.new(color.red, 85) : not na(result) and result < -3 ? color.new(color.green, 85) : na)

Overview

Pretty Good Oscillator renders as an oscillator line in a separate pane alongside a zero line and threshold levels at positive and negative three. Background highlights indicate potential overbought conditions above three and oversold conditions below negative three. Users can configure the calculation period and input source. The indicator does not generate automated entry or exit orders.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum, Mean reversion

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescriptpinescript-indicatorstechnical-analysistradingview-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Periodint14i_period
Sourcesourceclosei_source
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

261 stars

This indicator calculates standardized z-scores of price data across four different lookback periods (6, 14, 26, and 100). Standardizing data subtracts the moving average from the price and divides by the standard deviation. It applies a Double Exponential Mov

Indicator v5 Mean reversionVolatility & bands
114 stars

This indicator calculates normalized percentage bounds around price highs and lows to anticipate local trend reversals. It oscillates within a fixed -1 to 1 channel and plots two boundary tracking curves alongside a background Karobein oscillator histogram for

Indicator v4 Mean reversionOscillators & momentum Repaints
Updated 1 year ago · None stated GitHub repository of Mean Reversion Indicator
99 stars

This indicator calculates the statistical autocorrelation coefficient of an input series over a user-defined lookback window at a specified lag. It tracks values using an internal circular buffer, computes sample mean, variance, and autocovariance, and normali

Indicator v6 TrendMean reversion
Updated 4 weeks ago · Apache-2.0 GitHub repository of Autocorrelation Function (ACF)

Calculates a composite carry and decay oscillator fusing impulse-autocorrelation half-life, a Hurst exponent approximation, path efficiency, and range expansion ratios. It evaluates whether an impulse is likely to persist or mean-revert, plotting the oscillato

Indicator v6 TrendOscillators & momentum
Updated yesterday · MIT GitHub repository of Market Memory & Decay Oscillator [WavesUnchained]

This indicator plots trade entry and exit signals on the price chart by combining Bollinger Bands with optional filter conditions. It calculates standard Bollinger Bands along with RSI, Aroon, and moving average filters. Reversal signals trigger when price bre

Indicator v4 Volatility & bandsOscillators & momentum
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner IND [Alorse]

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers