Python Automated Trading Development, Kiwoom Securities API, Account Opening and Module Installation

The automated trading system is one of the rapidly growing fields in financial technology. Many traders are developing algorithm-based automated trading systems to enhance trading efficiency. This article will explain in detail how to use the Kiwoom Securities API required for automated trading development using Python, from opening an account to module installation.

1. Opening a Kiwoom Securities Account

To develop an automated trading system, you first need to open a securities account. Here is how to open an account with Kiwoom Securities.

1.1. Visit the Kiwoom Securities Website

Access the official Kiwoom Securities website (https://www.kiwoom.com) and click on the ‘Open Account’ menu.

1.2. Open Account Online

To open an account online, you need to complete identity verification. Prepare your digital certificate and identification (resident registration card, driver’s license).

1.3. Input Information and Submit Documents

Enter the required personal information and select the type of securities account. Then, verify your identification and submit the required documents.

1.4. Account Opening Completed

Once all procedures are completed, the account will be opened within about 1 to 2 days. After the account is opened, you will be eligible to use the API for automated trading programs.

2. Installing Kiwoom Securities OPEN API

To use Kiwoom Securities’ API, specific software is required. This software can be downloaded from Kiwoom’s website.

2.1. Download OPEN API

Kiwoom Securities OPEN API can be found in the ‘Download’ menu. Download several files related to the SDK.

2.2. Check User Manual

Kiwoom Securities provides a user manual for using the OPEN API. You can learn about the functionalities and usage of the API through the manual.

2.3. Environment Setup

After unzipping the downloaded files in an appropriate location, you need to set the environment variables. If you are a Windows user, add the following to the system variables in ‘My Computer’ > ‘Properties’ > ‘Advanced System Settings’ > ‘Environment Variables’:

PATH : C:\Kiwoom\OpenAPI\

2.4. API Authentication Key Issuance

To use Kiwoom Securities’ API, an authentication key is required. This key can be obtained by contacting Kiwoom Securities’ customer service.

3. Installing Python and Required Libraries

In this section, we will learn how to install Python and related libraries.

3.1. Install Python

Python can be installed from the official website (https://www.python.org/downloads/). Make sure to check the ‘Add Python to PATH’ option during the installation process.

3.2. Install PyQt5 and numpy Libraries

PyQt5 is needed to build the UI of the automated trading program. Additionally, Numpy will be used for data processing. Use the following command in CMD to install:

pip install PyQt5 numpy

4. Integrating Kiwoom Securities API with Python

Now, let’s develop a real trading program using the Kiwoom Securities API.

4.1. Define API Class

First, we define a class to call Kiwoom Securities’ OpenAPI.

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QAxContainer import QAxWidget

class Kiwoom:
    def __init__(self):
        self.app = QApplication(sys.argv)
        self.kw_app = QAxWidget('KWOpenAPI.KWOpenAPICtrl.1')
        self.kw_app.OnEventConnect.connect(self.event_connect)

    def event_connect(self, err_code):
        if err_code == 0:
            print("Connection successful")
        else:
            print(f"Connection failed: {err_code}")

    def login(self):
        self.kw_app.dynamicCall("CommConnect()")

if __name__ == "__main__":
    kiwoom = Kiwoom()
    kiwoom.login()
    sys.exit(kiwoom.app.exec_())

4.2. Add Trading Request Function

Add a function for trading requests.

def buy(self, code, qty):
    self.kw_app.dynamicCall("SendOrder(QString, QString, QString, int, QString, int, double, QString)", "buy", "YOUR_ACCOUNT", code, qty, "", "0", "0", "0")

4.3. Run and Test the Program

Run the entire code to check if the trading connection is functioning properly. The environment setup must be correct, and Kiwoom Securities’ API should work appropriately.

if __name__ == "__main__":
    kiwoom = Kiwoom()
    kiwoom.login()
    # Trading Test
    kiwoom.buy("005930", 1)  # Buy one share of Samsung Electronics
    sys.exit(kiwoom.app.exec_())

5. Conclusion

This article explained the basics of opening a Kiwoom Securities account, module installation, and API integration for automated trading development using Python. More testing and stability assurance are needed to execute actual trades. Additionally, risk management and various trading strategies are necessary for actual trading.

In the next article, we will explore the implementation of more advanced automated trading systems, data analysis, and algorithm improvement.

We hope that all readers can build a successful automated trading system!