// IBroker.GetEquityOrderBook
        public BrokerErrorCode GetEquityOrderBookToday(bool newOrdersOnly,
                                                       bool bOnlyOutstandingOrders,
                                                       string stockCode,
                                                       out Dictionary <string, EquityOrderBookRecord> orders)
        {
            orders = new Dictionary <string, EquityOrderBookRecord>();
            // Login If needed
            BrokerErrorCode errorCode = CheckAndLogInIfNeeded(false);

            if (errorCode != BrokerErrorCode.Success)
            {
                return(errorCode);
            }

            string postData = "pgname=eqOrdBook&ismethodcall=0&mthname=";

            string orderBookData = IciciGetWebPageResponse(URL_ICICI_EQT_ORDERBOOK,
                                                           postData,
                                                           URL_ICICI_REFERRER,
                                                           mCookieContainer,
                                                           out errorCode);

            if (errorCode.Equals(BrokerErrorCode.Success) && !orderBookData.Contains("No matching Record"))
            {
                // Trim it
                orderBookData = orderBookData.Trim(' ', '\t', '\r', '\n');

                orderBookData = HtmlUtilities.EnsureHtmlParsable(orderBookData);

                string subOrderBookData = StringParser.GetStringBetween(orderBookData,
                                                                        0,
                                                                        "<thead>",
                                                                        "</table>",
                                                                        new string[] { });

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

                //orders = new Dictionary<string, EquityOrderBookRecord>();
                for (int i = 1; i < table.RowCount; i++)
                {
                    EquityOrderBookRecord info = new EquityOrderBookRecord();
                    info.StockCode = table[i, 3].ToString().Trim();
                    info.StockCode = StringParser.GetStringBetween(info.StockCode, 0, "GetQuote('", "'", null);
                    // If stockCode parameter is empty/null i.e. all stocks are intended
                    // and if stock specific orders are intended and current element's stockCode doesnt match then iterate to next element
                    if (!string.IsNullOrEmpty(stockCode) && !info.StockCode.Equals(stockCode, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var temp = StringParser.GetStringBetween(table[i, 0].ToString(), 0, "nowrap;\">", "<", null);
                    DateTime.TryParse(temp, out info.Date);

                    string orderRefString = table[i, 14].ToString();
                    orderRefString = StringParser.GetStringBetween(orderRefString, 0, "FML_ORD_ORDR_RFRNC=", "&", null);
                    // Trim it
                    orderRefString = orderRefString.Trim();//(' ', '\t', '\r', '\n');

                    info.OrderRefenceNumber = orderRefString;
                    string tempStr = table[i, 4].ToString().ToUpperInvariant();
                    info.Direction = (OrderDirection)Enum.Parse(typeof(OrderDirection), tempStr);
                    info.Quantity  = int.Parse(table[i, 5].ToString());

                    string price = StringParser.GetStringBetween(table[i, 3].ToString(),
                                                                 0,
                                                                 ">",
                                                                 "<",
                                                                 new string[] { "onclick", "font" });

                    /*
                     * string price = StringParser.GetStringBetween(table[i, 3].ToString(),
                     *  0,
                     *  "<font color=\"blue\">",
                     *  "</font>",
                     *  new string[] { });
                     *
                     * if (String.IsNullOrEmpty(price))
                     * {
                     *  price = StringParser.GetStringBetween(table[i, 6].ToString(),
                     *  0,
                     *  "<font color=\"black\">",
                     *  "</font>",
                     *  new string[] { });
                     * }
                     *
                     * if (String.IsNullOrEmpty(price))
                     * {
                     *  price = StringParser.GetStringBetween(table[i, 6].ToString(),
                     *  0,
                     *  "\">",
                     *  "</a>",
                     *  new string[] { "onclick"});
                     * }
                     */

                    info.Price = double.Parse(price);

                    tempStr = table[i, 6].ToString().ToUpperInvariant();


                    // Executed orders have different string format, so ignore the clutter
                    if (tempStr.Contains("EXECUTED"))
                    {
                        tempStr = "EXECUTED";
                    }
                    else
                    {
                        tempStr = tempStr.Remove(tempStr.IndexOf("&"));
                    }

                    info.Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), tempStr);

                    info.OpenQty     = int.Parse(table[i, 8].ToString());
                    info.ExecutedQty = int.Parse(table[i, 9].ToString());

                    // Only add valid outstanding orders if bOnlyOutstandingOrders is true
                    // PARTEXEC is considered OUTSTANDING (i.e. not considered EXEC until fully executed)
                    if (!bOnlyOutstandingOrders ||
                        info.Status == OrderStatus.PARTEXEC ||
                        info.Status == OrderStatus.QUEUED ||
                        info.Status == OrderStatus.REQUESTED ||
                        info.Status == OrderStatus.ORDERED)
                    {
                        lock (lockObjectEquity)
                        {
                            if (mEquityOrderBook.ContainsKey(orderRefString))
                            {
                                if (newOrdersOnly)
                                {
                                }
                                else
                                {
                                    orders.Add(orderRefString, info);
                                }
                                // Update the order
                                mEquityOrderBook[orderRefString] = info;
                            }
                            else
                            {
                                mEquityOrderBook.Add(orderRefString, info);
                                orders.Add(orderRefString, info);
                            }
                        }
                    }
                }
            }
            return(errorCode);
        }
        public BrokerErrorCode GetEquitySpread(string stockCode, out EquitySymbolSpread[] info)
        {
            BrokerErrorCode errorCode = BrokerErrorCode.Success;

            info = new EquitySymbolSpread[2];

            string quoteData  = null;
            int    retryCount = 0;

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

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

            ParsedTable table = (ParsedTable)HtmlTableParser.ParseHtmlIntoTables(quoteData, true);

            // NSE price info
            info[0]          = new EquitySymbolSpread();
            info[0].Symbol   = stockCode;
            info[0].Exchange = Exchange.NSE;
            string   tempStr       = ParsedTable.GetValue(table, new int[] { 0, 3, 0, 1 });
            DateTime lastTradeTime = DateTime.Parse(tempStr);

            tempStr           = ParsedTable.GetValue(table, new int[] { 0, 3, 1, 1 });
            lastTradeTime    += TimeSpan.Parse(tempStr);
            info[0].QuoteTime = lastTradeTime;

            info[0].TotalBidQty   = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, 7, 1 }));
            info[0].TotalOfferQty = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, 8, 1 }));

            for (int i = 0; i < 5; i++)
            {
                info[0].BestBidQty[i]   = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 0 }));
                info[0].BestBidPrice[i] = MarketUtils.GetPrice(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 1 }));

                info[0].BestOfferQty[i]   = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 2 }));
                info[0].BestOfferPrice[i] = MarketUtils.GetPrice(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 3 }));
            }

            // BSE price info
            info[1]           = new EquitySymbolSpread();
            info[1].Symbol    = stockCode;
            info[1].Exchange  = Exchange.BSE;
            tempStr           = ParsedTable.GetValue(table, new int[] { 0, 3, 0, 3 });
            lastTradeTime     = DateTime.Parse(tempStr);
            tempStr           = ParsedTable.GetValue(table, new int[] { 0, 3, 1, 3 });
            lastTradeTime    += TimeSpan.Parse(tempStr);
            info[1].QuoteTime = lastTradeTime;

            info[1].TotalBidQty   = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, 7, 3 }));
            info[1].TotalOfferQty = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, 8, 3 }));

            for (int i = 0; i < 5; i++)
            {
                info[1].BestBidQty[i]   = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 4 }));
                info[1].BestBidPrice[i] = MarketUtils.GetPrice(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 5 }));

                info[1].BestOfferQty[i]   = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 6 }));
                info[1].BestOfferPrice[i] = MarketUtils.GetPrice(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 7 }));
            }
            return(errorCode);
        }
        // IBroker.GetEquityTradeBook

        // Get trade book for a date range and for specific stock if stockCode is valid
        // else gets complete trade book fro the date range
        public BrokerErrorCode GetEquityTradeBookToday(bool newTradesOnly,
                                                       string stockCode,
                                                       out Dictionary <string, EquityTradeBookRecord> trades)
        {
            trades = new Dictionary <string, EquityTradeBookRecord>();
            // Login If needed
            BrokerErrorCode errorCode = CheckAndLogInIfNeeded(false);

            if (errorCode != BrokerErrorCode.Success)
            {
                return(errorCode);
            }

            string postData = "pgname=eqTrdBook&ismethodcall=0&mthname=";

            string tradeBookData = IciciGetWebPageResponse(URL_ICICI_EQT_TRADEBOOK,
                                                           postData,
                                                           URL_ICICI_REFERRER,
                                                           mCookieContainer,
                                                           out errorCode);

            if (errorCode.Equals(BrokerErrorCode.Success) && !tradeBookData.Contains("No matching Trades"))
            {
                // Trim it
                tradeBookData = tradeBookData.Trim(' ', '\t', '\r', '\n');

                tradeBookData = HtmlUtilities.EnsureHtmlParsable(tradeBookData);

                string subTradeBookData = StringParser.GetStringBetween(tradeBookData,
                                                                        0,
                                                                        "<thead>",
                                                                        "</table>",
                                                                        new string[] { });

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

                //trades = new Dictionary<string, EquityTradeBookRecord>();

                for (int i = 1; i < table.RowCount - 1; i++)
                {
                    EquityTradeBookRecord info = new EquityTradeBookRecord();
                    info.StockCode = table[i, 1].ToString().Trim();
                    // If stockCode parameter is not null i.e. all stocks are intended
                    // and if stock specific orders are intended and current element's stockCode doesnt match then iterate to next element
                    if (!string.IsNullOrEmpty(stockCode) && !info.StockCode.Equals(stockCode, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var temp = StringParser.GetStringBetween(table[i, 0].ToString(), 0, "nowrap;\">", "<", null);
                    DateTime.TryParse(temp, out info.Date);

                    //if (bSuccess)
                    //{
                    //    info[0].UpdateTime = result;
                    //}
                    string tempStr = table[i, 2].ToString().ToUpperInvariant().StartsWith("B") ? "BUY" : "SELL";
                    info.Direction  = (OrderDirection)Enum.Parse(typeof(OrderDirection), tempStr);
                    info.Quantity   = int.Parse(table[i, 3].ToString());
                    info.Price      = double.Parse(table[i, 4].ToString());
                    info.TradeValue = double.Parse(table[i, 5].ToString());
                    //tempStr = StringParser.GetStringBetween(table[i, 6].ToString(), 0, "\">", "</a>", null);
                    info.Brokerage        = double.Parse(table[i, 6].ToString());
                    info.SettlementNumber = StringParser.GetStringBetween(table[i, 7].ToString(), 0, "setno=", "&", null);
                    string orderRefString = StringParser.GetStringBetween(table[i, 10].ToString(), 0, "FML_ORD_ORDR_RFRNC=", "&", null);
                    info.OrderRefenceNumber = orderRefString.Trim();
                    lock (lockObjectEquity)
                    {
                        if (mEquityTradeBook.ContainsKey(info.OrderRefenceNumber))
                        {
                            if (newTradesOnly)
                            {
                            }
                            else
                            {
                                trades.Add(info.OrderRefenceNumber, info);
                            }
                            // Update the trade
                            // update required because PartExec may have become full exec
                            mEquityTradeBook[info.OrderRefenceNumber] = info;
                        }
                        else
                        {
                            mEquityTradeBook.Add(info.OrderRefenceNumber, info);
                            trades.Add(info.OrderRefenceNumber, info);
                        }
                    }
                }
            }
            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);
        }