分級基金折溢價WinForm網絡計算器
通過子/母基金代碼,從 [東方財富網,天天基金網,新浪] 抓取分級基金的子母基金數據(代碼,名稱,凈值,價格),
并計算出子基金(A基金,B基金)以及母基金的溢價率與折價率,
便于投資/投機客從中套利。
數據通過網站獲取,使用基金凈值,非估值,一般晚間網站會更新當天基金凈值,不可用于實時計算。
------
效果圖:
----
代碼:
1.窗體如效果圖
2.常用變量與數據抓取代碼:
1 namespace fundGetAndCal 2 { 3 public static class CONST 4 { 5 //獲取母子基金代碼 6 public static string fundF10Url = "http://fund.eastmoney.com/f10/jbgk_{%fundCode%}.html"; 7 //獲取凈值 8 public static string fundValUrl = "http://fund.eastmoney.com/{%fundCode%}.html"; 9 //獲取價格10 public static string fundPRiceUrl = "http://hq.sinajs.cn/?list={%fundAllCode%}";11 //母子基金列表12 public static List<string[]> allFunCodeLst;13 //讀取完了14 public static bool okFlg = false;15 //狀態顯示文字16 public static string show = "就緒";17 public static int barMax = 100;18 public static int barNow = 100;19 20 public static string GetHtml(string url, Encoding encode)21 {22 string strBuff = "";//定義文本字符串,用來保存下載的html23 try24 {25 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);26 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();27 //若成功取得網頁的內容,則以System.IO.Stream形式返回,若失敗則產生ProtoclViolationException錯 誤。在此正確的做法應將以下的代碼放到一個try塊中處理。這里簡單處理28 Stream reader = webResponse.GetResponseStream();29 ///返回的內容是Stream形式的,所以可以利用StreamReader類獲取GetResponseStream的內容,并以StreamReader類的Read方法依次讀取網頁源程序代碼每一行的內容,直至行尾(讀取的編碼格式:UTF8)30 StreamReader respStreamReader = new StreamReader(reader,/*Encoding.UTF8*/encode);31 32 strBuff = respStreamReader.ReadToEnd();33 }34 catch (Exception e)35 {36 Console.Write(url+"/n"+e.Message);37 }38 return strBuff;39 }40 }41 }
3.窗體按鈕等事件代碼:
1 namespace fundGetAndCal 2 { 3 public partial class Form1 : Form 4 { 5 public Form1() 6 { 7 InitializeComponent(); 8 } 9 10 private void Form1_Load(object sender, EventArgs e) 11 { 12 getFundCode(); 13 } 14 private void btnFundCodeRead_Click(object sender, EventArgs e) 15 { 16 getFundCode(); 17 } 18 private void getFundCode() 19 { 20 string waitFuncCodes = ConfigurationManager.AppSettings.Get("fundCode"); 21 waitFuncCodes = waitFuncCodes.Replace(",", "/r/n"); 22 fundCodeLst.Text = waitFuncCodes; 23 } 24 private string delStr(string org, string other) 25 { 26 if (!other.Equals("")) 27 org = org.Replace(other, ""); 28 return Regex.Replace(org, @"<.*?>", ""); 29 } 30 private void btnStart_Click(object sender, EventArgs e) 31 { 32 Thread thread = new Thread(getData); 33 thread.IsBackground = true; 34 CONST.barMax = 100; 35 CONST.barNow = 0; 36 37 btnStart.Enabled = false; 38 btnStop.Enabled = true; 39 CONST.okFlg = false; 40 41 thread.Start(); 42 timer1.Start(); 43 } 44 45 private void getData() 46 { 47 /// 提取母子基金代碼 48 49 // 提取母基金正則 50 Regex regMJJ = new Regex(@"母子基金.*?<//td>"); 51 // 提取基金凈值正則 52 Regex regJZ = new Regex(@"<span class=/'left12/'>.*?<//span>"); 53 // 提取基金名稱正則 54 Regex regMC = new Regex(@"<title.*?<//title>"); 55 string[] fcl = fundCodeLst.Text.Replace("/r/n", ",").Split(','); 56 List<string[]> allFCLst = new List<string[]>(); 57 List<string> geted = new List<string>(); 58 59 foreach (string fc in fcl) 60 { 61 CONST.show = "開始獲取[" + fc + "]子母基金代碼"; 62 if (geted.Contains(fc.Substring(2, fc.Length - 2))) 63 continue; 64 string html = CONST.GetHtml(CONST.fundF10Url.Replace("{%fundCode%}", fc.Substring(2, fc.Length - 2)), Encoding.GetEncoding("gb2312")); 65 66 var result = regMJJ.Match(html).Groups; 67 if (result.Count > 0) 68 { 69 // 1-3 代碼,4-6 凈值 7-9 價格 10 sz/ss 11-13 名稱 70 string[] allFC = new string[13] { "", "", "", "", "", "", "", "", "", "", "", "", "" }; 71 var r = delStr(result[0].Value, "母子基金"); 72 string[] t1 = Regex.Split(r, "(母)", RegexOptions.IgnoreCase); 73 geted.Add(t1[0]); 74 allFC[0] = t1[0]; 75 if (t1.Length > 1) 76 { 77 string[] t2 = t1[1].Split(' '); 78 for (int i = 0; i < t2.Length; i++) 79 { 80 geted.Add(t2[i].Replace("(子)", "")); 81 allFC[i + 1] = t2[i].Replace("(子)", ""); 82 } 83 if (t2.Length == 2) 84 { 85 allFC[9] = fc.Substring(0, 2); 86 allFCLst.Add(allFC); 87 } 88 } 89 } 90 //Thread.Sleep(1000); 91 } 92 CONST.barNow = CONST.barNow + (int)Math.Floor((decimal)CONST.barMax / 3); 93 CONST.allFunCodeLst = allFCLst; 94 95 // 獲取凈值,名稱 96 foreach (string[] allFC in CONST.allFunCodeLst) 97 { 98 for (int i = 0; i < 3; i++) 99 {100 CONST.show = "開始獲取[" + allFC[i] + "]凈值與名稱";101 string html = CONST.GetHtml(CONST.fundValUrl.Replace("{%fundCode%}", allFC[i]), Encoding.GetEncoding("gb2312"));102 103 var result = regJZ.Match(html).Groups;104 if (result.Count > 0)105 {106 allFC[3 + i] = delStr(result[0].Value, "");107 }108 result = regMC.Match(html).Groups;109 string[] a = delStr(result[0].Value, "").Split('(');110 if (a.Length > 0)111 {112 allFC[10 + i] = a[0];113 }114 115 //Thread.Sleep(1000);116 }117 }118 CONST.barNow = CONST.barNow + (int)Math.Floor((decimal)CONST.barMax / 3);119 120 // 獲取價格121 foreach (string[] allFC in CONST.allFunCodeLst)122 {123 for (int i = 1; i < 3; i++)124 {125 CONST.show = "開始獲取[" + allFC[i] + "]價格";126 string html = CONST.GetHtml(CONST.fundPriceUrl.Replace("{%fundAllCode%}", allFC[9] + allFC[i]), Encoding.GetEncoding("gb2312"));127 128 string[] result = html.Split(',');129 if (result.Length > 4)130 {131 allFC[6 + i] = result[3];132 }133 //Thread.Sleep(1000);134 }135 }136 CONST.barNow = CONST.barNow + (int)Math.Floor((decimal)CONST.barMax / 3);137 CONST.okFlg = true;138 139 }140 141 private void timer1_Tick(object sender, EventArgs e)142 {143 if (CONST.okFlg)144 {145 CONST.show = "開始獲取計算溢折價,并輸出";146 // 計算A,B基金溢折價,母基金溢折價147 dataGridView1.Rows.Clear();148 dataGridView1.Columns.Clear();149
新聞熱點
疑難解答