5

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://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 }}

Are there any solid examples as to how this could be done?

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Casey Harrils
  • 2,793
  • 12
  • 52
  • 93

1 Answers1

13

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.

bajaco
  • 830
  • 1
  • 4
  • 11
  • Thanks so much for the response - how would one use this as part of a Strategy (for example, strategy.entry, strategy.exit, etc. – Casey Harrils Apr 04 '21 at 11:29
  • 1
    That's a matter of personal preference, a strategy has no relation to whatever you're using to execute on the server-side. However in many cases you could use whatever condition enters your strategy in an if statement and use that to trigger the alert. There is no one answer, this could be implemented a number of ways. – bajaco Apr 04 '21 at 22:06