Пример #1
0
        public BrokerErrorCode GetOrderStatus(string orderNumber,
                                              InstrumentType instrumentType,
                                              DateTime fromDate,
                                              DateTime toDate,
                                              out OrderStatus orderStatus)
        {
            BrokerErrorCode errorCode = BrokerErrorCode.Success;

            orderStatus = OrderStatus.NOTFOUND;

            if (instrumentType == InstrumentType.Share)
            {
                Dictionary <string, EquityOrderBookRecord> orders;

                errorCode = GetEquityOrderBook(fromDate,
                                               toDate,
                                               false,
                                               false,
                                               null,
                                               out orders);

                if (errorCode.Equals(BrokerErrorCode.Success))
                {
                    foreach (KeyValuePair <string, EquityOrderBookRecord> orderPair in orders)
                    {
                        EquityOrderBookRecord order = orderPair.Value;
                        if (order.OrderRefenceNumber == orderNumber)
                        {
                            orderStatus = order.Status;
                            break;
                        }
                    }
                }
            }

            FileTracing.TraceOut(!errorCode.Equals(BrokerErrorCode.Success), "GetOrderStatus:" + errorCode.ToString(), TraceType.Error);
            return(errorCode);
        }
Пример #2
0
        //EXCHANGE, TOKEN, SYMBOL, PRODUCT, ORDER_TYPE, DURATION, PRICE, TRIGGER_PRICE, QUANTITY, DISCLOSED_QUANTITY, TRANSACTION_TYPE, AVERAGE_PRICE,TRADED_QUANTITY, (13)...
        //...MESSAGE, EXCHANGE_ORDER_ID, PARENT_ORDER_ID, ORDER_ID, EXCHANGE_TIME, TIME_IN_MICRO, STATUS, IS_AMO, VALID_DATE, ORDER_REQUEST_ID   (23 total field count)
        public BrokerErrorCode GetOrderBook(bool getOnlyNewOrders, bool getOnlyOpenOrders, string stockCode, out Dictionary <string, EquityOrderBookRecord> orders)
        {
            lock (lockSingleThreadedUpstoxCall)
            {
                BrokerErrorCode errorCode = BrokerErrorCode.Unknown;
                orders = new Dictionary <string, EquityOrderBookRecord>();
                int retryCount    = 0;
                int maxRetryCount = 3;

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

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

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

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

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

                            var order = new EquityOrderBookRecord();
                            order.OrderId = line[16];
                            var status = line[19];

                            order.Status    = ParseOrderStatus(status);
                            order.Direction = line[10] == "B" ? OrderDirection.BUY : OrderDirection.SELL;
                            var milliseconds = long.Parse(line[18]) / 1000;
                            order.DateTime = ((new DateTime(1970, 1, 1)).AddMilliseconds(milliseconds)).ToLocalTime();
                            //order.DateTime = DateTimeOffset.FromUnixTimeMilliseconds(milliseconds).LocalDateTime;
                            order.Quantity        = int.Parse(line[8]);
                            order.ExecutedQty     = int.Parse(line[12]);
                            order.Price           = double.Parse(line[6]);
                            order.EquityOrderType = line[3] == "D" ? EquityOrderType.DELIVERY : EquityOrderType.MARGIN;
                            order.StockCode       = line[2];
                            order.Exchange        = line[0];

                            if (!getOnlyOpenOrders ||
                                order.Status == OrderStatus.PARTEXEC ||
                                order.Status == OrderStatus.QUEUED ||
                                order.Status == OrderStatus.REQUESTED ||
                                order.Status == OrderStatus.ORDERED)
                            {
                                lock (lockObjectEquity)
                                {
                                    if (mEquityOrderBook.ContainsKey(order.OrderId))
                                    {
                                        if (getOnlyNewOrders)
                                        {
                                        }
                                        else
                                        {
                                            orders.Add(order.OrderId, order);
                                        }
                                        // Update the order
                                        mEquityOrderBook[order.OrderId] = order;
                                    }
                                    else
                                    {
                                        mEquityOrderBook.Add(order.OrderId, order);
                                        orders.Add(order.OrderId, order);
                                    }
                                }
                            }
                        }

                        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);
            }
        }
        // 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);
        }