This commit is contained in:
dosangyoon
2022-07-30 16:08:37 +09:00
parent d230a51652
commit cddcdf4f35
6 changed files with 321 additions and 165 deletions

View File

@@ -29,20 +29,6 @@ class HTS_252670 (HTS):
self.buySellChecker = BuySellChecker()
return
def checkTransaction(self, data):
size = len(data["close"])
bsLine = {}
bsLine['buy'] = [-1 for i in range(size)]
bsLine['weight'] = [-1 for i in range(size)]
bsLine['sell'] = [-1 for i in range(size)]
last_index = size - 1
#buy, weight, sell = self.buySellChecker.getPriceAndWeight3(hts, last_index)
sell, weight = self.buySellChecker.getSellPriceAndWeight_3000(data, last_index)
buy, weight = self.buySellChecker.getBuyPriceAndWeight_3000(data, last_index)
return buy, weight, sell
def getSellingPrice(self, final_price):
# 만약 잔고가 있으면 장부가보다 5원 높게 매도한다.
jangoDic = self.requstJango()
@@ -78,7 +64,7 @@ class HTS_252670 (HTS):
return 0, 0
def buyRealTime(self, lastday, today):
def buyRealTime(self, today):
timecheckList = pd.read_csv("hts/timecheck.csv").values.tolist()
timecheck = {today + " " + str(second).zfill(6):False for second, check in timecheckList}
@@ -86,29 +72,33 @@ class HTS_252670 (HTS):
print ("START...")
THIS_TIME = datetime.now()
final_sell_check = False
LAST_DATA = self.getLastData(self.stock_code, today)
while datetime.strptime(today + " 070000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 153100", '%Y%m%d %H%M%S'):
if datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 150000", '%Y%m%d %H%M%S'):
if datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151500", '%Y%m%d %H%M%S'):
# 3시 까지만 매수를 시도한다.
if THIS_TIME.strftime('%Y%m%d %H%M%S') in timecheck and not timecheck[THIS_TIME.strftime('%Y%m%d %H%M%S')]:
# 데이터를 가지고 온다.
result = self.getRealTime(self.stock_code, lastday, today)
result = self.getRealTime(self.stock_code, today, LAST_DATA)
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyze(result)
# 규칙 기반의 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyzeByRule(result)
# 사야 할 시점/가격과 팔아야 할 시점/가격을 체크한다.
bs_buy_price, bs_weight, bs_sell_price = self.checkTransaction(data)
bsLine, data = self.buySellChecker.checkTransaction(data, self.stock_code, True)
bs_buy_price = bsLine['buy']
bs_weight = bsLine['buy_weight']
bs_sell_price = bsLine['sell']
data_size = len(data["close"])
final_price = data["close"][data_size-1]
if bs_buy_price > 0:
# 기본 100 주에 가중치를 추가해서 매수한다.
BUY_COUNT = int(self.buy_count * bs_weight)
#BUY_COUNT = int(self.buy_count * 1)
# 매수를 주문한다.
orderNum = self.requestOrder(OrderType.buy, self.stock_code, BUY_COUNT , bs_buy_price)
@@ -159,6 +149,9 @@ class HTS_252670 (HTS):
# 3시 15분 30초부터 3시 16분 사이는 잔량을 매도한다.
if not final_sell_check:
####
# 손해 보지 않는 가격에 매도한다.
####
# 주문 리스트를 가져온다.
orderList = self.requestOrderList()
@@ -166,10 +159,12 @@ class HTS_252670 (HTS):
self.cancelOrderList(orderList)
# 매도 가격을 가져온다.
result = self.getRealTime(self.stock_code, lastday, today)
result = self.getRealTime(self.stock_code, today, LAST_DATA)
final_price = result["close"][len(result["close"]) - 1]
#selling_count, selling_price = self.getFinalSellingPrice(final_price)
selling_count, selling_price = self.getSellingPrice(final_price)
# 분석된 가격으로 매도 요청한다.
if selling_count != 0 and selling_price != 0:
orderNum = self.requestOrder(OrderType.sell, self.stock_code, selling_count, selling_price)
@@ -197,15 +192,8 @@ if __name__ == "__main__":
hts = HTS_252670(RESOURCE_PATH, stock_code, buy_count)
today_str = today.strftime('%Y%m%d')
lastday_str = ""
for i in range(1, 10):
lastday_str = (today - timedelta(i)).strftime('%Y%m%d')
last_hts_data_filename = os.path.join(RESOURCE_PATH, "hts", stock_code + "_" + lastday_str + ".csv")
if os.path.isfile(last_hts_data_filename):
break
hts.buyRealTime(lastday_str, today_str)
#hts.writeStockData(stock_code, today_str)
hts.buyRealTime(today_str)
db_filename = os.path.join(RESOURCE_PATH, "hts.db")
hts.insertStockData(db_filename, stock_code, stock_name, today_str)