コード例 #1
0
ファイル: Biz.cs プロジェクト: kim2509/stockManager
        /// <summary>
        /// 현재가로 손절
        /// </summary>
        /// <param name="stock"></param>
        public void 매도정정요청(string inqDate, string orgOrderNo, string stockCode, string stockName, int qty, int price)
        {
            log.Info("매도정정요청 start: " + inqDate + " orderNo:" + orgOrderNo + " stockCode:" + stockCode + " stockName:" + stockName
                     + " price:" + price.ToString() + " qty:" + qty.ToString());

            if (string.IsNullOrWhiteSpace(orgOrderNo))
            {
                log.Error("매도요청 주문번호 없음!!!");
                return;
            }

            List <StockOrder> orderlist = dacStock.tbl_stock_order_주문조회(inqDate, stockCode, "매수", "요청중");

            if (orderlist.Count > 0)
            {
                log.Info("매수요청중인 주문이 존재!!!");
                return;
            }

            orderlist = dacStock.tbl_stock_order_주문조회(inqDate, stockCode, "매수", "완료");
            for (int i = 0; i < orderlist.Count; i++)
            {
                if ("추가매수".Equals(orderlist[i].OrderOption))
                {
                    DateTime updateDate = DateTime.Parse(orderlist[i].updateDate);

                    if (DateTime.Now.AddSeconds(-10) < updateDate)
                    {
                        log.Info("20초내의 추가매수완료건이 존재함");
                        return;
                    }
                }
            }

            orderlist = dacStock.tbl_stock_order_주문조회(inqDate, stockCode, "매도정정", "요청중");

            if (orderlist != null && orderlist.Count > 0)
            {
                StockOrder tmpOrder    = orderlist.OrderByDescending(x => x.Seq).FirstOrDefault();
                DateTime   createdDate = DateTime.Parse(tmpOrder.createdDate);

                if (DateTime.Now.AddMinutes(-3) < createdDate)
                {
                    log.Info("3분내의 매도정정요청이 존재함");
                    return;
                }

                if (Util.GetInt(tmpOrder.Price) <= price)
                {
                    log.Info("요청가보다 현재가가 더 크기때문에 체결안될 이유 없으므로 매도정정요청안함. 요청가 : " + tmpOrder.Price + " 현재가:" + price);
                    return;
                }

                if (!string.IsNullOrWhiteSpace(tmpOrder.orderNo))
                {
                    log.Info("매도정정 주문번호가 없음. orderSeq:" + tmpOrder.Seq);
                    return;
                }

                if (Util.GetInt(tmpOrder.ConfirmQty) > 0)
                {
                    qty = Util.GetInt(tmpOrder.Qty) - Util.GetInt(tmpOrder.ConfirmQty);
                }

                orgOrderNo = tmpOrder.orderNo;
            }

            if (!string.IsNullOrWhiteSpace(orgOrderNo))
            {
                StockOrder orgOrder = dacStock.주문번호로주문조회(inqDate, orgOrderNo);
                orgOrder.Status = "정정으로변경";
                dacStock.주문정보업데이트_byOrderSeq(orgOrder);
            }

            StockOrder order = new StockOrder();

            order.inqDate     = inqDate;
            order.stockCode   = stockCode;
            order.stockName   = stockName;
            order.Qty         = qty.ToString();
            order.Price       = price.ToString();
            order.OrderType   = "매도정정";
            order.OrderOption = "";
            order.Status      = "요청중";
            order.BuySeq      = 마지막매수주문Seq가져오기(inqDate, stockCode);
            order.orgOrderNo  = orgOrderNo;

            int orderSeq = dacStock.주식주문이력추가(order);

            log.Info("매도정정주문 orderSeq:" + orderSeq);

            // 매도정정요청
            int resultCode = OpenAPI.SendOrder("매도정정요청", orderSeq.ToString(), AccountNo, 6, order.stockCode, qty, price, "00", order.orgOrderNo);

            order.APIResult = resultCode.ToString();

            dacStock.주문정보업데이트(orderSeq.ToString(), order.APIResult, "");

            log.Info("매도정정 주문 end[" + orderSeq.ToString() + "] : " + Newtonsoft.Json.JsonConvert.SerializeObject(order));
        }