Skip to main content

Interpreter

Struct Interpreter 

Source
pub struct Interpreter<O: PineOutput> {
    pub output: O,
    pub user_series_history: HashMap<String, Vec<Value<O>>>,
    pub broker: Option<Box<dyn Broker>>,
    pub request_provider: Option<Rc<dyn DataProvider>>,
    pub chart_period: Option<i64>,
    /* private fields */
}
Expand description

The interpreter executes a program with a given bar

Fields§

§output: O

Output storage for plots, labels, logs, etc.

§user_series_history: HashMap<String, Vec<Value<O>>>

Per-variable history for user-computed series (var declarations). history[len-1] = previous bar, history[len-2] = two bars ago, etc. Populated on each Stmt::Assignment; supports Pine’s name[n] lookback.

§broker: Option<Box<dyn Broker>>

The simulated broker a strategy script trades against. None for an indicator. The strategy.* order builtins reach it through ctx.

§request_provider: Option<Rc<dyn DataProvider>>

The feed request.security draws other symbols/timeframes from, and the chart’s bar spacing in ms (for request.security_lower_tf). None when no host provider is set. The request.* builtins reach these through ctx, the same way strategy.* reaches the broker.

§chart_period: Option<i64>

Implementations§

Source§

impl<O: PineOutput> Interpreter<O>

Source

pub fn new() -> Self

Source

pub fn bar_seq(&self) -> u64

How many bars have been executed. A stateful builtin advances its state when this differs from the value it last saw.

Source

pub fn set_library_loader(&mut self, library_loader: Box<dyn LibraryLoader>)

Set the library loader

Source

pub fn snapshot(&self) -> HashMap<String, Value<O>>

A copy of every defined variable’s value, so a secondary interpreter (request.security) can start from the same namespaces and builtins without re-registering them.

Source

pub fn exports(&self) -> &HashMap<String, Value<O>>

Get the exported items from this interpreter (for library mode)

Source

pub fn execute(&mut self, program: &Program) -> Result<O, RuntimeError>

Execute a program with a single bar

Source

pub fn get_variable(&self, name: &str) -> Option<&Value<O>>

Get a variable value

Source

pub fn set_variable(&mut self, name: &str, value: Value<O>)

Set a variable value (useful for loading objects and test setup)

Source

pub fn advance_series(&mut self, name: &str, value: Value<O>)

Set a built-in series to its value for a new bar, keeping the outgoing value reachable as name[1].

This is how the OHLCV series and their derivations get the same lookback as any user variable: history accumulates as bars execute, so close[1] is na until a second bar has run.

Source

pub fn set_object_field(&mut self, object: &str, field: &str, value: Value<O>)

Set a field on a namespace object (e.g. strategy.position_size), leaving the object’s other fields untouched. A no-op if object is not a registered namespace object.

Source

pub fn set_const_variable(&mut self, name: &str, value: Value<O>)

Set a const variable (cannot be reassigned)

Trait Implementations§

Source§

impl<O: PineOutput> Default for Interpreter<O>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<O> !RefUnwindSafe for Interpreter<O>

§

impl<O> !Send for Interpreter<O>

§

impl<O> !Sync for Interpreter<O>

§

impl<O> !UnwindSafe for Interpreter<O>

§

impl<O> Freeze for Interpreter<O>
where O: Freeze,

§

impl<O> Unpin for Interpreter<O>
where O: Unpin,

§

impl<O> UnsafeUnpin for Interpreter<O>
where O: UnsafeUnpin,

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, 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.