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

时间:2021-04-03 01:12:49

标签: json pine-script

我正在使用 Tradingview Pinescript 4.0.

此消息是参考: combining data from different Tradingview 4.0 Indicators for the purpose of creating and managing a Position

基本上,我想做的是将 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 }}

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

TIA

1 个答案:

答案 0 :(得分:3)

如果字符串的格式为 JSON,Tradingview 会以 JSON 格式发送警报。我建议使用 @Data @JsonIgnoreProperties(ignoreUnknown = true) public class User { private String name; private Integer age; private String address; ... } 函数而不是 alertcondition。然后很容易以任何你想要的方式构造一个 JSON 格式的字符串。这是我用来生成警报的讨厌的函数。

alert()

你不需要做任何复杂的事情,最重要的一行是 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)

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

也可以很容易:

alertstring = '{' + array.join(alert_array,', ') + '}'

你只需要小心你的双引号和单引号。

此外,如果您去设置警报并单击该选项以了解有关警报的更多信息,至少会解释这一点,即格式化为 JSON 决定了警报是作为文本还是 JSON 发送。至于 alert('{"Symbol": "' + syminfo.ticker + '", "Action": "Entry"}', alert.freq_once_per_bar_close) s,它们用于发送一组有限的值以与 {{}} 函数一起使用,我真的没有看到将它们与 alertcondition() 一起使用的优势tostring()

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