From 0f280508aee6ef2d821de2ca3b1c65e7591f126c Mon Sep 17 00:00:00 2001 From: aaron <> Date: Tue, 3 Jun 2025 16:55:34 +0800 Subject: [PATCH] update --- cryptoai/api/alltick_api.py | 21 ++++++++++++++++++--- cryptoai/config/config.yaml | 2 +- cryptoai/routes/alltick.py | 27 +++++++++++++++++++++++++++ cryptoai/routes/fastapi_app.py | 4 ++-- docker-compose.yml | 2 +- 5 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 cryptoai/routes/alltick.py diff --git a/cryptoai/api/alltick_api.py b/cryptoai/api/alltick_api.py index 2c0da57..301bc10 100644 --- a/cryptoai/api/alltick_api.py +++ b/cryptoai/api/alltick_api.py @@ -22,10 +22,25 @@ class AllTickAPI: """ self.api_key = api_key self.base_url = "https://quote.alltick.io/quote-b-api" + self.stock_url = "https://quote.alltick.io/quote-stock-b-api" self.session = requests.Session() + + def get_stock_klines(self, symbol: str, interval: str, + limit: int = 500) -> pd.DataFrame: + """ + 获取股票历史K线数据 + """ + return self._get_historical_klines(symbol, interval, limit, self.stock_url) - def get_historical_klines(self, symbol: str, interval: str, start_str: Optional[str] = None, - limit: int = 500) -> pd.DataFrame: + def get_common_klines(self, symbol: str, interval: str, + limit: int = 500) -> pd.DataFrame: + """ + 获取历史K线数据 + """ + return self._get_historical_klines(symbol, interval, limit, self.base_url) + + def _get_historical_klines(self, symbol: str, interval: str, + limit: int = 500, base_url: str = None) -> pd.DataFrame: """ 获取历史K线数据 @@ -63,7 +78,7 @@ class AllTickAPI: encoded_query = urllib.parse.quote(json.dumps(query_data)) # 构建完整的URL - url = f"{self.base_url}/kline?token={self.api_key}&query={encoded_query}" + url = f"{base_url}/kline?token={self.api_key}&query={encoded_query}" try: # 发送请求 diff --git a/cryptoai/config/config.yaml b/cryptoai/config/config.yaml index 4fdf4c7..8d3b7d5 100644 --- a/cryptoai/config/config.yaml +++ b/cryptoai/config/config.yaml @@ -23,7 +23,7 @@ qwen: # AllTick API设置(用于获取黄金数据) alltick: - api_key: "ee66d8e2868fd988fffacec40d078df8-c-app" + api_key: "6c7ba077eee07f6f270e219d4848700e-c-app" symbols: - "XAUUSD" # 黄金/美元 diff --git a/cryptoai/routes/alltick.py b/cryptoai/routes/alltick.py new file mode 100644 index 0000000..73cbff5 --- /dev/null +++ b/cryptoai/routes/alltick.py @@ -0,0 +1,27 @@ +from fastapi import APIRouter, Depends, HTTPException, status, Body, Query, Path +import logging +from cryptoai.api.alltick_api import AllTickAPI +import json + +# 创建路由 +router = APIRouter() + +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) + +api_key = "6c7ba077eee07f6f270e219d4848700e-c-app" + + +@router.get("/stock/kline") +async def get_stock_kline(symbol: str, interval: str, limit: int = 200): + api = AllTickAPI(api_key=api_key) + result = api.get_stock_klines(symbol, interval, limit) + + return json.loads(result.to_json(orient="records")) + + +@router.get("/common/kline") +async def get_common_kline(symbol: str, interval: str, limit: int = 200): + api = AllTickAPI(api_key=api_key) + result = api.get_common_klines(symbol, interval, limit) + return json.loads(result.to_json(orient="records")) \ No newline at end of file diff --git a/cryptoai/routes/fastapi_app.py b/cryptoai/routes/fastapi_app.py index fa47404..1538ccf 100644 --- a/cryptoai/routes/fastapi_app.py +++ b/cryptoai/routes/fastapi_app.py @@ -20,7 +20,7 @@ from cryptoai.routes.adata import router as adata_router from cryptoai.routes.crypto import router as crypto_router from cryptoai.routes.platform import router as platform_router from cryptoai.routes.analysis import router as analysis_router - +from cryptoai.routes.alltick import router as alltick_router # 配置日志 logging.basicConfig( level=logging.INFO, @@ -54,7 +54,7 @@ app.include_router(user_router, prefix="/user", tags=["用户管理"]) app.include_router(adata_router, prefix="/adata", tags=["A股数据"]) app.include_router(crypto_router, prefix="/crypto", tags=["加密货币数据"]) app.include_router(analysis_router, prefix="/analysis", tags=["分析历史"]) - +app.include_router(alltick_router, prefix="/alltick", tags=["AllTick数据"]) # 请求计时中间件 @app.middleware("http") async def add_process_time_header(request: Request, call_next): diff --git a/docker-compose.yml b/docker-compose.yml index 40786b0..0da6f61 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,7 +29,7 @@ services: cryptoai-api: build: . container_name: cryptoai-api - image: cryptoai-api:0.1.36 + image: cryptoai-api:0.1.37 restart: always ports: - "8000:8000"