Skip to content
Visual C++
2013.02.20 07:38

C# - etrade api site 게시물

조회 수 32889 추천 수 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
번호 분류 제목 날짜 조회 수
837 AutoHotKey autohotkey와 autohotkey_l 의 인터넷 창 띄우기 비교예제 2 2011.02.11 17772
836 컴퓨터잡담 AVR 강좌 3 2012.07.07 4858
835 컴퓨터잡담 bat(배치) 파일 제대로 쓰기 2009.11.24 6874
834 컴퓨터잡담 bat(배치)파일 문법 3 2009.11.19 13294
833 컴퓨터잡담 bat(배치)파일 문법 3 2009.11.19 12458
832 컴퓨터잡담 blogapi 기능을 사용하여 xe 게시판으로 글쓰기 2012.11.20 7171
831 Visual C++ c 와 c++ 의 차이점 12 2010.12.05 22340
830 Visual C++ C# - Etrade API 11 file 2013.02.20 15596
» Visual C++ C# - etrade api site 게시물 1 2013.02.20 32889
828 Visual C++ C#) 입문 - 프로젝트 생성하기 4 file 2013.02.12 18317
827 Visual C++ c++의 상식, 프로그램의 시작 15 2011.01.25 18835
826 컴퓨터잡담 CANON PRINTER ERROR CODE B203, B204 해결방법 2023.09.17 70128
825 HTMLPHPMSQL CCS 메뉴 스타일 링크 2021.08.04 3891
824 컴퓨터잡담 CCTV VIEWEASYV2 file 2019.05.31 4151
823 파이썬 chatGPT file 2023.02.11 4377
822 프로세스 CKAgent.exe npkcmsvc.exe nvsvc32.exe 1 2011.02.07 13217
821 AutoHotKey COM 사용 1 2011.02.21 19065
820 AutoHotKey com.ahk 1 2011.02.09 18260
819 AutoHotKey Com_invoke to login 2011.02.11 10712
818 컴퓨터잡담 CPU-Z file 2019.02.23 3884
Board Pagination Prev 1 ... 3 4 5 6 7 ... 46 Next
/ 46

http://urin79.com

우린친구블로그

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


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

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

설치 취소