Accumulative swing index smoothed

Download script
Author: © mladen, 2018

This script functions as a sophisticated technical analysis tool designed to track and smooth out significant price movements in financial markets. Its primary goal is to identify the underlying trend of "swings" while filtering out minor market noise.

Core Logic Overview

The indicator operates by analyzing several different dimensions of price action simultaneously—specifically looking at how high and low prices interact with previous closing values. Instead of just following the current price blindly, it calculates a specialized value based on:

  • Range Analysis: It measures the distance between the current bar's extremes (high/low) and the previous close.
  • Volatility Adjustment: It accounts for the "True Range" of the movement, subtracting certain components to focus on actual directional shifts rather than just high-frequency fluctuations.
  • Momentum Weighting: It incorporates the difference between opening and closing prices to determine the strength of the move.

Data Processing Pipeline

The script processes this data through a multi-stage pipeline to ensure the resulting visual line is useful for human traders:

  1. Swing Index Calculation: It first calculates a "Swing Index," which represents the raw intensity of price changes relative to the established volatility.
  2. Adaptive Smoothing: The logic employs a custom smoothing algorithm. Unlike a simple moving average (which can be laggy), this system uses mathematical power functions and phase shifts to adjust its sensitivity dynamically. It tries to maintain a balance between being responsive enough to catch new trends quickly and smooth enough to ignore "jitter."
  3. Accumulative Summation: The script takes these smoothed values and accumulates them over time. This transforms the data into an "Accumulative Swing Index."

Visual Representation

On your trading chart, this logic produces a continuous line that changes color based on the direction of the trend:

  • Upward Trend: When the accumulated index is rising, the line typically shifts to a lime green color.
  • Downward Trend: When the accumulated index is falling, the line shifts to a crimson red color.

Summary for the User

Essentially, this tool acts as a "trend filter." It looks at the market's volatility and price swings, strips away the unnecessary noise, smooths out the remaining data using a complex mathematical model, and then provides you with a clean, color-coded visual of whether the primary momentum is currently moving up or down.

Price Data Components
14 Views
0 Downloads
0 Favorites
Accumulative swing index smoothed
ÿþ//------------------------------------------------------------------

#property copyright   "© mladen, 2018"

#property link        "mladenfx@gmail.com"

#property version     "1.00"

//------------------------------------------------------------------

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_plots   1

#property indicator_label1  "Accumulative swing index"

#property indicator_type1   DRAW_COLOR_LINE

#property indicator_color1  clrLimeGreen,clrCrimson

#property indicator_width1  2

//

//---

//

input  double inpT            = 300.0; // T (maximum price changing)

input  int    inpSmoothLength = 14;    // Smoothing period

input  double inpSmoothPhase  = 0;     // Smoothing phase

//

//---

//

double asi[],asic[],si[];



//------------------------------------------------------------------

//

//------------------------------------------------------------------

//

//

//

//

//



int OnInit()

{

   SetIndexBuffer(0,asi,INDICATOR_DATA);

   SetIndexBuffer(1,asic,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,si ,INDICATOR_CALCULATIONS);

   IndicatorSetString(INDICATOR_SHORTNAME,"Accumulative swing index ("+(string)inpT+","+(string)inpSmoothLength+")");

   return(INIT_SUCCEEDED);

}

void OnDeinit(const int reason) { }



//------------------------------------------------------------------

//

//------------------------------------------------------------------

//

//

//

//

//



int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime& time[],

                const double& open[],

                const double& high[],

                const double& low[],

                const double& close[],

                const long& tick_volume[],

                const long& volume[],

                const int& spread[])

{

   if (Bars(_Symbol,_Period)<rates_total) return(-1);

   double _tpoints = inpT*_Point;

   int i=(int)MathMax(prev_calculated-1,0); for (; i<rates_total  && !_StopFlag; i++)

   {

      double TR = (i>0) ? MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]) : high[i]-low[i];

      double ER = (i>0) ? (close[i-1]>=low[i] && close[i-1]<=high[i]) ? 0 : (close[i-1]>high[i]) ? MathAbs(high[i]-close[i-1]) : (close[i-1]<low[i]) ? MathAbs(low[i]-close[i-1]) : 0 : 0;

      double K  = (i>0) ? MathMax(MathAbs(high[i]-close[i-1]), MathAbs(low[i]-close[i-1])) : 0;

      double SH = (i>0) ? MathAbs(close[i-1]-open[i-1]) : 0;

      double R  = TR - 0.5*ER + 0.25*SH;

      double SI = (R==0) ? 0 : (i>0) ? (50*(close[i]-close[i-1]+0.5*(close[i]-open[i])+0.25*(close[i-1]-open[i-1]))*(K/_tpoints)/R) : 0;

         si[i]  = iSmooth(SI,inpSmoothLength,inpSmoothPhase,i,rates_total,0);

         asi[i] = iSmooth((i>0) ? asi[i-1]+si[i] : 0,inpSmoothLength,inpSmoothPhase,i,rates_total,1);

         asic[i] = (i>0) ? asi[i]>asi[i-1] ? 0 :  asi[i]<asi[i-1] ? 1 : asic[i-1] : 0;

   }      

   return(i);

}



