資源簡介
量化投資:以Python為工具,代碼和數據第三部分
代碼片段和文件信息
‘‘‘
Please?use?the?following?example?commands?to?specify?the?path?containing?code?and?data:
import?os
os.chdir(‘E:\\book_data\\part?3\\018‘)
‘‘‘
import?pandas?as?pd
stock=pd.read_csv(‘stockszA.csv‘index_col=‘Trddt‘)
Vanke=stock[stock.Stkcd==2]
close=Vanke.Clsprc
close.head()
close.index=pd.to_datetime(close.index)
close.index.name=‘Date‘
close.head()
lagclose=close.shift(1)
lagclose.head()
Calclose=pd.Dataframe({‘close‘:close‘lagclose‘:lagclose})
Calclose.head()
simpleret=(close-lagclose)/lagclose
simpleret.name=‘simpleret‘
simpleret.head()
calret=pd.merge(Calclosepd.Dataframe(simpleret)left_index=True
right_index=True)
calret.head()
simpleret2=(close-close.shift(2))/close.shift(2)
simpleret2.name=‘simpleret2‘
calret[‘simpleret2‘]=simpleret2
calret.head()
calret.iloc[5:]
import?ffn
ffnSimpleret=ffn.to_returns(close)
ffnSimpleret.name=‘ffnSimpleret‘
ffnSimpleret.head()
#假設一年有245個交易日
annualize=(1+simpleret).cumprod()[-1]**(245/311)-1
annualize
def?annualize(returnsperiod):
????if?period==‘day‘:
????????return((1+returns).cumprod()[-1]**(245/len(returns))-1)
????elif?period==‘month‘:
????????return((1+returns).cumprod()[-1]**(12/len(returns))-1)
????elif?period==‘quarter‘:
????????return((1+returns).cumprod()[-1]**(4/len(returns))-1)
????elif?period==‘year‘:
????????return((1+returns).cumprod()[-1]**(1/len(returns))-1)
????else:
????????raise?Exception(“Wrong?period“)
import?numpy?as?np
comporet=np.log(close/lagclose)
comporet.name=‘comporet‘
comporet.head()
comporet[5]
ffnComporet=ffn.to_log_returns(close)
ffnComporet.head()
comporet2=np.log(close/close.shift(2))
comporet2.name=‘comporet2‘
comporet2.head()
comporet2[5]
comporet=comporet.dropna()
comporet.head()
sumcomporet=comporet+comporet.shift(1)
sumcomporet.head()
simpleret.plot()
((1+simpleret).cumprod()-1).plot()
#數據日期為2014年1月1日到2014年12月31日
#SAPower代表“航天動力”股票,股票代碼為“600343”
#DalianRP代表“大橡塑”股票,股票代碼為“600346”
SAPower=pd.read_csv(‘SAPower.csv‘index_col=‘Date‘)
SAPower.index=pd.to_datetime(SAPower.index)
DalianRP=pd.read_csv(‘DalianRP.csv‘index_col=‘Date‘)
DalianRP.index=pd.to_datetime(DalianRP.index)
returnS=ffn.to_returns(SAPower.Close).dropna()
returnD=ffn.to_returns(DalianRP.Close).dropna()
returnS.std()
returnD.std()
def?cal_half_dev(returns):
????mu=returns.mean()
????temp=returns[returns ????half_deviation=(sum((mu-temp)**2)/len(returns))**0.5
????return(half_deviation)
cal_half_dev(returnS)
cal_half_dev(returnD)
#歷史模擬法
returnS.quantile(0.05)
returnD.quantile(0.05)
#協方差矩陣法
from?scipy.stats?import?norm
norm.ppf(0.05returnS.mean()returnS.std())
norm.ppf(0.05returnD.mean()returnD.std())
returnS[returnS<=returnS.quantile(0.05)].mean()
returnD[returnD<=returnD.quantile(0.05)].mean()
import?datetime
r=pd.Series([00.1-0.1-0.010.010.02]index=[datetime.date(20157x)?for?x?in?range(39)])
r
value=(1+r).cumprod()
value
D=value.cummax()-value
D
d=D/(D+value)
d
MDD=D.max()
MDD
mdd=d.max()
mdd
ffn.calc_max_drawdown(value)
ffn.calc_max_drawdown((1+returnS).cum
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-01-30?09:26??part?3\
?????目錄???????????0??2017-01-30?09:26??part?3\018\
?????文件????????3186??2017-01-19?01:04??part?3\018\018.py
?????文件???????13256??2015-08-01?03:20??part?3\018\DalianRP.csv
?????文件???????12716??2015-08-01?03:13??part?3\018\SAPower.csv
?????文件????43710144??2015-08-21?17:13??part?3\018\stockszA.csv
?????目錄???????????0??2017-01-30?09:26??part?3\019\
?????文件????????5093??2017-01-21?14:44??part?3\019\019.py
?????文件???????71398??2014-07-10?17:07??part?3\019\ret.csv
?????文件????39124607??2015-07-05?22:26??part?3\019\stock.txt
?????目錄???????????0??2017-02-13?08:23??part?3\020\
?????文件????????1146??2017-01-21?14:43??part?3\020\020.py
?????文件??????289951??2015-07-07?00:33??part?3\020\TRD_Index_20.csv
?????文件???????12434??2015-08-01?03:33??part?3\020\xin_an.csv
?????目錄???????????0??2017-01-30?09:26??part?3\021\
?????文件????????1207??2017-01-21?14:42??part?3\021\021.py
?????文件????39124607??2015-07-05?22:26??part?3\021\stock.txt
?????文件??????416706??2015-07-05?20:34??part?3\021\ThreeFactors.txt
評論
共有 條評論