From 07fafdd7fc31f59254d2f496a1773ebdd8581b1c Mon Sep 17 00:00:00 2001 From: Caoyuan Deng Date: Wed, 4 Mar 2026 19:30:37 -0800 Subject: [PATCH] bar_index extra handling? Just for your consideration. #139 --- src/PineTS.class.ts | 6 ++++++ src/transpiler/settings.ts | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/PineTS.class.ts b/src/PineTS.class.ts index 411d377..78ec6f2 100644 --- a/src/PineTS.class.ts +++ b/src/PineTS.class.ts @@ -24,6 +24,7 @@ export class PineTS { public hlcc4: any = []; public openTime: any = []; public closeTime: any = []; + public bar_index: any = []; //#endregion //#region run context @@ -82,6 +83,7 @@ export class PineTS { const _hlcc4 = marketData.map((d) => (d.high + d.low + d.close + d.close) / 4); const _openTime = marketData.map((d) => d.openTime); const _closeTime = marketData.map((d) => d.closeTime); + const _bar_index = marketData.map((d, index) => index); this.open = _open; this.close = _close; @@ -94,6 +96,7 @@ export class PineTS { this.hlcc4 = _hlcc4; this.openTime = _openTime; this.closeTime = _closeTime; + this.bar_index = _bar_index; if (source && (source as IProvider).getSymbolInfo) { const symbolInfo = (source as IProvider) @@ -482,6 +485,7 @@ export class PineTS { this.hlcc4[index] = (candle.high + candle.low + candle.close + candle.close) / 4; this.openTime[index] = candle.openTime; this.closeTime[index] = candle.closeTime; + this.bar_index[index] = index; } /** @@ -501,6 +505,7 @@ export class PineTS { this.hlcc4.push((candle.high + candle.low + candle.close + candle.close) / 4); this.openTime.push(candle.openTime); this.closeTime.push(candle.closeTime); + this.bar_index.push(this.bar_index.length) } /** @@ -587,6 +592,7 @@ export class PineTS { context.data.hlcc4 = new Series([]); context.data.openTime = new Series([]); context.data.closeTime = new Series([]); + context.data.bar_index = new Series([]); context.length = this.data.length; diff --git a/src/transpiler/settings.ts b/src/transpiler/settings.ts index 2dd83ec..eb09a71 100644 --- a/src/transpiler/settings.ts +++ b/src/transpiler/settings.ts @@ -14,7 +14,7 @@ export const ASYNC_METHODS = ['request.security', 'request.security_lower_tf']; export const FACTORY_METHODS = ['line.new', 'line.copy', 'label.new', 'label.copy', 'polyline.new', 'box.new', 'box.copy', 'table.new']; // All known data variables in the context -export const CONTEXT_DATA_VARS = ['open', 'high', 'low', 'close', 'volume', 'hl2', 'hlc3', 'ohlc4', 'hlcc4', 'openTime', 'closeTime']; +export const CONTEXT_DATA_VARS = ['open', 'high', 'low', 'close', 'volume', 'hl2', 'hlc3', 'ohlc4', 'hlcc4', 'openTime', 'closeTime', 'bar_index']; // All known Pine variables in the context export const CONTEXT_PINE_VARS = [ @@ -69,7 +69,6 @@ export const CONTEXT_PINE_VARS = [ 'barstate', //builtin variables - 'bar_index', 'last_bar_index', 'last_bar_time', 'inputs',