Skip to content
← All examples

Example 31

Level alert

the script of a Price Trigger Line that raises an alert: a chime on this terminal and, if you give it an address, a

Category
Workspace: reads or changes charts and panels
Permissions
workspace.writeexternal.access
Before you run it
set as the script of a Price Trigger Line with workspace.write and external.access ticked on its Trigger page
What it does
sounds the alert chime; with a WEBHOOK, POSTS a message to that address (at most once a second, 20 a minute).
How to run it
place a Price Trigger Line (left rail, Lines), Trigger page: Choose… this file. "Run now" tests it without waiting.
Language
>=0.1.1, <0.2.0
Host
>=0.1.6, <0.2.0
Needs
alert >=0.1.0, <0.2.0event >=0.1.0, <0.2.0

Source

// Purpose: the script of a Price Trigger Line that raises an alert: a chime on this terminal and, if you give it an address, a
// message to your Discord channel. The message says what crossed and where; the address is yours and never leaves this file.
// Category: workspace
// Permissions: workspace.write, external.access
// Preconditions: set as the script of a Price Trigger Line with workspace.write and external.access ticked on its Trigger page
// (a program asks for what it can ever use, and this one can post). To post to Discord, paste your channel's webhook address into
// WEBHOOK below; left empty it only sounds the chime on this terminal.
// Side effects: sounds the alert chime; with a WEBHOOK, POSTS a message to that address (at most once a second, 20 a minute).
// Run: place a Price Trigger Line (left rail, Lines), Trigger page: Choose… this file. "Run now" tests it without waiting.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.6, <0.2.0";
requires "alert" ">=0.1.0, <0.2.0";
requires "event" ">=0.1.0, <0.2.0";

const WEBHOOK: String = "";

fn main() -> Result<Unit, Error> {
    let e = event.current();
    let symbol = e.symbol.unwrap_or("the market");
    let level = match e.price {
        Some(p) => to_string(p),
        None => "the line",
    };
    let message = symbol + " crossed " + level;
    alert.notify("Level crossed", message)?;
    if WEBHOOK != "" {
        match alert.webhook("discord", WEBHOOK, message + " · sent from TTT") {
            Ok(_) => print("posted to Discord"),
            Err(err) => print("the webhook failed: " + err.code + ": " + err.message),
        }
    }
    print(message);
    return Ok(());
}