diff --git a/app/services/__pycache__/stock_service.cpython-311.pyc b/app/services/__pycache__/stock_service.cpython-311.pyc index 2ce52d3..63524a8 100644 Binary files a/app/services/__pycache__/stock_service.cpython-311.pyc and b/app/services/__pycache__/stock_service.cpython-311.pyc differ diff --git a/app/services/stock_service.py b/app/services/stock_service.py index 3db2885..2e162ed 100644 --- a/app/services/stock_service.py +++ b/app/services/stock_service.py @@ -212,15 +212,22 @@ class StockService: loop.close() # 转换为Symbol模型 - from app.models import Symbol, SymbolType + from app.models import Symbol, SymbolType, Exchange symbols = [] for s in symbols_info: + # 将字符串 exchange 转换为 Exchange 枚举 + try: + exchange_enum = Exchange(s.exchange) + except ValueError: + exchange_enum = Exchange.SH # 默认上海 + symbols.append(Symbol( symbol_id=s.symbol_id, symbol_type=SymbolType.STOCK, - exchange=s.exchange, + exchange=exchange_enum, name=s.name, - underlying=s.underlying + underlying=s.underlying, + status="active" # 添加必需的 status 字段 )) info(f"Fetched {len(symbols)} symbols from adapter") @@ -271,4 +278,85 @@ class StockService: def get_trading_dates(self, req: TradingDatesRequest) -> TradingDatesData: """获取交易日历""" - return self.repository.get_trading_dates(req.start, req.end) + # 从数据库获取 + data = self.repository.get_trading_dates(req.start, req.end) + + # 如果数据库没有数据,从适配器获取 + if not data.trading_dates: + info(f"No trading dates in DB for {req.start}~{req.end}, fetching from adapter...") + adapter_dates = self._fetch_trading_dates_from_adapter(req.start, req.end) + if adapter_dates: + # 保存到数据库 + self._save_trading_dates_to_db(adapter_dates) + # 重新查询 + data = self.repository.get_trading_dates(req.start, req.end) + + return data + + def _fetch_trading_dates_from_adapter(self, start: str, end: str) -> List[str]: + """从适配器获取交易日历""" + try: + adapter_service = AdapterService() + + # 确保适配器已连接 + adapter = adapter_service.get_active_adapter("stock") + if not adapter: + asyncio.run(adapter_service._connect_adapter("amazingdata")) + adapter = adapter_service.get_active_adapter("stock") + + if not adapter: + error("No active adapter available") + return [] + + # 异步获取数据 + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + calendar_data = loop.run_until_complete( + adapter.fetch_trading_calendar("SH", start, end) + ) + loop.close() + + # 提取交易日日期 + dates = [] + for cal in calendar_data: + if cal.is_trading_day: + # 转换为 YYYYMMDD 格式 + date_str = cal.date.strftime("%Y%m%d") + dates.append(date_str) + + info(f"Fetched {len(dates)} trading dates from adapter") + return dates + + except Exception as e: + error(f"Failed to fetch trading dates from adapter: {e}") + return [] + + def _save_trading_dates_to_db(self, dates: List[str]) -> None: + """保存交易日历到数据库""" + try: + from app.repositories.models import StockTradingCalendar + from datetime import datetime as dt + + for date_str in dates: + # 检查是否已存在 + existing = self.db.query(StockTradingCalendar).filter( + StockTradingCalendar.trade_date == date_str + ).first() + + if not existing: + # 解析日期获取星期几 + date_obj = dt.strptime(date_str, "%Y%m%d") + week_day = date_obj.weekday() + 1 # 1=周一, 7=周日 + + new_record = StockTradingCalendar( + trade_date=date_str, + is_trading_day=True, + week_day=week_day + ) + self.db.add(new_record) + + self.db.commit() + info(f"Saved {len(dates)} trading dates to DB") + except Exception as e: + error(f"Failed to save trading dates to DB: {e}") + self.db.rollback() diff --git a/todo_20260311.md b/todo_20260311.md new file mode 100644 index 0000000..bd0a87a --- /dev/null +++ b/todo_20260311.md @@ -0,0 +1,10 @@ +# 待完成TODO + +1. 总股本、机构持股未能获取 + +# 已完成DONE + +1. 获取股票列表接口 stock/symbols -- 第一次请求慢,会超时 +2. 获取k线 stock/klines/{symbol} -- 缺少股本等信息 +3. 获取交易日历 stock/trading-dates + diff --git a/venv/Lib/site-packages/h11/__pycache__/__init__.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/__init__.cpython-311.pyc index e597744..ce9a6e4 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/__init__.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/__init__.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_abnf.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_abnf.cpython-311.pyc index 57c140b..c4de30f 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_abnf.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_abnf.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_connection.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_connection.cpython-311.pyc index 43ebd12..e3294bd 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_connection.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_connection.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_events.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_events.cpython-311.pyc index c341d6e..c8c03d9 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_events.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_events.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_headers.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_headers.cpython-311.pyc index dc45200..4dc3c3b 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_headers.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_headers.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_readers.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_readers.cpython-311.pyc index 329eee8..f34a445 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_readers.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_readers.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_receivebuffer.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_receivebuffer.cpython-311.pyc index 6776988..8992d9b 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_receivebuffer.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_receivebuffer.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_state.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_state.cpython-311.pyc index 9be8328..9efceac 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_state.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_state.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_util.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_util.cpython-311.pyc index 0f0bb8d..34162b4 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_util.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_util.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_version.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_version.cpython-311.pyc index 5461486..bc59ceb 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_version.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_version.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/h11/__pycache__/_writers.cpython-311.pyc b/venv/Lib/site-packages/h11/__pycache__/_writers.cpython-311.pyc index bff9d7a..f1bb7ff 100644 Binary files a/venv/Lib/site-packages/h11/__pycache__/_writers.cpython-311.pyc and b/venv/Lib/site-packages/h11/__pycache__/_writers.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/__pycache__/__init__.cpython-311.pyc b/venv/Lib/site-packages/httpcore/__pycache__/__init__.cpython-311.pyc index d6551c9..06992e1 100644 Binary files a/venv/Lib/site-packages/httpcore/__pycache__/__init__.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/__pycache__/__init__.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/__pycache__/_api.cpython-311.pyc b/venv/Lib/site-packages/httpcore/__pycache__/_api.cpython-311.pyc index 46361bc..6f02d83 100644 Binary files a/venv/Lib/site-packages/httpcore/__pycache__/_api.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/__pycache__/_api.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/__pycache__/_exceptions.cpython-311.pyc b/venv/Lib/site-packages/httpcore/__pycache__/_exceptions.cpython-311.pyc index 9699288..2a0462f 100644 Binary files a/venv/Lib/site-packages/httpcore/__pycache__/_exceptions.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/__pycache__/_exceptions.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/__pycache__/_models.cpython-311.pyc b/venv/Lib/site-packages/httpcore/__pycache__/_models.cpython-311.pyc index 12e855e..6452c07 100644 Binary files a/venv/Lib/site-packages/httpcore/__pycache__/_models.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/__pycache__/_models.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/__pycache__/_ssl.cpython-311.pyc b/venv/Lib/site-packages/httpcore/__pycache__/_ssl.cpython-311.pyc index 9de40ae..8973487 100644 Binary files a/venv/Lib/site-packages/httpcore/__pycache__/_ssl.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/__pycache__/_ssl.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/__pycache__/_synchronization.cpython-311.pyc b/venv/Lib/site-packages/httpcore/__pycache__/_synchronization.cpython-311.pyc index 6346997..c32bafa 100644 Binary files a/venv/Lib/site-packages/httpcore/__pycache__/_synchronization.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/__pycache__/_synchronization.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/__pycache__/_trace.cpython-311.pyc b/venv/Lib/site-packages/httpcore/__pycache__/_trace.cpython-311.pyc index 3a59abc..d30145e 100644 Binary files a/venv/Lib/site-packages/httpcore/__pycache__/_trace.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/__pycache__/_trace.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/__pycache__/_utils.cpython-311.pyc b/venv/Lib/site-packages/httpcore/__pycache__/_utils.cpython-311.pyc index 6a5353f..2f4b17d 100644 Binary files a/venv/Lib/site-packages/httpcore/__pycache__/_utils.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/__pycache__/_utils.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_async/__pycache__/__init__.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_async/__pycache__/__init__.cpython-311.pyc index 09a01ad..dc768fa 100644 Binary files a/venv/Lib/site-packages/httpcore/_async/__pycache__/__init__.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_async/__pycache__/__init__.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_async/__pycache__/connection.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_async/__pycache__/connection.cpython-311.pyc index a2cf31f..fe64a71 100644 Binary files a/venv/Lib/site-packages/httpcore/_async/__pycache__/connection.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_async/__pycache__/connection.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_async/__pycache__/connection_pool.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_async/__pycache__/connection_pool.cpython-311.pyc index 2f257aa..c2cae81 100644 Binary files a/venv/Lib/site-packages/httpcore/_async/__pycache__/connection_pool.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_async/__pycache__/connection_pool.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_async/__pycache__/http11.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_async/__pycache__/http11.cpython-311.pyc index b9e9935..60231b1 100644 Binary files a/venv/Lib/site-packages/httpcore/_async/__pycache__/http11.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_async/__pycache__/http11.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_async/__pycache__/http2.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_async/__pycache__/http2.cpython-311.pyc index bcf8ace..cb99610 100644 Binary files a/venv/Lib/site-packages/httpcore/_async/__pycache__/http2.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_async/__pycache__/http2.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_async/__pycache__/http_proxy.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_async/__pycache__/http_proxy.cpython-311.pyc index dac8689..38dbe24 100644 Binary files a/venv/Lib/site-packages/httpcore/_async/__pycache__/http_proxy.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_async/__pycache__/http_proxy.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_async/__pycache__/interfaces.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_async/__pycache__/interfaces.cpython-311.pyc index 536cd2b..72e7ce6 100644 Binary files a/venv/Lib/site-packages/httpcore/_async/__pycache__/interfaces.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_async/__pycache__/interfaces.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_async/__pycache__/socks_proxy.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_async/__pycache__/socks_proxy.cpython-311.pyc index f014396..ba56a2c 100644 Binary files a/venv/Lib/site-packages/httpcore/_async/__pycache__/socks_proxy.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_async/__pycache__/socks_proxy.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_backends/__pycache__/__init__.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_backends/__pycache__/__init__.cpython-311.pyc index 3012ca2..40d3468 100644 Binary files a/venv/Lib/site-packages/httpcore/_backends/__pycache__/__init__.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_backends/__pycache__/__init__.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_backends/__pycache__/anyio.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_backends/__pycache__/anyio.cpython-311.pyc index 072e022..5e25576 100644 Binary files a/venv/Lib/site-packages/httpcore/_backends/__pycache__/anyio.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_backends/__pycache__/anyio.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_backends/__pycache__/auto.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_backends/__pycache__/auto.cpython-311.pyc index 8318942..e84745f 100644 Binary files a/venv/Lib/site-packages/httpcore/_backends/__pycache__/auto.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_backends/__pycache__/auto.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_backends/__pycache__/base.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_backends/__pycache__/base.cpython-311.pyc index 344eb78..8879a1e 100644 Binary files a/venv/Lib/site-packages/httpcore/_backends/__pycache__/base.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_backends/__pycache__/base.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_backends/__pycache__/mock.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_backends/__pycache__/mock.cpython-311.pyc index e67772d..2ffa595 100644 Binary files a/venv/Lib/site-packages/httpcore/_backends/__pycache__/mock.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_backends/__pycache__/mock.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_backends/__pycache__/sync.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_backends/__pycache__/sync.cpython-311.pyc index 53f45d6..7ef2f64 100644 Binary files a/venv/Lib/site-packages/httpcore/_backends/__pycache__/sync.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_backends/__pycache__/sync.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_backends/__pycache__/trio.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_backends/__pycache__/trio.cpython-311.pyc index e9b7ffb..ca550ea 100644 Binary files a/venv/Lib/site-packages/httpcore/_backends/__pycache__/trio.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_backends/__pycache__/trio.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_sync/__pycache__/__init__.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_sync/__pycache__/__init__.cpython-311.pyc index ed67906..dbaca7f 100644 Binary files a/venv/Lib/site-packages/httpcore/_sync/__pycache__/__init__.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_sync/__pycache__/__init__.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_sync/__pycache__/connection.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_sync/__pycache__/connection.cpython-311.pyc index 370fb92..40876fc 100644 Binary files a/venv/Lib/site-packages/httpcore/_sync/__pycache__/connection.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_sync/__pycache__/connection.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_sync/__pycache__/connection_pool.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_sync/__pycache__/connection_pool.cpython-311.pyc index 6d76765..2f5439d 100644 Binary files a/venv/Lib/site-packages/httpcore/_sync/__pycache__/connection_pool.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_sync/__pycache__/connection_pool.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_sync/__pycache__/http11.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_sync/__pycache__/http11.cpython-311.pyc index 3e845d3..7ea647a 100644 Binary files a/venv/Lib/site-packages/httpcore/_sync/__pycache__/http11.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_sync/__pycache__/http11.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_sync/__pycache__/http2.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_sync/__pycache__/http2.cpython-311.pyc index 8241a01..a7f4d6e 100644 Binary files a/venv/Lib/site-packages/httpcore/_sync/__pycache__/http2.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_sync/__pycache__/http2.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_sync/__pycache__/http_proxy.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_sync/__pycache__/http_proxy.cpython-311.pyc index bd2334c..2b61ba8 100644 Binary files a/venv/Lib/site-packages/httpcore/_sync/__pycache__/http_proxy.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_sync/__pycache__/http_proxy.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_sync/__pycache__/interfaces.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_sync/__pycache__/interfaces.cpython-311.pyc index 8310ee0..4e2653e 100644 Binary files a/venv/Lib/site-packages/httpcore/_sync/__pycache__/interfaces.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_sync/__pycache__/interfaces.cpython-311.pyc differ diff --git a/venv/Lib/site-packages/httpcore/_sync/__pycache__/socks_proxy.cpython-311.pyc b/venv/Lib/site-packages/httpcore/_sync/__pycache__/socks_proxy.cpython-311.pyc index 8b629f8..3439399 100644 Binary files a/venv/Lib/site-packages/httpcore/_sync/__pycache__/socks_proxy.cpython-311.pyc and b/venv/Lib/site-packages/httpcore/_sync/__pycache__/socks_proxy.cpython-311.pyc differ