 # TASC 2023.06 Stochastic Distance Oscillator █ OVERVIEW

This script implements the stochastic distance oscillator (SDO), a momentum indicator introduced by Vitali Apirine in an article featured in TASC's June 2023 edition of Traders' Tips. The SDO is a variation of the classic stochastic oscillator and is designed to identify overbought and oversold levels, as well as detect bull and bear trend changes.

█ CONCEPTS

Unlike the classic stochastic oscillator, which compares an asset's price to its past price range, the SDO measures the size of the current distance relative to the maximum-minimum distance range over a set number of periods. The current distance is defined as the distance between the current price and the price n periods ago.

The readings of the SDO can be used to identify the following states of the asset price:
• Uptrend state: the oscillator crosses over 50 from a non-uptrend state.
• Downtrend state: the oscillator crosses under -50 from a non-downtrend state.
• Overbought state: the oscillator is in an uptrend and crosses -50 for the first time.
• Oversold state: the oscillator is in a downtrend and crosses 50 for the first time.
• Trend continuity: the oscillator crosses 0 in the direction of the current trend.
The script indicates these five conditions using on-chart signals and background coloring.

█ CALCULATIONS

The SDO is calculated as follows:

1. Calculate the distance between the current price and the price n periods ago, as well as the maximum and minimum distances for the selected lookback period. The author recommends using one of two values of n, 14 or 40 bars.

2. Calculate the time series %D that represents the relation between the asset's current distance and its distance range over a loockback period:
%D = (Abs(current distance) − Abs(minimum distance)) / (Abs(maximum distance) − Abs(minimum distance)) * 100

3. Use the calculated %D to obtain the SDO:
• If the closing price is above the close n periods ago, SDO = %D
• If the closing price is below the close n periods ago, SDO = −%D
• If the closing price equals the close n periods ago or the current distance equals the minimum distance, SDO = 0
4. Smooth the SDO using an exponential moving average (​EMA). The author recommends using an EMA in the range from 3 to 6.

Adjustable input parameters include the number of periods n, the lookback period for calculating %D, the smoothing EMA length, and the overbought/oversold threshold level.

Tools and ideas for all Pine coders: www.tradingview.com/u/PineCoders/