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

Cas*_*ils 7 json pine-script

我在用Tradingview Pinescript 4.0.

此消息参考: /sf/ask/4684880441/ Different-tradingview-4-0-indicators-for-the- Purpose-of-crea

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

我见过的最接近的提供了如何做到这一点的提示可以在这个特定的视频中找到(针对不同的产品): https://support.mudrex.com/hc/en-us/articles/360050211072-Automating -来自交易策略的警报视图

它会使用类似下面的“评论”:

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

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

{{ strategy.comment }}
Run Code Online (Sandbox Code Playgroud)

有没有任何可靠的例子来说明如何做到这一点?

小智 16

如果字符串格式为 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)
Run Code Online (Sandbox Code Playgroud)

不过,您不需要做任何复杂的事情,最重要的一行是alertstring = '{' + array.join(alert_array,', ') + '}'

您只需确保字符串以大括号开头和结尾,并且是正确格式的 JSON。

可以很容易地:

alert('{"Symbol": "' + syminfo.ticker + '", "Action": "Entry"}', alert.freq_once_per_bar_close)
Run Code Online (Sandbox Code Playgroud)

您只需要小心双引号和单引号即可。

此外,如果您去设置警报并单击该选项以了解有关警报的更多信息,则会对此进行解释,至少 JSON 格式决定了警报是作为文本还是 JSON 发送。至于{{}}s,它们用于发送一组有限的值以与该alertcondition()函数一起使用,我并没有真正看到使用它们tostring()alert().

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