본문으로 건너뛰기
← 모든 예제

예제 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

분류
Workspace: 차트와 패널을 읽거나 변경
권한
workspace.writeexternal.access
실행하기 전에
set as the script of a Price Trigger Line with workspace.write and external.access ticked on its Trigger page
하는 일
sounds the alert chime; with a WEBHOOK, POSTS a message to that address (at most once a second, 20 a minute).
실행 방법
place a Price Trigger Line (left rail, Lines), Trigger page: Choose… this file. "Run now" tests it without waiting.
언어
>=0.1.1, <0.2.0
호스트
>=0.1.6, <0.2.0
필요한 것
alert >=0.1.0, <0.2.0event >=0.1.0, <0.2.0

소스

// 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(());
}