Skip to content

Commit

Permalink
Added commentary on ADF result.
Browse files Browse the repository at this point in the history
  • Loading branch information
IanLKaplan committed Sep 22, 2022
1 parent a9ef441 commit 6317a73
Show file tree
Hide file tree
Showing 506 changed files with 640 additions and 78 deletions.
172 changes: 113 additions & 59 deletions pairs_trading.ipynb

Large diffs are not rendered by default.

42 changes: 23 additions & 19 deletions pairs_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,20 @@ def plot_ts(data_s: pd.Series, title: str, x_label: str, y_label: str) -> None:
plt.show()

def plot_two_ts(data_a: pd.Series, data_b: pd.Series, title: str, x_label: str, y_label: str) -> None:
color_a = 'blue'
color_b = 'green'
fig, ax = plt.subplots(figsize=(10, 8))
ax.grid(True)
plt.gca().xaxis.set_major_locator(mdates.YearLocator())
fig.autofmt_xdate()
plt.title(title)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.plot(data_a, 'blue', label=data_a.columns[0])
ax.tick_params(axis='y', labelcolor='blue')
ax.axhline(y=0, color='black')
ax.plot(data_a, color=color_a, label=data_a.columns[0])
ax.tick_params(axis='y', labelcolor=color_a)
ax2 = ax.twinx()
ax2.plot(data_b, color='green', label=data_b.columns[0])
ax2.tick_params(axis='y', labelcolor='green')
ax2.plot(data_b, color=color_b, label=data_b.columns[0])
ax2.tick_params(axis='y', labelcolor=color_b)
plt.legend(loc='lower right')
plt.show()

Expand Down Expand Up @@ -461,19 +462,23 @@ def apply_return(start_val: float, return_df: pd.DataFrame) -> np.array:
port_a[i] = port_a[i - 1] + port_a[i - 1] * return_a[i - 1]
return port_a

aapl_df = pd.DataFrame(aapl_s)
mpwr_df = pd.DataFrame(mpwr_s)
ret_aapl = return_df(aapl_df)
ret_mpwr = return_df(mpwr_df)
adj_aapl = pd.DataFrame(apply_return(1, ret_aapl))
adj_aapl.columns = aapl_df.columns
adj_aapl.index = aapl_df.index
adj_mpwr = pd.DataFrame(pd.DataFrame(apply_return(1, ret_mpwr)))
adj_mpwr.columns = mpwr_df.columns
adj_mpwr.index = mpwr_df.index

plot_df = pd.concat([adj_aapl, adj_mpwr], axis=1)
plot_df.plot(grid=True, title=f'AAPL/MPWR', figsize=(10, 6))

def calc_holding_return(close_prices_df: pd.DataFrame, symbol: str, start_ix: int, end_ix: int, holding: int) -> pd.DataFrame:
stock_df = pd.DataFrame(close_prices_df[symbol].iloc[start_ix:end_ix])
stock_ret = return_df(stock_df)
holding_ret_df = pd.DataFrame(apply_return(holding, stock_ret))
holding_ret_df.columns = [symbol]
holding_ret_df.index = stock_df.index
return holding_ret_df


holding_ret_aapl = calc_holding_return(close_prices_df=close_prices_df, symbol='AAPL', start_ix=period_start_ix,
end_ix=period_start_ix + lookback_window, holding=1)
holding_ret_mpwr = calc_holding_return(close_prices_df=close_prices_df, symbol='MPWR', start_ix=period_start_ix,
end_ix=period_start_ix + lookback_window, holding=1)

plot_df = pd.concat([holding_ret_aapl, holding_ret_mpwr], axis=1)
plot_df.plot(grid=True, title=f'AAPL/MPWR with a $1 investment', figsize=(10, 6))
plt.axhline(y=1, color='red')
plt.show()

