// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("TS RevMACD on Price");
_MACD_     = 1;
_MACD_HLC  = 2;
_MACD_BB   = 3;
_MACD_MTF  = 4;

//-------------------------
//-- Created by KelvinHand
//-------------------------
ColorDeepSkyBlue = ColorRGB(65,105,225);

function _PMACDeq (Price, period_X,period_Y)
/*
{ TASC January 2012 }
{ Reversing MACD by Johnny Dough }
{ returns price where MACD is equal to previous bar 
  MACD }
	
inputs:
	Price( numericseries ),
	period_X( numericsimple ),
	period_Y( numericsimple ) ;

variables:
*/
{
	alphaX = 2 / ( 1 + period_X ) ;
	alphaY = 2 / ( 1 + period_Y ) ;
	
  return (( EMA( Price, period_X ) * alphaX -  EMA( Price, period_Y ) * alphaY ) /( alphaX - alphaY )) ;
}

function _PMACDlevel (level,Price,period_X, period_Y)
/*
{ TASC January 2012 }
{ Reversing MACD by Johnny Dough }

inputs:
	level( numericsimple ),
	Price( numericseries ),
	period_X( numericsimple ),
	period_Y( numericsimple ) ;
*/
{
//variables:
 alphaX = 2 / ( 1 + period_X ) ;
 alphaY = 2 / ( 1 + period_Y ) ;

 One_alphaX = 1 - alphaX ;
 One_alphaY = 1 - alphaY ;

 return ((Level + EMA(Price, period_Y)* One_alphaY - EMA(Price, period_X)* One_alphaX ) / ( alphaX - alphaY )) ;

}

function _PMACDzero(Price, period_X, period_Y)
{
/*{ TASC January 2012 }
{ Reversing MACD by Johnny Dough }

inputs:
	Price( numericseries ),
	period_X( numericsimple ),
	period_Y( numericsimple ) ;
*/	
 return _PMACDlevel( 0, Price, period_X, period_Y ) ;
}

/*
_PMACD_Ind1 (Indicator for Price Sub-Graph)
{ TASC January 2012 }
{ Reversing MACD by Johnny Dough }

inputs:
*/
switch ( ParamList("PlotTemplate", "MACD|MACD_HLC|MACD_BB|MACD_MTF") )
{
 case "MACD_HLC": PlotTemplate=_MACD_HLC; break;
 case "MACD_BB":  PlotTemplate=_MACD_BB; break;
 case "MACD_MTF": PlotTemplate=_MACD_MTF; break;

 default: PlotTemplate=_MACD_; break;
 
}


switch(ParamList("Price used", "Close|Open|High|Low|MP(HL/2)|TP(HLC/3)|WP(HLCC/4)"))
{

  case "Open": iPrice = Open; break; 
  case "High": iPrice = High; break; 
  case "Low":  iPrice = Low; break; 

  case "MP(HL/2)":  iPrice = (H+L)/2; break; 
  case "TP(HLC/3)": iPrice = (H+L+C)/2; break; 
  case "WP(HLCC/4)": iPrice = (H+L+C+C)/2; break; 
  default: iPrice = Close; break; 
     
}

P_Fast= Param("Fast periods", 12, 2,  200);
P_Slow= Param("Slow periods", 26, 2,  200);
P_Signal= Param("Signal periods", 9, 2,  200); //{ PMACDeq EMA Signal length } 

PMACD_TF_Mult1 = Param("Multiplier for Higher TF1", 5,  1,  100);  
PMACD_TF_Mult2 = Param("Multiplier for Higher TF2", 21, 1,  100);  

p_bbperiod = Param("BB average length", 10, 2,  200); 
p_bbwidth  = Param("BB # of StdDev", 1, 1,  200); 
Displace   = Param("displacement", 1, 0,  200); 
	
//variables:
PMACDeq = Null;
PMACDeqSignal= Null;
PMACDeq_TF1 = Null;
PMACDeq_TF2 = Null;
UpperBand = Null;
LowerBand = Null;

PMACDzero = _PMACDzero( iPrice, P_Fast, P_Slow ) ;

//{ Plots }
 if (PlotTemplate != _MACD_HLC)
 {	
	PMACDeq = _PMACDeq( iPrice, P_Fast, P_Slow ) ;
	//Plot1[Displace]( PMACDeq, "PMACDeq" ) ;
   Plot(PMACDeq, "PMACDeq", 
				ParamColor("PMACDeq Color",ColorDeepSkyBlue), 
          ParamStyle("PMACDeq Style",styleLine|styleNoLabel), 
					0, 0, Displace);  
 }

 //Plot2[Displace]( PMACDzero, "PMACDzero" ) ;
 Plot(PMACDzero, "PMACDzero", 
			ParamColor("PMACDzero Color",colorWhite), 
			ParamStyle("PMACDzero Style",styleLine|styleNoLabel), 
				0, 0, Displace);  

 if (PlotTemplate == _MACD_HLC)
 {
  Plot(_PMACDeq( Close, P_Fast, P_Slow ), "PMACD_C", 
     ParamColor("PMACD_C Color",colorTeal), 
		ParamStyle("PMACD_C Style",styleLine|styleNoLabel) 
    );


  Plot(_PMACDeq( High,  P_Fast, P_Slow ), "PMACD_H", 
     ParamColor("PMACD_H Color",colorDarkYellow), 
		ParamStyle("PMACD_H Style",styleLine|styleNoLabel)
    );

  Plot(_PMACDeq( Low,   P_Fast, P_Slow ), "PMACD L", 
     ParamColor("PMACD_L Color",colorDarkYellow), 
		ParamStyle("PMACD_L Style",styleLine|styleNoLabel) 
    );
 }	
	
 if (PlotTemplate == _MACD_)
 {
  PMACDeqSignal = EMA( PMACDeq, P_Signal ) ;
  Plot(PMACDeqSignal, "PMACDeqSig", 
			ParamColor("PMACDeqSig Color",colorAqua), 
       ParamStyle("PMACDeqSig Style",styleLine|styleNoLabel), 
    		0, 0, Displace);  
 }
	  
 if (PlotTemplate == _MACD_BB)
 {
 	UpperBand = BBandTop(PMACDeq, p_bbperiod, p_bbwidth );  
	LowerBand = BBandBot(PMACDeq, p_bbperiod, p_bbwidth );
  Plot(UpperBand, "UpperBand", 
			ParamColor("UpperBand Color",colorTeal), 
       ParamStyle("UpperBand Style",styleLine|styleNoLabel), 
    		0, 0, Displace);  
  Plot(LowerBand, "LowerBand", 
			ParamColor("LowerBand Color",colorTeal), 
       ParamStyle("LowerBand Style",styleLine|styleNoLabel), 
    		0, 0, Displace);  
 }	

 if (PlotTemplate == _MACD_MTF)
 {
	PMACDeq_TF1 = _PMACDeq( iPrice, P_Fast *  PMACD_TF_Mult1, P_Slow * PMACD_TF_Mult1 ) ;
	PMACDeq_TF2 = _PMACDeq( iPrice, P_Fast *  PMACD_TF_Mult2, P_Slow * PMACD_TF_Mult2 ) ;
  Plot(PMACDeq_TF1, "PMACDeqTF1", 
			ParamColor("PMACDeqTF1 Color",colorDarkYellow), 
       ParamStyle("PMACDeqTF1 Style",styleLine|styleNoLabel), 
    		0, 0, Displace);  
  Plot(PMACDeq_TF2, "PMACDeqTF2", 
			ParamColor("PMACDeqTF2 Color",colorDarkYellow), 
 
       ParamStyle("PMACDeqTF2 Style",styleLine|styleNoLabel), 
    		0, 0, Displace);  
 }	

_SECTION_END();