TTTB or T3B trading System

#13
this is great information, thank you so much for that, just curious if you have code for 3/6/9 month volume high/low and 2/3/4/5 x volumes spikes as well.
 
#14
Not tried but ,see an image and link.
Maybe paid as it comes with an exe.(sometimes dangerous to install unknown exe's on your pc)




http://www.stoc kmania cs.net/blog/1-2-3-blast-our-super-tttb-amibroker-afl-library-code-to-find-blasting-stocks/
(please remove spaces)

Moderators

Please remove this post if found objectionable.
This exe file doesn't give any AFL please test and revert back.
 
#16
After Thorough Research and finding on Internet i have found Original AFL for TTTB system. It is exactly Same what is shown on Original Creator Site. Moreover i have matched certain trades with him. it is OK

Now i want to learn how to use this chart? if any body knows in detail pls share
I know when a breakout above the peak or below the trough happens that is a good buy or sell opportunity but what is the role of KRA ...i want to know. ..How is the KRA related to decision making..........

Anybody who knows come on ........we together can make history
 

laotze

Active Member
#17
After Thorough Research and finding on Internet i have found Original AFL for TTTB system. It is exactly Same what is shown on Original Creator Site. Moreover i have matched certain trades with him. it is OK

Now i want to learn how to use this chart? if any body knows in detail pls share
I know when a breakout above the peak or below the trough happens that is a good buy or sell opportunity but what is the role of KRA ...i want to know. ..How is the KRA related to decision making..........

Anybody who knows come on ........we together can make history
Post image of LIC Housing breakout......i will tell you what is KRA ?
 

KelvinHand

Well-Known Member
#18
After Thorough Research and finding on Internet i have found Original AFL for TTTB system. It is exactly Same what is shown on Original Creator Site. Moreover i have matched certain trades with him. it is OK

Now i want to learn how to use this chart? if any body knows in detail pls share
I know when a breakout above the peak or below the trough happens that is a good buy or sell opportunity but what is the role of KRA ...i want to know. ..How is the KRA related to decision making..........

Anybody who knows come on ........we together can make history
Originally in metastock, Keane of T3B use the 2 lines MACD(12,26,9). Later he move to chartnexus, he changed the MACD a bit faster by using 1 line MACD(11,25), to hide the macd and make it as KRA.

So you know the role of MACD, then you know KRA. so you can actually use then macd(12,26,9).
 
#19
mhm mine afl seems to give same .attaching todays nifty pic i have named this afl santoshji afl ..yup
original afl is of sanjoshji i modified it and made it cooler
just ignore message box look when it breaks out

and it;s Free :)

Code:
equty = ParamToggle("Equity","Hide|Show",1);
aama = ParamToggle("Plot Ema ","Off|On",1);
stoploss = ParamToggle("Plot stoploss ","Off|On",1);
font = ParamToggle("font","Show|Hide",1);
KeltnerBands = ParamToggle("Keltner Bands","Show|Hide",1);
SR = ParamToggle("SR","Hide|Show",0);
kandi =  ParamToggle("kandi","Hide|Show",1);





if(font==0)
{
_SECTION_BEGIN("Name");
GfxSetOverlayMode(0);
GfxSelectFont("Tahoma", Status("pxheight")/8 );
GfxSetTextAlign( 6 );// center alignment
GfxSetTextColor( ColorHSB( 42, 42, 42 ) );
GfxSetBkMode(0); // transparent
GfxTextOut( Name(), Status("pxwidth")/2, Status("pxheight")/12 );
GfxSelectFont("Tahoma", Status("pxheight")/18 );
GfxTextOut("" , Status("pxwidth")/2, Status("pxheight")/4 );
GfxSelectFont("Tahoma", Status("pxheight")/18 );
GfxSelectFont("Tahoma", Status("pxheight")/36 );
GfxTextOut( "", Status("pxwidth")/2, Status("pxheight")/3 );
_SECTION_END();

//Magfied Market Price
GfxSetTextAlign( 6 );// center alignment
FS=Param("Font Size",72,11,100,1);
GfxSelectFont("Times New Roman", FS, 700, True ); 
GfxSetBkMode(0); // transparent 
GfxSetTextColor( ColorHSB( 42, 42, 42 ) );
Hor=Param("Horizonta Position",615,1,1200,1);
Ver=Param("Vertica Position",152,1,830,1); 
GfxTextOut(""+C, Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
GfxSetTextAlign( 6 );// center alignment
xx=Prec((DD/YC)*100,2);
FS2=Param("Font Size2",35,11,100,1);
GfxSelectFont("Times New Roman", FS2,11, 700, True ); 
GfxSetBkMode( colorBlack );
Hor1=Param("Horizontal Position",615,1,1200,1);
Ver1=Param("Vertical Position",188,1,830,1);   
GfxSetTextColor( ColorHSB( 42, 42, 42 ) ); 
GfxTextOut(""+DD+"  ("+xx+"%)", Hor1 , Ver1+45 );
}
else
{{
//Magfied Market Price
GfxSetTextAlign( 6 );// center alignment
fse=Param("Font Sizee",35,11,100,1);
GfxSelectFont("Times New Roman", fse, 700, True ); 
GfxSetBkMode( colorGold );  
GfxSetTextColor( ParamColor("Color",colorGold) ); 
Hora=Param("Horizontal Positiona",625,1,1200,1);
Vera=Param("Vertical Positiona",17,1,830,1); 
GfxTextOut(""+C, Hora , Vera );
YCa=TimeFrameGetPrice("C",inDaily,-1);
DDa=Prec(C-YCa,2);
GfxSetTextAlign( 6 );// center alignment
xxa=Prec((DDa/YCa)*100,2);
FSb=Param("Font Sizeb",16,11,100,1);
GfxSelectFont("Times New Roman",fsb, 700, True ); 
GfxSetBkMode( colorBlack );  
GfxSetTextColor(ParamColor("Color",colorYellow) ); 
GfxTextOut(""+DDa+"  ("+xxa+"%)", Hora , Vera+45 );
 _SECTION_END();
}}



_SECTION_BEGIN("TREND");


SetBarsRequired(100000,0);
GraphXSpace = 15;

ea = EMA (C,10);
eb = EMA (C,20);
SetBarFillColor( IIf( ea > eb, colorBlue, colorRed ) ); 
Plot( C, "Price", IIf( ea > eb, colorGreen, colorRed ), styleCandle );
Buy = ea > eb AND TimeNum() > 092000 AND TimeNum() < 150000;
Sell = eb > ea OR TimeNum() > 150000;
Short = 0;
Cover = 0;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);


SetTradeDelays(1,1,1,1);

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

Factor=Param("Factor",6,1,10,1);
Pd=Param("ATR Periods",6,1,100,1);
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount; i++) {
TrendUp[i] = Null;
TrendDown[i] = Null;

trend[i]=1;


if (Close[i]>Up[i-1]) {
trend[i]=1;
if (trend[i-1] == -1) changeOfTrend = 1;

}
else if (Close[i]<Dn[i-1]) {
trend[i]=-1;
if (trend[i-1] == 1) changeOfTrend = 1;
}
else if (trend[i-1]==1) {
trend[i]=1;
changeOfTrend = 0; 
}
else if (trend[i-1]==-1) {
trend[i]=-1;
changeOfTrend = 0;
}

if (trend[i]<0 && trend[i-1]>0) {
flag=1;
}
else {
flag=0;
}

if (trend[i]>0 && trend[i-1]<0) {
flagh=1;
}
else {
flagh=0;
}

if (trend[i]>0 && Dn[i]<Dn[i-1]){
Dn[i]=Dn[i-1];
}

if (trend[i]<0 && Up[i]>Up[i-1])
{ Up[i]=Up[i-1];
}

if (flag==1)
{ Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
} 
if (flagh==1)
{ Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
}
if (trend[i]==1) {
TrendUp[i]=Dn[i];
if (changeOfTrend == 1) {
TrendUp[i-1] = TrendDown[i-1];
changeOfTrend = 0;
}
}
else if (trend[i]==-1) {
TrendDown[i]=Up[i];
if (changeOfTrend == 1) {
TrendDown[i-1] = TrendUp[i-1];
changeOfTrend = 0;
}
}
} 

Plot(TrendUp,"Trend",colorGreen);
Plot(TrendDown,"Down",colorRed);

Buy = trend==1;
Sell=trend==-1;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=Sell;
Cover=Buy;


BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);


Title = EncodeColor(colorWhite)+ "win" + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+ 
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+" ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+" ","")+"\n"+EncodeColor(colorYellow)+
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","");

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); 
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); 
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

TrendSL=IIf(trend==1,TrendUp,TrendDown);

for(i=BarCount-1;i>1;i--)
{
if(Buy[i] == 1)
{
entry = C[i];
sig = "BUY";
sl = TrendSL[i];
tar1 = entry + (entry * .0050);
tar2 = entry + (entry * .0092);
tar3 = entry + (entry * .0179);

bars = i;
i = 0;
}
if(Sell[i] == 1)
{
sig = "SELL";
entry = C[i];
sl = TrendSL[i];
tar1 = entry - (entry * .0050);
tar2 = entry - (entry * .0112);
tar3 = entry - (entry * .0212);


bars = i;
i = 0;
}
}
Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, TrendSL[BarCount-1], Ref(TrendSL, -1));
sl = ssl[BarCount-1];


//Plot(LineArray(bars-Offset, tar1, BarCount, tar1,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
//Plot(LineArray(bars-Offset, tar2, BarCount, tar2,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
//Plot(LineArray(bars-Offset, tar3, BarCount, tar3,1), "", Clr, styleLine|styleDots, Null, Null, Offset);

//Plot(LineArray(bars-Offset, sl, BarCount, sl,1), "", colorDarkRed, styleLine|styleLine, Null, Null, Offset);
//Plot(LineArray(bars-Offset, entry, BarCount, entry,1), "", colorGreen, styleLine|styleLine, Null, Null, Offset);

for (i=bars; i <BarCount;i++)
{
PlotText(""+sig+"@"+entry, BarCount+1,entry,Null,colorBlue);
PlotText("T1@"+tar1,BarCount+3,tar1,Null,Clr);//PlotText("T2@"+tar2,BarCount+3,tar2,Null,Clr);Plot Text ("T3@"+tar3,BarCount+3,tar3,Null,Clr);

}

messageboard = ParamToggle("Message Board","Show|Hide",1);
if (messageboard == 1 )
{
GfxSelectFont( "Book Antiqua bold", 10, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );

if ( sig =="BUY")
{
GfxSelectSolidBrush( colorBlue ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 5;
x2 = 290;

y = pxHeight;

GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "TREND"),13,y-100);
GfxTextOut( (" "),27,y-100);
GfxTextOut( ("Last " + sig + " Signal came " + (BarCount-bars-1) * Interval()/60 + " mins ago"), 13, y-80) ; // The text format location
GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 13, y-60);
GfxTextOut( ("Trailing SL : " + TrendSL + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"), 13, y-40);
/*GfxTextOut( ("TGT:1 : " + tar1), 13, y -80);
GfxTextOut( ("TGT:2 : " + tar2), 13,y-60);
GfxTextOut( ("TGT:3 : " + tar3), 13,y-40);*/
GfxTextOut( ("Current P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)), 13, y-22);;

}

// TO ADD//

//Previous Days HI LO //

DayH = TimeFrameGetPrice("H", inDaily, -1);    DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);    DayLI = LastValue (DayL,1);   // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1);    // yesterdays close
DayO = TimeFrameGetPrice("O", inDaily);    // current day open
DayH2= TimeFrameGetPrice("H", inDaily, -2); DayH2I = LastValue (DayH2,1); // Two days before high
DayL2= TimeFrameGetPrice("L", inDaily, -2); DayL2I = LastValue (DayL2,1); // Two days before low
DayH3= TimeFrameGetPrice("H", inDaily, -3); DayH3I = LastValue (DayH3,1); // Three days before high
DayL3= TimeFrameGetPrice("L", inDaily, -3); DayL3I = LastValue (DayL3,1); // Three days before low

numbars = LastValue(Cum(Status("barvisible")));
hts = -33.5;

YHL = ParamToggle("Yesterday HI LO","Show|Hide",1);
if(YHL==1) {
Plot(DayL,"YL",colorTurquoise,styleLine|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH,"YH",colorTurquoise,styleLine|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" YH " , LastValue(BarIndex())-(numbars/Hts), DayHI, colorTurquoise);
PlotText(" YL " , LastValue(BarIndex())-(numbars/Hts), DayLI, colorTurquoise);
}

TDBHL = ParamToggle("2/3Days before HI LO","Show|Hide",0);
if(TDBHL==1) {
Plot(DayL2,"2DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH2,"2DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayL3,"3DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH3,"3DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" 2DBH " , LastValue(BarIndex())-(numbars/Hts), DayH2I, colorTurquoise);
PlotText(" 2DBL " , LastValue(BarIndex())-(numbars/Hts), DayL2I, colorTurquoise);
PlotText(" 3DBH " , LastValue(BarIndex())-(numbars/Hts), DayH3I, colorTurquoise);
PlotText(" 3DBL " , LastValue(BarIndex())-(numbars/Hts), DayL3I, colorTurquoise);
}


PP = (DayL + DayH + DayC)/3; PPI = LastValue (PP,1); // Pivot
R1 = (PP * 2) - DayL; R1I = LastValue (R1,1); // Resistance 1
S1 = (PP * 2) - DayH; S1I = LastValue (S1,1); // Support 1
R2 = PP + R1 - S1; R2I = LastValue (R2,1); // Resistance 2
S2 = PP - R1 + S1; S2I = LastValue (S2,1); // Support 2
R3 = PP + R2 - S1; R3I = LastValue (R3,1); // Resistance 3
S3 = PP - R2 + S1; S3I = LastValue (S3,1); // Support 3

ppl = ParamToggle("Pivot Levels","Show|Hide",1);
if(ppl==1) {
Plot(PP, "PP",colorYellow,styleDashed|styleLine|styleNoRescale|styleNoTitle);
//Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
//Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
//Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
//Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
//Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
//Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);

PlotText(" Pivot ", LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
PlotText(" R1 " , LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
PlotText(" S1 " , LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
PlotText(" R2 " , LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
PlotText(" S2 " , LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
PlotText(" R3 " , LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
PlotText(" S3 " , LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
}


rg = (DayH - DayL);

H5=DayC+1.1*rg; H5I = LastValue (H5,1);
H4=DayC+1.1*rg/2; H4I = LastValue (H4,1);
H3=DayC+1.1*rg/4; H3I = LastValue (H3,1);
H2=DayC+1.1*rg/6; H2I = LastValue (H2,1);
H1=DayC+1.1*rg/12; H1I = LastValue (H1,1);
L1=DayC-1.1*rg/12; L1I = LastValue (L1,1);
L2=DayC-1.1*rg/6; L2I = LastValue (L2,1);
L3=DayC-1.1*rg/4; L3I = LastValue (L3,1);
L4=DayC-1.1*rg/2; L4I = LastValue (L4,1);
L5=DayC-1.1*rg; L5I = LastValue (L5,1);

pcl = ParamToggle("Camerilla Levels","Show|Hide",0);
if(pcl==1) {
Plot(H5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" H5 = " , LastValue(BarIndex())-(numbars/Hts), H5I +0.05, colorRose);
PlotText(" H4 = " , LastValue(BarIndex())-(numbars/Hts), H4I +0.05, colorRose);
PlotText(" H3 = " , LastValue(BarIndex())-(numbars/Hts), H3I +0.05, colorRose);
PlotText(" H2 = " , LastValue(BarIndex())-(numbars/Hts), H2I +0.05, colorRose);
PlotText(" H1 = " , LastValue(BarIndex())-(numbars/Hts), H1I +0.05, colorRose);
PlotText(" L1 = " , LastValue(BarIndex())-(numbars/Hts), L1I +0.05, colorRose);
PlotText(" L2 = " , LastValue(BarIndex())-(numbars/Hts), L2I +0.05, colorRose);
PlotText(" L3 = " , LastValue(BarIndex())-(numbars/Hts), L3I +0.05, colorRose);
PlotText(" L4 = " , LastValue(BarIndex())-(numbars/Hts), L4I +0.05, colorRose);
PlotText(" L5 = " , LastValue(BarIndex())-(numbars/Hts), L5I +0.05, colorRose);
}


THL = ParamToggle("Todays Hi Lo","Show|Hide",1);
if(THL==1) {
isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
isdRth = TimeNum() >= 084500 & TimeNum() <= 160000;
aRthL = IIf(isRth, L, 1000000);
aRthH = IIf(isdRth, H, Null);
aRthLd = IIf(isdRth, L, 1000000);
DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);//,BarIndex(),1); // AND DateNum()==LastValue(DateNum());
x0 = BarCount-LastValue(Bars);
x1 = BarCount-1;
DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0); 
DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0); 
DayHlineI = LastValue (DayHline,1);
DayLlineI = LastValue (DayLline,1);
Plot(DayHline,"DayH",colorYellow,styleBar|styleNoRescale|styleNoTitle);
Plot(DayLline,"DayL",colorYellow,styleBar|styleNoRescale|styleNoTitle);
PlotText(" Day Hi " , LastValue(BarIndex())-(numbars/Hts), DayHlineI +0.05, colorYellow);
PlotText(" Day Lo " , LastValue(BarIndex())-(numbars/Hts), DayLlineI +0.05, colorYellow);
}

  


/*                     HARMONIC PATTERN DETECTION                        


Automatic Detection of Harmonic Patterns - Gartley, Bat, Butterfly and Crab. 

Zig Zag is not used in this AFL. It is based on fractals

Contact - [email protected]

*/


_SECTION_BEGIN("Gartley");

GBmin = Param("Swing B Min.",0.55,0.3,1,0.01);
GBmax = Param("Swing B Max.",0.72,0.4,1,0.01);
GCmin = Param("Swing C Min.",0.38,0.3,1.27,0.01);
GCmax = Param("Swing C Max.",1.0,0.4,1.27,0.01);
GDmin = Param("Swing D Min.(XA)",0.55,0.3,1,0.01);
GDmax = Param("Swing D Max.(XA)",1.0,0.4,1.0,0.01);

_SECTION_END();

_SECTION_BEGIN("Bat");

BatBmin = Param("Swing B Min.",0.38,0.3,1,0.01);
BatBmax = Param("Swing B Max.",0.55,0.4,1,0.01);
BatCmin = Param("Swing C Min.",0.38,0.3,1.62,0.01);
BatCmax = Param("Swing C Max.",1.27,0.4,1.62,0.01);
BatDmin = Param("Swing D Min.(XA)",0.5,0.3,1,0.01);
BatDmax = Param("Swing D Max.(XA)",1.0,0.4,1.0,0.01);

_SECTION_END();

_SECTION_BEGIN("Butterfly");

BtBmin = Param("Swing B Min.",0.55,0.3,1,0.01);
BtBmax = Param("Swing B Max.",0.9,0.4,1,0.01);
BtCmin = Param("Swing C Min.",0.38,0.3,1.62,0.01);
BtCmax = Param("Swing C Max.",1.27,0.4,1.62,0.01);
BtDmin = Param("Swing D Min.(XA)",1,1,1.8,0.01);
BtDmax = Param("Swing D Max.(XA)",1.8,1,1.8,0.01);						// Max XA of Butterfly = (1.0 - 1.618)

_SECTION_END();

_SECTION_BEGIN("Crab");

CBmin = Param("Swing B Min.",0.38,0.3,1,0.01);
CBmax = Param("Swing B Max.",0.65,0.4,1,0.01);
CCmin = Param("Swing C Min.",0.38,0.3,1.62,0.01);
CCmax = Param("Swing C Max.",1.270,0.4,1.62,0.01);
CDmin = Param("Swing D Min.(XA)",1.25,1,1.8,0.01);
CDmax = Param("Swing D Max.(XA)",1.8,1,2,0.01);

_SECTION_END();

_SECTION_BEGIN("AB=CD");

abcd_Cmin = Param("Swing C Min.",0.3,		0.3	,	1,		0.01);
abcd_Cmax = Param("Swing C Max.",0.8,		0.8	,	1,		0.01);
abcd_Dmin = Param("Swing D Min.",1.2,		1,		2.7,	0.01);
abcd_Dmax = Param("Swing D Max.",3.7,		1,		4,		0.01);

_SECTION_END();

_SECTION_BEGIN("Patterns");
	
strength = Param("BARS of each LINE",5,2,15,1);							// So luong bar cho moi duong XA, AB, BC, 
bu = ParamToggle("Bullish Pattern","Off|On",1);							// So bar/lines se quyet dinh. mo^ hinh` duoc ve the' nao`
be = ParamToggle("Bearish Pattern","Off|On",1);

bi = Cum(1)-1;

function GetTop(bars) 														// Lay' gia' tri cao nhat' = di?nh
	{
		Top = H == HHV(H,2*bars) AND Ref(HHV(H,bars),bars) < H;
		Top = Top AND LastValue(bi)-ValueWhen(Top,bi) > bars;
		return Top;
	}

function GetValley(bars)													// La'y gia tri thap' nhat' = day'
	{
		Valley = L == LLV(L,2*bars) AND Ref(LLV(L,bars),bars) > L;
		Valley = Valley AND LastValue(bi)-ValueWhen(Valley,bi) > bars;
		return Valley;
	}


// Build fractals array

P1 = GetTop(strength);										// so' bar cho 1 duong` XA, AB, BC, CD
V1 = GetValley(Strength);

P1 = IIf(P1,IIf(ValueWhen(P1,bi,2) < ValueWhen(V1,bi),P1,IIf(ValueWhen(P1,H,2) > H,False,P1)),P1);
P1 = IIf(P1 AND ValueWhen(P1,bi,0) > bi,IIf(ValueWhen(P1,bi,0) < ValueWhen(V1,bi,0),IIf(ValueWhen(P1,H,0) >= H,False,P1),P1),P1);
V1 = IIf(V1,IIf(ValueWhen(V1,bi,2) < ValueWhen(P1,bi),V1,IIf(ValueWhen(V1,L,2)<L,False,V1)),V1);
V1 = IIf(V1 AND ValueWhen(V1,bi,0) > bi ,IIf(ValueWhen(V1,bi,0) < ValueWhen(P1,bi,0),IIf(ValueWhen(V1,L,0) <= L, False,V1),V1),V1); 


P1H1 = ValueWhen(P1,H);
P1Bar1 = ValueWhen(P1,bi);
P1H2 = ValueWhen(P1,H,2);
P1Bar2 = ValueWhen(P1,bi,2);
V1L1 = ValueWhen(V1,L);
V1Bar1 = ValueWhen(V1,bi);
V1L2 = ValueWhen(V1,L,2);
V1Bar2 = ValueWhen(V1,bi,2);


//============================================
//				BULLISH PATTERNS
//============================================
/*
	Mo hinh Bullish:
	A	=	P1H2
	B	=	V1L1
	C	=	P1H1
	X	=	V1L2

*/

PTvalid = (P1Bar1 > V1Bar1 AND V1Bar1 > P1Bar2 AND P1bar2 > V1Bar2) AND P1; // Peaks and troughs are in order

myAX			=	P1H2-V1L2;
myAB			=	P1H2-V1L1;
myBC			=	P1H1-V1L1;

myAB_AX		=	myAB/ myAX;
myBC_AB		=	myBC/ myAB;	

BullGartley4 		= PTvalid 	AND 	(	myAB_AX > GBmin	) 		AND (	myAB_AX < GBmax	)
								AND  	(	myBC_AB > GCMin 	) 		AND (	myBC_AB < GCMax	); 

BullBat4 			= PTvalid 	AND 	(	myAB_AX > BatBmin ) 		AND (	myAB_AX < BatBmax	)
								AND 	(	myBC_AB > BatCMin ) 		AND (	myBC_AB < BatCMax	); 

BullButterfly4 	= PTvalid 	AND 	(	myAB_AX > BtBmin ) 		AND (	myAB_AX < BtBMax	)
								AND  	(	myBC_AB > BtCmin ) 		AND (	myBC_AB < BtCmin 	);

BullCrab4 			= PTvalid 	AND 	(	myAB_AX > CBmin )	  		AND (	myAB_AX < CBmax 	)
								AND  	(	myBC_AB > CCmin ) 		AND (	myBC_AB < CCmax	);

BullABCD4			= PTvalid AND 	(	myBC_AB > abcd_Cmin) 	AND (	myBC_AB < abcd_Cmax	);

strPattern = "";

//==================================================
//				 BULLISH ABCD
// 	Bullish pattern found. D retracement level is not evaluated
//==================================================
	dHigh		=		HighestSince(BullABCD4,H);				// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BullABCD4,L);
	
	myC			=		ValueWhen(BullABCD4,P1H1);
	myB			=		ValueWhen(BullABCD4,V1L1);
	myA			=		ValueWhen(BullABCD4,P1H2);
	myX			=		ValueWhen(BullABCD4,V1L2);
	myCB		=		myC - myB;

	my_d_min	=		myCB	*	abcd_DMin ;					// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myCB	*	abcd_DMax ;
	my_Cd_min	=		myC - my_d_min;					   // Khoang dich chuyen cua duong Ad con.
	my_Cd_max	=		myC - my_d_max;

BullABCD	 	= 		IIf(		( dLow  <	my_Cd_min	)	AND		( dLow	> my_Cd_max )	
								AND	( dHigh	<=	myC		)	AND		( dLow	==	L), 
								True, False
							);

BullABCD		=		BullABCD	AND (dLow		<	myB);


//==================================================
// 				BULLISH GARTLEY
//==================================================
	dHigh		=		HighestSince(BullGartley4,H);				// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BullGartley4,L);

	myC			=		ValueWhen(BullGartley4,P1H1);
	myB			=		ValueWhen(BullGartley4,V1L1);
	myA			=		ValueWhen(BullGartley4,P1H2);
	myX			=		ValueWhen(BullGartley4,V1L2);
	myAX		=		myA - myX;

	my_d_min	=		myAX	*	GDmin;							// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myAX	*	GDMax;
	my_Ad_min	=		myA - my_d_min;							// Khoang dich chuyen cua duong Ad con.
	my_Ad_max	=		myA - my_d_max;

BullGartley 	= 		IIf(		( dLow  <	my_Ad_min	)	AND		( dLow	> my_Ad_max )	
								AND	( dHigh	<=	myC		)	AND		( dLow	==	L), 
								True, False
							);
BullGartley 	=		BullGartley 	AND (dLow		<	myB);						// diem D thap' hon B
strPattern 	=		WriteIf(BullGartley,"BULLISH GARTLEY",strPattern);



//==================================================
// 				BULLISH BAT
//==================================================
	dHigh		=		HighestSince(BullBat4,H);				// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BullBat4,L);	

	myC			=		ValueWhen(BullBat4,P1H1);
	myB			=		ValueWhen(BullBat4,V1L1);
	myA			=		ValueWhen(BullBat4,P1H2);
	myX			=		ValueWhen(BullBat4,V1L2);
	myAX		=		myA - myX;

	my_d_min	=		myAX	*	BatDmin;						// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myAX	*	BatDmax ;
	my_Ad_min	=		myA - my_d_min;							// Khoang dich chuyen cua duong Ad con.
	my_Ad_max	=		myA - my_d_max;

BullBat 		= 		IIf(		( dLow  <	my_Ad_min	)	AND		( dLow	> my_Ad_max )	
								AND	( dHigh	<=	myC		)	AND		( dLow	==	L), 
								True, False
							);
BullBat 		=		BullBat 	AND (dLow		<	myB);			// diem d thap hon diem B
strPattern 	=		WriteIf(BullBat,"BULLISH BAT",strPattern);


//==================================================
// 				BULLISH CRAB - CUA
//==================================================
	dHigh		=		HighestSince(BullCrab4,H);				// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BullCrab4,L);

	myC			=		ValueWhen(BullCrab4,P1H1);
	myB			=		ValueWhen(BullCrab4,V1L1);
	myA			=		ValueWhen(BullCrab4,P1H2);
	myX			=		ValueWhen(BullCrab4,V1L2);
	myAX		=		myA - myX;

	my_d_min	=		myAX	*	CDmin ;					// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myAX	*	CDmax ;
	my_Ad_min	=		myA - my_d_min;						// Khoang dich chuyen cua duong Ad con.
	my_Ad_max	=		myA - my_d_max;

BullCrab 		= 		IIf(		( dLow  <	my_Ad_min	)	AND		( dLow	> my_Ad_max )	
								AND	( dHigh	<=	myC		)	AND		( dLow	==	L), 
								True, False
							);
BullCrab 		=		BullCrab 	AND (dLow		<	myX);					// diem D thap' hon X
strPattern 	=		WriteIf(BullCrab ,"BULLISH CRAB",strPattern);


//==================================================
// 				BULLISH  BUTTTERFLY
//==================================================
	dHigh		=		HighestSince(BullButterfly4,H);				// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BullButterfly4,L);

	myC			=		ValueWhen(BullButterfly4,P1H1);
	myB			=		ValueWhen(BullButterfly4,V1L1);
	myA			=		ValueWhen(BullButterfly4,P1H2);
	myX			=		ValueWhen(BullButterfly4,V1L2);
	myAX		=		myA - myX;

	my_d_min	=		myAX	*	BtDmin ;								// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myAX	*	BtDmax ;
	my_Ad_min	=		myA - my_d_min;									// Khoang dich chuyen cua duong Ad con.
	my_Ad_max	=		myA - my_d_max;

BullButterfly 	= 		IIf(		( dLow  <	my_Ad_min	)	AND		( dLow	> my_Ad_max )	
									AND	( dHigh	<=	myC		)	AND		( dLow	==	L), 
								True, False
								);
BullButterfly 	=		BullButterfly 	AND (dLow		<	myX);					// diem D thap' hon X
strPattern 		=		WriteIf(BullButterfly ,"BULLISH BUTTERFLY",strPattern);



//==========================================================
//   VE DUONG CHO MO HINH BULLISH ABCB 
//==========================================================
BullHar4 	=  BullABCD4;
BullHar 	=  BullABCD;

Point4 = IIf(BullHar,ValueWhen(BullHar4,bi),Null);
BullHar = IIf(BullHar, IIf(Point4 == ValueWhen(BullHar,point4,0) AND ValueWhen(BullHar,bi,0) > bi ,False,BullHar),BullHar);

A = ValueWhen(BullHar4,P1H2);
Abar = ValueWhen(BullHar4,P1bar2);
B = ValueWhen(BullHar4,V1L1);
Bbar = ValueWhen(BullHar4,V1bar1);
C1 = ValueWhen(BullHar4,P1H1);
C1bar = ValueWhen(BullHar4,P1bar1);
D = ValueWhen(BullHar,L);
Dbar = ValueWhen(BullHar,bi);

BCdAB = (C1-B)/(A-B);
BCdCD = (C1-D)/(C1-B);

PlotPattern = Dbar > C1bar;

if(LastValue(PlotPattern) AND bu)
{
		ColorX = colorGreen;
	// Ve cac duong AB, BC, CD
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);

	// Ve cac gia tri Fibo
		PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX );
		PlotText(NumToStr(LastValue(BCdCD),1.2),(LastValue(Bbar)+LastValue(Dbar))/2,(LastValue(B)+LastValue(D))/2,ColorX );

	//---------- Viet cac diem X, A, B, C, D: by binhnd---------------------
		xlech		=	0;
		ylech 		= 	2;
		PlotText("A",LastValue(Abar)	+ 	xlech,	LastValue(A)	+	ylech,	ColorX );
		PlotText("B",LastValue(Bbar)	+ 	xlech,	LastValue(B)	-	ylech,	ColorX );
		PlotText("C",LastValue(C1bar)	+ 	xlech,	LastValue(C1)	+	ylech,	ColorX );
		PlotText("D",LastValue(Dbar)	+ 	xlech,	LastValue(D)	-	ylech,	ColorX );

	//--------- Viet thuyet minh mo hinh: by binhnd--------------
		if (strPattern!="")  
		{
			myStr			=	"Pattern: BULLISH AB=CD";
			toadoX			=	LastValue(Abar);
			toadoY			=	LastValue(D);

			PlotText(myStr,toadoX,toadoY,ColorX );
		}

}			//	end of Ve duong` bullish abcd



//==========================================================
//   VE DUONG CHO MO HINH BULLISH BAT, GARTLEY, BUTTERFLY, CRAB
//==========================================================


BullHar4 = BullGartley4 OR BullButterfly4 OR BullBat4 OR BullCrab4 ;
BullHar = BullGartley OR BullButterfly OR BullBat OR BullCrab;

Point4 = IIf(BullHar,ValueWhen(BullHar4,bi),Null);
BullHar = IIf(BullHar, IIf(Point4 == ValueWhen(BullHar,point4,0) AND ValueWhen(BullHar,bi,0) > bi ,False,BullHar),BullHar);

X = ValueWhen(BullHar4,V1L2);
Xbar = ValueWhen(BullHar4,V1Bar2);
A = ValueWhen(BullHar4,P1H2);
Abar = ValueWhen(BullHar4,P1bar2);
B = ValueWhen(BullHar4,V1L1);
Bbar = ValueWhen(BullHar4,V1bar1);
C1 = ValueWhen(BullHar4,P1H1);
C1bar = ValueWhen(BullHar4,P1bar1);
D = ValueWhen(BullHar,L);
Dbar = ValueWhen(BullHar,bi);

ABdXA = (A-B)/(A-X);
BCdAB = (C1-B)/(A-B);
ADdXA = (A-D)/(A-X);
BCdCD = (C1-D)/(C1-B);

PlotPattern = Dbar > C1bar;

if(LastValue(PlotPattern) AND bu)
{
			ColorX	= colorBlue;
		// Ve cac duong XA, AB, BC, CD
			Plot( LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Bbar),LastValue(B)),"",ColorX,styleDashed);
			Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX,styleDashed);
			Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX,styleDashed);
			Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Dbar),LastValue(D)),"",ColorX,styleDashed);

		// Ve cac gia tri Fibo
			PlotText(NumToStr(LastValue(ABdXA),1.2),(LastValue(Bbar)+LastValue(Xbar))/2,(LastValue(B)+LastValue(X))/2,ColorX);
			PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX);
			PlotText(NumToStr(LastValue(ADdXA),1.2) ,(LastValue(Dbar)+LastValue(Xbar))/2,(LastValue(D)+LastValue(X))/2,ColorX);
			PlotText(NumToStr(LastValue(BCdCD),1.2),(LastValue(Bbar)+LastValue(Dbar))/2,(LastValue(B)+LastValue(D))/2,ColorX);

		//---------- Viet cac diem X, A, B, C, D: by binhnd---------------------
			xlech		=	0;
			ylech 		= 	2;
			PlotText("X",LastValue(Xbar)	+ 	xlech,	LastValue(X)	-	ylech,	ColorX);
			PlotText("A",LastValue(Abar)	+ 	xlech,	LastValue(A)	+	ylech,	ColorX);
			PlotText("B",LastValue(Bbar)	+ 	xlech,	LastValue(B)	-	ylech,	ColorX);
			PlotText("C",LastValue(C1bar)	+ 	xlech,	LastValue(C1)	+	ylech,	ColorX);
			PlotText("D",LastValue(Dbar)	+ 	xlech,	LastValue(D)	-	ylech,	ColorX);

		//--------- Viet thuyet minh mo hinh: by binhnd--------------
			if (strPattern!="")  
			{
				strPattern 	= 	"Pattern: " + strPattern;
				toadoX			=	(LastValue(Dbar)+LastValue(Xbar))/2;
				toadoY			=	(LastValue(D)+LastValue(X))/2;

				PlotText(strPattern,toadoX,toadoY-2,ColorX);
			}

}			// end of Ve duong cho cac mo hinh Crab, Butterfly, Bat


//=============================================================
//				BEARISH PATTERNS
//=============================================================

PTvalid = (V1Bar1 > P1Bar1 AND P1Bar1 > V1Bar2 AND V1Bar2 > P1Bar2) AND V1;

/*=====================
		X 	= 	P1H2					 Trong mo hinh` bear: Die^m X cao hon diem A. MyAX = X-> A
		A	=	V1L2
		B	=	P1H1
		C	=	V1L1

=======================*/
myAX			=	P1H2-V1L2;				
myAB			=	P1H1-V1L2;
myBC			=	P1H1-V1L1;

myAB_AX		=	myAB/ myAX;
myBC_AB		=	myBC/ myAB;	

BearGartley4 		= PTvalid 	AND 	(	myAB_AX > GBmin	) 		AND (	myAB_AX < GBmax	)
								AND  	(	myBC_AB > GCMin 	) 		AND (	myBC_AB < GCMax	); 

BearBat4 			= PTvalid 	AND 	(	myAB_AX > BatBmin ) 		AND (	myAB_AX < BatBmax	)
								AND 	(	myBC_AB > BatCMin ) 		AND (	myBC_AB < BatCMax	); 

BearButterfly4 	= PTvalid 	AND 	(	myAB_AX > BtBmin ) 		AND (	myAB_AX < BtBMax	)
								AND  	(	myBC_AB > BtCmin ) 		AND (	myBC_AB < BtCmin 	);

BearCrab4 			= PTvalid 	AND 	(	myAB_AX > CBmin )	  		AND (	myAB_AX < CBmax 	)
								AND  	(	myBC_AB > CCmin ) 		AND (	myBC_AB < CCmax	);

BearABCD4			= PTvalid AND 	(	myBC_AB > abcd_Cmin) 	AND (	myBC_AB < abcd_Cmax	);

strPattern = "";



//==========================================================
//				 BEARISH ABCD
// 	Bearish pattern found. D retracement level is not evaluated
//==========================================================
	dHigh		=		HighestSince(BearABCD4,H);				// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BearABCD4,L);
	
	myA			=		ValueWhen(BearABCD4,V1L2);
	myB			=		ValueWhen(BearABCD4,P1H1);
	myC			=		ValueWhen(BearABCD4,V1L1);
	myCB		=		myB - myC;

	my_d_min	=		myCB	*	abcd_DMin ;					// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myCB	*	abcd_DMax ;
	my_Cd_min	=		myC + my_d_min;					   // Khoang dich chuyen cua duong Ad con.
	my_Cd_max	=		myC + my_d_max;

BearABCD	 	= 		IIf(		( dHigh  	>	my_Cd_min	)	AND		( dHigh	< my_Cd_max )	
								AND	( dLow		>=	myC			)	AND		( dHigh	==	H), 
								True, False
							);

BearABCD		=		BearABCD	AND (dHigh		>	myB);

//=============================================================
//				BEARISH GARTLEY
//=============================================================
	dHigh		=		HighestSince(BearGartley4,H);		// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BearGartley4,L);

	myX			=		ValueWhen(BearGartley4,P1H2);
	myA			=		ValueWhen(BearGartley4,V1L2);
	myAX		=		myX - myA;

	myB			=		ValueWhen(BearGartley4,P1H1);
	myC			=		ValueWhen(BearGartley4,V1L1);


	my_d_min	=		myAX	*	GDmin;						// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myAX	*	GDMax;
	my_Ad_min	=		myA 	+ 	my_d_min;					// Khoang dich chuyen cua duong Ad con.
	my_Ad_max	=		myA 	+ 	my_d_max;

BearGartley 	= 		IIf(		( dHigh	>	my_Ad_min	)	AND		( dHigh	< my_Ad_max )	
								AND	( dLow		>=	myC			)	AND		( dHigh	==	H), 
								True, False
							);
BearGartley 	=		BearGartley 	AND (dHigh		>	myB);						// diem D cao hon B
strPattern 	=		WriteIf(BearGartley ,"BEARISH GARTLEY",strPattern);

//=============================================================
//				BEARISH BAT
//=============================================================
	dHigh		=		HighestSince(BearBat4,H);		// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BearBat4,L);

	myX			=		ValueWhen(BearBat4,P1H2);
	myA			=		ValueWhen(BearBat4,V1L2);
	myAX		=		myX - myA;

	myB			=		ValueWhen(BearBat4,P1H1);
	myC			=		ValueWhen(BearBat4,V1L1);


	my_d_min	=		myAX	*	BatDmin ;						// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myAX	*	BatDMax ;
	my_Ad_min	=		myA 	+ 	my_d_min;					// Khoang dich chuyen cua duong Ad con.
	my_Ad_max	=		myA 	+ 	my_d_max;

BearBat 		= 		IIf(		( dHigh	>	my_Ad_min	)	AND		( dHigh	< my_Ad_max )	
								AND	( dLow		>=	myC			)	AND		( dHigh	==	H), 
								True, False
							);
