예제 #1
0
        public bool TryGetFinancialData(string ticker, Exchange?exchange, out FinancialStatements financialData, out string errorMessage)
        {
            //Example
            //https://www.reuters.com/companies/api/getFetchCompanyFinancials/AAPL.OQ

            bool   ok             = false;
            string jsonResponse   = "{}";
            string internalTicker = "";

            internalTicker = $"{ticker}{Translate(exchange.Value)}";
            ok             = ReutersApiCaller.GetFinancialData(internalTicker, out jsonResponse, out errorMessage);

            //If it fails the first try, it tries with all exchanges
            if (ok == false)
            {
                internalTicker = $"{ticker}";
                ok             = ReutersApiCaller.GetFinancialData(internalTicker, out jsonResponse, out errorMessage);

                Exchange[] exchanges = (Exchange[])Enum.GetValues(typeof(Exchange));
                int        i         = 0;
                while (ok == false && i < exchanges.Length)
                {
                    Exchange ex = exchanges[i];
                    internalTicker = $"{ticker}{Translate(ex)}";
                    ok             = ReutersApiCaller.GetSummary(internalTicker, out jsonResponse, out errorMessage);
                    i++;
                }
            }

            if (ok)
            {
                dynamic rawdata = JsonConvert.DeserializeObject(jsonResponse);
                financialData = new FinancialStatements()
                {
                    IncomeStatement = new AccountingItem()
                    {
                        IsAnnual = true, Childs = new List <AccountingItem>(),
                    },
                    BalanceSheet = new AccountingItem()
                    {
                        IsAnnual = true, Childs = new List <AccountingItem>(),
                    },
                    CashFlowStatement = new AccountingItem()
                    {
                        IsAnnual = true, Childs = new List <AccountingItem>(),
                    },
                };

                AddValues(financialData.IncomeStatement.Childs, rawdata.market_data.financial_statements.income.annual);
                AddValues(financialData.BalanceSheet.Childs, rawdata.market_data.financial_statements.balance_sheet.annual);
                AddValues(financialData.CashFlowStatement.Childs, rawdata.market_data.financial_statements.cash_flow.annual);

                return(true);
            }
            else
            {
                if (exchange.HasValue)
                {
                    errorMessage = $"It can't find financial data for ticker {ticker} in exchange {exchange.Value.ToString()}.";
                }
                else
                {
                    errorMessage = $"It can't find financial data for ticker {ticker} in all markets available.";
                }
                financialData = null;
                return(false);
            }
        }
