티스토리 뷰

XingAPI/개발가이드

연속 데이터 조회

히처리 2018. 3. 18. 21:41

연속 조회는 Occurs 데이터의 총 갯수가 너무 많아서 한번에 다 가져오지 못할 경우 여러번에 걸쳐서 가져오는 것을 의미한다. 즉 반복조회를 이어서 다음 반복조회를 하는 것과 같다. 연속 조회를 할 수 있는 TR은 TR Layout에 연속 조회를 위한 필드가 존재한다. 보통 연속데이터와 관련된 필드의 이름은 cts_로 시작한다.

 

 

t1301의 TR Layout을 보면 cts_time이 있다.

 

연속 데이터 조회 과정

연속 데이터 조회 과정을 t1301을 이용해서 설명하겠다. (다른 TR의 연속 데이터 조회도 별 차이가 없을 것이다.)

  1. TR Query 객체를 생성 및 초기화
  2. RES 파일 등록
  3. ReceiveData 이벤트 메서드 등록
  4. .SetFieldData()를 이용해서 TR 입력값 설정
  5. .Request(false)를 이용해서 서버로 데이터 요청
  6. 서버로부터 ReceiceData()메서드가 호출되면 필요한 TR 출력값을 .GetFieldData로 데이터를 얻음
  7. .GetFieldData()로 "cts_time" 데이터를 얻음
  8. .SetFieldData()로 "ctes_time" 데이터를 설정
  9. Request(true)를 이용해서 서버로 데이터 요청
  10. 6 ~ 9 과정을 반복해서 데이터를 얻음

연속 데이터 조회

이 예제는 "반복 데이터 조회"를 이어서 프로그래밍한다

 

1. 디자인 영역에 버튼을 추가하고 아래와 같이 속성을 수정한다.

 

2. "t1301 연속 데이터 조회"버튼을 더블클릭하여 클릭이벤트 소스를 추가한다.

 

3. 아래와 같이 코드를 수정한다.

using System;
using System.Windows.Forms;
using XA_SESSIONLib;
using XA_DATASETLib;

namespace XingAPI
{
    public partial class Form1 : Form
    {
        XASessionClass session;
        XAQueryClass t1101;
        XAQueryClass t1301Occurs;

        string t1301CtsTime;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            session = new XASessionClass();
            session._IXASessionEvents_Event_Login += XASession_Login;

            t1101 = new XAQueryClass();
            t1101.ResFileName = @"C:\eBEST\xingAPI\Header\t1101.res";
            t1101.ReceiveData += t1101ReceiveData;

            t1301Occurs = new XAQueryClass();
            t1301Occurs.ResFileName = @"C:\eBEST\xingAPI\Header\t1301.res";
            t1301Occurs.ReceiveData += t1301ReceiveData;

            severTypeComboBox.SelectedIndex = 0;
        }

        private void t1101ReceiveData(string trCode)
        {
            var name = t1101.GetFieldData("t1101OutBlock", "hname", 0);
            listBox.Items.Add(name);
            var price = t1101.GetFieldData("t1101OutBlock", "price", 0);
            listBox.Items.Add(price);
        }

        private void t1301ReceiveData(string trCode)
        {
            var count = t1301Occurs.GetBlockCount("t1301OutBlock1");

            string str;
            for (int index = 0; index < count; ++index)
            {
                str = "";
                str += t1301Occurs.GetFieldData("t1301OutBlock1", "chetime", index);
                str += " ";
                str += t1301Occurs.GetFieldData("t1301OutBlock1", "price", index);
                str += " ";
                str += t1301Occurs.GetFieldData("t1301OutBlock1", "volume", index);
                listBox.Items.Add(str);
            }

            t1301CtsTime = t1301Occurs.GetFieldData("t1301OutBlock", "cts_time", 0);
            if (t1301CtsTime == "")
            {
                t1301CtsButton.Enabled = false;
            }
            else
            {
                t1301CtsButton.Enabled = true;
            }
        }

        private void XASession_Login(string code, string msg)
        {
            if (code == "0000")
            {
                listBox.Items.Add("로그인 성공");
                loginButton.Enabled = false;
                t1101Button.Enabled = true;
                t1301OccursButton.Enabled = true;
            }
            else
            {
                MessageBox.Show("로그인 실패[" + code + "]: " + msg);
            }
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            string serverType;
            if (severTypeComboBox.SelectedIndex == 0)
            {
                // 실서버
                serverType = "hts.ebestsec.co.kr";
            }
            else
            {
                // 모의투자
                serverType = "demo.ebestsec.co.kr";
            }

            bool connectResult;
            connectResult = session.ConnectServer(serverType, 20001);
            if (connectResult)
            {
                listBox.Items.Add(serverType);
                listBox.Items.Add("서버 접속 성공");
            }
            else
            {
                var errCode = session.GetLastError();
                var errMsg = session.GetErrorMessage(errCode);
                MessageBox.Show(errMsg);
                return;
            }

            var id = idTextBox.Text;
            var pwd = pwdTextBox.Text;
            var certPwd = certPwdTextBox.Text;

            bool loginResult;
            loginResult = session.Login(id, pwd, certPwd, 0, false);
            if (!loginResult)
            {
                MessageBox.Show("로그인 서버전송 실패");
                return;
            }
        }

