首页 > 学院 > 开发设计 > 正文

[原][C#][winForm]分级基金折溢价WinForm网络计算器

2019-11-17 02:20:48
字体:
来源:转载
供稿:网友

[原][C#][winForm]分级基金折溢价WinForm网络计算器

分级基金折溢价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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表