{"id":37371,"date":"2024-11-01T09:57:03","date_gmt":"2024-11-01T09:57:03","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37371"},"modified":"2024-11-01T11:51:05","modified_gmt":"2024-11-01T11:51:05","slug":"developing-python-automated-trading-setting-up-zipline-transaction-fees","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37371\/","title":{"rendered":"Developing Python Automated Trading, Setting Up Zipline Transaction Fees"},"content":{"rendered":"<p><body><\/p>\n<p>It is very important to consider trading fees when developing an Algorithmic Trading system. Many traders often overlook fees, but they can have a significant impact on actual profit margins. Zipline is an open-source algorithmic trading library written in Python that provides functionality to easily set and test these trading fees.<\/p>\n<h2>Overview of Zipline<\/h2>\n<p>Zipline is a backtesting framework developed by Quantopian, primarily used for stocks and futures markets. With Zipline, you can implement various strategies and run simulations that include trading costs.<\/p>\n<h3>Installing Zipline<\/h3>\n<p>To use Zipline, you first need to install it as follows:<\/p>\n<pre><code>pip install zipline<\/code><\/pre>\n<h2>The Importance of Trading Costs<\/h2>\n<p>Trading costs consist of several elements, notably including:<\/p>\n<ul>\n<li><strong>Spread:<\/strong> The difference between the buy and sell prices; the larger this difference, the higher the trading costs.<\/li>\n<li><strong>Commission:<\/strong> A fixed cost per trade, charged for each transaction.<\/li>\n<li><strong>Tax:<\/strong> Taxes incurred during trading, which vary according to each country&#8217;s tax laws.<\/li>\n<\/ul>\n<p>These costs can negatively impact the performance of trading strategies, so they must be carefully considered when designing strategies.<\/p>\n<h2>Setting Trading Fees in Zipline<\/h2>\n<p>In Zipline, you can set trading fees using the <code>set_commission()<\/code> function. Here\u2019s how to set fees in the Zipline initialization code.<\/p>\n<pre><code>from zipline.api import set_commission, commission\n\ndef initialize(context):\n    # Set the commission.\n    set_commission(commission.PerShare(cost=0.01, min_trade_cost=0.0))\n<\/code><\/pre>\n<p>In this code, the <code>PerShare<\/code> class sets a per-share commission for a specific stock. Here, <code>cost=0.01<\/code> means that a fee of $0.01 per share will be charged for each transaction.<\/p>\n<h3>Types of Trading Commissions<\/h3>\n<p>Zipline supports various commission structures. The most common methods include:<\/p>\n<ul>\n<li><strong>PerShare:<\/strong> Set a fee per share. <code>cost<\/code> defines the per-share fee, and <code>min_trade_cost<\/code> defines the minimum fee per trade.<\/li>\n<li><strong>Fixed:<\/strong> Set a fixed fee per transaction. <code>cost<\/code> defines the fee charged per trade.<\/li>\n<\/ul>\n<h2>Example: Including Commission in a Zipline Strategy<\/h2>\n<p>The following example implements a simple buy and sell strategy using Zipline and runs a simulation that includes trading fees.<\/p>\n<pre><code>from zipline import run_algorithm\nfrom zipline.api import order, record, symbol\nimport pandas as pd\nfrom datetime import datetime\n\ndef initialize(context):\n    set_commission(commission.PerShare(cost=0.01, min_trade_cost=0.0))\n\ndef handle_data(context, data):\n    # Buy\/sell under specific conditions\n    if data.current(symbol('AAPL'), 'price') &lt; 150:\n        order(symbol('AAPL'), 10)  # Buy 10 shares of AAPL\n    elif data.current(symbol('AAPL'), 'price') &gt; 160:\n        order(symbol('AAPL'), -10)  # Sell 10 shares of AAPL\n    record(AAPL=data.current(symbol('AAPL'), 'price'))\n\nstart = datetime(2020, 1, 1)\nend = datetime(2020, 12, 31)\n\n# Download data and run the algorithm\nrun_algorithm(start=start, end=end, initialize=initialize, handle_data=handle_data, capital_base=10000)\n<\/code><\/pre>\n<h3>Explanation of the Example<\/h3>\n<p>The above code monitors the price of AAPL stock from January 1, 2020, to December 31, 2020, implementing a simple strategy to buy 10 shares when the price is below $150 and to sell 10 shares when the price exceeds $160. The commission is set at $0.01 per share, so this fee applies for each buy and sell transaction.<\/p>\n<h2>Result Analysis<\/h2>\n<p>After executing the trading strategy, you can record price data using the <code>record()<\/code> function. It is important to review performance based on this data. Zipline\u2019s simulator conducts performance analysis, including trading fees.<\/p>\n<h3>Check Simulation Results<\/h3>\n<pre><code>import matplotlib.pyplot as plt\n\nresults = run_algorithm(...)\n\n# Display performance graph\nplt.plot(results.index, results.portfolio_value)\nplt.title('Portfolio Value Over Time')\nplt.xlabel('Date')\nplt.ylabel('Portfolio Value')\nplt.show()\n<\/code><\/pre>\n<h2>Customizing Trading Fees<\/h2>\n<p>When analyzing the market, various strategies may be required. At that time, you may need to customize fees appropriately. Zipline supports customized commission structures, allowing you to set fees optimized for each strategy.<\/p>\n<h3>Example: Setting Trading Fees with External Parameters<\/h3>\n<pre><code>def initialize(context):\n    # Set the commission using external parameters\n    cost_per_share = context.cost_per_share\n    set_commission(commission.PerShare(cost=cost_per_share, min_trade_cost=0.0))\n\ndef run_my_algorithm(cost_per_share=0.01):\n    run_algorithm(start=start, end=end, initialize=initialize, handle_data=handle_data, capital_base=10000,\n                  cost_per_share=cost_per_share)\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Using Zipline, you can efficiently build an automated trading system in Python, with easy and flexible settings for trading fees. Since trading fees significantly impact the performance of strategies, they must be designed and managed carefully. It is hoped that the examples provided in this material will help in developing more effective automated trading strategies.<\/p>\n<p>Additionally, explore the various features of Zipline to conduct different experiments and develop your own trading strategies. Automated trading is more than just writing code; it requires a deep understanding of the market and a strategic approach.<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.zipline.io\/\">Zipline Official Documentation<\/a><\/li>\n<li><a href=\"https:\/\/quantopian.com\/\">Quantopian<\/a><\/li>\n<li><a href=\"https:\/\/www.quantstart.com\/\">QuantStart<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is very important to consider trading fees when developing an Algorithmic Trading system. Many traders often overlook fees, but they can have a significant impact on actual profit margins. Zipline is an open-source algorithmic trading library written in Python that provides functionality to easily set and test these trading fees. Overview of Zipline Zipline &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37371\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Developing Python Automated Trading, Setting Up Zipline Transaction Fees&#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-37371","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>Developing Python Automated Trading, Setting Up Zipline Transaction Fees - \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\/37371\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Developing Python Automated Trading, Setting Up Zipline Transaction Fees - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"It is very important to consider trading fees when developing an Algorithmic Trading system. Many traders often overlook fees, but they can have a significant impact on actual profit margins. Zipline is an open-source algorithmic trading library written in Python that provides functionality to easily set and test these trading fees. Overview of Zipline Zipline &hellip; \ub354 \ubcf4\uae30 &quot;Developing Python Automated Trading, Setting Up Zipline Transaction Fees&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37371\/\" \/>\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-01T11:51:05+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\/37371\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37371\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Developing Python Automated Trading, Setting Up Zipline Transaction Fees\",\"datePublished\":\"2024-11-01T09:57:03+00:00\",\"dateModified\":\"2024-11-01T11:51:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37371\/\"},\"wordCount\":536,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Auto Trading\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37371\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37371\/\",\"name\":\"Developing Python Automated Trading, Setting Up Zipline Transaction Fees - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:57:03+00:00\",\"dateModified\":\"2024-11-01T11:51:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37371\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37371\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37371\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Developing Python Automated Trading, Setting Up Zipline Transaction Fees\"}]},{\"@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":"Developing Python Automated Trading, Setting Up Zipline Transaction Fees - \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\/37371\/","og_locale":"ko_KR","og_type":"article","og_title":"Developing Python Automated Trading, Setting Up Zipline Transaction Fees - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"It is very important to consider trading fees when developing an Algorithmic Trading system. Many traders often overlook fees, but they can have a significant impact on actual profit margins. Zipline is an open-source algorithmic trading library written in Python that provides functionality to easily set and test these trading fees. Overview of Zipline Zipline &hellip; \ub354 \ubcf4\uae30 \"Developing Python Automated Trading, Setting Up Zipline Transaction Fees\"","og_url":"https:\/\/atmokpo.com\/w\/37371\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:57:03+00:00","article_modified_time":"2024-11-01T11:51:05+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\/37371\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37371\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Developing Python Automated Trading, Setting Up Zipline Transaction Fees","datePublished":"2024-11-01T09:57:03+00:00","dateModified":"2024-11-01T11:51:05+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37371\/"},"wordCount":536,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Auto Trading"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37371\/","url":"https:\/\/atmokpo.com\/w\/37371\/","name":"Developing Python Automated Trading, Setting Up Zipline Transaction Fees - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:57:03+00:00","dateModified":"2024-11-01T11:51:05+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37371\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37371\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37371\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Developing Python Automated Trading, Setting Up Zipline Transaction Fees"}]},{"@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\/37371","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=37371"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37371\/revisions"}],"predecessor-version":[{"id":37372,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37371\/revisions\/37372"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}