// Downloaded From https://www.WiseStockTrader.com
// This is all-in-one formula for the indicator panel.
// To use it, enter the formula in the AFL Editor, then press "Insert indicator."
// To change indicator, select parameters and select the value from the parameter option
// "Plot Indicator"
//
_SECTION_BEGIN( "Indicator selection" );
SetChartOptions( 0, chartWrapTitle );
GraphXSpace = Param("Chart whitespace", 10, 2, 15, 1 );
p_plot_indicator = ParamList( "Plot indicator", "MACD|MACD_HLC|MACD_BB|MACD_MTF", 0 );
_SECTION_END();

_SECTION_BEGIN( "PMACD functions" );
// General MACD function
function MACDa( price, fast, slow )
{
    return EMA( price, fast ) - EMA( price, slow );
}

// returns price where MACD is equal to previous bar MACD
// note that PMACDeq() computes the next bar value, so
// - it should be plotted by shifting 1 bar forward
// - or reference 1 bar back when comparing to price
function PMACDeq( price, period_X, period_Y )
{
    alphaX = 2. / ( 1. + period_X );
    alphaY = 2. / ( 1. + period_Y );
    return (EMA(price,period_X)*alphaX - EMA(price,period_Y)*alphaY)/(alphaX - alphaY);
}


// returns price where MACD is equal to level value
// e.g. PMACDlevel(0, C, 12, 16) would return the series
//      where next price would make MACD=0
//
// note that PMACDLevel() computes the next bar value, so
// - it should be plotted by shifting 1 bar forward
// - or reference 1 bar back when comparing to price
function PMACDlevel( level, price, period_X, period_Y )
{
    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 );
}

// for simplicity, case where level=0 in PMACDlevel()
function PMACDzero( price, period_X, period_Y )
{
    return PMACDlevel( 0, price, period_X, period_Y );
}
_SECTION_END();

_SECTION_BEGIN( "MACD" );
// Plot MACD
p_px = ParamField( "Price field", 3 ) ;
p_fast = Param( "Period fast", 12, 1, 24, 1 );
p_slow = Param( "Period slow", 26, 2, 52, 1 );
p_signal  = Param( "Period signal", 9, 1, 18, 1 );

if ( p_plot_indicator == "MACD" ) {
    Plot( md = MACDa( p_px, p_fast, p_slow ), _DEFAULT_NAME(), colorBlue, styleDots |
          styleLine | styleThick );
    PlotShapes( shapeSmallCircle, colorBlue, 0, md, 0 );

    Plot( sl = EMA( md, p_signal ), StrFormat( "Signal(%g)", p_signal ), colorRed,
          styleLine | styleThick );
    Plot( md - sl, "MACD Histogram", colorBlack, styleNoTitle | styleHistogram |
          styleNoLabel );

    // Use for title only - the next price required for MACD to be the same
    Plot( PMACDeq( p_px, p_fast, p_slow ), "Next_PMACDeq" + _PARAM_VALUES() + " from "
          + SelectedValue( p_px ), colorBlue, styleNoDraw | styleOwnScale );

    // Use for title only - the next price required for MACD to across zero
    Plot( PMACDzero( p_px, p_fast, p_slow ), "Next_PMACDzero" + _PARAM_VALUES(),
          colorBlack, styleNoDraw | styleOwnScale );
}
_SECTION_END();

_SECTION_BEGIN( "MACD_HLC" );
// Plot MACD of the High, Low and Close
p_fast = Param( "period fast", 12, 1, 24, 1 );
p_slow = Param( "period slow", 26, 2, 52, 1 );

if ( p_plot_indicator == "MACD_HLC" )
{
    Plot( 0, "", colorBlack, styleLine | styleNoLabel );

    Plot( md = MACDa( H, p_fast, p_slow ), StrFormat( "MACD(H,%g,%g)", p_fast, p_slow ),
          colorGreen, styleDots | styleLine | styleThick );
    // mark each point with a PlotShapes() circle that is larger than default Plot() styleDots
    PlotShapes( shapeSmallCircle, colorGreen, 0, md, 0 );

    Plot( md = MACDa( C, p_fast, p_slow ), StrFormat( "MACD(C,%g,%g)", p_fast, p_slow ),
          colorBlue, styleDots | styleLine | styleThick );
    PlotShapes( shapeSmallCircle, colorBlue, 0, md, 0 );

    Plot( md = MACDa( L, p_fast, p_slow ), StrFormat( "MACD(L,%g,%g)", p_fast, p_slow ),
          colorRed, styleDots | styleLine | styleThick );
    PlotShapes( shapeSmallCircle, colorRed, 0, md, 0 );
}
_SECTION_END();

