Skip to content

Commit

Permalink
Added some discussion.
Browse files Browse the repository at this point in the history
  • Loading branch information
IanLKaplan committed Aug 18, 2022
1 parent d95ad6c commit c3e1a50
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
85 changes: 77 additions & 8 deletions pairs_trading.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {
"pycharm": {
"name": "#%%\n"
Expand Down Expand Up @@ -392,7 +392,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {
"pycharm": {
"name": "#%%\n"
Expand Down Expand Up @@ -449,14 +449,20 @@
"Correlation\n",
"</h3>\n",
"<p>\n",
"\n",
"In selecting pairs for trading, the selection attempts to find pairs that have similar market price behavior and mean reversion.\n",
"One measure of similar market price behavior is correlation. This section examines the correlation distribution for the\n",
"S&P 500 sector pairs.\n",
"</p>\n",
"<p>\n",
"The correlation is performed on the natural log of the close prices. The correlation period consists of one year of trading\n",
"days.\n",
"</p>\n"
],
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"outputs": [],
"source": [
"\n",
Expand Down Expand Up @@ -489,14 +495,22 @@
" return pairs_list\n",
"\n",
"\n",
"def calc_pairs_correlation(stock_close_df: pd.DataFrame, pair: Tuple, window: int, all_cor_v: np.array) -> np.array:\n",
"def calc_pairs_correlation(stock_close_df: pd.DataFrame, pair: Tuple, window: int) -> np.array:\n",
" \"\"\"\n",
" Calculate the windowed correlations for a stock pair over the entire data set.\n",
" :param stock_close_df: A data frame containing the stock close prices\n",
" :param pair: the stock pair\n",
" :param window: The data window\n",
" :return: a numpy array of windowed correlations for the pair over the entire time period.\n",
" \"\"\"\n",
" cor_v = np.zeros(0)\n",
" stock_a = pair[0]\n",
" stock_b = pair[1]\n",
" a_close = stock_close_df[stock_a]\n",
" b_close = stock_close_df[stock_b]\n",
" a_log_close = log(a_close)\n",
" b_log_close = log(b_close)\n",
"\n",
" assert len(a_log_close) == len(b_log_close)\n",
" for i in range(0, len(a_log_close), window):\n",
" sec_a = a_log_close[i:i + window]\n",
Expand All @@ -507,9 +521,15 @@
"\n",
"\n",
"def calc_yearly_correlation(stock_close_df: pd.DataFrame, pairs_list: List[Tuple]) -> np.array:\n",
" \"\"\"\n",
" Calculate the yearly pairs correlation over the entire time period\n",
" :param stock_close_df: A data frame containing the stock close prices. The columns are the stock tickers.\n",
" :param pairs_list: A list of the pairs formed from the S&P 500 sectors.\n",
" :return: A numpy array with the correlcations.\n",
" \"\"\"\n",
" all_cor_v = np.zeros(0)\n",
" for pair in pairs_list:\n",
" cor_v: np.array = calc_pairs_correlation(stock_close_df, pair, trading_days, all_cor_v)\n",
" cor_v: np.array = calc_pairs_correlation(stock_close_df, pair, trading_days)\n",
" all_cor_v = np.append(all_cor_v, cor_v)\n",
" return all_cor_v\n",
"\n",
Expand Down Expand Up @@ -551,8 +571,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"execution_count": 8,
"outputs": [
{
"data": {
"text/plain": "<Figure size 720x576 with 1 Axes>",
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmQAAAHgCAYAAAAL2HHvAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAqe0lEQVR4nO3dfbRkVXnn8e8jKCa2EVDmpgPGVoNjiIngbV+iyaQbjaIrEUx8wSRKFKc1QZOYaNS4JiFxmBDDkixj4stSA8YMLRIdW8QQhG4VR8C+BnkzLY3oshmU8Y14dSRCnvmj9oXiUvd23Vt1ap+q8/2sVeue2uel9t733K5f7/MWmYkkSZLquVftCkiSJHWdgUySJKkyA5kkSVJlBjJJkqTKDGSSJEmVGcgkSZIqO7B2BUbxoAc9KDdt2jTSNr773e9yv/vdbzwVmlJd7wPb3+32g33Q9faDf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JEfI5eKFgaxbkKuCMiPhcRr1y2zqnAfERcRe/k/6FCSJ/vAo8rdToW+LNS/uvAyaUu1wLHl/LtwKvLye8Pz8wv0Rv9WzqP7lJ6o2vfKu9/B9gcvYsOrgNeVsrfQO8Q7VWlf9+wxnoDHFLa/bv0Rheh1x/vLxcbfH3I7fxe9C7SuAr4AfDRddRF0gii9++uJGmaRMSXgM2ZOWzoktRijpBJkiRV5giZJElSZY6QSZIkVWYgkyRJqsxAJkmSVJmBTJIkqTIDmSRJUmUGMkmSpMr+P8/fIyt3RQnrAAAAAElFTkSuQmCC\n"
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"\n",
"\n",
Expand All @@ -565,6 +596,44 @@
}
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"<p>\n",
"The lookback period for pairs selection is one year (254 trading days). As a first step, all of the S&P 500 sector\n",
"pairs will be tested for correlation over the past year. The pairs that have a correlation of 0.75 or more will be\n",
"tested for mean reversion.\n",
"</p>"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%% md\n"
}
},
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"#\n",
"# At this point we have close_prices_df which is a DataFrame containing the close prices for the S&P 500 stocks over\n",
"# the backtest time period.\n",
"#\n",
"# pairs_list contains the list of pairs formed from the S&P 500 sectors.\n",
"#\n",
"\n"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
Expand Down
Loading

0 comments on commit c3e1a50

Please sign in to comment.