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.

1884 lines
65 KiB

2 years ago
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "8721fe9f-083b-4ba3-8380-31dc4e541e27",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"from tqdm import tqdm\n",
"import itertools\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"from pprint import pprint\n",
"\n",
"import pyTSL\n",
"\n",
"TINYSOFT_HOSTNAME = 'tsl.tinysoft.com.cn'\n",
"TINYSOFT_PORT = 443\n",
"TINYSOFT_USERNAME = 'xunuo2005'\n",
"TINYSOFT_PASSWORD = '20220613'\n",
"\n",
"TINYSOFT_DATA_PATH = 'tinysoft-data'\n",
"\n",
"\n",
"class tsl():\n",
" \n",
" def __enter__(self):\n",
" self.c = pyTSL.Client(TINYSOFT_USERNAME, TINYSOFT_PASSWORD, TINYSOFT_HOSTNAME, 443)\n",
" self.c.login()\n",
" return self\n",
" \n",
" \n",
" def __exit__(self, exc_type, exc_val, exc_tb):\n",
" self.c.logout()\n",
" \n",
" \n",
" def get_stock_list(self, date, bk_name='A股'):\n",
" code = \"\"\"\n",
" bkName := \"%s\";\n",
" endT := %d;\n",
" return getAbkbydate(bkName,endT);\n",
" \"\"\" % (bk_name, date)\n",
" r = self.c.exec(code)\n",
" \n",
" return r.value()\n",
" \n",
"\n",
" def get_cmp_report(self, table_id, stock_id, start_date):\n",
" code = \"\"\"\n",
" SetSysParam('ReportMode', -1);\n",
" return Select * \n",
" from infotable {table_id} of '{stock_id}' \n",
" where ['截止日'] >= {start_date}\n",
" end;\n",
" \"\"\".format(\n",
" table_id=table_id, \n",
" stock_id=stock_id, \n",
" start_date=start_date\n",
" )\n",
" r = self.c.exec(code)\n",
"\n",
" df = pd.DataFrame(r.value())\n",
" return df\n",
" \n",
" \n",
" def get_cmp_indicator(self, stock_id, start_year, indicator_config_fname):\n",
" index_list = ['StockID', '报告期']\n",
" \n",
" year_list = range(start_year, 2022, 1)\n",
" quarter_list = ['0331', '0630', '0930', '1231']\n",
" report_date_list = [str(t[0]) + t[1] for t in itertools.product(year_list, quarter_list)]\n",
" \n",
" config_df = pd.read_csv(indicator_config_fname)\n",
" config_df['indicator_id'] = config_df['indicator_id'].astype('string')\n",
" indicator_dict = config_df.set_index('indicator_id')['indicator_name'].to_dict()\n",
" indicator_id_list = list(indicator_dict.keys())\n",
" indicator_name_list = [\"'%s'\" % indicator_dict[indicator_id].strip() for indicator_id in indicator_id_list]\n",
" \n",
" code = \"\"\"\n",
" SetSysParam('ReportMode', -1);\n",
" SetSysParam(pn_reportmode(), -1);\n",
" SetSysParam(pn_stock(), '{stock_id}');\n",
"\n",
" date_array := Array({date_list_input});\n",
" indicator_array := Array({indicator_id_list_input});\n",
" indicator_name_array := Array({indicator_name_list_input});\n",
" r := Array();\n",
" \n",
" for i:=0 to length(date_array)-1 do\n",
" begin\n",
" r[i]['StockID'] := '{stock_id}';\n",
" r[i]['报告期'] := date_array[i];\n",
" for j:=0 to length(indicator_array)-1 do\n",
" begin\n",
" r[i][indicator_name_array[j]] := ReportOfAll(indicator_array[j], date_array[i]);\n",
" end\n",
" end\n",
" return r;\n",
" \"\"\".format(\n",
" stock_id=stock_id, \n",
" date_list_input=', '.join(report_date_list),\n",
" indicator_id_list_input=', '.join(indicator_id_list),\n",
" indicator_name_list_input=', '.join(indicator_name_list)\n",
" )\n",
" r = self.c.exec(code)\n",
" r = r.value()\n",
" df = pd.DataFrame(r, index=range(len(r)))\n",
" df.set_index(index_list, inplace=True)\n",
" return df\n",
"\n",
" \n",
" def get_mkt_trading_days(self, start_date, end_date):\n",
" # TradeDays: \n",
" # nday3: 返回用第二个参数计算N日的一维数字下标数组。\n",
" # SpecDate: 临时修改当前时间的快捷方法\n",
" # Spec: 指定StockID股票为当前股票计算表达式的值返回\n",
" code = \"\"\"\n",
" begt:=inttodate(%d);\n",
" endt:=inttodate(%d);\n",
" return spec(specdate(nday3(tradedays(begt,endt),DateToInt(sp_time())),endt),'SH000001');\n",
" \"\"\" % (start_date, end_date);\n",
" r = self.c.exec(code)\n",
" \n",
" return r.value()\n",
" \n",
" \n",
" def get_mkt_stock_k_daily(self, start_date, end_date, stock_id): \n",
" # vol: 成交量\n",
" # amount: 成交金额\n",
" # cjbs: 成交笔数\n",
" # yclose: 上次价\n",
" code = \"\"\"\n",
" SetSysParam(pn_cycle(),cy_day());\n",
" return \n",
" select \n",
" ['StockID'],\n",
" datetoint(['date']) as 'date',\n",
" ['open'],\n",
" ['high'],\n",
" ['low'],\n",
" ['close'], \n",
" ['vol'], \n",
" ['amount'], \n",
" ['cjbs'],\n",
" ['yclose']\n",
" from markettable \n",
" datekey {start_date}T to {end_date}T+0.999 \n",
" of \\'{stock_id}\\'\n",
" end;\n",
" \"\"\".format(\n",
" stock_id=stock_id, \n",
" start_date=start_date,\n",
" end_date=end_date\n",
" )\n",
" r = self.c.exec(code)\n",
" df = pd.DataFrame(r.value())\n",
" df.set_index(['StockID', 'date'], inplace=True)\n",
" return df\n",
" \n",
"\n",
" def get_mkt_stock_k_daily_ext(self, start_date, end_date, stock_id):\n",
" \n",
" date_list = self.get_mkt_trading_days(start_date, end_date)\n",
" \n",
" # IsSt_3: 是否是ST(指定日)判断当前股票在指定日是否是ST股。函数主要是通过名称变更进行判断。 \n",
" # Stockzf3涨幅(%) =(收盘-昨收)/昨收*100%(已复权)\n",
" # http://www.tinysoft.com.cn/tsdn/helpdoc/display.tsl?id=12582\n",
" # AuthorityFactor: 以上市日为复权基准日向后复权得到指定日比例复权方式的复权因子值即复权方式为1。调用该函数需注意设置当前股票和当前时间。\n",
" code = \"\"\"\n",
" SetSysParam(pn_cycle(),cy_day());\n",
" SetSysParam(pn_stock(), \\'{stock_id}\\');\n",
" \n",
" date_array := Array({date_list_input});\n",
" r := Array();\n",
" \n",
" for i:=0 to length(date_array)-1 do\n",
" begin\n",
" SetSysParam(PN_Date(), inttodate(date_array[i]));\n",
" \n",
" r[i]['StockID'] := \\'{stock_id}\\';\n",
" r[i]['date'] := date_array[i];\n",
" r[i]['PctChg'] := StockZf3();\n",
" r[i]['IsST'] := IsST_3(date_array[i]);\n",
" r[i]['FloatShares'] := StockNegotiableShares(date_array[i]);\n",
" r[i]['Factor'] := AuthorityFactor();\n",
" end;\n",
" return r;\n",
" \"\"\".format(\n",
" stock_id=stock_id,\n",
" date_list_input=', '.join([str(date) for date in date_list])\n",
" )\n",
" r = self.c.exec(code)\n",
"\n",
" df = pd.DataFrame(r.value(), index=range(len(r.value())))\n",
" df.set_index(['StockID', 'date'], inplace=True)\n",
" return df\n",
" \n",
" \n",
" def get_fund_info(self, table_id, fund_id):\n",
" code = \"\"\"\n",
" SetSysParam('ReportMode', -1);\n",
" return Select * \n",
" from infotable {table_id} of '{fund_id}' \n",
" end;\n",
" \"\"\".format(\n",
" table_id=table_id, \n",
" fund_id=fund_id\n",
" )\n",
" r = self.c.exec(code)\n",
"\n",
" df = pd.DataFrame(r.value())\n",
" return df\n",
" \n",
" \n",
" def get_fund_list(self, date):\n",
" pass\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "f71302c9-af94-4cc4-942a-281e76468470",
"metadata": {},
"outputs": [],
"source": [
"with tsl() as t:\n",
" df1 = t.get_mkt_stock_k_daily(20220501, 20220609, 'SZ000012')\n",
" df2 = t.get_mkt_stock_k_daily_ext(20220501, 20220609, 'SZ000012')\n",
" df = pd.concat([df1, df2], axis=1)\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "9ff6c11f-6038-43cf-bd80-a2a3fa09e733",
"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></th>\n",
" <th>close</th>\n",
" <th>vol</th>\n",
" <th>low</th>\n",
" <th>yclose</th>\n",
" <th>cjbs</th>\n",
" <th>amount</th>\n",
" <th>high</th>\n",
" <th>open</th>\n",
" <th>IsST</th>\n",
" <th>Factor</th>\n",
" <th>PctChg</th>\n",
" <th>FloatShares</th>\n",
" </tr>\n",
" <tr>\n",
" <th>StockID</th>\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",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"25\" valign=\"top\">SZ000012</th>\n",
" <th>20220505</th>\n",
" <td>5.78</td>\n",
" <td>52548342.0</td>\n",
" <td>5.61</td>\n",
" <td>5.62</td>\n",
" <td>47277.0</td>\n",
" <td>3.036327e+08</td>\n",
" <td>5.88</td>\n",
" <td>5.63</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>2.846975</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220506</th>\n",
" <td>5.64</td>\n",
" <td>30434955.0</td>\n",
" <td>5.55</td>\n",
" <td>5.78</td>\n",
" <td>37648.0</td>\n",
" <td>1.721508e+08</td>\n",
" <td>5.77</td>\n",
" <td>5.65</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>-2.422145</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220509</th>\n",
" <td>5.68</td>\n",
" <td>26512956.0</td>\n",
" <td>5.61</td>\n",
" <td>5.64</td>\n",
" <td>31115.0</td>\n",
" <td>1.513519e+08</td>\n",
" <td>5.81</td>\n",
" <td>5.62</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.709220</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220510</th>\n",
" <td>5.76</td>\n",
" <td>44880370.0</td>\n",
" <td>5.55</td>\n",
" <td>5.68</td>\n",
" <td>39085.0</td>\n",
" <td>2.556482e+08</td>\n",
" <td>5.77</td>\n",
" <td>5.58</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>1.408451</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220511</th>\n",
" <td>5.82</td>\n",
" <td>63441266.0</td>\n",
" <td>5.80</td>\n",
" <td>5.76</td>\n",
" <td>50303.0</td>\n",
" <td>3.741822e+08</td>\n",
" <td>6.02</td>\n",
" <td>5.81</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>1.041667</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220512</th>\n",
" <td>5.83</td>\n",
" <td>40711757.0</td>\n",
" <td>5.72</td>\n",
" <td>5.82</td>\n",
" <td>30591.0</td>\n",
" <td>2.373038e+08</td>\n",
" <td>5.93</td>\n",
" <td>5.82</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.171821</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220513</th>\n",
" <td>5.95</td>\n",
" <td>44013643.0</td>\n",
" <td>5.81</td>\n",
" <td>5.83</td>\n",
" <td>32703.0</td>\n",
" <td>2.597157e+08</td>\n",
" <td>5.97</td>\n",
" <td>5.89</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>2.058319</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220516</th>\n",
" <td>5.80</td>\n",
" <td>51952217.0</td>\n",
" <td>5.76</td>\n",
" <td>5.95</td>\n",
" <td>46334.0</td>\n",
" <td>3.046104e+08</td>\n",
" <td>6.05</td>\n",
" <td>6.00</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>-2.521008</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220517</th>\n",
" <td>5.88</td>\n",
" <td>37310901.0</td>\n",
" <td>5.67</td>\n",
" <td>5.80</td>\n",
" <td>33021.0</td>\n",
" <td>2.155254e+08</td>\n",
" <td>5.88</td>\n",
" <td>5.82</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>1.379310</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220518</th>\n",
" <td>5.92</td>\n",
" <td>43071593.0</td>\n",
" <td>5.82</td>\n",
" <td>5.88</td>\n",
" <td>36175.0</td>\n",
" <td>2.553750e+08</td>\n",
" <td>6.02</td>\n",
" <td>5.87</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.680272</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220519</th>\n",
" <td>6.09</td>\n",
" <td>71322634.0</td>\n",
" <td>5.77</td>\n",
" <td>5.92</td>\n",
" <td>55347.0</td>\n",
" <td>4.292484e+08</td>\n",
" <td>6.20</td>\n",
" <td>5.81</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>2.871622</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220520</th>\n",
" <td>6.15</td>\n",
" <td>63335796.0</td>\n",
" <td>6.03</td>\n",
" <td>6.09</td>\n",
" <td>47644.0</td>\n",
" <td>3.862153e+08</td>\n",
" <td>6.16</td>\n",
" <td>6.11</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.985222</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220523</th>\n",
" <td>6.17</td>\n",
" <td>44809479.0</td>\n",
" <td>6.10</td>\n",
" <td>6.15</td>\n",
" <td>33057.0</td>\n",
" <td>2.758453e+08</td>\n",
" <td>6.23</td>\n",
" <td>6.19</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.325203</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220524</th>\n",
" <td>5.93</td>\n",
" <td>67558001.0</td>\n",
" <td>5.91</td>\n",
" <td>6.17</td>\n",
" <td>48715.0</td>\n",
" <td>4.162481e+08</td>\n",
" <td>6.37</td>\n",
" <td>6.18</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>-3.889789</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220525</th>\n",
" <td>6.02</td>\n",
" <td>37408360.0</td>\n",
" <td>5.89</td>\n",
" <td>5.93</td>\n",
" <td>28801.0</td>\n",
" <td>2.233981e+08</td>\n",
" <td>6.02</td>\n",
" <td>5.97</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>1.517707</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220526</th>\n",
" <td>6.06</td>\n",
" <td>31951823.0</td>\n",
" <td>5.91</td>\n",
" <td>6.02</td>\n",
" <td>28125.0</td>\n",
" <td>1.925206e+08</td>\n",
" <td>6.11</td>\n",
" <td>6.04</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.664452</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220527</th>\n",
" <td>6.07</td>\n",
" <td>31871879.0</td>\n",
" <td>5.98</td>\n",
" <td>6.06</td>\n",
" <td>26731.0</td>\n",
" <td>1.931219e+08</td>\n",
" <td>6.12</td>\n",
" <td>6.09</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.165017</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220530</th>\n",
" <td>6.12</td>\n",
" <td>27955000.0</td>\n",
" <td>6.03</td>\n",
" <td>6.07</td>\n",
" <td>23213.0</td>\n",
" <td>1.700274e+08</td>\n",
" <td>6.13</td>\n",
" <td>6.11</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.823723</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220531</th>\n",
" <td>6.22</td>\n",
" <td>50331033.0</td>\n",
" <td>6.06</td>\n",
" <td>6.12</td>\n",
" <td>37964.0</td>\n",
" <td>3.103057e+08</td>\n",
" <td>6.24</td>\n",
" <td>6.17</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>1.633987</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220601</th>\n",
" <td>6.15</td>\n",
" <td>35835989.0</td>\n",
" <td>6.11</td>\n",
" <td>6.22</td>\n",
" <td>29394.0</td>\n",
" <td>2.208082e+08</td>\n",
" <td>6.22</td>\n",
" <td>6.22</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>-1.125402</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220602</th>\n",
" <td>6.31</td>\n",
" <td>57818462.0</td>\n",
" <td>6.08</td>\n",
" <td>6.15</td>\n",
" <td>39913.0</td>\n",
" <td>3.613558e+08</td>\n",
" <td>6.35</td>\n",
" <td>6.13</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>2.601626</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220606</th>\n",
" <td>6.35</td>\n",
" <td>49949153.0</td>\n",
" <td>6.23</td>\n",
" <td>6.31</td>\n",
" <td>40078.0</td>\n",
" <td>3.153994e+08</td>\n",
" <td>6.38</td>\n",
" <td>6.29</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.633914</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220607</th>\n",
" <td>6.39</td>\n",
" <td>68554508.0</td>\n",
" <td>6.32</td>\n",
" <td>6.35</td>\n",
" <td>46616.0</td>\n",
" <td>4.418079e+08</td>\n",
" <td>6.59</td>\n",
" <td>6.44</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>0.629921</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220608</th>\n",
" <td>6.33</td>\n",
" <td>55527059.0</td>\n",
" <td>6.20</td>\n",
" <td>6.39</td>\n",
" <td>38473.0</td>\n",
" <td>3.512041e+08</td>\n",
" <td>6.42</td>\n",
" <td>6.35</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>-0.938967</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20220609</th>\n",
" <td>6.18</td>\n",
" <td>34466429.0</td>\n",
" <td>6.13</td>\n",
" <td>6.33</td>\n",
" <td>28828.0</td>\n",
" <td>2.143454e+08</td>\n",
" <td>6.32</td>\n",
" <td>6.32</td>\n",
" <td>0</td>\n",
" <td>0.030667</td>\n",
" <td>-2.369668</td>\n",
" <td>1.956586e+09</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" close vol low yclose cjbs amount \\\n",
"StockID date \n",
"SZ000012 20220505 5.78 52548342.0 5.61 5.62 47277.0 3.036327e+08 \n",
" 20220506 5.64 30434955.0 5.55 5.78 37648.0 1.721508e+08 \n",
" 20220509 5.68 26512956.0 5.61 5.64 31115.0 1.513519e+08 \n",
" 20220510 5.76 44880370.0 5.55 5.68 39085.0 2.556482e+08 \n",
" 20220511 5.82 63441266.0 5.80 5.76 50303.0 3.741822e+08 \n",
" 20220512 5.83 40711757.0 5.72 5.82 30591.0 2.373038e+08 \n",
" 20220513 5.95 44013643.0 5.81 5.83 32703.0 2.597157e+08 \n",
" 20220516 5.80 51952217.0 5.76 5.95 46334.0 3.046104e+08 \n",
" 20220517 5.88 37310901.0 5.67 5.80 33021.0 2.155254e+08 \n",
" 20220518 5.92 43071593.0 5.82 5.88 36175.0 2.553750e+08 \n",
" 20220519 6.09 71322634.0 5.77 5.92 55347.0 4.292484e+08 \n",
" 20220520 6.15 63335796.0 6.03 6.09 47644.0 3.862153e+08 \n",
" 20220523 6.17 44809479.0 6.10 6.15 33057.0 2.758453e+08 \n",
" 20220524 5.93 67558001.0 5.91 6.17 48715.0 4.162481e+08 \n",
" 20220525 6.02 37408360.0 5.89 5.93 28801.0 2.233981e+08 \n",
" 20220526 6.06 31951823.0 5.91 6.02 28125.0 1.925206e+08 \n",
" 20220527 6.07 31871879.0 5.98 6.06 26731.0 1.931219e+08 \n",
" 20220530 6.12 27955000.0 6.03 6.07 23213.0 1.700274e+08 \n",
" 20220531 6.22 50331033.0 6.06 6.12 37964.0 3.103057e+08 \n",
" 20220601 6.15 35835989.0 6.11 6.22 29394.0 2.208082e+08 \n",
" 20220602 6.31 57818462.0 6.08 6.15 39913.0 3.613558e+08 \n",
" 20220606 6.35 49949153.0 6.23 6.31 40078.0 3.153994e+08 \n",
" 20220607 6.39 68554508.0 6.32 6.35 46616.0 4.418079e+08 \n",
" 20220608 6.33 55527059.0 6.20 6.39 38473.0 3.512041e+08 \n",
" 20220609 6.18 34466429.0 6.13 6.33 28828.0 2.143454e+08 \n",
"\n",
" high open IsST Factor PctChg FloatShares \n",
"StockID date \n",
"SZ000012 20220505 5.88 5.63 0 0.030667 2.846975 1.956586e+09 \n",
" 20220506 5.77 5.65 0 0.030667 -2.422145 1.956586e+09 \n",
" 20220509 5.81 5.62 0 0.030667 0.709220 1.956586e+09 \n",
" 20220510 5.77 5.58 0 0.030667 1.408451 1.956586e+09 \n",
" 20220511 6.02 5.81 0 0.030667 1.041667 1.956586e+09 \n",
" 20220512 5.93 5.82 0 0.030667 0.171821 1.956586e+09 \n",
" 20220513 5.97 5.89 0 0.030667 2.058319 1.956586e+09 \n",
" 20220516 6.05 6.00 0 0.030667 -2.521008 1.956586e+09 \n",
" 20220517 5.88 5.82 0 0.030667 1.379310 1.956586e+09 \n",
" 20220518 6.02 5.87 0 0.030667 0.680272 1.956586e+09 \n",
" 20220519 6.20 5.81 0 0.030667 2.871622 1.956586e+09 \n",
" 20220520 6.16 6.11 0 0.030667 0.985222 1.956586e+09 \n",
" 20220523 6.23 6.19 0 0.030667 0.325203 1.956586e+09 \n",
" 20220524 6.37 6.18 0 0.030667 -3.889789 1.956586e+09 \n",
" 20220525 6.02 5.97 0 0.030667 1.517707 1.956586e+09 \n",
" 20220526 6.11 6.04 0 0.030667 0.664452 1.956586e+09 \n",
" 20220527 6.12 6.09 0 0.030667 0.165017 1.956586e+09 \n",
" 20220530 6.13 6.11 0 0.030667 0.823723 1.956586e+09 \n",
" 20220531 6.24 6.17 0 0.030667 1.633987 1.956586e+09 \n",
" 20220601 6.22 6.22 0 0.030667 -1.125402 1.956586e+09 \n",
" 20220602 6.35 6.13 0 0.030667 2.601626 1.956586e+09 \n",
" 20220606 6.38 6.29 0 0.030667 0.633914 1.956586e+09 \n",
" 20220607 6.59 6.44 0 0.030667 0.629921 1.956586e+09 \n",
" 20220608 6.42 6.35 0 0.030667 -0.938967 1.956586e+09 \n",
" 20220609 6.32 6.32 0 0.030667 -2.369668 1.956586e+09 "
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "cbbd95e0-0734-46c7-a314-a11e0a882bbc",
"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>date</th>\n",
" <th>close</th>\n",
" <th>vol</th>\n",
" <th>low</th>\n",
" <th>yclose</th>\n",
" <th>StockID</th>\n",
" <th>cjbs</th>\n",
" <th>amount</th>\n",
" <th>high</th>\n",
" <th>open</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>20220505</td>\n",
" <td>5.78</td>\n",
" <td>52548342.0</td>\n",
" <td>5.61</td>\n",
" <td>5.62</td>\n",
" <td>SZ000012</td>\n",
" <td>47277.0</td>\n",
" <td>3.036327e+08</td>\n",
" <td>5.88</td>\n",
" <td>5.63</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>20220506</td>\n",
" <td>5.64</td>\n",
" <td>30434955.0</td>\n",
" <td>5.55</td>\n",
" <td>5.78</td>\n",
" <td>SZ000012</td>\n",
" <td>37648.0</td>\n",
" <td>1.721508e+08</td>\n",
" <td>5.77</td>\n",
" <td>5.65</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>20220509</td>\n",
" <td>5.68</td>\n",
" <td>26512956.0</td>\n",
" <td>5.61</td>\n",
" <td>5.64</td>\n",
" <td>SZ000012</td>\n",
" <td>31115.0</td>\n",
" <td>1.513519e+08</td>\n",
" <td>5.81</td>\n",
" <td>5.62</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>20220510</td>\n",
" <td>5.76</td>\n",
" <td>44880370.0</td>\n",
" <td>5.55</td>\n",
" <td>5.68</td>\n",
" <td>SZ000012</td>\n",
" <td>39085.0</td>\n",
" <td>2.556482e+08</td>\n",
" <td>5.77</td>\n",
" <td>5.58</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>20220511</td>\n",
" <td>5.82</td>\n",
" <td>63441266.0</td>\n",
" <td>5.80</td>\n",
" <td>5.76</td>\n",
" <td>SZ000012</td>\n",
" <td>50303.0</td>\n",
" <td>3.741822e+08</td>\n",
" <td>6.02</td>\n",
" <td>5.81</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>20220512</td>\n",
" <td>5.83</td>\n",
" <td>40711757.0</td>\n",
" <td>5.72</td>\n",
" <td>5.82</td>\n",
" <td>SZ000012</td>\n",
" <td>30591.0</td>\n",
" <td>2.373038e+08</td>\n",
" <td>5.93</td>\n",
" <td>5.82</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>20220513</td>\n",
" <td>5.95</td>\n",
" <td>44013643.0</td>\n",
" <td>5.81</td>\n",
" <td>5.83</td>\n",
" <td>SZ000012</td>\n",
" <td>32703.0</td>\n",
" <td>2.597157e+08</td>\n",
" <td>5.97</td>\n",
" <td>5.89</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>20220516</td>\n",
" <td>5.80</td>\n",
" <td>51952217.0</td>\n",
" <td>5.76</td>\n",
" <td>5.95</td>\n",
" <td>SZ000012</td>\n",
" <td>46334.0</td>\n",
" <td>3.046104e+08</td>\n",
" <td>6.05</td>\n",
" <td>6.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>20220517</td>\n",
" <td>5.88</td>\n",
" <td>37310901.0</td>\n",
" <td>5.67</td>\n",
" <td>5.80</td>\n",
" <td>SZ000012</td>\n",
" <td>33021.0</td>\n",
" <td>2.155254e+08</td>\n",
" <td>5.88</td>\n",
" <td>5.82</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>20220518</td>\n",
" <td>5.92</td>\n",
" <td>43071593.0</td>\n",
" <td>5.82</td>\n",
" <td>5.88</td>\n",
" <td>SZ000012</td>\n",
" <td>36175.0</td>\n",
" <td>2.553750e+08</td>\n",
" <td>6.02</td>\n",
" <td>5.87</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>20220519</td>\n",
" <td>6.09</td>\n",
" <td>71322634.0</td>\n",
" <td>5.77</td>\n",
" <td>5.92</td>\n",
" <td>SZ000012</td>\n",
" <td>55347.0</td>\n",
" <td>4.292484e+08</td>\n",
" <td>6.20</td>\n",
" <td>5.81</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>20220520</td>\n",
" <td>6.15</td>\n",
" <td>63335796.0</td>\n",
" <td>6.03</td>\n",
" <td>6.09</td>\n",
" <td>SZ000012</td>\n",
" <td>47644.0</td>\n",
" <td>3.862153e+08</td>\n",
" <td>6.16</td>\n",
" <td>6.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>20220523</td>\n",
" <td>6.17</td>\n",
" <td>44809479.0</td>\n",
" <td>6.10</td>\n",
" <td>6.15</td>\n",
" <td>SZ000012</td>\n",
" <td>33057.0</td>\n",
" <td>2.758453e+08</td>\n",
" <td>6.23</td>\n",
" <td>6.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>20220524</td>\n",
" <td>5.93</td>\n",
" <td>67558001.0</td>\n",
" <td>5.91</td>\n",
" <td>6.17</td>\n",
" <td>SZ000012</td>\n",
" <td>48715.0</td>\n",
" <td>4.162481e+08</td>\n",
" <td>6.37</td>\n",
" <td>6.18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>20220525</td>\n",
" <td>6.02</td>\n",
" <td>37408360.0</td>\n",
" <td>5.89</td>\n",
" <td>5.93</td>\n",
" <td>SZ000012</td>\n",
" <td>28801.0</td>\n",
" <td>2.233981e+08</td>\n",
" <td>6.02</td>\n",
" <td>5.97</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>20220526</td>\n",
" <td>6.06</td>\n",
" <td>31951823.0</td>\n",
" <td>5.91</td>\n",
" <td>6.02</td>\n",
" <td>SZ000012</td>\n",
" <td>28125.0</td>\n",
" <td>1.925206e+08</td>\n",
" <td>6.11</td>\n",
" <td>6.04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>20220527</td>\n",
" <td>6.07</td>\n",
" <td>31871879.0</td>\n",
" <td>5.98</td>\n",
" <td>6.06</td>\n",
" <td>SZ000012</td>\n",
" <td>26731.0</td>\n",
" <td>1.931219e+08</td>\n",
" <td>6.12</td>\n",
" <td>6.09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>20220530</td>\n",
" <td>6.12</td>\n",
" <td>27955000.0</td>\n",
" <td>6.03</td>\n",
" <td>6.07</td>\n",
" <td>SZ000012</td>\n",
" <td>23213.0</td>\n",
" <td>1.700274e+08</td>\n",
" <td>6.13</td>\n",
" <td>6.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>20220531</td>\n",
" <td>6.22</td>\n",
" <td>50331033.0</td>\n",
" <td>6.06</td>\n",
" <td>6.12</td>\n",
" <td>SZ000012</td>\n",
" <td>37964.0</td>\n",
" <td>3.103057e+08</td>\n",
" <td>6.24</td>\n",
" <td>6.17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>20220601</td>\n",
" <td>6.15</td>\n",
" <td>35835989.0</td>\n",
" <td>6.11</td>\n",
" <td>6.22</td>\n",
" <td>SZ000012</td>\n",
" <td>29394.0</td>\n",
" <td>2.208082e+08</td>\n",
" <td>6.22</td>\n",
" <td>6.22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>20220602</td>\n",
" <td>6.31</td>\n",
" <td>57818462.0</td>\n",
" <td>6.08</td>\n",
" <td>6.15</td>\n",
" <td>SZ000012</td>\n",
" <td>39913.0</td>\n",
" <td>3.613558e+08</td>\n",
" <td>6.35</td>\n",
" <td>6.13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>20220606</td>\n",
" <td>6.35</td>\n",
" <td>49949153.0</td>\n",
" <td>6.23</td>\n",
" <td>6.31</td>\n",
" <td>SZ000012</td>\n",
" <td>40078.0</td>\n",
" <td>3.153994e+08</td>\n",
" <td>6.38</td>\n",
" <td>6.29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>20220607</td>\n",
" <td>6.39</td>\n",
" <td>68554508.0</td>\n",
" <td>6.32</td>\n",
" <td>6.35</td>\n",
" <td>SZ000012</td>\n",
" <td>46616.0</td>\n",
" <td>4.418079e+08</td>\n",
" <td>6.59</td>\n",
" <td>6.44</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>20220608</td>\n",
" <td>6.33</td>\n",
" <td>55527059.0</td>\n",
" <td>6.20</td>\n",
" <td>6.39</td>\n",
" <td>SZ000012</td>\n",
" <td>38473.0</td>\n",
" <td>3.512041e+08</td>\n",
" <td>6.42</td>\n",
" <td>6.35</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>20220609</td>\n",
" <td>6.18</td>\n",
" <td>34466429.0</td>\n",
" <td>6.13</td>\n",
" <td>6.33</td>\n",
" <td>SZ000012</td>\n",
" <td>28828.0</td>\n",
" <td>2.143454e+08</td>\n",
" <td>6.32</td>\n",
" <td>6.32</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" date close vol low yclose StockID cjbs \\\n",
"0 20220505 5.78 52548342.0 5.61 5.62 SZ000012 47277.0 \n",
"1 20220506 5.64 30434955.0 5.55 5.78 SZ000012 37648.0 \n",
"2 20220509 5.68 26512956.0 5.61 5.64 SZ000012 31115.0 \n",
"3 20220510 5.76 44880370.0 5.55 5.68 SZ000012 39085.0 \n",
"4 20220511 5.82 63441266.0 5.80 5.76 SZ000012 50303.0 \n",
"5 20220512 5.83 40711757.0 5.72 5.82 SZ000012 30591.0 \n",
"6 20220513 5.95 44013643.0 5.81 5.83 SZ000012 32703.0 \n",
"7 20220516 5.80 51952217.0 5.76 5.95 SZ000012 46334.0 \n",
"8 20220517 5.88 37310901.0 5.67 5.80 SZ000012 33021.0 \n",
"9 20220518 5.92 43071593.0 5.82 5.88 SZ000012 36175.0 \n",
"10 20220519 6.09 71322634.0 5.77 5.92 SZ000012 55347.0 \n",
"11 20220520 6.15 63335796.0 6.03 6.09 SZ000012 47644.0 \n",
"12 20220523 6.17 44809479.0 6.10 6.15 SZ000012 33057.0 \n",
"13 20220524 5.93 67558001.0 5.91 6.17 SZ000012 48715.0 \n",
"14 20220525 6.02 37408360.0 5.89 5.93 SZ000012 28801.0 \n",
"15 20220526 6.06 31951823.0 5.91 6.02 SZ000012 28125.0 \n",
"16 20220527 6.07 31871879.0 5.98 6.06 SZ000012 26731.0 \n",
"17 20220530 6.12 27955000.0 6.03 6.07 SZ000012 23213.0 \n",
"18 20220531 6.22 50331033.0 6.06 6.12 SZ000012 37964.0 \n",
"19 20220601 6.15 35835989.0 6.11 6.22 SZ000012 29394.0 \n",
"20 20220602 6.31 57818462.0 6.08 6.15 SZ000012 39913.0 \n",
"21 20220606 6.35 49949153.0 6.23 6.31 SZ000012 40078.0 \n",
"22 20220607 6.39 68554508.0 6.32 6.35 SZ000012 46616.0 \n",
"23 20220608 6.33 55527059.0 6.20 6.39 SZ000012 38473.0 \n",
"24 20220609 6.18 34466429.0 6.13 6.33 SZ000012 28828.0 \n",
"\n",
" amount high open \n",
"0 3.036327e+08 5.88 5.63 \n",
"1 1.721508e+08 5.77 5.65 \n",
"2 1.513519e+08 5.81 5.62 \n",
"3 2.556482e+08 5.77 5.58 \n",
"4 3.741822e+08 6.02 5.81 \n",
"5 2.373038e+08 5.93 5.82 \n",
"6 2.597157e+08 5.97 5.89 \n",
"7 3.046104e+08 6.05 6.00 \n",
"8 2.155254e+08 5.88 5.82 \n",
"9 2.553750e+08 6.02 5.87 \n",
"10 4.292484e+08 6.20 5.81 \n",
"11 3.862153e+08 6.16 6.11 \n",
"12 2.758453e+08 6.23 6.19 \n",
"13 4.162481e+08 6.37 6.18 \n",
"14 2.233981e+08 6.02 5.97 \n",
"15 1.925206e+08 6.11 6.04 \n",
"16 1.931219e+08 6.12 6.09 \n",
"17 1.700274e+08 6.13 6.11 \n",
"18 3.103057e+08 6.24 6.17 \n",
"19 2.208082e+08 6.22 6.22 \n",
"20 3.613558e+08 6.35 6.13 \n",
"21 3.153994e+08 6.38 6.29 \n",
"22 4.418079e+08 6.59 6.44 \n",
"23 3.512041e+08 6.42 6.35 \n",
"24 2.143454e+08 6.32 6.32 "
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8ebd92ac-9831-4f66-9614-8cf06a73033c",
"metadata": {},
"outputs": [],
"source": [
"def dump_to_csv(path_template, path_args={}):\n",
" def inner(func):\n",
" def func_wrapper(*args, **kwargs):\n",
" r = func(*args, **kwargs)\n",
" print(path_template.format(**path_args))\n",
" print('Will dump to a csv file...')\n",
" return r\n",
" return func_wrapper\n",
" return inner"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "36926197-5016-459b-a91d-5f2486ff600b",
"metadata": {},
"outputs": [],
"source": [
"@dump_to_csv(path_template='aaa{name}dd', path_args={'name':'ccc'})\n",
"def dump_test():\n",
" print('do test')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "389c2c4a-6e7d-45ad-9da1-780dffab3371",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"do test\n",
"aaacccdd\n",
"Will dump to a csv file...\n"
]
}
],
"source": [
"dump_test()"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "0c5b725b-7ad7-4466-ba3c-ec6624c2575d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inside decorator\n",
"Inside inner function\n",
"I like geeksforgeeks\n",
"Inside actual function\n"
]
}
],
"source": [
"def decorator(*args, **kwargs):\n",
" print(\"Inside decorator\")\n",
" \n",
" def inner(func):\n",
" \n",
" # code functionality here\n",
" print(\"Inside inner function\")\n",
" print(\"I like\", kwargs['like'])\n",
" \n",
" func()\n",
" \n",
" # returning inner function \n",
" return inner\n",
" \n",
"@decorator(like = \"geeksforgeeks\")\n",
"def my_func():\n",
" print(\"Inside actual function\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "c7ec2d92-5d6b-4b0c-afa0-d0210b64e0df",
"metadata": {},
"outputs": [],
"source": [
"with tsl() as t:\n",
" date_list = t.get_mkt_trading_days(20000101,20220620)\n",
" s = pd.Series(date_list, dtype=np.int64, name='date')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "dd10b07e-a170-4428-a6d7-19a7ac06dd18",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 20000104\n",
"1 20000105\n",
"2 20000106\n",
"3 20000107\n",
"4 20000110\n",
" ... \n",
"5436 20220614\n",
"5437 20220615\n",
"5438 20220616\n",
"5439 20220617\n",
"5440 20220620\n",
"Name: date, Length: 5441, dtype: int64\n"
]
}
],
"source": [
"pprint(s)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "0a34a466-141f-42ec-8d84-7ca4f28efd7e",
"metadata": {},
"outputs": [],
"source": [
"def dump_mkt_calendar(start_date, end_date, stock_pool_date):\n",
" \n",
" def _dump_df_to_csv(series):\n",
" dump_path = '{}/行情数据/calendar.csv'.format(\n",
" TINYSOFT_DATA_PATH\n",
" )\n",
" series.to_csv(dump_path, index=False)\n",
" \n",
" with tsl() as t:\n",
" date_list = t.get_mkt_trading_days(20000101,20220620)\n",
" \n",
" s = pd.Series(date_list, dtype=np.int64, name='date')\n",
" _dump_df_to_csv(s)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "74b4d3d1-fbbc-4fbd-be36-790901b9270c",
"metadata": {},
"outputs": [],
"source": [
"dump_mkt_calendar(20200520, 20220620, 20220531)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "fde93bc5-bef0-4a3a-a30a-ebe04d65a474",
"metadata": {},
"outputs": [],
"source": [
"def dump_mkt_k_daily(start_date, end_date, stock_pool_date):\n",
" \n",
" def _dump_df_to_csv(stock_id, df):\n",
" dump_path = '{}/行情数据/k-daily/{}.csv'.format(\n",
" TINYSOFT_DATA_PATH, stock_id\n",
" )\n",
" df.to_csv(dump_path)\n",
" \n",
" with tsl() as t:\n",
" stock_list = t.get_stock_list(stock_pool_date)\n",
" print('正在获取日频行情数据:')\n",
" \n",
" with tqdm(stock_list) as pbar:\n",
" for stock_id in pbar:\n",
" pbar.set_description(stock_id)\n",
" df1 = t.get_mkt_stock_k_daily(start_date, end_date, stock_id)\n",
" df2 = t.get_mkt_stock_k_daily_ext(start_date, end_date, stock_id)\n",
" df = pd.concat([df1, df2], axis=1)\n",
" \n",
" _dump_df_to_csv(stock_id, df)\n",
" time.sleep(0.001)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "bde355b6-bfc9-45b5-9b8f-3d640306647f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"正在获取日频行情数据:\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"SH600021: 2%|█▌ | 113/4822 [00:37<26:17, 2.98it/s]\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Input \u001b[1;32mIn [29]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mdump_mkt_k_daily\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m20200610\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m20220620\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m20220531\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Input \u001b[1;32mIn [28]\u001b[0m, in \u001b[0;36mdump_mkt_k_daily\u001b[1;34m(start_date, end_date, stock_pool_date)\u001b[0m\n\u001b[0;32m 15\u001b[0m pbar\u001b[38;5;241m.\u001b[39mset_description(stock_id)\n\u001b[0;32m 16\u001b[0m df1 \u001b[38;5;241m=\u001b[39m t\u001b[38;5;241m.\u001b[39mget_mkt_stock_k_daily(start_date, end_date, stock_id)\n\u001b[1;32m---> 17\u001b[0m df2 \u001b[38;5;241m=\u001b[39m \u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_mkt_stock_k_daily_ext\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstart_date\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mend_date\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstock_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 18\u001b[0m df \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mconcat([df1, df2], axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m 20\u001b[0m _dump_df_to_csv(stock_id, df)\n",
"Input \u001b[1;32mIn [16]\u001b[0m, in \u001b[0;36mtsl.get_mkt_stock_k_daily_ext\u001b[1;34m(self, start_date, end_date, stock_id)\u001b[0m\n\u001b[0;32m 159\u001b[0m \u001b[38;5;66;03m# IsSt_3: 是否是ST(指定日)判断当前股票在指定日是否是ST股。函数主要是通过名称变更进行判断。 \u001b[39;00m\n\u001b[0;32m 160\u001b[0m \u001b[38;5;66;03m# Stockzf3涨幅(%) =(收盘-昨收)/昨收*100%(已复权)\u001b[39;00m\n\u001b[0;32m 161\u001b[0m \u001b[38;5;66;03m# http://www.tinysoft.com.cn/tsdn/helpdoc/display.tsl?id=12582\u001b[39;00m\n\u001b[0;32m 162\u001b[0m \u001b[38;5;66;03m# AuthorityFactor: 以上市日为复权基准日向后复权得到指定日比例复权方式的复权因子值即复权方式为1。调用该函数需注意设置当前股票和当前时间。\u001b[39;00m\n\u001b[0;32m 163\u001b[0m code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[0;32m 164\u001b[0m \u001b[38;5;124m SetSysParam(pn_cycle(),cy_day());\u001b[39m\n\u001b[0;32m 165\u001b[0m \u001b[38;5;124m SetSysParam(pn_stock(), \u001b[39m\u001b[38;5;130;01m\\'\u001b[39;00m\u001b[38;5;132;01m{stock_id}\u001b[39;00m\u001b[38;5;130;01m\\'\u001b[39;00m\u001b[38;5;124m);\u001b[39m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 184\u001b[0m date_list_input\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin([\u001b[38;5;28mstr\u001b[39m(date) \u001b[38;5;28;01mfor\u001b[39;00m date \u001b[38;5;129;01min\u001b[39;00m date_list])\n\u001b[0;32m 185\u001b[0m )\n\u001b[1;32m--> 186\u001b[0m r \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mc\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexec\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcode\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 188\u001b[0m df \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mDataFrame(r\u001b[38;5;241m.\u001b[39mvalue(), index\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mrange\u001b[39m(\u001b[38;5;28mlen\u001b[39m(r\u001b[38;5;241m.\u001b[39mvalue())))\n\u001b[0;32m 189\u001b[0m df\u001b[38;5;241m.\u001b[39mset_index([\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mStockID\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdate\u001b[39m\u001b[38;5;124m'\u001b[39m], inplace\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"dump_mkt_k_daily(20200610, 20220620, 20220531)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "93a4db53-fe61-4b80-983d-01c6434dd33a",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('../TslDataFeed/tinysoft-data/行情数据/k-daily/NE430047.csv')"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "0dc92194-f2fb-432b-a081-ba70688ec766",
"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>StockID</th>\n",
" <th>date</th>\n",
" <th>close</th>\n",
" <th>vol</th>\n",
" <th>low</th>\n",
" <th>yclose</th>\n",
" <th>cjbs</th>\n",
" <th>amount</th>\n",
" <th>high</th>\n",
" <th>open</th>\n",
" <th>IsST</th>\n",
" <th>Factor</th>\n",
" <th>PctChg</th>\n",
" <th>FloatShares</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>NE430047</td>\n",
" <td>20150309</td>\n",
" <td>26.98</td>\n",
" <td>40000.0</td>\n",
" <td>26.5</td>\n",
" <td>26.48</td>\n",
" <td>0.0</td>\n",
" <td>1079070.0</td>\n",
" <td>27.4</td>\n",
" <td>26.5</td>\n",
" <td>0</td>\n",
" <td>1.000000</td>\n",
" <td>1.888218</td>\n",
" <td>149259448.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>NE430047</td>\n",
" <td>20150310</td>\n",
" <td>27.50</td>\n",
" <td>9000.0</td>\n",
" <td>27.5</td>\n",
" <td>26.98</td>\n",
" <td>0.0</td>\n",
" <td>247500.0</td>\n",
" <td>27.5</td>\n",
" <td>27.5</td>\n",
" <td>0</td>\n",
" <td>1.000000</td>\n",
" <td>1.927354</td>\n",
" <td>149259448.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>NE430047</td>\n",
" <td>20150311</td>\n",
" <td>27.60</td>\n",
" <td>29000.0</td>\n",
" <td>27.4</td>\n",
" <td>27.50</td>\n",
" <td>0.0</td>\n",
" <td>797310.0</td>\n",
" <td>27.6</td>\n",
" <td>27.4</td>\n",
" <td>0</td>\n",
" <td>1.000000</td>\n",
" <td>0.363636</td>\n",
" <td>149259448.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>NE430047</td>\n",
" <td>20150313</td>\n",
" <td>27.80</td>\n",
" <td>31000.0</td>\n",
" <td>27.5</td>\n",
" <td>27.60</td>\n",
" <td>0.0</td>\n",
" <td>861900.0</td>\n",
" <td>28.0</td>\n",
" <td>27.6</td>\n",
" <td>0</td>\n",
" <td>1.000000</td>\n",
" <td>0.724638</td>\n",
" <td>149259448.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>NE430047</td>\n",
" <td>20150316</td>\n",
" <td>28.00</td>\n",
" <td>110000.0</td>\n",
" <td>28.0</td>\n",
" <td>27.80</td>\n",
" <td>0.0</td>\n",
" <td>3099050.0</td>\n",
" <td>28.8</td>\n",
" <td>28.0</td>\n",
" <td>0</td>\n",
" <td>1.000000</td>\n",
" <td>0.719424</td>\n",
" <td>149259448.0</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",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5436</th>\n",
" <td>NE430047</td>\n",
" <td>20201117</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",
" <td>0</td>\n",
" <td>0.211911</td>\n",
" <td>0.000000</td>\n",
" <td>149259448.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5437</th>\n",
" <td>NE430047</td>\n",
" <td>20201118</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",
" <td>0</td>\n",
" <td>0.211911</td>\n",
" <td>0.000000</td>\n",
" <td>149259448.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5438</th>\n",
" <td>NE430047</td>\n",
" <td>20201119</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",
" <td>0</td>\n",
" <td>0.211911</td>\n",
" <td>0.000000</td>\n",
" <td>149259448.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5439</th>\n",
" <td>NE430047</td>\n",
" <td>20201120</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",
" <td>0</td>\n",
" <td>0.211911</td>\n",
" <td>0.000000</td>\n",
" <td>149259448.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5440</th>\n",
" <td>NE430047</td>\n",
" <td>20201123</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",
" <td>0</td>\n",
" <td>0.211911</td>\n",
" <td>0.000000</td>\n",
" <td>149259448.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5441 rows × 14 columns</p>\n",
"</div>"
],
"text/plain": [
" StockID date close vol low yclose cjbs amount \\\n",
"0 NE430047 20150309 26.98 40000.0 26.5 26.48 0.0 1079070.0 \n",
"1 NE430047 20150310 27.50 9000.0 27.5 26.98 0.0 247500.0 \n",
"2 NE430047 20150311 27.60 29000.0 27.4 27.50 0.0 797310.0 \n",
"3 NE430047 20150313 27.80 31000.0 27.5 27.60 0.0 861900.0 \n",
"4 NE430047 20150316 28.00 110000.0 28.0 27.80 0.0 3099050.0 \n",
"... ... ... ... ... ... ... ... ... \n",
"5436 NE430047 20201117 NaN NaN NaN NaN NaN NaN \n",
"5437 NE430047 20201118 NaN NaN NaN NaN NaN NaN \n",
"5438 NE430047 20201119 NaN NaN NaN NaN NaN NaN \n",
"5439 NE430047 20201120 NaN NaN NaN NaN NaN NaN \n",
"5440 NE430047 20201123 NaN NaN NaN NaN NaN NaN \n",
"\n",
" high open IsST Factor PctChg FloatShares \n",
"0 27.4 26.5 0 1.000000 1.888218 149259448.0 \n",
"1 27.5 27.5 0 1.000000 1.927354 149259448.0 \n",
"2 27.6 27.4 0 1.000000 0.363636 149259448.0 \n",
"3 28.0 27.6 0 1.000000 0.724638 149259448.0 \n",
"4 28.8 28.0 0 1.000000 0.719424 149259448.0 \n",
"... ... ... ... ... ... ... \n",
"5436 NaN NaN 0 0.211911 0.000000 149259448.0 \n",
"5437 NaN NaN 0 0.211911 0.000000 149259448.0 \n",
"5438 NaN NaN 0 0.211911 0.000000 149259448.0 \n",
"5439 NaN NaN 0 0.211911 0.000000 149259448.0 \n",
"5440 NaN NaN 0 0.211911 0.000000 149259448.0 \n",
"\n",
"[5441 rows x 14 columns]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "6c924316-cd8c-445b-808d-74b8e4e84131",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"df = pd.DataFrame(columns=['a'])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "f4fe5b31-b287-4338-8b8e-a46454f9b307",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['a'], dtype='object')"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.columns"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "9aea4161-4b67-4133-b778-97d8a1328096",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'a' in df.columns"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "fbc79e1d-21a9-40aa-abbc-c123a53a1513",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(df.index)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f510f510-03da-4663-a584-176fa76d1ff0",
"metadata": {},
"outputs": [],
"source": [
"code = \"\"\"\n",
"return getAbkbydate('A股',20220630T); \n",
"\"\"\"\n",
"\n",
"with tsl() as t:\n",
" r = t.c.exec(code)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "cbd25e2b-69dc-4c9b-9c34-a184d282c479",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4817"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(r.value())"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "240086e1-673d-4ebd-ad18-d397c43ac1b6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5012"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"code = \"\"\"\n",
"return getbk('A股;暂停上市;终止上市');\n",
"\"\"\"\n",
"\n",
"with tsl() as t:\n",
" r = t.c.exec(code)\n",
" \n",
"len(r.value())"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "2104d772-8224-4310-84df-48f2bd7f1d20",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2023"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datetime import datetime\n",
"datetime.now().year + 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b7eede1-b09f-4de5-b531-b28487005afa",
"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
}