init
This commit is contained in:
@@ -3,145 +3,25 @@ import os
|
||||
from datetime import datetime
|
||||
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
|
||||
|
||||
|
||||
class HTS_122630:
|
||||
class HTS_122630 (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):
|
||||
@@ -156,7 +36,7 @@ class HTS_122630:
|
||||
if i < 5:
|
||||
return -1, -1, -1
|
||||
|
||||
buy, weight, sell = self.buySellChecker.getPriceAndWeight1(data, i)
|
||||
buy, weight, sell = self.buySellChecker.getPriceAndWeight2(data, i)
|
||||
return buy, weight, sell
|
||||
|
||||
def getSellingPrice(self, final_price):
|
||||
@@ -186,7 +66,6 @@ class HTS_122630:
|
||||
|
||||
def buyRealTime(self, GIVEN_DAY):
|
||||
orderChecker = OrderChecker(self.stock_code)
|
||||
BASE_COUNT = 10
|
||||
|
||||
timecheckList = pd.read_csv("timecheck.csv").values.tolist()
|
||||
timecheck = {GIVEN_DAY + " " + str(second).zfill(6):False for second, check in timecheckList}
|
||||
@@ -219,8 +98,8 @@ class HTS_122630:
|
||||
|
||||
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)
|
||||
|
||||
# 매수를 주문한다.
|
||||
orderNum = self.requestOrder(OrderType.buy, self.stock_code, BUY_COUNT , bs_buy_price)
|
||||
@@ -244,7 +123,7 @@ class HTS_122630:
|
||||
|
||||
# 매도 가격을 가져온다.
|
||||
selling_count, selling_price = self.getSellingPrice(final_price)
|
||||
# 분석되 가격으로 매도 요청한다.
|
||||
# 분석된 가격으로 매도 요청한다.
|
||||
if selling_count != 0 and selling_price != 0:
|
||||
# 매도를 요청한다.
|
||||
orderNum = self.requestOrder(OrderType.sell, self.stock_code, selling_count, selling_price)
|
||||
@@ -268,13 +147,14 @@ class HTS_122630:
|
||||
|
||||
# 매도 가격을 가져온다.
|
||||
selling_count, selling_price = self.getSellingPrice(final_price)
|
||||
# 분석되 가격으로 매도 요청한다.
|
||||
# 분석된 가격으로 매도 요청한다.
|
||||
if selling_count != 0 and selling_price != 0:
|
||||
orderNum = self.requestOrder(OrderType.sell, self.stock_code, selling_count, selling_price)
|
||||
# 로그 출력
|
||||
print("SELL", THIS_TIME, selling_count, selling_price)
|
||||
|
||||
break
|
||||
|
||||
time.sleep(0.9)
|
||||
THIS_TIME = datetime.now()
|
||||
|
||||
@@ -289,10 +169,12 @@ if __name__ == "__main__":
|
||||
|
||||
# KODEX 인버스 * 2
|
||||
stock_code = "122630"
|
||||
hts = HTS_122630(stock_code)
|
||||
buy_count = 120
|
||||
|
||||
hts = HTS_122630(stock_code, buy_count)
|
||||
given_day = datetime.today().strftime('%Y%m%d')
|
||||
|
||||
hts.writeStockData(stock_code, "20211026")
|
||||
#hts.buyRealTime(given_day)
|
||||
#hts.writeStockData(stock_code, "20211026")
|
||||
hts.buyRealTime(given_day)
|
||||
|
||||
print ("done...")
|
||||
|
||||
Reference in New Issue
Block a user