192 lines
9.1 KiB
Python
192 lines
9.1 KiB
Python
import time
|
|
import os
|
|
from datetime import datetime, timedelta
|
|
import pandas as pd
|
|
|
|
from hts.HTS import HTS
|
|
from hts.OrderType import OrderType
|
|
|
|
from hts.BuySellChecker import BuySellChecker
|
|
from hts.OrderChecker import OrderChecker
|
|
|
|
|
|
class HTS_122630 (HTS):
|
|
|
|
buySellChecker = None
|
|
stock_code = None
|
|
buy_count = None
|
|
|
|
def __init__(self, stock_code, buy_count):
|
|
super().__init__()
|
|
|
|
self.buySellChecker = BuySellChecker()
|
|
self.stock_code = stock_code
|
|
self.buy_count = buy_count
|
|
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(data, last_index)
|
|
return buy, weight, sell
|
|
|
|
def getSellingPrice(self, final_price):
|
|
# 만약 잔고가 있으면 장부가보다 5원 높게 매도한다.
|
|
jangoDic = self.requstJango()
|
|
if jangoDic and len(jangoDic.keys()) > 0:
|
|
for code in jangoDic:
|
|
if jangoDic[code]['매도가능'] > 0:
|
|
if final_price >= jangoDic[code]['장부가'] + 5:
|
|
return jangoDic[code]['매도가능'], final_price
|
|
else:
|
|
# 장부가 가격의 마지막 자리를 0으로 만든다. (2090 -> 2090, 2092 -> 2090, 2098 -> 2090)
|
|
sell_price = int(jangoDic[code]['장부가'] / 10) * 10
|
|
# 장부가의 마지막 자리수를 가져온다.
|
|
last_number = int(jangoDic[code]['장부가']) % 10
|
|
if last_number in [0, 1, 2, 3]:
|
|
# 장부가의 마지막 자리수가 0,1,2,3 이라면 (2090, 2091, 2092 -> 2095 에 매도)
|
|
return jangoDic[code]['매도가능'], sell_price + 5
|
|
elif last_number in [4, 5, 6, 7]:
|
|
# 장부가의 마지막 자리수가 4,5,6,7 이라면 (2093, 2094, 2095, 2096 -> 2100 에 매도)
|
|
return jangoDic[code]['매도가능'], sell_price + 10
|
|
else:
|
|
# 장부가의 마지막 자리수가 8,9 라면 (2098, 2099 -> 2105 에 매도)
|
|
return jangoDic[code]['매도가능'], sell_price + 15
|
|
|
|
return 0, 0
|
|
|
|
def buyRealTime(self, lastday, today):
|
|
orderChecker = OrderChecker(self.stock_code)
|
|
|
|
timecheckList = pd.read_csv("timecheck.csv").values.tolist()
|
|
timecheck = {today + " " + str(second).zfill(6):False for second, check in timecheckList}
|
|
|
|
print ("START...")
|
|
THIS_TIME = datetime.now()
|
|
final_sell_check = False
|
|
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 + " 151500", '%Y%m%d %H%M%S'):
|
|
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)
|
|
|
|
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
|
|
data = self.buySellChecker.analyze(result)
|
|
|
|
# 사야 할 시점/가격과 팔아야 할 시점/가격을 체크한다.
|
|
bs_buy_price, bs_weight, bs_sell_price = self.checkTransaction(data)
|
|
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)
|
|
|
|
# 미체결 기록을 가져온다.
|
|
ORDER_LIST = self.requestOrderList()
|
|
# 매수 주문을 기록한다.
|
|
orderListToCancel = orderChecker.add(self.stock_code, OrderType.buy, orderNum, BUY_COUNT, bs_buy_price, ORDER_LIST)
|
|
# 두 시간 이전 미체결을 모두 취소한다.
|
|
self.cancelOrderList(orderListToCancel)
|
|
# 로그 출력
|
|
print("BUY", THIS_TIME.strftime('%Y%m%d %H%M%S'), BUY_COUNT, bs_buy_price, len(orderListToCancel), len(ORDER_LIST))
|
|
|
|
|
|
if bs_sell_price > 0:
|
|
# 미체결 기록을 가져온다.
|
|
ORDER_LIST = self.requestOrderList()
|
|
# 매도 주문을 기록을 가져온다.
|
|
orderListToCancel = orderChecker.remove(self.stock_code, OrderType.sell, ORDER_LIST)
|
|
# 매도 미체결을 모두 취소한다.
|
|
self.cancelOrderList(orderListToCancel)
|
|
|
|
# 매도 가격을 가져온다.
|
|
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)
|
|
|
|
# 로그 출력
|
|
print("SELL", THIS_TIME.strftime('%Y%m%d %H%M%S'), selling_count, selling_price, len(orderListToCancel), len(ORDER_LIST))
|
|
|
|
|
|
# 로그 출력
|
|
print("TIMECHECK: %s, price: %d, open: %d, high: %d, low: %d, avg3: %.2f, avg5: %.2f, avg10: %.2f, avg20: %.2f, avg30: %.2f, avg60: %.2f,\n lower: %.2f, upper: %.2f, macd: %.2f, macds: %.2f, macdo: %.2f,\n rsi: %.2f, rsis: %.2f, fast_k: %.2f, slow_k: %.2f, slow_d: %.2f" %
|
|
(str(THIS_TIME), final_price, data["open"][data_size - 1], data["high"][data_size - 1],
|
|
data["low"][data_size - 1],
|
|
data["avg3"][data_size - 1], data["avg5"][data_size - 1], data["avg10"][data_size - 1],
|
|
data["avg20"][data_size - 1], data["avg30"][data_size - 1], data["avg60"][data_size - 1],
|
|
data["lower"][data_size - 1], data["upper"][data_size - 1],
|
|
data["macd"][data_size - 2], data["macds"][data_size - 2], data["macdo"][data_size - 1],
|
|
data["fast_k"][data_size - 2], data["slow_k"][data_size - 2], data["slow_d"][data_size - 1],
|
|
data["rsi"][data_size - 1], data["rsis"][data_size - 1]))
|
|
timecheck[THIS_TIME] = True
|
|
|
|
elif datetime.strptime(today + " 151530", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151600", '%Y%m%d %H%M%S'):
|
|
if final_sell_check:
|
|
continue
|
|
|
|
####
|
|
# 손해 보지 않는 가격에 매도한다.
|
|
####
|
|
|
|
# 주문 리스트를 가져온다.
|
|
orderList = self.requestOrderList()
|
|
# 15:10:00 이후라면 모든 미체결 취소한다.
|
|
self.cancelOrderList(orderList)
|
|
|
|
# 매도 가격을 가져온다.
|
|
result = self.getRealTime(self.stock_code, lastday, today)
|
|
final_price = result["close"][len(result["close"])-1]
|
|
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)
|
|
# 로그 출력
|
|
print("SELL", THIS_TIME, selling_count, selling_price)
|
|
|
|
final_sell_check = True
|
|
|
|
time.sleep(0.9)
|
|
THIS_TIME = datetime.now()
|
|
|
|
return
|
|
|
|
if __name__ == "__main__":
|
|
|
|
today = datetime.today()
|
|
|
|
PROJECT_HOME = os.path.join(os.path.dirname(os.path.join(os.path.dirname(__file__))))
|
|
RESOURCE_DIR = PROJECT_HOME + "/resources/analysis/"+today.strftime("%Y%m%d")
|
|
|
|
# KODEX 인버스 * 2
|
|
stock_code = "122630"
|
|
buy_count = 20
|
|
|
|
hts = HTS_122630(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')
|
|
if os.path.isfile("./data/" + stock_code + "_" + lastday_str + ".csv"):
|
|
break
|
|
|
|
hts.buyRealTime(lastday_str, today_str)
|
|
hts.writeStockData(stock_code, today_str)
|
|
|
|
print ("done...")
|