Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

COG - Center of Gravity for Amibroker (AFL)

Rating:
4 / 5 (Votes 5)
Tags:
amibroker, polynomial

Center of Gravity (COG) indicator, original idea from El Mostafa Belkhayate
Amibroker AFL code by E.M.Pottasch, 2011
Based on code by Fred Tonetti, 2006, n-th order Polynomial fit (see Amibroker Lib)
JohnCW provided Gaussian_Eliminationsv function based on static variables.

Screenshots

Similar Indicators / Formulas

POLYNOMIAL
Submitted by elliot over 13 years ago
bad tick clean
Submitted by pious243 about 12 years ago
3_6Day GuaiLiLv
Submitted by motorfly over 13 years ago
Williams Alligator System
Submitted by durgesh1712 over 13 years ago
*Level Breakout system*
Submitted by Tinych over 13 years ago

Indicator / Formula

Copy & Paste Friendly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Center of Gravity (COG) indicator, original idea from El Mostafa Belkhayate
// Amibroker AFL code by E.M.Pottasch, 2011
// Based on code by Fred Tonetti, 2006, n-th order Polynomial fit (see Amibroker Lib)
// JohnCW provided Gaussian_Eliminationsv function based on static variables.
 
//SetBarsRequired(sbrAll,sbrAll);
BI=BarIndex();
PF_EndBar=LastValue(BI);
PF_Y=(H+L)/2;
PF_Order=Param("nth Order",3,1,8,1);
PF_ExtraB=Param("Extrapolate Backwards",0,0,50,1);
PF_ExtraF=Param("Extrapolate Forwards",0,0,50,1);
Lookback=Param("Lookback Period",100,50,500,1);
sv=ParamToggle("Use Selected Value","Off|On",1);
norm=ParamToggle("Error Levels","Fibonacci|Normal",1);
 
