Parallel Trend Lines 2 Amibroker AFL

Parallel Trend Lines 2 Amibroker AFL 



Code:


//Parallel Trend Lines 2 Amibroker AFL 


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


_SECTION_BEGIN("Pattern Recognition Exploration 3");
GfxSetOverlayMode(1);
GfxSelectFont("Tahoma", Status("pxheight")/2 );
GfxSetTextAlign( 6 );// center alignment
GfxSetTextColor( ColorRGB( 200, 200, 200 ) );
GfxSetBkMode(1); // transparent
GfxTextOut( Name(), Status("pxwidth")/2, Status("pxheight")/12 );
// Parallel lines defined by the automatic support and the HighestBetween point
x = Cum(1);per = 3;s1=L;s11=H;
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;
Plot(IIf(x>startt-10,trendlineS,-1e10),"",colorBlue,1);
H1=LastValue(Highest((Cum(1)>startt AND Cum(1)<endt)*H));
tH1=LastValue(ValueWhen(H==H1,Cum(1)));
Color=IIf((Cum(1)==startt OR Cum(1)==endt),colorBlue,IIf(Cum(1)==tH1,colorOrange,colorBlack));
Plot(C,"Close",Color,64);
UpperParallel=aS*(x-tH1)+H1;Plot(IIf(x>startt-10,UpperParallel,-1e10),"UpperLine",colorOrange,1);

// Parallel lines defined by the automatic resistance and the LowestBetween point
x = Cum(1);per = 3;s1=L;s11=H;
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(IIf(x>startt1-10,trendlineR,-1e10),"",colorBlue,1);
L1=LastValue(Lowest(IIf(Cum(1)>startt1 AND Cum(1)<endt1,L,10^10)));
tL1=LastValue(ValueWhen(L==L1,Cum(1)));
Color=IIf((Cum(1)==startt1 OR Cum(1)==endt1),colorBlue,IIf(Cum(1)==tL1,colorOrange,colorBlack));
Plot(C,"Close",Color,64);
LowerParallel=aR*(x-tL1)+L1;Plot(IIf(x>startt1-10,LowerParallel,-1e10),"LowerLine",colorOrange,1);
_SECTION_END();





How To Install 32 Bit Software On 64 Bit OS - Run 32 Bit Program On 64 Bit Windows 10/8/7

How To Install 32 Bit Software On 64 Bit OS - Run 32 Bit Program On 64 Bit Windows 10/8/7

STEP 1
Go To Windows Search
Search Control Panel and Open Control Panel
Go to Programs and then Click on Uninstall Program
Click on Turn Windows Feature On or OFF
Mark On Internet Information Services (It is hidden by Default) PRESS Ok
Click Close after process Finishes
Close Control Panel







STEP 2
Go To Windows Search
Search IIS (Internet Information Service)
Open Internet Internet Service Manager
Click Expand Menu on Your Computer Name
Click on Application pools
Right Click on DefaultAppPool and Select advanced Settings
Click on Enable 32-bit Application option and Change it to True  [Default option is false]
Click OK









Now RESTART YOUR COMPUTER OR LAPTOP

Done....!!! 









Why can't x86 (32-bit) operating systems use more than 3.25 GB of RAM memory and not whole 4 GB?

Why can't x86 (32-bit) operating systems use more than 3.25 GB of RAM memory and not whole 4 GB? /  32 bit operating system 3.25 GB RAM Limit



A 32 bit client version of Windows has a physical address space of 4 GB. It would be nice if all of this space could be used for RAM but that is not the case. A portion of this address space is used for memory mapped hardware devices. The CPU can communicate with these devices by reading or writing to their address space just as it does with RAM. There are other methods available but memory mapped hardware is much faster, important for high performance devices like video systems. Typically memory mapped devices consume about .5 to 1 GB but is tan be more or less in some cases.

You can't have 4 GB RAM and 0.5 to 1 GB memory mapped devices in the same 4 GB address space. It just won't fit. Thus whatever space is used for hardware devices is not available for RAM. If you have 3 GB or less RAM there is usually address space to spare and no RAM is lost.

If the address space used for hardware devices is subtracted from 4 GB that leaves about 3 to 3.5 space available for RAM. 3.25 GB is just typical and the number often quoted.

I suppose all of this could have been done differently but that would have complicated the design, and a 4 GB address space was huge at the time. Changing it now would have serious compatibility issues. And we already have a much better solution in using a 64 bit OS. A 64 bit OS has a much larger address space so there is plenty of room for everything.

PAE is often suggested as a solution. This is a special setting that allows for a much larger physical address space. Conflicting RAM can be addressed above 4 GB where it can then be addressed. This option is supported by 32 bit server operating systems. The problem was that many device drivers behaved very poorly in such an environment with very unfortunate results, such as crashes or data corruption. For that reason client systems simply ignore RAM above 4 GB. This has become a license restriction and enforced in the kernel.

As PAE doesn't work in client systems and 32 bit servers are virtually obsolete it is essentially a dead issue.



How to Change the Number of Processors Used at Boot Up in windows 7/ windows 10

How to Change the Number of Processors Used at Boot Up in windows 7/ windows 10

