openpinescript 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +346 -2
  3. package/dist/cli.mjs +108686 -0
  4. package/dist/index.cjs +108670 -0
  5. package/dist/index.mjs +108624 -0
  6. package/dist/types/index.d.ts +31 -0
  7. package/dist/types/parser/v1/generated/PineV1Lexer.d.ts +66 -0
  8. package/dist/types/parser/v1/generated/PineV1Parser.d.ts +555 -0
  9. package/dist/types/parser/v1/generated/PineV1ParserVisitor.d.ts +318 -0
  10. package/dist/types/parser/v1/index.d.ts +39 -0
  11. package/dist/types/parser/v2/generated/PineV2Lexer.d.ts +67 -0
  12. package/dist/types/parser/v2/generated/PineV2Parser.d.ts +568 -0
  13. package/dist/types/parser/v2/generated/PineV2ParserVisitor.d.ts +325 -0
  14. package/dist/types/parser/v2/index.d.ts +2 -0
  15. package/dist/types/parser/v3/generated/PineV3Lexer.d.ts +67 -0
  16. package/dist/types/parser/v3/generated/PineV3Parser.d.ts +568 -0
  17. package/dist/types/parser/v3/generated/PineV3ParserVisitor.d.ts +325 -0
  18. package/dist/types/parser/v3/index.d.ts +2 -0
  19. package/dist/types/parser/v4/generated/PineV4Lexer.d.ts +86 -0
  20. package/dist/types/parser/v4/generated/PineV4Parser.d.ts +673 -0
  21. package/dist/types/parser/v4/generated/PineV4ParserVisitor.d.ts +367 -0
  22. package/dist/types/parser/v4/index.d.ts +2 -0
  23. package/dist/types/parser/v5/generated/PineV5Lexer.d.ts +95 -0
  24. package/dist/types/parser/v5/generated/PineV5Parser.d.ts +964 -0
  25. package/dist/types/parser/v5/generated/PineV5ParserVisitor.d.ts +521 -0
  26. package/dist/types/parser/v5/index.d.ts +2 -0
  27. package/dist/types/parser/v6/generated/PineV6Lexer.d.ts +98 -0
  28. package/dist/types/parser/v6/generated/PineV6Parser.d.ts +984 -0
  29. package/dist/types/parser/v6/generated/PineV6ParserVisitor.d.ts +528 -0
  30. package/dist/types/parser/v6/index.d.ts +2 -0
  31. package/dist/types/runtime/v1/Series.d.ts +61 -0
  32. package/dist/types/runtime/v1/broker/Broker.d.ts +325 -0
  33. package/dist/types/runtime/v1/broker/index.d.ts +158 -0
  34. package/dist/types/runtime/v1/broker/val.d.ts +28 -0
  35. package/dist/types/runtime/v1/constDefault.d.ts +11 -0
  36. package/dist/types/runtime/v1/context.d.ts +1705 -0
  37. package/dist/types/runtime/v1/footprint.d.ts +32 -0
  38. package/dist/types/runtime/v1/index.d.ts +7 -0
  39. package/dist/types/runtime/v1/language-spec.d.ts +12 -0
  40. package/dist/types/runtime/v1/openTrades.d.ts +12 -0
  41. package/dist/types/runtime/v1/palette.d.ts +18 -0
  42. package/dist/types/runtime/v1/session.d.ts +482 -0
  43. package/dist/types/runtime/v1/sessions.d.ts +31 -0
  44. package/dist/types/runtime/v1/stateClone.d.ts +19 -0
  45. package/dist/types/runtime/v1/stdlib/alerts.d.ts +31 -0
  46. package/dist/types/runtime/v1/stdlib/arrays.d.ts +133 -0
  47. package/dist/types/runtime/v1/stdlib/barmerge.d.ts +27 -0
  48. package/dist/types/runtime/v1/stdlib/barstate.d.ts +79 -0
  49. package/dist/types/runtime/v1/stdlib/cast.d.ts +81 -0
  50. package/dist/types/runtime/v1/stdlib/chart.d.ts +283 -0
  51. package/dist/types/runtime/v1/stdlib/color.d.ts +51 -0
  52. package/dist/types/runtime/v1/stdlib/core.d.ts +162 -0
  53. package/dist/types/runtime/v1/stdlib/dataset.d.ts +36 -0
  54. package/dist/types/runtime/v1/stdlib/display.d.ts +50 -0
  55. package/dist/types/runtime/v1/stdlib/drawings.d.ts +407 -0
  56. package/dist/types/runtime/v1/stdlib/errors.d.ts +21 -0
  57. package/dist/types/runtime/v1/stdlib/font.d.ts +20 -0
  58. package/dist/types/runtime/v1/stdlib/footprint.d.ts +76 -0
  59. package/dist/types/runtime/v1/stdlib/format.d.ts +57 -0
  60. package/dist/types/runtime/v1/stdlib/index.d.ts +71 -0
  61. package/dist/types/runtime/v1/stdlib/input.d.ts +14 -0
  62. package/dist/types/runtime/v1/stdlib/inputs.d.ts +60 -0
  63. package/dist/types/runtime/v1/stdlib/late_members.d.ts +97 -0
  64. package/dist/types/runtime/v1/stdlib/logging.d.ts +31 -0
  65. package/dist/types/runtime/v1/stdlib/maps.d.ts +55 -0
  66. package/dist/types/runtime/v1/stdlib/math.d.ts +71 -0
  67. package/dist/types/runtime/v1/stdlib/matrix.d.ts +201 -0
  68. package/dist/types/runtime/v1/stdlib/metadata.d.ts +14 -0
  69. package/dist/types/runtime/v1/stdlib/mtf.d.ts +15 -0
  70. package/dist/types/runtime/v1/stdlib/renames.d.ts +117 -0
  71. package/dist/types/runtime/v1/stdlib/renames5.d.ts +113 -0
  72. package/dist/types/runtime/v1/stdlib/renames6.d.ts +81 -0
  73. package/dist/types/runtime/v1/stdlib/request.d.ts +207 -0
  74. package/dist/types/runtime/v1/stdlib/scale.d.ts +19 -0
  75. package/dist/types/runtime/v1/stdlib/session.d.ts +113 -0
  76. package/dist/types/runtime/v1/stdlib/sort_field.d.ts +12 -0
  77. package/dist/types/runtime/v1/stdlib/sources.d.ts +24 -0
  78. package/dist/types/runtime/v1/stdlib/str.d.ts +117 -0
  79. package/dist/types/runtime/v1/stdlib/strategy.d.ts +314 -0
  80. package/dist/types/runtime/v1/stdlib/syminfo.d.ts +209 -0
  81. package/dist/types/runtime/v1/stdlib/ta.d.ts +409 -0
  82. package/dist/types/runtime/v1/stdlib/time.d.ts +158 -0
  83. package/dist/types/runtime/v1/stdlib/trades.d.ts +108 -0
  84. package/dist/types/runtime/v1/stdlib/ui.d.ts +196 -0
  85. package/dist/types/runtime/v1/stdlib/v6_members.d.ts +68 -0
  86. package/dist/types/runtime/v1/timeframes.d.ts +139 -0
  87. package/dist/types/runtime/v1/zones.d.ts +30 -0
  88. package/dist/types/transpiler/common/index.d.ts +51 -0
  89. package/dist/types/transpiler/common/libraries.d.ts +29 -0
  90. package/dist/types/transpiler/common/parseErrors.d.ts +38 -0
  91. package/dist/types/transpiler/common/passes/ScopeAnalysis.d.ts +158 -0
  92. package/dist/types/transpiler/common/passes/TypeRules.d.ts +26 -0
  93. package/dist/types/transpiler/common/profiles/index.d.ts +96 -0
  94. package/dist/types/transpiler/common/version.d.ts +40 -0
  95. package/dist/types/transpiler/v1/ToJsVisitor.d.ts +720 -0
  96. package/dist/types/transpiler/v2/ToJsVisitor.d.ts +72 -0
  97. package/dist/types/transpiler/v3/ToJsVisitor.d.ts +87 -0
  98. package/dist/types/transpiler/v4/ToJsVisitor.d.ts +263 -0
  99. package/dist/types/transpiler/v5/ToJsVisitor.d.ts +1101 -0
  100. package/dist/types/transpiler/v6/ToJsVisitor.d.ts +237 -0
  101. package/dist/types/transpiler/v6/rules.d.ts +94 -0
  102. package/dist/types/utils/v2/common.d.ts +4 -0
  103. package/package.json +92 -4
