Friday 5 February 2016

Swing trading system for Amibroker





 function GetSecondNum()
{
    Time = Now( 4 );
    Seconds = int( Time % 100 );
    Minutes = int( Time / 100 % 100 );
    Hours = int( Time / 10000 % 100 );
    SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
    return SecondNum;
}

function PopupWindowEx( popupID, bodytext, captiontext, timeout, left, top )
{
    displayText = bodytext + captiontext;
    if ( ( StaticVarGetText( "prevPopup" + popupID ) != displayText) OR ( StaticVarGet( "prevPopupTime" + popupID ) < GetSecondNum() ) )
    {
        StaticVarSetText( "prevPopup" + popupID, displayText);
        StaticVarSet( "prevPopupTime" + popupID, GetSecondNum() + timeout );
        PopupWindow( bodytext, Captiontext + popupID, timeout, Left, top );
        Say("crossover.. action");
    }
}

_SECTION_BEGIN("EMA CROSSOVER");

SetTradeDelays(1,1,1,1);
SetPositionSize(150,spsShares);

 Buy = Cross(EMA(C,5), EMA(C,20));
 Sell = Cross(EMA(C,20),EMA(C,5));
 Buy = ExRem(Buy,Sell);
 Sell= ExRem(Sell,Buy);
 Short = Sell ;
 Cover = Buy ;
 Cover=ExRem(Cover,Short);
 Short=ExRem(Short,Cover);
 Filter= Cover OR Short;

AlertIf( Buy, "", "Buy @ " + C, 1 );
AlertIf( Sell, "", "Sell @ " + C, 2 );

for(i=BarCount-1;i>1;i--)
{
if(Buy[i] == 1)
{
entry = C[i];
sig = "Buy";

bars = i;
i = 0;
}
if(Sell[i] == 1)
{
sig = "Sell";
entry = C[i];
bars = i;
i = 0;
}
}
Offset = 20;

Clr = IIf(sig == "Buy", colorLime, colorRed);
for (i=bars; i <BarCount;i++)
{
PlotText(""+sig+"@"+entry, BarCount-3,entry,Null,colorBlue);
}

messageboard = ParamToggle("Message Board","Show|Hide",1);
if (messageboard == 1 )
{

GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );

if ( sig =="Buy")
{
GfxSelectSolidBrush( colorDarkGreen ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorDarkRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;

xx = Status( "pxchartwidth");

Left = 1100;

width = 310;

x = 5;

x2 = 238;

y = pxHeight;


}

_SECTION_END();

EMA2=EMA(C,2);
EMA4=EMA(C,4);
EMA8=EMA(C,8);
EMA17=EMA(C,17);
EMA34=EMA(C,34);
FMA1=(EMA2-EMA4)+EMA2;
FMA2=(EMA4-EMA8)+EMA4;
FMA4=(EMA8-EMA17)+EMA8;
FMA8=(EMA17-EMA34)+EMA17;
Plot(FMA4,"FMA4",colorGreen,styleThick);
Plot(FMA8,"FMA8",colorRed,styleThick);
Buy=FMA4>FMA8;
Sell=FMA4<FMA8;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
PlotShapes(shapeHollowUpArrow * Buy,colorBrightGreen,0,L, Offset=-45);
PlotShapes(shapeHollowDownArrow * Sell,colorRed,0,H, Offset=-45);
A=(H+L)/2;
var1=Wilders(A,34);
var2=Wilders(A,5);
var3=var2-var1;
var4=var3-Wilders(var3,5);
Var5=(H-L)/V;
AO=Var3;
AC=Var4;
barcolor=IIf(AO>Ref(AO,-1) AND AC>Ref(AC,-1),colorGreen,IIf(AO<Ref(AO,-1) AND AC<Ref(AC,-1),colorRed,IIf(V>0 AND Var5<Ref(Var5,-1) AND V>Ref(V,-1),colorGold,colorBlack)));
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 ) );
HaClose = ( HaClose + HaOpen + HaHigh + HaLow )/4;
Plot( C, "Regular candles " + Name(), barcolor, styleCandle );


duration = 10000000;

LastClose= Ref(C,-1); 

