205 lines
9.2 KiB
Python
205 lines
9.2 KiB
Python
import numpy as np
|
|
from datetime import datetime
|
|
from stock.util.DBManager import DBManager
|
|
|
|
|
|
class BuySell_Minutely:
|
|
dBManager = None
|
|
|
|
def __init__(self, RESOURCE_PATH):
|
|
self.dBManager = DBManager(RESOURCE_PATH)
|
|
return
|
|
|
|
def getBuy_Count(self, ticker, price):
|
|
|
|
buy_count = ticker['MAX_BUY'] / price
|
|
|
|
if 'BUY_INFO' in ticker and "buy_amount" in ticker['BUY_INFO']:
|
|
amount = ticker['BUY_INFO']["buy_amount"]
|
|
if 1000000 < amount:
|
|
return 0
|
|
|
|
profit = (price * ticker['BUY_INFO']["buy_count"]) - amount
|
|
last_buy_count, last_buy_price = self.dBManager.getLastBuyInfo(ticker["ticker_code"])
|
|
if last_buy_count is not None and last_buy_price is not None:
|
|
if last_buy_price < price and 1000 < profit:
|
|
buy_count = 3 * last_buy_count
|
|
elif last_buy_price > price and 1000 < profit:
|
|
buy_count = 2 * last_buy_count
|
|
elif last_buy_price < price and 1000 > profit:
|
|
buy_count = 1.5 * last_buy_count
|
|
else:
|
|
buy_count = 1 * last_buy_count
|
|
|
|
if 'today_buy_type' in ticker and ticker['today_buy_type'] == 3:
|
|
buy_count *= 2
|
|
else:
|
|
buy_count = 1.5 * ticker['MAX_BUY'] / price
|
|
|
|
if 200000 < price * buy_count:
|
|
buy_count = 200000 / price
|
|
|
|
return buy_count
|
|
|
|
def getBuyPrice(self, ticker, data, data_scaled, i, BS=None):
|
|
buy_ymd, buy_price, buy_count, buy_weight, buy_type, buy_cut = None, 0, 0, 1, '', None
|
|
|
|
# buy_ymd, buy_price, buy_count, buy_type, buy_cut = self.getBuyPrice_PolyLine(ticker, data, data_scaled, i, BS)
|
|
#tmp_buy_ymd, tmp_buy_price, tmp_buy_count, tmp_buy_type, tmp_buy_cut = self.getBuyPrice_Candle(ticker, data, data_scaled, i, BS)
|
|
tmp_buy_ymd, tmp_buy_price, tmp_buy_count, tmp_buy_type, tmp_buy_cut = self.getBuyPrice_Slow(ticker, data, data_scaled, i,BS)
|
|
if 0 < tmp_buy_count:
|
|
buy_ymd = tmp_buy_ymd;
|
|
buy_price = tmp_buy_price;
|
|
buy_count = tmp_buy_count;
|
|
buy_type = tmp_buy_type;
|
|
buy_cut = tmp_buy_cut
|
|
|
|
if 0 < len(ticker['BUY_INFO']['buy_list']):
|
|
diff = (datetime.strptime(str(data['ymd'].iloc[i]), '%Y-%m-%d %H:%M:%S') - ticker['BUY_INFO']['buy_list'][-1]['buy_ymd'])
|
|
d = diff.days
|
|
s = diff.seconds
|
|
|
|
# 해당 종목에 대해서 10분 이내 매수 금지
|
|
if s < 15 * 60:
|
|
return None, 0, 0, '', None
|
|
|
|
return buy_ymd, buy_price, buy_count, buy_type, buy_cut
|
|
|
|
def getSellPrice(self, ticker, data, data_scaled, i, BS=None):
|
|
sell_price, sell_count, sell_type = 0, 0, ''
|
|
sell_type_list = []
|
|
|
|
"""
|
|
tmp_sell_price, tmp_sell_count, tmp_sell_type_list = self.getSelllPrice_Umbong(ticker, data, data_scaled, i, BS)
|
|
sell_count += tmp_sell_count
|
|
sell_type_list += tmp_sell_type_list
|
|
sell_price += tmp_sell_price
|
|
"""
|
|
|
|
if 0 < len(sell_type_list) or 0 < sell_price:
|
|
sell_type = ','.join(list(set(sell_type_list)))
|
|
|
|
return sell_price, sell_count, sell_type
|
|
|
|
""""""""""""""""""
|
|
""""""""""""""""""
|
|
|
|
def getBuyPrice_Slow(self, ticker, data, data_scaled, i, BS):
|
|
|
|
buy_ymd, buy_price, buy_count, buy_weight, buy_type, buy_cut = None, 0, 0, 1, '', None
|
|
|
|
check = False
|
|
|
|
if 5 < i:
|
|
"""
|
|
if data['poly_20'].iloc[i - 1] < data['poly_20'].iloc[i] and data_scaled['disparity_diff_60_20_rate'].iloc[i] < -0.5:
|
|
if data_scaled['macd_720'].iloc[i - 1] < data_scaled['macd_720'].iloc[i] and data_scaled['macd'].iloc[i - 1] < data_scaled['macd'].iloc[i]:
|
|
if data['avg10'].iloc[i] < data['avg5'].iloc[i]:
|
|
check = True
|
|
buy_price = data['close'][i] - 2 * ticker['unit']
|
|
buy_count = (buy_weight * ticker['MAX_BUY']) / data['close'][i]
|
|
buy_type = 'slowk_10'
|
|
# buy_cut = data['support'].iloc[i]
|
|
"""
|
|
|
|
if data["slowk_10"].iloc[i-1] < data["slowk_10"].iloc[i] < 20:
|
|
if data["slowk_10"].iloc[i-1] < data["slowd_10"].iloc[i-1] and data["slowd_10"].iloc[i] < data["slowk_10"].iloc[i]:
|
|
check = True
|
|
buy_price = data['close'][i] - 2 * ticker['unit']
|
|
buy_count = (buy_weight * ticker['MAX_BUY']) / data['close'][i]
|
|
buy_type = 'slowk_10'
|
|
# buy_cut = data['support'].iloc[i]
|
|
|
|
if check:
|
|
buy_ymd = data['ymd'].iloc[i]
|
|
buy_price = data['close'][i] - 2 * ticker['unit']
|
|
buy_count = (buy_weight * ticker['MAX_BUY']) / data['close'][i]
|
|
|
|
return buy_ymd, buy_price, buy_count, buy_type, buy_cut
|
|
|
|
|
|
""""""""""""""""""
|
|
""""""""""""""""""
|
|
|
|
def getBuyPrice_Candle(self, ticker, data, data_scaled, i, BS):
|
|
|
|
buy_ymd, buy_price, buy_count, buy_weight, buy_type, buy_cut = None, 0, 0, 1, '', None
|
|
|
|
check = False
|
|
|
|
if 60 < i:
|
|
if data_scaled['disparity_diff_20_5_rate'].iloc[i] < 1 and data_scaled['disparity_diff_60_20_rate'].iloc[i] < 1 and data_scaled['disparity_diff_120_20_rate'].iloc[i] < 1:
|
|
|
|
if data['slowk_1440'].iloc[i - 1] < data['slowd_1440'].iloc[i - 1]:
|
|
if data['slowd_1440'].iloc[i] < data['slowk_1440'].iloc[i] < 40:
|
|
check = True
|
|
buy_price = data['close'].iloc[i] - 2 * ticker['unit']
|
|
buy_count = self.getBuy_Count(ticker, data['close'].iloc[i])
|
|
buy_type = 'slowk_1440'
|
|
#buy_cut = data['support'].iloc[i]
|
|
|
|
if data['avg240'].iloc[i - 1] < data['avg240'].iloc[i]:
|
|
if data_scaled['poly_480'].iloc[i - 1] <= 0 and 0 < data_scaled['poly_480'].iloc[i]:
|
|
check = True
|
|
buy_price = data['close'].iloc[i] - 2 * ticker['unit']
|
|
buy_count = self.getBuy_Count(ticker, data['close'].iloc[i])
|
|
buy_type = 'poly_480'
|
|
#buy_cut = data['support'].iloc[i]
|
|
|
|
if data_scaled['poly_720'].iloc[i - 1] < data_scaled['poly_720'].iloc[i] and data['slowk_720'].iloc[i] < 50:
|
|
if data['close'].iloc[i - 1] < data['avg720'].iloc[i-1] and data['avg720'].iloc[i] < data['close'].iloc[i]:
|
|
check = True
|
|
buy_price = data['close'].iloc[i] - 2 * ticker['unit']
|
|
buy_count = self.getBuy_Count(ticker, data['close'].iloc[i])
|
|
buy_type = 'poly_720'
|
|
#buy_cut = data['support'].iloc[i]
|
|
|
|
if data_scaled['poly_1440'].iloc[i - 1] < data_scaled['poly_1440'].iloc[i] and data['slowk_1440'].iloc[i] < 50:
|
|
if data['close'].iloc[i - 1] < data['avg1440'].iloc[i-1] and data['avg1440'].iloc[i] < data['close'].iloc[i]:
|
|
check = True
|
|
buy_price = data['close'].iloc[i] - 2 * ticker['unit']
|
|
buy_count = self.getBuy_Count(ticker, data['close'].iloc[i])
|
|
buy_type = 'poly_1440'
|
|
#buy_cut = data['support'].iloc[i]
|
|
|
|
if check:
|
|
buy_ymd = data['ymd'].iloc[i]
|
|
buy_price = data['close'][i] - 2 * ticker['unit']
|
|
buy_count = (buy_weight * ticker['MAX_BUY']) / data['close'][i]
|
|
|
|
return buy_ymd, buy_price, buy_count, buy_type, buy_cut
|
|
|
|
""""""""""""""""""
|
|
""""""""""""""""""
|
|
|
|
def getSelllPrice_Umbong(self, ticker, data, data_scaled, i, BS):
|
|
sell_price, sell_count = 0, 0
|
|
sell_type_list = []
|
|
|
|
if 0 < len(ticker['BUY_INFO']['buy_list']):
|
|
check = False
|
|
sell_count = 0
|
|
|
|
if data['close'].iloc[i] < data['open'].iloc[i]:
|
|
for c in range(i - 1, i - 10, -1):
|
|
if data['open'].iloc[c] < data['close'].iloc[c] == data['high'].iloc[c]:
|
|
if data['close'].iloc[i] < data['open'].iloc[c]:
|
|
check = True
|
|
sell_count_1 = sum([price['buy_count'] for price in ticker['BUY_INFO']['buy_list'] if price['buy_type'] == "slowk_1440"])
|
|
if 0 < sell_count_1:
|
|
sell_type_list.append('slowk_1440')
|
|
|
|
sell_count_2 = sum([price['buy_count'] for price in ticker['BUY_INFO']['buy_list'] if price['buy_type'] == "poly_480"])
|
|
if 0 < sell_count_2:
|
|
sell_type_list.append('poly_480')
|
|
|
|
if "buy_amount" in ticker['BUY_INFO'] and ticker['BUY_INFO']["buy_amount"] < 50000:
|
|
sell_count = sell_count_1 + sell_count_2
|
|
else:
|
|
sell_count = (sell_count_1 + sell_count_2) * 0.8
|
|
|
|
if check and 0 < sell_count:
|
|
sell_price = data['close'].iloc[i] + 2 * ticker['unit']
|
|
|
|
return sell_price, sell_count, sell_type_list
|