From 0548a77cc48f50675f14c54e93482a3ab8b02547 Mon Sep 17 00:00:00 2001 From: dsyoon Date: Thu, 13 Apr 2023 23:33:22 +0900 Subject: [PATCH] init --- HTS_etf_long.py | 40 ++++++++++----- HTS_stocks.py | 127 +++++++++++++++++++++++++++--------------------- 2 files changed, 99 insertions(+), 68 deletions(-) diff --git a/HTS_etf_long.py b/HTS_etf_long.py index 9a37f84..6568d48 100644 --- a/HTS_etf_long.py +++ b/HTS_etf_long.py @@ -167,20 +167,36 @@ class HTS_etf (HTS): type_stock = {'day':1, 'week':10, 'month':100} - if slow_k < 10: type_stock['day'] = 10 - if 10 < slow_k < 15: type_stock['day'] = 7.5 - if 15 < slow_k < 20: type_stock['day'] = 5 - if 20 < slow_k < 25: type_stock['day'] = 2.5 + if stock_code == "^KS11": + if slow_k < 20: type_stock['day'] = 10 + if 20 < slow_k < 25: type_stock['day'] = 7.5 + if 25 < slow_k < 30: type_stock['day'] = 5 + if 30 < slow_k < 35: type_stock['day'] = 2.5 - if slow_k_week < 10: type_stock['week'] = 100 - if 10 < slow_k_week < 15: type_stock['week'] = 75 - if 15 < slow_k_week < 20: type_stock['week'] = 50 - if 20 < slow_k_week < 25: type_stock['week'] = 25 + if slow_k_week < 20: type_stock['week'] = 100 + if 20 < slow_k_week < 25: type_stock['week'] = 75 + if 25 < slow_k_week < 30: type_stock['week'] = 50 + if 30 < slow_k_week < 35: type_stock['week'] = 25 - if slow_k_month < 10: type_stock['month'] = 1000 - if 10 < slow_k_month < 15: type_stock['month'] = 750 - if 15 < slow_k_month < 20: type_stock['month'] = 500 - if 20 < slow_k_month < 25: type_stock['month'] = 250 + if slow_k_month < 20: type_stock['month'] = 1000 + if 20 < slow_k_month < 25: type_stock['month'] = 750 + if 25 < slow_k_month < 30: type_stock['month'] = 500 + if 30 < slow_k_month < 35: type_stock['month'] = 250 + else: + if slow_k < 10: type_stock['day'] = 10 + if 10 < slow_k < 15: type_stock['day'] = 7.5 + if 15 < slow_k < 20: type_stock['day'] = 5 + if 20 < slow_k < 25: type_stock['day'] = 2.5 + + if slow_k_week < 10: type_stock['week'] = 100 + if 10 < slow_k_week < 15: type_stock['week'] = 75 + if 15 < slow_k_week < 20: type_stock['week'] = 50 + if 20 < slow_k_week < 25: type_stock['week'] = 25 + + if slow_k_month < 10: type_stock['month'] = 1000 + if 10 < slow_k_month < 15: type_stock['month'] = 750 + if 15 < slow_k_month < 20: type_stock['month'] = 500 + if 20 < slow_k_month < 25: type_stock['month'] = 250 return type_stock diff --git a/HTS_stocks.py b/HTS_stocks.py index dab4905..cc58270 100644 --- a/HTS_stocks.py +++ b/HTS_stocks.py @@ -125,10 +125,12 @@ class HTS_Stocks (HTS): return all_stocks, valid_company - def getSlowK(self, stock_code): + def getStockType(self, stock_code, short=False): 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,)) + 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): @@ -139,9 +141,11 @@ class HTS_Stocks (HTS): 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, )) + 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: + if items is not None and len(items) > 1: for i, item in enumerate(items): if i == 0: slow_k_week = item[0] @@ -150,9 +154,11 @@ class HTS_Stocks (HTS): 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, )) + 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: + if items is not None and len(items) > 1: for i, item in enumerate(items): if i == 0: slow_k_month = item[0] @@ -168,67 +174,76 @@ class HTS_Stocks (HTS): 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 + type_stock = {'day': 1, 'week': 10, 'month': 100} - def getBuyCount(self, stock_code, valid_company, bs_buy_price, 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): + if stock_code == "^KS11": + if slow_k < 20: type_stock['day'] = 10 + if 20 < slow_k < 25: type_stock['day'] = 7.5 + if 25 < slow_k < 30: type_stock['day'] = 5 + if 30 < slow_k < 35: type_stock['day'] = 2.5 - base_price = 5000 - if stock_code in valid_company: - if 0 < valid_company[stock_code] <= 250: - base_price = 20000 - elif 250 < valid_company[stock_code] <= 500: - base_price = 10000 - elif 500 < valid_company[stock_code] <= 1000: - base_price = 5000 - elif 1000 < valid_company[stock_code] <= 1500: - base_price = 2000 - else: - base_price = 1000 + if slow_k_week < 20: type_stock['week'] = 100 + if 20 < slow_k_week < 25: type_stock['week'] = 75 + if 25 < slow_k_week < 30: type_stock['week'] = 50 + if 30 < slow_k_week < 35: type_stock['week'] = 25 - # kospi 상태 파악 - type_kospi = {'day':1, 'week':1, 'month':1} - if slow_k_kospi < p_slow_k_kospi: - if slow_k_kospi < 20: type_kospi['day'] = 4 - if 20 < slow_k_kospi < 30: type_kospi['day'] = 3 - if 30 < slow_k_kospi < 40: type_kospi['day'] = 2 - if slow_k_week_kospi < p_slow_k_week_kospi: - if slow_k_week_kospi < 20: type_kospi['week'] = 4 - if 20 < slow_k_week_kospi < 30: type_kospi['week'] = 3 - if 30 < slow_k_week_kospi < 40: type_kospi['week'] = 2 - if slow_k_month_kospi < p_slow_k_month_kospi: - if slow_k_month_kospi < 20: type_kospi['month'] = 4 - if 20 < slow_k_month_kospi < 30: type_kospi['month'] = 3 - if 30 < slow_k_month_kospi < 40: type_kospi['month'] = 2 + if slow_k_month < 20: type_stock['month'] = 1000 + if 20 < slow_k_month < 25: type_stock['month'] = 750 + if 25 < slow_k_month < 30: type_stock['month'] = 500 + if 30 < slow_k_month < 35: type_stock['month'] = 250 + else: + if slow_k < 10: type_stock['day'] = 10 + if 10 < slow_k < 15: type_stock['day'] = 7.5 + if 15 < slow_k < 20: type_stock['day'] = 5 + if 20 < slow_k < 25: type_stock['day'] = 2.5 - type_stock = {'day':1, 'week':1, 'month':1} - slow_k, p_slow_k, slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = self.getSlowK(stock_code) - if slow_k < p_slow_k: - if slow_k < 20: type_stock['day'] = 4 - if 20 < slow_k < 30: type_stock['day'] = 3 - if 30 < slow_k < 40: type_stock['day'] = 2 - if slow_k_week < p_slow_k_week: - if slow_k_week < 20: type_stock['week'] = 4 - if 20 < slow_k_week < 30: type_stock['week'] = 3 - if 30 < slow_k_week < 40: type_stock['week'] = 2 - if slow_k_month < p_slow_k_month: - if slow_k_month < 20: type_stock['month'] = 4 - if 20 < slow_k_month < 30: type_stock['month'] = 3 - if 30 < slow_k_month < 40: type_stock['month'] = 2 + if slow_k_week < 10: type_stock['week'] = 100 + if 10 < slow_k_week < 15: type_stock['week'] = 75 + if 15 < slow_k_week < 20: type_stock['week'] = 50 + if 20 < slow_k_week < 25: type_stock['week'] = 25 - if (type_kospi['day'] == 1 and type_kospi['week'] == 1 and type_kospi['month'] == 1 and - type_stock['day'] == 1 and type_stock['week'] == 1 and type_stock['month'] == 1): - return 0 + if slow_k_month < 10: type_stock['month'] = 1000 + if 10 < slow_k_month < 15: type_stock['month'] = 750 + if 15 < slow_k_month < 20: type_stock['month'] = 500 + if 20 < slow_k_month < 25: type_stock['month'] = 250 - max_price = math.log(type_kospi['day']*type_kospi['week']*type_kospi['month']*type_stock['day']*type_stock['week']*type_stock['month'], 1.3) * base_price - buy_count = int(math.floor(max_price / bs_buy_price)) + return type_stock + + def getBuyCount(self, bs_buy_price, kospi_type, stock_type): + + base_price = 10000 + log_base = 1.2 + p_k_m, p_k_w, p_k_d, p_s_m, p_s_w, p_s_d = 0.3, 0.2, 0.05, 0.25, 0.18, 0.02 + weight_1, weight_2, weight_3, weight_4, weight_5 = 0.5, 0.3, 0.14, 0.05, 0.01 + kospi_weight = weight_5 + if kospi_type['day'] == 10: kospi_weight = weight_1 + if kospi_type['day'] == 7.5: kospi_weight = weight_2 + if kospi_type['day'] == 5: kospi_weight = weight_3 + if kospi_type['day'] == 2.5: kospi_weight = weight_4 + stock_weight = weight_5 + if stock_type['day'] == 10: stock_weight = weight_1 + if stock_type['day'] == 7.5: stock_weight = weight_2 + if stock_type['day'] == 5: stock_weight = weight_3 + if stock_type['day'] == 2.5: stock_weight = weight_4 + + max_price = math.log( + kospi_weight * p_k_m * kospi_type['month'] + + kospi_weight * p_k_w * kospi_type['week'] + + kospi_weight * p_k_d * kospi_type['day'] + + stock_weight * p_s_m * stock_type['month'] + + stock_weight * p_s_w * stock_type['week'] + + stock_weight * p_s_d * stock_type['day'], log_base) * base_price + + buy_count = 0 + if max_price > 1: + buy_count = int(math.floor(max_price / bs_buy_price)) return buy_count def buyRealTime(self, today, n = 200): 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") + kospi_type = self.getStockType("^KS11", short=False) 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'): @@ -237,7 +252,7 @@ class HTS_Stocks (HTS): if datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151500", '%Y%m%d %H%M%S'): # 매도를 체크한다. - self.sellStocks() + #self.sellStocks() 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: