Skip to content

Custom studies

Use a CustomIndicator when your host can calculate a complete output from the current bars. Its Send + Sync closure receives the full oldest-first slice and returns exactly one IndicatorValue per input slot. The chart calls it again when that input changes. This extension point is available without the built-in studies feature.

The scene calculates close now minus close ten slots earlier, in price units. It uses the first chart’s sample bars, with slots 35 through 38 replaced by Bar::whitespace. The first ten outputs are missing because there is no earlier close. Outputs are also missing at slots 35 through 38 and 45 through 48, where either endpoint of the subtraction is unavailable. Slot numbers start at zero.

Ten-slot price change with gapsOpen full size

Inspect the blank warm-up on the left and the two breaks in the lower study line.

The interactive example needs JavaScript and WebAssembly. You can still use the chart setup alongside it.

custom_study.rs
use egui_charts::Chart;
pub fn custom() -> Chart {
use eframe::egui::Color32;
use egui_charts::{
model::Bar,
studies::{CustomIndicator, IndicatorValue, PlotType},
widget::SeriesId,
};
let mut chart = crate::first_chart::first_chart();
chart.config_mut().show_volume = false;
let mut bars = chart.bars(SeriesId::MAIN).to_vec();
for bar in &mut bars[35..39] {
*bar = Bar::whitespace(bar.time);
}
chart.set_data(SeriesId::MAIN, bars);
chart.set_visible_bars(120);
chart.add_indicator(Box::new(
CustomIndicator::new("Price change (10)", |bars| {
bars.iter()
.enumerate()
.map(|(i, bar)| {
i.checked_sub(10)
.filter(|&before| !bar.is_whitespace() && !bars[before].is_whitespace())
.map_or(IndicatorValue::None, |before| {
IndicatorValue::Single(bar.close - bars[before].close)
})
})
.collect()
})
.with_overlay(false)
.with_plot_types(vec![PlotType::LineWithBreaks])
.with_color(Color32::from_rgb(126, 87, 194))
.with_description("Close minus the close ten slots earlier; gaps stay missing"),
));
chart
}

Return IndicatorValue::None for a missing result. Removing it from the vector would shift every later value to the wrong time. Returning Single(0.0) would draw a real zero and affect the pane’s scale. The scene draws its output as a line with breaks, so each missing value leaves a gap; a plain line, the default, joins the values on either side.

Only the two endpoints matter for this definition. A missing intermediate slot does not suppress an otherwise valid subtraction. For example, the sample’s slot 10 closes at 101.8 and slot 0 at 99.4, giving 101.8 - 99.4 = 2.4. This is an absolute change, so it does not divide by the earlier close.

with_overlay(false) creates the lower pane. The default is a price overlay; use that for values expressed on the price scale. The pane guide covers moving and resizing a study after it has been added.

Create this chart once and call show each frame. Feed changes through the series API or a datafeed; the chart recalculates the study, including after older history is prepended. A closure recalculates the full slice. For large or frequently changing histories, implement Indicator::update to recompute from the first changed bar, using Stepper where appropriate.

For inputs, plot styles, additional symbols and other intervals, see the study contracts and additional study data.