@@ -0,0 +1,133 @@
1
+ import type { Context } from "../context.js";
2
+ export declare const array: {
3
+ new_float: (size?: any, initial_value?: any) => any[];
4
+ new_int: (size?: any, initial_value?: any) => any[];
5
+ new_bool: (size?: any, initial_value?: any) => any[];
6
+ new_string: (size?: any, initial_value?: any) => any[];
7
+ new_color: (size?: any, initial_value?: any) => any[];
8
+ new_line: (size?: any, initial_value?: any) => any[];
9
+ new_linefill: (size?: any, initial_value?: any) => any[];
10
+ /**
11
+ * The value `percentage` percent of the array's values are at or below,
12
+ * by nearest rank: `ceil(P/100 * N)` over the ascending values, so the
13
+ * answer is always one of them. The rule `ta.percentile_nearest_rank`
14
+ * applies to a window. `na` values are left out.
15
+ */
16
+ percentile_nearest_rank: (id: any, percentage: any) => number;
17
+ /**
18
+ * The same percentile, interpolating linearly between the two values that
19
+ * straddle it — the rule `ta.percentile_linear_interpolation` applies.
20
+ */
21
+ percentile_linear_interpolation: (id: any, percentage: any) => number;
22
+ new_label: (size?: any, initial_value?: any) => any[];
23
+ new_box: (size?: any, initial_value?: any) => any[];
24
+ new_table: (size?: any, initial_value?: any) => any[];
25
+ /** `array.from(v1, v2, …)` — build an array from its arguments. */
26
+ from: (...items: any[]) => any[];
27
+ get: (ctx: Context, id: any, index: any) => any;
28
+ set: (ctx: Context, id: any, index: any, value: any) => void;
29
+ size: (id: any) => number;
30
+ /**
31
+ * `array.first(id)` and `array.last(id)` — January 2023.
32
+ *
33
+ * "Returns the array's first element" / "Returns the array's last element."
34
+ *
35
+ * An EMPTY array is an error rather than `na`, which is the same rule
36
+ * `array.get` follows for an out-of-range index: the two are the same
37
+ * mistake, and answering `na` would let a script average nothing and plot
38
+ * it.
39
+ */
40
+ first: (id: any) => any;
41
+ last: (id: any) => any;
42
+ /**
43
+ * `array.binary_search(id, val)` — the index of `val`, or -1.
44
+ *
45
+ * Requires the array to be sorted ASCENDING, which is the contract of every
46
+ * binary search and is why the three `binary_search*` functions exist
47
+ * alongside `array.indexof`: on a sorted array they are the fast answer, and
48
+ * on an unsorted one they are a wrong one. This engine does not verify the
49
+ * ordering — checking it would cost the very thing the function is for.
50
+ */
51
+ binary_search: (ctx: Context, id: any, value: any, sort_field?: any) => number;
52
+ push: (id: any, value: any) => void;
53
+ pop: (id: any) => any;
54
+ shift: (id: any) => any;
55
+ unshift: (id: any, value: any) => void;
56
+ insert: (ctx: Context, id: any, index: any, value: any) => void;
57
+ remove: (ctx: Context, id: any, index: any) => any;
58
+ clear: (id: any) => void;
59
+ /**
60
+ * Fills a contiguous range, and RETURNS THE ARRAY.
61
+ *
62
+ * The reference gives `array.fill(id, value, index_from, index_to) → void`,
63
+ * and the manual's Methods page gives this example
64
+ *
65
+ * srcArray.copy().fill(1.0, 0.0, min, val).avg()
66
+ *
67
+ * with the explanation: "We are able to call `copy()`, `fill()`, and
68
+ * `avg()` successively on the same line of code because the first two
69
+ * methods return an `array<float>` instance."
70
+ *
71
+ * The two statements disagree. The example is the one a script can depend
72
+ * on — it is TradingView's own published code, and returning the receiver
73
+ * is a strict superset of returning nothing: a caller that ignores the
74
+ * result cannot tell the difference. So the receiver is returned, and the
75
+ * conflict is recorded here rather than resolved silently.
76
+ */
77
+ fill: (id: any, value: any, index_from?: any, index_to?: any) => any[];
78
+ copy: (id: any) => any[];
79
+ slice: (id: any, index_from: any, index_to: any) => any[];
80
+ concat: (id1: any, id2: any) => any[];
81
+ reverse: (id: any) => void;
82
+ /**
83
+ * `array.sort_indices(id, order, sort_field)` — the indices that WOULD
84
+ * sort the array, which is left as it is. v6 (April 2026) adds
85
+ * `sort_field` for arrays of objects.
86
+ *
87
+ * TradingView's log (conformance/common/v5_gaps_golden.test.ts) settles the
88
+ * two things the reference leaves open: ascending is a STABLE sort with
89
+ * `na` last, and descending is exactly that order REVERSED — so `na` comes
90
+ * first, and equal values come out in reverse index order.
91
+ */
92
+ sort_indices: (ctx: Context, id: any, order?: any, sort_field?: any) => number[];
93
+ sort: (ctx: Context, id: any, order?: any, sort_field?: any) => void;
94
+ /**
95
+ * `binary_search_leftmost` / `_rightmost`. v6 (August 2026) adds
96
+ * `sort_field` for arrays of objects.
97
+ *
98
+ * From the reference: the index of the value when it is found; otherwise
99
+ * leftmost gives "the index of the next smallest element to the left of
100
+ * where the value would lie", rightmost the element to its right. With
101
+ * duplicates, each returns the occurrence its name says — and below the
102
+ * whole array leftmost answers 0, not -1. Both pinned by TradingView's log.
103
+ */
104
+ binary_search_leftmost: (ctx: Context, id: any, value: any, sort_field?: any) => number;
105
+ binary_search_rightmost: (ctx: Context, id: any, value: any, sort_field?: any) => number;
106
+ includes: (id: any, value: any) => boolean;
107
+ indexof: (id: any, value: any) => number;
108
+ lastindexof: (id: any, value: any) => number;
109
+ sum: (id: any) => number;
110
+ avg: (id: any) => number;
111
+ min: (id: any) => number;
112
+ max: (id: any) => number;
113
+ range: (id: any) => number;
114
+ median: (id: any) => number;
115
+ mode: (id: any) => number;
116
+ /** Sample variance (n-1), matching `variance()` elsewhere in the stdlib. */
117
+ variance: (id: any) => number;
118
+ stdev: (id: any) => number;
119
+ covariance: (id1: any, id2: any) => number;
120
+ /** `array.join(id, separator)` — v4 spells the default separator as ",". */
121
+ join: (id: any, separator?: any) => string;
122
+ };
123
+ /**
124
+ * `array.sort`'s direction argument.
125
+ *
126
+ * A namespace of its own in Pine rather than a string, which is why the tests
127
+ * spell `order.descending` and not `"desc"`. `array.sort` accepts either, since
128
+ * it only asks whether the value contains "desc".
129
+ */
130
+ export declare const order: {
131
+ ascending: string;
132
+ descending: string;
133
+ };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * barmerge.* — how security() aligns higher-timeframe data onto chart bars.
3
+ *
4
+ * The `lookahead` argument is security()'s fifth parameter. Its DEFAULT changed
5
+ * between versions and that change is observable in the numbers:
6
+ *
7
+ * v1/v2 → barmerge.lookahead_on (the historical default)
8
+ * v3+ → barmerge.lookahead_off (flipped by the v2→v3 migration)
9
+ *
10
+ * See docs/common/VERSIONS.md §2 D3.1.
11
+ */
12
+ export declare const __IS_NAMESPACE__ = true;
13
+ /**
14
+ * Use data from the HTF bar the current chart bar falls INSIDE, including the
15
+ * part that has not happened yet. On historical bars this reveals a value
16
+ * before it could have been known — the classic repainting behaviour.
17
+ */
18
+ export declare const lookahead_on = "barmerge.lookahead_on";
19
+ /**
20
+ * Use only HTF bars that have already CLOSED. No future information, so
21
+ * historical results match what the script would have seen live.
22
+ */
23
+ export declare const lookahead_off = "barmerge.lookahead_off";
24
+ /** Fill gaps with the previous available value (default). */
25
+ export declare const gaps_off = "barmerge.gaps_off";
26
+ /** Leave gaps as na. */
27
+ export declare const gaps_on = "barmerge.gaps_on";
@@ -0,0 +1,79 @@
1
+ import { Context } from "../context.js";
2
+ export declare const __IS_NAMESPACE__ = true;
3
+ /**
4
+ * Returns true if the current bar is a historical bar.
5
+ * @getter
6
+ */
7
+ export declare function ishistory(ctx: Context): boolean;
8
+ /**
9
+ * Returns true if the current bar is a real-time bar.
10
+ * @getter
11
+ */
12
+ export declare function isrealtime(ctx: Context): boolean;
13
+ /**
14
+ * Returns true if this is the first execution on the current bar.
15
+ * @getter
16
+ */
17
+ export declare function isnew(ctx: Context): boolean;
18
+ /**
19
+ * Returns true if this is the last historically available bar.
20
+ * @getter
21
+ */
22
+ export declare function islast(ctx: Context): boolean;
23
+ /**
24
+ * Returns true on the script's FIRST bar.
25
+ *
26
+ * Derived from the bar counter rather than stored as a flag, so it cannot fall
27
+ * out of step with `bar_index` — they are the same fact.
28
+ *
29
+ * Present in Pine since v1 and absent here until v5 needed it: every harness
30
+ * generated by scripts/common/make-log-harness.ts writes its CSV header under
31
+ * `if barstate.isfirst`, and a missing member of an EXISTING namespace reads as
32
+ * `undefined` rather than erroring (the namespace-root check in the emitter
33
+ * deliberately does not assert registry completeness), so the header was
34
+ * silently never emitted.
35
+ *
36
+ * @getter
37
+ */
38
+ export declare function isfirst(ctx: Context): boolean;
39
+ /**
40
+ * True on every historical bar, and on a realtime bar's CLOSING update.
41
+ *
42
+ * "barstate.isconfirmed is true on all historical bars and on the last
43
+ * (closing) update of a realtime bar."
44
+ *
45
+ * The bar's values will not change again once this is true, which is why it is
46
+ * the standard guard around anything a script must do once per bar — an alert,
47
+ * a log line, a queue push. Eighteen of TradingView's own examples use it, and
48
+ * every one of them was reading `undefined`: a missing member of an EXISTING
49
+ * namespace does not error, so `if barstate.isconfirmed` was silently never
50
+ * taken and the scripts drew nothing.
51
+ *
52
+ * TradingView runs the script ONE MORE time on a realtime bar when it closes
53
+ * (a rollback re-execution like a tick, on the bar's final OHLCV, with
54
+ * `isnew` false) and `isconfirmed` is true only there. `Session.commit` does
55
+ * that execution and sets `Context.is_confirmed` for it. Verified:
56
+ * dev-docs/tv-parity/_probes/realtime/rt-confirmed-v6/run-20261113-042353 and
57
+ * rt-confirmed-v5/run-20261113-043223. History is always confirmed.
58
+ *
59
+ * @getter
60
+ */
61
+ export declare function isconfirmed(ctx: Context): boolean;
62
+ /**
63
+ * True on the last bar the script can treat as settled history.
64
+ *
65
+ * "barstate.islastconfirmedhistory is true if the script is executing on the
66
+ * dataset's last bar when the market is closed, or on the bar immediately
67
+ * preceding the realtime bar if the market is open."
68
+ *
69
+ * Two readings of one idea, and which applies depends on whether a realtime bar
70
+ * exists at all. With no realtime bar the last bar IS the last confirmed one;
71
+ * with one, the bar before it is.
72
+ *
73
+ * The commonest guard in TradingView's documentation — twenty-nine examples
74
+ * draw their tables and labels under it, because it fires exactly once and on
75
+ * the rightmost settled bar.
76
+ *
77
+ * @getter
78
+ */
79
+ export declare function islastconfirmedhistory(ctx: Context): boolean;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Explicit type-casting functions — v4's `float(x)`, `bool(x)`, `string(x)`.
3
+ *
4
+ * SOURCING. The v4 release notes, June 2019, list "functions for explicit type
5
+ * casting" among that version's additions, and the manual's type-system page
6
+ * says why they exist: "It's sometimes necessary to cast one type to another
7
+ * when auto-casting rules do not suffice." It names ten of them — `int()`,
8
+ * `float()`, `bool()`, `color()`, `string()`, `line()`, `linefill()`,
9
+ * `label()`, `box()` and `table()` — and gives their second job:
10
+ *
11
+ * "Explicit type casting is also handy when declaring variables assigned to
12
+ * na", with `label myLabel = na` and `myLabel = label(na)` shown as the two
13
+ * spellings of one thing.
14
+ *
15
+ * `float(na)` is by far the common case in TradingView's own examples: it is
16
+ * how a script says "an absent FLOAT" where the bare `na` would be untyped.
17
+ *
18
+ * ── Why this file is a NAMESPACE that nothing can spell ─────────────────────
19
+ *
20
+ * `int()` and `color()` are plain flat exports elsewhere, because nothing else
21
+ * in the language is called `int` or `color`. These three are: `float`, `bool`
22
+ * and `string` are ALSO the v1–v3 `input()` type constants —
23
+ * `input(1.0, type = float)` — which v4 renamed to `input.float`. One flat
24
+ * registry key cannot be a string at v1 and a function at v4.
25
+ *
26
+ * So they are generated under `cast.*`, which no script may write, and
27
+ * runtime/v1/stdlib/index.ts installs them under their flat names in v4's view
28
+ * ONLY. v1–v3 keep the constant; v4 and v5 get the cast; neither version can
29
+ * see the other's meaning. The same device that lets `session` be a v1–v3
30
+ * string and a v5 namespace at once.
31
+ *
32
+ * `int` is deliberately NOT here — it has no collision, it is already a flat
33
+ * v4-only export in ./core.ts, and moving it would change a working name for
34
+ * symmetry's sake.
35
+ */
36
+ export declare const __IS_NAMESPACE__ = true;
37
+ /**
38
+ * `float(x)` — the value as a float.
39
+ *
40
+ * `na` in, `na` out: a cast changes how a value is READ, and cannot invent one.
41
+ * That is what makes `float(na)` the typed absent value rather than a zero.
42
+ *
43
+ * @returns {series float} `x` as a float, or na.
44
+ */
45
+ export declare function float(x: any): number;
46
+ /**
47
+ * `bool(x)` — the value as a boolean.
48
+ *
49
+ * Pine's truthiness, not JavaScript's: `na` is not `false`, it stays `na`, and
50
+ * a script that tests `if bool(x)` on an absent value must take neither branch
51
+ * rather than the else. Zero IS false, and every other finite number true —
52
+ * which is the whole reason the manual's example exists:
53
+ *
54
+ * if bool(randomValue)
55
+ *
56
+ * "does not raise a warning, as the `bool()` function explicitly casts".
57
+ *
58
+ * @returns {series bool} `x` as a boolean, or na.
59
+ */
60
+ export declare function bool(x: any): boolean | number;
61
+ /**
62
+ * `string(x)` — the value as a string.
63
+ *
64
+ * NOT `str.tostring`. That one FORMATS — it takes a format mask, renders a
65
+ * float to a fixed number of decimals, and turns `na` into the four characters
66
+ * "NaN" so a label can display them. This is a cast: it reinterprets a value
67
+ * that is already a string, and propagates `na` rather than spelling it out.
68
+ *
69
+ * @returns {series string} `x` as a string, or na.
70
+ */
71
+ export declare function string(x: any): string | number;
72
+ /** @returns {series line} `x` as a line id, or na. */
73
+ export declare function line(x: any): any;
74
+ /** @returns {series linefill} `x` as a linefill id, or na. */
75
+ export declare function linefill(x: any): any;
76
+ /** @returns {series label} `x` as a label id, or na. */
77
+ export declare function label(x: any): any;
78
+ /** @returns {series box} `x` as a box id, or na. */
79
+ export declare function box(x: any): any;
80
+ /** @returns {series table} `x` as a table id, or na. */
81
+ export declare function table(x: any): any;
@@ -0,0 +1,283 @@
1
+ /**
2
+ * Chart metadata — the v1–v3 spellings.
3
+ *
4
+ * These are the identifiers a pre-v4 script uses to ask what it is running on.
5
+ * v4 moved them into namespaces (`timeframe.period`, `syminfo.tickerid`), which
6
+ * is why they are unprefixed globals here and not under one.
7
+ *
8
+ * All read from Context, so a host that runs 5-minute bars gets "5" rather than
9
+ * a hard-coded guess. Scripts branch on these to pick pivot windows and session
10
+ * boundaries, so getting them wrong changes the numbers, not just a label.
11
+ */
12
+ import { Context } from "../context.js";
13
+ /**
14
+ * The chart's timeframe as a string: "1", "5", "60", "D", "W", "M".
15
+ * v4 renamed this to `timeframe.period`.
16
+ * @getter
17
+ * @returns {string} The resolution string.
18
+ */
19
+ export declare function period(ctx: Context): string;
20
+ /**
21
+ * The chart's timeframe as a NUMBER of minutes, or 0 for non-intraday.
22
+ * Pine reports "D"/"W"/"M" as 0 here, which is why `period` exists alongside it.
23
+ * @getter
24
+ * @returns {integer} Minutes per bar, 0 for daily and above.
25
+ */
26
+ export declare function interval(ctx: Context): number;
27
+ /**
28
+ * Is the chart's timeframe intraday (minutes/seconds rather than days or more)?
29
+ * @getter
30
+ * @returns {bool}
31
+ */
32
+ export declare function isintraday(ctx: Context): boolean;
33
+ /**
34
+ * Is the chart's timeframe daily?
35
+ * @getter
36
+ * @returns {bool}
37
+ */
38
+ export declare function isdaily(ctx: Context): boolean;
39
+ /**
40
+ * Is the chart's timeframe weekly?
41
+ * @getter
42
+ * @returns {bool}
43
+ */
44
+ export declare function isweekly(ctx: Context): boolean;
45
+ /**
46
+ * Is the chart's timeframe monthly?
47
+ * @getter
48
+ * @returns {bool}
49
+ */
50
+ export declare function ismonthly(ctx: Context): boolean;
51
+ /**
52
+ * The chart's symbol WITHOUT its exchange prefix — "BTCUSD", not
53
+ * "BITSTAMP:BTCUSD". v4 renamed this to `syminfo.ticker`.
54
+ *
55
+ * Published strategies branch on it to pick per-symbol constants, so it changes
56
+ * results rather than just labelling them.
57
+ * @getter
58
+ * @returns {string}
59
+ */
60
+ export declare function ticker(ctx: Context): string;
61
+ export declare const timeframe: {
62
+ /**
63
+ * `timeframe.in_seconds(timeframe)` — May 2024.
64
+ *
65
+ * "The typical number of seconds in the chart's timeframe."
66
+ *
67
+ * The argument defaults to the chart's own timeframe, which is how nearly
68
+ * every published use spells it: `timeframe.in_seconds()` compared against
69
+ * `timeframe.in_seconds(higherTimeframe)` is the standard guard against
70
+ * requesting data finer than the chart.
71
+ *
72
+ * @getter
73
+ * @returns {simple int} Seconds per bar.
74
+ */
75
+ in_seconds: (ctx: Context, timeframe?: any) => number;
76
+ /**
77
+ * `timeframe.from_seconds(seconds)` — September 2023.
78
+ *
79
+ * "Converts a specified number of `seconds` into a valid timeframe string
80
+ * based on our timeframe specification format."
81
+ *
82
+ * The inverse of `in_seconds`, and not a total one: 90 seconds is not a whole
83
+ * number of minutes, so the largest unit that divides evenly is chosen and
84
+ * the remainder stays in the smaller one. Under a minute the answer is
85
+ * seconds; a value that is not a positive number has no timeframe and gives
86
+ * `na` rather than a plausible "1".
87
+ */
88
+ from_seconds: (seconds: any) => string;
89
+ /**
90
+ * `timeframe.change(timeframe)` — June 2022.
91
+ *
92
+ * "Returns `true` on the first bar of a new `timeframe`, `false` otherwise."
93
+ *
94
+ * Answered by comparing which PERIOD the current bar's timestamp falls into
95
+ * against the previous bar's, so it fires exactly once per period however
96
+ * many chart bars that period contains — and is false on the very first bar
97
+ * of the script, which begins no period the script has seen end.
98
+ */
99
+ change: (ctx: Context, timeframe: any) => boolean;
100
+ /**
101
+ * `timeframe.isseconds` — is the chart's timeframe measured in seconds?
102
+ *
103
+ * The sixth member of the `is*` family, and the only one with no v1–v3 flat
104
+ * spelling, because seconds-based timeframes postdate them.
105
+ *
106
+ * @getter
107
+ * @returns {bool}
108
+ */
109
+ isseconds: (ctx: Context) => boolean;
110
+ /**
111
+ * `timeframe.isminutes` — is the chart's timeframe measured in minutes?
112
+ * A bare number is minutes in the timeframe format ("1", "60", "240").
113
+ * @getter
114
+ * @returns {bool}
115
+ */
116
+ isminutes: (ctx: Context) => boolean;
117
+ /**
118
+ * `timeframe.isdwm` — is it days, weeks or months?
119
+ * @getter
120
+ * @returns {bool}
121
+ */
122
+ isdwm: (ctx: Context) => boolean;
123
+ /**
124
+ * `timeframe.isticks` — is the chart's timeframe measured in ticks ("1T")?
125
+ * False on every time-based timeframe, which is every one a host charts
126
+ * today.
127
+ * @getter
128
+ * @returns {bool}
129
+ */
130
+ isticks: (ctx: Context) => boolean;
131
+ };
132
+ /** @returns {integer} */
133
+ export declare const sunday = 1;
134
+ /** @returns {integer} */
135
+ export declare const monday = 2;
136
+ /** @returns {integer} */
137
+ export declare const tuesday = 3;
138
+ /** @returns {integer} */
139
+ export declare const wednesday = 4;
140
+ /** @returns {integer} */
141
+ export declare const thursday = 5;
142
+ /** @returns {integer} */
143
+ export declare const friday = 6;
144
+ /** @returns {integer} */
145
+ export declare const saturday = 7;
146
+ /**
147
+ * `timenow` — the current UNIX time in milliseconds.
148
+ *
149
+ * Distinct from `time`, which is the timestamp of the bar being evaluated.
150
+ * Scripts use it to age-out signals ("only alert if the bar is recent").
151
+ *
152
+ * Reads from Context rather than calling Date.now() directly so a replay or a
153
+ * backtest can pin it; an unpinned wall clock would make runs irreproducible.
154
+ * @getter
155
+ * @returns {integer} UNIX timestamp in milliseconds.
156
+ */
157
+ export declare function timenow(ctx: Context): number;
158
+ /**
159
+ * `heikinashi(symbol)` — a symbol whose bars are Heikin-Ashi transformed.
160
+ *
161
+ * Pine models this as a TICKER TRANSFORM, not a series function: the result is
162
+ * passed to security(), which then evaluates its expression against smoothed
163
+ * candles.
164
+ *
165
+ * ha = heikinashi(tickerid)
166
+ * haClose = security(ha, period, close)
167
+ *
168
+ * Returning a marked symbol keeps that shape intact — Context.getSecurityData()
169
+ * recognises the marker and applies the transform when the candles are read, so
170
+ * the whole HTF machinery (alignment, no-lookahead, per-call-site state) is
171
+ * reused rather than duplicated.
172
+ *
173
+ * @returns {string} The symbol, marked for Heikin-Ashi transformation.
174
+ */
175
+ export declare function heikinashi(symbol: any): string;
176
+ /** Marker understood by Context.getSecurityData(). */
177
+ export declare const HEIKINASHI_PREFIX = "heikinashi:";
178
+ /** @returns {string} Never returns — Renko bricks cannot be rebuilt here. */
179
+ export declare function renko(_symbol: any, _style?: any, _param?: any): string;
180
+ /** @returns {string} Never returns — Line Break bars cannot be rebuilt here. */
181
+ export declare function linebreak(_symbol: any, _lines?: any): string;
182
+ /** @returns {string} Never returns — Kagi lines cannot be rebuilt here. */
183
+ export declare function kagi(_symbol: any, _reversal?: any): string;
184
+ /** @returns {string} Never returns — P&F columns cannot be rebuilt here. */
185
+ export declare function pointfigure(_symbol: any, _source?: any, _style?: any, _param?: any, _reversal?: any): string;
186
+ /** One coordinate on the chart. */
187
+ interface ChartPoint {
188
+ index: number;
189
+ time: number;
190
+ price: number;
191
+ }
192
+ export declare const chart: {
193
+ /**
194
+ * `chart.point` — v5's coordinate object.
195
+ *
196
+ * Written INLINE rather than as a `const` referenced by shorthand: the
197
+ * registry generator walks object literals with ts-morph and does not
198
+ * evaluate the file, so `point,` pointing at a declaration elsewhere produced
199
+ * no `chart.point.*` keys at all. Same rule, and the same reason, as the
200
+ * "written out literally" note at the top of ./drawings.ts.
201
+ */
202
+ point: {
203
+ /** `chart.point.new(time, index, price)` — every field given. */
204
+ new: (time: any, index: any, price: any) => ChartPoint;
205
+ /**
206
+ * `chart.point.now(price)` — the CURRENT bar's index and time.
207
+ *
208
+ * The only constructor that needs the Context, and that is the whole point
209
+ * of it: "now" is a fact about the run, not about the arguments.
210
+ */
211
+ now: (ctx: Context, price: any) => ChartPoint;
212
+ /** `chart.point.from_index(index, price)` — time left `na`. */
213
+ from_index: (index: any, price: any) => ChartPoint;
214
+ /** `chart.point.from_time(time, price)` — index left `na`. */
215
+ from_time: (time: any, price: any) => ChartPoint;
216
+ /** `chart.point.copy(point)` — a new point with the same three fields. */
217
+ copy: (p: any) => ChartPoint;
218
+ };
219
+ /**
220
+ * The chart's background colour.
221
+ *
222
+ * "chart.bg_color returns the color of the chart's background from the
223
+ * 'Chart settings/Appearance/Background' field."
224
+ *
225
+ * Host-supplied via `Context.provideChartBackground`; see the note there for
226
+ * the default and why it is a choice rather than a fact.
227
+ * @getter
228
+ */
229
+ bg_color: (ctx: Context) => string;
230
+ /**
231
+ * A colour that contrasts well with the background.
232
+ *
233
+ * "chart.fg_color returns a color providing optimal contrast with
234
+ * chart.bg_color."
235
+ *
236
+ * Black on a light background, white on a dark one, chosen by relative
237
+ * luminance. TradingView does not publish the exact rule, so this is the
238
+ * standard one rather than a reproduction of theirs — the two agree on which
239
+ * END of the scale to pick, which is what the promise is about, and may
240
+ * differ on a mid-grey background.
241
+ * @getter
242
+ */
243
+ fg_color: (ctx: Context) => string;
244
+ /**
245
+ * Is the chart type standard?
246
+ * @getter
247
+ */
248
+ is_standard: (_ctx: Context) => boolean;
249
+ /**
250
+ * The non-standard chart types. This engine draws standard candles only,
251
+ * so each is false — the same fact `is_standard` states from the other side.
252
+ * @getter
253
+ */
254
+ is_heikinashi: (_ctx: Context) => boolean;
255
+ /** @getter */
256
+ is_kagi: (_ctx: Context) => boolean;
257
+ /** @getter */
258
+ is_linebreak: (_ctx: Context) => boolean;
259
+ /** @getter */
260
+ is_pnf: (_ctx: Context) => boolean;
261
+ /** @getter */
262
+ is_range: (_ctx: Context) => boolean;
263
+ /** @getter */
264
+ is_renko: (_ctx: Context) => boolean;
265
+ /**
266
+ * Opening time of the leftmost VISIBLE bar.
267
+ *
268
+ * "chart.left_visible_bar_time returns the time of the leftmost bar
269
+ * currently visible on the chart."
270
+ *
271
+ * There is no viewport here, so this is the first bar of the run unless the
272
+ * host declares one — see `Context.provideVisibleRange`, which also explains
273
+ * why that is a divergence worth stating.
274
+ * @getter
275
+ */
276
+ left_visible_bar_time: (ctx: Context) => number;
277
+ /**
278
+ * Opening time of the rightmost VISIBLE bar. See `left_visible_bar_time`.
279
+ * @getter
280
+ */
281
+ right_visible_bar_time: (ctx: Context) => number;
282
+ };
283
+ export {};
@@ -0,0 +1,51 @@
1
+ /**
2
+ * runtime/v1/stdlib/color.ts
3
+ * Implements Pine Script Color Namespace.
4
+ * STRATEGY: Performance. We use Hex Strings (#RRGGBBAA) directly.
5
+ * We do NOT create Objects.
6
+ */
7
+ declare const color: {
8
+ /**
9
+ * color.new(color, transp)
10
+ * Adds transparency to an existing hex color.
11
+ * @param colorStr Hex string "#RRGGBB" or "#RRGGBBAA"
12
+ * @param transp Number 0 (Opaque) to 100 (Invisible)
13
+ */
14
+ new: (colorStr: any, transp: any) => string;
15
+ /**
16
+ * color.rgb(red, green, blue, transp)
17
+ * Creates a color from RGBA components.
18
+ */
19
+ rgb: (r: any, g: any, b: any, transp?: any) => string;
20
+ /**
21
+ * `color.from_gradient(value, bottom_value, top_value, bottom_color, top_color)`
22
+ *
23
+ * Added with `color.rgb` and `color.r/g/b/t` in the same v4 batch (May
24
+ * 2021): "returns color calculated from the linear gradient between
25
+ * bottom_color to top_color".
26
+ *
27
+ * Every OTHER member of that batch was implemented; this one was missed,
28
+ * and the gap was invisible because `color` IS a namespace — a missing
29
+ * MEMBER of a real namespace emits `opsv2_color.opsv2_from_gradient`,
30
+ * reads `undefined`, and only fails when the result is called. Two of
31
+ * TradingView's own documentation examples died that way with "The
32
+ * provided reference is not a function".
33
+ *
34
+ * `value` is clamped to [bottom_value, top_value] — outside the range Pine
35
+ * returns the endpoint colour rather than extrapolating — and RGB
36
+ * channels truncate while transparency is interpolated in 0-100 space and
37
+ * rounded (Pine Logs probe color-gradient).
38
+ */
39
+ from_gradient: (value: any, bottomValue: any, topValue: any, bottomColor: any, topColor: any) => string;
40
+ /**
41
+ * color.r / .g / .b / .t
42
+ * Extract components from a hex string.
43
+ */
44
+ r: (c: any) => number;
45
+ g: (c: any) => number;
46
+ b: (c: any) => number;
47
+ t: (c: any) => number;
48
+ };
49
+ export default color;
50
+ /** v4's `color.from_gradient`: not probed on TradingView, so it keeps per-channel rounding. */
51
+ export declare const legacyFromGradient: (value: any, bottomValue: any, topValue: any, bottomColor: any, topColor: any) => string;