// COG: Center of Gravity indicator
// AFL code by E.M.Pottasch, 2011
// translated from: http://chartstudio.whselfinvest.com/files/CenterGravity_0.ctl
tf = Param( "TF", 1, 1, 375, 1 );
TimeFrameSet(in1Minute*tf );
sx=Null;b=Null;ai=Null;x=Null;
reg=x1=x2=x3=z1=z2=z3=Null;
rega=x1a=x2a=x3a=z1a=z2a=z3a=Null;
bi=BarIndex();
eb=LastValue(bi);
order=Param("n-th Order",1,1,50,1);
bars=Param("Lookback Period",100,50,5000,1);
sv=ParamToggle("Use Selected Value","Off|On",1);
alt=ParamToggle("Error Levels","Fibonacci|Standard",0);
ecart=1.61803399;
if(sv)
{
eb=SelectedValue(bi);
bb=Max(0,eb-bars);
}
else
{
bb=Max(0,eb-bars);
}
fin=eb;
nn=order+1;
sx[1]=bars+1;
if(fin>bars)
{
for(mi=1;mi<=2*nn-2;mi++)
{
suml=0;
for(n=0;n<=bars;n++)
{
suml=suml+n^mi;
}
sx[mi+1]=suml;
}
for(mi=1;mi<=nn;mi++)
{
suml=0;
for(n=0;n<=bars;n++)
{
if (mi==1)
suml=suml+Close[fin-n];
else
suml=suml+Close[fin-n]*n^(mi-1);
b[mi]=suml;
}
}
for(jj=1;jj<=nn;jj++)
{
for(ii=1;ii<=nn;ii++)
{
kk=ii+jj-1;
ai[(ii-1)*nn+jj]=sx[kk];
}
}
for(kk=1;kk<=nn-1;kk++)
{
ll=0;
mm=0;
for(ii=kk;ii<=nn;ii++)
{
if(abs(ai[(ii-1)*nn+kk])>mm)
{
mm=abs(ai[(ii-1)*nn+kk]);
ll=ii;
}
}
if(ll==0) break;
if(ll!=kk)
{
for(jj=1;jj<=nn;jj++)
{
tt=ai[(kk-1)*nn+jj];
ai[(kk-1)*nn+jj]=ai[(ll-1)*nn+jj];
ai[(ll-1)*nn+jj]=tt;
}
tt=b[kk];
b[kk]=b[ll];
b[ll]=tt;
}
for(ii=kk+1;ii<=nn;ii++)
{
qq=ai[(ii-1)*nn+kk]/ai[(kk-1)*nn+kk];
for(jj=1;jj<=nn;jj++)
{
if(jj==kk)
ai[(ii-1)*nn+jj]=0;
else
ai[(ii-1)*nn+jj]=ai[(ii-1)*nn+jj]-qq*ai[(kk-1)*nn+jj];
}
b[ii]=b[ii]-qq*b[kk];
}
}
x[nn]=b[nn]/ai[nn*nn];
for(kk=1;kk<=nn-1;kk++)
{
tt=0;
ii=nn-kk;
for(jj=1;jj<=nn-ii;jj++)
{
tt=tt+ai[(ii-1)*nn+ii+jj]*x[ii+jj];
if(ai[(ii-1)*nn+ii]!=0)
x[ii]=(b[ii]-tt)/ai[(ii-1)*nn+ii];
}
}
for(n=0;n<=bars;n++)
{
suml=0;
for(kk=1;kk<=order;kk++)
{
suml=suml+x[kk+1]*n^kk;
}
reg[fin-n]=x[1]+suml;
}
}
SetChartOptions(0, chartShowDates);
Title = "Symbol: "+ Name()+ "\n>>>AMIT TRADING SYSTEM<<<\nmade by BUNTI_K23 ";
if(alt)
{
dev=StDev(Close-reg,bars);
sd=dev[fin];
x1a=reg+sd*1;//68%
x2a=reg+sd*2;//95%
x3a=reg+sd*3;//99.83%
z1a=reg-sd*1;
z2a=reg-sd*2;
z3a=reg-sd*3;
Plot(C, "Close",colorLightGrey,styleCandle);
Plot(reg,"reg",colorBlue,1);
Plot(x3a,"x3a",ColorRGB(255,0,0),styleThick);
Plot(x2a,"x2a",ColorRGB(255,100,100),styleDashed);
Plot(x1a,"x1a",ColorRGB(255,200,200),styleDashed);
Plot(z3a,"z3a",ColorRGB(0,255,0),styleThick);
Plot(z2a,"z2a",ColorRGB(100,255,100),styleDashed);
Plot(z1a,"z1a",ColorRGB(200,255,200),styleDashed);
PlotOHLC(x3a,x3a,x1a,x1a,"",ColorRGB(30,0,0),styleCloud|styleNoLabel,0,0,0,-1);
PlotOHLC(z1a,z1a,z3a,z3a,"",ColorRGB(0,30,0),styleCloud|styleNoLabel,0,0,0,-1);
}
else
{
dev=StDev(Close,bars);
sd=ecart*dev[fin];
x1=reg+sd/(1.382*1.618);
x2=reg+sd/1.382;
x3=reg+sd;
z1=reg-sd/(1.382*1.618);
z2=reg-sd/1.382;
z3=reg-sd;
Plot(C, "Close",colorLightGrey,styleCandle);
//Plot(reg,"reg",colorBlue,1);
//Plot(x3,"x3",ColorRGB(255,0,0),styleThick);
//Plot(x2,"x2",ColorRGB(255,100,100),styleDashed);
Plot(x1,"x1",ColorRGB(255,200,200),styleDashed);
//Plot(z3,"z3",ColorRGB(0,255,0),styleThick);
//Plot(z2,"z2",ColorRGB(100,255,100),styleDashed);
Plot(z1,"z1",ColorRGB(200,255,200),styleDashed);
PlotOHLC(x3,x3,x2,x2,"",ColorRGB(30,0,0),styleCloud|styleNoLabel,0,0,0,-1);
PlotOHLC(z2,z2,z3,z3,"",ColorRGB(0,30,0),styleCloud|styleNoLabel,0,0,0,-1);
}
TimeFrameRestore();
/////////////////////////////////////
////////////////////////
Length = 8;
Price = EMA(Close, Length);
// Keltner
kLength = Length;
kN = 1.5;
kATR = ATR(kLength);
kUpper = Price + kN * kATR;
kLower = Price - kN * kATR;
// Bollinger
bbLength = Length;
bbN = 2;
bbStDevValues = StDev(Close, bbLength);
bbUpper = Price + bbN * bbStDevValues;
bbLower = Price - bbN * bbStDevValues;
IsSignal =
bbUpper <= kUpper AND
bbLower >= kLower;
///////////////////////////////////
///////////////////////////////////////////
Buy = C > x1 AND PDI(20) > Ref(PDI(20),-1) AND MDI(20) < ADX(20) AND PDI(20) > ADX(20) AND issignal < 1;
Short = C < z1 AND MDI(20) > Ref(MDI(20),-1) AND PDI(20) < ADX(20) AND MDI(20) > ADX(20) AND issignal < 1;
Buy = ExRem( Buy, Short );
Short = ExRem( Short, Buy );
PlotShapes( Buy*shapeUpArrow, colorWhite, 0, Low );
PlotShapes( Short*shapeDownArrow, colorWhite, 0, High );
//////////////////////////////////////
DayH = TimeFrameGetPrice("H", in1Minute, 0);
DayL = TimeFrameGetPrice("L", in1Minute, 0);
DayC = TimeFrameGetPrice("C", in1Minute, 0);
DayO = TimeFrameGetPrice("O", in1Minute, 0);
pDayC = TimeFrameGetPrice("C", in1Minute, -1);
FS=Param("Font Size",15,11,100,1);
GfxSelectFont("Comic Sans M", FS , 700, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor( colorBlack );
Hor=Param("Horizontal Position",820,1,1200,1);
Ver=Param("Vertical Position",1,1,830,1);
GfxTextOut(""+C, Hor, Ver);
GfxSetTextColor( colorGrey50 );
GfxTextOut("PC:"+pDayC,700, Ver);
GfxSetTextColor( colorBlue );
GfxTextOut("C:"+DayC, 600, Ver);
GfxSetTextColor(colorRed );
GfxTextOut("L:"+DayL,500, Ver);
GfxSetTextColor(colorBrightGreen );
GfxTextOut("H:"+DayH,400, Ver);
GfxSetTextColor( colorLightBlue );
GfxTextOut("O:"+DayO,300, Ver);
/////////////////////////////////////
_SECTION_BEGIN("Background");
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(ParamColor("Outer panel",colorGrey40)); // color of outer border
SetChartBkGradientFill( ParamColor("Inner panel upper",colorGrey40),ParamColor("Inner panel lower",colorBlack));
tchoice=Param("Title Selection ",2,1,2,1);
Plot(C, "", IIf(O>=C, colorOrange, colorGreen), ParamStyle("Price Style",styleCandle, maskPrice));
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
_SECTION_BEGIN("Fib Retracements");
fibs = ParamToggle("Plot Fibs","Off|On",1);
pctH = Param ("Pivot Hi %", 0.325,0.001,2.0,0.002);
HiLB = Param ("Hi LookBack",1,1,BarCount-1,1);
pctL = Param ("Pivot Lo %", 0.325,0.001,2.0,0.002);
LoLB = Param ("Lo LookBack",1,1,BarCount-1,1);
Back = Param ("Extend Left = 2",1,1,500,1);
Fwd = Param("Plot Forward", 0, 0, 500, 1);
text = ParamToggle("Plot Text","Off|On",1);
hts = Param ("Text Shift", -33.5,-50,50,0.10);
style =ParamStyle("Line Style",styleLine,styleNoLabel);
x = BarIndex();
pRp = PeakBars( H, pctH, 1) == 0;
yRp0 = SelectedValue(ValueWhen( pRp, H, HiLB));
xRp0 = SelectedValue(ValueWhen( pRp, x, HiLB));
pSp = TroughBars( L, pctL, 1) == 0;
ySp0 = SelectedValue(ValueWhen( pSp, L, LoLB));
xSp0 = SelectedValue(ValueWhen( pSp, x, LoLB));
Delta = yRp0 - ySp0;
function fib(ret)
{
retval = (Delta * ret);
Fibval = IIf(ret < 1.0
AND xSp0 < xRp0, yRp0 - retval, IIf(ret < 1.0
AND xSp0 > xRp0, ySp0 + retval,IIf(ret > 1.0
AND xSp0 < xRp0, yRp0 - retval, IIf(ret > 1.0
AND xSp0 > xRp0, ySp0 + retval, Null))));
return FibVal;
}
x0 = Min(xSp0,xRp0)-Back;
x1 = (BarCount -1);
//////////////////////////////////////////////////////////////////
r236 = fib(0.236); r236I = LastValue (r236,1);
r382 = fib(0.382); r382I = LastValue (r382,1);
r050 = fib(0.50); r050I = LastValue (r050,1);
r618 = fib(0.618); r618I = LastValue (r618,1);
r786 = fib(0.786); r786I = LastValue (r786,1);
e127 = fib(1.27); e127I = LastValue (e127,1);
e162 = fib(1.62); e162I = LastValue (e162,1);
e200 = fib(2.00); e200I = LastValue (e200,1);
e262 = fib(2.62); e262I = LastValue (e262,1);
e424 = fib(4.24); e424I = LastValue (e424,1);
//////////////////////////////////////////////////////////////////
p00 = IIf(xSp0 > xRp0,ySp0,yRp0); p00I = LastValue (p00,1);
p100 = IIf(xSp0 < xRp0,ySp0,yRp0); p100I = LastValue (p100,1);
color00 =IIf(xSp0 > xRp0,colorLime,colorRed);
color100 =IIf(xSp0 < xRp0,colorLime,colorRed);
//////////////////////////////////////////////////////////////////
numbars = LastValue(Cum(Status("barvisible")));
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);
//////////////////////////////////////////////////////////////////
if(fibs==1)
{
Plot(LineArray(xRp0-Fwd,yRp0,x1,yRp0,Back),"PR",32,8|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(xSp0-Fwd,ySp0,x1,ySp0,Back),"PS",27,8|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r236,x1,r236,Back),"",45,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r382,x1,r382,Back),"",44,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r050,x1,r050,Back),"",41,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r618,x1,r618,Back),"",43,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r786,x1,r786,Back),"",42,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e127,x1,e127,Back),"e127",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e162,x1,e162,Back),"e162",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e200,x1,e200,Back),"p200",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e262,x1,e262,Back),"p262",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e424,x1,e424,Back),"p424",25,style|styleNoRescale,Null, Null,Fwd);
}
//////////////////////////////////////////////////////////////////
if(text==1)
{
PlotText(" 0% = " + WriteVal(p00,fraction), LastValue(BarIndex())-(numbars/hts), p00I + 0.05, color00);
PlotText("23% = " + WriteVal(r236,fraction), LastValue(BarIndex())-(numbars/hts), r236I + 0.05, 45);
PlotText("38% = " + WriteVal(r382,fraction), LastValue(BarIndex())-(numbars/hts), r382I + 0.05, 44);
PlotText("50% = " + WriteVal(r050,fraction), LastValue(BarIndex())-(numbars/hts), r050I + 0.05, 41);
PlotText("62% = " + WriteVal(r618,fraction), LastValue(BarIndex())-(numbars/hts), r618I + 0.05, 43);
PlotText("78% = " + WriteVal(r786,fraction), LastValue(BarIndex())-(numbars/hts), r786I + 0.05, 42);
PlotText("100% = " + WriteVal(p100,fraction), LastValue(BarIndex())-(numbars/hts),p100I + 0.05, color100);
PlotText("127% = " + WriteVal(e127,fraction), LastValue(BarIndex())-(numbars/hts),e127I + 0.05, 47);
PlotText("162% = " + WriteVal(e162,fraction), LastValue(BarIndex())-(numbars/hts),e162I + 0.05, 47);
PlotText("200% = " + WriteVal(e200,fraction), LastValue(BarIndex())-(numbars/hts),e200I + 0.05, 47);
PlotText("262% = " + WriteVal(e262,fraction), LastValue(BarIndex())-(numbars/hts),e262I + 0.05, 47);
PlotText("424% = " + WriteVal(e424,fraction), LastValue(BarIndex())-(numbars/hts),e424I + 0.05, 25);
}
_SECTION_END();