Information
If you have a multiple core CPU (Ex: Intel Core Duo or Quad CPU) or multiple CPUs, then this will show you how to change the Number of Processors that will be used during boot up for troubleshooting and debugging purposes to be able to determine if there is a problem with a single processor/core, or for a programmer to test their code against a single core while running on their multi-core system.

Note
Vista will use all your processors by default at boot up without this Number of Processors setting box checked. Using all of your available processors (cores or CPUs) durring boot allows the computer to boot faster only because a multiple core CPU or multiple CPUs can handle and do more at once than a single core or CPU. Multiple cores do not actually increase the speed, but only increase the number of processor cores working on the job which in turn has the job finish quicker. If you do not have much running at startup, then you may not notice much improvement in how long it takes to startup.

Tip
People have reported that by checking this setting and selecting all of their available CPUs, they have seen a performance increase in boot up speed despite the Noteabove . See the example below. If it does not help, then you can do step 7 to undo this and go back to the default settings.

 Warning
Selecting a lower amount of processors to use at boot up than you actually have can increase the amount boot up time causing a longer boot.


EXAMPLE: Boot Up Time Before and After
NOTE: This is the before and after boot up time for my computer after I changed it to use all 4 cores. For fun you can post your results here: ReBoot Time

Before_ReBoot-Time.jpg After_ReBoot_Time.jpg 


How To do it :
NOTE: Before you do this, you should see what your current boot up time is so you can compare it to what it will be before and after applying this.

1. Open the Start Menu.​

2. In the white line (Start Search) area, type msconfig and press Enter.​

3. Click Continue in the UAC prompt.​

4. Click on the Boot tab. (See screenshot below)​

5. Click on the Advanced options button.​

Step1.jpg 

6. To Change the Number of Processors at Boot -​
(i) Check the Number of processors box, then click on the drop down arrow and select the number of available processors (cores or CPUs) you want to have used at boot up. (See screenshots below step 7)​

NOTE: To test to see if this can speed up your boot time or not, then select the highest number listed for all of the available processors to be used at boot up and check the Detect Hal box.​

(ii) Click on OK.​​
(iii) Go to step 8.​


7. To Restore the Default Number of Processors at Boot -​
(i) Uncheck the Number of processors box.​
(ii) Click on OK.​
​​
Step2A.jpg Step2B.jpg 

8. Check the Make all boot settings permanent box and click on OK. (See screenshot below)​
NOTE: If you do not check this, then you will Selective startup dotted instead of Normal startup under the General tab. You can still make changes again by checking this again after any changes.​

Step3.jpg 
9. Click on Yes at the confirmation window. (See screenshot below)​
NOTE: You can restore or make changes to this again later if you want. I will confirm it. Just remember or write down what you had set (checked or unchecked) before if you wanted to restore back to those settings later.​

Step4.jpg 

10. Check the Don't show this message again box. (See screenshot below step 11)​
NOTE: You will not see this again after checked.​

11. Click on the Restart button.​
NOTE: If you checked the box and want to restore this Restart box, see: How to Restore the System Configuration Restart Prompt in Vista
WARNING: This will restart the computer immediately. Make sure you save and close anything open you do not want to lose.​

Step5.jpg 
12. After you have started back to the desktop, you can check to see what your new boot up time is for fun.​

PRICE VOLUME DIVERGENCE EXPLORER

PRICE VOLUME DIVERGENCE EXPLORER



Code:

_SECTION_BEGIN("ROC-Close");
safeguard="This is developed by StockBangladesh Research and Development department if you need any asistance please contact us at amibroker@stockbangladesh.com";

pre=StrMid( safeguard,21, 15 );

