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

예제 32

Hook session layout

a hook for "every N seconds" that changes the desk when a trading session opens: the active chart's timeframe and

분류
Workspace: 차트와 패널을 읽거나 변경
권한
workspace.readworkspace.write
실행하기 전에
attached to "Every N seconds" in the Script Hooks window, set to 60 s, with workspace.read and workspace.write
하는 일
CHANGES the active chart's timeframe and its EMA at a session open. Nothing else.
실행 방법
Script Hooks, "+ Add script" under "Every N seconds", choose this file, every_s = 60, tick the two permissions. Run by hand
언어
>=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 "every N seconds" that changes the desk when a trading session opens: the active chart's timeframe and
// its EMA, for Asia (00:00 UTC), London (07:00) and New York (13:00). It acts only in the first two minutes of a session, so you
// are free to change anything afterwards.
// Category: workspace
// Permissions: workspace.read, workspace.write
// Preconditions: attached to "Every N seconds" in the Script Hooks window, set to 60 s, with workspace.read and workspace.write
// ticked on the hook
// Side effects: CHANGES the active chart's timeframe and its EMA at a session open. Nothing else.
// Run: Script Hooks, "+ Add script" under "Every N seconds", choose this file, every_s = 60, tick the two permissions. Run by hand
// it reads a "manual" event and applies the layout of the session that is on now, so you can see it work.

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

fn session_of(hour: Int) -> String {
    if hour >= 13 && hour < 21 {
        return "New York";
    }
    if hour >= 7 && hour < 13 {
        return "London";
    }
    return "Asia";
}

fn main() -> Result<Unit, Error> {
    let e = event.current();
    let ms = time.unix_ms(e.time);
    let hour = (ms / 3600000) % 24;
    let into_hour = ms % 3600000;
    let opens = hour == 0 || hour == 7 || hour == 13;
    // Run by hand: show the current session's layout. By the timer: only at an open.
    if e.kind != "manual" && !(opens && into_hour < 120000) {
        return Ok(());
    }
    let session = session_of(hour);
    let timeframe = if session == "Asia" { "15m" } else { "5m" };
    let chart = workspace.active_chart().expect("no chart is open");
    workspace.set_timeframe(chart.chart, timeframe)?;
    workspace.set_indicator(chart.chart, "EMA", session != "Asia")?;
    print(session + " session: " + chart.symbol + " on " + timeframe + ", EMA " + (if session != "Asia" { "on" } else { "off" }));
    return Ok(());
}