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

nth ( 1 - 8 ) Order Polynomial Fit for Amibroker (AFL)

Rating:
5 / 5 (Votes 2)
Tags:
amibroker

nth ( 1 – 8 ) Order Polynomial Fit of data using Gaussian Elimination for simultaneous solution of multiple linear equations. It can extrapolate forward and/or backwards

By Fred Tonetti

Similar Indicators / Formulas

Kavach Of Karna v2
Submitted by hbkwarez over 10 years ago
Advanced Elliott Waves
Submitted by MarcosEn over 13 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
_SECTION_BEGIN("nth ( 1 - 8)");
//------------------------------------------------------------------------------
//
//  nth ( 1 - 8 ) Order Polynomial Fit of data using Gaussian Elimination for
//  simultaneous solution of multiple linear equations. It can extrapolate
//  forward and/or backwards
//
//------------------------------------------------------------------------------
 
 
// *********************************************************
// *
// * VBS Function for Gaussian Elimination
// *
// *     Called by PolyFit ( AFL )
// *
// *********************************************************
 
EnableScript("VBScript");
 
<%
function Gaussian_Elimination (GE_Order, GE_N, GE_SumXn, GE_SumYXn)
    Dim b(10, 10)
    Dim w(10)
    Dim Coeff(10)
 
    for i = 1 To 10
        Coeff(i) = 0
    next
 
    n = GE_Order + 1
 
    for i = 1 to n
        for j = 1 to  n
            if i = 1 AND j = 1 then
                b(i, j) = cDBL(GE_N)
            else
                b(i, j) = cDbl(GE_SumXn(i + j - 2))
            end if
        next     
        w(i) = cDbl(GE_SumYXn(i))
    next
 
    n1 = n - 1
    for i = 1 to n1
        big = cDbl(abs(b(i, i)))
        q = i
        i1 = i + 1
 
        for j = i1 to n
            ab = cDbl(abs(b(j, i)))
            if (ab >= big) then
                big = ab
                q = j
            end if
        next
 
        if (big <> 0.0) then
            if (q <> i) then
                for j = 1 to n
                    Temp = cDbl(b(q, j))
                    b(q, j) = b(i, j)
                    b(i, j) = Temp
                next
                Temp = w(i)
                w(i) = w(q)
                w(q) = Temp
            end if
        end if
 
        for j = i1 to n
            t = cDbl(b(j, i) / b(i, i))
            for k = i1 to n
                b(j, k) = b(j, k) - t * b(i, k)
            next        
            w(j) = w(j) - t * w(i)
        next     
    next
 
    if (b(n, n) <> 0.0) then
 
        Coeff(n) = w(n) / b(n, n)
        i = n - 1
 
        while i > 0
            SumY = cDbl(0)
            i1 = i + 1
            for j = i1 to n
                SumY = SumY + b(i, j) * Coeff(j)
            next
            Coeff(i) = (w(i) - SumY) / b(i, i)
            i = i - 1
        wend  
 
        Gaussian_Elimination = Coeff
 
    end if
end function
%>
 
// *********************************************************
// *
// * AFL Function for nth Order Polynomial Fit
// *     Calls Gaussian_Elimination ( VBS )
// *
// *     Y      = The array to Fit
// *     BegBar = Beg Bar in range to fit
// *     EndBar = End Bar in range to fit
// *     Order  = 1 - 8 = Order of Poly Fit (Integer)
// *     ExtraB = Number of Bars to Extrapolate (Backward)
// *     ExtraF = Number of Bars to Extrapolate (Forward)
// *
// *********************************************************
  
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;
    AddColumn(X1, "X1", 1.9);
 
    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_VBS    = GetScriptObject();
    GE_Coeff  = GE_VBS.Gaussian_Elimination(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;
}
 
// *********************************************************
// *
// * Demo AFL to use PolyFit
// *
// *********************************************************
 
Filter = 1;
 
BI        = BarIndex();
PF_BegBar = BeginValue(BI);
PF_EndBar = EndValue(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);
 
Yn = PolyFit(PF_Y, PF_BegBar, PF_EndBar, PF_Order, PF_ExtraB, PF_ExtraF);
 
GraphXSpace = 10;
 
Plot(Yn, "nth Order Polynomial Fit - " + NumToStr(PF_Order, 1.0), IIf(BI > PF_EndBar - PF_ExtraF, colorWhite, IIf(BI < PF_BegBar - PF_ExtraF, colorWhite, colorBrightGreen)), styleThick, Null, Null, PF_ExtraF);
PlotOHLC(O, H, L, C, "Close", colorLightGrey, styleCandle);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
_SECTION_END();

0 comments

Leave Comment

Please login here to leave a comment.

Back