diff --git a/HTS_etf.py b/HTS_etf.py index 7016d03..307ccd3 100644 --- a/HTS_etf.py +++ b/HTS_etf.py @@ -1,5 +1,6 @@ import time import os +import math import sqlite3 from datetime import datetime, timedelta @@ -120,17 +121,6 @@ class HTS_etf (HTS): return result - def getCount(self, stock_code, price, data=None): - MAX_BUY_PRICE = 50000 - if stock_code == "252670" or stock_code == "122630": - if data['slow_k'][-2] < data['slow_k'][-1] < 40: - MAX_BUY_PRICE = 200000 - if data['slow_k'][-2] < data['slow_k'][-1] < 20: - MAX_BUY_PRICE = 300000 - - count = int(MAX_BUY_PRICE / price) - return count - def getSlowK(self, stock_code): 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 @@ -176,20 +166,55 @@ class HTS_etf (HTS): return slow_k, p_slow_k, slow_k_week, p_slow_k_week, slow_k_month, p_slow_k_month + def getBuyCount(self, stock_code, 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): + + base_price = 10000 + + # 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 + + 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 (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 + + 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.ceil(max_price / bs_buy_price)) + + return buy_count + def buyRealTime(self, today, stocks, analyzed_day=1000): - VALID_DAY = True 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)): - print("#1 매수/매도 일이 아닙니다.", slow_k_kospi, p_slow_k_kospi) - VALID_DAY = False - 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))): - print("#2 매수/매도 일이 아닙니다.", p_slow_k_week_kospi, slow_k_week_kospi, p_slow_k_month_kospi, slow_k_month_kospi) - VALID_DAY = False LAST_DATA = {} for stock in stocks: @@ -239,33 +264,28 @@ class HTS_etf (HTS): bs_buy_weight = bsLine['buy_weight'][0] bs_sell_price = bsLine['sell'][0] + # 미체결 기록을 가져와서 10분 이상 된 매수 주문을 취소 한다. + ORDER_LIST = self.requestOrderList() + orderListToCancel = self.orderChecker.cancel(today, "A" + stock['stock_code'], ORDER_LIST, mins=10) + if len(orderListToCancel) > 0: + self.cancelOrderList(orderListToCancel) - 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 < 30 and 0 < slow_k_month < 30) 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))): + if bs_buy_price > 1000: - # 미체결 기록을 가져와서 10분 이상 된 매수 주문을 취소 한다. - ORDER_LIST = self.requestOrderList() - orderListToCancel = self.orderChecker.cancel(today, "A" + stock['stock_code'], ORDER_LIST, mins=10) - if len(orderListToCancel) > 0: - self.cancelOrderList(orderListToCancel) + if not self.orderChecker.exist(today, "A" + stock['stock_code'], hours=9): + buy_count = self.getBuyCount(stock['stock_code'], 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 bs_buy_price > 1000: + if buy_count > 0: - if not self.orderChecker.exist(today, "A" + stock['stock_code'], hours=9): - buy_count = self.getCount(stock['stock_code'], bs_buy_price, data) + # 매수를 주문한다. + orderNum = self.requestOrder(OrderType.buy, stock['stock_code'], buy_count , bs_buy_price) + self.orderChecker.buy(today, "A" + stock['stock_code'], buy_count, bs_buy_price, orderNum) - if buy_count > 0: + # slackbot에 메시지를 보냄 + self.slackBot.post_to_slack(stock['stock_code'], stock['stock_name'], "BUY", bsLine['buy'][len(bsLine['buy']) - 1], buy_count) - # 매수를 주문한다. - orderNum = self.requestOrder(OrderType.buy, stock['stock_code'], buy_count , bs_buy_price) - self.orderChecker.buy(today, "A" + stock['stock_code'], buy_count, bs_buy_price, orderNum) - - # slackbot에 메시지를 보냄 - self.slackBot.post_to_slack(stock['stock_code'], stock['stock_name'], "BUY", bsLine['buy'][len(bsLine['buy']) - 1], buy_count) - - # 로그 출력 - print("BUY", THIS_TIME.strftime('%Y%m%d %H%M%S'), orderNum, stock['stock_code'], stock['stock_name'], bs_buy_price, buy_count) + # 로그 출력 + print("BUY", THIS_TIME.strftime('%Y%m%d %H%M%S'), orderNum, stock['stock_code'], stock['stock_name'], bs_buy_price, buy_count) if bs_sell_price > 1000: check = self.sellStocks(stock['stock_code'], bs_sell_price)