BearBat 		=		BearBat 	AND (dHigh		>	myB);						// diem D cao hon B
strPattern 	=		WriteIf(BearBat ,"BEARISH BAT",strPattern);


//=============================================================
//				BEARISH BUTTERFLY
//=============================================================
	dHigh		=		HighestSince(BearButterfly4,H);		// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BearButterfly4,L);

	myX			=		ValueWhen(BearButterfly4,P1H2);
	myA			=		ValueWhen(BearButterfly4,V1L2);
	myAX		=		myX - myA;

	myB			=		ValueWhen(BearButterfly4,P1H1);
	myC			=		ValueWhen(BearButterfly4,V1L1);


	my_d_min	=		myAX	*	BtDmin ;						// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myAX	*	BtDmax ;
	my_Ad_min	=		myA 	+ 	my_d_min;						// Khoang dich chuyen cua duong Ad con.
	my_Ad_max	=		myA 	+ 	my_d_max;

BearButterfly = 		IIf(		( dHigh	>	my_Ad_min	)	AND		( dHigh	< my_Ad_max )	
								AND	( dLow		>=	myC			)	AND		( dHigh	==	H), 
								True, False
							);
BearButterfly	=		BearButterfly 	AND (dHigh		>	myX);						// diem D cao hon X
strPattern		=		WriteIf(BearButterfly ,"BEARISH BUTTERFLY",strPattern);



//=============================================================
//				BEARISH CRAB
//=============================================================
	dHigh		=		HighestSince(BearCrab4,H);				// Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
	dLow 		= 		LowestSince(BearCrab4,L);

	myX			=		ValueWhen(BearCrab4,P1H2);
	myA			=		ValueWhen(BearCrab4,V1L2);
	myAX		=		myX - myA;

	myB			=		ValueWhen(BearCrab4,P1H1);
	myC			=		ValueWhen(BearCrab4,V1L1);


	my_d_min	=		myAX	*	CDmin ;						// Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
	my_d_max	=		myAX	*	CDmax ;
	my_Ad_min	=		myA 	+ 	my_d_min;						// Khoang dich chuyen cua duong Ad con.
	my_Ad_max	=		myA 	+ 	my_d_max;

BearCrab 		= 		IIf(		( dHigh	>	my_Ad_min	)	AND		( dHigh	< my_Ad_max )	
								AND	( dLow		>=	myC			)	AND		( dHigh	==	H), 
								True, False
							);
BearCrab 		=		BearCrab 	AND (dHigh		>	myX);						// diem D cao hon X
strPattern 	=		WriteIf(BearCrab ,"BEARISH CRAB",strPattern);



//==========================================================
//   VE DUONG CHO MO HINH BEARISH ABCD
//==========================================================


BearHar4 = BearABCD4;
BearHar = BearABCD;

Point4 = IIf(BearHar,ValueWhen(BearHar4,bi),Null);
BearHar = IIf(BearHar, IIf(Point4 == ValueWhen(BearHar,point4,0) AND ValueWhen(BearHar,bi,0) > bi ,False,BearHar),BearHar);

A = ValueWhen(BearHar4,V1L2);
Abar = ValueWhen( BearHar4,V1bar2);
B = ValueWhen(BearHar4,P1H1);
Bbar = ValueWhen(BearHar4,P1bar1);
C1 = ValueWhen(BearHar4,V1L1);
C1bar = ValueWhen(BearHar4,V1bar1);
D = ValueWhen(BearHar,H);
Dbar = ValueWhen(BearHar,bi);

BCdAB = (B-C1)/(B-A);
BCdCD = (D-C1)/(B-C1);

PlotPattern = Dbar > C1bar;

//--------- Ve duong ------------------
if(LastValue(Plotpattern) AND be)
{
		ColorX = colorYellow;
	// Ve duong AB, BC
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);

	// Viet cac gia tri Fibo tren duong AB, BC
		PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX );
		PlotText(NumToStr(LastValue(BCdCD),1.2) ,(LastValue(Dbar)+LastValue(Bbar))/2,(LastValue(D)+LastValue(B))/2,ColorX );

	//---------- Viet cac diem A, B, C, D: by binhnd---------------------
		xlech		=	-1;
		ylech 		= 	1;
		PlotText("A",LastValue(Abar)	+ 	xlech,	LastValue(A)	-	ylech,	ColorX );
		PlotText("B",LastValue(Bbar)	+ 	xlech,	LastValue(B)	+	ylech,	ColorX );
		PlotText("C",LastValue(C1bar)	+ 	xlech,	LastValue(C1)	-	ylech,	ColorX );
		PlotText("D",LastValue(Dbar)	+ 	xlech,	LastValue(D)	+	ylech,	ColorX );

	//--------- Viet thuyet minh mo hinh: by binhnd--------------
		if (strPattern!="") 
			{
				myStr			=	"Pattern: BEARISH AB=CD";
				toadoaX		=	LastValue(Abar);
				toadoY			=	LastValue(D);

				PlotText(myStr,toadoaX,toadoY+1,ColorX );
			}
	
}			// end of VE DUONG CHO MO HINH BEARISH ABCD


//==========================================================
//   VE DUONG CHO MO HINH BEARISH BAT, GARTLEY, BUTTERFLY, CRAB
//==========================================================

BearHar4 = BearGartley4 OR BearButterfly4 OR BearBat4 OR BearCrab4 ;
BearHar = BearGartley OR BearButterfly OR BearBat OR BearCrab ;

Point4 = IIf(BearHar,ValueWhen(BearHar4,bi),Null);
BearHar = IIf(BearHar, IIf(Point4 == ValueWhen(BearHar,point4,0) AND ValueWhen(BearHar,bi,0) > bi ,False,BearHar),BearHar);

X = ValueWhen(BearHar4,P1H2);
Xbar = ValueWhen(BearHar4,P1Bar2);
A = ValueWhen(BearHar4,V1L2);
Abar = ValueWhen( BearHar4,V1bar2);
B = ValueWhen(BearHar4,P1H1);
Bbar = ValueWhen(BearHar4,P1bar1);
C1 = ValueWhen(BearHar4,V1L1);
C1bar = ValueWhen(BearHar4,V1bar1);
D = ValueWhen(BearHar,H);
Dbar = ValueWhen(BearHar,bi);

ABdXA = (B-A)/(X-A);
BCdAB = (B-C1)/(B-A);
ADdXA = (D-A)/(X-A);
BCdCD = (D-C1)/(B-C1);

PlotPattern = Dbar > C1bar;

//--------- Ve duong ------------------
if(LastValue(Plotpattern) AND be)
{
		ColorX = colorRed;
	// Ve duong XA, AB, BC
		Plot( LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Bbar),LastValue(B)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);

	// Viet cac gia tri Fibo tren duong XA, AB, BC
		PlotText(NumToStr(LastValue(ABdXA),1.2),(LastValue(Bbar)+LastValue(Xbar))/2,(LastValue(B)+LastValue(X))/2,ColorX );
		PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX );
		PlotText(NumToStr(LastValue(BCdCD),1.2) ,(LastValue(Dbar)+LastValue(Bbar))/2,(LastValue(D)+LastValue(B))/2,ColorX );
		PlotText(NumToStr(LastValue(ADdXA),1.2) ,(LastValue(Dbar)+LastValue(Xbar))/2,(LastValue(D)+LastValue(X))/2,ColorX );

	//---------- Viet cac diem X, A, B, C, D: by binhnd---------------------
		xlech		=	-1;
		ylech 		= 	1;
		PlotText("X",LastValue(Xbar)	+ 	xlech,	LastValue(X)	+	ylech,	ColorX );
		PlotText("A",LastValue(Abar)	+ 	xlech,	LastValue(A)	-	ylech,	ColorX );
		PlotText("B",LastValue(Bbar)	+ 	xlech,	LastValue(B)	+	ylech,	ColorX );
		PlotText("C",LastValue(C1bar)	+ 	xlech,	LastValue(C1)	-	ylech,	ColorX );
		PlotText("D",LastValue(Dbar)	+ 	xlech,	LastValue(D)	+	ylech,	ColorX );

	//--------- Viet thuyet minh mo hinh: by binhnd--------------
		if (strPattern!="") 
			{
				strPattern 	= 	"Pattern: " + strPattern;
				toadoaX		=	(LastValue(Dbar)+LastValue(Xbar))/2;
				toadoY			=	(LastValue(D)+LastValue(X))/2;

				PlotText(strPattern,toadoaX,toadoY+1,ColorX );
			}
	
}			// end of VE DUONG CHO MO HINH BEARISH BAT, GARTLEY, BUTTERFLY, CRAB




//=================================
// Show diem ho^~ tro. va` khang' cu. ko?
//=================================

plotFractals = ParamToggle("Plot Fractals","Off|On",1);				
if(PlotFractals)
{
	PlotShapes(shapeSmallCircle*P1,colorBlack,0,H,10);
	PlotShapes(shapeSmallCircle*V1,colorBlue,0,L,-10);
}



//==============================================
// DAT DIEU KIEN cho TIM KIEM BULL
//==============================================
dkBull = False;
ListBull 		= 	ParamList("Type of Bullish", "None|AB=CD|Gartley|Butterfly|Bat|Crab|All Patterns", 6);
	if 	(	ListBull == "None"		)		dkBull = 	True;
	if (	ListBull =="AB=CD"		) 		dkBull	=	BullABCD ;
	if (	ListBull =="Gartley"		) 		dkBull	=	BullGartley ;
	if (	ListBull =="Butterfly"	) 		dkBull	=	BullButterfly ;
	if (	ListBull =="Bat"			) 		dkBull	=	BullBat ;
	if (	ListBull =="Crab"			) 		dkBull	=	BullCrab ;
	if (	ListBull =="All Patterns") 		dkBull	=	(BullABCD) OR (BullGartley) OR (BullButterfly ) OR (BullBat ) OR (BullCrab);

//==============================================
// DAT DIEU KIEN cho TIM KIEM BEAR
//==============================================
dkBear = False;
ListBear 		= 	ParamList("Type of Bearish", "None|AB=CD|Gartley|Butterfly|Bat|Crab|All Patterns", 6);
	if 	(	ListBear == "None"		)		dkBear = 	True;
	if (	ListBear =="AB=CD"		) 		dkBear	=	BearABCD ;
	if (	ListBear =="Gartley"		) 		dkBear	=	BearGartley ;
	if (	ListBear =="Butterfly"	) 		dkBear	=	BearButterfly ;
	if (	ListBear =="Bat"			) 		dkBear	=	BearBat ;
	if (	ListBear =="Crab"			) 		dkBear =	BearCrab ;
	if (	ListBear =="All Patterns") 		dkBear =	(BearABCD ) OR (BearGartley ) OR (BearButterfly ) OR (BearBat ) OR (BearCrab );
//===============================

AddColumn(V,"Volume",1.0);
Filter = (dkBull) AND (dkBear);


// Supertrend - Translated from Kolier MQ4
// see: http://kolier.li/indicator/kolier-supertrend-indi
// translation in Amibroker AFL code by E.M.Pottasch, 2011

procedure calcTrend_proc(ATR_Period,tr,ATR_Multiplier,TrendMode,CalcPrice)
{
global buffer_line_down;
global buffer_line_up;
buffer_line_down = Null;
buffer_line_up = Null;
 
PHASE_NONE = 0;
PHASE_BUY = 1;
PHASE_SELL = -1;
 
phase=PHASE_NONE;
band_upper = 0;band_lower = 0;
   
for(i = ATR_Period + 1; i < BarCount; i++)
{
    band_upper = CalcPrice[i] + ATR_Multiplier * tr[i];
    band_lower = CalcPrice[i] - ATR_Multiplier * tr[i];
     
    if(phase==PHASE_NONE)
    {
        buffer_line_up[i] = CalcPrice[i];
        buffer_line_down[i] = CalcPrice[i];
    }
    if(phase!=PHASE_BUY && Close[i]>buffer_line_down[i-1] && !IsEmpty(buffer_line_down[i-1])) 
    {
        phase = PHASE_BUY;
        buffer_line_up[i] = band_lower;
        buffer_line_up[i-1] = buffer_line_down[i-1];
    }
    if(phase!=PHASE_SELL && Close[i]<buffer_line_up[i-1] && !IsEmpty(buffer_line_up[i-1]))
    {
        phase = PHASE_SELL;
        buffer_line_down[i] = band_upper;
        buffer_line_down[i-1] = buffer_line_up[i-1];
    }   
    if(phase==PHASE_BUY && ((TrendMode==0 && !IsEmpty(buffer_line_up[i-2])) || TrendMode==1) )
    {
        if(band_lower>buffer_line_up[i-1]) 
        {
            buffer_line_up[i] = band_lower;
        }
        else
        {
            buffer_line_up[i] = buffer_line_up[i-1];
        }
    }
    if(phase==PHASE_SELL && ((TrendMode==0 && !IsEmpty(buffer_line_down[i-2])) || TrendMode==1) )
    {
        if(band_upper<buffer_line_down[i-1])
        {
            buffer_line_down[i] = band_upper;
        }
        else
        {
            buffer_line_down[i] = buffer_line_down[i-1];
        }
    }
}
}
 
SetBarsRequired(sbrAll,sbrAll);

TrendMode = ParamToggle("TrendMode","Off|On",0);
ATR_Multiplier = Param("ATR_Multiplier",2,0.1,10,0.1);
ATR_Period = Param( "ATR_Period",5,1,20,1);
tr = ATR(ATR_Period);
 
CalcPrice = (H+L)/2;
calcTrend_proc(ATR_Period,tr,ATR_Multiplier,TrendMode,CalcPrice);
 

