|
我的MQL还在入门阶段,先把在网上找到的拉格朗日滤波附上,以后再告知修改版。
//+------------------------------------------------------------------+
//| AdaptiveLaguerreFilter.mq4
//|
//+------------------------------------------------------------------+
#property indicator_chart_window
#include <stdlib.mqh>
#property indicator_buffers 6
extern int LookBack = 20;
extern int Median = 5;
extern int PriceType = PRICE_MEDIAN;
extern color Color1 = Black;
int ZeroCount;
int i,j;
double Price,HH,LL;
double alpha;
double Filter[];
double Diff[];
double L0[];
double L1[];
double L2[];
double L3[];
double sortDiff[];
int init()
{
IndicatorBuffers(6);
SetIndexBuffer(0,Filter);
SetIndexStyle(0,DRAW_LINE,0,1,Color1);
SetIndexLabel(0,"ALF");
SetIndexBuffer(1,Diff);
SetIndexStyle(1,DRAW_NONE);
SetIndexBuffer(2,L0);
SetIndexStyle(2,DRAW_NONE);
SetIndexBuffer(3,L1);
SetIndexStyle(3,DRAW_NONE);
SetIndexBuffer(4,L2);
SetIndexStyle(4,DRAW_NONE);
SetIndexBuffer(5,L3);
SetIndexStyle(5,DRAW_NONE);
ArrayResize(sortDiff,Median);
return (0);
}
int start()
{
int countedBars = IndicatorCounted();
int limit = Bars-countedBars-1;
for (i=limit;i>=0;i--)
{
Price = iMA(NULL,0,1,0,MODE_SMA,PriceType,i);
Diff = MathAbs(Price - Filter[i+1]);
HH = Diff;
LL = Diff;
for (j=0;j<LookBack;j++)
{
if (Diff[i+j] > HH)
{HH = Diff[i+j];}
if (Diff[i+j] < LL)
{LL = Diff[i+j];}
}
if (!CompareDoubles(HH-LL,0))
{
for (int j=0;j<Median;j++)
{
sortDiff[j] = (Diff[i+j] - LL) / (HH - LL);
}
ArraySort(sortDiff,WHOLE_ARRAY,0,MODE_ASCEND);
alpha = sortDiff[Median/2];
}
L0 = alpha*Price + (1 - alpha)*L0[i+1];
L1 = -(1 - alpha)*L0 + L0[i+1] + (1 - alpha)*L1[i+1];
L2 = -(1 - alpha)*L1 + L1[i+1] + (1 - alpha)*L2[i+1];
L3 = -(1 - alpha)*L2 + L2[i+1] + (1 - alpha)*L3[i+1];
Filter = (L0 + 2.0 * L1 + 2.0 * L2 + L3) / 6.0;
}
return (0);
}
[ 本帖最后由 Stanwell 于 3-9-2010 07:27 编辑 ] |
|