This commit is contained in:
dsyoon
2023-02-01 22:37:30 +09:00
parent 346e1655f3
commit abbd852c6c
4 changed files with 80 additions and 96 deletions

View File

@@ -15,6 +15,9 @@ class HTS:
objCpCodeMgr = None
RESOURCE_PATH = None
conn = None
cursor = None
def __init__(self, RESOURCE_PATH):
self.RESOURCE_PATH = RESOURCE_PATH
#self.connect()
@@ -29,6 +32,23 @@ class HTS:
exit()
return
def connect2DB(self, dbfile_name="hts.db"):
try:
self.conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, dbfile_name))
self.cursor = self.conn.cursor()
except:
return False
return True
def disconnect(self):
try:
self.cursor.close()
self.conn.close()
except:
return False
return True
def all_stocks(self):
# 종목코드 리스트 구하기
self.objCpCodeMgr = win32com.client.Dispatch("CpUtil.CpCodeMgr")
@@ -456,27 +476,18 @@ class HTS:
return data
def insertStockData(self, db_filename, today, stocks):
tableName = 'hts'
conn = sqlite3.connect(db_filename)
cursor = conn.cursor()
def insertStockData(self, today, stocks):
# 테이블 생성
cursor.execute("CREATE TABLE IF NOT EXISTS " + tableName + " (CODE text, NAME text, ymd text, hms text, close REAL, open REAL, high REAL, low REAL, volume REAL, label INTEGER DEFAULT 0)")
self.cursor.execute("CREATE TABLE IF NOT EXISTS hts (CODE text, NAME text, ymd text, hms text, close REAL, open REAL, high REAL, low REAL, volume REAL, label INTEGER DEFAULT 0)")
# 키 생성
create_key = "CREATE INDEX IF NOT EXISTS " + tableName + "_idx on " + tableName + " (CODE, ymd, hms) "
cursor.execute(create_key)
conn.commit()
cursor.close()
conn.close()
create_key = "CREATE INDEX IF NOT EXISTS hts_idx on hts(CODE, ymd, hms) "
self.cursor.execute(create_key)
for stock in stocks:
items = self.getStockInfo(stock["stock_code"], today)
conn = sqlite3.connect(db_filename)
cursor = conn.cursor()
idx = 0
for item in items:
ymd = item[0]
@@ -489,13 +500,10 @@ class HTS:
idx += 1
cursor.execute('DELETE FROM ' + tableName + ' WHERE CODE=? and ymd=? and hms=?', (stock["stock_code"], ymd, hms,))
cursor.execute("INSERT INTO " + tableName + "(CODE, NAME, ymd, hms, close, open, high, low, volume) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", (stock["stock_code"], stock["stock_name"], ymd, hms, close, open, high, low, volume))
conn.commit()
cursor.close()
conn.close()
self.cursor.execute('DELETE FROM hts WHERE CODE=? and ymd=? and hms=?', (stock["stock_code"], ymd, hms,))
self.cursor.execute("INSERT INTO hts (CODE, NAME, ymd, hms, close, open, high, low, volume) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", (stock["stock_code"], stock["stock_name"], ymd, hms, close, open, high, low, volume))
self.conn.commit()
print("insert...", stock["stock_code"], stock["stock_name"], today)
return
@@ -545,65 +553,46 @@ class HTS:
result["vol"].append(int(vol))
return
def updateLabel(self, db_filename, stock_code, bsLine, data, ymd):
tableName = 'hts'
conn = sqlite3.connect(db_filename)
cursor = conn.cursor()
def updateLabel(self, stock_code, bsLine, data, ymd):
cursor.execute('Update ' + tableName + ' set label=? WHERE CODE=? and ymd=?', (0, stock_code, ymd,))
self.cursor.execute('Update hts set label=? WHERE CODE=? and ymd=?', (0, stock_code, ymd,))
for i in range(len(bsLine["buy"])):
if bsLine["buy"][i] > 0:
ymd = data['date'][i].strftime('%Y%m%d')
hms = data['date'][i].strftime('%H%M')
cursor.execute('Update ' + tableName + ' set label=? WHERE CODE=? and ymd=? and hms=?', (2, stock_code, ymd, hms))
self.cursor.execute('Update hts set label=? WHERE CODE=? and ymd=? and hms=?', (2, stock_code, ymd, hms))
for i in range(len(bsLine["sell"])):
if bsLine["sell"][i] > 0:
ymd = data['date'][i].strftime('%Y%m%d')
hms = data['date'][i].strftime('%H%M')
cursor.execute('Update ' + tableName + ' set label=? WHERE CODE=? and ymd=? and hms=?', (1, stock_code, ymd, hms))
self.cursor.execute('Update hts set label=? WHERE CODE=? and ymd=? and hms=?', (1, stock_code, ymd, hms))
self.conn.commit()
conn.commit()
cursor.close()
conn.close()
print("update...", stock_code, ymd)
return
def clearLabel(self, db_filename, stock_code, ymd):
tableName = 'hts'
conn = sqlite3.connect(db_filename)
cursor = conn.cursor()
def clearLabel(self, stock_code, ymd):
cursor.execute('update ' + tableName + ' set label=? WHERE CODE=? and ymd=? ', (0, stock_code, ymd,))
conn.commit()
cursor.close()
conn.close()
print("delete...", stock_code, ymd)
self.cursor.execute('update hts set label=? WHERE CODE=? and ymd=? ', (0, stock_code, ymd,))
self.conn.commit()
print("update...", stock_code, ymd)
return
def makeLabel(self, db_filename, stock_code, ymd, hms, label):
tableName = 'hts'
conn = sqlite3.connect(db_filename)
cursor = conn.cursor()
def makeLabel(self, stock_code, ymd, hms, label):
cursor.execute('Update ' + tableName + ' set label=? WHERE CODE=? and ymd=? and hms=?', (label, stock_code, ymd, hms,))
self.cursor.execute('Update hts set label=? WHERE CODE=? and ymd=? and hms=?', (label, stock_code, ymd, hms,))
self.conn.commit()
conn.commit()
cursor.close()
conn.close()
print("update...", stock_code, ymd, hms, label)
return
def getYMD(self, stock_code):
tableName = 'hts'
conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "hts.db"))
cursor = conn.cursor()
result = []
cursor.execute('SELECT distinct ymd FROM ' + tableName + ' WHERE CODE=? order by ymd', (stock_code,))
db_result = cursor.fetchall()
self.cursor.execute('SELECT distinct ymd FROM hts WHERE CODE=? order by ymd', (stock_code,))
db_result = self.cursor.fetchall()
for rows in db_result:
ymd = rows[0] # hts.날짜
@@ -612,12 +601,9 @@ class HTS:
return result
def getDBData(self, stock_code, day, result):
tableName = 'hts'
conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "hts.db"))
cursor = conn.cursor()
cursor.execute('SELECT ymd, hms, open, high, low, close, volume, label FROM ' + tableName + ' WHERE CODE=? and ymd=? order by ymd, hms', (stock_code, day,))
db_result = cursor.fetchall()
self.cursor.execute('SELECT ymd, hms, open, high, low, close, volume, label FROM hts WHERE CODE=? and ymd=? order by ymd, hms', (stock_code, day,))
db_result = self.cursor.fetchall()
for rows in db_result:
ymd = rows[0] # hts.날짜
hms = rows[1] # hts.시간
@@ -641,12 +627,9 @@ class HTS:
return
def isValidYMD(self, stock_code, day):
tableName = 'hts'
conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "hts.db"))
cursor = conn.cursor()
cursor.execute('SELECT ymd, count(*) as cnt FROM ' + tableName + ' WHERE CODE=? and ymd=?', (stock_code, day,))
db_result = cursor.fetchone()
self.cursor.execute('SELECT ymd, count(*) as cnt FROM hts WHERE CODE=? and ymd=?', (stock_code, day,))
db_result = self.cursor.fetchone()
if db_result[1] > 0:
return True
return False