基于九转序列的股票选股及四天持股交易系统
- 实时数据: 支持多个数据源(Tushare、AkShare等)
- 多种图表: K线图、分时图、技术指标图表
- 响应式设计: 适配桌面和移动设备
- 高性能: 数据库连接池、缓存机制、异步处理
- 可扩展: 模块化设计,易于扩展新功能
td_stock_web/
├── app.py # Flask主应用
├── config/ # 配置文件
│ ├── development.yaml
│ └── production.yaml
├── utils/ # 工具模块
│ ├── config_utils.py # 配置管理
│ ├── database_utils.py # 数据库工具
│ ├── response_utils.py # API响应工具
│ ├── env_config.py # 环境配置
│ └── ...
├── static/ # 静态资源
│ ├── css/
│ ├── js/
│ └── images/
├── templates/ # HTML模板
└── data/ # 数据文件
- Python 3.8+
- SQLite 3
- 现代浏览器
pip install -r requirements.txt
- 复制配置文件:
cp config/development.yaml.example config/development.yaml
-
编辑配置文件,设置API密钥等参数
-
设置环境变量:
export ENVIRONMENT=development
export TUSHARE_TOKEN=68a7f380e45182b216eb63a9666c277ee96e68e3754476976adc5019
# 开发环境
python3 app.py
# 生产环境部署
./deploy.sh production
访问 http://localhost:8080 查看应用
- Tushare API: 专业的金融数据接口
- AkShare: 开源金融数据库
- 实时数据: 支持分时数据获取
- 历史数据: K线数据、财务数据等
- K线图: 基于ECharts的专业K线图表
- 分时图: 实时价格走势图
- 技术指标: MA、RSI、MACD等
- 成交量: 成交量柱状图
- 缓存机制: 内存+文件双重缓存
- 数据验证: 多级数据验证机制
- 性能监控: API调用性能统计
- 错误处理: 完善的异常处理机制
# API路由示例
@app.route('/api/stock/<stock_code>')
@api_response_wrapper
def get_stock_data(stock_code):
"""获取股票基本信息"""
# 业务逻辑
return stock_data
# 数据库操作示例
from utils.database_utils import get_database_pool
pool = get_database_pool()
with pool.get_connection() as conn:
cursor = conn.execute("SELECT * FROM stocks WHERE code = ?", (stock_code,))
return cursor.fetchall()
// 创建图表
const chartManager = new StockChartManager();
const chart = chartManager.createKlineChart('container', data);
// 数据处理
const processor = new DataProcessor();
const ma5 = processor.calculateMA(data, 5);
from utils.env_config import ConfigManager
config = ConfigManager()
db_config = config.get_database_config()
api_config = config.get_api_config()
database:
type: sqlite
path: data/stock_data.db
pool_size: 10
timeout: 30
api:
tushare:
token: ${TUSHARE_TOKEN}
timeout: 30
akshare:
timeout: 15
cache:
memory:
max_size: 1000
ttl: 300
file:
enabled: true
path: data/cache
- 连接池管理
- 索引优化
- 查询缓存
- WAL模式
- 组件化设计
- 懒加载
- 代码分割
- 图片优化
- 多级缓存
- 智能过期
- 预加载机制
- 缓存预热
- 输入验证: 严格的参数验证
- SQL注入防护: 参数化查询
- CSRF保护: 跨站请求伪造防护
- 错误处理: 安全的错误信息返回
- ✅ Tushare Token:已预设,无需手动配置
- ✅ 域名配置:已设置为 wuxiancai.win,无需手动配置
- 🔧 防火墙设置:确保开放80端口(Nginx)和8080端口(Flask)
- 🔧 SSL证书:建议配置HTTPS,使用命令:
sudo certbot --nginx -d wuxiancai.win -d www.wuxiancai.win
# 启动开发服务器
python3 app.py
# 使用部署脚本
./deploy.sh production
# 或手动部署
gunicorn --bind 0.0.0.0:8080 --workers 4 app:app
# 构建镜像
docker build -t stock-web .
# 运行容器
docker run -p 8080:8080 -e ENVIRONMENT=production stock-web
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static/ {
alias /path/to/static/;
expires 1y;
}
}
- 性能指标收集
- 错误率统计
- API响应时间
- 系统资源使用
- 结构化日志
- 日志轮转
- 错误告警
- 审计日志
pytest tests/unit/
pytest tests/integration/
locust -f tests/performance/locustfile.py
GET /api/stock/{stock_code}
响应示例:
{
"code": 200,
"message": "success",
"data": {
"code": "000001",
"name": "平安银行",
"price": 12.34,
"change": 0.12,
"change_pct": 0.98
}
}
GET /api/kline/{stock_code}?period=1d&limit=100
GET /api/realtime/{stock_code}
- Fork 项目
- 创建特性分支 (
git checkout -b feature/AmazingFeature
) - 提交更改 (
git commit -m 'Add some AmazingFeature'
) - 推送到分支 (
git push origin feature/AmazingFeature
) - 打开 Pull Request
- 使用 Black 进行代码格式化
- 遵循 PEP 8 编码规范
- 编写单元测试
- 更新文档
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情
如有问题或建议,请:
- 创建 Issue
- 发送邮件至 support@example.com
- 查看 Wiki 文档
最后更新: 2025年7月18日
版本: v2.0.0