Skip to content
← All examples

Example 27

Trigger close charts

the script of a Price Trigger Line: when the price crosses the line,

Category
Workspace: reads or changes charts and panels
Permissions
workspace.readworkspace.write
Before you run it
set as the script of a Price Trigger Line, with
What it does
CHANGES YOUR WORKSPACE — closes the charts it names, with
How to run it
place a Price Trigger Line (left rail, Lines), open its properties,
Language
>=0.1.1, <0.2.0
Host
>=0.1.5, <0.2.0
Needs
workspace >=0.1.3, <0.2.0event >=0.1.0, <0.2.0

Source

// Purpose: the script of a Price Trigger Line: when the price crosses the line,
// close every chart whose symbol starts with BTCUSDT (the last chart is
// always kept).
// Category: workspace
// Permissions: workspace.read, workspace.write
// Preconditions: set as the script of a Price Trigger Line, with
// workspace.read and workspace.write ticked on its Trigger page
// Side effects: CHANGES YOUR WORKSPACE — closes the charts it names, with
// whatever is drawn on them. Nothing is saved first.
// Run: place a Price Trigger Line (left rail, Lines), open its properties,
// Trigger page: Choose… this file, tick the two permissions. "Run now"
// tests it without waiting for the price.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.5, <0.2.0";
requires "workspace" ">=0.1.3, <0.2.0";
requires "event" ">=0.1.0, <0.2.0";
import std.text;

fn main() -> Result<Unit, Error> {
    let e = event.current();
    print("trigger line: " + e.kind);
    for line in e.data {
        print("  " + line);
    }
    let prefix = "BTCUSDT";
    let mut closed = 0;
    for c in workspace.charts() {
        if text.starts_with(c.symbol, prefix) {
            match workspace.close_chart(c.chart) {
                Ok(_) => {
                    closed = closed + 1;
                    print("closed the " + c.symbol + " " + c.timeframe + " chart");
                }
                Err(err) => print("kept the " + c.symbol + " " + c.timeframe + " chart: " + err.code),
            }
        }
    }
    print(to_string(closed) + " closed; " + to_string(workspace.charts().len()) + " chart(s) open now");
    return Ok(());
}