stockbangladesh_periods = Param("Periods", 3, 1, 200, 1 );
stockbangladesh_ROCclose=ROC( C, stockbangladesh_periods);
Plot( ROC( C, StockBangladesh_periods), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style")  );
pre= StrMid( safeguard, 33, 15 );

StockBangladesh_PriceUp = LinRegSlope( StockBangladesh_ROCclose, StockBangladesh_periods) > 0 ; 

printf("PriceUp =%f\n", StockBangladesh_PriceUp);



_SECTION_END();

_SECTION_BEGIN("ROC-Volume");
StockBangladesh_periods = Param("Periods", 3, 1, 200, 1 );
Plot( ROC( V, StockBangladesh_periods), _DEFAULT_NAME(), ParamColor( "Color", colorGreen ), ParamStyle("Style")  );

StockBangladesh_ROCvolume=ROC( V, StockBangladesh_periods);
StockBangladesh_VolumeUp = LinRegSlope( StockBangladesh_ROCvolume, StockBangladesh_periods) > 0 ; 

printf("VolumeUp =%f\n", StockBangladesh_VolumeUp);


StockBangladesh_BullishDiv = NOT StockBangladesh_PriceUP AND StockBangladesh_ROCvolume;
StockBangladesh_BearishDiv = StockBangladesh_PriceUP AND NOT StockBangladesh_VolumeUp;
Filter = StockBangladesh_BullishDiv OR StockBangladesh_BearishDiv;
pre="StockBangladesh";
AddColumn( StockBangladesh_BullishDiv, "Bullish Divergence", 1.0, 
       colorDefault, IIf(StockBangladesh_BullishDiv, colorGreen, colorDefault ) ); 
AddColumn( StockBangladesh_BearishDiv , "Bearish Divergence", 1.0, 
       colorDefault, IIf(StockBangladesh_BearishDiv , colorRed, colorDefault) );

GfxSetOverlayMode(1);
GfxSelectFont("Tahoma", Status("pxheight")/5 );
GfxSetTextAlign( 6 );// center alignment
GfxSetTextColor( ColorRGB( 200, 200, 200 ) );
GfxSetBkMode(1); // transparent
GfxTextOut( pre, Status("pxwidth")/2, Status("pxheight")/12 );


_SECTION_END();








VOLUME BASED ADX

BUYING AND SELLING PRESSURE INDICATOR



Code:

//Indicator Buy and sell pressure - Smoothed with Histoggram
// By Karthik Marar - www.karthikmarar.blogspot.com

_SECTION_BEGIN("Buy & Sell Pressure - Smoothed with Histogram - V.1.0");
SetChartOptions(0,chartShowArrows|chartShowDates);
sp = H-C;
bp = C-L;
bpavg = EMA(bp,80);
spavg = EMA(sp,80);
nbp = bp/bpavg;
nsp = sp/spavg;
diff = nbp-nsp;
diffcolor = IIf(diff>0,colorGreen,colorOrange);
Varg = EMA(V,80);
nv = V/Varg;
nbfraw = nbp * nv;
nsfraw = nsp * nv;
nbf = EMA(nbp * nv,20);
nsf = EMA(nsp * nv,20);
Plot(nbf,"Buying Pressure",colorTurquoise,1|styleThick);
Plot(nsf,"selling Pressure",colorYellow,1|styleThick);
diff = nbf-nsf;
diffcolor = IIf(diff>0,colorGreen,colorRed);
SetBarFillColor( diffcolor ); 
PlotOHLC(0,diff,0,diff,"Force",IIf(diff>0,colorLime,colorOrange), styleCandle | styleOwnScale ) ;
_SECTION_END();
_SECTION_BEGIN("Title");
if( Status("action") == actionIndicator ) 
(
Title = EncodeColor(colorWhite)+ "Buy & Selll Pressure - Smoothed with Histogram V.1.0 " + " - " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
 "  - " + Date() +" - "+"\n" + EncodeColor(colorYellow) + "Raw Buying pressure = "+ EncodeColor(colorLime) + WriteVal(nbf)+
"\n"+ EncodeColor(colorYellow) + "Raw Selling pressure = "+ EncodeColor(colorRed) +  WriteVal(nsf)+
"\n"+ EncodeColor(colorYellow) + "Force = "+ WriteVal(diff));
_SECTION_END();







SEPARATE BULL AND BEAR AFL FOR AMIBROKER

SEPARATE BULL AND BEAR AFL FOR AMIBROKER



Code:

formulaName = "Sum of Bull and Bear Volumes";
SetChartOptions(0,chartShowArrows|chartShowDates);
GfxSetBkMode(1);
GfxSelectFont("Verdana", 12, 600, italic=False);
GfxSetTextColor(colorLightYellow); 
GfxTextOut(FormulaName, 4, 20);


_SECTION_BEGIN("Sum of Bull and Bear Volumes");
SetChartOptions(0, chartShowArrows|chartShowDates);
GfxSetBkMode(1);
periods = Param("Periods to Sum", 5, 1, 100, 1);

BullCond = V>Ref(V,-1) AND C>Ref(C,-1) OR V<Ref(V,-1) AND C<Ref

(C,-1);
BearCond = V>Ref(V,-1) AND C<Ref(C,-1) OR V<Ref(V,-1) AND C>Ref(C,-1);

SumBull= Sum (V*BullCond, periods);
SumBear= Sum (V*BearCond, periods);
Delta  = SumBull-SumBear;
Summa  = SumBull+SumBear;

Out = ParamToggle("Show Style", "Bull+Bear|Bull-Bear", 0 );
if ( Out == 0 )
{
GraphXSpace = 20;
Plot(SumBull,"Bull Vol", colorLime, styleThick);
Plot(SumBear,"Bear Vol", colorRed, styleThick);
Plot(SumBear,"", colorBrown, styleHistogram|styleNoLabel|styleNoTitle);
Plot(SumBull,"", colorDarkGreen, styleHistogram|styleNoLabel|styleNoTitle);
Plot(Summa,  "All Vol", colorWhite, styleDashed|styleNoRescale);
else 
GraphXSpace = 10;
DeltaColor = IIf( Delta==0, colorWhite, IIf(Delta>0, colorGreen,colorRed));
Plot(Delta,"Delta", DeltaColor, styleLine|styleNoLine|styleNoRescale);
Plot(Delta,"Delta", colorWhite, styleThick|styleNoLabel|styleNoTitle|

styleNoRescale);
PlotOHLC(Delta,Delta,0,Delta,"",DeltaColor, styleCloud|styleClipMinMax|

styleNoLabel);
}
_SECTION_END();





USING COLOR MACD IN AMIBROKER

USING COLOR MACD IN AMIBROKER



Code:

formulaName = "Sum of Bull and Bear Volumes";
SetChartOptions(0,chartShowArrows|chartShowDates);
GfxSetBkMode(1);
GfxSelectFont("Verdana", 12, 600, italic=False);
GfxSetTextColor(colorLightYellow); 
GfxTextOut(FormulaName, 4, 20);


_SECTION_BEGIN("Sum of Bull and Bear Volumes");
SetChartOptions(0, chartShowArrows|chartShowDates);
GfxSetBkMode(1);
periods = Param("Periods to Sum", 5, 1, 100, 1);

BullCond = V>Ref(V,-1) AND C>Ref(C,-1) OR V<Ref(V,-1) AND C<Ref

(C,-1);
BearCond = V>Ref(V,-1) AND C<Ref(C,-1) OR V<Ref(V,-1) AND C>Ref(C,-1);

SumBull= Sum (V*BullCond, periods);
SumBear= Sum (V*BearCond, periods);
Delta  = SumBull-SumBear;
Summa  = SumBull+SumBear;

Out = ParamToggle("Show Style", "Bull+Bear|Bull-Bear", 0 );
if ( Out == 0 )
{
GraphXSpace = 20;
Plot(SumBull,"Bull Vol", colorLime, styleThick);
Plot(SumBear,"Bear Vol", colorRed, styleThick);
Plot(SumBear,"", colorBrown, styleHistogram|styleNoLabel|styleNoTitle);
Plot(SumBull,"", colorDarkGreen, styleHistogram|styleNoLabel|styleNoTitle);
Plot(Summa,  "All Vol", colorWhite, styleDashed|styleNoRescale);
else 
GraphXSpace = 10;
DeltaColor = IIf( Delta==0, colorWhite, IIf(Delta>0, colorGreen,colorRed));
Plot(Delta,"Delta", DeltaColor, styleLine|styleNoLine|styleNoRescale);
Plot(Delta,"Delta", colorWhite, styleThick|styleNoLabel|styleNoTitle|

styleNoRescale);
PlotOHLC(Delta,Delta,0,Delta,"",DeltaColor, styleCloud|styleClipMinMax|

styleNoLabel);
}
_SECTION_END();




HIGHER HIGH LOWER HIGH AFL

HIGHER HIGH LOWER HIGH AFL



Code:

_SECTION_BEGIN("HA");
//www.stockbangladesh.com
//Mehedi Arafat

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));

