Python Automated Trading Development, Kiwoom Securities API, Fetching Account Information

Developing an automated trading system has become a very popular topic among stock investors these days. Such systems refer to mechanisms that use computer programs to buy and sell stocks automatically. In this course, we will learn about the first step in automated trading development using Python, focusing on how to retrieve account information using the Kiwoom Securities API.

1. What is the Kiwoom Securities API?

The Kiwoom Securities API is a service provided through Kiwoom Securities’ Open API, which enables users to view and trade stocks, futures, and options, as well as access account information and market data through programs. This API allows investors to automatically execute various strategies.

2. Preparing to Use the Kiwoom Securities API

To use the Kiwoom Securities API, the following preparations are necessary.

  1. Open a Kiwoom Securities account: You need to open an account with Kiwoom Securities.
  2. Apply for Open API: You need to apply for permission to use the Open API at Kiwoom Securities.
  3. Set up API environment: You need to configure the environment on the computer where the API will be used.

2.1 How to Apply for Open API

After logging into the Kiwoom Securities website, you can request permission to use the API through the Open API service application. Once your request is approved, you will receive an API key, allowing you to access the API.

2.2 API Environment Setup

To use the API, you need a package that can send and receive HTTP requests in Python. We will mainly use the pywin32 library to interact with Windows COM objects. This library can be installed using the following method.

pip install pywin32

3. Retrieving Account Information

Now, let’s learn how to retrieve account information through the Kiwoom Securities API with actual code.

3.1 Logging into the Kiwoom Securities API

To use the API, you must first log in. The following code shows how to create a login window using PyQt5 and check the login status.

import os
import sys
import pythoncom
import win32com.client

from PyQt5 import QtWidgets

class Kiwoom:
    def __init__(self):
        self.app = QtWidgets.QApplication(sys.argv)
        self.khw = win32com.client.Dispatch("KH OPENAPI")  # Create Kiwoom API object
        self.khw.OnEventConnect.connect(self._on_event_connect)

    def login(self):
        self.khw.CommConnect()  # Request login

    def _on_event_connect(self, err_code):
        if err_code == 0:
            print("Login successful")
        else:
            print("Login failed")

if __name__ == "__main__":
    kiwoom = Kiwoom()
    kiwoom.login()
    pythoncom.PumpThreads()  # Maintain event loop

3.2 Retrieving Account Information

If you have successfully logged in, you can now retrieve account information. Here is how to query account information through the Kiwoom Securities API.

class Kiwoom:
    # Following the previous code
    def get_account_info(self):
        account_list = self.khw.GetLoginInfo("ACCNO")  # Query account numbers
        print("Account list:", account_list)

        # Request information for all accounts
        for account in account_list.split(';'):
            if account:
                print("Account number:", account)

if __name__ == "__main__":
    kiwoom = Kiwoom()
    kiwoom.login()
    pythoncom.PumpThreads()
    kiwoom.get_account_info()  # Retrieve account information

The code above retrieves the user’s account list and prints each account number. You can retrieve account information using the GetLoginInfo method. Based on this information, you can retrieve other necessary information or proceed with trading.

4. Querying Asset Information by Account

Let’s learn how to query asset information for each account. You can check the quantity of stocks held and the assessed value, among other details.

class Kiwoom:
    # Following the previous code
    def get_account_assets(self):
        # Request asset information (AT: assessed value/current price)
        self.khw.SetInputValue("Account Number", "Enter account number")
        self.khw.SetInputValue("Password", "")  # Password
        self.khw.SetInputValue("Consultation Type", "0")  # 0: General, 1: Spot, 2: Futures, 3: Options
        self.khw.CommRcvData("010101", 0)  # Request

if __name__ == "__main__":
    kiwoom = Kiwoom()
    kiwoom.login()
    pythoncom.PumpThreads()
    kiwoom.get_account_assets()  # Retrieve asset information

5. Conclusion

In this course, we explained how to retrieve account information using the Kiwoom Securities API. In the future, you will learn how to implement trading functionalities and market information retrieval to develop your own automated trading system. In the next course, we will implement additional features for actual trading.

6. Reference Materials