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

예제 30

Prior day levels

yesterday's high, low and close as levels on the chart, from the daily candles.

분류
Workspace: 차트와 패널을 읽거나 변경
권한
market.readworkspace.readworkspace.write
실행하기 전에
a terminal window with a chart open
하는 일
DRAWS three horizontal lines on the active chart, named "prior_day". Running it again replaces them.
실행 방법
grant market.read, workspace.read and workspace.write in the T0 Editor, Run.
언어
>=0.1.1, <0.2.0
호스트
>=0.1.6, <0.2.0
필요한 것
market >=0.1.2, <0.2.0workspace >=0.1.0, <0.2.0plot >=0.1.0, <0.2.0

소스

// Purpose: yesterday's high, low and close as levels on the chart, from the daily candles.
// Category: workspace
// Permissions: market.read, workspace.read, workspace.write
// Preconditions: a terminal window with a chart open
// Side effects: DRAWS three horizontal lines on the active chart, named "prior_day". Running it again replaces them.
// Run: grant market.read, workspace.read and workspace.write in the T0 Editor, Run.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.6, <0.2.0";
requires "market" ">=0.1.2, <0.2.0";
requires "workspace" ">=0.1.0, <0.2.0";
requires "plot" ">=0.1.0, <0.2.0";

const OWNER: String = "prior_day";

fn main() -> Result<Unit, Error> {
    let chart = workspace.active_chart().expect("no chart is open");
    // Two closed daily candles: the day before yesterday is ignored, the last closed one is yesterday.
    let days = market.chart_candles(chart.symbol, "1D", 1)?;
    let yesterday = days.last().expect("the backend has no closed daily candle for this symbol");
    plot.clear(chart.chart, OWNER)?;
    plot.level(chart.chart, OWNER, yesterday.high, "prior day high", "red")?;
    plot.level(chart.chart, OWNER, yesterday.low, "prior day low", "green")?;
    plot.level(chart.chart, OWNER, yesterday.close, "prior day close", "gray")?;
    print(chart.symbol + ": high " + to_string(yesterday.high) + ", low " + to_string(yesterday.low) + ", close " + to_string(yesterday.close));
    return Ok(());
}