Skip to content
← All examples

Example 30

Prior day levels

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

Category
Workspace: reads or changes charts and panels
Permissions
market.readworkspace.readworkspace.write
Before you run it
a terminal window with a chart open
What it does
DRAWS three horizontal lines on the active chart, named "prior_day". Running it again replaces them.
How to run it
grant market.read, workspace.read and workspace.write in the T0 Editor, Run.
Language
>=0.1.1, <0.2.0
Host
>=0.1.6, <0.2.0
Needs
market >=0.1.2, <0.2.0workspace >=0.1.0, <0.2.0plot >=0.1.0, <0.2.0

Source

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