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

예제 72

On level cross

what to do when an indicator gives a signal. Attached as a hook to the event `indicator_signal` (Script Hooks, with the indicator's name as the source filter), it chimes, says what the

분류
Workspace: 차트와 패널을 읽거나 변경
권한
workspace.writescript.run
실행하기 전에
attached to `indicator_signal` in Script Hooks with workspace.write and script.run ticked; the script it starts (REACTION) is in the scripts folder
하는 일
sounds the alert chime; starts REACTION (a script may start scripts to a depth of 3, at most 30 starts a minute in all)
실행 방법
Script Hooks > Hooks > Add hook > event "An indicator gives a signal" > Choose… this file. "Run now" tests it without waiting.
언어
>=0.2.0, <0.3.0
호스트
>=0.1.23, <0.2.0
필요한 것
alert >=0.1.0, <0.2.0event >=0.1.0, <0.2.0script >=0.1.0, <0.2.0

소스

// Purpose: what to do when an indicator gives a signal. Attached as a hook to the event `indicator_signal` (Script Hooks, with the indicator's name as the source filter), it chimes, says what the
// indicator saw, and starts another control script (`script.run`) with the signal in its arguments -- the chain "the price crosses, the indicator signals, this runs, the next one runs".
// To tell the world as well, add a webhook hook to the same event instead of writing the post here: it goes through the same guards and needs no script.
// Category: workspace
// Permissions: workspace.write, script.run
// Preconditions: attached to `indicator_signal` in Script Hooks with workspace.write and script.run ticked; the script it starts (REACTION) is in the scripts folder
// Side effects: sounds the alert chime; starts REACTION (a script may start scripts to a depth of 3, at most 30 starts a minute in all)
// Run: Script Hooks > Hooks > Add hook > event "An indicator gives a signal" > Choose… this file. "Run now" tests it without waiting.

lang ">=0.2.0, <0.3.0";
host "ttt" ">=0.1.23, <0.2.0";
requires "alert" ">=0.1.0, <0.2.0";
requires "event" ">=0.1.0, <0.2.0";
requires "script" ">=0.1.0, <0.2.0";

// The control script started next; empty starts nothing.
const REACTION: String = "";

fn main() -> Result<Unit, Error> {
    let e = event.current();
    let symbol = e.symbol.unwrap_or("the market");
    let price = match e.price {
        Some(p) => to_string(p),
        None => "an unknown price",
    };
    alert.notify("Indicator signal", symbol + " at " + price)?;
    for line in e.data {
        print(line);
    }
    if REACTION != "" {
        script.run(REACTION, ["symbol=" + symbol, "price=" + price])?;
    }
    return Ok(());
}