Plot(buffer_line_up,"\ntu",ColorRGB(28,134,238),styleThick);
Plot(buffer_line_down,"\ntd",ColorRGB(205,51,51),styleThick);

 
 
Plot( 2,"",IIf(buffer_line_up,colorGreen,colorBlack),styleOwnScale|styleArea|styleNoLabel, -6, 100 );
Plot( 4,"",IIf(buffer_line_down,colorRed,colorBlack),styleOwnScale|styleArea|styleNoLabel, -2, 100 );
_SECTION_BEGIN("Advanced Elliot Wave ");
Option = ParamToggle("Insert To", "Price Chart|Indicator");
pr=Param("Elliot Wave minimum % move",0.5, 0.25,5,0.25);
//{ Beginner Elliot Wave stuff }
EWpk=PeakBars(H,pr)==0;
EWtr=TroughBars(L,pr)==0;
//{ Intermediate Elliot Wave stuff }
zz=Zig(C,pr);
zzHi=Zig(H,pr);
zzLo=Zig(L,pr);
Avg=(zzHi+zzLo)/2;
//{ Advanced Elliot Wave stuff }
RetroSuccessSecret=IIf(EWpk,zzHi,
IIf(EWtr,zzLo,IIf(Avg>Ref(Avg,-1),H,L)));
EW=Zig(RetroSuccessSecret,pr);
//{ Plot on price chart }

//{ Buy/Sell Elliot Wave stuff }
EWbuy=TroughBars(EW,pr)==1;
EWsell=PeakBars(EW,pr)==1;
PlotShapes(EWbuy*shapeUpArrow,5,0,L,-5);
PlotShapes(EWbuy*shapeUpArrow,5,0,L,-25);
PlotShapes(EWsell*shapeDownArrow,4,0,H,-5);
PlotShapes(EWsell*shapeDownArrow,4,0,H,-25);

//-- Script End -------
_SECTION_END();





_SECTION_BEGIN("Kpl System");

no=Param( "Swing", 8, 1, 55 );
tsl_col=ParamColor( "Color", colorLightGrey );

res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);
Buy=Cover=Cross(C,tsl) ;
Sell=Short=Cross(tsl,C) ;


Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);



SetPositionSize(300,spsShares);
ApplyStop(0,1,10,1);
//-----------end--------------
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy);

BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
PlotShapes(IIf(Cover, shapeHollowCircle, shapeNone),colorWhite, 0,Close,0);  
PlotShapes(IIf(Short, shapeHollowCircle, shapeNone),colorYellow, 0,Close,0); 

