티스토리 뷰

프로그래밍/C#

xingAPI 로그인, 조회

에어버스 2022. 11. 10. 13:39

COM 등록 : https://petra.tistory.com/1739
전종목 실시간등록 : 
https://petra.tistory.com/1741 

COM 을 등록했으면 이를 참조해서 프로그램 로그인 해본다.

(VisualStudio 2022) 프로젝트 파일

Stock.zip
0.18MB
4.pdf
2.17MB

 

1. 4.pdf 는 엑셀로 하지만  이를 참고해서 C# 에서 참조추가 해야한다. (eBest 홈페이지에서 xingAPI 설치하고 DevCenter 실행하면 도움말에 전체 PDF 파일 있음)

2. 클래스 뷰에서 참조추가 메뉴 클릭

3. 아래 그림처럼 eBest Xing DataSet Lib 와 eBset Xing Session Lib 를 체크해서 참조 추가한다.

4. VisualStudio 솔루션에 XA_DATASETLib 와 XA_SESSIONLib 가 추가된다.

5. using  XA_SESSIONLib, XA_SESSIONLib 추가하기
using XA_DATASETLib;
using XA_DATASETLib;추가

6. 코드에서 using XA_SESSIONLib; 와 using XA_DATASETLib; 추가 (10, 11행)
7. XASessionClass 변수를 선언하면 아래와 같이 에러가 난다.

8.솔루션 탐색기에서 참조 추가한 XA_SESSIONLib 속성을 메뉴를 클릭

9. Interop 형식 포함 기본값인 True 를 False 로 바꾸면 에러가 사라진다.

10. XA_DATASETLib 도 Interop 형신포함을 False 로 만든다.

11. Form_Login.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Windows.Forms;
using XA_SESSIONLib;
using XA_DATASETLib;
 
namespace Stock
{
    public partial class From_Login : Form
    {
        Form1 f;
        bool bRealServer;
        string strID, strPWD, strCertPWD;
        XASessionClass myXASessionClass;
 
        
 
        public From_Login()
        {
            InitializeComponent();
        }
        public From_Login(Form1 form1)
        {
            f = form1;
        }
 
        private void button_Login_Click(object sender, EventArgs e)
        {
            strID = textBox_ID.Text;
            strPWD = textBox_PWD.Text;
            strCertPWD = textBox_CertPWD.Text;
            Login();
        }
 
        private void button_Cancle_Click(object sender, EventArgs e)
        {
           // f.Close();
        }
 
        private void radioButton_Real_CheckedChanged(object sender, EventArgs e)
        {
            textBox_CertPWD.Enabled = true;
            bRealServer = true;
        }
 
        private void radioButton_Virtual_CheckedChanged(object sender, EventArgs e)
        {
            textBox_CertPWD.Enabled = false;
            bRealServer = false;
        }
 
        private void From_Login_Load(object sender, EventArgs e)
        {
            bRealServer = false;
            radioButton_Virtual.Checked = !bRealServer;
            textBox_CertPWD.Enabled = false;
        }
 
        public void Login()
        {
            myXASessionClass = new XASessionClass();
            myXASessionClass._IXASessionEvents_Event_Login += new _IXASessionEvents_LoginEventHandler(Event_Login);
            
            string strServerType;
 
            if (true == bRealServer)
                strServerType = "hts.ebestsec.co.kr";
            else
                strServerType = "demo.ebestsec.co.kr";
 
            if (false == myXASessionClass.ConnectServer(strServerType, 20001))
            {
                int ErrCode = myXASessionClass.GetLastError();
                string ErrMsg = myXASessionClass.GetErrorMessage(ErrCode);
                MessageBox.Show(ErrMsg);
                return;
            }
            if(false == ((IXASession)myXASessionClass).Login(strID, strPWD, strCertPWD, 0false))
            {
                int ErrCode = myXASessionClass.GetLastError();
                string ErrMsg = myXASessionClass.GetErrorMessage(ErrCode);
                MessageBox.Show(ErrMsg);
            }            
        }
        
        private void Event_Login(string szCode, string szMsg)
        {
            //throw new NotImplementedException();
 
            if ("0000" == szCode)
            {
                //MessageBox.Show("로그인 성공");
                Close();
            }
            else
            {
                MessageBox.Show("[" + szCode + "] " + szMsg);
            }
        }
    }
}
 
cs

 

60행 : myXASessionClass = new XASessionClass();
61행 : myXASessionClass._IXASessionEvents_Event_Login +=new _IXASessionEvents_LoginEventHandler(Event_Login);
XASessionClass 는 XA_SESSIONLib 에 있으며 3개의 이벤트를 가지며 로그인 이벤트인 _IXASessionEvents_Event_Login 에 Event_Login 이벤트 처리기를 등록한다.
70행 :
if (false== myXASessionClass.ConnectServer(strServerType, 20001))
이베스트증권 API 서버에 연결한다.
77행 : if(false== ((IXASession)myXASessionClass).Login(strID, strPWD, strCertPWD, 0false))
API 에 로그인 한다.
85행 : privatevoid Event_Login(string szCode, string szMsg)
89행 : if ("0000"== szCode)
로그인이 성공하면 "0000" 문자열을 받는다.

12. Form.cs 코드 - 탭 안에 ListBox 추가

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Windows.Forms;
 
using Stock;
using XA_DATASETLib;
 
namespace Stock
{
    public partial class Form1 : Form
    {
        XAQueryClass t0424; // 잔고조회
        XARealClass nws;
 
