TradingView发送Webhook至PHP服务器报错:request.post未找到
问题解决指南
1. 核心错误原因
TradingView Pine脚本不支持直接调用request.post发起HTTP请求,该函数不属于Pine的内置函数库。所有Webhook请求必须通过TradingView的Alert系统触发,而非在脚本内直接调用请求方法。
2. 修正后的Pine脚本
先修复重复条件、移除无效的request.post代码,同时正确拼接合约代码信息:
//@version=5 indicator("RSI Alert with Webhook", overlay=false) // 示例基础指标定义(根据你的实际逻辑调整) rsi = ta.rsi(close, 14) rsi2 = ta.rsi(close, 28) rsiColor1 = color.new(#0ebb23, 0) // 对应原代码中的绿色 rsiDowntrend = ta.rsi(close,14) < 50 // 示例下跌趋势判断,按需修改 // 修复重复的crossunder条件 condition_alert = ta.crossunder(rsi, rsi2) and rsiColor1[1] == #0ebb23 and rsiDowntrend if (condition_alert) // 拼接消息:将条件标识与合约代码组合 alert_message = "condition_alert," + syminfo.ticker // 触发Alert,设置为每根K线收盘仅触发一次 alert(alert_message, alert.freq_once_per_bar_close)
3. 配置TradingView Alert实现Webhook
脚本修改完成后,需在TradingView中配置Alert来发送Webhook:
- 右键图表 → 选择
Add Alert打开设置面板 - 在
Condition中选择你的脚本名称,触发条件选Alert() function calls - 在
Actions区域勾选Webhook URL,输入你的PHP服务器地址:https://MYserver.com/hook.php(注意移除多余的斜杠) - 保存Alert配置
4. 字符串拼接的正确性验证
原代码中的"condition_alert" + syminfo.ticker写法是合法的,Pine脚本支持字符串与内置变量直接拼接。如果需要更结构化的格式,也可以用str.format("condition_alert:{0}", syminfo.ticker)生成消息,方便PHP端解析。
5. PHP服务器端接收示例
你的hook.php可以这样处理Webhook数据:
<?php // 获取TradingView发送的POST原始数据 $data = file_get_contents("php://input"); // 示例:将数据写入日志 file_put_contents("webhook_log.txt", $data . PHP_EOL, FILE_APPEND); // 返回成功响应 http_response_code(200); ?>
内容的提问来源于stack exchange,提问作者metokitt
相关产品推荐
相关产品推荐

