从零到一用 Cron+Web‑Fetch+OpenClaw 搭建全自动的 A 股行情提醒系统
摘要 本文介绍如何利用OpenClaw的Cron和ollama_web_fetch功能实现A股行情自动化监控系统。系统通过Python脚本每5分钟抓取10支蓝筹股行情数据,计算技术指标(5日均线、RSI14等),当出现超卖信号(RSI<30且价格低于20日均线)时自动推送提醒。配置过程包括:1)准备Web Fetch功能;2)编写行情抓取和分析脚本;3)设置Cron定时任务;4)测试验证。该
背景
在 2020‑2025 年间,A 股的波动性大幅提升,传统的手动盯盘已难以满足快速决策的需求。借助 OpenClaw 的 Cron 与 ollama_web_fetch,我们可以把行情抓取、技术指标计算和策略触发全部自动化,形成 “看盘‑策略‑提醒” 的闭环。
1️⃣ 准备工作
-
开启 Web Fetch(已在网关配置
tools.web.fetch.enabled: true),确保可以访问公开行情接口。 -
确认 Token:
gateway.auth.token已经生成,后面调用系统事件时需要在 Header 中加入Authorization: Bearer <token>。 -
选定股票(本文示例十支常见蓝筹):
sh600519 # 贵州茅台 sh600036 # 招商银行 sh600030 # 中信证券 sh600887 # 伊利股份 sh600585 # 海螺水泥 sh600276 # 恒瑞医药 sh601166 # 兴业银行 sh600009 # 上海机场 sh600016 # 民生银行 sh601318 # 平安保险
2️⃣ 编写抓取脚本(Python 示例)
路径:~/workspace/scripts/a股_monitor.py
import json, os, urllib.request, datetime
# -------------------
# 配置(可通过环境变量注入)
# -------------------
SYMBOLS = [
"sh600519","sh600036","sh600030","sh600887","sh600585",
"sh600276","sh601166","sh600009","sh600016","sh601318"
]
BASE_URL = "https://qt.gtimg.cn/q=" # 腾讯行情接口(返回类似 v_sh600519="...")
# -------------------
# 工具函数
# -------------------
def fetch(symbols):
url = BASE_URL + ",".join(symbols)
with urllib.request.urlopen(url, timeout=8) as resp:
raw = resp.read().decode("utf-8")
# raw 示例:v_sh600519="1~贵州茅台~600519~1474.32~..."
data = {}
for line in raw.strip().split("\n"):
if not line: continue
key, val = line.split("=", 1)
# 去掉前缀 v_ 并解析字段(第 4 项为最新价)
fields = val.strip('"').split('~')
code = fields[2]
price = float(fields[3])
high = float(fields[5])
low = float(fields[6])
prev_close = float(fields[4])
data[code] = {"price":price,"high":high,"low":low,"prev":prev_close}
return data
# -------------------
# 计算技术指标(示例:5日均线、RSI14)
# -------------------
def sma(prices, n):
return sum(prices[-n:]) / n if len(prices) >= n else None
def rsi(closes, period=14):
if len(closes) <= period:
return None
gains, losses = [], []
for i in range(-period, 0):
diff = closes[i] - closes[i-1]
if diff > 0:
gains.append(diff); losses.append(0)
else:
gains.append(0); losses.append(-diff)
avg_gain = sum(gains) / period
avg_loss = sum(losses) / period
if avg_loss == 0:
return 100
rs = avg_gain / avg_loss
return 100 - (100/(1+rs))
# -------------------
# 主函数 – 被 Cron 调用
# -------------------
def main():
# 1. 抓取最新行情(一次请求返回全部 10 只)
market = fetch(SYMBOLS)
# 2. 简单历史缓存(本地 JSON)
hist_path = os.path.expanduser('~/a股_history.json')
try:
with open(hist_path) as f:
history = json.load(f)
except FileNotFoundError:
history = {sym: [] for sym in SYMBOLS}
alerts = []
for sym, data in market.items():
price = data["price"]
# 保存本次收盘价,保留最近 30 天(约 300 条)
hist = history.get(sym, [])
hist.append(price)
if len(hist) > 300:
hist = hist[-300:]
history[sym] = hist
# 计算指标
sma5 = sma(hist, 5)
sma20 = sma(hist, 20)
cur_rsi = rsi(hist, 14)
# 示例策略:
# - RSI < 30(超卖)
# - 当前价低于 20 日均线
if cur_rsi is not None and cur_rsi < 30 and sma20 and price < sma20:
alerts.append(f"{sym} 超卖信号:RSI={cur_rsi:.1f}, 价={price:.2f}, 20日均线={sma20:.2f}")
# 3. 持久化历史
with open(hist_path, 'w') as f:
json.dump(history, f)
# 4. 若有提醒,则通过系统事件推送(OpenClaw 会在当前会话弹出)
if alerts:
msg = "\n".join(alerts)
# 系统事件 payload(OpenClaw 通过内部管道发送)
# 这里直接 print,Cron job 会把它包装成 systemEvent
print(msg)
if __name__ == "__main__":
main()
要点:脚本自行维护本地
~/a股_history.json(价格时间序列),避免每次请求都需要下载历史数据。Cron 只负责 每 5 分钟 执行一次python3 a股_monitor.py,输出的文本会被 OpenClaw 捕获并转为 systemEvent,进而在聊天窗口里弹出提醒。
3️⃣ 在 OpenClaw 中创建 Cron Job
# 将脚本放到工作目录(已完成)
# 创建 cron,间隔 5 分钟(300000 ms)
cron add \
--name "A股行情提醒" \
--schedule "{\"kind\":\"every\",\"everyMs\":300000}" \
--payload '{"kind":"systemEvent","text":"$(python3 ~/workspace/scripts/a股_monitor.py)"}' \
--delivery '{"mode":"announce"}' \
--sessionTarget "main"
解释:
- schedule.kind = “every”,
everyMs: 300000→ 每 5 分钟运行一次。 - payload.kind = “systemEvent”,
text为脚本输出(使用$(python3 …)直接在 shell 中执行)。 - delivery.mode = “announce” → 当触发时把文本直接推送到当前聊天窗口,等同于
HEARTBEAT的提醒。 - sessionTarget = “main” 必须使用
systemEvent(只能在主会话),因此提醒会在你当前的聊天对话里出现。
注意:如果你想把提醒发送到特定渠道(如 Feishu 群),把
delivery改成{ "mode": "announce", "channel": "feishu", "to": "<group-id>" }。
4️⃣ 完整运行示例
# 手动测试一次(确保脚本可跑)
python3 ~/workspace/scripts/a股_monitor.py
# 查看 cron 列表确认已添加
cron list
# 等待 5 分钟后,聊天窗口会出现类似:
# sh600519 超卖信号:RSI=28.3, 价=1472.11, 20日均线=1480.45
# sh600276 超卖信号:RSI=26.7, 价=92.05, 20日均线=94.20
5️⃣ 常见问题 & 调优
| 场景 | 解决方案 |
|---|---|
| 抓取失败(403 / 429) | 通过 tools.web.fetch 已自动加 User-Agent,若仍被限速,可在脚本里加 time.sleep(1) 限制请求频率;或者换用 https://hq.sinajs.cn/list=(新浪)作为备选。 |
| 指标计算偏差 | 本示例使用简化的 SMA、RSI 算法;若需要更精确的技术指标,建议引入 pandas-ta(需要在容器里 pip install pandas-ta),或借助外部 API 如 Alpha Vantage(需 API Key)。 |
| 提醒频繁 | 在脚本里加入 去重:记录上一次触发的时间戳,仅在间隔 > 30 分钟时再次发送同一支股票的提醒。 |
| 跨平台部署 | 若想在 VPS 或云主机上长期运行,建议把脚本和 cron 放入 systemd service,或在 Kubernetes 中使用 CronJob 替代 OpenClaw 内置。 |
6️⃣ 小结 & 进一步拓展
- 全自动:抓取 → 计算 → 触发 → 推送,全部在 OpenClaw 中配置完成。
- 可定制:把策略换成 均线金叉、MACD 胜率、新闻情感 等,直接在 Python 脚本里实现。
- 多渠道:通过
delivery.channel把提醒推送到 Feishu、Telegram、Discord 等任意已配置的渠道。 - AI 辅助:后续可以让 OpenClaw 使用
text2vec把每支股票的基本面信息向量化,再在提醒里加入 AI 生成的简短分析(如 “近期业绩提升,建议关注”。)
更多推荐



所有评论(0)