Skip to content
Visual C++
2013.02.20 07:38

C# - etrade api site 게시물

조회 수 32885 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

 

 참고 : http://etrade.co.kr/index.jsp?url=%2Fxingapi%2Finfo%2FXingInfoOverview.jsp%3Fleft_menu_no%3D8%26front_menu_no%3D603

 

c#으로만든 API 래퍼 입니다.

접속, 로그인, 주문(매수, 매도) 까지만 테스트 완료 되었습니다.

필요한분 참고 하세요.

 

//

// 만든이 : 김국은 kbank@empal.com

// 배포   : 자유

// 배포시 배포자 정보 지우지 마세요.

//

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;

 

namespace 이트레이드HTS

{

    public class XingApi

    {

        #region const

        public const int WM_APP = 0x8000;

        public const string 실서버 = "hts.etrade.co.kr";

        public const string 모의서버 = "mis.etrade.co.kr";

        public const int 서버포트 = 20001;

        #endregion

 

        #region enum

        public enum Xing메시지

        {

            연결끊김         = WM_APP + 1,

            데이터수신       = WM_APP + 3,

            실시간데이터수신 = WM_APP + 4,

            로그인         = WM_APP + 5,

            로그아웃        = WM_APP + 6,

            시간초과        = WM_APP + 7,

        }

 

        public enum Xing수신

        {

            요청데이터 = 1,

            메시지데이터 = 2,

            시스템오류데이터 = 3,

            릴리즈데이터 = 4,

        }

        #endregion

 

        static string 대표계좌번호 = null;

        static string 계좌비밀번호 = "0000";

        static IntPtr 윈도핸들 = IntPtr.Zero;

 

        public static string 비밀번호

        {

            get { return 계좌비밀번호; }

            set { 계좌비밀번호 = value; }

        }

 

        public static IntPtr 핸들

        {

            get { return 윈도핸들; }

            set { 윈도핸들 = value; }

        }

 

        #region 서버 연결 - 테스트 완료

        public static bool 연결(IntPtr 핸들, String 서버주소, int 포트, int 시작메시지)

        {

            윈도핸들 = 핸들;

            return ETK_Connect(핸들, 서버주소, 포트, 시작메시지, -1, -1);

        }

 

        public static bool 연결중인가

        {

            get { return ETK_IsConnected(); }

        }

 

        public static bool 연결종료()

        {

            return ETK_Disconnect();

        }

        #endregion

 

        #region 로그인 - 테스트 완료

        public static bool 로그인(String 아이디, String 암호, String 인증서암호, int 서버타입)

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_Login(윈도핸들, 아이디, 암호, 인증서암호, 서버타입, false);

        }

 