Title = (EncodeColor(colorGold)+Day() + EncodeColor(colorWhite) + Name()+"  " + Date() +"  "+Interval(2) +"  "+ EncodeColor(colorWhite)+",Open "+Open +" , High "+H+" , Low "+L+" , Close "+C+" "+"{{VALUES}}");

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();





































       /****************/
/*              */
/* ALERT OUTPUT */
/*              */
/****************/

TxtX = Param("Text Left", 600, 0, 1000, 10);
TxtY = Param("Text Top", 60, 1, 650, 10);

GfxSelectFont("Tahoma", 14, 700 ); 
GfxSetBkMode(1); 

if(LastValue(Buy) OR SelectedValue(Buy))
{
GfxSetTextColor(colorGold); 
GfxTextOut("Waiting For Next Signal", TxtX, TxtY);
Say(""); 
}

else if(LastValue(Sell) OR SelectedValue(Sell))
{
GfxSetTextColor(colorGold); 
GfxTextOut("Waiting For Next Signal", TxtX, TxtY); 
Say("");
}

else
{
GfxSetTextColor(colorGold);
              GfxTextOut("Waiting For Next Signal ", TxtX, TxtY);  
GfxTextOut("", TxtX, TxtY); 
}
_SECTION_BEGIN("sys");

x=Param("xposn",315,0,1000,1);
y=Param("yposn",576,0,1000,1);



_SECTION_END();




_SECTION_BEGIN("Background text");
C11=ParamColor("up panel",colorBlack );
C12=ParamColor("dn panel",colorBlack );
C13=Param("fonts",20,10,30,1 );
C14=Param("left-right",2.1,1.0,5.0,0.1 );
C15=Param("up-down",8,1,30,1 );
Miny = Status("axisminy");
Maxy = Status("axismaxy");
lvb = Status("lastvisiblebar");
fvb = Status("firstvisiblebar");
pxwidth = Status("pxwidth");
pxheight = Status("pxheight");
GfxSetBkMode( 0 ); 
GfxSelectFont("Tahoma", 12, 500, False, False, 0);
GfxSetTextColor(colorCustom12); 
GfxSetTextAlign( 6 );
GfxSelectFont("Tahoma", 15, 600, False, False, 0);
GfxSetTextColor(colorWhite); 
GfxSetTextAlign( 6 );

GfxSelectFont("Tahoma", 12, 400, False, False, 0);
GfxSetTextColor(colorBrightGreen);

GfxSelectFont("Tahoma", 12, 400, False, False, 0);
GfxSetTextColor(colorRed);