        From_Login f = new From_Login();
        public Form1()
        {
            f.ShowDialog();    
            InitializeComponent();            
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        
        private void button_Request_Click(object sender, EventArgs e)
        {
            t0424 = new XAQueryClass();
            t0424.ResFileName = @"Res\t0424.res"//t0424.ResFileName = @"C:\eBest\xingAPI\Rest\t0424.res";
            t0424.ReceiveData += t0424ReceiveData;
 
            t0424.SetFieldData("t0424InBlock""accno"0"0-----"); // 계좌번호
            t0424.SetFieldData("t0424InBlock""passwd"0"------"); // 계좌암호
            t0424.SetFieldData("t0424InBlock""prcgb"0"1");
            t0424.SetFieldData("t0424InBlock""chegb"0"2");
            t0424.SetFieldData("t0424InBlock""dangb"0"0");
            t0424.SetFieldData("t0424InBlock""charge"0"0");
            t0424.SetFieldData("t0424InBlock""cts_expcode"0" ");
            int result = t0424.Request(false);
            if (0 > result)
            {
                string str = string.Format("[보유계좌] 계좌 잔고리스트(t0424) 요청이 실패했습니다. result={0}", result);
                MessageBox.Show(str);
            }
            else
                textBox_Msg.Text = "t0424요청 성공";
 
            listBox_Request.Items.Clear();
        }
 
        private void t0424ReceiveData(string szTrCode)
        {
            //throw new NotImplementedException();
 
            int count = t0424.GetBlockCount("t0424OutBlock1");
            for (int i = 0; i < count; i++)
            {
                var temp_value = t0424.GetFieldData("t0424OutBlock1""expcode", i);
                string expcode = Convert.ToString(temp_value);
                temp_value = t0424.GetFieldData("t0424OutBlock1""hname", i);
                string hname = Convert.ToString(temp_value);
                temp_value = t0424.GetFieldData("t0424OutBlock1""price", i);
                Decimal price = Convert.ToDecimal(temp_value);
                temp_value = t0424.GetFieldData("t0424OutBlock1""janqty", i);
                Decimal janqty = Convert.ToDecimal(temp_value);
                string str = string.Format("{0} {1} {2} {3}", expcode, hname, price, janqty);
                listBox_Request.Items.Add(str);
            }
        }
 
        private void button_Real_Click(object sender, EventArgs e)
        {
            nws = new XARealClass(); // 실시간 뉴스
 
            nws.ResFileName = @"Res\NWS.res"//t0424.ResFileName = @"C:\eBest\xingAPI\Rest\t0424.res";
            nws.ReceiveRealData += dha_ReceiveRealData;
 
            nws.SetFieldData("InBlock""nwcode""NWS001");
            nws.AdviseRealData();
 
            //listBox_Real.Items.Clear();
 
            textBox_Msg.Text = "NWS 요청성공";
        }
 
        private void dha_ReceiveRealData(string szTrCode)
        {
            //throw new NotImplementedException();
            var temp_value = nws.GetFieldData("OutBlock""date");
            string date = Convert.ToString(temp_value);
            temp_value = nws.GetFieldData("OutBlock""time");
            string time = Convert.ToString(temp_value);
            //temp_value = nws.GetFieldData("OutBlock", "id");
            //string id = Convert.ToString(temp_value);
            //temp_value = nws.GetFieldData("OutBlock", "realkey");
            //string realkey = Convert.ToString(temp_value);
            temp_value = nws.GetFieldData("OutBlock""title");
            string title = Convert.ToString(temp_value);
            //temp_value = nws.GetFieldData("OutBlock", "code");
            //string code = Convert.ToString(temp_value);
            //temp_value = nws.GetFieldData("OutBlock", "bodysize");
            //if (null == temp_value) return;
            //Decimal bodysize = Convert.ToDecimal(temp_value);
            //string str = string.Format("{0} {1} {2} {3} {4} {5]", date, time, id, realkey, code, bodysize);
            string str = string.Format("{0} {1} {2}", date, time, title);
            listBox_Real.Items.Add(str);
        }
    }    
}
 
cs

28행 :  t0424 = new XAQueryClass();
TR 조회를 위해 XAQueryClass 를 사용하한다.
29행 : t0424.ResFileName = @"Res\t0424.res";
TR의 InBlock 과 OutBlock 구조를 갖는 Res 파일을 지정한다.
30행 : t0424.ReceiveData += t0424ReceiveData;

개체 브라우저에서 XAQueryClass 는 XA_DATASETLib 안에 있고, XAQueryClass 는 아벤트 4개 중 조회 데이터를 받는 이벤트는 ReceiveData 이므로 이곳에 t0424ReceiveData 이벤트 처리기를 등록한다.
51행 : private void t0424ReceiveData(string szTrCode)
이벤트 처리기 함수를 정의해서 t0424 조회 결과를 받아 처리한다.
32~38행 : t0424.SetFieldData("t0424InBlock""accno"0"0-----"); // 계좌번호 
DevCenter 에서 TR t0424 inBlock 또는 t0424.res 파일에서 구조를 참고해서 추가한다.
39행 : int result = t0424.Request(false);
TR t0424 조회요청하고 조회요청이 성공하면 조회 결과 데이타는 30행에서 등록한 이벤트 처리기 51행 t0424ReceiveData 로 받게 된다.

13. 로그인 후 잔고 조회와 실시간뉴스 요청 후 실행화면 - 종료할때 로그아웃과 연결 끊기 코드는 없음. 이베스트에서 API 종료하면 자동으로 연결 끊긴다고 함.




 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31