Ir al contenido
← Todos los ejemplos

Ejemplo 33

Leverage trap

"the leverage trap": funding at an extreme, open interest climbing, price going nowhere -- the setup of a squeeze. Scores it

Categoría
Host: necesita los datos y el motor de la terminal
Permisos
market.readworkspace.write
Antes de ejecutarlo
a terminal connected to a backend that captures funding and open interest for the symbol
Qué hace
sounds the terminal's alert chime when the score is 3. Reads only.
Cómo ejecutarlo
grant market.read and workspace.write, Run. Attach it to "Every N seconds" (300 s) to keep watch.
Lenguaje
>=0.1.1, <0.2.0
Host
>=0.1.7, <0.2.0
Necesita
derivs >=0.1.0, <0.2.0alert >=0.1.0, <0.2.0event >=0.1.0, <0.2.0

Código

// Purpose: "the leverage trap": funding at an extreme, open interest climbing, price going nowhere -- the setup of a squeeze. Scores it
// from 0 to 3 over the last four hours and raises an alert at 3. **It scores only what it knows**: if the capture has no reading at
// either end of the window it says so and gives no score, because an open interest carried across a hole would invent the climb.
// Category: host
// Permissions: market.read, workspace.write
// Preconditions: a terminal connected to a backend that captures funding and open interest for the symbol
// Side effects: sounds the terminal's alert chime when the score is 3. Reads only.
// Run: grant market.read and workspace.write, Run. Attach it to "Every N seconds" (300 s) to keep watch.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.7, <0.2.0";
requires "derivs" ">=0.1.0, <0.2.0";
requires "alert" ">=0.1.0, <0.2.0";
requires "event" ">=0.1.0, <0.2.0";
import std.fin;

const SYMBOL: String = "BTCUSDT";

// One point for each of the three legs. A leg whose number is not known scores nothing and is named.
fn main() -> Result<Unit, Error> {
    let now = event.current().time;
    let before = derivs.at(SYMBOL, now - 4h)?;
    let after = derivs.at(SYMBOL, now)?;
    match after.unknown {
        Some(why) => {
            print("no score: there is no reading now (" + why + ")");
            return Ok(());
        }
        None => {}
    }
    match before.unknown {
        Some(why) => {
            print("no score: there is no reading four hours ago to compare with (" + why + ")");
            return Ok(());
        }
        None => {}
    }

    let mut score = 0;
    let mut missing = 0;
    match after.funding_rate {
        Some(rate) => {
            let percent = to_float(rate.value()) * 100.0;
            if percent >= 0.05 || percent <= -0.05 {
                score = score + 1;
            }
            print("funding " + to_string(percent) + " % (extreme at 0.05 %)");
        }
        None => {
            missing = missing + 1;
            print("funding: the venue published none");
        }
    }
    match before.open_interest {
        Some(was) => {
            match after.open_interest {
                Some(is) => {
                    let change = (to_float(is.value()) / to_float(was.value()) - 1.0) * 100.0;
                    if change >= 5.0 {
                        score = score + 1;
                    }
                    print("open interest " + to_string(change) + " % in four hours (climbing at 5 %)");
                }
                None => {
                    missing = missing + 1;
                    print("open interest: not known now");
                }
            }
        }
        None => {
            missing = missing + 1;
            print("open interest: not known four hours ago");
        }
    }
    match before.mark {
        Some(was) => {
            match after.mark {
                Some(is) => {
                    let moved = to_float(fin.pct_change(was, is).value()) * 100.0;
                    if moved <= 0.5 && moved >= -0.5 {
                        score = score + 1;
                    }
                    print("price " + to_string(moved) + " % in four hours (flat within 0.5 %)");
                }
                None => {
                    missing = missing + 1;
                    print("price: not known now");
                }
            }
        }
        None => {
            missing = missing + 1;
            print("price: not known four hours ago");
        }
    }

    if missing > 0 {
        print(SYMBOL + ": " + to_string(score) + " of " + to_string(3 - missing) + " known legs; " + to_string(missing) + " unknown, so no alert");
        return Ok(());
    }
    print(SYMBOL + ": leverage trap score " + to_string(score) + " of 3");
    if score == 3 {
        alert.notify("Leverage trap", SYMBOL + ": funding extreme, open interest climbing, price flat")?;
    }
    return Ok(());
}