init
This commit is contained in:
10
HTS_etf.py
10
HTS_etf.py
@@ -84,6 +84,7 @@ class HTS_etf (HTS):
|
||||
return result
|
||||
|
||||
def buyRealTime(self, today, stocks, analyzed_day=1000):
|
||||
self.connect2DB("hts.db")
|
||||
|
||||
print ("START...")
|
||||
THIS_TIME = datetime.now()
|
||||
@@ -191,11 +192,12 @@ class HTS_etf (HTS):
|
||||
time.sleep(10)
|
||||
THIS_TIME = datetime.now()
|
||||
|
||||
self.disconnect()
|
||||
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)
|
||||
self.labelChecker.updateLabel(db_filename, stock_code, bsLine, data, today_str)
|
||||
self.labelChecker.updateLabel(stock_code, bsLine, data, today_str)
|
||||
return
|
||||
|
||||
|
||||
@@ -209,7 +211,7 @@ if __name__ == "__main__":
|
||||
# KODEX 인버스 * 2
|
||||
stocks = [
|
||||
{"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)
|
||||
@@ -217,6 +219,6 @@ if __name__ == "__main__":
|
||||
hts.buyRealTime(today_str, stocks, analyzed_day=1000)
|
||||
|
||||
db_filename = os.path.join(RESOURCE_PATH, "hts.db")
|
||||
hts.insertStockData(db_filename, today_str, stocks)
|
||||
hts.insertStockData(today_str, stocks)
|
||||
|
||||
print ("done...")
|
||||
|
||||
@@ -23,8 +23,8 @@ class HTS_Stocks (HTS):
|
||||
analyzed_day = None
|
||||
MAX_BUY_PRICE = None
|
||||
|
||||
conn = None
|
||||
cursor = None
|
||||
conn_stock = None
|
||||
cursor_stock = None
|
||||
|
||||
def __init__(self, RESOURCE_PATH):
|
||||
super().__init__(RESOURCE_PATH)
|
||||
@@ -38,23 +38,20 @@ class HTS_Stocks (HTS):
|
||||
|
||||
self.analyzed_day = 120
|
||||
self.MAX_BUY_PRICE = 50000
|
||||
|
||||
return
|
||||
|
||||
def connect2DB(self, dbfile_name):
|
||||
try:
|
||||
self.conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, dbfile_name))
|
||||
self.cursor = self.conn.cursor()
|
||||
except:
|
||||
return False
|
||||
return True
|
||||
def connect2StockDB(self):
|
||||
|
||||
def disconnect(self):
|
||||
try:
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
except:
|
||||
return False
|
||||
return True
|
||||
self.conn_stock = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "stock.db"))
|
||||
self.cursor_stock = self.conn_stock.cursor()
|
||||
return
|
||||
|
||||
def disconnectStockDB(self):
|
||||
|
||||
self.cursor_stock.close()
|
||||
self.conn_stock.close()
|
||||
return
|
||||
|
||||
def getSellingPrice(self, log_time, stock_code, final_price, without_loss=False):
|
||||
# final_price와 diff를 받으면, 해당 가격으로 그냥 매도한다는 의미
|
||||
@@ -86,31 +83,34 @@ class HTS_Stocks (HTS):
|
||||
|
||||
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()
|
||||
self.cursor.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()
|
||||
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_stock.fetchall()
|
||||
for item in items:
|
||||
valid_company.add(item[0])
|
||||
return valid_company
|
||||
|
||||
return all_stocks, valid_company
|
||||
|
||||
def buyRealTime(self, today, n = 200):
|
||||
self.connect2DB("stock.db")
|
||||
self.connect2DB("hts.db")
|
||||
self.connect2StockDB()
|
||||
|
||||
print ("START...")
|
||||
THIS_TIME = datetime.now()
|
||||
|
||||
valid_company = self.valid_company()
|
||||
|
||||
self.cursor.execute('SELECT distinct code, name FROM stock order by code')
|
||||
items = self.cursor.fetchall()
|
||||
all_stocks, valid_company = self.getCompanyInfo()
|
||||
|
||||
while datetime.strptime(today + " 070000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 153100", '%Y%m%d %H%M%S'):
|
||||
|
||||
# 1515 까지만 매수를 시도한다.
|
||||
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:
|
||||
break
|
||||
|
||||
@@ -122,7 +122,7 @@ class HTS_Stocks (HTS):
|
||||
continue
|
||||
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:
|
||||
self.getRealTime_DailyCheck(today, stock_code, stock)
|
||||
data = self.stockStatus.analyze(stock, self.analyzed_day)
|
||||
@@ -205,6 +205,7 @@ class HTS_Stocks (HTS):
|
||||
time.sleep(10)
|
||||
THIS_TIME = datetime.now()
|
||||
|
||||
self.disconnectStockDB()
|
||||
self.disconnect()
|
||||
return
|
||||
|
||||
|
||||
107
hts/HTS.py
107
hts/HTS.py
@@ -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
|
||||
|
||||
@@ -86,11 +86,9 @@ class StockStatus (HTS):
|
||||
if cursor is None:
|
||||
return
|
||||
|
||||
stockTableName = 'stock'
|
||||
|
||||
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)
|
||||
cursor.execute(sql, (stock['CODE'],))
|
||||
items = cursor.fetchall()
|
||||
|
||||
Reference in New Issue
Block a user