Exemple #1
0
        private void axKHOpenAPI1_OnReceiveTrData(object sender, AxKHOpenAPILib._DKHOpenAPIEvents_OnReceiveTrDataEvent e)
        {
            try
            {
                log.Info("axKHOpenAPI1_OnReceiveTrData start e.sScrNo:" + e.sScrNo + " e.sRQName:" + e.sRQName + " e.sTrCode:" + e.sTrCode
                         + " e.sRecordName:" + e.sRecordName + " e.sPrevNext:" + e.sPrevNext);

                if (e.sRQName == "계좌평가잔고내역요청")
                {
                    long   totalPurchase   = long.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총매입금액"));
                    long   totalEstimate   = long.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총평가금액"));
                    long   totalProfitLoss = long.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총평가손익금액"));
                    double totalProfitRate = double.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총수익률(%)"));

                    totalProfitRateLabel.Text = String.Format("{0:#,###}", totalPurchase);
                    totalEstimateLabel.Text   = String.Format("{0:#,###}", totalEstimate);
                    totalProfitLabel.Text     = String.Format("{0:#,###}", totalProfitLoss);
                    lblProfitRate.Text        = String.Format("{0:f2}", totalProfitRate);
                }
                else if (e.sRQName.Equals("예수금상세현황요청"))
                {
                    long   lBalance   = long.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "예수금"));
                    string strBalance = String.Format("{0:#,###}", lBalance);
                    MessageBox.Show(strBalance);
                }
                else if (e.sRQName.Equals("종목정보요청"))
                {
                    long   stockPrice = long.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "현재가").Trim().Replace("-", ""));
                    string stockName  = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "종목명").Trim();
                    long   upDown     = long.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "전일대비").Trim());
                    long   volume     = long.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "거래량").Trim());
                    string upDownRate = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "등락율").Trim();

                    lblCurrentPrice.Text = String.Format("{0:#,###}", stockPrice);
                    lblStockName.Text    = stockName;
                    lblDiffPrice.Text    = String.Format("{0:#,###}", upDown);
                    lblVolume.Text       = String.Format("{0:#,###}", volume);
                    if (upDown == 0)
                    {
                        lblDiffPrice.Text = "0";
                    }
                    if (volume == 0)
                    {
                        lblDiffPrice.Text = "0";
                    }
                    lblUpDownRate.Text = upDownRate + "%";

                    StockDaily stockInfo = new StockDaily();
                    stockInfo.inqDate = inqDate;

                    dacStock.insertStockDaily(stockInfo);
                }
                else if (e.sRQName.Equals("종목정보요청Job"))
                {
                    string currentPrice = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "현재가").Trim().Replace("-", "");
                    long   stockPrice   = long.Parse(string.IsNullOrEmpty(currentPrice.Trim()) ? "0" : currentPrice.Trim());
                    string stockCode    = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "종목코드").Trim();

                    if (!string.IsNullOrWhiteSpace(stockCode))
                    {
                        string stockName  = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "종목명").Trim();
                        long   upDown     = long.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "전일대비").Trim());
                        long   volume     = long.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "거래량").Trim());
                        string upDownRate = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "등락율").Trim();

                        StockDaily stockInfo = new StockDaily();
                        stockInfo.inqDate      = inqDate;
                        stockInfo.stockCode    = stockCode;
                        stockInfo.stockName    = stockName;
                        stockInfo.currentPrice = stockPrice.ToString();
                        stockInfo.diffBefore   = upDown.ToString();
                        stockInfo.traffic      = volume.ToString();
                        stockInfo.upDownRate   = upDownRate;

                        dacStock.insertStockDaily(stockInfo);
                    }
                }
                else if (e.sRQName.Equals("거래량순조회"))
                {
                    trBiz.거래량순리스트조회처리(sender, e);
                }
                else if (e.sRQName.Equals("계좌별주문체결현황요청"))
                {
                    trBiz.계좌별주문체결현황요청응답처리(inqDate, sender, e, e.sPrevNext);
                }
                else if (e.sRQName.Equals("종목신규매수") || e.sRQName.Equals("신규종목매도주문") || e.sRQName.Equals("매도취소요청"))
                {
                    string orderSeq  = e.sScrNo;
                    string orderNo   = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "주문번호").Trim();
                    string stockCode = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "종목코드").Trim();

                    log.Info(e.sRQName + " 주문번호: " + orderNo + " orderSeq:" + orderSeq + " stockCode:" + stockCode);

                    if (string.IsNullOrWhiteSpace(orderNo))
                    {
                        log.Error("주문번호가 없어 씨벌");
                    }
                    else
                    {
                        log.Info("주문번호업데이트!! orderSeq: " + orderSeq + " orderNo:" + orderNo);
                        dacStock.주문번호업데이트_bySeq(orderSeq, orderNo);
                    }
                }
                else if (e.sRQName.Equals("매도정정요청"))
                {
                    string orderSeq  = e.sScrNo;
                    string orderNo   = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "주문번호").Trim();
                    string stockCode = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "종목코드").Trim();

                    log.Info(e.sRQName + " 주문번호: " + orderNo + " orderSeq:" + orderSeq + " stockCode:" + stockCode);

                    dacStock.주문번호업데이트_bySeq(orderSeq, orderNo);
                }
                else if (e.sRQName.Equals("계좌평가현황요청"))
                {
                    int rowCount = apiManager.GetRepeatCnt(e.sTrCode, e.sRQName);

                    for (int i = 0; i < rowCount; i++)
                    {
                        StockOrder stockInfo = new StockOrder();
                        stockInfo.inqDate   = inqDate;
                        stockInfo.stockCode = apiManager.GetCommData(e.sTrCode, e.sRQName, i, "종목코드").Trim();
                        stockInfo.stockName = apiManager.GetCommData(e.sTrCode, e.sRQName, i, "종목명").Trim();

                        if (stockInfo.stockCode.StartsWith("A"))
                        {
                            stockInfo.stockCode = stockInfo.stockCode.Substring(1);
                        }

                        stockInfo.Price = apiManager.GetCommData(e.sTrCode, e.sRQName, i, "현재가").Trim();
                        if (string.IsNullOrWhiteSpace(stockInfo.Price))
                        {
                            stockInfo.Price = "0";
                        }

                        int price = int.Parse(stockInfo.Price); // - (int)(int.Parse(stockInfo.Price) * 0.02);

                        stockInfo.Qty = apiManager.GetCommData(e.sTrCode, e.sRQName, i, "보유수량").Trim();

                        if (string.IsNullOrWhiteSpace(stockInfo.Qty))
                        {
                            stockInfo.Qty = "0";
                        }

                        int qty = int.Parse(stockInfo.Qty);

                        apiManager.SendOrder("매도처리", "1234", comboBox1.SelectedItem.ToString(), 2, stockInfo.stockCode, qty, price, "00", "");

                        Thread.Sleep(500);

                        log.Info("잔고정리:" + JsonConvert.SerializeObject(stockInfo));
                    }
                }
                else if (e.sRQName.Equals("실시간미체결요청"))
                {
                    trBiz.실시간미체결요청응답처리(sender, e);
                }
                else if (e.sRQName.Equals("당일거래량순조회") || "당일거래대금순조회".Equals(e.sRQName))
                {
                    trBiz.당일거래량상위응답처리(sender, e);
                }
                else if (e.sRQName.Equals("전일거래량순조회") || "전일거래대금순조회".Equals(e.sRQName))
                {
                    trBiz.전일거래량상위응답처리(sender, e);
                }
                else if (e.sRQName.Equals("종목현재가조회"))
                {
                    trBiz.종목현재가조회응답처리(sender, e);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                MessageBox.Show(ex.Message);
            }
            finally
            {
                log.Info("axKHOpenAPI1_OnReceiveTrData end");
            }
        }
