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,196 @@
1
+ /**
2
+ * runtime/v1/stdlib/ui.ts
3
+ * Implements "Full" Plotting Support (Colors, Styles, Shapes, Fills)
4
+ */
5
+ import { Context } from "../context.js";
6
+ export declare const color_red = "#FF5252";
7
+ export declare const color_green = "#4CAF50";
8
+ export declare const color_blue = "#2196F3";
9
+ export declare const color_orange = "#FF9800";
10
+ export declare const color_teal = "#009688";
11
+ export declare const color_navy = "#3F51B5";
12
+ export declare const color_white = "#FFFFFF";
13
+ export declare const color_black = "#000000";
14
+ export declare const color_gray = "#9E9E9E";
15
+ export declare const color_purple = "#9C27B0";
16
+ export declare function plot(ctx: Context, series: number, title?: string, color?: string, linewidth?: number, style?: number, transp?: any, trackprice?: any, histbase?: any, offset?: any, join?: any, editable?: any, show_last?: any, display?: any, format?: any, precision?: any, force_overlay?: any, linestyle?: any): string;
17
+ export declare function plotshape(ctx: Context, series: boolean | number, title?: string, style?: string, location?: string, color?: string, transp?: any, offset?: any, text?: any, textcolor?: any, editable?: any, size?: any, show_last?: any, display?: any, format?: any, precision?: any, force_overlay?: any): void;
18
+ export declare function plotchar(ctx: Context, series: boolean | number, title?: string, char?: string, location?: string, color?: string, transp?: any, offset?: any, text?: any, textcolor?: any, editable?: any, size?: any, show_last?: any, display?: any, format?: any, precision?: any, force_overlay?: any): void;
19
+ export declare function hline(ctx: Context, price: number, title?: string, color?: string, linestyle?: number, linewidth?: number, editable?: any, display?: any, force_overlay?: any): string;
20
+ export declare function plotbar(ctx: Context, open: number, high: number, low: number, close: number, title?: string, color?: string, transp?: any, editable?: any, show_last?: any, display?: any, format?: any, precision?: any, force_overlay?: any): void;
21
+ export declare function plotcandle(ctx: Context, open: number, high: number, low: number, close: number, title?: string, color?: string, transp?: any, wickcolor?: any, editable?: any, show_last?: any, bordercolor?: any, display?: any, format?: any, precision?: any, force_overlay?: any): void;
22
+ export declare function plotarrow(ctx: Context, series: number, title?: string, colorup?: string, colordown?: string, _minheight?: number, _maxheight?: number): void;
23
+ export declare function bgcolor(ctx: Context, color: string, transp?: number, offset?: any, editable?: any, show_last?: any, title?: any, display?: any, force_overlay?: any): void;
24
+ export declare function barcolor(ctx: Context, color: string, offset?: any, editable?: any, show_last?: any, title?: any, display?: any): void;
25
+ export declare function fill(ctx: Context, plot1: string, plot2: string, color?: string, title?: string, editable?: boolean, fillgaps?: boolean, transp?: any, display?: any, force_overlay?: any, show_last?: any, top_value?: any, bottom_value?: any, top_color?: any, bottom_color?: any): void;
26
+ export declare const line = "line";
27
+ export declare const histogram = "histogram";
28
+ export declare const columns = "columns";
29
+ export declare const circles = "circles";
30
+ export declare const areabr = "areabr";
31
+ export declare const linebr = "linebr";
32
+ export declare const stepline = "stepline";
33
+ export declare const area = "area";
34
+ export declare const dotted = "dotted";
35
+ export declare const dashed = "dashed";
36
+ export declare const solid = "solid";
37
+ export declare const location: {
38
+ abovebar: string;
39
+ belowbar: string;
40
+ top: string;
41
+ bottom: string;
42
+ absolute: string;
43
+ };
44
+ export declare const shape: {
45
+ xcross: string;
46
+ cross: string;
47
+ triangleup: string;
48
+ triangledown: string;
49
+ flag: string;
50
+ circle: string;
51
+ arrowup: string;
52
+ arrowdown: string;
53
+ labelup: string;
54
+ labeldown: string;
55
+ square: string;
56
+ diamond: string;
57
+ };
58
+ export declare const size: {
59
+ auto: string;
60
+ tiny: string;
61
+ small: string;
62
+ normal: string;
63
+ large: string;
64
+ huge: string;
65
+ };
66
+ /**
67
+ * color(col, transp) — the v1-v3 spelling. v4 renamed it to color.new().
68
+ * Applies a transparency percentage (0 = opaque, 100 = invisible) to a hex
69
+ * colour, returning #RRGGBBAA.
70
+ *
71
+ * `color(na)` is `na`. It used to build the string "NaNff" from the NaN, so a
72
+ * label drawn with `color = color(na)` carried a colour no renderer can parse,
73
+ * where `color = na` carries `na` (NaN, which serialises as null) and is drawn
74
+ * with no fill. On TradingView the ▲/▼ labels of harness_trader_club, drawn
75
+ * with `color(na)`, show as bare text with no background (seen on the chart;
76
+ * Pine Logs do not carry colours).
77
+ */
78
+ export declare function color(col: any, transp?: any): string | number;
79
+ /**
80
+ * `tickerid(prefix, ticker, session, adjustment)` — builds a ticker id string.
81
+ *
82
+ * The FUNCTION, not the variable: `syminfo.tickerid` is the current chart's id
83
+ * and lives in ./syminfo.ts. Pine keeps functions and variables in separate
84
+ * namespaces, so v1–v4 spell both of them `tickerid`; v5 renames this one to
85
+ * `ticker.new` and leaves the variable alone.
86
+ *
87
+ * `session` and `adjustment` are accepted and IGNORED. Both select a different
88
+ * data feed on TradingView — extended hours, dividend adjustment — and this
89
+ * engine has one feed, the bars it was handed. Accepting them keeps
90
+ * `tickerid("BATS", "AAPL", session.regular)` (a v4 documentation example)
91
+ * running against the chart's own data, which is what it would produce anyway
92
+ * for a regular-session request; asking for `session.extended` gets the same
93
+ * bars, which is a divergence rather than a refusal because the ticker id is
94
+ * usually handed straight to `security()`, where an unknown symbol already
95
+ * reports itself.
96
+ */
97
+ export declare function tickerid(prefix: any, ticker: any, _session?: any, _adjustment?: any): string;
98
+ /**
99
+ * alertcondition(condition, title, message) — stub.
100
+ *
101
+ * There is no alert delivery system to integrate with locally, so the fired
102
+ * condition is recorded into the context instead of dispatched, which keeps it
103
+ * inspectable. Signature-compatible so real scripts run. See R-10.
104
+ */
105
+ export declare function alertcondition(ctx: Context, condition: any, title?: any, message?: any): void;
106
+ /**
107
+ * `alert(message, freq)` — the v4 script-alert call.
108
+ *
109
+ * ── How it differs from alertcondition() above ─────────────────────────────
110
+ *
111
+ * `alertcondition()` DECLARES an alert a user can then configure from the
112
+ * chart UI; `alert()` FIRES one at the moment it is called, with a message that
113
+ * may be computed on the bar. Both land in `ctx.alerts` here, because there is
114
+ * no delivery system to integrate with locally — recording keeps them
115
+ * inspectable, which is what a backtest can actually use.
116
+ *
117
+ * ── Realtime only, deliberately ────────────────────────────────────────────
118
+ *
119
+ * The v4 manual is explicit that an `alert()` call only triggers on realtime
120
+ * bars — it is the property that separates it from Pine Logs, which run on
121
+ * history too. Firing on every historical bar would make a backtest report
122
+ * thousands of alerts TradingView would never have sent, so `is_realtime`
123
+ * gates it.
124
+ *
125
+ * `freq` is recorded rather than enforced (the constants live in
126
+ * ./alerts.ts — see the note there on why they cannot sit in this file): the three frequencies differ in how
127
+ * many times a single realtime bar may re-fire as it updates, and that depends
128
+ * on a tick feed the host supplies rather than on anything decidable here.
129
+ *
130
+ * Source: https://www.tradingview.com/pine-script-docs/v4/essential/alerts/
131
+ *
132
+ * @returns {void}
133
+ */
134
+ export declare function alert(ctx: Context, message: any, freq?: any): void;
135
+ /**
136
+ * currency.* — the account currency the strategy reports in. Host-provided;
137
+ * NONE means "same as the symbol". See R-15.
138
+ */
139
+ export declare const currency: {
140
+ NONE: string;
141
+ USD: string;
142
+ EUR: string;
143
+ GBP: string;
144
+ JPY: string;
145
+ CHF: string;
146
+ AUD: string;
147
+ CAD: string;
148
+ NZD: string;
149
+ HKD: string;
150
+ SGD: string;
151
+ INR: string;
152
+ RUB: string;
153
+ ZAR: string;
154
+ TRY: string;
155
+ KRW: string;
156
+ BRL: string;
157
+ CNY: string;
158
+ DKK: string;
159
+ ILS: string;
160
+ MYR: string;
161
+ NOK: string;
162
+ PHP: string;
163
+ PLN: string;
164
+ SEK: string;
165
+ THB: string;
166
+ MXN: string;
167
+ ARS: string;
168
+ BTC: string;
169
+ ETH: string;
170
+ HUF: string;
171
+ CZK: string;
172
+ RON: string;
173
+ RSD: string;
174
+ ISK: string;
175
+ IDR: string;
176
+ TWD: string;
177
+ VND: string;
178
+ PKR: string;
179
+ BDT: string;
180
+ LKR: string;
181
+ KWD: string;
182
+ BHD: string;
183
+ QAR: string;
184
+ SAR: string;
185
+ AED: string;
186
+ EGP: string;
187
+ NGN: string;
188
+ KES: string;
189
+ MAD: string;
190
+ TND: string;
191
+ CLP: string;
192
+ COP: string;
193
+ PEN: string;
194
+ VES: string;
195
+ USDT: string;
196
+ };
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Built-ins that arrived with v6, or in the monthly releases after it, and
3
+ * that have no implementation to inherit.
4
+ *
5
+ * Declared as object literals for the reason ./late_members.ts gives: each
6
+ * joins a namespace that is older than it (`plot`, `syminfo`, `timeframe`),
7
+ * and the generator turns an object literal straight into dotted keys that
8
+ * `createStdlib` merges onto the namespace of the same name.
9
+ *
10
+ * Every name here is listed in V6_ONLY_NAMES (./renames6.ts), which keeps it
11
+ * out of every earlier version's view — the runtime is one implementation, and
12
+ * the views are what make a name a v6 name.
13
+ *
14
+ * Sources, by month: https://www.tradingview.com/pine-script-docs/release-notes/
15
+ */
16
+ import { Context } from "../context.js";
17
+ /** September 2025: `plot()`'s `linestyle` — "affects only style values that draw lines". */
18
+ export declare const plot: {
19
+ linestyle_solid: string;
20
+ linestyle_dashed: string;
21
+ linestyle_dotted: string;
22
+ };
23
+ export declare const syminfo: {
24
+ /**
25
+ * November 2024: "the smallest number of contracts, shares, lots, or units
26
+ * the exchange allows for trading the symbol". Host-declared, like the rest
27
+ * of `syminfo` — see Context.provideSymbolInfo.
28
+ * @getter
29
+ */
30
+ mincontract: (ctx: Context) => number;
31
+ /**
32
+ * November 2024: "the ticker ID of the script's main context, even inside
33
+ * a request.*() expression". A requested expression runs in a sub-context
34
+ * that reports the REQUESTED symbol; `main` is the chart's context.
35
+ * @getter
36
+ */
37
+ main_tickerid: (ctx: Context) => string;
38
+ /**
39
+ * November 2025: the 12-character ISIN, "or an empty string if none is
40
+ * available". Host-declared.
41
+ * @getter
42
+ */
43
+ isin: (ctx: Context) => string;
44
+ /**
45
+ * July 2025: for a continuous futures symbol, "the ticker ID of the
46
+ * underlying contract. Otherwise returns na." Host-declared.
47
+ * @getter
48
+ */
49
+ current_contract: (ctx: Context) => any;
50
+ };
51
+ export declare const timeframe: {
52
+ /**
53
+ * November 2024: "the timeframe of the script's main context, even inside
54
+ * a request.*() expression" — the chart's period, multiplier and all.
55
+ * @getter
56
+ */
57
+ main_period: (ctx: Context) => string;
58
+ };
59
+ /**
60
+ * February 2025: "bid is the highest price a buyer will pay now; ask is the
61
+ * lowest price a seller will accept now. Both are available only on the "1T"
62
+ * timeframe and are na elsewhere." A quote is not in an OHLCV bar, so the host
63
+ * supplies it — Context.provideQuote.
64
+ * @getter
65
+ */
66
+ export declare function bid(ctx: Context): number;
67
+ /** @getter */
68
+ export declare function ask(ctx: Context): number;
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Timeframe arithmetic: which PERIOD a timestamp belongs to, and how to build
3
+ * higher-timeframe candles out of lower-timeframe ones.
4
+ *
5
+ * ── Why this is not in stdlib/ ──────────────────────────────────────────────
6
+ *
7
+ * Everything under runtime/v1/stdlib/ is scanned by
8
+ * scripts/common/generate-metadata.ts and every exported name there becomes a
9
+ * Pine BUILT-IN. `periodIndex` is not a Pine name and must not become one, so
10
+ * it lives one level up, where the generator does not look. `stdlib/chart.ts`
11
+ * and `context.ts` both import it.
12
+ *
13
+ * ── The specification format ────────────────────────────────────────────────
14
+ *
15
+ * From the manual: a timeframe string is a count and a unit — "S" seconds, no
16
+ * suffix minutes, "D" days, "W" weeks, "M" months — so "1", "60", "240", "D",
17
+ * "3D", "W", "12M" are all valid, and a bare unit means one of it.
18
+ */
19
+ /** Seconds in one period of `spec`, or NaN. "The typical number of seconds". */
20
+ export declare function specSeconds(spec: string): number;
21
+ /**
22
+ * One spelling for each timeframe: "1D" is "D", "1W" is "W", "d" is "D". A
23
+ * minute count keeps its number ("1", "60"), and what is not a timeframe is
24
+ * returned as it came.
25
+ */
26
+ export declare function canonicalTimeframe(spec: string): string;
27
+ /**
28
+ * The end of the bar's timeframe, given its opening time.
29
+ *
30
+ * Here rather than in stdlib/time.ts, which uses it, because
31
+ * stdlib/mtf.ts needs it too and every export under stdlib/ is a Pine
32
+ * built-in.
33
+ *
34
+ * Pine reports a bar's CLOSING time as the instant the next bar of the same
35
+ * timeframe opens, so this is calendar arithmetic rather than a fixed offset:
36
+ * a month is not 30 days and a week is not always 7 × 24 h across a DST
37
+ * boundary. Days/weeks/months are advanced on the UTC calendar for the same
38
+ * reason every other accessor in this file reads UTC — see the header.
39
+ *
40
+ * Resolution strings follow TradingView's spelling: "1"/"5"/"60" are minutes,
41
+ * "30S" is seconds, and "D"/"W"/"M" carry an optional multiplier ("2W").
42
+ */
43
+ export declare function periodEnd(openTime: number, resolution: string): number;
44
+ /**
45
+ * Which period of `spec` the instant `t` falls in.
46
+ *
47
+ * Two bars share a period index exactly when they belong to the same bar of
48
+ * that timeframe. That is all a caller needs, so the origin does not matter as
49
+ * long as it is stable — except for weeks and months, where it decides which
50
+ * DAY a bar starts on and is therefore visible.
51
+ *
52
+ * Seconds, minutes and days divide the epoch evenly and are grouped modularly.
53
+ * Weeks are grouped from a Monday. Months are grouped by the CALENDAR, because
54
+ * they are not a fixed length: "1M" is 28 to 31 days, and a nominal 30-day
55
+ * window drifts a whole month out of step inside two years.
56
+ *
57
+ * Returns NaN for a spec that is not a timeframe, which no bar can match.
58
+ */
59
+ export declare function periodIndex(t: number, spec: string): number;
60
+ /** One OHLCV bar — the same shape `SecurityCandle` has in context.ts. */
61
+ export interface Candle {
62
+ time: number;
63
+ open: number;
64
+ high: number;
65
+ low: number;
66
+ close: number;
67
+ volume: number;
68
+ /** Set on a DERIVED candle whose last chart bar is known: when it ends. See `DerivedSeries`. */
69
+ closeTime?: number;
70
+ }
71
+ /**
72
+ * Fold consecutive bars into the bars of a HIGHER timeframe.
73
+ *
74
+ * A higher-timeframe bar IS the aggregate of the lower-timeframe bars inside
75
+ * it: it opens where the first one opened, closes where the last one closed,
76
+ * spans their extremes, and carries their total volume. Nothing here is an
77
+ * approximation — this is the relationship between the two series.
78
+ *
79
+ * `bars` must be in ascending time order and must contain ONLY bars the script
80
+ * has already seen. Aggregating the whole dataset would hand a script the
81
+ * future: the last group would close before its final bar had arrived.
82
+ */
83
+ export declare function aggregate(bars: readonly Candle[], spec: string): Candle[];
84
+ /**
85
+ * A higher-timeframe series that grows one chart bar at a time, giving what
86
+ * `aggregate` gives over the same bars without folding them all again.
87
+ *
88
+ * Only the LAST chart bar can change between two calls (a live re-tick
89
+ * overwrites it), so the fold is undone for that bar and redone from it:
90
+ * `before` is the last candle as it stood before that bar was folded in, or
91
+ * null when that bar opened the candle.
92
+ */
93
+ export declare class DerivedSeries {
94
+ private readonly spec;
95
+ private readonly chartMs?;
96
+ readonly candles: Candle[];
97
+ /** How many chart bars are folded in. */
98
+ upto: number;
99
+ private before;
100
+ private valid;
101
+ /** The bar before the last one folded: if it is not there now, `bars` is another run's. */
102
+ private anchor;
103
+ /** Where the current session began, once a gap has shown there is one; null until then. */
104
+ private session;
105
+ private prevT;
106
+ private curKey;
107
+ /** The three of them as they stood before the last bar was folded, to undo it. */
108
+ private saved;
109
+ private readonly spanMs;
110
+ private readonly intraday;
111
+ /**
112
+ * `chartMs` is the chart's own bar length. With it, an intraday spec (minutes,
113
+ * hours) is anchored at the first bar of each SESSION, which the engine
114
+ * infers: a gap between consecutive chart bars longer than both one chart
115
+ * interval and the requested span starts a new one. A stock's hourly bars then
116
+ * open at 09:30, 10:30, ... as TradingView's do
117
+ * (conformance/v5/parity/harness_security_session.AAPL.5.csv), where clock
118
+ * buckets would open at 09:00. A 24/7 series has no such gap, so its buckets
119
+ * stay on the clock; so does the first session of the series, whose start is
120
+ * not known. Days, weeks and months are calendar buckets either way.
121
+ */
122
+ constructor(spec: string, chartMs?: number);
123
+ /** The bucket of a bar at `t`; moves the session state on. */
124
+ private keyOf;
125
+ /** The bucket a bar at `t` WOULD be in, leaving the state alone. */
126
+ private peekKey;
127
+ /**
128
+ * Folds `bars[0 .. seen)`, of which those before `upto - 1` are as they were.
129
+ *
130
+ * `nextTime` is the open of the chart bar after the last one folded, when the
131
+ * caller knows it (a history run: the dataset is in hand). The last candle is
132
+ * then complete if that bar falls in another bucket (or after a session gap),
133
+ * and ends with its own last bar, which `closeTime` records: 15:55 + 5 minutes
134
+ * for a session's last hourly or daily bar, instead of a clock end that comes
135
+ * later. Where it is not known (the live bar), `closeTime` stays unset and the
136
+ * calendar end applies.
137
+ */
138
+ extend(bars: readonly Candle[], seen: number, nextTime?: number): void;
139
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Named time zones, without a time-zone database.
3
+ *
4
+ * `Intl.DateTimeFormat` already carries one, so the offset of a zone at a given
5
+ * instant can be recovered by formatting that instant IN the zone and reading
6
+ * the result back as if it were UTC: the difference is the offset. That handles
7
+ * daylight saving without a table, because ICU already knows.
8
+ *
9
+ * Not in runtime/v1/stdlib/, where every exported name would become a Pine
10
+ * built-in — see the header of ./timeframes.ts for the same reason.
11
+ */
12
+ /**
13
+ * The UTC instant of a wall-clock reading in `zone`.
14
+ *
15
+ * The inverse of `asIfUtc`, and iterated once: the offset that applies is the
16
+ * one at the ANSWER, not at the guess, and near a daylight-saving boundary
17
+ * those differ by an hour. One correction is enough for every real zone.
18
+ */
19
+ export declare function utcFromZoned(year: number, month: number, day: number, hour: number, minute: number, second: number, zone: string | undefined): number;
20
+ /**
21
+ * An instant re-expressed so its UTC fields ARE the wall clock in `zone`.
22
+ *
23
+ * The forward direction of `utcFromZoned`, and the one the calendar accessors
24
+ * need: `hour(t, "America/New_York")` wants the hour a New York clock shows at
25
+ * `t`, which is `new Date(zonedFields(t, "America/New_York")).getUTCHours()`.
26
+ *
27
+ * The result is NOT a real instant and must not be compared with one. It exists
28
+ * to be read field by field.
29
+ */
30
+ export declare function zonedFields(ms: number, zone: string | undefined): number;
@@ -0,0 +1,51 @@
1
+ import { PineVersion } from "./version.js";
2
+ import { LanguageProfile } from "./profiles/index.js";
3
+ /** Versions with a working pipeline, ascending. */
4
+ export declare const IMPLEMENTED_VERSIONS: readonly PineVersion[];
5
+ export interface TranspileOptions {
6
+ /**
7
+ * Force a version, ignoring any //@version annotation. Used by the
8
+ * cross-version test matrix to run one source under every version.
9
+ */
10
+ version?: PineVersion;
11
+ /**
12
+ * Sources for the libraries a v5 script `import`s, keyed by path
13
+ * (`"someuser/somelib/1"`).
14
+ *
15
+ * A library lives on TradingView and this engine has no way to fetch one, so
16
+ * the host provides it — the same arrangement as `Context.provideSecurityData`
17
+ * for higher-timeframe candles. An import with no source is refused rather
18
+ * than stubbed out.
19
+ */
20
+ libraries?: Readonly<Record<string, string>>;
21
+ /**
22
+ * Apply the checks TradingView's chart editor makes (v5/v6 only). `Session`
23
+ * sets it; a bare transpile leaves it off so fixtures can be partial scripts.
24
+ */
25
+ editorRules?: boolean;
26
+ }
27
+ export interface CompileResult {
28
+ js: string;
29
+ version: PineVersion;
30
+ profile: LanguageProfile;
31
+ }
32
+ /**
33
+ * Transpiles Pine Script and reports which version was applied.
34
+ * @throws UnsupportedVersionError annotation names a version outside 1–5
35
+ * @throws UnimplementedVersionError version is recognised but not built yet
36
+ */
37
+ export declare function compileScript(source: string, opts?: TranspileOptions): CompileResult;
38
+ /**
39
+ * Transpiles Pine Script to JavaScript.
40
+ *
41
+ * The version comes from the `//@version` annotation; a script without one is
42
+ * Pine Script v1. Use `compileScript` when you also need the resolved version.
43
+ */
44
+ export declare function transpile(source: string, opts?: TranspileOptions): string;
45
+ /**
46
+ * The hint for an error found after the transpiler, on the compile dry run: ""
47
+ * unless the source has no `//@version` line and parses as v5 or v6.
48
+ */
49
+ export declare function noVersionHint(source: string): string;
50
+ export { detectVersion } from "./version.js";
51
+ export type { PineVersion } from "./version.js";
@@ -0,0 +1,29 @@
1
+ export interface ImportsOptions {
2
+ /**
3
+ * The version the script will be compiled as, when the host forces one
4
+ * (`TranspileOptions.version`) instead of letting `//@version` decide.
5
+ */
6
+ readonly version?: number;
7
+ }
8
+ /**
9
+ * The import paths written in one source, in order, without duplicates.
10
+ * Only v5 has `import`: a script annotated v1–v4 (or forced to one), or with a
11
+ * version this engine does not know, imports nothing as far as the host is
12
+ * concerned.
13
+ */
14
+ export declare function importsOf(source: string, opts?: ImportsOptions): string[];
15
+ /**
16
+ * Every library path the script needs that is not in `supplied`, following
17
+ * imports through the supplied ones. Empty means every library the script
18
+ * reaches has a source; whether they then compile -- a library that does not
19
+ * declare `library()`, one that exports nothing, libraries that import each
20
+ * other in a cycle, a chain deeper than the compiler allows -- is a compile
21
+ * error that names the library.
22
+ *
23
+ * A source that does not parse yields the imports error recovery could still
24
+ * read. For a supplied library that means its later imports may go unasked:
25
+ * that library cannot compile whatever else is supplied, and the compile says
26
+ * so by name ("parsing the library 'x/Y/1' failed"), which is the error the
27
+ * author has to fix first.
28
+ */
29
+ export declare function requiredLibraries(source: string, supplied?: Readonly<Record<string, string>>, opts?: ImportsOptions): string[];
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Parse errors a person can act on.
3
+ *
4
+ * ANTLR's messages describe the parser's state — `token recognition error at:
5
+ * '$'`, `no viable alternative at input 'if…<BEGIN>…'` — and say nothing about
6
+ * what went wrong in the script. Four causes are common enough to name:
7
+ *
8
+ * - a string literal broken across two lines
9
+ * - `$` in a name
10
+ * - a script with no `//@version` line whose body is newer than v1
11
+ * - a file that ends in the middle of a statement (a truncated paste)
12
+ *
13
+ * The parser's own text is kept in `original`; `line` and `column` are never
14
+ * touched. The visitor's errors get the same treatment for the one case that
15
+ * is about the end of the file: see `explainEndOfFile`.
16
+ */
17
+ import type { ParserError } from "../../parser/v1/index.js";
18
+ export interface ParseErrorContext {
19
+ /** The source has no `//@version` line at all. */
20
+ noVersionLine: boolean;
21
+ /** True when the body parses as v5 or v6, evaluated only if asked. */
22
+ parsesAsNewer: () => boolean;
23
+ }
24
+ export declare function explainParseErrors(source: string, errors: readonly ParserError[], ctx: ParseErrorContext): ParserError[];
25
+ /**
26
+ * What to add to ANY error in a script with no `//@version` line that parses
27
+ * as v5 or v6: the script was read as v1 and the error is very likely only
28
+ * that. A parse error, a transpile error ("Undeclared identifier 'indicator'")
29
+ * and a built-in the dry run finds missing all start here.
30
+ */
31
+ export declare const NO_VERSION_HINT: string;
32
+ /**
33
+ * The visitor's "Undeclared identifier 'x'" and "'ns' has no member 'x'" are
34
+ * ordinary errors in the middle of a file. At the very end they are a script
35
+ * cut off in the middle of a word: nothing but whitespace follows the name.
36
+ * Returns the sentence to append, or null.
37
+ */
38
+ export declare function explainEndOfFile(source: string, message: string): string | null;