/* --------------------------------------------------------------
Custom Directional Oscillator
Author by Vitali Apirine
TASC June 2023
The stochastic distance oscillator can be used with securities
or indexes that trend. It is also suitable for trading
ranges. The SDO can be used to identify buying/selling
opportunities in harmony with the larger trend.
The SDO can also be used to identify trend changes.
A break in support or a bearish price crossover
--------------------------------------------------------------
This indicator measures the normalized magnitude of price
change and applies a direction (positive for up, negative
for down), then smooths the result with an EMA.
--------------------------------------------------------------
*/
// SECTION 1: USER-DEFINABLE PARAMETERS
// Use Param() to create easy-to-adjust inputs in the chart window.
LBPeriod = Param( "Stochastic Lookback", 200, 10, 500, 1 );
Period = Param( "Price Change Period", 12, 1, 100, 1 );
Pds = Param( "Smoothing Period", 3, 1, 100, 1 );
// SECTION 2: CORE CALCULATIONS
// 1. Calculate the absolute price change over 'Period' bars.
Dist = Abs( C - Ref( C, -Period ) );
// 2. Normalize the price change into a 0-1 range (like a Stochastic Oscillator).
// We use IIf() to prevent division-by-zero errors in flat markets.
HHV_Dist = HHV( Dist, LBPeriod );
LLV_Dist = LLV( Dist, LBPeriod );
Denominator = HHV_Dist - LLV_Dist;
D = SafeDivide(( Dist - LLV_Dist ) , Denominator);
// 3. Assign a direction to the normalized value.
// If price is up, value is positive. If price is down, value is negative.
DD = IIf( C > Ref( C, -Period ), D, IIf( C < Ref( C, -Period ), -D, 0 ) );
// 4. Calculate the final indicator line by smoothing DD with an EMA and scaling by 100.
// The original `Mov(..., E)` is equivalent to AFL's `EMA()`.
FinalOscillator = EMA( DD, Pds ) * 100;
// SECTION 3: PLOTTING
_SECTION_BEGIN( "stochastic distance oscillator (SDO)" );
// Plot the final smoothed oscillator.
Plot( FinalOscillator, "Oscillator", colorBlue, styleLine | styleThick );
// Plot a zero line for reference.
Plot( 0, " ", colorGrey50, styleDashed );
Plot( 40, "", colorGrey50, styleDashed );
Plot( -40, "", colorGrey50, styleDashed );
_SECTION_END();
Buy =Cross(finaloscillator,-40);
Sell=Cross(40,finaloscillator);
Plotshapes (sell*shapeDownarrow,colorred,0,finaloscillator);
Plotshapes (buy*shapeUparrow,colorGreen,0,finaloscillator);
//https://www.traders.com/Documentation/FEEDbk_docs/2023/06/TradersTips.html**stron//g text**
// Optional: Add a title with current values to the chart pane.
Title = EncodeColor( colorwhite ) + Name() + " (" +
NumToStr( LBPeriod, 1.0 ) + ", " +
NumToStr( Period, 1.0 ) + ", " +
NumToStr( Pds, 1.0 ) + ") | Value: " +
EncodeColor( colorBlue ) + NumToStr( FinalOscillator, 2.2 );