人肉盯盘不仅低效,而且极易因情绪干扰错失关键的阻力位突破时机。针对多市场(A股、港股、美股)异动难以统一监控的痛点,本文介绍如何使用 Python 编写自动化监控脚本,借助 QuantDash 标准化数据接口实时抓取行情,配合飞书或企业微信 Webhook 机器人,实现跨市场价格突破的自动报警推送。
在多市场实时监控场景中,传统的自建行情接收模块存在三个主要技术卡点:
下面是一个完整的盘中价格监控脚本。脚本会获取指定股票的最新日内收盘价,并与设定的监控目标价(阻力位)进行对比。当发生向上突破时,将自动向飞书或企业微信的 Webhook 发送卡片告警。
(注:此处提供了飞书与企微通用的 JSON 卡片构建逻辑,使用 demo_public_token 调取真实行情数据,以便直接测试。)
import requests
import quantdash as qd
# 1. 基础配置
qd.set_token("demo_public_token")
# 飞书或企微机器人的 Webhook 地址 (请替换为您群聊机器人的真实 Webhook URL)
WEBHOOK_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxx"
# 2. 监控股票池与设定的突破警戒价格
MONITOR_LIST = {
"00700.HK": {"name": "腾讯控股", "target_price": 380.0},
"600519.SH": {"name": "贵州茅台", "target_price": 1660.0},
"AAPL.US": {"name": "苹果公司", "target_price": 240.0}
}
def send_feishu_alert(stock_code, stock_name, current_price, target_price):
"""
向飞书群机器人发送突破卡片通知
"""
payload = {
"msg_type": "interactive",
"card": {
"header": {
"title": {
"tag": "plain_text",
"content": f"🚨 【量化监控】多市场异动警报"
},
"template": "red"
},
"elements": [
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": (
f"**标的名称**: {stock_name} ({stock_code})\n"
f"**最新价格**: <font color='red'>**{current_price}**</font>\n"
f"**阻力目标价**: {target_price}\n"
f"**状态描述**: 当前价格已向上**突破**预设阻力点,请注意仓位调配。"
)
}
}
]
}
}
try:
response = requests.post(WEBHOOK_URL, json=payload, timeout=5)
if response.status_code == 200:
print(f"[{stock_name}] 突破消息推送成功")
else:
print(f"[{stock_name}] 推送失败: {response.text}")
except Exception as e:
print(f"网络推送异常: {str(e)}")
def run_price_monitor():
print(">>> 启动多市场行情监控...")
for symbol, config in MONITOR_LIST.items():
try:
# 实时获取最新的一条 K 线(作为盘中最新价参考)
df = qd.get_kline(symbol=symbol, start_date="2025-11-01", freq="1d", adjust="qfq")
if df.empty:
continue
# 取最新一条数据的收盘价
latest_data = df.iloc[-1]
current_price = latest_data['close']
target_price = config['target_price']
print(f"[{config['name']}] 当前价: {current_price} | 目标价: {target_price}")
# 触发判定
if current_price >= target_price:
print(f"🔥 发现突破!触发报警...")
send_feishu_alert(symbol, config['name'], current_price, target_price)
except Exception as e:
print(f"获取 {symbol} 行情失败: {str(e)}")
if __name__ == '__main__':
run_price_monitor()当您在盘中或测试环境中运行此脚本时,会在本地和飞书群内获得如下联动反馈:
>>> 启动多市场行情监控...
[腾讯控股] 当前价: 382.4 | 目标价: 380.0
🔥 发现突破!触发报警...
[腾讯控股] 突破消息推送成功
[贵州茅台] 当前价: 1648.5 | 目标价: 1660.0
[苹果公司] 当前价: 243.5 | 目标价: 240.0
🔥 发现突破!触发报警...
[苹果公司] 突破消息推送成功群聊机器人卡片效果:
🚨 【量化监控】多市场异动警报
-----------------------------
标的名称: 腾讯控股 (00700.HK)
最新价格: 382.4
阻力目标价: 380.0
状态描述: 当前价格已向上突破预设阻力点,请注意仓位调配。如果您想利用 Cursor 深度扩展此盯盘脚本,可以直接复制以下 Prompt 喂给 AI:
我有一个基于 `quantdash` API 开发的飞书报警推送脚本。现在我想在此基础上增加以下功能:
1. 修改逻辑,不仅监控绝对价格,还要计算该股票的最新日 K 线价格是否突破了过去 20 天的最高收盘价(唐奇安通道上轨突破)。
2. 在飞书卡片消息中加入过去 5 天的涨跌幅数据。
请基于 `demo_public_token` 的 API 接口规则帮我实现重构。在多市场策略的盯盘任务中,相比于使用复杂笨重的传统柜台行情软件或者不稳定的网络爬虫,QuantDash + 群机器人的轻量化无头方案展现出了优异的敏捷性。它既不占用本地屏幕空间,又便于通过云服务器定时任务进行全天候无人值守盯盘。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。