This commit is contained in:
dosang.yoon
2022-06-24 21:25:57 +09:00
parent 0d523678a6
commit 0b4a87b334
5 changed files with 176 additions and 281 deletions

View File

@@ -5,7 +5,6 @@ import pandas as pd
from hts.HTS import HTS
from hts.OrderType import OrderType
from hts.OrderItem import OrderItem
from hts.BuySellChecker import BuySellChecker
from hts.OrderChecker import OrderChecker
@@ -15,134 +14,14 @@ class HTS_252670 (HTS):
buySellChecker = None
stock_code = None
buy_count = None
def __init__(self, stock_code):
def __init__(self, stock_code, buy_count):
super().__init__()
self.buySellChecker = BuySellChecker()
self.stock_code = stock_code
return
# 주식 현재가 조회
def writeStockData(self, stock_code, given_day):
objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos")
bConnect = objCpCybos.IsConnect
if (bConnect == 0):
print("PLUS가 정상적으로 연결되지 않음. ")
exit()
# 차트 객체 구하기
objStockChart = win32com.client.Dispatch("CpSysDib.StockChart")
outfp = open("./data/"+stock_code+"_"+given_day+".csv", mode="w", encoding="utf-8")
objStockChart.SetInputValue(0, 'A' + stock_code) # 종목 코드
objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회
objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일
objStockChart.SetInputValue(3, given_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)
objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용
objStockChart.BlockRequest()
size = objStockChart.GetHeaderValue(3)
outfp.write("%s,%s,%s,%s,%s,%s,%s\n" % ("날짜", "시간", "시가", "고가", "저가", "종가", "거래량"))
for i in range(size - 1, -1, -1):
day = objStockChart.GetDataValue(0, i)
time = objStockChart.GetDataValue(1, i)
start = objStockChart.GetDataValue(2, i)
high = objStockChart.GetDataValue(3, i)
low = objStockChart.GetDataValue(4, i)
close = objStockChart.GetDataValue(5, i)
vol = objStockChart.GetDataValue(6, i)
outfp.write("%d,%s,%d,%d,%d,%d,%d\n" % (day, str(time).zfill(4), start, high, low, close, vol))
outfp.close()
return
def write(self, day, result):
#날짜,시간,시가,고가,저가,종가,거래량
#20210909,900,2070,2070,2070,2070,0
outFp = open(day+".csv", mode="w", encoding="UTF-8")
outFp.write("날짜,시간,시가,고가,저가,종가,거래량\n")
for i in range(len(result["time"])):
outFp.write("%s,%s,%s,%s,%s,%s,%s\n"%(
result["time"][i].strftime('%Y%m%d'),
result["time"][i].strftime('%H%M'),
result["open"][i],
result["high"][i],
result["low"][i],
result["close"][i],
result["vol"][i]))
outFp.close()
return
# 주식 현재가 조회
def getRealTime(self, stock_code, given_day, result):
int_given_day = int(given_day)
objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos")
bConnect = objCpCybos.IsConnect
if (bConnect == 0):
print("PLUS가 정상적으로 연결되지 않음. ")
exit()
# 차트 객체 구하기
objStockChart = win32com.client.Dispatch("CpSysDib.StockChart")
objStockChart.SetInputValue(0, 'A'+stock_code) # 종목 코드
objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회
objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일
objStockChart.SetInputValue(3, given_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)
objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용
objStockChart.BlockRequest()
size = objStockChart.GetHeaderValue(3)
#print("날짜", "시간", "시가", "고가", "저가", "종가", "거래량")
start_time = datetime.strptime(given_day + " 090000", '%Y%m%d %H%M%S')
for i in range(size-1, -1, -1):
int_day = objStockChart.GetDataValue(0, i)
int_time = objStockChart.GetDataValue(1, i)
if int_day < int_given_day:
continue
time = datetime.strptime(str(int_day)+" "+str(int_time).zfill(4)+"00", '%Y%m%d %H%M%S')
if time < start_time:
continue
open = objStockChart.GetDataValue(2, i)
close = objStockChart.GetDataValue(5, i)
high = objStockChart.GetDataValue(3, i)
low = objStockChart.GetDataValue(4, i)
vol = objStockChart.GetDataValue(6, i)
if len(result["check"]) == 0:
result["check"].add(start_time)
result["time"].append(start_time)
result["open"].append(open)
result["close"].append(open)
result["high"].append(open)
result["low"].append(open)
result["vol"].append(0)
if time not in result["check"]:
result["check"].add(time)
result["time"].append(time)
result["open"].append(open)
result["close"].append(close)
result["high"].append(high)
result["low"].append(low)
result["vol"].append(vol)
self.buy_count = buy_count
return
def checkTransaction(self, data):
@@ -187,7 +66,6 @@ class HTS_252670 (HTS):
def buyRealTime(self, GIVEN_DAY):
orderChecker = OrderChecker(self.stock_code)
BASE_COUNT = 500
timecheckList = pd.read_csv("timecheck.csv").values.tolist()
timecheck = {GIVEN_DAY + " " + str(second).zfill(6):False for second, check in timecheckList}
@@ -219,8 +97,8 @@ class HTS_252670 (HTS):
if bs_buy_price > 0:
# 기본 100 주에 가중치를 추가해서 매수한다.
#BUY_COUNT = int(BASE_COUNT * bs_weight)
BUY_COUNT = int(BASE_COUNT * 1)
#BUY_COUNT = int(self.buy_count * bs_weight)
BUY_COUNT = int(self.buy_count * 1)
# 매수를 주문한다.
@@ -235,7 +113,7 @@ class HTS_252670 (HTS):
# 로그 출력
print("BUY", THIS_TIME.strftime('%Y%m%d %H%M%S'), BUY_COUNT, bs_buy_price, len(orderListToCancel), len(ORDER_LIST))
"""
if bs_sell_price > 0:
# 미체결 기록을 가져온다.
ORDER_LIST = self.requestOrderList()
@@ -253,14 +131,15 @@ class HTS_252670 (HTS):
# 로그 출력
print("SELL", THIS_TIME.strftime('%Y%m%d %H%M%S'), selling_count, selling_price, len(orderListToCancel), len(ORDER_LIST))
"""
# 로그 출력
print("TIMECHECK: %s, price: %d, low: %d, lower: %.2f, avg5: %.2f, slow_k_1: %.2f, slow_d_1: %.2f, slow_k: %.2f, slow_d: %.2f, rsi: %.2f, rsis: %.2f" %
(str(THIS_TIME), final_price, data["low"][data_size-1], data["lower"][data_size-1], data["avg5"][data_size-1],
data["slow_k"][data_size-2], data["slow_d"][data_size-2], data["slow_k"][data_size-1], data["slow_d"][data_size-1],
data["rsi"][data_size-1], data["rsis"][data_size-1]))
timecheck[THIS_TIME] = True
"""
if datetime.strptime(GIVEN_DAY + " 151530", '%Y%m%d %H%M%S') < THIS_TIME:
####
# 손해 보지 않는 가격에 매도한다.
@@ -280,7 +159,7 @@ class HTS_252670 (HTS):
print("SELL", THIS_TIME, selling_count, selling_price)
break
"""
time.sleep(0.9)
THIS_TIME = datetime.now()
@@ -295,10 +174,12 @@ if __name__ == "__main__":
# KODEX 인버스 * 2
stock_code = "252670"
hts = HTS_252670(stock_code)
buy_count = 120
hts = HTS_252670(stock_code, buy_count)
given_day = datetime.today().strftime('%Y%m%d')
#hts.writeStockData(stock_code, "20220520")
hts.buyRealTime(given_day)
hts.writeStockData(stock_code, "20220520")
print ("done...")