Skip to content

Instantly share code, notes, and snippets.

@dear983604
Last active December 23, 2018 16:43
Show Gist options
  • Save dear983604/3d6d8f6af57a25f1c727800805a18947 to your computer and use it in GitHub Desktop.
Save dear983604/3d6d8f6af57a25f1c727800805a18947 to your computer and use it in GitHub Desktop.
Dow stock analysis
#輸入套件
%matplotlib inline
import pandas as pd
import numpy as np
#輸入台股歷年股災資料
TAIEX = {'TAIEX_H':[10328.98, 6484.93, 7135, 9859.65, 9220.69, 10014.28, 11270.18],
'TAIEX_L':[3411.68, 3845.76, 5255.06, 3955.43, 6609.11, 7203.07, 9400.69]}
df_TAIEX = pd.DataFrame(TAIEX, index=['2000', '2002', '2004', '2007', '2011', '2015', '2018'])
df_TAIEX['TAIEX_dif'] = (df_TAIEX['TAIEX_H'] - df_TAIEX['TAIEX_L']) / df_TAIEX['TAIEX_H']
df_TAIEX
#輸入美股歷年股災資料,採用道瓊指數
Dow = {'Dow_H':[11750, 10673, 10653.85, 14198.02, 18351.36, 26616.71, 26616.71],
'Dow_L':[8064, 7197, 9710.69, 6470.11, 10606.79, 15370.33, 22445.37]}
df_Dow = pd.DataFrame(Dow, index=['2000', '2002', '2004', '2007', '2011', '2015', '2018'])
df_Dow['Dow_dif'] = (df_Dow['Dow_H'] - df_Dow['Dow_L']) / df_Dow['Dow_H']
df_Dow
#輸入台灣50歷年股災資料,2000、2002年還沒成立
T50 = {'T50_H':[53.7, 72.3, 63.2, 73.3, 88.4],
'T50_L':[40.9, 28.53, 46.61, 55.4, 74.45]}
df_T50 = pd.DataFrame(T50, index=['2004', '2007', '2011', '2015', '2018'])
df_T50['T50_dif'] = (df_T50['T50_H'] - df_T50['T50_L']) / df_T50['T50_H']
df_T50
#做台股、美股、台灣50之間股災跌幅的比較
data = [df_TAIEX['TAIEX_dif'], df_Dow['Dow_dif'], df_T50['T50_dif']]
df_corr = pd.DataFrame(data).T ##Transpose
df_corr
#可以先把2004年差異過大的資料刪除
df_corr1 = df_corr.drop(['2004'])
df_corr1
#用corr指令找出相關係數
df_corr.corr()
#計算美股比照台股跌幅,最深會跌至幾點
from sympy import *
def solve(eq,var='x'):
eq1 = eq.replace("=","-(")+")"
c = eval(eq1,{var:1j})
return -c.real/c.imag
solve('(26616.71-x)/26616.71 = (11270.18-9400.69)/11270.18', 'x')
#--------------------加碼-------------------------
#用describe指令列出均值、標準差等資訊
df_corr.describe()
#用平均跌幅計算台股最低會跌至幾點
11270.18 - 11270.18 * df_corr['TAIEX_dif'].mean()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment