Hello! In this post, we will explore how to request basic information using the Kiwoom Securities API, which is one of the methods for developing an automated trading system using Python. I believe many of you who are interested in stock trading have chosen this topic to automate your trading.
1. What is Kiwoom Securities API?
The Kiwoom Securities API is an interface that allows stock trading and information retrieval through a program provided by Kiwoom Securities. By using this API, you can access your Kiwoom Securities account to automatically perform stock trading and retrieve information. In particular, using Python allows for easy requests and responses handling, making it popular among many developers.
2. Setting Up the Development Environment
To use the Kiwoom Securities API, the following preparations are necessary:
- Open a Kiwoom Securities account
- Sign up for Kiwoom Securities OpenAPI+
- Install and set up Kiwoom Securities API
- Install Python and required libraries
2.1 Installing Kiwoom Securities OpenAPI+
Download and install the OpenAPI+ program provided by Kiwoom Securities. After installation, you need to log in to use the API.
2.2 Installing Python
If Python is not installed, please download and install it from the official website.
2.3 Installing Required Libraries
pip install pyqt5
The Kiwoom Securities API uses the Qt library, so you need to install PyQt5.
3. Accessing Kiwoom Securities API
To access the Kiwoom Securities API, you need to initialize the Kiwoom API first. You can write the following code for this.
import sys
from PyQt5.QtWidgets import QApplication
from kiwoom import Kiwoom
app = QApplication(sys.argv)
kiwoom = Kiwoom()
kiwoom.CommConnect() # Display the login window
This code creates a popup window for logging in using PyQt5. Once the user logs in with their account, they can access the API.
4. Requesting Basic Information
After logging in, you can request basic stock information by using the GetMasterCodeName method. This method allows you to retrieve the name corresponding to the stock code.
code = "005930" # Samsung Electronics
name = kiwoom.GetMasterCodeName(code)
print(name) # Samsung Electronics
4.1 Example of Stock Information Retrieval
The following code is an example that takes several stock codes as input and outputs their corresponding names.
codes = ["005930", "000660", "035420", "051910"] # Samsung Electronics, SK Hynix, NAVER, LG Household & Health Care
for code in codes:
name = kiwoom.GetMasterCodeName(code)
print(f"{code}: {name}")
5. Requesting Additional Information
In addition to retrieving basic stock information, you can request other information as well. For example, you can inquire about current price, opening price, high price, and low price. To do this, you can use the GetMasterLastPrice, GetMasterOpen, GetMasterHigh, and GetMasterLow methods.
current_price = kiwoom.GetMasterLastPrice(code)
open_price = kiwoom.GetMasterOpen(code)
high_price = kiwoom.GetMasterHigh(code)
low_price = kiwoom.GetMasterLow(code)
print(f"{code} Current Price: {current_price}, Opening Price: {open_price}, High Price: {high_price}, Low Price: {low_price}")
6. Conclusion
In this post, we looked at how to request basic information using the Kiwoom Securities API. Through the API, various information can be easily retrieved and utilized in automated trading logic. In the next post, we will explain how to fetch real-time stock information and the basic structure of an automated trading system using it. We appreciate your interest!