if (Buy[BarCount-2]==True)    
{
PopupWindowEx( "ID:1", "Get Ready to BUY  \n"+Name() + "  "+ Interval(2)+" :  "+ " Last ="+LastClose  , "Buy Alert -", 1000, 100, 1 ) ; 
}
if (Short[BarCount-2]==True)
{
PopupWindowEx( "ID:2", "Get Ready to SHORT  \n"+Name() + "  "+ Interval(2) + "  :  "+ " Last ="+LastClose , "Short   Alert ", 1000, 1, 150 ) ;
}

_SECTION_BEGIN("Background text");
SetChartBkColor(colorBlack);
GraphXSpace=Param("GraphXSpace",10,-45,200,1);
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(transparent=1);
GfxSetOverlayMode(1);
GfxSelectFont("Candara", Status("pxheight")/C13 );
GfxSetTextAlign( 6 );
GfxSetTextColor( ColorRGB (217,217,213));
_SECTION_END();

_SECTION_BEGIN("BACKGROUD LTRS 1");
SetChartOptions(0,chartShowArrows|chartShowDates);
GfxSetOverlayMode(1);
GfxSetTextAlign( 6 );// center alignment
GfxSetTextColor( ParamColor("Text Color",colorSeaGreen));
GfxSetBkMode(0); // transparent
GfxSelectFont("arial narrow", Status("pxheight")/18);
_SECTION_END();

_SECTION_BEGIN("BACKGROUD LTRS 2");
GfxTextOut( Name(), Status("pxwidth")/2, Status("pxheight")/4.1);
GfxSetTextAlign( 6 );// center alignment
GfxSetTextColor( ParamColor("Text Color",colorDarkOliveGreen));
GfxSetBkMode(0); // transparent
GfxSelectFont("arial narrow", Status("pxheight")/28);


_SECTION_BEGIN("BB price chart");
 SetChartOptions(0,chartShowArrows|chartShowDates);
 Plot( Close, "Close", ParamColor("Color", colorLightBlue ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

 P = ParamField("Price field",-1);
 Periods = Param("Periods", 20, 2, 300, 1 );
 Width = Param("Width", 2, 0, 10, 0.05 );
 Color = ParamColor("Color", colorCycle );
 Style = ParamStyle("Style");

 if( ParamToggle("Plot BB price chart", "No,Yes", 1 ) )
 Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style );
 if( ParamToggle("Plot BB price chart", "No,Yes", 1 ) )
 Plot( BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style );
 _SECTION_END();

_SECTION_BEGIN("PSAR");
Plot(C,"",1,128);
acc = Param("Acceleration", 0.04, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.4, 0, 1, 0.001 );
if( ParamToggle("Plot PSAR", "No,Yes", 1 ) )
Plot( SAR( acc, accm ), _DEFAULT_NAME(), ParamColor( "Color", colorGold ), ParamStyle("Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );
_SECTION_END();

_SECTION_BEGIN("SMA crossover 200 ");
P = ParamField("Price field",-1);
Periods = Param("Periods", 200, 2, 300, 1, 10 );
if( ParamToggle("Plot SMA cross over 200", "No,Yes", 1 ) )
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorRed ) );
 _SECTION_END();

_SECTION_BEGIN("SMA crossover 100 ");
P = ParamField("Price field",-1);
Periods = Param("Periods", 100, 2, 300, 1, 10 );
if( ParamToggle("Plot SMA cross over 100", "No,Yes", 1 ) )
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorSeaGreen )  );
 _SECTION_END();

_SECTION_BEGIN("SMA crossover 50 ");
P = ParamField("Price field",-1);
Periods = Param("Periods", 50, 2, 300, 1, 10 );
if( ParamToggle("Plot SMA crossover 100", "No,Yes", 1 ) )
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorTan) );
 _SECTION_END();


//Magfied Market Price

FS=Param("Font Size",23,11,100,1);

GfxSelectFont("Times New Roman", FS, 700, True );

GfxSetBkMode( colorWhite );

GfxSetTextColor( ParamColor("Color",colorSeaGreen) );

Hor=Param("Horizontal Position",640,1,1000,1);

