{"id":37395,"date":"2024-11-01T09:57:13","date_gmt":"2024-11-01T09:57:13","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37395"},"modified":"2024-11-01T11:51:00","modified_gmt":"2024-11-01T11:51:00","slug":"automated-trading-development-in-python-drawing-moving-averages","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37395\/","title":{"rendered":"Automated Trading Development in Python, Drawing Moving Averages"},"content":{"rendered":"<p><body><\/p>\n<p>The automated trading system refers to a program that automatically executes investment decisions in various financial markets such as stocks, forex, and cryptocurrencies. When developing such systems, various technical indicators are used to generate trading signals, one of which is the Moving Average (MA). The moving average is a useful tool for identifying price trend changes over a specific period. In this article, we will explain in detail how to plot moving averages using Python.<\/p>\n<h2>Overview of Moving Averages<\/h2>\n<p>A moving average is calculated by averaging the prices of a stock or other price indicators over a specific period and displayed on a chart. There are several types based on the form of the moving average:<\/p>\n<ul>\n<li><strong>Simple Moving Average (SMA)<\/strong>: The simple average of prices over the selected period.<\/li>\n<li><strong>Exponential Moving Average (EMA)<\/strong>: An average calculated with more weight on recent prices, showing a more sensitive response.<\/li>\n<li><strong>Weighted Moving Average (WMA)<\/strong>: An average calculated by assigning weights to each price.<\/li>\n<\/ul>\n<p>In this tutorial, we will use the simple moving average (SMA) as an example to demonstrate how to plot it on a graph.<\/p>\n<h2>Installing Required Libraries<\/h2>\n<p>To plot moving averages in Python, the following libraries are needed:<\/p>\n<ul>\n<li><code>pandas<\/code>: A library for data processing and analysis.<\/li>\n<li><code>numpy<\/code>: A library for numerical calculations.<\/li>\n<li><code>matplotlib<\/code>: A library for data visualization.<\/li>\n<li><code>yfinance<\/code>: A library to fetch data from Yahoo Finance.<\/li>\n<\/ul>\n<p>You can install the libraries using the following command:<\/p>\n<pre><code>pip install pandas numpy matplotlib yfinance<\/code><\/pre>\n<h2>Fetching Stock Data<\/h2>\n<p>Now, let&#8217;s learn how to fetch stock data from Yahoo Finance. The following code can be used to fetch data for Apple Inc.:<\/p>\n<pre><code>import yfinance as yf\n\n# Download Apple stock data\nticker = 'AAPL'\ndata = yf.download(ticker, start='2020-01-01', end='2021-01-01')\nprint(data.head())<\/code><\/pre>\n<p>The above code retrieves and prints the Apple stock data from January 1, 2020, to January 1, 2021. The data consists of columns such as Date, Open, High, Low, Close, and Volume.<\/p>\n<h2>Calculating the Simple Moving Average<\/h2>\n<p>Calculating the moving average is very simple. Using the <code>rolling<\/code> function from the <code>pandas<\/code> library, you can easily calculate the moving average for a specific period. For example, the code to calculate the 20-day moving average is as follows:<\/p>\n<pre><code># Calculate the 20-day moving average\ndata['SMA_20'] = data['Close'].rolling(window=20).mean()<\/code><\/pre>\n<p>The above code adds a new column \u2018SMA_20\u2019 based on the closing prices of Apple stock data for the 20-day moving average.<\/p>\n<h2>Visualizing the Moving Average<\/h2>\n<p>Now it\u2019s time to visualize the calculated moving average on a chart. We will proceed with the visualization using the <code>matplotlib<\/code> library:<\/p>\n<pre><code>import matplotlib.pyplot as plt\n\n# Visualization\nplt.figure(figsize=(14, 7))\nplt.plot(data['Close'], label='Close Price', color='blue')\nplt.plot(data['SMA_20'], label='20-Day Moving Average', color='orange')\nplt.title('Apple Stock (Weekly) Closing Price and Moving Average')\nplt.xlabel('Date')\nplt.ylabel('Price ($)')\nplt.legend()\nplt.grid()\nplt.show()<\/code><\/pre>\n<p>When you run the code, a chart will appear showing both the closing price and the 20-day moving average of Apple stock. The blue line represents the closing price, and the orange line represents the 20-day moving average.<\/p>\n<h2>Changing Parameters to Plot Different Moving Averages<\/h2>\n<p>You can also add moving averages for different periods. For example, let\u2019s add the 50-day and 200-day moving averages:<\/p>\n<pre><code># Calculate the 50-day and 200-day moving averages\ndata['SMA_50'] = data['Close'].rolling(window=50).mean()\ndata['SMA_200'] = data['Close'].rolling(window=200).mean()\n\n# Visualization\nplt.figure(figsize=(14, 7))\nplt.plot(data['Close'], label='Close Price', color='blue')\nplt.plot(data['SMA_20'], label='20-Day Moving Average', color='orange')\nplt.plot(data['SMA_50'], label='50-Day Moving Average', color='green')\nplt.plot(data['SMA_200'], label='200-Day Moving Average', color='red')\nplt.title('Apple Stock (Weekly) Closing Price and Moving Averages')\nplt.xlabel('Date')\nplt.ylabel('Price ($)')\nplt.legend()\nplt.grid()\nplt.show()<\/code><\/pre>\n<p>Executing this code will generate a chart displaying three different moving averages. Visualizing different moving averages can provide clearer insights into market trends.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we discussed how to calculate and visualize moving averages using Python. Moving averages are helpful in understanding market trends and generating trading signals. We can integrate moving averages into automated trading systems, enabling smarter trading strategies. The moving average is one of the most fundamental yet effective tools in the development of automated trading systems.<\/p>\n<h2>Next Steps<\/h2>\n<p>Building upon this foundation, you can develop more complex trading strategies and extend the system by using other technical indicators or algorithms. For example, you can use moving averages along with the RSI (Relative Strength Index). Diversify and experiment with your strategy!<\/p>\n<h2>References<\/h2>\n<ul>\n<li>Wikipedia, <a href=\"https:\/\/ko.wikipedia.org\/wiki\/%EC%9D%B4%EB%8F%99%ED%8F%89%EA%B7%A0\" target=\"_blank\" rel=\"noopener\">Moving Average<\/a><\/li>\n<li>Yahoo Finance, <a href=\"https:\/\/finance.yahoo.com\/\" target=\"_blank\" rel=\"noopener\">Financial Data<\/a><\/li>\n<li>Python Pandas Documentation, <a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/\" target=\"_blank\" rel=\"noopener\">Official Pandas Documentation<\/a><\/li>\n<li>Matplotlib Documentation, <a href=\"https:\/\/matplotlib.org\/stable\/contents.html\" target=\"_blank\" rel=\"noopener\">Official Matplotlib Documentation<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The automated trading system refers to a program that automatically executes investment decisions in various financial markets such as stocks, forex, and cryptocurrencies. When developing such systems, various technical indicators are used to generate trading signals, one of which is the Moving Average (MA). The moving average is a useful tool for identifying price trend &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37395\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Automated Trading Development in Python, Drawing Moving Averages&#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-37395","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 in Python, Drawing Moving Averages - \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\/37395\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automated Trading Development in Python, Drawing Moving Averages - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"The automated trading system refers to a program that automatically executes investment decisions in various financial markets such as stocks, forex, and cryptocurrencies. When developing such systems, various technical indicators are used to generate trading signals, one of which is the Moving Average (MA). The moving average is a useful tool for identifying price trend &hellip; \ub354 \ubcf4\uae30 &quot;Automated Trading Development in Python, Drawing Moving Averages&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37395\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:57:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:51:00+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\/37395\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37395\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Automated Trading Development in Python, Drawing Moving Averages\",\"datePublished\":\"2024-11-01T09:57:13+00:00\",\"dateModified\":\"2024-11-01T11:51:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37395\/\"},\"wordCount\":594,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Auto Trading\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37395\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37395\/\",\"name\":\"Automated Trading Development in Python, Drawing Moving Averages - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:57:13+00:00\",\"dateModified\":\"2024-11-01T11:51:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37395\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37395\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37395\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automated Trading Development in Python, Drawing Moving Averages\"}]},{\"@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 in Python, Drawing Moving Averages - \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\/37395\/","og_locale":"ko_KR","og_type":"article","og_title":"Automated Trading Development in Python, Drawing Moving Averages - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"The automated trading system refers to a program that automatically executes investment decisions in various financial markets such as stocks, forex, and cryptocurrencies. When developing such systems, various technical indicators are used to generate trading signals, one of which is the Moving Average (MA). The moving average is a useful tool for identifying price trend &hellip; \ub354 \ubcf4\uae30 \"Automated Trading Development in Python, Drawing Moving Averages\"","og_url":"https:\/\/atmokpo.com\/w\/37395\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:57:13+00:00","article_modified_time":"2024-11-01T11:51:00+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\/37395\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37395\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Automated Trading Development in Python, Drawing Moving Averages","datePublished":"2024-11-01T09:57:13+00:00","dateModified":"2024-11-01T11:51:00+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37395\/"},"wordCount":594,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Auto Trading"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37395\/","url":"https:\/\/atmokpo.com\/w\/37395\/","name":"Automated Trading Development in Python, Drawing Moving Averages - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:57:13+00:00","dateModified":"2024-11-01T11:51:00+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37395\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37395\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37395\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Automated Trading Development in Python, Drawing Moving Averages"}]},{"@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\/37395","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=37395"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37395\/revisions"}],"predecessor-version":[{"id":37396,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37395\/revisions\/37396"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}