Expand Down Expand Up @@ -630,7 +635,6 @@ def select_pairs(self, start_ix: int, end_ix: int, pairs_list: List[Tuple], thre
res_s = pd.Series(stats_l[0].residuals)
plot_ts(data_s=res_s, title=f'linear regression residuals for {stats_l[0].stock_a} and {stats_l[0].stock_b}',
x_label='Window Start Date', y_label='')
pass


def compute_halflife(prices: pd.Series, lookback_window: int) -> float:
Expand Down
1 change: 1 addition & 0 deletions s_and_p_data/A.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,129.89
2022-09-19,130.72
2022-09-20,128.14
2022-09-21,126.48
1 change: 1 addition & 0 deletions s_and_p_data/AAL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,13.75
2022-09-19,14.21
2022-09-20,13.97
2022-09-21,13.23
1 change: 1 addition & 0 deletions s_and_p_data/AAP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,166.8
2022-09-19,168.18
2022-09-20,165.87
2022-09-21,165.5
1 change: 1 addition & 0 deletions s_and_p_data/AAPL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,150.7
2022-09-19,154.48
2022-09-20,156.9
2022-09-21,153.72
1 change: 1 addition & 0 deletions s_and_p_data/ABBV.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2446,3 +2446,4 @@ Date,Close
2022-09-16,144.06
2022-09-19,142.66
2022-09-20,141.77
2022-09-21,140.31
1 change: 1 addition & 0 deletions s_and_p_data/ABC.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,140.55
2022-09-19,140.37
2022-09-20,138.9
2022-09-21,139.12
1 change: 1 addition & 0 deletions s_and_p_data/ABMD.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,258.67
2022-09-19,257.87
2022-09-20,258.99
2022-09-21,252.81
1 change: 1 addition & 0 deletions s_and_p_data/ABT.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,104.0
2022-09-19,104.09
2022-09-20,102.31
2022-09-21,100.65
1 change: 1 addition & 0 deletions s_and_p_data/ACN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,272.68
2022-09-19,274.98
2022-09-20,270.24
2022-09-21,265.42
1 change: 1 addition & 0 deletions s_and_p_data/ADBE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,299.5
2022-09-19,296.06
2022-09-20,291.06
2022-09-21,286.3
1 change: 1 addition & 0 deletions s_and_p_data/ADI.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,149.31
2022-09-19,149.61
2022-09-20,149.78
2022-09-21,148.44
1 change: 1 addition & 0 deletions s_and_p_data/ADM.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,84.77
2022-09-19,87.33
2022-09-20,86.75
2022-09-21,86.0
1 change: 1 addition & 0 deletions s_and_p_data/ADP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,233.64
2022-09-19,235.72
2022-09-20,233.54
2022-09-21,230.42
1 change: 1 addition & 0 deletions s_and_p_data/ADSK.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,194.97
2022-09-19,196.89
2022-09-20,194.97
2022-09-21,192.42
1 change: 1 addition & 0 deletions s_and_p_data/AEE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,91.73
2022-09-19,92.3
2022-09-20,91.24
2022-09-21,90.46
1 change: 1 addition & 0 deletions s_and_p_data/AEP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,100.36
2022-09-19,101.11
2022-09-20,99.76
2022-09-21,98.57
1 change: 1 addition & 0 deletions s_and_p_data/AES.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,25.92
2022-09-19,26.6
2022-09-20,25.89
2022-09-21,25.4
1 change: 1 addition & 0 deletions s_and_p_data/AFL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,59.5
2022-09-19,60.37
2022-09-20,60.53
2022-09-21,59.79
1 change: 1 addition & 0 deletions s_and_p_data/AIG.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,53.65
2022-09-19,54.59
2022-09-20,53.41
2022-09-21,52.29
1 change: 1 addition & 0 deletions s_and_p_data/AIZ.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,154.83
2022-09-19,155.01
2022-09-20,150.79
2022-09-21,147.99
1 change: 1 addition & 0 deletions s_and_p_data/AJG.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,179.45
2022-09-19,181.39
2022-09-20,179.17
2022-09-21,174.32
1 change: 1 addition & 0 deletions s_and_p_data/AKAM.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,87.16
2022-09-19,88.56
2022-09-20,85.93
2022-09-21,83.8
1 change: 1 addition & 0 deletions s_and_p_data/ALB.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,287.32
2022-09-19,296.95
2022-09-20,287.65
2022-09-21,285.03
1 change: 1 addition & 0 deletions s_and_p_data/ALGN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,249.02
2022-09-19,244.19
2022-09-20,232.22
2022-09-21,226.68
1 change: 1 addition & 0 deletions s_and_p_data/ALK.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,44.38
2022-09-19,45.78
2022-09-20,45.33
2022-09-21,43.49
1 change: 1 addition & 0 deletions s_and_p_data/ALL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,129.42
2022-09-19,132.08
2022-09-20,130.08
2022-09-21,127.85
1 change: 1 addition & 0 deletions s_and_p_data/ALLE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2224,3 +2224,4 @@ Date,Close
2022-09-16,89.69
2022-09-19,92.21
2022-09-20,91.1
2022-09-21,91.13
1 change: 1 addition & 0 deletions s_and_p_data/AMAT.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,88.87
2022-09-19,89.72
2022-09-20,88.12
2022-09-21,87.09
1 change: 1 addition & 0 deletions s_and_p_data/AMCR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2604,3 +2604,4 @@ Date,Close
2022-09-16,11.66
2022-09-19,11.76
2022-09-20,11.44
2022-09-21,11.22
1 change: 1 addition & 0 deletions s_and_p_data/AMD.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,76.51
2022-09-19,76.77
2022-09-20,75.25
2022-09-21,74.48
1 change: 1 addition & 0 deletions s_and_p_data/AME.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,118.95
2022-09-19,120.53
2022-09-20,118.86
2022-09-21,117.56
1 change: 1 addition & 0 deletions s_and_p_data/AMGN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,231.14
2022-09-19,230.76
2022-09-20,227.71
2022-09-21,224.46
1 change: 1 addition & 0 deletions s_and_p_data/AMP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,272.26
2022-09-19,278.14
2022-09-20,275.14
2022-09-21,269.87
1 change: 1 addition & 0 deletions s_and_p_data/AMT.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,245.89
2022-09-19,247.5
2022-09-20,237.9
2022-09-21,234.15
1 change: 1 addition & 0 deletions s_and_p_data/AMZN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,123.53
2022-09-19,124.66
2022-09-20,122.19
2022-09-21,118.54
1 change: 1 addition & 0 deletions s_and_p_data/ANET.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2087,3 +2087,4 @@ Date,Close
2022-09-16,115.73
2022-09-19,114.94
2022-09-20,114.07
2022-09-21,113.82
1 change: 1 addition & 0 deletions s_and_p_data/ANSS.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,240.74
2022-09-19,241.43
2022-09-20,239.35
2022-09-21,235.42
1 change: 1 addition & 0 deletions s_and_p_data/AON.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,277.12
2022-09-19,278.71
2022-09-20,276.22
2022-09-21,272.99
1 change: 1 addition & 0 deletions s_and_p_data/AOS.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,51.72
2022-09-19,52.59
2022-09-20,51.22
2022-09-21,51.06
1 change: 1 addition & 0 deletions s_and_p_data/APA.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,40.46
2022-09-19,40.45
2022-09-20,39.6
2022-09-21,38.43
1 change: 1 addition & 0 deletions s_and_p_data/APD.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,247.36
2022-09-19,249.38
2022-09-20,244.89
2022-09-21,241.46
1 change: 1 addition & 0 deletions s_and_p_data/APH.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,72.73
2022-09-19,73.17
2022-09-20,72.15
2022-09-21,71.81
1 change: 1 addition & 0 deletions s_and_p_data/APTV.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2726,3 +2726,4 @@ Date,Close
2022-09-16,93.28
2022-09-19,96.2
2022-09-20,93.03
2022-09-21,90.85
1 change: 1 addition & 0 deletions s_and_p_data/ARE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,150.66
2022-09-19,149.02
2022-09-20,144.74
2022-09-21,141.24
1 change: 1 addition & 0 deletions s_and_p_data/ATO.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,114.01
2022-09-19,115.22
2022-09-20,113.51
2022-09-21,112.63
1 change: 1 addition & 0 deletions s_and_p_data/ATVI.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,76.02
2022-09-19,75.94
2022-09-20,75.04
2022-09-21,75.32
1 change: 1 addition & 0 deletions s_and_p_data/AVB.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,201.98
2022-09-19,199.63
2022-09-20,192.88
2022-09-21,188.97
1 change: 1 addition & 0 deletions s_and_p_data/AVGO.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3303,3 +3303,4 @@ Date,Close
2022-09-16,502.5
2022-09-19,501.84
2022-09-20,493.06
2022-09-21,482.14
1 change: 1 addition & 0 deletions s_and_p_data/AVY.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,173.37
2022-09-19,179.24
2022-09-20,175.83
2022-09-21,172.94
1 change: 1 addition & 0 deletions s_and_p_data/AWK.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3628,3 +3628,4 @@ Date,Close
2022-09-16,148.4
2022-09-19,148.55
2022-09-20,145.44
2022-09-21,144.32
1 change: 1 addition & 0 deletions s_and_p_data/AXP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,153.08
2022-09-19,155.36
2022-09-20,153.48
2022-09-21,148.71
1 change: 1 addition & 0 deletions s_and_p_data/AZO.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,2165.65
2022-09-19,2097.59
2022-09-20,2131.47
2022-09-21,2104.02
1 change: 1 addition & 0 deletions s_and_p_data/BA.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,144.29
2022-09-19,144.88
2022-09-20,145.94
2022-09-21,143.29
1 change: 1 addition & 0 deletions s_and_p_data/BAC.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,34.12
2022-09-19,34.69
2022-09-20,34.17
2022-09-21,33.15
1 change: 1 addition & 0 deletions s_and_p_data/BALL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,56.66
2022-09-19,56.99
2022-09-20,55.27
2022-09-21,53.9
1 change: 1 addition & 0 deletions s_and_p_data/BAX.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,58.18
2022-09-19,58.43
2022-09-20,56.58
2022-09-21,55.47
1 change: 1 addition & 0 deletions s_and_p_data/BBWI.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,37.82
2022-09-19,38.77
2022-09-20,37.55
2022-09-21,37.01
1 change: 1 addition & 0 deletions s_and_p_data/BBY.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,72.63
2022-09-19,73.97
2022-09-20,70.92
2022-09-21,69.98
1 change: 1 addition & 0 deletions s_and_p_data/BDX.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,254.32
2022-09-19,251.26
2022-09-20,244.11
2022-09-21,238.01
1 change: 1 addition & 0 deletions s_and_p_data/BEN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,24.14
2022-09-19,24.82
2022-09-20,24.54
2022-09-21,24.25
1 change: 1 addition & 0 deletions s_and_p_data/BF-B.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,70.56
2022-09-19,71.23
2022-09-20,71.01
2022-09-21,70.8
1 change: 1 addition & 0 deletions s_and_p_data/BIIB.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,208.26
2022-09-19,209.26
2022-09-20,204.2
2022-09-21,199.34
1 change: 1 addition & 0 deletions s_and_p_data/BIO.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,448.54
2022-09-19,445.5
2022-09-20,437.39
2022-09-21,430.97
1 change: 1 addition & 0 deletions s_and_p_data/BK.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,43.89
2022-09-19,44.21
2022-09-20,43.27
2022-09-21,42.3
1 change: 1 addition & 0 deletions s_and_p_data/BKNG.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3956,3 +3956,4 @@ Date,Close
2022-09-16,1876.45
2022-09-19,1902.99
2022-09-20,1896.63
2022-09-21,1806.7
Loading

0 comments on commit 6317a73

Please sign in to comment.