_StochNR

This script is a custom technical analysis tool designed to track and visualize price momentum in a separate window on a trading chart. It functions similarly to a "Stochastic Oscillator," which helps traders identify overbought or oversold conditions by comparing the current price relative to a specific range of historical data.

Core Functionality

The logic operates by calculating two distinct lines: a Main Line and a Signal Line.

1. The Main Line (Momentum Tracking) The tool examines a specific number of past candles (defined by the "Kperiod") to determine the highest highs and lowest lows. It then calculates where the current price sits within that range.

  • If the price is near the top of its recent range, the value moves toward 100.
  • If the price is near the bottom, the value moves toward 0. The script also includes a "Slowing" mechanism to smooth out these movements, preventing the line from reacting too erratically to minor price flickers.

2. The Signal Line (Trend Smoothing) While the Main Line tracks raw momentum, the Signal Line acts as a secondary moving average of that momentum. It is designed to filter out noise and provide a clearer picture of the trend direction. Users can choose between two types of smoothing for this line:

  • Simple Moving Average (SMA): A straightforward average that treats all recent data points equally.
  • Exponential Moving Average (EMA): A calculation that gives more weight to the most recent data, allowing the signal to react slightly faster to new price changes.

Key Technical Features

  • Dynamic Price Selection: The logic allows you to choose whether the calculations should be based on the "High and Low" prices of candles or the "Close" prices. This helps in tailoring the indicator to different trading styles (e.g., aggressive vs. conservative).
  • Sensitivity Thresholds: The script includes a sensitivity setting. If the difference between the highest and lowest prices in a range is too small, the tool automatically adjusts the calculation to prevent mathematical errors or "flat" lines during periods of extremely low volatility.
  • Visual Boundaries: The indicator is plotted with two fixed horizontal levels (80 and 20). In technical analysis, these are typically used as benchmarks: values above 80 may suggest a market is overextended to the upside, while values below 20 may suggest it is overextended to the downside.

Summary for the User

Essentially, this tool simplifies complex price action into a readable "pulse." By watching the relationship between the Main Line and the Signal Line, traders can look for crossovers or extreme levels to gauge when a market might be losing steam or gaining significant momentum.

Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
_StochNR
/*
âûçîâ èíäèêàòîðà èç äð.ïðîãðàìì
iCustom(string symbol, int timeframe, "_StochNR", int %Kperiod, int %Dperiod, 
int slowing, int method, int price_field, double sens, int mode, int shift);
iCustom(NULL,0,"_StochNR",Kperiod,Dperiod,Slowing,Dmethod,PriceFild,Sens, 0,i);
*/

#property indicator_separate_window // â îòä. îêíå 
#property indicator_buffers 2
#property indicator_color1 LightSeaGreen // ãëàâíàÿ
#property indicator_style1 0
#property indicator_color2 Red // ñèãíàëüíàÿ
#property indicator_style2 2
#property indicator_maximum 100
#property indicator_minimum 0
#property indicator_level1 80
#property indicator_level2 20

// âõîäíûå ïàðàìåòðû
extern int Kperiod=5; // %K
extern int Dperiod=3; // %D ñèãíàëüíàÿ
extern int Slowing=3; // çàìåäëåíèå
extern int Dmethod=0; // òèï MA ñèãíàëüíîé: 0-SMA, 1-EMA
extern int PriceFild=0; // òèï öåíû: 0-High/Low; 1-Close/Close
extern int Sens=0; // ÷óâñòâèòåëüíîñòü â ïï.
 int History=0; // èñòîðèÿ ïåðåñ÷åòà: 0 - âñå áàðû

// èíä.áóôåðû
double   Main[]; // ãëàâíàÿ
double   Signal[]; // ñèãíàëüíàÿ

// îáùèå ïåðåìåííûå
bool first=1; // ôëàã ïåðâîãî çàïóñêà
double sens; // ÷óâñòâèòåëüíîñòü â öåíàõ
double kd; // êîýôô. EMA äëÿ ñèãíàëüíîé

int init()
  {
   first=1;
   sens=Sens*Point; // ÷óâñòâèòåëüíîñòü â öåíàõ
   if(Dmethod==1) kd=2.0/(1+Dperiod); // êîýôô. EMA äëÿ ñèãíàëüíîé

   // áóôåð ãëàâíîé   
   SetIndexBuffer(0,Main);
   SetIndexStyle(0,DRAW_LINE);
   string _str="("+Kperiod+","+Slowing+")";
   SetIndexLabel(0,"Main"+_str);
   // áóôåð ñèãíàëüíîé
   SetIndexBuffer(1,Signal);
   SetIndexStyle(1,DRAW_LINE);
   _str="("+Dperiod+")";
   SetIndexLabel(1,"Signal"+_str);

   // êîðîòêîå èìÿ
   if(Sens!=0) string ShName=Sens+" ";
   ShName=ShName+"Stoch ";
   if(PriceFild==0) ShName=ShName+"H/L";
   else ShName=ShName+"C/C";
   ShName=ShName+" ("+Kperiod+","+Dperiod+","+Slowing+")";
   IndicatorShortName(ShName);   
   
   return(0);
  }

int reinit() // ô-ÿ äîïîëíèòåëüíîé èíèöèàëèçàöèè
  {
   ArrayInitialize(Main,0.0);
   ArrayInitialize(Signal,0.0);
   return(0);
  }

int start()
  {
   int ic=IndicatorCounted();
   if(!first && Bars-ic-1>1) ic=reinit(); first=0;
   int limit=Bars-ic-1; // êîë-âî ïåðåñ÷åòîâ
   if(History!=0 && limit>History) limit=History-1; // êîë-âî ïåðåñ÷åòîâ ïî èñòîðèè

   for(int i=limit; i>=0; i--) { // öèêë ïåðåñ÷åòà ïî ÂÑÅÌ áàðàì
      // ãëàâíàÿ
      Main[i]=Stoch(Kperiod, Slowing, PriceFild, sens, i);
      // ñèãíàëüíàÿ¸
      switch(Dmethod) {
         case 1: // EMA
            Signal[i]=kd*Main[i]+(1-kd)*Signal[i+1]; break;
         case 0: // SMA
            int sh=i+Dperiod;
            double ma=Signal[i+1]*Dperiod-Main[sh];
            Signal[i]=(ma+Main[i])/Dperiod;
        }
     }
   return(0);
  }

double Stoch(int Kperiod, int Slowing, int PriceFild, double sens, int i) {  
   // ýêñòðåìóìû öåíû
   double max,min,c;
   for(int j=i; j<i+Slowing; j++) {
      if(PriceFild==1) { // ïî Close
         max+=Close[ArrayMaximum(Close,Kperiod,j)];
         min+=Close[ArrayMinimum(Close,Kperiod,j)];
        }
      else { // ïî High/Low
         max+=High[ArrayMaximum(High,Kperiod,j)];
         min+=Low[ArrayMinimum(Low,Kperiod,j)];
        }
      c+=Close[j];
     }
   
   double delta=max-min;
   if(delta<sens) {
      double sens2=sens/2;
      max+=sens2; min-=sens2;
     }
   delta=max-min;
   if(delta==0) double s0=1;
   else s0=(c-min)/delta;

   return(100*s0);
  }

Comments