Edc=(
WriteIf (Buy AND Ref(shrt,-1), " BUY@ "+C+"  ","")+
WriteIf (Sell AND Ref(Long,-1), " SEll@ "+C+"  ","")+
WriteIf(Sell , "Last Trade Profit Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy  , "Last Trade Profit Rs."+(SellPrice-C)+"",""));
_SECTION_END();


if(aama==1){
_SECTION_BEGIN("ema");
P = ParamField("Field");
Type = ParamList("Type", "Weighted,Simple,Exponential,Double Exponential,Tripple Exponential,Wilders");

Periods89 = Param("Periods180", 180, 2, 300 );
Displacement2 = Param("Displacement2", 2, -50, 50 );
Plot( EMA( P, Periods89 ),
_DEFAULT_NAME(), colorWhite, styleDots, 0, 0, Displacement2 );
_SECTION_END();


}




if(equty==0){
SetOption( "InitialEquity", 100000 );
//SetPositionSize(10000,spsValue);
SetPositionSize( 100, spsPercentOfEquity );

Delay = 0;

SetTradeDelays( Delay, Delay, Delay, Delay );
BuyPrice = Close;
SellPrice = Close;
ShortPrice = Close;
CoverPrice = Close;

LongTermAverage = MA( C, 200 );
midavg50 = MA( C, 50 );
midavg10 = MA( C, 10 );
Buy = Cross( C, LongTermAverage );
Sell = Cross( LongTermAverage, C );
Short = Sell;
Cover = Buy;


e = Equity();
Plot( e, "equity", colorWhite, styleLine | styleOwnScale );
}
_SECTION_BEGIN("Fib Retracements");
    fibs = ParamToggle("Plot Fibs","Off|On",1);
    pctH = Param ("Pivot Hi %", 0.325,0.001,2.0,0.002);
    HiLB = Param ("Hi LookBack",1,1,BarCount-1,1);
    pctL = Param ("Pivot Lo %", 0.325,0.001,2.0,0.002);
    LoLB = Param ("Lo LookBack",1,1,BarCount-1,1);
    Back = Param ("Extend Left = 2",1,1,500,1);
    Fwd  = Param("Plot Forward", 0, 0, 500, 1);
    text = ParamToggle("Plot Text","Off|On",1);
    hts  = Param ("Text Shift", -33.5,-50,50,0.10);
    style =ParamStyle("Line Style",styleLine,styleNoLabel);
x = BarIndex();
pRp  = PeakBars( H, pctH, 1) == 0;
yRp0 = SelectedValue(ValueWhen( pRp, H, HiLB));
xRp0 = SelectedValue(ValueWhen( pRp, x, HiLB));
pSp  = TroughBars( L, pctL, 1) == 0;
ySp0 = SelectedValue(ValueWhen( pSp, L, LoLB));
xSp0 = SelectedValue(ValueWhen( pSp, x, LoLB));
Delta = yRp0 - ySp0;
 
function fib(ret)
{
retval = (Delta * ret);
Fibval = IIf(ret < 1.0
AND xSp0 < xRp0, yRp0 - retval, IIf(ret < 1.0
AND xSp0 > xRp0, ySp0 + retval,IIf(ret > 1.0
AND xSp0 < xRp0, yRp0 - retval, IIf(ret > 1.0
AND xSp0 > xRp0, ySp0 + retval, Null))));
return FibVal;
}
 
x0 = Min(xSp0,xRp0)-Back;
x1 = (BarCount -1);
//////////////////////////////////////////////////////////////////
r236 = fib(0.236);  r236I = LastValue (r236,1);
r382 = fib(0.382);  r382I = LastValue (r382,1);
r050 = fib(0.50);       r050I = LastValue (r050,1);
r618 = fib(0.618);  r618I = LastValue (r618,1);
r786 = fib(0.786);  r786I = LastValue (r786,1);
e127 = fib(1.27);       e127I = LastValue (e127,1);
e162 = fib(1.62);       e162I = LastValue (e162,1);
e200 = fib(2.00);       e200I = LastValue (e200,1);
e262 = fib(2.62);       e262I = LastValue (e262,1);
e424 = fib(4.24);       e424I = LastValue (e424,1);
//////////////////////////////////////////////////////////////////
p00 = IIf(xSp0 > xRp0,ySp0,yRp0);    p00I = LastValue (p00,1);
p100 = IIf(xSp0 < xRp0,ySp0,yRp0);   p100I = LastValue (p100,1);
color00 =IIf(xSp0 > xRp0,colorLime,colorRed);
color100 =IIf(xSp0 < xRp0,colorLime,colorRed);
//////////////////////////////////////////////////////////////////
numbars = LastValue(Cum(Status("barvisible")));
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);
//////////////////////////////////////////////////////////////////
if(fibs==1)
{
Plot(LineArray(xRp0-Fwd,yRp0,x1,yRp0,Back),"PR",32,8|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(xSp0-Fwd,ySp0,x1,ySp0,Back),"PS",27,8|styleNoRescale,Null, Null,Fwd);
//Plot(LineArray(x0-Fwd,r236,x1,r236,Back),"",45,style|styleNoRescale,Null, Null,Fwd);
//Plot(LineArray(x0-Fwd,r382,x1,r382,Back),"",44,style|styleNoRescale,Null, Null,Fwd);
//Plot(LineArray(x0-Fwd,r050,x1,r050,Back),"",41,style|styleNoRescale,Null, Null,Fwd);
//Plot(LineArray(x0-Fwd,r618,x1,r618,Back),"",43,style|styleNoRescale,Null, Null,Fwd);
//Plot(LineArray(x0-Fwd,r786,x1,r786,Back),"",42,style|styleNoRescale,Null, Null,Fwd);
//Plot(LineArray(x0-Fwd,e127,x1,e127,Back),"e127",47,style|styleNoRescale,Null, Null,Fwd);
//Plot(LineArray(x0-Fwd,e162,x1,e162,Back),"e162",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e200,x1,e200,Back),"p200",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e262,x1,e262,Back),"p262",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e424,x1,e424,Back),"p424",25,style|styleNoRescale,Null, Null,Fwd);
}
//////////////////////////////////////////////////////////////////
if(text==1)
{
PlotText(" 0% = " + WriteVal(p00,fraction),     LastValue(BarIndex())-(numbars/hts), p00I  + 0.05, color00);
PlotText("23% = " + WriteVal(r236,fraction), LastValue(BarIndex())-(numbars/hts), r236I + 0.05, 45);
PlotText("38% = " + WriteVal(r382,fraction), LastValue(BarIndex())-(numbars/hts), r382I + 0.05, 44);
PlotText("50% = " + WriteVal(r050,fraction), LastValue(BarIndex())-(numbars/hts), r050I + 0.05, 41);
PlotText("62% = " + WriteVal(r618,fraction), LastValue(BarIndex())-(numbars/hts), r618I + 0.05, 43);
PlotText("78% = " + WriteVal(r786,fraction), LastValue(BarIndex())-(numbars/hts), r786I + 0.05, 42);
PlotText("100% = " + WriteVal(p100,fraction), LastValue(BarIndex())-(numbars/hts),p100I + 0.05, color100);
PlotText("127% = " + WriteVal(e127,fraction), LastValue(BarIndex())-(numbars/hts),e127I + 0.05, 47);
PlotText("162% = " + WriteVal(e162,fraction), LastValue(BarIndex())-(numbars/hts),e162I + 0.05, 47);
PlotText("200% = " + WriteVal(e200,fraction), LastValue(BarIndex())-(numbars/hts),e200I + 0.05, 47);
PlotText("262% = " + WriteVal(e262,fraction), LastValue(BarIndex())-(numbars/hts),e262I + 0.05, 47);
PlotText("424% = " + WriteVal(e424,fraction), LastValue(BarIndex())-(numbars/hts),e424I + 0.05, 25);
}
_SECTION_END();
//////////////////////////////////////////////////////////////////


tchoice=Param("Title Selection ",2,1,2,1);


_N(Title = EncodeColor(colorWhite)+StrFormat(" {{NAME}} -   {{INTERVAL}}      {{DATE}}    Open:  %g,    High:  %g,     Low:  %g,     Close:  %g     {{VALUES}}",O, H, L, C, SelectedValue( ROC( C, 1   ) ) ));

//////////////////////////////////////////////////////////////////
if (tchoice==2 )
{
Title = EncodeColor(colorWhite)+  Date() + "   Tick = " + EncodeColor(5) + Interval()+
EncodeColor(colorWhite) + "     Open = " + EncodeColor(colorWhite) + O +
EncodeColor(colorWhite) + "     High = " + EncodeColor(5) + H +
EncodeColor(colorWhite) + "      Low = " + EncodeColor(colorRed) + L +
EncodeColor(colorWhite) + "     Close = " + EncodeColor(colorWhite) + C + "\n" +
EncodeColor( colorWhite) +"_______________"+"\n"+
EncodeColor( colorWhite)  + "424%   =  "    +   EncodeColor(25)+ e424 + " " +"\n"+
EncodeColor( colorWhite)  + "262%   =  "    +   EncodeColor(47)+ e262 + " " +"\n"+
EncodeColor( colorWhite)  + "200%   =  "    +   EncodeColor(47)+ e200 + " " +"\n"+
EncodeColor( colorWhite)  + "162%   =  "    +   EncodeColor(47)+ e162 + " " +"\n"+
EncodeColor( colorWhite)  + "127%   =  "    +   EncodeColor(47)+ e127 + " " +"\n"+
EncodeColor( colorYellow) + "  Res    =  "  +   EncodeColor(32)+ p100 + " " +"\n"+
EncodeColor( colorWhite)  + "  78%   =  "   +   EncodeColor(42)+ r786 + " " +"\n"+
EncodeColor( colorWhite)  + "  62%   =  "   +   EncodeColor(43)+ r618 + " " +"\n"+
EncodeColor( colorWhite)  + "  50%   =  "   +   EncodeColor(41)+ r050 + " " +"\n"+
EncodeColor( colorWhite)  + "  38%   =  "   +   EncodeColor(44)+ r382 + " " +"\n"+
EncodeColor( colorWhite)  + "  23%   =  "   +   EncodeColor(45)+ r236+ " " +"\n"+
EncodeColor( colorYellow) + "  Sup   =   "  +   EncodeColor(34)+ p00 + " " ;
}


GraphXSpace=5;
 
SetChartOptions(1, chartShowDates);   // put dates at bottom of chart
 
pr = Param("Elliot Wave minimum % move", 1, 1, 100);
// Beginner Elliot Wave stuff
EWpk = PeakBars(H, pr, 1) == 0;
EWtr = TroughBars(L, pr, 1) == 0;
 
// Intermediate Elliot Wave stuff
zz = Zig(C, pr);
zzHi = Zig(H, pr);
zzLo = Zig(L, pr);
Avg = (zzHi+zzLo)/2;
 
// Advanced Elliot Wave stuff
RetroSuccessSecret = IIf(EWpk, zzHi, IIf(EWtr, zzLo, IIf(Avg > Ref(Avg,-1), H, L)));
EW = Zig(RetroSuccessSecret, pr);
 
// Plot on price chart
/*Plot(C, "Close", colorBlack, styleCandle);*/
//Plot(EW, "EW", colorWhite, styleLine);
 
// Plot buy and sell arrows
Buy = TroughBars(EW, pr, 1) == 0;
Sell = PeakBars(EW, pr, 1) ==0;
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

 
AlertIf( Buy , "SOUND C:\\Windows\\Media\\Notify.wav", "Audio alert", 2 );
AlertIf( Sell, "SOUND C:\\Windows\\Media\\Tada.wav", "Audio alert", 2 );
 
_SECTION_BEGIN("Elliot Fractals");
 
/*
The basic definition of an 'up' fractal is a bar high that is both higher than the two bars immediately preceding it
and higher than the two bars immediately following it.
The lows of the bars are NOT considered in determining the up fractal progression.
 
If two bars in the progression have equal highs followed by two consecutive bars with lower highs,
then a total of six bars rather than the usual five bars will make up the progression.
The first High becomes the counting fractal. Reverse for 'down' fractals.
 
The 5 bar formation works best on Daily or longer time frame charts.For intraday data charts we often use 9 bar, 13 bar and 21 bar formations for fractal counting
*/
Up5BarFractal = Ref(H,-2) < H AND Ref(H,-1) < H AND Ref(H,1) < H AND Ref(H,2) < H;
Up6BarFractal = Ref(H,-2) < H AND Ref(H,-1) < H AND (H == Ref(H,1)) AND Ref(H,2) < H AND Ref(H,3) < H;
Down5BarFractal = Ref(L,-2) > L AND Ref(L,-1) > L AND Ref(L,1) > L AND Ref(L,2) > L;
Down6BarFractal = Ref(L,-2) > L AND Ref(L,-1) > L AND (L == Ref(L,1)) AND Ref(L,2) > L AND Ref(L,3) > L;
 
//TODO: More filtering: Show only troughs that are around atrough in trix(9).

 
Up = (Up5BarFractal OR Up6BarFractal);
Down = (Down5BarFractal OR Down6BarFractal);
//Removing false fractals:
DownSignal = Flip(Ref(Up,-1), Ref(Down,-1));
UpSignal = Flip(Ref(Down,-1), Ref(Up,-1));
 
LastHigh[0] = H[0];
LastLow[0] = L[0];
 
LastLowIndex = 0;
LastHighIndex = 0;
Valid = 0;
for (i=1; i < BarCount; i++)
{
 
    LastHigh[i] = LastHigh[i-1];
    LastLow[i] = LastLow[i-1];
    if (Up[i])
    {                              
        Valid[i] = True;
        if (DownSignal[i])
        {
            //Sequence of 2 Up Fractals. Validate only the higher one.
            Valid[i] = H[i] >= H[LastHighIndex];
            Valid[LastHighIndex] = H[LastHighIndex] >  H[i];
        }
        LastHigh[i] = Max(H[i], H[LastHighIndex ]);
        LastHighIndex = i; 
    }
 
    if (Down[i])
    {  
        Valid[i] = True;
        if (UpSignal[i])
        {
            //Sequence of 2 Down Fractals. Validate only the lower one.
            Valid[i] = L[i] <= L[LastLowIndex];
            Valid[LastLowIndex] = L[LastLowIndex] <  L[i];
        }
     
        LastLow[i] = Min(L[i], L[LastLowIndex]);
        LastLowIndex = i;      
    }  
}
 
TrixN = Trix(9);
TroughLow = Ref(TrixN, -3) > TrixN AND Ref(TrixN, -2) > TrixN AND Ref(TrixN, -1) > TrixN AND Ref(TrixN, 1) > TrixN AND Ref(TrixN, 2) > TrixN AND Ref(TrixN, 3) > TrixN;
TroughHigh = Ref(TrixN, -3) < TrixN AND Ref(TrixN, -2) < TrixN AND Ref(TrixN, -1) < TrixN AND Ref(TrixN, 1) < TrixN AND Ref(TrixN, 2) < TrixN AND Ref(TrixN, 3) < TrixN;
//TroughLow = Ref(TrixN, -2) > TrixN AND Ref(TrixN, -1) > TrixN AND Ref(TrixN, 1) > TrixN AND Ref(TrixN, 2) > TrixN;
//TroughHigh = Ref(TrixN, -2) < TrixN AND Ref(TrixN, -1) < TrixN AND Ref(TrixN, 1) < TrixN AND Ref(TrixN, 2) < TrixN;
ZeroValid = Cross(TrixN, 0) OR Cross(0, TrixN) OR Ref(Cross(TrixN, 0),1) OR Ref(Cross(0, TrixN),1);
ValidLow = TroughLow OR Ref(TroughLow, 1) OR Ref(TroughLow, 2) OR Ref(TroughLow, 3) OR Ref(TroughLow, 4);// OR Ref(TroughLow, 5));
ValidHigh = TroughHigh OR Ref(TroughHigh, 1) OR Ref(TroughHigh, 2) OR Ref(TroughHigh, 3) OR Ref(TroughHigh, 4);// OR Ref(TroughHigh, 5));
 
//Plot(LastHigh-10 ,"LastHigh", colorBlue, styleLine);
//Plot(LastLow-10 ,"LastLow ", colorRed, styleLine);
//Plot(Valid*5 + 10 ,"LastLow ", colorGreen, styleLine | styleThick);
 
//PlotShapes( IIf(Down AND Valid,shapeSmallUpTriangle,0) ,colorGreen, 0, L,-12);
//PlotShapes( IIf(Up AND Valid,shapeSmallDownTriangle,0) ,colorRed, 0, H,-12);
Maxi = Up AND (ValidHigh OR ZeroValid);
Mini = Down AND (ValidLow OR ZeroValid);
//PlotShapes( IIf(Down AND (ValidLow OR ZeroValid),shapeSmallUpTriangle,0) ,colorBlue, 0, L,-12);
//PlotShapes( IIf(Up AND (ValidHigh OR ZeroValid),shapeSmallDownTriangle,0) ,colorOrange, 0, H,-12);
//Plot(UpSignal*3+5,"UpSignal", colorBlue, styleLine| styleThick);
//Plot(DownSignal*3 ,"DownSignal", colorRed, styleLine| styleThick);
 
/*
LastMaxi = 0;
LastMini = 0;
ElliotLines = 0;
State = 0;
for (i=1; i < BarCount; i++)
{
    State[i] = State[i-1];
    if (Maxi[i])
    {      
        State[i] = 1;//down
    }
 
    if (Mini[i])
    {      
        State[i] = 2;
    }
 
}
 
PlotShapes(IIf(State > 0, shapeSmallCircle, 0), IIf(State == 1, colorRed, colorBlue), 0, IIf(State == 1, H, L), -5);
*/
//Line = LineArray( x0, y0, x1, y1, 1 );
//Plot( Line, "Trend line", colorBlue );
 
/*
Wave B
Usually 50% of Wave A
Should not exceed 75% of Wave A
Wave C
either 1 x Wave A
or 1.62 x Wave A
or 2.62 x Wave A
*/
function CorrectiveRatios(StartPrice, A, B, C, RatioDelta, Delta)
{
     
    ALength = abs(startPrice - A);  BLength = abs(A-B);
    CLength = abs(B-C);
 
    Ratio1 = BLength  / CLength ;
    Cond1 = Ration1 >= 0.5 - RatioDelta AND ratio1 <= 0.75 + RatioDelta;
    Cond2 = abs(Clength - ALength) < Delta  OR abs(Clength - 1.62 * ALength) < Delta OR abs(CLength - 2.62 * ALength) < Delta;
     
    return Cond1 AND Cond2;
}
 
function ImpulseRules(StartPrice, One, Two, Three, Four, Five)
{
    //Wave 2 should be beneath wave 1 start:
    Cond1 = Two > StartPrice AND Two < One;
    //Wave 4 - the same:
    Cond2 = Four > Two AND Four < Three;
    //Wave 5 should be <= wave 3
    Cond3 = abs(Three-Two) >= abs(Five - Four);
    //Wave 1 should be smaller than wave five, making wave 3 the biggest:
    Cond4 = abs(StartPrice - One) < abs(Five - Four);
    return Cond1 AND Cond2 AND Cond3 AND Cond4;
}
_SECTION_END();

if(SR == 1) {


_SECTION_BEGIN("SR ");

//sbrAll

SetBarsRequired(100,0);
xx = Cum(1);
nbar = Param("nbar",5,2,50,1);

PHigh = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
PHighPrice = ValueWhen(PHigh,H);
PLow = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
PLowPrice = ValueWhen(PLow,L);

startval_L = ValueWhen(PLow,L,1);
endval_L = ValueWhen(PLow,L,0);
startbar_L = ValueWhen(PLow,xx,1);
endbar_L = ValueWhen(PLow,xx,0);
aa_L = (endval_L-startval_L)/(endbar_L-startbar_L);
bb_L = startval_L;
trendline_L = aa_L * (xx - startbar_L) + bb_L;
dtrendline_L = trendline_L - Ref(trendline_L,-1);

startval_L_extend = ValueWhen(PLow,L,2);
endval_L_extend = ValueWhen(PLow,L,1);
startbar_L_extend = ValueWhen(PLow,xx,2);
endbar_L_extend = ValueWhen(PLow,xx,1);
aa_L_extend = (endval_L_extend-startval_L_extend)/(endbar_L_extend-startbar_L_extend);
bb_L_extend = startval_L;
trendline_L_extend = aa_L_extend * (xx - startbar_L) + endval_L_extend;

dtrendline_L_extend = trendline_L_extend - Ref(trendline_L_extend,-1);
dtrendline_L_extend = IIf(PLow,Ref(dtrendline_L,-1),dtrendline_L_extend);

startval_L_extend2 = ValueWhen(PLow,L,3);
endval_L_extend2 = ValueWhen(PLow,L,2);
startbar_L_extend2 = ValueWhen(PLow,xx,3);
endbar_L_extend2 = ValueWhen(PLow,xx,2);
aa_L_extend2 = (endval_L_extend2-startval_L_extend2)/(endbar_L_extend2-startbar_L_extend2);
bb_L_extend2 = endval_L_extend2;
trendline_L_extend2 = aa_L_extend2 * (xx - endbar_L_extend2) + endval_L_extend2;

dtrendline_L_extend2 = trendline_L_extend2 - Ref(trendline_L_extend2,-1);
dtrendline_L_extend2 = IIf(PLow,Ref(dtrendline_L_extend,-1),dtrendline_L_extend2);

startval_H = ValueWhen(PHigh,H,1);
endval_H = ValueWhen(PHigh,H,0);
startbar_H = ValueWhen(PHigh,xx,1);
endbar_H = ValueWhen(PHigh,xx,0);
aa_H = (endval_H-startval_H)/(endbar_H-startbar_H);
bb_H = startval_H;
trendline_H = aa_H * (xx - startbar_H) + bb_H;
dtrendline_H = trendline_H - Ref(trendline_H,-1);

startval_H_extend = ValueWhen(PHigh,H,2);
endval_H_extend = ValueWhen(PHigh,H,1);
startbar_H_extend = ValueWhen(PHigh,xx,2);
endbar_H_extend = ValueWhen(PHigh,xx,1);
aa_H_extend = (endval_H_extend-startval_H_extend)/(endbar_H_extend-startbar_H_extend);
bb_H_extend = startval_H;
trendline_H_extend = aa_H_extend * (xx - startbar_H) + endval_H_extend;
dtrendline_H_extend = trendline_H_extend - Ref(trendline_H_extend,-1);
dtrendline_H_extend = IIf(PHigh,Ref(dtrendline_H,-1),dtrendline_H_extend);

startval_H_extend2 = ValueWhen(PHigh,H,3);
endval_H_extend2 = ValueWhen(PHigh,H,2);
startbar_H_extend2 = ValueWhen(PHigh,xx,3);
endbar_H_extend2 = ValueWhen(PHigh,xx,2);
aa_H_extend2 = (endval_H_extend2-startval_H_extend2)/(endbar_H_extend2-startbar_H_extend2);
bb_H_extend2 = endval_H_extend2;
trendline_H_extend2 = aa_H_extend2 * (xx - endbar_H_extend2) + endval_H_extend2;

dtrendline_H_extend2 = trendline_H_extend2 - Ref(trendline_H_extend2,-1);
dtrendline_H_extend2 = IIf(PHigh,Ref(dtrendline_H_extend,-1),dtrendline_H_extend2);

tld = ParamToggle("All trendlines","Show|Hide",1);
if (tld)
{

trendline_L = IIf(dtrendline_L > 0,trendline_L,Null);
trendline_L_extend = IIf(dtrendline_L_extend > 0,trendline_L_extend,Null);
trendline_L_extend2 = IIf(dtrendline_L_extend2 > 0,trendline_L_extend2,Null);
trendline_H = IIf(dtrendline_H < 0,trendline_H,Null);
trendline_H_extend = IIf(dtrendline_H_extend < 0,trendline_H_extend,Null);
trendline_H_extend2 = IIf(dtrendline_H_extend2 < 0,trendline_H_extend2,Null);
}

trendline_L_extend2 = IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null);
trendline_H_extend2 = IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null);

GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
chartflag = ParamToggle("Heikin Ashi","show|hide",1);
if (chartFlag)
{
Plot( C, "",colorWhite, styleBar );
}
else
{
HaClose = (O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
//PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "Modified " + Name(), colorWhite, styleBar);
}

//PlotShapes(shapeHollowStar*PLow,colorYellow,0,L,-10);
//PlotShapes(shapeHollowStar*PHigh,colorRed,0,H,10);

Plot(trendline_L, "", colorBrightGreen,styleLine);

Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend,Null), "", colorLightGrey,styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(Plow) > nbar,trendline_L_extend,Null), "",colorBrightGreen, styleNoLine | styleDots | styleThick);

Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null), "",colorDarkGreen,styleNoLine | styleDots| styleThick);