        public static bool 로그아웃()

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_Logout(윈도핸들);

        }

        #endregion

 

        #region 오류 - 테스트 완료

        public static int 오류코드

        {

            get { return ETK_GetLastError(); }

        }

 

        public static String 오류메시지(int 코드)

        {

            IntPtr 포인터 = Marshal.AllocHGlobal(512);

            ETK_GetErrorMessage(코드, 포인터, 512);

 

            String 메시지 = Marshal.PtrToStringAnsi(포인터);

            Marshal.FreeHGlobal(포인터);

 

            return 메시지;

        }

        #endregion

 

        #region 조회성TR 관련 - 테스트 완료

        public static int 요청(String Tr코드, IntPtr 데이터, int 데이터크기, bool 계속여부, String 다음키, int 시간초과)

        {

            if (IntPtr.Zero == 윈도핸들)

                return -1;

 

            return ETK_Request(윈도핸들, Tr코드, 데이터, 데이터크기, 계속여부, 다음키, 시간초과);

        }

 

        public static int 데이터요청(String 코드, IntPtr 데이터, int 데이터크기,

            byte 헤더타입, bool 압축여부, bool 암호와여부, bool 인증여부, bool 계속여부, int 시간초과)

        {

            if (IntPtr.Zero == 윈도핸들)

                return -1;

 

            return ETK_RequestData(윈도핸들, 코드, 데이터, 데이터크기, 헤더타입, 압축여부, 암호와여부, 인증여부, 계속여부, 시간초과);

        }

 

        public static int 데이터요청2(String 코드, IntPtr 데이터, int 데이터크기,

            int nAccPos, byte 헤더타입, String ServiceCode, bool 압축여부, bool 인증여부,

            bool bCert, bool 계속여부, String 다음키, int 시간초과)

        {

            if (IntPtr.Zero == 윈도핸들)

                return -1;

 

            return ETK_RequestDataEx(윈도핸들, 코드, 데이터, 데이터크기,

            nAccPos, 헤더타입, ServiceCode, 압축여부, 인증여부,

            bCert, 계속여부, 다음키, 시간초과);

        }

 

        public static bool 릴리즈요청데이터(int nRequestID)

        {

            return ETK_ReleaseRequestData(nRequestID);

        }

 

        public static bool 릴리즈메시지데이터(IntPtr lParam)

        {

            return ETK_ReleaseMessageData(lParam);

        }

        #endregion

 

        #region 실시간TR 관련

        public static bool 실시간데이터요청(String TrNo, String strData, int nDataUnitLen)

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_AdviseRealData(윈도핸들, TrNo, strData, nDataUnitLen);

        }

 

        public static bool 실시간데이터요청해지(String TrNo, String strData, int nDataUnitLen)

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_UnadviseRealData(윈도핸들, TrNo, strData, nDataUnitLen);

        }

 

        public static bool 실시간요청해지()

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_UnadviseWindow(윈도핸들);

        }

        #endregion

 

        #region 계좌 관련 - 테스트 완료

        public static int 계좌수

        {

            get { return ETK_GetAccountListCount(); }

        }

 

        public static bool 계좌정보(int iIndex, ref string 계좌번호)

        {

            IntPtr 포인터 = Marshal.AllocHGlobal(512);

            bool 성공 = ETK_GetAccountList(iIndex, 포인터, 512);

            if (true == 성공)

                계좌번호 = Marshal.PtrToStringAnsi(포인터);

            else

                계좌번호 = "";

            Marshal.FreeHGlobal(포인터);

 

            return 성공;

        }

        #endregion

 

        #region 정보얻기 - 테스트 완료

        public static String 통신미디어

        {

            get

            {

                IntPtr 포인터 = Marshal.AllocHGlobal(512);

                ETK_GetCommMedia(포인터);

 

                String 미디어 = Marshal.PtrToStringAnsi(포인터);

                Marshal.FreeHGlobal(포인터);

                return 미디어;

            }

        }

 

        public static String ETK미디어

        {

            get

            {

                IntPtr 포인터 = Marshal.AllocHGlobal(512);

                ETK_GetETKMedia(포인터);

 

                String 미디어 = Marshal.PtrToStringAnsi(포인터);

                Marshal.FreeHGlobal(포인터);

                return 미디어;

            }

        }

 

        public static String 로컬IP

        {

            get

            {

                IntPtr 포인터 = Marshal.AllocHGlobal(512);

                ETK_GetClientIP(포인터);

 

                String ip = Marshal.PtrToStringAnsi(포인터);

                Marshal.FreeHGlobal(포인터);

                return ip;

            }

        }

 

        public static String 서버이름

        {

            get

            {

                IntPtr 포인터 = Marshal.AllocHGlobal(512);

                ETK_GetServerName(포인터);

 

                String 이름 = Marshal.PtrToStringAnsi(포인터);

                Marshal.FreeHGlobal(포인터);

                return 이름;

            }

        }

        #endregion

 

        #region XingApi

        // 서버 연결

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_Connect(IntPtr hWnd, String strServerIP, int iPort, int iStartMsgID, int iTimeout, int iSendMaxPacketSize);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_IsConnected();

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_Disconnect();

 

        // 로그인

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_Login(IntPtr hWnd, String strID, String strPwd, String CertPwd, int iType, bool bShowCertErrDlg);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_Logout(IntPtr hWnd);

 

        // 오류 메시지

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_GetLastError();

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_GetErrorMessage(int iErrorCode, IntPtr pBuffer, int iSize);

 

        // 조회성TR 관련

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_Request(IntPtr hWnd, String strTrCode, IntPtr pData, int iDataSize,

            [In, MarshalAs(UnmanagedType.Bool)] bool bNext, String strNextKey, int nTimeOut);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_RequestData(IntPtr hWnd, String strCode, IntPtr pData, int iDataSize,

            byte chHeaderType,

            [In, MarshalAs(UnmanagedType.Bool)] bool bCompress,

            [In, MarshalAs(UnmanagedType.Bool)] bool bEncrypt,

            [In, MarshalAs(UnmanagedType.Bool)] bool bCert,

            [In, MarshalAs(UnmanagedType.Bool)] bool bNext,

            int nTimeOut);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_RequestDataEx(IntPtr hWnd, String strCode, IntPtr pData, int iDataSize,

            int nAccPos, byte chHeaderType, String ServiceCode,

            [In, MarshalAs(UnmanagedType.Bool)] bool bCompress,

            [In, MarshalAs(UnmanagedType.Bool)] bool bEncrypt,

            [In, MarshalAs(UnmanagedType.Bool)] bool bCert,

            [In, MarshalAs(UnmanagedType.Bool)] bool bNext,

            String NextKey, int nTimeOut);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_ReleaseRequestData(int nRequestID);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_ReleaseMessageData(IntPtr lParam);

 

        // 실시간TR 관련

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_AdviseRealData(IntPtr hWnd, String TrNo, String strData, int nDataUnitLen);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_UnadviseRealData(IntPtr hWnd, String TrNo, String strData, int nDataUnitLen);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_UnadviseWindow(IntPtr hWnd);

 

        // 계좌 관련

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_GetAccountListCount();

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_GetAccountList(int iIndex, IntPtr strAcc, int iAccSize);

 

        // 정보얻기

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern void ETK_GetCommMedia(IntPtr pMedia);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern void ETK_GetETKMedia(IntPtr pMedia);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern void ETK_GetClientIP(IntPtr pClient);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern void ETK_GetServerName(IntPtr pServerName);

        #endregion

    }

 

    #region 자료구조

    [StructLayout(LayoutKind.Sequential, Pack = 1), ComVisible(false)]

    public class 조회TR수신패킷

    {

        public int RequestID;                       // Request ID

        public int 받은데이터크기;              // 받은 데이터 크기

        public int 할당된크기;      // lpData에 할당된 크기

        public int 걸린시간;                // 전송에서 수신까지 걸린시간(1/1000초)

        public int nDataMode;                   // 1:BLOCK MODE, 2:NON-BLOCK MODE

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5 + 1)]

        public byte[] szTrCode;         // AP Code

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]

        public byte[] 다음조회;         // '0' : 다음조회 없음, '1' : 다음조회 있음

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 18 + 1)]

        public byte[] 연속키;           // 연속키, Data Header가 B 인 경우에만 사용

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30 + 1)]

        public byte[] 사용자데이터;         // 사용자 데이터

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]

        public byte[] Block명;          // Block 명, Block Mode 일때 사용

    }

 

    [StructLayout(LayoutKind.Sequential, Pack = 1), ComVisible(false)]

    public class 메시지수신패킷

    {

        public int RequestID;                       // Request ID

        public int nIsSystemError;              // 0:일반메시지, 1:System Error 메시지

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5 + 1)]

        public byte[] szMsgCode;                // 메시지 코드

        public int nMsgLength;                  // Message 길이

        [MarshalAs(UnmanagedType.LPStr)]

        public string 메시지;

    }

 

    [StructLayout(LayoutKind.Sequential, Pack = 1), ComVisible(false)]

    public struct 실시간TR수신패킷

    {

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3 + 1)]

        public byte[] szTrCode;

        public int nKeyLength;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32 + 1)]

        public byte[] szKeyData;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32 + 1)]

        public byte[] szRegKey;

        public int nDataLength;

    }

    #endregion

}

