{"id":5435,"date":"2024-04-15T00:10:46","date_gmt":"2024-04-14T15:10:46","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=5435"},"modified":"2024-04-15T00:10:47","modified_gmt":"2024-04-14T15:10:47","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%91rsi%e3%81%ae%e7%ae%97%e5%87%ba","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=5435","title":{"rendered":"\u3010Python\u30c6\u30af\u30cb\u30ab\u30eb\u5206\u6790\u3011RSI\u306e\u7b97\u51fa"},"content":{"rendered":"\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=5435\/#%E6%A6%82%E8%A6%81\" >\u6982\u8981<\/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=5435\/#TA-Lib%E3%82%92%E7%94%A8%E3%81%84%E3%82%8B%E6%96%B9%E6%B3%95\" >TA-Lib\u3092\u7528\u3044\u308b\u65b9\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/obenkyolab.com\/?p=5435\/#%E6%89%8B%E7%B5%84%E3%81%A7RSI%E3%82%92%E8%A8%88%E7%AE%97%E3%81%99%E3%82%8B%E5%A0%B4%E5%90%88\" >\u624b\u7d44\u3067RSI\u3092\u8a08\u7b97\u3059\u308b\u5834\u5408<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/obenkyolab.com\/?p=5435\/#%E3%81%BE%E3%81%A8%E3%82%81\" >\u307e\u3068\u3081<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%A6%82%E8%A6%81\"><\/span>\u6982\u8981<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python\u3092\u4f7f\u7528\u3057\u3066RSI\u3092\u7b97\u51fa\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u4eca\u56de\u306f\u30c6\u30af\u30cb\u30ab\u30eb\u5206\u6790\u306e\u6307\u6a19\u3092\u7c21\u5358\u306b\u8a08\u7b97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3042\u308bTA-Lib\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5\u3068pandas\u3060\u3051\u3092\u4f7f\u7528\u3057\u3066\u624b\u7d44\u3067\u8a08\u7b97\u3059\u308b\u65b9\u6cd5\u306e\u4e8c\u3064\u3067\u8a66\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"TA-Lib%E3%82%92%E7%94%A8%E3%81%84%E3%82%8B%E6%96%B9%E6%B3%95\"><\/span>TA-Lib\u3092\u7528\u3044\u308b\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u307e\u305a\u521d\u3081\u306bTA-Lib\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306f\u4ee5\u4e0b\u306b\u8a18\u8f09\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/obenkyolab.com\/?p=5421\">\u3010Python\u30c6\u30af\u30cb\u30ab\u30eb\u5206\u6790\u3011TA-Lib\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb \u2013 IT Learning (obenkyolab.com)<\/a><\/p>\n\n\n\n<p>TA-Lib\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u3066\u3044\u308c\u3070\u3042\u3068\u306f\u7c21\u5358\u306bRSI\u3092\u7b97\u51fa\u3057\u3066\u63cf\u753b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u304c\u65e5\u7d4c\u5e73\u5747\u3092\u53d6\u5f97\u3057\u306614\u65e5RSI\u3092\u63cf\u753b\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\u3002pandas_DataReader\u3067\u53d6\u5f97\u3057\u305f\u5f8c\u306btalib.RSI\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\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\nimport matplotlib.pyplot as plt\nfrom datetime import datetime, timedelta\nimport talib\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\n\n# \u7d42\u5024\u306e\u307f\u3092\u62bd\u51fa\ndf.sort_values(&#39;Date&#39;, inplace=True) # \u65e5\u4ed8\u9806\u306b\u30bd\u30fc\u30c8\n\n# RSI\u3092\u8a08\u7b97\nrsi = talib.RSI(df[&#39;Close&#39;], timeperiod=14)\n\n# \u63cf\u753b\nplt.plot(rsi, label=&#39;RSI&#39;)\nplt.xlabel(&#39;date&#39;)\nplt.ylabel(&#39;RSI&#39;)\nplt.legend()\nplt.show()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"420\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-4.png\" alt=\"\" class=\"wp-image-5437\" srcset=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-4.png 557w, https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-4-300x226.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/figure>\n\n\n\n<p>\u5b9f\u969b\u306bRSI\u3092\u8a08\u7b97\u3057\u3066\u3044\u308b\u90e8\u5206\u306frsi = talib.RSI(df[&#8216;Close&#8217;], timeperiod=14)\u306e\u90e8\u5206\u3067\u3001\u975e\u5e38\u306b\u7c21\u5358\u306b\u8a08\u7b97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%89%8B%E7%B5%84%E3%81%A7RSI%E3%82%92%E8%A8%88%E7%AE%97%E3%81%99%E3%82%8B%E5%A0%B4%E5%90%88\"><\/span>\u624b\u7d44\u3067RSI\u3092\u8a08\u7b97\u3059\u308b\u5834\u5408<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6b21\u306bTA-Lib\u3092\u4f7f\u308f\u305a\u306b\u624b\u7d44\u3067RSI\u3092\u8a08\u7b97\u3057\u3066\u307f\u307e\u3059\u3002\u4eca\u56de\u306fCutler\u306eRSI\u306e\u8a08\u7b97\u5f0f\u3092\u4f7f\u7528\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u5b9a\u7fa9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>RSI = n\u65e5\u9593\u306e\u5024\u4e0a\u304c\u308a\u5e45\u5408\u8a08 \u00f7 (n\u65e5\u9593\u306e\u5024\u4e0a\u304c\u308a\u5e45\u5408\u8a08 \uff0b n\u65e5\u9593\u306e\u5024\u4e0b\u304c\u308a\u5e45\u5408\u8a08) \u00d7 100<\/p>\n<cite><a href=\"https:\/\/ja.wikipedia.org\/wiki\/%E3%83%86%E3%82%AF%E3%83%8B%E3%82%AB%E3%83%AB%E6%8C%87%E6%A8%99%E4%B8%80%E8%A6%A7\">\u30c6\u30af\u30cb\u30ab\u30eb\u6307\u6a19\u4e00\u89a7 &#8211; Wikipedia<\/a><\/cite><\/blockquote>\n\n\n\n<p>\u3053\u306e\u5b9a\u7fa9\u5f0f\u3092\u7528\u3044\u3066\u7b97\u51fa\u3057\u3066\u307f\u305f\u30b3\u30fc\u30c9\u304c\u4ee5\u4e0b\u306b\u306a\u308a\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\nimport matplotlib.pyplot as plt\nfrom datetime import datetime, timedelta\nimport talib as ta\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# RSI\u7b97\u51fa\ndef nrsi(df_close, n):\n    df[&#39;Close_previous&#39;] = df_close.shift(1) # \u524d\u56de\u306e\u7d42\u5024\u3092\u5217\u3068\u3057\u3066\u8ffd\u52a0\n    df[&#39;Close_dif&#39;] = df[&#39;Close&#39;]-df[&#39;Close_previous&#39;] \n    df[&#39;Close_dif_up&#39;] = df[&#39;Close_dif&#39;].mask(df[&#39;Close_dif&#39;] &lt; 0, 0) # \u4e0a\u6607\u3057\u305f\u65e5\u4ee5\u5916\u30920\u306b\u3059\u308b\n    df[&#39;Close_dif_down&#39;] = df[&#39;Close_dif&#39;].mask(df[&#39;Close_dif&#39;] &gt; 0, 0) # \u4e0b\u843d\u3057\u305f\u65e5\u4ee5\u5916\u30920\u306b\u3059\u308b\n    df[&#39;n_up_sum&#39;]= df[&#39;Close_dif_up&#39;].rolling(n).sum().abs() # \u5024\u4e0a\u304c\u308a\u5e45\u306e\u5408\u8a08\n    df[&#39;n_down_sum&#39;] = df[&#39;Close_dif_down&#39;].rolling(n).sum().abs() # \u5024\u4e0b\u304c\u308a\u5e45\u306e\u5408\u8a08\n    df[&#39;RSI&#39;] = (df[&#39;n_up_sum&#39;] \/ (df[&#39;n_up_sum&#39;] + df[&#39;n_down_sum&#39;]))*100 # Culter\u306eRSI\u8a08\u7b97\n    return df[&#39;RSI&#39;]\n\nrsi = nrsi(df[&#39;Close&#39;], 14)\n\n# \u63cf\u753b\nplt.plot(rsi, label=&#39;RSI&#39;)\nplt.xlabel(&#39;date&#39;)\nplt.ylabel(&#39;RSI&#39;)\nplt.legend()\nplt.show()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"578\" height=\"422\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-5.png\" alt=\"\" class=\"wp-image-5439\" srcset=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-5.png 578w, https:\/\/obenkyolab.com\/wp-content\/uploads\/2024\/04\/image-5-300x219.png 300w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/figure>\n\n\n\n<p>dataframe\u3067\u5024\u4e0a\u304c\u308a\u5e45\u3068\u5024\u4e0b\u304c\u308a\u5e45\u3092\u8ffd\u52a0\u3057\u3001rolling\u3092\u7528\u3044\u308b\u3053\u3068\u3067\u8a08\u7b97\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>TA-Lib\u306e\u30b0\u30e9\u30d5\u3068\u82e5\u5e72\u7570\u306a\u3063\u3066\u3044\u307e\u3059\u304c\u3001TA-Lib\u306fWilder\u306eRSI\u306e\u5b9a\u7fa9\u5f0f\u3092\u7528\u3044\u3066\u3044\u308b\u304b\u3001\u4fee\u6b63\u79fb\u52d5\u5e73\u5747\u306b\u3088\u3063\u3066\u8a08\u7b97\u3057\u3066\u3044\u308b\u53ef\u80fd\u6027\u3082\u3042\u308b\u3068\u601d\u308f\u308c\u307e\u3059\u304c\u3001\u6b63\u3057\u304f\u78ba\u8a8d\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u5206\u304b\u308a\u307e\u305b\u3093\u3002\u305f\u3060\u3001\u5168\u4f53\u306e\u50be\u5411\u3068\u3057\u3066\u306f\u540c\u3058\u3088\u3046\u306a\u5f62\u306b\u306f\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%81%BE%E3%81%A8%E3%82%81\"><\/span>\u307e\u3068\u3081<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>TA-lib\u306e\u307b\u3046\u304c\u4f55\u3082\u8003\u3048\u305a\u306b\u8a08\u7b97\u3067\u304d\u308b\u306e\u3067\u697d\u3067\u3057\u305f\u3002\u305f\u3060\u3001\u5f0f\u3092\u7406\u89e3\u3057\u305f\u304b\u3063\u305f\u308a\u3001TA-Lib\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3092\u3059\u308b\u306e\u304c\u9762\u5012\u306a\u5834\u5408\u306f\u624b\u7d44\u3067\u3084\u3063\u3066\u307f\u3066\u9762\u767d\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 Python\u3092\u4f7f\u7528\u3057\u3066RSI\u3092\u7b97\u51fa\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u4eca\u56de&#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=5435","footnotes":""},"categories":[7,114],"tags":[],"class_list":["post-5435","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\/5435","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=5435"}],"version-history":[{"count":3,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/5435\/revisions"}],"predecessor-version":[{"id":5440,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/5435\/revisions\/5440"}],"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=5435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}