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

예제 27

Trigger close charts

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

분류
Workspace: 차트와 패널을 읽거나 변경
권한
workspace.readworkspace.write
실행하기 전에
set as the script of a Price Trigger Line, with
하는 일
CHANGES YOUR WORKSPACE — closes the charts it names, with
실행 방법
place a Price Trigger Line (left rail, Lines), open its properties,
언어
>=0.1.1, <0.2.0
호스트
>=0.1.5, <0.2.0
필요한 것
workspace >=0.1.3, <0.2.0event >=0.1.0, <0.2.0

소스

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