23 9月 2008

Exponential Moving Average(EMA)

Exponential Moving Average(EMA)-MetaQuotes
Exponential Moving Average(EMA)-DanielsTrading

Calculation:

Exponential Moving Average (EMA)
Exponentially smoothed moving average is calculated by adding the moving
average of a certain share of the current closing price to the previous value.
With exponentially smoothed moving averages, the latest prices are of more
value. P-percent exponential moving average will look like:

EMA = (CLOSE(i)*P)+(EMA(i-1)*(100-P))

Where:
CLOSE(i) — the price of the current period closure;
EMA(i-1) — Exponentially Moving Average of the previous period closure;
P — the percentage of using the price value.

這指標在TS內部並無內建指標,但是HTS內建有,因此在轉部份HTS
程式到TS上的時候會需要自己新增自訂函數,在這裡就直接把這部
份的程式碼直接轉成TS,使用者自己加入就可以了。

  • 函數名稱取名自定,或者同HTS內建名稱:EMA
  • 設定完後要記得按Verify(F3)

TS新增自訂函數

TS程式碼(修改HTS源碼)
Inputs: Price(Numeric), Length(Numeric);
Variables: Factor(0);

If CurrentBar = 1 Then begin
   EMA = Price;
end
Else begin
   If CurrentBar <= Length -1 Then begin     
       Factor = 2 / (CurrentBar + 1);     
       EMA = Price * Factor + (1 - Factor) * EMA[1];  
   end  
   Else begin     
       Factor = 2 / (Length + 1);                
       EMA = Factor * Price + (1 - Factor) * EMA[1];  
   end;
End;

3 則留言:

  1. 請教小達人,我function照copy這個TS程式是verify OK,但是我又設成indicator後,雖然verify OK,但圖卻畫不出來,我indicator只寫下面這三行

    inputs : price(close),length(21);
    value1 = EMA(price,length);
    plot1(value1,"EMA");

    請問有哪個地方是錯誤的? 謝謝指導

    回覆刪除
  2. 抱歉..現在才看到你的問題
    我要先去Meeting

    晚點會在另一篇回復^^

    回覆刪除
  3. 哈哈~ 沒關係啦! 後來發現其實TS裏的xaverage就是EMA,所以根本不必寫function ...
    不過就算是再寫一個function,應該也要畫得出來,呵呵!太笨了想不出來 ...

    回覆刪除