pricechange=(C-Ref(C,-1))*100/Ref(C,-1);
changeponit=C-Ref(C,-1);
Vlp=Param("Volume lookback period",15,10,300,10);
Vrg=MA(V,Vlp);
St = StDev(Vrg,Vlp); 
Vp3 = Vrg + 3*st; 
Vp2 = Vrg + 2*st;;
Vp1 = Vrg + 1*st;;
Vn1 = Vrg -1*st; 
Vn2 = Vrg -2*st; 

x=Param("xposn",1,0,1000,1);
y=Param("yposn",1,0,1000,1);

GfxGradientRect( 1, 1, 1400, 40, colorViolet, colorBlack);
GfxSetBkMode(0);
GfxSelectFont( "Georgia", 18, 800, False );
GfxSetTextColor( colorGold );
GfxTextOut( "www.stockbangladesh.com ", x+600, y+10 );
GfxSelectFont( "Tahoma", 16, 800, False );
GfxSetTextColor( colorGold );
GfxTextOut( Name(), x+100, y+10 );
GfxSelectFont( "Century Gothic", 15, 100, False );
GfxSetTextColor( colorGold );
GfxTextOut( Date(), x+1000, y+10 );
GfxSelectFont( "Comic Sans MS", 15, 500, False );
GfxSetTextColor( colorYellow );
GfxTextOut( Interval(2), x+275, y+10 );
//------------------------------------------------------------ H, L, O, C----------------------
GfxGradientRect( 1, 40, 1400, 55, colorViolet, colorBlack );
GfxSetBkMode(0);
GfxSelectFont( "Areal", 9, 200, False );
GfxSetTextColor( colorWhite );
GfxTextOut( "High:"+WriteVal(H,1.2), x+100, y+40 );
GfxSelectFont( "Areal", 9, 200, False );
GfxSetTextColor( colorWhite );
GfxTextOut( "Low:"+WriteVal(L,1.2), x+200, y+40 );
GfxSelectFont( "Areal", 9, 200, False );
GfxSetTextColor( colorWhite );
GfxTextOut( "Open:"+WriteVal(O,1.2), x+300, y+40 );
GfxSelectFont( "Areal", 9, 200, False );
GfxSetTextColor( colorWhite );
GfxTextOut( "Close:"+WriteVal(C,1.2), x+400, y+40 );
GfxSelectFont( "comic sans ms", 12, 100, False );
//-----------------------------------------------------------Valume-------------------------------


// set criteria to scan for big stock only;
BigStock = MA( V, 10 ) * MA( C, 10 ) > 100;

//---------------Color------------------------
per1 = 6;
per2 = 2;
Om = MA( O, per1 );
hm = MA( H, per1 );
lm = MA( L, per1 );
Cm = MA( C, per1 );

// 1. Heiken Ashi
HACLOSE = ( Om + Hm + Lm + Cm ) / 4;
HaOpen = AMA( Ref( HaClose,  -1 ),  0.5 );
HaHigh = Max( Hm,  Max( HaClose,  HaOpen ) );
HaLow = Min( Lm,  Min( HaClose,  HaOpen ) );


Of = MA( Haopen, per2 );
Cf = MA( Haclose, per2 );
Lf = IIf( haOpen < haClose, MA( Halow, per2 ), MA( Hahigh, per2 ) );
Hf = IIf( haOpen < haClose, MA( Hahigh, per2 ), MA( Halow, per2 ) );
//Color = IIf( Cf > Of, colorGreen, colorRed );


//----------------------------------------------------

TrailStop = HHV( C - 2 * ATR( 10 ), 15 );
ProfitTaker = EMA( H, 13 ) + 2 * ATR( 10 );

/* **********************************
Code to automatically identify pivots
********************************** */

// -- what will be our lookback range for the hh and ll?
farback = 140; //How Far back to go
nBars = 12; //Number of bars

// -- Create 0-initialized arrays the size of barcount
aHPivs = H - H;
aLPivs = L - L;

// -- More for future use, not necessary for basic plotting
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
nHPivs = 0;
nLPivs = 0;
lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;

// -- looking back from the current bar, how many bars
// back were the hhv and llv values of the previous
// n bars, etc.?
aHHVBars = HHVBars( O, nBars );
aLLVBars = LLVBars( O, nBars );
aHHV = HHV( O, nBars );
aLLV = LLV( O, nBars );

// -- Would like to set this up so pivots are calculated back from
// last visible bar to make it easy to "go back" and see the pivots
// this code would find. However, the first instance of
// _Trace output will show a value of 0
aVisBars = Status( "barvisible" );
nLastVisBar = LastValue( Highest( IIf( aVisBars, BarIndex(), 0 ) ) );
_TRACE( "Last visible bar: " + nLastVisBar );

// -- Initialize value of curTrend
curBar = ( BarCount - 1 );
curTrend = "";

if ( aLLVBars[curBar] < aHHVBars[curBar] )
{
    curTrend = "D";
}
else
{
    curTrend = "U";
}