//+------------------------------------------------------------------+

//| Custom functions                                                 |

//+------------------------------------------------------------------+

#define _smoothInstances     2

#define _smoothInstancesSize 10

double m_wrk[][_smoothInstances*_smoothInstancesSize];

int    m_size=0;

//

//---

//

double iSmooth(double price,double length,double phase,int r,int bars,int instanceNo=0)

  {

   #define bsmax  5

   #define bsmin  6

   #define volty  7

   #define vsum   8

   #define avolty 9



   if(ArrayRange(m_wrk,0)!=bars) ArrayResize(m_wrk,bars); if(ArrayRange(m_wrk,0)!=bars) return(price); instanceNo*=_smoothInstancesSize;

   if(r==0 || length<=1) { int k=0; for(; k<7; k++) m_wrk[r][instanceNo+k]=price; for(; k<10; k++) m_wrk[r][instanceNo+k]=0; return(price); }



//

//---

//



   double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);

   double pow1   = MathMax(len1-2.0,0.5);

   double del1   = price - m_wrk[r-1][instanceNo+bsmax];

   double del2   = price - m_wrk[r-1][instanceNo+bsmin];

   int    forBar = MathMin(r,10);



   m_wrk[r][instanceNo+volty]=0;

   if(MathAbs(del1) > MathAbs(del2)) m_wrk[r][instanceNo+volty] = MathAbs(del1);

   if(MathAbs(del1) < MathAbs(del2)) m_wrk[r][instanceNo+volty] = MathAbs(del2);

   m_wrk[r][instanceNo+vsum]=m_wrk[r-1][instanceNo+vsum]+(m_wrk[r][instanceNo+volty]-m_wrk[r-forBar][instanceNo+volty])*0.1;



//

//---

//



   m_wrk[r][instanceNo+avolty]=m_wrk[r-1][instanceNo+avolty]+(2.0/(MathMax(4.0*length,30)+1.0))*(m_wrk[r][instanceNo+vsum]-m_wrk[r-1][instanceNo+avolty]);

   double dVolty=(m_wrk[r][instanceNo+avolty]>0) ? m_wrk[r][instanceNo+volty]/m_wrk[r][instanceNo+avolty]: 0;

   if(dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);

   if(dVolty < 1)                      dVolty = 1.0;



//

//---

//



   double pow2 = MathPow(dVolty, pow1);

   double len2 = MathSqrt(0.5*(length-1))*len1;

   double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));



   if(del1 > 0) m_wrk[r][instanceNo+bsmax] = price; else m_wrk[r][instanceNo+bsmax] = price - Kv*del1;

   if(del2 < 0) m_wrk[r][instanceNo+bsmin] = price; else m_wrk[r][instanceNo+bsmin] = price - Kv*del2;



//

//---

//



   double corr  = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;

   double beta  = 0.45*(length-1)/(0.45*(length-1)+2);

   double alpha = MathPow(beta,pow2);



   m_wrk[r][instanceNo+0] = price + alpha*(m_wrk[r-1][instanceNo+0]-price);

   m_wrk[r][instanceNo+1] = (price - m_wrk[r][instanceNo+0])*(1-beta) + beta*m_wrk[r-1][instanceNo+1];

   m_wrk[r][instanceNo+2] = (m_wrk[r][instanceNo+0] + corr*m_wrk[r][instanceNo+1]);

   m_wrk[r][instanceNo+3] = (m_wrk[r][instanceNo+2] - m_wrk[r-1][instanceNo+4])*MathPow((1-alpha),2) + MathPow(alpha,2)*m_wrk[r-1][instanceNo+3];

   m_wrk[r][instanceNo+4] = (m_wrk[r-1][instanceNo+4] + m_wrk[r][instanceNo+3]);



//

//---

//



   return(m_wrk[r][instanceNo+4]);



   #undef bsmax

   #undef bsmin

   #undef volty

   #undef vsum

   #undef avolty

  }



Comments