Product Update Automatically Build JSON Messages with the WebhookMessage TradingView Library

Mike Christensen Mike Christensen

Mike Christensen

Lots of people have a hard time making a proper JSON message for signals and alerts on TradingView, and they get confused when they see errors in their TradersPost log. To make it easier to create JSON messages, we made a tool in Pine Script that does it for you, so it works every time. Complete documentation and type hinting is available in TradingView as you create new messages.

The WebhookMessage library can be found here:

TradersPost WebhookMessage Library

As an example, let's say you want to create an alert that sends a market entry order with an indicator signal and you also want it to attach a trailing stop of 0.75%. First, import the library, then instantiate the constants, and compile a message:

import TradersPostInc/WebhookMessage/1 as wm
cnst = wm.CONSTANTS.new()

if (entryCondition)
  msg = wm.webhookMessage.new(
    ticker = syminfo.ticker,
    action = cnst.ACTION_BUY,
    sentiment = cnst.SENTIMENT_BULLISH,
    quantity = 1,
    stopLoss = wm.stopLossMessage.new(type = cnst.STOP_LOSS_TYPE_TRAILING_STOP, trailPercent = 0.75).buildStopLossJson()
    ).buildWebhookJson()
  alert(msg)

if (exitCondition)
  msg = wm.webhookMessage.new(action = cnst.ACTION_EXIT ).buildWebhookJson()
  alert(msg)

How about a simple strategy with an entry and exit?

import TradersPostInc/WebhookMessage/1 as wm
cnst = wm.CONSTANTS.new()

//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))

if (longCondition)
  msg = wm.webhookMessage.new(action = cnst.ACTION_BUY, quantity = 1).buildWebhookJson()
  strategy.entry("My Long Entry Id", strategy.long, alert_message = msg)

shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (shortCondition)
  msg = wm.webhookMessage.new(action = cnst.ACTION_SELL, quantity = 1).buildWebhookJson()
  strategy.entry("My Short Entry Id", strategy.short, alert_message =
msg)

https://www.tradingview.com/script/Dfo3ErmN-TradersPost-WebhookMessage-Library-Automatically-Build-JSON/

Are you ready to start automating your trading? Start your free account today.