diff --git a/Simulation.py b/Simulation.py index db3350d..cf431d3 100644 --- a/Simulation.py +++ b/Simulation.py @@ -1,10 +1,11 @@ from math import nan -from datetime import datetime +from datetime import datetime, timedelta import csv import pandas as pd import plotly.graph_objects as go from plotly import subplots -from glob import glob +import sqlite3 +import os from hts.BuySellChecker import BuySellChecker @@ -13,10 +14,11 @@ class Simulation: buySellChecker = None stock_code = None - def __init__(self, stock_code): + def __init__(self, RESOURCE_PATH, stock_code): self.buySellChecker = BuySellChecker() self.stock_code = stock_code + self.RESOURCE_PATH = RESOURCE_PATH #self.connect() return @@ -47,6 +49,32 @@ class Simulation: result["vol"].append(int(vol)) return + def getDBData(self, stock_code, lastday, result): + tableName = 'hts' + conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "hts.db")) + cursor = conn.cursor() + + cursor.execute('SELECT ymd, hms, open, high, low, close, volume FROM ' + tableName + ' WHERE CODE=? and ymd=? order by ymd, hms', (stock_code, lastday,)) + db_result = cursor.fetchall() + for rows in db_result: + ymd = rows[0] # hts.날짜 + hms = rows[1] # hts.시간 + open = rows[2] # hts.시가 + high = rows[3] # hts.고가 + low = rows[4] # hts.저가 + close = rows[5] # hts.종가 + vol = rows[6] # hts.거래량 + + temp = datetime.strptime(str(ymd) + " " + str(hms).zfill(4) + "00", '%Y%m%d %H%M%S') + + result["time"].append(temp) + result["open"].append(int(open)) + result["close"].append(int(close)) + result["high"].append(int(high)) + result["low"].append(int(low)) + result["vol"].append(int(vol)) + return + def draw(self, stock_code, given_day, data, bsLine): # 어제 데이터는 지운다. data = data.loc[pd.DatetimeIndex(data.index).day == int(given_day[6:])] @@ -152,7 +180,7 @@ class Simulation: return - def simulate(self, days, path=None): + def simulate(self, today): result = {"check": set(), "time": [], "open": [], @@ -161,15 +189,15 @@ class Simulation: "low": [], "vol": []} - last_day = days[0] - today = days[1] - - if path == None: - path = "resources/hts" - # 데이터를 가지고 온다. - self.getCSV(path + "/" + self.stock_code + "_" + last_day + ".csv", last_day, result) - self.getCSV(path + "/" + self.stock_code + "_" + today + ".csv", today, result) + #self.getCSV(path + "/" + self.stock_code + "_" + last_day + ".csv", last_day, result) + #self.getCSV(path + "/" + self.stock_code + "_" + today + ".csv", last_day, result) + for i in range(1, 10): + last_day = (datetime.strptime(today, '%Y%m%d') - timedelta(i)).strftime('%Y%m%d') + self.getDBData(self.stock_code, last_day, result) + if len(result['time']) > 0: + break + self.getDBData(self.stock_code, today, result) # 분석을 통해서 볼린저밴드 상/하단을 계산한다. data = self.buySellChecker.analyze(result) @@ -184,29 +212,20 @@ class Simulation: if __name__ == "__main__": + PROJECT_HOME = os.path.join(os.path.dirname(__file__)) + RESOURCE_PATH = os.path.join(PROJECT_HOME, "resources") + # to check bying stock_codes = { # 252670 # 122630 - "122630": [ - - ('20220722', '20220725') - ], + "122630": ['20220725'], } - path = 'resources/hts' - """ - path = './hts/backup' - fileList = glob(path + '/122630*.csv') - fileList = sorted(fileList, reverse=True) - stock_codes = {'122630':[]} - for i in range(11, 21): - stock_codes['122630'].append((fileList[i][20:28], fileList[i-1][20:28])) - """ for stock_code in stock_codes: - simulation = Simulation(stock_code) + simulation = Simulation(RESOURCE_PATH, stock_code) for given_day in stock_codes[stock_code]: - simulation.simulate(given_day, path) + simulation.simulate(given_day) print ("done...") diff --git a/resources/hts.db b/resources/hts.db index 7992199..9e60fbc 100644 Binary files a/resources/hts.db and b/resources/hts.db differ diff --git a/stock/util/DBManager.py b/stock/util/DBManager.py index f045096..f2590a9 100644 --- a/stock/util/DBManager.py +++ b/stock/util/DBManager.py @@ -35,7 +35,7 @@ class DBManager: stock_code = "252670" stock_name = "KODEX 200선물인버스2X" elif fileName.find("122630") >= 0: - stock_code = "252670" + stock_code = "122630" stock_name = "KODEX 레버리지" if stock_code is None or stock_name is None: continue