Plot(trendline_H, "", colorRed,styleLine);

Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend,Null), "", colorLightGrey, styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(PHigh) > nbar,trendline_H_extend,Null), "",colorOrange, styleNoLine | styleDots | styleThick);

Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null), "",colorOrange, styleNoLine | styleDots | styleThick);


signalFlag = ParamToggle("Possible Signals","show|hide",1);

if (signalFlag)
{

Buy = (!IsEmpty(trendline_H_extend) AND Cross(C,trendline_H_extend) AND BarsSince(PHigh) > nbar) OR
(!IsEmpty(trendline_H_extend2) AND Cross(C,trendline_H_extend2) AND !PHigh) OR
(PHigh AND C > trendline_H_extend2 AND Ref(C,-1) < Ref(trendline_H_extend,-1) AND !IsEmpty(trendline_H_extend) AND !IsEmpty(trendline_H_extend2) );
BuyPrice = C;
Short = (!IsEmpty(trendline_L_extend) AND Cross(trendline_L_extend,C) AND BarsSince(PLow) > nbar) OR
(!IsEmpty(trendline_L_extend2) AND Cross(trendline_L_extend2,C) AND !PLow) OR
(PLow AND C < trendline_L_extend2 AND Ref(C,-1) > Ref(trendline_L_extend,-1) AND !IsEmpty(trendline_L_extend) AND !IsEmpty(trendline_L_extend2));
ShortPrice = C;
Sell = 0;
Cover = 0;
}
_SECTION_END();
}



_SECTION_BEGIN("Stupid Functions");

function HAI_F1(no)
{
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
return (IIf(avn==1,sup,res));
}

function HAI_F2(no)
{
return (Cross(C,HAI_F1(no)));
}

function HAI_F3(no)
{
return (Cross(HAI_F1(no),C));
}

function HAI_F4(no)
{
prev=AMA2(C,1,0);
d=IIf(C>Ref(Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),-1),Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),
IIf(C<Ref(Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),-1),Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),PREV));
a=Cross(Close,d);
b=Cross(d,Close);
return (IIf(BarsSince(a)<BarsSince(b),1,0));
}

function HAI_F5(no)
{
state = HAI_F4(no);
s=state>Ref(state,-1);
ss=state<Ref(state,-1);
return (state==Ref(state,-1));
}


function HAI_F6(p,n,s,m)
{
return (PDI(p)>MDI(n)AND Signal(s)<MACD(m));
}

function HAI_F7(p,n,s,m)
{
return (MDI(n)>PDI(p)AND Signal(s)>MACD(m));
}


_SECTION_END();


_SECTION_BEGIN("Swing");

no = 22;
sloss = HAI_F1(no);
a = HAI_F2(no);
b = HAI_F3(no);
state = HAI_F4(no);
sss = HAI_F5(no);
uptrend = HAI_F6(20,10,29,22);
downtrend = HAI_F7(20,10,29,13);

style = a * styleStaircase + b * styleStaircase;
//PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));
_SECTION_END();

col=IIf(state == 1 ,51,IIf(state ==0,4,1));
//Plot(C,"",Col,64);
//PlotShapes( shapeUpArrow * a, colorGreen,0,L);
//PlotShapes( shapeDownArrow * b, colorRed,0,H);

Filter = a OR b OR sss ;
AddColumn(C,"close",1.2);
AddColumn( IIf( a, 66,1 ), "buy", formatChar, 1, bkcolor =IIf (a,colorYellow, colorPink ));
AddColumn( IIf( b, 83,1 ), "sell", formatChar, 1, bkcolor =IIf (b,colorPink, colorYellow ));
AddColumn( IIf( sss, 87,1 ), "wait", formatChar, 1, bkcolor =IIf (sss,colorYellow, colorRed ));


_SECTION_BEGIN("Intraday AFL System");
Buy = a;
Sell = b;
/*
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-30);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-35);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=30);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=40);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-35);

dist = ATR(20);
for (i=0; i<BarCount; i++) {
if ( Buy[i] )
PlotText("Buy:" + O[ i ], i, L[ i ]-3*dist[i], colorGreen);
if ( Sell[i] )
PlotText("Sell:" + O[ i ], i, H[ i ]+3*dist[i], colorRed);
}
_SECTION_END();*/


//d = Close > Ref( ChandelierHL(ATR(3),20), -1);
//e =Close < Ref( ChandelierHL(ATR(3),20), -1);
//f = Close < Ref( ChandelierHL(ATR(3),20), -1);
//g = Close > Ref( ChandelierHL(ATR(3),20), -1);

Buy = a AND uptrend ;
Short = b AND downtrend ;
Sell = b AND downtrend ;
Cover = a AND uptrend;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Cover=ExRem(Cover,Short);
Short=ExRem(Short,Cover);

Filter=Buy OR Sell;
Filter= Cover OR Short;

AddColumn( Buy, "Buy", 1);
AddColumn(Sell, "Sell", 1);
AddColumn(Close,"Close",1.2);
AddColumn(Volume,"Volume",1.0);

// Plot the Buy and Sell arrows.
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
//PlotShapes(shape, IIf(Buy,colorGreen,colorRed), 0, IIf(Buy,Low,High));
//attack
if (stoploss ==1)
{
Plot(sloss,"Swing",colorCustom8,styleDots);
}
//SetChartBkGradientFill( ParamColor("BgTop", ColorRGB( 172,172,172 )),

//ParamColor("BgBottom", ColorRGB( 172,172,172 )),ParamColor("titleblock",ColorRGB( 172,172,172 )));
//Alerts
AlertIf( Buy, "SOUND C:\\Windows\\Media\\tada.wav", "Buy",1,1+2+4+8);
AlertIf( Short, "SOUND C:\\Windows\\Media\\notify.wav", "Short",2,1+2+4+8);

GraphXSpace = 5;

/* -------------------------------------------------------------------------------------- */

//SetChartBkColor(colorDarkOliveGreen = 46);
SetChartOptions(0,chartShowArrows|chartShowDates);

/* -------------------------------------------------------------------------------------- */

////////////////////////////////////////////////////////////////////////////////////////////////
messageboard = ParamToggle("Message Board","Show|Hide",0);
showsl = ParamToggle("Stop Loss Line", "Show|Hide", 0);

style = a * styleStaircase + b * styleStaircase;

PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));

exitlong = Cross(sloss, H);
PlotShapes(exitlong * shapeNone, colorBlack,0,H,-10);
exitshort = Cross(L, sloss);
PlotShapes(exitshort * shapeNone, colorBlack,0,L,-15);

Buy = exitshort;
Sell = exitlong;
//Short = Sell;
//Cover = Buy;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
//Short = ExRem(Short, Cover);
//Cover = ExRem(Cover, Short);
AlertIf( Buy, "", "BUY @ " + C, 1 );
AlertIf( Sell, "", "SELL @ " + C, 2 );

for (i=BarCount-1; i>1; i--) {
if (Buy[i] == 1) {
entry = O[i];
sig = "BUY";
sl = sloss[i];
tar1 = entry + (entry * .0040);
tar2 = entry + (entry * .0085);
tar3 = entry + (entry * .0179);
bars = i;
i = 0;
}
if (Sell[i] == 1) {
sig = "SELL";
entry = O[i];
sl = sloss[i];
tar1 = entry - (entry * .0040);
tar2 = entry - (entry * .0085);
tar3 = entry - (entry * .0212);
bars = i;
i = 0;
}
}

Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, sloss[BarCount-1], Ref(sloss, -1));
sl = ssl[BarCount-1];

printf("Last " + sig + " Signal came " + (BarCount-bars) + " bars ago");
printf("\n" + sig + " @ : " + entry + "\nStop Loss : " + sl + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"+ "\nTarget_1 : " + tar1 + "\nTarget_2 : " + tar2 + "\nTarget_3 : " + tar3);
printf("\nCurrent P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2));

if (messageboard == 0) {
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );

if (sig =="BUY")
GfxSelectSolidBrush( colorGreen ); // this is the box background color
else
GfxSelectSolidBrush( colorRed ); // this is the box background color

pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 05;
x2 = 200;

y = Status( "pxchartheight" );

GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 143, x2, y , 7, 7 ) ;
//GfxTextOut( ( " AFLIntraday "),80, y-140) ;
GfxTextOut( Name(),88,y-140);

GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 73,y-120);
GfxTextOut( ("Trailing SL : " + sl + " "), 88, y-60);
GfxTextOut( ("TGT:1 : " + tar1), 78,y-100);
GfxTextOut( ("TGT:2 : " + tar2),78,y-80);
GfxTextOut( ("Current P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)),88,y-40);;
}

_SECTION_BEGIN("Parameters");
LB1= Param("Long LookBack Periods",42,1,60,1);
LB3= Param("Mid LookBack Periods",18,1,30,1);
LB2= Param("Short LookBack Periods",6,1,30,1);
 
_SECTION_END();

_SECTION_BEGIN("EMA");
Plot( EMA( Close, 34 ), "", IIf(EMA( Close, 34 ) > Ref(EMA( Close, 34 ),-1),colorGreen,colorRed),styleThick ); 
Plot( EMA( Close, 33 ), "", IIf(EMA( Close, 34 ) > Ref(EMA( Close, 34 ),-1),colorGreen,colorRed),styleThick+styleNoLabel ); 
Plot( EMA( Close, 32 ), "", IIf(EMA( Close, 34 ) > Ref(EMA( Close, 34 ),-1),colorGreen,colorRed),styleThick+styleNoLabel ); 
 
_SECTION_END();

if(KeltnerBands==1) {

Smooth = Param("Smoothness",14,2,100,1);
Length = 20; Num_ATRs = 1.8;
// Length and Num_ATRs parameters should be personalized for your preferred settings.
 
Mov_Avg = MA((H+L+C)/3,Length);
KUP = Mov_Avg + Num_ATRs * ATR(Length);
KDOWN = Mov_Avg - Num_ATRs * ATR(Length);
 
 
Plot (MA(KUP,smooth),"KUP",colorGreen,styleLine);
Plot (MA(KDown,smooth), "Kdown",colorOrange,styleSwingDots);
Plot (MA(Mov_Avg,smooth),"Mov_Avg",colorYellow , styleDashed);

}

//**pink line///////////////
m = Param("Pds", 17, 10, 30, 1 );
     
    a=MA(Close,m)+abs(MA(Close,m)-Ref(MA(Close,m),-1));
    b=MA(Close,m)+MA(Close,m)-Ref(MA(Close,m),-1);
    balance=IIf(MA(Close,m)<b,b,MA(Close,m));
    pma=balance;
    sig = MA(2*balance-EMA(C,3),1);
    s=(pma>sig);
    pm=MA(C,5);
    pmcolor=IIf(pm>Ref(pm,-1),10, 11);
    Plot(pm,"PM", pmcolor,4);

 //**pink line///////////////
_SECTION_BEGIN("BBands");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 100, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorWhite );
Color = ColorBlend( Color,  GetChartBkColor(), 0.5 );
Style = ParamStyle("Style", styleLine | styleNoLabel ) | styleNoRescale;;
Plot( bbt = BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style ); 
Plot( bbb = BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style ); 
PlotOHLC( bbt, bbt, bbb, bbb, "", ColorBlend( Color, GetChartBkColor(), 0.7 ), styleNoLabel | styleCloud | styleNoRescale, Null, Null, Null, -1 );
_SECTION_END();
 
 
 
 
 
_SECTION_BEGIN("HA");


tchoice=Param("Title Selection ",2,1,2,1);

Q=Param("% Change",2.1,1,10,1);
Z= Ref(Zig(C ,q ),-1) ;
HH=Ref(((Z<Ref(Z,-1) AND Ref(Z,-1) > Ref(Z,-2)) AND (Peak(z,q,1 ) >Peak(Z,q,2))),-1);
LH=Ref(((Z<Ref(Z,-1) AND Ref(Z,-1) > Ref(Z,-2)) AND (Peak(Z,q,1 ) <Peak(Z,q,2))),-1);
HL=Ref(((Z>Ref(Z,-1) AND Ref(Z,-1) < Ref(Z,-2)) AND (Trough(Z,q,1 ) >Trough(Z,q,2))),-1);
LL=Ref(((Z>Ref(Z,-1) AND Ref(Z,-1) < Ref(Z,-2)) AND (Trough(Z,q,1 ) <Trough(Z,q,2))),-1);
GraphXSpace = 5;
dist = 0.5*ATR(20);

for( i = 0; i < BarCount; i++ )
{
if( HH[i] ) PlotText( "HH", i, H[ i ]+dist[i], colorCustom12 );
if( LH[i] ) PlotText( "LH", i, H[ i ]+dist[i], colorCustom12 );
if( HL[i] ) PlotText( "HL", i, L[ i ]-dist[i], colorBlack );
if( LL[i] ) PlotText( "LL", i, L[ i ]-dist[i], colorBlack );

}

//calculates floor pivots from previous days data & plots it on the present intraday chart.
//RED LINE = RESISTANCE
//RED DASHED LINE = MINI RESISTANCE
//BLUE LINE = SUPPORT
//BLUE DASHED LINE = MINI SUPPORT
//YELLOW DOTTED LINE = PIVOT
//RED DOTTED LINE = PREVIOUS Day High
//BLUE DOTTED LINE = PREVIOUS Day Low
//BLACK DOTTED LINE = PREVIOUS Day Close