// -- Loop through bars. Search for
// entirely array-based approach
// in future version
for ( i = 0; i < BarCount; i++ )
{
    curBar = ( BarCount - 1 ) - i;
// -- Have we identified a pivot? If trend is down...

    if ( aLLVBars[curBar] < aHHVBars[curBar] )
    {
// ... and had been up, this is a trend change
        if ( curTrend == "U" )
        {
            curTrend = "D";
// -- Capture pivot information
            curPivBarIdx = curBar - aLLVBars[curBar];
            aLPivs[curPivBarIdx] = 1;
            aLPivLows[nLPivs] = L[curPivBarIdx];
            aLPivIdxs[nLPivs] = curPivBarIdx;
            nLPivs++;
        }

// -- or current trend is up
    }
    else
    {
        if ( curTrend == "D" )
        {
            curTrend = "U";
            curPivBarIdx = curBar - aHHVBars[curBar];
            aHPivs[curPivBarIdx] = 1;
            aHPivHighs[nHPivs] = H[curPivBarIdx];
            aHPivIdxs[nHPivs] = curPivBarIdx;
            nHPivs++;
        }

// -- If curTrend is up...else...
    }

// -- loop through bars
}

// -- Basic attempt to add a pivot this logic may have missed
// -- OK, now I want to look at last two pivots. If the most
// recent low pivot is after the last high, I could
// still have a high pivot that I didn't catch
// -- Start at last bar
curBar = ( BarCount - 1 );

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if ( lastLPIdx > lastHPIdx )
{
// -- Bar and price info for candidate pivot
    candIdx = curBar - aHHVBars[curBar];
    candPrc = aHHV[curBar];

    if (
        lastHPH < candPrc AND
        candIdx > lastLPIdx AND
        candIdx < curBar )
    {
// -- OK, we'll add this as a pivot...
        aHPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays

        for ( j = 0; j < nHPivs; j++ )
        {
            aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs- ( j+1 )];
            aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-( j+1 )];
        }

        aHPivHighs[0] = candPrc ;

        aHPivIdxs[0] = candIdx;
        nHPivs++;
    }
}
else
{
// -- Bar and price info for candidate pivot
    candIdx = curBar - aLLVBars[curBar];
    candPrc = aLLV[curBar];

    if (
        lastLPL > candPrc AND
        candIdx > lastHPIdx AND
        candIdx < curBar )
    {
// -- OK, we'll add this as a pivot...
        aLPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays

        for ( j = 0; j < nLPivs; j++ )
        {
            aLPivLows[nLPivs-j] = aLPivLows[nLPivs-( j+1 )];
            aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-( j+1 )];
        }

        aLPivLows[0] = candPrc;

        aLPivIdxs[0] = candIdx;
        nLPivs++;
    }
}

//============== EXPLORATION ==============
Buy = Cover = BigStock AND aLPivs == 1;

Sell = Short = BigStock AND aHPivs == 1;

SellPrice = ValueWhen( Sell, C, 1 );

BuyPrice = ValueWhen( Buy, C, 1 );

Long = Flip( Buy, Sell );

Shrt = Flip( Sell, Buy );

//============== Plot price ==============
n = 15;

a =  C > ( MA( H, n ) + MA( L, n ) ) / 2;// then Buy next bar at market;

b = C < ( MA( H, n ) + MA( L, n ) ) / 2;// then Sell Short next bar at market;

state = IIf( BarsSince( a ) < BarsSince( b ), 1, 0 );

Longs = state == 1;

shorts = state == 0;

_SECTION_BEGIN("SkyBlue's Animated BkGround");

for( i = 1; i < BarCount; i++ )
z = (GetPerformanceCounter()/100)%256;
anim=ColorHSB( ( i + z ) % 256, 255, 100 );
SetChartBkColor(anim);
RequestTimedRefresh(1);


_SECTION_END();








KRISHNA HARMONIC PATTERNS DYNAMIC LEVELS AFL

KRISHNA HARMONIC PATTERNS  DYNAMIC LEVELS AFL



Code:

/* 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
*/

//amibroker afl


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

_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("Strength",5,2,15,1); // Best use: 3, 4, 5
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", 0);
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);

_SECTION_BEGIN("Kpl System");
/* my entry is very simple(daily data for trading)

kpl system for entry only & exit as follow:

1 st exit at x % from entry price only 1/3 quantity.(ie 1st profit target)
2 nd exit when exit Signal comes from kpl sys remaining 1/3 quantity.
3. scale-in to initial quantity if new kpl Buy Signal comes.
re-do above scaling-out & scaling-in till filal exit.
4. final exit all quantity when Close below 21 Day EMA.

kpl system code bellow :
*/
//AFL by Kamalesh Langote. Email:kpl@...
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) ;

Plot(tsl, _DEFAULT_NAME(), tsl_col, styleStaircase);

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

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,Low,-15);  
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorYellow, 0,High,-15);  
PlotShapes(IIf(Cover, shapeHollowCircle, shapeNone),colorWhite, 0,Close,0);  
PlotShapes(IIf(Short, shapeHollowCircle, shapeNone),colorYellow, 0,Close,0); 



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);


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();

