Reversing MACD
Code :
// This is all-in-one formula for the price chart. // To use it, enter the formula in the AFL Editor, then press "Insert indicator." // To change chart, select parameters and select the value from the parameter option // "Plot Chart" _SECTION_BEGIN( "Price" ); SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle ); GraphXSpace = Param("Chart whitespace", 2, 2, 15, 1 ); _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " + WriteVal(V, 1.0) + " {{VALUES}}", O,H,L,C, SelectedValue(ROC(C,1)))); Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle(), Null, Null, 0, -1 ); _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_PRICE" ); 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, 0, 18, 1 ); // Overlay parameter option p_plot_shift = ParamToggle( "Plot computed price shift forward by 1", "NO|YES", 1 ); p_plot_chart = ParamList( "Plot chart", "NONE|PMACD|PMACD_HLC|PMACD_BB|PMACD_MTF", 1 ); p_plot_rescale = IIf( ParamToggle( "Plot rescale", "NO|YES", 0 ) == 0, styleNoRescale, 1 ); // BB values used in indicator panel p_bbperiod = 10; p_bbwidth = 1; // predefine the plotting styles used styleA = styleDots | styleThick | p_plot_rescale; styleB = styleLine | styleThick | p_plot_rescale; styleC = styleCloud | styleNoLabel | styleNoTitle | p_plot_rescale; styleD = styleArea | styleOwnScale | styleNoLabel; switch ( p_plot_chart ) { case "PMACD": Plot( PMACD_px = PMACDeq( p_px, p_fast, p_slow ), "PMACDeq" + _PARAM_VALUES(), colorBlue, styleA, Null, Null, p_plot_shift ); Plot( EMA( PMACD_px, p_signal ), "EMA(PMACDeq," + p_signal + ")", colorRed, styleB, Null, Null, p_plot_shift ); // By default (when p_plot_shift=1, plot is shifted by 1 to plot the next bar price // value of PMACDzero() Plot( PMACD_0 = PMACDzero( p_px, p_fast, p_slow ), "PMACDzero" + _PARAM_VALUES(), colorBlack, styleA, Null, Null, p_plot_shift ); // plot ribbon of PMACD_0 vs price selected Plot( 1, "", IIf(Ref(PMACD_0,-1) < p_px, colorBrightGreen, colorRed), styleD, 0, 100, 0, -2 ); break; case "PMACD_HLC": sbTitle = StrFormat( ",%g,%g)", p_fast, p_slow ); Plot( PMACD_H = PMACDeq( H, p_fast, p_slow ), "PMACDeq(H" + sbTitle, colorGreen, styleB, Null, Null, p_plot_shift ); Plot( PMACD_C = PMACDeq( C, p_fast, p_slow ), "PMACDeq(C" + sbTitle, colorBlue, styleA, Null, Null, p_plot_shift ); Plot( PMACD_L = PMACDeq( L, p_fast, p_slow ), "PMACDeq(L" + sbTitle, colorRed, styleB, Null, Null, p_plot_shift ); PlotOHLC( PMACD_H, PMACD_H, PMACD_L, PMACD_L, "", colorLightGrey, styleC, Null, Null, p_plot_shift, -1 ); Plot( PMACD_0 = PMACDzero( p_px, p_fast, p_slow ), "PMACDzero" + _PARAM_VALUES(), colorBlack, styleA, Null, Null, p_plot_shift ); Plot( 1, "", IIf(Ref(PMACD_0, -1) < p_px, colorBrightGreen, colorRed), styleD, 0, 100, 0, -2 ); break; case "PMACD_BB": Plot( PMACD_px = PMACDeq( p_px, p_fast, p_slow ), "PMACDeq" + _PARAM_VALUES(), colorBlue, styleA, Null, Null, p_plot_shift ); Plot( PMACD_BT = BBandTop( PMACD_px, p_bbperiod, p_bbwidth ), "BBTop", colorGreen, styleB, Null, Null, p_plot_shift ); Plot( PMACD_BB = BBandBot( PMACD_px, p_bbperiod, p_bbwidth ), "BBBot", colorRed, styleB, Null, Null, p_plot_shift ); PlotOHLC( PMACD_BT, PMACD_BT, PMACD_BB, PMACD_BB, "", colorLightGrey, styleC, Null, Null, p_plot_shift, -2 ); Plot( PMACD_0 = PMACDzero( p_px, p_fast, p_slow ), "PMACDzero" + _PARAM_VALUES(), colorBlack, styleA, Null, Null, p_plot_shift ); Plot( 1, "", IIf(Ref(PMACD_0, -1) < p_px, colorBrightGreen, colorRed), styleD, 0, 100, 0, -2 ); break; case "PMACD_MTF": Plot( PMACD_px = PMACDeq( p_px, p_fast, p_slow ), "PMACDeq" + _PARAM_VALUES(), colorBlue, styleA, Null, Null, p_plot_shift ); Plot( PMACD_0 = PMACDzero( p_px, p_fast, p_slow ), "PMACDzero" + _PARAM_VALUES(), colorBlack, styleA, Null, Null, p_plot_shift ); X = 5; Plot( PMACDeq( p_px, X * p_fast, X * p_slow ), NumToStr( X, 0 ) + "*PMACDeq" + _PARAM_VALUES(), colorGreen, styleA, Null, Null, p_plot_shift ); X = 21; Plot( PMACDeq( p_px, X * p_fast, X * p_slow ), NumToStr( X, 0 ) + "*PMACDeq" + _PARAM_VALUES(), colorRed, styleA, Null, Null, p_plot_shift ); Plot( 1, "", IIf(Ref(PMACD_0,-1) < p_px, colorBrightGreen, colorRed), styleD, 0, 100, 0, -2 ); break; } _SECTION_END();

No comments:
Post a Comment