This commit is contained in:
dsyoon
2023-01-31 00:01:31 +09:00
parent b6f2d6cbab
commit 8e7c3fdd81
3 changed files with 103 additions and 61 deletions

View File

@@ -34,7 +34,7 @@ class HTS_etf (HTS):
self.slackBot = SlackBot() self.slackBot = SlackBot()
self.stockStatus = StockStatus(RESOURCE_PATH) self.stockStatus = StockStatus(RESOURCE_PATH)
self.MAX_BUY_PRICE = 10000 self.MAX_BUY_PRICE = 100000
return return
@@ -42,25 +42,24 @@ class HTS_etf (HTS):
# final_price와 diff를 받으면, 해당 가격으로 그냥 매도한다는 의미 # final_price와 diff를 받으면, 해당 가격으로 그냥 매도한다는 의미
# final_price와 diff가 None이면 장부가와 final 중 max로 팔겠다는 의미 # final_price와 diff가 None이면 장부가와 final 중 max로 팔겠다는 의미
# final_price가 0이고 diff가 None이면 장부가로 팔겠다는 의미임 # final_price가 0이고 diff가 None이면 장부가로 팔겠다는 의미임
orderNum = None
sell_price = -1
jangoDic = self.requstJango() jangoDic = self.requstJango()
if jangoDic and len(jangoDic.keys()) > 0: if jangoDic and len(jangoDic.keys()) > 0:
for code in jangoDic: for code in jangoDic:
if jangoDic[code]['매도가능'] > 0: if jangoDic[code]['매도가능'] > 0:
if without_loss: if without_loss:
if jangoDic[code]['장부가']*0.05 < jangoDic[code]['장부가'] - final_price: if jangoDic[code]['장부가']*0.07 < jangoDic[code]['장부가'] - final_price:
sell_price = jangoDic[code]['장부가'] sell_price = jangoDic[code]['장부가']
if code == "A" + stock_code: if code == "A" + stock_code:
orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price) orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price)
print("ORDER_SELL", stock_code, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price) return orderNum, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price
else: else:
max_price = max(jangoDic[code]['장부가'], final_price) max_price = max(jangoDic[code]['장부가'], final_price)
sell_price = (int(max_price) - int(max_price) % 5) + 5 sell_price = (int(max_price) - int(max_price) % 5) + 5
if code == "A"+stock_code: if code == "A"+stock_code:
orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price) orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price)
print("ORDER_SELL", stock_code, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price) return orderNum, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price
return return orderNum, None, None, None
def makeTickData(self, data, mins=30): def makeTickData(self, data, mins=30):
result = {"check": set(), result = {"check": set(),
@@ -127,16 +126,19 @@ class HTS_etf (HTS):
# 미체결 기록을 가져와서 10분 이상 된 매수 주문을 취소 한다. # 미체결 기록을 가져와서 10분 이상 된 매수 주문을 취소 한다.
ORDER_LIST = self.requestOrderList() ORDER_LIST = self.requestOrderList()
orderListToCancel = self.orderChecker.cancel(today, stock['stock_code'], ORDER_LIST) orderListToCancel = self.orderChecker.cancel(today, "A" + stock['stock_code'], ORDER_LIST, mins=10)
if len(orderListToCancel) > 0: if len(orderListToCancel) > 0:
self.cancelOrderList(orderListToCancel) self.cancelOrderList(orderListToCancel)
if bs_buy_price > 0: if bs_buy_price > 0:
if not self.orderChecker.exist(today, "A" + stock['stock_code'], mins=10):
buy_count = int(self.MAX_BUY_PRICE/bs_buy_price) buy_count = int(self.MAX_BUY_PRICE/bs_buy_price)
# 매수를 주문한다. # 매수를 주문한다.
orderNum = self.requestOrder(OrderType.buy, stock['stock_code'], buy_count , bs_buy_price) orderNum = self.requestOrder(OrderType.buy, stock['stock_code'], buy_count , bs_buy_price)
self.orderChecker.buy(today, stock['stock_code'], buy_count, bs_buy_price, orderNum) self.orderChecker.buy(today, "A" + stock['stock_code'], buy_count, bs_buy_price, orderNum)
# slackbot에 메시지를 보냄 # slackbot에 메시지를 보냄
self.slackBot.post_to_slack(stock['stock_code'], stock['stock_name'], "BUY", bsLine['buy'][len(bsLine['buy']) - 1], buy_count) self.slackBot.post_to_slack(stock['stock_code'], stock['stock_name'], "BUY", bsLine['buy'][len(bsLine['buy']) - 1], buy_count)
@@ -147,14 +149,15 @@ class HTS_etf (HTS):
if bs_sell_price > 0: if bs_sell_price > 0:
# 매도한다. # 매도한다.
orderNum = self.getSellingPrice(THIS_TIME, stock['stock_code'], bs_sell_price, without_loss=True) orderNum, sell_time, jango, sell_price = self.getSellingPrice(THIS_TIME, stock['stock_code'], bs_sell_price, without_loss=True)
self.orderChecker.sell(today, stock['stock_code']) if orderNum is not None:
self.orderChecker.sell(today, "A" + stock['stock_code'])
# slackbot에 메시지를 보냄 # slackbot에 메시지를 보냄
self.slackBot.post_to_slack(stock['stock_code'], stock['stock_name'], "SELL", bsLine['sell'][len(bsLine['sell']) - 1], -1) self.slackBot.post_to_slack(stock['stock_code'], stock['stock_name'], "SELL", bsLine['sell'][len(bsLine['sell']) - 1], -1)
# 로그 출력 # 로그 출력
print("SELL", THIS_TIME.strftime('%Y%m%d %H%M%S'), str(orderNum), stock['stock_code'], stock['stock_name'], bs_sell_price) print("SELL", sell_time, stock['stock_code'], stock['stock_name'], bs_sell_price, str(orderNum), jango, sell_price)
# 로그 출력 # 로그 출력
print("TIMECHECK: %s, code: %s, name: %s, buy: %d, sell: %d, avg5: %.2f, avg30: %.2f, open: %d, high: %d, low: %d, slow_k: %.2f, slow_k_5: %.2f, slow_k_30: %.2f" % print("TIMECHECK: %s, code: %s, name: %s, buy: %d, sell: %d, avg5: %.2f, avg30: %.2f, open: %d, high: %d, low: %d, slow_k: %.2f, slow_k_5: %.2f, slow_k_30: %.2f" %
@@ -179,7 +182,9 @@ class HTS_etf (HTS):
result = self.getRealTime(stock['stock_code'], today, LAST_DATA) result = self.getRealTime(stock['stock_code'], today, LAST_DATA)
final_price = result["close"][len(result["close"]) - 1] final_price = result["close"][len(result["close"]) - 1]
self.getSellingPrice(THIS_TIME, stock['stock_code'], final_price) orderNum, sell_time, jango, sell_price = self.getSellingPrice(THIS_TIME, stock['stock_code'], final_price, without_loss=True)
# 로그 출력
print("SELL", sell_time, stock['stock_code'], stock['stock_name'], final_price, str(orderNum), jango, sell_price)
final_sell_check = True final_sell_check = True

View File

@@ -1,5 +1,6 @@
import re import re
import os import os
import time
import sqlite3 import sqlite3
from datetime import datetime from datetime import datetime
@@ -32,10 +33,10 @@ class HTS_Stocks (HTS):
self.orderChecker = OrderChecker(self.RESOURCE_PATH, "STOCK") self.orderChecker = OrderChecker(self.RESOURCE_PATH, "STOCK")
self.analyzed_day = 120 self.analyzed_day = 120
self.MAX_BUY_PRICE = 200000 self.MAX_BUY_PRICE = 100000
return return
def getSellingPrice(self, log_time, stock_code, final_price, check=False): def getSellingPrice(self, log_time, stock_code, final_price, without_loss=False):
# final_price와 diff를 받으면, 해당 가격으로 그냥 매도한다는 의미 # final_price와 diff를 받으면, 해당 가격으로 그냥 매도한다는 의미
# final_price와 diff가 None이면 장부가와 final 중 max로 팔겠다는 의미 # final_price와 diff가 None이면 장부가와 final 중 max로 팔겠다는 의미
# final_price가 0이고 diff가 None이면 장부가로 팔겠다는 의미임 # final_price가 0이고 diff가 None이면 장부가로 팔겠다는 의미임
@@ -47,12 +48,12 @@ class HTS_Stocks (HTS):
for code in jangoDic: for code in jangoDic:
if code == "A" + stock_code: if code == "A" + stock_code:
if jangoDic[code]['매도가능'] > 0: if jangoDic[code]['매도가능'] > 0:
if check: if without_loss:
if jangoDic[code]['장부가']*0.05 < jangoDic[code]['장부가'] - final_price: if jangoDic[code]['장부가']*0.07 < jangoDic[code]['장부가'] - final_price:
sell_price = jangoDic[code]['장부가'] sell_price = jangoDic[code]['장부가']
orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price) orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price)
print("ORDER_SELL", stock_code, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price) return orderNum, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price
else: else:
#max_price = max(jangoDic[code]['장부가'], final_price) #max_price = max(jangoDic[code]['장부가'], final_price)
# 10% 이상 수익이어야 매도한다. # 10% 이상 수익이어야 매도한다.
@@ -61,8 +62,9 @@ class HTS_Stocks (HTS):
sell_price = (int(max_price) - int(max_price) % 5) sell_price = (int(max_price) - int(max_price) % 5)
orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price) orderNum = self.requestOrder(OrderType.sell, stock_code, jangoDic[code]['매도가능'], sell_price)
print("ORDER_SELL", stock_code, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price) return orderNum, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price
return orderNum
return orderNum, None, None, None
def valid_company(self): def valid_company(self):
valid_company = set() valid_company = set()
@@ -83,11 +85,6 @@ class HTS_Stocks (HTS):
valid_company = self.valid_company() valid_company = self.valid_company()
while datetime.strptime(today + " 070000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 153100", '%Y%m%d %H%M%S'):
# 1515 까지만 매수를 시도한다.
if datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151500", '%Y%m%d %H%M%S'):
stockTableName = 'stock' stockTableName = 'stock'
conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "stock.db")) conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "stock.db"))
cursor = conn.cursor() cursor = conn.cursor()
@@ -96,6 +93,11 @@ class HTS_Stocks (HTS):
cursor.close() cursor.close()
conn.close() conn.close()
while datetime.strptime(today + " 070000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 153100", '%Y%m%d %H%M%S'):
# 1515 까지만 매수를 시도한다.
if datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151500", '%Y%m%d %H%M%S'):
for idx, item in enumerate(items): for idx, item in enumerate(items):
if THIS_TIME < datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') or datetime.strptime(today + " 151500", '%Y%m%d %H%M%S') < THIS_TIME: if THIS_TIME < datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') or datetime.strptime(today + " 151500", '%Y%m%d %H%M%S') < THIS_TIME:
break break
@@ -121,37 +123,74 @@ class HTS_Stocks (HTS):
bsLine, data = self.buySellChecker.checkTransactionWithEnvelope(data, stock_code, self.analyzed_day, isRealTime=False) bsLine, data = self.buySellChecker.checkTransactionWithEnvelope(data, stock_code, self.analyzed_day, isRealTime=False)
# 미체결 기록을 가져와서 10분 이상 된 매수 주문을 취소 한다.
ORDER_LIST = self.requestOrderList()
orderListToCancel = self.orderChecker.cancel(today, "A" + stock_code, ORDER_LIST, mins=10)
if len(orderListToCancel) > 0:
self.cancelOrderList(orderListToCancel)
# 다음 조건이면 매수한다. # 다음 조건이면 매수한다.
if len(data.index) > 10 and max(bsLine['buy'][len(bsLine['buy']) - 1:]) > 1000: if len(data) > 10 and max(bsLine['buy'][len(bsLine['buy']) - 1:]) > 1000:
if not self.orderChecker.exist(today, stock_code, hours=2):
if not self.orderChecker.exist(today, "A" + stock_code, mins=10):
last_index = len(bsLine['buy'])-1 last_index = len(bsLine['buy'])-1
if bsLine['buy'][last_index] > 0: if bsLine['buy'][last_index] > 0:
bs_buy_price = bsLine['buy'][last_index] bs_buy_price = bsLine['buy'][last_index]
bs_buy_weight = bsLine['buy_weight'][last_index] bs_buy_weight = bsLine['buy_weight'][last_index]
buy_count = int(self.MAX_BUY_PRICE / bs_buy_price) buy_count = int(self.MAX_BUY_PRICE / bs_buy_price)
# 매수를 주문한다. # 매수를 주문한다.
orderNum = self.requestOrder(OrderType.buy, stock_code, buy_count, bs_buy_price) orderNum = self.requestOrder(OrderType.buy, stock_code, buy_count, bs_buy_price)
self.orderChecker.buy(today, "A" + stock_code, buy_count, bs_buy_price, orderNum)
# slackbot에 메시지를 보냄 # slackbot에 메시지를 보냄
self.slackBot.post_to_slack(stock_code, stock_name, "BUY", bsLine['buy'][len(bsLine['buy']) - 1], buy_count) self.slackBot.post_to_slack(stock_code, stock_name, "BUY", bsLine['buy'][len(bsLine['buy']) - 1], buy_count)
self.orderChecker.buy(today, stock_code, buy_count, bs_buy_price, orderNum)
# 로그 출력 # 로그 출력
print("BUY", THIS_TIME.strftime('%Y%m%d %H%M%S'), orderNum, stock_code, stock_name, bs_buy_price, buy_count) print("BUY", THIS_TIME.strftime('%Y%m%d %H%M%S'), orderNum, stock_code, stock_name, bs_buy_price, buy_count)
# 다음 조건이면 매도한다. # 다음 조건이면 매도한다.
if len(data.index) > 10 and max(bsLine['sell'][len(bsLine['sell']) - 1:]) > 0: if len(data.index) > 10 and max(bsLine['sell'][len(bsLine['sell']) - 1:]) > 0:
last_index = len(bsLine['sell']) - 1 last_index = len(bsLine['sell']) - 1
if bsLine['sell'][last_index] > 0: if bsLine['sell'][last_index] > 0:
bs_sell_price = bsLine['sell'][last_index] bs_sell_price = bsLine['sell'][last_index]
orderNum = self.getSellingPrice(THIS_TIME, stock_code, bs_sell_price) orderNum, sell_time, jango, sell_price = self.getSellingPrice(THIS_TIME, stock_code, bs_sell_price, without_loss=True)
if orderNum is not None:
self.orderChecker.sell(today, "A" + stock_code)
# slackbot에 메시지를 보냄 # slackbot에 메시지를 보냄
self.slackBot.post_to_slack(stock_code, stock_name, "SELL", bsLine['sell'][len(bsLine['sell']) - 1], -1) self.slackBot.post_to_slack(stock_code, stock_name, "SELL", bsLine['sell'][len(bsLine['sell']) - 1], -1)
self.orderChecker.sell(today, stock_code)
# 로그 출력 # 로그 출력
print("SELL", THIS_TIME.strftime('%Y%m%d %H%M%S'), orderNum, stock_code, stock_name, bs_sell_price) print("SELL", sell_time, stock_code, stock_name, bs_sell_price, str(orderNum), jango, sell_price)
elif datetime.strptime(today + " 151530", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151600", '%Y%m%d %H%M%S'):
# 3시 15분 30초부터 3시 16분 사이는 잔량을 매도한다.
####
# 손해 보지 않는 가격에 매도한다.
####
# 주문 리스트를 가져온다.
orderList = self.requestOrderList()
# 15:10:00 이후라면 모든 미체결 취소한다.
self.cancelOrderList(orderList)
for idx, item in enumerate(items):
stock_code = item[0]
stock_name = item[1]
# 매도 가격을 가져온다.
orderNum, sell_time, jango, sell_price = self.getSellingPrice(THIS_TIME, stock_code, final_price=-1, without_loss=True)
# 로그 출력
print("SELL", sell_time, stock_code, stock_name, -1, str(orderNum), jango, sell_price)
time.sleep(10)
THIS_TIME = datetime.now() THIS_TIME = datetime.now()
return return

View File

@@ -29,25 +29,23 @@ class OrderChecker:
order_df['datetime'] = pd.to_datetime(order_df['datetime']) order_df['datetime'] = pd.to_datetime(order_df['datetime'])
return order_df return order_df
def exist(self, ymd, stock_code, hours=2): def exist(self, ymd, stock_code, mins=30):
order_df = self.read(ymd) order_df = self.read(ymd)
try: if order_df != None and len(order_df)>0:
tmp = order_df.loc[order_df["stock_code"] == int(stock_code)] tmp = order_df.loc[order_df["stock_code"] == stock_code]
now = datetime.now() - timedelta(hours=hours) now = datetime.now() - timedelta(minutes=mins)
tmp = tmp.loc[now < tmp["datetime"]] tmp = tmp.loc[now < tmp["datetime"]]
except: if tmp is not None and len(tmp) > 0:
return False
if tmp is None or len(tmp) == 0:
return False
return True return True
return False
def buy(self, ymd, stock_code: str, count: int, price: int, orderNum=""): def buy(self, ymd, stock_code: str, count: int, price: int, orderNum=""):
order_df = self.read(ymd) order_df = self.read(ymd)
# 새로운 주문을 추가한다. # 새로운 주문을 추가한다.
order_df = order_df.append({"stock_code": "A" + stock_code, "type": 0, "orderNum": str(orderNum), "canceled": 0, "count": count, order_df = order_df.append({"stock_code": stock_code, "type": 0, "orderNum": str(orderNum), "canceled": 0, "count": count,
"price": price, "datetime": datetime.now()}, ignore_index=True) "price": price, "datetime": datetime.now()}, ignore_index=True)
order_df = order_df.astype({"stock_code": str, "type": int, "orderNum": str, "canceled": int, "count": int, "price": int}) order_df = order_df.astype({"stock_code": str, "type": int, "orderNum": str, "canceled": int, "count": int, "price": int})
@@ -62,7 +60,7 @@ class OrderChecker:
order_df = self.read(ymd) order_df = self.read(ymd)
# 기존 매수 주문을 매도로 변경 한다. # 기존 매수 주문을 매도로 변경 한다.
order_df.loc[(order_df["stock_code"] == "A" + stock_code, 'type')] = 1 order_df.loc[(order_df["stock_code"] == stock_code, 'type')] = 1
# 파일로 기록 한다. # 파일로 기록 한다.
saveFileName = os.path.join(self.RESOURCE_PATH, "order", self.TYPE+"_"+ymd + ".csv") saveFileName = os.path.join(self.RESOURCE_PATH, "order", self.TYPE+"_"+ymd + ".csv")
@@ -70,20 +68,20 @@ class OrderChecker:
return True return True
def cancel(self, ymd, stock_code, ORDER_LIST, min=10): def cancel(self, ymd, stock_code, ORDER_LIST, mins=10):
orderListToCancel = [] orderListToCancel = []
order_df = self.read(ymd) order_df = self.read(ymd)
if len(order_df) > 0: if len(order_df) > 0:
now = datetime.now() - timedelta(minutes=min) now = datetime.now() - timedelta(minutes=mins)
# min 분 이상 된 시간인 내용을 가지고 옴 # min 분 이상 된 시간인 내용을 가지고 옴
df = order_df.loc[(order_df["datetime"] <= now)] df = order_df.loc[(order_df["datetime"] <= now)]
# 취소가 되지 않은 것만 가지고 옴 (0: 취소 되지 않음, 1: 취소함) # 취소가 되지 않은 것만 가지고 옴 (0: 취소 되지 않음, 1: 취소함)
df = df.loc[(order_df["canceled"] == 0)] df = df.loc[(order_df["canceled"] == 0)]
for i in range(len(df)): for i in range(len(df)):
order_df.loc[(order_df['stock_code']=="A" + stock_code) & (order_df.index == df.index[i]), 'canceled'] = 1 order_df.loc[(order_df['stock_code']==stock_code) & (order_df.index == df.index[i]), 'canceled'] = 1
saveFileName = os.path.join(self.RESOURCE_PATH, "order", self.TYPE+"_"+ymd + ".csv") saveFileName = os.path.join(self.RESOURCE_PATH, "order", self.TYPE+"_"+ymd + ".csv")
order_df.to_csv(saveFileName, index=False) order_df.to_csv(saveFileName, index=False)