diff --git a/hts/BuySellChecker.py b/hts/BuySellChecker.py index 32056ba..f4318b9 100644 --- a/hts/BuySellChecker.py +++ b/hts/BuySellChecker.py @@ -269,7 +269,7 @@ class BuySellChecker: if data["slow_k"][i] <= 35: if (data["close"][i] - data["lower"][i]) / (data["upper"][i] - data["lower"][i]) < 0.35: if data["slow_k"][i - 1] < data["slow_d"][i - 1] and data["slow_d"][i] < data["slow_k"][i]: - if data['avg3'][i] < data['avg2'][i]: + if data['avg10'][i] < data['avg5'][i]: if data["open"][i] < data["close"][i]: buy = data["close"][i] else: @@ -281,7 +281,7 @@ class BuySellChecker: if data["slow_k"][i] <= 30: if (data["close"][i] - data["lower"][i]) / (data["upper"][i] - data["lower"][i]) < 0.35: if data["slow_k"][i - 1] < data["slow_d"][i - 1] and data["slow_d"][i] < data["slow_k"][i]: - if data['avg3'][i] < data['avg2'][i]: + if data['avg10'][i] < data['avg5'][i]: if data["close"][i] < data["avg5"][i]: buy = data["close"][i] else: @@ -370,6 +370,8 @@ class BuySellChecker: vol = result["vol"] close_df = pd.DataFrame(close) + avg2_list = close_df.rolling(window=2).mean().fillna(close[0]).values.tolist() + avg2 = [item[0] for item in avg2_list] avg5_list = close_df.rolling(window=5).mean().fillna(close[0]).values.tolist() avg5 = [item[0] for item in avg5_list] avg10_list = close_df.rolling(window=10).mean().fillna(close[0]).values.tolist() @@ -399,24 +401,24 @@ class BuySellChecker: STOCK = [] for i in range(len(open)): STOCK.append({'volume': vol[i], 'close': close[i], 'open': open[i], 'high': high[i], 'low': low[i], - 'avg5': avg5[i],'avg10': avg10[i],'avg30': avg30[i],'avg60': avg60[i]}) + 'avg2': avg2[i], 'avg5': avg5[i],'avg10': avg10[i],'avg30': avg30[i],'avg60': avg60[i]}) # stochastic 계산 - stochastic_df = self.stochastic.apply(STOCK, n=12, m=5, t=5) + stochastic_df = self.stochastic.apply(STOCK, n=30, m=5, t=5) stochastic_df = stochastic_df.fillna(100) fast_k = stochastic_df['fast_k'].values.tolist() slow_k = stochastic_df['slow_k'].values.tolist() slow_d = stochastic_df['slow_d'].values.tolist() # rsi 계산 - rsi_df = self.rsi.apply(STOCK, period=14, window=9) + rsi_df = self.rsi.apply(STOCK, period=30, window=5) rsi_df = rsi_df.fillna(100) rsi = rsi_df['rsi'].values.tolist() rsis = rsi_df['rsis'].values.tolist() temp = {"date": point_temp, "open": open, "high": high, "low": low, "close": close, "volume": vol, "upper": upper, "lower": lower, - "avg5": avg5, "avg10": avg10, "avg30": avg30, "avg60": avg60, + "avg2": avg2, "avg5": avg5, "avg10": avg10, "avg30": avg30, "avg60": avg60, "fast_k": fast_k, "slow_k": slow_k, "slow_d": slow_d, "rsi": rsi, "rsis": rsis} data = pd.DataFrame(temp) df_final_time = pd.DatetimeIndex(point_temp) diff --git a/hts/HTS.py b/hts/HTS.py index 3d87287..647e79b 100644 --- a/hts/HTS.py +++ b/hts/HTS.py @@ -369,3 +369,125 @@ class HTS: break return + + # 주식 현재가 조회 + def writeStockData(self, stock_code, given_day): + objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos") + bConnect = objCpCybos.IsConnect + if (bConnect == 0): + print("PLUS가 정상적으로 연결되지 않음. ") + exit() + + # 차트 객체 구하기 + objStockChart = win32com.client.Dispatch("CpSysDib.StockChart") + + outfp = open("./data/"+stock_code+"_"+given_day+".csv", mode="w", encoding="utf-8") + objStockChart.SetInputValue(0, 'A' + stock_code) # 종목 코드 + objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회 + objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일 + objStockChart.SetInputValue(3, given_day) # 기간 조회 시, 종료일 + objStockChart.SetInputValue(4, 400) # 조회 시 가져오는 Line 개수 + objStockChart.SetInputValue(5, [0, 1, 2, 3, 4, 5, 8]) # 날짜,시간,시가,고가,저가,종가,거래량 + objStockChart.SetInputValue(6, ord('m')) # '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청 + objStockChart.SetInputValue(7, 1) + objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용 + objStockChart.BlockRequest() + + size = objStockChart.GetHeaderValue(3) + + outfp.write("%s,%s,%s,%s,%s,%s,%s\n" % ("날짜", "시간", "시가", "고가", "저가", "종가", "거래량")) + for i in range(size - 1, -1, -1): + day = objStockChart.GetDataValue(0, i) + time = objStockChart.GetDataValue(1, i) + start = objStockChart.GetDataValue(2, i) + high = objStockChart.GetDataValue(3, i) + low = objStockChart.GetDataValue(4, i) + close = objStockChart.GetDataValue(5, i) + vol = objStockChart.GetDataValue(6, i) + outfp.write("%d,%s,%d,%d,%d,%d,%d\n" % (day, str(time).zfill(4), start, high, low, close, vol)) + outfp.close() + + return + + + def write(self, day, result): + #날짜,시간,시가,고가,저가,종가,거래량 + #20210909,900,2070,2070,2070,2070,0 + + outFp = open(day+".csv", mode="w", encoding="UTF-8") + outFp.write("날짜,시간,시가,고가,저가,종가,거래량\n") + for i in range(len(result["time"])): + outFp.write("%s,%s,%s,%s,%s,%s,%s\n"%( + result["time"][i].strftime('%Y%m%d'), + result["time"][i].strftime('%H%M'), + result["open"][i], + result["high"][i], + result["low"][i], + result["close"][i], + result["vol"][i])) + outFp.close() + return + + # 주식 현재가 조회 + def getRealTime(self, stock_code, given_day, result): + int_given_day = int(given_day) + objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos") + bConnect = objCpCybos.IsConnect + if (bConnect == 0): + print("PLUS가 정상적으로 연결되지 않음. ") + exit() + + # 차트 객체 구하기 + objStockChart = win32com.client.Dispatch("CpSysDib.StockChart") + + objStockChart.SetInputValue(0, 'A'+stock_code) # 종목 코드 + objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회 + objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일 + objStockChart.SetInputValue(3, given_day) # 기간 조회 시, 종료일 + objStockChart.SetInputValue(4, 400) # 조회 시 가져오는 Line 개수 + objStockChart.SetInputValue(5, [0, 1, 2, 3, 4, 5, 8]) # 날짜,시간,시가,고가,저가,종가,거래량 + objStockChart.SetInputValue(6, ord('m')) # '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청 + objStockChart.SetInputValue(7, 1) + objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용 + objStockChart.BlockRequest() + + size = objStockChart.GetHeaderValue(3) + + #print("날짜", "시간", "시가", "고가", "저가", "종가", "거래량") + start_time = datetime.strptime(given_day + " 090000", '%Y%m%d %H%M%S') + for i in range(size-1, -1, -1): + int_day = objStockChart.GetDataValue(0, i) + int_time = objStockChart.GetDataValue(1, i) + + if int_day < int_given_day: + continue + time = datetime.strptime(str(int_day)+" "+str(int_time).zfill(4)+"00", '%Y%m%d %H%M%S') + if time < start_time: + continue + + open = objStockChart.GetDataValue(2, i) + close = objStockChart.GetDataValue(5, i) + high = objStockChart.GetDataValue(3, i) + low = objStockChart.GetDataValue(4, i) + vol = objStockChart.GetDataValue(6, i) + + if len(result["check"]) == 0: + result["check"].add(start_time) + result["time"].append(start_time) + result["open"].append(open) + result["close"].append(open) + result["high"].append(open) + result["low"].append(open) + result["vol"].append(0) + + if time not in result["check"]: + result["check"].add(time) + result["time"].append(time) + + result["open"].append(open) + result["close"].append(close) + result["high"].append(high) + result["low"].append(low) + result["vol"].append(vol) + + return \ No newline at end of file diff --git a/hts/HTS_122630.py b/hts/HTS_122630.py index 56600e7..d0b44d1 100644 --- a/hts/HTS_122630.py +++ b/hts/HTS_122630.py @@ -3,145 +3,25 @@ import os from datetime import datetime import pandas as pd +from hts.HTS import HTS from hts.OrderType import OrderType -from hts.OrderItem import OrderItem from hts.BuySellChecker import BuySellChecker from hts.OrderChecker import OrderChecker -class HTS_122630: +class HTS_122630 (HTS): buySellChecker = None stock_code = None + buy_count = None - def __init__(self, stock_code): + def __init__(self, stock_code, buy_count): super().__init__() self.buySellChecker = BuySellChecker() self.stock_code = stock_code - return - - # 주식 현재가 조회 - def writeStockData(self, stock_code, given_day): - objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos") - bConnect = objCpCybos.IsConnect - if (bConnect == 0): - print("PLUS가 정상적으로 연결되지 않음. ") - exit() - - # 차트 객체 구하기 - objStockChart = win32com.client.Dispatch("CpSysDib.StockChart") - - outfp = open("./data/"+stock_code+"_"+given_day+".csv", mode="w", encoding="utf-8") - objStockChart.SetInputValue(0, 'A' + stock_code) # 종목 코드 - objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회 - objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일 - objStockChart.SetInputValue(3, given_day) # 기간 조회 시, 종료일 - objStockChart.SetInputValue(4, 400) # 조회 시 가져오는 Line 개수 - objStockChart.SetInputValue(5, [0, 1, 2, 3, 4, 5, 8]) # 날짜,시간,시가,고가,저가,종가,거래량 - objStockChart.SetInputValue(6, ord('m')) # '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청 - objStockChart.SetInputValue(7, 1) - objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용 - objStockChart.BlockRequest() - - size = objStockChart.GetHeaderValue(3) - - outfp.write("%s,%s,%s,%s,%s,%s,%s\n" % ("날짜", "시간", "시가", "고가", "저가", "종가", "거래량")) - for i in range(size - 1, -1, -1): - day = objStockChart.GetDataValue(0, i) - time = objStockChart.GetDataValue(1, i) - start = objStockChart.GetDataValue(2, i) - high = objStockChart.GetDataValue(3, i) - low = objStockChart.GetDataValue(4, i) - close = objStockChart.GetDataValue(5, i) - vol = objStockChart.GetDataValue(6, i) - outfp.write("%d,%s,%d,%d,%d,%d,%d\n" % (day, str(time).zfill(4), start, high, low, close, vol)) - outfp.close() - - return - - - def write(self, day, result): - #날짜,시간,시가,고가,저가,종가,거래량 - #20210909,900,2070,2070,2070,2070,0 - - outFp = open(day+".csv", mode="w", encoding="UTF-8") - outFp.write("날짜,시간,시가,고가,저가,종가,거래량\n") - for i in range(len(result["time"])): - outFp.write("%s,%s,%s,%s,%s,%s,%s\n"%( - result["time"][i].strftime('%Y%m%d'), - result["time"][i].strftime('%H%M'), - result["open"][i], - result["high"][i], - result["low"][i], - result["close"][i], - result["vol"][i])) - outFp.close() - return - - # 주식 현재가 조회 - def getRealTime(self, stock_code, given_day, result): - int_given_day = int(given_day) - objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos") - bConnect = objCpCybos.IsConnect - if (bConnect == 0): - print("PLUS가 정상적으로 연결되지 않음. ") - exit() - - # 차트 객체 구하기 - objStockChart = win32com.client.Dispatch("CpSysDib.StockChart") - - objStockChart.SetInputValue(0, 'A'+stock_code) # 종목 코드 - objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회 - objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일 - objStockChart.SetInputValue(3, given_day) # 기간 조회 시, 종료일 - objStockChart.SetInputValue(4, 400) # 조회 시 가져오는 Line 개수 - objStockChart.SetInputValue(5, [0, 1, 2, 3, 4, 5, 8]) # 날짜,시간,시가,고가,저가,종가,거래량 - objStockChart.SetInputValue(6, ord('m')) # '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청 - objStockChart.SetInputValue(7, 1) - objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용 - objStockChart.BlockRequest() - - size = objStockChart.GetHeaderValue(3) - - #print("날짜", "시간", "시가", "고가", "저가", "종가", "거래량") - start_time = datetime.strptime(given_day + " 090000", '%Y%m%d %H%M%S') - for i in range(size-1, -1, -1): - int_day = objStockChart.GetDataValue(0, i) - int_time = objStockChart.GetDataValue(1, i) - - if int_day < int_given_day: - continue - time = datetime.strptime(str(int_day)+" "+str(int_time).zfill(4)+"00", '%Y%m%d %H%M%S') - if time < start_time: - continue - - open = objStockChart.GetDataValue(2, i) - close = objStockChart.GetDataValue(5, i) - high = objStockChart.GetDataValue(3, i) - low = objStockChart.GetDataValue(4, i) - vol = objStockChart.GetDataValue(6, i) - - if len(result["check"]) == 0: - result["check"].add(start_time) - result["time"].append(start_time) - result["open"].append(open) - result["close"].append(open) - result["high"].append(open) - result["low"].append(open) - result["vol"].append(0) - - if time not in result["check"]: - result["check"].add(time) - result["time"].append(time) - - result["open"].append(open) - result["close"].append(close) - result["high"].append(high) - result["low"].append(low) - result["vol"].append(vol) - + self.buy_count = buy_count return def checkTransaction(self, data): @@ -156,7 +36,7 @@ class HTS_122630: if i < 5: return -1, -1, -1 - buy, weight, sell = self.buySellChecker.getPriceAndWeight1(data, i) + buy, weight, sell = self.buySellChecker.getPriceAndWeight2(data, i) return buy, weight, sell def getSellingPrice(self, final_price): @@ -186,7 +66,6 @@ class HTS_122630: def buyRealTime(self, GIVEN_DAY): orderChecker = OrderChecker(self.stock_code) - BASE_COUNT = 10 timecheckList = pd.read_csv("timecheck.csv").values.tolist() timecheck = {GIVEN_DAY + " " + str(second).zfill(6):False for second, check in timecheckList} @@ -219,8 +98,8 @@ class HTS_122630: if bs_buy_price > 0: # 기본 100 주에 가중치를 추가해서 매수한다. - #BUY_COUNT = int(BASE_COUNT * bs_weight) - BUY_COUNT = int(BASE_COUNT * 1) + #BUY_COUNT = int(self.buy_count * bs_weight) + BUY_COUNT = int(self.buy_count * 1) # 매수를 주문한다. orderNum = self.requestOrder(OrderType.buy, self.stock_code, BUY_COUNT , bs_buy_price) @@ -244,7 +123,7 @@ class HTS_122630: # 매도 가격을 가져온다. selling_count, selling_price = self.getSellingPrice(final_price) - # 분석되 가격으로 매도 요청한다. + # 분석된 가격으로 매도 요청한다. if selling_count != 0 and selling_price != 0: # 매도를 요청한다. orderNum = self.requestOrder(OrderType.sell, self.stock_code, selling_count, selling_price) @@ -268,13 +147,14 @@ class HTS_122630: # 매도 가격을 가져온다. selling_count, selling_price = self.getSellingPrice(final_price) - # 분석되 가격으로 매도 요청한다. + # 분석된 가격으로 매도 요청한다. if selling_count != 0 and selling_price != 0: orderNum = self.requestOrder(OrderType.sell, self.stock_code, selling_count, selling_price) # 로그 출력 print("SELL", THIS_TIME, selling_count, selling_price) break + time.sleep(0.9) THIS_TIME = datetime.now() @@ -289,10 +169,12 @@ if __name__ == "__main__": # KODEX 인버스 * 2 stock_code = "122630" - hts = HTS_122630(stock_code) + buy_count = 120 + + hts = HTS_122630(stock_code, buy_count) given_day = datetime.today().strftime('%Y%m%d') - hts.writeStockData(stock_code, "20211026") - #hts.buyRealTime(given_day) + #hts.writeStockData(stock_code, "20211026") + hts.buyRealTime(given_day) print ("done...") diff --git a/hts/HTS_252670.py b/hts/HTS_252670.py index 35b778a..c8c2b74 100644 --- a/hts/HTS_252670.py +++ b/hts/HTS_252670.py @@ -5,7 +5,6 @@ import pandas as pd from hts.HTS import HTS from hts.OrderType import OrderType -from hts.OrderItem import OrderItem from hts.BuySellChecker import BuySellChecker from hts.OrderChecker import OrderChecker @@ -15,134 +14,14 @@ class HTS_252670 (HTS): buySellChecker = None stock_code = None + buy_count = None - def __init__(self, stock_code): + def __init__(self, stock_code, buy_count): super().__init__() self.buySellChecker = BuySellChecker() self.stock_code = stock_code - return - - # 주식 현재가 조회 - def writeStockData(self, stock_code, given_day): - objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos") - bConnect = objCpCybos.IsConnect - if (bConnect == 0): - print("PLUS가 정상적으로 연결되지 않음. ") - exit() - - # 차트 객체 구하기 - objStockChart = win32com.client.Dispatch("CpSysDib.StockChart") - - outfp = open("./data/"+stock_code+"_"+given_day+".csv", mode="w", encoding="utf-8") - objStockChart.SetInputValue(0, 'A' + stock_code) # 종목 코드 - objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회 - objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일 - objStockChart.SetInputValue(3, given_day) # 기간 조회 시, 종료일 - objStockChart.SetInputValue(4, 400) # 조회 시 가져오는 Line 개수 - objStockChart.SetInputValue(5, [0, 1, 2, 3, 4, 5, 8]) # 날짜,시간,시가,고가,저가,종가,거래량 - objStockChart.SetInputValue(6, ord('m')) # '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청 - objStockChart.SetInputValue(7, 1) - objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용 - objStockChart.BlockRequest() - - size = objStockChart.GetHeaderValue(3) - - outfp.write("%s,%s,%s,%s,%s,%s,%s\n" % ("날짜", "시간", "시가", "고가", "저가", "종가", "거래량")) - for i in range(size - 1, -1, -1): - day = objStockChart.GetDataValue(0, i) - time = objStockChart.GetDataValue(1, i) - start = objStockChart.GetDataValue(2, i) - high = objStockChart.GetDataValue(3, i) - low = objStockChart.GetDataValue(4, i) - close = objStockChart.GetDataValue(5, i) - vol = objStockChart.GetDataValue(6, i) - outfp.write("%d,%s,%d,%d,%d,%d,%d\n" % (day, str(time).zfill(4), start, high, low, close, vol)) - outfp.close() - - return - - - def write(self, day, result): - #날짜,시간,시가,고가,저가,종가,거래량 - #20210909,900,2070,2070,2070,2070,0 - - outFp = open(day+".csv", mode="w", encoding="UTF-8") - outFp.write("날짜,시간,시가,고가,저가,종가,거래량\n") - for i in range(len(result["time"])): - outFp.write("%s,%s,%s,%s,%s,%s,%s\n"%( - result["time"][i].strftime('%Y%m%d'), - result["time"][i].strftime('%H%M'), - result["open"][i], - result["high"][i], - result["low"][i], - result["close"][i], - result["vol"][i])) - outFp.close() - return - - # 주식 현재가 조회 - def getRealTime(self, stock_code, given_day, result): - int_given_day = int(given_day) - objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos") - bConnect = objCpCybos.IsConnect - if (bConnect == 0): - print("PLUS가 정상적으로 연결되지 않음. ") - exit() - - # 차트 객체 구하기 - objStockChart = win32com.client.Dispatch("CpSysDib.StockChart") - - objStockChart.SetInputValue(0, 'A'+stock_code) # 종목 코드 - objStockChart.SetInputValue(1, ord('1')) # 1: 기간으로 조회, 2: 개수로 조회 - objStockChart.SetInputValue(2, given_day) # 기간 조회 시, 시작일 - objStockChart.SetInputValue(3, given_day) # 기간 조회 시, 종료일 - objStockChart.SetInputValue(4, 400) # 조회 시 가져오는 Line 개수 - objStockChart.SetInputValue(5, [0, 1, 2, 3, 4, 5, 8]) # 날짜,시간,시가,고가,저가,종가,거래량 - objStockChart.SetInputValue(6, ord('m')) # '차트 주가 - 월(M), 주(W), 일(D), 시(H), 분(m), 초(S) 차트 요청 - objStockChart.SetInputValue(7, 1) - objStockChart.SetInputValue(9, ord('1')) # 수정주가 사용 - objStockChart.BlockRequest() - - size = objStockChart.GetHeaderValue(3) - - #print("날짜", "시간", "시가", "고가", "저가", "종가", "거래량") - start_time = datetime.strptime(given_day + " 090000", '%Y%m%d %H%M%S') - for i in range(size-1, -1, -1): - int_day = objStockChart.GetDataValue(0, i) - int_time = objStockChart.GetDataValue(1, i) - - if int_day < int_given_day: - continue - time = datetime.strptime(str(int_day)+" "+str(int_time).zfill(4)+"00", '%Y%m%d %H%M%S') - if time < start_time: - continue - - open = objStockChart.GetDataValue(2, i) - close = objStockChart.GetDataValue(5, i) - high = objStockChart.GetDataValue(3, i) - low = objStockChart.GetDataValue(4, i) - vol = objStockChart.GetDataValue(6, i) - - if len(result["check"]) == 0: - result["check"].add(start_time) - result["time"].append(start_time) - result["open"].append(open) - result["close"].append(open) - result["high"].append(open) - result["low"].append(open) - result["vol"].append(0) - - if time not in result["check"]: - result["check"].add(time) - result["time"].append(time) - - result["open"].append(open) - result["close"].append(close) - result["high"].append(high) - result["low"].append(low) - result["vol"].append(vol) - + self.buy_count = buy_count return def checkTransaction(self, data): @@ -187,7 +66,6 @@ class HTS_252670 (HTS): def buyRealTime(self, GIVEN_DAY): orderChecker = OrderChecker(self.stock_code) - BASE_COUNT = 500 timecheckList = pd.read_csv("timecheck.csv").values.tolist() timecheck = {GIVEN_DAY + " " + str(second).zfill(6):False for second, check in timecheckList} @@ -219,8 +97,8 @@ class HTS_252670 (HTS): if bs_buy_price > 0: # 기본 100 주에 가중치를 추가해서 매수한다. - #BUY_COUNT = int(BASE_COUNT * bs_weight) - BUY_COUNT = int(BASE_COUNT * 1) + #BUY_COUNT = int(self.buy_count * bs_weight) + BUY_COUNT = int(self.buy_count * 1) # 매수를 주문한다. @@ -235,7 +113,7 @@ class HTS_252670 (HTS): # 로그 출력 print("BUY", THIS_TIME.strftime('%Y%m%d %H%M%S'), BUY_COUNT, bs_buy_price, len(orderListToCancel), len(ORDER_LIST)) - """ + if bs_sell_price > 0: # 미체결 기록을 가져온다. ORDER_LIST = self.requestOrderList() @@ -253,14 +131,15 @@ class HTS_252670 (HTS): # 로그 출력 print("SELL", THIS_TIME.strftime('%Y%m%d %H%M%S'), selling_count, selling_price, len(orderListToCancel), len(ORDER_LIST)) - """ + + # 로그 출력 print("TIMECHECK: %s, price: %d, low: %d, lower: %.2f, avg5: %.2f, slow_k_1: %.2f, slow_d_1: %.2f, slow_k: %.2f, slow_d: %.2f, rsi: %.2f, rsis: %.2f" % (str(THIS_TIME), final_price, data["low"][data_size-1], data["lower"][data_size-1], data["avg5"][data_size-1], data["slow_k"][data_size-2], data["slow_d"][data_size-2], data["slow_k"][data_size-1], data["slow_d"][data_size-1], data["rsi"][data_size-1], data["rsis"][data_size-1])) timecheck[THIS_TIME] = True - """ + if datetime.strptime(GIVEN_DAY + " 151530", '%Y%m%d %H%M%S') < THIS_TIME: #### # 손해 보지 않는 가격에 매도한다. @@ -280,7 +159,7 @@ class HTS_252670 (HTS): print("SELL", THIS_TIME, selling_count, selling_price) break - """ + time.sleep(0.9) THIS_TIME = datetime.now() @@ -295,10 +174,12 @@ if __name__ == "__main__": # KODEX 인버스 * 2 stock_code = "252670" - hts = HTS_252670(stock_code) + buy_count = 120 + + hts = HTS_252670(stock_code, buy_count) given_day = datetime.today().strftime('%Y%m%d') - #hts.writeStockData(stock_code, "20220520") hts.buyRealTime(given_day) + hts.writeStockData(stock_code, "20220520") print ("done...") diff --git a/hts/Simulation.py b/hts/Simulation.py index 2518db5..23121b1 100644 --- a/hts/Simulation.py +++ b/hts/Simulation.py @@ -74,7 +74,11 @@ class Simulation: data['low'] = pd.to_numeric(data['low']) data['close'] = pd.to_numeric(data['close']) data['volume'] = pd.to_numeric(data['volume']) + data['avg2'] = pd.to_numeric(data['avg2']) data['avg5'] = pd.to_numeric(data['avg5']) + data['avg10'] = pd.to_numeric(data['avg10']) + data['avg30'] = pd.to_numeric(data['avg30']) + data['avg60'] = pd.to_numeric(data['avg60']) data["fast_k"] = pd.to_numeric(data['fast_k']) data["slow_k"] = pd.to_numeric(data['slow_k']) data["slow_d"] = pd.to_numeric(data['slow_d']) @@ -101,6 +105,7 @@ class Simulation: sell_check = go.Scatter(x=data['date'], y=sell_line, mode='markers', name="sell", marker=dict(size=14, color=sell_colors, line_width=0)) bolinger_upper = go.Scatter(x=data['date'], y=data["upper"], name="upper", line_color='#8B4513') bolinger_lower = go.Scatter(x=data['date'], y=data["lower"], name="lower", line_color='#8B4513') + avg2 = go.Scatter(x=data['date'], y=data["avg2"], name="avg2", line_color='#800080') avg5 = go.Scatter(x=data['date'], y=data["avg5"], name="avg5", line_color='#800080') avg10 = go.Scatter(x=data['date'], y=data["avg10"], name="avg10", line_color='#ff00ff') avg30 = go.Scatter(x=data['date'], y=data["avg30"], name="avg30", line_color='#00ffff') @@ -115,9 +120,9 @@ class Simulation: rsis_line = go.Scatter(x=data['date'], y=data["rsis"], mode='lines', name='rsis') #candle_data = [candle_stick, bolinger_upper, bolinger_lower, buy_check, sell_check, avg1, avg2, avg5, avg10, avg20, avg30, avg40, avg50, avg60] - candle_data = [candle_stick, bolinger_upper, bolinger_lower, avg5, avg10, avg30, avg60, buy_check, sell_check] + candle_data = [candle_stick, bolinger_upper, bolinger_lower, avg2, avg5, avg10, avg30, avg60, buy_check, sell_check] volume_data = [volume_line] - stochastic_data = [slow_k_line, slow_d_line] + stochastic_data = [fast_k_line, slow_k_line, slow_d_line] rsi_data = [rsi_line, rsis_line] # 그래프를 그린다. @@ -179,12 +184,15 @@ if __name__ == "__main__": PROJECT_HOME = os.path.join(os.path.dirname(os.path.join(os.path.dirname(__file__)))) RESOURCE_DIR = PROJECT_HOME + "/resources/analysis/"+today.strftime("%Y%m%d") - stock_codes = ["252670", "122630"] - given_days = ['20210901', '20211020', '20220128', '20220520'] - simulation = Simulation(stock_codes[0]) + stock_codes = { + "252670": ['20220520', '20220128', '20220121', '20220120', '20211026'], + "122630": ['20211026', '20211025', '20211022', '20211021', '20211020'] + } - given_days = sorted(given_days, reverse=True) - for given_day in given_days: - simulation.simulate(given_day) + for stock_code in stock_codes: + simulation = Simulation(stock_code) + + for given_day in stock_codes[stock_code]: + simulation.simulate(given_day) print ("done...")