한국투자증권 주식매매프로그램 만들기

파이썬 주식매매프로그램 만들기

미국 주식 프로그램 만들기(eFriend expert)

1. 미국 주식 자동매매 프로그램 만들기 - 사고 팔기 기본

토폴로지 2022. 4. 2. 21:40
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import sys
from PyQt5.QtWidgets import *  #pip install PyQt5
from PyQt5.QtGui import *
from PyQt5.QAxContainer import *
from PyQt5.QtCore import *
 
koreanInvest_id = "@1111111"
koreanInvest_account = "12345678"
koreanInvest_account_code = "01"
koreanInvest_account_password = "1234"
 
koreanInvest_stock_buy_exchange = "NASD"
koreanInvest_stock_buy_code = "TQQQ"
koreanInvest_stock_buy_count = "1"
koreanInvest_stock_buy_price = "6.11"
koreanInvest_stock_buy_type = "00" # 00:지정가, 01:시장가, LOO:32, LOC:34
 
koreanInvest_stock_sell_exchange = "NASD"
koreanInvest_stock_sell_code = "TQQQ"
koreanInvest_stock_sell_count = "1"
koreanInvest_stock_sell_price = "6.11"
koreanInvest_stock_sell_type = "00" # 00:지정가, 01:시장가, LOO:32, LOC:34
 
 
class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
    
    def initUI(self):
        self.setWindowTitle("한국투자증권-API")
        self.setGeometry(300300800500)
 
        self.text_edit = QTextEdit(self)
        self.text_edit.setGeometry(00800400)
        # self.text_edit.setEnabled(False)
 
        # 매수 버튼
        btn_buy = QPushButton("Buy"self)   # 버튼 텍스트
        btn_buy.move(0400# 버튼 위치
        btn_buy.clicked.connect(self.btn_buy_clicked) # 클릭 시 실행할
 
        # 매도 버튼
        btn_sell = QPushButton("Sell"self)   # 버튼 텍스트
        btn_sell.move(100400# 버튼 위치
        btn_sell.clicked.connect(self.btn_sell_clicked) # 클릭 시 실행할
 
    def btn_buy_clicked(self):
        # 주문 입력 및 전송
        self.tran_os_us_buy = QAxWidget("ITGExpertCtl.ITGExpertCtlCtrl.1")
        self.tran_os_us_buy.SetSingleData(0, koreanInvest_account) #종합계좌번호 ##계좌일련번호[8],) 
        self.tran_os_us_buy.SetSingleData(1, koreanInvest_account_code) #계좌상품코드 ##계좌 상품코드, GetAccount() 함수 이용(뒤 2자리)
        self.tran_os_us_buy.SetSingleData(2self.tran_os_us_buy.GetEncryptPassword(koreanInvest_account_password)) #계좌비밀번호(반드시 암호화 처리 필요)
        self.tran_os_us_buy.SetSingleData(3, koreanInvest_stock_buy_exchange) #해외거래소코드 ##거래소코드 나스닥 NASD 뉴욕 NVSE 아맥스 AMEX
        self.tran_os_us_buy.SetSingleData(4, koreanInvest_stock_buy_code) #상품번호 ##종목코드
        self.tran_os_us_buy.SetSingleData(5, koreanInvest_stock_buy_count) #주문수량
        self.tran_os_us_buy.SetSingleData(6, koreanInvest_stock_buy_price) #해외주문단가 ##주문가격
        self.tran_os_us_buy.SetSingleData(7""#연락전화번호 #전화번호 필수값 아님
        self.tran_os_us_buy.SetSingleData(8""#운용사지정주문번호 입력하지 않음
        self.tran_os_us_buy.SetSingleData(9"0"#주문서버구분코드 고정값 0
        self.tran_os_us_buy.SetSingleData(10, koreanInvest_stock_buy_type) # 주문구분, 00: 지정가, 01: 시장가 LOO:32 LOC:34
 
        #항목 설정
        self.tran_os_us_buy.RequestData("OS_US_BUY")
        self.text_edit.append("미국 매수주문 전송 [" + str(self.tran_os_us_buy.GetSendRqID()) + "] ")
        self.tran_os_us_buy.ReceiveData.connect(self.tran_os_us_buy_receivedata) #이벤트 연결
 
    def tran_os_us_buy_receivedata(self):
        # 주문응답 이벤트
        self.text_edit.append("미국 매수주문 응답 [" + str(self.tran_os_us_buy.GetSendRqID()) + "] ")
 
        #결과 메세지
        self.text_edit.append("[" + str(self.tran_os_us_buy.GetSendRqID()) + "] " + self.tran_os_us_buy.GetReqMsgCode() + " " + self.tran_os_us_buy.GetReqMessage())
        self.text_edit.append("주문번호 : " + self.tran_os_us_buy.GetSingleData(10))
        self.text_edit.append("주문시간 : " + self.tran_os_us_buy.GetSingleData(20))
 
    def btn_sell_clicked(self):
        # 주문 입력 및 전송
        self.tran_os_us_sell = QAxWidget("ITGExpertCtl.ITGExpertCtlCtrl.1")
        self.tran_os_us_sell.SetSingleData(0, koreanInvest_account) #계좌일련번호[8], GetAccount() 함수 이용(앞 8자리)
        self.tran_os_us_sell.SetSingleData(1, koreanInvest_account_code) #계좌 상품코드, GetAccount() 함수 이용(뒤 2자리)
        self.tran_os_us_sell.SetSingleData(2self.tran_os_us_sell.GetEncryptPassword(koreanInvest_account_password)) #계좌비밀번호(반드시 암호화 처리 필요)
        self.tran_os_us_sell.SetSingleData(3, koreanInvest_stock_sell_exchange) #거래소코드
        self.tran_os_us_sell.SetSingleData(4, koreanInvest_stock_sell_code) #종목코드
        self.tran_os_us_sell.SetSingleData(5, koreanInvest_stock_sell_count) #주문수량
        self.tran_os_us_sell.SetSingleData(6, koreanInvest_stock_sell_price) #주문가격
        self.tran_os_us_sell.SetSingleData(7""#전화번호
        self.tran_os_us_sell.SetSingleData(8""#운용사지정주문번호
        self.tran_os_us_sell.SetSingleData(9"0"#주문서버구분코드
        self.tran_os_us_sell.SetSingleData(10, koreanInvest_stock_sell_type) # 주문구분, 지정가:00 MOO:31 MOC:33 LOO:32 LOC:34
 
        #항목 설정
        self.tran_os_us_sell.RequestData("OS_US_SELL")
        self.text_edit.append("미국 매도주문 전송 [" + str(self.tran_os_us_sell.GetSendRqID()) + "] ")
        self.tran_os_us_sell.ReceiveData.connect(self.tran_os_us_sell_receivedata) #이벤트 연결
 
    def tran_os_us_sell_receivedata(self):
        # 주문응답 이벤트
        self.text_edit.append("미국 매도주문 응답 [" + str(self.tran_os_us_sell.GetSendRqID()) + "] ")
 
        #결과 메세지
        self.text_edit.append("[" + str(self.tran_os_us_sell.GetSendRqID()) + "] " + self.tran_os_us_sell.GetReqMsgCode() + " " + self.tran_os_us_sell.GetReqMessage())
        self.text_edit.append("주문번호 : " + self.tran_os_us_sell.GetSingleData(10))
        self.text_edit.append("주문시간 : " + self.tran_os_us_sell.GetSingleData(20))
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    app.exec_()
cs

koreanInvest_id 본인 한투 아이디

koreanInvest_account 한투 계좌 번호

koreanInvest_account_code 한투 계좌 끝 두자리 번호

koreanInvest_account_password 한투 계좌 패스워드

 

koreanInvest_stock_buy_exchange 사려는 거래소 NASD 는 나스닥

koreanInvest_stock_buy_code 사려는 종목 상남자는 3배 레버리지 TQQQ

koreanInvest_stock_buy_count 사려는 개수

koreanInvest_stock_buy_price 사려는 가격 

koreanInvest_stock_buy_type 00으로 세팅하면 지정가로 사지지만 낮으면 시장가로 사짐

 

cmd 창은 반드시 관리자 권한으로 실행할 것!!!!

 

cmd 창에서 실행시 필요한 모듈

pip install PyQt5

 

cmd 창에서

python 소스이름 

실행

 

실행 결과

 

 

 

반응형