private BrokerErrorCode RefreshEquityOrderBookToday()
        {
            DateTime EarliestValidMarketOpenDate = MarketUtils.GetMarketCurrentDate();
            Dictionary <string, EquityOrderBookRecord> orders;

            return(GetEquityOrderBookToday(false, false, null, out orders));
        }
        public static BrokerErrorCode RefreshEquityStockStateToday(IBroker broker, EquityStockTradeStats stockInfo)
        {
            DateTime EarliestValidMarketOpenDate = MarketUtils.GetMarketCurrentDate();

            if (stockInfo == null)
            {
                return(BrokerErrorCode.InValidArg);
            }

            return(stockInfo.EquityRefreshStockState(broker, EarliestValidMarketOpenDate, EarliestValidMarketOpenDate));
        }
        ////////////////////////////////////
        //////      EQUITY STOCK STATE      //////
        //////////////////////////////////


        public static BrokerErrorCode EquityRefreshStockStateToday(IBroker broker, EquityStockTradeStats stockInfo)
        {
            DateTime EarliestValidMarketOpenDate = MarketUtils.GetMarketCurrentDate();

            return(EquityRefreshStockState(broker, EarliestValidMarketOpenDate, EarliestValidMarketOpenDate, stockInfo));
        }
        private BrokerErrorCode CancelEquityOrder(string orderRef, bool refreshOrderBook)
        {
            // Login If needed
            BrokerErrorCode errorCode = CheckAndLogInIfNeeded(false);

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

            if (refreshOrderBook)
            {
                // Make sure we have the latest order book data
                errorCode = RefreshEquityOrderBookToday();
                if (errorCode != BrokerErrorCode.Success)
                {
                    return(errorCode);
                }
            }

            EquityOrderBookRecord orderInfo;

            // Search for order in the Orders dictionary
            lock (lockObjectEquity)
            {
                if (mEquityOrderBook.ContainsKey(orderRef))
                {
                    orderInfo = mEquityOrderBook[orderRef];
                }
                else
                {
                    return(BrokerErrorCode.OrderDoesNotExist);
                }
            }

            if (orderInfo.Status != OrderStatus.ORDERED &&
                orderInfo.Status != OrderStatus.PARTEXEC &&
                orderInfo.Status != OrderStatus.REQUESTED)
            {
                if (orderInfo.Status == OrderStatus.QUEUED)
                {
                    errorCode = BrokerErrorCode.OrderQueuedCannotCancel;
                }
                else if (orderInfo.Status == OrderStatus.EXECUTED)
                {
                    errorCode = BrokerErrorCode.OrderExecutedCannotCancel;
                }
                else
                {
                    errorCode = BrokerErrorCode.InValidOrderToCancel;
                }
                return(errorCode);
            }

            // Instead of getting it actually from order book
            // We simply assume current day only
            DateTime currentDate = MarketUtils.GetMarketCurrentDate();
            string   orderStatus = IciciConstants.OrderStatusString[(int)orderInfo.Status];
            string   zipCode     = orderRef.Substring(8, 2);

            string postData = "Submit1=Yes&FML_PASS=1&NicValue=&m_pipeId=N9" +
                              "&pgname=CanOrd&ismethodcall=0&mthname=" +
                              "&m_status=" + orderStatus +
                              "&m_nsestatus=C" +
                              "&m_bsestatus=C" +
                              "&FML_ORDR_REF=" + orderRef +
                                                                                                              // TODO: C for CASH , currently hardcoded. needs to be handled.
                                                                                                              // Get it in order book stock info somehow
                              "&m_ProductType=C" +
                              "&m_ordFlow=" + IciciConstants.OrderDirectionString[(int)orderInfo.Direction] + // order direction S/B
                              "&m_exchCd=" + IciciConstants.ExchangeString[(int)orderInfo.Exchange] +         // get exchange
                              "";

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

            if (errorCode.Equals(BrokerErrorCode.Success))
            {
                string cancelConfirm = "Your order has been cancelled";
                string cancelRequestSentToExchange = "Your request for order cancellation has been sent to the exchange";

                // Either cancel confirm or cancellation request sent to exchange both are succes cases
                // Success
                if (orderBookData.Contains(cancelConfirm) || orderBookData.Contains(cancelRequestSentToExchange))
                {
                    // TODO: revist if any prob
                    // Delete the orderRef from mEquityOrderBook
                    // We ideally want it to remain in equity order book in cancelled state
                    // but right now remove it rather than update
                    // because let the GetEquityOrddrBook update it as per its protocol
                    lock (lockObjectEquity)
                    {
                        mEquityOrderBook.Remove(orderRef);
                    }
                    return(BrokerErrorCode.Success);
                }
            }

            return(BrokerErrorCode.OrderCancelFailed);
        }