c# KDJ算法实现
此算法在WP7 个人炒股软件 GilStock 中用到
同花顺算法描述:
1 RSV=(CLOSE-LLV(LOW,N))/(HHV(HIGH,N)-LLV(LOW,N))*100;
2 a=SMA(RSV,M1,1);
3 b=SMA(a,M2,1);
4 e=3*a-2*b;
5 IF(a<0) a=0;
6 IF(a>100) a=100;
7 IF(b<0) b=0;
8 IF(b>100) b=100;
9 IF(e<0) e=0;
10 IF(e>100) e=100;
11 K:a;
12 D:b;
13 J:e;
2 a=SMA(RSV,M1,1);
3 b=SMA(a,M2,1);
4 e=3*a-2*b;
5 IF(a<0) a=0;
6 IF(a>100) a=100;
7 IF(b<0) b=0;
8 IF(b>100) b=100;
9 IF(e<0) e=0;
10 IF(e>100) e=100;
11 K:a;
12 D:b;
13 J:e;
c#实现,理论区间内和新股上市的9天后的KDJ数据才是正确的。
View Code
1 /// <summary>
2 /// KDJ算法
3 /// </summary>
4 /// <param name="N">9</param>
5 /// <param name="M1">3</param>
6 /// <param name="M2">3</param>
7 /// <param name="KLStocklist">K线数据</param>
8 /// <returns></returns>
9 public static List<KLinfo> ComputationKJD(int N, int M1, int M2, List<KLinfo> KLStocklist)
10 {
11
12 for (int i = 0; i < KLStocklist.Count; i++)
13 {
14 double RSV = 0;
15 double a = 0;
16 double b = 0;
17 double e = 0;
18
19 GetMinMaxPirce(i + 1, N, KLStocklist);
20
21 if (KLStocklist[i].KID == 1)
22 {
23 RSV = (KLStocklist[i].ClosePrice - KLStocklist[i].MinPrice) / (KLStocklist[i].MaxPrice - KLStocklist[i].MinPrice) * 100;
24 a = (1 * RSV + (M1 - 1) * 0) / 1;
25 b = (1 * a + (M2 - 1) * 0) / 1;
26 e = 3 * a - 2 * b;
27
28 }
29 else
30 {
31 RSV = (KLStocklist[i].ClosePrice - KLStocklist[i].MinPrice) / (KLStocklist[i].MaxPrice - KLStocklist[i].MinPrice) * 100;
32 a = (1 * RSV + (M1 - 1) * KLStocklist[i - 1].Kvalue) / M1;
33 b = (1 * a + (M2 - 1) * KLStocklist[i - 1].Dvalue) / M2;
34 e = 3 * a - 2 * b;
35 }
36
37 KLStocklist[i].RSV = RSV;
38 KLStocklist[i].Kvalue = a;
39 KLStocklist[i].Dvalue = b;
40 KLStocklist[i].Jvalue = e;
41
42 if (a < 0) KLStocklist[i].Kvalue = 0;
43 if (a > 100) KLStocklist[i].Kvalue = 100;
44 if (b < 0) KLStocklist[i].Dvalue = 0;
45 if (b > 100) KLStocklist[i].Dvalue = 100;
46 if (e < 0) KLStocklist[i].Jvalue = 0;
47 if (e > 100) KLStocklist[i].Jvalue = 100;
48 }
49
50
51
52 return null;
53
54 }
55
56 /// <summary>
57 /// 获取区间最大最小
58 /// </summary>
59 /// <param name="Index">当日K线</param>
60 /// <param name="N">9</param>
61 /// <param name="KLStocklist">K线数据</param>
62 /// <returns></returns>
63 public static void GetMinMaxPirce(int Index, int N, List<KLinfo> KLStocklist)
64 {
65
66 var MinPirce = from n in KLStocklist where n.KID <= Index && n.KID > ((Index - N) < 0 ? 0 : (Index - N)) select n;
67
68 if (MinPirce.Count() != 0)
69 {
70 KLStocklist[Index-1].MinPrice = MinPirce.Select(n => n.LowPrice).Min();
71 KLStocklist[Index-1].MaxPrice = MinPirce.Select(n => n.HighPrice).Max();
72 }
73
74
75 }
2 /// KDJ算法
3 /// </summary>
4 /// <param name="N">9</param>
5 /// <param name="M1">3</param>
6 /// <param name="M2">3</param>
7 /// <param name="KLStocklist">K线数据</param>
8 /// <returns></returns>
9 public static List<KLinfo> ComputationKJD(int N, int M1, int M2, List<KLinfo> KLStocklist)
10 {
11
12 for (int i = 0; i < KLStocklist.Count; i++)
13 {
14 double RSV = 0;
15 double a = 0;
16 double b = 0;
17 double e = 0;
18
19 GetMinMaxPirce(i + 1, N, KLStocklist);
20
21 if (KLStocklist[i].KID == 1)
22 {
23 RSV = (KLStocklist[i].ClosePrice - KLStocklist[i].MinPrice) / (KLStocklist[i].MaxPrice - KLStocklist[i].MinPrice) * 100;
24 a = (1 * RSV + (M1 - 1) * 0) / 1;
25 b = (1 * a + (M2 - 1) * 0) / 1;
26 e = 3 * a - 2 * b;
27
28 }
29 else
30 {
31 RSV = (KLStocklist[i].ClosePrice - KLStocklist[i].MinPrice) / (KLStocklist[i].MaxPrice - KLStocklist[i].MinPrice) * 100;
32 a = (1 * RSV + (M1 - 1) * KLStocklist[i - 1].Kvalue) / M1;
33 b = (1 * a + (M2 - 1) * KLStocklist[i - 1].Dvalue) / M2;
34 e = 3 * a - 2 * b;
35 }
36
37 KLStocklist[i].RSV = RSV;
38 KLStocklist[i].Kvalue = a;
39 KLStocklist[i].Dvalue = b;
40 KLStocklist[i].Jvalue = e;
41
42 if (a < 0) KLStocklist[i].Kvalue = 0;
43 if (a > 100) KLStocklist[i].Kvalue = 100;
44 if (b < 0) KLStocklist[i].Dvalue = 0;
45 if (b > 100) KLStocklist[i].Dvalue = 100;
46 if (e < 0) KLStocklist[i].Jvalue = 0;
47 if (e > 100) KLStocklist[i].Jvalue = 100;
48 }
49
50
51
52 return null;
53
54 }
55
56 /// <summary>
57 /// 获取区间最大最小
58 /// </summary>
59 /// <param name="Index">当日K线</param>
60 /// <param name="N">9</param>
61 /// <param name="KLStocklist">K线数据</param>
62 /// <returns></returns>
63 public static void GetMinMaxPirce(int Index, int N, List<KLinfo> KLStocklist)
64 {
65
66 var MinPirce = from n in KLStocklist where n.KID <= Index && n.KID > ((Index - N) < 0 ? 0 : (Index - N)) select n;
67
68 if (MinPirce.Count() != 0)
69 {
70 KLStocklist[Index-1].MinPrice = MinPirce.Select(n => n.LowPrice).Min();
71 KLStocklist[Index-1].MaxPrice = MinPirce.Select(n => n.HighPrice).Max();
72 }
73
74
75 }
最终图WP7:
同花顺图: