Zig-zag with signals-auto trend lines-sound alerts Amibroker AFL



_SECTION_BEGIN("Price");
weekdays= WriteIf(DayOfWeek() == 1, "Thu Hai", WriteIf(DayOfWeek() == 2, "Thu Ba", WriteIf(DayOfWeek() == 3, "Thu Tu", WriteIf(DayOfWeek() == 4, "Thu Nam", WriteIf(DayOfWeek() == 5, "Thu Sau", " ")))));
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = EncodeColor(colorYellow) + StrFormat("{{NAME}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) + EncodeColor(colorWhite) + StrFormat("_Open %g, Hi %g, Lo %g, Close %g (%.1f%%)", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) + EncodeColor(colorWhite) + "_Volume " + WriteVal(V,1.0) + EncodeColor(colorYellow) + " _ " + WEEKDAYS + " " + Date() + "\n" + EncodeColor(colorWhite) + FullName());
;
UpClose = Close - Ref(Close,-1);
Color = IIf(UpClose > 0, colorBrightGreen, colorRed);
PlotOHLC(Open,High,Low,Close,"", Color, styleCandle);
PlotOHLC(Open,High,Low,Close, "Close", ParamColor("Color", styleCandle), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

function PlotGradientArea( array, caption, ColorTop, ColorBottom )
{
 bkclr = GetChartBkColor();

 HH = HighestVisibleValue( array );
 if( NOT IsNull( hh ) ) SetChartBkGradientFill( ColorTop, ColorBottom, bkclr, Null, HH );
 Plot( array, Caption, ColorBlend( ColorBottom, colorBlack ) );
 PlotOHLC( HH, HH, array, HH, "", bkclr, styleNoLabel | styleNoTitle | styleCloud, Null, Null, 0, -10 );
}

_SECTION_BEGIN("Zig-zag with buy and sell signals-auto trend lines");


//////////////////////////////////////////////////////////////////////////////////////////


//z_ZigZagValid
// ******** CHARTING
per=Param ("per1", 1,0.1,50,0.10);

PercentChange = per;
mystartbar = SelectedValue(BarIndex()); // FOR GRAPHING

mystartbardate = LastValue(ValueWhen(mystartbar == BarIndex(), DateNum(),1));

InitialValue = LastValue(ValueWhen(mystartbardate == DateNum(), C , 1 ) ) ;
Temp1 = IIf(BarIndex() >= mystartbar, InitialValue, Null) ;
Plot(Temp1, " ", colorBlack,styleLine);
Plot((1+(LastValue(PercentChange)/100))*(Temp1), " ", colorGreen, styleLine) ;
Plot((1-(LastValue(PercentChange)/100))*(Temp1), " ", colorRed, styleLine) ;

ZZ = Zig(C,LastValue(PercentChange)) ;
PivotLow = Ref(IIf(Ref(ROC(ZZ,1),-1) < 0 AND ROC(ZZ,1) > 0, 1, Null),1);
PivotHigh = Ref(IIf(Ref(ROC(ZZ,1),-1) > 0 AND ROC(ZZ,1) < 0, 1, Null),1);



Buy_Valid = IIf(C>(1+(LastValue(PercentChange)/100))*(ValueWhen(PivotLow, C,
1))
AND ROC(ZZ,1) > 0,1,0);
Sell_Valid = IIf(C<(1-(LastValue(PercentChange)/100))*(ValueWhen(PivotHigh, C,
1))
AND ROC(ZZ,1) < 0,1,0);

Buy_Valid = ExRem(Buy_Valid,Sell_Valid);
Sell_Valid = ExRem(Sell_Valid,Buy_Valid);

PlotShapes( shapeUpArrow*Buy_Valid, colorGreen,0, L, -20);
PlotShapes( shapeDownArrow*Sell_Valid, colorRed,0,H, -20) ;

BarColors =
IIf(BarsSince(Buy_Valid) < BarsSince(Sell_Valid)
AND BarsSince(Buy_Valid)!=0, colorGreen,
IIf(BarsSince(Sell_Valid) < BarsSince(Buy_Valid)
AND BarsSince(Sell_Valid)!=0, colorRed, colorBlue));

Plot(C, " ", BarColors, styleCandle ) ;
Plot(ZZ," ", colorLightGrey,styleLine|styleThick);
Plot(ZZ," ", BarColors,styleDots|styleNoLine);

Title = Name() + " " + Date() + WriteIf(PivotLow, " Up Pivot
","")+WriteIf(PivotHigh," Down Pivot ","")+ WriteIf(Buy_Valid, " Buy Point ",
"") + WriteIf(Sell_Valid, " Sell Point ", "") ;


AlertIf( Buy_Valid, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 2 );
AlertIf( Sell_Valid,"SOUND C:\\Windows\\Media\\Windows XP Critical Stop.wav", "Audio alert", 2 );


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

x = Cum(1);
s1=C;
s11=C;
pS = TroughBars( s1, per, 1 ) == 0;
endt= SelectedValue(ValueWhen( pS, x, 1 ));
startt=SelectedValue(ValueWhen( pS, x, 2 ));
dtS =endt-startt;
endS = SelectedValue(ValueWhen( pS, s1, 1 ) );
startS = SelectedValue( ValueWhen( pS, s1, 2 ));
aS = (endS-startS)/dtS;
bS = endS;
trendlineS = aS * ( x -endt ) + bS;
g3= IIf(x>startt-10,trendlineS,-1e10);
Plot(g3,"",colorRed,styleThick);

pR = PeakBars( s11, per, 1 ) == 0;
endt1= SelectedValue(ValueWhen( pR, x, 1 ));
startt1=SelectedValue(ValueWhen( pR, x, 2 ));
dtR =endt1-startt1;
endR = SelectedValue(ValueWhen( pR, s11, 1 ) );
startR = SelectedValue( ValueWhen( pR, s11, 2 ));
aR = (endR-startR)/dtR;
bR = endR;
trendlineR = aR * ( x -endt1 ) + bR;
g4= IIf(x>startT1-10,trendlineR,-1e10);
Plot(g4,"",colorGreen,styleThick);

//////////////////////////////////////////////////////////////////////////////////////
_SECTION_END();

1 nhận xét: