init
This commit is contained in:
@@ -120,105 +120,104 @@ class BuySellChecker():
|
||||
df_signal = data_signal.loc[df_tmp]
|
||||
si = len(df_signal) - 1
|
||||
|
||||
"""
|
||||
duration = 5
|
||||
if duration < i:
|
||||
if sum(data['avg20'][i - duration:i])/len(data['avg20'][i - duration:i]) < data['avg20'][i]:
|
||||
min_value1 = min(data['close'][i - 1], data['close'][i - 1])
|
||||
min_value2 = min(data['close'][i - 2], data['close'][i - 2])
|
||||
min_value3 = min(data['close'][i - 3], data['close'][i - 3])
|
||||
min_sum = min_value1 + min_value2 + min_value3
|
||||
if min_sum / 3 < data['close'][i] and data['close'][i] == data['high'][i]:
|
||||
if data['avg60'][i] < data['avg20'][i] and data['avg5'][i-1] < data['avg5'][i]:
|
||||
if data['middle'][i-1] < data['middle'][i]:
|
||||
if 0 < len(BUY_LIST['buy_list']):
|
||||
if BUY_LIST['buy_list'][-1]['buy_price'] < data['close'][i]:
|
||||
buy_price = data['close'][i]
|
||||
buy_type = 'avg20_close_up'
|
||||
buy_ymd = data['ymd'][i]
|
||||
buy_cut = -1
|
||||
if data['slow_k'][si] < 30:
|
||||
buy_count = MAX_BUY_PRICE / (1 * data['close'][i])
|
||||
elif data['slow_k'][si] < 50:
|
||||
buy_count = MAX_BUY_PRICE / (1.5 * data['close'][i])
|
||||
else:
|
||||
buy_count = MAX_BUY_PRICE / (2 * data['close'][i])
|
||||
|
||||
return buy_ymd, buy_price, buy_count, buy_cut, buy_type
|
||||
else:
|
||||
buy_price = data['close'][i]
|
||||
buy_type = 'avg20_close_up'
|
||||
buy_ymd = data['ymd'][i]
|
||||
buy_cut = -1
|
||||
if data['slow_k'][si] < 30:
|
||||
buy_count = MAX_BUY_PRICE / (1 * data['close'][i])
|
||||
elif data['slow_k'][si] < 50:
|
||||
buy_count = MAX_BUY_PRICE / (1.5 * data['close'][i])
|
||||
else:
|
||||
buy_count = MAX_BUY_PRICE / (2 * data['close'][i])
|
||||
|
||||
return buy_ymd, buy_price, buy_count, buy_cut, buy_type
|
||||
"""
|
||||
|
||||
duration = 5
|
||||
check = False
|
||||
duration = 5 + 60
|
||||
if duration < i:
|
||||
if np.average(data['trend_avg'][i - duration:i]) < data['trend_avg'][i]:
|
||||
if self.is_Support(data, i-10, observation_time = 300):
|
||||
if data['open'][i] < data['close'][i]:
|
||||
if np.max(data['high'][i-2:i]) < data['close'][i]:
|
||||
|
||||
buy_price = data['close'][i]
|
||||
buy_type = 'support_300'
|
||||
buy_ymd = data['ymd'][i]
|
||||
buy_cut = data['close'][i] * 0.995
|
||||
BUY_LIST['buy_limit'] = 0
|
||||
if data['slow_k'][si] < 30:
|
||||
buy_count = MAX_BUY_PRICE*5 / (data['close'][i])
|
||||
elif data['slow_k'][si] < 50:
|
||||
buy_count = MAX_BUY_PRICE*4 / (data['close'][i])
|
||||
if np.average(data['avg480'][i - duration:i]) < data['avg480'][i]:
|
||||
if data['avg480'][i] < data['trend_avg'][i]:
|
||||
if data['avg20'][i] < data['avg480'][i] and data['avg20'][i - 1] < data['avg20'][i]:
|
||||
if len(BUY_LIST['buy_list']) == 0:
|
||||
check = True
|
||||
else:
|
||||
buy_count = MAX_BUY_PRICE*3 / (data['close'][i])
|
||||
if BUY_LIST['buy_list'][-1]['buy_price'] < data['close'][i]:
|
||||
check = True
|
||||
|
||||
return buy_ymd, buy_price, buy_count, buy_cut, buy_type
|
||||
if 2800 < len(data['close'][i - 2880:i]) and np.max(data['close'][i - 2880:i]) < data['close'][i]:
|
||||
if np.max(data['rsi'][i - 30:i]) < data['rsi'][i]:
|
||||
if data['disparity_avg1500'][i] < 1.1:
|
||||
if data['close'][i] < data['trend_avg'][i]:
|
||||
buy_type = 'upward'
|
||||
check = True
|
||||
|
||||
if data['slow_k'][i] < 15:
|
||||
if data['slow_k'][i-1] < data['slow_d'][i-1] and data['slow_d'][i] < data['slow_k'][i]:
|
||||
buy_price = data['close'][i]
|
||||
buy_type = 'slow_k'
|
||||
buy_ymd = data['ymd'][i]
|
||||
buy_cut = data['close'][i] * 0.995
|
||||
BUY_LIST['buy_limit'] = 0
|
||||
buy_count = MAX_BUY_PRICE * 2 / (data['close'][i])
|
||||
return buy_ymd, buy_price, buy_count, buy_cut, buy_type
|
||||
if data['disparity_avg20'][i] < BUY_LIST['disparity']['limit_bottom_3']['avg20']:
|
||||
buy_type = 'disparity_avg20'
|
||||
check = True
|
||||
|
||||
if data['disparity_avg60'][i] < BUY_LIST['disparity']['limit_bottom_3']['avg60']:
|
||||
buy_type = 'disparity_avg60'
|
||||
check = True
|
||||
|
||||
if data['disparity_avg480'][i] < BUY_LIST['disparity']['limit_bottom_3']['avg480']:
|
||||
buy_type = 'disparity_avg1500'
|
||||
check = True
|
||||
|
||||
if data['disparity_avg1500'][i] < BUY_LIST['disparity']['limit_bottom_3']['avg1500']:
|
||||
buy_type = 'disparity_avg1500'
|
||||
check = True
|
||||
|
||||
if data['disparity_avg20'][i] < BUY_LIST['disparity']['limit_bottom_1']['avg20']:
|
||||
buy_type = 'disparity_avg20'
|
||||
check = True
|
||||
|
||||
if data['disparity_avg60'][i] < BUY_LIST['disparity']['limit_bottom_1']['avg60']:
|
||||
buy_type = 'disparity_avg60'
|
||||
check = True
|
||||
|
||||
if data['disparity_avg480'][i] < BUY_LIST['disparity']['limit_bottom_1']['avg480']:
|
||||
buy_type = 'disparity_avg1500'
|
||||
check = True
|
||||
|
||||
if data['disparity_avg1500'][i] < BUY_LIST['disparity']['limit_bottom_1']['avg1500']:
|
||||
buy_type = 'disparity_avg1500'
|
||||
check = True
|
||||
|
||||
if check:
|
||||
buy_price = data['close'][i]
|
||||
buy_ymd = data['ymd'][i]
|
||||
if data['slow_k'][si] < 30:
|
||||
buy_count = MAX_BUY_PRICE * 2 / (data['close'][i])
|
||||
elif data['slow_k'][si] < 50:
|
||||
buy_count = MAX_BUY_PRICE * 1.5 / (data['close'][i])
|
||||
else:
|
||||
buy_count = MAX_BUY_PRICE * 1 / (data['close'][i])
|
||||
|
||||
return buy_ymd, buy_price, buy_count, buy_cut, buy_type
|
||||
|
||||
return buy_ymd, buy_price, buy_count, buy_cut, buy_type
|
||||
|
||||
def getSellPriceAndWeight1(self, ticker, i, data, data_signal, BUY_LIST=None):
|
||||
sell_price, sell_count, sell_type = -1, -1, ''
|
||||
|
||||
df_tmp = data_signal['ymd'] <= data['ymd'][i]
|
||||
df_signal = data_signal.loc[df_tmp]
|
||||
si = len(df_signal) - 1
|
||||
|
||||
check = False
|
||||
if 0 < len(BUY_LIST['buy_list']):
|
||||
duration = 5
|
||||
if duration < i:
|
||||
if data['trend_avg'][i] < np.average(data['trend_avg'][i - duration:i]):
|
||||
if self.is_Resistance(data, i - 10, observation_time=300):
|
||||
sell_price = data['close'][i]
|
||||
sell_count = sum([price['buy_count'] for price in BUY_LIST['buy_list']])
|
||||
|
||||
if 75 < np.max(data_signal['rsi'][si-5:si]):
|
||||
if self.is_Resistance(data, i - 10, observation_time=300):
|
||||
sell_price = data['close'][i]
|
||||
sell_count = sum([price['buy_count'] for price in BUY_LIST['buy_list']])
|
||||
"""
|
||||
if 1.05 < data['disparity_avg20'][i]:
|
||||
check = True
|
||||
|
||||
if 1.10 < data['disparity_avg480'][i]:
|
||||
check = True
|
||||
|
||||
if 70 < data['slow_k'][i]:
|
||||
if data['slow_d'][i-1] < data['slow_k'][i-1] and data['slow_k'][i] <= data['slow_d'][i]:
|
||||
sell_price = data['close'][i]
|
||||
sell_count = sum([price['buy_count'] for price in BUY_LIST['buy_list'] if price['buy_type'] == 'slow_k'])
|
||||
sell_type = 'slow_k'
|
||||
if 1.15 < data['disparity_avg1500'][i]:
|
||||
check = True
|
||||
"""
|
||||
|
||||
if BUY_LIST['disparity']['limit_top_1']['avg20'] < data['disparity_avg20'][i]:
|
||||
check = True
|
||||
|
||||
if BUY_LIST['disparity']['limit_top_1']['avg480'] < data['disparity_avg480'][i]:
|
||||
check = True
|
||||
|
||||
if BUY_LIST['disparity']['limit_top_1']['avg1500'] < data['disparity_avg1500'][i]:
|
||||
check = True
|
||||
|
||||
if data['avg1500'][i - 1] < data['trend_avg'][i - 1] and data['trend_avg'][i] <= data['avg1500'][i]:
|
||||
check = True
|
||||
|
||||
if check:
|
||||
sell_price = data['close'][i]
|
||||
sell_count = sum([price['buy_count'] for price in BUY_LIST['buy_list']])
|
||||
|
||||
return sell_price, sell_count, sell_type
|
||||
|
||||
@@ -268,7 +267,7 @@ class BuySellChecker():
|
||||
sell_price, sell_count, sell_type = self.getSellPriceAndWeight1(ticker, last_index, data, data_signal, BUY_LIST)
|
||||
bsLine['sell_price'][last_index] = sell_price
|
||||
bsLine['sell_count'][last_index] = sell_count
|
||||
bsLine['sell_type'] = [sell_type]
|
||||
bsLine['sell_type'][last_index] = sell_type
|
||||
|
||||
if 0 < sell_price:
|
||||
BUY_LIST['buy_limit'] = 0
|
||||
|
||||
107
hts/HTS.py
107
hts/HTS.py
@@ -9,6 +9,7 @@ import sqlite3
|
||||
from datetime import datetime, timedelta
|
||||
from hts.OrderItem import OrderItem
|
||||
from stock.util.TelegramBot import TelegramBot
|
||||
from stock.analysis.MovingAverage import MovingAverage
|
||||
|
||||
class HTS:
|
||||
|
||||
@@ -474,7 +475,7 @@ class HTS:
|
||||
def insertStockData(self, this_day, stock_code, stock_name=''):
|
||||
|
||||
# 테이블 생성
|
||||
self.cursor.execute("CREATE TABLE IF NOT EXISTS hts (CODE text, NAME text, ymd text, hms text, close REAL, open REAL, high REAL, low REAL, volume REAL, label INTEGER DEFAULT 0)")
|
||||
self.cursor.execute("CREATE TABLE IF NOT EXISTS hts (CODE text, NAME text, ymd text, hms text, close REAL, open REAL, high REAL, low REAL, volume REAL, disparity_avg5 REAL, disparity_avg20 REAL, disparity_avg60 REAL, disparity_avg120 REAL, disparity_avg240 REAL, disparity_avg480 REAL, disparity_avg1500 REAL)")
|
||||
|
||||
# 키 생성
|
||||
create_key = "CREATE INDEX IF NOT EXISTS hts_idx on hts(CODE, ymd, hms) "
|
||||
@@ -497,6 +498,49 @@ class HTS:
|
||||
self.conn.commit()
|
||||
return
|
||||
|
||||
def getQ(self):
|
||||
q_5 = MovingAverage(5)
|
||||
q_20 = MovingAverage(20)
|
||||
q_60 = MovingAverage(60)
|
||||
q_120 = MovingAverage(120)
|
||||
q_240 = MovingAverage(240)
|
||||
q_480 = MovingAverage(480)
|
||||
q_1500 = MovingAverage(1500)
|
||||
q = {'q_5': q_5, 'q_20': q_20, 'q_60': q_60, 'q_120': q_120, 'q_240': q_240, 'q_480': q_480, 'q_1500': q_1500}
|
||||
|
||||
return q
|
||||
|
||||
def updateDisparity(self, ticker_code):
|
||||
|
||||
self.cursor.execute('SELECT ymd, hms, close FROM hts WHERE CODE=? order by ymd, hms', (ticker_code,))
|
||||
|
||||
q = self.getQ()
|
||||
db_result = self.cursor.fetchall()
|
||||
for rows in db_result:
|
||||
ymd = rows[0]
|
||||
hms = rows[1]
|
||||
close = rows[2]
|
||||
|
||||
q['q_5'].enqueue(close)
|
||||
q['q_20'].enqueue(close)
|
||||
q['q_60'].enqueue(close)
|
||||
q['q_120'].enqueue(close)
|
||||
q['q_240'].enqueue(close)
|
||||
q['q_480'].enqueue(close)
|
||||
q['q_1500'].enqueue(close)
|
||||
|
||||
disparity_avg5 = close / q['q_5'].avg()
|
||||
disparity_avg20 = close / q['q_20'].avg()
|
||||
disparity_avg60 = close / q['q_60'].avg()
|
||||
disparity_avg120 = close / q['q_120'].avg()
|
||||
disparity_avg240 = close / q['q_240'].avg()
|
||||
disparity_avg480 = close / q['q_480'].avg()
|
||||
disparity_avg1500 = close / q['q_1500'].avg()
|
||||
|
||||
self.cursor.execute( "update hts set disparity_avg5=?, disparity_avg20=?, disparity_avg60=?, disparity_avg120=?, disparity_avg240=?, disparity_avg480=?, disparity_avg1500=? where CODE=? and ymd=? and hms=?", (disparity_avg5, disparity_avg20, disparity_avg60, disparity_avg120, disparity_avg240, disparity_avg480, disparity_avg1500, ticker_code, ymd, hms, ))
|
||||
self.conn.commit()
|
||||
|
||||
return
|
||||
|
||||
def write(self, day, result):
|
||||
#날짜,시간,시가,고가,저가,종가,거래량
|
||||
@@ -516,67 +560,6 @@ class HTS:
|
||||
outFp.close()
|
||||
return
|
||||
|
||||
def getCSV(self, fileName, result):
|
||||
with open(fileName, 'r', encoding='utf-8') as infp:
|
||||
reader = csv.reader(infp)
|
||||
next(reader)
|
||||
|
||||
for rows in reader:
|
||||
days = rows[0] # hts.날짜
|
||||
ymd = rows[1] # hts.시간
|
||||
open_v = rows[2] # hts.시가
|
||||
high = rows[3] # hts.고가
|
||||
low = rows[4] # hts.저가
|
||||
close = rows[5] # hts.종가
|
||||
vol = rows[6] # hts.거래량
|
||||
|
||||
temp = datetime.strptime(str(days) + " " + str(ymd).zfill(4) + "00", '%Y%m%d %H%M%S')
|
||||
#if temp < start_time:
|
||||
# continue
|
||||
|
||||
result["ymd"].append(temp)
|
||||
result["open"].append(int(open_v))
|
||||
result["close"].append(int(close))
|
||||
result["high"].append(int(high))
|
||||
result["low"].append(int(low))
|
||||
result["volume"].append(int(volume))
|
||||
return
|
||||
|
||||
def updateLabel(self, stock_code, bsLine, data, ymd):
|
||||
|
||||
self.cursor.execute('Update hts set label=? WHERE CODE=? and ymd=?', (0, stock_code, ymd,))
|
||||
|
||||
for i in range(len(bsLine["buy"])):
|
||||
if bsLine["buy"][i] > 0:
|
||||
ymd = data['date'][i].strftime('%Y%m%d')
|
||||
hms = data['date'][i].strftime('%H%M')
|
||||
self.cursor.execute('Update hts set label=? WHERE CODE=? and ymd=? and hms=?', (2, stock_code, ymd, hms))
|
||||
|
||||
for i in range(len(bsLine["sell"])):
|
||||
if bsLine["sell"][i] > 0:
|
||||
ymd = data['date'][i].strftime('%Y%m%d')
|
||||
hms = data['date'][i].strftime('%H%M')
|
||||
self.cursor.execute('Update hts set label=? WHERE CODE=? and ymd=? and hms=?', (1, stock_code, ymd, hms))
|
||||
self.conn.commit()
|
||||
|
||||
print("update...", stock_code, ymd)
|
||||
return
|
||||
|
||||
def clearLabel(self, stock_code, ymd):
|
||||
|
||||
self.cursor.execute('update hts set label=? WHERE CODE=? and ymd=? ', (0, stock_code, ymd,))
|
||||
self.conn.commit()
|
||||
print("update...", stock_code, ymd)
|
||||
return
|
||||
|
||||
def makeLabel(self, stock_code, ymd, hms, label):
|
||||
|
||||
self.cursor.execute('Update hts set label=? WHERE CODE=? and ymd=? and hms=?', (label, stock_code, ymd, hms,))
|
||||
self.conn.commit()
|
||||
|
||||
print("update...", stock_code, ymd, hms, label)
|
||||
return
|
||||
|
||||
def getYMD(self, stock_code):
|
||||
|
||||
result = []
|
||||
|
||||
Reference in New Issue
Block a user