Beispiel #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");
            }
        }