Docs·TapeScript

Telegram alerts

Send TapeScript alerts to Telegram — a bot per script with // @telegram, chat ID, an HTML message template, HTML cards built by a script, and the Telegram Alerts sample.

A script sends its alerts to Telegram when its code names a bot. Each script can use a different bot, and a script without that line sends nothing. The desktop app sends the messages directly to Telegram, so alerts arrive only while the app is open.

Add a bot to a script

  1. In Telegram, open @BotFather and send /newbot. Choose a name and a username for the bot.
  2. Copy the bot token BotFather sends you. It looks like 123456789:AAH….
  3. Open your new bot and send it any message, for example hi.
  4. Add this line at the top of the script, with your token:
JavaScript
// @telegram 123456789:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
  1. Run the script. The Console shows Telegram: sending with bot … and the last four characters of the token.

Every alert() of that script is now also sent to the bot. The first message goes to the chat you wrote from in step 3; the app remembers that chat for this bot.

To send to a specific chat, put its ID after the token:

JavaScript
// @telegram 123456789:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw -1001234567890
// @telegram 123456789:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw @my_alerts

One bot per script

Give each script its own // @telegram line. Scripts can share a bot or use different ones:

ScriptLineWhere alerts go
Big Prints// @telegram <bot A token>Bot A, your private chat
Wall Pulls// @telegram <bot B token> -1001234567890Bot B, a group
VWAP Bandsno lineNot sent to Telegram

Each bot sends at its own pace, so a busy script does not delay the others.

Groups and channels

  • Group: add the bot to the group and post a message there. Without a chat ID in the line, the newest chat that wrote to the bot is used. To be sure, put the group's chat ID in the line.
  • Channel: add the bot as an administrator that can post, then put the channel's chat ID or @name in the line.

Message template

The Message template box in the widget's Telegram tab sets how messages from every script look. It is written in Telegram HTML and can span several lines. Leave it empty to use the default:

HTML
<b>{script}</b> · <code>{symbol}</code>
{body}
PlaceholderReplaced with
{script}Script name
{symbol}Symbol of the chart the script runs on
{text}The alert text (the first argument of alert())
{body}The script's own HTML for this alert, or the alert text when the script gives none
{time}Time of the trade that triggered the alert, HH:MM:SS, your computer's time zone
{date}Date of that trade, YYYY-MM-DD

Values are inserted as text: a < in a symbol or an alert text shows as <, it does not start a tag.

Template examples:

HTML
{time} <b>{symbol}</b> {text}
HTML
<blockquote><b>{symbol}</b> · {script}
{body}</blockquote>
<i>{date} {time}</i>
HTML
<b>{symbol}</b> · {body}
<a href="https://app.tapedelta.com">Open Tape Delta</a>

The next alert of a script with a bot uses the new template.

HTML tags

Telegram accepts these tags. Any other tag is shown as text.

TagResult
<b>, <strong>Bold
<i>, <em>Italic
<u>, <ins>Underline
<s>, <strike>, <del>Strikethrough
<code>Monospace, inline
<pre>Monospace block. <pre><code class="language-js"> adds syntax highlighting
<a href="https://…">Link. Only http://, https:// and tg:// links are kept
<blockquote>Quote. <blockquote expandable> is collapsed until tapped
<tg-spoiler> or <span class="tg-spoiler">Hidden until tapped
<br>Line break
  • Unclosed tags are closed at the end of the message.
  • A link with any other address keeps its text and loses the link.
  • If Telegram still refuses the formatting, the message is sent again as plain text and the script's Console shows Telegram: sent as plain text: check the HTML.
  • Emoji are removed from alerts. Use characters such as ▲ ▼ • · → —.

HTML from a script

The telegram option of alert() sets the message for that alert:

ValueTelegram message
Not setThe alert text, placed in {body}
A stringThat string as HTML, placed in {body}. Up to 1,000 characters
falseNothing is sent for this alert. Sound, voice and the Console are unchanged

The first argument stays short, because it is also the Console line and the spoken text. The telegram string can carry more detail:

JavaScript
alert("Big buy " + round(size) + " @ " + price, {
  sound: "fill", key: "big", cooldown: 2000,
  telegram: "<b>▲ Big buy</b>\nPrice <code>" + price + "</code>\nSize <code>" + round(size) + "</code>",
})

Use "\n" for a new line. To send only some alerts, pass false for the others, or switch it with an input:

JavaScript
// @input    tgBig: bool = true "Big prints to Telegram"

alert("Big buy " + round(size), { telegram: input.tgBig ? card : false })

Sample script

Telegram Alerts is in the widget under Samples. It marks big prints and absorption and sends each one as a card with price, size and the delta of the last 30 seconds. Two inputs turn each kind of Telegram message on or off. Replace PASTE_BOT_TOKEN with your bot token before running it; until then the Console reminds you and nothing is sent.

