Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Python script to calculate the 90-day risk adjusted return on assets
import pandas as pd
df_asset_prices = pd.read_csv('asset_prices.csv', header=0, parse_dates=['Date'], index_col=0)
df_asset_prices_shifted89 = df_asset_prices.shift(89).dropna()
df_asset_prices_trunc89 = df_asset_prices[89:]
df_asset_prices_90day_return = (df_asset_prices_trunc89-df_asset_prices_shifted89)/df_asset_prices_shifted89*100
df_DTB3 = pd.read_csv('DTB3.csv', header=0, parse_dates=['DATE'], index_col=0)
df_DTB3 = df_DTB3.dropna()
df_DTB3_shifted89 = df_DTB3.shift(89).dropna()
df_asset_prices_90day_return = pd.concat([df_asset_prices_90day_return, df_DTB3_shifted89], axis=1,
join='inner', ignore_index=False, copy=True)
df_90day_RAR = pd.DataFrame()
df_90day_RAR['RAR_Energy']=df_asset_prices_90day_return[
'Close_Energy']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Metals']=df_asset_prices_90day_return[
'Close_Metals']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Auto']=df_asset_prices_90day_return['Close_Auto']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Technology']=df_asset_prices_90day_return[
'Close_Technology']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Chevron']=df_asset_prices_90day_return['Close_Chevron']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Halliburton']=df_asset_prices_90day_return['Close_Halliburton']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Alcoa']=df_asset_prices_90day_return[
'Close_Alcoa']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Nucor']=df_asset_prices_90day_return['Close_Nucor']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Ford']=df_asset_prices_90day_return['Close_Ford']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Tesla']=df_asset_prices_90day_return['Close_Tesla']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Google']=df_asset_prices_90day_return['Close_Google']-df_asset_prices_90day_return['DTB3']
df_90day_RAR['RAR_Microsoft']=df_asset_prices_90day_return['Close_Microsoft']-df_asset_prices_90day_return['DTB3']
df_90day_RAR.to_csv('90day_RAR_on_assets.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment