You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

593 lines
19 KiB

2 years ago
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "7c265be0-6004-4685-aa82-f10e186eb417",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from pprint import pprint\n",
"\n",
"from src.tsl import tsl"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "1a06cac5-7690-43a2-adde-a30217e05469",
"metadata": {},
"outputs": [],
"source": [
"stock_id = 'SZ000019'\n",
"start_date = 20220226\n",
"end_date = 20220228"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1f86064d-adbb-48a3-926e-5a3716f50aca",
"metadata": {},
"outputs": [],
"source": [
"with tsl() as t:\n",
" stock_list = t.get_stock_list(20220630)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "0d923151-698c-40a2-86fb-b325972d71d3",
"metadata": {},
"outputs": [],
"source": [
"stock_list_input = ', '.join(['\\'%s\\'' % stock_id for stock_id in stock_list])\n",
"date = 20220630"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "34b7196a-6f31-48b3-8764-96ad6f5a95ab",
"metadata": {},
"outputs": [],
"source": [
"code = \"\"\"\n",
" SetSysParam(pn_cycle(), cy_1m());\n",
"\n",
" stock_list := Array({stock_list});\n",
" r := select \n",
" ['StockID'],\n",
" DateTimeToStr(['date']) as 'time',\n",
" ['open'],\n",
" ['high'],\n",
" ['low'],\n",
" ['close'],\n",
" ['vol'],\n",
" ['amount'],\n",
" ['yclose'] as 'AdjPreClose',\n",
" (['close']/['yclose'] - 1) * 100 as 'PctChg' \n",
" from markettable\n",
" datekey {start_date}T to {end_date}T+0.999 \n",
" of stock_list\n",
" end;\n",
" \n",
" return r;\n",
" \"\"\".format(\n",
" stock_list=stock_list_input,\n",
" start_date=date,\n",
" end_date=date\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "a7203971-d654-477d-8b46-a3a6cabc09a6",
"metadata": {},
"outputs": [],
"source": [
"with tsl() as t:\n",
" r = t.c.exec(code)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "1472c1b0-0c9d-4b43-aa1b-563fd22ab2f2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<bound method PyCapsule.message of TSResultValue<+OK>>"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame(r.value())\n",
"r.message"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "3e3bfa5d-3f74-4653-b74f-05b7cfe5cd42",
"metadata": {},
"outputs": [
{
"data": {
"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>open</th>\n",
" <th>time</th>\n",
" <th>close</th>\n",
" <th>AdjPreClose</th>\n",
" <th>vol</th>\n",
" <th>low</th>\n",
" <th>StockID</th>\n",
" <th>PctChg</th>\n",
" <th>amount</th>\n",
" <th>high</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>43.80</td>\n",
" <td>2022-06-30 09:31:00</td>\n",
" <td>44.30</td>\n",
" <td>42.60</td>\n",
" <td>137348.0</td>\n",
" <td>43.80</td>\n",
" <td>SH688181</td>\n",
" <td>3.990610</td>\n",
" <td>6054242.00</td>\n",
" <td>44.49</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>44.38</td>\n",
" <td>2022-06-30 09:32:00</td>\n",
" <td>44.32</td>\n",
" <td>44.30</td>\n",
" <td>57212.0</td>\n",
" <td>44.30</td>\n",
" <td>SH688181</td>\n",
" <td>0.045147</td>\n",
" <td>2538700.00</td>\n",
" <td>44.50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>44.13</td>\n",
" <td>2022-06-30 09:33:00</td>\n",
" <td>44.10</td>\n",
" <td>44.32</td>\n",
" <td>22417.0</td>\n",
" <td>44.05</td>\n",
" <td>SH688181</td>\n",
" <td>-0.496390</td>\n",
" <td>989985.00</td>\n",
" <td>44.30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>44.07</td>\n",
" <td>2022-06-30 09:34:00</td>\n",
" <td>44.14</td>\n",
" <td>44.10</td>\n",
" <td>43188.0</td>\n",
" <td>44.07</td>\n",
" <td>SH688181</td>\n",
" <td>0.090703</td>\n",
" <td>1906572.00</td>\n",
" <td>44.25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>44.14</td>\n",
" <td>2022-06-30 09:35:00</td>\n",
" <td>44.08</td>\n",
" <td>44.14</td>\n",
" <td>34658.0</td>\n",
" <td>44.08</td>\n",
" <td>SH688181</td>\n",
" <td>-0.135931</td>\n",
" <td>1529313.00</td>\n",
" <td>44.15</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",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1152715</th>\n",
" <td>61.41</td>\n",
" <td>2022-06-30 14:56:00</td>\n",
" <td>61.36</td>\n",
" <td>61.40</td>\n",
" <td>58300.0</td>\n",
" <td>61.36</td>\n",
" <td>SZ301302</td>\n",
" <td>-0.065147</td>\n",
" <td>3579031.00</td>\n",
" <td>61.41</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1152716</th>\n",
" <td>61.38</td>\n",
" <td>2022-06-30 14:57:00</td>\n",
" <td>61.34</td>\n",
" <td>61.36</td>\n",
" <td>61383.0</td>\n",
" <td>61.33</td>\n",
" <td>SZ301302</td>\n",
" <td>-0.032595</td>\n",
" <td>3765668.56</td>\n",
" <td>61.38</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1152717</th>\n",
" <td>61.34</td>\n",
" <td>2022-06-30 14:58:00</td>\n",
" <td>61.34</td>\n",
" <td>61.34</td>\n",
" <td>2500.0</td>\n",
" <td>61.34</td>\n",
" <td>SZ301302</td>\n",
" <td>0.000000</td>\n",
" <td>153310.00</td>\n",
" <td>61.34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1152718</th>\n",
" <td>61.34</td>\n",
" <td>2022-06-30 14:59:00</td>\n",
" <td>61.34</td>\n",
" <td>61.34</td>\n",
" <td>0.0</td>\n",
" <td>61.34</td>\n",
" <td>SZ301302</td>\n",
" <td>0.000000</td>\n",
" <td>0.00</td>\n",
" <td>61.34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1152719</th>\n",
" <td>61.34</td>\n",
" <td>2022-06-30 15:00:00</td>\n",
" <td>61.34</td>\n",
" <td>61.34</td>\n",
" <td>78700.0</td>\n",
" <td>61.34</td>\n",
" <td>SZ301302</td>\n",
" <td>0.000000</td>\n",
" <td>4827458.00</td>\n",
" <td>61.34</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1152720 rows × 10 columns</p>\n",
"</div>"
],
"text/plain": [
" open time close AdjPreClose vol low \\\n",
"0 43.80 2022-06-30 09:31:00 44.30 42.60 137348.0 43.80 \n",
"1 44.38 2022-06-30 09:32:00 44.32 44.30 57212.0 44.30 \n",
"2 44.13 2022-06-30 09:33:00 44.10 44.32 22417.0 44.05 \n",
"3 44.07 2022-06-30 09:34:00 44.14 44.10 43188.0 44.07 \n",
"4 44.14 2022-06-30 09:35:00 44.08 44.14 34658.0 44.08 \n",
"... ... ... ... ... ... ... \n",
"1152715 61.41 2022-06-30 14:56:00 61.36 61.40 58300.0 61.36 \n",
"1152716 61.38 2022-06-30 14:57:00 61.34 61.36 61383.0 61.33 \n",
"1152717 61.34 2022-06-30 14:58:00 61.34 61.34 2500.0 61.34 \n",
"1152718 61.34 2022-06-30 14:59:00 61.34 61.34 0.0 61.34 \n",
"1152719 61.34 2022-06-30 15:00:00 61.34 61.34 78700.0 61.34 \n",
"\n",
" StockID PctChg amount high \n",
"0 SH688181 3.990610 6054242.00 44.49 \n",
"1 SH688181 0.045147 2538700.00 44.50 \n",
"2 SH688181 -0.496390 989985.00 44.30 \n",
"3 SH688181 0.090703 1906572.00 44.25 \n",
"4 SH688181 -0.135931 1529313.00 44.15 \n",
"... ... ... ... ... \n",
"1152715 SZ301302 -0.065147 3579031.00 61.41 \n",
"1152716 SZ301302 -0.032595 3765668.56 61.38 \n",
"1152717 SZ301302 0.000000 153310.00 61.34 \n",
"1152718 SZ301302 0.000000 0.00 61.34 \n",
"1152719 SZ301302 0.000000 4827458.00 61.34 \n",
"\n",
"[1152720 rows x 10 columns]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "02c05004-32f2-474a-b666-54bf6c2a0cd7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"NE430047\n",
" open time close AdjPreClose vol low \\\n",
"7680 11.93 2022-06-30 09:31:00 11.99 11.81 39942.0 11.93 \n",
"7681 11.99 2022-06-30 09:32:00 12.10 11.99 34679.0 11.99 \n",
"7682 12.10 2022-06-30 09:33:00 12.10 12.10 24770.0 12.10 \n",
"7683 11.86 2022-06-30 09:34:00 11.86 12.10 800.0 11.86 \n",
"7684 11.86 2022-06-30 09:35:00 12.00 11.86 800.0 11.86 \n",
"... ... ... ... ... ... ... \n",
"7915 11.75 2022-06-30 14:56:00 11.84 11.75 101.0 11.75 \n",
"7916 11.84 2022-06-30 14:57:00 11.84 11.84 0.0 11.84 \n",
"7917 11.84 2022-06-30 14:58:00 11.84 11.84 0.0 11.84 \n",
"7918 11.84 2022-06-30 14:59:00 11.84 11.84 0.0 11.84 \n",
"7919 11.84 2022-06-30 15:00:00 11.76 11.84 3100.0 11.76 \n",
"\n",
" StockID PctChg amount high \n",
"7680 NE430047 1.524132 478045.98 12.00 \n",
"7681 NE430047 0.917431 419565.86 12.10 \n",
"7682 NE430047 0.000000 299718.68 12.13 \n",
"7683 NE430047 -1.983471 9488.00 11.86 \n",
"7684 NE430047 1.180438 9600.00 12.00 \n",
"... ... ... ... ... \n",
"7915 NE430047 0.765957 1195.84 11.84 \n",
"7916 NE430047 0.000000 0.00 11.84 \n",
"7917 NE430047 0.000000 0.00 11.84 \n",
"7918 NE430047 0.000000 0.00 11.84 \n",
"7919 NE430047 -0.675676 36456.00 11.84 \n",
"\n",
"[240 rows x 10 columns]\n"
]
}
],
"source": [
"for stock_id, df_stock in df.groupby('StockID'):\n",
" print(stock_id)\n",
" pprint(df_stock)\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "8cbfb47e-d6db-40e0-a761-bb8c5f25e40c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" SetSysParam(pn_cycle(), cy_1m());\n",
" SetSysParam(pn_stock(), 'SZ000019');\n",
" \n",
" r := select \n",
" ['StockID'],\n",
" DateTimeToStr(['date']) as 'time',\n",
" ['open'],\n",
" ['high'],\n",
" ['low'],\n",
" ['close'],\n",
" ['vol'],\n",
" ['amount'],\n",
" ['yclose'] as 'AdjPreClose',\n",
" (['close']/['yclose'] - 1) * 100 as 'PctChg' \n",
" from markettable\n",
" datekey 20220226T to 20220228T+0.999 \n",
" of 'SZ000019'\n",
" end;\n",
" \n",
" return r;\n",
" \n"
]
}
],
"source": [
"code = \"\"\"\n",
" SetSysParam(pn_cycle(), cy_1m());\n",
" SetSysParam(pn_stock(), \\'{stock_id}\\');\n",
" \n",
" r := select \n",
" ['StockID'],\n",
" DateTimeToStr(['date']) as 'time',\n",
" ['open'],\n",
" ['high'],\n",
" ['low'],\n",
" ['close'],\n",
" ['vol'],\n",
" ['amount'],\n",
" ['yclose'] as 'AdjPreClose',\n",
" (['close']/['yclose'] - 1) * 100 as 'PctChg' \n",
" from markettable\n",
" datekey {start_date}T to {end_date}T+0.999 \n",
" of \\'{stock_id}\\'\n",
" end;\n",
" \n",
" return r;\n",
" \"\"\".format(\n",
" stock_id=stock_id,\n",
" start_date=start_date,\n",
" end_date=end_date\n",
" )\n",
"print(code)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b59b8b84-ddff-4152-b083-c2d45bd196db",
"metadata": {},
"outputs": [],
"source": [
"with tsl() as ts:\n",
" r = ts.c.exec(code)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "10c48da7-826a-433a-98cc-50ef61ea5f2c",
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(r.value())\n",
"if 'StockID' in df.columns and 'time' in df.columns:\n",
" df.set_index(['StockID', 'time'], inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "326fae08-3bc0-4732-8bb3-d6ae2e2078f4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" open close AdjPreClose vol low \\\n",
"StockID time \n",
"SZ000019 2022-02-28 09:31:00 7.59 7.56 7.55 522000.0 7.54 \n",
" 2022-02-28 09:32:00 7.57 7.54 7.56 159400.0 7.51 \n",
" 2022-02-28 09:33:00 7.54 7.57 7.54 179300.0 7.52 \n",
" 2022-02-28 09:34:00 7.57 7.55 7.57 122800.0 7.54 \n",
" 2022-02-28 09:35:00 7.55 7.60 7.55 410600.0 7.55 \n",
"... ... ... ... ... ... \n",
" 2022-02-28 14:56:00 7.66 7.65 7.66 85936.0 7.65 \n",
" 2022-02-28 14:57:00 7.66 7.66 7.65 59100.0 7.65 \n",
" 2022-02-28 14:58:00 7.66 7.66 7.66 0.0 7.66 \n",
" 2022-02-28 14:59:00 7.66 7.66 7.66 0.0 7.66 \n",
" 2022-02-28 15:00:00 7.66 7.65 7.66 131320.0 7.65 \n",
"\n",
" PctChg amount high \n",
"StockID time \n",
"SZ000019 2022-02-28 09:31:00 0.132450 3955443.0 7.65 \n",
" 2022-02-28 09:32:00 -0.264550 1202779.0 7.57 \n",
" 2022-02-28 09:33:00 0.397878 1354210.0 7.59 \n",
" 2022-02-28 09:34:00 -0.264201 928471.0 7.57 \n",
" 2022-02-28 09:35:00 0.662252 3105064.0 7.60 \n",
"... ... ... ... \n",
" 2022-02-28 14:56:00 -0.130548 657771.4 7.66 \n",
" 2022-02-28 14:57:00 0.130719 452458.0 7.66 \n",
" 2022-02-28 14:58:00 0.000000 0.0 7.66 \n",
" 2022-02-28 14:59:00 0.000000 0.0 7.66 \n",
" 2022-02-28 15:00:00 -0.130548 1004598.0 7.66 \n",
"\n",
"[240 rows x 8 columns]\n"
]
}
],
"source": [
"pprint(df)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "e5616039-f4e4-49a8-9b23-f1185de6c507",
"metadata": {},
"outputs": [],
"source": [
"from itertools import product\n",
"\n",
"quarter_range = [\n",
" ('0101', '0331'),\n",
" ('0401', '0630'),\n",
" ('0701', '0930'),\n",
" ('1001', '1231')\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "c4bd0990-d69c-4f5a-9b97-b5891f88d061",
"metadata": {},
"outputs": [],
"source": [
"year_list = range(2010, 2022)\n",
"date_range_list = list(product(year_list, quarter_range))\n",
"\n",
"date_range_list = [\n",
" (str(year) + quarter_start, str(year) + quarter_end)\n",
" for year, (quarter_start, quarter_end) in date_range_list\n",
"]\n",
"\n",
"date_range_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff4ec8b3-eda5-451f-a6dd-a0bd2517f9ca",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}