-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathExample3_8.py
47 lines (42 loc) · 1.25 KB
/
Example3_8.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
"""
Example 3.8
# Simple Mean-Reverting Model with open prices
"""
import numpy as np
import pandas as pd
startDate = 20060101
endDate = 20061231
df = pd.read_table("miscFiles/SPX_op_20071123.txt")
df["Date"] = df["Date"].astype("int")
df.set_index("Date", inplace=True)
df.sort_index(inplace=True)
dailyret = df.pct_change()
marketDailyret = dailyret.mean(axis=1)
weights = -(
np.array(dailyret) - np.array(marketDailyret).reshape((dailyret.shape[0], 1))
)
wtsum = np.nansum(abs(weights), axis=1)
weights[
wtsum == 0,
] = 0
wtsum[wtsum == 0] = 1
weights = weights / wtsum.reshape((dailyret.shape[0], 1))
dailypnl = np.nansum(
np.array(pd.DataFrame(weights).shift()) * np.array(dailyret), axis=1
)
dailypnl = dailypnl[np.logical_and(df.index >= startDate, df.index <= endDate)]
sharpeRatio = np.sqrt(252) * np.mean(dailypnl) / np.std(dailypnl)
print(sharpeRatio)
"""
# With transaction costs
"""
onewaytcost = 0.0005
weights = weights[np.logical_and(df.index >= startDate, df.index <= endDate)]
dailypnlminustcost = dailypnl - (
np.nansum(abs(weights - np.array(pd.DataFrame(weights).shift())), axis=1)
* onewaytcost
)
sharpeRatioMinusTcost = (
np.sqrt(252) * np.mean(dailypnlminustcost) / np.std(dailypnlminustcost)
)
print(sharpeRatioMinusTcost)