This commit is contained in:
dosangyoon
2021-10-03 20:33:37 +09:00
parent ce00541b80
commit 35a5d1694a
2 changed files with 334 additions and 57 deletions

View File

@@ -1,10 +1,10 @@
import win32com.client
#import win32com.client
import time
import os
from datetime import datetime, timedelta
import pandas as pd
from enum import Enum
#import plotly.graph_objects as go
import plotly.graph_objects as go
from stockpredictor.analysis.Common import Common
# enum 주문 상태 세팅용
@@ -736,8 +736,8 @@ class HTS:
STOCK.append({'close': data["Close"][i], 'open': data["Open"][i], 'high': data["High"][i], 'low': data["Low"][i], 'avg5': data["avg5"][i], 'avg20': data["avg20"][i], 'avg60': data["avg60"][i], 'avg120': data["avg120"][i]})
bsLine = {}
bsLine['buy'] = [-1 for i in range(len(lower))]
bsLine['sell'] = [-1 for i in range(len(lower))]
bsLine['buy'] = [-1 for i in range(size)]
bsLine['sell'] = [-1 for i in range(size)]
i = size - 1
status = self.checkStatus(STOCK, i)
@@ -767,13 +767,15 @@ class HTS:
if "DARKCLOUD_" in status: count_0 += 1
if "EVENINGSTAR" in status: count_0 += 1
# real time 에서는 현재 기점에 사고 파는 가격을 표기한다.
if count_0 == 0 and count_1 > 0:
bsLine['buy'][i + 1] = STOCK[i]['close'] - 5
bsLine['sell'][i + 1] = STOCK[i]['close']
bsLine['buy'][i] = STOCK[i]['close'] - 5
bsLine['sell'][i] = STOCK[i]['close']
if count_0 > 0:
bsLine['sell'][i + 1] = STOCK[i]['close'] + 5
bsLine['buy'][i] = 0
bsLine['sell'][i] = STOCK[i]['close'] + 5
return bsLine
return bsLine['buy'][i], bsLine['sell'][i]
def checkTransaction_Simulation(self, data, upper, lower):
@@ -814,6 +816,7 @@ class HTS:
if "DARKCLOUD_" in status: count_0 += 1
if "EVENINGSTAR" in status: count_0 += 1
# 시뮬레이션은 이번에 사고 파는 것으로 판단했기 때문에, 다음 봉에서 사고 판 위치를 표시한다.
if count_0 == 0 and count_1 > 0:
bsLine['buy'][i + 1] = STOCK[i]['close'] - 5
bsLine['sell'][i + 1] = STOCK[i]['close']
@@ -853,11 +856,9 @@ class HTS:
return
def buyRealTime(self, stock_code, given_day):
data, upper, lower = None, None, None
previous_price = 0
buy_count = 260
total_byu_amt = 0
PREVIOUS_PRICE = 0
BUY_COUNT = 200
TOTAL_BUY_AMT = 0
logFp = open(given_day+".log", "w")
@@ -872,7 +873,7 @@ class HTS:
"low": [],
"vol": []}
while datetime.strptime(given_day + " 083000", '%Y%m%d %H%M%S') < datetime.now() < datetime.strptime(given_day + " 151000", '%Y%m%d %H%M%S'):
while datetime.strptime(given_day + " 083000", '%Y%m%d %H%M%S') < datetime.now() < datetime.strptime(given_day + " 151100", '%Y%m%d %H%M%S'):
second = datetime.now().strftime('%Y%m%d %H%M%S')
if second in timecheck and not timecheck[second]:
@@ -886,41 +887,35 @@ class HTS:
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data, upper, lower = self.analyze(result)
# 사야 할 시점과 팔아야 할 시점을 체크한다.
bsLine = self.checkTransaction_Realtime(data, upper, lower)
# 사야 할 시점/가격과 팔아야 할 시점/가격을 체크한다.
bs_buy_price, bs_sell_price = self.checkTransaction_Realtime(data, upper, lower)
buy_line = bsLine['buy']
# 주문 및 매도 처리
price = bsLine['buy'][len(buy_line)-1]
# 매수신청과 5원 높여서 매도신청
if price > 0:
if previous_price > 0:
if previous_price > price:
if buy_count < 200:
buy_count = 200
buy_count += 40
if buy_count > 500:
buy_count = 500
elif previous_price < price:
if buy_count > 400:
buy_count = 400
buy_count -= 40
if buy_count < 100:
buy_count = 100
previous_price = price
if bs_buy_price > 0:
if PREVIOUS_PRICE > 0:
if PREVIOUS_PRICE > bs_buy_price:
if BUY_COUNT < 100:
BUY_COUNT = 90
BUY_COUNT += 10
if BUY_COUNT > 250:
BUY_COUNT = 250
elif PREVIOUS_PRICE < bs_buy_price:
if BUY_COUNT > 250:
BUY_COUNT = 260
BUY_COUNT -= 10
if BUY_COUNT < 100:
BUY_COUNT = 100
PREVIOUS_PRICE = bs_buy_price
# 매수 주문
# 현재까지 매입금액이 7백만원 이하일 때만 매수를 한다.
if total_byu_amt < 7000000:
self.requestOrder("2", stock_code, buy_count , price)
if TOTAL_BUY_AMT < 7000000:
self.requestOrder("2", stock_code, BUY_COUNT , bs_buy_price)
## 매도 주문 (아래 잔고를 체크해서 매도를 호출하는 것으로 시도한다.)
#time.sleep(60)
#self.requestOrder("1", stock_code, buy_count , price + 5)
print("BUY", second, price)
logFp.write("%s,%s, %d\n" % ("BUY", second, price))
#self.requestOrder("1", stock_code, BUY_COUNT , price + 5)
print("BUY", second, bs_buy_price)
logFp.write("%s,%s, %d\n" % ("BUY", second, bs_buy_price))
logFp.flush()
# 가져온 만큼 데이터를 누적해서 파일로 작성한다.
@@ -936,18 +931,10 @@ class HTS:
jangoDic = self.requstJango()
if jangoDic and len(jangoDic.keys()) > 0:
for code in jangoDic:
total_byu_amt = jangoDic[code]['매입금액']
TOTAL_BUY_AMT = jangoDic[code]['매입금액']
if jangoDic[code]['매도가능'] > 0:
# 장부가 가격의 마지막 자리를 0으로 만든다. (2090 -> 2090, 2092 -> 2090, 2098 -> 2090)
sell_price = int(jangoDic[code]['장부가'] / 10) * 10
"""
# 만약 오후 1시 이전이라면 한 호가 (5원) 더 올려서 매도한다.
if datetime.strptime(given_day + " 092000", '%Y%m%d %H%M%S') < datetime.now() < datetime.strptime(given_day + " 104000", '%Y%m%d %H%M%S'):
lower_size = len(lower)
if lower != None and lower_size > 3:
if lower[lower_size-3] < lower[lower_size-2] < lower[lower_size-1]:
sell_price += 5
"""
# 장부가의 마지막 자리수를 가져온다.
last_number = int(jangoDic[code]['장부가']) % 10
if last_number in [0, 1, 2]:
@@ -982,9 +969,9 @@ if __name__ == "__main__":
#hts.currentStock(stock_codes)
for given_day in given_days:
#hts.writeStockData(stock_codes, given_day)
#for stock_code in stock_codes:
#hts.simulate(stock_code, given_day)
for stock_code in stock_codes:
hts.simulate(stock_code, given_day)
hts.buyRealTime(stock_codes, given_day)
#hts.buyRealTime(stock_codes[0], given_day)
print ("done...")