如果字符串格式为 JSON,Tradingview 会以 JSON 格式发送警报。我建议使用alert() 函数而不是alertcondition。然后,您可以轻松地以 JSON 格式构造字符串。这是我用来生成警报的一个讨厌的函数。
customalert(_name, _symbol, _type, _asset_type, _action, _risk_value, _risk_percent, _sl, _tp1,_tp1_percent, _tp2, _tp2_percent, _message) =>
alert_array = array.new_string()
if _name != ''
array.push(alert_array, '"Name": "' + _name + '"')
if _symbol != ''
array.push(alert_array, '"Symbol": "' + _symbol + '"')
if _type != ''
array.push(alert_array, '"Type": "' + _type + '"')
if _asset_type != ''
array.push(alert_array, '"Asset Type": "' + _asset_type + '"')
if _action != ''
array.push(alert_array, '"Action": "' + _action + '"')
if _risk_value != 0
array.push(alert_array, '"Risk Value": "' + tostring(_risk_value) + '"')
if _risk_percent != 0
array.push(alert_array, '"Risk Percentage": "' + tostring(_risk_percent) + '"')
if _tp1 != 0
array.push(alert_array, '"Take Profit 1 Level": "' + tostring(_tp1) + '"')
if _tp1_percent != 0
array.push(alert_array, '"Take Profit 1 Percent": "' + tostring(_tp1_percent) + '"')
if _tp2 != 0
array.push(alert_array, '"Take Profit 2 Level": "' + tostring(_tp2) + '"')
if _tp2_percent != 0
array.push(alert_array, '"Take Profit 2 Percent": "' + tostring(_tp2_percent) + '"')
if _sl != 0
array.push(alert_array, '"Stop Loss Level": "' + tostring(_sl) + '"')
if _message != ''
array.push(alert_array, '"Message": "' + _message + '"')
alertstring = '{' + array.join(alert_array,', ') + '}'
alert(alertstring, alert.freq_once_per_bar_close)
你不需要做任何复杂的事情,最重要的一行是alertstring = '{' + array.join(alert_array,', ') + '}'
您只需要确保字符串以花括号开头和结尾并且是正确格式的 JSON。
同样容易:
alert('{"Symbol": "' + syminfo.ticker + '", "Action": "Entry"}', alert.freq_once_per_bar_close)
你只需要小心你的双引号和单引号。
此外,如果您要设置警报并单击选项以了解有关警报的更多信息,至少会说明这一点,即格式化为 JSON 决定了警报是以文本还是 JSON 形式发送的。至于{{}}s,它们用于发送一组有限的值以供alertcondition() 函数使用,与tostring() 和alert() 一起使用它们并没有真正的优势。
要记住的另一件事是,这需要将数据作为字符串发送,因此您需要确保在服务器端根据需要转换回整数和浮点数。