public void GetAllFundTest() { WebService target = new WebService(); // TODO: 初始化为适当的值 //Assert.Inconclusive("验证此测试方法的正确性。"); string HtmlText = target.GetWebContent("http://fund.eastmoney.com/js/fundcode_search.js?v=20130718.js", Encoding.UTF8); HtmlText = HtmlText.Replace(",", ""); HtmlText = HtmlText.Replace("][", ""); HtmlText = HtmlText.Replace("\"\"", "\""); //对处理过后的数据进行匹配 Regex regex = new Regex("(?<=\").+?(?=\")", RegexOptions.None); MatchCollection MC = regex.Matches(HtmlText); List<String> AllFund = new List<String>(); //将匹配后的数据存在一个字符串数组中 foreach (Match ma in MC) { AllFund.Add(ma.Value); } //按四个一组读,并将读后的数据存入基金类列表中 List<Fund> Fundlist = new List<Fund>(); for (int n = 0; n < AllFund.Count; n = n + 4) { Fund found = new Fund(); found.Code = AllFund[n]; found.Abbr = AllFund[n + 1]; found.Name = AllFund[n + 2]; found.Type = AllFund[n + 3]; Fundlist.Add(found); } bool expected = true; bool actual = Fundlist.Count > 0; Assert.AreEqual(expected, actual); }
public void comparTest() { Fund f1 = new Fund(); // TODO: 初始化为适当的值 Fund f2 = new Fund(); // TODO: 初始化为适当的值 Fund f3 = new Fund(); f1.Name = "华夏成长"; f2.Name = "华夏成长"; f3.Name = "敏捷开发"; List<int> expected = new List<int>(); // TODO: 初始化为适当的值 expected.Add(-1); expected.Add(0); expected.Add(1); List<int> actual = new List<int>(); //actual = Search_Accessor.compar(f1, f2); if (f1.Name.CompareTo(f3.Name) < 0) actual.Add(-1); if (f1.Name == f2.Name) actual.Add(0); if (f3.Name.CompareTo(f2.Name) > 0) actual.Add(1); Assert.AreEqual(expected[0], actual[0]); Assert.AreEqual(expected[1], actual[1]); Assert.AreEqual(expected[2], actual[2]); }
private static int compar(Fund f1, Fund f2) { if (f1.Name == f2.Name) return 0; else if (f1.Name.CompareTo(f2.Name) < 0) return -1; else if (f1.Name.CompareTo(f2.Name) > 0) return 1; return -2; }
public static double Predict(Fund fund, double todayValue, int index) { double incOnce = todayValue - fund.HistoryList[index].Item2; //double minValue = double.MaxValue; double lastMaxValue = -1; double lastMinValue = -1; for (int i = index; i >= fund.ThinkStartIndex; i--) { if (lastMaxValue > 0 && lastMinValue > 0) { break; } if (fund.IncFlags[i - fund.ThinkStartIndex] == 1) { //找到极大值 lastMaxValue = fund.HistoryList[i].Item2; } else if (fund.IncFlags[i - fund.ThinkStartIndex] == -1) { lastMinValue = fund.HistoryList[i].Item2; } } double incSum1 = todayValue - lastMaxValue; double incSum2 = todayValue - lastMinValue; double incSum = 0.0; if (Math.Abs(incSum1) > Math.Abs(incSum2)) { incSum = incSum1; } else { incSum = incSum2; } double equation = EquationCalculate(fund.Coefs[1], fund.Coefs[0], index + 1 - fund.ThinkStartIndex); double regress = todayValue - equation; double score = GetScore(fund.V1, fund.V2, fund.V3, incOnce, incSum, regress); //fund.V1 * incOnce + fund.V2 * incSum + fund.V3 * regress; return(GetFundResult(fund, score)); }
private void ChartDarw(Fund fund) { chartFundCode = fund.Code; tabControl1.TabPages[3].Text = fund.Name; chart1.Series["line1"].Points.Clear(); chart1.Series["line2"].Points.Clear(); chart1.Series["line3"].Points.Clear(); chart1.Series["line4"].Points.Clear(); chart1.Series["line5"].Points.Clear(); int x1 = 0; double y1 = Think.EquationCalculate(fund.Coefs[1], fund.Coefs[0], x1); int x2 = fund.ThinkEndIndex - fund.ThinkStartIndex; double y2 = Think.EquationCalculate(fund.Coefs[1], fund.Coefs[0], x2); chart1.Series["line4"].Points.AddXY(x1, y1); chart1.Series["line4"].Points.AddXY(x2, y2); for (int i = fund.ThinkStartIndex; i < fund.ThinkEndIndex; i++) { chart1.Series["line1"].Points.AddXY(i - fund.ThinkStartIndex, fund.HistoryList[i].Item2); if (fund.IncFlags[i - fund.ThinkStartIndex] == 1) { chart1.Series["line2"].Points.AddXY(i - fund.ThinkStartIndex, fund.HistoryList[i].Item2); } else if (fund.IncFlags[i - fund.ThinkStartIndex] == -1) { chart1.Series["line3"].Points.AddXY(i - fund.ThinkStartIndex, fund.HistoryList[i].Item2); } //if (fund.Tages[i - fund.ThinkStartIndex] == 1) //{ // chart1.Series["line5"].Points.AddXY(i - fund.ThinkStartIndex, fund.HistoryList[i].Item2); //} } chart1.Series["line1"].Points.AddXY(fund.ThinkEndIndex - fund.ThinkStartIndex, fund.RealValue); //实时的加到最后 TabPage page = tabControl1.TabPages[3]; tabControl1.SelectedTab = page; //tabControl1.TabPages.Remove(page); //tabControl1.TabPages.Add(page); }
private static double GetFundResult(Fund fund, double score) { double result = 0; if (score > fund.μMax - fund.σMax) { //卖出 double vμ = fund.MaxNormalDistribution(fund.μMax); double vx = fund.MaxNormalDistribution(score); double vσ = fund.MaxNormalDistribution(fund.μMax - fund.σMax); result = score > fund.μMax ? 2 * (vμ - vσ) - (vx - vσ) : vx - vσ; return(-result); //卖出为负 } else if (score < fund.μMin + fund.σMin) { //买入 double vμ = fund.MinNormalDistribution(fund.μMin); double vx = fund.MinNormalDistribution(score); double vσ = fund.MinNormalDistribution(fund.μMin - fund.σMin); result = score < fund.μMin ? 2 * (vμ - vσ) - (vx - vσ) : vx - vσ; return(result); //卖出为负 } return(result); }
private void ToLineChart() { Fund fund = FundList[RowIndex]; Dictionary <string, string> value = new Dictionary <string, string>(); ArrayList al = new ArrayList(); chart1.ChartAreas[0].AxisX.Title = "日期"; chart1.ChartAreas[0].AxisY.Title = "净值"; //chart1.ChartAreas[0].CursorX.IsUserEnabled = true; //chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true; chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true; //chart1.ChartAreas[0].CursorY.IsUserEnabled = true; //chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; //chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true; chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true; chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true; chart1.ChartAreas[0].AxisX.ScrollBar.Size = 10; chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All; chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = double.NaN; chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 2; //chart1.Titles[0].Name = fund.Name + "基金净值变化情况"; //无数据情况 if (fund.NetValue.Count == 0) { return; } if (chart1.Series.Count < 2) { chart1.Series.Add("Series2"); chart1.Series.Add("Series3"); chart1.Series["Series3"].ChartType = SeriesChartType.Line; chart1.Series["Series2"].ChartType = SeriesChartType.Line; } if (fund.NetValue[0].NetValue != string.Empty && fund.NetValue[0].AccumulativeNetValue != string.Empty) { foreach (NetValueOfFund netValue in fund.NetValue) { value.Add(netValue.Date, netValue.NetValue); al.Add(netValue.AccumulativeNetValue); } chart1.Series[0].LegendText = "单位净值(元)"; chart1.Series[1].LegendText = "累计净值(元)"; } if (fund.NetValue[0].EarningPer10000 != string.Empty) { foreach (NetValueOfFund netValue in fund.NetValue) { value.Add(netValue.Date, netValue.EarningPer10000); } chart1.Series[0].LegendText = "每万份收益(元)"; //chart1.Legends[0].Position chart1.Series.Remove(chart1.Series[1]); } if (al.Count != 0) { chart1.Series["Series3"].Points.DataBindXY(value.Keys, al); chart1.Series[1].ToolTip = "日期:#VALX\n净值:#VAL"; } chart1.Series["Series2"].Points.DataBindXY(value.Keys, value.Values); chart1.Series[0].ToolTip = "日期:#VALX\n净值:#VAL"; chart1.Visible = true; }
public List <Fund> GetAllFund() { String HtmlText; try { HtmlText = GetWebContent("http://fund.eastmoney.com/js/fundcode_search.js?v=20130718.js", Encoding.UTF8); } catch (Exception) { return(null); } HtmlText = HtmlText.Replace(",", ""); HtmlText = HtmlText.Replace("][", ""); HtmlText = HtmlText.Replace("\"\"", "\""); //对处理过后的数据进行匹配 Regex regex = new Regex("(?<=\").+?(?=\")", RegexOptions.None); MatchCollection MC = regex.Matches(HtmlText); List <String> AllFund = new List <String>(); //将匹配后的数据存在一个字符串数组中 foreach (Match ma in MC) { AllFund.Add(ma.Value); } //按四个一组读,并将读后的数据存入基金类列表中 List <Fund> Fundlist = new List <Fund>(); try { for (int n = 0; n < AllFund.Count; n = n + 4) { Fund found = new Fund(); found.Code = AllFund[n]; found.Abbr = AllFund[n + 1]; found.Name = AllFund[n + 2]; found.Type = AllFund[n + 3]; Fundlist.Add(found); } } catch (Exception e) { Console.WriteLine(e.ToString()); return(null); } HtmlText = GetWebContent("http://fund.eastmoney.com/fund.html", Encoding.GetEncoding("gb2312")); int head = 0; head = HtmlText.IndexOf("type=\"checkbox\" id=", head); int foot = 0; String code; Double netValueToday, totalNetValueToday, netValueInsToday, netValueInsRateToday; while (head > 0) { head = HtmlText.IndexOf("</td><td>", head) + 1; head = HtmlText.IndexOf("</td><td>", head) + 9; foot = head + 6; code = HtmlText.Substring(head, foot - head); head = HtmlText.IndexOf("<td class=\"TD2\">", head); head += 16; foot = HtmlText.IndexOf("<", head); netValueToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head)); head = HtmlText.IndexOf("<td class=\"TD2\">", head); head += 16; foot = HtmlText.IndexOf("<", head); totalNetValueToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head)); head = HtmlText.IndexOf("<td class=", head) + 1; head = HtmlText.IndexOf("<td class=", head) + 1; head = HtmlText.IndexOf("<td class=", head) + 1; head = HtmlText.IndexOf("'>", head); head += 2; foot = HtmlText.IndexOf("<", head); netValueInsToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head)); foot += 10; head = HtmlText.IndexOf(">", foot); head += 1; foot = HtmlText.IndexOf("<", head); netValueInsRateToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head - 1)); foreach (Fund f in Fundlist) { if (f.Code == code) { f.NetValueToday = netValueToday; f.TotalNetValueToday = totalNetValueToday; f.NetValueInsToday = netValueInsToday; f.NetValueInsRateToday = netValueInsRateToday; } } head = HtmlText.IndexOf("type=\"checkbox\" id=", foot); //MessageBox.Show(HtmlText.Substring(head, 500)); } return(Fundlist); }
public static double Predict(Fund fund) { return(Predict(fund, (double)fund.RealValue, fund.HistoryList.Count - 1)); }
/// <summary> /// /// </summary> public static void Calculate(DateTime startTime, DateTime endTime, Fund fund) { int startIndex = fund.HistoryList.FindIndex(x => x.Item1 >= startTime); int endIndex = fund.HistoryList.FindLastIndex(x => x.Item1 <= endTime) + 1; fund.ThinkStartIndex = startIndex; fund.ThinkEndIndex = endIndex; var needList = fund.HistoryList.GetRange(startIndex, endIndex - startIndex); //需要用到的历史纪录 Point[] points = GetPoints(needList); DouglasFun(ref points, 0, points.Length - 1, 0.01); //简化折线 int[] tags = new int[points.Length]; for (int i = 0; i < points.Length; i++) { tags[i] = points[i].tag; } fund.Tages = tags; //fund.NeedList = needList; //需要用到的的列表 int length = endIndex - startIndex; int[] incFlags = GetExtremePoints(needList, tags); //涨跌标识 double[] arrX = new double[length]; double[] arrY = new double[length]; for (int i = 0; i < length; i++) { arrX[i] = i; arrY[i] = needList[i].Item2; } double[] coefs = MultiLine(arrX, arrY, length, 1); fund.Coefs = coefs; //int[] incFinalFlags = ExtremePointsFiltrate(needList, incFlags); fund.IncFlags = incFlags; double[] regs = new double[length]; for (int i = 1; i < needList.Count; i++) { double valueNow = needList[i].Item2; double equation = EquationCalculate(coefs[1], coefs[0], i); regs[i] = (valueNow - equation) / equation; } List <double> regList = regs.ToList(); double μ = Mean(regList); double σ = Variance(regList); fund.μInc = μ; fund.σInc = σ; /* 遗传算法计算 * double vMax = double.MinValue; * double v1Max = 0.0; * double v2Max = 0.0; * double v3Max = 0.0; * double maxMeanWin = 0.0; * double maxVarianceWin = 0.0; * * double minMeanWin = 0.0; * double minVarianceWin = 0.0; * * double nolMeanWin = 0.0; * double nolVarianceWin = 0.0; * * for (double v1=0.0;v1<1;v1+=0.01) * { * for (double v2 = 0.0; v2 < (1-v1); v2 += 0.01) * { * double v3 = 1 - v1 - v2; * if (v3 < 0.0) continue; * //分析 * List<double> maxScores = new List<double>(); * List<double> nolScores = new List<double>(); * List<double> minScores = new List<double>(); * for (int i = 1; i < needList.Count; i++) * { * double lastValue = needList[i - 1].Item2; * double valueNow = needList[i].Item2; * double incOnce = (valueNow - lastValue); * * int lastIndex = -1; * for(int j=i-1;j>=0;j--) * { * if(incFlags[j]!=0) * { * lastIndex = j; * break; * } * } * if (lastIndex < 0) continue; * double lastFinalValue = needList[lastIndex].Item2; * double incSum = (valueNow - lastFinalValue); * * double equation = EquationCalculate(coefs[1], coefs[0], i); * double regress = (valueNow - equation) ; * * double score = GetScore(v1, v2, v3, incOnce, incSum, regress); * switch(incFlags[i]) * { * case 1: //极大值 * maxScores.Add(score); * break; * case 0: //常值 * nolScores.Add(score); * break; * case -1: //极小值 * minScores.Add(score); * break; * } * } * * //去掉偶然值 * double proportion = 0.05; * maxScores.RemoveAbnormalValue(proportion); * double maxMean = Mean(maxScores); * double maxVariance = Variance(maxScores); * * nolScores.RemoveAbnormalValue(proportion); * double nolMean = Mean(nolScores); * double nolVariance = Variance(nolScores); * * minScores.RemoveAbnormalValue(proportion); * double minMean = Mean(minScores); * double minVariance = Variance(minScores); * * if (!(maxMean > nolMean && nolMean > minMean)) * { //不符合条件 * continue; * } * * double v = (maxMean - maxVariance) - (nolMean + nolVariance) + (nolMean - nolVariance) - (minMean + nolVariance); * if (v > vMax) * { * vMax = v; * v1Max = v1; * v2Max = v2; * v3Max = v3; * * maxMeanWin = maxMean; * maxVarianceWin = maxVariance; * * minMeanWin = minMean; * minVarianceWin = minVariance; * * nolMeanWin = nolMean; * nolVarianceWin = nolVariance; * } * } * } * * fund.V1 = v1Max; * fund.V2 = v2Max; * fund.V3 = v3Max; * fund.μMax = maxMeanWin; * fund.σMax = maxVarianceWin; * fund.μNol = nolMeanWin; * fund.σNol = nolVarianceWin; * fund.μMin = minMeanWin; * fund.σMin = minVarianceWin; */ }
/// <summary> /// 收益率计算 /// </summary> /// <param name="fund"></param> /// <param name="startTime"></param> /// <param name="endTimeStart"></param> /// <param name="endTimeEnd"></param> /// <param name="chipSum"></param> /// <returns></returns> public static double RateCalculate(Fund fund, DateTime endTimeStart, DateTime endTimeEnd, out double chipSum) { double money = 100; double costSum = 0.0; //花费 double earnSum = 0.0; //收益 chipSum = 0.0; double chipSumMax = double.MinValue; double chipSumMin = double.MaxValue; double moneyMax = double.MinValue; double moneyMin = double.MaxValue; double valueNow = 0.0; for (DateTime endTime = endTimeStart; endTime < endTimeEnd; endTime = endTime.AddDays(1)) { if (!fund.HistoryDic.Keys.Contains(endTime)) { continue; } int index = fund.HistoryList.FindIndex(x => x.Item1 > endTime); if (index < 0) { break; } valueNow = fund.HistoryList[index].Item2; double chip = Predict(fund, valueNow, index - 1); if (chip == 0) { continue; //没有变动 } double cost = chip * fund.HistoryList[index].Item2; //花费 if (chip > 0) { //买入 if (money - cost < 0) { cost = money; chip = cost / fund.HistoryList[index].Item2; } money -= cost; costSum += cost; } else if (chip < 0) { //卖出 if (chipSum + chip < 0) { chip = -chipSum; } cost = chip * fund.HistoryList[index].Item2; earnSum -= cost; money -= cost; } //记录最大最小值 if (money > moneyMax) { moneyMax = money; } if (money < moneyMin) { moneyMin = money; } chipSum += chip; if (chipSum > chipSumMax) { chipSumMax = chipSum; } if (chipSum < chipSumMin) { chipSumMin = chipSum; } } double rate = ((earnSum + chipSum * valueNow) / costSum - 1) * 100; //总收益率(%) return(rate); }
public List<Fund> GetAllFund() { String HtmlText; try { HtmlText = GetWebContent("http://fund.eastmoney.com/js/fundcode_search.js?v=20130718.js", Encoding.UTF8); } catch (Exception) { return null; } HtmlText = HtmlText.Replace(",", ""); HtmlText = HtmlText.Replace("][", ""); HtmlText = HtmlText.Replace("\"\"", "\""); //对处理过后的数据进行匹配 Regex regex = new Regex("(?<=\").+?(?=\")", RegexOptions.None); MatchCollection MC = regex.Matches(HtmlText); List<String> AllFund = new List<String>(); //将匹配后的数据存在一个字符串数组中 foreach (Match ma in MC) { AllFund.Add(ma.Value); } //按四个一组读,并将读后的数据存入基金类列表中 List<Fund> Fundlist = new List<Fund>(); try { for (int n = 0; n < AllFund.Count; n = n + 4) { Fund found = new Fund(); found.Code = AllFund[n]; found.Abbr = AllFund[n + 1]; found.Name = AllFund[n + 2]; found.Type = AllFund[n + 3]; Fundlist.Add(found); } } catch (Exception e) { Console.WriteLine(e.ToString()); return null; } HtmlText = GetWebContent("http://fund.eastmoney.com/fund.html", Encoding.GetEncoding("gb2312")); int head = 0; head = HtmlText.IndexOf("type=\"checkbox\" id=", head); int foot = 0; String code; Double netValueToday, totalNetValueToday, netValueInsToday, netValueInsRateToday; while (head > 0) { head = HtmlText.IndexOf("</td><td>", head) + 1; head = HtmlText.IndexOf("</td><td>", head) + 9; foot = head + 6; code = HtmlText.Substring(head, foot - head); head = HtmlText.IndexOf("<td class=\"TD2\">", head); head += 16; foot = HtmlText.IndexOf("<", head); netValueToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head)); head = HtmlText.IndexOf("<td class=\"TD2\">", head); head += 16; foot = HtmlText.IndexOf("<", head); totalNetValueToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head)); head = HtmlText.IndexOf("<td class=", head) + 1; head = HtmlText.IndexOf("<td class=", head) + 1; head = HtmlText.IndexOf("<td class=", head) + 1; head = HtmlText.IndexOf("'>", head); head += 2; foot = HtmlText.IndexOf("<", head); netValueInsToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head)); foot += 10; head = HtmlText.IndexOf(">", foot); head += 1; foot = HtmlText.IndexOf("<", head); netValueInsRateToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head - 1)); foreach (Fund f in Fundlist) { if (f.Code == code) { f.NetValueToday = netValueToday; f.TotalNetValueToday = totalNetValueToday; f.NetValueInsToday = netValueInsToday; f.NetValueInsRateToday = netValueInsRateToday; } } head = HtmlText.IndexOf("type=\"checkbox\" id=", foot); //MessageBox.Show(HtmlText.Substring(head, 500)); } return Fundlist; }
/// <summary> /// 收益率计算 /// </summary> /// <param name="fund"></param> /// <param name="startTime"></param> /// <param name="endTimeStart"></param> /// <param name="endTimeEnd"></param> /// <returns></returns> public double YieldRate(Fund fund, DateTime startTime, DateTime endTimeStart, DateTime endTimeEnd) { //fund.CreateHistoryList(); double money = 100; double costSum = 0.0; //花费 double earnSum = 0.0; //收益 double chipSum = 0.0; double chipSumMax = double.MinValue; double chipSumMin = double.MaxValue; double moneyMax = double.MinValue; double moneyMin = double.MaxValue; double valueNow = 0.0; //Think.Calculate(startTime, DateTime.Now, fund, out needFundValues, out fundPointsFinal, out t1, out t2); DateTime endTime = DateTime.Now; for (endTime = endTimeStart; endTime < endTimeEnd; endTime = endTime.AddDays(1)) { Console.WriteLine(endTime); Think.Calculate(startTime, endTime, fund); if (!fund.HistoryDic.Keys.Contains(endTime)) { continue; } int index = fund.HistoryList.FindIndex(x => x.Item1 > endTime); if (index < 0) { break; } valueNow = fund.HistoryList[index].Item2; double chip = Think.Predict(fund, valueNow, index - 1); if (chip == 0) { continue; //没有变动 } double cost = chip * fund.HistoryList[index].Item2; //花费 if (chip > 0) { //买入 //if (money - cost < 0) //{ // cost = money; // chip = cost / fund.HistoryList[index].Item2; //} money -= cost; costSum += cost; } else if (chip < 0) { //卖出 //if (chipSum + chip < 0) chip = -chipSum; cost = chip * fund.HistoryList[index].Item2; earnSum -= cost; money -= cost; } //记录最大最小值 if (money > moneyMax) { moneyMax = money; } if (money < moneyMin) { moneyMin = money; } chipSum += chip; if (chipSum > chipSumMax) { chipSumMax = chipSum; } if (chipSum < chipSumMin) { chipSumMin = chipSum; } } double rate = ((earnSum + chipSum * valueNow) / costSum - 1) * 100; //总收益率(%) return(rate); }