示例#1
0
        public static List <HistoricalStock> DowloadData(string ticker, int startDate, int endDate)
        {
            List <HistoricalStock> retval = new List <HistoricalStock>();

            using (WebClient web = new WebClient())
            {
                string row = web.DownloadString(string.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&a=00&b=01&c={1}&d=00&e=01&f={2}&g=w&ignore=.csv", ticker, startDate, endDate));

                string[] rows = row.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

                for (int i = 1; i < rows.Length - 1; i++)
                {
                    string[] cols = rows[i].Split(',');
                    //cols[1] = cols[1].Replace(".", ",");
                    //cols[2] = cols[2].Replace(".", ",");
                    //cols[3] = cols[3].Replace(".", ",");
                    cols[4] = cols[4].Replace(".", ",");
                    //cols[5] = cols[5].Replace(".", ",");
                    //cols[6] = cols[6].Replace(".", ",");
                    HistoricalStock hs = new HistoricalStock();
                    hs.Date = Convert.ToDateTime(cols[0]);
                    //hs.Open = Convert.ToDouble(cols[1]);
                    //hs.High = Convert.ToDouble(cols[2]);
                    //hs.Low = Convert.ToDouble(cols[3]);
                    hs.Close = Convert.ToDouble(cols[4]);
                    //hs.Volume = Convert.ToDouble(cols[5]);
                    //hs.AdjClose = Convert.ToDouble(cols[6]);

                    retval.Add(hs);
                }
                return(retval);
            }
        }
示例#2
0
        public void CreateReadDelete_HistoricalStockData()
        {
            var id = Guid.Parse("73F8252A-01A8-4710-97C4-A2D00108271E");
            var historicalStock = new HistoricalStock(id, "TEST", "TEST", DateTime.Now, 100M, 104M, 98M, 94.5M, 4500);

            Assert.DoesNotThrow(() => historicalStockRepository.Insert(historicalStock));

            // Verify insertion by performing a select
            var selectHistoricalStock = historicalStockRepository.GetBy(x => x.HistoricalStockId, id).Single();

            Assert.AreEqual(historicalStock.HistoricalStockId, selectHistoricalStock.HistoricalStockId);

            // Remove test data
            Assert.DoesNotThrow(() => historicalStockRepository.Delete(historicalStock));
        }
示例#3
0
        public void CreateUpdateRead_HistoricalStockData()
        {
            // Create
            var id = Guid.Parse("73F8252A-01A8-4710-97C4-A2D00108271E");
            var historicalStock = new HistoricalStock(id, "TEST", "TEST", DateTime.Now, 100M, 104M, 98M, 94.5M, 4500);

            Assert.DoesNotThrow(() => historicalStockRepository.Insert(historicalStock));

            // Update
            historicalStock.Open = 1000M;
            Assert.DoesNotThrow(() => historicalStockRepository.Update(historicalStock));

            // Read, check if updated to new value.
            var selectHistoricalStock = historicalStockRepository.GetBy(x => x.HistoricalStockId, id).Single();

            Assert.AreEqual(1000M, selectHistoricalStock.Open);

            // Delete
            Assert.DoesNotThrow(() => historicalStockRepository.Delete(historicalStock));
        }
        public void LoadHistoricalChartData(object chartIdentifier, DateTime startDate, DateTime endDate)
        {
            string ticker = chartIdentifier as string;

            int startYear  = startDate.Year;
            int startMonth = startDate.Month - 1;   // have to subtract 1 from the month to get the right data from Yahoo
            int startDay   = startDate.Day;
            int endYear    = endDate.Year;
            int endMonth   = endDate.Month - 1;     // have to subtract 1 from the month to get the right data from Yahoo
            int endDay     = endDate.Day;

            ezDateRange dateRangeRequested = new ezDateRange();
            ezDateRange dateRangeProcessed = new ezDateRange();

            List <HistoricalStock> historical = YahooHistoricalDownloader.Instance.DownloadData(ticker, startDate, endDate);

            // Start with an empty set of TradeBars.
            chartData.TradeBars.Clear();

            // Yahoo downloads the data in reverse date order (most recent dates first, then previous day, and so on). So
            // we need to reverse it when we put it into our chart data.
            for (int i = historical.Count - 1; i >= 0; i--)
            {
                HistoricalStock hs  = historical[i];
                ezBarDataPoint  bdp = new ezBarDataPoint();
                bdp.TradeDate = hs.Date;
                bdp.Open      = hs.Open;
                bdp.High      = hs.High;
                bdp.Low       = hs.Low;
                bdp.Close     = hs.Close;
                bdp.Volume    = hs.Volume;

                chartData.TradeBars.Add(bdp);
            }

            if (DataLoadComplete != null)
            {
                DataLoadComplete(this, new ezDataLoadCompleteEventArgs(zDataLoadStatus.Success, dateRangeRequested, dateRangeProcessed));
            }
        }
        // Query the stock service.
        public List <HistoricalStock> GetDataUpdatesFoOneDataSource
            (string ticker, string mostRecentDate)
        {
            DateTime _startDate = DateTime.Now.Date;
            DateTime _endDate;

            _endDate = Convert.ToDateTime(mostRecentDate);

            List <HistoricalStock> retval = new List <HistoricalStock>();

            if (_startDate.Date != _endDate.Date)
            {
                int    _startMonthTemp = _startDate.Month - 1;
                string _startMonth     = _startMonthTemp.ToString();
                string _startDay       = _startDate.Day.ToString();
                string _startYear      = _startDate.Year.ToString();

                _endDate = _endDate.AddDays(1);
                int    _endMonthTemp = _endDate.Month - 1;
                string _endMonth     = _endMonthTemp.ToString();
                string _endDay       = _endDate.Day.ToString();
                string _endYear      = _endDate.Year.ToString();

                using (WebClient web = new WebClient())
                {
                    string _inputString = "http://ichart.finance.yahoo.com/table.csv?s=" +
                                          ticker + "&d=" + _startMonth + "&e=" + _startDay + "&f=" + _startYear +
                                          "&g=d&a=" + _endMonth + "&b=" + _endDay + "&c=" + _endYear + "&ignore=.csv";

                    string data = web.DownloadString(_inputString);

                    data = data.Replace("r", "");
                    string[] rows = data.Split('\n');

                    //First row is headers so Ignore it
                    for (int i = 1; i < rows.Length; i++)
                    {
                        if (rows[i].Replace("n", "").Trim() == "")
                        {
                            continue;
                        }
                        string[]        cols = rows[i].Split(',');
                        HistoricalStock hs   = new HistoricalStock();
                        hs.Date     = Convert.ToDateTime(cols[0]);
                        hs.Open     = Convert.ToDouble(cols[1]);
                        hs.High     = Convert.ToDouble(cols[2]);
                        hs.Low      = Convert.ToDouble(cols[3]);
                        hs.Close    = Convert.ToDouble(cols[4]);
                        hs.Volume   = Convert.ToDouble(cols[5]);
                        hs.AdjClose = Convert.ToDouble(cols[6]);
                        retval.Add(hs);
                    }

                    if (retval.Count > 1)
                    {
                        if (retval[0].Date == retval[1].Date)
                        {
                            retval.RemoveAt(0);
                        }
                    }
                }
            }
            return(retval);
        }