예제 #1
0
        private static string Translate(PriceInterval priceInterval)
        {
            switch (priceInterval)
            {
            case PriceInterval.Daily: return("1d");

            case PriceInterval.Monthly: return("1mo");

            default:
                throw new NotImplementedException($"There is no interval configured for value={priceInterval.ToString()}");
            }
        }
 public bool TryGetHistoricalPrices(string ticker, Exchange?exchange, DateTime?from, DateTime?to, PriceInterval interval, out PriceList prices, out string errorMessage)
 {
     if (cacheManager.TryGetFromCache(ticker, exchange, from, to, interval, out prices))
     {
         errorMessage = "Obtained from cache";
         return(true);
     }
     return(pricesDataSouce.TryGetHistoricalPrices(ticker, exchange, from, to, interval, out prices, out errorMessage));
 }
예제 #3
0
        internal static bool GetPrices(string ticker, Exchange?exchange, DateTime from, DateTime to, PriceInterval interval, out HttpStatusCode statusCode, out NasdaqResponse nasdaqResponse, out string jsonResponse, out string message)
        {
            //https://api.nasdaq.com/api/quote/AAPL/chart?assetclass=stocks&fromdate=2010-04-15&todate=2020-04-15
            string fromDate = from.ToString("yyyy-MM-dd");
            string toDate   = to.ToString("yyyy-MM-dd");
            string uri      = $"{httpClient.BaseAddress}quote/{ticker}/chart?assetclass=stocks&fromdate={fromDate}5&todate={toDate}";
            HttpResponseMessage responseMessage = httpClient.GetAsync(uri).Result;
            string originalContent = responseMessage.Content.ReadAsStringAsync().Result;
            string content         = originalContent;

            statusCode = responseMessage.StatusCode;
            if (responseMessage.StatusCode == HttpStatusCode.OK)
            {
                jsonResponse   = content;
                nasdaqResponse = JsonConvert.DeserializeObject <NasdaqResponse>(jsonResponse);
                message        = "OK";
                return(true);
            }
            else
            {
                dynamic error = new
                {
                    HttpStatusCode  = responseMessage.StatusCode.ToString(),
                    ReasonPhrase    = responseMessage.ReasonPhrase,
                    ContentResponse = content,
                };
                jsonResponse   = JsonConvert.SerializeObject(error);
                nasdaqResponse = null;
                message        = responseMessage.ReasonPhrase;
                return(false);
            }
        }
예제 #4
0
        public bool TryGetHistoricalPrices(string ticker, Exchange?exchange, DateTime?from, DateTime?to, PriceInterval interval, out PriceList prices, out string errorMessage)
        {
            //https://api.nasdaq.com/api/quote/AAPL/chart?assetclass=stocks&fromdate=2010-04-15&todate=2020-04-15
            if (from.HasValue == false)
            {
                from = DateTime.Now;
            }

            if (to.HasValue == false)
            {
                to = DateTime.Now;
            }

            bool ok = NasdaqApiCaller.GetPrices(ticker, exchange, from.Value, to.Value, interval, out HttpStatusCode statusCode, out NasdaqResponse nasdaqResponse, out string jsonResponse, out errorMessage);

            prices = new PriceList();
            foreach (var nasdaqPrice in nasdaqResponse.Data.Prices)
            {
                HistoricalPrice price = new HistoricalPrice();
                price.Close = nasdaqPrice.Price;
                prices.Add(price);
            }
            return(true);
        }
예제 #5
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);
        }
예제 #6
0
        internal static string GetHistoricalPrices(string ticker, double from, double to, PriceInterval priceInterval)
        {
            //daily
            //https://query1.finance.yahoo.com/v7/finance/download/%5EGSPC?period1=-1325635200&period2=1584144000&interval=1d&events=history

            //monthly
            //https://query1.finance.yahoo.com/v7/finance/download/%5EGSPC?period1=-1325635200&period2=1584144000&interval=1mo&events=history

            string interval   = Translate(priceInterval);
            string requestUrl = $"{httpClientHistoricalPrices.BaseAddress}/{ticker}?period1={(long)from}&period2={(long)to}&interval={interval}&events=history";

            //var content = new KeyValuePair<string, string>[] {
            //    };
            //var formUrlEncodedContent = new FormUrlEncodedContent(content);

            using (var request = new HttpRequestMessage(HttpMethod.Get, requestUrl))
            {
                var sendTask   = httpClientHistoricalPrices.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
                var response   = sendTask.Result.EnsureSuccessStatusCode();
                var httpStream = response.Content.ReadAsStreamAsync().Result;


                using (MemoryStream ms = new MemoryStream())
                {
                    httpStream.CopyTo(ms);
                    return(Encoding.UTF8.GetString(ms.ToArray()));
                }
            }
        }
 public bool TryGetHistoricalPrices(string ticker, Exchange?exchange, DateTime?from, DateTime?to, PriceInterval interval, out PriceList prices, out string errorMessage)
 {
     throw new NotImplementedException();
 }
예제 #8
0
        public static APIResponse <PriceList> GetPrices(string ticker, Exchange?exchange, DateTime?from, DateTime?to, PriceInterval interval)
        {
            string uri = $"api/DataSources/getprices?ticker={ticker}";

            if (exchange.HasValue)
            {
                uri += $"&market={exchange.ToString()}";
            }

            if (from.HasValue)
            {
                uri += $"&from={from.Value.ToString("")}";
            }

            if (to.HasValue)
            {
                uri += $"&to={to.Value.ToString("")}";
            }

            uri += $"&interval={interval.ToString("")}";
            HttpStatusCode statusCode = HttpClientWebAPI.Get(uri, out string jsonResponse);

            return(JsonConvert.DeserializeObject <APIResponse <PriceList> >(jsonResponse));
        }
예제 #9
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
        }