init
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
from math import nan
|
||||
from datetime import datetime, timedelta
|
||||
import csv
|
||||
import pandas as pd
|
||||
import plotly.graph_objects as go
|
||||
from plotly import subplots
|
||||
@@ -19,110 +20,102 @@ class Simulation:
|
||||
return
|
||||
|
||||
def getCSV(self, fileName, given_day, result):
|
||||
data = pd.read_csv(fileName)
|
||||
with open(fileName, 'r') as infp:
|
||||
reader = csv.reader(infp)
|
||||
next(reader)
|
||||
|
||||
days = data.날짜
|
||||
time = data.시간
|
||||
open = data.시가
|
||||
close = data.종가
|
||||
high = data.고가
|
||||
low = data.저가
|
||||
vol = data.거래량
|
||||
start_time = datetime.strptime(given_day + " 090000", '%Y%m%d %H%M%S')
|
||||
for rows in reader:
|
||||
days = rows[0] # data.날짜
|
||||
time = rows[1] # data.시간
|
||||
open_v = rows[2] # data.시가
|
||||
high = rows[3] # data.고가
|
||||
low = rows[4] # data.저가
|
||||
close = rows[5] # data.종가
|
||||
vol = rows[6] # data.거래량
|
||||
start_time = datetime.strptime(given_day + " 090000", '%Y%m%d %H%M%S')
|
||||
|
||||
for i in range(len(data)):
|
||||
temp = datetime.strptime(str(days[i]) + " " + str(time[i]).zfill(4)+"00", '%Y%m%d %H%M%S')
|
||||
if temp < start_time:
|
||||
continue
|
||||
temp = datetime.strptime(str(days) + " " + str(time).zfill(4)+"00", '%Y%m%d %H%M%S')
|
||||
if temp < start_time:
|
||||
continue
|
||||
|
||||
if temp not in result["check"]:
|
||||
result["check"].add(temp)
|
||||
result["time"].append(temp)
|
||||
result["open"].append(open[i])
|
||||
result["close"].append(close[i])
|
||||
result["high"].append(high[i])
|
||||
result["low"].append(low[i])
|
||||
result["vol"].append(vol[i])
|
||||
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 checkTransaction(self, data):
|
||||
size = len(data["close"])
|
||||
|
||||
bsLine = {}
|
||||
bsLine['buy'] = [-1 for i in range(size)]
|
||||
bsLine['weight'] = [-1 for i in range(size)]
|
||||
bsLine['sell'] = [-1 for i in range(size)]
|
||||
|
||||
for i in range(6, size-5):
|
||||
if self.stock_code == "252670":
|
||||
buy, weight, sell = self.buySellChecker.getPriceAndWeight1(data, i)
|
||||
else:
|
||||
buy, weight, sell = self.buySellChecker.getPriceAndWeight2(data, i)
|
||||
bsLine['buy'][i] = buy
|
||||
bsLine['weight'][i] = weight
|
||||
bsLine['sell'][i] = sell
|
||||
|
||||
return bsLine
|
||||
|
||||
def draw(self, stock_code, given_day, data, bsLine):
|
||||
buy_line = bsLine['buy']
|
||||
sell_line = bsLine['sell']
|
||||
# 어제 데이터는 지운다.
|
||||
data = data.loc[pd.DatetimeIndex(data.index).day == int(given_day[6:])]
|
||||
buy_line = bsLine['buy'][381:]
|
||||
sell_line = bsLine['sell'][381:]
|
||||
#buy_line = bsLine['buy']
|
||||
#sell_line = bsLine['sell']
|
||||
|
||||
# 그래프 설정을 위한 변수를 생성한다.
|
||||
data['open'] = pd.to_numeric(data['open'])
|
||||
data['high'] = pd.to_numeric(data['high'])
|
||||
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'])
|
||||
data["rsi"] = pd.to_numeric(data['rsi'])
|
||||
data["rsis"] = pd.to_numeric(data['rsis'])
|
||||
data = data.astype({'open': 'int',
|
||||
'high': 'int',
|
||||
'low': 'int',
|
||||
'close': 'int',
|
||||
'volume': 'int',
|
||||
'avg3': 'float',
|
||||
'avg5': 'float',
|
||||
'avg10': 'float',
|
||||
'avg30': 'float',
|
||||
'avg60': 'float',
|
||||
'fast_k': 'float',
|
||||
'slow_k': 'float',
|
||||
'slow_d': 'float',
|
||||
'rsi': 'float',
|
||||
'rsis': 'float'
|
||||
})
|
||||
|
||||
buy_colors = []
|
||||
for i in range(len(buy_line)):
|
||||
if buy_line[i] < 0:
|
||||
buy_colors.append("#ffffff")
|
||||
buy_line[i] = data["lower"][0]
|
||||
buy_line[i] = nan
|
||||
else:
|
||||
buy_colors.append("#ff00ff")
|
||||
sell_colors = []
|
||||
for i in range(len(sell_line)):
|
||||
if sell_line[i] < 0:
|
||||
sell_colors.append("#ffffff")
|
||||
sell_line[i] = data["lower"][0]
|
||||
sell_line[i] = nan
|
||||
else:
|
||||
sell_colors.append("#00ced1")
|
||||
|
||||
# 그래프를 설정한다.
|
||||
buy_check = go.Scatter(x=data['date'], y=buy_line, mode='markers', name="buy", marker=dict(size=14, color=buy_colors, line_width=0))
|
||||
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')
|
||||
upper = go.Scatter(x=data['date'], y=data["upper"], name="upper", line_color='#000000')
|
||||
lower = go.Scatter(x=data['date'], y=data["lower"], name="lower", line_color='#000000')
|
||||
avg3 = go.Scatter(x=data['date'], y=data["avg3"], name="avg3", line_color='#000000')
|
||||
avg5 = go.Scatter(x=data['date'], y=data["avg5"], name="avg5", line_color='#00FF00')
|
||||
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')
|
||||
avg60 = go.Scatter(x=data['date'], y=data["avg60"], name="avg60", line_color='#008000')
|
||||
avg30 = go.Scatter(x=data['date'], y=data["avg30"], name="avg30", line_color='#FFA500')
|
||||
#avg60 = go.Scatter(x=data['date'], y=data["avg60"], name="avg60", line_color='#008000')
|
||||
|
||||
candle_stick = go.Candlestick(x=data['date'], open=data['open'], high=data['high'], low=data['low'], close=data['close'], increasing_line_color='red', decreasing_line_color='blue')
|
||||
volume_line = go.Scatter(x=data['date'], y=data["volume"], mode='lines', name='volume')
|
||||
fast_k_line = go.Scatter(x=data['date'], y=data["fast_k"], mode='lines', name='fast_k')
|
||||
#fast_k_line = go.Scatter(x=data['date'], y=data["fast_k"], mode='lines', name='fast_k')
|
||||
|
||||
macd_line = go.Scatter(x=data['date'], y=data["macd"], mode='lines', name='macd')
|
||||
macd_s_line = go.Scatter(x=data['date'], y=data["macds"], mode='lines', name='macds')
|
||||
macd_o_line = go.Scatter(x=data['date'], y=data["macdo"], mode='lines', name='macdo')
|
||||
|
||||
slow_k_line = go.Scatter(x=data['date'], y=data["slow_k"], mode='lines', name='slow_k')
|
||||
slow_d_line = go.Scatter(x=data['date'], y=data["slow_d"], mode='lines', name='slow_d')
|
||||
|
||||
rsi_line = go.Scatter(x=data['date'], y=data["rsi"], mode='lines', name='rsi')
|
||||
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, avg2, avg5, avg10, avg30, avg60, buy_check, sell_check]
|
||||
candle_data = [candle_stick, upper, lower, avg3, avg5, avg10, avg30, buy_check, sell_check]
|
||||
volume_data = [volume_line]
|
||||
stochastic_data = [fast_k_line, slow_k_line, slow_d_line]
|
||||
macd_data = [macd_line, macd_s_line, macd_o_line]
|
||||
stochastic_data = [slow_k_line, slow_d_line]
|
||||
rsi_data = [rsi_line, rsis_line]
|
||||
|
||||
# 그래프를 그린다.
|
||||
@@ -132,15 +125,17 @@ class Simulation:
|
||||
fig.show()
|
||||
"""
|
||||
|
||||
fig = subplots.make_subplots(rows=4, cols=1, subplot_titles=('캔들', "거래량", "스토캐스틱", "RSI"))
|
||||
fig = subplots.make_subplots(rows=5, cols=1, subplot_titles=('캔들', "거래량", "MACD", "스토캐스틱", "RSI"))
|
||||
for trace in candle_data:
|
||||
fig.append_trace(trace, 1, 1)
|
||||
for trace in volume_data:
|
||||
fig.append_trace(trace, 2, 1)
|
||||
for trace in stochastic_data:
|
||||
for trace in macd_data:
|
||||
fig.append_trace(trace, 3, 1)
|
||||
for trace in rsi_data:
|
||||
for trace in stochastic_data:
|
||||
fig.append_trace(trace, 4, 1)
|
||||
for trace in rsi_data:
|
||||
fig.append_trace(trace, 5, 1)
|
||||
#fig.update_xaxes(nticks=5)
|
||||
#fig.update_layout(height=1800, title=stock_code + "_" + given_day, xaxis_rangeslider_visible=False)
|
||||
|
||||
@@ -149,12 +144,12 @@ class Simulation:
|
||||
buy_count = len(df.loc[df["buy"] > 0])
|
||||
sell_count = len(df.loc[df["sell"] > 0])
|
||||
|
||||
fig.update_layout(height=1800, title=stock_code + "_" + given_day + "_" + str(buy_count)+","+str(sell_count))
|
||||
fig.update_layout(height=5000, title=stock_code + "_" + given_day + "_" + str(buy_count)+","+str(sell_count))
|
||||
fig.show()
|
||||
|
||||
return
|
||||
|
||||
def simulate(self, GIVEN_DAY):
|
||||
def simulate(self, days):
|
||||
result = {"check": set(),
|
||||
"time": [],
|
||||
"open": [],
|
||||
@@ -163,30 +158,31 @@ class Simulation:
|
||||
"low": [],
|
||||
"vol": []}
|
||||
|
||||
last_day = days[0]
|
||||
today = days[1]
|
||||
|
||||
# 데이터를 가지고 온다.
|
||||
self.getCSV("./data/"+self.stock_code+"_"+GIVEN_DAY+".csv", GIVEN_DAY, result)
|
||||
self.getCSV("./data/" + self.stock_code + "_" + last_day + ".csv", last_day, result)
|
||||
self.getCSV("./data/" + self.stock_code + "_" + today + ".csv", today, result)
|
||||
|
||||
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
|
||||
data = self.buySellChecker.analyze(result)
|
||||
|
||||
# 사야 할 시점과 팔아야 할 시점을 체크한다.
|
||||
bsLine = self.checkTransaction(data)
|
||||
bsLine = self.buySellChecker.checkTransaction(data, self.stock_code)
|
||||
|
||||
# 그래프를 그린다.
|
||||
self.draw(self.stock_code, GIVEN_DAY, data, bsLine)
|
||||
self.draw(self.stock_code, today, data, bsLine)
|
||||
|
||||
return
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
today = datetime.today()
|
||||
|
||||
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": ['20220520', '20220128', '20220121', '20220120', '20211026'],
|
||||
"122630": ['20211026', '20211025', '20211022', '20211021', '20211020']
|
||||
# "252670": ['20220620', '20220621', '20220622', '20220623', '20220624'],
|
||||
# "122630": ['20220620', '20220621', '20220622', '20220623', '20220624']
|
||||
"252670": [('20220620', '20220621')],
|
||||
"122630": [('20220620', '20220621')]
|
||||
}
|
||||
|
||||
for stock_code in stock_codes:
|
||||
|
||||
Reference in New Issue
Block a user