//============== TITLE ==============
_SECTION_BEGIN("Title");
if( Status("action") == actionIndicator ) 
(
Title = EncodeColor(colorGold)+ "KRISHNA Trading System" + EncodeColor(colorRose)+" (" +  Name() + ") " + EncodeColor(colorGold)+ Interval(2) + 
 "  " + Date() +" " +" •  Open "+WriteVal(O,1.2)+"  •  "+"Hi "+WriteVal(H,1.2)+"  •  "+"Lo "+WriteVal(L,1.2)+"  •  "+
"Close "+WriteVal(C,1.2)+" ("+WriteVal(C-Ref(C,-1),1,0)+" "+WriteVal((C-Ref(C,-1))*100/Ref(C,-1),1.1)+ "%)  •  Vol= "+ WriteVal(V,1.0)



+"\n"+EncodeColor(colorGrey50)+"------------------------------------------------------------------------------------------------------------"

+"\n"+EncodeColor(colorGold)+
WriteIf (Buy , "Signal: Go Long - Entry Price: "+WriteVal(C)+" - Traget: "+WriteVal((BuyPrice-tsl)+BuyPrice)
+" - StopLoss:"+WriteVal(tsl)+"  "
,"")+


WriteIf (Sell , "Signal: Go Short - Entry Price: "+WriteVal(C)+" - Target: "+WriteVal((tsl-SellPrice)-SellPrice)+" - StopLoss:"+WriteVal(tsl)+" ","")+
EncodeColor(ColorRGB(111,208,255))+
WriteIf(Long AND NOT Buy, "Trade: Long - Entry Price: "+WriteVal((BuyPrice))+" - Profit: "+WriteVal((C-BuyPrice))+" "+EncodeColor(colorLime)+"Let your profit runs!","")+
WriteIf(shrt AND NOT Sell, "Trade: Short - Entry Price: "+WriteVal((SellPrice))+" - Profit: "+WriteVal((SellPrice-C))+"  - "+EncodeColor(colorLime)+"Let your profit runs!","")

+"\n"+EncodeColor(colorGrey50)+"------------------------------------------------------------------------------------------------------------"
);
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
dist = 3*ATR(15);
dist1 = 3*ATR(15);
for( i = 0; i < BarCount; i++ ) 

 if( Buy[i] ) 
 {
  PlotText( "\nBuy:" + C[ i ] + "\nT= " + ((C[i]-tsl[i])+C[i]) + "\nSL= " + tsl[i], i, C[ i ]-dist[i], colorGreen, colorDarkOliveGreen );
 }
 if( Sell[i] )
 {
  PlotText( "Sell:" + C[ i ] + "\nT= " + (C[i]-(tsl[i]-C[i])) + "\nSL= " + tsl[i], i, C[ i ]+dist1[i], colorRed, colorDarkOliveGreen ); 
 }
}

/*Plot Ribbon */
Ribbon1=IIf(  (C) >(tsl)  ,colorWhite, IIf(( tsl )>( C ), colorOrange,colorYellow));
Plot(3, "Ribbon", Ribbon1, styleOwnScale| styleArea| styleNoLabel,-0.5,100);

_SECTION_END();


MAPeriod = Param("MA Period", 4, 1, 100);
MAOpen = EMA(Open, MAPeriod);
MAHigh = EMA(High, MAPeriod);
MALow = EMA(Low, MAPeriod);
MAClose = EMA(Close, MAPeriod);

HaClose = (MAOpen + MAHigh + MALow + MAClose) / 4;
HaOpen = AMA(Ref(HaClose, -1), 0.5);

// for graph collapse
//for(i = 0; i <= MAPeriod; i++) HaClose[i] = Null;
/*
// same 
// HaOpen = (Ref(HaOpen, -1) + Ref(HaClose, -1)) / 2;
HaOpen[ 0 ] = HaClose[ 0 ]; 
for(i = 1; i < BarCount; i++) { 
    HaOpen[i] = (HaOpen[i - 1] + HaClose[i - 1]) / 2;

*/

HaHigh = Max(MAHigh, Max(HaClose, HaOpen)); 
HaLow = Min(MALow, Min(HaClose, HaOpen)); 


//////////////////////////////////////////////////////////////////////////
_SECTION_BEGIN("Peak");

PK = HaHigh > Ref(HaHigh,-1) AND Ref(HaHigh,1) < HaHigh;//Peak
PKV0 = ValueWhen(PK,HaHigh,0);//PeakValue0
PKV1 = ValueWhen(PK,HaHigh,1);//PeakValue1
PKV2 = ValueWhen(PK,HaHigh,2);//PeakValue2
MPK = PKV2 < PKV1 AND PKV1 > PKV0 ;//MajorPeak
MPKV = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, PKV1,1); //MajorPeakValue
MPKD = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, DateNum(),1); //MajorPeakDate
SD = IIf(DateNum() < LastValue(MPKD,lastmode = True ), Null, LastValue(MPKV,Lastmode = True));//SelectedDate
Plot(SD, "LastMinorResistance",colorRed,styleLine,styleThick);