//2 - IN - 1 PRICE CHART
Pstyle = ParamToggle("Heikin Ashi","On|Off",1);
if(Pstyle==0){
HaClose = (O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
//PlotOHLC(HaOpen,HaHigh,HaLow,HaClose,"",IIf(HaClose > HaOpen,colorGreen,colorBrown),styleCandle);

//Sell = (abs(HaOpen -HaClose )<= ((HaHigh -HaLow )*0.1));
//Buy = (abs(HaOpen -HaClose )<= ((HaHigh -HaLow )*0.1)) ;

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

//PlotShapes( IIf( Sell, shapeDownArrow+ shapePositionAbove, shapeNone), colorCustom12 );
//Plot(EMA(HaClose,20), "", colorBlue , styleLine|styleThick );
//Plot(Wilders(H,260.6), "",colorCustom12 , styleDashed|styleThick );
//Plot(EMA(HaClose,10), "",colorTan, styleLine|styleThick );



TimeFrameSet(inHourly);
Hh=HHV (L,24);
Ll =LLV (H,24);
TimeFrameRestore();
Hh1=(TimeFrameExpand( Hh, inHourly));
Ll1=(TimeFrameExpand( Ll, inHourly));

Plot(Hh1 , "27dayssellbelclose",colorRed, 4+8+2048);
Plot(Ll1 , "27daysbuyabvclose",colorGreen, 4+8+2048);

Buy=Cross(C,LL);
Sell=Cross(HH,C);
Filter=Buy OR Sell;



_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 ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
}


Pivot = ParamToggle("Pivot", "weekly|monthly", 1);

if(Pivot)
{
H1 = TimeFrameGetPrice("H", inMonthly, -1); // yesterdays high
L1 = TimeFrameGetPrice("L", inMonthly, -1); // low
C1= TimeFrameGetPrice("C", inMonthly, -1); // close


}
else
{
H1 = TimeFrameGetPrice("H", inWeekly, -1); // yesterdays high
L1 = TimeFrameGetPrice("L", inWeekly, -1); // low
C1= TimeFrameGetPrice("C", inWeekly, -1); // close
}

// PIVOT Calculation
P = ( H1+ L1+ C1 )/3;
R1 = P+(H1-L1)/2-(P-C1)/2;
R2 = H1-L1+P;
R3 = H1-L1+R1;
R4 = H1-L1+R2;
R5 = H1-L1+R3;
R6 = H1-L1+R4;
R7 = H1-L1+R5;
R8 = H1-L1+R6;
R9 = H1-L1+R7;
R10 = H1-L1+R8;



S1 = P -(H1-L1)/2-(P-C1)/2;
S2 = P -(H1-L1);
S3 = S1 -(H1-L1);
S4 = S2 -(H1-L1);
S5 = S3 -(H1-L1);
S6 = S4 -(H1-L1);
S7 = S5 -(H1-L1);
S8 = S6 -(H1-L1);
S9 = S7 -(H1-L1);
S10 =S8 -(H1-L1);













// PIVOT mid points
MIDR1 = (P+R1)/2;
MIDR2 = (R1+R2)/2;
MIDR3 = (R2+R3)/2;
MIDR4 = (R3+R4)/2;
MIDR5 = (R4+R5)/2;
MIDR6 = (R5+R6)/2;
MIDR7 = (R6+R7)/2;
MIDR8 = (R7+R8)/2;
MIDR9 = (R8+R9)/2;
MIDR10 = (R9+R10)/2;




MIDS1 = (P+S1)/2;
MIDS2 = (S1+S2)/2;
MIDS3 = (S2+S3)/2;
MIDS4 = (S3+S4)/2;
MIDS5 = (S4+S5)/2;
MIDS6 = (S5+S6)/2;
MIDS7 = (S6+S7)/2;
MIDS8 = (S7+S8)/2;
MIDS9 = (S8+S9)/2;
MIDS10 = (S9+S10)/2;













n=130;
per =11.5;
x = Cum(1);
s1=IIf(RSIa(L,n)>Min(RSIa(C,n),RSIa(O,n)),Min(RSIa (C,n),RSIa(O,n)),RSIa(L,n));
s11=IIf(RSIa(H,n)<Max(RSIa(C,n),RSIa(O,n)),Max(RSIa(C,n),RSIa(O,n)),RSIa(H,n));
pS = TroughBars( s1, per, 1 ) == 0;
endt= LastValue(ValueWhen( pS, x, 1 ));
startt=LastValue(ValueWhen( pS, x, 2 ));
dtS =endt-startt;
endS = LastValue(ValueWhen( pS, s1, 1 ) );
startS = LastValue( ValueWhen( pS, s1, 2 ));
aS = (endS-startS)/dtS;bS = endS;
trendlineS = aS * ( x -endt ) + bS;
pR = PeakBars( s11, per, 1 ) == 0;
endt1= LastValue(ValueWhen( pR, x, 1 ));
startt1=LastValue(ValueWhen( pR, x, 2 ));
dtR =endt1-startt1;
endR = LastValue(ValueWhen( pR, s11, 1 ) );
startR = LastValue( ValueWhen( pR, s11, 2 ));
aR = (endR-startR)/dtR;
bR = endR;
trendlineR = aR * ( x -endt1 ) + bR;
//Plot(ValueWhen( (S1==trendlineS ), L),"", colorGreen, styleDashed|styleNoRescale|styleThick);
//Plot(ValueWhen((S11==trendlineR ), H),"", colorRed, styleDashed|styleNoRescale|styleThick);
Buy = Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(S1==trendlineS,-1),-1),-1),-1),-1),-1),-1),-1),-1),-1),-1);
Sell = Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(S11==trendlineR,-1 ),-1),-1),-1),-1),-1),-1),-1),-1),-1),-1),-1),-1 ),-1 ),-1 ),-1 ),-1 );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
AlertIf( Buy, "", "BUY @ " + C, 1 );
AlertIf( Sell, "", "SELL @ " + C, 2 );
PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow, IIf( Buy, colorGold, colorPink) );
dist = 2.5*ATR(10);
dist1 = 0.7*ATR(10);
for( i = 0; i < BarCount; i++ ) 
{ 
 if( Buy[i] ) 
 {
  PlotText( "\n     Heavy Fire Up Ahead     "  , i, L[ i ]-dist[i], colorBlack, colorGreen );
//Say("Heavy Fire Up Ahead");
 }
 if( Sell[i] )
 {
  PlotText( "     Heavy Down Fall Ahead     "  , i, H[ i ]+dist1[i], colorBlack, colorRed ); 
 //Say("Heavy Down Fall Ahead");
 }
}

n=45;
per =20;
x = Cum(1);
s1=IIf(RSIa(L,n)>Min(RSIa(C,n),RSIa(O,n)),Min(RSIa (C,n),RSIa(O,n)),RSIa(L,n));
s11=IIf(RSIa(H,n)<Max(RSIa(C,n),RSIa(O,n)),Max(RSIa(C,n),RSIa(O,n)),RSIa(H,n));
pS = TroughBars( s1, per, 1 ) == 0;
endt= LastValue(ValueWhen( pS, x, 1 ));
startt=LastValue(ValueWhen( pS, x, 2 ));
dtS =endt-startt;
endS = LastValue(ValueWhen( pS, s1, 1 ) );
startS = LastValue( ValueWhen( pS, s1, 2 ));
aS = (endS-startS)/dtS;bS = endS;
trendlineS = aS * ( x -endt ) + bS;
pR = PeakBars( s11, per, 1 ) == 0;
endt1= LastValue(ValueWhen( pR, x, 1 ));
startt1=LastValue(ValueWhen( pR, x, 2 ));
dtR =endt1-startt1;
endR = LastValue(ValueWhen( pR, s11, 1 ) );
startR = LastValue( ValueWhen( pR, s11, 2 ));
aR = (endR-startR)/dtR;
bR = endR;
trendlineR = aR * ( x -endt1 ) + bR;
//Plot(trendlineR , "trendlineR" ,colorRed , styleLine);
//Plot(ValueWhen( (S1==trendlineS ), L),"", colorGreen, styleDashed|styleNoRescale|styleThick);
//Plot(ValueWhen((S11==trendlineR ), H),"", colorRed, styleDashed|styleNoRescale|styleThick);
Buy = Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(S1==trendlineS,-1),-1),-1),-1),-1),-1),-1),-1),-1),-1),-1);
Sell = Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(Ref(S11==trendlineR,-1 ),-1),-1),-1),-1),-1),-1),-1),-1),-1),-1),-1),-1 ),-1 ),-1 ),-1 ),-1 );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

 //Plot the ribbon
ribboncol= IIf( BarsSince(Buy)<BarsSince(Sell), colorGreen, colorRed );
//Plot( 3, "", ribboncol, styleArea | styleOwnScale | styleNoLabel, 0, 100 );
PlotShapes(IIf(Buy, shapeSquare,shapeNone) ,colorBlue,0, Close, Offset=-10) ; 
PlotShapes(IIf(Buy, shapeSmallUpTriangle,shapeNone) ,colorLime,0, Close, Offset=-10) ;
PlotShapes(IIf(Sell, shapeSquare,shapeNone),colorRed,0, Close, Offset=-1) ;
PlotShapes(IIf(Sell, shapeSmallDownTriangle,shapeNone),colorLime,0, Close, Offset=-1) ;
PlotShapes( Buy * shapeNone + Sell * shapeNone, IIf( Buy, colorBlue, colorPink) );
dist = 0.8*ATR(10);
dist1 = 2*ATR(10);
for( i = 0; i < BarCount; i++ ) 
{ 
 if( Buy[i] ) 
 {
 // PlotText( "\nHNI Buy 10 Lots @" + C[ i ] + "\nTGT= " + (C[i]*1.0045) + "\nSL= " + (C[i]*0.9970), i, L[ i ]-dist[i], colorGreen, colorBlack );
 }
 if( Sell[i] )
 {
  //PlotText( "HNI Sell 10 Lots @" + C[ i ] + "\nT= " + (C[i]*0.995) + "\nSL= " + (C[i]*1.0025), i, H[ i ]+dist1[i], colorRed, colorBlack ); 
 }
}




_SECTION_END();

Uphigh =  HHV(C,90);
downhigh = LLV (C, 40);
//Buy = C>= Uphigh;
//Sell = C<= downhigh;
Buy  = ExRem (Buy,Sell);
Sell = ExRem (Sell,Buy);


_SECTION_BEGIN("BACK COLOR");
SetChartBkGradientFill( ParamColor("BgTop", colorPlum),

ParamColor("BgBottom", colorBlack),ParamColor("titleblock",colorPlum));


BuyPrice = Open;
SellPrice = Open;

Derived=WMA(WMA(WMA(((H+L)/2)+(O-C),92),1),1); 
i=WMA(Derived,3); 
Upper=(Derived+StDev(Derived,7))+ATR(2)/1.5; 
Lower=(Derived-StDev(Derived,7))-ATR(2)/1; 

color=IIf(BarsSince(Cross(Lower,Low))> 
BarsSince(Cross(C,Upper)), colorBrightGreen,colorRed); 
Plot(C,"",color,64); 



//WriteIf(s,"EXIT all Short positions\nif trading long positions, enter long Now-\nOR at the market price on tomorrow's Open with stop="+EncodeColor(4)+WriteVal(L+.75*ATR(5),1.4)+" ,","");
//WriteIf(ss,"exit all long positions today with a Market On Close (MOC) order\nOR at the market price on tomorrow's Open with stop="+EncodeColor(4)+WriteVal(Ref(H+.75*ATR(5), -1),1.4)+",","");
//WriteIf( sss ,"No trading signals today.","") ;



SetBarsRequired( 600, 0 );
 
GraphXSpace = 8;
SetChartOptions( 0, chartShowArrows | chartShowDates );
 
 
//============== TITLE ==============

_SECTION_BEGIN("Magnified Market Price");
FS=Param("Font Size",30,11,100,1);
GfxSelectFont("Times New Roman", FS, 700, True ); 
GfxSetBkMode( colorWhite );  
GfxSetTextColor( ParamColor("Color",colorGold) ); 
Hor=Param("Horizontal Position",970,1,1200,1);
Ver=Param("Vertical Position",50,1,830,1); 
GfxTextOut(""+C, Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Times New Roman", 11, 700, True ); 
GfxSetBkMode( colorWhite );  
GfxSetTextColor(ParamColor("Color",colorBlack) ); 
GfxTextOut(""+DD+"  ("+xx+"%)", Hor , Ver+45 );

_SECTION_END();



_SECTION_BEGIN("Price Line");
dp= Param("dps",3,0,1000,1);
PriceLineColor=ParamColor("PriceLineColor",colorOrange);
PriceLevel = ParamField("PriceField", field = 3 ); 
PriceLeve2 = ParamField("PriceField", field = dp );
Daysback    = Param("Bars Back",1,10,500,1); 
FirstBar    = BarCount - DaysBack; 
 YY        = IIf(BarIndex() >= Firstbar,EndValue(PriceLevel),Null); 

Plot(YY,"Current Price",PriceLineColor,ParamStyle("LineStyle",styleLine|styleDashed|styleNoTitle|styleNoLabel|styleThick,maskAll)); 

side = Param("side",1,0,1000,1);

dist = 0; 

for( i = 0; i < BarCount; i++ ) 
{ 
 if(i+side== BarCount) PlotText( "\n    " + PriceLevel[ i ], i, YY[ i ]-dist[i], colorGold );

} 

_SECTION_END();
 
#20
QUOTE=laotze;1031398]Post image of LIC Housing breakout......i will tell you what is KRA ?[/QUOTE


here is the image of chart of LIC Housing

Breakout Happened on 31 October . At that time KRA was 9.12
Previous Breakout was on 16th Sept
at that time KRA was 4.27

Pls explain this chart with all indicators shown here and tell how to trade? what factors to look in chart?
 

Similar threads