PYTHON/Kiwoom Open API

키움증권 Open API - 거래 내역 조회하기 (3)

  • -

이번 게시글에서는 "주문구분"의 값이 "매수"인가 "매도"인가를 바탕으로 주문의 성격을 구분하고, 그를 바탕으로 거래 내역 데이터를 데이터프레임에 입력할 것이다.

다만 정말 중요하고 중요한 주의사항이 있다면, 이는 항상 일관적인 거래 방식을 따라 거래가 이루어졌어야 한다는 것이다. 예를 들어, 최대 2번의 분할매수가 이루어질 수 있고 매도는 최대 2번에 걸쳐 분할 매도가 이루어지는 거래 알고리즘이 있다고 가정해보자. 이 때 본인이 장을 보다가 프로그램이 아닌 본인이 "어, 이 종목은 사야겠는데?"하는 생각에 외부의 개입으로 추가적인 매수가 이루어진 경우에는 2번의 분할매수와 2번의 분할매도라는 기존의 알고리즘에서 벗어나기 때문에 거래 내역을 정확하게 조회할 수 없다(물론 본인이 편법을 써서 만들 수 있을 것 같다면 만들어도 된다.) 

 

 

거래 데이터를 입력할 변수 생성하기 : def _run_psb29_sub1()

일단 기본적으로 현재 제작하고 있는 함수인   self.opw00007()  는 앞전 게시글 중 1편에서 제작한   def _run_psb29_sub1()  을 통해 이루어졌기 때문에, 해당 함수 내에서 데이터를 입력받을 변수를 생성해주도록 하자. 여기서 생성하는 변수에 들어가는 데이터 목록은 개인의 거래 전략에 따라 모두 다르게 설정하면 된다.

본인의 경우에는 2차 분할 매수 이후 2차 분할 매도를 거치도록 설정했으며 특정 가격대를 벗어나면 손절매를 진행하도록 코드를 구축했기 때문에 데이터 목록이 많다. 여기서 생성하는 변수의 이름은   self.temporary_hold_account  이며, 데이터 형태는 데이터프레임에 해당한다.

  • 종목 정보 : 기준일자, 종목코드, 거래번호
  • 1차 매수 : 매수일, 매수 예정가격, 매수가격, 매수수량
  • 2차 매수 : 매수일, 매수 예정가격, 매수가격, 매수수량
  • 손절매 : 손절매일, 손절 예정가격, 손절가격, 손절수량
  • 1차 매도 : 매도일, 매도가격, 매도수량
  • 2차 매도 : 매도일, 매도가격, 매도수량
def _run_psb29_sub1(self):
	self.day_data = self.cal_day_data(10)
    

	temporary_hold_account = {'stan_date': [], 'item_code': [], 'identify_code': [],
	'first_buy_date': [], 'first': [], 'first_buy_price': [], 'first_buy_quan': [],
	'second_buy_date': [], 'second': [], 'second_buy_price': [], 'second_buy_quan': [],
	'cut_sell_date': [], 'cut': [], 'cut_sell_price': [], 'cut_sell_quan': [],
	'first_sell_date': [], 'first_sell_price': [], 'first_sell_quan': [],
	'second_sell_date': [], 'second_sell_price': [], 'second_sell_quan': [],
	'profit': [], 'profit_rate': [], 'lowest': []}

	self.temporary_hold_account = pd.DataFrame(temporary_hold_account, columns=['stan_date', 'item_code', 'identify_code',
	'first_buy_date', 'first', 'first_buy_price', 'first_buy_quan',
	'second_buy_date', 'second', 'second_buy_price', 'second_buy_quan',
	'cut_sell_date', 'cut', 'cut_sell_price', 'cut_sell_quan',
	'first_sell_date', 'first_sell_price', 'first_sell_quan',
	'second_sell_date', 'second_sell_price', 'second_sell_quan'])

	for day in reversed(self.day_data):
		self.opw00007_day = day
		self._setinputvalue("주문일자", day)
		self._setinputvalue("계좌번호", accno)
		self._setinputvalue("비밀번호", setting_value.password)
		self._setinputvalue("비밀번호입력매체구분", "00")
		self._setinputvalue("조회구분", 0)
		self._setinputvalue("주식채권구분", 0)
		self._setinputvalue("매도수구분", 0)
		self._setinputvalue("종목코드", "")
		self._setinputvalue("시작주문번호", "")
		self._commrqdata("rq_opw00007", "opw00007", 0, '0351')
		self.tr_event_loop = QEventLoop()
		self.tr_event_loop.exec_()
		time.sleep(0.3)

 

 


