From b5832e40f0ad1230787ef0d3067c6ce2dc6c3b70 Mon Sep 17 00:00:00 2001 From: wjsjwr Date: Thu, 14 Nov 2024 23:53:06 +0800 Subject: [PATCH] save all eligible stock codes --- .gitignore | 1 + data/adata.ipynb | 113 +++++++++++++++++++++++++++++++++ data/save_all_eligible_code.py | 15 +++++ requirements.txt | 2 + 4 files changed, 131 insertions(+) create mode 100644 data/adata.ipynb create mode 100644 data/save_all_eligible_code.py diff --git a/.gitignore b/.gitignore index 5d381cc..7613971 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.db # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/data/adata.ipynb b/data/adata.ipynb new file mode 100644 index 0000000..59b0b02 --- /dev/null +++ b/data/adata.ipynb @@ -0,0 +1,113 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import adata\n", + "\n", + "res_df = adata.stock.info.all_code()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import sqlite3\n", + "\n", + "conn = sqlite3.connect('big-a.db')\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5661" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "res_df.to_sql('all_code', con=conn, if_exists='replace', index=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "no_exit = res_df['short_name'].map(lambda x: not x.endswith('退'))\n", + "have_date = res_df['list_date'].map(lambda x: not (not x))\n", + "no_st = res_df['short_name'].map(lambda x: 'ST' not in x)\n", + "no_pt = res_df['short_name'].map(lambda x: 'PT' not in x)\n", + "\n", + "filtered = res_df[no_exit & no_st & no_pt & res_df['list_date'].notnull()]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5255" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "filtered.to_sql('all_code', con=conn, if_exists='replace', index=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas\n", + "\n", + "local = pandas.read_sql_table('all_code', con='sqlite:///big-a.db', parse_dates=['list_date'])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "big-a", + "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.13.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/data/save_all_eligible_code.py b/data/save_all_eligible_code.py new file mode 100644 index 0000000..e39d9fa --- /dev/null +++ b/data/save_all_eligible_code.py @@ -0,0 +1,15 @@ +import adata +import sqlite3 + +conn = sqlite3.connect('big-a.db') +res_df = adata.stock.info.all_code() + +no_exit = res_df['short_name'].map(lambda x: not x.endswith('退')) +have_date = res_df['list_date'].map(lambda x: not (not x)) +no_st = res_df['short_name'].map(lambda x: 'ST' not in x) +no_pt = res_df['short_name'].map(lambda x: 'PT' not in x) + +filtered = res_df[no_exit & no_st & no_pt & res_df['list_date'].notnull()] +filtered.to_sql('all_code', con=conn, if_exists='replace', index=False) + +print('Done') diff --git a/requirements.txt b/requirements.txt index a872be6..78f0ad1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,5 @@ ruff granian fastapi[standard] orjson>=3.10,<4 +adata +sqlalchemy