pub struct Instance { /* private fields */ }Expand description
An instance used to execute a compiled program.
Implementations§
Source§impl Instance
impl Instance
Sourcepub fn warnings(&self) -> &[CompileError]
pub fn warnings(&self) -> &[CompileError]
Returns the warnings generated during compilation.
Sourcepub fn run(
&mut self,
symbol: &str,
timeframe: TimeFrame,
) -> impl Stream<Item = Result<Event, Error>> + '_
pub fn run( &mut self, symbol: &str, timeframe: TimeFrame, ) -> impl Stream<Item = Result<Event, Error>> + '_
Runs the script against the full candlestick stream supplied by the
injected DataProvider, starting from the
beginning of available data.
This is equivalent to calling run_from with
from_time = 0.
CandlestickItem::Confirmed— advancesbar_index.CandlestickItem::Realtime— same-timestamp ticks becomeRealtimeUpdateautomatically; confirmed at stream end.CandlestickItem::HistoryEnd— skipped (internal boundary marker).
Returns immediately with Ok(()) if no
DataProvider was passed to
Instance::builder.
Returns a stream of events produced by running the script against the
candlestick stream from the injected
DataProvider.
Equivalent to run_from with from_time = 0.
Sourcepub fn run_from(
&mut self,
symbol: &str,
timeframe: TimeFrame,
from_time: i64,
) -> impl Stream<Item = Result<Event, Error>> + '_
pub fn run_from( &mut self, symbol: &str, timeframe: TimeFrame, from_time: i64, ) -> impl Stream<Item = Result<Event, Error>> + '_
Returns a stream of events produced by running the script, starting
at from_time.
Each bar’s execution yields Event::BarStart, any log/alert/draw
events, then Event::BarEnd. The stream yields
Event::HistoryEnd when the boundary marker is received.
Sourcepub async fn run_to_end(
&mut self,
symbol: &str,
timeframe: TimeFrame,
) -> Result<(), Error>
pub async fn run_to_end( &mut self, symbol: &str, timeframe: TimeFrame, ) -> Result<(), Error>
Sourcepub async fn run_from_to_end(
&mut self,
symbol: &str,
timeframe: TimeFrame,
from_time: i64,
) -> Result<(), Error>
pub async fn run_from_to_end( &mut self, symbol: &str, timeframe: TimeFrame, from_time: i64, ) -> Result<(), Error>
Runs the script to completion starting at from_time, discarding all
events.
Sourcepub fn chart(&self) -> &Chart
pub fn chart(&self) -> &Chart
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);
}
}Sourcepub fn script_info(&self) -> &ScriptInfo
pub fn script_info(&self) -> &ScriptInfo
Returns a reference to the script info (script type, inputs, etc.).
Sourcepub fn candlesticks(&self) -> &Series<Candlestick>
pub fn candlesticks(&self) -> &Series<Candlestick>
Returns a reference to the candlestick series accumulated during execution.
Sourcepub fn strategy_report(&self) -> Option<StrategyReport>
pub fn strategy_report(&self) -> Option<StrategyReport>
Returns the strategy backtest report, or None if the script is
not a strategy.
Sourcepub fn into_chart(self) -> Chart
pub fn into_chart(self) -> Chart
Consumes the instance and returns the chart data.
This avoids cloning the chart when the instance is no longer needed (e.g. in a WASM playground that only needs the chart for rendering).
Source§impl Instance
impl Instance
Sourcepub fn builder<'a, P>(
provider: P,
source: &'a str,
timeframe: TimeFrame,
symbol: impl Into<String>,
) -> InstanceBuilder<'a, ()>where
P: DataProvider + 'static,
pub fn builder<'a, P>(
provider: P,
source: &'a str,
timeframe: TimeFrame,
symbol: impl Into<String>,
) -> InstanceBuilder<'a, ()>where
P: DataProvider + 'static,
Creates a new InstanceBuilder for the given provider, source,
timeframe and symbol string.
provider supplies the main chart K-lines consumed by
Instance::run() and the data for any request.security() calls.
For simple single-symbol backtests pass a Vec<Candlestick> directly;
for multi-symbol or live data implement DataProvider directly.
§Examples
use openpine_vm::{Candlestick, Instance, TimeFrame, TradeSession};
let source = r#"
//@version=6
indicator("My Script")
plot(close)
"#;
let bar = Candlestick::new(
1_700_000_000_000,
150.0,
155.0,
149.0,
153.0,
1_000_000.0,
0.0,
TradeSession::Regular,
);
let provider = vec![bar];
let mut instance = Instance::builder(provider, source, TimeFrame::days(1), "NASDAQ:AAPL")
.with_path("my_script.pine")
.build()
.await?;
instance
.run_to_end("NASDAQ:AAPL", TimeFrame::days(1))
.await?;
let chart = instance.chart();
for (id, graph) in chart.series_graphs() {
println!("Graph {:?}: {:?}", id, graph);
}Sourcepub fn builder_from_project<'a, P>(
provider: P,
project: &'a Project,
timeframe: TimeFrame,
symbol: impl Into<String>,
) -> InstanceBuilder<'a, ()>where
P: DataProvider + 'static,
pub fn builder_from_project<'a, P>(
provider: P,
project: &'a Project,
timeframe: TimeFrame,
symbol: impl Into<String>,
) -> InstanceBuilder<'a, ()>where
P: DataProvider + 'static,
Creates a new InstanceBuilder from an already-loaded [Project].
Skips the parse + load phase, going straight to optimize + compile
when InstanceBuilder::build() is called. This is useful when the
caller has already loaded the project (e.g. to share a cached
LoadResult between
compilation and LSP analysis).
Source§impl Instance
impl Instance
Sourcepub fn restore_state(data: &[u8]) -> Result<RestoreBuilder, SnapshotError>
pub fn restore_state(data: &[u8]) -> Result<RestoreBuilder, SnapshotError>
Deserializes a snapshot and returns a RestoreBuilder for
configuring optional parameters before completing the restore.
§Errors
Returns SnapshotError::Decode if decoding fails, or
SnapshotError::VersionMismatch if the snapshot version does not
match the current build.
Source§impl Instance
impl Instance
Sourcepub fn save_state(&mut self) -> Result<Vec<u8>, SnapshotError>
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.
§Errors
Returns SnapshotError::Encode if bincode serialization fails.
Auto Trait Implementations§
impl Freeze for Instance
impl !RefUnwindSafe for Instance
impl !Send for Instance
impl !Sync for Instance
impl Unpin for Instance
impl !UnwindSafe for Instance
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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§impl<T> Pointable for T
impl<T> Pointable for T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.