{"id":5448,"date":"2024-04-30T16:45:38","date_gmt":"2024-04-30T07:45:38","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=5448"},"modified":"2024-05-01T08:26:45","modified_gmt":"2024-04-30T23:26:45","slug":"%e3%80%90python%e3%83%86%e3%82%af%e3%83%8b%e3%82%ab%e3%83%ab%e5%88%86%e6%9e%90%e3%80%91%e7%a7%bb%e5%8b%95%e5%b9%b3%e5%9d%87%e3%81%ae%e6%8f%8f%e7%94%bb","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=5448","title":{"rendered":"\u3010Python\u30c6\u30af\u30cb\u30ab\u30eb\u5206\u6790\u3011\u30ed\u30fc\u30bd\u30af\u8db3\u3068\u79fb\u52d5\u5e73\u5747\u7dda\u306e\u63cf\u753b"},"content":{"rendered":"\n<p>Python\u3067\u79fb\u52d5\u5e73\u5747\u3068\u30ed\u30fc\u30bd\u30af\u30c1\u30e3\u30fc\u30c8\u3092\u540c\u6642\u306b\u63cf\u753b\u3059\u308b\u30b3\u30fc\u30c9\u306b\u3064\u3044\u3066\u8a18\u8f09\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/obenkyolab.com\/?p=5448\/#%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA\" >\u30e9\u30a4\u30d6\u30e9\u30ea<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/obenkyolab.com\/?p=5448\/#%E3%82%B3%E3%83%BC%E3%83%89\" >\u30b3\u30fc\u30c9<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA\"><\/span>\u30e9\u30a4\u30d6\u30e9\u30ea<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u3068\u79fb\u52d5\u5e73\u5747\u306e\u8a08\u7b97\u306b\u306fpandas_datareader\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u30c1\u30e3\u30fc\u30c8\u306e\u63cf\u753b\u306b\u306fmplfinance\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u63cf\u753b\u306fmatplotlib\u3092\u4f7f\u7528\u3057\u3066\u3082\u826f\u3044\u306e\u3067\u3059\u304c\u3001\u30ed\u30fc\u30bd\u30af\u8db3\u3068\u5408\u308f\u305b\u3066\u63cf\u753b\u3057\u305f\u304b\u3063\u305f\u305f\u3081\u3001mplfinance\u3067\u3001addplot\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u73fe\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%82%B3%E3%83%BC%E3%83%89\"><\/span>\u30b3\u30fc\u30c9<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u65e5\u7d4c\u5e73\u5747\u3092\u53d6\u5f97\u3057\u3066\u79fb\u52d5\u5e73\u5747\u3092\u8a08\u7b97\u3057\u63cf\u753b\u3059\u308b\u30b3\u30fc\u30c9\u306b\u306a\u308a\u307e\u3059\u3002T1, T2, T3\u3067\u79fb\u52d5\u5e73\u5747\u306e\u671f\u9593\u3092\u5b9a\u7fa9\u3057\u3001rolling.mean()\u3092\u4f7f\u7528\u3057\u3066\u79fb\u52d5\u5e73\u5747\u3092\u8a08\u7b97\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u8aad\u307f\u8fbc\u307f\nimport pandas_datareader as pdr\nfrom datetime import datetime, timedelta\nimport mplfinance as mpf\n\n# \u30c7\u30fc\u30bf\u53d6\u5f97\u671f\u9593\u8a2d\u5b9a\ncurrent_date = datetime.now()\ntwo_years_ago = current_date - timedelta(days=365) # 1\u5e74\u524d\u3092\u8a2d\u5b9a\nformatted_date = two_years_ago.strftime(&quot;%Y-%m-%d&quot;)\ndf = pdr.DataReader(&#39;^NKX&#39;, &#39;stooq&#39;,start=formatted_date) # \u30c7\u30fc\u30bf\u306e\u53d6\u5f97\ndf.sort_values(&#39;Date&#39;, inplace=True) # \u65e5\u4ed8\u9806\u306b\u30bd\u30fc\u30c8\n\n# \u671f\u9593\u6307\u5b9a\nT1 = 5\nT2 = 25\nT3 = 75\n\n# \u79fb\u52d5\u5e73\u5747\u8a08\u7b97\ndf[&#39;short&#39;] = df[&#39;Close&#39;].rolling(T1).mean()\ndf[&#39;middle&#39;] = df[&#39;Close&#39;].rolling(T2).mean()\ndf[&#39;long&#39;] = df[&#39;Close&#39;].rolling(T3).mean()\n\n# mplfinance\u3067\u30b0\u30e9\u30d5\u5316\nadds = [\n  mpf.make_addplot(df[&#39;short&#39;], label=&#39;Short &#39;+str(T1), color=&#39;g&#39;, width=1, alpha=0.5),\n  mpf.make_addplot(df[&#39;middle&#39;], label=&#39;Middle &#39;+str(T2), width=0.5),\n  mpf.make_addplot(df[&#39;long&#39;], label=&#39;Long &#39;+str(T3), width=0.5),\n]\nmpf.plot(df[df.columns], type=&#39;candle&#39;, volume=True, style=&#39;yahoo&#39;, figsize=(11,8), addplot=adds)\nmpf.show()<\/code><\/pre><\/div>\n\n\n\n<p>\u63cf\u753b\u3057\u305f\u7d50\u679c\u306f\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"527\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-7-700x527.png\" alt=\"\" class=\"wp-image-5449\" srcset=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-7-700x527.png 700w, https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-7-300x226.png 300w, https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-7-768x578.png 768w, https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-7.png 875w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Python\u3067\u79fb\u52d5\u5e73\u5747\u3068\u30ed\u30fc\u30bd\u30af\u30c1\u30e3\u30fc\u30c8\u3092\u540c\u6642\u306b\u63cf\u753b\u3059\u308b\u30b3\u30fc\u30c9\u306b\u3064\u3044&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/obenkyolab.com\/?p=5448","footnotes":""},"categories":[7,114],"tags":[],"class_list":["post-5448","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-114","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/5448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5448"}],"version-history":[{"count":4,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/5448\/revisions"}],"predecessor-version":[{"id":5476,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/5448\/revisions\/5476"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}