oakscriptjs 0.18.0 → 0.19.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.
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/script/index.js +1 -1
- package/dist/cjs/script/index.js.map +2 -2
- package/dist/cjs/session/bars.d.ts +9 -0
- package/dist/cjs/session/bars.d.ts.map +1 -1
- package/dist/cjs/session/bars.js +36 -15
- package/dist/cjs/session/bars.js.map +2 -2
- package/dist/cjs/session/calendar.d.ts +10 -0
- package/dist/cjs/session/calendar.d.ts.map +1 -1
- package/dist/cjs/session/calendar.js +15 -0
- package/dist/cjs/session/calendar.js.map +2 -2
- package/dist/cjs/session/context.js +1 -1
- package/dist/cjs/session/context.js.map +2 -2
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/script/index.js +1 -1
- package/dist/esm/script/index.js.map +1 -1
- package/dist/esm/session/bars.d.ts +9 -0
- package/dist/esm/session/bars.d.ts.map +1 -1
- package/dist/esm/session/bars.js +40 -15
- package/dist/esm/session/bars.js.map +1 -1
- package/dist/esm/session/calendar.d.ts +10 -0
- package/dist/esm/session/calendar.d.ts.map +1 -1
- package/dist/esm/session/calendar.js +16 -0
- package/dist/esm/session/calendar.js.map +1 -1
- package/dist/esm/session/context.js +1 -1
- package/dist/esm/session/context.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/script/index.ts +1 -1
- package/src/session/bars.ts +40 -15
- package/src/session/calendar.ts +22 -0
- package/src/session/context.ts +1 -1
|
@@ -63,6 +63,15 @@ export declare class SessionBars {
|
|
|
63
63
|
* trading day of the next chart period. NaN when none is found within 400 days.
|
|
64
64
|
*/
|
|
65
65
|
nextBarTime(time: number): number;
|
|
66
|
+
/**
|
|
67
|
+
* Trading day and period of a chart bar that opens at `time` (see TradingCalendar.locate). On the clock grid
|
|
68
|
+
* (`barOrigin`), an intraday bar that opens before a session period but holds its open belongs to that period
|
|
69
|
+
* (a 01:00 five-minute bar when trading opens at 01:01).
|
|
70
|
+
*/
|
|
71
|
+
locateBar(time: number): ReturnType<TradingCalendar['locate']>;
|
|
72
|
+
/** Close of the intraday `timeframe` period that holds `time`: cut at the session period `end`, except on the
|
|
73
|
+
* clock grid, whose bars run their whole length. */
|
|
74
|
+
private intradayClose;
|
|
66
75
|
/** An intraday bar that opens after an early close of its day (see TradingCalendar.afterEarlyClose). */
|
|
67
76
|
stray(time: number): boolean;
|
|
68
77
|
/** True when the bar at `time` is inside the session. */
|
|
@@ -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;IAQpB,QAAQ,CAAC,QAAQ,EAAE,eAAe;IAPpC,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAE9B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgC;IACzD,+DAA+D;IAC/D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoC;gBAGpD,QAAQ,EAAE,eAAe,EAClC,cAAc,EAAE,MAAM;IAKxB,iDAAiD;IACjD,OAAO,CAAC,aAAa;IAYrB;;;OAGG;IACH,OAAO,CAAC,WAAW;IA0BnB,OAAO,CAAC,SAAS;IAMjB,OAAO,CAAC,QAAQ;IAIhB,OAAO,CAAC,MAAM;IAKd;;;;OAIG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM;
|
|
1
|
+
{"version":3,"file":"bars.d.ts","sourceRoot":"","sources":["../../../src/session/bars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GAAwG;IACxG,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI5B,yDAAyD;IACzD,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI7B,mEAAmE;IACnE,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG;QAAE,IAAI,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;CAI9E;AAED;;;;;;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,CA+CnG"}
|
package/dist/cjs/session/bars.js
CHANGED
|
@@ -104,13 +104,14 @@ class SessionBars {
|
|
|
104
104
|
*/
|
|
105
105
|
timeOf(time, timeframe) {
|
|
106
106
|
const tf = this.timeframe(timeframe);
|
|
107
|
-
const at = this.
|
|
107
|
+
const at = this.locateBar(time);
|
|
108
108
|
if (!at) return NaN;
|
|
109
109
|
if (tf.isintraday) {
|
|
110
110
|
if (this.chart.isdwm) return time;
|
|
111
111
|
if (this.stray(time)) return NaN;
|
|
112
|
-
const [start] = this.calendar.periods(at.date)[at.index];
|
|
113
112
|
const ms = (0, import_timeframe.in_seconds)(tf.period) * 1e3;
|
|
113
|
+
if (this.calendar.barOrigin !== null) return this.calendar.gridStart(time, ms);
|
|
114
|
+
const [start] = this.calendar.periods(at.date)[at.index];
|
|
114
115
|
return start + Math.floor((time - start) / ms) * ms;
|
|
115
116
|
}
|
|
116
117
|
const { unit, n } = unitOf(tf);
|
|
@@ -122,13 +123,13 @@ class SessionBars {
|
|
|
122
123
|
/** PineScript `time_close(tf)` for a bar that opens at `time`. */
|
|
123
124
|
closeOf(time, timeframe) {
|
|
124
125
|
const tf = this.timeframe(timeframe);
|
|
125
|
-
const at = this.
|
|
126
|
+
const at = this.locateBar(time);
|
|
126
127
|
if (!at) return NaN;
|
|
127
128
|
if (tf.isintraday) {
|
|
128
129
|
const [, end] = this.calendar.periods(at.date)[at.index];
|
|
129
130
|
if (this.chart.isdwm) return end;
|
|
130
131
|
if (this.stray(time)) return NaN;
|
|
131
|
-
return
|
|
132
|
+
return this.intradayClose(time, timeframe, end);
|
|
132
133
|
}
|
|
133
134
|
const { unit, n } = unitOf(tf);
|
|
134
135
|
if (unit === "D" && n === 1 && this.stray(time)) return NaN;
|
|
@@ -143,7 +144,7 @@ class SessionBars {
|
|
|
143
144
|
}
|
|
144
145
|
/** Trading day of a bar: on D / W / M charts the last trading day of the chart period. */
|
|
145
146
|
tradingDay(time) {
|
|
146
|
-
const at = this.
|
|
147
|
+
const at = this.locateBar(time);
|
|
147
148
|
if (!at || this.stray(time)) return null;
|
|
148
149
|
if (this.chart.isdwm) {
|
|
149
150
|
const { unit, n } = unitOf(this.chart);
|
|
@@ -159,11 +160,11 @@ class SessionBars {
|
|
|
159
160
|
*/
|
|
160
161
|
periodEnd(time, timeframe) {
|
|
161
162
|
const tf = this.timeframe(timeframe);
|
|
162
|
-
const at = this.
|
|
163
|
+
const at = this.locateBar(time);
|
|
163
164
|
if (!at) return NaN;
|
|
164
165
|
if (tf.isintraday) {
|
|
165
166
|
const [, end] = this.calendar.periods(at.date)[at.index];
|
|
166
|
-
return
|
|
167
|
+
return this.intradayClose(time, timeframe, end);
|
|
167
168
|
}
|
|
168
169
|
const { unit, n } = unitOf(tf);
|
|
169
170
|
const [from, to] = this.periodRange(at.date, unit, n);
|
|
@@ -181,34 +182,53 @@ class SessionBars {
|
|
|
181
182
|
* trading day of the next chart period. NaN when none is found within 400 days.
|
|
182
183
|
*/
|
|
183
184
|
nextBarTime(time) {
|
|
184
|
-
const at = this.
|
|
185
|
+
const at = this.locateBar(time);
|
|
185
186
|
if (!at) return NaN;
|
|
186
187
|
if (!this.chart.isdwm) {
|
|
187
188
|
const periods = this.calendar.periods(at.date);
|
|
188
189
|
const [start, end] = periods[at.index];
|
|
189
190
|
const ms = (0, import_timeframe.in_seconds)(this.chart.period) * 1e3;
|
|
190
|
-
const
|
|
191
|
+
const grid = this.calendar.barOrigin !== null;
|
|
192
|
+
const open = (t) => grid ? this.calendar.gridStart(t, ms) : t;
|
|
193
|
+
const next = grid ? this.calendar.gridStart(time, ms) + ms : start + (Math.floor((time - start) / ms) + 1) * ms;
|
|
191
194
|
if (at.inside && next < end) return next;
|
|
192
|
-
if (at.index + 1 < periods.length) return periods[at.index + 1][0];
|
|
195
|
+
if (at.index + 1 < periods.length) return open(periods[at.index + 1][0]);
|
|
193
196
|
const day2 = this.calendar.firstTradingDay((0, import_calendar.addDays)(at.date, 1), (0, import_calendar.addDays)(at.date, 400));
|
|
194
|
-
return day2 === null ? NaN : this.dayStart(day2);
|
|
197
|
+
return day2 === null ? NaN : open(this.dayStart(day2));
|
|
195
198
|
}
|
|
196
199
|
const { unit, n } = unitOf(this.chart);
|
|
197
200
|
const [, to] = this.periodRange(at.date, unit, n);
|
|
198
201
|
const day = this.calendar.firstTradingDay((0, import_calendar.addDays)(to, 1), (0, import_calendar.addDays)(to, 400));
|
|
199
202
|
return day === null ? NaN : this.dayStart(day);
|
|
200
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Trading day and period of a chart bar that opens at `time` (see TradingCalendar.locate). On the clock grid
|
|
206
|
+
* (`barOrigin`), an intraday bar that opens before a session period but holds its open belongs to that period
|
|
207
|
+
* (a 01:00 five-minute bar when trading opens at 01:01).
|
|
208
|
+
*/
|
|
209
|
+
locateBar(time) {
|
|
210
|
+
const at = this.calendar.locate(time);
|
|
211
|
+
if (this.calendar.barOrigin === null || this.chart.isdwm || at?.inside) return at;
|
|
212
|
+
const ahead = this.calendar.locate(time + (0, import_timeframe.in_seconds)(this.chart.period) * 1e3 - 1);
|
|
213
|
+
return ahead?.inside && this.calendar.periods(ahead.date)[ahead.index][0] > time ? ahead : at;
|
|
214
|
+
}
|
|
215
|
+
/** Close of the intraday `timeframe` period that holds `time`: cut at the session period `end`, except on the
|
|
216
|
+
* clock grid, whose bars run their whole length. */
|
|
217
|
+
intradayClose(time, timeframe, end) {
|
|
218
|
+
const close = this.timeOf(time, timeframe) + (0, import_timeframe.in_seconds)(this.timeframe(timeframe).period) * 1e3;
|
|
219
|
+
return this.calendar.barOrigin !== null ? close : Math.min(close, end);
|
|
220
|
+
}
|
|
201
221
|
/** An intraday bar that opens after an early close of its day (see TradingCalendar.afterEarlyClose). */
|
|
202
222
|
stray(time) {
|
|
203
223
|
return !this.chart.isdwm && this.calendar.afterEarlyClose(time);
|
|
204
224
|
}
|
|
205
225
|
/** True when the bar at `time` is inside the session. */
|
|
206
226
|
inside(time) {
|
|
207
|
-
return this.
|
|
227
|
+
return this.locateBar(time)?.inside ?? false;
|
|
208
228
|
}
|
|
209
229
|
/** Session start and end of the trading day of `time`, or null. */
|
|
210
230
|
dayBounds(time) {
|
|
211
|
-
const at = this.
|
|
231
|
+
const at = this.locateBar(time);
|
|
212
232
|
return at ? { date: at.date, start: this.dayStart(at.date), end: this.dayEnd(at.date) } : null;
|
|
213
233
|
}
|
|
214
234
|
}
|
|
@@ -243,7 +263,7 @@ function sessionFlags(times, bars, regular) {
|
|
|
243
263
|
for (const k of ["isfirstbar", "islastbar", "ismarket", "isfirstbar_regular", "islastbar_regular"]) out[k][i] = true;
|
|
244
264
|
continue;
|
|
245
265
|
}
|
|
246
|
-
const at = bars.
|
|
266
|
+
const at = bars.locateBar(t);
|
|
247
267
|
if (!at) {
|
|
248
268
|
previousPeriod = previousRegular = "";
|
|
249
269
|
continue;
|
|
@@ -257,7 +277,8 @@ function sessionFlags(times, bars, regular) {
|
|
|
257
277
|
const stray = bars.stray(t);
|
|
258
278
|
out.islastbar[i] = !stray && close >= end;
|
|
259
279
|
const regularPeriods = regular.calendar.periods(at.date);
|
|
260
|
-
const
|
|
280
|
+
const open = Math.max(t, start);
|
|
281
|
+
const r = regularPeriods.findIndex(([s, e]) => s <= open && open < e);
|
|
261
282
|
const market = r !== -1;
|
|
262
283
|
out.ismarket[i] = market || regular.stray(t);
|
|
263
284
|
if (!market && regularPeriods.length) {
|
|
@@ -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 /** 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;A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AAK,yBAAQ,MAAM,IAAI,IAAI,CAAC;AAClC,aAAO,CAAC,MAAM,KAAK,OAAO,SAAK,yBAAQ,MAAM,EAAE,CAAC;AAAA,IAClD;AACA,UAAM,OAAO,OAAO,IAAI;AACxB,UAAM,QAAQ,KAAK,MAAM,OAAO,GAAG,IAAI;AACvC,UAAM,aAAa,SAAU,QAAQ,KAAK;AAC1C,UAAM,YAAY,KAAK,IAAI,aAAa,IAAI,GAAG,EAAE;AACjD,WAAO,KAAC,4BAAW,OAAO,MAAQ,aAAa,MAAM,CAAC,OAAG,0BAAS,OAAO,MAAQ,YAAY,MAAM,CAAC,CAAC;AAAA,EACvG;AAAA,EAEQ,UAAU,WAAkC;AAClD,QAAI,KAAK,KAAK,WAAW,IAAI,SAAS;AACtC,QAAI,CAAC,GAAI,MAAK,WAAW,IAAI,WAAY,SAAK,uBAAK,SAAS,CAAE;AAC9D,WAAO;AAAA,EACT;AAAA,EAEQ,SAAS,MAAuB;AACtC,WAAO,KAAK,SAAS,QAAQ,IAAI,EAAE,CAAC,EAAG,CAAC;AAAA,EAC1C;AAAA,EAEQ,OAAO,MAAuB;AACpC,UAAM,IAAI,KAAK,SAAS,QAAQ,IAAI;AACpC,WAAO,EAAE,EAAE,SAAS,CAAC,EAAG,CAAC;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,MAAc,WAA2B;AAC9C,UAAM,KAAK,KAAK,UAAU,SAAS;AACnC,UAAM,KAAK,KAAK,
|
|
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.locateBar(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 ms = in_seconds(tf.period) * 1000;\n if (this.calendar.barOrigin !== null) return this.calendar.gridStart(time, ms);\n const [start] = this.calendar.periods(at.date)[at.index]!;\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.locateBar(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 this.intradayClose(time, timeframe, 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.locateBar(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.locateBar(time);\n if (!at) return NaN;\n if (tf.isintraday) {\n const [, end] = this.calendar.periods(at.date)[at.index]!;\n return this.intradayClose(time, timeframe, 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.locateBar(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 grid = this.calendar.barOrigin !== null;\n // on the clock grid, the next session opens in the bar of the grid that holds it\n const open = (t: number): number => (grid ? this.calendar.gridStart(t, ms) : t);\n const next = grid ? this.calendar.gridStart(time, ms) + ms : start + (Math.floor((time - start) / ms) + 1) * ms;\n if (at.inside && next < end) return next;\n if (at.index + 1 < periods.length) return open(periods[at.index + 1]![0]);\n const day = this.calendar.firstTradingDay(addDays(at.date, 1), addDays(at.date, 400));\n return day === null ? NaN : open(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 /**\n * Trading day and period of a chart bar that opens at `time` (see TradingCalendar.locate). On the clock grid\n * (`barOrigin`), an intraday bar that opens before a session period but holds its open belongs to that period\n * (a 01:00 five-minute bar when trading opens at 01:01).\n */\n locateBar(time: number): ReturnType<TradingCalendar['locate']> {\n const at = this.calendar.locate(time);\n if (this.calendar.barOrigin === null || this.chart.isdwm || at?.inside) return at;\n const ahead = this.calendar.locate(time + in_seconds(this.chart.period) * 1000 - 1);\n return ahead?.inside && this.calendar.periods(ahead.date)[ahead.index]![0] > time ? ahead : at;\n }\n\n /** Close of the intraday `timeframe` period that holds `time`: cut at the session period `end`, except on the\n * clock grid, whose bars run their whole length. */\n private intradayClose(time: number, timeframe: string, end: number): number {\n const close = this.timeOf(time, timeframe) + in_seconds(this.timeframe(timeframe).period) * 1000;\n return this.calendar.barOrigin !== null ? close : Math.min(close, end);\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.locateBar(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.locateBar(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.locateBar(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 // a bar of the clock grid that holds the open (see locateBar) is read at the open\n const open = Math.max(t, start);\n const r = regularPeriods.findIndex(([s, e]) => s <= open && open < 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,OAA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,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
|
}
|
|
@@ -25,6 +25,12 @@ export interface SessionSpec {
|
|
|
25
25
|
corrections?: string;
|
|
26
26
|
/** Exchange holidays as "YYYYMMDD" (symbol info "session_holidays"), comma-separated or as an array */
|
|
27
27
|
holidays?: string | string[];
|
|
28
|
+
/**
|
|
29
|
+
* Minutes after midnight (in the time zone) where the intraday bars of every size start their grid, for a source
|
|
30
|
+
* whose bars ignore the session opens (a broker terminal: 5-minute bars at 01:00, 01:05... when trading opens at
|
|
31
|
+
* 01:01). Unset: intraday periods start at each session period start.
|
|
32
|
+
*/
|
|
33
|
+
barOrigin?: number | null;
|
|
28
34
|
}
|
|
29
35
|
/** Date as YYYYMMDD. */
|
|
30
36
|
export type DateNum = number;
|
|
@@ -54,6 +60,8 @@ export declare class TradingCalendar {
|
|
|
54
60
|
/** The last time located and its result: the functions of one bar locate the same time several times */
|
|
55
61
|
private lastTime;
|
|
56
62
|
private lastAt;
|
|
63
|
+
/** Minutes after midnight of the intraday bar grid (`SessionSpec.barOrigin`), null when bars start at the periods */
|
|
64
|
+
readonly barOrigin: number | null;
|
|
57
65
|
/**
|
|
58
66
|
* @param timezone - Time zone of the session hours (the exchange time zone)
|
|
59
67
|
* @param spec - Session information (hours, corrections, holidays)
|
|
@@ -66,6 +74,8 @@ export declare class TradingCalendar {
|
|
|
66
74
|
periods(date: DateNum): Array<[number, number]>;
|
|
67
75
|
/** The session version in force on `date` (the last one that starts on or before it). */
|
|
68
76
|
private versionOf;
|
|
77
|
+
/** Start of the `ms` bar of the clock grid (`barOrigin`) that holds `time`; each local day restarts the grid. */
|
|
78
|
+
gridStart(time: number, ms: number): number;
|
|
69
79
|
isTradingDay(date: DateNum): boolean;
|
|
70
80
|
/**
|
|
71
81
|
* True for a bar that opens after the session of its day was cut by a correction (an early close) but inside the
|
|
@@ -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;
|
|
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;IAC7B;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAED,wBAAwB;AACxB,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC;AAoB7B,eAAO,MAAM,SAAS,G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,iHAAiH;IACjH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM;IAY3C,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO;IAIpC;;;OAGG;IACH,eAAe,CAAC,IAAI,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"}
|
|
@@ -117,6 +117,7 @@ class TradingCalendar {
|
|
|
117
117
|
constructor(timezone, spec, rule = "symbol", dayPlacement = false) {
|
|
118
118
|
this.timezone = timezone;
|
|
119
119
|
this.pine = rule === "pine" && dayPlacement;
|
|
120
|
+
this.barOrigin = spec.barOrigin ?? null;
|
|
120
121
|
const [first = "", ...changes] = spec.session.split("#");
|
|
121
122
|
this.versions = [parseVersion(first, 0, spec.session, rule)];
|
|
122
123
|
for (const change of changes) {
|
|
@@ -144,6 +145,8 @@ class TradingCalendar {
|
|
|
144
145
|
/** The last time located and its result: the functions of one bar locate the same time several times */
|
|
145
146
|
lastTime = NaN;
|
|
146
147
|
lastAt = null;
|
|
148
|
+
/** Minutes after midnight of the intraday bar grid (`SessionSpec.barOrigin`), null when bars start at the periods */
|
|
149
|
+
barOrigin;
|
|
147
150
|
/** Session periods [start, end) of the trading day `date`; empty when the day has no session. */
|
|
148
151
|
periods(date) {
|
|
149
152
|
let out = this.cache.get(date);
|
|
@@ -174,6 +177,18 @@ class TradingCalendar {
|
|
|
174
177
|
while (k >= 0 && !(this.versions[k].from <= date)) k--;
|
|
175
178
|
return this.versions[k];
|
|
176
179
|
}
|
|
180
|
+
/** Start of the `ms` bar of the clock grid (`barOrigin`) that holds `time`; each local day restarts the grid. */
|
|
181
|
+
gridStart(time, ms) {
|
|
182
|
+
const origin = this.barOrigin ?? 0;
|
|
183
|
+
const local = Math.floor((time + (0, import_timezone.zoneOffset)(time, this.timezone)) / MS_PER_DAY);
|
|
184
|
+
const anchorOf = (day) => {
|
|
185
|
+
const { year, month, day: d } = (0, import_timezone.civilFromDays)(day);
|
|
186
|
+
return (0, import_timezone.zonedToUnix)(this.timezone, year, month, d, Math.floor(origin / 60), origin % 60);
|
|
187
|
+
};
|
|
188
|
+
let anchor = anchorOf(local);
|
|
189
|
+
if (anchor > time) anchor = anchorOf(local - 1);
|
|
190
|
+
return anchor + Math.floor((time - anchor) / ms) * ms;
|
|
191
|
+
}
|
|
177
192
|
isTradingDay(date) {
|
|
178
193
|
return this.periods(date).length > 0;
|
|
179
194
|
}
|
|
@@ -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 { 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;
|
|
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 * Minutes after midnight (in the time zone) where the intraday bars of every size start their grid, for a source\n * whose bars ignore the session opens (a broker terminal: 5-minute bars at 01:00, 01:05... when trading opens at\n * 01:01). Unset: intraday periods start at each session period start.\n */\n barOrigin?: number | null;\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 /** Minutes after midnight of the intraday bar grid (`SessionSpec.barOrigin`), null when bars start at the periods */\n readonly barOrigin: number | 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 this.barOrigin = spec.barOrigin ?? null;\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 /** Start of the `ms` bar of the clock grid (`barOrigin`) that holds `time`; each local day restarts the grid. */\n gridStart(time: number, ms: number): number {\n const origin = this.barOrigin ?? 0;\n const local = Math.floor((time + zoneOffset(time, this.timezone)) / MS_PER_DAY);\n const anchorOf = (day: number): number => {\n const { year, month, day: d } = civilFromDays(day);\n return zonedToUnix(this.timezone, year, month, d, Math.floor(origin / 60), origin % 60);\n };\n let anchor = anchorOf(local);\n if (anchor > time) anchor = anchorOf(local - 1);\n return anchor + Math.floor((time - anchor) / ms) * ms;\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;AAuC1B,MAAM,YAAY,CAAC,GAAW,GAAW,MAAuB,IAAI,MAAQ,IAAI,MAAM;AAC7F,MAAM,QAAQ,CAAC,SAA0B,KAAK,IAAI,KAAK,MAAM,OAAO,GAAK,GAAG,KAAK,MAAM,OAAO,GAAG,IAAI,MAAM,GAAG,OAAO,GAAG;AAExH,MAAM,aAAa;AAEnB,MAAM,UAAU;AAOhB,SAAS,MAAM,MAAuB;AACpC,MAAI,IAAI,KAAK,MAAM,OAAO,GAAK;AAC/B,QAAM,IAAI,KAAK,MAAM,OAAO,GAAG,IAAI;AACnC,MAAI,EAAE,KAAK,OAAO,KAAK,KAAK,KAAK,IAAK,QAAO,MAAM,IAAI,IAAI;AAE3D,MAAI,KAAK,EAAG;AACZ,QAAM,M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K,IAAI,GAAG;AAC1B,QAAI,CAAC,IAAI;AACP,YAAM,OAAO,OAAO,GAAG;AACvB,WAAK,EAAE,MAAM,SAAS,KAAK,QAAQ,IAAI,EAAE;AACzC,WAAK,KAAK,IAAI,KAAK,EAAE;AAAA,IACvB;AACA,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,gBAAgB,MAAe,IAA6B;AAC1D,aAAS,OAAO,MAAM,QAAQ,IAAI,OAAO,QAAQ,MAAM,CAAC,EAAG,KAAI,KAAK,aAAa,IAAI,EAAG,QAAO;AAC/F,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,eAAe,MAAe,IAA6B;AACzD,aAAS,OAAO,IAAI,QAAQ,MAAM,OAAO,QAAQ,MAAM,EAAE,EAAG,KAAI,KAAK,aAAa,IAAI,EAAG,QAAO;AAChG,WAAO;AAAA,EACT;AACF;AAGO,MAAM,YAAY,CAAC,SAA2B,QAAQ,MAAM,GAAG,UAAU,IAAI,IAAI,KAAK,EAAE;AAExF,MAAM,aAAa,CAAC,SAA2B,KAAK,MAAM,OAAO,GAAG,IAAI,MAAM;AAE9E,MAAM,WAAW,CAAC,SAA2B,QAAQ,WAAW,QAAQ,WAAW,IAAI,GAAG,EAAE,CAAC,GAAG,EAAE;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -159,7 +159,7 @@ function resampleSession(bars, timeframe, chart) {
|
|
|
159
159
|
const calendar = tf.isdwm ? s.regular : s.bars;
|
|
160
160
|
const starts = times.map((t) => calendar.timeOf(t, tf.period));
|
|
161
161
|
const dayPeriods = times.map((t, i) => {
|
|
162
|
-
const at = s.bars.
|
|
162
|
+
const at = s.bars.locateBar(t);
|
|
163
163
|
const periods = at ? calendar.calendar.periods(at.date) : [];
|
|
164
164
|
return periods.length ? calendar.timeOf(periods[0][0], tf.period) : starts[i];
|
|
165
165
|
});
|