407 lines
331 KiB
Plaintext
407 lines
331 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"# Chap 2. Deciphering the Markets with Technical Analysis\n",
|
|||
|
"\n",
|
|||
|
"### 1.1 Support and Resistance Line"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 1,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"import pandas as pd\n",
|
|||
|
"from pandas_datareader import data\n",
|
|||
|
"\n",
|
|||
|
"start_date = '2014-01-01'\n",
|
|||
|
"end_date = '2018-01-01'\n",
|
|||
|
"SRC_DATA_FILENAME = 'goog_data.pkl'\n",
|
|||
|
"\n",
|
|||
|
"try:\n",
|
|||
|
" goog_data2 = pd.read_pickle(SRC_DATA_FILENAME)\n",
|
|||
|
"except FileNotFoundError:\n",
|
|||
|
" goog_data2 = data.DataReader('GOOG', 'yahoo', start_date, end_date)\n",
|
|||
|
" goog_data2.to_pickle(SRC_DATA_FILENAME)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"把GOOGLE数据下载下来"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 2,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"output_type": "execute_result",
|
|||
|
"data": {
|
|||
|
"text/plain": " High Low Open Close Volume \\\nDate \n2015-07-17 674.468018 645.000000 649.000000 672.929993 11164900.0 \n2015-07-20 668.880005 653.010010 659.239990 663.020020 5860900.0 \n2015-07-21 673.000000 654.299988 655.210022 662.299988 3377200.0 \n2015-07-22 678.640015 659.000000 660.890015 662.099976 3929300.0 \n2015-07-23 663.630005 641.000000 661.270020 644.280029 3029100.0 \n... ... ... ... ... ... \n2017-12-22 1064.199951 1059.439941 1061.109985 1060.119995 755100.0 \n2017-12-26 1060.119995 1050.199951 1058.069946 1056.739990 760600.0 \n2017-12-27 1058.369995 1048.050049 1057.390015 1049.369995 1271900.0 \n2017-12-28 1054.750000 1044.770020 1051.599976 1048.140015 837100.0 \n2017-12-29 1049.699951 1044.900024 1046.719971 1046.400024 887500.0 \n\n Adj Close \nDate \n2015-07-17 672.929993 \n2015-07-20 663.020020 \n2015-07-21 662.299988 \n2015-07-22 662.099976 \n2015-07-23 644.280029 \n... ... \n2017-12-22 1060.119995 \n2017-12-26 1056.739990 \n2017-12-27 1049.369995 \n2017-12-28 1048.140015 \n2017-12-29 1046.400024 \n\n[620 rows x 6 columns]",
|
|||
|
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>High</th>\n <th>Low</th>\n <th>Open</th>\n <th>Close</th>\n <th>Volume</th>\n <th>Adj Close</th>\n </tr>\n <tr>\n <th>Date</th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>2015-07-17</th>\n <td>674.468018</td>\n <td>645.000000</td>\n <td>649.000000</td>\n <td>672.929993</td>\n <td>11164900.0</td>\n <td>672.929993</td>\n </tr>\n <tr>\n <th>2015-07-20</th>\n <td>668.880005</td>\n <td>653.010010</td>\n <td>659.239990</td>\n <td>663.020020</td>\n <td>5860900.0</td>\n <td>663.020020</td>\n </tr>\n <tr>\n <th>2015-07-21</th>\n <td>673.000000</td>\n <td>654.299988</td>\n <td>655.210022</td>\n <td>662.299988</td>\n <td>3377200.0</td>\n <td>662.299988</td>\n </tr>\n <tr>\n <th>2015-07-22</th>\n <td>678.640015</td>\n <td>659.000000</td>\n <td>660.890015</td>\n <td>662.099976</td>\n <td>3929300.0</td>\n <td>662.099976</td>\n </tr>\n <tr>\n <th>2015-07-23</th>\n <td>663.630005</td>\n <td>641.000000</td>\n <td>661.270020</td>\n <td>644.280029</td>\n <td>3029100.0</td>\n <td>644.280029</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n </tr>\n <tr>\n <th>2017-12-22</th>\n <td>1064.199951</td>\n <td>1059.439941</td>\n <td>1061.109985</td>\n <td>1060.119995</td>\n <td>755100.0</td>\n <td>1060.119995</td>\n </tr>\n <tr>\n <th>2017-12-26</th>\n <td>1060.119995</td>\n <td>1050.199951</td>\n <td>1058.069946</td>\n <td>1056.739990</td>\n <td>760600.0</td>\n <td>1056.739990</td>\n </tr>\n <tr>\n <th>2017-12-27</th>\n <td>1058.369995</td>\n <td>1048.050049</td>\n <td>1057.390015</td>\n <td>1049.369995</td>\n <td>1271900.0</td>\n <td>1049.369995</td>\n </tr>\n <tr>\n <th>2017-12-28</th>\n <td>1054.750000</td>\n <td>1044.770020</td>\n <td>1051.599976</td>\n <td>1048.140015</td>\n <td>837100.0</td>\n <td>1048.140015</td>\n </tr>\n <tr>\n <th>2017-12-29</th>\n <td>1049.699951</td>\n <td>1044.900024</td>\n <td>1046.719971</td>\n <td>1046.400024</td>\n <td>887500.0</td>\n <td>1046.400024</td>\n </tr>\n </tbody>\n</table>\n<p>620 rows × 6 columns</p>\n</div>"
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"execution_count": 2
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"goog_data = goog_data2.tail(620)\n",
|
|||
|
"lows = goog_data['Low']\n",
|
|||
|
"highs = goog_data['High']\n",
|
|||
|
"goog_data"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 3,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"output_type": "display_data",
|
|||
|
"data": {
|
|||
|
"text/plain": "<Figure size 432x288 with 1 Axes>",
|
|||
|
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"262.547074pt\" version=\"1.1\" viewBox=\"0 0 395.328125 262.547074\" width=\"395.328125pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 262.547074 \nL 395.328125 262.547074 \nL 395.328125 0 \nL 0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 53.328125 205.39604 \nL 388.128125 205.39604 \nL 388.128125 9.55604 \nL 53.328125 9.55604 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"matplotlib.axis_1\">\n <g id=\"xtick_1\">\n <g id=\"line2d_1\">\n <defs>\n <path d=\"M 0 0 \nL 0 3.5 \n\" id=\"m81b2c59812\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n </defs>\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"63.111242\" xlink:href=\"#m81b2c59812\" y=\"205.39604\"/>\n </g>\n </g>\n <g id=\"text_1\">\n <!-- 2015-07 -->\n <defs>\n <path d=\"M 19.1875 8.296875 \nL 53.609375 8.296875 \nL 53.609375 0 \nL 7.328125 0 \nL 7.328125 8.296875 \nQ 12.9375 14.109375 22.625 23.890625 \nQ 32.328125 33.6875 34.8125 36.53125 \nQ 39.546875 41.84375 41.421875 45.53125 \nQ 43.3125 49.21875 43.3125 52.78125 \nQ 43.3125 58.59375 39.234375 62.25 \nQ 35.15625 65.921875 28.609375 65.921875 \nQ 23.96875 65.921875 18.8125 64.3125 \nQ 13.671875 62.703125 7.8125 59.421875 \nL 7.8125 69.390625 \nQ 13.765625 71.78125 18.9375 73 \nQ 24.125 74.21875 28.421875 74.21875 \nQ 39.75 74.21875 46.484375 68.546875 \nQ 53.21875 62.890625 53.21875 53.421875 \nQ 53.21875 48.921875 51.53125 44.890625 \nQ 49.859375 40.875 45.40625 35.40625 \nQ 44.1875 33.984375 37.640625 27.21875 \nQ 31.109375 20.453125 19.1875 8.296875 \nz\n\" id=\"DejaVuSans-50\"/>\n <path d=\"M 31.78125 66.40625 \nQ 24.171875 66.40625 20.328125 58.90625 \nQ 16.5 51.421875 16.5 36.375 \nQ 16.5 21.390625 20.328125 13.890625 \nQ 24.171875 6.390625 31.78125 6.390625 \nQ 39.453125 6.390625 43.28125 13.890625 \nQ 47.125 21.390625 47.125 36.375 \nQ 47.125 51.421875 43.28125 58.90625 \nQ 39.453125 66.40625 31.78125 66.40625 \nz\nM 31.78125 74.21875 \nQ 44.046875 74.21875 50.515625 64.515625 \nQ 56.984375 54.828125 56.984375 36.375 \nQ 56.984375 17.96875 50.515625 8.265625 \nQ 44.046875 -1.421875 31.78125 -1.421875 \nQ 19.53125 -1.421875 13.0625 8.265625 \nQ 6.59375 17.96875 6.59375 36.375 \nQ 6.59375 54.828125 13.0625 64.515625 \nQ 19.53125 74.21875 31.78125 74.21875 \nz\n\" id=\"DejaVuSans-48\"/>\n <path d=\"M 12.40625 8.296875 \nL 28.515625 8.296875 \nL 28.515625 63.921875 \nL 10.984375 60.40625 \nL 10.984375 69.390625 \nL 28.421875 72.90625 \nL 38.28125 72.90625 \nL 38.28125 8.296875 \nL 54.390625 8.296875 \nL 54.390625 0 \nL 12.40625 0 \nz\n\" id=\"DejaVuSans-49\"/>\n <path d=\"M 10.796875 72.90625 \nL 49.515625 72.90625 \nL 49.515625 64.59375 \nL 19.828125 64.59375 \nL 19.828125 46.734375 \nQ 21.96875 47.46875 24.109375 47.828125 \nQ 26.265625 48.1875 28.421875 48.1875 \nQ 40.625 48.1875 47.75 41.5 \nQ 54.890625 34.8125 54.890625 23.390625 \nQ 54.890625 11.625 47.5625 5.09375 \nQ 40.234375 -1.421875 26.90625 -1.421875 \nQ 22.3125 -1.421875 17.546875 -0.640625 \nQ 12.796875 0.140625 7.71875 1.703125 \nL 7.71875 11.625 \nQ 12.109375 9.234375 16.796875 8.0625 \nQ 21.484375 6.890625 26.703125 6.890625 \nQ 35.15625 6.890625 40.078125 11.328125 \nQ 45.015625 15.765625 45.015625 23.390625 \nQ 45.015625 31 40.078125 35.4375 \nQ 35.15625 39.890625 26.703125 39.890625 \nQ 22.75 39.890625 18.8125 39.015625 \nQ 14.890625 38.140625 10.796875 36.28125 \nz\n\" id=\"DejaVuSans-53\"/>\n <path d=\"M 4.890625 31.390625 \nL 31.203125 31.390625 \nL 31.203125 23.390625 \nL 4.890625 23.390625 \nz\n\" id=\
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAEHCAYAAABfkmooAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOydd3hcxfWw39le1ItlyZIsd5viKmxjTE/oLZUOP0gwEEJJQoCQQk0gBcIHhN5Dh4TmUANJjA3uNsa4ylW21XvZvvP9ce82aVWtldbyvM8zz947d+7cszLcs3POmXOElBKFQqFQKLrDMNQCKBQKhSL5UcpCoVAoFD2ilIVCoVAoekQpC4VCoVD0iFIWCoVCoegRpSwUCoVC0SMJUxZCiGeEENVCiPVRfT8QQnwjhAgKIUo7jP+VEKJMCLFZCHFyVP8pel+ZEOKWRMmrUCgUiq5J5MriOeC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LwNG6j2+XDSylNcwUgqWcxRVPtOjhnrCgSwGQyIODs6g1IyUfd3ALiCwU5jBootLhdz+ZLTeR+AzMyTOimLFNOBoyxUinJF0vLEE1pTHNxIKbl882b2ejWn8Lf5hJFov9pns5yaKFPSHrebwi+/5NwNG+LO9YVe79pAgAL2JlRZ7PN4uIdbw+dW6yhmzVoVM8Zo6P2KaKhRKwtF0nLlldqnKq96cNMWCLDV5aKAvfzK+BiHBRaHr5UxnnZdiQDctnMn9X4/b9TUxJ1rRYtWD+LX/J4T+A9L2l8DEhNltC9KLgCLJQ+7fRxgBAIAGI0HzspCKQtF0nLFFT2PUQx/mgMB0mnUsrQGYq+Z8FOtrywCUvJSVVW
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
}
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import matplotlib.pyplot as plt\n",
|
|||
|
"fig = plt.figure()\n",
|
|||
|
"ax1 = fig.add_subplot(111, ylabel='Google price in $')\n",
|
|||
|
"highs.plot(ax=ax1, color='c', lw=2.)\n",
|
|||
|
"lows.plot(ax=ax1, color='y', lw=2.)\n",
|
|||
|
"\n",
|
|||
|
"# Return the first 200 rows using pandas.DataFrame.head(200)\n",
|
|||
|
"plt.hlines(highs.head(200).max(), lows.index.values[0], lows.index.values[-1], linewidth=2, color='g') \n",
|
|||
|
"plt.hlines(lows.head(200).min(), lows.index.values[0], lows.index.values[-1], linewidth=2, color='r')\n",
|
|||
|
"plt.axvline(linewidth=2, color='b', x=lows.index.values[200], linestyle=':')\n",
|
|||
|
"plt.show()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"* 获取2015-07-01 至2018-01-01的GOOGLE的股票价格。\n",
|
|||
|
"* 画出了 support line (红线), resistence line (绿线)\n",
|
|||
|
"* 蓝线表示数据积累点 (200天)\n",
|
|||
|
"\n",
|
|||
|
"基于这个简单的技术分析,我们可以定出策略,在200天的数据积累后(蓝色虚线):\n",
|
|||
|
"\n",
|
|||
|
"* 股价高至 resistence line 后,挂空头仓位 (short the stock).\n",
|
|||
|
"* 股价低至 support line 后,挂多头仓位 (long the stock).\n",
|
|||
|
"\n",
|
|||
|
"实际效果:\n",
|
|||
|
"\n",
|
|||
|
"* 2016-08 后,GOOG 股价触发 resistance line, 算法开始持续做空 GOOGLE, 损失惨重。\n",
|
|||
|
"\n",
|
|||
|
"分析: \n",
|
|||
|
"\n",
|
|||
|
"* 即便 support & resistance indicator 有经济学逻辑,实际中需要对其进行矫正。例如:移动 support/resistance line.\n",
|
|||
|
"\n",
|
|||
|
"改进(增加特性):\n",
|
|||
|
"\n",
|
|||
|
"* 使用 rolling window 滚动窗口\n",
|
|||
|
"* 数股价触及支撑线或阻力线的数量\n",
|
|||
|
"* 加入 tolerance margin, 从而把支撑/阻力线的空间缩小(如下图)\n",
|
|||
|
"\n",
|
|||
|
"![](../../img/2_1.jpg)\n",
|
|||
|
"\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"方案选择(增加两种参数):\n",
|
|||
|
"\n",
|
|||
|
"* 价格触及 supporting/resistance line 的最少次数\n",
|
|||
|
"* 定一个 tolorance margine"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"metadata": {
|
|||
|
"tags": []
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"output_type": "stream",
|
|||
|
"name": "stdout",
|
|||
|
"text": "File data found...reading GOOG data\n"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import pandas as pd\n",
|
|||
|
"import numpy as np\n",
|
|||
|
"from pandas_datareader import data\n",
|
|||
|
"\n",
|
|||
|
"start_date = '2014-01-01'\n",
|
|||
|
"end_date = '2018-01-01'\n",
|
|||
|
"SRC_DATA_FILENAME = 'goog_data.pkl'\n",
|
|||
|
"\n",
|
|||
|
"try:\n",
|
|||
|
" goog_data = pd.read_pickle(SRC_DATA_FILENAME)\n",
|
|||
|
" print('File data found...reading GOOG data')\n",
|
|||
|
"except FileNotFoundError:\n",
|
|||
|
" print('File not found...downloading the GOOG data')\n",
|
|||
|
" goog_data = data.DataReader('GOOG', 'yahoo', start_date, end_date)\n",
|
|||
|
" goog_data.to_pickle(SRC_DATA_FILENAME)\n",
|
|||
|
"\n",
|
|||
|
"goog_data_signal = pd.DataFrame(index=goog_data.index)\n",
|
|||
|
"goog_data_signal['price'] = goog_data['Adj Close']"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 5,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"import numpy as np\n",
|
|||
|
"def trading_support_resistance(data, bin_width=20):\n",
|
|||
|
"\n",
|
|||
|
" data['sup_tolerance'] = pd.Series(np.zeros(len(data))) # tolerance of support line\n",
|
|||
|
" data['res_tolerance'] = pd.Series(np.zeros(len(data))) # tolerance of resistance line\n",
|
|||
|
"\n",
|
|||
|
" data['sup_count'] = pd.Series(np.zeros(len(data))) # number of hitting support line\n",
|
|||
|
" data['res_count'] = pd.Series(np.zeros(len(data)))\n",
|
|||
|
"\n",
|
|||
|
" data['sup'] = pd.Series(np.zeros(len(data))) # support line value (constant)\n",
|
|||
|
" data['res'] = pd.Series(np.zeros(len(data)))\n",
|
|||
|
"\n",
|
|||
|
" data['position'] = pd.Series(np.zeros(len(data)))\n",
|
|||
|
" data['signal'] = pd.Series(np.zeros(len(data)))\n",
|
|||
|
"\n",
|
|||
|
" in_support = 0\n",
|
|||
|
" in_resistance = 0\n",
|
|||
|
"\n",
|
|||
|
" for x in range((bin_width-1) + bin_width, len(data)):\n",
|
|||
|
" data_section = data[x-bin_width : x+1] # get data within rolling window\n",
|
|||
|
" \n",
|
|||
|
" support_level = min(data_section['price'])\n",
|
|||
|
" resistance_level = max(data_section['price'])\n",
|
|||
|
" range_level = resistance_level - support_level\n",
|
|||
|
"\n",
|
|||
|
" data['res'][x] = resistance_level\n",
|
|||
|
" data['sup'][x] = support_level\n",
|
|||
|
"\n",
|
|||
|
" data['sup_tolerance'][x] = support_level + 0.2*range_level # 20% tolorance\n",
|
|||
|
" data['res_tolerance'][x] = resistance_level - 0.2*range_level\n",
|
|||
|
"\n",
|
|||
|
" if (data['price'][x] >= data['res_tolerance'][x]) and (data['price'][x] <= data['res'][x]):\n",
|
|||
|
" # if price is within resistance tolerance region\n",
|
|||
|
" in_resistance += 1\n",
|
|||
|
" data['res_count'][x] = in_resistance\n",
|
|||
|
" elif (data['price'][x] <= data['sup_tolerance'][x]) and (data['price'][x] >= data['sup'][x]):\n",
|
|||
|
" # price is within support tolerance region\n",
|
|||
|
" in_support += 1\n",
|
|||
|
" data['sup_count'][x] = in_support\n",
|
|||
|
" else:\n",
|
|||
|
" # if not within any region, clear count\n",
|
|||
|
" in_support = 0\n",
|
|||
|
" in_resistance = 0\n",
|
|||
|
"\n",
|
|||
|
" if in_resistance > 2:\n",
|
|||
|
" # If enter resistance region twice\n",
|
|||
|
" data['signal'][x] = 1\n",
|
|||
|
" elif in_support > 2:\n",
|
|||
|
" data['signal'][x] = 0\n",
|
|||
|
" else:\n",
|
|||
|
" data['signal'][x] = data['signal'][x-1]\n",
|
|||
|
" \n",
|
|||
|
" data['position'] = data['signal'].diff()\n",
|
|||
|
"\n",
|
|||
|
"trading_support_resistance(goog_data_signal)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"上述代码完成了\n",
|
|||
|
"\n",
|
|||
|
"* 在规定的滚动时间窗口内(默认20天)基于过去的数据计算支撑线、阻力线。\n",
|
|||
|
"* 基于支撑线、阻力线,计算了20%的区域。\n",
|
|||
|
"* 用diff()计算了下单的时间\n",
|
|||
|
"* 当价格低于支撑线,到达支撑区间后(或者高于阻力线,到达阻力区间),我们会挂多头仓位(空头仓位)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 6,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"output_type": "execute_result",
|
|||
|
"data": {
|
|||
|
"text/plain": " price sup_tolerance res_tolerance sup_count res_count \\\nDate \n2013-12-31 558.262512 NaN NaN NaN NaN \n2014-01-02 554.481689 NaN NaN NaN NaN \n2014-01-03 550.436829 NaN NaN NaN NaN \n2014-01-06 556.573853 NaN NaN NaN NaN \n2014-01-07 567.303589 NaN NaN NaN NaN \n... ... ... ... ... ... \n2017-12-22 1060.119995 1014.371997 1061.44801 NaN NaN \n2017-12-26 1056.739990 1014.371997 1061.44801 NaN NaN \n2017-12-27 1049.369995 1014.371997 1061.44801 NaN NaN \n2017-12-28 1048.140015 1014.371997 1061.44801 NaN NaN \n2017-12-29 1046.400024 1014.371997 1061.44801 NaN NaN \n\n sup res position signal \nDate \n2013-12-31 NaN NaN NaN NaN \n2014-01-02 NaN NaN NaN NaN \n2014-01-03 NaN NaN NaN NaN \n2014-01-06 NaN NaN NaN NaN \n2014-01-07 NaN NaN NaN NaN \n... ... ... ... ... \n2017-12-22 998.679993 1077.140015 0.0 1.0 \n2017-12-26 998.679993 1077.140015 0.0 1.0 \n2017-12-27 998.679993 1077.140015 0.0 1.0 \n2017-12-28 998.679993 1077.140015 0.0 1.0 \n2017-12-29 998.679993 1077.140015 0.0 1.0 \n\n[1008 rows x 9 columns]",
|
|||
|
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>price</th>\n <th>sup_tolerance</th>\n <th>res_tolerance</th>\n <th>sup_count</th>\n <th>res_count</th>\n <th>sup</th>\n <th>res</th>\n <th>position</th>\n <th>signal</th>\n </tr>\n <tr>\n <th>Date</th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>2013-12-31</th>\n <td>558.262512</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2014-01-02</th>\n <td>554.481689</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2014-01-03</th>\n <td>550.436829</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2014-01-06</th>\n <td>556.573853</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2014-01-07</th>\n <td>567.303589</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n </tr>\n <tr>\n <th>2017-12-22</th>\n <td>1060.119995</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2017-12-26</th>\n <td>1056.739990</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2017-12-27</th>\n <td>1049.369995</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2017-12-28</th>\n <td>1048.140015</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2017-12-29</th>\n <td>1046.400024</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n </tbody>\n</table>\n<p>1008 rows × 9 columns</p>\n</div>"
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"execution_count": 6
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"goog_data_signal"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 7,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"output_type": "execute_result",
|
|||
|
"data": {
|
|||
|
"text/plain": " price sup_tolerance res_tolerance sup_count res_count \\\nDate \n2013-12-31 558.262512 NaN NaN NaN NaN \n2014-01-02 554.481689 NaN NaN NaN NaN \n2014-01-03 550.436829 NaN NaN NaN NaN \n2014-01-06 556.573853 NaN NaN NaN NaN \n2014-01-07 567.303589 NaN NaN NaN NaN \n... ... ... ... ... ... \n2017-12-22 1060.119995 1014.371997 1061.44801 NaN NaN \n2017-12-26 1056.739990 1014.371997 1061.44801 NaN NaN \n2017-12-27 1049.369995 1014.371997 1061.44801 NaN NaN \n2017-12-28 1048.140015 1014.371997 1061.44801 NaN NaN \n2017-12-29 1046.400024 1014.371997 1061.44801 NaN NaN \n\n sup res position signal \nDate \n2013-12-31 NaN NaN NaN NaN \n2014-01-02 NaN NaN NaN NaN \n2014-01-03 NaN NaN NaN NaN \n2014-01-06 NaN NaN NaN NaN \n2014-01-07 NaN NaN NaN NaN \n... ... ... ... ... \n2017-12-22 998.679993 1077.140015 0.0 1.0 \n2017-12-26 998.679993 1077.140015 0.0 1.0 \n2017-12-27 998.679993 1077.140015 0.0 1.0 \n2017-12-28 998.679993 1077.140015 0.0 1.0 \n2017-12-29 998.679993 1077.140015 0.0 1.0 \n\n[1008 rows x 9 columns]",
|
|||
|
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>price</th>\n <th>sup_tolerance</th>\n <th>res_tolerance</th>\n <th>sup_count</th>\n <th>res_count</th>\n <th>sup</th>\n <th>res</th>\n <th>position</th>\n <th>signal</th>\n </tr>\n <tr>\n <th>Date</th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>2013-12-31</th>\n <td>558.262512</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2014-01-02</th>\n <td>554.481689</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2014-01-03</th>\n <td>550.436829</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2014-01-06</th>\n <td>556.573853</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2014-01-07</th>\n <td>567.303589</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n </tr>\n <tr>\n <th>2017-12-22</th>\n <td>1060.119995</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2017-12-26</th>\n <td>1056.739990</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2017-12-27</th>\n <td>1049.369995</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2017-12-28</th>\n <td>1048.140015</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2017-12-29</th>\n <td>1046.400024</td>\n <td>1014.371997</td>\n <td>1061.44801</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>998.679993</td>\n <td>1077.140015</td>\n <td>0.0</td>\n <td>1.0</td>\n </tr>\n </tbody>\n</table>\n<p>1008 rows × 9 columns</p>\n</div>"
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"execution_count": 7
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"goog_data_signal"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 8,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"output_type": "display_data",
|
|||
|
"data": {
|
|||
|
"text/plain": "<Figure size 432x288 with 1 Axes>",
|
|||
|
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"262.034398pt\" version=\"1.1\" viewBox=\"0 0 395.328125 262.034398\" width=\"395.328125pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 262.034398 \nL 395.328125 262.034398 \nL 395.328125 0 \nL 0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 53.328125 204.883365 \nL 388.128125 204.883365 \nL 388.128125 9.043365 \nL 53.328125 9.043365 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"matplotlib.axis_1\">\n <g id=\"xtick_1\">\n <g id=\"line2d_1\">\n <defs>\n <path d=\"M 0 0 \nL 0 3.5 \n\" id=\"mcbf5ccb456\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n </defs>\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"68.754918\" xlink:href=\"#mcbf5ccb456\" y=\"204.883365\"/>\n </g>\n </g>\n <g id=\"text_1\">\n <!-- 2014-01 -->\n <defs>\n <path d=\"M 19.1875 8.296875 \nL 53.609375 8.296875 \nL 53.609375 0 \nL 7.328125 0 \nL 7.328125 8.296875 \nQ 12.9375 14.109375 22.625 23.890625 \nQ 32.328125 33.6875 34.8125 36.53125 \nQ 39.546875 41.84375 41.421875 45.53125 \nQ 43.3125 49.21875 43.3125 52.78125 \nQ 43.3125 58.59375 39.234375 62.25 \nQ 35.15625 65.921875 28.609375 65.921875 \nQ 23.96875 65.921875 18.8125 64.3125 \nQ 13.671875 62.703125 7.8125 59.421875 \nL 7.8125 69.390625 \nQ 13.765625 71.78125 18.9375 73 \nQ 24.125 74.21875 28.421875 74.21875 \nQ 39.75 74.21875 46.484375 68.546875 \nQ 53.21875 62.890625 53.21875 53.421875 \nQ 53.21875 48.921875 51.53125 44.890625 \nQ 49.859375 40.875 45.40625 35.40625 \nQ 44.1875 33.984375 37.640625 27.21875 \nQ 31.109375 20.453125 19.1875 8.296875 \nz\n\" id=\"DejaVuSans-50\"/>\n <path d=\"M 31.78125 66.40625 \nQ 24.171875 66.40625 20.328125 58.90625 \nQ 16.5 51.421875 16.5 36.375 \nQ 16.5 21.390625 20.328125 13.890625 \nQ 24.171875 6.390625 31.78125 6.390625 \nQ 39.453125 6.390625 43.28125 13.890625 \nQ 47.125 21.390625 47.125 36.375 \nQ 47.125 51.421875 43.28125 58.90625 \nQ 39.453125 66.40625 31.78125 66.40625 \nz\nM 31.78125 74.21875 \nQ 44.046875 74.21875 50.515625 64.515625 \nQ 56.984375 54.828125 56.984375 36.375 \nQ 56.984375 17.96875 50.515625 8.265625 \nQ 44.046875 -1.421875 31.78125 -1.421875 \nQ 19.53125 -1.421875 13.0625 8.265625 \nQ 6.59375 17.96875 6.59375 36.375 \nQ 6.59375 54.828125 13.0625 64.515625 \nQ 19.53125 74.21875 31.78125 74.21875 \nz\n\" id=\"DejaVuSans-48\"/>\n <path d=\"M 12.40625 8.296875 \nL 28.515625 8.296875 \nL 28.515625 63.921875 \nL 10.984375 60.40625 \nL 10.984375 69.390625 \nL 28.421875 72.90625 \nL 38.28125 72.90625 \nL 38.28125 8.296875 \nL 54.390625 8.296875 \nL 54.390625 0 \nL 12.40625 0 \nz\n\" id=\"DejaVuSans-49\"/>\n <path d=\"M 37.796875 64.3125 \nL 12.890625 25.390625 \nL 37.796875 25.390625 \nz\nM 35.203125 72.90625 \nL 47.609375 72.90625 \nL 47.609375 25.390625 \nL 58.015625 25.390625 \nL 58.015625 17.1875 \nL 47.609375 17.1875 \nL 47.609375 0 \nL 37.796875 0 \nL 37.796875 17.1875 \nL 4.890625 17.1875 \nL 4.890625 26.703125 \nz\n\" id=\"DejaVuSans-52\"/>\n <path d=\"M 4.890625 31.390625 \nL 31.203125 31.390625 \nL 31.203125 23.390625 \nL 4.890625 23.390625 \nz\n\" id=\"DejaVuSans-45\"/>\n </defs>\n <g transform=\"translate(31.530097 239.355211)rotate(-30)scale(0.1 -0.1)\">\n <use xlink:href=\"#DejaVuSans-50\"/>\n <use x=\"63.623047\" xlink:href=\"#DejaVuSans-48\"/>\n <use x=\"127.246094\" xlink:href=\"#DejaVuSans-49\"/>\n <use x=\"190.869141\" xlink:href=\"#DejaVuSans-52\"/>\n <use x=\"254.492188\" xlink:href=\"#DejaVuSans-45\"/>\n <use x=\"290.576172\" xlink:href=\"#DejaVuSans-
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAEGCAYAAACUzrmNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGx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5ftzm6NRqMpac5GxVE3oiF1gTv5kWXcmavPggXw4IOQejaNmBiw+frl6tO6Nfj6FvysBK96uYUe7g7u1CwVPVnNu1qR36G4KMloqAfyaeqTR18JjMvnPvOAecU4NI1GoykSJ9742hGFM7PK//FP8zvJzobsbEhKgrNnYeNGGDFcktGgGSFWaEwsFtx9Bt26webcfmgHx347zC1bp+du8HT/RFdIZaHRaDTXMjHLDxD57X8d5036h7N3mbP955/h9tvh5Ek4+vMBmlnPANCz3UUueCkVY7XC3r1w6FDBz4p99QvCjOOkzn0IiFqrTsqRstDpPsohL774ImvWrCnrYWg0lZbsrGzCB7d2F7Zo4XZqj9A/cQL2TXmfnsAJYPUKC1FREBUFmzapPllZhTwwOclxGOCV5pTHxbl1m797PqAtCw2QnZ2tU3xoNGVM1Ku/0iWnMDvb7bShEb95IlYSnT6HTcAEYKmLZvA2ZqMKUxYyNd15MmMG9O4NQJK3jf/7Zayj6YcDPwDQqEbpryXTlkUpcvz4cVq2bMmwYcNo1aoVQ4YMIT09ndDQUCZNmkSnTp1YsmQJo0aN4rvvvgNg+/btdO/enQ4dOtClSxdSUlLIzs7m2WefdaQ3//jjj8v4zTSaikX6tn25hRYLnDsHDz8MUVEEBEC1auCbfppZgA1YBWz
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
}
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import matplotlib.pyplot as plt\n",
|
|||
|
"\n",
|
|||
|
"fig = plt.figure()\n",
|
|||
|
"ax1 = fig.add_subplot(111, ylabel='Google price in $')\n",
|
|||
|
"goog_data_signal['sup'].plot(ax=ax1, color='g', lw=2.)\n",
|
|||
|
"goog_data_signal['res'].plot(ax=ax1, color='b', lw=2.)\n",
|
|||
|
"goog_data_signal['price'].plot(ax=ax1, color='r', lw=2.)\n",
|
|||
|
"\n",
|
|||
|
"ax1.plot(goog_data_signal.loc[goog_data_signal.position == 1.0].index,\n",
|
|||
|
" goog_data_signal.price[goog_data_signal.position == 1.0], '^', markersize=7, color='k', label='buy')\n",
|
|||
|
"ax1.plot(goog_data_signal.loc[goog_data_signal.position == -1.0].index,\n",
|
|||
|
" goog_data_signal.price[goog_data_signal.position == -1.0], 'v', markersize=7, color='k', label='sell')\n",
|
|||
|
"\n",
|
|||
|
"plt.legend()\n",
|
|||
|
"plt.show()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"交易策略:\n",
|
|||
|
"\n",
|
|||
|
"* 当价格进入支撑线区域后连续两天,就可以买多仓\n",
|
|||
|
"* 价格进入阻力线区域后连续两天,买空仓"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## 2. Creating trading signals based on fundamental technical analysis\n",
|
|||
|
"\n",
|
|||
|
"## SMA"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 9,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"output_type": "display_data",
|
|||
|
"data": {
|
|||
|
"text/plain": "<Figure size 432x288 with 1 Axes>",
|
|||
|
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"262.034398pt\" version=\"1.1\" viewBox=\"0 0 395.328125 262.034398\" width=\"395.328125pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 262.034398 \nL 395.328125 262.034398 \nL 395.328125 0 \nL 0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 53.328125 204.883365 \nL 388.128125 204.883365 \nL 388.128125 9.043365 \nL 53.328125 9.043365 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"matplotlib.axis_1\">\n <g id=\"xtick_1\">\n <g id=\"line2d_1\">\n <defs>\n <path d=\"M 0 0 \nL 0 3.5 \n\" id=\"m2aa93fa3ac\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n </defs>\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"68.754918\" xlink:href=\"#m2aa93fa3ac\" y=\"204.883365\"/>\n </g>\n </g>\n <g id=\"text_1\">\n <!-- 2014-01 -->\n <defs>\n <path d=\"M 19.1875 8.296875 \nL 53.609375 8.296875 \nL 53.609375 0 \nL 7.328125 0 \nL 7.328125 8.296875 \nQ 12.9375 14.109375 22.625 23.890625 \nQ 32.328125 33.6875 34.8125 36.53125 \nQ 39.546875 41.84375 41.421875 45.53125 \nQ 43.3125 49.21875 43.3125 52.78125 \nQ 43.3125 58.59375 39.234375 62.25 \nQ 35.15625 65.921875 28.609375 65.921875 \nQ 23.96875 65.921875 18.8125 64.3125 \nQ 13.671875 62.703125 7.8125 59.421875 \nL 7.8125 69.390625 \nQ 13.765625 71.78125 18.9375 73 \nQ 24.125 74.21875 28.421875 74.21875 \nQ 39.75 74.21875 46.484375 68.546875 \nQ 53.21875 62.890625 53.21875 53.421875 \nQ 53.21875 48.921875 51.53125 44.890625 \nQ 49.859375 40.875 45.40625 35.40625 \nQ 44.1875 33.984375 37.640625 27.21875 \nQ 31.109375 20.453125 19.1875 8.296875 \nz\n\" id=\"DejaVuSans-50\"/>\n <path d=\"M 31.78125 66.40625 \nQ 24.171875 66.40625 20.328125 58.90625 \nQ 16.5 51.421875 16.5 36.375 \nQ 16.5 21.390625 20.328125 13.890625 \nQ 24.171875 6.390625 31.78125 6.390625 \nQ 39.453125 6.390625 43.28125 13.890625 \nQ 47.125 21.390625 47.125 36.375 \nQ 47.125 51.421875 43.28125 58.90625 \nQ 39.453125 66.40625 31.78125 66.40625 \nz\nM 31.78125 74.21875 \nQ 44.046875 74.21875 50.515625 64.515625 \nQ 56.984375 54.828125 56.984375 36.375 \nQ 56.984375 17.96875 50.515625 8.265625 \nQ 44.046875 -1.421875 31.78125 -1.421875 \nQ 19.53125 -1.421875 13.0625 8.265625 \nQ 6.59375 17.96875 6.59375 36.375 \nQ 6.59375 54.828125 13.0625 64.515625 \nQ 19.53125 74.21875 31.78125 74.21875 \nz\n\" id=\"DejaVuSans-48\"/>\n <path d=\"M 12.40625 8.296875 \nL 28.515625 8.296875 \nL 28.515625 63.921875 \nL 10.984375 60.40625 \nL 10.984375 69.390625 \nL 28.421875 72.90625 \nL 38.28125 72.90625 \nL 38.28125 8.296875 \nL 54.390625 8.296875 \nL 54.390625 0 \nL 12.40625 0 \nz\n\" id=\"DejaVuSans-49\"/>\n <path d=\"M 37.796875 64.3125 \nL 12.890625 25.390625 \nL 37.796875 25.390625 \nz\nM 35.203125 72.90625 \nL 47.609375 72.90625 \nL 47.609375 25.390625 \nL 58.015625 25.390625 \nL 58.015625 17.1875 \nL 47.609375 17.1875 \nL 47.609375 0 \nL 37.796875 0 \nL 37.796875 17.1875 \nL 4.890625 17.1875 \nL 4.890625 26.703125 \nz\n\" id=\"DejaVuSans-52\"/>\n <path d=\"M 4.890625 31.390625 \nL 31.203125 31.390625 \nL 31.203125 23.390625 \nL 4.890625 23.390625 \nz\n\" id=\"DejaVuSans-45\"/>\n </defs>\n <g transform=\"translate(31.530097 239.355211)rotate(-30)scale(0.1 -0.1)\">\n <use xlink:href=\"#DejaVuSans-50\"/>\n <use x=\"63.623047\" xlink:href=\"#DejaVuSans-48\"/>\n <use x=\"127.246094\" xlink:href=\"#DejaVuSans-49\"/>\n <use x=\"190.869141\" xlink:href=\"#Dej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kN27d7Nu3ToOHjzI0aNH2bRpE1WrVuWXX37B29u7SHSwsbHhiy++4OTJk+zZs4cZM2aYDNWnn35Kx44dOXv2LB07duTTTz81tRs4cCCHDh3i7NmzjB8/nr59+xYooGDt2rVZvXo1oIUU+eeff/Dw8LivvipXrnzfYVSMnDp1ivT0dLZv386dO3cK1BdAWlpagftQPLqkpqdS9SstGnUlp0ocHnkYJDyuB3tYVwd+agr1XoEr40aAg4PFdVLGogi5fv06rq6ulCpVCgBXV1cqV65MYGAgxkOETk5OvPnmmzRo0IAnnniCffv2ERgYSM2aNVmzZg2g/dLv1asXgYGBeHl58cEHH+Q43ueff07Tpk3x9fXl/fffB8Dd3Z1GjRoB4OzsTP369bl69SoAq1evJigoCICgoCBWrVqVY7/t27dnxIgRprAlP//8M02bNsXPz49+/fqRkJBAXFwcNWrUIDU1FdACEGYuDxo0iCVLlgCwdetWWrVqhY1Nhgvtyy+/pGHDhjRs2JCvv/4agPHjxzNjxgxTnUmTJjFt2jRCQ0NNyaPmzp1L37596dq1K15eXrz11lum+rNmzaJOnTo0a9aMF198kZdfftl0b9GiRTz77LN07tzZZMRatGjBiRMnTHWM/0537tzh+eefp1mzZgQEBJjqz507l549e9KhQwc6duxIfHw8HTt2pFGjRvj4+JjqAXz00UfUrVuX1q1b88wzzzBNX28+f/48Xbt2pXHjxrRp04bTp0/n+G+geLjJPKvwcPbAr5IfU05WokISRJSz41KmFDTOpZyLRikp5UP3aty4sczKyZMnMwpgmVcexMXFST8/P+nl5SVfeukluXXrVimllO3atZP79+/XVUOuX79eSill7969ZadOnWRKSoo8fPiw9PPzk1JKOWfOHFmpUiV569Y
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
}
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import statistics as stats\n",
|
|||
|
"\n",
|
|||
|
"time_period = 20 # number of days over which to average\n",
|
|||
|
"history = [] # to track a history of prices\n",
|
|||
|
"sma_values = [] \n",
|
|||
|
"\n",
|
|||
|
"for close_price in goog_data['Adj Close']:\n",
|
|||
|
" history.append(close_price)\n",
|
|||
|
" if len(history) > time_period:\n",
|
|||
|
" del(history[0])\n",
|
|||
|
" sma_values.append(stats.mean(history))\n",
|
|||
|
"\n",
|
|||
|
"goog_data = goog_data.assign(ClosePrice=pd.Series(goog_data['Adj Close'], index=goog_data.index))\n",
|
|||
|
"goog_data = goog_data.assign(Simple20DayMovingAverage=pd.Series(sma_values, index=goog_data.index))\n",
|
|||
|
"close_price = goog_data['ClosePrice']\n",
|
|||
|
"sma = goog_data['Simple20DayMovingAverage']\n",
|
|||
|
"\n",
|
|||
|
"import matplotlib.pyplot as plt\n",
|
|||
|
"\n",
|
|||
|
"fig = plt.figure()\n",
|
|||
|
"ax1 = fig.add_subplot(111, ylabel='Google price in $')\n",
|
|||
|
"close_price.plot(ax=ax1, color='g', lw=2., legend=True)\n",
|
|||
|
"sma.plot(ax=ax1, color='r', lw=2., legend=True)\n",
|
|||
|
"plt.show()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"20日SMA线消除了部分噪音"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"language_info": {
|
|||
|
"codemirror_mode": {
|
|||
|
"name": "ipython",
|
|||
|
"version": 3
|
|||
|
},
|
|||
|
"file_extension": ".py",
|
|||
|
"mimetype": "text/x-python",
|
|||
|
"name": "python",
|
|||
|
"nbconvert_exporter": "python",
|
|||
|
"pygments_lexer": "ipython3",
|
|||
|
"version": "3.8.3-final"
|
|||
|
},
|
|||
|
"orig_nbformat": 2,
|
|||
|
"kernelspec": {
|
|||
|
"name": "python3",
|
|||
|
"display_name": "Python 3"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 2
|
|||
|
}
|