简体   繁体   中英

How to plot the correlation coefficient for every last 30 days of two dataframe columns over the past year and plot it? (pandas)

在此处输入图片说明 Through the following code, i get the 1 year history data for both eth and btc price, i know how to get the correlation of the two columns for the 12 months. But how to get past 30 days correlation coefficient for each value of 1 year data and plot it?

def get_price(pair):
    df=binance.fetch_ohlcv(pair,timeframe="1d",limit=365)
    df=pd.DataFrame(df).rename(columns={0:"date",1:"open",2:"high",3:"low",4:"close",5:"vol"})
    df.set_index("date",inplace=True)
    df.index=pd.to_datetime(btc.index,unit="ms")+pd.Timedelta(hours=8)
    return df


eth=get_price("ETH/USDT")
btc=get_price("BTC/USDT")


btc["close"].corr(eth["close"])

i tried the following code but not sure if it is correct?

btc["corre"]=btc["close"].rolling(30).corr(eth["close"].rolling(30))

You can groupby month, deriving month from your index. You can then subset your groupby to the two variables you want to correlate.

btc.groupby(btc.index.month)[['Val1','Val2']].corr()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM