diff --git a/HTS_etf.py b/HTS_etf.py index 2f898c8..8ed33b7 100644 --- a/HTS_etf.py +++ b/HTS_etf.py @@ -1,5 +1,6 @@ import time import os +import sqlite3 from datetime import datetime, timedelta from hts.HTS import HTS @@ -35,6 +36,19 @@ class HTS_etf (HTS): return + def connect2StockDB(self): + + 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 sellStocks(self, stock_code=None, bs_sell_price=None): jangoDic = self.requstJango() if jangoDic and len(jangoDic.keys()) > 0: @@ -113,6 +127,19 @@ class HTS_etf (HTS): count = int(MAX_BUY_PRICE / price) return count + def getSlowK(self, stock_code): + slow_k_week, slow_k_month = -1, -1 + self.cursor_stock.execute('select stochastic_slow_k, max(ymd) from stock_analysis_weekly where code=? group by 1 order by ymd desc', (stock_code, )) + items = self.cursor_stock.fetchone() + if len(items)>0: + slow_k_week = items[0] + self.cursor_stock.execute('select stochastic_slow_k, max(ymd) from stock_analysis_monthly where code=? group by 1 order by ymd desc', (stock_code, )) + items = self.cursor_stock.fetchone() + if len(items)>0: + slow_k_month = items[0] + + return slow_k_week, slow_k_month + def buyRealTime(self, today, stocks, analyzed_day=1000): print ("START...") @@ -134,6 +161,10 @@ class HTS_etf (HTS): time.sleep(0.1) + slow_k_week, slow_k_month = self.getSlowK(stock['stock_code']) + if slow_k_week < 0 or 20 < slow_k_week or slow_k_month < 0 or 20 < slow_k_month: + continue + try: # 데이터를 가지고 온다. result = self.getRealTime(stock['stock_code'], today, LAST_DATA[stock['stock_code']]) @@ -297,6 +328,7 @@ if __name__ == "__main__": hts = HTS_etf(RESOURCE_PATH) hts.connect2DB("hts.db") + hts.connect2StockDB() today_str = today.strftime('%Y%m%d') hts.buyRealTime(today_str, stocks, analyzed_day=1000) @@ -304,5 +336,6 @@ if __name__ == "__main__": db_filename = os.path.join(RESOURCE_PATH, "hts.db") hts.insertStockData(stocks, today) + hts.disconnectStockDB() hts.disconnect() print ("done...") diff --git a/HTS_stocks.py b/HTS_stocks.py index b7972b4..0dd8793 100644 --- a/HTS_stocks.py +++ b/HTS_stocks.py @@ -46,6 +46,7 @@ class HTS_Stocks (HTS): self.conn_stock = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "stock.db")) self.cursor_stock = self.conn_stock.cursor() + return def disconnectStockDB(self): @@ -115,8 +116,22 @@ class HTS_Stocks (HTS): return all_stocks, valid_company + def getSlowK(self, stock_code): + slow_k_week, slow_k_month = -1, -1 + self.cursor_stock.execute('select stochastic_slow_k, max(ymd) from stock_analysis_weekly where code=? group by 1 order by ymd desc', (stock_code, )) + items = self.cursor_stock.fetchone() + if len(items)>0: + slow_k_week = items[0] + self.cursor_stock.execute('select stochastic_slow_k, max(ymd) from stock_analysis_monthly where code=? group by 1 order by ymd desc', (stock_code, )) + items = self.cursor_stock.fetchone() + if len(items)>0: + slow_k_month = items[0] + + return slow_k_week, slow_k_month + def buyRealTime(self, today, n = 200): + a,b = self.getSlowK("001430") print ("START...") THIS_TIME = datetime.now() @@ -144,6 +159,10 @@ class HTS_Stocks (HTS): continue print(idx, stock_code, stock_name, ", CODE: ", stock_code, ", NAME: ", stock_name) + slow_k_week, slow_k_month = self.getSlowK(stock_code) + if slow_k_week < 0 or 20 < slow_k_week or slow_k_month < 0 or 20 < slow_k_month: + continue + stock = self.stockStatus.fetchLastData(self.cursor_stock, stock_code, n) try: self.getRealTime_DailyCheck(today, stock_code, stock)