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

예제 36

Pulled wall

the wall that was not there: the largest resting bid of the book five minutes ago, followed every ten seconds, marked on the

분류
Workspace: 차트와 패널을 읽거나 변경
권한
market.readworkspace.readworkspace.write
실행하기 전에
a terminal connected to a data engine that builds the book of the chart's symbol
하는 일
DRAWS a level and a mark named "pulled_wall" on the active chart (run again: replaced).
실행 방법
grant market.read, workspace.read and workspace.write, Run.
언어
>=0.1.1, <0.2.0
호스트
>=0.1.7, <0.2.0
필요한 것
book >=0.1.0, <0.2.0workspace >=0.1.0, <0.2.0plot >=0.1.0, <0.2.0event >=0.1.0, <0.2.0

소스

// Purpose: the wall that was not there: the largest resting bid of the book five minutes ago, followed every ten seconds, marked on the
// chart at the moment most of it was pulled. **Two readings are compared only when both exist**: a stretch the book was not recorded
// through is not a wall being pulled, and the program says how much of the window it could not see.
// Category: workspace
// Permissions: market.read, workspace.read, workspace.write
// Preconditions: a terminal connected to a data engine that builds the book of the chart's symbol
// Side effects: DRAWS a level and a mark named "pulled_wall" on the active chart (run again: replaced).
// Run: grant market.read, workspace.read and workspace.write, Run.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.7, <0.2.0";
requires "book" ">=0.1.0, <0.2.0";
requires "workspace" ">=0.1.0, <0.2.0";
requires "plot" ">=0.1.0, <0.2.0";
requires "event" ">=0.1.0, <0.2.0";

const OWNER: String = "pulled_wall";

// The size resting in the band at `wall`, or None when the snapshot is unknown. A known snapshot without the band holds zero there
// (the band is no longer among the largest, or is gone).
fn resting(snapshot: book.Snapshot, wall: Price) -> Option<Float> {
    if !snapshot.known {
        return None;
    }
    let mut size: Float = 0.0;
    for level in snapshot.bids {
        if level.price == wall {
            size = to_float(level.size.value());
        }
    }
    return Some(size);
}

fn main() -> Result<Unit, Error> {
    let chart = workspace.active_chart().expect("no chart is open");
    let now = event.current().time;
    let history = book.history(chart.symbol, now - 5min, now, 10s, 20)?;
    plot.clear(chart.chart, OWNER)?;
    if history.missing > 0 {
        print(to_string(history.missing) + " of " + to_string(history.columns.len()) + " readings of the book were not recorded");
    }

    // The wall: the largest bid of the first reading there is.
    let mut wall_price: Option<Price> = None;
    let mut wall_size: Float = 0.0;
    for snapshot in history.columns {
        match wall_price {
            Some(_) => {}
            None => {
                if snapshot.known {
                    for level in snapshot.bids {
                        let size = to_float(level.size.value());
                        if size > wall_size {
                            wall_size = size;
                            wall_price = Some(level.price);
                        }
                    }
                }
            }
        }
    }
    if wall_price.is_none() {
        print("no verdict: the book was not recorded in the last five minutes");
        return Ok(());
    }
    let wall = wall_price.expect("checked above");
    plot.level(chart.chart, OWNER, wall, "bid wall " + to_string(wall_size), "blue")?;

    // Followed reading by reading. `before` is forgotten across a reading that is missing: the two sides of a hole are not compared.
    let mut before: Option<Float> = None;
    let mut pulled = false;
    for snapshot in history.columns {
        let here = resting(snapshot, wall);
        match here {
            Some(size) => {
                match before {
                    Some(was) => {
                        if !pulled && was >= wall_size * 0.5 && size <= was * 0.3 {
                            plot.marker(chart.chart, OWNER, snapshot.time, wall, "pulled: " + to_string(was) + " -> " + to_string(size), "orange")?;
                            pulled = true;
                        }
                    }
                    None => {}
                }
                before = Some(size);
            }
            None => {
                before = None;
            }
        }
    }
    print(chart.symbol + ": bid wall of " + to_string(wall_size) + " at " + to_string(wall) + (if pulled { " was PULLED" } else { " was not pulled while the book was being recorded" }));
    return Ok(());
}