← Todos los ejemplos
Ejemplo 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
- Categoría
- Espacio de trabajo: lee o cambia gráficos y paneles
- Permisos
- workspace.readworkspace.write
- Antes de ejecutarlo
- attached to "Every N seconds" in the Script Hooks window, set to 60 s, with workspace.read and workspace.write
- Qué hace
- CHANGES the active chart's timeframe and its EMA at a session open. Nothing else.
- Cómo ejecutarlo
- Script Hooks, "+ Add script" under "Every N seconds", choose this file, every_s = 60, tick the two permissions. Run by hand
- Lenguaje
>=0.1.1, <0.2.0- Host
>=0.1.5, <0.2.0- Necesita
- workspace >=0.1.3, <0.2.0event >=0.1.0, <0.2.0
Código
// 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(()); }