Skip to main content

navi_vm\native_funcs/
impls.rs

1mod alert;
2mod alert_condition;
3mod array_abs;
4mod array_avg;
5mod array_binary_search;
6mod array_binary_search_leftmost;
7mod array_binary_search_rightmost;
8mod array_clear;
9mod array_concat;
10mod array_copy;
11mod array_covariance;
12mod array_every;
13mod array_fill;
14mod array_get;
15mod array_includes;
16mod array_indexof;
17mod array_insert;
18mod array_join;
19mod array_lastindexof;
20mod array_max;
21mod array_median;
22mod array_min;
23mod array_mode;
24mod array_new;
25mod array_percentile_linear_interpolation;
26mod array_percentile_nearest_rank;
27mod array_percentrank;
28mod array_pop;
29mod array_push;
30mod array_remove;
31mod array_reverse;
32mod array_set;
33mod array_shift;
34mod array_size;
35mod array_slice;
36mod array_some;
37mod array_sort_indices_number;
38mod array_sort_indices_string;
39mod array_sort_number;
40mod array_sort_string;
41mod array_standardize;
42mod array_stdev;
43mod array_sum;
44mod array_unshift;
45mod array_variance;
46mod bar_color;
47mod bar_index;
48mod bar_state;
49mod bg_color;
50mod bitflags_add;
51mod bitflags_remove;
52mod box_all;
53mod box_copy;
54mod box_delete;
55mod box_get_attr;
56mod box_new;
57mod box_set_attr;
58mod candlestick;
59mod chart_bgcolor;
60mod chart_fgcolor;
61mod chart_is_standard;
62mod color_component;
63mod color_from_gradient;
64mod color_new;
65mod color_rgb;
66mod fill_gradient;
67mod fill_solid_hline;
68mod fill_solid_plot;
69mod hline;
70mod indicator;
71mod input_bool;
72mod input_color;
73mod input_enum;
74mod input_float;
75mod input_int;
76mod input_price;
77mod input_session;
78mod input_source;
79mod input_string;
80mod input_symbol;
81mod input_textarea;
82mod input_time;
83mod input_timeframe;
84mod is_na;
85mod label_all;
86mod label_copy;
87mod label_delete;
88mod label_get_attr;
89mod label_new;
90mod label_set_attr;
91mod last_info;
92mod library;
93mod line_all;
94mod line_copy;
95mod line_delete;
96mod line_get_attr;
97mod line_get_price;
98mod line_new;
99mod line_set_attr;
100mod linefill_all;
101mod linefill_delete;
102mod linefill_get_attr;
103mod linefill_new;
104mod linefill_set_color;
105mod log;
106mod map_clear;
107mod map_contains;
108mod map_copy;
109mod map_get;
110mod map_keys;
111mod map_new;
112mod map_put;
113mod map_put_all;
114mod map_remove;
115mod map_size;
116mod map_values;
117mod math_abs;
118mod math_acos;
119mod math_asin;
120mod math_atan;
121mod math_ceil;
122mod math_cos;
123mod math_exp;
124mod math_floor;
125mod math_log;
126mod math_log10;
127mod math_pow;
128mod math_random;
129mod math_round;
130mod math_sign;
131mod math_sin;
132mod math_sqrt;
133mod math_tan;
134mod matrix_add_col;
135mod matrix_add_row;
136mod matrix_avg;
137mod matrix_col;
138mod matrix_columns;
139mod matrix_concat;
140mod matrix_copy;
141mod matrix_det;
142mod matrix_diff;
143mod matrix_eigenvalues;
144mod matrix_eigenvectors;
145mod matrix_fill;
146mod matrix_get;
147mod matrix_inv;
148mod matrix_is_antidiagonal;
149mod matrix_is_antisymmetric;
150mod matrix_is_binary;
151mod matrix_is_diagonal;
152mod matrix_is_identity;
153mod matrix_is_stochastic;
154mod matrix_is_symmetric;
155mod matrix_is_triangular;
156mod matrix_is_zero;
157mod matrix_kron;
158mod matrix_max;
159mod matrix_median;
160mod matrix_min;
161mod matrix_mode;
162mod matrix_multi;
163mod matrix_new;
164mod matrix_pinv;
165mod matrix_pow;
166mod matrix_rank;
167mod matrix_remove_col;
168mod matrix_remove_row;
169mod matrix_reshape;
170mod matrix_reverse;
171mod matrix_row;
172mod matrix_rows;
173mod matrix_set;
174mod matrix_submatrix;
175mod matrix_sum;
176mod matrix_swap_columns;
177mod matrix_swap_rows;
178mod matrix_trace;
179mod matrix_transpose;
180mod matrx_sort_number;
181mod matrx_sort_string;
182mod max_bars_back;
183mod plot;
184mod plot_arrow;
185mod plot_bar;
186mod plot_candle;
187mod plot_char;
188mod plot_shape;
189mod polyline_all;
190mod polyline_delete;
191mod polyline_new;
192mod request_currency_rate;
193mod request_data;
194mod request_dividends;
195mod request_earnings;
196mod request_economic;
197mod request_financial;
198mod request_security;
199mod request_security_lower_tf;
200mod request_splits;
201mod runtime_error;
202mod runtime_error_i18n;
203mod session;
204mod str_contains;
205mod str_endswith;
206mod str_format;
207mod str_format_time;
208mod str_length;
209mod str_lower;
210mod str_match;
211mod str_pos;
212mod str_repeat;
213mod str_replace;
214mod str_replace_all;
215mod str_split;
216mod str_startswith;
217mod str_substring;
218mod str_tonumber;
219mod str_tostring;
220mod str_tostring_format;
221mod str_trim;
222mod str_upper;
223mod strategy;
224mod strategy_account_currency;
225mod strategy_cancel;
226mod strategy_cancel_all;
227mod strategy_close;
228mod strategy_close_all;
229mod strategy_closedtrades;
230mod strategy_closedtrades_count;
231mod strategy_closedtrades_first_index;
232mod strategy_convert_to_account;
233mod strategy_convert_to_symbol;
234mod strategy_default_entry_qty;
235mod strategy_entry;
236mod strategy_exit;
237mod strategy_get_attr;
238mod strategy_opentrades;
239mod strategy_opentrades_capital_held;
240mod strategy_opentrades_count;
241mod strategy_order;
242mod strategy_position_entry_name;
243mod strategy_risk_allow_entry_in;
244mod strategy_risk_max_cons_loss_days;
245mod strategy_risk_max_drawdown;
246mod strategy_risk_max_intraday_filled_orders;
247mod strategy_risk_max_intraday_loss;
248mod strategy_risk_max_position_size;
249mod syminfo;
250mod syminfo_prefix;
251mod syminfo_ticker;
252mod table_all;
253mod table_cell;
254mod table_cell_set_attr;
255mod table_clear;
256mod table_delete;
257mod table_merge_cells;
258mod table_new;
259mod table_set_attr;
260mod time;
261mod time_close;
262pub(crate) mod time_common;
263mod time_component;
264mod time_now;
265mod time_tradingday;
266mod timeframe_from_seconds;
267mod timeframe_in_seconds;
268mod timeframe_is;
269mod timeframe_main_period;
270mod timeframe_multiplier;
271mod timeframe_period;
272mod timeframe_round;
273mod timestamp_from_components;
274mod timestamp_parse;
275
276use navi_visitor::{NativeFunctionInfo, NativeFunctionRegistry};
277
278use crate::native_funcs::{NativeFunction, NativeFunctionItem};
279
280macro_rules! native_functions {
281    // Base case: done accumulating.
282    (@acc [$($e:expr,)*]) => { &[$($e,)*] };
283    // async entry
284    (@acc [$($e:expr,)*] async $m:ident, $($rest:tt)*) => {
285        native_functions!(@acc [$($e,)* NativeFunctionItem {
286            name: stringify!($m),
287            num_generic_arguments: $m::$m::NUM_GENERIC_ARGUMENTS,
288            num_arguments: $m::$m::NUM_ARGUMENTS,
289            has_result: $m::$m::HAS_RESULT,
290            call_fn: $m::$m::execute,
291            is_async: true,
292        },] $($rest)*)
293    };
294    // sync entry
295    (@acc [$($e:expr,)*] $m:ident, $($rest:tt)*) => {
296        native_functions!(@acc [$($e,)* NativeFunctionItem {
297            name: stringify!($m),
298            num_generic_arguments: $m::$m::NUM_GENERIC_ARGUMENTS,
299            num_arguments: $m::$m::NUM_ARGUMENTS,
300            has_result: $m::$m::HAS_RESULT,
301            call_fn: $m::$m::execute,
302            is_async: false,
303        },] $($rest)*)
304    };
305    ($($rest:tt)*) => { native_functions!(@acc [] $($rest)*) };
306}
307
308pub(crate) const NATIVE_FUNCTIONS: &[NativeFunctionItem] = native_functions!(
309    alert,
310    alert_condition,
311    bar_color,
312    bg_color,
313    fill_solid_hline,
314    fill_solid_plot,
315    fill_gradient,
316    hline,
317    plot_arrow,
318    plot_bar,
319    plot_candle,
320    plot_char,
321    plot_shape,
322    indicator,
323    library,
324    strategy,
325    log,
326    last_info,
327    bar_index,
328    bar_state,
329    max_bars_back,
330    array_new,
331    array_get,
332    array_set,
333    array_push,
334    array_size,
335    array_clear,
336    array_insert,
337    array_max,
338    array_min,
339    array_mode,
340    array_percentile_linear_interpolation,
341    array_percentile_nearest_rank,
342    array_percentrank,
343    array_pop,
344    array_median,
345    array_remove,
346    array_reverse,
347    array_shift,
348    array_abs,
349    array_avg,
350    array_concat,
351    array_copy,
352    array_every,
353    array_includes,
354    array_indexof,
355    array_lastindexof,
356    array_fill,
357    array_covariance,
358    array_slice,
359    array_join,
360    array_some,
361    array_sort_number,
362    array_sort_string,
363    array_sort_indices_number,
364    array_sort_indices_string,
365    array_standardize,
366    array_stdev,
367    array_sum,
368    array_unshift,
369    array_variance,
370    array_binary_search,
371    array_binary_search_leftmost,
372    array_binary_search_rightmost,
373    map_new,
374    map_clear,
375    map_get,
376    map_put,
377    map_remove,
378    map_contains,
379    map_size,
380    map_keys,
381    map_values,
382    map_put_all,
383    map_copy,
384    math_abs,
385    math_cos,
386    math_exp,
387    math_log,
388    math_sin,
389    math_tan,
390    math_acos,
391    math_asin,
392    math_atan,
393    math_ceil,
394    math_sqrt,
395    math_floor,
396    math_log10,
397    math_round,
398    math_pow,
399    math_sign,
400    str_tostring,
401    str_tostring_format,
402    str_format,
403    str_contains,
404    str_endswith,
405    str_length,
406    str_lower,
407    str_upper,
408    str_match,
409    str_pos,
410    str_repeat,
411    str_replace,
412    str_replace_all,
413    str_split,
414    str_startswith,
415    str_substring,
416    str_trim,
417    str_tonumber,
418    str_format_time,
419    plot,
420    input_int,
421    input_float,
422    input_bool,
423    input_color,
424    input_string,
425    input_price,
426    input_symbol,
427    input_timeframe,
428    input_source,
429    input_enum,
430    input_session,
431    input_time,
432    input_textarea,
433    is_na,
434    candlestick,
435    time_now,
436    timestamp_parse,
437    timestamp_from_components,
438    time_component,
439    time,
440    time_close,
441    time_tradingday,
442    timeframe_is,
443    timeframe_main_period,
444    timeframe_multiplier,
445    timeframe_period,
446    timeframe_in_seconds,
447    timeframe_from_seconds,
448    timeframe_round,
449    color_component,
450    color_from_gradient,
451    color_new,
452    color_rgb,
453    matrix_new,
454    matrix_add_col,
455    matrix_add_row,
456    matrix_columns,
457    matrix_rows,
458    matrix_avg,
459    matrix_col,
460    matrix_row,
461    matrix_concat,
462    matrix_copy,
463    matrix_det,
464    matrix_diff,
465    matrix_eigenvalues,
466    matrix_eigenvectors,
467    matrix_fill,
468    matrix_get,
469    matrix_inv,
470    matrix_pinv,
471    matrix_is_antidiagonal,
472    matrix_is_antisymmetric,
473    matrix_is_binary,
474    matrix_is_diagonal,
475    matrix_is_identity,
476    matrix_is_stochastic,
477    matrix_is_symmetric,
478    matrix_is_triangular,
479    matrix_is_zero,
480    matrix_kron,
481    matrix_max,
482    matrix_min,
483    matrix_median,
484    matrix_mode,
485    matrix_multi,
486    matrix_pow,
487    matrix_rank,
488    matrix_remove_col,
489    matrix_remove_row,
490    matrix_reshape,
491    matrix_reverse,
492    matrix_set,
493    matrx_sort_number,
494    matrx_sort_string,
495    matrix_submatrix,
496    matrix_sum,
497    matrix_swap_columns,
498    matrix_swap_rows,
499    matrix_trace,
500    matrix_transpose,
501    bitflags_add,
502    bitflags_remove,
503    syminfo,
504    syminfo_ticker,
505    syminfo_prefix,
506    session,
507    label_new,
508    label_delete,
509    label_get_attr,
510    label_set_attr,
511    label_copy,
512    label_all,
513    line_new,
514    line_all,
515    line_copy,
516    line_delete,
517    line_get_attr,
518    line_get_price,
519    line_set_attr,
520    linefill_new,
521    linefill_delete,
522    linefill_get_attr,
523    linefill_set_color,
524    linefill_all,
525    box_new,
526    box_all,
527    box_delete,
528    box_copy,
529    box_get_attr,
530    box_set_attr,
531    polyline_new,
532    polyline_delete,
533    polyline_all,
534    table_all,
535    table_new,
536    table_cell,
537    table_set_attr,
538    table_cell_set_attr,
539    table_delete,
540    table_clear,
541    table_merge_cells,
542    async request_currency_rate,
543    async request_data,
544    async request_dividends,
545    async request_earnings,
546    async request_economic,
547    async request_financial,
548    async request_security,
549    async request_security_lower_tf,
550    async request_splits,
551    runtime_error,
552    runtime_error_i18n,
553    chart_bgcolor,
554    chart_fgcolor,
555    chart_is_standard,
556    strategy_entry,
557    strategy_exit,
558    strategy_order,
559    strategy_cancel,
560    strategy_cancel_all,
561    strategy_close,
562    strategy_close_all,
563    strategy_closedtrades,
564    strategy_closedtrades_count,
565    strategy_closedtrades_first_index,
566    strategy_opentrades,
567    strategy_opentrades_count,
568    strategy_opentrades_capital_held,
569    strategy_risk_allow_entry_in,
570    strategy_risk_max_drawdown,
571    strategy_risk_max_cons_loss_days,
572    strategy_risk_max_intraday_filled_orders,
573    strategy_risk_max_intraday_loss,
574    strategy_risk_max_position_size,
575    strategy_get_attr,
576    strategy_account_currency,
577    strategy_position_entry_name,
578    strategy_default_entry_qty,
579    async strategy_convert_to_account,
580    async strategy_convert_to_symbol,
581);
582
583/// Registry of native functions implemented by `navi-vm`.
584///
585/// Pass this to `navi_compiler::CompileOptions::with_native_func_registry`
586/// when compiling a script outside of [`crate::compile`] so the
587/// compiler knows about all builtins the VM provides.
588#[derive(Debug)]
589pub struct NativeFuncs;
590
591impl NativeFunctionRegistry for NativeFuncs {
592    fn lookup(&self, name: &str) -> Option<NativeFunctionInfo> {
593        NATIVE_FUNCTIONS
594            .iter()
595            .enumerate()
596            .find(|(_, item)| item.name == name)
597            .map(|(id, item)| {
598                NativeFunctionInfo::new(
599                    id,
600                    item.num_generic_arguments,
601                    item.num_arguments,
602                    item.has_result,
603                )
604            })
605    }
606}