此演算法在自己WP7應用中 用到 同花順 演算法DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);
DEA : EMA(DIFF,M);
MACD : 2*(DIFF-DEA);
Zero : 0;關鍵點是第一天的資料如果是區間的資料,區間第一天的DIF DEA MACD 都是0 View Code
1 /// <summary>
2 /// MACD演算法
3 /// </summary>
4 /// <param name="n1">12</param>
5 /// <param name="n2">26</param>
6 /// <param name="n3">9</param>
7 /// <param name="day">K線資料</param>
8 /// <returns></returns>
9 public static List<KLinfo> ComputationMACD2(int SHORT, int LONG, int M, List<KLinfo> KLStocklist)
10 {
11 for (int i = 0; i < KLStocklist.Count;i++ )
12 {
13
14 if (KLStocklist[i].KID == 1)
15 {
16 KLStocklist[i].Ema12Value = KLStocklist[i].ClosePrice;
17 KLStocklist[i].Ema26Value = KLStocklist[i].ClosePrice;
18 KLStocklist[i].DifValue = KLStocklist[i].Ema12Value - KLStocklist[i].Ema26Value;
19 KLStocklist[i].DeaValue = KLStocklist[i].DifValue;
20 KLStocklist[i].MacdValue = 2.0 * (KLStocklist[i].DifValue - KLStocklist[i].DeaValue);
21 }
22 else
23 {
24 KLStocklist[i].Ema12Value = (2 * KLStocklist[i].ClosePrice + (SHORT - 1) * KLStocklist[KLStocklist[i].KID - 2].Ema12Value) / (SHORT + 1);
25 KLStocklist[i].Ema26Value = (2 * KLStocklist[i].ClosePrice + (LONG - 1) * KLStocklist[KLStocklist[i].KID - 2].Ema26Value) / (LONG + 1);
26 KLStocklist[i].DifValue = KLStocklist[i].Ema12Value - KLStocklist[i].Ema26Value;
27 KLStocklist[i].DeaValue = (2 * KLStocklist[i].DifValue + (M - 1) * KLStocklist[KLStocklist[i].KID - 2].DeaValue) / (M + 1);
28 KLStocklist[i].MacdValue = 2.0 * (KLStocklist[i].DifValue - KLStocklist[i].DeaValue);
29
30 }
31 }
32
33 return KLStocklist;
34 }