本文へ移動
← すべてのサンプル

サンプル 25

Hook on start open charts

a hook for "the terminal starts": make sure the charts you work with

カテゴリ
Workspace:チャートやパネルを読む・変更する
権限
workspace.readworkspace.write
実行する前に
attached to "Terminal starts" with workspace.read and
何をするか
CHANGES YOUR WORKSPACE — opens a chart for each symbol of the
実行方法
in Script Hooks, "+ Add script" under "Terminal starts", choose this
言語
>=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: a hook for "the terminal starts": make sure the charts you work with
// are open, opening only the ones that are missing.
// Category: workspace
// Permissions: workspace.read, workspace.write
// Preconditions: attached to "Terminal starts" with workspace.read and
// workspace.write ticked on the hook
// Side effects: CHANGES YOUR WORKSPACE — opens a chart for each symbol of the
// watch list that has none (none, the second time).
// Run: in Script Hooks, "+ Add script" under "Terminal starts", choose this
// file, tick workspace.read and workspace.write on it. Edit `watch`.

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.list;

fn main() -> Result<Unit, Error> {
    let e = event.current();
    print("started by " + e.kind);
    let watch = ["BTCUSDT", "ETHUSDT"];
    let offered = workspace.symbols();
    for symbol in watch {
        if !list.contains(offered, symbol) {
            print(symbol + ": not offered by this terminal, skipped");
        } else {
            let mut open = false;
            for c in workspace.charts() {
                if c.symbol == symbol {
                    open = true;
                }
            }
            if open {
                print(symbol + ": already has a chart");
            } else {
                let opened = workspace.open_chart(symbol, "15m")?;
                print(symbol + ": opened " + opened.timeframe);
            }
        }
    }
    return Ok(());
}