This commit is contained in:
dosangyoon
2022-07-29 16:26:22 +09:00
parent d1dafd524b
commit 801b650980
5 changed files with 88 additions and 8 deletions

View File

@@ -188,6 +188,7 @@ if __name__ == "__main__":
# KODEX 인버스 * 2
stock_code = "122630"
stock_name = "KODEX 레버리지"
buy_count = 80
hts = HTS_122630(RESOURCE_PATH, stock_code, buy_count)
@@ -203,6 +204,6 @@ if __name__ == "__main__":
hts.writeStockData(stock_code, today_str)
db_filename = os.path.join(RESOURCE_PATH, "hts.db")
hts.insertStockData(db_filename, stock_code, "KODEX 레버리지", today_str)
hts.insertStockData(db_filename, stock_code, stock_name, today_str)
print ("done...")

View File

@@ -192,6 +192,7 @@ if __name__ == "__main__":
# KODEX 인버스 * 2
stock_code = "252670"
stock_name = "KODEX 200선물인버스2X"
buy_count = 100
hts = HTS_252670(RESOURCE_PATH, stock_code, buy_count)
@@ -207,6 +208,6 @@ if __name__ == "__main__":
hts.writeStockData(stock_code, today_str)
db_filename = os.path.join(RESOURCE_PATH, "hts.db")
hts.insertStockData(db_filename, stock_code, "KODEX 200선물인버스2X", today_str)
hts.insertStockData(db_filename, stock_code, stock_name, today_str)
print ("done...")

View File

@@ -472,6 +472,8 @@ class HTS:
items = self.getStockInfo(stock_code, today)
conn = sqlite3.connect(inFileName)
cursor = conn.cursor()
idx = 0
for item in items:
ymd = item[0]
@@ -484,9 +486,6 @@ class HTS:
idx += 1
conn = sqlite3.connect(inFileName)
cursor = conn.cursor()
cursor.execute('DELETE FROM ' + tableName + ' WHERE CODE=? and ymd=? and hms=?', (stock_code, ymd, hms,))
cursor.execute('SELECT * FROM ' + tableName + ' WHERE CODE=? and ymd=? and hms=?', (stock_code, ymd, hms,))
result = cursor.fetchone()
@@ -495,9 +494,9 @@ class HTS:
"INSERT INTO " + tableName + "(CODE, NAME, ymd, hms, close, open, high, low, volume) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)",
(stock_code, stock_name, ymd, hms, close, open, high, low, volume))
conn.commit()
cursor.close()
conn.close()
conn.commit()
cursor.close()
conn.close()
print("insert...", stock_code, stock_name, today)
return

Binary file not shown.

79
stock/util/DBManager.py Normal file
View File

@@ -0,0 +1,79 @@
import os
import csv
import sqlite3
from glob import glob
class DBManager:
def __init__(self):
return
def insert(self, db_filename, hts_dir):
tableName = 'hts'
conn = sqlite3.connect(db_filename)
cursor = conn.cursor()
# 테이블 생성
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, type INTEGER)")
# 키 생성
create_key = "CREATE INDEX IF NOT EXISTS " + tableName + "_idx on " + tableName + " (CODE, ymd, hms, type) "
cursor.execute(create_key)
conn.commit()
cursor.close()
conn.close()
conn = sqlite3.connect(db_filename)
cursor = conn.cursor()
fileNames = glob(hts_dir)
for fileName in fileNames:
stock_code = None
stock_name = None
if fileName.find("252670") >= 0:
stock_code = "252670"
stock_name = "KODEX 200선물인버스2X"
elif fileName.find("122630") >= 0:
stock_code = "252670"
stock_name = "KODEX 레버리지"
if stock_code is None or stock_name is None:
continue
infp = open(fileName, 'r', encoding='utf-8')
reader = csv.reader(infp)
next(reader)
for rows in reader:
ymd = rows[0] # hts.날짜
hms = rows[1] # hts.시간
open_v = rows[2] # hts.시가
high = rows[3] # hts.고가
low = rows[4] # hts.저가
close = rows[5] # hts.종가
volume = rows[6] # hts.거래량
cursor.execute('DELETE FROM ' + tableName + ' WHERE CODE=? and ymd=? and hms=?', (stock_code, ymd, hms,))
cursor.execute('SELECT * FROM ' + tableName + ' WHERE CODE=? and ymd=? and hms=?', (stock_code, ymd, hms,))
result = cursor.fetchone()
if result == None:
cursor.execute("INSERT INTO " + tableName + "(CODE, NAME, ymd, hms, close, open, high, low, volume) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)",
(stock_code, stock_name, ymd, hms, close, open_v, high, low, volume))
infp.close()
print("insert...", stock_code, stock_name, fileName)
conn.commit()
cursor.close()
conn.close()
return
if __name__ == "__main__":
PROJECT_HOME = os.path.join(os.path.dirname(os.path.join(os.path.dirname(os.path.join(os.path.dirname(__file__))))))
RESOURCE_PATH = os.path.join(PROJECT_HOME, "resources")
dbManager = DBManager()
db_filename = os.path.join(RESOURCE_PATH, "hts.db")
hts_dir = os.path.join(RESOURCE_PATH, "hts", "*.csv")
dbManager.insert(db_filename, hts_dir)