// Downloaded From https://www.WiseStockTrader.com
//1st AFL - Swing tread with BUY & Sell signal AFL 

_SECTION_BEGIN("Price");
SetChartOptions(0,ShowArrows|ShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

SD = StochD(8, 3, 3);
MH = MACD(8, 21) - Signal(8, 21, 5);

trendup = IIf(MH > 0 OR (MH > 0 AND MH > Ref(MH, -1)) AND RSI(3) >50 AND SD < 80 AND SD > Ref(SD, -1) AND ValueWhen(C,O<C), colorGreen, colorWhite);
trendcolor = IIf(MH < 0 OR (MH < 0 AND MH < Ref(MH, -1)) AND RSI(3) <50 AND SD > 20 AND SD < Ref(SD, -1) AND ValueWhen(C,O>C), colorRed, trendup);
Plot( C, "Close", trendcolor, styleCandle | styleThick );

_SECTION_END();

no=Param( "Swing", 8, 1, 55 );

res=HHV(H,no);
sup=LLV(L,no);
tsl=IIf(ValueWhen(IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0))!=0,IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0)),1)==1,sup,res);

Plot(tsl, _DEFAULT_NAME(), colorPink, styleLine);
Buy = Cross(C,res) ;
Sell = Cross(sup,C)  ;
_SECTION_END();

a=C;
g=(EMA(Close,3) * (2 / 4 - 1)-EMA(Close,5) * (2 / 6 - 1)) / (2 /4- 2 /6);
e=Ref(tsl,-1);
Buy = Cross(C,tsl) ;
Sell = Cross(tsl,C)  ;
SellPrice=ValueWhen(Sell,e,1);
BuyPrice=ValueWhen(Buy,e,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );
Filter=Buy OR Sell;
Buy = Cross(C,tsl) ;
Sell = Cross(tsl,C)  ;
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

PlotShapes( shape, IIf( Buy, colorYellow, colorRed ),0, IIf( Buy, Low, High ) );
a1=Ref(tsl,-1);
GraphXSpace = 5;
dist = 2*ATR(5); 
for( i = 0; i < BarCount; i++ ) 
{if( Buy[i] ) PlotText( "Buy @" + a1[ i ], i, L[ i ]-dist[i], colorGreen ); 
if( Sell[i] ) PlotText( "Sell @" + a1[ i ], i, H[ i ]+dist[i], colorRed); 
} 


GraphXSpace = 5;
dist = 2.5*ATR(5); 
for( i = 0; i < BarCount; i++ ) 
{if( Buy[i] ) PlotText( "Stop @" + tsl[ i ], i, L[ i ]-dist[i], colorRed ); 
if( Sell[i] ) PlotText( "Stop @" + tsl[ i ], i, H[ i ]+dist[i], colorGreen); 
}




// **** 2nd  MACD AFL

//Indicator McGinley MACD
// Public release for personal use only.

_SECTION_BEGIN("McGinley MACD - V.1.0");
SetChartOptions(0,ShowArrows|ShowDates);
Output[0] = C[0];
for( i = 1; i < BarCount; i++ )
{
output[ i ] = output[ i - 1 ] + (C[i]-Output[i-1])/( 12*(C[i] / Output[i-1])^4)  ;
}


Outputa[0] = C[0];

for( i = 1; i < BarCount; i++ )
{
outputa[ i ] = outputa[ i - 1 ] + (C[i]-Outputa[i-1])/( 26*(C[i]  / Outputa[i-1])^4)  ;
}


MDM = Output-Outputa;

Plot(mdm,"MD MACD",colorLime,1| styleThick);
Plot(MA(mdm,9),"Md Signal",colorRed,1| styleThick);
Hist =mdm-MA(mdm,9);
Plot(Hist,"",IIf(Hist>0,colorLime,colorRed),2| styleThick|styleOwnScale);
_SECTION_END(); 
_SECTION_BEGIN("Title");
if( Status("action") == actionIndicator ) 
(
Title = EncodeColor(colorWhite)+ "McGinley MACD- V.1.0 " + " - " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2)+
WriteIf(MDM > MA(mdm,9), EncodeColor(colorLime) + "  MD MACD = ", EncodeColor(colorRed)  + "  MD MACD")+ WriteVal(MDM));

_SECTION_END();