jsonpine-script

sending Strategy data using Webhooks for Pinescript 4.0


I am using Tradingview Pinescript 4.0.

This message was made with reference to: https://stackoverflow.com/questions/66926863/combining-data-from-different-tradingview-4-0-indicators-for-the-purpose-of-crea

Basically, what I would like to do is use Tradingview 4.0 Pinescript Strategies with the Tradingview Webhook Alert system.

The closest I have seen that provides a hint a to how one can do this is found in this particular video (for a different product): https://web.archive.org/web/20211022134351/https://support.mudrex.com/hc/en-us/articles/360050211072-Automating-an-alert-from-a-Strategy-on-TradingView

It would use something like the "comment" below:

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

I need to send JSON from the Strategy as part of the Web alert. According to the video, one would use something like:

{{ strategy.comment }}

How can this be done?


Solution

  • Tradingview sends the alert as JSON if the string is formatted as JSON. I suggest using the alert() function instead of alertcondition. It's then easy to construct a string in a JSON format however you want. Here's a nasty function I use to generate alerts.

    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)
    

    You don't need to do anything complex though, the most important line is the alertstring = '{' + array.join(alert_array,', ') + '}'

    You just need to make sure the string starts and ends with curly braces and is properly formatted JSON.

    Could just as easily:

    alert('{"Symbol": "' + syminfo.ticker + '", "Action": "Entry"}', alert.freq_once_per_bar_close)
    

    You just need to be careful with your double and single quotes.

    Also, this is explained if you go to set up an alert and click the option to learn more about alerts, at least the fact that formatting as JSON is what determines whether the alert is sent as text or JSON. As for the {{}}s, those are used to send a limited set of values for use with the alertcondition() function, and I don't really see an advantage to using them vs tostring() with alert().

    Another thing to keep in mind is that this necessitates sending your data as strings, so you will need to make sure to convert back to ints and floats as necessary on the server side.