-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathlongshort.py
51 lines (41 loc) · 1.41 KB
/
longshort.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
"""
Illustrate the rec array utility funcitons by loading prices from a
csv file, computing the daily returns, appending the results to the
record arrays, joining on date
"""
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from six.moves import urllib
start_date = "2004-8-19"
end_date = '2018-1-20'
# grab the price data off yahoo csv quotes
r1 = pd.read_csv('data/yahoofinance-AAPL-20040819-20180120.csv',
index_col=0,
parse_dates=True,
infer_datetime_format=True)
r2 = pd.read_csv('data/yahoofinance-GOOG-20040819-20180120.csv',
index_col=0,
parse_dates=True,
infer_datetime_format=True)
# compute the daily returns and add these columns to the arrays
gains1 = np.zeros_like(r1["Adj Close"])
gains2 = np.zeros_like(r2["Adj Close"])
gains1[1:] = np.diff(r1["Adj Close"]) / r1["Adj Close"][:-1]
gains2[1:] = np.diff(r2["Adj Close"]) / r2["Adj Close"][:-1]
r1['gains'] = gains1
r2['gains'] = gains2
# now join them by date
r = pd.merge(r1, r2, left_index=True, right_index=True,
suffixes=(['1', '2']), sort=True)
# long appl, short goog
g = r.gains1 - r.gains2
tr = (1 + g).cumprod() # the total return
# plot the return
fig, ax = plt.subplots()
ax.plot(r.index, tr)
ax.set_title('total return: long APPL, short GOOG')
ax.grid()
fig.autofmt_xdate()
plt.show()