pine-builtins 0.2.2

Built-in functions and namespaces for the Pine Script interpreter.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use pine_builtin_macro::BuiltinFunction;
use pine_core::{PineOutput, SeriesBuffer};
use pine_interpreter::{Interpreter, RuntimeError, Value};

/// The current-bar value of a built-in price series (`high`, `low`, …). Used as
/// the default `source` for the one-argument overloads.
fn bar_source<O: PineOutput>(ctx: &Interpreter<O>, name: &str) -> f64 {
    ctx.get_variable(name)
        .and_then(|value| value.as_number().ok())
        .unwrap_or(f64::NAN)
}

/// ta.change(source, length) - Difference between current and N bars ago
#[derive(BuiltinFunction)]
#[builtin(name = "ta.change", stateful)]
pub struct TaChange {
    source: f64,
    #[arg(default = 1.0)]
    length: f64,
    #[state]
    window: SeriesBuffer<f64>,
}

impl TaChange {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let length = self.length as usize;
        if length == 0 {
            return Ok(Value::Number(0.0));
        }

        // Reaching `length` bars back needs `length + 1` values in hand.
        let Some(values) = self.window.observe(self.source, length + 1) else {
            return Ok(Value::Na);
        };

        Ok(Value::Number(values[0] - values[length]))
    }
}

/// ta.highest(source, length) - Highest value over N bars
#[derive(BuiltinFunction)]
#[builtin(name = "ta.highest", stateful)]
pub struct TaHighest {
    #[arg(default = bar_source(ctx, "high"))]
    source: f64,
    #[length_check]
    length: f64,
    #[state]
    window: SeriesBuffer<f64>,
}

impl TaHighest {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let length = self.length as usize;

        let Some(values) = self.window.observe(self.source, length) else {
            return Ok(Value::Na);
        };

        Ok(Value::Number(
            values.iter().copied().fold(f64::NEG_INFINITY, f64::max),
        ))
    }
}

/// ta.lowest(source, length) - Lowest value over N bars
#[derive(BuiltinFunction)]
#[builtin(name = "ta.lowest", stateful)]
pub struct TaLowest {
    #[arg(default = bar_source(ctx, "low"))]
    source: f64,
    #[length_check]
    length: f64,
    #[state]
    window: SeriesBuffer<f64>,
}

impl TaLowest {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let length = self.length as usize;

        let Some(values) = self.window.observe(self.source, length) else {
            return Ok(Value::Na);
        };

        Ok(Value::Number(
            values.iter().copied().fold(f64::INFINITY, f64::min),
        ))
    }
}

/// The offset of the extreme value in `values` (newest first), as the negative
/// bar count Pine reports: 0 is the current bar, -1 the one before it.
fn extreme_offset(values: &[f64], better: fn(f64, f64) -> bool) -> f64 {
    let mut best = 0;
    for (i, &value) in values.iter().enumerate() {
        if better(value, values[best]) {
            best = i;
        }
    }
    // Negating 0 would give `-0`, which prints as "-0" rather than "0".
    if best == 0 {
        0.0
    } else {
        -(best as f64)
    }
}

/// ta.highestbars(source, length) - Offset to highest value (0 = current bar)
#[derive(BuiltinFunction)]
#[builtin(name = "ta.highestbars", stateful)]
pub struct TaHighestbars {
    #[arg(default = bar_source(ctx, "high"))]
    source: f64,
    #[length_check]
    length: f64,
    #[state]
    window: SeriesBuffer<f64>,
}

impl TaHighestbars {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let length = self.length as usize;

        let Some(values) = self.window.observe(self.source, length) else {
            return Ok(Value::Na);
        };

        Ok(Value::Number(extreme_offset(&values, |a, b| a > b)))
    }
}

/// ta.lowestbars(source, length) - Offset to lowest value (0 = current bar)
#[derive(BuiltinFunction)]
#[builtin(name = "ta.lowestbars", stateful)]
pub struct TaLowestbars {
    #[arg(default = bar_source(ctx, "low"))]
    source: f64,
    #[length_check]
    length: f64,
    #[state]
    window: SeriesBuffer<f64>,
}

