SelfAdjustRSI_v1

Download script
Author: Copyright � 2006, Forex-TSD.com
SelfAdjustRSI_v1

The Self-Adjusting RSI is a sophisticated technical analysis tool designed to measure the momentum of an asset's price while dynamically adapting to market volatility. While it is based on the traditional Relative Strength Index (RSI), it introduces unique mathematical logic to provide more nuanced insights into "overbought" and "oversold" conditions.

Core Logic Overview

The script functions by breaking down price movement into two distinct components: positive momentum and negative momentum. Instead of simply looking at the price change between two points, it calculates how much the price moved upward versus how much it dropped over a specific timeframe.

1. Momentum Calculation

Depending on your settings, the tool processes these gains and losses in one of two ways:

  • Smoothed Mode: It uses a rolling average to "smooth" the data. This prevents the indicator from reacting too violently to minor, insignificant price flickers, resulting in a steadier line that represents the general trend.
  • Classic Mode: It performs a direct calculation of the gains and losses over the lookback period, offering a more immediate reflection of recent price action.

2. The Relative Strength Index (RSI)

Once the positive and negative momentum are established, the tool calculates the RSI. This value typically fluctuates between 0 and 100. A higher value indicates that upward momentum is dominating, while a lower value suggests that downward pressure is taking control.

3. Dynamic Adjustment (The "Self-Adjusting" Element)

This is where the script differs from a standard RSI. Instead of using fixed horizontal lines (like the common 70 and 30 levels) to identify overbought or oversold territory, this tool calculates Standard Deviation.

  • Volatility Awareness: The script looks at how much the RSI itself has been fluctuating recently.
  • Dynamic Boundaries: It creates two moving boundaries based on that volatility. If the market is very volatile, these boundaries expand; if the market is calm, they contract.
  • Overbought/Oversold Zones: The "Overbought" and "Oversold" lines are automatically drawn based on this calculation. This means the indicator "self-adjusts" to the current state of the market—telling you what a significant price move looks like right now, rather than relying on a static rule that might not apply to the current trading environment.

Summary for the User

By using this logic, the indicator attempts to filter out "market noise." It identifies when an asset is stretched too far in one direction by comparing current momentum against its own historical behavior, providing a more responsive and context-aware visual guide for potential trend reversals.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
6 Views
0 Downloads
0 Favorites
SelfAdjustRSI_v1
//+------------------------------------------------------------------+
//|                                              SelfAjustRSI_v1.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                       Author IgorAD by idea of David Sepiashvili |   
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |                                      
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1  30
#property indicator_level2  70
#property indicator_level3  50
#property indicator_buffers 3
#property indicator_color1 DodgerBlue
#property indicator_color2 Lime
#property indicator_color3 Lime
//---- input parameters
extern int Price        = 0; // O-Close; 1-Open; 2-High; 3-Low; 4-Median; 5-Typical; 6-Weighted 
extern int RSIPeriod    =14; // Period of RSI
extern double K         = 1; // Deviation ratio
extern int Mode         = 0; // RSI mode : 0 - typical(smoothed by SMMA); 1- clssic (smoothed by SMA)
//---- buffers
double RSIBuffer[];
double OBBuffer[];
double OSBuffer[];
double PosBuffer[];
double NegBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
   SetIndexBuffer(3,PosBuffer);
   SetIndexBuffer(4,NegBuffer);
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,RSIBuffer);
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,OBBuffer);
   
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,OSBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="SelfAjustRSI("+RSIPeriod+","+DoubleToStr(K,2)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexLabel(1,"Overbought");
   SetIndexLabel(2,"OverSold");
//----
   SetIndexDrawBegin(0,RSIPeriod);
   SetIndexDrawBegin(1,RSIPeriod);
   SetIndexDrawBegin(2,RSIPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Self-Adjusting Relative Strength Index by David Sepiashvili      |
//+------------------------------------------------------------------+
int start()
  {
   int    i,limit,counted_bars=IndicatorCounted();
   double rel,negative,positive;
//----
   if(Bars<=RSIPeriod) return(0);
//---- initial zero
   //if(counted_bars<1)
   //   for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0;
//----
   if ( counted_bars > 0 )  limit=Bars-counted_bars;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-RSIPeriod-1; 
      
   for(i=limit;i>=0;i--) 
   {	
      double sumn=0.0,sump=0.0;
      if(i==Bars-RSIPeriod-1)
       {
         int k=Bars-2;
         //---- initial accumulation
         while(k>=i)
           {
            rel=iMA(NULL,0,1,0,MODE_SMA,Price,k)-iMA(NULL,0,1,0,MODE_SMA,Price,k+1);
            if(rel>0) sump+=rel;
            else      sumn-=rel;
            k--;
           }
         positive=sump/RSIPeriod;
         negative=sumn/RSIPeriod;
        }
      else
        {
         //---- smoothed moving average
         if (Mode == 0)
         {
         rel=iMA(NULL,0,1,0,MODE_SMA,Price,i)-iMA(NULL,0,1,0,MODE_SMA,Price,i+1);
         if(rel>0) sump=rel;
         else      sumn=-rel;
                 
         positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
         negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
         }
         else
         if (Mode == 1)
         {
          sumn=0.0;sump=0.0;
          for ( k=RSIPeriod-1;k>=0;k--)
           { 
            rel=iMA(NULL,0,1,0,MODE_SMA,Price,i+k)-iMA(NULL,0,1,0,MODE_SMA,Price,i+k+1);
            if(rel>0) sump+=rel;
            else      sumn-=rel;
           }
         
         
         positive=sump/RSIPeriod;
         negative=sumn/RSIPeriod;
         }
        }
      PosBuffer[i]=positive;
      NegBuffer[i]=negative;
      if(negative==0.0) RSIBuffer[i]=100.0;
      else RSIBuffer[i]=100.0-100.0/(1+positive/negative);
  //  }
  // 
  // for(int j=limit;j>=0;j--) 
  // {	
      double SumRSI = 0; 
	   for ( k=RSIPeriod-1;k>=0;k--) SumRSI += RSIBuffer[i+k];
      double AvgRSI = SumRSI/RSIPeriod;
		
	   double SumSqr = 0;
	   for ( k=RSIPeriod-1;k>=0;k--) SumSqr += (RSIBuffer[i+k] - AvgRSI) * (RSIBuffer[i+k] - AvgRSI);
	   double StdDev = MathPow(SumSqr/RSIPeriod,0.5);
   
      OBBuffer[i] = 50 + K * StdDev;
      OSBuffer[i] = 50 - K * StdDev;
      
   }    
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments