init
This commit is contained in:
10
HTS_etf.py
10
HTS_etf.py
@@ -84,6 +84,7 @@ class HTS_etf (HTS):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def buyRealTime(self, today, stocks, analyzed_day=1000):
|
def buyRealTime(self, today, stocks, analyzed_day=1000):
|
||||||
|
self.connect2DB("hts.db")
|
||||||
|
|
||||||
print ("START...")
|
print ("START...")
|
||||||
THIS_TIME = datetime.now()
|
THIS_TIME = datetime.now()
|
||||||
@@ -191,11 +192,12 @@ class HTS_etf (HTS):
|
|||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
THIS_TIME = datetime.now()
|
THIS_TIME = datetime.now()
|
||||||
|
|
||||||
|
self.disconnect()
|
||||||
return
|
return
|
||||||
|
|
||||||
def updteTodayStock(self, db_filename, stock_code, today_str):
|
def updteTodayStock(self, stock_code, today_str):
|
||||||
bsLine, data = self.labelChecker.makeCandidate(stock_code, today_str)
|
bsLine, data = self.labelChecker.makeCandidate(stock_code, today_str)
|
||||||
self.labelChecker.updateLabel(db_filename, stock_code, bsLine, data, today_str)
|
self.labelChecker.updateLabel(stock_code, bsLine, data, today_str)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
@@ -209,7 +211,7 @@ if __name__ == "__main__":
|
|||||||
# KODEX 인버스 * 2
|
# KODEX 인버스 * 2
|
||||||
stocks = [
|
stocks = [
|
||||||
{"stock_code": "252670", "stock_name": "KODEX 200선물인버스2X"},
|
{"stock_code": "252670", "stock_name": "KODEX 200선물인버스2X"},
|
||||||
#{"stock_code": "122630", "stock_name": "KODEX 레버리지"}
|
{"stock_code": "122630", "stock_name": "KODEX 레버리지"}
|
||||||
]
|
]
|
||||||
|
|
||||||
hts = HTS_etf(RESOURCE_PATH)
|
hts = HTS_etf(RESOURCE_PATH)
|
||||||
@@ -217,6 +219,6 @@ if __name__ == "__main__":
|
|||||||
hts.buyRealTime(today_str, stocks, analyzed_day=1000)
|
hts.buyRealTime(today_str, stocks, analyzed_day=1000)
|
||||||
|
|
||||||
db_filename = os.path.join(RESOURCE_PATH, "hts.db")
|
db_filename = os.path.join(RESOURCE_PATH, "hts.db")
|
||||||
hts.insertStockData(db_filename, today_str, stocks)
|
hts.insertStockData(today_str, stocks)
|
||||||
|
|
||||||
print ("done...")
|
print ("done...")
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ class HTS_Stocks (HTS):
|
|||||||
analyzed_day = None
|
analyzed_day = None
|
||||||
MAX_BUY_PRICE = None
|
MAX_BUY_PRICE = None
|
||||||
|
|
||||||
conn = None
|
conn_stock = None
|
||||||
cursor = None
|
cursor_stock = None
|
||||||
|
|
||||||
def __init__(self, RESOURCE_PATH):
|
def __init__(self, RESOURCE_PATH):
|
||||||
super().__init__(RESOURCE_PATH)
|
super().__init__(RESOURCE_PATH)
|
||||||
@@ -38,23 +38,20 @@ class HTS_Stocks (HTS):
|
|||||||
|
|
||||||
self.analyzed_day = 120
|
self.analyzed_day = 120
|
||||||
self.MAX_BUY_PRICE = 50000
|
self.MAX_BUY_PRICE = 50000
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def connect2DB(self, dbfile_name):
|
def connect2StockDB(self):
|
||||||
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):
|
self.conn_stock = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "stock.db"))
|
||||||
try:
|
self.cursor_stock = self.conn_stock.cursor()
|
||||||
self.cursor.close()
|
return
|
||||||
self.conn.close()
|
|
||||||
except:
|
def disconnectStockDB(self):
|
||||||
return False
|
|
||||||
return True
|
self.cursor_stock.close()
|
||||||
|
self.conn_stock.close()
|
||||||
|
return
|
||||||
|
|
||||||
def getSellingPrice(self, log_time, stock_code, final_price, without_loss=False):
|
def getSellingPrice(self, log_time, stock_code, final_price, without_loss=False):
|
||||||
# final_price와 diff를 받으면, 해당 가격으로 그냥 매도한다는 의미
|
# final_price와 diff를 받으면, 해당 가격으로 그냥 매도한다는 의미
|
||||||
@@ -86,31 +83,34 @@ class HTS_Stocks (HTS):
|
|||||||
|
|
||||||
return orderNum, None, None, None
|
return orderNum, None, None, None
|
||||||
|
|
||||||
def valid_company(self):
|
def getCompanyInfo(self):
|
||||||
|
|
||||||
|
self.cursor.execute('SELECT distinct code, name FROM stock order by code')
|
||||||
|
all_stocks = self.cursor.fetchall()
|
||||||
|
|
||||||
valid_company = set()
|
valid_company = set()
|
||||||
self.cursor.execute('select CODE, NAME, max(ymd) as ymd from fnguide where type != "E" group by 1 order by total_assets desc')
|
self.cursor_stock.execute('select CODE, NAME, max(ymd) as ymd from fnguide where type != "E" group by 1 order by total_assets desc')
|
||||||
items = self.cursor.fetchall()
|
items = self.cursor_stock.fetchall()
|
||||||
for item in items:
|
for item in items:
|
||||||
valid_company.add(item[0])
|
valid_company.add(item[0])
|
||||||
return valid_company
|
|
||||||
|
return all_stocks, valid_company
|
||||||
|
|
||||||
def buyRealTime(self, today, n = 200):
|
def buyRealTime(self, today, n = 200):
|
||||||
self.connect2DB("stock.db")
|
self.connect2DB("hts.db")
|
||||||
|
self.connect2StockDB()
|
||||||
|
|
||||||
print ("START...")
|
print ("START...")
|
||||||
THIS_TIME = datetime.now()
|
THIS_TIME = datetime.now()
|
||||||
|
|
||||||
valid_company = self.valid_company()
|
all_stocks, valid_company = self.getCompanyInfo()
|
||||||
|
|
||||||
self.cursor.execute('SELECT distinct code, name FROM stock order by code')
|
|
||||||
items = self.cursor.fetchall()
|
|
||||||
|
|
||||||
while datetime.strptime(today + " 070000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 153100", '%Y%m%d %H%M%S'):
|
while datetime.strptime(today + " 070000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 153100", '%Y%m%d %H%M%S'):
|
||||||
|
|
||||||
# 1515 까지만 매수를 시도한다.
|
# 1515 까지만 매수를 시도한다.
|
||||||
if datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151500", '%Y%m%d %H%M%S'):
|
if datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151500", '%Y%m%d %H%M%S'):
|
||||||
|
|
||||||
for idx, item in enumerate(items):
|
for idx, item in enumerate(all_stocks):
|
||||||
if THIS_TIME < datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') or datetime.strptime(today + " 151500", '%Y%m%d %H%M%S') < THIS_TIME:
|
if THIS_TIME < datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') or datetime.strptime(today + " 151500", '%Y%m%d %H%M%S') < THIS_TIME:
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -122,7 +122,7 @@ class HTS_Stocks (HTS):
|
|||||||
continue
|
continue
|
||||||
print(idx, stock_code, stock_name, ", CODE: ", stock_code, ", NAME: ", stock_name)
|
print(idx, stock_code, stock_name, ", CODE: ", stock_code, ", NAME: ", stock_name)
|
||||||
|
|
||||||
stock = self.stockStatus.fetchLastData(self.cursor, stock_code, n)
|
stock = self.stockStatus.fetchLastData(self.cursor_stock, stock_code, n)
|
||||||
try:
|
try:
|
||||||
self.getRealTime_DailyCheck(today, stock_code, stock)
|
self.getRealTime_DailyCheck(today, stock_code, stock)
|
||||||
data = self.stockStatus.analyze(stock, self.analyzed_day)
|
data = self.stockStatus.analyze(stock, self.analyzed_day)
|
||||||
@@ -205,6 +205,7 @@ class HTS_Stocks (HTS):
|
|||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
THIS_TIME = datetime.now()
|
THIS_TIME = datetime.now()
|
||||||
|
|
||||||
|
self.disconnectStockDB()
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
107
hts/HTS.py
107
hts/HTS.py
@@ -15,6 +15,9 @@ class HTS:
|
|||||||
objCpCodeMgr = None
|
objCpCodeMgr = None
|
||||||
RESOURCE_PATH = None
|
RESOURCE_PATH = None
|
||||||
|
|
||||||
|
conn = None
|
||||||
|
cursor = None
|
||||||
|
|
||||||
def __init__(self, RESOURCE_PATH):
|
def __init__(self, RESOURCE_PATH):
|
||||||
self.RESOURCE_PATH = RESOURCE_PATH
|
self.RESOURCE_PATH = RESOURCE_PATH
|
||||||
#self.connect()
|
#self.connect()
|
||||||
@@ -29,6 +32,23 @@ class HTS:
|
|||||||
exit()
|
exit()
|
||||||
return
|
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):
|
def all_stocks(self):
|
||||||
# 종목코드 리스트 구하기
|
# 종목코드 리스트 구하기
|
||||||
self.objCpCodeMgr = win32com.client.Dispatch("CpUtil.CpCodeMgr")
|
self.objCpCodeMgr = win32com.client.Dispatch("CpUtil.CpCodeMgr")
|
||||||
@@ -456,27 +476,18 @@ class HTS:
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def insertStockData(self, db_filename, today, stocks):
|
def insertStockData(self, today, stocks):
|
||||||
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, 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) "
|
create_key = "CREATE INDEX IF NOT EXISTS hts_idx on hts(CODE, ymd, hms) "
|
||||||
cursor.execute(create_key)
|
self.cursor.execute(create_key)
|
||||||
|
|
||||||
conn.commit()
|
|
||||||
cursor.close()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
for stock in stocks:
|
for stock in stocks:
|
||||||
items = self.getStockInfo(stock["stock_code"], today)
|
items = self.getStockInfo(stock["stock_code"], today)
|
||||||
|
|
||||||
conn = sqlite3.connect(db_filename)
|
|
||||||
cursor = conn.cursor()
|
|
||||||
idx = 0
|
idx = 0
|
||||||
for item in items:
|
for item in items:
|
||||||
ymd = item[0]
|
ymd = item[0]
|
||||||
@@ -489,13 +500,10 @@ class HTS:
|
|||||||
|
|
||||||
idx += 1
|
idx += 1
|
||||||
|
|
||||||
cursor.execute('DELETE FROM ' + tableName + ' WHERE CODE=? and ymd=? and hms=?', (stock["stock_code"], ymd, hms,))
|
self.cursor.execute('DELETE FROM hts 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))
|
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))
|
||||||
|
|
||||||
conn.commit()
|
|
||||||
cursor.close()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
|
self.conn.commit()
|
||||||
print("insert...", stock["stock_code"], stock["stock_name"], today)
|
print("insert...", stock["stock_code"], stock["stock_name"], today)
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -545,65 +553,46 @@ class HTS:
|
|||||||
result["vol"].append(int(vol))
|
result["vol"].append(int(vol))
|
||||||
return
|
return
|
||||||
|
|
||||||
def updateLabel(self, db_filename, stock_code, bsLine, data, ymd):
|
def updateLabel(self, stock_code, bsLine, data, ymd):
|
||||||
tableName = 'hts'
|
|
||||||
conn = sqlite3.connect(db_filename)
|
|
||||||
cursor = conn.cursor()
|
|
||||||
|
|
||||||
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"])):
|
for i in range(len(bsLine["buy"])):
|
||||||
if bsLine["buy"][i] > 0:
|
if bsLine["buy"][i] > 0:
|
||||||
ymd = data['date'][i].strftime('%Y%m%d')
|
ymd = data['date'][i].strftime('%Y%m%d')
|
||||||
hms = data['date'][i].strftime('%H%M')
|
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"])):
|
for i in range(len(bsLine["sell"])):
|
||||||
if bsLine["sell"][i] > 0:
|
if bsLine["sell"][i] > 0:
|
||||||
ymd = data['date'][i].strftime('%Y%m%d')
|
ymd = data['date'][i].strftime('%Y%m%d')
|
||||||
hms = data['date'][i].strftime('%H%M')
|
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)
|
print("update...", stock_code, ymd)
|
||||||
return
|
return
|
||||||
|
|
||||||
def clearLabel(self, db_filename, stock_code, ymd):
|
def clearLabel(self, stock_code, ymd):
|
||||||
tableName = 'hts'
|
|
||||||
conn = sqlite3.connect(db_filename)
|
|
||||||
cursor = conn.cursor()
|
|
||||||
|
|
||||||
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,))
|
||||||
|
self.conn.commit()
|
||||||
conn.commit()
|
print("update...", stock_code, ymd)
|
||||||
cursor.close()
|
|
||||||
conn.close()
|
|
||||||
print("delete...", stock_code, ymd)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def makeLabel(self, db_filename, stock_code, ymd, hms, label):
|
def makeLabel(self, stock_code, ymd, hms, label):
|
||||||
tableName = 'hts'
|
|
||||||
conn = sqlite3.connect(db_filename)
|
|
||||||
cursor = conn.cursor()
|
|
||||||
|
|
||||||
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)
|
print("update...", stock_code, ymd, hms, label)
|
||||||
return
|
return
|
||||||
|
|
||||||
def getYMD(self, stock_code):
|
def getYMD(self, stock_code):
|
||||||
tableName = 'hts'
|
|
||||||
conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "hts.db"))
|
|
||||||
cursor = conn.cursor()
|
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
cursor.execute('SELECT distinct ymd FROM ' + tableName + ' WHERE CODE=? order by ymd', (stock_code,))
|
self.cursor.execute('SELECT distinct ymd FROM hts WHERE CODE=? order by ymd', (stock_code,))
|
||||||
db_result = cursor.fetchall()
|
db_result = self.cursor.fetchall()
|
||||||
for rows in db_result:
|
for rows in db_result:
|
||||||
ymd = rows[0] # hts.날짜
|
ymd = rows[0] # hts.날짜
|
||||||
|
|
||||||
@@ -612,12 +601,9 @@ class HTS:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def getDBData(self, stock_code, day, 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,))
|
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 = cursor.fetchall()
|
db_result = self.cursor.fetchall()
|
||||||
for rows in db_result:
|
for rows in db_result:
|
||||||
ymd = rows[0] # hts.날짜
|
ymd = rows[0] # hts.날짜
|
||||||
hms = rows[1] # hts.시간
|
hms = rows[1] # hts.시간
|
||||||
@@ -641,12 +627,9 @@ class HTS:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def isValidYMD(self, stock_code, day):
|
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,))
|
self.cursor.execute('SELECT ymd, count(*) as cnt FROM hts WHERE CODE=? and ymd=?', (stock_code, day,))
|
||||||
db_result = cursor.fetchone()
|
db_result = self.cursor.fetchone()
|
||||||
if db_result[1] > 0:
|
if db_result[1] > 0:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -86,11 +86,9 @@ class StockStatus (HTS):
|
|||||||
if cursor is None:
|
if cursor is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
stockTableName = 'stock'
|
|
||||||
|
|
||||||
stock = {"CODE": stock_code, "NAME": "", "PRICE": []}
|
stock = {"CODE": stock_code, "NAME": "", "PRICE": []}
|
||||||
|
|
||||||
sql = 'SELECT ymd, close, diff, open, high, low, volume FROM ' + stockTableName + ' where CODE=? order by ymd desc '
|
sql = 'SELECT ymd, close, diff, open, high, low, volume FROM stock where CODE=? order by ymd desc '
|
||||||
sql += ' limit ' + str(limit)
|
sql += ' limit ' + str(limit)
|
||||||
cursor.execute(sql, (stock['CODE'],))
|
cursor.execute(sql, (stock['CODE'],))
|
||||||
items = cursor.fetchall()
|
items = cursor.fetchall()
|
||||||
|
|||||||
Reference in New Issue
Block a user