// Downloaded From https://www.WiseStockTrader.com
/// Var MA afl converted from mq4 file
/// created by Er. Gagandeep Singh email: bhatiags (at) gmail (dot) com
/// This file is under creatives common licence
// free to use and distribute, the only condition is to return the file if any modifications/ code changes are done.

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

// Decleration of Parameters
Weighting = Param("Weighting factor", 4, 1,15,0.1);
adxperiods = Param("adxperiods",  15, 4,80,0.1);
Ribbon_min = Param("Ribbon Min Period", 13, 8 , 21, 1);
Ribbon_max = Param("Ribbon Max Period", 55, 22 , 255, 1);
MA_Type = ParamList("Type", "Simple,Exponential,Double Exponential,Tripple Exponential,Wilders,Weighted");
RibbonDensity = Param ("Ribbon Density", 1, 1, 5,1);

// initializations
TR=0;
STR[0]=H[0] - L[0];
sPDI = sMDI =0;
DPDI = DMDI = 0;


			Hi  = H;
   			Hi1 = Ref(H,-1);
   			Lo  = L;
   			Lo1 = Ref(L,-1);
   			Close1= Ref(C,-1);
   			Bulls = 0.5*(abs(Hi-Hi1)+(Hi-Hi1));
   			Bears = 0.5*(abs(Lo1-Lo)+(Lo1-Lo));
			
			for( i = 0; i < BarCount; i++ )
				{
   					if( Bulls [ i ] > Bears[ i ] ) // CORRECT
       				{Bears [ i ] = 0;}
					else if (Bears [ i ] > Bulls[ i ] ) // CORRECT
       				{Bulls [ i ] = 0;}
					else if(Bears [ i ] == Bulls[ i ] ) // CORRECT
       				{Bulls [ i ] = 0;
						 Bears [ i ] = 0;	}
				}
   			sPDI = (Weighting*Ref(PDI(adxperiods), -1) + Bulls)/(Weighting+1);//ma weighting 
   			sMDI = (Weighting*Ref(MDI(adxperiods), -1) + Bears)/(Weighting+1);//ma weighting 
			
			TR = Max(Hi-Lo,Hi-Close1); 
   			STR  = (Weighting*Ref(STR,1) + TR)/(Weighting+1);//ma weighting 
			for( i = 0; i < BarCount; i++ )
				{
     				if(STR[i]>0 )
      					{
      						DPDI = sPDI[i]/STR[i];
      						DMDI = sMDI[i]/STR[i];
	 					}
            
   					if((DPDI[i] + DMDI[i]) > 0) 
   						DX[i] = abs(DPDI - DMDI)/(DPDI + DMDI); 
   					else DX[i] = 0;
				}
// initialization DADX
DADX = 0;				
					DADX = (Weighting*Ref(DADX, -1) + DX)/(Weighting+1);//ma weighting    
   					//vADX = DADX[i]; 

				   
    
// initialization of parameter   
ADXmin = 0;
ADXmax = -100;
VarMA[0] = C[0];
      ADXmin = Min(ADXmin,DADX);
      ADXmax = Max(ADXmax,DADX);
      Diff = ADXmax - ADXmin;

	for( i = 1; i < BarCount; i++ )
		{

   			if(Diff[i] > 0) Const[i] = (DADX[i]- ADXmin[i])/Diff[i]; else Const = 0;
		}
		   VarMA=((2-Const*Ref(VarMA, -1)+Const*Close));
	    
for ( i = Ribbon_min; i<=Ribbon_max;i= i+RibbonDensity)
	{
		if( MA_Type == "Simple" )
			Plot ( MA(VarMA,i),"",ColorHSB( 3*(i-10),Param("Saturation", 128, 0, 255 ), Param("Brightness", 255, 0, 255 ) ), styleNoLabel, Zorder = -1);
		else if ( MA_Type == "Exponential" )
			Plot ( EMA(VarMA,i),"",ColorHSB( 3*(i-10),Param("Saturation", 128, 0, 255 ), Param("Brightness", 255, 0, 255 ) ), styleNoLabel);
		else if ( MA_Type == "Double Exponential" )
			Plot ( DEMA(VarMA,i),"",ColorHSB( 3*(i-10),Param("Saturation", 128, 0, 255 ), Param("Brightness", 255, 0, 255 ) ), styleNoLabel);
		else if ( MA_Type == "Tripple Exponential" )
			Plot ( TEMA(VarMA,i),"",ColorHSB( 3*(i-10),Param("Saturation", 128, 0, 255 ), Param("Brightness", 255, 0, 255 ) ), styleNoLabel);
		else if ( MA_Type == "Wilders" )
			Plot ( Wilders(VarMA,i),"",ColorHSB( 3*(i-10),Param("Saturation", 128, 0, 255 ), Param("Brightness", 255, 0, 255 ) ), styleNoLabel);
		else if ( MA_Type == "Weighted" )
			Plot ( WMA(VarMA,i),"",ColorHSB( 3*(i-10),Param("Saturation", 128, 0, 255 ), Param("Brightness", 255, 0, 255 ) ), styleNoLabel);
	}
_SECTION_END();