JavaScript
// @name     Telegram Alerts
// @input    mult: float = 8 "Big print vs average (x)"
// @input    absVol: float = 40 "Absorbed size"
// @input    windowMs: int = 3000 "Absorption window (ms)"
// @input    tgBig: bool = true "Big prints to Telegram"
// @input    tgAbs: bool = true "Absorption to Telegram"
// @telegram PASTE_BOT_TOKEN

const avg = rolling(200)
const flow = timeWindow(30000)
const bidAbs = timeWindow(input.windowMs)
const askAbs = timeWindow(input.windowMs)
let lastBid = 0
let lastAsk = 0

function num(x) {
  return round(x * 100) / 100
}

function card(title, side, price, size) {
  const head = side === BUY ? "▲ " : side === SELL ? "▼ " : "• "
  return "<b>" + head + title + "</b>\n" +
    "Price  <code>" + price + "</code>\n" +
    "Size   <code>" + num(size) + "</code>\n" +
    "Delta 30s  <code>" + num(flow.sum) + "</code>\n" +
    "<i>" + venue.id + "</i>"
}

function onBbo(bid, bidSize, ask, askSize, time) {
  if (bid !== lastBid) bidAbs.clear()
  if (ask !== lastAsk) askAbs.clear()
  lastBid = bid
  lastAsk = ask
}

function onTrade(price, size, side, time) {
  flow.advance(time)
  if (side === BUY) flow.add(time, size)
  else if (side === SELL) flow.add(time, -size)

  const ready = avg.full
  const mean = avg.mean
  avg.add(size)
  if (ready && size >= mean * input.mult) {
    const what = side === SELL ? "Big sell" : "Big buy"
    marker(time, price, what, side === SELL ? "#F6465D" : "#0ECB81")
    alert(what + " " + num(size) + " @ " + price, {
      sound: "fill", key: "big", cooldown: 2000,
      telegram: input.tgBig ? card(what, side, price, size) : false,
    })
  }

  if (side === SELL) bidAbs.add(time, size)
  else if (side === BUY) askAbs.add(time, size)
  if (bidAbs.sum >= input.absVol) {
    alert("Bid absorbing " + num(bidAbs.sum) + " @ " + price, {
      sound: "place", key: "abs", cooldown: 3000,
      telegram: input.tgAbs ? card("Bid absorption", BUY, price, bidAbs.sum) : false,
    })
    bidAbs.clear()
  } else if (askAbs.sum >= input.absVol) {
    alert("Ask absorbing " + num(askAbs.sum) + " @ " + price, {
      sound: "place", key: "abs", cooldown: 3000,
      telegram: input.tgAbs ? card("Ask absorption", SELL, price, askAbs.sum) : false,
    })
    askAbs.clear()
  }
}

With the default template, a big buy on ES arrives as:

Code
Telegram Alerts · ESZ6
▲ Big buy
Price  5012.25
Size   46
Delta 30s  318
rithmic

The script name and the title are bold; the numbers are monospace.

What is sent

Behaviour
ScriptsOnly scripts with a valid // @telegram line
MessagesOnly alert(). sound(), speak() and log() are not sent
RateAbout 1 message per second per bot and chat. Up to 30 more wait in line; beyond that the oldest are dropped
Same alert from two charts on the same symbolSent once
@history replayNothing is sent
App closed or computer asleepNothing is sent
Alert limitsThe alert rate limits apply first

Messages in the Console

Telegram problems appear in the Console of the script that sent the alert. Each message is shown once until an alert gets through again.

MessageWhat to do
Telegram: @telegram needs a bot token from @BotFatherThe line has no token, or a placeholder such as PASTE_BOT_TOKEN. Paste the whole token, including the part before :
Telegram: chat not found: check the chat ID in @telegramCheck the chat ID, or remove it to use the chat that wrote to the bot
Telegram: write any message to the bot, or add a chat ID to @telegramNobody has written to the bot yet. Send it a message
Telegram: Telegram rejected the bot tokenThe token was revoked. Send /token to BotFather, choose the bot and paste the new token into the line
Telegram: the bot cannot write to this chatUnblock the bot, or give it permission to post in the group or channel
Telegram: this bot has a webhook set: use a separate botThe bot is used by another service. Create a separate bot for TapeScript
Telegram: no connection to TelegramCheck the internet connection. Alerts that failed are not sent again
Telegram: too many alerts: the oldest were not sentMore than 30 alerts were waiting for one bot. Raise the cooldown or send fewer alerts to Telegram
Telegram: sent as plain text: check the HTMLFix the template or the script's HTML
Telegram: update the desktop app to send to TelegramInstall the latest Tape Delta desktop app

Bot token

The token is part of the script, in plain text: it is saved in the script file and in released copies of the script. Anyone who has the file can send messages as your bot.

  • Do not share a script with your token in it. Replace the token with PASTE_BOT_TOKEN first.
  • If a token leaks, send /revoke to BotFather, then paste the new token into your scripts.
  • The app does not store tokens itself. It only remembers which chat each bot sends to.