1
0

初始化项目

This commit is contained in:
2024-12-30 22:37:50 +08:00
commit 306ffe620d
21 changed files with 34212 additions and 0 deletions

260
.gitignore vendored Normal file
View File

@@ -0,0 +1,260 @@
### VisualStudioCode template
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
.idea/artifacts
.idea/compiler.xml
.idea/jarRepositories.xml
.idea/modules.xml
.idea/*.iml
.idea/modules
*.iml
*.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# Custom
temp/

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,123 @@
{
"cells": [
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-12-27T08:11:08.133652Z",
"start_time": "2024-12-27T08:11:08.099503Z"
}
},
"source": [
"import pymysql\n",
"\n",
"# db = pymysql.connect(host=\"frp-air.top\", port=43458, user=\"stock\", password=\"c&J#USr9~J5hrUiP\", database=\"stock\")\n",
"db = pymysql.connect(host=\"localhost\", port=3307, user=\"test\", password=\"test\", database=\"main\")"
],
"outputs": [],
"execution_count": 1
},
{
"cell_type": "code",
"id": "7fdf7baf7e494f5d",
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-27T08:11:43.530387Z",
"start_time": "2024-12-27T08:11:43.527288Z"
}
},
"source": "cursor = db.cursor()",
"outputs": [],
"execution_count": 3
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-27T08:29:21.672366Z",
"start_time": "2024-12-27T08:29:13.248490Z"
}
},
"cell_type": "code",
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv('../document/Tushare股票信息.csv',\n",
" names=['ts_code', 'symbol', 'name', 'area', 'industry', 'fullname', 'enname', 'cnspell', 'market',\n",
" 'exchange', 'curr_type', 'list_status', 'list_date', 'act_name', 'act_ent_type'])\n",
"# ts_code列按英文句号分割将分割后的两个字符串交换位置\n",
"df['ts_code'] = df['ts_code'].str.split('.').str[::-1].str.join('.')\n",
"# 只留下ts_code、name、marketfullname列\n",
"df = df[['ts_code', 'name', 'market', 'fullname']]\n",
"# 遍历所有行生成插入sql语句\n",
"for i in range(len(df)):\n",
" sql = \"insert into target(code, name, description, market, type) values ('%s','%s','%s','%s','%s') on duplicate key update name = values(name), description = values(description), market = values(market), type = values(type)\" % (\n",
" df.iloc[i]['ts_code'], df.iloc[i]['name'], df.iloc[i]['fullname'], df.iloc[i]['market'], 'stock'\n",
" )\n",
" cursor.execute(sql)\n",
"db.commit()"
],
"id": "bff56fafd1219dfa",
"outputs": [],
"execution_count": 24
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-27T08:29:45.830427Z",
"start_time": "2024-12-27T08:29:44.310167Z"
}
},
"cell_type": "code",
"source": [
"df = pd.read_csv('../document/ETF信息.csv', usecols=[0, 1, 2], names=['index', 'code', 'name'], index_col='index')\n",
"for i in range(len(df)):\n",
" sql = \"insert into target(code, name, type) values ('%s','%s','%s') on duplicate key update name = values(name), type = values(type)\" % (\n",
" df.iloc[i]['code'], df.iloc[i]['name'], 'etf'\n",
" )\n",
" cursor.execute(sql)\n",
"db.commit()"
],
"id": "577d97871a0acbe7",
"outputs": [],
"execution_count": 25
},
{
"cell_type": "code",
"id": "9cc56d42dac5ea9",
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-27T08:49:40.805003Z",
"start_time": "2024-12-27T08:49:40.762113Z"
}
},
"source": [
"db.close()"
],
"outputs": [],
"execution_count": 26
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,309 @@
{
"cells": [
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-12-27T09:11:40.994988Z",
"start_time": "2024-12-27T09:11:40.446727Z"
}
},
"source": [
"import akshare as ak\n",
"\n",
"root = \"/Users/lanyuanxiaoyao/Project/PyCharmProjects/stock_leanrning/temp\"\n",
"# root = \"/Users/lanyuanxiaoyao/SynologyDrive/data\"\n",
"stock_path = root + \"/stock\"\n",
"etf_path = root + \"/etf\"\n",
"\n",
"df = ak.stock_zh_a_hist(symbol=\"000001\", period='daily', adjust=\"hfq\")\n",
"# 跳过第一行\n",
"df.to_csv(stock_path + \"/SZ000001.csv\")"
],
"outputs": [],
"execution_count": 9
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-27T09:12:58.597549Z",
"start_time": "2024-12-27T09:12:58.554934Z"
}
},
"cell_type": "code",
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv('../temp/stock/SZ000001.csv', index_col=0)\n",
"df"
],
"id": "6f34bd06fd451f6e",
"outputs": [
{
"data": {
"text/plain": [
" 日期 股票代码 开盘 收盘 最高 最低 成交量 \\\n",
"0 1991-04-03 1 49.00 49.00 49.00 49.00 1 \n",
"1 1991-04-04 1 48.76 48.76 48.76 48.76 3 \n",
"2 1991-04-05 1 48.52 48.52 48.52 48.52 2 \n",
"3 1991-04-06 1 48.28 48.28 48.28 48.28 7 \n",
"4 1991-04-08 1 48.04 48.04 48.04 48.04 2 \n",
"... ... ... ... ... ... ... ... \n",
"8059 2024-12-23 1 2300.88 2315.51 2333.39 2300.88 1659405 \n",
"8060 2024-12-24 1 2313.89 2336.64 2338.27 2313.89 1350837 \n",
"8061 2024-12-25 1 2336.64 2346.39 2362.64 2333.39 1475283 \n",
"8062 2024-12-26 1 2346.39 2336.64 2348.02 2323.64 1000075 \n",
"8063 2024-12-27 1 2338.27 2331.76 2343.14 2304.13 1290012 \n",
"\n",
" 成交额 振幅 涨跌幅 涨跌额 换手率 \n",
"0 5.000000e+03 0.00 22.50 9.00 0.00 \n",
"1 1.500000e+04 0.00 -0.49 -0.24 0.00 \n",
"2 1.000000e+04 0.00 -0.49 -0.24 0.00 \n",
"3 3.400000e+04 0.00 -0.49 -0.24 0.00 \n",
"4 1.000000e+04 0.00 -0.50 -0.24 0.00 \n",
"... ... ... ... ... ... \n",
"8059 1.953519e+09 1.41 0.78 17.88 0.86 \n",
"8060 1.595699e+09 1.05 0.91 21.13 0.70 \n",
"8061 1.759957e+09 1.25 0.42 9.75 0.76 \n",
"8062 1.183746e+09 1.04 -0.42 -9.75 0.52 \n",
"8063 1.518383e+09 1.67 -0.21 -4.88 0.66 \n",
"\n",
"[8064 rows x 12 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>日期</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>0</th>\n",
" <td>1991-04-03</td>\n",
" <td>1</td>\n",
" <td>49.00</td>\n",
" <td>49.00</td>\n",
" <td>49.00</td>\n",
" <td>49.00</td>\n",
" <td>1</td>\n",
" <td>5.000000e+03</td>\n",
" <td>0.00</td>\n",
" <td>22.50</td>\n",
" <td>9.00</td>\n",
" <td>0.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1991-04-04</td>\n",
" <td>1</td>\n",
" <td>48.76</td>\n",
" <td>48.76</td>\n",
" <td>48.76</td>\n",
" <td>48.76</td>\n",
" <td>3</td>\n",
" <td>1.500000e+04</td>\n",
" <td>0.00</td>\n",
" <td>-0.49</td>\n",
" <td>-0.24</td>\n",
" <td>0.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1991-04-05</td>\n",
" <td>1</td>\n",
" <td>48.52</td>\n",
" <td>48.52</td>\n",
" <td>48.52</td>\n",
" <td>48.52</td>\n",
" <td>2</td>\n",
" <td>1.000000e+04</td>\n",
" <td>0.00</td>\n",
" <td>-0.49</td>\n",
" <td>-0.24</td>\n",
" <td>0.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1991-04-06</td>\n",
" <td>1</td>\n",
" <td>48.28</td>\n",
" <td>48.28</td>\n",
" <td>48.28</td>\n",
" <td>48.28</td>\n",
" <td>7</td>\n",
" <td>3.400000e+04</td>\n",
" <td>0.00</td>\n",
" <td>-0.49</td>\n",
" <td>-0.24</td>\n",
" <td>0.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1991-04-08</td>\n",
" <td>1</td>\n",
" <td>48.04</td>\n",
" <td>48.04</td>\n",
" <td>48.04</td>\n",
" <td>48.04</td>\n",
" <td>2</td>\n",
" <td>1.000000e+04</td>\n",
" <td>0.00</td>\n",
" <td>-0.50</td>\n",
" <td>-0.24</td>\n",
" <td>0.00</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",
" </tr>\n",
" <tr>\n",
" <th>8059</th>\n",
" <td>2024-12-23</td>\n",
" <td>1</td>\n",
" <td>2300.88</td>\n",
" <td>2315.51</td>\n",
" <td>2333.39</td>\n",
" <td>2300.88</td>\n",
" <td>1659405</td>\n",
" <td>1.953519e+09</td>\n",
" <td>1.41</td>\n",
" <td>0.78</td>\n",
" <td>17.88</td>\n",
" <td>0.86</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8060</th>\n",
" <td>2024-12-24</td>\n",
" <td>1</td>\n",
" <td>2313.89</td>\n",
" <td>2336.64</td>\n",
" <td>2338.27</td>\n",
" <td>2313.89</td>\n",
" <td>1350837</td>\n",
" <td>1.595699e+09</td>\n",
" <td>1.05</td>\n",
" <td>0.91</td>\n",
" <td>21.13</td>\n",
" <td>0.70</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8061</th>\n",
" <td>2024-12-25</td>\n",
" <td>1</td>\n",
" <td>2336.64</td>\n",
" <td>2346.39</td>\n",
" <td>2362.64</td>\n",
" <td>2333.39</td>\n",
" <td>1475283</td>\n",
" <td>1.759957e+09</td>\n",
" <td>1.25</td>\n",
" <td>0.42</td>\n",
" <td>9.75</td>\n",
" <td>0.76</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8062</th>\n",
" <td>2024-12-26</td>\n",
" <td>1</td>\n",
" <td>2346.39</td>\n",
" <td>2336.64</td>\n",
" <td>2348.02</td>\n",
" <td>2323.64</td>\n",
" <td>1000075</td>\n",
" <td>1.183746e+09</td>\n",
" <td>1.04</td>\n",
" <td>-0.42</td>\n",
" <td>-9.75</td>\n",
" <td>0.52</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8063</th>\n",
" <td>2024-12-27</td>\n",
" <td>1</td>\n",
" <td>2338.27</td>\n",
" <td>2331.76</td>\n",
" <td>2343.14</td>\n",
" <td>2304.13</td>\n",
" <td>1290012</td>\n",
" <td>1.518383e+09</td>\n",
" <td>1.67</td>\n",
" <td>-0.21</td>\n",
" <td>-4.88</td>\n",
" <td>0.66</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>8064 rows × 12 columns</p>\n",
"</div>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 11
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

14
0_材料准备/table.sql Normal file
View File

@@ -0,0 +1,14 @@
create table if not exists target
(
code varchar(50) not null primary key comment '编码',
name varchar(100) not null comment '名称',
description varchar(500) comment '描述',
market varchar(10) not null default '' comment '市场',
type varchar(10) not null comment '类型: stock, fund, etf'
);
create table if not exists daily
(
code varchar(50) not null comment '编码',
date date not null comment '日期'
);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

6
README.md Normal file
View File

@@ -0,0 +1,6 @@
### 量化学习笔记
docker mysql
```shell
docker run --rm -p 3307:3306 -e "MYSQL_RANDOM_ROOT_PASSWORD=yes" -e "MYSQL_DATABASE=main" -e "MYSQL_USER=test" -e "MYSQL_PASSWORD=test" -d mysql:8
```

1012
document/ETF信息.csv Normal file

File diff suppressed because it is too large Load Diff

8064
document/SZ000001.csv Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

3375
poetry.lock generated Normal file

File diff suppressed because it is too large Load Diff

22
pyproject.toml Normal file
View File

@@ -0,0 +1,22 @@
[tool.poetry]
name = "stock-learning"
version = "0.1.0"
description = ""
authors = ["lanyuanxiaoyao <lanyuanxiaoyao@gmail.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
tushare = "^1.4.14"
jupyter = "^1.1.1"
akshare = "^1.15.60"
pymysql = "^1.1.1"
pandas = "^2.2.3"
matplotlib = "^3.10.0"
prophet = "^1.1.6"
mplfinance = "^0.12.10b0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"