# 请问下如何使用iMA（）计算某根线的10日移动平均

iMA最後一個參數是用來設定K線的位置

Hung Wen Lin:

iMA最後一個參數是用來設定K線的位置

iMA只能算K线的数据。

```//+------------------------------------------------------------------+
//|                                               MovingAverages.mqh |
//|                   Copyright 2009-2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
//+------------------------------------------------------------------+
//| Simple Moving Average                                            |
//+------------------------------------------------------------------+
double SimpleMA(const int position,const int period,const double &price[])
{
//---
double result=0.0;
//--- check position
if(position>=period-1 && period>0)
{
//--- calculate value
for(int i=0;i<period;i++) result+=price[position-i];
result/=period;
}
//---
return(result);
}
//+------------------------------------------------------------------+
//| Exponential Moving Average                                       |
//+------------------------------------------------------------------+
double ExponentialMA(const int position,const int period,const double prev_value,const double &price[])
{
//---
double result=0.0;
//--- calculate value
if(period>0)
{
double pr=2.0/(period+1.0);
result=price[position]*pr+prev_value*(1-pr);
}
//---
return(result);
}```

Ziheng Zhuang:

iMA只能算K线的数据。