728x90

 

 

거래 데이터 입력하기 : def opw00007(trcode, recordname)

이제 앞서   def _run_psb29_sub1()  내에서   self.temporary_hold_account  라는 변수를 생성했으니, 이제는 다시   def opw00007  로 돌아와 해당 변수 안에 데이터를 입력해주면 된다. 여기서 주의해야 할 부분은 앞서 설명했듯, 주문구분의 데이터가 매수인가 매도인가에 따라 데이터를 다른 지점에 입력해주면 된다는 것이다.

그렇다면 일단 현재 조회하고자 하는 종목코드가   self.temporary_hold_account  내에서 몇 번째 지점에 위치해 있는지를 알아야만 해당 지점에 데이터를 입력할 수 있기 때문에, 종목코드가   self.temporary_hold_account  변수 내에서 갖고 있는 인덱스(index) 값을 얻어와보자.(11번째 줄에서 15번째 줄까지)

아래의 코드를 보면 알겠지만,   self.temporary_hold_account  변수의    ['item_code']  칼럼 안에 있는 종목코드 데이터를 리스트 형태로 바꾸어   code_list  라는 변수 안에 입력해주었고, 조건문(if 문)을 통해 거래 데이터가 있는 종목 코드 변수(  real_item_code  )가   code_list  변수 내에 있는지 없는지를 확인해주는 것이다.

이 코드를 사용해야 하는 이유는 ①거래 데이터가 없는 경우라면(아래의 elif에 해당) 데이터를 새롭게 입력해주어야 하고 ②데이터가 있는 경우라면(아래의 if에 해당) 해당 데이터의 index에 그대로 입력해주면 되기 때문이다.

def opw00007(self, trcode, recordname):
	repeat = self._getrepeatcnt(trcode, recordname)
 
	for i in range(repeat):
		item_code = self._getcommdata(trcode, recordname, i, "종목번호").strip()
		real_item_code = item_code[1:]  ## item_code는 A005930의 형식, real은 005930의 형식
		trade_num = int(self._getcommdata(trcode, recordname, i, "체결수량").strip())
		trade_price = int(self._getcommdata(trcode, recordname, i, "체결단가").strip())
		order_gubun = self._getcommdata(trcode, recordname, i, "주문구분").strip()
        
		code_list = self.temporary_hold_account['item_code'].to_list()
		if real_item_code in code_list:
			idx = self.temporary_hold_account.index[self.temporary_hold_account['item_code'] == real_item_code].to_list()[-1]
		elif real_item_code not in code_list:
			idx = len(code_list)

 

그 후에는 이제 "주문구분"(  order_gubun  )에 따라 데이터를 다른 지점에 입력해주면 된다.(17번째 줄부터 21번째 줄)

def opw00007(self, trcode, recordname):
	repeat = self._getrepeatcnt(trcode, recordname)
 
	for i in range(repeat):
		item_code = self._getcommdata(trcode, recordname, i, "종목번호").strip()
		real_item_code = item_code[1:]  ## item_code는 A005930의 형식, real은 005930의 형식
		trade_num = int(self._getcommdata(trcode, recordname, i, "체결수량").strip())
		trade_price = int(self._getcommdata(trcode, recordname, i, "체결단가").strip())
		order_gubun = self._getcommdata(trcode, recordname, i, "주문구분").strip()
        
		code_list = self.temporary_hold_account['item_code'].to_list()
		if real_item_code in code_list:
			idx = self.temporary_hold_account.index[self.temporary_hold_account['item_code'] == real_item_code].to_list()[-1]
		elif real_item_code not in code_list:
			idx = len(code_list)

		if order_gubun == "현금매수":
			pass

		if order_gubun == "현금매도":
			pass

 

 


728x90
반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.