【问题标题】:sending Strategy data using Webhooks for Pinescript 4.0使用 Webhooks for Pinescript 4.0 发送策略数据
【发布时间】:2021-10-03 22:05:49
【问题描述】:

我正在使用Tradingview Pinescript 4.0.

此消息是针对: https://stackoverflow.com/questions/66926863/combining-data-from-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-an-alert-from-a-Strategy-on-TradingView

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

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

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

{{ strategy.comment }}

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

【问题讨论】:

    标签: json pine-script


    【解决方案1】:

    如果字符串格式为 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() 一起使用它们并没有真正的优势。

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

    【讨论】:

    • 非常感谢您的回复 - 如何将其用作策略的一部分(例如,strategy.entry、strategy.exit 等。
    • 这是个人喜好问题,策略与您在服务器端用来执行的任何内容都没有关系。但是,在许多情况下,您可以使用在 if 语句中输入策略的任何条件并使用它来触发警报。没有一个答案,这可以通过多种方式实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多