python 日本企業の株価データを銘柄ごとに取得

Pythonでは、jsmモジュールで日本の証券取引所に上場している株価を取得できます

書式】
get_stock(銘柄, 開始日, 終了日)

■返り値
指定した銘柄の開始~終了日の株価(終値、始値、高値、安値)

python3ソース

[py] <pre class="prettyprint hljs processing"><span class="com">#-*- coding:utf<span class="hljs-number">-8</span> -*-</span><span class="pln">
</span><span class="kwd"><span class="hljs-keyword">import</span></span><span class="pln"> jsm
</span><span class="kwd">import</span><span class="pln"> datetime

</span><span class="com"># 株価のデータ取得(銘柄コード, 開始日, 終了日)</span><span class="pln">
</span><span class="kwd">def</span><span class="pln"> get_stock</span><span class="pun">(</span><span class="pln">code</span><span class="pun">,</span><span class="pln"> start_date</span><span class="pun">,</span><span class="pln"> end_date</span><span class="pun">):</span><span class="pln">
    </span><span class="com"># 期間設定</span><span class="pln">
    <span class="hljs-built_in">year</span></span><span class="pun">,</span><span class="pln"> <span class="hljs-built_in">month</span></span><span class="pun">,</span><span class="pln"> <span class="hljs-built_in">day</span> </span><span class="pun">=</span><span class="pln"> start_date</span><span class="pun">.</span><span class="pln"><span class="hljs-built_in">split</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"-"</span></span><span class="pun">)</span><span class="pln">
    start </span><span class="pun">=</span><span class="pln"> datetime</span><span class="pun">.</span><span class="pln">date</span><span class="pun">(</span><span class="kwd"><span class="hljs-built_in">int</span></span><span class="pun">(</span><span class="pln"><span class="hljs-built_in">year</span></span><span class="pun">),</span> <span class="kwd"><span class="hljs-built_in">int</span></span><span class="pun">(</span><span class="pln"><span class="hljs-built_in">month</span></span><span class="pun">),</span> <span class="kwd"><span class="hljs-built_in">int</span></span><span class="pun">(</span><span class="pln"><span class="hljs-built_in">day</span></span><span class="pun">))</span><span class="pln">
    <span class="hljs-built_in">year</span></span><span class="pun">,</span><span class="pln"> <span class="hljs-built_in">month</span></span><span class="pun">,</span><span class="pln"> <span class="hljs-built_in">day</span> </span><span class="pun">=</span><span class="pln"> end_date</span><span class="pun">.</span><span class="pln"><span class="hljs-built_in">split</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"-"</span></span><span class="pun">)</span><span class="pln">
    </span><span class="kwd">end</span> <span class="pun">=</span><span class="pln"> datetime</span><span class="pun">.</span><span class="pln">date</span><span class="pun">(</span><span class="kwd"><span class="hljs-built_in">int</span></span><span class="pun">(</span><span class="pln"><span class="hljs-built_in">year</span></span><span class="pun">),</span> <span class="kwd"><span class="hljs-built_in">int</span></span><span class="pun">(</span><span class="pln"><span class="hljs-built_in">month</span></span><span class="pun">),</span> <span class="kwd"><span class="hljs-built_in">int</span></span><span class="pun">(</span><span class="pln"><span class="hljs-built_in">day</span></span><span class="pun">))</span><span class="pln">
    </span><span class="com"># 株価データ取得</span><span class="pln">
    q </span><span class="pun">=</span><span class="pln"> jsm</span><span class="pun">.</span><span class="typ">Quotes</span><span class="pun">()</span><span class="pln">
    target </span><span class="pun">=</span><span class="pln"> q</span><span class="pun">.</span><span class="pln">get_historical_prices</span><span class="pun">(</span><span class="pln">code</span><span class="pun">,</span><span class="pln"> jsm</span><span class="pun">.</span><span class="pln">DAILY</span><span class="pun">,</span><span class="pln"> start_date </span><span class="pun">=</span><span class="pln"> start</span><span class="pun">,</span><span class="pln"> end_date </span><span class="pun">=</span> <span class="kwd">end</span><span class="pun">)</span><span class="pln">
    </span><span class="com"># 項目ごとにリストに格納して返す</span><span class="pln">
    date </span><span class="pun">=</span> <span class="pun">[</span><span class="pln">data</span><span class="pun">.</span><span class="pln">date </span><span class="kwd"><span class="hljs-keyword">for</span></span><span class="pln"> data </span><span class="kwd">in</span><span class="pln"> target</span><span class="pun">]</span><span class="pln">
    <span class="hljs-built_in">open</span> </span><span class="pun">=</span> <span class="pun">[</span><span class="pln">data</span><span class="pun">.</span><span class="pln"><span class="hljs-built_in">open</span> </span><span class="kwd"><span class="hljs-keyword">for</span></span><span class="pln"> data </span><span class="kwd">in</span><span class="pln"> target</span><span class="pun">]</span><span class="pln">
    close </span><span class="pun">=</span> <span class="pun">[</span><span class="pln">data</span><span class="pun">.</span><span class="pln">close </span><span class="kwd"><span class="hljs-keyword">for</span></span><span class="pln"> data </span><span class="kwd">in</span><span class="pln"> target</span><span class="pun">]</span><span class="pln">
    high </span><span class="pun">=</span> <span class="pun">[</span><span class="pln">data</span><span class="pun">.</span><span class="pln">high </span><span class="kwd"><span class="hljs-keyword">for</span></span><span class="pln"> data </span><span class="kwd">in</span><span class="pln"> target</span><span class="pun">]</span><span class="pln">
    low </span><span class="pun">=</span> <span class="pun">[</span><span class="pln">data</span><span class="pun">.</span><span class="pln">low </span><span class="kwd"><span class="hljs-keyword">for</span></span><span class="pln"> data </span><span class="kwd">in</span><span class="pln"> target</span><span class="pun">]</span><span class="pln">

    </span><span class="kwd"><span class="hljs-keyword">return</span></span> <span class="pun">[</span><span class="pln">date</span><span class="pun">,</span><span class="pln"> <span class="hljs-built_in">open</span></span><span class="pun">,</span><span class="pln"> close</span><span class="pun">,</span><span class="pln"> high</span><span class="pun">,</span><span class="pln"> low</span><span class="pun">]</span><span class="pln">

