forked from jironghuang/trend_following
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
executable file
·66 lines (42 loc) · 1.84 KB
/
util.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
65
66
import datetime as dt
import pandas as pd
import numpy as np
from pandas.tseries.holiday import USFederalHolidayCalendar
from datetime import datetime
import yfinance as yf
import re
import matplotlib.pyplot as plt
import os
import socket
def get_sharpe(return_stream):
sharpe_ratio = (return_stream.mean() / return_stream.std()) * np.sqrt(252)
return sharpe_ratio
def get_sortino(return_stream):
downside_returns = return_stream[return_stream < 0]
sortino_ratio = (return_stream.mean() / downside_returns.std()) * np.sqrt(252)
return sortino_ratio
def get_max_drawdown(return_stream):
# Cumulative product of portfolio returns
cumprod_ret = (return_stream + 1).cumprod()*100
# Convert the index in datetime format
cumprod_ret.index = pd.to_datetime(cumprod_ret.index)
# Define a variable trough_index to store the index of lowest value before new high
trough_index = (np.maximum.accumulate(cumprod_ret) - cumprod_ret).idxmax()
# Define a variable peak_index to store the index of maximum value before largest drop
peak_index = cumprod_ret.loc[:trough_index].idxmax()
# Calculate the maximum drawdown using the given formula
maximum_drawdown = 100 * \
(cumprod_ret[trough_index] - cumprod_ret[peak_index]) / \
cumprod_ret[peak_index]
return maximum_drawdown
def get_annual_returns(return_stream):
# Total number of trading days in a year is 252
trading_days = 252
# Calculate the average daily returns
average_daily_returns = return_stream.mean()
annual_returns = ((1 + average_daily_returns)**(trading_days) - 1) * 100
return annual_returns
def get_skewness(return_stream):
return(return_stream.skew())
def get_kurtosis(return_stream):
return(return_stream.kurtosis())