This commit is contained in:
dosangyoon
2022-07-29 17:22:23 +09:00
parent 87d058ead4
commit d230a51652
3 changed files with 47 additions and 28 deletions

View File

@@ -1,10 +1,11 @@
from math import nan from math import nan
from datetime import datetime from datetime import datetime, timedelta
import csv import csv
import pandas as pd import pandas as pd
import plotly.graph_objects as go import plotly.graph_objects as go
from plotly import subplots from plotly import subplots
from glob import glob import sqlite3
import os
from hts.BuySellChecker import BuySellChecker from hts.BuySellChecker import BuySellChecker
@@ -13,10 +14,11 @@ class Simulation:
buySellChecker = None buySellChecker = None
stock_code = None stock_code = None
def __init__(self, stock_code): def __init__(self, RESOURCE_PATH, stock_code):
self.buySellChecker = BuySellChecker() self.buySellChecker = BuySellChecker()
self.stock_code = stock_code self.stock_code = stock_code
self.RESOURCE_PATH = RESOURCE_PATH
#self.connect() #self.connect()
return return
@@ -47,6 +49,32 @@ class Simulation:
result["vol"].append(int(vol)) result["vol"].append(int(vol))
return 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): def draw(self, stock_code, given_day, data, bsLine):
# 어제 데이터는 지운다. # 어제 데이터는 지운다.
data = data.loc[pd.DatetimeIndex(data.index).day == int(given_day[6:])] data = data.loc[pd.DatetimeIndex(data.index).day == int(given_day[6:])]
@@ -152,7 +180,7 @@ class Simulation:
return return
def simulate(self, days, path=None): def simulate(self, today):
result = {"check": set(), result = {"check": set(),
"time": [], "time": [],
"open": [], "open": [],
@@ -161,15 +189,15 @@ class Simulation:
"low": [], "low": [],
"vol": []} "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 + "_" + last_day + ".csv", last_day, result)
self.getCSV(path + "/" + self.stock_code + "_" + today + ".csv", today, 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) data = self.buySellChecker.analyze(result)
@@ -184,29 +212,20 @@ class Simulation:
if __name__ == "__main__": if __name__ == "__main__":
PROJECT_HOME = os.path.join(os.path.dirname(__file__))
RESOURCE_PATH = os.path.join(PROJECT_HOME, "resources")
# to check bying # to check bying
stock_codes = { stock_codes = {
# 252670 # 252670
# 122630 # 122630
"122630": [ "122630": ['20220725'],
('20220722', '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: for stock_code in stock_codes:
simulation = Simulation(stock_code) simulation = Simulation(RESOURCE_PATH, stock_code)
for given_day in stock_codes[stock_code]: for given_day in stock_codes[stock_code]:
simulation.simulate(given_day, path) simulation.simulate(given_day)
print ("done...") print ("done...")

Binary file not shown.

View File

@@ -35,7 +35,7 @@ class DBManager:
stock_code = "252670" stock_code = "252670"
stock_name = "KODEX 200선물인버스2X" stock_name = "KODEX 200선물인버스2X"
elif fileName.find("122630") >= 0: elif fileName.find("122630") >= 0:
stock_code = "252670" stock_code = "122630"
stock_name = "KODEX 레버리지" stock_name = "KODEX 레버리지"
if stock_code is None or stock_name is None: if stock_code is None or stock_name is None:
continue continue