*

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

公開日: : python

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

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

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

python3ソース

 

<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>

参考サイト

関連記事

no image

python 基礎

インタラクティブモード $ python Python 3.5.4 |Anacond

記事を読む

no image

Python Web Scraping Tutorial using BeautifulSoup 分かりやすい

<html></span> <span class=

記事を読む

no image

python3 日経平均取得 cvsに保存

cd ~ cd python vi nikkei.py # coding: UTF

記事を読む

no image

BeautifulSoup sample

sample # coding: UTF-8 from bs4 import B

記事を読む

no image

python finance

参考サイト 取得したいデータの位置を確かめる webスクレイピングではHTML/XML中の特定の

記事を読む

no image

python BeautifulSoup

BeautifulSoup公式サイト 自動でデータを収集する方法 requests モジ

記事を読む

no image

python2 pytone3 使い分け方法

現在の標準pythonのバージョンの確認 $python --version 使い分け方法 v

記事を読む

Message

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

no image
rear レスキュシステムプログラムとバックアップファイルをひとつのisoファイルにまとめる

vi /etc/rear/local.conf OUTPUT=I

no image
rear backup usbメモリにレスキュシステムとバックアップをusbメモリにまとめる

このusbひとつリストアが完了する /usr/sbin/rear

no image
rear backup

レスキューシステムとバックファイルの両方をバックアップで作る レ

no image
rear backup 日次 週次 月次 バックアップ

/etc/rear内にdaily、weekly、monthlyディレク

no image
Red Hat Enterprise Linux 7 システム管理者のガイド

ここにlinuxを管理するための手順が書かれている  

→もっと見る

PAGE TOP ↑