{"id":37425,"date":"2024-11-01T09:57:28","date_gmt":"2024-11-01T09:57:28","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37425"},"modified":"2024-11-01T11:50:53","modified_gmt":"2024-11-01T11:50:53","slug":"python-automated-trading-development-kiwoom-securities-api-widgets-and-windows","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37425\/","title":{"rendered":"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows"},"content":{"rendered":"<p>In recent years, the changes in financial markets have been progressing very rapidly. In particular, automated trading systems are providing more opportunities for individual investors. This article will start from the basics of automated trading development using Python, explain how to use the Kiwoom Securities API, and detail the use of GUI widgets and windows.<\/p>\n<h2>1. Understanding Automated Trading Systems<\/h2>\n<p>An automated trading system is software that automatically executes trades when certain conditions are met. These systems can be applied to various assets such as stocks, foreign exchange, and cryptocurrencies. Many approaches can be used in this system, including algorithmic trading, high-frequency trading, and event-driven trading.<\/p>\n<h2>2. Python and Automated Trading<\/h2>\n<p>Python is a very popular language for developing stock trading algorithms, along with powerful data processing libraries. Data analysis libraries such as <strong>Pandas<\/strong>, <strong>Numpy<\/strong>, and <strong>Matplotlib<\/strong> make data collection and processing easier.<\/p>\n<h2>3. Overview of Kiwoom Securities API<\/h2>\n<p>Kiwoom Securities is one of the most popular securities firms in Korea, offering an API for stock trading. This allows users to conduct stock trades directly through a program.<\/p>\n<p>The Kiwoom Securities API is called <strong>Kiwoom Open API<\/strong>, providing functionalities such as real-time data collection, order execution, and account information verification. To use this API, you need to install the HTS (Home Trading System) provided by Kiwoom Securities and configure settings for API usage.<\/p>\n<h3>3.1. Installing and Configuring the Kiwoom Securities API<\/h3>\n<ol>\n<li>Download and install the HTS from the Kiwoom Securities website.<\/li>\n<li>After installation, configure the settings for API usage in the program.<\/li>\n<li>Apply for API usage through the Kiwoom Securities customer center.<\/li>\n<li>Obtain the API key and prepare it for use in the code.<\/li>\n<\/ol>\n<h2>4. Using the Kiwoom Securities API in Python<\/h2>\n<p>The Kiwoom Securities API is a COM (Component Object Model)-based API; to use this API in Python, you need a library called <strong>pywin32<\/strong>. Below is how to install this library.<\/p>\n<pre><code>pip install pywin32<\/code><\/pre>\n<h3>4.1. Example Code for Kiwoom Securities API<\/h3>\n<p>Below is a basic example code to connect to the Kiwoom Securities API and retrieve account information using Python.<\/p>\n<pre><code>import win32com.client\nimport pythoncom\nimport time\n\n# Initialize Kiwoom API\nkiwoom = win32com.client.Dispatch(\"KHOPENAPI.KHOpenAPICtrl.1\")\n\n# Initialize COM\ndef run_loop():\n    pythoncom.PumpWaitingMessages()\n\n# Login\nkiwoom.CommConnect()\n\n# Wait after login\nrun_loop()\n    \n# Retrieve account number\naccount_num = kiwoom.GetLoginInfo(\"ACCNO\")\nprint(f\"Account Number: {account_num}\")\n\n# Retrieve account balance\ndef get_account_balance(account_num):\n    kiwoom.SetInputValue(\"Account Number\", account_num)\n    kiwoom.CommRqData(\"Balance Inquiry\", \"opw00001\", 0, \"4100\")\n\nget_account_balance(account_num)\n<\/code><\/pre>\n<h2>5. Implementing Automated Trading Logic<\/h2>\n<p>The core of an automated trading system is the trading logic. It is necessary to generate buy and sell signals based on specific trading strategies and execute them through the API.<\/p>\n<h3>5.1. Example of a Basic Trading Strategy<\/h3>\n<p>Below is an example implementing a simple moving average crossover strategy. This strategy generates a buy signal when the short-term moving average crosses above the long-term moving average and generates a sell signal in the opposite case.<\/p>\n<pre><code>\nimport pandas as pd\n\n# Stock data collection function\ndef get_stock_data(stock_code, start_date, end_date):\n    # Fetch stock data (can use yfinance or another data source)\n    return pd.DataFrame()  # Return the actual stock data frame\n\n# Trading strategy function\ndef trading_strategy(stock_code):\n    data = get_stock_data(stock_code, '2022-01-01', '2023-01-01')\n    \n    # Calculate moving averages\n    data['short_ma'] = data['close'].rolling(window=5).mean()\n    data['long_ma'] = data['close'].rolling(window=20).mean()\n    \n    # Generate buy and sell signals\n    data['signal'] = 0\n    data['signal'][data['short_ma'] > data['long_ma']] = 1  # Buy signal\n    data['signal'][data['short_ma'] < data['long_ma']] = -1  # Sell signal\n    \n    return data.final_signals\n\n# Test\nstock_code = 'A005930'  # Samsung Electronics\nsignals = trading_strategy(stock_code)\nprint(signals)\n<\/code><\/pre>\n<h2>6. GUI Widgets and Windows<\/h2>\n<p>When developing an automated trading system, the user interface should also be considered. Libraries such as <strong>PyQt<\/strong> or <strong>Tkinter<\/strong> can be used to create a user-friendly GUI.<\/p>\n<h3>6.1. Simple GUI Example Using Tkinter<\/h3>\n<p>Below is a simple example of a GUI for an automated trading system implemented using Tkinter.<\/p>\n<pre><code>\nimport tkinter as tk\nfrom tkinter import messagebox\n\ndef start_trading():\n    # Implement the trading start feature\n    messagebox.showinfo(\"Notification\", \"Automated trading started\")\n\n# GUI setup\nroot = tk.Tk()\nroot.title(\"Automated Trading System\")\n\nstart_button = tk.Button(root, text=\"Start Automated Trading\", command=start_trading)\nstart_button.pack()\n\nroot.mainloop()\n<\/code><\/pre>\n<h2>7. Conclusion<\/h2>\n<p>This article introduced basic knowledge and example codes necessary for developing an automated trading system using Python. By using the Kiwoom Securities API, you can collect real-time data and implement a system that interacts with users through a GUI. Automated trading systems provide many benefits to individual investors; however, incorrect strategies or excessive risk settings can lead to significant losses, so a cautious approach is necessary.<\/p>\n<p>In the future, I hope to contribute to successful investments while studying the advancement of automated trading systems and various strategies.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In recent years, the changes in financial markets have been progressing very rapidly. In particular, automated trading systems are providing more opportunities for individual investors. This article will start from the basics of automated trading development using Python, explain how to use the Kiwoom Securities API, and detail the use of GUI widgets and windows. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37425\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows&#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-37425","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>Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows - \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\/37425\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In recent years, the changes in financial markets have been progressing very rapidly. In particular, automated trading systems are providing more opportunities for individual investors. This article will start from the basics of automated trading development using Python, explain how to use the Kiwoom Securities API, and detail the use of GUI widgets and windows. &hellip; \ub354 \ubcf4\uae30 &quot;Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37425\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:57:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:50:53+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\/37425\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37425\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows\",\"datePublished\":\"2024-11-01T09:57:28+00:00\",\"dateModified\":\"2024-11-01T11:50:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37425\/\"},\"wordCount\":561,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Auto Trading\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37425\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37425\/\",\"name\":\"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:57:28+00:00\",\"dateModified\":\"2024-11-01T11:50:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37425\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37425\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37425\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows\"}]},{\"@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":"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows - \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\/37425\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In recent years, the changes in financial markets have been progressing very rapidly. In particular, automated trading systems are providing more opportunities for individual investors. This article will start from the basics of automated trading development using Python, explain how to use the Kiwoom Securities API, and detail the use of GUI widgets and windows. &hellip; \ub354 \ubcf4\uae30 \"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows\"","og_url":"https:\/\/atmokpo.com\/w\/37425\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:57:28+00:00","article_modified_time":"2024-11-01T11:50:53+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\/37425\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37425\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows","datePublished":"2024-11-01T09:57:28+00:00","dateModified":"2024-11-01T11:50:53+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37425\/"},"wordCount":561,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Auto Trading"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37425\/","url":"https:\/\/atmokpo.com\/w\/37425\/","name":"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:57:28+00:00","dateModified":"2024-11-01T11:50:53+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37425\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37425\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37425\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Automated Trading Development, Kiwoom Securities API, Widgets and Windows"}]},{"@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\/37425","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=37425"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37425\/revisions"}],"predecessor-version":[{"id":37426,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37425\/revisions\/37426"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}