Automated Trading Development in Python, Kiwoom Securities API, Handling Login Events

Handling Login Events with Kiwoom Securities API

Automating stock trading is becoming an increasingly important factor in today’s investment environment. In this course, we will explain in detail how to handle login events during the development process of automated trading software using the Kiwoom Securities API with Python. The Kiwoom Securities API is very popular in the Korean stock market, offering powerful features and is particularly favored by many traders.

Introduction to Kiwoom Securities API

The Kiwoom Securities API is a programming interface provided by Kiwoom Securities that allows users to programmatically execute stock trading. By using the API, various tasks such as buying and selling stocks, checking balances, and collecting market data can be performed. The designed client software can primarily perform the following functions:

  • Buying and selling stocks
  • Real-time market data inquiry
  • Transaction history check
  • Profit and loss analysis

Setting Up the Environment

To use the Kiwoom Securities API, the following environment setup is required.

  1. Open a Kiwoom Securities account and register a certified certificate
  2. Install Kiwoom Securities Open API+ on your PC
  3. Install the necessary libraries

When you install Kiwoom Securities Open API+, dedicated DLL files that allow you to use <OpenAPI> are provided. To communicate with Python, you need to install the pywin32 library. To do this, enter the following command:

pip install pywin32

Handling Login Events

The first step of the Kiwoom Securities API is to log in. The basic procedure for handling login events is as follows:

  1. Initialize the API
  2. Send login request
  3. Check login success

Initializing the API

To use the API, you must first initialize the COM object. Below is a basic code example for initialization:

import win32com.client

# Initialize Kiwoom Securities API
kiwoom = win32com.client.Dispatch("KHOpenAPI.KHOpenAPICtrl.1")

Sending a Login Request

Login is accomplished by calling the <CommConnect> method. This method opens a login window and processes the login. Below is the code for sending a login request:

# Send login request
kiwoom.CommConnect()  # Create login window

Handling Login Events

After sending the login request, you need to handle the information about the login result. You can check the login result using the <OnEventConnect> event handler. This event is called upon successful login and can be handled as follows:

class MyKiwoom:
    def __init__(self):
        self.kiwoom = win32com.client.Dispatch("KHOpenAPI.KHOpenAPICtrl.1")
        self.kiwoom.OnEventConnect.connect(self.on_event_connect)

    def login(self):
        self.kiwoom.CommConnect()  # Send login request

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

# Example usage
my_kiwoom = MyKiwoom()
my_kiwoom.login()

Actions After Successful Login

After a successful login, you can now make various requests through the API. Below is how to retrieve a list of held stocks after logging in:

def get_stock_list(self):
        count = self.kiwoom.GetAccountList()
        stock_list = []
        for i in range(count):
            stock_code = self.kiwoom.GetAccountStock(i)
            if stock_code:
                stock_list.append(stock_code)
        return stock_list

# Example of retrieving held stocks
print(my_kiwoom.get_stock_list())

Conclusion and Further Advice

In this session, we learned how to handle login events using the Kiwoom Securities API. The possibilities for automating stock trading through various API methods are limitless. It is also advisable to explore how to build automated trading algorithms using real-time data.

Finally, remember that stock trading always carries risks, so be sure to conduct sufficient testing before proceeding with automated trading!