Example #1
0
        public static List <StockSummary> GetStockHistoryFromQuandl(string symbol, DateTime startDate, DateTime endDate, string interval)
        {
            List <StockSummary> list = new List <StockSummary>();

            var fmt = "https://www.quandl.com/api/v3/datasets/WIKI/{0}.csv?auth_token={1}&start_date={2}&end_date={3}";
            // 0: stock symbol
            // auth_token 1: token
            // start_date 2: date yyyy-MM-dd
            // auth_token 3: date yyyy-MM-dd
            var token = "n5zwD2oCokRjh96x_yxV";
            var url   = string.Format(fmt, symbol, token, startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"));

            // get content
            var sb = new StringBuilder();
            var wc = new WebClient();

            IWebProxy defaultProxy = WebRequest.DefaultWebProxy;

            if (defaultProxy != null)
            {
                defaultProxy.Credentials = CredentialCache.DefaultCredentials;
                wc.Proxy = defaultProxy;
            }

            using (var sr = new StreamReader(wc.OpenRead(url)))
            {
                // skip headers
                sr.ReadLine();

                int i = 0;
                // read each line
                for (var line = sr.ReadLine(); line != null; line = sr.ReadLine())
                {
                    var items = line.Split(',');

                    var stock = new StockDetails();
                    stock.Open       = float.Parse(items[1], CultureInfo.InvariantCulture);
                    stock.Close      = float.Parse(items[4], CultureInfo.InvariantCulture);
                    stock.DayHigh    = float.Parse(items[2], CultureInfo.InvariantCulture);
                    stock.DayLow     = float.Parse(items[3], CultureInfo.InvariantCulture);
                    stock.Volume     = double.Parse(items[5], CultureInfo.InvariantCulture);
                    stock.Valid      = true;
                    stock.Symbol     = symbol;
                    stock.DateString = DateTime.Parse(items[0], CultureInfo.InvariantCulture).ToString(@"MM'/'dd'/'yyyy");

                    list.Add(stock);
                }
            }

            return(list);
        }
Example #2
0
        public static StockSummary GetStockFromBarchart(string symbol)
        {
            StockDetails stock  = new StockDetails();
            var          fmt    = "https://marketdata.websol.barchart.com/getQuote.csv?apikey={0}&symbols={1}&fields={2}&mode=I&jerq=false";
            var          token  = "a12e94cc748160856f9039fcc6f6177b";
            var          fields = "fiftyTwoWkHigh,fiftyTwoWkLow,settlement,previousSettlement,ask,askSize,bid,bidSize,avgVolume";
            // api key 0: the api key from https://www.barchart.com/ondemand
            // symbols 1:A symbol or code that identifies a financial instrument. Multiple symbols separated by a comma may be used.
            // fields 2: the fields requested
            var url = string.Format(fmt, token, symbol, fields);

            // get content
            var sb = new StringBuilder();
            var wc = new WebClient();

            IWebProxy defaultProxy = WebRequest.DefaultWebProxy;

            if (defaultProxy != null)
            {
                defaultProxy.Credentials = CredentialCache.DefaultCredentials;
                wc.Proxy = defaultProxy;
            }

            using (var sr = new StreamReader(wc.OpenRead(url)))
            {
                // skip headers
                var header  = sr.ReadLine();
                var headers = new List <string>(header.Split(','));

                var line = sr.ReadLine();
                if (line == null)
                {
                    stock.Valid = false;
                }
                else
                {
                    var items = line.Split(',').Select(item => item.Trim('"')).ToArray();
                    stock.Open     = ParseFloat(items[headers.IndexOf("open")]);
                    stock.Close    = ParseFloat(items[headers.IndexOf("close")]);
                    stock.DayHigh  = ParseFloat(items[headers.IndexOf("high")]);
                    stock.DayLow   = ParseFloat(items[headers.IndexOf("low")]);
                    stock.YearHigh = ParseFloat(items[headers.IndexOf("fiftyTwoWkHigh")]); // as requested
                    stock.YearLow  = ParseFloat(items[headers.IndexOf("fiftyTwoWkLow")]);  // as requested
                    stock.Volume   = ParseDouble(items[headers.IndexOf("volume")]);
                    //stock.PreviousVolume = ParseDouble(items[headers.IndexOf("previousVolume")]);
                    stock.Settlement         = ParseDouble(items[headers.IndexOf("settlement")]);         // as requested
                    stock.PreviousSettlement = ParseDouble(items[headers.IndexOf("previousSettlement")]); // as requested
                    stock.Symbol             = items[headers.IndexOf("symbol")];
                    stock.Name               = items[headers.IndexOf("name")];
                    stock.Change             = ParseFloat(items[headers.IndexOf("netChange")]);
                    stock.ChangePercent      = items[headers.IndexOf("percentChange")];
                    stock.Ask                = ParseFloat(items[headers.IndexOf("ask")]);      // as requested
                    stock.Bid                = ParseFloat(items[headers.IndexOf("bid")]);      // as requested
                    stock.AskSize            = ParseInt(items[headers.IndexOf("askSize")]);    // as requested
                    stock.BidSize            = ParseInt(items[headers.IndexOf("bidSize")]);    // as requested
                    stock.AverageDailyVolume = ParseLong(items[headers.IndexOf("avgVolume")]); // as requested
                    stock.LastSale           = ParseFloat(items[headers.IndexOf("lastPrice")]);
                }
            }

            stock.DateString = DateTime.Now.Date.ToString();
            return(stock);
        }