impl TaLowestbars {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let length = self.length as usize;

        let Some(values) = self.window.observe(self.source, length) else {
            return Ok(Value::Na);
        };

        Ok(Value::Number(extreme_offset(&values, |a, b| a < b)))
    }
}

/// ta.rising(source, length) - Test if source rose on each of the last N bars
#[derive(BuiltinFunction)]
#[builtin(name = "ta.rising", stateful)]
pub struct TaRising {
    source: f64,
    length: f64,
    #[state]
    window: SeriesBuffer<f64>,
}

impl TaRising {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let length = self.length as usize;
        if length == 0 {
            return Ok(Value::Bool(false));
        }

        let Some(values) = self.window.observe(self.source, length + 1) else {
            return Ok(Value::Bool(false));
        };

        Ok(Value::Bool(values.windows(2).all(|pair| pair[0] > pair[1])))
    }
}

/// ta.falling(source, length) - Test if source fell on each of the last N bars
#[derive(BuiltinFunction)]
#[builtin(name = "ta.falling", stateful)]
pub struct TaFalling {
    source: f64,
    length: f64,
    #[state]
    window: SeriesBuffer<f64>,
}

impl TaFalling {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let length = self.length as usize;
        if length == 0 {
            return Ok(Value::Bool(false));
        }

        let Some(values) = self.window.observe(self.source, length + 1) else {
            return Ok(Value::Bool(false));
        };

        Ok(Value::Bool(values.windows(2).all(|pair| pair[0] < pair[1])))
    }
}

/// How two series sat relative to each other on this bar and the last, which is
/// all the cross tests need.
struct Crossing {
    now_above: bool,
    was_above: bool,
}

impl Crossing {
    /// `None` until both series have two bars of history.
    fn observe(
        first: &mut SeriesBuffer<f64>,
        second: &mut SeriesBuffer<f64>,
        source1: f64,
        source2: f64,
    ) -> Option<Self> {
        // Both sides advance together, so they fill on the same bar.
        let firsts = first.observe(source1, 2);
        let seconds = second.observe(source2, 2);
        let (firsts, seconds) = (firsts?, seconds?);

        Some(Self {
            now_above: firsts[0] > seconds[0],
            was_above: firsts[1] > seconds[1],
        })
    }
}

/// ta.cross(source1, source2) - Test if two series crossed in either direction
#[derive(BuiltinFunction)]
#[builtin(name = "ta.cross", stateful)]
pub struct TaCross {
    source1: f64,
    source2: f64,
    #[state]
    first: SeriesBuffer<f64>,
    #[state]
    second: SeriesBuffer<f64>,
}

impl TaCross {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let Some(crossing) = Crossing::observe(
            &mut self.first,
            &mut self.second,
            self.source1,
            self.source2,
        ) else {
            return Ok(Value::Bool(false));
        };

        Ok(Value::Bool(crossing.now_above != crossing.was_above))
    }
}

/// ta.crossover(source1, source2) - Test if source1 crossed above source2
#[derive(BuiltinFunction)]
#[builtin(name = "ta.crossover", stateful)]
pub struct TaCrossover {
    source1: f64,
    source2: f64,
    #[state]
    first: SeriesBuffer<f64>,
    #[state]
    second: SeriesBuffer<f64>,
}

impl TaCrossover {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let Some(crossing) = Crossing::observe(
            &mut self.first,
            &mut self.second,
            self.source1,
            self.source2,
        ) else {
            return Ok(Value::Bool(false));
        };

        Ok(Value::Bool(crossing.now_above && !crossing.was_above))
    }
}

/// ta.crossunder(source1, source2) - Test if source1 crossed below source2
#[derive(BuiltinFunction)]
#[builtin(name = "ta.crossunder", stateful)]
pub struct TaCrossunder {
    source1: f64,
    source2: f64,
    #[state]
    first: SeriesBuffer<f64>,
    #[state]
    second: SeriesBuffer<f64>,
}

