关于MA指标中嵌套的问题
唐老师,目前我想写一个关于EMA的指标,大概的策略是这样的:EMA(EMA(CLOSE,6) - EMA(CLOSE,21),10),目前想使用的方法是使用intiMA(
string symbol, // 交易品种名称
ENUM_TIMEFRAMES period, // 周期
int ma_period, // 平均周期
int ma_shift, // 平移
ENUM_MA_METHOD ma_method, // 平滑类型
ENUM_APPLIED_PRICE applied_price // 价格或者处理程序类型
);
这个函数,但是applied_price只能使用ENUM_APPLIED_PRICE中的类型数据 ,怎样添加其他的数值呢,或者使用别的方法呢,初学,还请唐老师指点下,多谢!!
这就涉及更复杂的计算了,可以用下面的这个函数试一下。
先计算差价,再对这个差价进行ma的处理。也就是把差价的数组加到下面函数的第一个参数里。
double iMAOnArrayMQL4(double &array[],
int total,
int period,
int ma_shift,
int ma_method,
int shift)
{
double buf[],arr[];
if(total==0) total=ArraySize(array);
if(total>0 && total<=period) return(0);
if(shift>total-period-ma_shift) return(0);
switch(ma_method)
{
case MODE_SMA :
{
total=ArrayCopy(arr,array,0,shift+ma_shift,period);
if(ArrayResize(buf,total)<0) return(0);
double sum=0;
int i,pos=total-1;
for(i=1;i<period;i++,pos--)
sum+=arr;
while(pos>=0)
{
sum+=arr;
buf=sum/period;
sum-=arr;
pos--;
}
return(buf);
}
case MODE_EMA :
{
if(ArrayResize(buf,total)<0) return(0);
double pr=2.0/(period+1);
int pos=total-2;
while(pos>=0)
{
if(pos==total-2) buf=array;
buf=array*pr+buf*(1-pr);
pos--;
}
return(buf);
}
case MODE_SMMA :
{
if(ArrayResize(buf,total)<0) return(0);
double sum=0;
int i,k,pos;
pos=total-period;
while(pos>=0)
{
if(pos==total-period)
{
for(i=0,k=pos;i<period;i++,k++)
{
sum+=array;
buf=0;
}
}
else sum=buf*(period-1)+array;
buf=sum/period;
pos--;
}
return(buf);
}
case MODE_LWMA :
{
if(ArrayResize(buf,total)<0) return(0);
double sum=0.0,lsum=0.0;
double price;
int i,weight=0,pos=total-1;
for(i=1;i<=period;i++,pos--)
{
price=array;
sum+=price*i;
lsum+=price;
weight+=i;
}
pos++;
i=pos+period;
while(pos>=0)
{
buf=sum/weight;
if(pos==0) break;
pos--;
i--;
price=array;
sum=sum-lsum+price*period;
lsum-=array;
lsum+=price;
}
return(buf);
}
default: return(0);
}
return(0);
}
页:
[1]