This commit is contained in:
dosangyoon
2022-07-30 16:08:37 +09:00
parent d230a51652
commit cddcdf4f35
6 changed files with 321 additions and 165 deletions

View File

@@ -1,20 +1,22 @@
from math import nan
from datetime import datetime, timedelta
import csv
import copy
import pandas as pd
import plotly.graph_objects as go
from plotly import subplots
import sqlite3
import os
from hts.HTS import HTS
from hts.BuySellChecker import BuySellChecker
class Simulation:
class Simulation (HTS):
buySellChecker = None
stock_code = None
def __init__(self, RESOURCE_PATH, stock_code):
super().__init__(RESOURCE_PATH)
self.buySellChecker = BuySellChecker()
self.stock_code = stock_code
@@ -22,59 +24,6 @@ class Simulation:
#self.connect()
return
def getCSV(self, fileName, given_day, result):
with open(fileName, 'r', encoding='utf-8') as infp:
reader = csv.reader(infp)
next(reader)
for rows in reader:
days = rows[0] # hts.날짜
time = rows[1] # hts.시간
open_v = rows[2] # hts.시가
high = rows[3] # hts.고가
low = rows[4] # hts.저가
close = rows[5] # hts.종가
vol = rows[6] # hts.거래량
start_time = datetime.strptime(given_day + " 090000", '%Y%m%d %H%M%S')
temp = datetime.strptime(str(days) + " " + str(time).zfill(4)+"00", '%Y%m%d %H%M%S')
#if temp < start_time:
# continue
result["time"].append(temp)
result["open"].append(int(open_v))
result["close"].append(int(close))
result["high"].append(int(high))
result["low"].append(int(low))
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:])]
@@ -180,30 +129,27 @@ class Simulation:
return
def getRealTime(self, stock_code, today, LAST_DATA=None):
if LAST_DATA is not None:
result = copy.deepcopy(LAST_DATA)
else:
result = {"check": set(), "time": [], "open": [], "close": [], "high": [], "low": [], "vol": []}
self.getDBData(stock_code, today, result)
return result
def simulate(self, today):
result = {"check": set(),
"time": [],
"open": [],
"close": [],
"high": [],
"low": [],
"vol": []}
LAST_DATA = self.getLastData(self.stock_code, today)
# 데이터를 가지고 온다.
#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)
result = self.getRealTime(self.stock_code, today, LAST_DATA)
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyze(result)
# 규칙 기반의 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyzeByRule(result)
# 사야 할 시점과 팔아야 할 시점을 체크한다.
bsLine = self.buySellChecker.checkTransaction(data, self.stock_code)
bsLine, data = self.buySellChecker.checkTransaction(data, self.stock_code, False)
# 그래프를 그린다.
self.draw(self.stock_code, today, data, bsLine)