取得股價
import ffn
prices = ffn.get('aapl, goog')
prices
輸出結果
aapl googDate 2015-03-27 113.421997 546.8386842015-03-30 116.293190 550.5185552015-03-31 114.507881 546.4995732015-04-01 114.342239 541.0744632015-04-02 115.326912 534.063721... ... ...2020-03-19 244.779999 1115.2900392020-03-20 229.240005 1072.3199462020-03-23 224.369995 1056.6199952020-03-24 246.880005 1134.4599612020-03-25 245.520004 1102.4899901258 rows × 2 columns使用dataframe的 plot 來繪圖,雖然可以比較Apple 跟Google的股價,但是起始股價是不一樣的,所以無法比較報酬率。
%matplotlib inline
prices.plot()
輸出結果
想要比較報酬率,可以使用 rebase( )函式,讓股價的起始值相同
prices.rebase().plot()
輸出結果
想要比較虧損的幅度,可以使用 to_drawdown_series( )函式
prices.to_drawdown_series().plot()
輸出結果
想要看熱圖,使用 plot_corr_heatmap( )函式
prices.plot_corr_heatmap()
輸出結果
想要比較這兩檔股票的歷史數據分析,可以使用calc_stats( )函式
stats = prices.calc_stats()
stats.display()
輸出結果
Stat aapl goog------------------- ---------- ----------Start 2015-03-27 2015-03-27End 2020-03-25 2020-03-25Risk-free rate 0.00% 0.00%