</span><span class="kwd">def</span><span class="pln"> main</span><span class="pun">():</span><span class="pln">
    </span><span class="com"># 株価の取得(銘柄コード, 開始日, 終了日)</span><span class="pln">
    data </span><span class="pun">=</span><span class="pln"> get_stock</span><span class="pun">(</span><span class="lit"><span class="hljs-number">3318</span></span><span class="pun">,</span> <span class="str"><span class="hljs-string">’2017-3-23′</span></span><span class="pun">,</span> <span class="str"><span class="hljs-string">’2017-3-27′</span></span><span class="pun">)</span><span class="pln">
   
    </span><span class="com"># 取得したデータの表示</span><span class="pln">
    </span><span class="kwd"><span class="hljs-built_in">print</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"日付\t始値\t終値\t高値\t安値"</span></span><span class="pun">)</span><span class="pln">
    </span><span class="kwd"><span class="hljs-keyword">for</span></span><span class="pln"> date</span><span class="pun">,</span><span class="pln"> <span class="hljs-built_in">open</span></span><span class="pun">,</span><span class="pln"> close</span><span class="pun">,</span><span class="pln"> high</span><span class="pun">,</span><span class="pln"> low </span><span class="kwd">in</span><span class="pln"> list</span><span class="pun">(</span><span class="pln">zip</span><span class="pun">(*</span><span class="pln">data</span><span class="pun">)):</span><span class="pln">
        </span><span class="kwd"><span class="hljs-built_in">print</span></span><span class="pun">(</span><span class="pln">date</span><span class="pun">.</span><span class="pln">strftime</span><span class="pun">(</span><span class="str"><span class="hljs-string">"%m/%d"</span></span><span class="pun">),</span> <span class="str"><span class="hljs-string">"\t"</span></span><span class="pun">,</span><span class="pln"> <span class="hljs-built_in">open</span></span><span class="pun">,</span> <span class="str"><span class="hljs-string">"\t"</span></span><span class="pun">,</span><span class="pln"> close</span><span class="pun">,</span> <span class="str"><span class="hljs-string">"\t"</span></span><span class="pun">,</span><span class="pln"> high</span><span class="pun">,</span> <span class="str"><span class="hljs-string">"\t"</span></span><span class="pun">,</span><span class="pln"> low</span><span class="pun">)</span><span class="pln">
   
</span><span class="kwd"><span class="hljs-keyword">if</span></span><span class="pln"> __name__ </span><span class="pun">==</span> <span class="str"><span class="hljs-string">"__main__"</span></span><span class="pun">:</span><span class="pln">
    main</span><span class="pun">()</span>
[/py]

参考サイト

シェアする

  • このエントリーをはてなブックマークに追加

フォローする