-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex1.py
97 lines (74 loc) · 2.38 KB
/
ex1.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import data
import math
def Run():
market_date = data.GetDates()
# artificial price
spy_aprc = data.GetSPY()
# can use other stock, e.g.
#ko_aprc = data.GetKO()
# aprc = data.GetPEP()
# aprc = data.GetXLP()
#RunStrategy(market_date, ko_aprc)
RunStrategy(market_date, spy_aprc)
def GetReturnSeries(nav_list):
return_series = []
for i in range(len(nav_list)):
if i == 0:
return_series.append(0)
else:
return_series.append((nav_list[i] / nav_list[i-1]) - 1)
return return_series
def Average(l):
return sum(l) / len(l)
def Analyze(nav_list):
# Make Return time series
return_list = GetReturnSeries(nav_list)
# Calculate return average
# nav_list[0] refers to the first element of nav_list.
# nav_list[-1] refers to the *last* element of nav_list.
average_return = math.pow(nav_list[-1] / nav_list[0], 1 / (len(nav_list)-1)) - 1
# Calculate return SD
variance_list = [(r - average_return)**2 for r in return_list]
return_sd = math.sqrt(Average(variance_list))
# Calculate sharpe ratio
trading_days = 252
annual_return = math.pow(nav_list[-1] / nav_list[0], 250.0 / (len(nav_list)-1)) - 1
annual_volatility = return_sd * math.sqrt(trading_days)
# Sharpe
sharpe = annual_return / annual_volatility
results = {
'return': annual_return,
'volatility': annual_volatility,
'sharpe': sharpe,
}
return results
def RunStrategy(market_date, aprc):
# When SPY rises for 2 consecutive days, then long.
# Exit when lower than previous day close.
cash = 100000.0
long_quantity = 0
nav_history = []
for i, d in enumerate(market_date):
# Skip first two days.
if i < 2:
continue
# Exit if lower than previous day close
if aprc[i] < aprc[i-1] and long_quantity > 0:
cash += long_quantity * aprc[i]
long_quantity = 0
# Enter if raised for two consecutive days
if aprc[i] > aprc[i-1] and aprc[i-1] > aprc[i-2] and long_quantity == 0:
long_quantity = cash / aprc[i]
cash = 0
# Book keeping
nav = cash + long_quantity * aprc[i]
nav_history.append(nav)
# Print out result every 250 days
if i % 250 == 0:
print 'Date: %s, NAV: %f' % (d, nav)
results = Analyze(nav_history)
print 'Return: ', results['return']
print 'Volatility: ', results['volatility']
print 'Sharpe: ', results['sharpe']
if __name__ == '__main__':
Run()