impl TaCrossunder {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let Some(crossing) = Crossing::observe(
            &mut self.first,
            &mut self.second,
            self.source1,
            self.source2,
        ) else {
            return Ok(Value::Bool(false));
        };

        Ok(Value::Bool(!crossing.now_above && crossing.was_above))
    }
}

/// ta.barssince(condition) - Bars since `condition` was last true (`0` on the
/// bar it is true); `na` until it has been true at least once.
#[derive(BuiltinFunction)]
#[builtin(name = "ta.barssince", stateful)]
pub struct TaBarssince {
    condition: bool,
    #[state]
    since: Option<u64>,
}

impl TaBarssince {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        self.since = if self.condition {
            Some(0)
        } else {
            self.since.map(|s| s + 1)
        };
        Ok(self.since.map_or(Value::Na, |s| Value::Number(s as f64)))
    }
}

/// ta.valuewhen(condition, source, occurrence) - The value of `source` when
/// `condition` was true `occurrence` times ago (`0` is the most recent).
#[derive(BuiltinFunction)]
#[builtin(name = "ta.valuewhen", stateful)]
pub struct TaValuewhen {
    condition: bool,
    source: f64,
    occurrence: f64,
    #[state]
    values: Vec<f64>,
}

impl TaValuewhen {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        if self.condition {
            self.values.push(self.source);
        }
        if self.occurrence < 0.0 || self.occurrence.is_nan() {
            return Ok(Value::Na);
        }
        let occurrence = self.occurrence as usize;
        let Some(index) = self.values.len().checked_sub(occurrence + 1) else {
            return Ok(Value::Na);
        };
        Ok(Value::Number(self.values[index]))
    }
}

/// The pivot value `rightbars` bars back if it is the strict extreme of the
/// `leftbars + 1 + rightbars` window, else `None`. `strict_max` picks a high pivot.
fn pivot(window: &[f64], rightbars: usize, strict_max: bool) -> Option<f64> {
    let candidate = window[rightbars];
    let is_pivot = window.iter().enumerate().all(|(i, &v)| {
        i == rightbars
            || if strict_max {
                candidate > v
            } else {
                candidate < v
            }
    });
    is_pivot.then_some(candidate)
}

/// ta.pivothigh(source, leftbars, rightbars) - The pivot high `rightbars` bars
/// back (`source` defaults to `high`).
#[derive(BuiltinFunction)]
#[builtin(name = "ta.pivothigh", stateful)]
pub struct TaPivothigh {
    #[arg(default = bar_source(ctx, "high"))]
    source: f64,
    leftbars: f64,
    rightbars: f64,
    #[state]
    window: SeriesBuffer<f64>,
}

impl TaPivothigh {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let (left, right) = (self.leftbars as usize, self.rightbars as usize);
        let Some(window) = self.window.observe(self.source, left + right + 1) else {
            return Ok(Value::Na);
        };
        Ok(pivot(&window, right, true).map_or(Value::Na, Value::Number))
    }
}

/// ta.pivotlow(source, leftbars, rightbars) - The pivot low `rightbars` bars back
/// (`source` defaults to `low`).
#[derive(BuiltinFunction)]
#[builtin(name = "ta.pivotlow", stateful)]
pub struct TaPivotlow {
    #[arg(default = bar_source(ctx, "low"))]
    source: f64,
    leftbars: f64,
    rightbars: f64,
    #[state]
    window: SeriesBuffer<f64>,
}

impl TaPivotlow {
    fn execute<O: PineOutput>(
        &mut self,
        _ctx: &mut Interpreter<O>,
    ) -> Result<Value<O>, RuntimeError> {
        let (left, right) = (self.leftbars as usize, self.rightbars as usize);
        let Some(window) = self.window.observe(self.source, left + right + 1) else {
            return Ok(Value::Na);
        };
        Ok(pivot(&window, right, false).map_or(Value::Na, Value::Number))
    }
}