c# MACD 算法实现
此算法在自己WP7应用中 用到
同花顺 算法
DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);
DEA : EMA(DIFF,M);
MACD : 2*(DIFF-DEA);
Zero : 0;
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 }
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 }