//PLOT THE SECOND LAST MAJOR PEAK RESISTANCE LINE
MPKV2 = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, PKV1,2); //MajorPeakValue
MPKD2 = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, DateNum(),2); //MajorPeakDate
SD2 = IIf(DateNum() < LastValue(MPKD2,lastmode = True ), Null, LastValue(MPKV2,Lastmode = True));//SelectedDate
Plot(SD2, "LastMajorResistance",colorRed,styleLine);
_SECTION_END();

_SECTION_BEGIN("Support");
SP = Ref(HaLow,1) > HaLow AND HaLow < Ref(HaLow,-1);//Peak
SPV0 = ValueWhen(SP,HaLow,0);//PeakValue0
SPV1 = ValueWhen(SP,HaLow,1);//PeakValue1
SPV2 = ValueWhen(SP,HaLow,2);//PeakValue2
MSP = SPV2 > SPV1 AND SPV1 < SPV0 ;//MajorPeak
MSPV = ValueWhen(Ref(MSP,-1) == 0 AND MSP == 1, SPV1,1); 
MSPD = ValueWhen(Ref(MSP,-1) == 0 AND MSP == 1, DateNum(),1); 
SD = IIf(DateNum() < LastValue(MSPD,lastmode = True ), Null, LastValue(MSPV,Lastmode = True));
Plot(SD,"LastMinorSupport",colorGreen,styleLine,styleThick);


MSPV2 = ValueWhen(Ref(MSP,-1) == 0 AND MSP == 1, SPV1,2); 
MSPD2 = ValueWhen(Ref(MSP,-1) == 0 AND MSP == 1, DateNum(),2); 
SD2 = IIf(DateNum() < LastValue(MSPD2,lastmode = True ), Null, LastValue(MSPV2,Lastmode = True));
Plot(SD2,"LastMajorSupport",colorGreen,styleLine);

_SECTION_END();



_SECTION_BEGIN("Background text");
C11=ParamColor("up panel",colorDarkOliveGreen );
C12=ParamColor("dn panel",colorDarkGrey );
C13=Param("fonts",20,10,30,1 );
C14=Param("left-right",2.1,1.0,5.0,0.1 );
C15=Param("up-down",12,1,20,1 );
Miny = Status("axisminy");
Maxy = Status("axismaxy");
lvb = Status("lastvisiblebar");
fvb = Status("firstvisiblebar");
pxwidth = Status("pxwidth");
pxheight = Status("pxheight");
GfxSetBkMode( 0 ); 
GfxSetOverlayMode(1);
GfxGradientRect(0,0,pxwidth, pxheight, C11, C12 ); 
GfxSelectFont("Tahoma", Status("pxheight")/C13 );
GfxSetTextAlign( 6 );
GfxTextOut( "LTP "+WriteVal(C,1.2), Status("pxwidth")/C14, Status("pxheight")/C15);
GfxSelectFont("MS Sans Serif", 10, 500, False, False, 0);
GfxSetTextColor(colorYellow);
GfxTextOut(""+edc+"", Status("pxwidth")/1.15, Status("pxheight")/C15*0.3 );

//ESSENTIAL TRADER TOOLS//
// Compiled by K.Vidyasagar, vkunisetty@yahoo.com //
//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
WeekH= TimeFrameGetPrice("H", inWeekly, 1);  WeekHI = LastValue (WeekH,1); // One Week before high
WeekL= TimeFrameGetPrice("L", inWeekly, 1);  WeekLI = LastValue (WeekL,1);  // One Week before low
MonthH= TimeFrameGetPrice("H", inMonthly, 1);  MonthHI = LastValue (MonthH,1);  // One Month before high
MonthL= TimeFrameGetPrice("L", inMonthly, 1);  MonthLI = LastValue (MonthL,1);  // One Month before low

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

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

TDBHL = ParamToggle("Week And Month HI LO","Hide|Show",1);
if(TDBHL==1) {
  Plot(WeekL,"WeekL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(WeekH,"WeekH",colorOrange,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(MonthL,"MonthL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(MonthH,"MonthH",colorOrange,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  PlotText(" WeekH " , LastValue(BarIndex())-(numbars/Hts), WeekHI, colorOrange);
  PlotText(" WeekL " , LastValue(BarIndex())-(numbars/Hts), WeekLI, colorTurquoise);
  PlotText(" MonthH " , LastValue(BarIndex())-(numbars/Hts), MonthHI, colorOrange);
  PlotText(" MonthL " , LastValue(BarIndex())-(numbars/Hts), MonthLI, colorTurquoise);
}

// Pivot Levels //
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","Hide|Show",0);
if(ppl==1)  {
  Plot(PP, "PP",colorYellow,styleDots|styleNoLine|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);
}
// Camerilla Levels //

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","Hide|Show",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);
}

// Current Days Hi Lo //
THL = ParamToggle("Todays Hi Lo","Hide|Show",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",colorGold,styleDashed|styleNoRescale|styleNoTitle);
  Plot(DayLline,"DayL",colorBlue,styleDashed|styleNoRescale|styleNoTitle);
  PlotText(" Day Hi " , LastValue(BarIndex())-(numbars/Hts), DayHlineI  +0.05, colorYellow);
  PlotText(" Day Lo " , LastValue(BarIndex())-(numbars/Hts), DayLlineI  +0.05, colorYellow);

 }