init
This commit is contained in:
@@ -1,225 +1,6 @@
|
||||
import time
|
||||
import os
|
||||
import math
|
||||
import sqlite3
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from hts.HTS import HTS
|
||||
from hts.OrderType import OrderType
|
||||
|
||||
from hts.BuySellChecker import BuySellChecker
|
||||
from hts.OrderChecker import OrderChecker
|
||||
from stock.util.LabelChecker import LabelChecker
|
||||
from stock.util.SlackBot import SlackBot
|
||||
from stock.analysis.StockStatus import StockStatus
|
||||
|
||||
|
||||
class HTS_etf(HTS):
|
||||
RESOURCE_PATH = None
|
||||
SELL_GAP = None
|
||||
stock_code = None
|
||||
stock_name = None
|
||||
buy_count = None
|
||||
orderChecker = None
|
||||
buySellChecker = None
|
||||
labelChecker = None
|
||||
slackBot = None
|
||||
stockStatus = None
|
||||
|
||||
def __init__(self, RESOURCE_PATH, stock_code, stock_name, SELL_GAP):
|
||||
super().__init__(RESOURCE_PATH)
|
||||
|
||||
self.RESOURCE_PATH = RESOURCE_PATH
|
||||
|
||||
self.SELL_GAP = SELL_GAP
|
||||
self.stock_code = stock_code
|
||||
self.stock_name = stock_name
|
||||
self.orderChecker = OrderChecker(self.RESOURCE_PATH, self.stock_code)
|
||||
self.buySellChecker = BuySellChecker()
|
||||
self.labelChecker = LabelChecker(RESOURCE_PATH)
|
||||
self.slackBot = SlackBot()
|
||||
self.stockStatus = StockStatus(RESOURCE_PATH)
|
||||
|
||||
return
|
||||
|
||||
|
||||
def getTodayRSI(self, dbfile_name="stock.db"):
|
||||
try:
|
||||
conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, dbfile_name))
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute('SELECT rsi FROM stock_analysis WHERE CODE=? order by ymd desc', (self.stock_code,))
|
||||
db_result = cursor.fetchall()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
if db_result is not None:
|
||||
return db_result[0][0]
|
||||
except:
|
||||
return 100
|
||||
return 100
|
||||
|
||||
def sellStocks(self, stock_code=None, bs_sell_price=None):
|
||||
check = False
|
||||
jangoDic = self.requstJango()
|
||||
if jangoDic and len(jangoDic.keys()) > 0:
|
||||
for code in jangoDic:
|
||||
if stock_code is not None:
|
||||
if code == "A" + stock_code:
|
||||
if bs_sell_price is not None:
|
||||
if jangoDic[code]['매도가능'] > 0:
|
||||
if jangoDic[code]['평가손익'] < -1.5 or 1 < jangoDic[code]['평가손익'] or self.SELL_GAP < jangoDic[code]['평가금액']-jangoDic[code]['매입금액']:
|
||||
# 1.5% 손해 혹은 2% 이상 시 수익 매도
|
||||
self.requestOrder(OrderType.sell, code[1:], jangoDic[code]['매도가능'], bs_sell_price)
|
||||
check = True
|
||||
else:
|
||||
if jangoDic[code]['매도가능'] > 0:
|
||||
if jangoDic[code]['평가손익'] < -1.5 or 1 < jangoDic[code]['평가손익'] or self.SELL_GAP < jangoDic[code]['평가금액']-jangoDic[code]['매입금액']:
|
||||
# 1.5% 손해 혹은 2% 이상 시 수익 매도
|
||||
self.requestOrder(OrderType.sell, code[1:], jangoDic[code]['매도가능'], jangoDic[code]['현재가'])
|
||||
check = True
|
||||
else:
|
||||
if jangoDic[code]['매도가능'] > 0:
|
||||
if jangoDic[code]['평가손익'] < -1.5 or 1 < jangoDic[code]['평가손익'] or self.SELL_GAP < jangoDic[code]['평가금액']-jangoDic[code]['매입금액']:
|
||||
# 1.5% 손해 혹은 2% 이상 시 수익 매도
|
||||
self.requestOrder(OrderType.sell, code[1:], jangoDic[code]['매도가능'], jangoDic[code]['현재가'])
|
||||
check = True
|
||||
return check
|
||||
|
||||
def getSellingPrice(self, log_time, stock_code, final_price, without_loss=False):
|
||||
# final_price와 diff를 받으면, 해당 가격으로 그냥 매도한다는 의미
|
||||
# final_price와 diff가 None이면 장부가와 final 중 max로 팔겠다는 의미
|
||||
# final_price가 0이고 diff가 None이면 장부가로 팔겠다는 의미임
|
||||
orderNum = None
|
||||
jangoDic = self.requstJango()
|
||||
if jangoDic and len(jangoDic.keys()) > 0:
|
||||
for code in jangoDic:
|
||||
if jangoDic[code]['매도가능'] > 0:
|
||||
if without_loss:
|
||||
if jangoDic[code]['장부가'] * 0.07 < jangoDic[code]['장부가'] - final_price:
|
||||
sell_price = jangoDic[code]['장부가']
|
||||
if code == "A" + stock_code:
|
||||
orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price)
|
||||
return orderNum, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price
|
||||
else:
|
||||
max_price = max(jangoDic[code]['장부가'], final_price)
|
||||
sell_price = (int(max_price) - int(max_price) % 5) + 5
|
||||
if code == "A" + stock_code:
|
||||
orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price)
|
||||
return orderNum, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price
|
||||
return orderNum, None, None, None
|
||||
|
||||
def makeTickData(self, data, mins=30):
|
||||
result = {"check": set(),
|
||||
"time": [],
|
||||
"open": [],
|
||||
"close": [],
|
||||
"high": [],
|
||||
"low": [],
|
||||
"vol": [],
|
||||
"label": []}
|
||||
|
||||
for i in range(mins, len(data['time']) + 1):
|
||||
result["check"].add(data['time'][i - 1])
|
||||
result["time"].append(data['time'][i - 1])
|
||||
|
||||
result["open"].append(data['open'][i - mins])
|
||||
result["close"].append(data['close'][i - 1])
|
||||
result["high"].append(max(data['high'][i - mins: i]))
|
||||
result["low"].append(min(data['low'][i - mins: i]))
|
||||
result["vol"].append(sum(data['vol'][i - mins: i]))
|
||||
|
||||
return result
|
||||
|
||||
def buyRealTime(self, today, analyzed_day=1000, MAX_PRICE = 100000, logFp=None):
|
||||
rsi = self.getTodayRSI()
|
||||
if rsi > 79:
|
||||
self.slackBot.sendMsg("exit... {} ({}) RSI: {}".format(self.stock_code, self.stock_name, rsi))
|
||||
return
|
||||
|
||||
print("START...")
|
||||
THIS_TIME = datetime.now()
|
||||
|
||||
LAST_DATA = self.getLastData(self.stock_code, today)
|
||||
|
||||
while datetime.strptime(today + " 063000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 153100",'%Y%m%d %H%M%S'):
|
||||
if THIS_TIME < datetime.strptime(today + " 085500", '%Y%m%d %H%M%S'):
|
||||
self.slackBot.sendMsg("WAIT... {} ({}) RSI: {}".format(self.stock_code, self.stock_name, rsi))
|
||||
if datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 090100", '%Y%m%d %H%M%S'):
|
||||
self.slackBot.sendMsg("START... {} ({}) RSI: {}".format(self.stock_code, self.stock_name, rsi))
|
||||
logFp.write("START {} ({}) RSI: {}\n".format(self.stock_code, self.stock_name, rsi))
|
||||
|
||||
if datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151500", '%Y%m%d %H%M%S'):
|
||||
|
||||
# 매도를 체크한다.
|
||||
self.sellStocks(self.stock_code)
|
||||
|
||||
time.sleep(0.1)
|
||||
|
||||
try:
|
||||
# 데이터를 가지고 온다.
|
||||
result = self.getRealTime(self.stock_code, today, LAST_DATA)
|
||||
except:
|
||||
print("#ERROR:", self.stock_code)
|
||||
continue
|
||||
|
||||
data = self.buySellChecker.analyze(result)
|
||||
data.drop(data.index[:len(data) - analyzed_day], inplace=True)
|
||||
|
||||
# 사야 할 시점과 팔아야 할 시점을 체크한다.
|
||||
bsLine = self.buySellChecker.checkTransaction(self.stock_code, data, None, None, isRealTime=True)
|
||||
bs_buy_price = bsLine['buy'][0]
|
||||
bs_buy_weight = bsLine['buy_weight'][0]
|
||||
bs_sell_price = bsLine['sell'][0]
|
||||
|
||||
# 미체결 기록을 가져와서 10분 이상 된 매수 주문을 취소 한다.
|
||||
ORDER_LIST = self.requestOrderList()
|
||||
orderListToCancel = self.orderChecker.cancel(today, "A" + self.stock_code, ORDER_LIST, mins=10)
|
||||
if len(orderListToCancel) > 0:
|
||||
self.cancelOrderList(orderListToCancel)
|
||||
|
||||
if bs_buy_price > 1000:
|
||||
|
||||
#if not self.orderChecker.exist(today, "A" + self.stock_code, hours=9):
|
||||
buy_count = int(MAX_PRICE / bs_buy_price)
|
||||
|
||||
if buy_count > 0:
|
||||
# 매수를 주문한다.
|
||||
orderNum = self.requestOrder(OrderType.buy, self.stock_code, buy_count, bs_buy_price)
|
||||
self.orderChecker.buy(today, "A" + self.stock_code, buy_count, bs_buy_price, orderNum)
|
||||
|
||||
# 로그 출력
|
||||
print("BUY", THIS_TIME.strftime('%Y%m%d %H%M%S'), orderNum, self.stock_code, bs_buy_price, buy_count)
|
||||
logFp.write("{} BUY {} {} {}\n".format(THIS_TIME.strftime('%Y%m%d %H%M%S'), self.stock_code, bs_buy_price, buy_count))
|
||||
|
||||
if bs_sell_price > 1000:
|
||||
check = self.sellStocks(self.stock_code, bs_sell_price)
|
||||
|
||||
if check:
|
||||
# 로그 출력
|
||||
print("SELL", THIS_TIME.strftime('%Y%m%d %H%M%S'), self.stock_code, self.stock_name, bs_sell_price)
|
||||
logFp.write("{} SELL {} {} {}\n".format(THIS_TIME.strftime('%Y%m%d %H%M%S'), self.stock_code, bs_buy_price, bs_sell_price))
|
||||
|
||||
# 로그 출력
|
||||
print("TIMECHECK: %s, code: %s, buy: %d, sell: %d, open: %d, close: %d, high: %d, low: %d, macd: %.2f" %
|
||||
(str(THIS_TIME), self.stock_code, bs_buy_price, bs_sell_price, data["open"][len(data["open"])-1], data["close"][len(data["close"])-1], data["high"][len(data["high"])-1], data["low"][len(data["low"])-1], data["macd"][len(data["macd"])-1]))
|
||||
logFp.write("TIMECHECK: %s, code: %s, buy: %d, sell: %d, open: %d, close: %d, high: %d, low: %d, macd: %.2f\n" %
|
||||
(str(THIS_TIME), self.stock_code, bs_buy_price, bs_sell_price, data["open"][len(data["open"])-1], data["close"][len(data["close"])-1], data["high"][len(data["high"])-1], data["low"][len(data["low"])-1], data["macd"][len(data["macd"])-1]))
|
||||
|
||||
if (int(THIS_TIME.strftime("%M")) % 50 == 0 or int(THIS_TIME.strftime("%M")) % 20 == 0):
|
||||
self.slackBot.post_live_to_slack(self.stock_code, self.stock_name, data["close"][len(data["close"])-1], data["macd"][len(data["macd"])-1])
|
||||
|
||||
logFp.flush()
|
||||
time.sleep(60)
|
||||
THIS_TIME = datetime.now()
|
||||
|
||||
return True
|
||||
|
||||
def updteTodayStock(self, stock_code, today_str):
|
||||
bsLine, data = self.labelChecker.makeCandidate(stock_code, today_str)
|
||||
self.labelChecker.updateLabel(stock_code, bsLine, data, today_str)
|
||||
return
|
||||
|
||||
from datetime import datetime
|
||||
from HTS_etf import HTS_etf
|
||||
|
||||
if __name__ == "__main__":
|
||||
today = datetime.today()
|
||||
|
||||
Reference in New Issue
Block a user