예제 #2
0
        public bool TryGetStockSummary(string ticker, Exchange?exchange, AssetClass assetType, out Stock asset, out string errorMessage)
        {
            bool   ok             = false;
            string jsonResponse   = "{}";
            string internalTicker = "";

            if (exchange.HasValue)
            {
                internalTicker = $"{ticker}{Translate(exchange.Value)}";
                ok             = ReutersApiCaller.GetSummary(internalTicker, out jsonResponse, out errorMessage);
            }

            //If it fails the first try, it tries with all exchanges
            if (ok == false)
            {
                internalTicker = $"{ticker}";
                ok             = ReutersApiCaller.GetSummary(internalTicker, out jsonResponse, out errorMessage);

                Exchange[] exchanges = (Exchange[])Enum.GetValues(typeof(Exchange));
                int        i         = 0;
                while (ok == false && i < exchanges.Length)
                {
                    Exchange ex = exchanges[i];
                    internalTicker = $"{ticker}{Translate(ex)}";
                    ok             = ReutersApiCaller.GetSummary(internalTicker, out jsonResponse, out errorMessage);
                    i++;
                }
            }

            if (ok)
            {
                #region Data transformation
                ReutersResponse summary = JsonConvert.DeserializeObject <ReutersResponse>(jsonResponse);
                Stock           s       = new Stock(ticker);
                s.CompanyName      = summary.MarketData.CompanyName;
                s.Description      = summary.MarketData.About;
                s.WebSite          = (string)summary.MarketData.Website;
                s.LastPrice        = summary.MarketData.Last.ToNullableDecimal();
                s.LastPrice_Date   = summary.MarketData.LastTime.ToDateTime("yyyy-MM-dd HH:mm:ss");
                s.StockRelatedData = new StockRelatedData();
                s.StockRelatedData.WebSite_DataSource     = $"https://www.reuters.com/companies/{internalTicker}";
                s.StockRelatedData.Sector                 = summary.MarketData.Sector;
                s.StockRelatedData.Industry               = summary.MarketData.Industry;
                s.StockRelatedData.Country                = summary.MarketData.Country;
                s.StockRelatedData.Price_FiftyTwoWeekHigh = summary.MarketData.FiftytwoWkHigh.ToNullableDouble();
                s.StockRelatedData.Price_FiftyTwoWeekLow  = summary.MarketData.FiftytwoWkLow.ToNullableDouble();
                s.StockRelatedData.Beta = summary.MarketData.Beta.ToNullableDouble();
                s.StockRelatedData.EarningsPerShare_ExcludingExtraItems_TTM = summary.MarketData.EpsExclExtraTtm.ToNullableDouble();
                s.StockRelatedData.PriceEarnings_ExcludingExtraITems_TTM    = summary.MarketData.PeExclExtraTtm.ToNullableDouble();
                s.StockRelatedData.PriceSales_Annual            = summary.MarketData.PsAnnual.ToNullableDouble();
                s.StockRelatedData.PriceSales_TTM               = summary.MarketData.PsTtm.ToNullableDouble();
                s.StockRelatedData.PriceToCashFlow_PerShare_TTM = summary.MarketData.PcfShareTtm.ToNullableDouble();
                s.StockRelatedData.PriceBook_Annual             = summary.MarketData.PbAnnual.ToNullableDouble();
                s.StockRelatedData.PriceBook_Quarterly          = summary.MarketData.PbQuarterly.ToNullableDouble();
                s.StockRelatedData.DividendYield = summary.MarketData.DividendYieldIndicatedAnnual.ToNullableDouble();
                s.StockRelatedData.LongTermDebtToEquity_Annual    = summary.MarketData.LtDebtEquityAnnual.ToNullableDouble();
                s.StockRelatedData.TotalDebtToEquity_Annual       = summary.MarketData.TotalDebtEquityAnnual.ToNullableDouble();
                s.StockRelatedData.LongTermDebtToEquity_Quarterly = summary.MarketData.LtDebtEquityQuarterly.ToNullableDouble();
                s.StockRelatedData.TotalDebtToEquity_Quarterly    = summary.MarketData.TotalDebtEquityQuarterly.ToNullableDouble();
                s.StockRelatedData.SharesOut = summary.MarketData.SharesOut.ToNullableDouble();
                s.StockRelatedData.ROE_TTM   = summary.MarketData.RoeTtm.ToNullableDouble();
                s.StockRelatedData.ROI_TTM   = summary.MarketData.RoiTtm.ToNullableDouble();
                s.StockRelatedData.NewsList  = new List <News>();
                if (summary.MarketData.SigDevs != null)
                {
                    foreach (var news in summary.MarketData.SigDevs)
                    {
                        News n = new News();
                        n.DateTime = news.LastUpdate.ToDateTime();
                        n.Title    = news.Headline;
                        n.Content  = news.Description;
                        s.StockRelatedData.NewsList.Add(n);
                    }
                }
                s.StockRelatedData.Officers = new List <Officer>();
                if (summary.MarketData.Officers != null)
                {
                    foreach (var officer in summary.MarketData.Officers)
                    {
                        Officer o = new Officer();
                        o.Name  = officer.Name;
                        o.Rank  = officer.Rank;
                        o.Title = officer.Title;
                        s.StockRelatedData.Officers.Add(o);
                    }
                }

                /*
                 * s.EarningsPerYear = summary.market_data.eps_per_year...;
                 * "eps_per_year": {
                 * "currency": "USD",
                 * "data": [
                 * {
                 * "fiscal_year": 2017,
                 * "value": 1.91,
                 * "estimate": false
                 * },
                 * {
                 * "fiscal_year": 2018,
                 * "value": 2.08,
                 * "estimate": false
                 * },
                 * {
                 * "fiscal_year": 2019,
                 * "value": 2.11,
                 * "estimate": false
                 * },
                 * {
                 * "fiscal_year": 2020,
                 * "value": 2.256,
                 * "estimate": true
                 * }
                 * ]
                 * },
                 */

                /*
                 * s.RevenuePerYear= = summary.market_data.revenue_per_year...;
                 * "revenue_per_year": {
                 * "currency": "USD",
                 * "data": [
                 * {
                 * "fiscal_year": 2017,
                 * "value": 35416,
                 * "estimate": false
                 * },
                 * {
                 * "fiscal_year": 2018,
                 * "value": 31847,
                 * "estimate": false
                 * },
                 * {
                 * "fiscal_year": 2019,
                 * "value": 37280,
                 * "estimate": false
                 * },
                 * {
                 * "fiscal_year": 2020,
                 * "value": 39016.251125,
                 * "estimate": true
                 * }
                 * ]
                 * },
                 */
                #endregion
                errorMessage = $"Ticker {internalTicker} founded.";
                asset        = s;
                return(true);
            }
            else
            {
                if (exchange.HasValue)
                {
                    errorMessage = $"It can't find asset data for ticker {ticker} in exchange {exchange.Value.ToString()}";
                }
                else
                {
                    errorMessage = $"It can't find asset data for ticker {ticker} in all markets available";
                }
                asset = null;
                return(false);
            }
        }