监控加密货币价格并在达到阈值时触发警报。支持 BTC、ETH、SOL 及 100 多种其他币种,使用 CoinGecko API。可配置价格上限和下限阈值。
加密价格警报 : 监控加密货币价格, 并在目标被击中时获得警告是一项面向实际任务的技能,主要用于不需要 API 键, 使用 CoinGecko 的 API;检查 BTC 、 EH 、 SOL 和 SOL 的当前价格。
从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
监控加密货币价格,并在达到设定目标时接收提醒。无需 API 密钥 —— 使用 CoinGecko 的免费 API。
# 直接调用 API(无需认证) curl "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,solana&vs_currencies=usd&include_24hr_change=true"
curl "https://api.coingecko.com/api/v3/coins/bitcoin?localization=false&tickers=false&community_data=false&developer_data=false&sparkline=false"
创建一个 crypto-alerts.json 文件:
{
"alerts": [
{
"coin": "bitcoin",
"symbol": "btc",
"condition": "below",
"price": 85000,
"message": "BTC 跌破 $85K!"
},
{
"coin": "bitcoin",
"symbol": "btc",
"condition": "above",
"price": 100000,
"message": "BTC 上穿 $100K!🚀"
},
{
"coin": "ethereum",
"symbol": "eth",
"condition": "below",
"price": 3000,
"message": "ETH 跌至 $3K 以下"
},
{
"coin": "solana",
"symbol": "sol",
"condition": "above",
"price": 200,
"message": "SOL 突破 $200!"
}
],
"telegram_bot_token": "YOUR_BOT_TOKEN",
"telegram_chat_id": "YOUR_CHAT_ID"
}
保存为 crypto_alert.py:
#!/usr/bin/env python3
import requests
import json
import sys
import os
from datetime import datetime
COINGECKO_API = "https://api.coingecko.com/api/v3"
def get_price(coin_id):
url = f"{COINGECKO_API}/simple/price"
params = {
"ids": coin_id,
"vs_currencies": "usd",
"include_24hr_change": "true",
"include_last_updated_at": "true"
}
r = requests.get(url, params=params, timeout=10)
r.raise_for_status()
data = r.json()
return data[coin_id]["usd"], data[coin_id].get("usd_24h_change", 0)
def check_alerts(alerts_config):
triggered = []
for alert in alerts_config.get("alerts", []):
coin = alert["coin"]
condition = alert["condition"]
target = alert["price"]
message = alert["message"]
try:
price, change_24h = get_price(coin)
except Exception as e:
print(f"Error fetching {coin}: {e}")
continue
should_fire = False
if condition == "above" and price >= target:
should_fire = True
elif condition == "below" and price <= target:
should_fire = True
if should_fire:
triggered.append({
"coin": coin,
"price": price,
"target": target,
"condition": condition,
"message": message,
"change_24h": change_24h,
"time": datetime.now().isoformat()
})
print(f"🚨 ALERT: {message} (Current: ${price:,.2f})")
else:
print(f" {coin.upper()}: ${price:,.2f} (24h: {change_24h:+.2f}%) — {condition} ${target:,.2f}: {'✓' if should_fire else 'not triggered'}")
return triggered
def send_telegram(message, bot_token, chat_id):
if not bot_token or not chat_id:
return
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
payload = {"chat_id": chat_id, "text": message, "parse_mode": "HTML"}
requests.post(url, json=payload, timeout=10)
if __name__ == "__main__":
config_path = os.path.join(os.path.dirname(__file__), "crypto-alerts.json")
if len(sys.argv) > 1:
config_path = sys.argv[1]
if os.path.exists(config_path):
with open(config_path) as f:
config = json.load(f)
else:
print("No crypto-alerts.json found. Using default BTC/ETH/SOL check.")
config = {
"alerts": [
{"coin": "bitcoin", "symbol": "btc", "condition": "above", "price": 0},
{"coin": "ethereum", "symbol": "eth", "condition": "above", "price": 0},
{"coin": "solana", "symbol": "sol", "condition": "above", "price": 0}
]
}
triggered = check_alerts(config)
if triggered:
summary = "🚨 Crypto Alert Triggered\n\n"
for t in triggered:
summary += f"{t['message']}\n"
summary += f"Price: ${t['price']:,.2f} | 24h: {t['change_24h']:+.2f}%\n\n"
bot_token = config.get("telegram_bot_token")
chat_id = config.get("telegram_chat_id")
if bot_token and chat_id:
send_telegram(summary, bot_token, chat_id)
with open("/tmp/crypto_alerts_triggered.json", "a") as f:
f.write(json.dumps({"triggered": triggered, "time": datetime.now().isoformat()}) + "\n")
python3 crypto_alert.py
# 每 15 分钟执行一次 */15 * * * * cd /path/to/skill && python3 crypto_alert.py >> /tmp/crypto_alerts.log 2>&1 # 每小时第 0 分钟执行一次 0 * * * * cd /path/to/skill && python3 crypto_alert.py >> /tmp/crypto_alerts.log 2>&1
# Bitcoin curl "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_24hr_change=true" # 多个代币 curl "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,solana,cardano,polkadot,avalanche-2,chainlink&vs_currencies=usd&include_24hr_change=true"
| 符号 | CoinGecko ID |
|---|---|
| BTC | bitcoin |
| ETH | ethereum |
| SOL | solana |
| ADA | cardano |
| DOT | polkadot |
| AVAX | avalanche-2 |
| LINK | chainlink |
| XRP | ripple |
| DOGE | dogecoin |
| MATIC | polygon |
| ARB | arbitrum |
| OP | optimism |
完整列表请访问:curl https://api.coingecko.com/api/v3/coins/list?per_page=250
above — 当价格 >= 目标值时触发below — 当价格 <= 目标值时触发percent_up — 当 24 小时涨跌幅 > X% 时触发(需添加 percent_threshold 字段)percent_down — 当 24 小时涨跌幅 < -X% 时触发(需添加 percent_threshold 字段)CoinGecko 免费 API 限制:
sleep(1.2)requests Python 库(pip install requests)基于真实加密货币监控实践构建。CoinGecko API 免费开放,但请合理、尊重地使用。
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.6万人学习
共89课时 | 133.4万人学习
共49课时 | 82.2万人学习