-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance_measure.py
64 lines (53 loc) · 2.08 KB
/
performance_measure.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# =============================================================================
# Measuring the performance of a buy and hold strategy
# Author : Mayank Rasu
# Please report bug/issues in the Q&A section
# =============================================================================
# Import necesary libraries
import pandas_datareader.data as pdr
import numpy as np
import datetime
# Download historical data for required stocks
ticker = "^GSPC"
SnP = pdr.get_data_yahoo(ticker,datetime.date.today()-datetime.timedelta(1825),datetime.date.today())
def CAGR(DF):
"function to calculate the Cumulative Annual Growth Rate of a trading strategy"
df = DF.copy()
df["daily_ret"] = DF["Adj Close"].pct_change()
df["cum_return"] = (1 + df["daily_ret"]).cumprod()
n = len(df)/252
CAGR = (df["cum_return"][-1])**(1/n) - 1
return CAGR
def volatility(DF):
"function to calculate annualized volatility of a trading strategy"
df = DF.copy()
df["daily_ret"] = DF["Adj Close"].pct_change()
vol = df["daily_ret"].std() * np.sqrt(252)
return vol
def sharpe(DF,rf):
"function to calculate sharpe ratio ; rf is the risk free rate"
df = DF.copy()
sr = (CAGR(df) - rf)/volatility(df)
return sr
def sortino(DF,rf):
"function to calculate sortino ratio ; rf is the risk free rate"
df = DF.copy()
df["daily_ret"] = DF["Adj Close"].pct_change()
neg_vol = df[df["daily_ret"]<0]["daily_ret"].std() * np.sqrt(252)
sr = (CAGR(df) - rf)/neg_vol
return sr
def max_dd(DF):
"function to calculate max drawdown"
df = DF.copy()
df["daily_ret"] = DF["Adj Close"].pct_change()
df["cum_return"] = (1 + df["daily_ret"]).cumprod()
df["cum_roll_max"] = df["cum_return"].cummax()
df["drawdown"] = df["cum_roll_max"] - df["cum_return"]
df["drawdown_pct"] = df["drawdown"]/df["cum_roll_max"]
max_dd = df["drawdown_pct"].max()
return max_dd
def calmar(DF):
"function to calculate calmar ratio"
df = DF.copy()
clmr = CAGR(df)/max_dd(df)
return clmr