Beispiel #1
0
 private static void CreateRequestFile()
 {
     try
     {
         String            strRawRequest        = File.ReadAllText(rowRequestFilePath);
         String[]          strStockRequestInfos = strRawRequest.Split(';');
         StockRequestInfos o_StockRequestInfos  = new StockRequestInfos();
         o_StockRequestInfos.ListOfStockRequests = new List <StockRequestInfo>();
         foreach (String strTempRequest in strStockRequestInfos)
         {
             if (!String.IsNullOrEmpty(strTempRequest))
             {
                 String[]         t   = strTempRequest.Split(' ');
                 StockRequestInfo obj = new StockRequestInfo();
                 obj.Ticker = t[0].Trim();
                 obj.URL    = t[1].Trim();
                 o_StockRequestInfos.ListOfStockRequests.Add(obj);
             }
         }
         if (o_StockRequestInfos.ListOfStockRequests != null && o_StockRequestInfos.ListOfStockRequests.Count > 0)
         {
             o_StockRequestInfos.ListOfStockRequests = o_StockRequestInfos.ListOfStockRequests.OrderBy(x => x.Ticker).ToList();
             SerializationHelper.WriteXML <StockRequestInfos>(o_StockRequestInfos, requestFilePath);
         }
         else
         {
             ApplicationLog.Instance.WriteError("List of request is not created. please check raw file");
         }
     }
     catch (Exception ex)
     {
         ApplicationLog.Instance.WriteException(ex);
     }
 }
Beispiel #2
0
        private static MarketValueInfo GetTickerInfo(StockRequestInfo stockRequestInfo)
        {
            MarketValueInfo rtnVal = null;

            try
            {
                HttpWebRequest req = HttpWebRequest.Create(stockRequestInfo.URL) as HttpWebRequest;
                req.Accept    = "*/*";
                req.Host      = "nseindia.com";
                req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0";
                req.Method    = "GET";
                req.KeepAlive = true;
                String strResult = String.Empty;
                using (HttpWebResponse res = req.GetResponse() as HttpWebResponse)
                {
                    using (Stream s = res.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(s))
                        {
                            strResult = sr.ReadToEnd().ToString().Trim();
                            strResult = strResult.Substring(strResult.IndexOf("futLink"));
                            int nt = strResult.IndexOf("</div>");
                            strResult = strResult.Substring(0, nt);
                            strResult = strResult.Substring(strResult.IndexOf("[{"));
                            nt        = strResult.IndexOf("}]");
                            strResult = strResult.Substring(2, nt);
                            var jsonLikeString = strResult.Split(new String[] { "\",\"" }, StringSplitOptions.None);
                            Dictionary <String, String> dTemp = new Dictionary <string, string>();
                            foreach (String str in jsonLikeString)
                            {
                                String str1 = str.Replace("\\", String.Empty);
                                str1 = str1.Replace("\"", String.Empty);
                                dTemp.Add(str1.Split(':')[0].Trim().ToUpper(), str1.Split(':')[1].Trim());
                            }

                            if (dTemp.Count > 0)
                            {
                                rtnVal = new MarketValueInfo();
                                PropertyInfo[] props = rtnVal.GetType().GetProperties();
                                foreach (PropertyInfo prop in props)
                                {
                                    try
                                    {
                                        Object propValue = null;

                                        TypeConverter typeConverter = TypeDescriptor.GetConverter(prop.PropertyType);
                                        try
                                        {
                                            propValue = typeConverter.ConvertFrom(dTemp[prop.Name]);
                                        }
                                        catch
                                        {
                                            if (typeConverter.CanConvertTo(prop.PropertyType) && dTemp.ContainsKey(prop.Name))
                                            {
                                                propValue = typeConverter.ConvertTo(null, System.Globalization.CultureInfo.CurrentCulture, dTemp[prop.Name], prop.PropertyType);
                                            }
                                        }

                                        rtnVal.GetType().GetProperty(prop.Name).SetValue(rtnVal, propValue, null);
                                    }
                                    catch (Exception ex)
                                    {
                                        ApplicationLog.Instance.WriteException(ex);
                                    }
                                }
                                #region keeping for ref only
                                //String previousClose = dTemp["previousClose"];
                                //String open = dTemp["open"];
                                //String dayHigh = dTemp["dayHigh"];
                                //String dayLow = dTemp["dayLow"];
                                //String lastPrice = dTemp["lastPrice"];
                                //String closePrice = dTemp["closePrice"];
                                //String averagePrice = dTemp["averagePrice"];
                                //String quantityTraded = dTemp["quantityTraded"];
                                #endregion
                            }
                            else
                            {
                                throw new Exception(String.Format("Webrequest failed for symbol: {0}", stockRequestInfo.Ticker));
                            }
                        }
                    }
                }
                Console.WriteLine(String.Format("Symbol read done: '{0}'", stockRequestInfo.Ticker));
            }
            catch (Exception ex)
            {
                ApplicationLog.Instance.WriteError(String.Format("Error in ticker : '{0}'", stockRequestInfo.Ticker));
                ApplicationLog.Instance.WriteException(ex);
            }
            return(rtnVal);
        }