# c# MACD 算法实现

DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);
DEA  : EMA(DIFF,M);
MACD : 2*(DIFF-DEA);
Zero : 0;

<pre name="code" class="csharp">/// <summary>
/// MACD算法
/// </summary>
/// <param name="n1">12</param>
/// <param name="n2">26</param>
/// <param name="n3">9</param>
/// <param name="day">K线数据</param>
/// <returns></returns>
public static List<KLinfo> ComputationMACD2(int SHORT, int LONG, int M, List<KLinfo> KLStocklist)
{
for (int i = 0; i < KLStocklist.Count;i++ )
{

if (KLStocklist[i].KID == 1)
{
KLStocklist[i].Ema12Value = KLStocklist[i].ClosePrice;
KLStocklist[i].Ema26Value = KLStocklist[i].ClosePrice;
KLStocklist[i].DifValue = KLStocklist[i].Ema12Value - KLStocklist[i].Ema26Value;
KLStocklist[i].DeaValue = KLStocklist[i].DifValue;
KLStocklist[i].MacdValue = 2.0 * (KLStocklist[i].DifValue - KLStocklist[i].DeaValue);
}
else
{
KLStocklist[i].Ema12Value = (2 * KLStocklist[i].ClosePrice + (SHORT - 1) * KLStocklist[KLStocklist[i].KID - 2].Ema12Value) / (SHORT + 1);
KLStocklist[i].Ema26Value = (2 * KLStocklist[i].ClosePrice + (LONG - 1) * KLStocklist[KLStocklist[i].KID - 2].Ema26Value) / (LONG + 1);
KLStocklist[i].DifValue = KLStocklist[i].Ema12Value - KLStocklist[i].Ema26Value;
KLStocklist[i].DeaValue = (2 * KLStocklist[i].DifValue + (M - 1) * KLStocklist[KLStocklist[i].KID - 2].DeaValue) / (M + 1);
KLStocklist[i].MacdValue = 2.0 * (KLStocklist[i].DifValue - KLStocklist[i].DeaValue);

}
}

return KLStocklist;
}

1  /// <summary>
/// KDJ算法
/// </summary>
/// <param name="N">9</param>
/// <param name="M1">3</param>
/// <param name="M2">3</param>
/// <param name="KLStocklist">K线数据</param>
/// <returns></returns>
public static List<KLinfo> ComputationKJD(int N, int M1, int M2, List<KLinfo> KLStocklist)
{

for (int i = 0; i < KLStocklist.Count; i++)
{
double RSV = 0;
double a = 0;
double b = 0;
double e = 0;

GetMinMaxPirce(i + 1, N, KLStocklist);

if (KLStocklist[i].KID == 1)
{
RSV = (KLStocklist[i].ClosePrice - KLStocklist[i].MinPrice) / (KLStocklist[i].MaxPrice - KLStocklist[i].MinPrice) * 100;
a = (1 * RSV + (M1 - 1) * 0) / 1;
b = (1 * a + (M2 - 1) * 0) / 1;
e = 3 * a - 2 * b;

}
else
{
RSV = (KLStocklist[i].ClosePrice - KLStocklist[i].MinPrice) / (KLStocklist[i].MaxPrice - KLStocklist[i].MinPrice) * 100;
a = (1 * RSV + (M1 - 1) * KLStocklist[i - 1].Kvalue) / M1;
b = (1 * a + (M2 - 1) * KLStocklist[i - 1].Dvalue) / M2;
e = 3 * a - 2 * b;
}

KLStocklist[i].RSV = RSV;
KLStocklist[i].Kvalue = a;
KLStocklist[i].Dvalue = b;
KLStocklist[i].Jvalue = e;

if (a < 0) KLStocklist[i].Kvalue = 0;
if (a > 100) KLStocklist[i].Kvalue = 100;
if (b < 0) KLStocklist[i].Dvalue = 0;
if (b > 100) KLStocklist[i].Dvalue = 100;
if (e < 0) KLStocklist[i].Jvalue = 0;
if (e > 100) KLStocklist[i].Jvalue = 100;
}

return null;

}

/// <summary>
/// 获取区间最大最小
/// </summary>
/// <param name="Index">当日K线</param>
/// <param name="N">9</param>
/// <param name="KLStocklist">K线数据</param>
/// <returns></returns>
public static void GetMinMaxPirce(int Index, int N, List<KLinfo> KLStocklist)
{

var MinPirce = from n in KLStocklist where n.KID <= Index && n.KID > ((Index - N) < 0 ? 0 : (Index - N)) select n;

if (MinPirce.Count() != 0)
{
KLStocklist[Index-1].MinPrice = MinPirce.Select(n => n.LowPrice).Min();
KLStocklist[Index-1].MaxPrice = MinPirce.Select(n => n.HighPrice).Max();
}

}