stocks = ["3632-T グリー", "2432-T DENA", "8604-T 野村HD", "8411-T みずほ", "7733-T オリンパス"]
tickers = [s.split(' ')[0] for s in stocks]
names = {}
for s in stocks:
nt = s.split(' ')
names[nt[0]] = nt[1]
shortest=250
prices = {}
dates = None
for t in tickers:
# urlをオープン
url = 'http://k-db.com/site/jikeiretsu.aspx?c=%s&year=0&download=csv'%t
rows = urllib2.urlopen(url).readlines()
# 各行の取引量のフィールドを抽出
prices[t] = [float(r.split(',')[5]) for r in rows[2:] if r.strip() != '']
if len(prices[t]) < shortest: shortest = len(prices[t])
# 日付をセット
if not dates:
dates = [r.split(',')[0] for r in rows[2:] if r.strip() != '']
# 解析用データをセットアップ
l1 = [[ prices[tickers[i]][j] for i in range(len(tickers)) ] for j in range(shortest)]
# データの正規化
m1 = matrix(l1)
maxies = [max(m1.column(i)) for i in range(ii)]
(ii, jj) = (len(tickers), shortest)
l2 = [[ l1[j][i]/maxies[i] for i in range(ii)] for j in range(jj)]
# 出来高をプロット
cls = ['red', 'blue', 'green', 'gray', 'yellow']
g = Graphics()
for i in range(len(tickers)):
g += list_plot([l1[shortest-j-1][i] for j in range(shortest)], rgbcolor=cls[i], plotjoined=True)
g.show()
# 正規化した出来高
g = Graphics()
for i in range(len(tickers)):
list_plot([l2[shortest-j-1][i] for j in range(shortest)], rgbcolor=cls[i], plotjoined=True, figsize=(5,2))
# 独立成分分析
import numpy as np
elmts = fastica(np.array(l2))
# 結果のプロット
# 符号は、異なるがすべての特徴が抽出されている
g = Graphics()
for i in range(len(tickers)):
list_plot([elmts[shortest-j-1][i] for j in range(shortest)], rgbcolor=cls[i], plotjoined=True, figsize=(5,2))
# 株価で分析
shortest=250
prices = {}
dates = None
# CSVファイルを取得
for t in tickers:
# urlをオープン
url = 'http://k-db.com/site/jikeiretsu.aspx?c=%s&year=0&download=csv'%t
rows = urllib2.urlopen(url).readlines()
# 各行の終値のフィールドを抽出
prices[t] = [float(r.split(',')[4]) for r in rows[2:] if r.strip() != '']
if len(prices[t]) < shortest: shortest = len(prices[t])
# 日付をセット
if not dates:
dates = [r.split(',')[0] for r in rows[2:] if r.strip() != '']
# 解析用データをセットアップ
l3 = [[ prices[tickers[i]][j] for i in range(len(tickers)) ] for j in range(shortest)]
# データの正規化
m3 = matrix(l3)
maxies = [max(m3.column(i)) for i in range(ii)]
(ii, jj) = (len(tickers), shortest)
l4 = [[ l3[j][i]/maxies[i] for i in range(ii)] for j in range(jj)]
# 正規化された株価
g = Graphics()
for i in range(len(tickers)):
list_plot([l3[shortest-j-1][i] for j in range(shortest)], rgbcolor=cls[i], plotjoined=True, figsize=(5,2))
# 独立成分分析の結果のプロット
elmts2 = fastica(np.array(l4))
g = Graphics()
for i in range(len(tickers)):
list_plot([elmts2[shortest-j-1][i] for j in range(shortest)], rgbcolor=cls[i], plotjoined=True, figsize=(5,2))