From 989b72223727d85731e5e43eff07163ced07f849 Mon Sep 17 00:00:00 2001 From: Sean Date: Thu, 6 Feb 2025 16:45:30 -0500 Subject: [PATCH] CompanyProfile --- MarketDataLib/Helper/MarketDataHelper.cs | 179 +++++++++++++++++------ 1 file changed, 133 insertions(+), 46 deletions(-) diff --git a/MarketDataLib/Helper/MarketDataHelper.cs b/MarketDataLib/Helper/MarketDataHelper.cs index 2c5553b..287dd3b 100644 --- a/MarketDataLib/Helper/MarketDataHelper.cs +++ b/MarketDataLib/Helper/MarketDataHelper.cs @@ -1542,55 +1542,74 @@ namespace MarketData.Helper if (null == companyProfile) companyProfile = GetCompanyProfileMorningStar(symbol, nyse); return companyProfile; } -// GetCompanyProfile - DataSource Yahoo Finance + + // GetCompanyProfile - DataSource Yahoo Finance public static CompanyProfile GetCompanyProfileYahoo(String symbol) { - HttpNetResponse httpNetResponse=null; + HttpNetResponse httpNetResponse = null; try { StringBuilder sb = new StringBuilder(); String strRequest; symbol = symbol.ToUpper(); - WebProxy webProxy=HttpNetRequest.GetProxy("GetCompanyProfileYahoo"); + WebProxy webProxy = HttpNetRequest.GetProxy("GetCompanyProfileYahoo"); sb.Append("http://finance.yahoo.com/q/pr?s=").Append(symbol).Append("+Profile"); strRequest = sb.ToString(); - httpNetResponse=HttpNetRequest.GetRequestNoEncoding(strRequest); - if(!httpNetResponse.Success) + httpNetResponse = HttpNetRequest.GetRequestNoEncoding(strRequest); + + if (!httpNetResponse.Success) { - MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Request:{0} failed with status {1}",httpNetResponse.Request,httpNetResponse.StatusCode)); + MDTrace.WriteLine(LogLevel.DEBUG, String.Format("Request:{0} failed with status {1}", httpNetResponse.Request, httpNetResponse.StatusCode)); return null; } byte[] streamBytes = Encoding.ASCII.GetBytes(httpNetResponse.ResponseString); - String strIndustry=Sections.LocateItem(httpNetResponse.ResponseString,"Industry",4); - String strSector=Sections.LocateItem(httpNetResponse.ResponseString,"Sector",4); - if(null==strSector)strSector=Sections.LocateItem(httpNetResponse.ResponseString,"Sector(s)",4); - if(null!=strIndustry && strIndustry.Contains(Constants.CONST_QUESTION))strIndustry=strIndustry.Replace(Constants.CONST_QUESTION," - "); - if(null!=strSector && strSector.Contains(Constants.CONST_QUESTION))strSector=strSector.Replace(Constants.CONST_QUESTION," - "); +// Locate Industry + String strIndustry = Sections.LocateItem(httpNetResponse.ResponseString, "Industry", 4); + if(strIndustry == null)strIndustry = Sections.LocateItem(httpNetResponse.ResponseString, "Industry:??", 2); - String strDescription=Sections.LocateItem(httpNetResponse.ResponseString,"Description",4); - if(null==strIndustry && null==strSector && null==strDescription)return null; +// Locate Sector + String strSector = Sections.LocateItem(httpNetResponse.ResponseString, "Sector", 4); + if (null == strSector) strSector = Sections.LocateItem(httpNetResponse.ResponseString, "Sector(s)", 4); + if(null == strSector) strSector = Sections.LocateItem(httpNetResponse.ResponseString, "Sector:??", 3); + + if (null != strIndustry && strIndustry.Contains(Constants.CONST_QUESTION)) strIndustry = strIndustry.Replace(Constants.CONST_QUESTION, " - "); + if (null != strSector && strSector.Contains(Constants.CONST_QUESTION)) strSector = strSector.Replace(Constants.CONST_QUESTION, " - "); + +// Locate Description + String strDescription = Sections.LocateItem(httpNetResponse.ResponseString, "Description", 4); + if(null == strDescription) + { + List indices = Sections.LocateAllOccurrences(httpNetResponse.ResponseString, "Description"); + if(indices.Count>0) + { + List sections = Sections.GetSections(httpNetResponse.ResponseString); + strDescription = Sections.GetFirstNonEmptyItemInSection(sections, indices[0]+1); + } + } + + if (null == strIndustry && null == strSector && null == strDescription) return null; CompanyProfile companyProfile = new CompanyProfile(); companyProfile.Symbol = symbol; companyProfile.Sector = strSector; companyProfile.Industry = strIndustry; - companyProfile.Description=strDescription; - if(null!=companyProfile.Sector)companyProfile.Sector=companyProfile.Sector.Trim(); - if(null!=companyProfile.Industry)companyProfile.Industry=companyProfile.Industry.Trim(); - if(null!=companyProfile.Description)companyProfile.Description=Utility.RemoveHtml(companyProfile.Description.Trim()); + companyProfile.Description = strDescription; + if (null != companyProfile.Sector) companyProfile.Sector = companyProfile.Sector.Trim(); + if (null != companyProfile.Industry) companyProfile.Industry = companyProfile.Industry.Trim(); + if (null != companyProfile.Description) companyProfile.Description = Utility.RemoveHtml(companyProfile.Description.Trim()); return companyProfile; - } - catch (Exception exception) + } catch (Exception exception) { - MDTrace.WriteLine(LogLevel.DEBUG,exception); + MDTrace.WriteLine(LogLevel.DEBUG, exception); return null; - } + } finally { - if(null!=httpNetResponse)httpNetResponse.Dispose(); + if (null != httpNetResponse) httpNetResponse.Dispose(); } } + public static CompanyProfile GetCompanyProfileMorningStar(String symbol,String exchange) { HttpNetResponse httpNetResponse=null; @@ -1603,7 +1622,9 @@ namespace MarketData.Helper sb.Append(String.Format("https://www.morningstar.com/stocks/{0}/{1}/quote",exchange,symbol)); strRequest = sb.ToString(); MDTrace.WriteLine(LogLevel.DEBUG,strRequest); - httpNetResponse = HttpNetRequest.GetRequestNoEncodingV4(strRequest); +// httpNetResponse = HttpNetRequest.GetRequestNoEncodingV5(strRequest, 5000, webProxy); + httpNetResponse = HttpNetRequest.GetRequestNoEncodingV5A(strRequest, 10000, webProxy); + if(!httpNetResponse.Success) { MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Request:{0} failed with status {1}",httpNetResponse.Request,httpNetResponse.StatusCode)); @@ -1624,8 +1645,17 @@ namespace MarketData.Helper found=Sections.FindInSections(sections,"Business Description",itemIndex,ref itemIndex); if(found)strDescription=sections[itemIndex+2]; + else + { + itemIndex=0; + found=Sections.FindInSections(sections,"Company Profile",itemIndex,ref itemIndex); + if(found)strDescription = Sections.GetFirstNonEmptyItemInSection(sections, itemIndex+1); + } if(null==strIndustry && null==strSector)return null; + + if(null!=strDescription && strDescription.Equals("Description Information Not Available"))return null; + strSector=strSector.Trim(); strIndustry=strIndustry.Trim(); CompanyProfile companyProfile = new CompanyProfile(); @@ -1637,9 +1667,14 @@ namespace MarketData.Helper if(null!=companyProfile.Industry)companyProfile.Industry=companyProfile.Industry.Trim(); if(null!=strDescription)companyProfile.Description=companyProfile.Description.Trim(); - if(null!=companyProfile.Industry && companyProfile.Industry.Contains(Constants.CONST_QUESTION))companyProfile.Industry=companyProfile.Industry.Replace(Constants.CONST_QUESTION," - "); - if(null!=companyProfile.Sector && companyProfile.Sector.Contains(Constants.CONST_QUESTION))companyProfile.Sector=companyProfile.Sector.Replace(Constants.CONST_QUESTION," - "); -// if(companyProfile.Sector.Equals(Constants.CONST_QUESTION) || companyProfile.Industry.Equals(Constants.CONST_QUESTION) || companyProfile.Description.Equals(Constants.CONST_QUESTION))return null; + if(null!=companyProfile.Industry && companyProfile.Industry.Contains(Constants.CONST_QUESTION)) + { + companyProfile.Industry=companyProfile.Industry.Replace(Constants.CONST_QUESTION," - "); + } + if(null!=companyProfile.Sector && companyProfile.Sector.Contains(Constants.CONST_QUESTION)) + { + companyProfile.Sector=companyProfile.Sector.Replace(Constants.CONST_QUESTION," - "); + } if((null!=companyProfile.Sector && companyProfile.Sector.Equals(Constants.CONST_QUESTION)) || (null!=companyProfile.Industry && companyProfile.Industry.Equals(Constants.CONST_QUESTION)) || (null!=companyProfile.Description && companyProfile.Description.Equals(Constants.CONST_QUESTION)))return null; @@ -2074,6 +2109,57 @@ namespace MarketData.Helper AnalystPriceTarget analystPriceTarget = GetAnalystPriceTargetMarketBeat(symbol); return analystPriceTarget; } + + //public static AnalystPriceTarget GetAnalystPriceTargetMarketBeat(String symbol) + //{ + // AnalystPriceTarget analystPriceTarget = null; + // String nyse="NYSE"; + // String nasdaq="NASDAQ"; + + // try + // { + // HtmlNodeCollection table = GetMarketBeatAnalystPriceTargetTable(symbol, nasdaq); + // if(null==table)table = GetMarketBeatAnalystPriceTargetTable(symbol, nyse); + // if(null==table)return null; + // HtmlNodeCollection headerColumns=table[0].SelectNodes(".//th"); + // HtmlNodeCollection dataColumns=table[0].SelectNodes(".//td"); + + + // analystPriceTarget=new AnalystPriceTarget(); + // analystPriceTarget.Symbol=symbol; + // analystPriceTarget.Date=DateTime.Now.Date; + // for(int index=0;index