This commit is contained in:
dsyoon
2023-04-13 23:33:22 +09:00
parent a2d2c79460
commit 0548a77cc4
2 changed files with 99 additions and 68 deletions

View File

@@ -167,6 +167,22 @@ class HTS_etf (HTS):
type_stock = {'day':1, 'week':10, 'month':100} type_stock = {'day':1, 'week':10, 'month':100}
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 < 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 < 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 slow_k < 10: type_stock['day'] = 10
if 10 < slow_k < 15: type_stock['day'] = 7.5 if 10 < slow_k < 15: type_stock['day'] = 7.5
if 15 < slow_k < 20: type_stock['day'] = 5 if 15 < slow_k < 20: type_stock['day'] = 5

View File

@@ -125,10 +125,12 @@ class HTS_Stocks (HTS):
return all_stocks, valid_company 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 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() 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): for i, item in enumerate(items):
@@ -139,7 +141,9 @@ class HTS_Stocks (HTS):
else: else:
break 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() 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): for i, item in enumerate(items):
@@ -150,7 +154,9 @@ class HTS_Stocks (HTS):
else: else:
break 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() 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): for i, item in enumerate(items):
@@ -168,58 +174,68 @@ class HTS_Stocks (HTS):
if slow_k_month is None or p_slow_k_month is None: if slow_k_month is None or p_slow_k_month is None:
slow_k_month, p_slow_k_month = -1, -1 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 slow_k_week < 20: type_stock['week'] = 100
if stock_code in valid_company: if 20 < slow_k_week < 25: type_stock['week'] = 75
if 0 < valid_company[stock_code] <= 250: if 25 < slow_k_week < 30: type_stock['week'] = 50
base_price = 20000 if 30 < slow_k_week < 35: type_stock['week'] = 25
elif 250 < valid_company[stock_code] <= 500:
base_price = 10000 if slow_k_month < 20: type_stock['month'] = 1000
elif 500 < valid_company[stock_code] <= 1000: if 20 < slow_k_month < 25: type_stock['month'] = 750
base_price = 5000 if 25 < slow_k_month < 30: type_stock['month'] = 500
elif 1000 < valid_company[stock_code] <= 1500: if 30 < slow_k_month < 35: type_stock['month'] = 250
base_price = 2000
else: else:
base_price = 1000 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
# kospi 상태 파악 if slow_k_week < 10: type_stock['week'] = 100
type_kospi = {'day':1, 'week':1, 'month':1} if 10 < slow_k_week < 15: type_stock['week'] = 75
if slow_k_kospi < p_slow_k_kospi: if 15 < slow_k_week < 20: type_stock['week'] = 50
if slow_k_kospi < 20: type_kospi['day'] = 4 if 20 < slow_k_week < 25: type_stock['week'] = 25
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
type_stock = {'day':1, 'week':1, 'month':1} if slow_k_month < 10: type_stock['month'] = 1000
slow_k, p_slow_k, slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month = self.getSlowK(stock_code) if 10 < slow_k_month < 15: type_stock['month'] = 750
if slow_k < p_slow_k: if 15 < slow_k_month < 20: type_stock['month'] = 500
if slow_k < 20: type_stock['day'] = 4 if 20 < slow_k_month < 25: type_stock['month'] = 250
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 (type_kospi['day'] == 1 and type_kospi['week'] == 1 and type_kospi['month'] == 1 and return type_stock
type_stock['day'] == 1 and type_stock['week'] == 1 and type_stock['month'] == 1):
return 0
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 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)) buy_count = int(math.floor(max_price / bs_buy_price))
return buy_count return buy_count
@@ -227,8 +243,7 @@ class HTS_Stocks (HTS):
def buyRealTime(self, today, n = 200): def buyRealTime(self, today, n = 200):
print ("START...") print ("START...")
THIS_TIME = datetime.now() THIS_TIME = datetime.now()
kospi_type = self.getStockType("^KS11", short=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")
all_stocks, valid_company = self.getCompanyInfo() 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'): 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'): 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): 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: