コード例 #1
0
        public bool TryGetFromCache(string ticker, Exchange?exchange, DateTime?from, DateTime?to, PriceInterval interval, out PriceList prices)
        {
            prices = new PriceList();
            try
            {
                string fileName = $"{ticker}";
                if (exchange.HasValue)
                {
                    fileName += $"_{exchange}";
                }

                if (from.HasValue)
                {
                    fileName += $"_{from}";
                }

                if (to.HasValue)
                {
                    fileName += $"_{to}";
                }

                fileName += $"_{interval.ToString()}.csv";

                string file = Path.Combine(FOLDER, fileName);

                if (File.Exists(file))
                {
                    string[] lines = File.ReadAllLines(file);
                    for (int i = 1; i < lines.Length; i++)
                    {
                        HistoricalPrice p = HistoricalPrice.From(lines[i]);
                        prices.Add(p);
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception)
            {
                return(false);
            }
#pragma warning restore CA1031 // Do not catch general exception types
        }
コード例 #2
0
        public bool TryGetHistoricalPrices(string ticker, Exchange?exchange, DateTime?from, DateTime?to, PriceInterval priceInterval, out PriceList prices, out string errorMessage)
        {
            double fromValue;

            if (from.HasValue)
            {
                fromValue = (from.Value - DATE_1970).TotalSeconds;
            }
            else
            {
                fromValue = (FIRST_DATE - DATE_1970).TotalSeconds;
            }

            double toValue;

            if (to.HasValue)
            {
                toValue = (to.Value - DATE_1970).TotalSeconds;
            }
            else
            {
                toValue = (DateTime.Now - DATE_1970).TotalSeconds;
            }


            string content = YahooApiCaller.GetHistoricalPrices(ticker, fromValue, toValue, priceInterval);

            string[] lines = content.Split('\n');
            prices = new PriceList();
            for (int i = 1; i < lines.Length; i++)
            {
                HistoricalPrice p = HistoricalPrice.From(lines[i]);
                prices.Add(p);
            }
            errorMessage = "ok";
            return(true);
        }