oakscriptjs 0.15.2 → 0.15.4
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.
- package/dist/cjs/check/format.d.ts.map +1 -1
- package/dist/cjs/check/format.js +5 -0
- package/dist/cjs/check/format.js.map +2 -2
- package/dist/cjs/check/harness.d.ts +24 -0
- package/dist/cjs/check/harness.d.ts.map +1 -1
- package/dist/cjs/check/harness.js +33 -0
- package/dist/cjs/check/harness.js.map +2 -2
- package/dist/cjs/check/index.d.ts +9 -0
- package/dist/cjs/check/index.d.ts.map +1 -1
- package/dist/cjs/check/index.js +7 -0
- package/dist/cjs/check/index.js.map +2 -2
- package/dist/cjs/color/index.d.ts.map +1 -1
- package/dist/cjs/color/index.js +19 -2
- package/dist/cjs/color/index.js.map +2 -2
- package/dist/cjs/color/parse.d.ts +8 -6
- package/dist/cjs/color/parse.d.ts.map +1 -1
- package/dist/cjs/color/parse.js +15 -2
- package/dist/cjs/color/parse.js.map +2 -2
- package/dist/cjs/drawing/registry.d.ts +32 -0
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +179 -45
- package/dist/cjs/drawing/registry.js.map +2 -2
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/runtime/series.d.ts +16 -0
- package/dist/cjs/runtime/series.d.ts.map +1 -1
- package/dist/cjs/runtime/series.js +19 -2
- package/dist/cjs/runtime/series.js.map +2 -2
- package/dist/cjs/script/closure-writes.d.ts +16 -0
- package/dist/cjs/script/closure-writes.d.ts.map +1 -0
- package/dist/cjs/script/closure-writes.js +435 -0
- package/dist/cjs/script/closure-writes.js.map +7 -0
- package/dist/cjs/script/globals.d.ts +2 -0
- package/dist/cjs/script/globals.d.ts.map +1 -1
- package/dist/cjs/script/globals.js.map +1 -1
- package/dist/cjs/script/incremental.d.ts +140 -0
- package/dist/cjs/script/incremental.d.ts.map +1 -0
- package/dist/cjs/script/incremental.js +142 -0
- package/dist/cjs/script/incremental.js.map +7 -0
- package/dist/cjs/script/index.d.ts +116 -31
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +471 -25
- package/dist/cjs/script/index.js.map +3 -3
- package/dist/cjs/session/bars.d.ts +3 -0
- package/dist/cjs/session/bars.d.ts.map +1 -1
- package/dist/cjs/session/bars.js +10 -3
- package/dist/cjs/session/bars.js.map +2 -2
- package/dist/cjs/session/calendar.d.ts +19 -6
- package/dist/cjs/session/calendar.d.ts.map +1 -1
- package/dist/cjs/session/calendar.js +57 -18
- package/dist/cjs/session/calendar.js.map +2 -2
- package/dist/cjs/session/context.d.ts.map +1 -1
- package/dist/cjs/session/context.js +25 -8
- package/dist/cjs/session/context.js.map +2 -2
- package/dist/cjs/str/numberformat.d.ts.map +1 -1
- package/dist/cjs/str/numberformat.js +23 -0
- package/dist/cjs/str/numberformat.js.map +2 -2
- package/dist/cjs/strategy/index.d.ts +8 -0
- package/dist/cjs/strategy/index.d.ts.map +1 -1
- package/dist/cjs/strategy/index.js.map +1 -1
- package/dist/cjs/ta/running-sum.d.ts.map +1 -1
- package/dist/cjs/ta/running-sum.js +72 -3
- package/dist/cjs/ta/running-sum.js.map +2 -2
- package/dist/cjs/ta/step.d.ts +476 -0
- package/dist/cjs/ta/step.d.ts.map +1 -0
- package/dist/cjs/ta/step.js +1000 -0
- package/dist/cjs/ta/step.js.map +7 -0
- package/dist/cjs/ta-series.d.ts +1 -1
- package/dist/cjs/ta-series.js +3 -3
- package/dist/cjs/ta-series.js.map +1 -1
- package/dist/cjs/time/timezone.d.ts +13 -1
- package/dist/cjs/time/timezone.d.ts.map +1 -1
- package/dist/cjs/time/timezone.js +52 -13
- package/dist/cjs/time/timezone.js.map +2 -2
- package/dist/esm/check/format.d.ts.map +1 -1
- package/dist/esm/check/format.js +5 -0
- package/dist/esm/check/format.js.map +1 -1
- package/dist/esm/check/harness.d.ts +24 -0
- package/dist/esm/check/harness.d.ts.map +1 -1
- package/dist/esm/check/harness.js +40 -6
- package/dist/esm/check/harness.js.map +1 -1
- package/dist/esm/check/index.d.ts +9 -0
- package/dist/esm/check/index.d.ts.map +1 -1
- package/dist/esm/check/index.js +11 -1
- package/dist/esm/check/index.js.map +1 -1
- package/dist/esm/color/index.d.ts.map +1 -1
- package/dist/esm/color/index.js +23 -3
- package/dist/esm/color/index.js.map +1 -1
- package/dist/esm/color/parse.d.ts +8 -6
- package/dist/esm/color/parse.d.ts.map +1 -1
- package/dist/esm/color/parse.js +19 -2
- package/dist/esm/color/parse.js.map +1 -1
- package/dist/esm/drawing/registry.d.ts +32 -0
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +214 -51
- package/dist/esm/drawing/registry.js.map +1 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/runtime/series.d.ts +16 -0
- package/dist/esm/runtime/series.d.ts.map +1 -1
- package/dist/esm/runtime/series.js +21 -1
- package/dist/esm/runtime/series.js.map +1 -1
- package/dist/esm/script/closure-writes.d.ts +16 -0
- package/dist/esm/script/closure-writes.d.ts.map +1 -0
- package/dist/esm/script/closure-writes.js +323 -0
- package/dist/esm/script/closure-writes.js.map +1 -0
- package/dist/esm/script/globals.d.ts +2 -0
- package/dist/esm/script/globals.d.ts.map +1 -1
- package/dist/esm/script/globals.js.map +1 -1
- package/dist/esm/script/incremental.d.ts +140 -0
- package/dist/esm/script/incremental.d.ts.map +1 -0
- package/dist/esm/script/incremental.js +173 -0
- package/dist/esm/script/incremental.js.map +1 -0
- package/dist/esm/script/index.d.ts +116 -31
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +561 -24
- package/dist/esm/script/index.js.map +1 -1
- package/dist/esm/session/bars.d.ts +3 -0
- package/dist/esm/session/bars.d.ts.map +1 -1
- package/dist/esm/session/bars.js +11 -3
- package/dist/esm/session/bars.js.map +1 -1
- package/dist/esm/session/calendar.d.ts +19 -6
- package/dist/esm/session/calendar.d.ts.map +1 -1
- package/dist/esm/session/calendar.js +74 -21
- package/dist/esm/session/calendar.js.map +1 -1
- package/dist/esm/session/context.d.ts.map +1 -1
- package/dist/esm/session/context.js +34 -9
- package/dist/esm/session/context.js.map +1 -1
- package/dist/esm/str/numberformat.d.ts.map +1 -1
- package/dist/esm/str/numberformat.js +30 -0
- package/dist/esm/str/numberformat.js.map +1 -1
- package/dist/esm/strategy/index.d.ts +8 -0
- package/dist/esm/strategy/index.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.js +79 -4
- package/dist/esm/ta/running-sum.js.map +1 -1
- package/dist/esm/ta/step.d.ts +476 -0
- package/dist/esm/ta/step.d.ts.map +1 -0
- package/dist/esm/ta/step.js +1089 -0
- package/dist/esm/ta/step.js.map +1 -0
- package/dist/esm/ta-series.d.ts +1 -1
- package/dist/esm/ta-series.js +4 -4
- package/dist/esm/time/timezone.d.ts +13 -1
- package/dist/esm/time/timezone.d.ts.map +1 -1
- package/dist/esm/time/timezone.js +78 -17
- package/dist/esm/time/timezone.js.map +1 -1
- package/package.json +1 -1
- package/skills/oakscript/SKILL.md +2 -2
- package/skills/oakscript/reference/check.md +5 -0
- package/skills/oakscript/reference/errors.md +0 -1
- package/skills/oakscript/reference/pine-differences.md +1 -2
- package/skills/oakscript/reference/plotting.md +5 -7
- package/skills/oakscript/reference/script-structure.md +6 -0
- package/src/check/format.ts +7 -0
- package/src/check/harness.ts +57 -0
- package/src/check/index.ts +16 -0
- package/src/color/index.ts +22 -3
- package/src/color/parse.ts +28 -3
- package/src/drawing/registry.ts +233 -50
- package/src/index.ts +4 -1
- package/src/runtime/series.ts +33 -1
- package/src/script/closure-writes.ts +279 -0
- package/src/script/globals.ts +2 -0
- package/src/script/incremental.ts +246 -0
- package/src/script/index.ts +695 -26
- package/src/session/bars.ts +11 -3
- package/src/session/calendar.ts +84 -21
- package/src/session/context.ts +36 -9
- package/src/str/numberformat.ts +29 -0
- package/src/strategy/index.ts +8 -0
- package/src/ta/running-sum.ts +78 -3
- package/src/ta/step.ts +1145 -0
- package/src/ta-series.ts +4 -4
- package/src/time/timezone.ts +76 -15
|
@@ -26,6 +26,8 @@ export declare class SessionBars {
|
|
|
26
26
|
readonly calendar: TradingCalendar;
|
|
27
27
|
readonly chart: TimeframeInfo;
|
|
28
28
|
private readonly yearDays;
|
|
29
|
+
/** Parsed timeframes: `timeOf` / `closeOf` run on every bar */
|
|
30
|
+
private readonly timeframes;
|
|
29
31
|
constructor(calendar: TradingCalendar, chartTimeframe: string);
|
|
30
32
|
/** Trading days of a calendar year, in order. */
|
|
31
33
|
private tradingDaysOf;
|
|
@@ -34,6 +36,7 @@ export declare class SessionBars {
|
|
|
34
36
|
* Periods restart each calendar year.
|
|
35
37
|
*/
|
|
36
38
|
private periodRange;
|
|
39
|
+
private timeframe;
|
|
37
40
|
private dayStart;
|
|
38
41
|
private dayEnd;
|
|
39
42
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bars.d.ts","sourceRoot":"","sources":["../../../src/session/bars.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAoB,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EACL,eAAe,EAMf,KAAK,OAAO,EACb,MAAM,eAAe,CAAC;AAiBvB,+DAA+D;AAC/D,qBAAa,WAAW;
|
|
1
|
+
{"version":3,"file":"bars.d.ts","sourceRoot":"","sources":["../../../src/session/bars.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAoB,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EACL,eAAe,EAMf,KAAK,OAAO,EAC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;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAUxI;AAED,gDAAgD;AAChD,MAAM,WAAW,YAAY;IAC3B,UAAU,EAAE,OAAO,EAAE,CAAC;IACtB,SAAS,EAAE,OAAO,EAAE,CAAC;IACrB,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,WAAW,EAAE,OAAO,EAAE,CAAC;IACvB,YAAY,EAAE,OAAO,EAAE,CAAC;IACxB,kBAAkB,EAAE,OAAO,EAAE,CAAC;IAC9B,iBAAiB,EAAE,OAAO,EAAE,CAAC;CAC9B;AAED;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,WAAW,GAAG,YAAY,CA6CnG"}
|
package/dist/cjs/session/bars.js
CHANGED
|
@@ -42,6 +42,8 @@ class SessionBars {
|
|
|
42
42
|
calendar;
|
|
43
43
|
chart;
|
|
44
44
|
yearDays = /* @__PURE__ */ new Map();
|
|
45
|
+
/** Parsed timeframes: `timeOf` / `closeOf` run on every bar */
|
|
46
|
+
timeframes = /* @__PURE__ */ new Map();
|
|
45
47
|
/** Trading days of a calendar year, in order. */
|
|
46
48
|
tradingDaysOf(year) {
|
|
47
49
|
let days = this.yearDays.get(year);
|
|
@@ -83,6 +85,11 @@ class SessionBars {
|
|
|
83
85
|
const lastMonth = Math.min(firstMonth + n - 1, 12);
|
|
84
86
|
return [(0, import_calendar.monthStart)(year * 1e4 + firstMonth * 100 + 1), (0, import_calendar.monthEnd)(year * 1e4 + lastMonth * 100 + 1)];
|
|
85
87
|
}
|
|
88
|
+
timeframe(timeframe) {
|
|
89
|
+
let tf = this.timeframes.get(timeframe);
|
|
90
|
+
if (!tf) this.timeframes.set(timeframe, tf = (0, import_timeframe.info)(timeframe));
|
|
91
|
+
return tf;
|
|
92
|
+
}
|
|
86
93
|
dayStart(date) {
|
|
87
94
|
return this.calendar.periods(date)[0][0];
|
|
88
95
|
}
|
|
@@ -96,7 +103,7 @@ class SessionBars {
|
|
|
96
103
|
* NaN on it (measured; `time("W")` keeps its value).
|
|
97
104
|
*/
|
|
98
105
|
timeOf(time, timeframe) {
|
|
99
|
-
const tf =
|
|
106
|
+
const tf = this.timeframe(timeframe);
|
|
100
107
|
const at = this.calendar.locate(time);
|
|
101
108
|
if (!at) return NaN;
|
|
102
109
|
if (tf.isintraday) {
|
|
@@ -114,7 +121,7 @@ class SessionBars {
|
|
|
114
121
|
}
|
|
115
122
|
/** PineScript `time_close(tf)` for a bar that opens at `time`. */
|
|
116
123
|
closeOf(time, timeframe) {
|
|
117
|
-
const tf =
|
|
124
|
+
const tf = this.timeframe(timeframe);
|
|
118
125
|
const at = this.calendar.locate(time);
|
|
119
126
|
if (!at) return NaN;
|
|
120
127
|
if (tf.isintraday) {
|
|
@@ -151,7 +158,7 @@ class SessionBars {
|
|
|
151
158
|
* bar completes a higher-timeframe period (request.security).
|
|
152
159
|
*/
|
|
153
160
|
periodEnd(time, timeframe) {
|
|
154
|
-
const tf =
|
|
161
|
+
const tf = this.timeframe(timeframe);
|
|
155
162
|
const at = this.calendar.locate(time);
|
|
156
163
|
if (!at) return NaN;
|
|
157
164
|
if (tf.isintraday) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/session/bars.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * PineScript values that depend on the chart timeframe and the symbol session (issue #100):\n * `time(tf)`, `time_close(tf)`, `timeframe.change(tf)`, `time_tradingday`, `session.*`.\n * PineScript rules:\n *\n * - intraday periods are aligned on the start of the session period of the trading day: 240 minutes from\n * 09:30 gives 09:30, 13:30; `time(tf)` is the start of the period that contains the bar open\n * - \"D\" is the session start of the trading day; \"W\" / \"M\" the session start of the first trading day\n * of the week (Monday to Sunday) / month\n * - intraday closes are cut at the end of the session period; the \"D\" close is the session end\n * - on intraday charts the \"W\" / \"M\" (and \"nD\") close is the session start of the next period's first\n * trading day; on D / W / M charts it is the session end of the last trading day of the week / month\n * - on D / W / M charts, intraday timeframes give the chart bar's time, and the end of the session\n * period that contains the bar open as close\n * - `time_tradingday` is 00:00 UTC of the trading day (on W / M charts, of the last trading day)\n *\n * Multipliers above one day (issue #100 follow-up): periods restart each\n * calendar year. \"nD\" groups n trading days from the first trading day of the year; \"nW\" groups n weeks from\n * the first week whose Monday is in the year; \"nM\" groups n months from January. The last group of a year\n * can be shorter.\n */\n\nimport { info, in_seconds, type TimeframeInfo } from '../timeframe/index.js';\nimport {\n TradingCalendar,\n addDays,\n dateUtcMs,\n monthEnd,\n monthStart,\n weekStart,\n type DateNum,\n} from './calendar.js';\n\ntype Unit = 'D' | 'W' | 'M';\n\nfunction unitOf(tf: TimeframeInfo): { unit: Unit; n: number } {\n return { unit: tf.isdaily ? 'D' : tf.isweekly ? 'W' : 'M', n: tf.multiplier };\n}\n\nconst yearOf = (date: DateNum): number => Math.floor(date / 10000);\n\n/** Monday of the first week of `year` (the first Monday on or after 1 January). */\nconst firstMonday = (year: number): DateNum => {\n const jan1 = year * 10000 + 101;\n const monday = weekStart(jan1);\n return monday === jan1 ? jan1 : addDays(monday, 7);\n};\n\n/** Values of the bars of one chart, for a trading calendar. */\nexport class SessionBars {\n readonly chart: TimeframeInfo;\n\n private readonly yearDays = new Map<number, DateNum[]>();\n\n constructor(\n readonly calendar: TradingCalendar,\n chartTimeframe: string\n ) {\n this.chart = info(chartTimeframe);\n }\n\n /** Trading days of a calendar year, in order. */\n private tradingDaysOf(year: number): DateNum[] {\n let days = this.yearDays.get(year);\n if (!days) {\n days = [];\n for (let date = year * 10000 + 101; yearOf(date) === year; date = addDays(date, 1)) {\n if (this.calendar.isTradingDay(date)) days.push(date);\n }\n this.yearDays.set(year, days);\n }\n return days;\n }\n\n /**\n * Dates [from, to] of the `n`-`unit` period that contains `date` (for \"D\": its first and last trading days).\n * Periods restart each calendar year.\n */\n private periodRange(date: DateNum, unit: Unit, n: number): [DateNum, DateNum] {\n if (unit === 'D') {\n if (n === 1) return [date, date];\n const days = this.tradingDaysOf(yearOf(date));\n let k = days.findIndex((d) => d >= date);\n if (k === -1) return this.periodRange(yearOf(date) * 10000 + 10000 + 101, unit, n);\n k -= k % n;\n return [days[k]!, days[Math.min(k + n, days.length) - 1]!];\n }\n if (unit === 'W') {\n const monday = weekStart(date);\n const year = yearOf(monday);\n const first = firstMonday(year);\n const next = firstMonday(year + 1);\n const k = Math.round((dateUtcMs(monday) - dateUtcMs(first)) / (7 * 86_400_000));\n const from = addDays(first, (k - (k % n)) * 7);\n const to = addDays(from, n * 7 - 1);\n return [from, to < next ? to : addDays(next, -1)];\n }\n const year = yearOf(date);\n const month = Math.floor(date / 100) % 100;\n const firstMonth = month - ((month - 1) % n);\n const lastMonth = Math.min(firstMonth + n - 1, 12);\n return [monthStart(year * 10000 + firstMonth * 100 + 1), monthEnd(year * 10000 + lastMonth * 100 + 1)];\n }\n\n private dayStart(date: DateNum): number {\n return this.calendar.periods(date)[0]![0];\n }\n\n private dayEnd(date: DateNum): number {\n const p = this.calendar.periods(date);\n return p[p.length - 1]![1];\n }\n\n /**\n * PineScript `time(tf)` for a bar that opens at `time`; NaN when no trading day is found. An intraday bar that\n * opens after an early close has no day and no intraday period: `time(\"D\")`, `time(\"240\")` and `time(\"60\")` are\n * NaN on it (measured; `time(\"W\")` keeps its value).\n */\n timeOf(time: number, timeframe: string): number {\n const tf = info(timeframe);\n const at = this.calendar.locate(time);\n if (!at) return NaN;\n if (tf.isintraday) {\n if (this.chart.isdwm) return time;\n // a bar after an early close has no intraday period either (#173)\n if (this.stray(time)) return NaN;\n const [start] = this.calendar.periods(at.date)[at.index]!;\n const ms = in_seconds(tf.period) * 1000;\n return start + Math.floor((time - start) / ms) * ms;\n }\n const { unit, n } = unitOf(tf);\n if (unit === 'D' && n === 1 && this.stray(time)) return NaN;\n const [from, to] = this.periodRange(at.date, unit, n);\n const first = this.calendar.firstTradingDay(from, to);\n return first === null ? NaN : this.dayStart(first);\n }\n\n /** PineScript `time_close(tf)` for a bar that opens at `time`. */\n closeOf(time: number, timeframe: string): number {\n const tf = info(timeframe);\n const at = this.calendar.locate(time);\n if (!at) return NaN;\n if (tf.isintraday) {\n const [, end] = this.calendar.periods(at.date)[at.index]!;\n // on D / W / M charts: the end of the session period that contains the bar open\n if (this.chart.isdwm) return end;\n if (this.stray(time)) return NaN;\n return Math.min(this.timeOf(time, timeframe) + in_seconds(tf.period) * 1000, end);\n }\n const { unit, n } = unitOf(tf);\n if (unit === 'D' && n === 1 && this.stray(time)) return NaN;\n const [from, to] = this.periodRange(at.date, unit, n);\n // intraday charts: the start of the next period, except \"1D\" (the session end)\n if (this.chart.isintraday && (unit !== 'D' || n > 1)) {\n const [nextFrom, nextTo] = this.periodRange(addDays(to, 1), unit, n);\n const first = this.calendar.firstTradingDay(nextFrom, addDays(nextTo, 31));\n return first === null ? NaN : this.dayStart(first);\n }\n const last = this.calendar.lastTradingDay(from, to);\n return last === null ? NaN : this.dayEnd(last);\n }\n\n /** Trading day of a bar: on D / W / M charts the last trading day of the chart period. */\n tradingDay(time: number): DateNum | null {\n const at = this.calendar.locate(time);\n if (!at || this.stray(time)) return null;\n if (this.chart.isdwm) {\n const { unit, n } = unitOf(this.chart);\n const [from, to] = this.periodRange(at.date, unit, n);\n return this.calendar.lastTradingDay(from, to);\n }\n return at.date;\n }\n\n /**\n * End of the `timeframe` period that contains `time`: the intraday close, the session end of the day,\n * or the session end of the last trading day of the period. Used to know whether the last loaded\n * bar completes a higher-timeframe period (request.security).\n */\n periodEnd(time: number, timeframe: string): number {\n const tf = info(timeframe);\n const at = this.calendar.locate(time);\n if (!at) return NaN;\n if (tf.isintraday) {\n const [, end] = this.calendar.periods(at.date)[at.index]!;\n return Math.min(this.timeOf(time, timeframe) + in_seconds(tf.period) * 1000, end);\n }\n const { unit, n } = unitOf(tf);\n const [from, to] = this.periodRange(at.date, unit, n);\n const last = this.calendar.lastTradingDay(from, to);\n return last === null ? NaN : this.dayEnd(last);\n }\n\n /** PineScript `time_tradingday`: 00:00 UTC of the trading day. */\n tradingDayTime(time: number): number {\n const day = this.tradingDay(time);\n return day === null ? NaN : dateUtcMs(day);\n }\n\n /**\n * Open time of the bar after the bar that opens at `time`, on the trading calendar: the next bar of the session\n * period, else the start of the next period or trading day; on D / W / M charts the session start of the first\n * trading day of the next chart period. NaN when none is found within 400 days.\n */\n nextBarTime(time: number): number {\n const at = this.calendar.locate(time);\n if (!at) return NaN;\n if (!this.chart.isdwm) {\n const periods = this.calendar.periods(at.date);\n const [start, end] = periods[at.index]!;\n const ms = in_seconds(this.chart.period) * 1000;\n const next = start + (Math.floor((time - start) / ms) + 1) * ms;\n if (at.inside && next < end) return next;\n if (at.index + 1 < periods.length) return periods[at.index + 1]![0];\n const day = this.calendar.firstTradingDay(addDays(at.date, 1), addDays(at.date, 400));\n return day === null ? NaN : this.dayStart(day);\n }\n const { unit, n } = unitOf(this.chart);\n const [, to] = this.periodRange(at.date, unit, n);\n const day = this.calendar.firstTradingDay(addDays(to, 1), addDays(to, 400));\n return day === null ? NaN : this.dayStart(day);\n }\n\n /** An intraday bar that opens after an early close of its day (see TradingCalendar.afterEarlyClose). */\n stray(time: number): boolean {\n return !this.chart.isdwm && this.calendar.afterEarlyClose(time);\n }\n\n /** True when the bar at `time` is inside the session. */\n inside(time: number): boolean {\n return this.calendar.locate(time)?.inside ?? false;\n }\n\n /** Session start and end of the trading day of `time`, or null. */\n dayBounds(time: number): { date: DateNum; start: number; end: number } | null {\n const at = this.calendar.locate(time);\n return at ? { date: at.date, start: this.dayStart(at.date), end: this.dayEnd(at.date) } : null;\n }\n}\n\n/**\n * Periods of the chart bars that open after an early close, for `request.security` (#173): such a bar has no period\n * (`time(\"D\")` and `time(\"240\")` are na on it), but PineScript resamples it into the D / W / M bar of the bar before\n * it, and into an intraday bar of its own (opening at the bar's time). The bar before it still completes its period\n * (its day period is cut there). `intradayMs` is the duration of an intraday timeframe, null for D / W / M.\n * Changes `starts` and `dayPeriods` in place.\n */\nexport function strayPeriods(starts: number[], dayPeriods: number[], times: number[], bars: SessionBars, intradayMs: number | null): void {\n for (let i = 1; i < starts.length; i++) {\n if (!bars.stray(times[i]!)) continue;\n if (Number.isNaN(starts[i]!)) {\n // intraday: periods of `intradayMs` from the first such bar (\"240\": 18:30 and 20:30 are one bar)\n const run = Number.isNaN(dayPeriods[i - 1]!) && times[i]! < starts[i - 1]! + (intradayMs ?? 0);\n starts[i] = intradayMs === null || run ? starts[i - 1]! : times[i]!;\n }\n dayPeriods[i] = NaN;\n }\n}\n\n/** PineScript `session.*` flags of each bar. */\nexport interface SessionFlags {\n isfirstbar: boolean[];\n islastbar: boolean[];\n ismarket: boolean[];\n ispremarket: boolean[];\n ispostmarket: boolean[];\n isfirstbar_regular: boolean[];\n islastbar_regular: boolean[];\n}\n\n/**\n * `session.*` flags for bars that open at `times` (in order). `regular` gives the regular trading\n * hours when the bars include extended hours. PineScript rules: flags use the bar open\n * time; first and last bars are per session period (a trading day can have two periods, e.g. after a\n * futures holiday); pre / post market compare with the regular hours of the same trading day; on\n * D / W / M charts every bar is its own session (first, last, market, first and last regular).\n * The first loaded bar is a first bar only when no earlier bar of its period can exist (it opens less than one chart\n * period after the period start). A bar that opens after an early close is in the market hours (the usual hours of\n * its day) but is no last bar.\n */\nexport function sessionFlags(times: number[], bars: SessionBars, regular: SessionBars): SessionFlags {\n const n = times.length;\n const flag = () => new Array<boolean>(n).fill(false);\n const out: SessionFlags = {\n isfirstbar: flag(), islastbar: flag(), ismarket: flag(), ispremarket: flag(), ispostmarket: flag(),\n isfirstbar_regular: flag(), islastbar_regular: flag(),\n };\n const chart = bars.chart;\n let previousPeriod = '';\n let previousRegular = '';\n for (let i = 0; i < n; i++) {\n const t = times[i]!;\n if (chart.isdwm) {\n for (const k of ['isfirstbar', 'islastbar', 'ismarket', 'isfirstbar_regular', 'islastbar_regular'] as const) out[k][i] = true;\n continue;\n }\n const at = bars.calendar.locate(t);\n if (!at) {\n previousPeriod = previousRegular = '';\n continue;\n }\n const close = bars.closeOf(t, chart.period);\n const [, end] = bars.calendar.periods(at.date)[at.index]!;\n const period = `${at.date}/${at.index}`;\n const [start] = bars.calendar.periods(at.date)[at.index]!;\n const barMs = in_seconds(chart.period) * 1000;\n // the first loaded bar: a first bar only when no earlier bar of the period can exist\n out.isfirstbar[i] = i === 0 ? t - start < barMs : period !== previousPeriod;\n const stray = bars.stray(t);\n out.islastbar[i] = !stray && close >= end;\n const regularPeriods = regular.calendar.periods(at.date);\n const r = regularPeriods.findIndex(([s, e]) => s <= t && t < e);\n const market = r !== -1;\n out.ismarket[i] = market || regular.stray(t);\n if (!market && regularPeriods.length) {\n out.ispremarket[i] = t < regularPeriods[0]![0];\n out.ispostmarket[i] = t >= regularPeriods[regularPeriods.length - 1]![1];\n }\n const regularPeriod = market ? `${at.date}/${r}` : '';\n out.isfirstbar_regular[i] = market && (i === 0 ? t - regularPeriods[r]![0] < barMs : regularPeriod !== previousRegular);\n out.islastbar_regular[i] = market && close >= regularPeriods[r]![1];\n previousPeriod = period;\n previousRegular = regularPeriod;\n }\n return out;\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAsBA,uBAAqD;AACrD,sBAQO;AAIP,SAAS,OAAO,IAA8C;AAC5D,SAAO,EAAE,MAAM,GAAG,UAAU,MAAM,GAAG,WAAW,MAAM,KAAK,GAAG,GAAG,WAAW;AAC9E;AAEA,MAAM,SAAS,CAAC,SAA0B,KAAK,MAAM,OAAO,GAAK;AAGjE,MAAM,cAAc,CAAC,SAA0B;AAC7C,QAAM,OAAO,OAAO,MAAQ;AAC5B,QAAM,aAAS,2BAAU,IAAI;AAC7B,SAAO,WAAW,OAAO,WAAO,yBAAQ,QAAQ,CAAC;AACnD;AAGO,MAAM,YAAY;AAAA,
|
|
4
|
+
"sourcesContent": ["/**\n * PineScript values that depend on the chart timeframe and the symbol session (issue #100):\n * `time(tf)`, `time_close(tf)`, `timeframe.change(tf)`, `time_tradingday`, `session.*`.\n * PineScript rules:\n *\n * - intraday periods are aligned on the start of the session period of the trading day: 240 minutes from\n * 09:30 gives 09:30, 13:30; `time(tf)` is the start of the period that contains the bar open\n * - \"D\" is the session start of the trading day; \"W\" / \"M\" the session start of the first trading day\n * of the week (Monday to Sunday) / month\n * - intraday closes are cut at the end of the session period; the \"D\" close is the session end\n * - on intraday charts the \"W\" / \"M\" (and \"nD\") close is the session start of the next period's first\n * trading day; on D / W / M charts it is the session end of the last trading day of the week / month\n * - on D / W / M charts, intraday timeframes give the chart bar's time, and the end of the session\n * period that contains the bar open as close\n * - `time_tradingday` is 00:00 UTC of the trading day (on W / M charts, of the last trading day)\n *\n * Multipliers above one day (issue #100 follow-up): periods restart each\n * calendar year. \"nD\" groups n trading days from the first trading day of the year; \"nW\" groups n weeks from\n * the first week whose Monday is in the year; \"nM\" groups n months from January. The last group of a year\n * can be shorter.\n */\n\nimport { info, in_seconds, type TimeframeInfo } from '../timeframe/index.js';\nimport {\n TradingCalendar,\n addDays,\n dateUtcMs,\n monthEnd,\n monthStart,\n weekStart,\n type DateNum,\n} from './calendar.js';\n\ntype Unit = 'D' | 'W' | 'M';\n\nfunction unitOf(tf: TimeframeInfo): { unit: Unit; n: number } {\n return { unit: tf.isdaily ? 'D' : tf.isweekly ? 'W' : 'M', n: tf.multiplier };\n}\n\nconst yearOf = (date: DateNum): number => Math.floor(date / 10000);\n\n/** Monday of the first week of `year` (the first Monday on or after 1 January). */\nconst firstMonday = (year: number): DateNum => {\n const jan1 = year * 10000 + 101;\n const monday = weekStart(jan1);\n return monday === jan1 ? jan1 : addDays(monday, 7);\n};\n\n/** Values of the bars of one chart, for a trading calendar. */\nexport class SessionBars {\n readonly chart: TimeframeInfo;\n\n private readonly yearDays = new Map<number, DateNum[]>();\n /** Parsed timeframes: `timeOf` / `closeOf` run on every bar */\n private readonly timeframes = new Map<string, TimeframeInfo>();\n\n constructor(\n readonly calendar: TradingCalendar,\n chartTimeframe: string\n ) {\n this.chart = info(chartTimeframe);\n }\n\n /** Trading days of a calendar year, in order. */\n private tradingDaysOf(year: number): DateNum[] {\n let days = this.yearDays.get(year);\n if (!days) {\n days = [];\n for (let date = year * 10000 + 101; yearOf(date) === year; date = addDays(date, 1)) {\n if (this.calendar.isTradingDay(date)) days.push(date);\n }\n this.yearDays.set(year, days);\n }\n return days;\n }\n\n /**\n * Dates [from, to] of the `n`-`unit` period that contains `date` (for \"D\": its first and last trading days).\n * Periods restart each calendar year.\n */\n private periodRange(date: DateNum, unit: Unit, n: number): [DateNum, DateNum] {\n if (unit === 'D') {\n if (n === 1) return [date, date];\n const days = this.tradingDaysOf(yearOf(date));\n let k = days.findIndex((d) => d >= date);\n if (k === -1) return this.periodRange(yearOf(date) * 10000 + 10000 + 101, unit, n);\n k -= k % n;\n return [days[k]!, days[Math.min(k + n, days.length) - 1]!];\n }\n if (unit === 'W') {\n const monday = weekStart(date);\n const year = yearOf(monday);\n const first = firstMonday(year);\n const next = firstMonday(year + 1);\n const k = Math.round((dateUtcMs(monday) - dateUtcMs(first)) / (7 * 86_400_000));\n const from = addDays(first, (k - (k % n)) * 7);\n const to = addDays(from, n * 7 - 1);\n return [from, to < next ? to : addDays(next, -1)];\n }\n const year = yearOf(date);\n const month = Math.floor(date / 100) % 100;\n const firstMonth = month - ((month - 1) % n);\n const lastMonth = Math.min(firstMonth + n - 1, 12);\n return [monthStart(year * 10000 + firstMonth * 100 + 1), monthEnd(year * 10000 + lastMonth * 100 + 1)];\n }\n\n private timeframe(timeframe: string): TimeframeInfo {\n let tf = this.timeframes.get(timeframe);\n if (!tf) this.timeframes.set(timeframe, (tf = info(timeframe)));\n return tf;\n }\n\n private dayStart(date: DateNum): number {\n return this.calendar.periods(date)[0]![0];\n }\n\n private dayEnd(date: DateNum): number {\n const p = this.calendar.periods(date);\n return p[p.length - 1]![1];\n }\n\n /**\n * PineScript `time(tf)` for a bar that opens at `time`; NaN when no trading day is found. An intraday bar that\n * opens after an early close has no day and no intraday period: `time(\"D\")`, `time(\"240\")` and `time(\"60\")` are\n * NaN on it (measured; `time(\"W\")` keeps its value).\n */\n timeOf(time: number, timeframe: string): number {\n const tf = this.timeframe(timeframe);\n const at = this.calendar.locate(time);\n if (!at) return NaN;\n if (tf.isintraday) {\n if (this.chart.isdwm) return time;\n // a bar after an early close has no intraday period either (#173)\n if (this.stray(time)) return NaN;\n const [start] = this.calendar.periods(at.date)[at.index]!;\n const ms = in_seconds(tf.period) * 1000;\n return start + Math.floor((time - start) / ms) * ms;\n }\n const { unit, n } = unitOf(tf);\n if (unit === 'D' && n === 1 && this.stray(time)) return NaN;\n const [from, to] = this.periodRange(at.date, unit, n);\n const first = this.calendar.firstTradingDay(from, to);\n return first === null ? NaN : this.dayStart(first);\n }\n\n /** PineScript `time_close(tf)` for a bar that opens at `time`. */\n closeOf(time: number, timeframe: string): number {\n const tf = this.timeframe(timeframe);\n const at = this.calendar.locate(time);\n if (!at) return NaN;\n if (tf.isintraday) {\n const [, end] = this.calendar.periods(at.date)[at.index]!;\n // on D / W / M charts: the end of the session period that contains the bar open\n if (this.chart.isdwm) return end;\n if (this.stray(time)) return NaN;\n return Math.min(this.timeOf(time, timeframe) + in_seconds(tf.period) * 1000, end);\n }\n const { unit, n } = unitOf(tf);\n if (unit === 'D' && n === 1 && this.stray(time)) return NaN;\n const [from, to] = this.periodRange(at.date, unit, n);\n // intraday charts: the start of the next period, except \"1D\" (the session end)\n if (this.chart.isintraday && (unit !== 'D' || n > 1)) {\n const [nextFrom, nextTo] = this.periodRange(addDays(to, 1), unit, n);\n const first = this.calendar.firstTradingDay(nextFrom, addDays(nextTo, 31));\n return first === null ? NaN : this.dayStart(first);\n }\n const last = this.calendar.lastTradingDay(from, to);\n return last === null ? NaN : this.dayEnd(last);\n }\n\n /** Trading day of a bar: on D / W / M charts the last trading day of the chart period. */\n tradingDay(time: number): DateNum | null {\n const at = this.calendar.locate(time);\n if (!at || this.stray(time)) return null;\n if (this.chart.isdwm) {\n const { unit, n } = unitOf(this.chart);\n const [from, to] = this.periodRange(at.date, unit, n);\n return this.calendar.lastTradingDay(from, to);\n }\n return at.date;\n }\n\n /**\n * End of the `timeframe` period that contains `time`: the intraday close, the session end of the day,\n * or the session end of the last trading day of the period. Used to know whether the last loaded\n * bar completes a higher-timeframe period (request.security).\n */\n periodEnd(time: number, timeframe: string): number {\n const tf = this.timeframe(timeframe);\n const at = this.calendar.locate(time);\n if (!at) return NaN;\n if (tf.isintraday) {\n const [, end] = this.calendar.periods(at.date)[at.index]!;\n return Math.min(this.timeOf(time, timeframe) + in_seconds(tf.period) * 1000, end);\n }\n const { unit, n } = unitOf(tf);\n const [from, to] = this.periodRange(at.date, unit, n);\n const last = this.calendar.lastTradingDay(from, to);\n return last === null ? NaN : this.dayEnd(last);\n }\n\n /** PineScript `time_tradingday`: 00:00 UTC of the trading day. */\n tradingDayTime(time: number): number {\n const day = this.tradingDay(time);\n return day === null ? NaN : dateUtcMs(day);\n }\n\n /**\n * Open time of the bar after the bar that opens at `time`, on the trading calendar: the next bar of the session\n * period, else the start of the next period or trading day; on D / W / M charts the session start of the first\n * trading day of the next chart period. NaN when none is found within 400 days.\n */\n nextBarTime(time: number): number {\n const at = this.calendar.locate(time);\n if (!at) return NaN;\n if (!this.chart.isdwm) {\n const periods = this.calendar.periods(at.date);\n const [start, end] = periods[at.index]!;\n const ms = in_seconds(this.chart.period) * 1000;\n const next = start + (Math.floor((time - start) / ms) + 1) * ms;\n if (at.inside && next < end) return next;\n if (at.index + 1 < periods.length) return periods[at.index + 1]![0];\n const day = this.calendar.firstTradingDay(addDays(at.date, 1), addDays(at.date, 400));\n return day === null ? NaN : this.dayStart(day);\n }\n const { unit, n } = unitOf(this.chart);\n const [, to] = this.periodRange(at.date, unit, n);\n const day = this.calendar.firstTradingDay(addDays(to, 1), addDays(to, 400));\n return day === null ? NaN : this.dayStart(day);\n }\n\n /** An intraday bar that opens after an early close of its day (see TradingCalendar.afterEarlyClose). */\n stray(time: number): boolean {\n return !this.chart.isdwm && this.calendar.afterEarlyClose(time);\n }\n\n /** True when the bar at `time` is inside the session. */\n inside(time: number): boolean {\n return this.calendar.locate(time)?.inside ?? false;\n }\n\n /** Session start and end of the trading day of `time`, or null. */\n dayBounds(time: number): { date: DateNum; start: number; end: number } | null {\n const at = this.calendar.locate(time);\n return at ? { date: at.date, start: this.dayStart(at.date), end: this.dayEnd(at.date) } : null;\n }\n}\n\n/**\n * Periods of the chart bars that open after an early close, for `request.security` (#173): such a bar has no period\n * (`time(\"D\")` and `time(\"240\")` are na on it), but PineScript resamples it into the D / W / M bar of the bar before\n * it, and into an intraday bar of its own (opening at the bar's time). The bar before it still completes its period\n * (its day period is cut there). `intradayMs` is the duration of an intraday timeframe, null for D / W / M.\n * Changes `starts` and `dayPeriods` in place.\n */\nexport function strayPeriods(starts: number[], dayPeriods: number[], times: number[], bars: SessionBars, intradayMs: number | null): void {\n for (let i = 1; i < starts.length; i++) {\n if (!bars.stray(times[i]!)) continue;\n if (Number.isNaN(starts[i]!)) {\n // intraday: periods of `intradayMs` from the first such bar (\"240\": 18:30 and 20:30 are one bar)\n const run = Number.isNaN(dayPeriods[i - 1]!) && times[i]! < starts[i - 1]! + (intradayMs ?? 0);\n starts[i] = intradayMs === null || run ? starts[i - 1]! : times[i]!;\n }\n dayPeriods[i] = NaN;\n }\n}\n\n/** PineScript `session.*` flags of each bar. */\nexport interface SessionFlags {\n isfirstbar: boolean[];\n islastbar: boolean[];\n ismarket: boolean[];\n ispremarket: boolean[];\n ispostmarket: boolean[];\n isfirstbar_regular: boolean[];\n islastbar_regular: boolean[];\n}\n\n/**\n * `session.*` flags for bars that open at `times` (in order). `regular` gives the regular trading\n * hours when the bars include extended hours. PineScript rules: flags use the bar open\n * time; first and last bars are per session period (a trading day can have two periods, e.g. after a\n * futures holiday); pre / post market compare with the regular hours of the same trading day; on\n * D / W / M charts every bar is its own session (first, last, market, first and last regular).\n * The first loaded bar is a first bar only when no earlier bar of its period can exist (it opens less than one chart\n * period after the period start). A bar that opens after an early close is in the market hours (the usual hours of\n * its day) but is no last bar.\n */\nexport function sessionFlags(times: number[], bars: SessionBars, regular: SessionBars): SessionFlags {\n const n = times.length;\n const flag = () => new Array<boolean>(n).fill(false);\n const out: SessionFlags = {\n isfirstbar: flag(), islastbar: flag(), ismarket: flag(), ispremarket: flag(), ispostmarket: flag(),\n isfirstbar_regular: flag(), islastbar_regular: flag(),\n };\n const chart = bars.chart;\n let previousPeriod = '';\n let previousRegular = '';\n for (let i = 0; i < n; i++) {\n const t = times[i]!;\n if (chart.isdwm) {\n for (const k of ['isfirstbar', 'islastbar', 'ismarket', 'isfirstbar_regular', 'islastbar_regular'] as const) out[k][i] = true;\n continue;\n }\n const at = bars.calendar.locate(t);\n if (!at) {\n previousPeriod = previousRegular = '';\n continue;\n }\n const close = bars.closeOf(t, chart.period);\n const [, end] = bars.calendar.periods(at.date)[at.index]!;\n const period = `${at.date}/${at.index}`;\n const [start] = bars.calendar.periods(at.date)[at.index]!;\n const barMs = in_seconds(chart.period) * 1000;\n // the first loaded bar: a first bar only when no earlier bar of the period can exist\n out.isfirstbar[i] = i === 0 ? t - start < barMs : period !== previousPeriod;\n const stray = bars.stray(t);\n out.islastbar[i] = !stray && close >= end;\n const regularPeriods = regular.calendar.periods(at.date);\n const r = regularPeriods.findIndex(([s, e]) => s <= t && t < e);\n const market = r !== -1;\n out.ismarket[i] = market || regular.stray(t);\n if (!market && regularPeriods.length) {\n out.ispremarket[i] = t < regularPeriods[0]![0];\n out.ispostmarket[i] = t >= regularPeriods[regularPeriods.length - 1]![1];\n }\n const regularPeriod = market ? `${at.date}/${r}` : '';\n out.isfirstbar_regular[i] = market && (i === 0 ? t - regularPeriods[r]![0] < barMs : regularPeriod !== previousRegular);\n out.islastbar_regular[i] = market && close >= regularPeriods[r]![1];\n previousPeriod = period;\n previousRegular = regularPeriod;\n }\n return out;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAsBA,uBAAqD;AACrD,sBAQO;AAIP,SAAS,OAAO,IAA8C;AAC5D,SAAO,EAAE,MAAM,GAAG,UAAU,MAAM,GAAG,WAAW,MAAM,KAAK,GAAG,GAAG,WAAW;AAC9E;AAEA,MAAM,SAAS,CAAC,SAA0B,KAAK,MAAM,OAAO,GAAK;AAGjE,MAAM,cAAc,CAAC,SAA0B;AAC7C,QAAM,OAAO,OAAO,MAAQ;AAC5B,QAAM,aAAS,2BAAU,IAAI;AAC7B,SAAO,WAAW,OAAO,WAAO,yBAAQ,QAAQ,CAAC;AACnD;AAGO,MAAM,YAAY;AAAA,EAOvB,YACW,UACT,gBACA;AAFS;AAGT,SAAK,YAAQ,uBAAK,cAAc;AAAA,EAClC;AAAA,EAJW;AAAA,EAPF;AAAA,EAEQ,WAAW,oBAAI,IAAuB;AAAA;AAAA,EAEtC,aAAa,oBAAI,IAA2B;AAAA;AAAA,EAUrD,cAAc,MAAyB;AAC7C,QAAI,OAAO,KAAK,SAAS,IAAI,IAAI;AACjC,QAAI,CAAC,MAAM;AACT,aAAO,CAAC;AACR,eAAS,OAAO,OAAO,MAAQ,KAAK,OAAO,IAAI,MAAM,MAAM,WAAO,yBAAQ,MAAM,CAAC,GAAG;AAClF,YAAI,KAAK,SAAS,aAAa,IAAI,EAAG,MAAK,KAAK,IAAI;AAAA,MACtD;AACA,WAAK,SAAS,IAAI,MAAM,IAAI;AAAA,IAC9B;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,YAAY,MAAe,MAAY,GAA+B;AAC5E,QAAI,SAAS,KAAK;AAChB,UAAI,MAAM,EAA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AS,QAAQ,GAAG,IAAI;AACvD,UAAM,IAAI,eAAe,UAAU,CAAC,CAAC,GAAG,CAAC,MAAM,KAAK,KAAK,IAAI,CAAC;AAC9D,UAAM,SAAS,MAAM;AACrB,QAAI,SAAS,CAAC,IAAI,UAAU,QAAQ,MAAM,CAAC;AAC3C,QAAI,CAAC,UAAU,eAAe,QAAQ;AACpC,UAAI,YAAY,CAAC,IAAI,IAAI,eAAe,CAAC,EAAG,CAAC;AAC7C,UAAI,aAAa,CAAC,IAAI,KAAK,eAAe,eAAe,SAAS,CAAC,EAAG,CAAC;AAAA,IACzE;AACA,UAAM,gBAAgB,SAAS,GAAG,GAAG,IAAI,IAAI,CAAC,KAAK;AACnD,QAAI,mBAAmB,CAAC,IAAI,WAAW,MAAM,IAAI,IAAI,eAAe,CAAC,EAAG,CAAC,IAAI,QAAQ,kBAAkB;AACvG,QAAI,kBAAkB,CAAC,IAAI,UAAU,SAAS,eAAe,CAAC,EAAG,CAAC;AAClE,qBAAiB;AACjB,sBAAkB;AAAA,EACpB;AACA,SAAO;AACT;",
|
|
6
6
|
"names": ["year", "day"]
|
|
7
7
|
}
|
|
@@ -29,11 +29,18 @@ export interface SessionSpec {
|
|
|
29
29
|
/** Date as YYYYMMDD. */
|
|
30
30
|
export type DateNum = number;
|
|
31
31
|
export declare const toDateNum: (y: number, m: number, d: number) => DateNum;
|
|
32
|
+
/** The date `n` (whole) days after `date`. */
|
|
32
33
|
export declare const addDays: (date: DateNum, n: number) => DateNum;
|
|
33
|
-
/** 1 = Sunday ... 7 = Saturday */
|
|
34
|
+
/** 1 = Sunday ... 7 = Saturday (01/01/1970 is a Thursday) */
|
|
34
35
|
export declare const weekdayOf: (date: DateNum) => number;
|
|
35
36
|
/** 00:00 UTC of the date, in ms (PineScript `time_tradingday`). */
|
|
36
37
|
export declare const dateUtcMs: (date: DateNum) => number;
|
|
38
|
+
/** Trading day and period of a time (see `TradingCalendar.locate`). */
|
|
39
|
+
interface Located {
|
|
40
|
+
date: DateNum;
|
|
41
|
+
index: number;
|
|
42
|
+
inside: boolean;
|
|
43
|
+
}
|
|
37
44
|
/** Session periods of every trading day of a symbol, as UNIX times (ms). */
|
|
38
45
|
export declare class TradingCalendar {
|
|
39
46
|
readonly timezone: string;
|
|
@@ -41,7 +48,12 @@ export declare class TradingCalendar {
|
|
|
41
48
|
private readonly corrections;
|
|
42
49
|
private readonly holidays;
|
|
43
50
|
private readonly cache;
|
|
51
|
+
/** The date and periods of each day since 01/01/1970 that `search` looked at */
|
|
52
|
+
private readonly days;
|
|
44
53
|
private readonly pine;
|
|
54
|
+
/** The last time located and its result: the functions of one bar locate the same time several times */
|
|
55
|
+
private lastTime;
|
|
56
|
+
private lastAt;
|
|
45
57
|
/**
|
|
46
58
|
* @param timezone - Time zone of the session hours (the exchange time zone)
|
|
47
59
|
* @param spec - Session information (hours, corrections, holidays)
|
|
@@ -52,6 +64,8 @@ export declare class TradingCalendar {
|
|
|
52
64
|
constructor(timezone: string, spec: SessionSpec, rule?: 'symbol' | 'pine', dayPlacement?: boolean);
|
|
53
65
|
/** Session periods [start, end) of the trading day `date`; empty when the day has no session. */
|
|
54
66
|
periods(date: DateNum): Array<[number, number]>;
|
|
67
|
+
/** The session version in force on `date` (the last one that starts on or before it). */
|
|
68
|
+
private versionOf;
|
|
55
69
|
isTradingDay(date: DateNum): boolean;
|
|
56
70
|
/**
|
|
57
71
|
* True for a bar that opens after the session of its day was cut by a correction (an early close) but inside the
|
|
@@ -62,11 +76,9 @@ export declare class TradingCalendar {
|
|
|
62
76
|
* Trading day and period of a time: the period that contains it, else the last period that started
|
|
63
77
|
* before it. Null when no trading day is found near the time.
|
|
64
78
|
*/
|
|
65
|
-
locate(time: number):
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
inside: boolean;
|
|
69
|
-
} | null;
|
|
79
|
+
locate(time: number): Located | null;
|
|
80
|
+
private search;
|
|
81
|
+
private dayAt;
|
|
70
82
|
/** First trading day in [from, to] (dates), or null. */
|
|
71
83
|
firstTradingDay(from: DateNum, to: DateNum): DateNum | null;
|
|
72
84
|
/** Last trading day in [from, to] (dates), or null. */
|
|
@@ -78,4 +90,5 @@ export declare const weekStart: (date: DateNum) => DateNum;
|
|
|
78
90
|
export declare const monthStart: (date: DateNum) => DateNum;
|
|
79
91
|
/** Last day of the month of `date`. */
|
|
80
92
|
export declare const monthEnd: (date: DateNum) => DateNum;
|
|
93
|
+
export {};
|
|
81
94
|
//# sourceMappingURL=calendar.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calendar.d.ts","sourceRoot":"","sources":["../../../src/session/calendar.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAKH,sEAAsE;AACtE,MAAM,WAAW,WAAW;IAC1B,uEAAuE;IACvE,OAAO,EAAE,MAAM,CAAC;IAChB,2GAA2G;IAC3G,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,uGAAuG;IACvG,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC9B;AAED,wBAAwB;AACxB,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC;AAoB7B,eAAO,MAAM,SAAS,GAAI,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,MAAM,KAAG,OAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"calendar.d.ts","sourceRoot":"","sources":["../../../src/session/calendar.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAKH,sEAAsE;AACtE,MAAM,WAAW,WAAW;IAC1B,uEAAuE;IACvE,OAAO,EAAE,MAAM,CAAC;IAChB,2GAA2G;IAC3G,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,uGAAuG;IACvG,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC9B;AAED,wBAAwB;AACxB,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC;AAoB7B,eAAO,MAAM,SAAS,GAAI,GAAG,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AI,EAAE,MAAM,GAAG,OAAO;IAetC;;;OAGG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI;IASpC,OAAO,CAAC,MAAM;IAkBd,OAAO,CAAC,KAAK;IAUb,wDAAwD;IACxD,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,GAAG,OAAO,GAAG,IAAI;IAK3D,uDAAuD;IACvD,cAAc,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,GAAG,OAAO,GAAG,IAAI;CAI3D;AAED,iEAAiE;AACjE,eAAO,MAAM,SAAS,GAAI,MAAM,OAAO,KAAG,OAAsD,CAAC;AACjG,wCAAwC;AACxC,eAAO,MAAM,UAAU,GAAI,MAAM,OAAO,KAAG,OAA2C,CAAC;AACvF,uCAAuC;AACvC,eAAO,MAAM,QAAQ,GAAI,MAAM,OAAO,KAAG,OAAiE,CAAC"}
|
|
@@ -32,12 +32,26 @@ var import_timezone = require("../time/timezone.js");
|
|
|
32
32
|
var import_session = require("../time/session.js");
|
|
33
33
|
const toDateNum = (y, m, d) => y * 1e4 + m * 100 + d;
|
|
34
34
|
const utcOf = (date) => Date.UTC(Math.floor(date / 1e4), Math.floor(date / 100) % 100 - 1, date % 100);
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
35
|
+
const MS_PER_DAY = 864e5;
|
|
36
|
+
const MAX_DAY = 1e8;
|
|
37
|
+
function dayOf(date) {
|
|
38
|
+
let y = Math.floor(date / 1e4);
|
|
39
|
+
const m = Math.floor(date / 100) % 100;
|
|
40
|
+
if (!(y >= 100 && m >= 1 && m <= 12)) return utcOf(date) / MS_PER_DAY;
|
|
41
|
+
if (m <= 2) y--;
|
|
42
|
+
const era = Math.floor(y / 400);
|
|
43
|
+
const yearOfEra = y - era * 400;
|
|
44
|
+
const dayOfYear = Math.floor((153 * (m > 2 ? m - 3 : m + 9) + 2) / 5) + date % 100 - 1;
|
|
45
|
+
const day = era * 146097 + yearOfEra * 365 + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100) + dayOfYear - 719468;
|
|
46
|
+
return Math.abs(day) <= MAX_DAY ? day : NaN;
|
|
47
|
+
}
|
|
48
|
+
function dateOf(day) {
|
|
49
|
+
if (!(Math.abs(day) <= MAX_DAY)) return NaN;
|
|
50
|
+
const { year, month, day: d } = (0, import_timezone.civilFromDays)(day);
|
|
51
|
+
return toDateNum(year, month, d);
|
|
52
|
+
}
|
|
53
|
+
const addDays = (date, n) => dateOf(dayOf(date) + n);
|
|
54
|
+
const weekdayOf = (date) => ((dayOf(date) + 4) % 7 + 7) % 7 + 1;
|
|
41
55
|
const dateUtcMs = utcOf;
|
|
42
56
|
function parseTime(token, spec) {
|
|
43
57
|
const m = /^(\d{2})(\d{2})(?:F(\d*))?$/.exec(token.trim());
|
|
@@ -124,7 +138,12 @@ class TradingCalendar {
|
|
|
124
138
|
corrections = /* @__PURE__ */ new Map();
|
|
125
139
|
holidays;
|
|
126
140
|
cache = /* @__PURE__ */ new Map();
|
|
141
|
+
/** The date and periods of each day since 01/01/1970 that `search` looked at */
|
|
142
|
+
days = /* @__PURE__ */ new Map();
|
|
127
143
|
pine;
|
|
144
|
+
/** The last time located and its result: the functions of one bar locate the same time several times */
|
|
145
|
+
lastTime = NaN;
|
|
146
|
+
lastAt = null;
|
|
128
147
|
/** Session periods [start, end) of the trading day `date`; empty when the day has no session. */
|
|
129
148
|
periods(date) {
|
|
130
149
|
let out = this.cache.get(date);
|
|
@@ -134,7 +153,7 @@ class TradingCalendar {
|
|
|
134
153
|
if (correction !== void 0) {
|
|
135
154
|
periods = correction ?? [];
|
|
136
155
|
} else if (!this.holidays.has(date)) {
|
|
137
|
-
const version =
|
|
156
|
+
const version = this.versionOf(date);
|
|
138
157
|
if (version.byDay) periods = version.byDay[weekdayOf(date)];
|
|
139
158
|
else if (version.days[weekdayOf(date)]) periods = version.periods;
|
|
140
159
|
}
|
|
@@ -149,6 +168,12 @@ class TradingCalendar {
|
|
|
149
168
|
this.cache.set(date, out);
|
|
150
169
|
return out;
|
|
151
170
|
}
|
|
171
|
+
/** The session version in force on `date` (the last one that starts on or before it). */
|
|
172
|
+
versionOf(date) {
|
|
173
|
+
let k = this.versions.length - 1;
|
|
174
|
+
while (k >= 0 && !(this.versions[k].from <= date)) k--;
|
|
175
|
+
return this.versions[k];
|
|
176
|
+
}
|
|
152
177
|
isTradingDay(date) {
|
|
153
178
|
return this.periods(date).length > 0;
|
|
154
179
|
}
|
|
@@ -159,7 +184,7 @@ class TradingCalendar {
|
|
|
159
184
|
afterEarlyClose(time) {
|
|
160
185
|
const at = this.locate(time);
|
|
161
186
|
if (!at || at.inside || !this.corrections.get(at.date)) return false;
|
|
162
|
-
const version =
|
|
187
|
+
const version = this.versionOf(at.date);
|
|
163
188
|
const usual = version.byDay ? version.byDay[weekdayOf(at.date)] : version.days[weekdayOf(at.date)] ? version.periods : [];
|
|
164
189
|
const y = Math.floor(at.date / 1e4);
|
|
165
190
|
const m = Math.floor(at.date / 100) % 100;
|
|
@@ -173,24 +198,38 @@ class TradingCalendar {
|
|
|
173
198
|
* before it. Null when no trading day is found near the time.
|
|
174
199
|
*/
|
|
175
200
|
locate(time) {
|
|
176
|
-
|
|
177
|
-
|
|
201
|
+
if (time !== this.lastTime) {
|
|
202
|
+
this.lastAt = this.search(time);
|
|
203
|
+
this.lastTime = time;
|
|
204
|
+
}
|
|
205
|
+
const at = this.lastAt;
|
|
206
|
+
return at && { date: at.date, index: at.index, inside: at.inside };
|
|
207
|
+
}
|
|
208
|
+
search(time) {
|
|
209
|
+
const wall = time + (0, import_timezone.zoneOffset)(time, this.timezone);
|
|
210
|
+
const local = dayOf(dateOf(Math.abs(wall) <= MAX_DAY * MS_PER_DAY ? Math.floor(Math.trunc(wall) / MS_PER_DAY) : NaN));
|
|
178
211
|
for (let k = 0; k <= 5; k++) {
|
|
179
|
-
const date =
|
|
180
|
-
const index =
|
|
212
|
+
const { date, periods } = this.dayAt(local + k);
|
|
213
|
+
const index = periods.findIndex(([s, e]) => s <= time && time < e);
|
|
181
214
|
if (index !== -1) return { date, index, inside: true };
|
|
182
215
|
}
|
|
183
216
|
for (let k = 5; k >= -7; k--) {
|
|
184
|
-
const date =
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
periods.forEach(([s], i) => {
|
|
188
|
-
if (s <= time) index = i;
|
|
189
|
-
});
|
|
217
|
+
const { date, periods } = this.dayAt(local + k);
|
|
218
|
+
let index = periods.length - 1;
|
|
219
|
+
while (index >= 0 && !(periods[index][0] <= time)) index--;
|
|
190
220
|
if (index !== -1) return { date, index, inside: false };
|
|
191
221
|
}
|
|
192
222
|
return null;
|
|
193
223
|
}
|
|
224
|
+
dayAt(day) {
|
|
225
|
+
let at = this.days.get(day);
|
|
226
|
+
if (!at) {
|
|
227
|
+
const date = dateOf(day);
|
|
228
|
+
at = { date, periods: this.periods(date) };
|
|
229
|
+
this.days.set(day, at);
|
|
230
|
+
}
|
|
231
|
+
return at;
|
|
232
|
+
}
|
|
194
233
|
/** First trading day in [from, to] (dates), or null. */
|
|
195
234
|
firstTradingDay(from, to) {
|
|
196
235
|
for (let date = from; date <= to; date = addDays(date, 1)) if (this.isTradingDay(date)) return date;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/session/calendar.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Trading calendar: the session periods of each trading day of a symbol, from the symbol session\n * information (issue #100).\n *\n * Session format (symbol session information, also PineScript session strings):\n * - periods \"HHMM-HHMM\", several separated by commas; \"24x7\" is the whole day, every day\n * - a period whose end is not after its start (\"1700-1600\", \"1700-1700\") starts the day before the\n * trading day: the session that opens Sunday 17:00 is Monday's\n * - \"HHMMF<n>\" moves a time <n> days before the trading day (\"1700F2-1200F1\"); \"F\" alone is one day (\"1700F-1900\":\n * 17:00 of the day before to 19:00)\n * - several sections joined by \"|\", each with its own days (\"1900-1900:3456|1700F-1900:2\": Monday opens on Sunday\n * 17:00, the other days at 19:00 of the day before)\n * - optional days after \":\" (1 = Sunday ... 7 = Saturday); without days a symbol session trades Monday to\n * Friday (\"24x7\": every day), and a PineScript session string follows PineScript's rules (one period:\n * every day; several periods: Monday to Friday)\n * - \"#YYYYMMDD/<session>\" changes the session from that date (\"0900-1746#20251201/0800-2200\")\n * - corrections (\"session-correction\"): \"<session>:<dates>;dayoff:<dates>\" replace the session of these\n * dates (early closes, days off); holidays (\"session_holidays\") are dates without session\n */\n\nimport { zonedFields, zonedToUnix } from '../time/timezone.js';\nimport { parsePineSession } from '../time/session.js';\n\n/** Session information of a symbol (hours, corrections, holidays). */\nexport interface SessionSpec {\n /** Session hours: \"0930-1600\", \"1700-1600\", \"24x7\", \"0400-2000\" ... */\n session: string;\n /** Exceptions by date (symbol info \"session-correction\"): \"0930-1300:20251128,20251224;dayoff:20250109\" */\n corrections?: string;\n /** Exchange holidays as \"YYYYMMDD\" (symbol info \"session_holidays\"), comma-separated or as an array */\n holidays?: string | string[];\n}\n\n/** Date as YYYYMMDD. */\nexport type DateNum = number;\n\ninterface Period {\n /** Minutes after midnight */\n start: number;\n end: number;\n /** Days before the trading day */\n startOffset: number;\n endOffset: number;\n}\n\ninterface Version {\n from: DateNum;\n periods: Period[];\n /** Index 1 (Sunday) to 7 (Saturday) */\n days: boolean[];\n /** PineScript session strings: the periods of each weekday (index 1 to 7), from the \"|\" sections */\n byDay?: Period[][];\n}\n\nexport const toDateNum = (y: number, m: number, d: number): DateNum => y * 10000 + m * 100 + d;\nconst utcOf = (date: DateNum): number => Date.UTC(Math.floor(date / 10000), Math.floor(date / 100) % 100 - 1, date % 100);\nconst fromUtc = (ms: number): DateNum => {\n const d = new Date(ms);\n return toDateNum(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate());\n};\nexport const addDays = (date: DateNum, n: number): DateNum => fromUtc(utcOf(date) + n * 86_400_000);\n/** 1 = Sunday ... 7 = Saturday */\nexport const weekdayOf = (date: DateNum): number => new Date(utcOf(date)).getUTCDay() + 1;\n/** 00:00 UTC of the date, in ms (PineScript `time_tradingday`). */\nexport const dateUtcMs = utcOf;\n\nfunction parseTime(token: string, spec: string): { minutes: number; offset?: number } {\n const m = /^(\\d{2})(\\d{2})(?:F(\\d*))?$/.exec(token.trim());\n if (!m) throw new SyntaxError(`Invalid session \"${spec}\"`);\n // \"F\" without a number is one day before the trading day\n return { minutes: Number(m[1]) * 60 + Number(m[2]), offset: m[3] === undefined ? undefined : Number(m[3] || 1) };\n}\n\nfunction parsePeriods(text: string, spec: string): Period[] {\n if (text.trim() === '24x7') return [{ start: 0, end: 1440, startOffset: 0, endOffset: 0 }];\n return text.split(',').map((p) => {\n const [a = '', b = ''] = p.split('-');\n const s = parseTime(a, spec);\n const e = parseTime(b, spec);\n if (s.minutes === 0 && e.minutes === 0 && s.offset === undefined && e.offset === undefined) {\n return { start: 0, end: 1440, startOffset: 0, endOffset: 0 };\n }\n return {\n start: s.minutes,\n end: e.minutes,\n startOffset: s.offset ?? (e.minutes <= s.minutes ? 1 : 0),\n endOffset: e.offset ?? 0,\n };\n });\n}\n\n/** A PineScript session string as periods per weekday (rules in time/session.ts). */\nfunction pineVersion(text: string, from: DateNum): Version {\n const ranges = parsePineSession(text);\n if (ranges === null) throw new SyntaxError(`Session \"${text}\" is the symbol session`);\n const byDay: Period[][] = Array.from({ length: 8 }, () => []);\n for (const { start, end, days, startDays } of ranges) {\n // a run that ends at 0000 ends at 24:00 of the day it starts and belongs to that day (#173)\n const period: Period =\n start === 0 && end === 0\n ? { start: 0, end: 1440, startOffset: 0, endOffset: 0 }\n : startDays !== undefined\n ? { start, end, startOffset: startDays, endOffset: 0 }\n : end === 0\n ? { start, end: 1440, startOffset: 0, endOffset: 0 }\n : { start, end, startOffset: end <= start ? 1 : 0, endOffset: 0 };\n for (const d of days) byDay[d]!.push(period);\n }\n return { from, periods: [], days: byDay.map((p) => p.length > 0), byDay };\n}\n\nfunction parseVersion(text: string, from: DateNum, spec: string, rule: 'symbol' | 'pine'): Version {\n if (rule === 'pine') return pineVersion(text, from);\n if (text.includes('|')) {\n // sections with their own days\n const byDay: Period[][] = Array.from({ length: 8 }, () => []);\n for (const section of text.split('|').filter((part) => part.trim())) {\n const one = parseVersion(section, from, spec, rule);\n one.days.forEach((on, d) => {\n if (on) byDay[d] = one.periods;\n });\n }\n return { from, periods: [], days: byDay.map((p) => p.length > 0), byDay };\n }\n const [periodText = '', dayText] = text.trim().split(':');\n const periods = parsePeriods(periodText, spec);\n const allDays = periodText.trim() === '24x7';\n const digits = dayText ?? (allDays ? '1234567' : '23456');\n if (!/^[1-7]+$/.test(digits)) throw new SyntaxError(`Invalid session days in \"${spec}\"`);\n const days = new Array<boolean>(8).fill(false);\n for (const d of digits) days[Number(d)] = true;\n return { from, periods, days };\n}\n\n/** Session periods of every trading day of a symbol, as UNIX times (ms). */\nexport class TradingCalendar {\n private readonly versions: Version[];\n private readonly corrections = new Map<DateNum, Period[] | null>();\n private readonly holidays: Set<DateNum>;\n private readonly cache = new Map<DateNum, Array<[number, number]>>();\n private readonly pine: boolean;\n\n /**\n * @param timezone - Time zone of the session hours (the exchange time zone)\n * @param spec - Session information (hours, corrections, holidays)\n * @param rule - Days without \":days\": 'symbol' (Monday to Friday) or 'pine' (PineScript session strings)\n * @param dayPlacement - PineScript session strings read for `time(\"D\", session)`: the wall times of a clock-change\n * day are placed as the JVM does (see `periods`); without it, as `time.inSession` places them\n */\n constructor(\n readonly timezone: string,\n spec: SessionSpec,\n rule: 'symbol' | 'pine' = 'symbol',\n dayPlacement = false\n ) {\n this.pine = rule === 'pine' && dayPlacement;\n const [first = '', ...changes] = spec.session.split('#');\n this.versions = [parseVersion(first, 0, spec.session, rule)];\n for (const change of changes) {\n const m = /^(\\d{8})\\/(.+)$/.exec(change.trim());\n if (!m) throw new SyntaxError(`Invalid session change in \"${spec.session}\"`);\n this.versions.push(parseVersion(m[2]!, Number(m[1]), spec.session, rule));\n }\n for (const part of (spec.corrections ?? '').split(';').filter((p) => p.trim())) {\n const i = part.lastIndexOf(':');\n const text = part.slice(0, i).trim();\n const periods = text === 'dayoff' ? null : parsePeriods(text, part);\n for (const date of part.slice(i + 1).split(',')) this.corrections.set(Number(date), periods);\n }\n const holidays = Array.isArray(spec.holidays) ? spec.holidays : (spec.holidays ?? '').split(',');\n this.holidays = new Set(holidays.filter((h) => h.trim()).map(Number));\n }\n\n /** Session periods [start, end) of the trading day `date`; empty when the day has no session. */\n periods(date: DateNum): Array<[number, number]> {\n let out = this.cache.get(date);\n if (out) return out;\n let periods: Period[] = [];\n const correction = this.corrections.get(date);\n if (correction !== undefined) {\n periods = correction ?? [];\n } else if (!this.holidays.has(date)) {\n const version = [...this.versions].reverse().find((v) => v.from <= date)!;\n if (version.byDay) periods = version.byDay[weekdayOf(date)]!;\n else if (version.days[weekdayOf(date)]) periods = version.periods;\n }\n const y = Math.floor(date / 10000);\n const m = Math.floor(date / 100) % 100;\n const d = date % 100;\n const at = (dd: number, minutes: number, earlier = false): number =>\n zonedToUnix(this.timezone, y, m, dd, Math.floor(minutes / 60), minutes % 60, 0, earlier);\n // for \"D\", PineScript session strings place the wall times of a clock-change day as the JVM does: a skipped time moves\n // past the gap, a repeated time is its first pass, and the end is the minute before it plus one minute (\"0000-0300\"\n // on a 02:00 -> 03:00 night ends at 03:00 of the old offset, \"0000-0400\" at 04:00 of the new one) (#173)\n out = periods.map((p) =>\n this.pine\n ? [at(d - p.startOffset, p.start, true), at(d - p.endOffset, p.end - 1, true) + 60_000]\n : [at(d - p.startOffset, p.start), at(d - p.endOffset, p.end)]\n );\n // for \"D\", the runs of a PineScript session string on one day are one period, from the earliest start to the\n // latest end: the time between two runs is inside the day (#182)\n if (this.pine && out.length > 1) out = [[Math.min(...out.map((p) => p[0])), Math.max(...out.map((p) => p[1]))]];\n this.cache.set(date, out);\n return out;\n }\n\n isTradingDay(date: DateNum): boolean {\n return this.periods(date).length > 0;\n }\n\n /**\n * True for a bar that opens after the session of its day was cut by a correction (an early close) but inside the\n * usual hours of that day: PineScript has no period for such a bar (`time(\"D\")` is na on it).\n */\n afterEarlyClose(time: number): boolean {\n const at = this.locate(time);\n if (!at || at.inside || !this.corrections.get(at.date)) return false;\n const version = [...this.versions].reverse().find((v) => v.from <= at.date)!;\n const usual = version.byDay ? version.byDay[weekdayOf(at.date)]! : version.days[weekdayOf(at.date)] ? version.periods : [];\n const y = Math.floor(at.date / 10000);\n const m = Math.floor(at.date / 100) % 100;\n const d = at.date % 100;\n return usual.some(\n (p) =>\n zonedToUnix(this.timezone, y, m, d - p.startOffset, Math.floor(p.start / 60), p.start % 60) <= time &&\n time < zonedToUnix(this.timezone, y, m, d - p.endOffset, Math.floor(p.end / 60), p.end % 60)\n );\n }\n\n /**\n * Trading day and period of a time: the period that contains it, else the last period that started\n * before it. Null when no trading day is found near the time.\n */\n locate(time: number): { date: DateNum; index: number; inside: boolean } | null {\n const f = zonedFields(time, this.timezone);\n const local = toDateNum(f.year, f.month, f.day);\n for (let k = 0; k <= 5; k++) {\n const date = addDays(local, k);\n const index = this.periods(date).findIndex(([s, e]) => s <= time && time < e);\n if (index !== -1) return { date, index, inside: true };\n }\n for (let k = 5; k >= -7; k--) {\n const date = addDays(local, k);\n const periods = this.periods(date);\n let index = -1;\n periods.forEach(([s], i) => {\n if (s <= time) index = i;\n });\n if (index !== -1) return { date, index, inside: false };\n }\n return null;\n }\n\n /** First trading day in [from, to] (dates), or null. */\n firstTradingDay(from: DateNum, to: DateNum): DateNum | null {\n for (let date = from; date <= to; date = addDays(date, 1)) if (this.isTradingDay(date)) return date;\n return null;\n }\n\n /** Last trading day in [from, to] (dates), or null. */\n lastTradingDay(from: DateNum, to: DateNum): DateNum | null {\n for (let date = to; date >= from; date = addDays(date, -1)) if (this.isTradingDay(date)) return date;\n return null;\n }\n}\n\n/** Monday of the week of `date` (weeks run Monday to Sunday). */\nexport const weekStart = (date: DateNum): DateNum => addDays(date, -((weekdayOf(date) + 5) % 7));\n/** First day of the month of `date`. */\nexport const monthStart = (date: DateNum): DateNum => Math.floor(date / 100) * 100 + 1;\n/** Last day of the month of `date`. */\nexport const monthEnd = (date: DateNum): DateNum => addDays(monthStart(addDays(monthStart(date), 32)), -1);\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAoBA,
|
|
4
|
+
"sourcesContent": ["/**\n * Trading calendar: the session periods of each trading day of a symbol, from the symbol session\n * information (issue #100).\n *\n * Session format (symbol session information, also PineScript session strings):\n * - periods \"HHMM-HHMM\", several separated by commas; \"24x7\" is the whole day, every day\n * - a period whose end is not after its start (\"1700-1600\", \"1700-1700\") starts the day before the\n * trading day: the session that opens Sunday 17:00 is Monday's\n * - \"HHMMF<n>\" moves a time <n> days before the trading day (\"1700F2-1200F1\"); \"F\" alone is one day (\"1700F-1900\":\n * 17:00 of the day before to 19:00)\n * - several sections joined by \"|\", each with its own days (\"1900-1900:3456|1700F-1900:2\": Monday opens on Sunday\n * 17:00, the other days at 19:00 of the day before)\n * - optional days after \":\" (1 = Sunday ... 7 = Saturday); without days a symbol session trades Monday to\n * Friday (\"24x7\": every day), and a PineScript session string follows PineScript's rules (one period:\n * every day; several periods: Monday to Friday)\n * - \"#YYYYMMDD/<session>\" changes the session from that date (\"0900-1746#20251201/0800-2200\")\n * - corrections (\"session-correction\"): \"<session>:<dates>;dayoff:<dates>\" replace the session of these\n * dates (early closes, days off); holidays (\"session_holidays\") are dates without session\n */\n\nimport { civilFromDays, zoneOffset, zonedToUnix } from '../time/timezone.js';\nimport { parsePineSession } from '../time/session.js';\n\n/** Session information of a symbol (hours, corrections, holidays). */\nexport interface SessionSpec {\n /** Session hours: \"0930-1600\", \"1700-1600\", \"24x7\", \"0400-2000\" ... */\n session: string;\n /** Exceptions by date (symbol info \"session-correction\"): \"0930-1300:20251128,20251224;dayoff:20250109\" */\n corrections?: string;\n /** Exchange holidays as \"YYYYMMDD\" (symbol info \"session_holidays\"), comma-separated or as an array */\n holidays?: string | string[];\n}\n\n/** Date as YYYYMMDD. */\nexport type DateNum = number;\n\ninterface Period {\n /** Minutes after midnight */\n start: number;\n end: number;\n /** Days before the trading day */\n startOffset: number;\n endOffset: number;\n}\n\ninterface Version {\n from: DateNum;\n periods: Period[];\n /** Index 1 (Sunday) to 7 (Saturday) */\n days: boolean[];\n /** PineScript session strings: the periods of each weekday (index 1 to 7), from the \"|\" sections */\n byDay?: Period[][];\n}\n\nexport const toDateNum = (y: number, m: number, d: number): DateNum => y * 10000 + m * 100 + d;\nconst utcOf = (date: DateNum): number => Date.UTC(Math.floor(date / 10000), Math.floor(date / 100) % 100 - 1, date % 100);\n\nconst MS_PER_DAY = 86_400_000;\n/** Days of a valid `Date` on each side of 01/01/1970 (8.64e15 ms) */\nconst MAX_DAY = 100_000_000;\n\n/**\n * Days since 01/01/1970 of a date, as `utcOf(date) / 1 day` but without a `Date`: the calendar functions step\n * through days on every bar. Years before 100 and months outside 1-12 keep the `Date.UTC` reading (years 0-99 are\n * 1900-1999 there). NaN outside the `Date` range.\n */\nfunction dayOf(date: DateNum): number {\n let y = Math.floor(date / 10000);\n const m = Math.floor(date / 100) % 100;\n if (!(y >= 100 && m >= 1 && m <= 12)) return utcOf(date) / MS_PER_DAY;\n // proleptic Gregorian day count, years starting in March (the leap day last)\n if (m <= 2) y--;\n const era = Math.floor(y / 400);\n const yearOfEra = y - era * 400;\n const dayOfYear = Math.floor((153 * (m > 2 ? m - 3 : m + 9) + 2) / 5) + (date % 100) - 1;\n const day = era * 146_097 + yearOfEra * 365 + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100) + dayOfYear - 719_468;\n return Math.abs(day) <= MAX_DAY ? day : NaN;\n}\n\n/** Date of a day since 01/01/1970 (the inverse of `dayOf`); NaN outside the `Date` range. */\nfunction dateOf(day: number): DateNum {\n if (!(Math.abs(day) <= MAX_DAY)) return NaN;\n const { year, month, day: d } = civilFromDays(day);\n return toDateNum(year, month, d);\n}\n\n/** The date `n` (whole) days after `date`. */\nexport const addDays = (date: DateNum, n: number): DateNum => dateOf(dayOf(date) + n);\n/** 1 = Sunday ... 7 = Saturday (01/01/1970 is a Thursday) */\nexport const weekdayOf = (date: DateNum): number => ((((dayOf(date) + 4) % 7) + 7) % 7) + 1;\n/** 00:00 UTC of the date, in ms (PineScript `time_tradingday`). */\nexport const dateUtcMs = utcOf;\n\nfunction parseTime(token: string, spec: string): { minutes: number; offset?: number } {\n const m = /^(\\d{2})(\\d{2})(?:F(\\d*))?$/.exec(token.trim());\n if (!m) throw new SyntaxError(`Invalid session \"${spec}\"`);\n // \"F\" without a number is one day before the trading day\n return { minutes: Number(m[1]) * 60 + Number(m[2]), offset: m[3] === undefined ? undefined : Number(m[3] || 1) };\n}\n\nfunction parsePeriods(text: string, spec: string): Period[] {\n if (text.trim() === '24x7') return [{ start: 0, end: 1440, startOffset: 0, endOffset: 0 }];\n return text.split(',').map((p) => {\n const [a = '', b = ''] = p.split('-');\n const s = parseTime(a, spec);\n const e = parseTime(b, spec);\n if (s.minutes === 0 && e.minutes === 0 && s.offset === undefined && e.offset === undefined) {\n return { start: 0, end: 1440, startOffset: 0, endOffset: 0 };\n }\n return {\n start: s.minutes,\n end: e.minutes,\n startOffset: s.offset ?? (e.minutes <= s.minutes ? 1 : 0),\n endOffset: e.offset ?? 0,\n };\n });\n}\n\n/** A PineScript session string as periods per weekday (rules in time/session.ts). */\nfunction pineVersion(text: string, from: DateNum): Version {\n const ranges = parsePineSession(text);\n if (ranges === null) throw new SyntaxError(`Session \"${text}\" is the symbol session`);\n const byDay: Period[][] = Array.from({ length: 8 }, () => []);\n for (const { start, end, days, startDays } of ranges) {\n // a run that ends at 0000 ends at 24:00 of the day it starts and belongs to that day (#173)\n const period: Period =\n start === 0 && end === 0\n ? { start: 0, end: 1440, startOffset: 0, endOffset: 0 }\n : startDays !== undefined\n ? { start, end, startOffset: startDays, endOffset: 0 }\n : end === 0\n ? { start, end: 1440, startOffset: 0, endOffset: 0 }\n : { start, end, startOffset: end <= start ? 1 : 0, endOffset: 0 };\n for (const d of days) byDay[d]!.push(period);\n }\n return { from, periods: [], days: byDay.map((p) => p.length > 0), byDay };\n}\n\nfunction parseVersion(text: string, from: DateNum, spec: string, rule: 'symbol' | 'pine'): Version {\n if (rule === 'pine') return pineVersion(text, from);\n if (text.includes('|')) {\n // sections with their own days\n const byDay: Period[][] = Array.from({ length: 8 }, () => []);\n for (const section of text.split('|').filter((part) => part.trim())) {\n const one = parseVersion(section, from, spec, rule);\n one.days.forEach((on, d) => {\n if (on) byDay[d] = one.periods;\n });\n }\n return { from, periods: [], days: byDay.map((p) => p.length > 0), byDay };\n }\n const [periodText = '', dayText] = text.trim().split(':');\n const periods = parsePeriods(periodText, spec);\n const allDays = periodText.trim() === '24x7';\n const digits = dayText ?? (allDays ? '1234567' : '23456');\n if (!/^[1-7]+$/.test(digits)) throw new SyntaxError(`Invalid session days in \"${spec}\"`);\n const days = new Array<boolean>(8).fill(false);\n for (const d of digits) days[Number(d)] = true;\n return { from, periods, days };\n}\n\n/** Trading day and period of a time (see `TradingCalendar.locate`). */\ninterface Located {\n date: DateNum;\n index: number;\n inside: boolean;\n}\n\n/** Session periods of every trading day of a symbol, as UNIX times (ms). */\nexport class TradingCalendar {\n private readonly versions: Version[];\n private readonly corrections = new Map<DateNum, Period[] | null>();\n private readonly holidays: Set<DateNum>;\n private readonly cache = new Map<DateNum, Array<[number, number]>>();\n /** The date and periods of each day since 01/01/1970 that `search` looked at */\n private readonly days = new Map<number, { date: DateNum; periods: Array<[number, number]> }>();\n private readonly pine: boolean;\n /** The last time located and its result: the functions of one bar locate the same time several times */\n private lastTime = NaN;\n private lastAt: Located | null = null;\n\n /**\n * @param timezone - Time zone of the session hours (the exchange time zone)\n * @param spec - Session information (hours, corrections, holidays)\n * @param rule - Days without \":days\": 'symbol' (Monday to Friday) or 'pine' (PineScript session strings)\n * @param dayPlacement - PineScript session strings read for `time(\"D\", session)`: the wall times of a clock-change\n * day are placed as the JVM does (see `periods`); without it, as `time.inSession` places them\n */\n constructor(\n readonly timezone: string,\n spec: SessionSpec,\n rule: 'symbol' | 'pine' = 'symbol',\n dayPlacement = false\n ) {\n this.pine = rule === 'pine' && dayPlacement;\n const [first = '', ...changes] = spec.session.split('#');\n this.versions = [parseVersion(first, 0, spec.session, rule)];\n for (const change of changes) {\n const m = /^(\\d{8})\\/(.+)$/.exec(change.trim());\n if (!m) throw new SyntaxError(`Invalid session change in \"${spec.session}\"`);\n this.versions.push(parseVersion(m[2]!, Number(m[1]), spec.session, rule));\n }\n for (const part of (spec.corrections ?? '').split(';').filter((p) => p.trim())) {\n const i = part.lastIndexOf(':');\n const text = part.slice(0, i).trim();\n const periods = text === 'dayoff' ? null : parsePeriods(text, part);\n for (const date of part.slice(i + 1).split(',')) this.corrections.set(Number(date), periods);\n }\n const holidays = Array.isArray(spec.holidays) ? spec.holidays : (spec.holidays ?? '').split(',');\n this.holidays = new Set(holidays.filter((h) => h.trim()).map(Number));\n }\n\n /** Session periods [start, end) of the trading day `date`; empty when the day has no session. */\n periods(date: DateNum): Array<[number, number]> {\n let out = this.cache.get(date);\n if (out) return out;\n let periods: Period[] = [];\n const correction = this.corrections.get(date);\n if (correction !== undefined) {\n periods = correction ?? [];\n } else if (!this.holidays.has(date)) {\n const version = this.versionOf(date);\n if (version.byDay) periods = version.byDay[weekdayOf(date)]!;\n else if (version.days[weekdayOf(date)]) periods = version.periods;\n }\n const y = Math.floor(date / 10000);\n const m = Math.floor(date / 100) % 100;\n const d = date % 100;\n const at = (dd: number, minutes: number, earlier = false): number =>\n zonedToUnix(this.timezone, y, m, dd, Math.floor(minutes / 60), minutes % 60, 0, earlier);\n // for \"D\", PineScript session strings place the wall times of a clock-change day as the JVM does: a skipped time moves\n // past the gap, a repeated time is its first pass, and the end is the minute before it plus one minute (\"0000-0300\"\n // on a 02:00 -> 03:00 night ends at 03:00 of the old offset, \"0000-0400\" at 04:00 of the new one) (#173)\n out = periods.map((p) =>\n this.pine\n ? [at(d - p.startOffset, p.start, true), at(d - p.endOffset, p.end - 1, true) + 60_000]\n : [at(d - p.startOffset, p.start), at(d - p.endOffset, p.end)]\n );\n // for \"D\", the runs of a PineScript session string on one day are one period, from the earliest start to the\n // latest end: the time between two runs is inside the day (#182)\n if (this.pine && out.length > 1) out = [[Math.min(...out.map((p) => p[0])), Math.max(...out.map((p) => p[1]))]];\n this.cache.set(date, out);\n return out;\n }\n\n /** The session version in force on `date` (the last one that starts on or before it). */\n private versionOf(date: DateNum): Version {\n let k = this.versions.length - 1;\n while (k >= 0 && !(this.versions[k]!.from <= date)) k--;\n return this.versions[k]!;\n }\n\n isTradingDay(date: DateNum): boolean {\n return this.periods(date).length > 0;\n }\n\n /**\n * True for a bar that opens after the session of its day was cut by a correction (an early close) but inside the\n * usual hours of that day: PineScript has no period for such a bar (`time(\"D\")` is na on it).\n */\n afterEarlyClose(time: number): boolean {\n const at = this.locate(time);\n if (!at || at.inside || !this.corrections.get(at.date)) return false;\n const version = this.versionOf(at.date);\n const usual = version.byDay ? version.byDay[weekdayOf(at.date)]! : version.days[weekdayOf(at.date)] ? version.periods : [];\n const y = Math.floor(at.date / 10000);\n const m = Math.floor(at.date / 100) % 100;\n const d = at.date % 100;\n return usual.some(\n (p) =>\n zonedToUnix(this.timezone, y, m, d - p.startOffset, Math.floor(p.start / 60), p.start % 60) <= time &&\n time < zonedToUnix(this.timezone, y, m, d - p.endOffset, Math.floor(p.end / 60), p.end % 60)\n );\n }\n\n /**\n * Trading day and period of a time: the period that contains it, else the last period that started\n * before it. Null when no trading day is found near the time.\n */\n locate(time: number): Located | null {\n if (time !== this.lastTime) {\n this.lastAt = this.search(time);\n this.lastTime = time;\n }\n const at = this.lastAt;\n return at && { date: at.date, index: at.index, inside: at.inside };\n }\n\n private search(time: number): Located | null {\n // the local date of the time, as `Date` reads the wall clock (whole ms, NaN outside its range)\n const wall = time + zoneOffset(time, this.timezone);\n const local = dayOf(dateOf(Math.abs(wall) <= MAX_DAY * MS_PER_DAY ? Math.floor(Math.trunc(wall) / MS_PER_DAY) : NaN));\n for (let k = 0; k <= 5; k++) {\n const { date, periods } = this.dayAt(local + k);\n const index = periods.findIndex(([s, e]) => s <= time && time < e);\n if (index !== -1) return { date, index, inside: true };\n }\n for (let k = 5; k >= -7; k--) {\n const { date, periods } = this.dayAt(local + k);\n let index = periods.length - 1;\n while (index >= 0 && !(periods[index]![0] <= time)) index--;\n if (index !== -1) return { date, index, inside: false };\n }\n return null;\n }\n\n private dayAt(day: number): { date: DateNum; periods: Array<[number, number]> } {\n let at = this.days.get(day);\n if (!at) {\n const date = dateOf(day);\n at = { date, periods: this.periods(date) };\n this.days.set(day, at);\n }\n return at;\n }\n\n /** First trading day in [from, to] (dates), or null. */\n firstTradingDay(from: DateNum, to: DateNum): DateNum | null {\n for (let date = from; date <= to; date = addDays(date, 1)) if (this.isTradingDay(date)) return date;\n return null;\n }\n\n /** Last trading day in [from, to] (dates), or null. */\n lastTradingDay(from: DateNum, to: DateNum): DateNum | null {\n for (let date = to; date >= from; date = addDays(date, -1)) if (this.isTradingDay(date)) return date;\n return null;\n }\n}\n\n/** Monday of the week of `date` (weeks run Monday to Sunday). */\nexport const weekStart = (date: DateNum): DateNum => addDays(date, -((weekdayOf(date) + 5) % 7));\n/** First day of the month of `date`. */\nexport const monthStart = (date: DateNum): DateNum => Math.floor(date / 100) * 100 + 1;\n/** Last day of the month of `date`. */\nexport const monthEnd = (date: DateNum): DateNum => addDays(monthStart(addDays(monthStart(date), 32)), -1);\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAoBA,sBAAuD;AACvD,qBAAiC;AAiC1B,MAAM,YAAY,CAAC,GAAW,GAAW,MAAuB,IA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;IAChD,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,GAAG,eAAe,CA0BtG;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,GAAG,MAAM,CAKvF;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,GAAG,MAAM,CAWjF"}
|
|
@@ -53,17 +53,31 @@ function chartSessions(chart) {
|
|
|
53
53
|
cache.set(chart, out);
|
|
54
54
|
return out;
|
|
55
55
|
}
|
|
56
|
+
const MAX_ARGUMENT_CALENDARS = 64;
|
|
57
|
+
const argumentCalendars = /* @__PURE__ */ new Map();
|
|
58
|
+
function argumentCalendar(timezone, session, rule, dayPlacement) {
|
|
59
|
+
const key = `${rule}|${dayPlacement}|${timezone}|${session}`;
|
|
60
|
+
let calendar = argumentCalendars.get(key);
|
|
61
|
+
if (!calendar) {
|
|
62
|
+
calendar = new import_calendar.TradingCalendar(timezone, { session }, rule, dayPlacement);
|
|
63
|
+
if (argumentCalendars.size >= MAX_ARGUMENT_CALENDARS) argumentCalendars.clear();
|
|
64
|
+
argumentCalendars.set(key, calendar);
|
|
65
|
+
}
|
|
66
|
+
return calendar;
|
|
67
|
+
}
|
|
56
68
|
function calendarFor(bars, chart, session, timezone, dayPlacement) {
|
|
57
69
|
if (!session && !timezone) return { bars, test: false };
|
|
58
70
|
const tz = timezone ?? chart.timezone;
|
|
59
71
|
if (session !== void 0 && (0, import_session.parsePineSession)(session) === null) {
|
|
60
72
|
const name = session.trim();
|
|
61
73
|
const symbol = name === "regular" && chart.regularSession ? chart.regularSession : chart.session;
|
|
62
|
-
return { bars: new import_bars.SessionBars(
|
|
74
|
+
return { bars: new import_bars.SessionBars(argumentCalendar(tz, toSpec(symbol).session, "symbol", false), bars.chart.period), test: true };
|
|
63
75
|
}
|
|
64
|
-
const spec = session ? { session } : { session: toSpec(chart.session).session };
|
|
65
76
|
return {
|
|
66
|
-
bars: new import_bars.SessionBars(
|
|
77
|
+
bars: new import_bars.SessionBars(
|
|
78
|
+
argumentCalendar(tz, session || toSpec(chart.session).session, session ? "pine" : "symbol", dayPlacement),
|
|
79
|
+
bars.chart.period
|
|
80
|
+
),
|
|
67
81
|
test: true,
|
|
68
82
|
// a PineScript session string: the rules of time.inSession (runs ending at 0000, days around a clock change)
|
|
69
83
|
inside: session ? (time) => (0, import_session.sessionRun)(time, session, tz, (0, import_timeframe.in_seconds)(bars.chart.period) * 1e3) !== null : void 0
|
|
@@ -104,11 +118,14 @@ function sessionPeriods(bars, chart, timeframe, session, timezone) {
|
|
|
104
118
|
const periodCache = /* @__PURE__ */ new WeakMap();
|
|
105
119
|
function periodsOfChart(chart, timeframe, session, timezone) {
|
|
106
120
|
const s = chartSessions(chart);
|
|
107
|
-
let
|
|
108
|
-
if (!
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
121
|
+
let byTimeframe = periodCache.get(chart);
|
|
122
|
+
if (!byTimeframe) periodCache.set(chart, byTimeframe = /* @__PURE__ */ new Map());
|
|
123
|
+
let bySession = byTimeframe.get(timeframe);
|
|
124
|
+
if (!bySession) byTimeframe.set(timeframe, bySession = /* @__PURE__ */ new Map());
|
|
125
|
+
let byZone = bySession.get(session ?? "");
|
|
126
|
+
if (!byZone) bySession.set(session ?? "", byZone = /* @__PURE__ */ new Map());
|
|
127
|
+
let p = byZone.get(timezone ?? "");
|
|
128
|
+
if (!p) byZone.set(timezone ?? "", p = sessionPeriods(s.bars, chart, timeframe, session, timezone));
|
|
112
129
|
return { s, p };
|
|
113
130
|
}
|
|
114
131
|
function periodTime(time, timeframe, chart, session, timezone) {
|