Example #1
0
        /*
         * [System.Runtime.InteropServices.DllImport("YuantaAPI.dll")]
         * private static extern int YuantaAPI();
         */

        //Test 버튼 클릭시
        private void textbtn_Click_1(object sender, EventArgs e)
        {
            int count = 0;

            // 일단 코스닥 종목만 돌림
            foreach (string jongCode in jongCodeList_kosdaq)
            {
                txtBTJongCode.Text = jongCode;
                // search 과정 셋팅
                m_basicTestHandler.m_strJongCode = jongCode;
                int nErr = m_iYuantaAPI.YOA_SetTRInfo("300001", "InBlock1");
                m_iYuantaAPI.YOA_SetFieldString("jang", "1", 0);
                m_iYuantaAPI.YOA_SetFieldString("jongcode", jongCode, 0);   // i로 수정함
                m_iYuantaAPI.YOA_SetFieldString("outflag", "N", 0);
                m_iYuantaAPI.YOA_SetFieldString("tsflag", "0", 0);
                int nReqID = m_iYuantaAPI.YOA_Request("300001", true, -1);
                if (CommDef.ERROR_MAX_CODE < nReqID)
                {
                    m_basicTestHandler.m_mapRequestTR[nReqID] = "300001";
                }
                // ReceiveData - 데이터를 받아야
                m_basicTestHandler.ReceiveData(nReqID);
                // auto 등록 - 가능
                m_basicTestHandler.RegistAuto();
                // ReceiveRealData - 실행해줘야 함
                m_basicTestHandler.ReceiveRealData(nReqID, "11");



                count++;
                if (count % 15 == 0)        // 5초당 15개씩 등록
                {
                    Delay(4000);
                    //Console.WriteLine(count + "번째 종목 등록-------------------" + jongCode + "-----------" + nReqID);
                    Console.WriteLine("등록 종목수: " + count);
                }
            }
            LogMessage(jongCodeList_kosdaq.Length + "개의 코스닥 종목이 auto 수신이 모두 등록되었습니다.");
            Console.WriteLine(jongCodeList_kosdaq.Length + "개의 코스닥 종목이 auto 수신이 모두 등록되었습니다.");

            while (true)
            {
                Delay(5000);
                receive_flag = false;
                foreach (KeyValuePair <string, List <string> > entry in Master종합정보)
                {
                    Console.WriteLine(entry.Key + "------------");
                    foreach (string x in entry.Value)
                    {
                        Console.WriteLine(x);
                    }
                }
                Console.WriteLine("------- ------- 등록 종목수: " + regist_count);
                Delay(1000);
                receive_flag = true;
            }
        }
        public void Search()
        {
            m_strJongCode = m_mainForm.txtBTJongCode.Text;
            m_strJongCode.Trim();
            int nErr = m_iYuantaAPI.YOA_SetTRInfo("300001", "InBlock1");

            nErr = m_iYuantaAPI.YOA_SetFieldString("jang", "1", 0);
            nErr = m_iYuantaAPI.YOA_SetFieldString("jongcode", m_strJongCode, 0);   // i로 수정함
            nErr = m_iYuantaAPI.YOA_SetFieldString("outflag", "N", 0);
            nErr = m_iYuantaAPI.YOA_SetFieldString("tsflag", "0", 0);

            int nReqID = m_iYuantaAPI.YOA_Request("300001", true, -1);

            if (CommDef.ERROR_MAX_CODE < nReqID)
            {
                m_mapRequestTR[nReqID] = "300001";
                m_mainForm.lbLog.Items.Insert(0, "[300001]주식현재가 조회를 요청하였습니다.");
            }
            //Console.WriteLine("1");
        }
        public void Order()
        {
            string strAcctPwd = m_mainForm.txtOrdAcctPwd.Text;

            strAcctPwd.Trim();
            if (0 == strAcctPwd.Length)
            {
                MessageBox.Show("계좌비밀번호를 입력해주세요.");
                m_mainForm.txtOrdAcctPwd.Focus();
                return;
            }

            string strJongCode = m_mainForm.txtOrdJongCode.Text;

            strJongCode.Trim();
            if (0 == strJongCode.Length)
            {
                MessageBox.Show("종목코드를 입력해주세요.");
                m_mainForm.txtOrdAcctPwd.Focus();
                return;
            }

            int nOrderType = m_mainForm.cbOrdType.SelectedIndex;

            int nPrice = Convert.ToInt32(m_mainForm.nudOrdPrice.Value);

            if (0 == nPrice)
            {
                if (
                    1 == nOrderType ||  // 05.시장가
                    3 == nOrderType ||  // 12.최유리지정가
                    4 == nOrderType ||  // 13.최우선지정가
                    5 == nOrderType ||  // 71.시간외종가
                    8 == nOrderType ||  // I2.시장가(IOC)
                    11 == nOrderType    // F2.시장가(FOC)
                    )
                {
                }
                else
                {
                    MessageBox.Show("가격을 입력해주세요.");
                    m_mainForm.nudOrdPrice.Focus();
                    return;
                }
            }

            int nQty = Convert.ToInt32(m_mainForm.nudOrdQty.Value);

            if (0 == nQty)
            {
                MessageBox.Show("수량을 입력해주세요.");
                m_mainForm.nudOrdQty.Focus();
                return;
            }

            string strDSOID = "";

            if (ORDER_TYPE_MESU == m_nOrderMesuMedo)
            {
                strDSOID = "100001";
            }
            else if (ORDER_TYPE_MEDO == m_nOrderMesuMedo)
            {
                strDSOID = "100002";
            }

            string strAccount = m_mainForm.cbOrdAccount.SelectedItem.ToString();

            strAccount = strAccount.Replace("-", "");

            string strMemeGubun = m_mainForm.cbOrdType.SelectedItem.ToString();
            int    nPos         = strMemeGubun.IndexOf('.');

            strMemeGubun = strMemeGubun.Remove(nPos);

            string strJumunCond = "00";

            if (1 < strMemeGubun.Length)
            {
                if ('I' == strMemeGubun[0])
                {
                    strJumunCond = "01";
                }
                else if ('F' == strMemeGubun[0])
                {
                    strJumunCond = "02";
                }
            }

            m_iYuantaAPI.YOA_SetTRInfo(strDSOID, "InBlock1");
            m_iYuantaAPI.YOA_SetFieldString("acnt_aid", strAccount, 0);
            m_iYuantaAPI.YOA_SetFieldString("gds_acnt_tp", "A01010001", 0);
            m_iYuantaAPI.YOA_SetFieldString("passwd", strAcctPwd, 0);
            m_iYuantaAPI.YOA_SetFieldString("meme_gubun", strMemeGubun, 0);
            m_iYuantaAPI.YOA_SetFieldString("jong_code", strJongCode, 0);
            m_iYuantaAPI.YOA_SetFieldString("jang_gubun", "0", 0);
            m_iYuantaAPI.YOA_SetFieldLong("order_cnt", nQty, 0);
            m_iYuantaAPI.YOA_SetFieldLong("order_price", nPrice, 0);
            m_iYuantaAPI.YOA_SetFieldString("jumun_condition", strJumunCond, 0);

            int nReqID = m_iYuantaAPI.YOA_Request(strDSOID, true, -1);

            if (ORDER_TYPE_MESU == m_nOrderMesuMedo)
            {
                m_nReqID_mesu = nReqID;
            }
            else if (ORDER_TYPE_MEDO == m_nOrderMesuMedo)
            {
                m_nReqID_medo = nReqID;
            }

            if (CommDef.ERROR_MAX_CODE < nReqID)
            {
                string strMsg = "";
                if (ORDER_TYPE_MESU == m_nOrderMesuMedo)
                {
                    m_mapRequestTR[nReqID] = "100001";
                    strMsg = "[ReqID:" + nReqID + "] [100001]현금매수 주문을 요청 하였습니다.";
                }
                else if (ORDER_TYPE_MEDO == m_nOrderMesuMedo)
                {
                    m_mapRequestTR[nReqID] = "100002";
                    strMsg = "[ReqID:" + nReqID + "] [100002]현금매도 주문을 요청 하였습니다.";
                }

                m_mainForm.LogMessage(strMsg);
            }
            else
            {
                if (ORDER_TYPE_MESU == m_nOrderMesuMedo)
                {
                    m_mainForm.LogMessage("[100001]현금매수 주문 요청중 오류가 발생하였습니다.", CommDef.YOALOG_ERROR);
                }
                else if (ORDER_TYPE_MEDO == m_nOrderMesuMedo)
                {
                    m_mainForm.LogMessage("[100002]현금매도 주문 요청중 오류가 발생하였습니다.", CommDef.YOALOG_ERROR);
                }

                int nErrorCode = m_iYuantaAPI.YOA_GetLastError();
                m_mainForm.LogMessage("[" + nErrorCode + " ]" + m_iYuantaAPI.YOA_GetErrorMessage(nErrorCode), CommDef.YOALOG_ERROR, false);
            }
        }
        public void Search()
        {
            if (0 != m_nReqID_301004)
            {
                m_iYuantaAPI.YOA_ReleaseData(m_nReqID_301004);
            }

            m_mainForm.lvConTick.Items.Clear();
            m_mainForm.lvDaily.Items.Clear();
            ListViewItem item = new ListViewItem();

            item.UseItemStyleForSubItems = false;
            item.SubItems.Add("현    재");
            item.SubItems.Add("");
            item.SubItems.Add("");
            item.SubItems.Add("");
            item.SubItems.Add("");
            item.SubItems.Add("");
            item.SubItems.Add("");
            item.SubItems.Add("");
            item.SubItems.Add("");
            m_mainForm.lvDaily.Items.Add(item);
            m_nReqID_301004 = 0;

            UnRegistAuto();

            // 종목코드란에 text를 가져와서 search함
            m_strJongCode = m_mainForm.txtJongCode.Text;
            m_strJongCode.Trim();



            m_iYuantaAPI.YOA_SetTRFieldString("300001", "InBlock1", "jang", "1", 0);
            m_iYuantaAPI.YOA_SetTRFieldString("300001", "InBlock1", "jongcode", m_strJongCode, 0);
            m_iYuantaAPI.YOA_SetTRFieldString("300001", "InBlock1", "outflag", "N", 0);
            m_iYuantaAPI.YOA_SetTRFieldString("300001", "InBlock1", "tsflag", "0", 0);

            int nResult = m_iYuantaAPI.YOA_Request("300001", true, -1);

            if (CommDef.ERROR_MAX_CODE < nResult)
            {
                m_mapRequestTR[nResult] = "300001";
                string strMsg = "[ReqID:" + nResult + "] [300001]주식현재가 조회를 요청하였습니다.";

                m_mainForm.LogMessage(strMsg);
            }
            else
            {
                string strMsg = "[300001]주식현재가 조회 요청중 오류가 발생하였습니다.";
                m_mainForm.LogMessage(strMsg, CommDef.YOALOG_ERROR);

                strMsg = "[" + nResult + "]" + m_iYuantaAPI.YOA_GetErrorMessage(nResult);
                m_mainForm.LogMessage(strMsg, CommDef.YOALOG_ERROR, false);
            }

            string strJang = m_iYuantaAPI.YOA_GetCodeInfo(CommDef.MARKET_TYPE_INTERNAL, CommDef.CODE_INFO_JANG_GUBUN, m_mainForm.txtJongCode.Text);

            m_iYuantaAPI.YOA_SetTRInfo("300002", "InBlock1");
            m_iYuantaAPI.YOA_SetFieldString("jang", strJang, 0);
            m_iYuantaAPI.YOA_SetFieldString("jongcode", m_strJongCode, 0);
            m_iYuantaAPI.YOA_SetFieldString("outflag", "N", 0);
            nResult = m_iYuantaAPI.YOA_Request("300002", true, -1);

            if (CommDef.ERROR_MAX_CODE < nResult)
            {
                m_mapRequestTR[nResult] = "300002";
                string strMsg = "[ReqID:" + nResult + "] [300002]주식현재가_호가체결 조회를 요청하였습니다.";

                m_mainForm.LogMessage(strMsg);
            }
            else
            {
                string strMsg = "[300002]주식현재가_호가체결 조회 요청중 오류가 발생하였습니다.";
                m_mainForm.LogMessage(strMsg, CommDef.YOALOG_ERROR);

                strMsg = "[" + nResult + "]" + m_iYuantaAPI.YOA_GetErrorMessage(nResult);
                m_mainForm.LogMessage(strMsg, CommDef.YOALOG_ERROR, false);
            }

            DateTime dt      = DateTime.Now;
            string   strDate = dt.ToString("yyyyMMdd");

            m_iYuantaAPI.YOA_SetTRInfo("301004", "InBlock1");
            m_iYuantaAPI.YOA_SetFieldString("jongcode", m_strJongCode, 0);
            m_iYuantaAPI.YOA_SetFieldString("janggubun", strJang, 0);
            m_iYuantaAPI.YOA_SetFieldString("searchdate", strDate, 0);
            m_iYuantaAPI.YOA_SetFieldString("daygubun", "1", 0);
            m_iYuantaAPI.YOA_SetFieldString("querygubun", "0", 0);
            m_iYuantaAPI.YOA_SetFieldString("bpquery", "0", 0);
            nResult = m_iYuantaAPI.YOA_Request("301004", false, -1);

            if (CommDef.ERROR_MAX_CODE < nResult)
            {
                m_nReqID_301004         = nResult;
                m_mapRequestTR[nResult] = "301004";
                string strMsg = "[ReqID:" + nResult + "] [301004]수정일자별주가 조회를 요청하였습니다.";

                m_mainForm.LogMessage(strMsg);
            }
            else
            {
                string strMsg = "[301004]수정일자별주가 조회 요청중 오류가 발생하였습니다.";
                m_mainForm.LogMessage(strMsg, CommDef.YOALOG_ERROR);

                strMsg = "[" + nResult + "]" + m_iYuantaAPI.YOA_GetErrorMessage(nResult);
                m_mainForm.LogMessage(strMsg, CommDef.YOALOG_ERROR, false);
            }
        }