_SECTION_BEGIN( "MACD_BB" );
// Plot MACD with BB
p_px = ParamField( "Price field", 3 );
p_fast = Param( "period fast", 12, 1, 24, 1 );
p_slow = Param( "period slow", 26, 2, 52, 1 );
p_signal  = Param( "period signal", 9, 1, 18, 1 );
p_bbperiod  = Param( "period bb", 10, 2, 20, 1 );
p_bbwidth = 1;

if ( p_plot_indicator == "MACD_BB" ) {
    md = MACDa( p_px, p_fast, p_slow );
    md1 = Ref( md, -1 );
    mdsig = EMA( md, p_signal );
    BBtop = BBandTop( md, p_bbperiod, p_bbwidth );
    BBbot = BBandBot( md, p_bbperiod, p_bbwidth );
    Hist = md - mdsig;

    Color = IIf( md<0 AND md>md1, colorLime, IIf( md > 0 AND md > md1, colorBrightGreen,
                 IIf( md > 0 AND md < md1, colorOrange, colorRed ) ) );
    Plot( md, _DEFAULT_NAME(), color, styleDots | styleLine );
    PlotShapes( shapeSmallCircle, color, 0, md, 0 );

    // show the MACD zero line
    Plot( 0, "", IIf( md > 0, colorIndigo, colorRed ), styleLine | styleThick |
          styleNoLabel );
    // show the MACD histo as a ribbon on the bottom of the panel
    Plot( 3, "", IIf( Hist > 0, colorIndigo, colorRed ), styleArea | styleOwnScale |
          styleNoLabel, 0, 100, 0, -2 );

    // Plot the BB
    Plot( BBtop, "BBtop", colorGreen, styleDashed | styleNoRescale );
    Plot( BBbot, "BBbot", colorRed, styleDashed |  styleNoRescale );
    PlotOHLC( BBtop, BBtop, BBbot, BBbot, "BB",  colorLightGrey, styleCloud |
              styleNoLabel | styleNoTitle | styleNoRescale, Null, Null, 0, -3 );
    Plot( (BBtop + BBBot)/2, "", colorBlack, styleLine | styleNoRescale, styleNoLabel );
}
_SECTION_END();

_SECTION_BEGIN( "MACD_MTF" );
// Plot MACD_MTF
p_px = ParamField( "Price field", 3 );
p_fast = Param( "period fast", 12, 1, 24, 1 );
p_slow = Param( "period slow", 26, 2, 52, 1 );
p_signal  = Param( "period signal", 9, 1, 18, 1 );

if ( p_plot_indicator == "MACD_MTF" ) {
    Plot( md = MACDa( p_px, p_fast, p_slow ), _DEFAULT_NAME(), colorBlue, styleDots |
          styleLine | styleThick );
    PlotShapes( shapeSmallCircle, colorBlue, 0, md, 0 );

    Plot( 0, "", colorBlack, styleLine );

    // Use for title only - the next price required for MACD to be the same
    Plot( PMACDeq( p_px, p_fast, p_slow ), "Next_PMACDeq" + _PARAM_VALUES() + " from "
          + SelectedValue( p_px ), colorBlue, styleNoDraw | styleOwnScale );

    // Use for title only - the next price required for MACD to across zero
    Plot( PMACDzero( p_px, p_fast, p_slow ), "Next_PMACDzero" + _PARAM_VALUES(),
          colorBlack, styleNoDraw | styleOwnScale );

    // plot higher period MACD lines
    X = 5;  Plot( MACDa( p_px, X * p_fast, X * p_slow ), NumToStr( X, 0 ) + "*"
                  + _DEFAULT_NAME(), colorGreen, styleDots | styleLine | styleThick );
    X = 21; Plot( MACDa( p_px, X * p_fast, X * p_slow ), NumToStr( X, 0 ) + "*"
                  + _DEFAULT_NAME(), colorRed, styleDots | styleLine | styleThick );
}
_SECTION_END();