Last active
May 2, 2020 00:44
-
-
Save satoshihirose/0e2bea2a64e7e142571da16d30adf554 to your computer and use it in GitHub Desktop.
株価間倍率を計算してグラフ化します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime as dt | |
import datetime | |
import pandas as pd | |
from pandas import DataFrame | |
import csv | |
from functools import reduce | |
import math | |
import matplotlib | |
import matplotlib.pyplot as plt | |
matplotlib.style.use('ggplot') | |
date = dt.now() | |
datestr = date.strftime('%Y%m%d') | |
def calc(): | |
list_file_name = 'data/stock_list_%s.csv' % datestr | |
stock_list1 = pd.read_csv(list_file_name, encoding = 'shift-jis') | |
stock_list2 = pd.read_csv(list_file_name, encoding = 'shift-jis') | |
for k1, stock1 in stock_list1.iterrows(): | |
code1 = stock1['コード'] | |
try: | |
df1 = code2df(code1) | |
except: | |
continue | |
var_list = {} | |
df_all = DataFrame(columns=(code1,)) | |
for k2, stock2 in stock_list2.iterrows(): | |
code2 = stock2['コード'] | |
if code1 == code2: | |
continue | |
try: | |
df2 = code2df(code2) | |
except: | |
continue | |
l = [] | |
df_ratio = DataFrame(columns=(code2,)) | |
for i, row1 in df1.iterrows(): | |
try: | |
row2 = df2.loc[df2['日付'] == row1['日付']] | |
ratio = float(row1['終値']) / float(row2['終値']) | |
df_ratio.loc[row1['日付']] = [ratio] | |
l.append(ratio) | |
except: | |
#print(row1['終値'], row2['終値']) | |
continue | |
# filter nan | |
l = list(filter(lambda v: v==v, l)) | |
avg = float(sum(l)) / len(l) | |
# calculate standard diviation / average | |
sd = math.sqrt(sum(map(lambda x: pow((x - avg), 2), l)) / len(l)) | |
var = sd / avg | |
last_diff = abs(l[-1] - avg) | |
print(code1, code2, 'avg:', avg, 'sd:', sd, 'var:', var, 'last_diff:', last_diff) | |
if math.isnan(sd) == False: | |
var_list[code2] = var | |
df_all = pd.concat([df_all, df_ratio], axis=1) | |
# plot variation and save image | |
sorted_var = sorted(var_list.items(), key=lambda x: x[1]) | |
for k,v in sorted_var: | |
if code1 < k: | |
print(code1,k,v) | |
df = df_all[k] | |
plot = df.plot() | |
fig = plot.get_figure() | |
fig.savefig("img/%s_%s.png" % (code1, k)) | |
fig.clf() | |
def get_file_name(code, datestr): | |
return "data/%s_%s.csv" % (code, datestr) | |
def code2df(code): | |
filename = get_file_name(code, datestr) | |
return pd.read_csv(filename, encoding = 'shift-jis') | |
if __name__ == "__main__": | |
calc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment