コード例 #1
0
ファイル: FrmMain.cs プロジェクト: xkevas24/MyStockAnalyzer
        private void btnGetDividend_Click(object sender, EventArgs e)
        {
            StringBuilder sb    = new StringBuilder();
            List <int>    years = new List <int>()
            {
                2014, 2013, 2012, 2010, 2009
            };

            txtDividendResult.Clear();
            txtDividendResult.Text += getDividendHeader(years) + "\r\n";

            var etfStocks = model.GetEtfStocksData();

            int cnt = 0;

            foreach (var etfStock in etfStocks)
            {
                var stockPrice = model.GetStockPriceData(new string[] { etfStock.StockId });
                var stockData  = model.GetStockData(new string[] { etfStock.StockId });
                Debug.WriteLine("{0}\t{1}", (++cnt).ToString("00"), etfStock.StockId);

                var     stockDividendsResult = stockHelper.GetStockDividends(etfStock.StockId);
                string  line         = "";
                decimal currentPrice = stockPrice[etfStock.StockId].OrderByDescending(stock => stock.Date).First().Close;

                line = String.Format("{0}\t{1}\t{2}\t{3}\t", etfStock.EtfId, etfStock.StockId,
                                     stockData.First(stock => stock.StockId == etfStock.StockId).StockName,
                                     currentPrice.ToString("0.00"));

                // 股利
                List <decimal> dividends = new List <decimal>();
                foreach (var year in years)
                {
                    if (stockDividendsResult.Any(stock => stock.Year == year))
                    {
                        var stockDividendData = stockDividendsResult.First(stock => stock.Year == year);
                        dividends.Add(stockDividendData.CashDividends);
                        line += stockDividendData.CashDividends.ToString("0.00") + "\t";
                    }
                    else
                    {
                        line += "0.00\t";
                    }
                }

                decimal avgDividend = dividends.Sum() / 5;
                line += String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t",
                                      avgDividend.ToString("0.00"),
                                      (avgDividend * 20).ToString("0.00"), (currentPrice - avgDividend * 20).ToString("0.00"),
                                      (avgDividend * 16).ToString("0.00"), (currentPrice - avgDividend * 16).ToString("0.00"));

                // 現金殖利率
                List <decimal> cashRates = new List <decimal>();
                foreach (var year in years)
                {
                    if (stockDividendsResult.Any(stock => stock.Year == year))
                    {
                        var stockDividendData = stockDividendsResult.First(stock => stock.Year == year);
                        cashRates.Add(stockDividendData.CashDividendsRate);
                        line += stockDividendData.CashDividendsRate.ToString("0.00") + "%\t";
                    }
                    else
                    {
                        line += "0.00%\t";
                    }
                }

                decimal avgCashRate    = cashRates.Sum() / 5;
                decimal stdAvgCashRate = cashRates.Count > 0 ? MathHelper.CaculateStdAvg(cashRates.ToArray()) : 0;
                if (stockDividendsResult.Count() == 0)
                {
                    stockDividendsResult.Add(new StockDividend());
                }
                line += String.Format("{0}%\t{1}%\t{2}%\t{3}%\t{4}\t",
                                      avgCashRate.ToString("0.00"),
                                      stdAvgCashRate.ToString("0.00"),
                                      stockDividendsResult.First().GrossMargin.ToString("0.00"),
                                      stockDividendsResult.First().ROE.ToString("0.00"),
                                      stockDividendsResult.First().EPS.ToString("0.00"));

                txtDividendResult.Text          += line + "\r\n";
                txtDividendResult.SelectionStart = txtDividendResult.Text.Length;
                Application.DoEvents();
            }

            txtDividendResult.Text += "Done";
        }