c# KDJ演算法實現

來源:互聯網
上載者:User

此演算法在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;

 

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         }

 

最終圖WP7:

同花順圖:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.