if (sv)
{
    PF_EndBar=SelectedValue(bi);
    PF_BegBar=PF_EndBar-Lookback;
}
else
{
    PF_BegBar=PF_EndBar-Lookback;
}
function D2Set(L_value,i,j,L_name)
{
    local L_value,L_name,i,j;
    StaticVarSet(L_name + ":" + i + "," + j, L_value);
}
function D2Get(i,j,L_name)
{
    local L_name,i,j;
    return(Nz(StaticVarGet(L_name + ":" + i + "," + j),0));
}
function Gaussian_Eliminationsv(GE_Order,GE_N,GE_SumXn,GE_SumYXn)
{
    w=0;Coeff=0;
    n=GE_Order+1;
    for(i=1;i<=n;i++ )
    {
        for(j=1;j<=n;j++)
        {
            if (i==1 AND j==1)
                D2Set(GE_N,i,j,"b");
            else
                D2Set(GE_SumXn[i+j-2],i,j,"b");
        }
        w[i]=GE_SumYXn[i];
    }
    n1=n-1;
    for(i=1;i<=n1;i++)
    {
        big=abs(D2Get(i,i,"b"));
        q=i;
        i1=i+1;
        for(j=i1;j<=n;j++)
        {
            ab=abs(D2Get(j,i,"b"));
            if(ab>=big)
            {
                big=ab;
                q=j;
            }
        }
        if (big!=0)
        {
            if (q!=i)
            {
                for (j=1;j<=n;j++)
                {
                    Temp=D2Get(q,j,"b");
                    D2Set(D2Get(i,j,"b"),q,j,"b");
                    D2Set(Temp,i,j,"b");
                }
                Temp=w[i];
                w[i]=w[q];
                w[q]=Temp;
            }
        }
        for(j=i1;j<=n;j++)
        {
            t=D2Get(j,i,"b")/D2Get(i,i,"b");
            for(k=i1;k<=n;k++)
            {
                D2Set(D2Get(j,k,"b")-t*D2Get(i,k,"b"),j,k,"b");
            }
            w[j]=w[j]-t*w[i];
        }
    }
    if(D2Get(n,n,"b")!=0)
    {
        Coeff[n]=w[n]/D2Get(n,n,"b");
        i=n-1;
        while(i>0)
        {
            SumY=0;
            i1=i+1;
            for(j=i1;j<=n;j++)
            {
                SumY=SumY+D2Get(i,j,"b")*Coeff[j];
            }
            Coeff[i]=(w[i]-SumY)/D2Get(i,i,"b");
            i=i-1;
        }
    }
    return Coeff;
}
function PolyFit(GE_Y,GE_BegBar,GE_EndBar,GE_Order,GE_ExtraB,GE_ExtraF)
{
    BI=BarIndex();
    GE_N=GE_EndBar-GE_BegBar+1;
    GE_XBegin=-(GE_N-1)/2;
    GE_X=IIf(BI<GE_BegBar,0,IIf(BI>GE_EndBar,0,(GE_XBegin+BI-GE_BegBar)));
    GE_X_Max=LastValue(Highest(GE_X));
    GE_X=GE_X/GE_X_Max;
    X1=GE_X;
    GE_Y=IIf(BI<GE_BegBar,0,IIf(BI>GE_EndBar,0,GE_Y));
    GE_SumXn=Cum(0);
     
    GE_SumXn[1]=LastValue(Cum(GE_X));
    GE_X2=GE_X*GE_X;GE_SumXn[2]=LastValue(Cum(GE_X2));
    GE_X3=GE_X*GE_X2;GE_SumXn[3]=LastValue(Cum(GE_X3));
    GE_X4=GE_X*GE_X3;GE_SumXn[4]=LastValue(Cum(GE_X4));
    GE_X5=GE_X*GE_X4;GE_SumXn[5]=LastValue(Cum(GE_X5));
    GE_X6=GE_X*GE_X5;GE_SumXn[6]=LastValue(Cum(GE_X6));
    GE_X7=GE_X*GE_X6;GE_SumXn[7]=LastValue(Cum(GE_X7));
    GE_X8=GE_X*GE_X7;GE_SumXn[8]=LastValue(Cum(GE_X8));
    GE_X9=GE_X*GE_X8;GE_SumXn[9]=LastValue(Cum(GE_X9));
    GE_X10=GE_X*GE_X9;GE_SumXn[10]=LastValue(Cum(GE_X10));
    GE_X11=GE_X*GE_X10;GE_SumXn[11]=LastValue(Cum(GE_X11));
    GE_X12=GE_X*GE_X11;GE_SumXn[12]=LastValue(Cum(GE_X12));
    GE_X13=GE_X*GE_X12;GE_SumXn[13]=LastValue(Cum(GE_X13));
    GE_X14=GE_X*GE_X13;GE_SumXn[14]=LastValue(Cum(GE_X14));
    GE_X15=GE_X*GE_X14;GE_SumXn[15]=LastValue(Cum(GE_X15));
    GE_X16=GE_X*GE_X15;GE_SumXn[16]=LastValue(Cum(GE_X16));
     
    GE_SumYXn=Cum(0);
    GE_SumYXn[1]=LastValue(Cum(GE_Y));
    GE_YX=GE_Y*GE_X;GE_SumYXn[2]=LastValue(Cum(GE_YX));
    GE_YX2=GE_YX*GE_X;GE_SumYXn[3]=LastValue(Cum(GE_YX2));
    GE_YX3=GE_YX2*GE_X;GE_SumYXn[4]=LastValue(Cum(GE_YX3));
    GE_YX4=GE_YX3*GE_X;GE_SumYXn[5]=LastValue(Cum(GE_YX4));
    GE_YX5=GE_YX4*GE_X;GE_SumYXn[6]=LastValue(Cum(GE_YX5));
    GE_YX6=GE_YX5*GE_X;GE_SumYXn[7]=LastValue(Cum(GE_YX6));
    GE_YX7=GE_YX6*GE_X;GE_SumYXn[8]=LastValue(Cum(GE_YX7));
    GE_YX8=GE_YX7*GE_X;GE_SumYXn[9]=LastValue(Cum(GE_YX8));
     
    GE_Coeff=Cum(0);
     
    GE_Coeff=Gaussian_Eliminationsv(GE_Order,GE_N,GE_SumXn,GE_SumYXn);
     
    for (i = 1; i <= GE_Order + 1; i++) printf(NumToStr(i, 1.0) + " = " + NumToStr(GE_Coeff[i], 1.9) + "\n");
 
    GE_X=IIf(BI<GE_BegBar-GE_ExtraB-GE_ExtraF,0,IIf(BI>GE_EndBar,0,(GE_XBegin+BI-GE_BegBar+GE_ExtraF)/GE_X_Max));
     
    GE_X2=GE_X*GE_X;GE_X3=GE_X2*GE_X;GE_X4=GE_X3*GE_X;GE_X5=GE_X4*GE_X;GE_X6=GE_X5*GE_X;
    GE_X7=GE_X6*GE_X;GE_X8=GE_X7*GE_X;GE_X9=GE_X8*GE_X;GE_X10=GE_X9*GE_X;GE_X11=GE_X10*GE_X;
    GE_X12=GE_X11*GE_X;GE_X13=GE_X12*GE_X;GE_X14=GE_X13*GE_X;GE_X15=GE_X14*GE_X;GE_X16=GE_X15*GE_X;
     
    GE_Yn=IIf(BI<GE_BegBar-GE_ExtraB-GE_ExtraF,-1e10,IIf(BI>GE_EndBar,-1e10,GE_Coeff[1]+
    GE_Coeff[2]*GE_X+GE_Coeff[3]*GE_X2+GE_Coeff[4]*GE_X3+GE_Coeff[5]*GE_X4+GE_Coeff[6]*GE_X5+
    GE_Coeff[7]*GE_X6+GE_Coeff[8]*GE_X7+GE_Coeff[9]*GE_X8));
     
    return GE_Yn;
}
     
Yn=PolyFit(PF_Y,PF_BegBar,PF_EndBar,PF_Order,PF_ExtraB,PF_ExtraF);
 
SetChartOptions(0, chartShowDates);
Title = "Symbol: "+ Name()+ "\nPoly Order: "+PF_Order;
Plot(C, "Close",colorLightGrey,styleCandle);
Plot(Yn,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,colorBlue)),styleThick,Null,Null,PF_ExtraF);
 
if(norm)
{
    se=StdErr((C-Yn),LookBack);se=se[PF_EndBar];
    //se=StDev(C,LookBack);se=se[PF_EndBar];
    seh2=Yn+ValueWhen(Yn,se*2);
    sel2=Yn-ValueWhen(Yn,se*2);
    seh1=Yn+ValueWhen(Yn,se*1);
    sel1=Yn-ValueWhen(Yn,se*1);
    Plot(seh2,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(255,0,0))),styleThick,Null,Null,PF_ExtraF);
    Plot(sel2,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(0,255,0))),styleThick,Null,Null,PF_ExtraF);
    Plot(seh1,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(255,100,100))),styleDashed,Null,Null,PF_ExtraF);
    Plot(sel1,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(100,255,100))),styleDashed,Null,Null,PF_ExtraF);
}
else
{
    se=StDev(C,LookBack);se=se[PF_EndBar];
    r1=(1+5^0.5)/2;
    se=se*r1;
    seh3=Yn+ValueWhen(Yn,se);
    sel3=Yn-ValueWhen(Yn,se);
    seh2=Yn+ValueWhen(Yn,se/(1.382));
    sel2=Yn-ValueWhen(Yn,se/(1.382));  
    seh1=Yn+ValueWhen(Yn,se/(1.382*1.618));
    sel1=Yn-ValueWhen(Yn,se/(1.382*1.618));
     
    Plot(seh3,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(255,0,0))),styleThick,Null,Null,PF_ExtraF);
    Plot(sel3,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(0,255,0))),styleThick,Null,Null,PF_ExtraF);
    Plot(seh2,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(255,100,100))),styleDashed,Null,Null,PF_ExtraF);
    Plot(sel2,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(100,255,100))),styleDashed,Null,Null,PF_ExtraF);
    Plot(seh1,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(255,200,200))),styleDashed,Null,Null,PF_ExtraF);
    Plot(sel1,"",IIf(BI>PF_EndBar-PF_ExtraF,colorWhite,IIf(BI<PF_BegBar-PF_ExtraF,colorWhite,ColorRGB(200,255,200))),styleDashed,Null,Null,PF_ExtraF);
}

6 comments

1. parfumeur

Super Duper! Thank you Mr. Pottasch for bringing this indicator to our attention, and writing such an elegant implementation.

2. tradestrong

The best indicator so far posted on this website. Really Thanks!!

3. kbaki

how to interpret with chart, when i in/out

4. morgen

For kbaki
Look at the COG chart and see price like “a boat on the river”;the river’s edges are green and red lines.
You buy when price breaked the green line and, for instance, MACD (or other strong indicator) says “buy”.
You sell when price breaked the red line and, for instance, MACD (or other strong indicator) says “sell”.

5. empottasch

I submitted another version which is faster. About the use. I am no expert on this but first thing to keep in mind is that this code “repaints”. So the way you see the lines nicely around the price in the past data is not what you will see in real time. With each new data point that comes in ALL points in the curve change their value with a little bit.

The way I understand how to use it:
1) only buy or short in the direction of the trend.
2) if the trend is up, the lines are pointing up. Then wait for the price to retrace within the lower green band.
3) Wait for the price to settle somewhat, if dropping hard wait. If settled and band are still pointing up you can buy.
4) use a stop since the price may continue to go down.

6. prasadmuni

Mr POTTASCH,I AM GREATFULL TO YOU FOR THIS WONDERFULL FORMULA AND THE PAINS TAKEN BY YOU.INFACT AFTER I GOT THIS ON THE SITE WISESTOCK TRADER.COM I AM REALLY SEEING SOME GOOD MONEY IN THE MARKET.I COMBINED IT WITH TIMING INDICATOR AND AS YOU SAID I TRADE WITH THE DIRECTION OF THE END LINE.IF SHOWING UP I CONFIRM AND TRADE LONG IF POINTS DOWN TRADE SHORT.MAY THE LORD BESTOW UPON YOU HIS GRACE FOR A LIFETIME.THANKS ADMINISTRATOR FOR THE AFL.
GOD BLESS POTTASCH
THANKYOU
PRASAD
BEST TO BE USED DURING REAL TIME TRADE.NOT POSITIONAL

Leave Comment

Please login here to leave a comment.

Back