Stochastic classical

Download script
Stochastic classical

This script functions as a technical analysis tool designed to help traders identify momentum and potential overbought or oversold conditions in the financial markets. It calculates two primary visual lines—often referred to as the Main Line (%K) and the Signal Line (%D)—which move independently within a separate window on your trading chart.

Core Operational Logic

The script follows a step-by-step mathematical process to transform raw price data into a readable oscillator:

1. Identifying Price Extremes The tool begins by looking back at a specific number of historical bars (defined by the period). It identifies the highest and lowest prices reached during this timeframe. Depending on your settings, it can choose to look strictly at the "High" and "Low" price points or focus specifically on the "Closing" prices of those bars.

2. Calculating Relative Position Once the extremes are identified, the script determines where the current market price sits in relation to that range. It calculates a percentage: essentially asking, "Of the distance between the lowest point and the highest point, what percentage of the way up (or down) is the current price?" This results in a value typically scaled between 0 and 100.

3. Smoothing the Data Raw percentage calculations can often be jagged and "noisy," making them difficult to read. To solve this, the script applies two layers of smoothing:

  • The Main Line (%K): The initial percentage calculation is smoothed out using a moving average. This creates the primary trend line you see on your screen.
  • The Signal Line (%D): The script then takes that smoothed Main Line and applies a second layer of smoothing to it. This creates the secondary, often dotted line, which serves as a reference point for the main trend.

Customization Options

The logic allows for several adjustable parameters to suit different trading styles:

  • Speed Control: You can adjust how quickly or slowly the indicator reacts to price changes by modifying the "Slowing" and "Smoothing" variables.
  • Calculation Methods: The script offers different types of moving averages (such as Simple, Exponential, Smoothed, or Linear Weighted), allowing you to choose whether you want the lines to prioritize recent price action or a broader historical average.
  • Data Source: You can toggle the logic to focus on the absolute price range (High/Low) or just the closing prices, depending on which data point you find more reliable for your specific strategy.
Indicators Used
Moving average indicator
15 Views
0 Downloads
0 Favorites
Stochastic classical
//+------------------------------------------------------------------+
//|                                         Stochastic classical.mq4 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers    2
#property indicator_color1     Green
#property indicator_width1     2
#property indicator_color2     DarkOrange
#property indicator_style2     STYLE_DOT
//#property indicator_minimum    0
//#property indicator_maximum    100
//#property indicator_level1     80
//#property indicator_level2     20
#property indicator_levelcolor DarkSlateGray

//
//
//
//
//

extern int  KPeriod        =  14;
extern int  Slowing        =  1;
extern int  SlowingMode    =  MODE_SMA;
extern int  DSmoothing     =  1;
extern int  DSmoothingMode =  MODE_SMA;
extern bool HighLow        = true;
extern int  calcVersion    = 0;

//
//
//
//
//

double MainBuffer[];
double SignalBuffer[];
double HighesBuffer[];
double LowesBuffer[];
double MainBuffera[];


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0, MainBuffer);   SetIndexLabel(0,"%K");
   SetIndexBuffer(1, SignalBuffer); SetIndexLabel(1,"%D");
   SetIndexBuffer(2, HighesBuffer);
   SetIndexBuffer(3, LowesBuffer);
   SetIndexBuffer(4, MainBuffera);

   //
   //
   //
   //
   //
   
   Slowing        = MathMax(Slowing,1);
   DSmoothing     = MathMax(DSmoothing,1);
   SlowingMode    = MathMax(MathMin(SlowingMode,3),0);
   DSmoothingMode = MathMax(MathMin(DSmoothingMode,3),0);
   
   string speed = "Full";
      switch (Slowing)
      {
         case 0:
         case 1: speed = "Fast"; break;
         case 3: speed = "Slow"; break;
      }         
   IndicatorShortName(speed+" stochastic ("+KPeriod+","+Slowing+","+MaName(SlowingMode)+","+DSmoothing+","+MaName(DSmoothingMode)+")");
   return(0);
}
int deinit() { return(0); }



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
   for(i = limit; i >= 0 ; i--)
   {
      if (HighLow) { LowesBuffer[i] = Low[  ArrayMinimum(Low  ,KPeriod,i)]; HighesBuffer[i]=High[ ArrayMaximum(High ,KPeriod,i)]; }
      else         { LowesBuffer[i] = Close[ArrayMinimum(Close,KPeriod,i)]; HighesBuffer[i]=Close[ArrayMaximum(Close,KPeriod,i)]; }
      
      //
      //
      //
      //
      //

      if (LowesBuffer[i] != HighesBuffer[i])
         switch (calcVersion)
         {
            case 0  : MainBuffera[i] = 100.00*(Close[i]-LowesBuffer[i]) /(HighesBuffer[i]-LowesBuffer[i]); break;
            case 1  : MainBuffera[i] = 100.00*(HighesBuffer[i]-Close[i])/(HighesBuffer[i]-LowesBuffer[i]); break;
            default : MainBuffera[i] = 100.00*(HighesBuffer[i]-Close[i])/(LowesBuffer[i]-HighesBuffer[i]);
         }            
      else  MainBuffera[i] = 0.00;
   }
   for(i=0; i<limit; i++) MainBuffer[i] = iMAOnArray(MainBuffera,0,Slowing   ,0,SlowingMode   ,i);
   for(i=0; i<limit; i++) SignalBuffer[i]=iMAOnArray(MainBuffer ,0,DSmoothing,0,DSmoothingMode,i);
   
   //
   //
   //
   //
   //
   
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

string MaName(int MaMethod)
{
   string name="";
   switch(MaMethod)
   {
      case 0 :  name="sma";  break;
      case 1 :  name="ema";  break;
      case 2 :  name="smma"; break;
      case 3 :  name="lwma";
   }
   return(name);
}   

Comments