Skip to content
← All examples

Example 20

All charts to timeframe

put every open chart on one timeframe, and say which ones

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 — sets the timeframe of every open
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: put every open chart on one timeframe, and say which ones
// were already there. A refused timeframe stops the program before it
// touches a chart.
// Category: workspace
// Permissions: workspace.read, workspace.write
// Preconditions: a terminal window
// Side effects: CHANGES YOUR WORKSPACE — sets the timeframe of every open
// chart.
// Run: in TTT, open the T0 Editor, open this file, grant workspace.read
// and workspace.write, Run. Change `timeframe` (5m, 1h, 1D ...).

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.1, <0.2.0";
requires "workspace" ">=0.1.1, <0.2.0";

fn main() -> Result<Unit, Error> {
    let timeframe = "15m";
    let mut changed = 0;
    for c in workspace.charts() {
        if c.timeframe == timeframe {
            print(c.symbol + ": already " + timeframe);
        } else {
            workspace.set_timeframe(c.chart, timeframe)?;
            changed = changed + 1;
            print(c.symbol + ": " + c.timeframe + " -> " + timeframe);
        }
    }
    print(to_string(changed) + " chart(s) changed");
    return Ok(());
}