This commit is contained in:
dosangyoon
2021-10-16 19:40:55 +09:00
parent b464987eba
commit 476e828226
3 changed files with 65 additions and 43 deletions

View File

@@ -545,12 +545,12 @@ class HTS:
return 0, 0
def buyRealTime(self, stock_code, given_day):
def buyRealTime(self, stock_code, GIVEN_DAY):
orderChecker = OrderChecker()
BASE_COUNT = 100
timecheckList = pd.read_csv("timecheck.csv").values.tolist()
timecheck = {given_day + " " + str(second).zfill(6):False for second, check in timecheckList}
timecheck = {GIVEN_DAY + " " + str(second).zfill(6):False for second, check in timecheckList}
result = {"check": set(),
"time": [],
@@ -562,15 +562,14 @@ class HTS:
final_price = 0
print ("START...")
while datetime.strptime(given_day + " 070000", '%Y%m%d %H%M%S') < datetime.now() < datetime.strptime(given_day + " 15200", '%Y%m%d %H%M%S'):
THIS_TIME = datetime.now()
while datetime.strptime(GIVEN_DAY + " 070000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(GIVEN_DAY + " 15200", '%Y%m%d %H%M%S'):
second = datetime.now().strftime('%Y%m%d %H%M%S')
if datetime.strptime(given_day + " 090100", '%Y%m%d %H%M%S') < datetime.now() < datetime.strptime(given_day + " 151000", '%Y%m%d %H%M%S'):
if second in timecheck and not timecheck[second]:
if datetime.strptime(GIVEN_DAY + " 090100", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(GIVEN_DAY + " 151000", '%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')]:
# 데이터를 가지고 온다.
self.getRealTime(stock_code, given_day, result)
self.getRealTime(stock_code, GIVEN_DAY, result)
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyze(result)
@@ -592,7 +591,7 @@ class HTS:
# 두 시간 이전 미체결을 취소한다.
self.cancelOrderList(orderListToCancel)
# 로그 출력
print("BUY", second, BUY_COUNT, bs_buy_price)
print("BUY", THIS_TIME, BUY_COUNT, bs_buy_price)
if bs_sell_price > 0:
# 매도 가격을 가져온다.
@@ -607,13 +606,13 @@ class HTS:
# 두 시간 이전 미체결을 취소한다.
self.cancelOrderList(orderListToCancel)
# 로그 출력
print("SELL", second, selling_count, selling_price)
print("SELL", THIS_TIME, selling_count, selling_price)
# 로그 출력
print("TIMECHECK", second, final_price, data["Low"][data_size-1], data["slow_k"][data_size-1], data["slow_d"][data_size-1])
timecheck[second] = True
print("TIMECHECK", THIS_TIME, final_price, data["Low"][data_size-1], data["slow_k"][data_size-1], data["slow_d"][data_size-1])
timecheck[THIS_TIME] = True
if datetime.strptime(given_day + " 151000", '%Y%m%d %H%M%S') < datetime.now():
if datetime.strptime(GIVEN_DAY + " 151000", '%Y%m%d %H%M%S') < THIS_TIME:
# 주문 리스트를 가져온다.
orderList = self.requestOrderList()
# 15:10:00 이후라면 모든 미체결 취소한다.
@@ -624,10 +623,11 @@ class HTS:
if selling_count != 0 and selling_price !=0:
orderNum = self.requestOrder(OrderType.sell, stock_code, selling_count, selling_price)
# 로그 출력
print("SELL", second, selling_count, selling_price)
print("SELL", THIS_TIME, selling_count, selling_price)
break
time.sleep(0.9)
THIS_TIME = datetime.now()
return

View File

@@ -18,28 +18,29 @@ class OrderChecker:
if os.path.isfile(fileName):
self.order_df = pd.read_csv(fileName)
else:
self.order_df = None
self.order_df = pd.DataFrame(columns=["type", "orderNum", "count", "price", "datetime"])
self.order_df = self.order_df.astype({"type": str, "orderNum": int, "count": int, "price": int})
self.order_df['datetime'] = pd.to_datetime(self.order_df['datetime'])
return
def add(self, type, orderNum, count, price, orderList):
orderListToCancel = []
if self.order_df is None:
self.order_df = pd.DataFrame()
else:
# 두 시간 전
before_two_hour = datetime.now() - timedelta(hours=2)
# 두 시간 전
before_two_hour = datetime.now() - timedelta(hours=2)
# 두 시간 전 주문을 찾는다.
if len(orderList) > 0:
# 만약 두시간 전 주문을 찾는다.
orderNum_df = self.order_df.loc[self.order_df["datetime"] <= before_two_hour]
orderNum_df = orderNum_df.loc[orderNum_df["type"] == OrderType.buy.value]
orderNumSet = set(list(orderNum_df["orderNum"]))
for item in orderList:
if item.orderNum in orderNumSet:
orderListToCancel.append(item)
# 해당 orderNum 제외하기
self.order_df = self.order_df.loc[self.order_df["orderNum"] != item.orderNum]
# 두 시간 전 주문을 찾는다.
if len(orderList) > 0:
# 만약 두시간 전 주문을 찾는다.
orderNum_df = self.order_df.loc[self.order_df["datetime"] <= before_two_hour]
orderNum_df = orderNum_df.loc[orderNum_df["type"] == OrderType.buy.value]
orderNumSet = set(list(orderNum_df["orderNum"]))
for item in orderList:
if item.orderNum in orderNumSet:
orderListToCancel.append(item)
# 해당 orderNum 제외하기
self.order_df = self.order_df.loc[self.order_df["orderNum"] != item.orderNum]
self.order_df = self.order_df.append({"type": type.value, "orderNum": orderNum, "count": count, "price": price, "datetime": datetime.now()}, ignore_index=True)
self.order_df.to_csv(self.saveFileName)
@@ -47,13 +48,19 @@ class OrderChecker:
return orderListToCancel
if __name__ == "__main__":
orderChecker = OrderChecker()
a = pd.DataFrame(columns=["type", "orderNum", "count", "price", "datetime"])
a = a.astype({"type": str, "orderNum": int, "count": int, "price": int})
a['datetime'] = pd.to_datetime(a['datetime'])
a = pd.DataFrame(columns=["a", "b", "c", "d", "e"])
a = a.append({"a": 0, "b": 1, "c": 3, "d": 7, "e": 8}, ignore_index=True)
a = a.append({"a": 1, "b": 3, "c": 0, "d": 6, "e": 6}, ignore_index=True)
a = a.append({"a": 2, "b": 3, "c": 0, "d": 3, "e": 9}, ignore_index=True)
a = a.append({"a": 3, "b": 9, "c": 9, "d": 8, "e": 4}, ignore_index=True)
this_time = datetime.now()
a = a.append({"type": "0", "orderNum": 1, "count": 3, "price": 7, "datetime": this_time}, ignore_index=True)
a = a.append({"type": "1", "orderNum": 3, "count": 0, "price": 6, "datetime": this_time - timedelta(hours=2)}, ignore_index=True)
a = a.append({"type": "2", "orderNum": 3, "count": 0, "price": 3, "datetime": this_time + timedelta(hours=1)}, ignore_index=True)
a = a.append({"type": "1", "orderNum": 9, "count": 9, "price": 8, "datetime": this_time}, ignore_index=True)
a = a.loc[a["b"] != 3]
print(a.tail())
b = a.loc[a["orderNum"] != 3]
print(b.tail())
print()
b = a.loc[a["datetime"] < this_time]
print(b.tail())

View File

@@ -1,5 +1,5 @@
import os
from datetime import datetime
from datetime import datetime, timedelta
import pandas as pd
import plotly.graph_objects as go
from plotly import subplots
@@ -133,7 +133,20 @@ class Simulation:
return
def simulate(self, stock_code, given_day):
def getAverageVolume(self, stock_code):
index = 0
while index < 10:
this_day = datetime.today() - timedelta(days=index+1)
fileName = "./data/"+stock_code+"_"+this_day.strftime("%Y%m%d")+".csv"
#fileName = "./data/" + stock_code + "_20211013.csv"
if os.path.isfile(fileName):
data = pd.read_csv(fileName)
return data
index += 1
return 2000000
def simulate(self, stock_code, GIVEN_DAY):
result = {"check": set(),
"time": [],
"open": [],
@@ -142,8 +155,10 @@ class Simulation:
"low": [],
"vol": []}
averageVolume = self.getAverageVolume(stock_code)
# 데이터를 가지고 온다.
self.getCSV("./data/"+stock_code+"_"+given_day+".csv", given_day, result)
self.getCSV("./data/"+stock_code+"_"+GIVEN_DAY+".csv", GIVEN_DAY, result)
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
data = self.buySellChecker.analyze(result)
@@ -152,7 +167,7 @@ class Simulation:
bsLine = self.checkTransaction(data)
# 그래프를 그린다.
self.draw(stock_code, given_day, data, bsLine)
self.draw(stock_code, GIVEN_DAY, data, bsLine)
return