Ir al contenido
← Todos los ejemplos

Ejemplo 09

Market state

What a strategy could have known at an instant, computed by TTT's own

Categoría
Host: necesita los datos y el motor de la terminal
Permisos
market.readresearch.submit
Lenguaje
>=0.1.1, <0.2.0
Host
>=0.1.1, <0.2.0
Necesita
market >=0.1.1, <0.2.0research >=0.1.0, <0.2.0

Código

// Category: host
// Permissions: market.read, research.submit
// What a strategy could have known at an instant, computed by TTT's own
// research engine with the strategy engine's features. A value that was
// not knowable yet is reported as unknown, with the reason; never as 0.
//
// Grant in the T0 Lang window: market.read, research.submit.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.1, <0.2.0";
requires "market" ">=0.1.1, <0.2.0";
requires "research" ">=0.1.0, <0.2.0";

fn main() -> Result<Unit, Error> {
    let ds = market.datasets("BTCUSDT")?.first().expect("no published BTCUSDT dataset on this machine");
    let start = ds.first.expect("an empty dataset");
    for minutes in [1min, 30min, 2h] {
        let state = research.market_state(ds, start + minutes, ["price(trades)", "sma(20)", "ema(50)"])?;
        print("as of " + to_string(state.as_of) + " (" + to_string(state.events_applied) + " events, " + to_string(state.gaps) + " gaps)");
        for r in state.readings {
            match r.value {
                Some(v) => print("  " + r.feature + " = " + to_string(v) + " " + r.unit + ", known since " + to_string(r.available_at.expect("a known value has a time"))),
                None => print("  " + r.feature + " unknown: " + r.unknown.expect("an unknown value says why")),
            }
        }
    }
    match research.market_state(ds, start + 1h, ["astrology(7)"]) {
        Ok(_) => print("accepted"),
        Err(e) => print("refused: " + e.code),
    }
    return Ok(());
}