feat: add akshare stock data provider and remove debug print statement
This commit is contained in:
parent
4fb458c156
commit
65d21fa621
|
|
@ -0,0 +1,39 @@
|
||||||
|
import akshare as ak
|
||||||
|
import pandas as pd
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
def get_stock_data_akshare(symbol: str, start_date: str, end_date: str) -> str:
|
||||||
|
"""获取 A 股历史行情数据"""
|
||||||
|
# 处理 symbol:akshare 通常需要纯数字,如 600519
|
||||||
|
# 假设传入的是 600519.SS 或 000001.SZ
|
||||||
|
clean_symbol = symbol.split('.')[0]
|
||||||
|
|
||||||
|
start_d = start_date.replace("-", "")
|
||||||
|
end_d = end_date.replace("-", "")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 使用 akshare 获取日频数据
|
||||||
|
df = ak.stock_zh_a_hist(symbol=clean_symbol, period="daily", start_date=start_d, end_date=end_d, adjust="qfq")
|
||||||
|
|
||||||
|
if df.empty:
|
||||||
|
return f"No data found for {symbol}"
|
||||||
|
|
||||||
|
# 统一列名以匹配 yfinance 格式,方便后续 Agent 处理
|
||||||
|
df = df.rename(columns={
|
||||||
|
"日期": "Date",
|
||||||
|
"开盘": "Open",
|
||||||
|
"最高": "High",
|
||||||
|
"最低": "Low",
|
||||||
|
"收盘": "Close",
|
||||||
|
"成交量": "Volume"
|
||||||
|
})
|
||||||
|
df.set_index("Date", inplace=True)
|
||||||
|
|
||||||
|
header = f"# Stock data for {symbol} (via akshare) from {start_date} to {end_date}\n\n"
|
||||||
|
return header + df.to_csv()
|
||||||
|
except Exception as e:
|
||||||
|
return f"Error retrieving A-share data: {str(e)}"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
result = get_stock_data_akshare(symbol="600519.SS", start_date="2026-01-01", end_date="2026-04-13")
|
||||||
|
print(result)
|
||||||
|
|
@ -12,6 +12,7 @@ from .y_finance import (
|
||||||
)
|
)
|
||||||
from .yfinance_news import get_news_yfinance, get_global_news_yfinance
|
from .yfinance_news import get_news_yfinance, get_global_news_yfinance
|
||||||
from .akshare_news import get_news_akshare
|
from .akshare_news import get_news_akshare
|
||||||
|
from .akshare_stock import get_stock_data_akshare
|
||||||
from .alpha_vantage import (
|
from .alpha_vantage import (
|
||||||
get_stock as get_alpha_vantage_stock,
|
get_stock as get_alpha_vantage_stock,
|
||||||
get_indicator as get_alpha_vantage_indicator,
|
get_indicator as get_alpha_vantage_indicator,
|
||||||
|
|
@ -73,6 +74,7 @@ VENDOR_METHODS = {
|
||||||
"get_stock_data": {
|
"get_stock_data": {
|
||||||
"alpha_vantage": get_alpha_vantage_stock,
|
"alpha_vantage": get_alpha_vantage_stock,
|
||||||
"yfinance": get_YFin_data_online,
|
"yfinance": get_YFin_data_online,
|
||||||
|
"akshare": get_stock_data_akshare,
|
||||||
},
|
},
|
||||||
# technical_indicators
|
# technical_indicators
|
||||||
"get_indicators": {
|
"get_indicators": {
|
||||||
|
|
@ -138,7 +140,6 @@ def route_to_vendor(method: str, *args, **kwargs):
|
||||||
"""Route method calls to appropriate vendor implementation with fallback support."""
|
"""Route method calls to appropriate vendor implementation with fallback support."""
|
||||||
category = get_category_for_method(method)
|
category = get_category_for_method(method)
|
||||||
if len(args) > 0 and isinstance(args[0], str):
|
if len(args) > 0 and isinstance(args[0], str):
|
||||||
print(f"AAAAAA args: {args}")
|
|
||||||
ticker = args[0].upper()
|
ticker = args[0].upper()
|
||||||
# 如果带有上海(.SS)或深圳(.SZ)后缀,强制优先使用 akshare
|
# 如果带有上海(.SS)或深圳(.SZ)后缀,强制优先使用 akshare
|
||||||
if ticker.endswith(".SS") or ticker.endswith(".SZ"):
|
if ticker.endswith(".SS") or ticker.endswith(".SZ"):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue