This commit is contained in:
dosangyoon
2021-09-23 02:45:22 +09:00
parent 77c1fb98b9
commit 76a15587a3
2 changed files with 157 additions and 65 deletions

View File

@@ -3,7 +3,6 @@ import time
import os
from datetime import datetime, timedelta
import pandas as pd
import schedule
import matplotlib.pyplot as plt
import plotly.graph_objects as go
@@ -141,7 +140,7 @@ class HTS:
return
# 주식 현금 매수주문
def orderToBuy(self, stock_code):
def orderToBuy(self, stock_code, count, price):
# 주문 초기화
objTrade = win32com.client.Dispatch("CpTrade.CpTdUtil")
initCheck = objTrade.TradeInit(0)
@@ -158,8 +157,8 @@ class HTS:
objStockOrder.SetInputValue(1, acc) # 계좌번호
objStockOrder.SetInputValue(2, accFlag[0]) # 상품구분 - 주식 상품 중 첫번째
objStockOrder.SetInputValue(3, "A"+stock_code) # 종목코드
objStockOrder.SetInputValue(4, 10) # 매수수량 10
objStockOrder.SetInputValue(5, 14100) # 주문단가 - 14,100
objStockOrder.SetInputValue(4, count) # 매수수량 count
objStockOrder.SetInputValue(5, price) # 주문단가 - price
objStockOrder.SetInputValue(7, "0") # 주문 조건 구분 코드, 0: 기본 1: IOC 2:FOK
objStockOrder.SetInputValue(8, "01") # 주문호가 구분코드 - 01: 보통
@@ -175,7 +174,7 @@ class HTS:
return
# 주식 현금 매도주문
def orderToSell(self, stock_code):
def orderToSell(self, stock_code, count, price):
# 주문 초기화
objTrade = win32com.client.Dispatch("CpTrade.CpTdUtil")
initCheck = objTrade.TradeInit(0)
@@ -192,8 +191,8 @@ class HTS:
objStockOrder.SetInputValue(1, acc) # 계좌번호
objStockOrder.SetInputValue(2, accFlag[0]) # 상품구분 - 주식 상품 중 첫번째
objStockOrder.SetInputValue(3, "A"+stock_code) # 종목코드 - A003540 - 대신증권 종목
objStockOrder.SetInputValue(4, 10) # 매도수량 10
objStockOrder.SetInputValue(5, 14100) # 주문단가 - 14,100
objStockOrder.SetInputValue(4, count) # 매도수량 count
objStockOrder.SetInputValue(5, price) # 주문단가 - price
objStockOrder.SetInputValue(7, "0") # 주문 조건 구분 코드, 0: 기본 1: IOC 2:FOK
objStockOrder.SetInputValue(8, "01") # 주문호가 구분코드 - 01: 보통
@@ -391,33 +390,7 @@ class HTS:
df_final_time = pd.DatetimeIndex(point_temp)
data.index = df_final_time
# 살 시점인지 체크
# 볼린저밴드 하단에 연속으로 같은 가격이 왔을 때,
# 해당 하단 가격 + 5원에 매수를 시도함
check = False
buy_line = [-1 for i in range(len(lower_temp))]
for i in range(3, len(lower_temp)):
for j in range(i-3, i):
if (low[j] < lower_temp[j]) and (low[i] < lower_temp[i] and low[j] == low[i]):
#buy_line[i] = low[i]
check = True
break
if check and i < len(lower_temp) - 1:
buy_line[i+1] = low[i] + 5
check = False
# 팔 시점 체크
# 산 가격에 5원 위로 매도를 건다.
sell_line = [-1 for i in range(len(lower_temp))]
for i in range(len(buy_line)):
if buy_line[i] > 0:
for j in range(i+1, len(buy_line)):
# 5원 이득을 보고 판다.
if close[j] >= buy_line[i] + 5:
sell_line[j] = buy_line[i] + 5
break
return data, upper_temp, lower_temp, buy_line, sell_line
return data, upper_temp, lower_temp
def draw(self, given_day, data, upper, lower, buy_line, sell_line):
# 그래프 설정을 위한 변수를 생성한다.
@@ -455,7 +428,42 @@ class HTS:
fig.show()
return
def checkTransaction(self, data, lower):
low = data["Low"]
close = data["Close"]
# 살 시점인지 체크
# 볼린저밴드 하단에 연속으로 같은 가격이 왔을 때,
# 해당 하단 가격 + 5원에 매수를 시도함
check = False
buy_line = [-1 for i in range(len(lower))]
for i in range(3, len(lower)):
for j in range(i-3, i):
if (low[j] < lower[j]) and (low[i] < lower[i] and low[j] == low[i]):
#buy_line[i] = low[i]
check = True
break
if check and i < len(lower) - 1:
buy_line[i+1] = low[i] + 5
check = False
# 팔 시점 체크
# 산 가격에 5원 위로 매도를 건다.
sell_line = [-1 for i in range(len(lower))]
for i in range(len(buy_line)):
if buy_line[i] > 0:
for j in range(i+1, len(buy_line)):
# 5원 이득을 보고 판다.
if close[j] >= buy_line[i] + 5:
sell_line[j] = buy_line[i] + 5
break
return buy_line, sell_line
def simulate(self, stock_code, given_day):
timecheckList = pd.read_csv("timecheck.csv").values.tolist()
timecheck = {datetime.strptime(given_day + " " + str(second).zfill(6), '%Y%m%d %H%M%S'): False for second, check in timecheckList}
result = {"check": set(),
"time": [],
"open": [],
@@ -471,29 +479,20 @@ class HTS:
self.write(given_day, result)
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data, upper, lower, buy_line, sell_line = self.analyze(result)
data, upper, lower = self.analyze(result)
# 사야 할 시점과 팔아야 할 시점을 체크한다.
buy_line, sell_line = self.checkTransaction(data, lower)
# 그래프를 그린다.
self.draw(given_day, data, upper, lower, buy_line, sell_line)
return
def checkRealTime(self, stock_code, given_day, result):
# 데이터를 가지고 온다.
self.getRealTime(stock_code, given_day, result)
# 가져온 만큼 데이터를 누적해서 파일로 작성한다.
self.write(given_day, result)
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data, upper, lower, buy_line, sell_line = self.analyze(result)
# 주문 및 매도 처리
# self.draw(given_day, data, upper, lower, buy_line, sell_line)
return
def buyRealTime(self, stock_code, given_day):
timecheckList = pd.read_csv("timecheck.csv").values.tolist()
timecheck = {datetime.strptime(given_day + " " + str(second).zfill(6), '%Y%m%d %H%M%S'):False for second, check in timecheckList}
result = {"check": set(),
"time": [],
"open": [],
@@ -502,22 +501,38 @@ class HTS:
"low": [],
"vol": []}
schedule.every().hour.at(":0:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 0분 5초에 작업 실행
schedule.every().hour.at(":5:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 5분 5초에 작업 실행
schedule.every().hour.at(":10:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 10분 5초에 작업 실행
schedule.every().hour.at(":15:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 15분 5초에 작업 실행
schedule.every().hour.at(":20:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 20분 5초에 작업 실행
schedule.every().hour.at(":25:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 25분 5초에 작업 실행
schedule.every().hour.at(":30:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 30분 5초에 작업 실행
schedule.every().hour.at(":35:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 35분 5초에 작업 실행
schedule.every().hour.at(":40:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 40분 5초에 작업 실행
schedule.every().hour.at(":45:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 45분 5초에 작업 실행
schedule.every().hour.at(":50:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 50분 5초에 작업 실행
schedule.every().hour.at(":55:5").do(self.checkRealTime(stock_code, given_day, result)) # 매시간 55분 5초에 작업 실행
while datetime.strptime(given_day + " 085900", '%Y%m%d %H%M%S') < datetime.now() < datetime.strptime(given_day + " 151500", '%Y%m%d %H%M%S'):
second = datetime.now().strftime('%Y%m%d %H%M%S')
if second in timecheck and timecheck[second] == False:
while datetime.now() < datetime.strptime(given_day + " 151500", '%Y%m%d %H%M%S'):
schedule.run_pending()
time.sleep(1)
# 데이터를 가지고 온다.
self.getRealTime(stock_code, given_day, result)
# 가져온 만큼 데이터를 누적해서 파일로 작성한다.
self.write(given_day, result)
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data, upper, lower = self.analyze(result)
# 사야 할 시점과 팔아야 할 시점을 체크한다.
buy_line, sell_line = self.checkTransaction(data, lower)
# 주문 및 매도 처리
price = buy_line[len(buy_line)-1]
# 매수신청과 5원 높여서 매도신청
if price > 0:
print(second, price)
count = 1
self.orderToBuy(stock_code, count, price)
self.orderToSell(stock_code, count, price + 5)
timecheck[second] = True
# self.draw(given_day, data, upper, lower, buy_line, sell_line)
time.sleep(0.5)
return