nth ( 1 - 8 ) Order Polynomial Fit.afl
上传用户:shiqiang
上传日期:2009-06-12
资源大小:1289k
文件大小:8k
源码类别:

金融证券系统

开发平台:

Others

  1. //------------------------------------------------------------------------------
  2. //
  3. //  Formula Name:    nth ( 1 - 8 ) Order Polynomial Fit
  4. //  Author/Uploader: Fred Tonetti 
  5. //  E-mail:          
  6. //  Date/Time Added: 2006-10-03 18:18:42
  7. //  Origin:          
  8. //  Keywords:        nth Order Polynomial Fit Trendline Gauss
  9. //  Level:           medium
  10. //  Flags:           showemail,function
  11. //  Formula URL:     http://www.amibroker.com/library/formula.php?id=731
  12. //  Details URL:     http://www.amibroker.com/library/detail.php?id=731
  13. //
  14. //------------------------------------------------------------------------------
  15. //
  16. //  nth ( 1 - 8 ) Order Polynomial Fit of data using Gaussian Elimination for
  17. //  simultaneous solution of multiple linear equations. It can extrapolate
  18. //  forward and/or backwards
  19. //
  20. //------------------------------------------------------------------------------
  21. // *********************************************************
  22. // *
  23. // * VBS Function for Gaussian Elimination
  24. // *
  25. // *     Called by PolyFit ( AFL )
  26. // *
  27. // *********************************************************
  28. EnableScript("VBScript");
  29. <%
  30. function Gaussian_Elimination (GE_Order, GE_N, GE_SumXn, GE_SumYXn)
  31.     Dim b(10, 10)
  32.     Dim w(10)
  33.     Dim Coeff(10)
  34.     for i = 1 To 10
  35.         Coeff(i) = 0
  36.     next
  37.     n = GE_Order + 1
  38.     for i = 1 to n
  39.         for j = 1 to  n
  40.             if i = 1 AND j = 1 then
  41.                 b(i, j) = cDBL(GE_N)
  42.             else
  43.                 b(i, j) = cDbl(GE_SumXn(i + j - 2))
  44.             end if
  45.         next      
  46.         w(i) = cDbl(GE_SumYXn(i))
  47.     next
  48.     n1 = n - 1
  49.     for i = 1 to n1
  50.         big = cDbl(abs(b(i, i)))
  51.         q = i
  52.         i1 = i + 1
  53.         for j = i1 to n
  54.             ab = cDbl(abs(b(j, i)))
  55.             if (ab >= big) then
  56.                 big = ab
  57.                 q = j
  58.             end if
  59.         next
  60.         if (big <> 0.0) then
  61.             if (q <> i) then
  62.                 for j = 1 to n
  63.                     Temp = cDbl(b(q, j))
  64.                     b(q, j) = b(i, j)
  65.                     b(i, j) = Temp
  66.                 next
  67.                 Temp = w(i)
  68.                 w(i) = w(q)
  69.                 w(q) = Temp
  70.             end if
  71.         end if
  72.         for j = i1 to n
  73.             t = cDbl(b(j, i) / b(i, i))
  74.             for k = i1 to n
  75.                 b(j, k) = b(j, k) - t * b(i, k)
  76.             next         
  77.             w(j) = w(j) - t * w(i)
  78.         next      
  79.     next
  80.     if (b(n, n) <> 0.0) then
  81.         Coeff(n) = w(n) / b(n, n)
  82.         i = n - 1
  83.         while i > 0
  84.             SumY = cDbl(0)
  85.             i1 = i + 1
  86.             for j = i1 to n
  87.                 SumY = SumY + b(i, j) * Coeff(j)
  88.             next
  89.             Coeff(i) = (w(i) - SumY) / b(i, i)
  90.             i = i - 1
  91.         wend   
  92.         Gaussian_Elimination = Coeff
  93.     end if
  94. end function
  95. %>
  96. // *********************************************************
  97. // *
  98. // * AFL Function for nth Order Polynomial Fit
  99. // *     Calls Gaussian_Elimination ( VBS )
  100. // *
  101. // *     Y      = The array to Fit 
  102. // *     BegBar = Beg Bar in range to fit
  103. // *     EndBar = End Bar in range to fit
  104. // *     Order  = 1 - 8 = Order of Poly Fit (Integer)
  105. // *     ExtraB = Number of Bars to Extrapolate (Backward)
  106. // *     ExtraF = Number of Bars to Extrapolate (Forward)
  107. // *
  108. // *********************************************************
  109.  
  110. function PolyFit(GE_Y, GE_BegBar, GE_EndBar, GE_Order, GE_ExtraB, GE_ExtraF)
  111. {
  112.     BI        = BarIndex();
  113.     GE_N      = GE_EndBar - GE_BegBar + 1;
  114.     GE_XBegin = -(GE_N - 1) / 2;
  115.     GE_X      = IIf(BI < GE_BegBar, 0, IIf(BI > GE_EndBar, 0, (GE_XBegin + BI - GE_BegBar)));
  116.     GE_X_Max  = LastValue(Highest(GE_X));
  117.     GE_X      = GE_X / GE_X_Max;
  118.     X1 = GE_X;
  119.     AddColumn(X1, "X1", 1.9);
  120.     GE_Y      = IIf(BI < GE_BegBar, 0, IIf(BI > GE_EndBar, 0, GE_Y));
  121.     GE_SumXn  = Cum(0);
  122.                                  GE_SumXn[1]   = LastValue(Cum(GE_X)); 
  123.     GE_X2     = GE_X * GE_X;     GE_SumXn[2]   = LastValue(Cum(GE_X2));
  124.     GE_X3     = GE_X * GE_X2;    GE_SumXn[3]   = LastValue(Cum(GE_X3)); 
  125.     GE_X4     = GE_X * GE_X3;    GE_SumXn[4]   = LastValue(Cum(GE_X4)); 
  126.     GE_X5     = GE_X * GE_X4;    GE_SumXn[5]   = LastValue(Cum(GE_X5)); 
  127.     GE_X6     = GE_X * GE_X5;    GE_SumXn[6]   = LastValue(Cum(GE_X6)); 
  128.     GE_X7     = GE_X * GE_X6;    GE_SumXn[7]   = LastValue(Cum(GE_X7)); 
  129.     GE_X8     = GE_X * GE_X7;    GE_SumXn[8]   = LastValue(Cum(GE_X8)); 
  130.     GE_X9     = GE_X * GE_X8;    GE_SumXn[9]   = LastValue(Cum(GE_X9)); 
  131.     GE_X10    = GE_X * GE_X9;    GE_SumXn[10]  = LastValue(Cum(GE_X10)); 
  132.     GE_X11    = GE_X * GE_X10;   GE_SumXn[11]  = LastValue(Cum(GE_X11)); 
  133.     GE_X12    = GE_X * GE_X11;   GE_SumXn[12]  = LastValue(Cum(GE_X12)); 
  134.     GE_X13    = GE_X * GE_X12;   GE_SumXn[13]  = LastValue(Cum(GE_X13)); 
  135.     GE_X14    = GE_X * GE_X13;   GE_SumXn[14]  = LastValue(Cum(GE_X14)); 
  136.     GE_X15    = GE_X * GE_X14;   GE_SumXn[15]  = LastValue(Cum(GE_X15)); 
  137.     GE_X16    = GE_X * GE_X15;   GE_SumXn[16]  = LastValue(Cum(GE_X16)); 
  138.     GE_SumYXn = Cum(0);
  139.                                  GE_SumYXn[1]  = LastValue(Cum(GE_Y));
  140.     GE_YX     = GE_Y    * GE_X;  GE_SumYXn[2]  = LastValue(Cum(GE_YX));
  141.     GE_YX2    = GE_YX   * GE_X;  GE_SumYXn[3]  = LastValue(Cum(GE_YX2)); 
  142.     GE_YX3    = GE_YX2  * GE_X;  GE_SumYXn[4]  = LastValue(Cum(GE_YX3));
  143.     GE_YX4    = GE_YX3  * GE_X;  GE_SumYXn[5]  = LastValue(Cum(GE_YX4));
  144.     GE_YX5    = GE_YX4  * GE_X;  GE_SumYXn[6]  = LastValue(Cum(GE_YX5));
  145.     GE_YX6    = GE_YX5  * GE_X;  GE_SumYXn[7]  = LastValue(Cum(GE_YX6));
  146.     GE_YX7    = GE_YX6  * GE_X;  GE_SumYXn[8]  = LastValue(Cum(GE_YX7));
  147.     GE_YX8    = GE_YX7  * GE_X;  GE_SumYXn[9]  = LastValue(Cum(GE_YX8));
  148.     GE_Coeff  = Cum(0);
  149.     GE_VBS    = GetScriptObject();
  150.     GE_Coeff  = GE_VBS.Gaussian_Elimination(GE_Order, GE_N, GE_SumXn, GE_SumYXn);
  151.     for (i = 1; i <= GE_Order + 1; i++)
  152.         printf(NumToStr(i, 1.0) + " = " + NumToStr(GE_Coeff[i], 1.9) + "n");
  153.     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));
  154.     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;
  155.     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; 
  156.     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; 
  157.     GE_Yn = IIf(BI < GE_BegBar - GE_ExtraB - GE_ExtraF, -1e10, IIf(BI > GE_EndBar, -1e10, 
  158.             GE_Coeff[1]  + 
  159.             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  +
  160.             GE_Coeff[7]  * GE_X6  + GE_Coeff[8]  * GE_X7  + GE_Coeff[9]  * GE_X8));
  161.     return GE_Yn;
  162. }
  163. // *********************************************************
  164. // *
  165. // * Demo AFL to use PolyFit
  166. // *
  167. // *********************************************************
  168. Filter = 1;
  169. BI        = BarIndex();
  170. PF_BegBar = BeginValue(BI);
  171. PF_EndBar = EndValue(BI);
  172. PF_Y      = (H + L) / 2;
  173. PF_Order  = Param("nth Order",             3, 1,  8, 1);
  174. PF_ExtraB = Param("Extrapolate Backwards", 0, 0, 50, 1);
  175. PF_ExtraF = Param("Extrapolate Forwards",  0, 0, 50, 1);
  176. Yn = PolyFit(PF_Y, PF_BegBar, PF_EndBar, PF_Order, PF_ExtraB, PF_ExtraF);
  177. GraphXSpace = 10;
  178. 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);
  179. PlotOHLC(O, H, L, C, "Close", colorLightGrey, styleCandle);