如何在Pine Script的alert()中传入3commas信号并解决语法报错问题
报错原因
你遇到的语法错误是字符串边界冲突导致的:你用双引号"包裹alert的消息内容,但是消息里的JSON本身也包含双引号,Pine Script解析时会把JSON里第一个双引号识别为整个字符串的结束标记,后续的message_type就变成了非法的语法片段,触发报错。
解决方法
方法1:用单引号包裹整个JSON字符串(更简便)
Pine Script支持用单引号'作为字符串的边界标记,此时字符串内部的双引号不需要额外转义,直接写即可。
正确的alert写法:
alert('{"message_type": "bot", "bot_id": 6517352, "email_token": "d106a1fc-6b41-410c-9532-38253f7273c5", "delay_seconds": 0}', alert.freq_once_per_bar)
方法2:转义JSON内部的双引号
如果你坚持用双引号包裹整个字符串,就需要给JSON里所有的双引号前面加转义符\,告诉编译器这个双引号是字符串内容的一部分,不是边界标记。
写法示例:
alert("{\"message_type\": \"bot\", \"bot_id\": 6517352, \"email_token\": \"d106a1fc-6b41-410c-9532-38253f7273c5\", \"delay_seconds\": 0}", alert.freq_once_per_bar)
修改后完整策略代码
if (not na(vrsi)) if (ta.crossover(vrsi, overSold) and ta.crossover(source, lower)) strategy.entry("RSI_BB_L", strategy.long, comment="RSI_BB_L") // 写入3commas买入信号JSON alert('{"message_type": "bot", "bot_id": 6517352, "email_token": "d106a1fc-6b41-410c-9532-38253f7273c5", "delay_seconds": 0}', alert.freq_once_per_bar) else if (ta.crossunder(vrsi, overBought) and ta.crossunder(source, upper)) strategy.close(id = "RSI_BB_L", comment="close") alert("SELL alert", alert.freq_once_per_bar)
内容的提问来源于stack exchange,提问作者ctgp
相关产品推荐
相关产品推荐

