This commit is contained in:
dsyoon
2021-09-23 21:08:05 +09:00
parent 119650de5a
commit 24424b6d6c
2 changed files with 424 additions and 39 deletions

View File

@@ -164,7 +164,10 @@ class HTS:
objStockOrder.SetInputValue(8, "01") # 주문호가 구분코드 - 01: 보통
# 매수 주문 요청
objStockOrder.BlockRequest()
nRet = objStockOrder.BlockRequest()
if (nRet != 0):
print("order error", nRet)
exit()
rqStatus = objStockOrder.GetDibStatus()
rqRet = objStockOrder.GetDibMsg1()
@@ -172,7 +175,9 @@ class HTS:
if rqStatus != 0:
exit()
return
orderNum = objStockOrder.GetHeaderValue(0)
return orderNum
# 주식 현금 매도주문
def orderToSell(self, stock_code, count, price):
@@ -207,7 +212,37 @@ class HTS:
if rqStatus != 0:
exit()
return
orderNum = objStockOrder.GetHeaderValue(0)
return orderNum
# 예약 취소 주문
def cancelOrder(self, ordernum, code):
objTrade = win32com.client.Dispatch("CpTrade.CpTdUtil")
initCheck = objTrade.TradeInit(0)
if (initCheck != 0):
print("주문 초기화 실패")
exit()
# 주식 매도 주문
acc = objTrade.AccountNumber[0] # 계좌번호
accFlag = objTrade.GoodsList(acc, 1) # 주식상품 구분
objCancel = win32com.client.Dispatch("CpTrade.CpTdNew9064")
objCancel.SetInputValue(0, ordernum)
objCancel.SetInputValue(1, acc)
objCancel.SetInputValue(2, accFlag[0])
objCancel.SetInputValue(3, code)
# 예약 취소 주문 요청
objCancel.BlockRequest()
if objCancel.GetDibStatus() != 0:
print("통신상태", objCancel.GetDibStatus(), objCancel.GetDibMsg1())
return False
print("예약주문 취소 ", ordernum, objCancel.GetDibMsg1())
return True
# 주식 현재가 조회
def printStockData(self, stock_code, day):
@@ -221,10 +256,10 @@ class HTS:
objStockChart = win32com.client.Dispatch("CpSysDib.StockChart")
objStockChart.SetInputValue(0, 'A' + stock_code) # 종목 코드
objStockChart.SetInputValue(1, ord('2')) # 1: 기간으로 조회, 2: 개수로 조회
objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회
objStockChart.SetInputValue(2, day) # 기간 조회 시, 시작일
objStockChart.SetInputValue(3, '20210909') # 기간 조회 시, 종료일
objStockChart.SetInputValue(4, 100000000) # 조회 시 가져오는 Line 개수
objStockChart.SetInputValue(3, day) # 기간 조회 시, 종료일
objStockChart.SetInputValue(4, 400) # 조회 시 가져오는 Line 개수
objStockChart.SetInputValue(5, [0, 1, 2, 3, 4, 5, 8]) # 날짜,시간,시가,고가,저가,종가,거래량
objStockChart.SetInputValue(6, ord('m')) # '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청
objStockChart.SetInputValue(7, 1)
@@ -246,6 +281,7 @@ class HTS:
return
def write(self, day, result):
#날짜,시간,시가,고가,저가,종가,거래량
#20210909,900,2070,2070,2070,2070,0
@@ -532,7 +568,7 @@ class HTS:
count = 3
self.orderToBuy(stock_code, count, price)
time.sleep(0.5)
time.sleep(60)
self.orderToSell(stock_code, count, price + 5)
# 가져온 만큼 데이터를 누적해서 파일로 작성한다.