init
This commit is contained in:
81
hts/HTS.py
81
hts/HTS.py
@@ -1,29 +1,14 @@
|
||||
import win32com.client
|
||||
import time
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime
|
||||
import pandas as pd
|
||||
from enum import Enum
|
||||
|
||||
from BS import BS
|
||||
from OrderType import OrderType
|
||||
from OrderItem import OrderItem
|
||||
|
||||
# enum 주문 상태 세팅용
|
||||
class EorderBS(Enum):
|
||||
buy = 1 # 매수
|
||||
sell= 2 # 매도
|
||||
none =3
|
||||
|
||||
class orderData:
|
||||
def __init__(self):
|
||||
self.dicEx = {EorderBS.buy: "매수", EorderBS.sell: "매도", EorderBS.none: "없음"}
|
||||
self.orderNum = 0
|
||||
self.bs = EorderBS.none # 0 : buy 1: sell
|
||||
self.code = ""
|
||||
self.amount = 0
|
||||
self.price = 0
|
||||
|
||||
def debugPrint(self):
|
||||
print(self.dicEx.get(self.bs), self.code, self.orderNum, self.amount, self.price)
|
||||
from BuySellChecker import BuySellChecker
|
||||
from OrderChecker import OrderChecker
|
||||
|
||||
|
||||
class HTS:
|
||||
@@ -31,10 +16,10 @@ class HTS:
|
||||
objCpCybos = None
|
||||
objCpCodeMgr = None
|
||||
|
||||
bs = None
|
||||
buySellChecker = None
|
||||
|
||||
def __init__(self):
|
||||
self.bs = BS()
|
||||
self.buySellChecker = BuySellChecker()
|
||||
#self.connect()
|
||||
return
|
||||
|
||||
@@ -189,13 +174,13 @@ class HTS:
|
||||
nRet = objStockOrder.BlockRequest()
|
||||
if (nRet != 0):
|
||||
print("order error", nRet)
|
||||
return
|
||||
return None
|
||||
|
||||
rqStatus = objStockOrder.GetDibStatus()
|
||||
rqRet = objStockOrder.GetDibMsg1()
|
||||
print("통신상태", rqStatus, rqRet)
|
||||
if rqStatus != 0:
|
||||
return
|
||||
return None
|
||||
|
||||
orderNum = objStockOrder.GetHeaderValue(0)
|
||||
|
||||
@@ -330,7 +315,7 @@ class HTS:
|
||||
break
|
||||
|
||||
for i in range(cnt):
|
||||
item = orderData()
|
||||
item = OrderItem()
|
||||
item.orderNum = objResult.GetDataValue(1, i)
|
||||
item.orderPrev = objResult.GetDataValue(2, i)
|
||||
item.code = objResult.GetDataValue(3, i) # 종목코드
|
||||
@@ -356,9 +341,7 @@ class HTS:
|
||||
return orderList
|
||||
|
||||
# 미체결 취소하기
|
||||
def cancelOrderList(self):
|
||||
#주문 리스트를 가져온다.
|
||||
orderList = self.requestOrderList()
|
||||
def cancelOrderList(self, orderList):
|
||||
|
||||
if len(orderList) < 1:
|
||||
return
|
||||
@@ -534,7 +517,7 @@ class HTS:
|
||||
if i < 5:
|
||||
return -1, -1, -1
|
||||
|
||||
buy, weight, sell = self.bs.getPriceAndWeight1(data, i)
|
||||
buy, weight, sell = self.buySellChecker.getPriceAndWeight1(data, i)
|
||||
return buy, weight, sell
|
||||
|
||||
def getSellingPrice(self, final_price):
|
||||
@@ -563,6 +546,9 @@ class HTS:
|
||||
return 0, 0
|
||||
|
||||
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}
|
||||
|
||||
@@ -587,19 +573,25 @@ class HTS:
|
||||
self.getRealTime(stock_code, given_day, result)
|
||||
|
||||
# 분석을 통해서 볼린저밴드 상/하단을 계산한다.
|
||||
data = self.bs.analyze(result)
|
||||
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:
|
||||
BUY_COUNT = int(200 * bs_weight)
|
||||
# 기본 100 주에 가중치를 추가해서 매수한다.
|
||||
BUY_COUNT = int(BASE_COUNT * bs_weight)
|
||||
|
||||
# 매수 전에 모든 미체결을 취소한다.
|
||||
# self.cancelOrderList()
|
||||
# 현재까지 매입금액이 7백만원 이하일 때만 매수를 한다.
|
||||
self.requestOrder("2", stock_code, bs_weight * BUY_COUNT , bs_buy_price)
|
||||
# 매수를 주문한다.
|
||||
orderNum = self.requestOrder(OrderType.buy, stock_code, BUY_COUNT , bs_buy_price)
|
||||
# 미체결 기록을 가져온다.
|
||||
orderList = self.requestOrderList()
|
||||
# 매수 주문을 기록한다.
|
||||
orderListToCancel = orderChecker.add("2", orderNum, BUY_COUNT, bs_buy_price, orderList)
|
||||
# 두 시간 이전 미체결을 취소한다.
|
||||
self.cancelOrderList(orderListToCancel)
|
||||
# 로그 출력
|
||||
print("BUY", second, BUY_COUNT, bs_buy_price)
|
||||
|
||||
if bs_sell_price > 0:
|
||||
@@ -607,20 +599,31 @@ class HTS:
|
||||
selling_count, selling_price = self.getSellingPrice(final_price)
|
||||
# 분석되 가격으로 매도 요청한다.
|
||||
if selling_count != 0 and selling_price != 0:
|
||||
self.requestOrder("1", stock_code, selling_count, selling_price)
|
||||
orderNum = self.requestOrder(OrderType.sell, stock_code, selling_count, selling_price)
|
||||
# 미체결 기록을 가져온다.
|
||||
orderList = self.requestOrderList()
|
||||
# 매도 주문을 기록한다.
|
||||
orderListToCancel = orderChecker.add(OrderType.sell, orderNum, selling_count, selling_price, orderList)
|
||||
# 두 시간 이전 미체결을 취소한다.
|
||||
self.cancelOrderList(orderListToCancel)
|
||||
# 로그 출력
|
||||
print("SELL", second, 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
|
||||
|
||||
if datetime.strptime(given_day + " 151000", '%Y%m%d %H%M%S') < datetime.now():
|
||||
# 15:10:00 이후라면 모든 미체결 취소
|
||||
self.cancelOrderList()
|
||||
# 주문 리스트를 가져온다.
|
||||
orderList = self.requestOrderList()
|
||||
# 15:10:00 이후라면 모든 미체결 취소한다.
|
||||
self.cancelOrderList(orderList)
|
||||
# 매도 가격을 가져온다.
|
||||
selling_count, selling_price = self.getSellingPrice(final_price)
|
||||
# 분석되 가격으로 매도 요청한다.
|
||||
if selling_count != 0 and selling_price !=0:
|
||||
self.requestOrder("1", stock_code, selling_count, selling_price)
|
||||
orderNum = self.requestOrder(OrderType.sell, stock_code, selling_count, selling_price)
|
||||
# 로그 출력
|
||||
print("SELL", second, selling_count, selling_price)
|
||||
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user