{"id":37373,"date":"2024-11-01T09:57:03","date_gmt":"2024-11-01T09:57:03","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37373"},"modified":"2024-11-01T12:25:39","modified_gmt":"2024-11-01T12:25:39","slug":"%ec%9e%90%eb%8f%99%eb%a7%a4%eb%a7%a4-%ec%8b%9c%ec%8a%a4%ed%85%9c-%ea%b0%9c%eb%b0%9c-zipline-basics","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37373\/","title":{"rendered":"Automated Trading, Zipline Basics"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this lecture, we will introduce the development of automated trading using Python, and how to build a basic automated trading system using the Zipline library. Automated trading is widely used in stock and cryptocurrency trading, where a system automatically makes buy or sell decisions based on algorithms grounded in technical analysis.<\/p>\n<h2>1. What is Automated Trading?<\/h2>\n<p>Automated trading is software that automatically makes investment decisions based on a pre-defined algorithm. It helps eliminate emotional factors that can affect decisions and encourages data-driven decision-making. Automated trading systems are utilized across various financial markets, including stock markets, futures, options, forex, and cryptocurrencies.<\/p>\n<h2>2. What is Zipline?<\/h2>\n<p>Zipline is a Python-based algorithmic trading library primarily developed for backtesting in the stock market. It supports integration with exchanges like Bitfinex to execute trades in real-time. Zipline provides a simple API that makes it easy to write and test algorithms.<\/p>\n<h3>2.1 Features of Zipline<\/h3>\n<ul>\n<li>Backtesting: Allows verification of algorithm performance using historical data.<\/li>\n<li>Simple API: Offers user-friendly functions so analysts and developers can easily write and run algorithms.<\/li>\n<li>Flexible Structure: Provides the flexibility to integrate with various data sources.<\/li>\n<\/ul>\n<h2>3. Installing Zipline<\/h2>\n<p>To use Zipline, you can install it using pip as follows:<\/p>\n<pre><code>pip install zipline<\/code><\/pre>\n<p>Once installation is complete, we need to prepare the dataset we will use. Zipline supports various data sources, but in this lecture, we will use an example that fetches stock price data from &#8216;Yahoo Finance&#8217;.<\/p>\n<h2>4. Basic Usage of Zipline<\/h2>\n<p>To understand the basic usage of Zipline, we will implement an automated trading strategy through a simple example. The example below shows a straightforward momentum-based trading strategy.<\/p>\n<h3>4.1 Example Code<\/h3>\n<p>The code below is just an example, calculating the 50-day moving average for a stock and executing a buy order when the current price exceeds the moving average, otherwise executing a sell order.<\/p>\n<pre><code>\nimport zipline\nfrom zipline.api import order, record, symbol\nimport pandas as pd\nfrom datetime import datetime\nfrom zipline import run_algorithm\n\ndef initialize(context):\n    context.asset = symbol('AAPL')\n    context.MA_length = 50\n\ndef handle_data(context, data):\n    # Get historical data\n    historical_data = data.history(context.asset, 'price', context.MA_length, '1d')\n\n    # Calculate moving average\n    moving_average = historical_data.mean()\n\n    if data.current(context.asset, 'price') > moving_average:\n        order(context.asset, 10)  # Buy 10 shares\n    else:\n        order(context.asset, -10)  # Sell 10 shares\n\n    # Record values for later inspection\n    record(price=data.current(context.asset, 'price'),\n           moving_average=moving_average)\n\nstart = datetime(2020, 1, 1)\nend = datetime(2021, 1, 1)\n\nresults = run_algorithm(start=start,\n                         end=end,\n                         initialize=initialize,\n                         capital_base=10000,\n                         handle_data=handle_data,\n                         data_frequency='daily',\n                         bundle='quantale')\n<\/code><\/pre>\n<h3>4.2 Code Explanation<\/h3>\n<p>The above code consists of the following components:<\/p>\n<ul>\n<li><strong>initialize<\/strong>: Responsible for setting up the strategy and initializing parameters.<\/li>\n<li><strong>handle_data<\/strong>: A function called every trading day that decides on buying and selling.<\/li>\n<li><strong>data.history<\/strong>: Fetches historical data for a given period.<\/li>\n<li><strong>order<\/strong>: Executes buy and sell orders for specific assets.<\/li>\n<li><strong>record<\/strong>: Sets the values to record for each trading day.<\/li>\n<\/ul>\n<h2>5. Analyzing Backtest Results<\/h2>\n<p>Zipline periodically records trading results, and the final <code>results<\/code> dataframe can be used to analyze performance.<\/p>\n<pre><code>\nimport matplotlib.pyplot as plt\n\n# Plot the results\nresults.portfolio_value.plot()\nplt.title('Portfolio Value Over Time')\nplt.xlabel('Date')\nplt.ylabel('Portfolio Value')\nplt.show()\n<\/code><\/pre>\n<h3>5.1 Interpreting Results<\/h3>\n<p>Using the code above, you can visually examine portfolio value over time. This graph allows you to evaluate the strategy&#8217;s performance at a glance.<\/p>\n<h2>6. Portfolio Basics and Additional Strategies<\/h2>\n<p>Having addressed a single asset example, let\u2019s briefly explain how to manage a portfolio of various assets. Zipline allows concurrent operation of multiple assets, applying the same principles to set routines for each asset.<\/p>\n<h3>6.1 Portfolio Initialization<\/h3>\n<pre><code>\ndef initialize(context):\n    context.assets = [symbol('AAPL'), symbol('GOOGL'), symbol('MSFT')]\n    context.MA_length = 50\n<\/code><\/pre>\n<h3>6.2 Multi-Asset Handling<\/h3>\n<pre><code>\ndef handle_data(context, data):\n    for asset in context.assets:\n        historical_data = data.history(asset, 'price', context.MA_length, '1d')\n        moving_average = historical_data.mean()\n        \n        if data.current(asset, 'price') > moving_average:\n            order(asset, 10)\n        else:\n            order(asset, -10)\n        record(price=data.current(asset, 'price'),\n               moving_average=moving_average)\n<\/code><\/pre>\n<h2>7. Conclusion<\/h2>\n<p>We have explored the basic aspects of building an automated trading system using Zipline. Automated trading is a useful tool that can help eliminate emotional factors through data-driven decisions and improve investment performance. I encourage you to utilize Zipline to try various strategies and analyze performance.<\/p>\n<p>I also recommend researching more advanced automated trading strategies combined with Zipline in the future. I hope the knowledge gained from this lecture aids you in your investment journey!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this lecture, we will introduce the development of automated trading using Python, and how to build a basic automated trading system using the Zipline library. Automated trading is widely used in stock and cryptocurrency trading, where a system automatically makes buy or sell decisions based on algorithms grounded in technical analysis. 1. What &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37373\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Automated Trading, Zipline Basics&#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-37373","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, Zipline Basics - \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\/37373\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automated Trading, Zipline Basics - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this lecture, we will introduce the development of automated trading using Python, and how to build a basic automated trading system using the Zipline library. Automated trading is widely used in stock and cryptocurrency trading, where a system automatically makes buy or sell decisions based on algorithms grounded in technical analysis. 1. What &hellip; \ub354 \ubcf4\uae30 &quot;Automated Trading, Zipline Basics&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37373\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:57:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T12:25:39+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\/37373\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37373\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Automated Trading, Zipline Basics\",\"datePublished\":\"2024-11-01T09:57:03+00:00\",\"dateModified\":\"2024-11-01T12:25:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37373\/\"},\"wordCount\":528,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Auto Trading\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37373\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37373\/\",\"name\":\"Automated Trading, Zipline Basics - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:57:03+00:00\",\"dateModified\":\"2024-11-01T12:25:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37373\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37373\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37373\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automated Trading, Zipline Basics\"}]},{\"@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, Zipline Basics - \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\/37373\/","og_locale":"ko_KR","og_type":"article","og_title":"Automated Trading, Zipline Basics - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this lecture, we will introduce the development of automated trading using Python, and how to build a basic automated trading system using the Zipline library. Automated trading is widely used in stock and cryptocurrency trading, where a system automatically makes buy or sell decisions based on algorithms grounded in technical analysis. 1. What &hellip; \ub354 \ubcf4\uae30 \"Automated Trading, Zipline Basics\"","og_url":"https:\/\/atmokpo.com\/w\/37373\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:57:03+00:00","article_modified_time":"2024-11-01T12:25:39+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\/37373\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37373\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Automated Trading, Zipline Basics","datePublished":"2024-11-01T09:57:03+00:00","dateModified":"2024-11-01T12:25:39+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37373\/"},"wordCount":528,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Auto Trading"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37373\/","url":"https:\/\/atmokpo.com\/w\/37373\/","name":"Automated Trading, Zipline Basics - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:57:03+00:00","dateModified":"2024-11-01T12:25:39+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37373\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37373\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37373\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Automated Trading, Zipline Basics"}]},{"@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\/37373","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=37373"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37373\/revisions"}],"predecessor-version":[{"id":38048,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37373\/revisions\/38048"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}