From 94f509725a0d4443a0154b7754ccb936efa1f488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E8=89=B3?= Date: Wed, 12 Nov 2025 21:58:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0API=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dags/ai/dag_ai_stock.py | 7 +++--- dags/ai/stock/stock.py | 49 ++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/dags/ai/dag_ai_stock.py b/dags/ai/dag_ai_stock.py index 4809ca5..636c1c5 100644 --- a/dags/ai/dag_ai_stock.py +++ b/dags/ai/dag_ai_stock.py @@ -5,9 +5,10 @@ from airflow import DAG with DAG( dag_id="ai_stock_dag", # 每天运行 - schedule_interval="20 9 * * 1-5", - start_date=pendulum.datetime(2025, 4, 23, tz="Asia/Shanghai"), - catchup=True + schedule_interval="30 8 * * 1-5", + start_date=pendulum.datetime(2025, 11, 10, tz="Asia/Shanghai"), + catchup=True, + tags=["ai_stock"], ) as dag: t1 = BashOperator( task_id='ai_stock', diff --git a/dags/ai/stock/stock.py b/dags/ai/stock/stock.py index d872659..5ce5775 100644 --- a/dags/ai/stock/stock.py +++ b/dags/ai/stock/stock.py @@ -1,4 +1,7 @@ from goojia_utils import MysqlClient, ArkAIClient, dingutils +from goojia_utils import dateutils +from datetime import datetime +import requests db_util = MysqlClient( host="192.168.0.93", @@ -7,28 +10,28 @@ db_util = MysqlClient( user="root", password="Goojia123456" ) - -stocks = db_util.scan_by_sql_to_data_frame("select * from mu_stocks where `year_month`= (select max(`year_month`) from mu_stocks)") - -url = "http://didata-cloud-server.fangjia.com/chat/deepseek" - +date = int(dateutils.format_date(dateutils.add_months(datetime.today(), months=-1), fm='%Y%m%d')) +select_stocks = db_util.scan_by_sql_to_data_frame("select ts_code as 股票代码, stock_name as 股票名称,close as 股价, pe as PE,turnover_rate as 换手率 from mu_stocks where `year_month`= (select max(`year_month`) from mu_stocks)") +stat = db_util.scan_by_sql_to_data_frame(f"select ts_code as 股票代码, count(ts_code) as 命中次数 from mu_stocks where `year_month` > {date} group by ts_code") +select_stocks = select_stocks.merge(stat, on='股票代码', how='left') +select_stocks['命中次数'] = select_stocks['命中次数'].fillna(0) +select_stocks = select_stocks.sort_values(by=['命中次数', 'PE'], ascending=[False, True]) +def df_to_nested_list(df): + """转换为嵌套的 Markdown 列表""" + lines = [] + i= 1 + for _, row in df.iterrows(): + lines.append(f"{i}. **{row['股票名称']}** ({row['股票代码']}) PE: {row['PE']} 股价: {row['股价']:.2f} 出现次数: {row['命中次数']} 换手率: {row['换手率']:.2f}") + i = i + 1 + return '\n'.join(lines) data = { - 'content': f"帮我分析一下以下哪只股票上涨概率比较大:\n{stocks.to_markdown()}", - 'roleTips': '你是一个股票投资者,用表格的形式展示结果,你用1-10分来综合评价股票,按照评分高低顺序输出' + "msgtype": "markdown", + "markdown": { + "title": dateutils.format_date(datetime.today(), fm='%Y%m%d') + "选股策略播报", + "text": df_to_nested_list(select_stocks) + } } - -headers = { - 'Content-Type': 'application/json' -} -ark_client = ArkAIClient() - -messages = [ - ark_client.build_system_message("你是一个股票投资者,用表格的形式展示结果,你用1-10分来综合评价股票,按照评分高低顺序输出"), - ark_client.build_user_message(f"帮我分析一下以下哪只股票上涨概率比较大:\n{stocks.to_markdown()}") -] - -# 发送请求 -response = ark_client.chat_completion(messages) - -if response: - dingutils.send_text("【选股】:" + response["content"], "1e5f9aaeb90b091bc79652b52800cad2f1dee1ac0990149252511965f6d1c12a") +access_token = "1e5f9aaeb90b091bc79652b52800cad2f1dee1ac0990149252511965f6d1c12a" +# 发送消息至群 +msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=' + access_token, json=data).json() +print(msg) \ No newline at end of file