添加API启动支持
This commit is contained in:
@@ -5,9 +5,10 @@ from airflow import DAG
|
|||||||
with DAG(
|
with DAG(
|
||||||
dag_id="ai_stock_dag",
|
dag_id="ai_stock_dag",
|
||||||
# 每天运行
|
# 每天运行
|
||||||
schedule_interval="20 9 * * 1-5",
|
schedule_interval="30 8 * * 1-5",
|
||||||
start_date=pendulum.datetime(2025, 4, 23, tz="Asia/Shanghai"),
|
start_date=pendulum.datetime(2025, 11, 10, tz="Asia/Shanghai"),
|
||||||
catchup=True
|
catchup=True,
|
||||||
|
tags=["ai_stock"],
|
||||||
) as dag:
|
) as dag:
|
||||||
t1 = BashOperator(
|
t1 = BashOperator(
|
||||||
task_id='ai_stock',
|
task_id='ai_stock',
|
||||||
|
|||||||
+26
-23
@@ -1,4 +1,7 @@
|
|||||||
from goojia_utils import MysqlClient, ArkAIClient, dingutils
|
from goojia_utils import MysqlClient, ArkAIClient, dingutils
|
||||||
|
from goojia_utils import dateutils
|
||||||
|
from datetime import datetime
|
||||||
|
import requests
|
||||||
|
|
||||||
db_util = MysqlClient(
|
db_util = MysqlClient(
|
||||||
host="192.168.0.93",
|
host="192.168.0.93",
|
||||||
@@ -7,28 +10,28 @@ db_util = MysqlClient(
|
|||||||
user="root",
|
user="root",
|
||||||
password="Goojia123456"
|
password="Goojia123456"
|
||||||
)
|
)
|
||||||
|
date = int(dateutils.format_date(dateutils.add_months(datetime.today(), months=-1), fm='%Y%m%d'))
|
||||||
stocks = db_util.scan_by_sql_to_data_frame("select * from mu_stocks where `year_month`= (select max(`year_month`) from mu_stocks)")
|
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")
|
||||||
url = "http://didata-cloud-server.fangjia.com/chat/deepseek"
|
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 = {
|
data = {
|
||||||
'content': f"帮我分析一下以下哪只股票上涨概率比较大:\n{stocks.to_markdown()}",
|
"msgtype": "markdown",
|
||||||
'roleTips': '你是一个股票投资者,用表格的形式展示结果,你用1-10分来综合评价股票,按照评分高低顺序输出'
|
"markdown": {
|
||||||
|
"title": dateutils.format_date(datetime.today(), fm='%Y%m%d') + "选股策略播报",
|
||||||
|
"text": df_to_nested_list(select_stocks)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
access_token = "1e5f9aaeb90b091bc79652b52800cad2f1dee1ac0990149252511965f6d1c12a"
|
||||||
headers = {
|
# 发送消息至群
|
||||||
'Content-Type': 'application/json'
|
msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=' + access_token, json=data).json()
|
||||||
}
|
print(msg)
|
||||||
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")
|
|
||||||
Reference in New Issue
Block a user