//@version=6 // This Pine Scriptâ„¢ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // Builds a webhook JSON body and can send it when a bar closes across a price you set. // Buy and sell bodies use strategy.order_action, ticker, and action. // Informational bodies use text. The alert dialog message is not what gets sent. indicator("Webhook Alert Message", "Webhook msg", overlay=true) string signal = input.string("Auto", "Signal", options=["Auto", "Buy", "Sell", "Informational"], group="Message", tooltip="Auto sends a buy when the bar closes above the level and a sell when it closes below. The other choices use that signal for every cross.") string custom = input.string("", "Spoken detail", group="Message", tooltip="Leave blank to describe the cross and the price. This text is placed inside the JSON.") bool showPreview = input.bool(true, "Show the message preview", group="Message") float level = input.float(0.0, "Price level", minval=0.0, group="Trigger", tooltip="The alert arms only when this is above zero.") bool arm = input.bool(false, "Arm price-level alert", group="Trigger") bool sendTest = input.bool(false, "Send one test on the next bar close", group="Trigger", tooltip="Sends a single informational message the next time a realtime bar closes. Adding the script to history does not send it.") // Quotes and line breaks in a sentence would break the JSON body. escapeJson(string source) => string out = str.replace_all(source, "\\", "\\\\") out := str.replace_all(out, "\"", "\\\"") out := str.replace_all(out, "\n", " ") out webhookMessage(string kind, string detail) => string safeDetail = escapeJson(detail) string body = "{\"text\":\"" + safeDetail + "\"}" if kind == "Buy" or kind == "Sell" string side = kind == "Buy" ? "buy" : "sell" body := "{\"strategy\":{\"order_action\":\"" + side + "\"},\"ticker\":\"" + escapeJson(syminfo.ticker) + "\",\"action\":\"" + safeDetail + "\"}" body // The side chooses the JSON shape. The sentence follows which way price crossed. detailFor(string customText, bool brokeDown, string aboveText, string belowText) => string generated = brokeDown ? belowText : aboveText customText == "" ? generated : customText string priceText = str.tostring(close, format.mintick) string levelText = str.tostring(level, format.mintick) string aboveText = "Closed above " + levelText + "." string belowText = "Closed below " + levelText + "." bool levelOk = arm and level > 0 // Crossover state must update on every bar, including bars where the level is not armed. bool rawUp = ta.crossover(close, level) bool rawDown = ta.crossunder(close, level) bool crossUp = levelOk and rawUp bool crossDown = levelOk and rawDown bool crossFire = crossUp or crossDown string crossKind = signal if signal == "Auto" crossKind := crossDown ? "Sell" : "Buy" var bool testSent = false bool testFire = sendTest and not testSent and not crossFire if barstate.isconfirmed and barstate.isrealtime and crossFire alert(webhookMessage(crossKind, detailFor(custom, crossDown, aboveText, belowText)), alert.freq_once_per_bar_close) if barstate.isconfirmed and barstate.isrealtime and testFire testSent := true string testDetail = custom == "" ? "Test alert. Price is " + priceText + "." : custom alert(webhookMessage("Informational", testDetail), alert.freq_once_per_bar_close) plot(level > 0 ? level : na, "Price level", color=color.new(color.gray, 0), linewidth=1, style=plot.style_linebr) plotshape(crossUp and barstate.isconfirmed, "Close above level", shape.triangleup, location.belowbar, color.teal, size=size.tiny) plotshape(crossDown and barstate.isconfirmed, "Close below level", shape.triangledown, location.abovebar, color.orange, size=size.tiny) var table panel = table.new(position.top_right, 1, 2, bgcolor=color.new(color.white, 0), border_width=1, frame_color=color.new(color.gray, 40), border_color=color.new(color.gray, 60)) if barstate.islast if showPreview string previewKind = signal == "Auto" ? "Buy" : signal string statusText = levelOk ? "Armed" : "Not armed" table.cell(panel, 0, 0, statusText + " | " + previewKind, text_color=color.black, text_size=size.small, text_halign=text.align_left) table.cell(panel, 0, 1, webhookMessage(previewKind, detailFor(custom, false, aboveText, belowText)), text_color=color.black, text_size=size.small, text_halign=text.align_left) else table.clear(panel, 0, 0, 0, 1)