使用 Pinescript 4.0 的 Webhooks 发送策略数据

sending Strategy data using Webhooks for Pinescript 4.0

我正在使用Tradingview Pinescript 4.0.

此消息参考了:

基本上,我想做的是将 Tradingview 4.0 Pinescript StrategiesTradingview Webhook Alert 系统一起使用。

在这个特定视频(针对不同的产品)中找到了我所看到的最接近的提示,可以提示如何做到这一点: https://support.mudrex.com/hc/en-us/articles/360050211072-Automating-an-alert-from-a-Strategy-on-TradingView

它将使用类似于下面的“评论”的内容:

// strategy.entry(id=tostring(randomNumber), long=false, comment="{"id" : " + tostring(randomNumber) + ", "action" : "reverse_short_to_long"}")

我需要从策略发送JSON作为网络警报的一部分。根据视频,人们会使用类似这样的东西:

{{ strategy.comment }}

有没有具体的例子说明如何做到这一点?

如果字符串格式为 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().

要记住的另一件事是,这需要将数据作为字符串发送,因此您需要确保在服务器端根据需要转换回整数和浮点数。