Files
kis_trader/sample_python/tr/tr_order.py
2026-07-30 18:05:07 +09:00

198 lines
6.5 KiB
Python

from dotenv import load_dotenv
import os
from tr.tr_0424 import Tr_0424
from PySide2 import QtTest
from PySide2 import QtCore
# from order_window import Order_window
from balance_window import Balance_window
import win32com.client #이베스트 관련
import pythoncom #이베스트 관련
load_dotenv()
class Tr_order:
# class Worker(QtCore.QThread):
# # def __init__(self,tr_0424):
# # self.tr_0424=tr_0424
# def call(self,tr_0424):
# self.tr_0424=tr_0424
# def run(self):
# while True:
# self.sleep(2)
# self.tr_0424.get_account(Balance_window)
def __init__(self):
super().__init__()
self.account=os.environ.get('ACCOUNT')
# self.tr_0424=win32com.client.DispatchWithEvents("XA_DataSet.XAQuery",Tr_0424)#계좌티알
# self.tr_0424.get_account(Balance_window)
# self.worker = self.Worker()
# self.worker.call(self.tr_0424)
# self.worker.start()
# self.order_window=Order_window()
self.the_pending={}#미체결
self.the_pending_list=[]
self.the_pending_temp={}#오더 에코 떨어지기 전에 메모리에 올려놓는 미체결
def init_set_text(self,func):
self.set_status_label=func
def OnReceiveData(self, tr_code):
# print('주문 에코: ',tr_code)
# self.tr_0424.get_account(Balance_window)
OrdQty = self.GetFieldData("CSPAT00600OutBlock1", "OrdQty",0).strip()
# print('AcntNo: ',OrdQty)
# if tr_code=='CSPAT00600':
# df,df1=result
# XAQueryEvents.query_state = True
# block_count=self.GetBlockCount("t8430OutBlock")
# #print('block_count: ',block_count)
# for i in range(block_count):
# hname = self.GetFieldData("t8430OutBlock", "hname", i).strip()
# shcode = self.GetFieldData("t8430OutBlock", "shcode", i).strip()
# gubun = self.GetFieldData("t8430OutBlock", "gubun", i).strip() #1은 코스피, 2는 코스닥
# master_dict[shcode]={
# 'hname':hname,
# 'gubun':gubun
# }
# #print('master_dict: ',master_dict['005930'])
# query_1857.set_master(master_dict)
# hname = self.GetFieldData("t8430OutBlock", "hname", 0)
# shcode = self.GetFieldData("t8430OutBlock", "shcode", 0)
# #print('tr_code: ',tr_code)
# #print('hname: ',hname)
# #print('shcode: ',shcode)
# param['hname']=hname
# param['shcode']=shcode
pass
def OnReceiveMessage(self, error, nMessageCode, szMessage):
# print('OnReceiveMessage nMessageCode: ',nMessageCode)
# print('OnReceiveMessage szMessage: ',szMessage)
if nMessageCode=='00040':
#미체결 obj 메모리에 올리는 부분
# print('the_pending: ',self.the_pending)
self.the_pending=self.the_pending_temp
# print('the_pending2: ',self.the_pending)
self.set_status_label(szMessage)
pass
def shoot_order(self,stock_code,how_many,price,open,order_price,order_style,order_type):
#order_style이 매수인지 매도인지 그거 말하는거임
#order_type은 지정가인지 시장가인지 그거임
# 00@지정가
# 03@시장가
is_market='00'
if order_type=='market':
is_market='03'
order_price=0
is_ask='1'
if order_style=='bid':
is_ask='2'
order_dict={
'account':self.account,
'stock_code':stock_code,
'how_many':how_many,
'order_price':order_price,
'is_ask':is_ask,
'is_market':is_market,
'order_type':order_type
}
#미체결에 넣는다 같은 스톡코드라도 리스트로 뿌려야되는거 아님?
#걍 미체결 리얼에서 떨어지는거 찍어볼까
# pending_stock=self.the_pending_temp.get(stock_code,None)
# self.the_pending_list.append({
# })
# if pending_stock is None:
# self.the_pending_temp[stock_code]={
# 'stock_code':stock_code,
# 'how_many':how_many,
# 'order_price':order_price,
# }
# else:
# self.the_pending_temp[stock_code]['how_many']=how_many
#미체결에 넣는다 끝
stock_code="A"+stock_code#모투라 이렇게 해놈
the_path=os.environ.get('ROOT_FILE_PATH')
self.ResFileName = the_path+"\\Res\\CSPAT00600.res"
self.ResFileName = "C:\\eBEST\\xingAPI\\Res\\CSPAT00600.res"
self.SetFieldData("CSPAT00600InBlock1", "AcntNo", 0, self.account)
self.SetFieldData("CSPAT00600InBlock1", "InptPwd", 0, "0000") #모투는 0000임
self.SetFieldData("CSPAT00600InBlock1", "IsuNo", 0, stock_code) #모투는 앞에 A 더하래
self.SetFieldData("CSPAT00600InBlock1", "OrdQty", 0, how_many) #얘네 왜 스트링임?
self.SetFieldData("CSPAT00600InBlock1", "OrdPrc", 0, str(order_price))#얘네 왜 스트링임?
self.SetFieldData("CSPAT00600InBlock1", "BnsTpCode", 0, is_ask)#1이 매도 2가 매수
# 00@지정가
# 03@시장가
# 05@조건부지정가
# 06@최유리지정가
# 07@최우선지정가
# 61@장개시전시간외종가
# 81@시간외종가
# 82@시간외단일가
self.SetFieldData("CSPAT00600InBlock1", "OrdprcPtnCode", 0, is_market)
# 000:보통
# 003:유통/자기융자신규
# 005:유통대주신규
# 007:자기대주신규
# 101:유통융자상환
# 103:자기융자상환
# 105:유통대주상환
# 107:자기대주상환
# 180:예탁담보대출상환(신용)
self.SetFieldData("CSPAT00600InBlock1", "MgntrnCode", 0, '000')
self.SetFieldData("CSPAT00600InBlock1", "LoanDt", 0, '')#대출일이라는데 일단 비워둠
self.SetFieldData("CSPAT00600InBlock1", "OrdCndiTpCode", 0, '0') #0:없음,1:IOC,2:FOK 주문조건구분
#print('ORDER!!!!!')
err_code = self.Request(False)
if err_code < 0:
pass
#print("error... {0}".format(err_code))