【问题标题】:Offset Index Charting抵消指数图表
【发布时间】:2017-02-20 22:54:47
【问题描述】:

我试图在一个指数图上绘制两个股票价格。这个情节很常见,因为它在同一个地方以不同的价格开始了两只股票。

请参阅下面的 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。因此,抵消了给定点的索引收敛。

有谁知道如何做到这一点?

【问题讨论】:

    标签: python charts timeserieschart


    【解决方案1】:

    试图让它变得比实际应该的更困难。见下文:

                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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多