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

@@ -1,5 +1,6 @@
import re
import os
import time
import sqlite3
from datetime import datetime
@@ -32,10 +33,10 @@ class HTS_Stocks (HTS):
self.orderChecker = OrderChecker(self.RESOURCE_PATH, "STOCK")
self.analyzed_day = 120
self.MAX_BUY_PRICE = 200000
self.MAX_BUY_PRICE = 100000
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가 None이면 장부가와 final 중 max로 팔겠다는 의미
# final_price가 0이고 diff가 None이면 장부가로 팔겠다는 의미임
@@ -47,12 +48,12 @@ class HTS_Stocks (HTS):
for code in jangoDic:
if code == "A" + stock_code:
if jangoDic[code]['매도가능'] > 0:
if check:
if jangoDic[code]['장부가']*0.05 < jangoDic[code]['장부가'] - final_price:
if without_loss:
if jangoDic[code]['장부가']*0.07 < jangoDic[code]['장부가'] - final_price:
sell_price = jangoDic[code]['장부가']
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:
#max_price = max(jangoDic[code]['장부가'], final_price)
# 10% 이상 수익이어야 매도한다.
@@ -61,8 +62,9 @@ class HTS_Stocks (HTS):
sell_price = (int(max_price) - int(max_price) % 5)
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
return orderNum, log_time.strftime('%Y%m%d %H%M%S'), jangoDic[code]['매도가능'], sell_price
return orderNum, None, None, None
def valid_company(self):
valid_company = set()
@@ -83,19 +85,19 @@ class HTS_Stocks (HTS):
valid_company = self.valid_company()
stockTableName = 'stock'
conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "stock.db"))
cursor = conn.cursor()
cursor.execute('SELECT distinct code, name FROM ' + stockTableName + ' order by code')
items = cursor.fetchall()
cursor.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'):
stockTableName = 'stock'
conn = sqlite3.connect(os.path.join(self.RESOURCE_PATH, "stock.db"))
cursor = conn.cursor()
cursor.execute('SELECT distinct code, name FROM ' + stockTableName + ' order by code')
items = cursor.fetchall()
cursor.close()
conn.close()
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:
break
@@ -121,37 +123,74 @@ class HTS_Stocks (HTS):
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 not self.orderChecker.exist(today, stock_code, hours=2):
if len(data) > 10 and max(bsLine['buy'][len(bsLine['buy']) - 1:]) > 1000:
if not self.orderChecker.exist(today, "A" + stock_code, mins=10):
last_index = len(bsLine['buy'])-1
if bsLine['buy'][last_index] > 0:
bs_buy_price = bsLine['buy'][last_index]
bs_buy_weight = bsLine['buy_weight'][last_index]
buy_count = int(self.MAX_BUY_PRICE / 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에 메시지를 보냄
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)
# 다음 조건이면 매도한다.
if len(data.index) > 10 and max(bsLine['sell'][len(bsLine['sell']) - 1:]) > 0:
last_index = len(bsLine['sell']) - 1
if bsLine['sell'][last_index] > 0:
bs_sell_price = bsLine['sell'][last_index]
orderNum = self.getSellingPrice(THIS_TIME, stock_code, bs_sell_price)
# slackbot에 메시지를 보냄
self.slackBot.post_to_slack(stock_code, stock_name, "SELL", bsLine['sell'][len(bsLine['sell']) - 1], -1)
self.orderChecker.sell(today, stock_code)
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)
# 로그 출력
print("SELL", THIS_TIME.strftime('%Y%m%d %H%M%S'), orderNum, stock_code, stock_name, bs_sell_price)
# slackbot에 메시지를 보냄
self.slackBot.post_to_slack(stock_code, stock_name, "SELL", bsLine['sell'][len(bsLine['sell']) - 1], -1)
# 로그 출력
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()
return