로그인 후 댓글쓰기가 가능합니다.

?

List of Articles
번호 분류 제목 날짜 조회 수
897 WindowsTip VBS) FTP.scriptlet and Shell.scriptlet 2013.09.21 48506
896 파이썬 python AttributeError: 'WebDriver' object has no attribute 'find_element_by_css_selector' 해결방법 2023.05.07 47327
895 AutoHotKey AHK) 보안프로그램 등으로 화면복사(Printscreen) 안될때 사용방법 1 12 file 2012.11.21 47162
894 프로세스 pinomate.exe 프로세스 삭제방법 6 2011.02.13 45972
893 WindowsTip VBS) PostMessage or SendMessage to external program 2013.09.21 45964
892 컴퓨터잡담 [PHP] 원격지의 이미지 사이즈 구하는 방법 2 2009.08.11 44501
891 AutoHotKey Ahk) 웹페이지 감시결과에 따라 마이피플로 글 전송하기 12 file 2013.01.06 43995
890 컴퓨터잡담 emule 서버리스트 2010.11.10 43018
889 Excel Excel Vba) 셀의 행, 열(column, row) 주소 알아내기 또는 삽입하기 더불어 제어하기 2012.01.05 42946
888 HTMLPHPMSQL 지정한 이미지파일명을 출력 시키는 시험문제풀이 html 1 2023.12.13 41483
887 AutoHotKey Autohotkey) 화면보호기(ScreenSaver) On/Off 방법 17 2012.03.16 40671
886 [Docs]스프레드시트 구글 스프레드시트에서 셀값이 특정일에서 현재일과 3일 이내의 범위에 들어오면 이메일을 발송하는 방법 2023.03.26 40260
885 Excel GET.CELL 매크로함수 응용 11 2012.07.16 40172
884 컴퓨터잡담 안드로이드 동영상 재생시 파란색 물음표 박스만 나올때 조치방법 2 file 2013.04.26 39278
883 컴퓨터잡담 URL Encoding 특수문자코드 4 2012.03.30 38972
882 컴퓨터잡담 mysql 날짜타입에 기본값으로 현재시간넣기 1 2009.12.07 38585
881 AutoHotKey ahk) 오토핫키 콤보박스 제어하기 file 2013.10.30 38146
880 WindowsTip 네트워크에 있는 다른 시스템과 ip 주소가 충돌합니다. 1 file 2013.03.29 38111
879 컴퓨터잡담 테블릿을 세컨트모니터로??? 2023.04.26 37857
878 컴퓨터잡담 다른 윈도우 창 프로그램 제어 1 2009.12.03 37720
Board Pagination Prev 1 2 3 4 5 ... 46 Next
/ 46

http://urin79.com

우린친구블로그

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소