Ver=Param("Vertical Position",14,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", 14, 700, True );

GfxSetBkMode( colorBlack );

GfxSetTextColor(ParamColor("Color",colorYellow) );

GfxTextOut(""+DD+"  ("+xx+"%)", Hor , Ver+45 );

_SECTION_END();


_SECTION_BEGIN("BREAK OUTS");

//  ============== Standard Chart Code ============================
Code = "Modified @ FazalHaq";
SetFormulaName("Volume Breakouts");

//   ---------------  Controls for Chart Display  ----------------------------------
SetChartOptions(0, chartShowDates | chartWrapTitle);
if (ParamToggle("Tooltip shows", "All Values|Only Prices"))
{
  ToolTip = StrFormat("Open: %g\nHigh:  %g\nLow:   %g\nClose:  %g (%.1f%%) \nVolume: " + NumToStr(V, 1), O, H, L, C, SelectedValue(ROC(C, 1)));
}

space = Param("Space on Chart", 2,  - 15, 50, 1);
GraphXSpace = space;
SetChartBkColor(ParamColor("Background Color", colorBlack));

// =================   (with Lookback and Shift default set to his specs) =================

H0 = H;
L0 = L;
C0 = C;
O0 = O;

Lookback = Param("LookBack", 10, 10, 50, 1); // Default for interday commodity currency trading
shift = Param("Shift", 5, 1, 50, 1); // ?
x0 = LastValue(Cum(1));
x1 = x0 - shift;
H = Ref(H,  - shift + 1);
L = Ref(L,  - shift + 1);
H1 = LastValue(Ref(HHV(H, Lookback),  - 1));
L1 = LastValue(Ref(LLV(L, Lookback),  - 1));

if( ParamToggle("Plot BREAK OUTS", "No,Yes", 1 ) )
Plot(H1, "H1", colorSkyblue);
if( ParamToggle("Plot BREAK OUTS", "No,Yes", 1 ) )
Plot(L1, "L1", colorSkyblue);

H = H0;
L = L0;
C = C0;
O = O0;

up = Cross(H, H1)AND Cum(1) > x1;
dn = Cross(L1, L)AND Cum(1) > x1;

_SECTION_END();

_SECTION_BEGIN("HeikenAshiSmoothed");
GraphXSpace=2;
//p=45;
p = Param("p",70,2,100,1);

Om=DEMA(O,p);
hm=DEMA(H,p);
lm=DEMA(L,p);
Cm=DEMA(C,p);
HACLOSE=(Om+Hm+Lm+Cm)/4;
HaOpen = AMA( Ref( HaClose, -1), 1);
HaHigh = Max( Hm, Max( HaClose, HaOpen ) );
HaLow = Min( Lm, Min( HaClose, HaOpen ) );

Color=IIf(HaClose >= HaOpen,colorSeaGreen,ColorRGB( 205, 92, 92 ) );
if( ParamToggle("Plot Heikin-Ashi", "No,Yes", 0 ) )
PlotOHLC( HaOpen, HaOpen, HaClose, HaClose, "" + Name(), Color, styleCandle);
_SECTION_END();


_SECTION_BEGIN("TREND LINES");

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    THIS SECTION DRAWS TD TREND LINES */

percent  = 0.01 * 1; /* Adjust this percent as necessary, I use .01 because
FOREX is a 0.0000 number */
firstpointL = 2;
firstpointH = 2;

y0=LastValue(Trough(L,percent,firstpointL));
y1=LastValue(Trough(Ref(L,-1),percent,1));

for( i = 1; i < BarCount AND y0 >= y1; i++ )
{
      
        firstpointL++;  
        y0=LastValue(Trough(L,percent,firstpointL));        
}

x0=BarCount - 1 - LastValue(TroughBars(L,percent,firstpointL));
x1=BarCount - 1 - LastValue(TroughBars(Ref(L,-1),percent,1));
LineL = LineArray( x0, y0, x1, y1, 1 );
/*
Plot(C, "C", colorBlack, styleCandle);
*/
if( ParamToggle("Plot TREND LINES", "No,Yes", 0 ) )
Plot( LineL, " Support Trend line", colorWhite,4 +8 );


yt0=LastValue(Peak(H,percent,firstpointH));
yt1=LastValue(Peak(Ref(H,-1),percent,1));

for(i = 1; i < BarCount AND yt0 <= yt1; i++ )
{
      
        firstpointH++;      
        yt0=LastValue(Peak(H,percent,firstpointH));
}
xt0=BarCount - 1 - LastValue(PeakBars(H,percent,firstpointH));
xt1=BarCount - 1 - LastValue(PeakBars(Ref(H,-1),percent,1));
LineH = LineArray( xt0, yt0, xt1, yt1, 1 );

if( ParamToggle("Plot TREND LINES", "No,Yes", 0 ) )
Plot( LineH, "Resistance Trend line", colorGold,4 + 8 );


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

_SECTION_END();

_SECTION_BEGIN("Fib Retracements");
    fibs = ParamToggle("Plot Fibs","Off|On",0);
    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",0);
    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();


_SECTION_BEGIN("Pivot Points,Daily,Monthly");

TimeFrameSet(inDaily);
DayH= LastValue(Ref(H,-1));
DayL= LastValue(Ref(L,-1));
DayC= LastValue(Ref(C,-1));
TimeFrameRestore();

//Daily
PP = (DayL + DayH + DayC)/3;
R1  =  (2 * PP) - DayL;
S1  =  (2 * PP)  - DayH;
R2  =  PP + (DayH - DayL);
S2  =  PP - (DayH - DayL);
R3 = DayH + 2*(PP - DayL);
S3 = DayL - 2*(DayH - PP);

TimeFrameSet(inMonthly);
MonthH= LastValue(Ref(H,-1));
MonthL= LastValue(Ref(L,-1));
MonthC= LastValue(Ref(C,-1));
TimeFrameRestore();

//Monthly
MPP = (MonthL + MonthH + MonthC)/3;
MR1  =  (2 * MPP) - MonthL;
MS1  =  (2 * MPP)  - MonthH;
MR2  =  MPP + (MonthH- MonthL);
MS2  =  MPP - (MonthH- MonthL);
MR3  =  MonthH + 2*(MPP - MonthL);
MS3 =   MonthL - 2*(MonthH- MPP);

SHOWDPIVOTS = ParamToggle("Daily Pivots", "No|Yes",0);
SHOWMPIVOTS = ParamToggle("Monthly Pivots","No|Yes",0);
SHOW3 = ParamToggle("3rd Line", "No|Yes",0);

  
for (i=BarCount-2;i>(BarCount-13);i--)//set the last bars to the final PP value
{
    PP[i] = PP[BarCount-1];
    R1[i] = R1[BarCount-1];
    R2[i] = R2[BarCount-1];
    S1[i] = S1[BarCount-1];
    S2[i] = S2[BarCount-1];

    ColorG[i] = colorGreen;
    ColorB[i] = colorBlue;
    ColorR[i] = colorRed;
}

//
// Conceal all but the trailing portion of the line
//
for (i=0;i<BarCount-15;i++) //hide the line except most recent 15 bars
{
    ColorR[i] = ColorG[i] = ColorB[i] = colorDarkGrey;
}


if (SHOWDPIVOTS) {
Plot(R2, "R2",ColorG);
Plot(R1, "R1",ColorG);
Plot(PP, "PP",ColorB);
Plot(S1, "S1",ColorR);
Plot(S2, "S2",ColorR);
}

if (SHOWDPIVOTS & SHOW3)
{
        Plot(S3, "S3",ColorR);
        Plot(R3, "R3",ColorG);
}



if (SHOWMPIVOTS) {
Plot(MPP, "MPP",ColorB);
Plot(MR1, "MR1",ColorG);
Plot(MS1, "MS1",ColorR);
Plot(MS2, "MS2",ColorR);
Plot(MR2, "MR2",ColorG);
}

if (SHOWMPIVOTS & SHOW3)
{
        Plot(MR3, "MR3",ColorG);
        Plot(MS3, "MS3",ColorR);
}

_SECTION_END();

 _SECTION_BEGIN("Elliot wave");
 X=BarIndex();
 p=Param("p",5,5,30,1);
 z=Zig(C,p);Plot(C,"C",2,64);
CONDP=PeakBars(C,P)==0;SP=Cum(CONDP);
 EP1=ValueWhen(CONDP,C,1);TP1=ValueWhen(CONDP,X,1);
 EP2=ValueWhen(CONDP,C,2);TP2=ValueWhen(CONDP,X,2);
 EP3=ValueWhen(CONDP,C,3);TP3=ValueWhen(CONDP,X,3);
 EP4=ValueWhen(CONDP,C,4);TP4=ValueWhen(CONDP,X,4);
 CONDT=TroughBars(C,P)==0;ST=Cum(CONDT);
 ET1=ValueWhen(CONDT,C,1);TT1=ValueWhen(CONDT,X,1);
 ET2=ValueWhen(CONDT,C,2);TT2=ValueWhen(CONDT,X,2);
 ET3=ValueWhen(CONDT,C,3);TT3=ValueWhen(CONDT,X,3);
 ET4=ValueWhen(CONDT,C,4);TT4=ValueWhen(CONDT,X,4);
 ET5=ValueWhen(CONDT,C,5);TT5=ValueWhen(CONDT,X,5);

 // EW definition
 EW8=EP3>EP4 AND EP2>EP3 AND EP2>EP1 AND ET4>ET5 AND ET3>ET2 AND
 ET2>ET1 AND ET3>ET4 AND ET4>ET5 AND CONDT;
 COLOR=colorWhite;

 PlotShapes(shapeDigit8*EW8,color);
 G=Cum(CONDP OR CONDT);
 GEW=SelectedValue(ValueWhen(EW8,G));
 for(n=1;n<=9;n++)
 {
 PlotShapes((49-(2*n-(n%2)))*(G==GEW-n AND (n%2)*CONDP+(-1+n%2)
 *CONDT),Color);
 }
 Plot(EW8,"",colorRed,2+styleOwnScale);
 if( ParamToggle("Plot Elliot Wave", "No,Yes", 0) )
 Plot(z,"",colorPink );
 Filter=EW8;// explore for all quotations
 AddColumn(C,"C");
 GraphXSpace=8;
 _SECTION_END();

_SECTION_BEGIN("Title");

DODay = TimeFrameGetPrice("O", inDaily);
DHiDay = TimeFrameGetPrice("H", inDaily);
DLoDay = TimeFrameGetPrice("L", inDaily);

Title = EncodeColor(colorWhite)+""+EncodeColor(colorWhite)+ Name() + ", " + Interval(2) + ", " + Date() +
EncodeColor(colorWhite)   +  "\nO " + EncodeColor(colorWhite) + O +

 ",   H :  "   + H +
 ",   L  :  " + L +
 ",   C  :  "  + C +
EncodeColor(colorYellow)+ "\nDay-Open : " +DODay + "  Day-High : " +DHiDay + "  Day-Low : " + DLoDay ;
_SECTION_END();

_SECTION_BEGIN("Linear Reg Channel");
//Plot(Close, "", 55, GetPriceStyle());
P        = ParamField("Price field", -1);
Daysback = Param("Period for Linear Regression Channel", 50, 1, 300, 1);
SDP1     = Param("Standard Deviation 1", 1.0, 0, 6, 0.05);
SDP2     = Param("Standard Deviation 2", 2.0, 0, 6, 0.05);
SDP3     = Param("Standard Deviation 3", 3.0, 0, 6, 0.05);
ext      = Param("extend Linear Regression Channel", 15, 0, 50, 1);
shift    = Param("Look back period", 0, 0, 240, 1);
Collg    = ParamColor("Color Linear Reg Line", colorRed);
Colsd1   = ParamColor("Color StDev 1", colorBlue);
Colsd2   = ParamColor("Color StDev 2", colorLime);
Colsd3   = ParamColor("Color StDev 3", colorGold);
SDP1     = SDP1/2;
SDP2     = SDP2/2;
SDP3     = SDP3/2;
pds      = Daysback;
x        = BarIndex() + 1;
sx       = SelectedValue(x)-shift;
aa       = SelectedValue(Ref(LinRegIntercept(P, pds), -shift));
bb       = SelectedValue(Ref(LinRegSlope(P, pds), -shift));
StDev0   = StDev(P, pds);
fd1      = SelectedValue(Ref(SDP1 * StDev0, -shift) );
fd2      = SelectedValue(Ref(SDP2 * StDev0, -shift) );
fd3      = SelectedValue(Ref(SDP3 * StDev0, -shift) );
ys       = SelectedValue(ValueWhen(x, aa, 1));
yi       = SelectedValue(ValueWhen(x, bb, 1));
xs       = sx - pds;
d        = ext;//Shift Line Right
xe       = xs + pds + d;
ye       = ys + yi * (xe - xs);
sty      = 1|32|2048;
if( ParamToggle("Plot Linear Reg Channel", "No,Yes", 0 ) )
Plot(LineArray(xs-d,ys,xe-d,ye,1),"",Collg,sty,0,0,d);
if( ParamToggle("Plot Linear Reg Channel", "No,Yes", 0 ) )
Plot(LineArray(xs-d,ys-fd1,xe-d,ye-fd1,1),"",Colsd1,sty,0,0,d);
if( ParamToggle("Plot Linear Reg Channel", "No,Yes", 0 ) )
Plot(LineArray(xs-d,ys+fd1,xe-d,ye+fd1,1),"",Colsd1,sty,0,0,d);
if( ParamToggle("Plot Linear Reg Channel", "No,Yes", 0 ) )
Plot(LineArray(xs-d,ys-fd2,xe-d,ye-fd2,1),"",Colsd2,sty,0,0,d);
if( ParamToggle("Plot Linear Reg Channel", "No,Yes", 0 ) )
Plot(LineArray(xs-d,ys+fd2,xe-d,ye+fd2,1),"",Colsd2,sty,0,0,d);
if( ParamToggle("Plot Linear Reg Channel", "No,Yes", 0 ) )
Plot(LineArray(xs-d,ys-fd3,xe-d,ye-fd3,1),"",Colsd3,sty,0,0,d);
if( ParamToggle("Plot Linear Reg Channel", "No,Yes", 0 ) )
Plot(LineArray(xs-d,ys+fd3,xe-d,ye+fd3,1),"",Colsd3,sty,0,0,d);
_SECTION_END();


_SECTION_BEGIN("Fazal's Pivot Points");

//---------------------------------------------------------------------------
// This section gets whether they want pivot level for intraday or thier eod
//---------------------------------------------------------------------------

_N(ioreod =ParamList("Pivot Levels for ",  "Intraday|EOD"));

if (ioreod=="Intraday")
    {
        yh = TimeFrameGetPrice( "H", inDaily, -1 );
        yl = TimeFrameGetPrice( "L", inDaily, -1 );
        yc = TimeFrameGetPrice( "C", inDaily, -1 );
    }
else
    {
        yh = TimeFrameGetPrice( "H", inDaily, 0 );
        yl = TimeFrameGetPrice( "L", inDaily, 0 );
        yc = TimeFrameGetPrice( "C", inDaily, 0 );
    }

//---------------------------------------------------------------------------
//                         To calculate the Pivot Levels
//---------------------------------------------------------------------------

to = TimeFrameGetPrice( "O", inDaily, 0 );
pivot = (yh + yl + yc) / 3;
range = yh - yl;
_N(pist =ParamList("Select Pivot Type ", "Classical Pivot|Woodie Pivot|Caramilla Pivot|Fibonacci Pivot"));

if (pist =="Classical Pivot" )
    {
        r1 = (2 * pivot) - yl ;
        s1 = (2 * pivot) - yh ;
        r2 = pivot - s1 + r1;
        s2 = pivot - (r1 - s1) ;
        r3 = 2 * (pivot - yl) + yh ;
        s3 = yl - (2 * (yh - pivot));
    }

else if(pist =="Woodie Pivot" )
    {
        pivot = (yh + yl + yc + to) / 4;
        r1 = (2 * pivot) - yl;
        r2 = pivot + range;
        r3 = yh + 2 * (pivot - yl);
        r4 = r3 + range;
        s1 = (2 * pivot) - yh;
        s2 = pivot - range;
        s3 = yl - 2 * (yh - pivot);
        s4 = S3 - range;
    }

else if(pist =="Caramilla Pivot" )
    {
        r4 = yc + range * 1.1/2;
        r3 = yc + range * 1.1/4;
        r2 = yc + range * 1.1/6;
        r1 = yc + range * 1.1/12;
        s1 = yc - range * 1.1/12;
        s2 = yc - range * 1.1/6;
        s3 = yc - range * 1.1/4;
        s4 = yc - range * 1.1/2;
    }

else
    {
        r3 = pivot + 1.000 * (yh - yl);
        r2 = pivot + 0.618 * (yh - yl);
        r1 = pivot + 0.382 * (yh - yl);
        s1 = pivot - 0.382 * (yh - yl);
        s2 = pivot - 0.618 * (yh - yl);
        s3 = pivot - 1.000 * (yh - yl);
    }

//---------------------------------------------------------------------------
//                         To Plot Pivot Levels in the screen
//---------------------------------------------------------------------------

_N(dsr =ParamList("Draw Intraday Pivot Levels ",  "None|Both|Support|Resistance"));

if (dsr =="Support" OR  dsr == "Both")
{
    Plot(pivot, "\n Pivot - ",colorWhite,1);
    Plot(r1, "Resistance 1 - ",colorYellow,1);
    Plot(r2, "Resistance 2 - ",colorYellow,1);
    Plot(r3, "Resistance 3 - ",colorYellow,1);
    Plot((pivot+r1)/2, "Mid Value of R1 & Pivot  ",colorYellow,1);
    Plot((r3+r2)/2, "Mid Value of R2 & R3 -  ",colorYellow,1);
    Plot((r1+r2)/2, "Mid Value of R1 & R2 - ",colorYellow,1);
}

if( dsr == "Resistance" OR  dsr == "Both")
{
    Plot(pivot, "\n Pivot - ",colorWhite,1);
    Plot(s3, "Support 2 - ",colorYellow,1);
    Plot(s2, "Support 2 - ",colorYellow,1);
    Plot(s1, "Support 1 - ",colorYellow,1);
    Plot((s3+s2)/2, "Mid Value of S2 & S3 ",colorYellow,1);
    Plot((s1+s2)/2, "Mid Value of S1 & S2 -  ",colorYellow,1);
    Plot((pivot+s1)/2, "Mid Value of S1 & Pivot  ",colorYellow,1);
}

//---------------------------------------------------------------------------
//                 Printing the pivot level in interpretation window
//---------------------------------------------------------------------------


printf(Name()+ "\n\nResistance - 3  |  %g\nResistance - 2  |  %g\nResistance - 1  |  %g\n" +
"Pivot                |  %g\nSupport - 1      |  %g\nSupport - 2      |  %g\nSupport - 3      |  %g",
r3,r2,r1,pivot,s1,s2,s3);


//---------------------------------------------------------------------------
//                         This section is for Exploration
//---------------------------------------------------------------------------

Filter = 1;
AddColumn(r3,"Resistance 3");
AddColumn(r2,"Resistance 2");
AddColumn(r1,"Resistance 1");
AddColumn(Pivot,"Pivot");
AddColumn(s1,"Support 1");
AddColumn(s2,"Support 2");
AddColumn(s3,"Support 3");

//---------------------------------------------------------------------------
//                         Add Pivot levels along with the title
//---------------------------------------------------------------------------

_N(Title = EncodeColor(colorLime)+ StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g(%.1f%%)\n"+
    EncodeColor(colorRed)+"Resistance =  %g || Resistance = %g || Resistance = %g || "+
    EncodeColor(colorWhite)+" Pivot = %g"+
    EncodeColor(colorGreen)+"\nSupport = %g || Support = %g ||::::: Support 3 -=- %g\n ",
 O, H, L, C,SelectedValue( ROC( C, 1 ) ),r3,r2,r1,pivot,s1,s2,s3));

//---------------------------------------------------------------------------
//                                 End of Pivot Point
//---------------------------------------------------------------------------

_SECTION_END();