{"id":37405,"date":"2024-11-01T09:57:19","date_gmt":"2024-11-01T09:57:19","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37405"},"modified":"2024-11-01T11:50:57","modified_gmt":"2024-11-01T11:50:57","slug":"automated-trading-development-with-python-kiwoom-securities-api-open-api-log-in","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37405\/","title":{"rendered":"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In"},"content":{"rendered":"<p><body><\/p>\n<article>\n<p>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&#8217; Open API.<\/p>\n<h2>1. What is Kiwoom Securities Open API?<\/h2>\n<p>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.<\/p>\n<h3>1.1 Advantages of the API<\/h3>\n<ul>\n<li>Ease of developing automated trading systems<\/li>\n<li>Real-time market data collection<\/li>\n<li>Developer community support<\/li>\n<li>Compatibility with various programming languages<\/li>\n<\/ul>\n<h2>2. Prerequisites<\/h2>\n<p>To use Kiwoom Securities Open API, several prerequisites are necessary. You need a Kiwoom Securities account and an API key.<\/p>\n<h3>2.1 Creating a Kiwoom Securities Account<\/h3>\n<p>Visit the Kiwoom Securities website to create an account. After creating the account, configure the necessary settings through the API-related menu.<\/p>\n<h3>2.2 Issuing an API Key<\/h3>\n<p>After logging in, fill out the API application form to get your API key. This is essential for using the API.<\/p>\n<h3>2.3 Setting Up the Python Environment<\/h3>\n<p>Prepare the Python development environment. Anaconda or Visual Studio Code is recommended. The necessary libraries are as follows:<\/p>\n<pre><code>pip install pyqt5\npip install pandas\npip install numpy\n<\/code><\/pre>\n<h2>3. Logging into Kiwoom Securities Open API+<\/h2>\n<p>Before using the API, you must first log in. The code below demonstrates the process of logging into the API.<\/p>\n<h3>3.1 Importing Kiwoom API Module<\/h3>\n<pre><code>import win32com.client\nimport pythoncom\n<\/code><\/pre>\n<h3>3.2 Defining Login-related Functions<\/h3>\n<p>Define a callback function for logging in. This is necessary for event handling with the API.<\/p>\n<pre><code>\nclass Kiwoom:\n    def __init__(self):\n        self.tr = win32com.client.Dispatch(\"KHOPENAPI.KHOpenAPICtrl.1\")\n        self.login_event_slot()\n\n    def login_event_slot(self):\n        self.tr.OnEventConnect.connect(self.login_event)\n\n    def login_event(self, err_code):\n        if err_code == 0:\n            print(\"Login Successful\")\n        else:\n            print(\"Login Failed\")\n<\/code><\/pre>\n<h3>3.3 Executing Login<\/h3>\n<pre><code>if __name__ == \"__main__\":\n    app = QtWidgets.QApplication([])\n    kiwoom = Kiwoom()\n    kiwoom.tr.CommConnect()\n    app.exec_()\n<\/code><\/pre>\n<p>Running the above code will display the Kiwoom Securities login window. After logging in, if the connection is successful, the message &#8220;Login Successful&#8221; will be printed.<\/p>\n<h2>4. Obtaining Stock Codes<\/h2>\n<p>After logging in, add the following code to obtain the code for the desired stock.<\/p>\n<pre><code>\ndef get_code_list(self):\n    code_list = self.tr.GetCodeListByMarket(0)  # 0: KOSPI\n    return code_list.split(';')\n<\/code><\/pre>\n<p>This function retrieves the codes of stocks listed on the KOSPI.<\/p>\n<h3>4.1 Printing Stock Codes<\/h3>\n<pre><code>if __name__ == \"__main__\":\n    # Existing code ...\n    code_list = kiwoom.get_code_list()\n    print(\"KOSPI Stock Code List:\", code_list)\n<\/code><\/pre>\n<h2>5. Placing Stock Orders<\/h2>\n<p>Next, we will write code to allow placing orders. Below is an example of placing a buy order.<\/p>\n<pre><code>\ndef buy_stock(self, code, quantity):\n    self.tr.SendOrder(\"Order Name\", \"130\", \"Stock Code\", quantity, 0, \"00\", \"0\", \"0\", \"\")\n<\/code><\/pre>\n<p>The above function shows an example of buying stocks at market price.<\/p>\n<h3>5.1 Executing Buy Orders<\/h3>\n<pre><code>if __name__ == \"__main__\":\n    # Existing code ...\n    kiwoom.buy_stock(\"005930\", 1)  # Buy 1 share of Samsung Electronics\n<\/code><\/pre>\n<h2>6. Placing Sell Orders<\/h2>\n<p>Sell orders are processed similarly.<\/p>\n<pre><code>\ndef sell_stock(self, code, quantity):\n    self.tr.SendOrder(\"Sell Order\", \"130\", \"Stock Code\", -quantity, 0, \"00\", \"0\", \"0\", \"\")\n<\/code><\/pre>\n<p>When placing an order, the quantity value is entered as a negative number to execute a sell.<\/p>\n<h3>6.1 Executing Sell Orders<\/h3>\n<pre><code>if __name__ == \"__main__\":\n    # Existing code ...\n    kiwoom.sell_stock(\"005930\", 1)  # Sell 1 share of Samsung Electronics\n<\/code><\/pre>\n<h2>7. Receiving Real-time Data<\/h2>\n<p>One of the important aspects of trading is receiving real-time data. Below explains how to receive real-time data.<\/p>\n<pre><code>\ndef setup_signal(self):\n    self.tr.OnReceiveRealData.connect(self.receive_real_data)\n\ndef receive_real_data(self, code, real_type, real_data):\n    print(f\"Stock: {code}, Real-time Data: {real_type}, Data: {real_data}\")\n<\/code><\/pre>\n<p>This function allows you to process real-time data.<\/p>\n<h3>7.1 Executing Real-time Data Reception<\/h3>\n<pre><code>if __name__ == \"__main__\":\n    # Existing code ...\n    kiwoom.setup_signal()\n    kiwoom.tr.SetRealReg(\"0001\", \"005930\", \"20\", \"0\")  # Register real-time data for Samsung Electronics\n<\/code><\/pre>\n<h2>8. Conclusion<\/h2>\n<p>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.<\/p>\n<h3>Helpful Resources<\/h3>\n<ul>\n<li><a href=\"https:\/\/www.kiwoom.com\/\">Kiwoom Securities Official Website<\/a><\/li>\n<li><a href=\"https:\/\/www.kiwoom.com\/nkw.template?menuid=1401\">Open API Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.pyqtgraph.org\/\">PyQtGraph<\/a><\/li>\n<\/ul>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217; Open API. 1. What &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37405\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[147],"tags":[],"class_list":["post-37405","post","type-post","status-publish","format-standard","hentry","category-python-auto-trading"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/37405\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"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&#8217; Open API. 1. What &hellip; \ub354 \ubcf4\uae30 &quot;Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37405\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:57:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:50:57+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/37405\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37405\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In\",\"datePublished\":\"2024-11-01T09:57:19+00:00\",\"dateModified\":\"2024-11-01T11:50:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37405\/\"},\"wordCount\":507,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Auto Trading\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37405\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37405\/\",\"name\":\"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:57:19+00:00\",\"dateModified\":\"2024-11-01T11:50:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37405\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37405\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37405\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/37405\/","og_locale":"ko_KR","og_type":"article","og_title":"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"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&#8217; Open API. 1. What &hellip; \ub354 \ubcf4\uae30 \"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In\"","og_url":"https:\/\/atmokpo.com\/w\/37405\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:57:19+00:00","article_modified_time":"2024-11-01T11:50:57+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/37405\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37405\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In","datePublished":"2024-11-01T09:57:19+00:00","dateModified":"2024-11-01T11:50:57+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37405\/"},"wordCount":507,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Auto Trading"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37405\/","url":"https:\/\/atmokpo.com\/w\/37405\/","name":"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:57:19+00:00","dateModified":"2024-11-01T11:50:57+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37405\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37405\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37405\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Automated Trading Development with Python, Kiwoom Securities API, Open API + Log In"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=37405"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37405\/revisions"}],"predecessor-version":[{"id":37406,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37405\/revisions\/37406"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}