        private void t1101Button_Click(object sender, EventArgs e)
        {
            var shcode = shcodeTextBox.Text;
            t1101.SetFieldData("t1101InBlock", "shcode", 0, shcode);

            var result = t1101.Request(false);

            if (result < 0)
            {
                listBox.Items.Add("- t1101 조회 요청 실패");
            }
        }

        private void t1301OccursButton_Click(object sender, EventArgs e)
        {
            var shcode = shcodeTextBox.Text;
            t1301Occurs.SetFieldData("t1301InBlock", "shcode", 0, shcode);

            var result = t1301Occurs.Request(false);

            if (result < 0)
            {
                listBox.Items.Add("- t1301Occurs 조회 요청 실패");
            }
        }

        private void t1301CtsButton_Click(object sender, EventArgs e)
        {
            t1301Occurs.SetFieldData("t1301InBlock", "cts_time", 0, t1301CtsTime);
            var result = t1301Occurs.Request(true);

            if (result < 0)
            {
                listBox.Items.Add("- t1301 연속 조회 요청 실패");
                t1301CtsButton.Enabled = false;
            }
        }
    }
}

 

4. F5를 눌러서 프로그램을 실행시켜서, 연속조회가 되는지 확인하자.

 

코드 설명

연속 데이터 조회를 하기 위해서는 "cts_time"값이 필요하다. 하지만 처음에는 "cts_time"값이 없으니 단일 데이터 조회를 이용해서 "cts_time" 값을 얻어야 한다.

        private void t1301OccursButton_Click(object sender, EventArgs e)
        {
            var shcode = shcodeTextBox.Text;
            t1301Occurs.SetFieldData("t1301InBlock", "shcode", 0, shcode);

            var result = t1301Occurs.Request(false);

            if (result < 0)
            {
                listBox.Items.Add("- t1301Occurs 조회 요청 실패");
            }
        }

위의 코드에서 "cts_time"이 없이 .Request(false)로 단일 데이터 조회를 하는 것을 볼 수 있다.

        private void t1301ReceiveData(string trCode)
        {
            var count = t1301Occurs.GetBlockCount("t1301OutBlock1");

            string str;
            for (int index = 0; index < count; ++index)
            {
                str = "";
                str += t1301Occurs.GetFieldData("t1301OutBlock1", "chetime", index);
                str += " ";
                str += t1301Occurs.GetFieldData("t1301OutBlock1", "price", index);
                str += " ";
                str += t1301Occurs.GetFieldData("t1301OutBlock1", "volume", index);
                listBox.Items.Add(str);
            }

            t1301CtsTime = t1301Occurs.GetFieldData("t1301OutBlock", "cts_time", 0);
            if (t1301CtsTime == "")
            {
                t1301CtsButton.Enabled = false;
            }
            else
            {
                t1301CtsButton.Enabled = true;
            }
        }

단일 데이터 조회를 요청하고 서버로 응답이 오면 t1301ReceiveData() 메서드가 실행되고, .GetFieldData()로 "cts_time"값을 얻을 수 있다. "cts_time"이 빈문자열("")이면 연속 조회할 "cts_time"이 없는 것을 의미한다. 이 정보를 이용해서 "연속 조회 버튼"의 Enable의 속성을 결정한다. 즉 연속 조회할 값이 있으면 "연속 조회 버튼"을 활성화 시킨다.

        private void t1301CtsButton_Click(object sender, EventArgs e)
        {
            t1301Occurs.SetFieldData("t1301InBlock", "cts_time", 0, t1301CtsTime);
            var result = t1301Occurs.Request(true);

            if (result < 0)
            {
                listBox.Items.Add("- t1301 연속 조회 요청 실패");
                t1301CtsButton.Enabled = false;
            }
        }

연속조회 버튼을 클릭하면 "cts_time"값을 입력값으로 설정하고 .Request()의 인자에 true로 전달하여 연속조회로 요청한다. 이 방법을 이용해서 "cts_time"값이 없을 때까지 연속 데이터를 조회할 수 있게 된다.

'XingAPI > 개발가이드' 카테고리의 다른 글

RES 파일 저장 위치에 대하여...  (0) 2018.03.18
실시간 데이터 조회  (2) 2018.03.18
반복 데이터 조회 (Occures)  (0) 2018.03.18
단일 데이터 조회  (0) 2018.03.15
로그인하기  (0) 2018.03.15
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   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
글 보관함