본문 바로가기

프로그래밍/한국투자증권API

한국투자증권 API 모바일앱 - 실시간 정보 조회 예제코드

반응형

한국투자증권 API 모바일앱 - 실시간 정보 조회 예제코드

package kr.co.enigmah2k.bapitest

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.commexpert.CommExpertMng
import com.commexpert.ExpertRealProc
import com.truefriend.corelib.commexpert.intrf.IExpertInitListener
import com.truefriend.corelib.commexpert.intrf.IExpertLoginListener
import com.truefriend.corelib.commexpert.intrf.IRealDataListener
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity(), IExpertInitListener, IExpertLoginListener, IRealDataListener {

    var m_PriceRealProc: ExpertRealProc? = null //현재가 체결실시간

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /** * ExpertMng 기본 셋팅 */
// Activity 셋팅
        CommExpertMng.InitActivity(this);
// 초기화 및 통신 접속
        CommExpertMng.InitCommExpert(this);
// Listener 셋팅
        CommExpertMng.getInstance().SetInitListener(this@MainActivity);
        CommExpertMng.getInstance().SetLoginListener(this@MainActivity);
// "0"리얼 , "1" 개발
        CommExpertMng.getInstance().SetDevSetting("0");

        //실시간 초기화
        m_PriceRealProc = ExpertRealProc(this@MainActivity)
        m_PriceRealProc!!.InitInstance(this@MainActivity);
        m_PriceRealProc!!.SetShowTrLog(true);

        bLogin.setOnClickListener(){
            //로그인 시작
            CommExpertMng.getInstance().StartLogin ( "회원ID", "회원PW", "공인인증서PW" );
        }

        bRun.setOnClickListener(){
            var trName = eTRName.text.toString()
            var stockCode = eStockCode.text.toString()
            tResult.setText("bRun" + trName + stockCode )
            m_PriceRealProc!!.RequestReal (trName, stockCode);
        }


    }

    override fun onDestroy() {
        super.onDestroy()
        /** * ExpertMng 종료...  */
        CommExpertMng.getInstance().Close()

        m_PriceRealProc!!.ClearInstance();
        m_PriceRealProc = null;
    }


    override fun onSessionConnecting() {}
    override fun onSessionConnected(isSuccess: Boolean, strErrorMsg: String?) {}
    override fun onAppVersionState(isDone: Boolean) {}
    override fun onMasterDownState(isDone: Boolean) {}
    override fun onMasterLoadState(isDone: Boolean) {}
    override fun onInitFinished() {}
    override fun onRequiredRefresh() {}

    override fun onLoginResult(isSuccess: Boolean, strErrorMsg: String?) {
        if(isSuccess == true )
            Toast.makeText( getBaseContext(), "로그인 TR 성공", Toast.LENGTH_SHORT ).show();
        else
            Toast.makeText( getBaseContext(), strErrorMsg, Toast.LENGTH_SHORT ).show();
    }
    override fun onAccListResult(isSuccess: Boolean, strErrorMsg: String?) {}
    override fun onPublicCertResult(isSuccess: Boolean) {}
    override fun onLoginFinished() {
        val strMsg = "로그인 성공"
        Toast.makeText(baseContext, strMsg, Toast.LENGTH_SHORT).show()
        tResult.text = "로그인 성공"
    }

    override fun onRealDataReceived(strServiceId: String?) {
        System.out.println("onRealDataReceived" + strServiceId)
        //주식 종목체결 실시간
        if (strServiceId!!.contains("sc_r")) {
            val strcode = m_PriceRealProc!!.GetRealData(0, 0).trim { it <= ' ' } //종목코드
            val strPrice = m_PriceRealProc!!.GetRealData(0, 2) //현재가

            var result_sc_r = ""
            for (i in 0 until 43) {
                result_sc_r = result_sc_r + m_PriceRealProc!!.GetRealData(0, i) + " \n"
            }
            if (tResult != null) tResult.text = result_sc_r
        } else if (strServiceId!!.contains("sh_r")) {
            var result_sh_r = ""
            for (i in 0 until 58) {
                result_sh_r = result_sh_r + m_PriceRealProc!!.GetRealData(0, i) + " \n"
            }
            if (tResult != null) tResult.text = result_sh_r
        } else if (strServiceId!!.contains("sm_r")) {
            var result_sm_r = ""
            for (i in 0 until 67) {
                result_sm_r = result_sm_r + m_PriceRealProc!!.GetRealData(0, i) + " \n"
            }
            if (tResult != null) tResult.text = result_sm_r
        } else if (strServiceId!!.contains("juc_r")) {
            var result_juc_r = ""
            for (i in 0 until 29) {
                result_juc_r = result_juc_r + m_PriceRealProc!!.GetRealData(0, i) + " \n"
            }
            if (tResult != null) tResult.text = result_juc_r
        }
    }
}

 

한국투자증권 OpenAPI 모바일앱 개발 전체 리스트

stockant.tistory.com/304

 

==================================================

한국투자증권 OpenAPI 관련 블로그 내용을 모아 편집하여 전자책을 만들었습니다.

블로그 내용과 거의 유사하므로 블로그를 참고하실 분은 구매하지 않으셔도 됩니다.

kmong.com/gig/248964

 

 

 

반응형