版本修改

This commit is contained in:
王文艳
2026-07-22 11:41:49 +08:00
parent 327b36e5a0
commit 9467a62152
9 changed files with 389 additions and 522 deletions
+23 -23
View File
@@ -1,23 +1,23 @@
from airflow.operators.bash import BashOperator
import pendulum
from airflow import DAG
with DAG(
dag_id="ai_stock_dag",
# 每天运行
schedule="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='stock_strategy',
bash_command='python /opt/airflow/dags/repo/dags/ai/stock/stock_strategy.py',
)
t2 = BashOperator(
task_id='ai_stock',
bash_command='python /opt/airflow/dags/repo/dags/ai/stock/stock.py',
)
t1 >> t2
# from airflow.operators.bash import BashOperator
# import pendulum
# from airflow import DAG
#
# with DAG(
# dag_id="ai_stock_dag",
# # 每天运行
# schedule="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='stock_strategy',
# bash_command='python /opt/airflow/dags/repo/dags/ai/stock/stock_strategy.py',
# )
#
# t2 = BashOperator(
# task_id='ai_stock',
# bash_command='python /opt/airflow/dags/repo/dags/ai/stock/stock.py',
# )
# t1 >> t2
+38 -38
View File
@@ -1,38 +1,38 @@
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",
port=3306,
database="stock",
user="root",
password="Goojia123456"
)
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_new where `trade_date`= (select max(`trade_date`) from mu_stocks_new)")
stat = db_util.scan_by_sql_to_data_frame(f"select ts_code as 股票代码, count(ts_code) as 命中次数 from mu_stocks_new where `trade_date` > {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 = {
"msgtype": "markdown",
"markdown": {
"title": dateutils.format_date(datetime.today(), fm='%Y%m%d') + "选股策略播报",
"text": df_to_nested_list(select_stocks)
}
}
access_token = "1e5f9aaeb90b091bc79652b52800cad2f1dee1ac0990149252511965f6d1c12a"
# 发送消息至群
msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=' + access_token, json=data).json()
msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=45ac47a34dc5491535d77d643512244a85d91d7e74676e9c42446dc06357326e', json=data).json()
print(msg)
# 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",
# port=3306,
# database="stock",
# user="root",
# password="Goojia123456"
# )
# 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_new where `trade_date`= (select max(`trade_date`) from mu_stocks_new)")
# stat = db_util.scan_by_sql_to_data_frame(f"select ts_code as 股票代码, count(ts_code) as 命中次数 from mu_stocks_new where `trade_date` > {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 = {
# "msgtype": "markdown",
# "markdown": {
# "title": dateutils.format_date(datetime.today(), fm='%Y%m%d') + "选股策略播报",
# "text": df_to_nested_list(select_stocks)
# }
# }
# access_token = "1e5f9aaeb90b091bc79652b52800cad2f1dee1ac0990149252511965f6d1c12a"
# # 发送消息至群
# msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=' + access_token, json=data).json()
# msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=45ac47a34dc5491535d77d643512244a85d91d7e74676e9c42446dc06357326e', json=data).json()
# print(msg)