-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.py
29 lines (23 loc) · 1.18 KB
/
helper_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np
def calculate_annualized_return(daily_returns):
cumulative_return = np.prod(1 + daily_returns) - 1
num_days = len(daily_returns)
annualized_return = (1 + cumulative_return) ** (252 / num_days) - 1
return annualized_return
def calculate_annualized_volatility(daily_returns):
daily_volatility = np.std(daily_returns)
annualized_volatility = daily_volatility * np.sqrt(252)
return annualized_volatility
def calculate_sharpe_ratio(daily_returns, risk_free_rate=0.01):
annualized_return = calculate_annualized_return(daily_returns)
annualized_volatility = calculate_annualized_volatility(daily_returns)
sharpe_ratio = (annualized_return - risk_free_rate) / annualized_volatility
return sharpe_ratio
if __name__ == "__main__":
daily_returns = np.array([0.001, -0.002, 0.003, 0.004, -0.001])
annualized_return = calculate_annualized_return(daily_returns)
annualized_volatility = calculate_annualized_volatility(daily_returns)
sharpe_ratio = calculate_sharpe_ratio(daily_returns)
print(f"Annualized Return: {annualized_return}")
print(f"Annualized Volatility: {annualized_volatility}")
print(f"Sharpe Ratio: {sharpe_ratio}")