-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.py
38 lines (28 loc) · 1.64 KB
/
performance.py
1
import pandas as pdimport matplotlib.pyplot as pltdef performance(df): d = dict() d['365days'] = round((df['Close'].iloc[-1] - df['Close'].rolling(7).mean().iloc[-356]) / df['Close'].iloc[-365] * 100, 2) d['180days'] = round((df['Close'].iloc[-1] - df['Close'].rolling(7).mean().iloc[-180]) / df['Close'].iloc[-180] * 100, 2) d['90days'] = round((df['Close'].iloc[-1] - df['Close'].rolling(7).mean().iloc[-90]) / df['Close'].iloc[-90] * 100, 2) d['21days'] = round((df['Close'].iloc[-1] - df['Close'].rolling(7).mean().iloc[-21]) / df['Close'].iloc[-21] * 100, 2) d['7days'] = round((df['Close'].iloc[-1] - df['Close'].rolling(2).mean().iloc[-7]) / df['Close'].iloc[-7] * 100, 2) return pd.DataFrame.from_dict(d, orient = 'index')def corr(firstAsset, secondAsset, ranges, firstAssetCol='Close', secondAssetCol = 'Close', firstAssetName='first', secondAssetName='second'): d = dict() firstAsset = firstAsset[[firstAssetCol]] secondAsset = secondAsset[[secondAssetCol]] firstAsset = firstAsset.rename(columns = {firstAssetCol:firstAssetName}) secondAsset = secondAsset.rename(columns = {secondAssetCol:secondAssetName}) joinDf = firstAsset.join(secondAsset) joinDf.dropna(inplace=True) for r in ranges: tmpDf = joinDf.iloc[-r:] d[str(r)+'days'] = tmpDf.corr().iloc[0, 1:].values return pd.DataFrame.from_dict(d, orient = 'index')if __name__ == '__main__': import yfinance as yf gold = yf.download('IAU', start='2020-01-01', end='2022-01-01') btc = yf.download('BTC-USD', start='2020-01-01', end='2022-01-01') ris = corr(btc, gold, [180,90,30]) print (ris)