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

예제 33

Leverage trap

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

분류
Host: 터미널의 데이터와 엔진 필요
권한
market.readworkspace.write
실행하기 전에
a terminal connected to a backend that captures funding and open interest for the symbol
하는 일
sounds the terminal's alert chime when the score is 3. Reads only.
실행 방법
grant market.read and workspace.write, Run. Attach it to "Every N seconds" (300 s) to keep watch.
언어
>=0.1.1, <0.2.0
호스트
>=0.1.7, <0.2.0
필요한 것
derivs >=0.1.0, <0.2.0alert >=0.1.0, <0.2.0event >=0.1.0, <0.2.0

소스

// 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(());
}