This commit is contained in:
aaron 2025-06-03 16:55:34 +08:00
parent 38a9841725
commit 0f280508ae
5 changed files with 49 additions and 7 deletions

View File

@ -22,10 +22,25 @@ class AllTickAPI:
""" """
self.api_key = api_key self.api_key = api_key
self.base_url = "https://quote.alltick.io/quote-b-api" 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() 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, def get_common_klines(self, symbol: str, interval: str,
limit: int = 500) -> pd.DataFrame: 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线数据 获取历史K线数据
@ -63,7 +78,7 @@ class AllTickAPI:
encoded_query = urllib.parse.quote(json.dumps(query_data)) encoded_query = urllib.parse.quote(json.dumps(query_data))
# 构建完整的URL # 构建完整的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: try:
# 发送请求 # 发送请求

View File

@ -23,7 +23,7 @@ qwen:
# AllTick API设置用于获取黄金数据 # AllTick API设置用于获取黄金数据
alltick: alltick:
api_key: "ee66d8e2868fd988fffacec40d078df8-c-app" api_key: "6c7ba077eee07f6f270e219d4848700e-c-app"
symbols: symbols:
- "XAUUSD" # 黄金/美元 - "XAUUSD" # 黄金/美元

View File

@ -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"))

View File

@ -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.crypto import router as crypto_router
from cryptoai.routes.platform import router as platform_router from cryptoai.routes.platform import router as platform_router
from cryptoai.routes.analysis import router as analysis_router from cryptoai.routes.analysis import router as analysis_router
from cryptoai.routes.alltick import router as alltick_router
# 配置日志 # 配置日志
logging.basicConfig( logging.basicConfig(
level=logging.INFO, 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(adata_router, prefix="/adata", tags=["A股数据"])
app.include_router(crypto_router, prefix="/crypto", tags=["加密货币数据"]) app.include_router(crypto_router, prefix="/crypto", tags=["加密货币数据"])
app.include_router(analysis_router, prefix="/analysis", tags=["分析历史"]) app.include_router(analysis_router, prefix="/analysis", tags=["分析历史"])
app.include_router(alltick_router, prefix="/alltick", tags=["AllTick数据"])
# 请求计时中间件 # 请求计时中间件
@app.middleware("http") @app.middleware("http")
async def add_process_time_header(request: Request, call_next): async def add_process_time_header(request: Request, call_next):

View File

@ -29,7 +29,7 @@ services:
cryptoai-api: cryptoai-api:
build: . build: .
container_name: cryptoai-api container_name: cryptoai-api
image: cryptoai-api:0.1.36 image: cryptoai-api:0.1.37
restart: always restart: always
ports: ports:
- "8000:8000" - "8000:8000"