Recently, algorithmic trading has become increasingly popular in the financial markets. In particular, many investors are interested in developing automated trading systems using Python. This course will explain in detail how to retrieve stock codes and Korean stock names using the Kiwoom Securities API.
1. What is an Automated Trading System?
An automated trading system is a system that automatically executes stock trades according to a specific algorithm. This system combines advanced analytical tools with automated trading functions, allowing trading to be conducted in the market without human intervention. Python is a very useful language for building such systems, as it supports a variety of libraries and APIs.
2. Introduction to the Kiwoom Securities API
The Kiwoom Securities API is a Java-based API provided by Kiwoom Securities, which can be integrated for use with various languages, such as Python. Through this API, you can query real-time stock information and execute trading orders. Additionally, it allows you to retrieve codes and names of specific stocks, making it easier to obtain the information necessary for investing.
2.1 Installing the Kiwoom Securities API
To use the Kiwoom Securities API, you first need to open a Kiwoom Securities account and apply for the API. After opening the account, download and install the API from the Kiwoom Securities website. Once the installation is complete, you can connect to the Kiwoom Securities API using Python.
2.2 Preparing to Use the Kiwoom API in Python
To use the Kiwoom API in Python, you need to install the pyqt5
and comtypes
packages. Use the command below to install the packages.
pip install pyqt5 comtypes
3. Retrieving Stock Codes and Korean Stock Names
The process of retrieving stock codes and Korean stock names using the Kiwoom Securities API is as follows. First, you need to connect to the API and then request the information for the desired stock.
3.1 Setting Up Connection with the API
First, write the code to set up the connection with the Kiwoom Securities API. Refer to the example code below to set up the connection.
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import pyqtSlot, QObject
import win32com.client
class KiwoomAPI(QObject):
def __init__(self):
super().__init__()
self.kiwoom = win32com.client.Dispatch("KHOPENAPI.KHOpenAPICtrl.1")
self.kiwoom.OnEventConnect.connect(self.connected)
@pyqtSlot(int)
def connected(self, err_code):
print(f"Connected with error code: {err_code}")
if __name__ == "__main__":
app = QApplication(sys.argv)
kiwoom_api = KiwoomAPI()
kiwoom_api.kiwoom.CommConnect()
app.exec_()
3.2 Retrieving Stock Codes and Korean Stock Names
After connecting to the server, write the code to retrieve stock codes and Korean stock names. Use the CommGetData
function to request the data.
def get_stock_info(self, stock_code):
data = self.kiwoom.CommGetData("", "OPT10001", 0, "0", stock_code)
stock_name = data.split(';')[0]
return stock_name
stock_code = "005930" # Samsung Electronics
stock_name = kiwoom_api.get_stock_info(stock_code)
print(f"Stock Code: {stock_code}, Stock Name: {stock_name}")
3.3 Retrieving the List of All Stocks
There is also a method to retrieve a list of all stocks. Kiwoom Securities can return the entire stock list for a specific data code.
def get_all_stocks(self):
stock_list = []
for i in range(0, 1000): # Example: Looping through 1000 stocks
stock_code = self.kiwoom.CommGetData("", "OPT10001", i, "0", "")
if stock_code == "":
break
stock_list.append(stock_code)
return stock_list
all_stocks = kiwoom_api.get_all_stocks()
for stock in all_stocks:
print(stock) # Print each stock code
4. Conclusion
In this course, we examined how to retrieve stock codes and Korean stock names using the Kiwoom Securities API with Python. Based on this functionality, various automated trading systems can be built, which can be utilized for real-time data analysis and the establishment of trading strategies. Each investor will acquire the ability to respond to the market more efficiently through this.