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
package/src/session/bars.ts
CHANGED
|
@@ -126,14 +126,15 @@ export class SessionBars {
|
|
|
126
126
|
*/
|
|
127
127
|
timeOf(time: number, timeframe: string): number {
|
|
128
128
|
const tf = this.timeframe(timeframe);
|
|
129
|
-
const at = this.
|
|
129
|
+
const at = this.locateBar(time);
|
|
130
130
|
if (!at) return NaN;
|
|
131
131
|
if (tf.isintraday) {
|
|
132
132
|
if (this.chart.isdwm) return time;
|
|
133
133
|
// a bar after an early close has no intraday period either (#173)
|
|
134
134
|
if (this.stray(time)) return NaN;
|
|
135
|
-
const [start] = this.calendar.periods(at.date)[at.index]!;
|
|
136
135
|
const ms = in_seconds(tf.period) * 1000;
|
|
136
|
+
if (this.calendar.barOrigin !== null) return this.calendar.gridStart(time, ms);
|
|
137
|
+
const [start] = this.calendar.periods(at.date)[at.index]!;
|
|
137
138
|
return start + Math.floor((time - start) / ms) * ms;
|
|
138
139
|
}
|
|
139
140
|
const { unit, n } = unitOf(tf);
|
|
@@ -146,14 +147,14 @@ export class SessionBars {
|
|
|
146
147
|
/** PineScript `time_close(tf)` for a bar that opens at `time`. */
|
|
147
148
|
closeOf(time: number, timeframe: string): number {
|
|
148
149
|
const tf = this.timeframe(timeframe);
|
|
149
|
-
const at = this.
|
|
150
|
+
const at = this.locateBar(time);
|
|
150
151
|
if (!at) return NaN;
|
|
151
152
|
if (tf.isintraday) {
|
|
152
153
|
const [, end] = this.calendar.periods(at.date)[at.index]!;
|
|
153
154
|
// on D / W / M charts: the end of the session period that contains the bar open
|
|
154
155
|
if (this.chart.isdwm) return end;
|
|
155
156
|
if (this.stray(time)) return NaN;
|
|
156
|
-
return
|
|
157
|
+
return this.intradayClose(time, timeframe, end);
|
|
157
158
|
}
|
|
158
159
|
const { unit, n } = unitOf(tf);
|
|
159
160
|
if (unit === 'D' && n === 1 && this.stray(time)) return NaN;
|
|
@@ -170,7 +171,7 @@ export class SessionBars {
|
|
|
170
171
|
|
|
171
172
|
/** Trading day of a bar: on D / W / M charts the last trading day of the chart period. */
|
|
172
173
|
tradingDay(time: number): DateNum | null {
|
|
173
|
-
const at = this.
|
|
174
|
+
const at = this.locateBar(time);
|
|
174
175
|
if (!at || this.stray(time)) return null;
|
|
175
176
|
if (this.chart.isdwm) {
|
|
176
177
|
const { unit, n } = unitOf(this.chart);
|
|
@@ -187,11 +188,11 @@ export class SessionBars {
|
|
|
187
188
|
*/
|
|
188
189
|
periodEnd(time: number, timeframe: string): number {
|
|
189
190
|
const tf = this.timeframe(timeframe);
|
|
190
|
-
const at = this.
|
|
191
|
+
const at = this.locateBar(time);
|
|
191
192
|
if (!at) return NaN;
|
|
192
193
|
if (tf.isintraday) {
|
|
193
194
|
const [, end] = this.calendar.periods(at.date)[at.index]!;
|
|
194
|
-
return
|
|
195
|
+
return this.intradayClose(time, timeframe, end);
|
|
195
196
|
}
|
|
196
197
|
const { unit, n } = unitOf(tf);
|
|
197
198
|
const [from, to] = this.periodRange(at.date, unit, n);
|
|
@@ -211,17 +212,20 @@ export class SessionBars {
|
|
|
211
212
|
* trading day of the next chart period. NaN when none is found within 400 days.
|
|
212
213
|
*/
|
|
213
214
|
nextBarTime(time: number): number {
|
|
214
|
-
const at = this.
|
|
215
|
+
const at = this.locateBar(time);
|
|
215
216
|
if (!at) return NaN;
|
|
216
217
|
if (!this.chart.isdwm) {
|
|
217
218
|
const periods = this.calendar.periods(at.date);
|
|
218
219
|
const [start, end] = periods[at.index]!;
|
|
219
220
|
const ms = in_seconds(this.chart.period) * 1000;
|
|
220
|
-
const
|
|
221
|
+
const grid = this.calendar.barOrigin !== null;
|
|
222
|
+
// on the clock grid, the next session opens in the bar of the grid that holds it
|
|
223
|
+
const open = (t: number): number => (grid ? this.calendar.gridStart(t, ms) : t);
|
|
224
|
+
const next = grid ? this.calendar.gridStart(time, ms) + ms : start + (Math.floor((time - start) / ms) + 1) * ms;
|
|
221
225
|
if (at.inside && next < end) return next;
|
|
222
|
-
if (at.index + 1 < periods.length) return periods[at.index + 1]![0];
|
|
226
|
+
if (at.index + 1 < periods.length) return open(periods[at.index + 1]![0]);
|
|
223
227
|
const day = this.calendar.firstTradingDay(addDays(at.date, 1), addDays(at.date, 400));
|
|
224
|
-
return day === null ? NaN : this.dayStart(day);
|
|
228
|
+
return day === null ? NaN : open(this.dayStart(day));
|
|
225
229
|
}
|
|
226
230
|
const { unit, n } = unitOf(this.chart);
|
|
227
231
|
const [, to] = this.periodRange(at.date, unit, n);
|
|
@@ -229,6 +233,25 @@ export class SessionBars {
|
|
|
229
233
|
return day === null ? NaN : this.dayStart(day);
|
|
230
234
|
}
|
|
231
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Trading day and period of a chart bar that opens at `time` (see TradingCalendar.locate). On the clock grid
|
|
238
|
+
* (`barOrigin`), an intraday bar that opens before a session period but holds its open belongs to that period
|
|
239
|
+
* (a 01:00 five-minute bar when trading opens at 01:01).
|
|
240
|
+
*/
|
|
241
|
+
locateBar(time: number): ReturnType<TradingCalendar['locate']> {
|
|
242
|
+
const at = this.calendar.locate(time);
|
|
243
|
+
if (this.calendar.barOrigin === null || this.chart.isdwm || at?.inside) return at;
|
|
244
|
+
const ahead = this.calendar.locate(time + in_seconds(this.chart.period) * 1000 - 1);
|
|
245
|
+
return ahead?.inside && this.calendar.periods(ahead.date)[ahead.index]![0] > time ? ahead : at;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Close of the intraday `timeframe` period that holds `time`: cut at the session period `end`, except on the
|
|
249
|
+
* clock grid, whose bars run their whole length. */
|
|
250
|
+
private intradayClose(time: number, timeframe: string, end: number): number {
|
|
251
|
+
const close = this.timeOf(time, timeframe) + in_seconds(this.timeframe(timeframe).period) * 1000;
|
|
252
|
+
return this.calendar.barOrigin !== null ? close : Math.min(close, end);
|
|
253
|
+
}
|
|
254
|
+
|
|
232
255
|
/** An intraday bar that opens after an early close of its day (see TradingCalendar.afterEarlyClose). */
|
|
233
256
|
stray(time: number): boolean {
|
|
234
257
|
return !this.chart.isdwm && this.calendar.afterEarlyClose(time);
|
|
@@ -236,12 +259,12 @@ export class SessionBars {
|
|
|
236
259
|
|
|
237
260
|
/** True when the bar at `time` is inside the session. */
|
|
238
261
|
inside(time: number): boolean {
|
|
239
|
-
return this.
|
|
262
|
+
return this.locateBar(time)?.inside ?? false;
|
|
240
263
|
}
|
|
241
264
|
|
|
242
265
|
/** Session start and end of the trading day of `time`, or null. */
|
|
243
266
|
dayBounds(time: number): { date: DateNum; start: number; end: number } | null {
|
|
244
|
-
const at = this.
|
|
267
|
+
const at = this.locateBar(time);
|
|
245
268
|
return at ? { date: at.date, start: this.dayStart(at.date), end: this.dayEnd(at.date) } : null;
|
|
246
269
|
}
|
|
247
270
|
}
|
|
@@ -302,7 +325,7 @@ export function sessionFlags(times: number[], bars: SessionBars, regular: Sessio
|
|
|
302
325
|
for (const k of ['isfirstbar', 'islastbar', 'ismarket', 'isfirstbar_regular', 'islastbar_regular'] as const) out[k][i] = true;
|
|
303
326
|
continue;
|
|
304
327
|
}
|
|
305
|
-
const at = bars.
|
|
328
|
+
const at = bars.locateBar(t);
|
|
306
329
|
if (!at) {
|
|
307
330
|
previousPeriod = previousRegular = '';
|
|
308
331
|
continue;
|
|
@@ -317,7 +340,9 @@ export function sessionFlags(times: number[], bars: SessionBars, regular: Sessio
|
|
|
317
340
|
const stray = bars.stray(t);
|
|
318
341
|
out.islastbar[i] = !stray && close >= end;
|
|
319
342
|
const regularPeriods = regular.calendar.periods(at.date);
|
|
320
|
-
|
|
343
|
+
// a bar of the clock grid that holds the open (see locateBar) is read at the open
|
|
344
|
+
const open = Math.max(t, start);
|
|
345
|
+
const r = regularPeriods.findIndex(([s, e]) => s <= open && open < e);
|
|
321
346
|
const market = r !== -1;
|
|
322
347
|
out.ismarket[i] = market || regular.stray(t);
|
|
323
348
|
if (!market && regularPeriods.length) {
|
package/src/session/calendar.ts
CHANGED
|
@@ -29,6 +29,12 @@ export interface SessionSpec {
|
|
|
29
29
|
corrections?: string;
|
|
30
30
|
/** Exchange holidays as "YYYYMMDD" (symbol info "session_holidays"), comma-separated or as an array */
|
|
31
31
|
holidays?: string | string[];
|
|
32
|
+
/**
|
|
33
|
+
* Minutes after midnight (in the time zone) where the intraday bars of every size start their grid, for a source
|
|
34
|
+
* whose bars ignore the session opens (a broker terminal: 5-minute bars at 01:00, 01:05... when trading opens at
|
|
35
|
+
* 01:01). Unset: intraday periods start at each session period start.
|
|
36
|
+
*/
|
|
37
|
+
barOrigin?: number | null;
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
/** Date as YYYYMMDD. */
|
|
@@ -178,6 +184,8 @@ export class TradingCalendar {
|
|
|
178
184
|
/** The last time located and its result: the functions of one bar locate the same time several times */
|
|
179
185
|
private lastTime = NaN;
|
|
180
186
|
private lastAt: Located | null = null;
|
|
187
|
+
/** Minutes after midnight of the intraday bar grid (`SessionSpec.barOrigin`), null when bars start at the periods */
|
|
188
|
+
readonly barOrigin: number | null;
|
|
181
189
|
|
|
182
190
|
/**
|
|
183
191
|
* @param timezone - Time zone of the session hours (the exchange time zone)
|
|
@@ -193,6 +201,7 @@ export class TradingCalendar {
|
|
|
193
201
|
dayPlacement = false
|
|
194
202
|
) {
|
|
195
203
|
this.pine = rule === 'pine' && dayPlacement;
|
|
204
|
+
this.barOrigin = spec.barOrigin ?? null;
|
|
196
205
|
const [first = '', ...changes] = spec.session.split('#');
|
|
197
206
|
this.versions = [parseVersion(first, 0, spec.session, rule)];
|
|
198
207
|
for (const change of changes) {
|
|
@@ -250,6 +259,19 @@ export class TradingCalendar {
|
|
|
250
259
|
return this.versions[k]!;
|
|
251
260
|
}
|
|
252
261
|
|
|
262
|
+
/** Start of the `ms` bar of the clock grid (`barOrigin`) that holds `time`; each local day restarts the grid. */
|
|
263
|
+
gridStart(time: number, ms: number): number {
|
|
264
|
+
const origin = this.barOrigin ?? 0;
|
|
265
|
+
const local = Math.floor((time + zoneOffset(time, this.timezone)) / MS_PER_DAY);
|
|
266
|
+
const anchorOf = (day: number): number => {
|
|
267
|
+
const { year, month, day: d } = civilFromDays(day);
|
|
268
|
+
return zonedToUnix(this.timezone, year, month, d, Math.floor(origin / 60), origin % 60);
|
|
269
|
+
};
|
|
270
|
+
let anchor = anchorOf(local);
|
|
271
|
+
if (anchor > time) anchor = anchorOf(local - 1);
|
|
272
|
+
return anchor + Math.floor((time - anchor) / ms) * ms;
|
|
273
|
+
}
|
|
274
|
+
|
|
253
275
|
isTradingDay(date: DateNum): boolean {
|
|
254
276
|
return this.periods(date).length > 0;
|
|
255
277
|
}
|
package/src/session/context.ts
CHANGED
|
@@ -235,7 +235,7 @@ export function resampleSession(bars: Bar[], timeframe: string, chart: SessionCo
|
|
|
235
235
|
const calendar = tf.isdwm ? s.regular : s.bars;
|
|
236
236
|
const starts = times.map((t) => calendar.timeOf(t, tf.period));
|
|
237
237
|
const dayPeriods = times.map((t, i) => {
|
|
238
|
-
const at = s.bars.
|
|
238
|
+
const at = s.bars.locateBar(t);
|
|
239
239
|
const periods = at ? calendar.calendar.periods(at.date) : [];
|
|
240
240
|
return periods.length ? calendar.timeOf(periods[0]![0], tf.period) : starts[i]!;
|
|
241
241
|
});
|