Skip to content
← All examples

Example 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

Category
Workspace: reads or changes charts and panels
Permissions
workspace.writescript.run
Before you run it
attached to `indicator_signal` in Script Hooks with workspace.write and script.run ticked; the script it starts (REACTION) is in the scripts folder
What it does
sounds the alert chime; starts REACTION (a script may start scripts to a depth of 3, at most 30 starts a minute in all)
How to run it
Script Hooks > Hooks > Add hook > event "An indicator gives a signal" > Choose… this file. "Run now" tests it without waiting.
Language
>=0.2.0, <0.3.0
Host
>=0.1.23, <0.2.0
Needs
alert >=0.1.0, <0.2.0event >=0.1.0, <0.2.0script >=0.1.0, <0.2.0

Source

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