Recently, automated trading has gained popularity among traders in the financial markets. In particular, Python has established itself as a suitable language for developing automated trading systems due to its concise syntax and various libraries. This article will cover the initial setup and login method for automated trading using Kiwoom Securities’ Open API.
1. What is Kiwoom Securities Open API?
Kiwoom Securities Open API is an interface that allows users to programmatically execute trades utilizing various trading functions provided by the user. Developers can access a variety of financial products such as stocks, futures, and options through this API, and can receive trade orders and real-time data.
1.1 Advantages of the API
- Ease of developing automated trading systems
- Real-time market data collection
- Developer community support
- Compatibility with various programming languages
2. Prerequisites
To use Kiwoom Securities Open API, several prerequisites are necessary. You need a Kiwoom Securities account and an API key.
2.1 Creating a Kiwoom Securities Account
Visit the Kiwoom Securities website to create an account. After creating the account, configure the necessary settings through the API-related menu.
2.2 Issuing an API Key
After logging in, fill out the API application form to get your API key. This is essential for using the API.
2.3 Setting Up the Python Environment
Prepare the Python development environment. Anaconda or Visual Studio Code is recommended. The necessary libraries are as follows:
pip install pyqt5
pip install pandas
pip install numpy
3. Logging into Kiwoom Securities Open API+
Before using the API, you must first log in. The code below demonstrates the process of logging into the API.
3.1 Importing Kiwoom API Module
import win32com.client
import pythoncom
3.2 Defining Login-related Functions
Define a callback function for logging in. This is necessary for event handling with the API.
class Kiwoom:
def __init__(self):
self.tr = win32com.client.Dispatch("KHOPENAPI.KHOpenAPICtrl.1")
self.login_event_slot()
def login_event_slot(self):
self.tr.OnEventConnect.connect(self.login_event)
def login_event(self, err_code):
if err_code == 0:
print("Login Successful")
else:
print("Login Failed")
3.3 Executing Login
if __name__ == "__main__":
app = QtWidgets.QApplication([])
kiwoom = Kiwoom()
kiwoom.tr.CommConnect()
app.exec_()
Running the above code will display the Kiwoom Securities login window. After logging in, if the connection is successful, the message “Login Successful” will be printed.
4. Obtaining Stock Codes
After logging in, add the following code to obtain the code for the desired stock.
def get_code_list(self):
code_list = self.tr.GetCodeListByMarket(0) # 0: KOSPI
return code_list.split(';')
This function retrieves the codes of stocks listed on the KOSPI.
4.1 Printing Stock Codes
if __name__ == "__main__":
# Existing code ...
code_list = kiwoom.get_code_list()
print("KOSPI Stock Code List:", code_list)
5. Placing Stock Orders
Next, we will write code to allow placing orders. Below is an example of placing a buy order.
def buy_stock(self, code, quantity):
self.tr.SendOrder("Order Name", "130", "Stock Code", quantity, 0, "00", "0", "0", "")
The above function shows an example of buying stocks at market price.
5.1 Executing Buy Orders
if __name__ == "__main__":
# Existing code ...
kiwoom.buy_stock("005930", 1) # Buy 1 share of Samsung Electronics
6. Placing Sell Orders
Sell orders are processed similarly.
def sell_stock(self, code, quantity):
self.tr.SendOrder("Sell Order", "130", "Stock Code", -quantity, 0, "00", "0", "0", "")
When placing an order, the quantity value is entered as a negative number to execute a sell.
6.1 Executing Sell Orders
if __name__ == "__main__":
# Existing code ...
kiwoom.sell_stock("005930", 1) # Sell 1 share of Samsung Electronics
7. Receiving Real-time Data
One of the important aspects of trading is receiving real-time data. Below explains how to receive real-time data.
def setup_signal(self):
self.tr.OnReceiveRealData.connect(self.receive_real_data)
def receive_real_data(self, code, real_type, real_data):
print(f"Stock: {code}, Real-time Data: {real_type}, Data: {real_data}")
This function allows you to process real-time data.
7.1 Executing Real-time Data Reception
if __name__ == "__main__":
# Existing code ...
kiwoom.setup_signal()
kiwoom.tr.SetRealReg("0001", "005930", "20", "0") # Register real-time data for Samsung Electronics
8. Conclusion
In this article, we explored the initial setup and login method for automated trading using Kiwoom Securities Open API with Python. There are many more functionalities, so I encourage you to add the features you need and build your own automated trading system. Through continuous learning and experimentation, you can develop your own trading strategies.