Exemple #2
0
        public void 손절완료처리및종목제외전환()
        {
            log.Info("손절완료처리및종목제외전환 start");
            // tbl_stock_myorderlist 에서 불러옴
            List <StockMyOrder> 매도정정대상 = dacStock.매도정정대상조회(inqDate);

            for (int i = 0; i < 매도정정대상.Count; i++)
            {
                log.Info("손절완료처리및종목제외전환 : " + JsonConvert.SerializeObject(매도정정대상[i]));

                if (string.IsNullOrWhiteSpace(매도정정대상[i].confirmQty) || "0".Equals(매도정정대상[i].confirmQty))
                {
                    log.Info("confirmQty is 0");
                    continue;
                }

                if (매도정정대상[i].confirmPrice == null)
                {
                    매도정정대상[i].confirmPrice = "0";
                }

                if (string.IsNullOrWhiteSpace(매도정정대상[i].confirmPrice) || "0".Equals(매도정정대상[i].confirmPrice))
                {
                    log.Info("confirmPrice is 0");
                    continue;
                }

                List <StockOrder> orderList = dacStock.tbl_stock_order_주문조회(inqDate, 매도정정대상[i].stockCode, "매도정정", "요청중");

                if (orderList == null || orderList.Count < 1)
                {
                    log.Info("해당 order 없음");
                    continue;
                }

                StockOrder order = orderList[0];

                if (매도정정대상[i].Qty == null)
                {
                    매도정정대상[i].Qty = "0";
                }
                if (string.IsNullOrWhiteSpace(order.Qty))
                {
                    order.Qty = "0";
                }

                int myOrderQty = int.Parse(매도정정대상[i].Qty);
                int orderQty   = int.Parse(order.Qty);

                if (myOrderQty != orderQty)
                {
                    log.Info("Qty 다름");
                    continue;
                }

                if (매도정정대상[i].Price == null)
                {
                    매도정정대상[i].Price = "0";
                }
                if (string.IsNullOrWhiteSpace(order.Price))
                {
                    order.Price = "0";
                }

                int myOrderPrice = int.Parse(매도정정대상[i].Price);
                int orderPrice   = int.Parse(order.Price);

                if (myOrderPrice != orderPrice)
                {
                    log.Info("Price 다름");
                    continue;
                }

                if (string.IsNullOrWhiteSpace(order.orderNo))
                {
                    log.Info("주문번호가 없어서 myorderlist 에서 주문번호 업데이트");
                    dacStock.주문번호업데이트_bySeq(order.Seq, 매도정정대상[i].orderNo);
                }

                // tbl_stock_order 업데이트
                dacStock.매도정정내역으로주문업데이트(inqDate, 매도정정대상[i].orderNo, 매도정정대상[i].stockCode,
                                        매도정정대상[i].confirmQty, 매도정정대상[i].confirmPrice, 매도정정대상[i].orgOrderNo);

                log.Info("주식상태대기로변경 stockCode: " + 매도정정대상[i].stockCode + " confirmQty:" + 매도정정대상[i].confirmQty + " confirmPrice:" + 매도정정대상[i].confirmPrice);
                // tbl_stock_target 업데이트
                dacStock.주식상태대기로변경(inqDate, 매도정정대상[i].stockCode, 매도정정대상[i].confirmQty, 매도정정대상[i].confirmPrice);

                int qty   = string.IsNullOrWhiteSpace(매도정정대상[i].confirmQty) ? 0 : int.Parse(매도정정대상[i].confirmQty);
                int price = string.IsNullOrWhiteSpace(매도정정대상[i].confirmPrice) ? 0 : int.Parse(매도정정대상[i].confirmPrice);

                if (qty * price > 0)
                {
                    TotalBalance += qty * price;

                    // 세금제외
                    TotalBalance -= (int)(qty * price * 0.0028);
                }
            }

            log.Info("손절완료처리및종목제외전환 end");
        }