Beispiel #1
0
        public bool TryGetOptionsChain(string ticker, Exchange?exchange, out OptionsChain optionChain, out string errorMessage)
        {
            DateTime from = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime to   = new DateTime(DateTime.Now.Year, DateTime.Now.Month, GetLastDay(DateTime.Now.Month)).AddYears(3);
            bool     ok   = NasdaqApiCaller.GetOptionChain(ticker, from, to, out string jsonResponse, out errorMessage);

            optionChain = new OptionsChain();
            NasdaqResponse nasdaqResponse = JsonConvert.DeserializeObject <NasdaqResponse>(jsonResponse);

            foreach (var optionRow in nasdaqResponse.Data.OptionChainList.Rows)
            {
                if (optionRow.Call != null)
                {
                    var    call = optionRow.Call;
                    Option o    = new Option(OptionClass.Call, call.Symbol)
                    {
                        LastPrice      = call.Last.ToNullableDecimal(),
                        Strike         = call.Strike.ToDouble(-1),
                        ExpirationDate = call.ExpiryDate.ToDateTime(),
                    };

                    o.LastPrice    = call.Last.ToNullableDecimal();
                    o.Change       = call.Change.ToNullableDouble();
                    o.Bid          = call.Bid.ToNullableDouble();
                    o.Ask          = call.Ask.ToNullableDouble();
                    o.Volume       = call.Volume.ToNullableInt();
                    o.OpenInterest = call.Openinterest.ToNullableInt();

                    optionChain.Add(o);
                }

                if (optionRow.Put != null)
                {
                    var    put = optionRow.Put;
                    Option o   = new Option(OptionClass.Put, put.Symbol)
                    {
                        LastPrice      = put.Last.ToNullableDecimal(),
                        Strike         = put.Strike.ToDouble(-1),
                        ExpirationDate = put.ExpiryDate.ToDateTime(),
                    };
                    o.LastPrice    = put.Last.ToNullableDecimal();
                    o.Change       = put.Change.ToNullableDouble();
                    o.Bid          = put.Bid.ToNullableDouble();
                    o.Ask          = put.Ask.ToNullableDouble();
                    o.Volume       = put.Volume.ToNullableInt();
                    o.OpenInterest = put.Openinterest.ToNullableInt();

                    optionChain.Add(o);
                }
            }

            errorMessage = "ok";
            return(true);
        }
        internal static bool GetStockSummary(string ticker, Exchange?exchange, AssetClass assetType, out HttpStatusCode statusCode, out NasdaqResponse nasdaqResponse, out string jsonResponse, out string message)
        {
            //https://api.nasdaq.com/api/quote/TQQQ/info?assetclass=etf
            //https://api.nasdaq.com/api/quote/AAPL/info?assetclass=stocks
            string uri = $"{httpClient.BaseAddress}quote/{ticker}/info";

            if (assetType == AssetClass.Stock)
            {
                uri += "?assetclass=stocks";
            }
            else if (assetType == AssetClass.ETF)
            {
                uri += "?assetclass=etf";
            }
            HttpResponseMessage responseMessage = httpClient.GetAsync(uri).Result;
            string content = responseMessage.Content.ReadAsStringAsync().Result;

            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);
            }
        }
        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);
            }
        }