Skip to content
← All examples

Example 21

Indicator by prefix

turn an indicator on for every chart whose symbol starts with

Category
Workspace: reads or changes charts and panels
Permissions
workspace.readworkspace.write
Before you run it
a terminal window
What it does
CHANGES YOUR WORKSPACE — shows or hides the indicator on
How to run it
in TTT, open the T0 Editor, open this file, grant workspace.read
Language
>=0.1.1, <0.2.0
Host
>=0.1.1, <0.2.0
Needs
workspace >=0.1.1, <0.2.0

Source

// Purpose: turn an indicator on for every chart whose symbol starts with
// a prefix, and off for the others — then read each chart back, so what
// is printed is what the terminal holds, not what the program asked for.
// Category: workspace
// Permissions: workspace.read, workspace.write
// Preconditions: a terminal window
// Side effects: CHANGES YOUR WORKSPACE — shows or hides the indicator on
// every open chart.
// Run: in TTT, open the T0 Editor, open this file, grant workspace.read
// and workspace.write, Run.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.1, <0.2.0";
requires "workspace" ">=0.1.1, <0.2.0";
import std.text;
import std.list;

fn main() -> Result<Unit, Error> {
    let prefix = "BTC";
    let indicator = "EMA";
    for c in workspace.charts() {
        let wanted = text.starts_with(c.symbol, prefix);
        workspace.set_indicator(c.chart, indicator, wanted)?;
        let drawn = list.contains(workspace.indicators(c.chart)?, "Exponential Moving Average");
        print(c.symbol + " " + c.timeframe + ": " + indicator + (if drawn { " on" } else { " off" }));
    }
    return Ok(());
}