Trend Detection Afl - 1 for amibroker
"TrendChart";
//Coding by rmike
p=Param("RSI Periods", 14, 2, 100,1);
A=EMA(C,9);
A1=WMA(C,45);
B=EMA(RSI(p),9);
B1=WMA(RSI(p),45);
TU=A>A1 AND B>B1;
TD=A<=A1 AND B<=B1;
TSU=A>A1 AND B<=B1;
TSD=A<=A1 AND B>B1;
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )
+ EncodeColor(colorYellow) + "\nTrend Interpretation - " + EncodeColor(colorGreen) + WriteIf(TU,"TREND UP","") + EncodeColor(colorRed) + WriteIf(TD,"TREND DOWN","" )
+ EncodeColor(colorPaleGreen) + WriteIf(TSU,"TREND SIDEWAYS TO UP","" ) + EncodeColor(colorPink) + WriteIf(TSD,"TREND SIDEWAYS TO DOWN","" ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
if( ParamToggle("Chart Type", "Trend Bacground|Trend Ribbon" ) )
{
Plot( 2, "", IIf( TU, colorGreen, IIf( TD, colorRed, IIf(TSU, colorLime, IIf(TSD, colorPink, Null )))), styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
}
else
{
Plot( 1, "", IIf( TU, colorGreen, IIf( TD, colorRed, IIf(TSU, colorLime, IIf(TSD, colorPink, Null )))), styleOwnScale|styleArea|styleNoLabel, 0, 1 );
}
CrossB=Cross(EMA(C,5),EMA(O,6));
CrossS=Cross(EMA(O,6),EMA(C,5));
shape = CrossB * shapeUpArrow + CrossS * shapeDownArrow;
_SECTION_BEGIN("tboly varázs afl");
SetChartOptions(0, chartShowArrows | chartShowDates);
// **************** INITIAL CONDITIONS:
bi = BarIndex() - 1;
// ************************************** ADAPTIVE PERIODS ***********************
// **************** BASE PERIODS
tf = Param("tf", 1, 1, 500, 1);
bp = tf * Param("bp", 4, 2, 500, 1);
// ************************************************************
HiHi = HHV(H, bp);
HiRH = Ref(HiHi, - 1);
LoLw = LLV(L, bp);
LoRL = Ref(LoLw, - 1); // long exit
//Plot( HiHi, "H", colorCustom12 , 1);
//Plot( LoLw, "L", colorCustom9 , 1);
//Plot( Lorl, "L", colorBlue , 32);
//Plot( Hirh, "H", colorRed , 32);
bs = BarsSince(H == HiHi); // Promt
bl = BarsSince(L == LoLw); // Promt
LtHL = ValueWhen(bs > bl, HiHi);
StLH = ValueWhen(bs < bl, LoLw);
BarLthl = BarsSince(bs < bl AND Hirh == Lthl);
BarStLH = BarsSince(bs > bl AND Lorl == Stlh);
PlotShapes(IIf(bs > bl AND Ref(bs, - 1) < Ref(bl, - 1), shapeSmallDownTriangle, 0), colorRed, layer = 0, yposition = H, offset = - 10);
PlotShapes(IIf(bs < bl AND Ref(bs, - 1) > Ref(bl, - 1), shapeSmallUpTriangle, 0), colorBlue, layer = 0, yposition = L, offset = - 10);
BarloSl = IIf(BarStLH > BarLthl, BarLthl + 1, 0);
BarshSl = IIf(BarStLH < BarLthl, Barstlh + 1, 0);
LoLL = LLV(L, bl + 1);
//Plot( IIf( bl > 0 , LoLL, Null) , "", colorBlue, 1 );
LoH = HHV(H, bl + 1);
Plot(IIf(bl > 0, LoH, Null), "", colorCustom9, 1); // Range sign
ShHH = HHV(H, bs + 1);
//Plot( IIf( bs > 0 , ShHH, Null) , "", colorRed, 1);
ShL = LLV(L, bs + 1);
Plot(IIf(bs > 0, ShL, Null), "", colorCustom12, 1); // Range sign
// ************************ NOISE ********************************
//Noise= MA( HHV(H,bp/6)-LLV(L,bp/6) ,bp);
// ********************** NOISE HL1 *******************************
np = Param("np", 2, 2, 500, 1);
NoHL = H - L;
NoHLMa = MA(Nohl, np);
mpHL1 = NoHL / NoHLMa;
// ********************** NOISE HL2 *******************************
Noise = HHV(H, 2 *tf) - LLV(L, 2 *tf);
NoiseMa = MA(Noise, np);
momHL = NoHL / NoiseMa;
momLo = momHL >= 1 AND C > O;
momSh = momHL >= 1 AND C < O;
momlb = 1100; //Param("momlb",100,2,2500,1);
momst = Sum(IIf(momHL >= 1, 1, 0), momlb) / momlb * 100;
//PlotShapes(IIf(momlo,shapeSmallCircle,0),colorBlue, layer = 0, yposition= H, offset = 15 );
//PlotShapes(IIf(momsh,shapeSmallCircle,0),colorRed, layer = 0, yposition =L, offset = - 15 );
mpnma = Noise / NoiseMa;
mpn = Param("mpnoise", 0.85, 0, 3, 0.025);
HLVlsl2 = LLV(L + noisema * mpn *(1+log10(bs + 1)), bs + 1);
Plot(IIf(bs > 0, Ref(Min(Hlvlsl2, shhh), - 1), Null), "Sh", colorBlue, 1); // Range sign + noise
//Plot( Ref(Min(Hlvlsl2,shhh),-1) , "Sh", colorBlue, 1); // Range sign + noise
Plot(IIf(bs > 0, (Ref(Min(Hlvlsl2, shhh), - 1) + shl) / 2, Null), "Shf", colorBlue, 32);
HLVlsl = HHV(H - noisema * mpn *(1+log10(bl + 1)), bl + 1);
Plot(IIf(bl > 0, Ref(Max(HLvlsl, Loll), - 1), Null), "Lo", colorRed, 1); // Range sign-noise
//Plot( Ref(Max(HLvlsl,Loll),-1) , "Lo", colorRed, 1); // Range sign-noise
Plot(IIf(bl > 0, (Ref(Max(HLvlsl, Loll), - 1) + Loh) / 2, Null), "Lo", colorRed, 32);
// ******************** HL2 NOISE BASE LG ****************************************
RHAb = (ShHH - LoLL);
//Plot( (H+L)/2+Ref(MA(H-L,bp),-1)/2,"",colorBlack,1);
//Plot( (H+L)/2-Ref(MA(H-L,bp),-1)/2,"",colorBlack,1);
dHLNoise = IIf((bs > 0 AND Ref(Hlvlsl2, - 1) < Ref(shhh, - 1))AND(bl > 0 AND Ref(HLvlsl, - 1) > Ref(Loll, - 1)),
(Ref(Min(Hlvlsl2, shhh), - 1) - Ref(Max(HLvlsl, Loll), - 1)) / (noisema *mpn), 0);
// **********************************************************************************************************
// **************************** BUY/SELL CONDITIONS ******************************************************
/*
Buy = bl>0 AND L< Ref(Max(HLvlsl,Loll),-1) AND H >Ref(Max(HLvlsl,Loll),-1) ;
//BuyPrice= LFLong ;
Sell= bs>0 AND H> Ref(Min(Hlvlsl2,shhh),-1) AND L< Ref(Min(Hlvlsl2,shhh),-1) ;
//SellPrice= LFShort ;
// **************** FLIP CONDITIONS
//Buy = ExRem(Buy,Sell);
//Sell = ExRem(Sell,Buy);
PlotShapes(IIf(Buy,shapeUpArrow,0),colorBlue, layer = 0, yposition= Low, offset = - 10 );
//PlotShapes(IIf(Sell,shapeHollowDownArrow,0),colorRed, layer = 0, yposition =High, offset = - 25 );
// **************************** SHORT/SELL CONDITIONS ******************************************************
Short=Sell;
Cover=Buy;
PlotShapes(IIf(Short,shapeDownArrow,0),colorRed, layer = 0, yposition =High, offset = - 10 );
//PlotShapes(IIf(Cover ,shapeHollowUpArrow,0),colorBlue, layer = 0, yposition= Low, offset = - 25 );
*/
// ************************** PLOT CANDLE ***********************
/*
Plot( Close, "",
IIf( (L > LFLong AND LFLongL < LFLongS) , colorBlue,
IIf( (H < LFShort AND !LFShortL < LFShortS) , colorRed,
//IIf( LFLongL < LFLongS AND !LFShortL < LFShortS , colorBlack,
//AND LFShortL < LFShortS AND !LFLongL < LFLongS
colorBlack)) , styleCandle );
//OR (LFShort> LFLong AND L>LFShort)
//OR (LFShort> LFLong AND H
*/
// ************************** PLOT RIBBON
Plot(1, "",
IIf(bl < bs, colorRed,
IIf(bl > bs, colorBlue,
54)),
styleOwnScale | styleArea | styleNoLabel, - 0.5, 100);
/*
Plot( 2, "",
IIf( bl > 0 , colorBlue,
54),
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
*/
// *************************** TITLE ****************************
Title =
StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}",
O, H, L, C, SelectedValue(ROC(C, 1)))
+ " \\c29 \np = \\c-1" + WriteVal(bp, 1) + " \\c29 Barindex = \\c-1" + WriteVal(bi, 1)
+ " \\c32 \nMom = \\c-1" + WriteVal(MomHL, 1.2) + " \\c32 Mom% = \\c-1" + WriteVal(Momst, 1.2)
+ " \\c32 \nNoise = \\c-1" + WriteVal(noise, 1.4) + " \\c32 Noisema = \\c-1" + WriteVal(noisema, 1.4)
+ " \\c32 mpNoise = \\c-1" + WriteVal(mpnma, 1.2)
+ " \\c29 \nNoiHL = \\c-1" + WriteVal(noHL, 1.4) + " \\c29 NoiHLma = \\c-1" + WriteVal(noHLma, 1.4)
+ " \\c29 mpNoiHL = \\c-1" + WriteVal(mpHL1, 1.2)
+ " \\c29 \nL = \\c-1" + WriteVal(bl, 1) + " \\c29 Lbp = \\c-1" + WriteVal(bl / bp, 1.1)
+ " \\c32 \nS = \\c-1" + WriteVal(bs, 1) + " \\c32 Sbp = \\c-1" + WriteVal(bs / bp, 1.1)
+ " \\c29 \nBarlong = \\c-1" + WriteVal(barlthl, 1.0) + " \\c32 BarShort = \\c-1" + WriteVal(barstlh, 1.0)
+ " \\c32 BarTrRange = \\c-1" + WriteVal(bl - bs, 1.0)
+ " \\c32 \nRHAb = \\c-1" + WriteVal(RHAb, 1.2) + " \\c32 R%Top = \\c-1" + WriteVal( - RHAb / ShHH * 100, 1.2)
+ " \\c29 R%Bot = \\c-1" + WriteVal(RHAb / LoLL * 100, 1.2) + " \\c29 \nRelLoH = \\c-1" + WriteVal(IIf(bl > 0, (H / Ref(L, - bl) *100-100) / (bl + 1), H / L * 100-100), 1.2)
//+ " \\c29 RelLoL = \\c-1" + WriteVal(IIf( bl>0, (L/Ref(L,-bl)*100-100)/(bl+1),0 ) ,1.2) + " \\c29 Lbase = \\c-1"+WriteVal( slg,1.2)
+ " \\c29 RelLoRew = \\c-1" + WriteVal(IIf(bl > 0, ((L / Ref(L, - bl) *100-100) / (bl + 1)) / ((H / Ref(L, - bl) *100-100) / (bl + 1)) *100-100, 0), 1.2)
+ " \\c32 \nRelShH = \\c-1" + WriteVal(IIf(bs > 0, (L / Ref(H, - bs) *100-100) / (bs + 1), L / H * 100-100), 1.2)
+ " \\c32 RelShH = \\c-1" + WriteVal(IIf(bs > 0, (H / Ref(H, - bs) *100-100) / (bs + 1), 0), 1.2)
+ " \\c32 RelShRew = \\c-1" + WriteVal(IIf(bs > 0, ((H / Ref(H, - bs) *100-100) / (bs + 1)) / ((L / Ref(H, - bs) *100-100) / (bs + 1)) *100-100, 0), 1.2)
;
_SECTION_END();
Plot(6, "", IIf(PDI(7) > MDI(7) AND PDI(7) > 25,colorGreen,IIf (MDI(7) > PDI(7) AND MDI(7) > 25,colorRed,colorYellow)), styleOwnScale| styleArea|styleNoLabel,-0.5,100);
_SECTION_BEGIN("Fibo for all bars");
TimeFrameSet( in15Minute*2 );
DH=Ref(H,-1);
DL=Ref(L,-1);
DC=Ref(C,-1);
pd = (O+ DH+ DL + DC )/4;
sd1 = (2*pd)-DH;
sd2 = pd -(DH - DL);
sd3 = Sd1 - (DH-DL);
rd1 = (2*pd)-DL;
rd2 = pd +(DH -DL);
rd3 = rd1 +(DH-DL);
//Plot (pd,"Pivot",colorBlue,styleDots);
//Plot (rd1," R1 ",35,styleDots);
//Plot (rd2," R2 ",35,styleDots);
//Plot (rd3," R3 ",35,styleDots);
//Plot (sd1," S1 ",4,styleDots);
//Plot (Sd2," S2 ",4,styleDots);
//Plot (Sd3," S3 ",4,8+16);
/*
style = IIf(ParamList("Chart style", "styleCandle|styleBar")=="styleCandle",64,128+4);
Plot (C,Date ()+" close",1,style); //ENABLE THIS TO HAVE CANDLES AS WELL
*/
TimeFrameRestore();
Title = EncodeColor(colorWhite)+ "" + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "
//+ WriteIf(Col_action==colorGreen, EncodeColor(colorGreen)+"stay LONG","")+ WriteIf(Col_action==colorRed, EncodeColor(colorRed)+"stay SHORT","")+ WriteIf(Col_action==colorBlack, EncodeColor(colorYellow)+"No Trend","")+"\n"
+ "Vol= "+ WriteVal(V) +WriteIf ( V > MA(V,26) ,EncodeColor(colorGreen)+" UP "+ (V/MA(V,26))*100 + " %", EncodeColor(colorRed)+" DOWN "+ (V/MA(V,26))*100 + " %")
+ EncodeColor(colorGreen)+ "\n R3 : "+ EncodeColor(colorWhite)+RD3
+ EncodeColor(colorGreen)+ "\n R2 : "+ EncodeColor(colorWhite)+RD2
+ EncodeColor(colorGreen)+ "\n R1 : "+ EncodeColor(colorWhite)+RD1+ EncodeColor(colorGreen)+ "` Hi: "+ EncodeColor(colorWhite)+H
+ EncodeColor(colorBlue)+ "\n Pivot : "+ EncodeColor(colorWhite)+pd + EncodeColor(colorYellow)+ "` Op: "+ EncodeColor(colorWhite)+O+ EncodeColor(colorAqua)+ "Cl: "+ EncodeColor(colorBrightGreen)+C
+ EncodeColor(colorRed)+ "\n S1 : "+ EncodeColor(colorWhite)+SD1+ EncodeColor(colorRed)+ "` Lo: "+ EncodeColor(colorWhite)+L
+ EncodeColor(colorRed)+ "\n S2 : "+ EncodeColor(colorWhite)+SD2
+ EncodeColor(colorRed)+ "\n S3 : "+ EncodeColor(colorWhite)+SD3
;
_SECTION_END();
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", IIf(MACD(3,34) > Signal(3,34,9),colorGreen, colorRed), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("Fibo for all bars");
TimeFrameSet( in15Minute*2 );
DH=Ref(H,-1);
DL=Ref(L,-1);
DC=Ref(C,-1);
pd = (O+ DH+ DL + DC )/4;
sd1 = (2*pd)-DH;
sd2 = pd -(DH - DL);
sd3 = Sd1 - (DH-DL);
rd1 = (2*pd)-DL;
rd2 = pd +(DH -DL);
rd3 = rd1 +(DH-DL);
//Plot (pd,"Pivot",colorBlue,styleDots);
//Plot (rd1," R1 ",35,styleDots);
//Plot (rd2," R2 ",35,styleDots);
//Plot (rd3," R3 ",35,styleDots);
//Plot (sd1," S1 ",4,styleDots);
//Plot (Sd2," S2 ",4,styleDots);
//Plot (Sd3," S3 ",4,8+16);
/*
style = IIf(ParamList("Chart style", "styleCandle|styleBar")=="styleCandle",64,128+4);
Plot (C,Date ()+" close",1,style); //ENABLE THIS TO HAVE CANDLES AS WELL
*/
TimeFrameRestore();
Title = EncodeColor(colorWhite)+ "" + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "
//+ WriteIf(Col_action==colorGreen, EncodeColor(colorGreen)+"stay LONG","")+ WriteIf(Col_action==colorRed, EncodeColor(colorRed)+"stay SHORT","")+ WriteIf(Col_action==colorBlack, EncodeColor(colorYellow)+"No Trend","")+"\n"
+ "Vol= "+ WriteVal(V) +WriteIf ( V > MA(V,26) ,EncodeColor(colorGreen)+" UP "+ (V/MA(V,26))*100 + " %", EncodeColor(colorRed)+" DOWN "+ (V/MA(V,26))*100 + " %")
+ EncodeColor(colorGreen)+ "\n R3 : "+ EncodeColor(colorWhite)+RD3
+ EncodeColor(colorGreen)+ "\n R2 : "+ EncodeColor(colorWhite)+RD2
+ EncodeColor(colorGreen)+ "\n R1 : "+ EncodeColor(colorWhite)+RD1+ EncodeColor(colorGreen)+ "` Hi: "+ EncodeColor(colorWhite)+H
+ EncodeColor(colorBlue)+ "\n Pivot : "+ EncodeColor(colorWhite)+pd + EncodeColor(colorYellow)+ "` Op: "+ EncodeColor(colorWhite)+O+ EncodeColor(colorAqua)+ "Cl: "+ EncodeColor(colorBrightGreen)+C
+ EncodeColor(colorRed)+ "\n S1 : "+ EncodeColor(colorWhite)+SD1+ EncodeColor(colorRed)+ "` Lo: "+ EncodeColor(colorWhite)+L
+ EncodeColor(colorRed)+ "\n S2 : "+ EncodeColor(colorWhite)+SD2
+ EncodeColor(colorRed)+ "\n S3 : "+ EncodeColor(colorWhite)+SD3
;
_SECTION_END();
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleCandle
| ParamStyle("Style") | GetPriceStyle() );
// Paste the code below to your price chart somewhere and green ribbon means both
// both MACD and ADX trending up so if the red ribbon shows up the MACD and the ADX
// are both trending down.
_SECTION_BEGIN("trending ribbon");
uptrend=PDI()>MDI()AND Signal()
downtrend=MDI()>PDI()AND Signal()>MACD();
Plot( 2, /* defines the height of the ribbon in percent of pane width */"ribbon",
IIf( uptrend, colorGreen, IIf( downtrend, colorRed, 0 )), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
_SECTION_END();
_SECTION_END();
_SECTION_BEGIN("LEMA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 20, 2, 200, 1, 10 );
lema = EMA (Close,Periods)+ EMA((Close-EMA(Close,Periods)),Periods);
Plot( lEMA, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
ParamStyle("Style") );
_SECTION_END();
Title = EncodeColor(colorPink)+ "Trend Detection " + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorYellow) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n";