Skip to content
Visual C++
2013.02.20 07:38

C# - etrade api site 게시물

조회 수 32892 추천 수 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
번호 분류 제목 날짜 조회 수
57 컴퓨터잡담 [Autohotkey] 인터넷 창을 여러개 띄우고 컨트롤 할때 ahk_id 알아내기 1 3 2009.12.19 19463
56 컴퓨터잡담 List of Windows Messages 2 1 2009.12.15 17963
55 컴퓨터잡담 wm_syscommand 등의 값 1 3 2009.12.15 8948
54 컴퓨터잡담 Spy & Capture Spy++과 같은 계열의 프로그램 1 2009.12.10 17416
53 컴퓨터잡담 [팁&테크] 하드디스크 파티션 숨기기 2 2009.12.08 13494
52 컴퓨터잡담 Windows XP에서 Boot.ini 파일을 편집하는 방법 2009.12.08 19534
51 컴퓨터잡담 mysql 날짜타입에 기본값으로 현재시간넣기 1 2009.12.07 38589
50 컴퓨터잡담 아스키 코드 변환(hex) 2009.12.06 19997
49 컴퓨터잡담 이더리얼, 윈캡, 윈덤프 사용방법 및 다운로드 3 3 2009.12.06 16195
48 컴퓨터잡담 mysql 명령어 1 2 2009.12.04 12370
47 컴퓨터잡담 주키(Primary Key, 프라이머리키)와 자동 증가(Auto Increment) 필드 2009.12.04 21562
46 컴퓨터잡담 다른 윈도우 창 프로그램 제어 1 2009.12.03 37795
45 컴퓨터잡담 TIME_WAIT 줄이기(소켓이 부족하여 프로세스 강제중단을 막기위해) 4 2009.11.30 34111
44 컴퓨터잡담 php 에서 mysql 제어하기 2009.11.28 28200
43 컴퓨터잡담 windows error 코드표 4 2009.11.25 22197
42 컴퓨터잡담 mysql 접속불가시 재부팅 2009.11.24 12089
41 컴퓨터잡담 mysql 접속에러시 재부팅 하는 배치파일 2009.11.24 26693
40 컴퓨터잡담 Windows 초간단 remote 백업 명령 2009.11.24 23511
39 컴퓨터잡담 Ghost용 배치 파일 작성 예 1 2009.11.24 8164
38 컴퓨터잡담 bat(배치) 파일 제대로 쓰기 2009.11.24 6874
Board Pagination Prev 1 ... 42 43 44 45 46 Next
/ 46

http://urin79.com

우린친구블로그

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


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

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

설치 취소