openpine_vm/script_info/
script_type.rs

1use num_enum::FromPrimitive;
2use openpine_macros::Enum;
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    Currency,
7    strategy::{CloseEntriesRule, CommissionType, QuantityType},
8};
9
10/// The type of script.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub enum ScriptType {
13    /// An indicator script.
14    Indicator(Indicator),
15    /// A library script.
16    Library(Library),
17    /// A strategy script.
18    Strategy(Strategy),
19}
20
21/// Display formatting for script outputs.
22#[derive(Debug, Copy, Clone, Eq, PartialEq, Enum, Serialize, Deserialize)]
23pub enum Format {
24    /// Use inherited/default formatting.
25    Inherit,
26    /// Format values as prices.
27    Price,
28    /// Format values as volumes.
29    Volume,
30    /// Format values as percentages.
31    Percent,
32    /// Format values using the symbol's min tick.
33    #[openpine(name = "Mintick")]
34    MinTick,
35}
36
37/// Which price scale to use when displaying an indicator/strategy.
38#[derive(Debug, Copy, Clone, Eq, PartialEq, Default, FromPrimitive, Serialize, Deserialize)]
39#[repr(i32)]
40pub enum ScaleType {
41    /// Do not show a dedicated scale.
42    #[default]
43    None = 0,
44    /// Show on the left scale.
45    Left = 1,
46    /// Show on the right scale.
47    Right = 2,
48}
49
50/// Information about an indicator script.
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct Indicator {
53    /// Script title.
54    pub title: String,
55    /// Optional short title.
56    pub short_title: Option<String>,
57    /// Whether to draw on the price chart.
58    pub overlay: bool,
59    /// Display formatting.
60    pub format: Format,
61    /// Optional decimal precision.
62    pub precision: Option<u8>,
63    /// Which price scale to use.
64    pub scale: ScaleType,
65    /// Maximum lookback in bars.
66    pub max_bars_back: Option<usize>,
67    /// Fixed script timeframe, if any.
68    pub timeframe: Option<String>,
69    /// Whether to show gaps for the selected timeframe.
70    pub timeframe_gaps: bool,
71    /// Whether plots specify explicit z-order.
72    pub explicit_plot_zorder: bool,
73    /// Maximum number of line objects.
74    pub max_lines_count: usize,
75    /// Maximum number of label objects.
76    pub max_labels_count: usize,
77    /// Maximum number of box objects.
78    pub max_boxes_count: usize,
79    /// Optional number of bars to calculate.
80    pub calc_bars_count: Option<usize>,
81    /// Maximum number of polyline objects.
82    pub max_polylines_count: usize,
83    /// Whether dynamic requests are enabled.
84    pub dynamic_requests: bool,
85    /// Whether to draw behind the chart.
86    pub behind_chart: bool,
87}
88
89impl ScriptType {
90    /// Maximum number of line objects allowed.
91    pub(crate) fn max_lines_count(&self) -> usize {
92        match self {
93            Self::Indicator(i) => i.max_lines_count,
94            Self::Strategy(s) => s.max_lines_count,
95            Self::Library(_) => unreachable!("library scripts are not executable"),
96        }
97    }
98
99    /// Maximum number of label objects allowed.
100    pub(crate) fn max_labels_count(&self) -> usize {
101        match self {
102            Self::Indicator(i) => i.max_labels_count,
103            Self::Strategy(s) => s.max_labels_count,
104            Self::Library(_) => unreachable!("library scripts are not executable"),
105        }
106    }
107
108    /// Maximum number of box objects allowed.
109    pub(crate) fn max_boxes_count(&self) -> usize {
110        match self {
111            Self::Indicator(i) => i.max_boxes_count,
112            Self::Strategy(s) => s.max_boxes_count,
113            Self::Library(_) => unreachable!("library scripts are not executable"),
114        }
115    }
116
117    /// Maximum number of polyline objects allowed.
118    pub(crate) fn max_polylines_count(&self) -> usize {
119        match self {
120            Self::Indicator(i) => i.max_polylines_count,
121            Self::Strategy(s) => s.max_polylines_count,
122            Self::Library(_) => unreachable!("library scripts are not executable"),
123        }
124    }
125}
126
127/// Information about a library script.
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct Library {
130    /// Script title.
131    pub title: String,
132    /// Whether to draw on the price chart.
133    pub overlay: bool,
134    /// Whether dynamic requests are enabled.
135    pub dynamic_requests: bool,
136}
137
138/// Information about a strategy script.
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct Strategy {
141    /// Script title.
142    pub title: String,
143    /// Optional short title.
144    pub short_title: Option<String>,
145    /// Whether to draw on the price chart.
146    pub overlay: bool,
147    /// Display formatting.
148    pub format: Format,
149    /// Optional decimal precision.
150    pub precision: Option<u8>,
151    /// Which price scale to use.
152    pub scale: ScaleType,
153    /// Maximum number of concurrent entries.
154    pub pyramiding: u32,
155    /// Recalculate on order fills.
156    pub calc_on_order_fills: bool,
157    /// Recalculate on every tick.
158    pub calc_on_every_tick: bool,
159    /// Maximum lookback in bars.
160    pub max_bars_back: Option<usize>,
161    /// Backtest fill-limits assumption.
162    pub backtest_fill_limits_assumption: u32,
163    /// Default quantity type.
164    pub default_qty_type: QuantityType,
165    /// Default quantity value.
166    pub default_qty_value: f64,
167    /// Initial capital for backtests.
168    pub initial_capital: f64,
169    /// Account currency.
170    pub currency: Currency,
171    /// Slippage in ticks.
172    pub slippage: u32,
173    /// Commission calculation type.
174    pub commission_type: CommissionType,
175    /// Commission value for the selected type.
176    pub commission_value: f64,
177    /// Whether to process orders on close.
178    pub process_orders_on_close: bool,
179    /// Close entries rule.
180    pub close_entries_rule: CloseEntriesRule,
181    /// Long margin requirement.
182    pub margin_long: f64,
183    /// Short margin requirement.
184    pub margin_short: f64,
185    /// Whether plots specify explicit z-order.
186    pub explicit_plot_zorder: bool,
187    /// Maximum number of line objects.
188    pub max_lines_count: usize,
189    /// Maximum number of label objects.
190    pub max_labels_count: usize,
191    /// Maximum number of box objects.
192    pub max_boxes_count: usize,
193    /// Optional number of bars to calculate.
194    pub calc_bars_count: Option<usize>,
195    /// Risk-free rate used in performance metrics.
196    pub risk_free_rate: f64,
197    /// Whether to use bar magnifier.
198    pub use_bar_magnifier: bool,
199    /// Whether to fill orders on standard OHLC.
200    pub fill_orders_on_standard_ohlc: bool,
201    /// Maximum number of polyline objects.
202    pub max_polylines_count: usize,
203    /// Whether dynamic requests are enabled.
204    pub dynamic_requests: bool,
205    /// Whether to draw behind the chart.
206    pub behind_chart: bool,
207}