From 7cd373d801774d906b556953a9b304a3765cd0d9 Mon Sep 17 00:00:00 2001 From: dsyoon Date: Mon, 27 Feb 2023 14:00:25 +0900 Subject: [PATCH] init --- HTS_etf.py | 51 ++++++++++++++++++++++++++++++-------------- HTS_stocks.py | 58 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 75 insertions(+), 34 deletions(-) diff --git a/HTS_etf.py b/HTS_etf.py index 06b49e4..0acea83 100644 --- a/HTS_etf.py +++ b/HTS_etf.py @@ -132,7 +132,19 @@ class HTS_etf (HTS): return count def getSlowK(self, stock_code): - slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = -1, -1, -1, -1 + slow_k, p_slow_k, slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = -1, -1, -1, -1, -1, -1 + + self.cursor_stock.execute('select stochastic_slow_k, max(ymd) from stock_analysis where code=? group by 1 order by ymd desc',(stock_code,)) + items = self.cursor_stock.fetchall() + if items is not None and len(items) > 1: + for i, item in enumerate(items): + if i == 0: + slow_k = item[0] + elif i == 1: + p_slow_k = item[0] + else: + break + 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.fetchall() if items is not None and len(items) > 1: @@ -143,31 +155,38 @@ class HTS_etf (HTS): p_slow_k_week = item[0] else: break - 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.fetchall() - if items is not None and len(items) > 1: - for i, item in enumerate(items): - if i == 0: - slow_k_month = item[0] - elif i == 1: - p_slow_k_month = item[0] - else: - break + 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.fetchall() + if items is not None and len(items) > 1: + for i, item in enumerate(items): + if i == 0: + slow_k_month = item[0] + elif i == 1: + p_slow_k_month = item[0] + else: + break + + if slow_k is None or p_slow_k is None: + slow_k , p_slow_k = -1, -1 if slow_k_week is None or p_slow_k_week is None: slow_k_week, p_slow_k_week = -1, -1 if slow_k_month is None or p_slow_k_month is None: slow_k_month, p_slow_k_month = -1, -1 - return slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month + return slow_k, p_slow_k, slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month def buyRealTime(self, today, stocks, analyzed_day=1000): print ("START...") THIS_TIME = datetime.now() - final_sell_check = False + + slow_k_kospi, p_slow_k_kospi, slow_k_week_kospi, p_slow_k_week_kospi, slow_k_month_kospi, p_slow_k_month_kospi = self.getSlowK("^KS11") + if (50 < slow_k_kospi or 50 < p_slow_k_kospi or (slow_k_kospi < 50 and p_slow_k_kospi < 50 and slow_k_kospi < p_slow_k_kospi)): + return + if ((0 < slow_k_week_kospi < 50 and 0 < slow_k_month_kospi < 50) and + not ((20 < slow_k_week_kospi and slow_k_week_kospi < p_slow_k_week_kospi) or (20 < slow_k_month_kospi and slow_k_month_kospi < p_slow_k_month_kospi))): + return LAST_DATA = {} for stock in stocks: @@ -218,7 +237,7 @@ class HTS_etf (HTS): bs_sell_price = bsLine['sell'][0] - slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = self.getSlowK(stock['stock_code']) + slow_k, p_slow_k, slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = self.getSlowK(stock['stock_code']) if ((0 < slow_k_week < 50 and 0 < slow_k_month < 50) and not ((20 < slow_k_week and slow_k_week < p_slow_k_week) or (20 < slow_k_month and slow_k_month < p_slow_k_month))): diff --git a/HTS_stocks.py b/HTS_stocks.py index 0162655..3ba28ec 100644 --- a/HTS_stocks.py +++ b/HTS_stocks.py @@ -121,7 +121,19 @@ class HTS_Stocks (HTS): return all_stocks, valid_company def getSlowK(self, stock_code): - slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = -1, -1, -1, -1 + slow_k, p_slow_k, slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = -1, -1, -1, -1, -1, -1 + + self.cursor_stock.execute('select stochastic_slow_k, max(ymd) from stock_analysis where code=? group by 1 order by ymd desc', (stock_code,)) + items = self.cursor_stock.fetchall() + if items is not None and len(items) > 1: + for i, item in enumerate(items): + if i == 0: + slow_k = item[0] + elif i == 1: + p_slow_k = item[0] + else: + break + 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.fetchall() if items is not None and len(items)>1: @@ -132,23 +144,26 @@ class HTS_Stocks (HTS): p_slow_k_week = item[0] else: break - 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.fetchall() - if items is not None and len(items)>1: - for i, item in enumerate(items): - if i == 0: - slow_k_month = item[0] - elif i == 1: - p_slow_k_month = item[0] - else: - break - if slow_k_week is None or p_slow_k_week is None: - slow_k_week, p_slow_k_week = -1, -1 - if slow_k_month is None or p_slow_k_month is None: - slow_k_month, p_slow_k_month = -1, -1 + 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.fetchall() + if items is not None and len(items)>1: + for i, item in enumerate(items): + if i == 0: + slow_k_month = item[0] + elif i == 1: + p_slow_k_month = item[0] + else: + break - return slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month + if slow_k is None or p_slow_k is None: + slow_k, p_slow_k = -1, -1 + if slow_k_week is None or p_slow_k_week is None: + slow_k_week, p_slow_k_week = -1, -1 + if slow_k_month is None or p_slow_k_month is None: + slow_k_month, p_slow_k_month = -1, -1 + + return slow_k, p_slow_k, slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month def getMaxPrice(self, code, valid_company): max_price = 100000 @@ -170,6 +185,13 @@ class HTS_Stocks (HTS): print ("START...") THIS_TIME = datetime.now() + slow_k_kospi, p_slow_k_kospi, slow_k_week_kospi, p_slow_k_week_kospi, slow_k_month_kospi, p_slow_k_month_kospi = self.getSlowK("^KS11") + if (50 < slow_k_kospi or 50 < p_slow_k_kospi or (slow_k_kospi < 50 and p_slow_k_kospi < 50 and slow_k_kospi < p_slow_k_kospi)): + return + if ((0 < slow_k_week_kospi < 50 and 0 < slow_k_month_kospi < 50) and + not ((20 < slow_k_week_kospi and slow_k_week_kospi < p_slow_k_week_kospi) or (20 < slow_k_month_kospi and slow_k_month_kospi < p_slow_k_month_kospi))): + return + 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'): @@ -208,9 +230,9 @@ class HTS_Stocks (HTS): data.drop(data.index[:len(data) - self.analyzed_day], inplace=True) bsLine, data = self.buySellChecker.checkTransactionWithEnvelope(data, stock_code, self.analyzed_day, isRealTime=False) - slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = self.getSlowK(stock_code) + slow_k, p_slow_k, slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = self.getSlowK(stock['stock_code']) if ((0 < slow_k_week < 50 and 0 < slow_k_month < 50) and - not ((20 < slow_k_week and slow_k_week < p_slow_k_week) or (20 < slow_k_month and slow_k_month < p_slow_k_month))): + not ((20 < slow_k_week and slow_k_week < p_slow_k_week) or (20 < slow_k_month and slow_k_month < p_slow_k_month))): # 미체결 기록을 가져와서 10분 이상 된 매수 주문을 취소 한다. ORDER_LIST = self.requestOrderList()