예제 #1
0
        public BrokerErrorCode GetSnapQuote(string exchange, string stockCode, out EquitySymbolQuote quote)
        {
            quote = new EquitySymbolQuote();

            //lock (lockSingleThreadedUpstoxCall)
            {
                BrokerErrorCode errorCode     = BrokerErrorCode.Unknown;
                int             retryCount    = 0;
                int             maxRetryCount = 3;

                while (errorCode != BrokerErrorCode.Success && retryCount++ < maxRetryCount)
                {
                    try
                    {
                        var response = upstox.GetSnapQuote(exchange, stockCode);

                        string[] lines = response.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

                        for (int i = 0; i < lines.Length; i++)
                        {
                            var line = lines[i].Split(',');

                            if (line.Length < 47)
                            {
                                continue;
                            }

                            if (!string.IsNullOrEmpty(stockCode) &&
                                !line[2].Equals(stockCode, StringComparison.OrdinalIgnoreCase))
                            {
                                continue;
                            }

                            quote.ExchangeStr       = line[1];
                            quote.StockCode         = line[2];
                            quote.ClosePrice        = double.Parse(line[7]);
                            quote.ATP               = double.Parse(line[9]);
                            quote.LowerCircuitPrice = double.Parse(line[14]);
                            quote.UpperCircuitPrice = double.Parse(line[15]);
                            errorCode               = BrokerErrorCode.Success;
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace(string.Format(genericErrorLogFormat, stockCode, GeneralUtils.GetCurrentMethod(), ex.Message, ex.StackTrace));
                        Trace(string.Format(retryLogFormat, retryCount, maxRetryCount));

                        if (retryCount >= maxRetryCount)
                        {
                            break;
                        }
                    }
                }

                return(errorCode);
            }
        }
        //////////////////////////////////////////
        //////      GET EQUITY QUOTE       //////
        ////////////////////////////////////////
        // NOTE: We dont want to use IciciGetWebPageResponse here since it updates the login refresh time
        // whereas getting equity quote doesnt actually refresh contact time with server
        // it doesnt even need us to be logged in
        public BrokerErrorCode GetEquityQuote(string stockCode, out EquitySymbolQuote[] info)
        {
            BrokerErrorCode errorCode = BrokerErrorCode.Success;

            info = new EquitySymbolQuote[2];

            string quoteData  = null;
            int    retryCount = 0;

            do
            {
                quoteData = HttpHelper.GetWebPageResponse(
                    URL_ICICI_EQT_QUOTE + stockCode.ToUpper(),
                    null,
                    null,
                    mCookieContainer);
                retryCount++;
            } while (quoteData == null && retryCount < 5);

            if (string.IsNullOrEmpty(quoteData) || quoteData.IndexOf("entered is not valid") > -1)
            {
                // web problems, slow connection, server down etc.
                return(BrokerErrorCode.NullResponse);
            }

            quoteData = quoteData.Substring(quoteData.IndexOf("Best 5 Bids/Offers", 0));

            string subQuoteData = StringParser.GetStringBetween(quoteData,
                                                                0,
                                                                "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"smallfont1\">",
                                                                "</table>",
                                                                new string[] { });

            ParsedTable table = (ParsedTable)HtmlTableParser.ParseHtmlIntoTables("<table>" + subQuoteData + "</table>", true);

            DateTime result   = DateTime.Now;
            bool     bSuccess = false;

            // NSE price info
            info[0] = new EquitySymbolQuote();

            info[0].StockCode = stockCode;
            info[0].Exchange  = Exchange.NSE;
            bSuccess          = DateTime.TryParse(table[1, 4] + " " + table[2, 4], out result);
            if (bSuccess)
            {
                info[0].QuoteTime = result;
            }

            info[0].LastTradePrice     = table[1, 1].ToString().Trim();
            info[0].OpenPrice          = table[3, 1].ToString().Trim();
            info[0].HighPrice          = table[4, 1].ToString().Trim();
            info[0].LowPrice           = table[5, 1].ToString().Trim();
            info[0].PreviousClosePrice = table[6, 1].ToString().Trim();
            info[0].PercentageChange   = table[8, 1].ToString().Trim();
            info[0].VolumeTraded       = table[11, 1].ToString().Trim();

            info[0].BestBidPrice   = table[3, 4].ToString().Trim();
            info[0].BestOfferPrice = table[4, 4].ToString().Trim();
            info[0].BestBidQty     = table[5, 4].ToString().Trim();
            info[0].BestOfferQty   = table[6, 4].ToString().Trim();
            info[0].Price52WkHigh  = table[7, 4].ToString().Trim();
            info[0].Price52WkLow   = table[8, 4].ToString().Trim();


            // BSE price info
            info[1] = new EquitySymbolQuote();

            info[1].StockCode = stockCode;
            info[1].Exchange  = Exchange.BSE;
            bSuccess          = DateTime.TryParse(table[1, 5] + " " + table[2, 5], out result);
            if (bSuccess)
            {
                info[1].QuoteTime = result;
            }

            info[1].LastTradePrice     = table[1, 2].ToString();
            info[1].OpenPrice          = table[3, 2].ToString();
            info[1].HighPrice          = table[4, 2].ToString();
            info[1].LowPrice           = table[5, 2].ToString();
            info[1].PreviousClosePrice = table[6, 2].ToString();
            info[1].PercentageChange   = table[8, 2].ToString();
            info[1].VolumeTraded       = table[11, 2].ToString();

            info[1].BestBidPrice   = table[3, 5].ToString();
            info[1].BestOfferPrice = table[4, 5].ToString();
            info[1].BestBidQty     = table[5, 5].ToString();
            info[1].BestOfferQty   = table[6, 5].ToString();
            info[1].Price52WkHigh  = table[7, 5].ToString();
            info[1].Price52WkLow   = table[8, 5].ToString();

            return(errorCode);
        }