2017-02-20 34 views
0

我試圖在指數圖上繪製兩個股票價格。這種情況非常普遍,因爲它在不同的價位開始兩個股票,在同一個地方。抵消指數圖表

請參閱以下IBM的與TSLA

def get_historical_closes(ticker, start_date, end_date): 
    # get the data for the tickers. This will be a panel 
    p = wb.DataReader(ticker, "yahoo", start_date, end_date) 
    # convert the panel to a DataFrame and selection only Adj Close 
    # while making all index levels columns 
    d = p.to_frame()['Adj Close'].reset_index() 
    # rename the columns 
    d.rename(columns={'minor': 'Ticker', 'Adj Close': 'Close'}, inplace=True) 
    # pivot each ticker to a column 
    pivoted = d.pivot(index='Date', columns='Ticker') 
    # and drop the one level on the columns 
    pivoted.columns = pivoted.columns.droplevel(0) 
    return pivoted 

tickers = ['IBM','TSLA'] 
start = '2015-12-31' 
end  ='2016-12-22' 

df_ret=get_historical_closes(tickers,start,end).pct_change().replace('NaN',0) 
df_ret=np.cumprod(1+df_ret) 
df_ret.plot() 

正如你所看到的圖表,都開始在1.00。

我想要做的是在日期索引中的某個任意點處收斂於1.00。例如,我希望看到相同的圖表,除了2016年7月31日的收盤價爲1時。因此,可以抵消指定點的指數收斂。

有沒有人有任何想法如何做到這一點?

回答

0

正試圖讓它比實際應該更困難。見下:

  df_day=df_ret[df_ret.index=='2016-03-31'] 

      df_plot = pd.DataFrame(index=df_ret.index, columns=df_ret.columns) 

      for col in df_ret.columns:  # For each factor 
       df_plot[col]=df_ret[col]/df_day[col].values 

      df_plot.plot()