Skip to main content

RunHandle

Struct RunHandle 

Source
pub struct RunHandle { /* private fields */ }
Expand description

A handle to an active execution run returned by Instance::run.

Call next_event to iterate events one at a time. The handle implements Deref and DerefMut targeting Instance, so all instance methods — including save_state — are available directly through the handle while the run is in progress.

§Example

let mut handle = instance.run("NASDAQ:AAPL", TimeFrame::days(1));
while let Some(result) = handle.next_event().await {
    let event = result?;
    if matches!(event, Event::BarEnd) && handle.current_bar_confirmed() {
        let snapshot = handle.save_state()?;
        persist_snapshot(snapshot);
    }
}
let instance = handle.into_instance();

Implementations§

Source§

impl RunHandle

Source

pub fn into_instance(self) -> Instance

Consumes the handle and returns the instance.

Source

pub async fn next_event(&mut self) -> Option<Result<Event, Error>>

Returns the next event from the run, or None when execution is complete.

Methods from Deref<Target = Instance>§

Source

pub fn program(&self) -> &Program

Returns the compiled program held by this instance.

The Program is reference-counted (Arc internally) so cloning it is cheap. Used by providers to cache compiled programs across stream restarts.

Source

pub fn reset(&mut self)

Resets the instance to its post-build state, as if no bars had been executed.

All runtime state — variable series, chart visuals, events, strategy positions, security sub-states, and bar counters — is discarded and re-initialised from scratch. The compiled program, symbol/timeframe configuration, data provider, execution limits, and output mode are preserved unchanged.

Note: Input values that were overridden via InstanceBuilder::with_input_value are not preserved across a reset; inputs revert to their script-default values.

§Examples
let mut instance = instance
    .run_to_end("NASDAQ:AAPL", TimeFrame::days(1))
    .await?;
instance.reset();
// instance is now clean and ready to re-run
Source

pub fn warnings(&self) -> Warnings

Returns the warnings generated during compilation, bundled with source files so the caller can render them via Warnings::display().

Source

pub fn gc_collect_all(&mut self)

Runs a full garbage collection cycle on the GC arena.

Normally the VM collects incrementally during execution. Call this after a batch of bars to reclaim all unreachable GC objects at once.

Source

pub fn symbol_info(&self) -> &SymbolInfo

Returns a reference to the chart containing the visuals.

The [Chart] holds all plots, lines, labels, boxes, and other visuals produced by the script. Query it after calling execute().

§Examples
let chart = instance.chart();

// Iterate over per-bar series graphs (plot, bgcolor, fill, etc.)
for (id, series_graph) in chart.series_graphs() {
    if let Some(plot) = series_graph.as_plot() {
        println!("Plot '{:?}': {} bars", plot.title, plot.series.len());
    }
}

// Iterate over non-series graphs (label, line, box, table, etc.)
for (id, graph) in chart.graphs() {
    if let Some(label) = graph.as_label() {
        println!("Label at ({}, {}): {:?}", label.x, label.y, label.text);
    }
}

Returns the symbol info for the current chart symbol.

Source

pub fn chart(&self) -> &Chart

Returns the chart state produced by this instance.

Source

pub fn script_info(&self) -> &ScriptInfo

Returns a reference to the script info (script type, inputs, etc.).

Source

pub fn total_bars(&self) -> usize

Returns the total number of bars processed since execution started, including bars that have been truncated from memory.

Source

pub fn current_bar_confirmed(&self) -> bool

Returns whether the current bar has been confirmed.

Historical bars are always confirmed immediately after execution. Realtime bars become confirmed only after RealtimeConfirmed runs, either explicitly at stream end or automatically when a later realtime timestamp arrives.

Source

pub fn strategy_report(&self) -> Option<StrategyReport>

Returns the strategy backtest report, or None if the script is not a strategy.

Source

pub fn save_state(&mut self) -> Result<Vec<u8>, SnapshotError>

Serializes the current VM state to a binary blob.

The returned bytes encode the full program, all variables, inputs, GC-heap objects, chart, strategy state, and configuration. Pass them to Instance::restore_state to resume execution.

The current realtime bar must be confirmed before calling this method (e.g. by executing ExecuteMode::Confirm). Returns SnapshotError::UnconfirmedBar otherwise.

Candlestick data is not included in the snapshot. When run() is called on a restored instance, the VM automatically passes last_bar_time + 1 to the data provider.

§Errors

Returns SnapshotError::UnconfirmedBar if the current bar has not been confirmed, or SnapshotError::Encode if postcard serialization fails.

Trait Implementations§

Source§

impl Deref for RunHandle

Source§

type Target = Instance

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Instance

Dereferences the value.
Source§

impl DerefMut for RunHandle

Source§

fn deref_mut(&mut self) -> &mut Instance

Mutably dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V