diff --git a/HTS_stocks.py b/HTS_stocks.py index 2535b0b..badee95 100644 --- a/HTS_stocks.py +++ b/HTS_stocks.py @@ -38,6 +38,7 @@ class HTS_Stocks (HTS): self.orderChecker = OrderChecker(self.RESOURCE_PATH, "STOCK") self.analyzed_day = 120 + self.MAX_BUY_PRICE = 300000 return @@ -54,64 +55,7 @@ class HTS_Stocks (HTS): self.conn_stock.close() return - def sellStocks(self, stock_code=None, bs_sell_price=None): - check = False - jangoDic = self.requstJango() - if jangoDic and len(jangoDic.keys()) > 0: - for code in jangoDic: - if stock_code is not None: - if code == "A"+stock_code and bs_sell_price is not None: - if jangoDic[code]['매도가능'] > 0: - #if 2 < jangoDic[code]['평가손익']: - # 2% 이상 시 수익 매도 - if jangoDic[code]['평가손익'] < -5: - # 5% 손해면 매도함 - self.requestOrder(OrderType.sell, code[1:], jangoDic[code]['매도가능'], bs_sell_price) - self.slackBot.post_to_slack(code, jangoDic[code]['종목명'], "SELL", bs_sell_price, jangoDic[code]['매도가능']) - check = True - else: - continue - else: - if jangoDic[code]['매도가능'] > 0: - #if 3 < jangoDic[code]['평가손익']: - # 3% 이상 시 수익 매도 - if jangoDic[code]['평가손익'] < -5: - # 5% 손해면 매도함 - currentStock = self.currentStock(code[1:]) - self.requestOrder(OrderType.sell, code[1:], jangoDic[code]['매도가능'], currentStock['close']) - self.slackBot.post_to_slack(code, jangoDic[code]['종목명'], "SELL", currentStock['close'], jangoDic[code]['매도가능']) - check = True - return check - - def getSellingPrice(self, log_time, stock_code, final_price, without_loss=False): - # final_price와 diff를 받으면, 해당 가격으로 그냥 매도한다는 의미 - # final_price와 diff가 None이면 장부가와 final 중 max로 팔겠다는 의미 - # final_price가 0이고 diff가 None이면 장부가로 팔겠다는 의미임 - - orderNum = None - jangoDic = self.requstJango() - if jangoDic and len(jangoDic.keys()) > 0: - for code in jangoDic: - if code == "A" + stock_code: - if jangoDic[code]['매도가능'] > 0: - if without_loss: - if final_price - jangoDic[code]['장부가'] > jangoDic[code]['장부가']*0.02: - orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], final_price) - return orderNum, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], final_price - else: - #max_price = max(jangoDic[code]['장부가'], final_price) - # 10% 이상 수익이어야 매도한다. - max_price = int(jangoDic[code]['장부가'] * 1.10) - if max_price <= final_price: - # 2% 이상 상승이면 매도 가능함 - sell_price = (int(max_price) - int(max_price) % 2) - - orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price) - return orderNum, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price - - return orderNum, None, None, None - - def getCompanyInfo(self): + def getCompanyInfo(self, today_stock): self.cursor_stock.execute('SELECT distinct code, name FROM stock order by code') all_stocks = self.cursor_stock.fetchall() @@ -120,131 +64,17 @@ class HTS_Stocks (HTS): self.cursor_stock.execute('select CODE, NAME, max(ymd) as ymd from fnguide where type != "E" and sales > 0 group by 1 order by sales desc') items = self.cursor_stock.fetchall() for i, item in enumerate(items): - if i < 1000: + if item[0] in today_stock: valid_company[item[0]] = i return all_stocks, valid_company - 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,)) - 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: - for i, item in enumerate(items): - if i == 0: - slow_k_week = item[0] - elif i == 1: - 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 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 - - 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 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 - - 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() - kospi_type = self.getStockType("^KS11", short=False) - all_stocks, valid_company = self.getCompanyInfo() + + today_stock = self.get_today_stock() + all_stocks, valid_company = self.getCompanyInfo(today_stock) while datetime.strptime(today + " 070000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 153100", '%Y%m%d %H%M%S'): @@ -262,10 +92,7 @@ class HTS_Stocks (HTS): stock_code = item[0] stock_name = item[1] - if ((stock_name.lower().find('ch') >= 0 or stock_name.find('차이나') >= 0 or - stock_name.find('바이오') >= 0 or stock_name.find('제약') >= 0 or stock_name.find('약품') >= 0 or - stock_name.find('스팩') >= 0 or re.search("\d.*?호", stock_name) is not None) or - stock_code not in valid_company): + if (stock_code not in valid_company): continue print("%5d: %8s, %-50s" % (idx, stock_code, stock_name)) @@ -293,8 +120,7 @@ class HTS_Stocks (HTS): if bs_buy_price > 1000: if not self.orderChecker.exist(today, "A" + stock_code, hours=5): - stock_type = self.getStockType(stock['stock_code'], short=False) - buy_count = self.getBuyCount(bs_buy_price, kospi_type, stock_type) + buy_count = int(self.MAX_BUY_PRICE / bs_buy_price) if buy_count > 0: @@ -308,29 +134,6 @@ class HTS_Stocks (HTS): # 로그 출력 print("BUY", THIS_TIME.strftime('%Y%m%d %H%M%S'), orderNum, stock_code, stock_name, bs_buy_price, buy_count) - """ - elif datetime.strptime(today + " 151530", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151600", '%Y%m%d %H%M%S'): - # 3시 15분 30초부터 3시 16분 사이는 잔량을 매도한다. - - #### - # 손해 보지 않는 가격에 매도한다. - #### - - # 주문 리스트를 가져온다. - orderList = self.requestOrderList() - # 15:10:00 이후라면 모든 미체결 취소한다. - self.cancelOrderList(orderList) - - for idx, item in enumerate(all_stocks): - stock_code = item[0] - stock_name = item[1] - - # 매도 가격을 가져온다. - orderNum, sell_time, jango, sell_price = self.getSellingPrice(THIS_TIME, stock_code, final_price=-1, without_loss=True) - # 로그 출력 - print("SELL", sell_time, stock_code, stock_name, -1, str(orderNum), jango, sell_price) - """ - time.sleep(3600) THIS_TIME = datetime.now() @@ -341,6 +144,27 @@ class HTS_Stocks (HTS): self.labelChecker.updateLabel(db_filename, stock_code, bsLine, data, today_str) return + def get_today_stock(self): + today_stock = set() + today_stock.add("311320") + today_stock.add("311390") + today_stock.add("290380") + today_stock.add("333430") + today_stock.add("200350") + today_stock.add("009410") + today_stock.add("053950") + today_stock.add("027710") + today_stock.add("246690") + today_stock.add("096530") + today_stock.add("201490") + today_stock.add("008420") + today_stock.add("302550") + today_stock.add("039240") + today_stock.add("377330") + today_stock.add("025550") + today_stock.add("006340") + today_stock.add("001440") + return today_stock if __name__ == "__main__": today = datetime.today()