pinets 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/README.md +47 -3
  2. package/dist/pinets.dev.browser.js +3019 -391
  3. package/dist/pinets.dev.cjs +3106 -365
  4. package/dist/pinets.dev.cjs.map +1 -1
  5. package/dist/pinets.dev.es.js +3110 -360
  6. package/dist/pinets.dev.es.js.map +1 -1
  7. package/dist/pinets.min.browser.js +15 -14
  8. package/dist/pinets.min.cjs +15 -14
  9. package/dist/pinets.min.es.js +5 -4
  10. package/dist/types/Context.class.d.ts +19 -3
  11. package/dist/types/PineTS.class.d.ts +2 -0
  12. package/dist/types/marketData/Binance/BinanceProvider.class.d.ts +2 -1
  13. package/dist/types/marketData/IProvider.d.ts +43 -0
  14. package/dist/types/marketData/Mock/MockProvider.class.d.ts +13 -1
  15. package/dist/types/namespaces/Barstate.d.ts +13 -0
  16. package/dist/types/namespaces/Core.d.ts +15 -0
  17. package/dist/types/namespaces/Log.d.ts +10 -0
  18. package/dist/types/namespaces/Str.d.ts +23 -0
  19. package/dist/types/namespaces/Timeframe.d.ts +17 -0
  20. package/dist/types/namespaces/Types.d.ts +86 -0
  21. package/dist/types/namespaces/array/PineArrayObject.d.ts +102 -1
  22. package/dist/types/namespaces/array/array.index.d.ts +1 -129
  23. package/dist/types/namespaces/array/methods/avg.d.ts +2 -1
  24. package/dist/types/namespaces/array/methods/binary_search.d.ts +2 -0
  25. package/dist/types/namespaces/array/methods/binary_search_leftmost.d.ts +2 -0
  26. package/dist/types/namespaces/array/methods/binary_search_rightmost.d.ts +2 -0
  27. package/dist/types/namespaces/array/methods/covariance.d.ts +2 -1
  28. package/dist/types/namespaces/array/methods/every.d.ts +1 -1
  29. package/dist/types/namespaces/array/methods/fill.d.ts +2 -1
  30. package/dist/types/namespaces/array/methods/join.d.ts +5 -0
  31. package/dist/types/namespaces/array/methods/median.d.ts +2 -0
  32. package/dist/types/namespaces/array/methods/mode.d.ts +2 -0
  33. package/dist/types/namespaces/array/methods/new.d.ts +2 -1
  34. package/dist/types/namespaces/array/methods/new_float.d.ts +2 -1
  35. package/dist/types/namespaces/array/methods/new_int.d.ts +2 -1
  36. package/dist/types/namespaces/array/methods/percentile_linear_interpolation.d.ts +3 -0
  37. package/dist/types/namespaces/array/methods/percentile_nearest_rank.d.ts +2 -0
  38. package/dist/types/namespaces/array/methods/percentrank.d.ts +2 -0
  39. package/dist/types/namespaces/array/methods/push.d.ts +2 -1
  40. package/dist/types/namespaces/array/methods/range.d.ts +2 -1
  41. package/dist/types/namespaces/array/methods/set.d.ts +2 -1
  42. package/dist/types/namespaces/array/methods/some.d.ts +1 -1
  43. package/dist/types/namespaces/array/methods/sort.d.ts +2 -1
  44. package/dist/types/namespaces/array/methods/sort_indices.d.ts +2 -1
  45. package/dist/types/namespaces/array/methods/stdev.d.ts +2 -1
  46. package/dist/types/namespaces/array/methods/sum.d.ts +2 -1
  47. package/dist/types/namespaces/array/methods/unshift.d.ts +2 -1
  48. package/dist/types/namespaces/array/methods/variance.d.ts +2 -1
  49. package/dist/types/namespaces/array/utils.d.ts +5 -0
  50. package/dist/types/namespaces/input/methods/any.d.ts +1 -2
  51. package/dist/types/namespaces/input/methods/bool.d.ts +1 -2
  52. package/dist/types/namespaces/input/methods/color.d.ts +1 -2
  53. package/dist/types/namespaces/input/methods/enum.d.ts +1 -2
  54. package/dist/types/namespaces/input/methods/float.d.ts +1 -2
  55. package/dist/types/namespaces/input/methods/int.d.ts +1 -2
  56. package/dist/types/namespaces/input/methods/param.d.ts +1 -1
  57. package/dist/types/namespaces/input/methods/price.d.ts +1 -2
  58. package/dist/types/namespaces/input/methods/session.d.ts +1 -2
  59. package/dist/types/namespaces/input/methods/source.d.ts +1 -2
  60. package/dist/types/namespaces/input/methods/string.d.ts +1 -2
  61. package/dist/types/namespaces/input/methods/symbol.d.ts +1 -2
  62. package/dist/types/namespaces/input/methods/text_area.d.ts +1 -2
  63. package/dist/types/namespaces/input/methods/time.d.ts +1 -2
  64. package/dist/types/namespaces/input/methods/timeframe.d.ts +1 -2
  65. package/dist/types/namespaces/input/types.d.ts +12 -1
  66. package/dist/types/namespaces/input/utils.d.ts +14 -0
  67. package/dist/types/namespaces/map/PineMapObject.d.ts +26 -0
  68. package/dist/types/namespaces/map/map.index.d.ts +7 -0
  69. package/dist/types/namespaces/map/methods/clear.d.ts +3 -0
  70. package/dist/types/namespaces/map/methods/contains.d.ts +3 -0
  71. package/dist/types/namespaces/map/methods/copy.d.ts +3 -0
  72. package/dist/types/namespaces/map/methods/get.d.ts +3 -0
  73. package/dist/types/namespaces/map/methods/keys.d.ts +4 -0
  74. package/dist/types/namespaces/map/methods/new.d.ts +3 -0
  75. package/dist/types/namespaces/map/methods/param.d.ts +1 -0
  76. package/dist/types/namespaces/map/methods/put.d.ts +3 -0
  77. package/dist/types/namespaces/map/methods/put_all.d.ts +3 -0
  78. package/dist/types/namespaces/map/methods/remove.d.ts +3 -0
  79. package/dist/types/namespaces/map/methods/size.d.ts +3 -0
  80. package/dist/types/namespaces/map/methods/values.d.ts +4 -0
  81. package/dist/types/namespaces/math/math.index.d.ts +18 -0
  82. package/dist/types/namespaces/math/methods/abs.d.ts +2 -1
  83. package/dist/types/namespaces/math/methods/e.d.ts +5 -0
  84. package/dist/types/namespaces/math/methods/phi.d.ts +5 -0
  85. package/dist/types/namespaces/math/methods/pi.d.ts +5 -0
  86. package/dist/types/namespaces/math/methods/round_to_mintick.d.ts +2 -0
  87. package/dist/types/namespaces/math/methods/rphi.d.ts +5 -0
  88. package/dist/types/namespaces/math/methods/sign.d.ts +2 -0
  89. package/dist/types/namespaces/matrix/PineMatrixObject.d.ts +102 -0
  90. package/dist/types/namespaces/matrix/matrix.index.d.ts +7 -0
  91. package/dist/types/namespaces/matrix/methods/add_col.d.ts +3 -0
  92. package/dist/types/namespaces/matrix/methods/add_row.d.ts +3 -0
  93. package/dist/types/namespaces/matrix/methods/avg.d.ts +3 -0
  94. package/dist/types/namespaces/matrix/methods/col.d.ts +4 -0
  95. package/dist/types/namespaces/matrix/methods/columns.d.ts +3 -0
  96. package/dist/types/namespaces/matrix/methods/concat.d.ts +3 -0
  97. package/dist/types/namespaces/matrix/methods/copy.d.ts +3 -0
  98. package/dist/types/namespaces/matrix/methods/det.d.ts +3 -0
  99. package/dist/types/namespaces/matrix/methods/diff.d.ts +3 -0
  100. package/dist/types/namespaces/matrix/methods/eigenvalues.d.ts +4 -0
  101. package/dist/types/namespaces/matrix/methods/eigenvectors.d.ts +3 -0
  102. package/dist/types/namespaces/matrix/methods/elements_count.d.ts +3 -0
  103. package/dist/types/namespaces/matrix/methods/fill.d.ts +3 -0
  104. package/dist/types/namespaces/matrix/methods/get.d.ts +3 -0
  105. package/dist/types/namespaces/matrix/methods/inv.d.ts +3 -0
  106. package/dist/types/namespaces/matrix/methods/is_antidiagonal.d.ts +3 -0
  107. package/dist/types/namespaces/matrix/methods/is_antisymmetric.d.ts +3 -0
  108. package/dist/types/namespaces/matrix/methods/is_binary.d.ts +3 -0
  109. package/dist/types/namespaces/matrix/methods/is_diagonal.d.ts +3 -0
  110. package/dist/types/namespaces/matrix/methods/is_identity.d.ts +3 -0
  111. package/dist/types/namespaces/matrix/methods/is_square.d.ts +3 -0
  112. package/dist/types/namespaces/matrix/methods/is_stochastic.d.ts +3 -0
  113. package/dist/types/namespaces/matrix/methods/is_symmetric.d.ts +3 -0
  114. package/dist/types/namespaces/matrix/methods/is_triangular.d.ts +3 -0
  115. package/dist/types/namespaces/matrix/methods/is_zero.d.ts +3 -0
  116. package/dist/types/namespaces/matrix/methods/kron.d.ts +3 -0
  117. package/dist/types/namespaces/matrix/methods/max.d.ts +3 -0
  118. package/dist/types/namespaces/matrix/methods/median.d.ts +3 -0
  119. package/dist/types/namespaces/matrix/methods/min.d.ts +3 -0
  120. package/dist/types/namespaces/matrix/methods/mode.d.ts +3 -0
  121. package/dist/types/namespaces/matrix/methods/mult.d.ts +4 -0
  122. package/dist/types/namespaces/matrix/methods/new.d.ts +3 -0
  123. package/dist/types/namespaces/matrix/methods/param.d.ts +1 -0
  124. package/dist/types/namespaces/matrix/methods/pinv.d.ts +3 -0
  125. package/dist/types/namespaces/matrix/methods/pow.d.ts +3 -0
  126. package/dist/types/namespaces/matrix/methods/rank.d.ts +3 -0
  127. package/dist/types/namespaces/matrix/methods/remove_col.d.ts +4 -0
  128. package/dist/types/namespaces/matrix/methods/remove_row.d.ts +4 -0
  129. package/dist/types/namespaces/matrix/methods/reshape.d.ts +3 -0
  130. package/dist/types/namespaces/matrix/methods/reverse.d.ts +3 -0
  131. package/dist/types/namespaces/matrix/methods/row.d.ts +4 -0
  132. package/dist/types/namespaces/matrix/methods/rows.d.ts +3 -0
  133. package/dist/types/namespaces/matrix/methods/set.d.ts +3 -0
  134. package/dist/types/namespaces/matrix/methods/sort.d.ts +3 -0
  135. package/dist/types/namespaces/matrix/methods/submatrix.d.ts +3 -0
  136. package/dist/types/namespaces/matrix/methods/sum.d.ts +3 -0
  137. package/dist/types/namespaces/matrix/methods/swap_columns.d.ts +3 -0
  138. package/dist/types/namespaces/matrix/methods/swap_rows.d.ts +3 -0
  139. package/dist/types/namespaces/matrix/methods/trace.d.ts +3 -0
  140. package/dist/types/namespaces/matrix/methods/transpose.d.ts +3 -0
  141. package/dist/types/namespaces/request/methods/security_lower_tf.d.ts +9 -0
  142. package/dist/types/namespaces/request/request.index.d.ts +3 -0
  143. package/dist/types/namespaces/ta/methods/atr.d.ts +2 -1
  144. package/dist/types/namespaces/ta/methods/tr.d.ts +2 -1
  145. package/dist/types/transpiler/settings.d.ts +4 -0
  146. package/dist/types/types/PineTypes.d.ts +11 -0
  147. package/package.json +6 -4
@@ -2340,7 +2340,7 @@
2340
2340
  this.generator = !!generator;
2341
2341
  };
2342
2342
 
2343
- var types = {
2343
+ var types$2 = {
2344
2344
  b_stat: new TokContext("{", false),
2345
2345
  b_expr: new TokContext("{", true),
2346
2346
  b_tmpl: new TokContext("${", false),
@@ -2356,7 +2356,7 @@
2356
2356
  var pp$6 = Parser.prototype;
2357
2357
 
2358
2358
  pp$6.initialContext = function() {
2359
- return [types.b_stat]
2359
+ return [types$2.b_stat]
2360
2360
  };
2361
2361
 
2362
2362
  pp$6.curContext = function() {
@@ -2365,9 +2365,9 @@
2365
2365
 
2366
2366
  pp$6.braceIsBlock = function(prevType) {
2367
2367
  var parent = this.curContext();
2368
- if (parent === types.f_expr || parent === types.f_stat)
2368
+ if (parent === types$2.f_expr || parent === types$2.f_stat)
2369
2369
  { return true }
2370
- if (prevType === types$1.colon && (parent === types.b_stat || parent === types.b_expr))
2370
+ if (prevType === types$1.colon && (parent === types$2.b_stat || parent === types$2.b_expr))
2371
2371
  { return !parent.isExpr }
2372
2372
 
2373
2373
  // The check for `tt.name && exprAllowed` detects whether we are
@@ -2378,7 +2378,7 @@
2378
2378
  if (prevType === types$1._else || prevType === types$1.semi || prevType === types$1.eof || prevType === types$1.parenR || prevType === types$1.arrow)
2379
2379
  { return true }
2380
2380
  if (prevType === types$1.braceL)
2381
- { return parent === types.b_stat }
2381
+ { return parent === types$2.b_stat }
2382
2382
  if (prevType === types$1._var || prevType === types$1._const || prevType === types$1.name)
2383
2383
  { return false }
2384
2384
  return !this.exprAllowed
@@ -2419,25 +2419,25 @@
2419
2419
  return
2420
2420
  }
2421
2421
  var out = this.context.pop();
2422
- if (out === types.b_stat && this.curContext().token === "function") {
2422
+ if (out === types$2.b_stat && this.curContext().token === "function") {
2423
2423
  out = this.context.pop();
2424
2424
  }
2425
2425
  this.exprAllowed = !out.isExpr;
2426
2426
  };
2427
2427
 
2428
2428
  types$1.braceL.updateContext = function(prevType) {
2429
- this.context.push(this.braceIsBlock(prevType) ? types.b_stat : types.b_expr);
2429
+ this.context.push(this.braceIsBlock(prevType) ? types$2.b_stat : types$2.b_expr);
2430
2430
  this.exprAllowed = true;
2431
2431
  };
2432
2432
 
2433
2433
  types$1.dollarBraceL.updateContext = function() {
2434
- this.context.push(types.b_tmpl);
2434
+ this.context.push(types$2.b_tmpl);
2435
2435
  this.exprAllowed = true;
2436
2436
  };
2437
2437
 
2438
2438
  types$1.parenL.updateContext = function(prevType) {
2439
2439
  var statementParens = prevType === types$1._if || prevType === types$1._for || prevType === types$1._with || prevType === types$1._while;
2440
- this.context.push(statementParens ? types.p_stat : types.p_expr);
2440
+ this.context.push(statementParens ? types$2.p_stat : types$2.p_expr);
2441
2441
  this.exprAllowed = true;
2442
2442
  };
2443
2443
 
@@ -2447,12 +2447,12 @@
2447
2447
 
2448
2448
  types$1._function.updateContext = types$1._class.updateContext = function(prevType) {
2449
2449
  if (prevType.beforeExpr && prevType !== types$1._else &&
2450
- !(prevType === types$1.semi && this.curContext() !== types.p_stat) &&
2450
+ !(prevType === types$1.semi && this.curContext() !== types$2.p_stat) &&
2451
2451
  !(prevType === types$1._return && lineBreak.test(this.input.slice(this.lastTokEnd, this.start))) &&
2452
- !((prevType === types$1.colon || prevType === types$1.braceL) && this.curContext() === types.b_stat))
2453
- { this.context.push(types.f_expr); }
2452
+ !((prevType === types$1.colon || prevType === types$1.braceL) && this.curContext() === types$2.b_stat))
2453
+ { this.context.push(types$2.f_expr); }
2454
2454
  else
2455
- { this.context.push(types.f_stat); }
2455
+ { this.context.push(types$2.f_stat); }
2456
2456
  this.exprAllowed = false;
2457
2457
  };
2458
2458
 
@@ -2462,20 +2462,20 @@
2462
2462
  };
2463
2463
 
2464
2464
  types$1.backQuote.updateContext = function() {
2465
- if (this.curContext() === types.q_tmpl)
2465
+ if (this.curContext() === types$2.q_tmpl)
2466
2466
  { this.context.pop(); }
2467
2467
  else
2468
- { this.context.push(types.q_tmpl); }
2468
+ { this.context.push(types$2.q_tmpl); }
2469
2469
  this.exprAllowed = false;
2470
2470
  };
2471
2471
 
2472
2472
  types$1.star.updateContext = function(prevType) {
2473
2473
  if (prevType === types$1._function) {
2474
2474
  var index = this.context.length - 1;
2475
- if (this.context[index] === types.f_expr)
2476
- { this.context[index] = types.f_expr_gen; }
2475
+ if (this.context[index] === types$2.f_expr)
2476
+ { this.context[index] = types$2.f_expr_gen; }
2477
2477
  else
2478
- { this.context[index] = types.f_gen; }
2478
+ { this.context[index] = types$2.f_gen; }
2479
2479
  }
2480
2480
  this.exprAllowed = true;
2481
2481
  };
@@ -2925,7 +2925,7 @@
2925
2925
  var startPos = this.start, startLoc = this.startLoc, containsEsc = this.containsEsc;
2926
2926
  var id = this.parseIdent(false);
2927
2927
  if (this.options.ecmaVersion >= 8 && !containsEsc && id.name === "async" && !this.canInsertSemicolon() && this.eat(types$1._function)) {
2928
- this.overrideContext(types.f_expr);
2928
+ this.overrideContext(types$2.f_expr);
2929
2929
  return this.parseFunction(this.startNodeAt(startPos, startLoc), 0, false, true, forInit)
2930
2930
  }
2931
2931
  if (canBeArrow && !this.canInsertSemicolon()) {
@@ -2974,7 +2974,7 @@
2974
2974
  return this.finishNode(node, "ArrayExpression")
2975
2975
 
2976
2976
  case types$1.braceL:
2977
- this.overrideContext(types.b_expr);
2977
+ this.overrideContext(types$2.b_expr);
2978
2978
  return this.parseObj(false, refDestructuringErrors)
2979
2979
 
2980
2980
  case types$1._function:
@@ -6130,7 +6130,7 @@
6130
6130
  tokTypes: types$1,
6131
6131
  keywordTypes: keywords,
6132
6132
  TokContext: TokContext,
6133
- tokContexts: types,
6133
+ tokContexts: types$2,
6134
6134
  isIdentifierChar: isIdentifierChar,
6135
6135
  isIdentifierStart: isIdentifierStart,
6136
6136
  Token: Token,
@@ -7376,9 +7376,9 @@
7376
7376
  return state.output
7377
7377
  }
7378
7378
 
7379
- var __defProp$9 = Object.defineProperty;
7380
- var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7381
- var __publicField$9 = (obj, key, value) => __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
7379
+ var __defProp$c = Object.defineProperty;
7380
+ var __defNormalProp$c = (obj, key, value) => key in obj ? __defProp$c(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7381
+ var __publicField$c = (obj, key, value) => __defNormalProp$c(obj, typeof key !== "symbol" ? key + "" : key, value);
7382
7382
  const JS_GLOBAL_LITERALS = /* @__PURE__ */ new Set([
7383
7383
  "Infinity",
7384
7384
  "NaN",
@@ -7419,23 +7419,23 @@
7419
7419
  ]);
7420
7420
  class ScopeManager {
7421
7421
  constructor() {
7422
- __publicField$9(this, "scopes", []);
7423
- __publicField$9(this, "scopeTypes", []);
7424
- __publicField$9(this, "scopeCounts", /* @__PURE__ */ new Map());
7425
- __publicField$9(this, "contextBoundVars", /* @__PURE__ */ new Set());
7426
- __publicField$9(this, "arrayPatternElements", /* @__PURE__ */ new Set());
7427
- __publicField$9(this, "rootParams", /* @__PURE__ */ new Set());
7428
- __publicField$9(this, "localSeriesVars", /* @__PURE__ */ new Set());
7429
- __publicField$9(this, "varKinds", /* @__PURE__ */ new Map());
7430
- __publicField$9(this, "loopVars", /* @__PURE__ */ new Set());
7431
- __publicField$9(this, "loopVarNames", /* @__PURE__ */ new Map());
7422
+ __publicField$c(this, "scopes", []);
7423
+ __publicField$c(this, "scopeTypes", []);
7424
+ __publicField$c(this, "scopeCounts", /* @__PURE__ */ new Map());
7425
+ __publicField$c(this, "contextBoundVars", /* @__PURE__ */ new Set());
7426
+ __publicField$c(this, "arrayPatternElements", /* @__PURE__ */ new Set());
7427
+ __publicField$c(this, "rootParams", /* @__PURE__ */ new Set());
7428
+ __publicField$c(this, "localSeriesVars", /* @__PURE__ */ new Set());
7429
+ __publicField$c(this, "varKinds", /* @__PURE__ */ new Map());
7430
+ __publicField$c(this, "loopVars", /* @__PURE__ */ new Set());
7431
+ __publicField$c(this, "loopVarNames", /* @__PURE__ */ new Map());
7432
7432
  // Map original names to transformed names
7433
- __publicField$9(this, "paramIdCounter", 0);
7434
- __publicField$9(this, "cacheIdCounter", 0);
7435
- __publicField$9(this, "tempVarCounter", 0);
7436
- __publicField$9(this, "taCallIdCounter", 0);
7437
- __publicField$9(this, "hoistingStack", []);
7438
- __publicField$9(this, "suppressHoisting", false);
7433
+ __publicField$c(this, "paramIdCounter", 0);
7434
+ __publicField$c(this, "cacheIdCounter", 0);
7435
+ __publicField$c(this, "tempVarCounter", 0);
7436
+ __publicField$c(this, "taCallIdCounter", 0);
7437
+ __publicField$c(this, "hoistingStack", []);
7438
+ __publicField$c(this, "suppressHoisting", false);
7439
7439
  this.pushScope("glb");
7440
7440
  }
7441
7441
  get nextParamIdArg() {
@@ -8002,6 +8002,45 @@
8002
8002
  }
8003
8003
  };
8004
8004
 
8005
+ const CONTEXT_DATA_VARS = ["open", "high", "low", "close", "volume", "hl2", "hlc3", "ohlc4", "openTime", "closeTime"];
8006
+ const CONTEXT_PINE_VARS = [
8007
+ "input",
8008
+ "ta",
8009
+ "math",
8010
+ "request",
8011
+ "array",
8012
+ "na",
8013
+ "plotchar",
8014
+ "color",
8015
+ "plot",
8016
+ "nz",
8017
+ "strategy",
8018
+ "library",
8019
+ "str",
8020
+ "box",
8021
+ "line",
8022
+ "label",
8023
+ "table",
8024
+ "map",
8025
+ "matrix",
8026
+ "log",
8027
+ "map",
8028
+ //market info
8029
+ "timeframe",
8030
+ "syminfo",
8031
+ "barstate",
8032
+ //builtin variables
8033
+ "bar_index",
8034
+ "last_bar_index",
8035
+ "last_bar_time",
8036
+ // Pine Script enum types
8037
+ "order",
8038
+ "currency",
8039
+ "display",
8040
+ "dayofweek"
8041
+ ];
8042
+ const CONTEXT_CORE_VARS = ["na", "nz", "plot", "plotchar", "color"];
8043
+
8005
8044
  function injectImplicitImports(ast) {
8006
8045
  let mainBody = null;
8007
8046
  let contextParamName = CONTEXT_NAME;
@@ -8067,28 +8106,8 @@
8067
8106
  addDeclared(stmt.id);
8068
8107
  }
8069
8108
  });
8070
- const contextDataVars = ["open", "high", "low", "close", "volume", "hl2", "hlc3", "ohlc4", "openTime", "closeTime"];
8071
- const contextPineVars = [
8072
- "input",
8073
- "ta",
8074
- "math",
8075
- "request",
8076
- "array",
8077
- "na",
8078
- "plotchar",
8079
- "color",
8080
- "plot",
8081
- "nz",
8082
- "strategy",
8083
- "library",
8084
- "str",
8085
- "box",
8086
- "line",
8087
- "label",
8088
- "table",
8089
- "map",
8090
- "matrix"
8091
- ];
8109
+ const contextDataVars = CONTEXT_DATA_VARS;
8110
+ const contextPineVars = CONTEXT_PINE_VARS;
8092
8111
  const missingDataVars = contextDataVars.filter((v) => !declaredVars.has(v));
8093
8112
  const missingPineVars = contextPineVars.filter((v) => !declaredVars.has(v));
8094
8113
  const neededDataVars = missingDataVars.filter((v) => usedIdentifiers.has(v));
@@ -8169,29 +8188,9 @@
8169
8188
  }
8170
8189
  }
8171
8190
  if (!mainBody) return;
8172
- const contextDataVars = /* @__PURE__ */ new Set(["open", "high", "low", "close", "volume", "hl2", "hlc3", "ohlc4", "openTime", "closeTime"]);
8173
- const contextPineVars = /* @__PURE__ */ new Set([
8174
- "input",
8175
- "ta",
8176
- "math",
8177
- "request",
8178
- "array",
8179
- "na",
8180
- "plotchar",
8181
- "color",
8182
- "plot",
8183
- "nz",
8184
- "strategy",
8185
- "library",
8186
- "str",
8187
- "box",
8188
- "line",
8189
- "label",
8190
- "table",
8191
- "map",
8192
- "matrix"
8193
- ]);
8194
- const contextCoreVars = /* @__PURE__ */ new Set(["na", "nz", "plot", "plotchar", "color"]);
8191
+ const contextDataVars = new Set(CONTEXT_DATA_VARS);
8192
+ const contextPineVars = new Set(CONTEXT_PINE_VARS);
8193
+ const contextCoreVars = new Set(CONTEXT_CORE_VARS);
8195
8194
  const renames = /* @__PURE__ */ new Map();
8196
8195
  mainBody.forEach((stmt) => {
8197
8196
  if (stmt.type === "VariableDeclaration") {
@@ -8443,6 +8442,7 @@ ${code}
8443
8442
  type: "Identifier",
8444
8443
  name: "undefined"
8445
8444
  };
8445
+ const KNOWN_NAMESPACES = ["ta", "math", "request", "array", "input"];
8446
8446
  function transformArrayIndex(node, scopeManager) {
8447
8447
  if (node.computed && node.property.type === "Identifier") {
8448
8448
  if (scopeManager.isLoopVariable(node.property.name)) {
@@ -8502,12 +8502,6 @@ ${code}
8502
8502
  if (scopeManager.isLoopVariable(node.name)) {
8503
8503
  return;
8504
8504
  }
8505
- if (scopeManager.isContextBound(node.name) && !scopeManager.isRootParam(node.name)) {
8506
- return;
8507
- }
8508
- const isNamespaceMember = node.parent && node.parent.type === "MemberExpression" && node.parent.object === node && scopeManager.isContextBound(node.name);
8509
- const isParamCall = node.parent && node.parent.type === "CallExpression" && node.parent.callee && node.parent.callee.type === "MemberExpression" && node.parent.callee.property.name === "param";
8510
- node.parent && node.parent.type === "AssignmentExpression" && node.parent.left === node;
8511
8505
  let isSeriesFunctionArg = false;
8512
8506
  if (node.parent && node.parent.type === "CallExpression" && node.parent.arguments.includes(node)) {
8513
8507
  const callee = node.parent.callee;
@@ -8518,13 +8512,20 @@ ${code}
8518
8512
  isSeriesFunctionArg = true;
8519
8513
  }
8520
8514
  } else {
8521
- isSeriesFunctionArg = true;
8515
+ const isNamespaceCall = callee.type === "MemberExpression" && callee.object && callee.object.type === "Identifier" && KNOWN_NAMESPACES.includes(callee.object.name);
8516
+ if (callee.type === "MemberExpression" && !isNamespaceCall) {
8517
+ isSeriesFunctionArg = false;
8518
+ } else {
8519
+ isSeriesFunctionArg = true;
8520
+ }
8522
8521
  }
8523
8522
  }
8524
- const isArrayAccess = node.parent && node.parent.type === "MemberExpression" && node.parent.computed;
8525
- const isArrayIndexInNamespaceCall = node.parent && node.parent.type === "MemberExpression" && node.parent.computed && node.parent.property === node && node.parent.parent && node.parent.parent.type === "CallExpression" && node.parent.parent.callee && node.parent.parent.callee.type === "MemberExpression" && scopeManager.isContextBound(node.parent.parent.callee.object.name);
8523
+ const isNamespaceMember = node.parent && node.parent.type === "MemberExpression" && node.parent.object === node && scopeManager.isContextBound(node.name);
8524
+ const isParamCall = node.parent && node.parent.type === "CallExpression" && node.parent.callee && node.parent.callee.type === "MemberExpression" && node.parent.callee.property.name === "param";
8525
+ node.parent && node.parent.type === "AssignmentExpression" && node.parent.left === node;
8526
8526
  const isFunctionCall = node.parent && node.parent.type === "CallExpression" && node.parent.callee === node;
8527
8527
  const hasArrayAccess = node.parent && node.parent.type === "MemberExpression" && node.parent.computed && node.parent.object === node;
8528
+ const isArrayIndexInNamespaceCall = node.parent && node.parent.type === "MemberExpression" && node.parent.computed && node.parent.property === node && node.parent.parent && node.parent.parent.type === "CallExpression" && node.parent.parent.callee && node.parent.parent.callee.type === "MemberExpression" && scopeManager.isContextBound(node.parent.parent.callee.object.name);
8528
8529
  if (isNamespaceMember || isParamCall || isSeriesFunctionArg || isArrayIndexInNamespaceCall || isFunctionCall) {
8529
8530
  if (isFunctionCall) {
8530
8531
  return;
@@ -8532,13 +8533,23 @@ ${code}
8532
8533
  if (scopeManager.isLocalSeriesVar(node.name)) {
8533
8534
  return;
8534
8535
  }
8536
+ if (scopeManager.isContextBound(node.name) && !scopeManager.isRootParam(node.name)) {
8537
+ return;
8538
+ }
8535
8539
  const [scopedName2, kind2] = scopeManager.getVariable(node.name);
8536
8540
  const memberExpr2 = ASTFactory.createContextVariableReference(kind2, scopedName2);
8537
8541
  Object.assign(node, memberExpr2);
8538
8542
  return;
8539
8543
  }
8544
+ const isContextBoundVar = scopeManager.isContextBound(node.name) && !scopeManager.isRootParam(node.name);
8545
+ if (isContextBoundVar) {
8546
+ const isFunctionArg = node.parent && node.parent.type === "CallExpression" && node.parent.arguments.includes(node);
8547
+ if (!isFunctionArg) {
8548
+ return;
8549
+ }
8550
+ }
8540
8551
  if (scopeManager.isLocalSeriesVar(node.name)) {
8541
- if (!hasArrayAccess && !isArrayAccess) {
8552
+ if (!hasArrayAccess) {
8542
8553
  const memberExpr2 = ASTFactory.createIdentifier(node.name);
8543
8554
  const accessExpr = ASTFactory.createGetCall(memberExpr2, 0);
8544
8555
  Object.assign(node, accessExpr);
@@ -8546,8 +8557,16 @@ ${code}
8546
8557
  return;
8547
8558
  }
8548
8559
  const [scopedName, kind] = scopeManager.getVariable(node.name);
8549
- const memberExpr = ASTFactory.createContextVariableReference(kind, scopedName);
8550
- if (!hasArrayAccess && !isArrayAccess) {
8560
+ let memberExpr;
8561
+ if (isContextBoundVar) {
8562
+ memberExpr = ASTFactory.createIdentifier(node.name);
8563
+ } else {
8564
+ if (scopedName === node.name && !scopeManager.isContextBound(node.name)) {
8565
+ return;
8566
+ }
8567
+ memberExpr = ASTFactory.createContextVariableReference(kind, scopedName);
8568
+ }
8569
+ if (!hasArrayAccess) {
8551
8570
  const accessExpr = ASTFactory.createGetCall(memberExpr, 0);
8552
8571
  Object.assign(node, accessExpr);
8553
8572
  } else {
@@ -8559,7 +8578,6 @@ ${code}
8559
8578
  if (memberNode.object && memberNode.object.type === "Identifier" && memberNode.object.name === "Math") {
8560
8579
  return;
8561
8580
  }
8562
- const KNOWN_NAMESPACES = ["ta", "math", "request", "array", "input"];
8563
8581
  const isDirectNamespaceMemberAccess = memberNode.object && memberNode.object.type === "Identifier" && KNOWN_NAMESPACES.includes(memberNode.object.name) && scopeManager.isContextBound(memberNode.object.name) && !memberNode.computed;
8564
8582
  if (isDirectNamespaceMemberAccess) {
8565
8583
  const isAlreadyBeingCalled = memberNode.parent && memberNode.parent.type === "CallExpression" && memberNode.parent.callee === memberNode;
@@ -8603,6 +8621,9 @@ ${code}
8603
8621
  if (memberNode.start) getCall.start = memberNode.start;
8604
8622
  if (memberNode.end) getCall.end = memberNode.end;
8605
8623
  Object.assign(memberNode, getCall);
8624
+ delete memberNode.object;
8625
+ delete memberNode.property;
8626
+ delete memberNode.computed;
8606
8627
  }
8607
8628
  }
8608
8629
  function transformIdentifierForParam(node, scopeManager) {
@@ -8755,16 +8776,31 @@ ${code}
8755
8776
  }
8756
8777
  },
8757
8778
  ConditionalExpression(node2, state, c) {
8779
+ const newState = { ...state, parent: node2 };
8758
8780
  if (node2.test) {
8759
- c(node2.test, { parent: node2, inNamespaceCall: state.inNamespaceCall });
8781
+ c(node2.test, newState);
8760
8782
  }
8761
8783
  if (node2.consequent) {
8762
- c(node2.consequent, { parent: node2, inNamespaceCall: state.inNamespaceCall });
8784
+ c(node2.consequent, newState);
8763
8785
  }
8764
8786
  if (node2.alternate) {
8765
- c(node2.alternate, { parent: node2, inNamespaceCall: state.inNamespaceCall });
8787
+ c(node2.alternate, newState);
8766
8788
  }
8767
8789
  },
8790
+ BinaryExpression(node2, state, c) {
8791
+ const newState = { ...state, parent: node2 };
8792
+ c(node2.left, newState);
8793
+ c(node2.right, newState);
8794
+ },
8795
+ LogicalExpression(node2, state, c) {
8796
+ const newState = { ...state, parent: node2 };
8797
+ c(node2.left, newState);
8798
+ c(node2.right, newState);
8799
+ },
8800
+ UnaryExpression(node2, state, c) {
8801
+ const newState = { ...state, parent: node2 };
8802
+ c(node2.argument, newState);
8803
+ },
8768
8804
  CallExpression(node2, state, c) {
8769
8805
  const isNamespaceCall = node2.callee && node2.callee.type === "MemberExpression" && node2.callee.object && node2.callee.object.type === "Identifier" && scopeManager.isContextBound(node2.callee.object.name);
8770
8806
  transformCallExpression(node2, scopeManager);
@@ -8853,6 +8889,20 @@ ${code}
8853
8889
  if (arg.type === "ObjectExpression") {
8854
8890
  arg.properties = arg.properties.map((prop) => {
8855
8891
  if (prop.value.name) {
8892
+ if (scopeManager.isContextBound(prop.value.name) && !scopeManager.isRootParam(prop.value.name)) {
8893
+ return {
8894
+ type: "Property",
8895
+ key: {
8896
+ type: "Identifier",
8897
+ name: prop.key.name
8898
+ },
8899
+ value: ASTFactory.createIdentifier(prop.value.name),
8900
+ kind: "init",
8901
+ method: false,
8902
+ shorthand: false,
8903
+ computed: false
8904
+ };
8905
+ }
8856
8906
  const [scopedName, kind] = scopeManager.getVariable(prop.value.name);
8857
8907
  return {
8858
8908
  type: "Property",
@@ -8921,6 +8971,12 @@ ${code}
8921
8971
  if (node._transformed) {
8922
8972
  return;
8923
8973
  }
8974
+ if (node.callee && node.callee.type === "Identifier" && KNOWN_NAMESPACES.includes(node.callee.name) && scopeManager.isContextBound(node.callee.name)) {
8975
+ node.callee = ASTFactory.createMemberExpression(
8976
+ node.callee,
8977
+ ASTFactory.createIdentifier("any")
8978
+ );
8979
+ }
8924
8980
  const isNamespaceCall = node.callee && node.callee.type === "MemberExpression" && node.callee.object && node.callee.object.type === "Identifier" && (scopeManager.isContextBound(node.callee.object.name) || node.callee.object.name === "math" || node.callee.object.name === "ta");
8925
8981
  if (isNamespaceCall) {
8926
8982
  if (node.callee.object.name === CONTEXT_NAME && ["get", "init", "param"].includes(node.callee.property.name)) {
@@ -8957,6 +9013,11 @@ ${code}
8957
9013
  });
8958
9014
  node._transformed = true;
8959
9015
  }
9016
+ if (!isNamespaceCall && node.callee && node.callee.type === "MemberExpression") {
9017
+ if (node.callee.object.type === "Identifier") {
9018
+ transformIdentifier(node.callee.object, scopeManager);
9019
+ }
9020
+ }
8960
9021
  node.arguments.forEach((arg) => {
8961
9022
  recursive(
8962
9023
  arg,
@@ -8978,6 +9039,20 @@ ${code}
8978
9039
  }
8979
9040
  }
8980
9041
  },
9042
+ BinaryExpression(node2, state, c) {
9043
+ const newState = { ...state, parent: node2 };
9044
+ c(node2.left, newState);
9045
+ c(node2.right, newState);
9046
+ },
9047
+ LogicalExpression(node2, state, c) {
9048
+ const newState = { ...state, parent: node2 };
9049
+ c(node2.left, newState);
9050
+ c(node2.right, newState);
9051
+ },
9052
+ UnaryExpression(node2, state, c) {
9053
+ const newState = { ...state, parent: node2 };
9054
+ c(node2.argument, newState);
9055
+ },
8981
9056
  CallExpression(node2, state, c) {
8982
9057
  if (!node2._transformed) {
8983
9058
  transformCallExpression(node2, scopeManager);
@@ -9119,7 +9194,8 @@ ${code}
9119
9194
  }
9120
9195
  const newName = scopeManager.addVariable(decl.id.name, varNode.kind);
9121
9196
  const kind = varNode.kind;
9122
- if (decl.init && !isArrowFunction) {
9197
+ const isArrayPatternVar = scopeManager.isArrayPatternElement(decl.id.name);
9198
+ if (decl.init && !isArrowFunction && !isArrayPatternVar) {
9123
9199
  if (decl.init.type === "CallExpression" && decl.init.callee.type === "MemberExpression" && decl.init.callee.object && decl.init.callee.object.type === "Identifier" && scopeManager.isContextBound(decl.init.callee.object.name)) {
9124
9200
  transformCallExpression(decl.init, scopeManager);
9125
9201
  } else {
@@ -9180,7 +9256,6 @@ ${code}
9180
9256
  }
9181
9257
  }
9182
9258
  const targetVarRef = ASTFactory.createContextVariableReference(kind, newName);
9183
- const isArrayPatternVar = scopeManager.isArrayPatternElement(decl.id.name);
9184
9259
  const isArrayInit = !isArrayPatternVar && decl.init && decl.init.type === "MemberExpression" && decl.init.computed && decl.init.property && (decl.init.property.type === "Literal" || decl.init.property.type === "MemberExpression");
9185
9260
  if (decl.init?.property?.type === "MemberExpression") {
9186
9261
  if (!decl.init.property._indexTransformed) {
@@ -9193,10 +9268,7 @@ ${code}
9193
9268
  if (isArrowFunction || isArrayPatternVar) {
9194
9269
  rightSide = decl.init;
9195
9270
  } else if (kind === "var") {
9196
- rightSide = ASTFactory.createInitVarCall(
9197
- targetVarRef,
9198
- decl.init
9199
- );
9271
+ rightSide = ASTFactory.createInitVarCall(targetVarRef, decl.init);
9200
9272
  } else {
9201
9273
  rightSide = ASTFactory.createInitCall(
9202
9274
  targetVarRef,
@@ -9209,7 +9281,9 @@ ${code}
9209
9281
  }
9210
9282
  const assignmentExpr = ASTFactory.createExpressionStatement(ASTFactory.createAssignmentExpression(targetVarRef, rightSide));
9211
9283
  if (isArrayPatternVar) {
9212
- const tempVarRef = assignmentExpr.expression.right.object;
9284
+ const tempVarName = decl.init.object.name;
9285
+ const [scopedTempName, tempKind] = scopeManager.getVariable(tempVarName);
9286
+ const tempVarRef = ASTFactory.createContextVariableReference(tempKind, scopedTempName);
9213
9287
  const arrayIndex = decl.init.property.value;
9214
9288
  const getCall = ASTFactory.createGetCall(tempVarRef, 0);
9215
9289
  const arrayAccess = {
@@ -9406,6 +9480,9 @@ ${code}
9406
9480
  } else if (node.argument.type === "ObjectExpression") {
9407
9481
  node.argument.properties = node.argument.properties.map((prop) => {
9408
9482
  if (prop.shorthand) {
9483
+ if (scopeManager.isContextBound(prop.value.name)) {
9484
+ return prop;
9485
+ }
9409
9486
  const [scopedName, kind] = scopeManager.getVariable(prop.value.name);
9410
9487
  return {
9411
9488
  type: "Property",
@@ -9418,9 +9495,7 @@ ${code}
9418
9495
  };
9419
9496
  }
9420
9497
  if (prop.value && prop.value.type === "Identifier") {
9421
- if (scopeManager.isContextBound(prop.value.name) && !scopeManager.isRootParam(prop.value.name)) {
9422
- prop.value = ASTFactory.createGetCall(prop.value, 0);
9423
- } else if (!scopeManager.isContextBound(prop.value.name)) {
9498
+ if (scopeManager.isContextBound(prop.value.name) && !scopeManager.isRootParam(prop.value.name)) ; else if (!scopeManager.isContextBound(prop.value.name)) {
9424
9499
  const [scopedName, kind] = scopeManager.getVariable(prop.value.name);
9425
9500
  prop.value = ASTFactory.createContextVariableReference(kind, scopedName);
9426
9501
  }
@@ -9605,72 +9680,207 @@ ${code}
9605
9680
  return _wraperFunction(this);
9606
9681
  }
9607
9682
 
9608
- class PineArrayObject {
9609
- constructor(array) {
9610
- this.array = array;
9611
- }
9612
- toString() {
9613
- return "PineArrayObject:" + this.array.toString();
9614
- }
9615
- }
9616
-
9617
9683
  function abs$1(context) {
9618
9684
  return (id) => {
9619
- return new PineArrayObject(id.array.map((val) => Math.abs(val)));
9685
+ return new PineArrayObject(
9686
+ id.array.map((val) => Math.abs(val)),
9687
+ id.type,
9688
+ context
9689
+ );
9620
9690
  };
9621
9691
  }
9622
9692
 
9623
- function avg$1(context) {
9693
+ function avg$2(context) {
9624
9694
  return (id) => {
9625
- return context.array.sum(id) / id.array.length;
9695
+ let mean = 0;
9696
+ let count = 0;
9697
+ for (const item of id.array) {
9698
+ const val = Number(item);
9699
+ if (!isNaN(val)) {
9700
+ count++;
9701
+ mean += (val - mean) / count;
9702
+ }
9703
+ }
9704
+ if (count === 0) return NaN;
9705
+ return context.precision(mean);
9626
9706
  };
9627
9707
  }
9628
9708
 
9629
- function clear(context) {
9709
+ function binary_search(context) {
9710
+ return (id, value) => {
9711
+ const array = id.array;
9712
+ let low = 0;
9713
+ let high = array.length - 1;
9714
+ while (low <= high) {
9715
+ const mid = Math.floor((low + high) / 2);
9716
+ const midVal = array[mid];
9717
+ if (midVal === value) {
9718
+ return mid;
9719
+ }
9720
+ if (midVal < value) {
9721
+ low = mid + 1;
9722
+ } else {
9723
+ high = mid - 1;
9724
+ }
9725
+ }
9726
+ return -1;
9727
+ };
9728
+ }
9729
+
9730
+ function binary_search_leftmost(context) {
9731
+ return (id, value) => {
9732
+ const array = id.array;
9733
+ let low = 0;
9734
+ let high = array.length;
9735
+ while (low < high) {
9736
+ const mid = Math.floor((low + high) / 2);
9737
+ if (array[mid] < value) {
9738
+ low = mid + 1;
9739
+ } else {
9740
+ high = mid;
9741
+ }
9742
+ }
9743
+ if (low < array.length && array[low] === value) {
9744
+ return low;
9745
+ }
9746
+ return low - 1;
9747
+ };
9748
+ }
9749
+
9750
+ function binary_search_rightmost(context) {
9751
+ return (id, value) => {
9752
+ const array = id.array;
9753
+ let low = 0;
9754
+ let high = array.length;
9755
+ while (low < high) {
9756
+ const mid = Math.floor((low + high) / 2);
9757
+ if (array[mid] <= value) {
9758
+ low = mid + 1;
9759
+ } else {
9760
+ high = mid;
9761
+ }
9762
+ }
9763
+ if (low > 0 && array[low - 1] === value) {
9764
+ return low - 1;
9765
+ }
9766
+ return low;
9767
+ };
9768
+ }
9769
+
9770
+ function clear$1(context) {
9630
9771
  return (id) => {
9631
9772
  id.array.length = 0;
9632
9773
  };
9633
9774
  }
9634
9775
 
9635
- function concat(context) {
9776
+ function concat$1(context) {
9636
9777
  return (id, other) => {
9637
9778
  id.array.push(...other.array);
9638
9779
  return id;
9639
9780
  };
9640
9781
  }
9641
9782
 
9642
- function copy(context) {
9783
+ function copy$2(context) {
9643
9784
  return (id) => {
9644
- return new PineArrayObject([...id.array]);
9785
+ return new PineArrayObject([...id.array], id.type, context);
9645
9786
  };
9646
9787
  }
9647
9788
 
9648
9789
  function covariance(context) {
9649
9790
  return (arr1, arr2, biased = true) => {
9650
- if (arr1.array.length !== arr2.array.length || arr1.array.length < 2) return NaN;
9651
- const divisor = biased ? arr1.array.length : arr1.array.length - 1;
9652
- const mean1 = context.array.avg(arr1);
9653
- const mean2 = context.array.avg(arr2);
9654
- let sum = 0;
9655
- for (let i = 0; i < arr1.array.length; i++) {
9656
- sum += (arr1.array[i] - mean1) * (arr2.array[i] - mean2);
9791
+ const a1 = arr1.array;
9792
+ const a2 = arr2.array;
9793
+ if (a1.length !== a2.length) return NaN;
9794
+ let sum1 = 0;
9795
+ let sum2 = 0;
9796
+ let count = 0;
9797
+ const validIndices = [];
9798
+ for (let i = 0; i < a1.length; i++) {
9799
+ const v1 = Number(a1[i]);
9800
+ const v2 = Number(a2[i]);
9801
+ if (!isNaN(v1) && v1 !== null && v1 !== void 0 && !isNaN(v2) && v2 !== null && v2 !== void 0) {
9802
+ sum1 += v1;
9803
+ sum2 += v2;
9804
+ count++;
9805
+ validIndices.push(i);
9806
+ }
9657
9807
  }
9658
- return sum / divisor;
9808
+ if (count === 0) return NaN;
9809
+ const mean1 = sum1 / count;
9810
+ const mean2 = sum2 / count;
9811
+ let sumProd = 0;
9812
+ for (const i of validIndices) {
9813
+ const v1 = Number(a1[i]);
9814
+ const v2 = Number(a2[i]);
9815
+ sumProd += (v1 - mean1) * (v2 - mean2);
9816
+ }
9817
+ const divisor = biased ? count : count - 1;
9818
+ if (divisor <= 0) return NaN;
9819
+ return context.precision(sumProd / divisor);
9659
9820
  };
9660
9821
  }
9661
9822
 
9662
9823
  function every(context) {
9663
- return (id, callback) => {
9664
- return id.array.every(callback);
9824
+ return (id) => {
9825
+ return id.array.every((value) => !isNaN(value) && value);
9665
9826
  };
9666
9827
  }
9667
9828
 
9668
- function fill(context) {
9829
+ function inferArrayType(values) {
9830
+ if (values.every((value) => typeof value === "number")) {
9831
+ if (values.every((value) => (value | 0) === value)) {
9832
+ return PineArrayType.int;
9833
+ } else {
9834
+ return PineArrayType.float;
9835
+ }
9836
+ } else if (values.every((value) => typeof value === "string")) {
9837
+ return PineArrayType.string;
9838
+ } else if (values.every((value) => typeof value === "boolean")) {
9839
+ return PineArrayType.bool;
9840
+ } else {
9841
+ throw new Error("Cannot infer type from values");
9842
+ }
9843
+ }
9844
+ function inferValueType(value) {
9845
+ if (typeof value === "number") {
9846
+ if ((value | 0) === value) {
9847
+ return PineArrayType.int;
9848
+ } else {
9849
+ return PineArrayType.float;
9850
+ }
9851
+ } else if (typeof value === "string") {
9852
+ return PineArrayType.string;
9853
+ } else if (typeof value === "boolean") {
9854
+ return PineArrayType.bool;
9855
+ } else {
9856
+ throw new Error("Cannot infer type from value");
9857
+ }
9858
+ }
9859
+ function isValueOfType(value, type) {
9860
+ switch (type) {
9861
+ case PineArrayType.int:
9862
+ return typeof value === "number" && (value | 0) === value || isNaN(value);
9863
+ case PineArrayType.float:
9864
+ return typeof value === "number" || isNaN(value);
9865
+ case PineArrayType.string:
9866
+ return typeof value === "string";
9867
+ case PineArrayType.bool:
9868
+ return typeof value === "boolean";
9869
+ }
9870
+ return false;
9871
+ }
9872
+
9873
+ function fill$1(context) {
9669
9874
  return (id, value, start = 0, end) => {
9670
9875
  const length = id.array.length;
9671
9876
  const adjustedEnd = end !== void 0 ? Math.min(end, length) : length;
9672
9877
  for (let i = start; i < adjustedEnd; i++) {
9673
- id.array[i] = value;
9878
+ if (!isValueOfType(value, id.type)) {
9879
+ throw new Error(
9880
+ `Cannot call 'array.fill' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
9881
+ );
9882
+ }
9883
+ id.array[i] = context.precision(value);
9674
9884
  }
9675
9885
  };
9676
9886
  }
@@ -9681,13 +9891,7 @@ ${code}
9681
9891
  };
9682
9892
  }
9683
9893
 
9684
- function from(context) {
9685
- return (...values) => {
9686
- return new PineArrayObject([...values]);
9687
- };
9688
- }
9689
-
9690
- function get(context) {
9894
+ function get$2(context) {
9691
9895
  return (id, index) => {
9692
9896
  return id.array[index];
9693
9897
  };
@@ -9707,6 +9911,11 @@ ${code}
9707
9911
 
9708
9912
  function insert(context) {
9709
9913
  return (id, index, value) => {
9914
+ if (!isValueOfType(value, id.type)) {
9915
+ throw new Error(
9916
+ `Cannot call 'array.insert' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
9917
+ );
9918
+ }
9710
9919
  id.array.splice(index, 0, value);
9711
9920
  };
9712
9921
  }
@@ -9729,84 +9938,136 @@ ${code}
9729
9938
  };
9730
9939
  }
9731
9940
 
9732
- function max$1(context) {
9941
+ function max$2(context) {
9733
9942
  return (id, nth = 0) => {
9734
9943
  const sorted = [...id.array].sort((a, b) => b - a);
9735
9944
  return sorted[nth] ?? context.NA;
9736
9945
  };
9737
9946
  }
9738
9947
 
9739
- function min$1(context) {
9740
- return (id, nth = 0) => {
9741
- const sorted = [...id.array].sort((a, b) => a - b);
9742
- return sorted[nth] ?? context.NA;
9743
- };
9744
- }
9745
-
9746
- function new_fn(context) {
9747
- return (size, initial_value) => {
9748
- return new PineArrayObject(Array(size).fill(initial_value));
9948
+ function median$2(context) {
9949
+ return (id) => {
9950
+ if (id.array.length === 0) return NaN;
9951
+ const sorted = [...id.array].sort((a, b) => {
9952
+ if (typeof a === "number" && typeof b === "number") {
9953
+ return a - b;
9954
+ }
9955
+ return 0;
9956
+ });
9957
+ const mid = Math.floor(sorted.length / 2);
9958
+ if (sorted.length % 2 !== 0) {
9959
+ return sorted[mid];
9960
+ }
9961
+ return (sorted[mid - 1] + sorted[mid]) / 2;
9749
9962
  };
9750
9963
  }
9751
9964
 
9752
- function new_bool(context) {
9753
- return (size, initial_value = false) => {
9754
- return new PineArrayObject(Array(size).fill(initial_value));
9965
+ function min$2(context) {
9966
+ return (id, nth = 0) => {
9967
+ const sorted = [...id.array].sort((a, b) => a - b);
9968
+ return sorted[nth] ?? context.NA;
9755
9969
  };
9756
9970
  }
9757
9971
 
9758
- function new_float(context) {
9759
- return (size, initial_value = NaN) => {
9760
- return new PineArrayObject(Array(size).fill(initial_value));
9972
+ function mode$2(context) {
9973
+ return (id) => {
9974
+ if (id.array.length === 0) return NaN;
9975
+ const counts = /* @__PURE__ */ new Map();
9976
+ let maxFreq = 0;
9977
+ for (const val of id.array) {
9978
+ const count = (counts.get(val) || 0) + 1;
9979
+ counts.set(val, count);
9980
+ if (count > maxFreq) {
9981
+ maxFreq = count;
9982
+ }
9983
+ }
9984
+ const modes = [];
9985
+ for (const [val, count] of counts) {
9986
+ if (count === maxFreq) {
9987
+ modes.push(val);
9988
+ }
9989
+ }
9990
+ modes.sort((a, b) => {
9991
+ if (typeof a === "number" && typeof b === "number") {
9992
+ return a - b;
9993
+ }
9994
+ if (typeof a === "string" && typeof b === "string") {
9995
+ return a < b ? -1 : a > b ? 1 : 0;
9996
+ }
9997
+ return 0;
9998
+ });
9999
+ return modes[0];
9761
10000
  };
9762
10001
  }
9763
10002
 
9764
- function new_int(context) {
9765
- return (size, initial_value = 0) => {
9766
- return new PineArrayObject(Array(size).fill(Math.round(initial_value)));
10003
+ function percentile_linear_interpolation$1(context) {
10004
+ return (id, percentage) => {
10005
+ const array = id.array;
10006
+ if (array.length === 0) return NaN;
10007
+ const validValues = [];
10008
+ for (const item of array) {
10009
+ const val = Number(item);
10010
+ if (isNaN(val) || val === null || val === void 0) {
10011
+ return NaN;
10012
+ }
10013
+ validValues.push(val);
10014
+ }
10015
+ validValues.sort((a, b) => a - b);
10016
+ if (percentage < 0) percentage = 0;
10017
+ if (percentage > 100) percentage = 100;
10018
+ const k = percentage / 100 * validValues.length - 0.5;
10019
+ if (k <= 0) return context.precision(validValues[0]);
10020
+ if (k >= validValues.length - 1) return context.precision(validValues[validValues.length - 1]);
10021
+ const i = Math.floor(k);
10022
+ const f = k - i;
10023
+ return context.precision(validValues[i] * (1 - f) + validValues[i + 1] * f);
9767
10024
  };
9768
10025
  }
9769
10026
 
9770
- function new_string(context) {
9771
- return (size, initial_value = "") => {
9772
- return new PineArrayObject(Array(size).fill(initial_value));
10027
+ function percentile_nearest_rank$1(context) {
10028
+ return (id, percentage) => {
10029
+ const array = id.array;
10030
+ if (array.length === 0) return NaN;
10031
+ const validValues = [];
10032
+ for (const item of array) {
10033
+ const val = Number(item);
10034
+ if (!isNaN(val) && val !== null && val !== void 0) {
10035
+ validValues.push(val);
10036
+ }
10037
+ }
10038
+ if (validValues.length === 0) return NaN;
10039
+ validValues.sort((a, b) => a - b);
10040
+ if (percentage < 0) percentage = 0;
10041
+ if (percentage > 100) percentage = 100;
10042
+ const totalCount = array.length;
10043
+ const rank = Math.ceil(percentage / 100 * totalCount);
10044
+ if (rank <= 0) {
10045
+ return validValues[0];
10046
+ }
10047
+ if (rank > validValues.length) return NaN;
10048
+ return validValues[rank - 1];
9773
10049
  };
9774
10050
  }
9775
10051
 
9776
- class Series {
9777
- constructor(data, offset = 0) {
9778
- this.data = data;
9779
- this.offset = offset;
9780
- }
9781
- get(index) {
9782
- const realIndex = this.data.length - 1 - (this.offset + index);
9783
- if (realIndex < 0 || realIndex >= this.data.length) {
9784
- return NaN;
9785
- }
9786
- return this.data[realIndex];
9787
- }
9788
- set(index, value) {
9789
- const realIndex = this.data.length - 1 - (this.offset + index);
9790
- if (realIndex >= 0 && realIndex < this.data.length) {
9791
- this.data[realIndex] = value;
10052
+ function percentrank$1(context) {
10053
+ return (id, index) => {
10054
+ if (id.array.length === 0) return NaN;
10055
+ const idx = Math.floor(index);
10056
+ if (idx < 0 || idx >= id.array.length) return NaN;
10057
+ const value = Number(id.array[idx]);
10058
+ if (isNaN(value) || value === null || value === void 0) return NaN;
10059
+ let lessThan = 0;
10060
+ for (const item of id.array) {
10061
+ const val = Number(item);
10062
+ if (!isNaN(val) && val !== null && val !== void 0) {
10063
+ if (val < value) {
10064
+ lessThan++;
10065
+ }
10066
+ }
9792
10067
  }
9793
- }
9794
- get length() {
9795
- return this.data.length;
9796
- }
9797
- toArray() {
9798
- return this.data;
9799
- }
9800
- static from(source) {
9801
- if (source instanceof Series) return source;
9802
- if (Array.isArray(source)) return new Series(source);
9803
- return new Series([source]);
9804
- }
9805
- }
9806
-
9807
- function param$4(context) {
9808
- return (source, index = 0) => {
9809
- return Series.from(source).get(index);
10068
+ const divisor = id.array.length - 1;
10069
+ if (divisor <= 0) return NaN;
10070
+ return lessThan / divisor * 100;
9810
10071
  };
9811
10072
  }
9812
10073
 
@@ -9818,17 +10079,22 @@ ${code}
9818
10079
 
9819
10080
  function push(context) {
9820
10081
  return (id, value) => {
9821
- id.array.push(value);
10082
+ if (!isValueOfType(value, id.type)) {
10083
+ throw new Error(
10084
+ `Cannot call 'array.push' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
10085
+ );
10086
+ }
10087
+ id.array.push(context.precision(value));
9822
10088
  };
9823
10089
  }
9824
10090
 
9825
10091
  function range$1(context) {
9826
10092
  return (id) => {
9827
- return context.array.max(id) - context.array.min(id);
10093
+ return context.precision(context.pine.array.max(id) - context.pine.array.min(id));
9828
10094
  };
9829
10095
  }
9830
10096
 
9831
- function remove(context) {
10097
+ function remove$1(context) {
9832
10098
  return (id, index) => {
9833
10099
  if (index >= 0 && index < id.array.length) {
9834
10100
  return id.array.splice(index, 1)[0];
@@ -9837,15 +10103,20 @@ ${code}
9837
10103
  };
9838
10104
  }
9839
10105
 
9840
- function reverse(context) {
10106
+ function reverse$1(context) {
9841
10107
  return (id) => {
9842
10108
  id.array.reverse();
9843
10109
  };
9844
10110
  }
9845
10111
 
9846
- function set(context) {
10112
+ function set$1(context) {
9847
10113
  return (id, index, value) => {
9848
- id.array[index] = value;
10114
+ if (!isValueOfType(value, id.type)) {
10115
+ throw new Error(
10116
+ `Cannot call 'array.set' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
10117
+ );
10118
+ }
10119
+ id.array[index] = context.precision(value);
9849
10120
  };
9850
10121
  }
9851
10122
 
@@ -9855,7 +10126,7 @@ ${code}
9855
10126
  };
9856
10127
  }
9857
10128
 
9858
- function size(context) {
10129
+ function size$1(context) {
9859
10130
  return (id) => {
9860
10131
  return id.array.length;
9861
10132
  };
@@ -9863,172 +10134,2013 @@ ${code}
9863
10134
 
9864
10135
  function slice(context) {
9865
10136
  return (id, start, end) => {
9866
- const adjustedEnd = end !== void 0 ? end + 1 : void 0;
9867
- return new PineArrayObject(id.array.slice(start, adjustedEnd));
10137
+ const adjustedEnd = end !== void 0 ? end : void 0;
10138
+ return new PineArrayObject(id.array.slice(start, adjustedEnd), id.type, context);
10139
+ };
10140
+ }
10141
+
10142
+ function some(context) {
10143
+ return (id) => {
10144
+ return id.array.some((value) => !isNaN(value) && value);
10145
+ };
10146
+ }
10147
+
10148
+ var order = /* @__PURE__ */ ((order2) => {
10149
+ order2[order2["ascending"] = 1] = "ascending";
10150
+ order2[order2["descending"] = 0] = "descending";
10151
+ return order2;
10152
+ })(order || {});
10153
+ var currency = /* @__PURE__ */ ((currency2) => {
10154
+ currency2["AED"] = "AED";
10155
+ currency2["ARS"] = "ARS";
10156
+ currency2["AUD"] = "AUD";
10157
+ currency2["BDT"] = "BDT";
10158
+ currency2["BHD"] = "BHD";
10159
+ currency2["BRL"] = "BRL";
10160
+ currency2["BTC"] = "BTC";
10161
+ currency2["CAD"] = "CAD";
10162
+ currency2["CHF"] = "CHF";
10163
+ currency2["CLP"] = "CLP";
10164
+ currency2["CNY"] = "CNY";
10165
+ currency2["COP"] = "COP";
10166
+ currency2["CZK"] = "CZK";
10167
+ currency2["DKK"] = "DKK";
10168
+ currency2["EGP"] = "EGP";
10169
+ currency2["ETH"] = "ETH";
10170
+ currency2["EUR"] = "EUR";
10171
+ currency2["GBP"] = "GBP";
10172
+ currency2["HKD"] = "HKD";
10173
+ currency2["HUF"] = "HUF";
10174
+ currency2["IDR"] = "IDR";
10175
+ currency2["ILS"] = "ILS";
10176
+ currency2["INR"] = "INR";
10177
+ currency2["ISK"] = "ISK";
10178
+ currency2["JPY"] = "JPY";
10179
+ currency2["KES"] = "KES";
10180
+ currency2["KRW"] = "KRW";
10181
+ currency2["KWD"] = "KWD";
10182
+ currency2["LKR"] = "LKR";
10183
+ currency2["MAD"] = "MAD";
10184
+ currency2["MXN"] = "MXN";
10185
+ currency2["MYR"] = "MYR";
10186
+ currency2["NGN"] = "NGN";
10187
+ currency2["NOK"] = "NOK";
10188
+ currency2["NONE"] = "NONE";
10189
+ currency2["NZD"] = "NZD";
10190
+ currency2["PEN"] = "PEN";
10191
+ currency2["PHP"] = "PHP";
10192
+ currency2["PKR"] = "PKR";
10193
+ currency2["PLN"] = "PLN";
10194
+ currency2["QAR"] = "QAR";
10195
+ currency2["RON"] = "RON";
10196
+ currency2["RSD"] = "RSD";
10197
+ currency2["RUB"] = "RUB";
10198
+ currency2["SAR"] = "SAR";
10199
+ currency2["SEK"] = "SEK";
10200
+ currency2["SGD"] = "SGD";
10201
+ currency2["THB"] = "THB";
10202
+ currency2["TND"] = "TND";
10203
+ currency2["TRY"] = "TRY";
10204
+ currency2["TWD"] = "TWD";
10205
+ currency2["USD"] = "USD";
10206
+ currency2["USDT"] = "USDT";
10207
+ currency2["VES"] = "VES";
10208
+ currency2["VND"] = "VND";
10209
+ currency2["ZAR"] = "ZAR";
10210
+ return currency2;
10211
+ })(currency || {});
10212
+ var dayofweek = /* @__PURE__ */ ((dayofweek2) => {
10213
+ dayofweek2[dayofweek2["sunday"] = 1] = "sunday";
10214
+ dayofweek2[dayofweek2["monday"] = 2] = "monday";
10215
+ dayofweek2[dayofweek2["tuesday"] = 3] = "tuesday";
10216
+ dayofweek2[dayofweek2["wednesday"] = 4] = "wednesday";
10217
+ dayofweek2[dayofweek2["thursday"] = 5] = "thursday";
10218
+ dayofweek2[dayofweek2["friday"] = 6] = "friday";
10219
+ dayofweek2[dayofweek2["saturday"] = 7] = "saturday";
10220
+ return dayofweek2;
10221
+ })(dayofweek || {});
10222
+ const types = {
10223
+ order,
10224
+ currency,
10225
+ dayofweek
10226
+ };
10227
+
10228
+ function sort$1(context) {
10229
+ return (id, _order = order.ascending) => {
10230
+ id.array.sort((a, b) => {
10231
+ let _a = isNaN(a) ? Infinity : a;
10232
+ let _b = isNaN(b) ? Infinity : b;
10233
+ return _order === order.ascending ? _a - _b : _b - _a;
10234
+ });
10235
+ };
10236
+ }
10237
+
10238
+ function sort_indices(context) {
10239
+ return (id, _order = order.ascending) => {
10240
+ const indices = id.array.map((_, index) => index);
10241
+ indices.sort((a, b) => {
10242
+ const valA = isNaN(id.array[a]) ? Infinity : id.array[a];
10243
+ const valB = isNaN(id.array[b]) ? Infinity : id.array[b];
10244
+ return _order === order.ascending ? valA - valB : valB - valA;
10245
+ });
10246
+ return new PineArrayObject(indices, PineArrayType.int, context);
10247
+ };
10248
+ }
10249
+
10250
+ function standardize(context) {
10251
+ return (id) => {
10252
+ const mean = context.array.avg(id);
10253
+ const stdev = context.array.stdev(id);
10254
+ if (isNaN(stdev)) {
10255
+ return new PineArrayObject(
10256
+ id.array.map(() => NaN),
10257
+ PineArrayType.int,
10258
+ context
10259
+ );
10260
+ }
10261
+ if (stdev === 0) {
10262
+ return new PineArrayObject(
10263
+ id.array.map(() => 1),
10264
+ PineArrayType.int,
10265
+ context
10266
+ );
10267
+ }
10268
+ return new PineArrayObject(
10269
+ id.array.map((x) => (x - mean) / stdev),
10270
+ PineArrayType.int,
10271
+ context
10272
+ );
10273
+ };
10274
+ }
10275
+
10276
+ function stdev$1(context) {
10277
+ return (id, biased = true) => {
10278
+ const array = id.array;
10279
+ const n_total = array.length;
10280
+ if (n_total === 0) return NaN;
10281
+ let sum = 0;
10282
+ let sumSq = 0;
10283
+ let count = 0;
10284
+ for (let i = 0; i < n_total; i++) {
10285
+ const val = Number(array[i]);
10286
+ if (!isNaN(val) && val !== null && val !== void 0) {
10287
+ sum += val;
10288
+ sumSq += val * val;
10289
+ count++;
10290
+ }
10291
+ }
10292
+ if (count === 0) return NaN;
10293
+ const mean = sum / count;
10294
+ const meanSq = sumSq / count;
10295
+ let variance = meanSq - mean * mean;
10296
+ if (variance < 0) return NaN;
10297
+ if (!biased && count > 1) {
10298
+ variance = variance * count / (count - 1);
10299
+ }
10300
+ if (!biased && count === 1) return 0;
10301
+ return context.precision(Math.sqrt(variance));
10302
+ };
10303
+ }
10304
+
10305
+ function sum$2(context) {
10306
+ return (id) => {
10307
+ return context.precision(
10308
+ id.array.reduce((a, b) => {
10309
+ const val = Number(b);
10310
+ return isNaN(val) ? a : a + val;
10311
+ }, 0)
10312
+ );
10313
+ };
10314
+ }
10315
+
10316
+ function unshift(context) {
10317
+ return (id, value) => {
10318
+ if (!isValueOfType(value, id.type)) {
10319
+ throw new Error(
10320
+ `Cannot call 'array.unshift' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
10321
+ );
10322
+ }
10323
+ id.array.unshift(context.precision(value));
10324
+ };
10325
+ }
10326
+
10327
+ function variance$1(context) {
10328
+ return (id, biased = true) => {
10329
+ let sum = 0;
10330
+ let count = 0;
10331
+ for (const item of id.array) {
10332
+ const val = Number(item);
10333
+ if (!isNaN(val) && val !== null && val !== void 0) {
10334
+ sum += val;
10335
+ count++;
10336
+ }
10337
+ }
10338
+ if (count === 0) return NaN;
10339
+ const mean = sum / count;
10340
+ let sumSqDiff = 0;
10341
+ for (const item of id.array) {
10342
+ const val = Number(item);
10343
+ if (!isNaN(val) && val !== null && val !== void 0) {
10344
+ sumSqDiff += (val - mean) * (val - mean);
10345
+ }
10346
+ }
10347
+ const divisor = biased ? count : count - 1;
10348
+ if (divisor <= 0) return NaN;
10349
+ return context.precision(sumSqDiff / divisor);
10350
+ };
10351
+ }
10352
+
10353
+ var __defProp$b = Object.defineProperty;
10354
+ var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10355
+ var __publicField$b = (obj, key, value) => __defNormalProp$b(obj, typeof key !== "symbol" ? key + "" : key, value);
10356
+ var PineArrayType = /* @__PURE__ */ ((PineArrayType2) => {
10357
+ PineArrayType2["any"] = "";
10358
+ PineArrayType2["box"] = "box";
10359
+ PineArrayType2["bool"] = "bool";
10360
+ PineArrayType2["color"] = "color";
10361
+ PineArrayType2["float"] = "float";
10362
+ PineArrayType2["int"] = "int";
10363
+ PineArrayType2["label"] = "label";
10364
+ PineArrayType2["line"] = "line";
10365
+ PineArrayType2["linefill"] = "linefill";
10366
+ PineArrayType2["string"] = "string";
10367
+ PineArrayType2["table"] = "table";
10368
+ return PineArrayType2;
10369
+ })(PineArrayType || {});
10370
+ class PineArrayObject {
10371
+ constructor(array, type, context) {
10372
+ this.array = array;
10373
+ this.type = type;
10374
+ this.context = context;
10375
+ __publicField$b(this, "_abs");
10376
+ __publicField$b(this, "_avg");
10377
+ __publicField$b(this, "_binary_search");
10378
+ __publicField$b(this, "_binary_search_leftmost");
10379
+ __publicField$b(this, "_binary_search_rightmost");
10380
+ __publicField$b(this, "_clear");
10381
+ __publicField$b(this, "_concat");
10382
+ __publicField$b(this, "_copy");
10383
+ __publicField$b(this, "_covariance");
10384
+ __publicField$b(this, "_every");
10385
+ __publicField$b(this, "_fill");
10386
+ __publicField$b(this, "_first");
10387
+ __publicField$b(this, "_get");
10388
+ __publicField$b(this, "_includes");
10389
+ __publicField$b(this, "_indexof");
10390
+ __publicField$b(this, "_insert");
10391
+ __publicField$b(this, "_join");
10392
+ __publicField$b(this, "_last");
10393
+ __publicField$b(this, "_lastindexof");
10394
+ __publicField$b(this, "_max");
10395
+ __publicField$b(this, "_median");
10396
+ __publicField$b(this, "_min");
10397
+ __publicField$b(this, "_mode");
10398
+ __publicField$b(this, "_percentile_linear_interpolation");
10399
+ __publicField$b(this, "_percentile_nearest_rank");
10400
+ __publicField$b(this, "_percentrank");
10401
+ __publicField$b(this, "_pop");
10402
+ __publicField$b(this, "_push");
10403
+ __publicField$b(this, "_range");
10404
+ __publicField$b(this, "_remove");
10405
+ __publicField$b(this, "_reverse");
10406
+ __publicField$b(this, "_set");
10407
+ __publicField$b(this, "_shift");
10408
+ __publicField$b(this, "_size");
10409
+ __publicField$b(this, "_slice");
10410
+ __publicField$b(this, "_some");
10411
+ __publicField$b(this, "_sort");
10412
+ __publicField$b(this, "_sort_indices");
10413
+ __publicField$b(this, "_standardize");
10414
+ __publicField$b(this, "_stdev");
10415
+ __publicField$b(this, "_sum");
10416
+ __publicField$b(this, "_unshift");
10417
+ __publicField$b(this, "_variance");
10418
+ this._abs = abs$1(this.context);
10419
+ this._avg = avg$2(this.context);
10420
+ this._binary_search = binary_search(this.context);
10421
+ this._binary_search_leftmost = binary_search_leftmost(this.context);
10422
+ this._binary_search_rightmost = binary_search_rightmost(this.context);
10423
+ this._clear = clear$1(this.context);
10424
+ this._concat = concat$1(this.context);
10425
+ this._copy = copy$2(this.context);
10426
+ this._covariance = covariance(this.context);
10427
+ this._every = every(this.context);
10428
+ this._fill = fill$1(this.context);
10429
+ this._first = first(this.context);
10430
+ this._get = get$2(this.context);
10431
+ this._includes = includes(this.context);
10432
+ this._indexof = indexof(this.context);
10433
+ this._insert = insert(this.context);
10434
+ this._join = join(this.context);
10435
+ this._last = last(this.context);
10436
+ this._lastindexof = lastindexof(this.context);
10437
+ this._max = max$2(this.context);
10438
+ this._median = median$2(this.context);
10439
+ this._min = min$2(this.context);
10440
+ this._mode = mode$2(this.context);
10441
+ this._percentile_linear_interpolation = percentile_linear_interpolation$1(this.context);
10442
+ this._percentile_nearest_rank = percentile_nearest_rank$1(this.context);
10443
+ this._percentrank = percentrank$1(this.context);
10444
+ this._pop = pop(this.context);
10445
+ this._push = push(this.context);
10446
+ this._range = range$1(this.context);
10447
+ this._remove = remove$1(this.context);
10448
+ this._reverse = reverse$1(this.context);
10449
+ this._set = set$1(this.context);
10450
+ this._shift = shift(this.context);
10451
+ this._size = size$1(this.context);
10452
+ this._slice = slice(this.context);
10453
+ this._some = some(this.context);
10454
+ this._sort = sort$1(this.context);
10455
+ this._sort_indices = sort_indices(this.context);
10456
+ this._standardize = standardize(this.context);
10457
+ this._stdev = stdev$1(this.context);
10458
+ this._sum = sum$2(this.context);
10459
+ this._unshift = unshift(this.context);
10460
+ this._variance = variance$1(this.context);
10461
+ }
10462
+ toString() {
10463
+ return "[" + this.array.toString().replace(/,/g, ", ") + "]";
10464
+ }
10465
+ abs(...args) {
10466
+ return this._abs(this, ...args);
10467
+ }
10468
+ avg(...args) {
10469
+ return this._avg(this, ...args);
10470
+ }
10471
+ binary_search(...args) {
10472
+ return this._binary_search(this, ...args);
10473
+ }
10474
+ binary_search_leftmost(...args) {
10475
+ return this._binary_search_leftmost(this, ...args);
10476
+ }
10477
+ binary_search_rightmost(...args) {
10478
+ return this._binary_search_rightmost(this, ...args);
10479
+ }
10480
+ clear(...args) {
10481
+ return this._clear(this, ...args);
10482
+ }
10483
+ concat(...args) {
10484
+ return this._concat(this, ...args);
10485
+ }
10486
+ copy(...args) {
10487
+ return this._copy(this, ...args);
10488
+ }
10489
+ covariance(...args) {
10490
+ return this._covariance(this, ...args);
10491
+ }
10492
+ every(...args) {
10493
+ return this._every(this, ...args);
10494
+ }
10495
+ fill(...args) {
10496
+ return this._fill(this, ...args);
10497
+ }
10498
+ first(...args) {
10499
+ return this._first(this, ...args);
10500
+ }
10501
+ get(...args) {
10502
+ return this._get(this, ...args);
10503
+ }
10504
+ includes(...args) {
10505
+ return this._includes(this, ...args);
10506
+ }
10507
+ indexof(...args) {
10508
+ return this._indexof(this, ...args);
10509
+ }
10510
+ insert(...args) {
10511
+ return this._insert(this, ...args);
10512
+ }
10513
+ join(...args) {
10514
+ return this._join(this, ...args);
10515
+ }
10516
+ last(...args) {
10517
+ return this._last(this, ...args);
10518
+ }
10519
+ lastindexof(...args) {
10520
+ return this._lastindexof(this, ...args);
10521
+ }
10522
+ max(...args) {
10523
+ return this._max(this, ...args);
10524
+ }
10525
+ median(...args) {
10526
+ return this._median(this, ...args);
10527
+ }
10528
+ min(...args) {
10529
+ return this._min(this, ...args);
10530
+ }
10531
+ mode(...args) {
10532
+ return this._mode(this, ...args);
10533
+ }
10534
+ percentile_linear_interpolation(...args) {
10535
+ return this._percentile_linear_interpolation(this, ...args);
10536
+ }
10537
+ percentile_nearest_rank(...args) {
10538
+ return this._percentile_nearest_rank(this, ...args);
10539
+ }
10540
+ percentrank(...args) {
10541
+ return this._percentrank(this, ...args);
10542
+ }
10543
+ pop(...args) {
10544
+ return this._pop(this, ...args);
10545
+ }
10546
+ push(...args) {
10547
+ return this._push(this, ...args);
10548
+ }
10549
+ range(...args) {
10550
+ return this._range(this, ...args);
10551
+ }
10552
+ remove(...args) {
10553
+ return this._remove(this, ...args);
10554
+ }
10555
+ reverse(...args) {
10556
+ return this._reverse(this, ...args);
10557
+ }
10558
+ set(...args) {
10559
+ return this._set(this, ...args);
10560
+ }
10561
+ shift(...args) {
10562
+ return this._shift(this, ...args);
10563
+ }
10564
+ size(...args) {
10565
+ return this._size(this, ...args);
10566
+ }
10567
+ slice(...args) {
10568
+ return this._slice(this, ...args);
10569
+ }
10570
+ some(...args) {
10571
+ return this._some(this, ...args);
10572
+ }
10573
+ sort(...args) {
10574
+ return this._sort(this, ...args);
10575
+ }
10576
+ sort_indices(...args) {
10577
+ return this._sort_indices(this, ...args);
10578
+ }
10579
+ standardize(...args) {
10580
+ return this._standardize(this, ...args);
10581
+ }
10582
+ stdev(...args) {
10583
+ return this._stdev(this, ...args);
10584
+ }
10585
+ sum(...args) {
10586
+ return this._sum(this, ...args);
10587
+ }
10588
+ unshift(...args) {
10589
+ return this._unshift(this, ...args);
10590
+ }
10591
+ variance(...args) {
10592
+ return this._variance(this, ...args);
10593
+ }
10594
+ }
10595
+
10596
+ function from(context) {
10597
+ return (...values) => {
10598
+ return new PineArrayObject([...values], inferArrayType(values), context);
10599
+ };
10600
+ }
10601
+
10602
+ function new_fn$2(context) {
10603
+ return (size, initial_value) => {
10604
+ return new PineArrayObject(
10605
+ Array(size).fill(context.precision(initial_value || 0)),
10606
+ inferValueType(initial_value || 0),
10607
+ context
10608
+ );
10609
+ };
10610
+ }
10611
+
10612
+ function new_bool(context) {
10613
+ return (size, initial_value = false) => {
10614
+ return new PineArrayObject(Array(size).fill(initial_value), PineArrayType.bool, context);
10615
+ };
10616
+ }
10617
+
10618
+ function new_float(context) {
10619
+ return (size, initial_value = NaN) => {
10620
+ return new PineArrayObject(Array(size).fill(context.precision(initial_value)), PineArrayType.float, context);
10621
+ };
10622
+ }
10623
+
10624
+ function new_int(context) {
10625
+ return (size, initial_value = 0) => {
10626
+ return new PineArrayObject(Array(size).fill(context.precision(initial_value)), PineArrayType.int, context);
10627
+ };
10628
+ }
10629
+
10630
+ function new_string(context) {
10631
+ return (size, initial_value = "") => {
10632
+ return new PineArrayObject(Array(size).fill(initial_value), PineArrayType.string, context);
10633
+ };
10634
+ }
10635
+
10636
+ class Series {
10637
+ constructor(data, offset = 0) {
10638
+ this.data = data;
10639
+ this.offset = offset;
10640
+ }
10641
+ get(index) {
10642
+ const realIndex = this.data.length - 1 - (this.offset + index);
10643
+ if (realIndex < 0 || realIndex >= this.data.length) {
10644
+ return NaN;
10645
+ }
10646
+ return this.data[realIndex];
10647
+ }
10648
+ set(index, value) {
10649
+ const realIndex = this.data.length - 1 - (this.offset + index);
10650
+ if (realIndex >= 0 && realIndex < this.data.length) {
10651
+ this.data[realIndex] = value;
10652
+ }
10653
+ }
10654
+ get length() {
10655
+ return this.data.length;
10656
+ }
10657
+ toArray() {
10658
+ return this.data;
10659
+ }
10660
+ static from(source) {
10661
+ if (source instanceof Series) return source;
10662
+ if (Array.isArray(source)) return new Series(source);
10663
+ return new Series([source]);
10664
+ }
10665
+ }
10666
+
10667
+ function param$6(context) {
10668
+ return (source, index = 0) => {
10669
+ return Series.from(source).get(index);
10670
+ };
10671
+ }
10672
+
10673
+ class PineArray {
10674
+ constructor(context) {
10675
+ this.context = context;
10676
+ this.abs = (id, ...args) => id.abs(...args);
10677
+ this.avg = (id, ...args) => id.avg(...args);
10678
+ this.binary_search = (id, ...args) => id.binary_search(...args);
10679
+ this.binary_search_leftmost = (id, ...args) => id.binary_search_leftmost(...args);
10680
+ this.binary_search_rightmost = (id, ...args) => id.binary_search_rightmost(...args);
10681
+ this.clear = (id, ...args) => id.clear(...args);
10682
+ this.concat = (id, ...args) => id.concat(...args);
10683
+ this.copy = (id, ...args) => id.copy(...args);
10684
+ this.covariance = (id, ...args) => id.covariance(...args);
10685
+ this.every = (id, ...args) => id.every(...args);
10686
+ this.fill = (id, ...args) => id.fill(...args);
10687
+ this.first = (id, ...args) => id.first(...args);
10688
+ this.from = from(context);
10689
+ this.get = (id, ...args) => id.get(...args);
10690
+ this.includes = (id, ...args) => id.includes(...args);
10691
+ this.indexof = (id, ...args) => id.indexof(...args);
10692
+ this.insert = (id, ...args) => id.insert(...args);
10693
+ this.join = (id, ...args) => id.join(...args);
10694
+ this.last = (id, ...args) => id.last(...args);
10695
+ this.lastindexof = (id, ...args) => id.lastindexof(...args);
10696
+ this.max = (id, ...args) => id.max(...args);
10697
+ this.median = (id, ...args) => id.median(...args);
10698
+ this.min = (id, ...args) => id.min(...args);
10699
+ this.mode = (id, ...args) => id.mode(...args);
10700
+ this.new = new_fn$2(context);
10701
+ this.new_bool = new_bool(context);
10702
+ this.new_float = new_float(context);
10703
+ this.new_int = new_int(context);
10704
+ this.new_string = new_string(context);
10705
+ this.param = param$6();
10706
+ this.percentile_linear_interpolation = (id, ...args) => id.percentile_linear_interpolation(...args);
10707
+ this.percentile_nearest_rank = (id, ...args) => id.percentile_nearest_rank(...args);
10708
+ this.percentrank = (id, ...args) => id.percentrank(...args);
10709
+ this.pop = (id, ...args) => id.pop(...args);
10710
+ this.push = (id, ...args) => id.push(...args);
10711
+ this.range = (id, ...args) => id.range(...args);
10712
+ this.remove = (id, ...args) => id.remove(...args);
10713
+ this.reverse = (id, ...args) => id.reverse(...args);
10714
+ this.set = (id, ...args) => id.set(...args);
10715
+ this.shift = (id, ...args) => id.shift(...args);
10716
+ this.size = (id, ...args) => id.size(...args);
10717
+ this.slice = (id, ...args) => id.slice(...args);
10718
+ this.some = (id, ...args) => id.some(...args);
10719
+ this.sort = (id, ...args) => id.sort(...args);
10720
+ this.sort_indices = (id, ...args) => id.sort_indices(...args);
10721
+ this.standardize = (id, ...args) => id.standardize(...args);
10722
+ this.stdev = (id, ...args) => id.stdev(...args);
10723
+ this.sum = (id, ...args) => id.sum(...args);
10724
+ this.unshift = (id, ...args) => id.unshift(...args);
10725
+ this.variance = (id, ...args) => id.variance(...args);
10726
+ }
10727
+ }
10728
+
10729
+ function clear(context) {
10730
+ return (id) => {
10731
+ id.map.clear();
10732
+ };
10733
+ }
10734
+
10735
+ function contains(context) {
10736
+ return (id, key) => {
10737
+ return id.map.has(key);
10738
+ };
10739
+ }
10740
+
10741
+ function copy$1(context) {
10742
+ return (id) => {
10743
+ const newMap = new PineMapObject(id.keyType, id.valueType, context);
10744
+ newMap.map = new Map(id.map);
10745
+ return newMap;
10746
+ };
10747
+ }
10748
+
10749
+ function get$1(context) {
10750
+ return (id, key) => {
10751
+ const val = id.map.get(key);
10752
+ return val === void 0 ? NaN : val;
10753
+ };
10754
+ }
10755
+
10756
+ function keys(context) {
10757
+ return (id) => {
10758
+ const keysArray = Array.from(id.map.keys());
10759
+ return new PineArrayObject(keysArray, id.keyType, context);
10760
+ };
10761
+ }
10762
+
10763
+ function put(context) {
10764
+ return (id, key, value) => {
10765
+ const prev = id.map.get(key);
10766
+ id.map.set(key, value);
10767
+ return prev === void 0 ? NaN : prev;
10768
+ };
10769
+ }
10770
+
10771
+ function put_all(context) {
10772
+ return (id, id2) => {
10773
+ for (const [key, value] of id2.map) {
10774
+ id.map.set(key, value);
10775
+ }
10776
+ };
10777
+ }
10778
+
10779
+ function remove(context) {
10780
+ return (id, key) => {
10781
+ const val = id.map.get(key);
10782
+ const existed = id.map.delete(key);
10783
+ return existed ? val : NaN;
10784
+ };
10785
+ }
10786
+
10787
+ function size(context) {
10788
+ return (id) => {
10789
+ return id.map.size;
10790
+ };
10791
+ }
10792
+
10793
+ function values(context) {
10794
+ return (id) => {
10795
+ const valuesArray = Array.from(id.map.values());
10796
+ return new PineArrayObject(valuesArray, id.valueType, context);
10797
+ };
10798
+ }
10799
+
10800
+ var __defProp$a = Object.defineProperty;
10801
+ var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10802
+ var __publicField$a = (obj, key, value) => __defNormalProp$a(obj, typeof key !== "symbol" ? key + "" : key, value);
10803
+ class PineMapObject {
10804
+ constructor(keyType, valueType, context) {
10805
+ this.keyType = keyType;
10806
+ this.valueType = valueType;
10807
+ this.context = context;
10808
+ __publicField$a(this, "map");
10809
+ __publicField$a(this, "_clear");
10810
+ __publicField$a(this, "_contains");
10811
+ __publicField$a(this, "_copy");
10812
+ __publicField$a(this, "_get");
10813
+ __publicField$a(this, "_keys");
10814
+ __publicField$a(this, "_put");
10815
+ __publicField$a(this, "_put_all");
10816
+ __publicField$a(this, "_remove");
10817
+ __publicField$a(this, "_size");
10818
+ __publicField$a(this, "_values");
10819
+ this.map = /* @__PURE__ */ new Map();
10820
+ this._clear = clear(this.context);
10821
+ this._contains = contains(this.context);
10822
+ this._copy = copy$1(this.context);
10823
+ this._get = get$1(this.context);
10824
+ this._keys = keys(this.context);
10825
+ this._put = put(this.context);
10826
+ this._put_all = put_all(this.context);
10827
+ this._remove = remove(this.context);
10828
+ this._size = size(this.context);
10829
+ this._values = values(this.context);
10830
+ }
10831
+ toString() {
10832
+ return `PineMapObject<${this.keyType}, ${this.valueType}>(${this.map.size})`;
10833
+ }
10834
+ clear(...args) {
10835
+ return this._clear(this, ...args);
10836
+ }
10837
+ contains(...args) {
10838
+ return this._contains(this, ...args);
10839
+ }
10840
+ copy(...args) {
10841
+ return this._copy(this, ...args);
10842
+ }
10843
+ get(...args) {
10844
+ return this._get(this, ...args);
10845
+ }
10846
+ keys(...args) {
10847
+ return this._keys(this, ...args);
10848
+ }
10849
+ put(...args) {
10850
+ return this._put(this, ...args);
10851
+ }
10852
+ put_all(...args) {
10853
+ return this._put_all(this, ...args);
10854
+ }
10855
+ remove(...args) {
10856
+ return this._remove(this, ...args);
10857
+ }
10858
+ size(...args) {
10859
+ return this._size(this, ...args);
10860
+ }
10861
+ values(...args) {
10862
+ return this._values(this, ...args);
10863
+ }
10864
+ }
10865
+
10866
+ function new_fn$1(context) {
10867
+ return (keyType, valueType) => {
10868
+ return new PineMapObject(keyType, valueType, context);
10869
+ };
10870
+ }
10871
+
10872
+ function param$5(context) {
10873
+ return (source, index = 0) => {
10874
+ return Series.from(source).get(index);
10875
+ };
10876
+ }
10877
+
10878
+ class PineMap {
10879
+ constructor(context) {
10880
+ this.context = context;
10881
+ this.clear = (id, ...args) => id.clear(...args);
10882
+ this.contains = (id, ...args) => id.contains(...args);
10883
+ this.copy = (id, ...args) => id.copy(...args);
10884
+ this.get = (id, ...args) => id.get(...args);
10885
+ this.keys = (id, ...args) => id.keys(...args);
10886
+ this.new = new_fn$1(context);
10887
+ this.param = param$5();
10888
+ this.put = (id, ...args) => id.put(...args);
10889
+ this.put_all = (id, ...args) => id.put_all(...args);
10890
+ this.remove = (id, ...args) => id.remove(...args);
10891
+ this.size = (id, ...args) => id.size(...args);
10892
+ this.values = (id, ...args) => id.values(...args);
10893
+ }
10894
+ }
10895
+
10896
+ function add_col(context) {
10897
+ return (id, column_index, values) => {
10898
+ const rows = id.matrix.length;
10899
+ let colValues = [];
10900
+ if (values) {
10901
+ if (values instanceof PineArrayObject) {
10902
+ colValues = values.array;
10903
+ } else if (Array.isArray(values)) {
10904
+ colValues = values;
10905
+ } else {
10906
+ colValues = [values];
10907
+ }
10908
+ }
10909
+ if (rows === 0) {
10910
+ for (let i = 0; i < colValues.length; i++) {
10911
+ let val = colValues[i];
10912
+ if (val instanceof Series) val = val.get(0);
10913
+ id.matrix.push([val]);
10914
+ }
10915
+ return;
10916
+ }
10917
+ const cols = id.matrix[0].length;
10918
+ const index = column_index !== void 0 ? column_index : cols;
10919
+ for (let i = 0; i < rows; i++) {
10920
+ let val = i < colValues.length ? colValues[i] : NaN;
10921
+ if (val instanceof Series) val = val.get(0);
10922
+ id.matrix[i].splice(index, 0, val);
10923
+ }
10924
+ };
10925
+ }
10926
+
10927
+ function add_row(context) {
10928
+ return (id, row_index, values) => {
10929
+ const rows = id.matrix.length;
10930
+ let rowValues = [];
10931
+ if (values) {
10932
+ if (values instanceof PineArrayObject) {
10933
+ rowValues = values.array;
10934
+ } else if (Array.isArray(values)) {
10935
+ rowValues = values;
10936
+ } else {
10937
+ if (values instanceof Series) {
10938
+ const val = values.get(0);
10939
+ if (Array.isArray(val)) {
10940
+ rowValues = val;
10941
+ } else {
10942
+ rowValues = [val];
10943
+ }
10944
+ } else {
10945
+ rowValues = [values];
10946
+ }
10947
+ }
10948
+ }
10949
+ const cols = rows > 0 ? id.matrix[0].length : rowValues.length;
10950
+ const index = row_index !== void 0 ? row_index : rows;
10951
+ const newRow = [];
10952
+ for (let i = 0; i < cols; i++) {
10953
+ if (i < rowValues.length) {
10954
+ let val = rowValues[i];
10955
+ if (val instanceof Series) {
10956
+ val = val.get(0);
10957
+ }
10958
+ newRow.push(val);
10959
+ } else {
10960
+ newRow.push(NaN);
10961
+ }
10962
+ }
10963
+ id.matrix.splice(index, 0, newRow);
10964
+ };
10965
+ }
10966
+
10967
+ function avg$1(context) {
10968
+ return (id) => {
10969
+ const rows = id.matrix.length;
10970
+ if (rows === 0) return NaN;
10971
+ const cols = id.matrix[0].length;
10972
+ if (cols === 0) return NaN;
10973
+ let sum = 0;
10974
+ let count = 0;
10975
+ for (let i = 0; i < rows; i++) {
10976
+ for (let j = 0; j < cols; j++) {
10977
+ const val = id.matrix[i][j];
10978
+ if (!isNaN(val)) {
10979
+ sum += val;
10980
+ count++;
10981
+ }
10982
+ }
10983
+ }
10984
+ if (count === 0) return NaN;
10985
+ return sum / count;
10986
+ };
10987
+ }
10988
+
10989
+ function col(context) {
10990
+ return (id, column) => {
10991
+ const rows = id.matrix.length;
10992
+ const result = [];
10993
+ for (let i = 0; i < rows; i++) {
10994
+ result.push(id.matrix[i][column]);
10995
+ }
10996
+ return new PineArrayObject(result, id.type, context);
10997
+ };
10998
+ }
10999
+
11000
+ function columns(context) {
11001
+ return (id) => {
11002
+ if (id.matrix.length === 0) return 0;
11003
+ return id.matrix[0].length;
11004
+ };
11005
+ }
11006
+
11007
+ function concat(context) {
11008
+ return (id, id2) => {
11009
+ const rows1 = id.matrix.length;
11010
+ const rows2 = id2.matrix.length;
11011
+ if (rows1 === 0) {
11012
+ for (let i = 0; i < rows2; i++) {
11013
+ id.matrix.push([...id2.matrix[i]]);
11014
+ }
11015
+ return;
11016
+ }
11017
+ const cols1 = id.matrix[0].length;
11018
+ const cols2 = rows2 > 0 ? id2.matrix[0].length : 0;
11019
+ if (cols1 !== cols2 && rows2 > 0) {
11020
+ console.error(`matrix.concat: Column count mismatch ${cols1} vs ${cols2}`);
11021
+ return;
11022
+ }
11023
+ for (let i = 0; i < rows2; i++) {
11024
+ id.matrix.push([...id2.matrix[i]]);
11025
+ }
11026
+ };
11027
+ }
11028
+
11029
+ function copy(context) {
11030
+ return (id) => {
11031
+ const rows = id.matrix.length;
11032
+ const cols = rows > 0 ? id.matrix[0].length : 0;
11033
+ const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
11034
+ newMatrix.matrix = id.matrix.map((row) => [...row]);
11035
+ return newMatrix;
11036
+ };
11037
+ }
11038
+
11039
+ function determinant(matrix) {
11040
+ const n = matrix.length;
11041
+ if (n === 0) return 0;
11042
+ if (n === 1) return matrix[0][0];
11043
+ if (n === 2) return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
11044
+ const mat = matrix.map((row) => [...row]);
11045
+ let det2 = 1;
11046
+ for (let i = 0; i < n; i++) {
11047
+ let pivot = i;
11048
+ while (pivot < n && mat[pivot][i] === 0) pivot++;
11049
+ if (pivot === n) return 0;
11050
+ if (pivot !== i) {
11051
+ [mat[i], mat[pivot]] = [mat[pivot], mat[i]];
11052
+ det2 *= -1;
11053
+ }
11054
+ det2 *= mat[i][i];
11055
+ for (let j = i + 1; j < n; j++) {
11056
+ const factor = mat[j][i] / mat[i][i];
11057
+ for (let k = i; k < n; k++) {
11058
+ mat[j][k] -= factor * mat[i][k];
11059
+ }
11060
+ }
11061
+ }
11062
+ return det2;
11063
+ }
11064
+ function det(context) {
11065
+ return (id) => {
11066
+ const rows = id.matrix.length;
11067
+ const cols = rows > 0 ? id.matrix[0].length : 0;
11068
+ if (rows !== cols) return NaN;
11069
+ return determinant(id.matrix);
11070
+ };
11071
+ }
11072
+
11073
+ function diff(context) {
11074
+ return (id, id2) => {
11075
+ const rows = id.matrix.length;
11076
+ if (rows === 0) return new PineMatrixObject(id.type, 0, 0, NaN, context);
11077
+ const cols = id.matrix[0].length;
11078
+ const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
11079
+ if (id2 instanceof PineMatrixObject) {
11080
+ for (let i = 0; i < rows; i++) {
11081
+ for (let j = 0; j < cols; j++) {
11082
+ const v1 = id.matrix[i][j];
11083
+ const v2 = id2.matrix[i] && id2.matrix[i][j] !== void 0 ? id2.matrix[i][j] : NaN;
11084
+ newMatrix.matrix[i][j] = v1 - v2;
11085
+ }
11086
+ }
11087
+ } else {
11088
+ const scalar = id2;
11089
+ for (let i = 0; i < rows; i++) {
11090
+ for (let j = 0; j < cols; j++) {
11091
+ newMatrix.matrix[i][j] = id.matrix[i][j] - scalar;
11092
+ }
11093
+ }
11094
+ }
11095
+ return newMatrix;
11096
+ };
11097
+ }
11098
+
11099
+ function calculateEigenvalues(matrix) {
11100
+ const n = matrix.length;
11101
+ if (n !== 2) return Array(n).fill(NaN);
11102
+ const a = matrix[0][0];
11103
+ const b = matrix[0][1];
11104
+ const c = matrix[1][0];
11105
+ const d = matrix[1][1];
11106
+ const trace = a + d;
11107
+ const det = a * d - b * c;
11108
+ const delta = trace * trace - 4 * det;
11109
+ if (delta < 0) return [NaN, NaN];
11110
+ const l1 = (trace + Math.sqrt(delta)) / 2;
11111
+ const l2 = (trace - Math.sqrt(delta)) / 2;
11112
+ return [l1, l2];
11113
+ }
11114
+ function eigenvalues(context) {
11115
+ return (id) => {
11116
+ const rows = id.matrix.length;
11117
+ const cols = rows > 0 ? id.matrix[0].length : 0;
11118
+ if (rows !== cols) return new PineArrayObject([], id.type, context);
11119
+ const vals = calculateEigenvalues(id.matrix);
11120
+ return new PineArrayObject(vals, id.type, context);
11121
+ };
11122
+ }
11123
+
11124
+ function eigenvectors(context) {
11125
+ return (id) => {
11126
+ const rows = id.matrix.length;
11127
+ const cols = rows > 0 ? id.matrix[0].length : 0;
11128
+ if (rows !== cols) return new PineMatrixObject(id.type, 0, 0, NaN, context);
11129
+ const newMatrix = new PineMatrixObject(id.type, rows, cols, 0, context);
11130
+ for (let i = 0; i < rows; i++) newMatrix.matrix[i][i] = 1;
11131
+ return newMatrix;
11132
+ };
11133
+ }
11134
+
11135
+ function elements_count(context) {
11136
+ return (id) => {
11137
+ const rows = id.matrix.length;
11138
+ if (rows === 0) return 0;
11139
+ return rows * id.matrix[0].length;
11140
+ };
11141
+ }
11142
+
11143
+ function fill(context) {
11144
+ return (id, value, from_row, to_row, from_col, to_col) => {
11145
+ const rows = id.matrix.length;
11146
+ if (rows === 0) return;
11147
+ const cols = id.matrix[0].length;
11148
+ const r1 = from_row !== void 0 ? from_row : 0;
11149
+ const r2 = to_row !== void 0 ? to_row : rows;
11150
+ const c1 = from_col !== void 0 ? from_col : 0;
11151
+ const c2 = to_col !== void 0 ? to_col : cols;
11152
+ for (let i = r1; i < r2; i++) {
11153
+ if (i >= rows) break;
11154
+ for (let j = c1; j < c2; j++) {
11155
+ if (j >= cols) break;
11156
+ id.matrix[i][j] = value;
11157
+ }
11158
+ }
11159
+ };
11160
+ }
11161
+
11162
+ function get(context) {
11163
+ return (id, row, col) => {
11164
+ if (!id.matrix[row]) return NaN;
11165
+ const val = id.matrix[row][col];
11166
+ return val === void 0 ? NaN : val;
11167
+ };
11168
+ }
11169
+
11170
+ function inverse(matrix) {
11171
+ const n = matrix.length;
11172
+ if (n !== 2) return matrix.map((r) => r.map(() => NaN));
11173
+ const det = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
11174
+ if (det === 0)
11175
+ return [
11176
+ [NaN, NaN],
11177
+ [NaN, NaN]
11178
+ ];
11179
+ return [
11180
+ [matrix[1][1] / det, -matrix[0][1] / det],
11181
+ [-matrix[1][0] / det, matrix[0][0] / det]
11182
+ ];
11183
+ }
11184
+ function inv(context) {
11185
+ return (id) => {
11186
+ const rows = id.matrix.length;
11187
+ const cols = rows > 0 ? id.matrix[0].length : 0;
11188
+ if (rows !== cols) return new PineMatrixObject(id.type, rows, cols, NaN, context);
11189
+ const invMat = inverse(id.matrix);
11190
+ const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
11191
+ newMatrix.matrix = invMat;
11192
+ return newMatrix;
11193
+ };
11194
+ }
11195
+
11196
+ function is_antidiagonal(context) {
11197
+ return (id) => {
11198
+ const rows = id.matrix.length;
11199
+ if (rows === 0) return false;
11200
+ const cols = id.matrix[0].length;
11201
+ if (rows !== cols) return false;
11202
+ for (let i = 0; i < rows; i++) {
11203
+ for (let j = 0; j < cols; j++) {
11204
+ if (i + j !== rows - 1) {
11205
+ if (id.matrix[i][j] !== 0) return false;
11206
+ }
11207
+ }
11208
+ }
11209
+ return true;
11210
+ };
11211
+ }
11212
+
11213
+ function is_antisymmetric(context) {
11214
+ return (id) => {
11215
+ const rows = id.matrix.length;
11216
+ if (rows === 0) return false;
11217
+ const cols = id.matrix[0].length;
11218
+ if (rows !== cols) return false;
11219
+ for (let i = 0; i < rows; i++) {
11220
+ for (let j = 0; j < cols; j++) {
11221
+ if (id.matrix[j][i] !== -id.matrix[i][j]) return false;
11222
+ }
11223
+ }
11224
+ return true;
11225
+ };
11226
+ }
11227
+
11228
+ function is_binary(context) {
11229
+ return (id) => {
11230
+ const rows = id.matrix.length;
11231
+ if (rows === 0) return false;
11232
+ const cols = id.matrix[0].length;
11233
+ for (let i = 0; i < rows; i++) {
11234
+ for (let j = 0; j < cols; j++) {
11235
+ const val = id.matrix[i][j];
11236
+ if (val !== 0 && val !== 1) return false;
11237
+ }
11238
+ }
11239
+ return true;
11240
+ };
11241
+ }
11242
+
11243
+ function is_diagonal(context) {
11244
+ return (id) => {
11245
+ const rows = id.matrix.length;
11246
+ if (rows === 0) return false;
11247
+ const cols = id.matrix[0].length;
11248
+ if (rows !== cols) return false;
11249
+ for (let i = 0; i < rows; i++) {
11250
+ for (let j = 0; j < cols; j++) {
11251
+ if (i !== j) {
11252
+ if (id.matrix[i][j] !== 0) return false;
11253
+ }
11254
+ }
11255
+ }
11256
+ return true;
11257
+ };
11258
+ }
11259
+
11260
+ function is_identity(context) {
11261
+ return (id) => {
11262
+ const rows = id.matrix.length;
11263
+ if (rows === 0) return false;
11264
+ const cols = id.matrix[0].length;
11265
+ if (rows !== cols) return false;
11266
+ for (let i = 0; i < rows; i++) {
11267
+ for (let j = 0; j < cols; j++) {
11268
+ if (i === j) {
11269
+ if (id.matrix[i][j] !== 1) return false;
11270
+ } else {
11271
+ if (id.matrix[i][j] !== 0) return false;
11272
+ }
11273
+ }
11274
+ }
11275
+ return true;
11276
+ };
11277
+ }
11278
+
11279
+ function is_square(context) {
11280
+ return (id) => {
11281
+ const rows = id.matrix.length;
11282
+ if (rows === 0) return false;
11283
+ const cols = id.matrix[0].length;
11284
+ return rows === cols;
11285
+ };
11286
+ }
11287
+
11288
+ function is_stochastic(context) {
11289
+ return (id) => {
11290
+ const rows = id.matrix.length;
11291
+ if (rows === 0) return false;
11292
+ const cols = id.matrix[0].length;
11293
+ for (let i = 0; i < rows; i++) {
11294
+ let sum = 0;
11295
+ for (let j = 0; j < cols; j++) {
11296
+ const val = id.matrix[i][j];
11297
+ if (val < 0) return false;
11298
+ sum += val;
11299
+ }
11300
+ if (Math.abs(sum - 1) > 1e-10) return false;
11301
+ }
11302
+ return true;
11303
+ };
11304
+ }
11305
+
11306
+ function is_symmetric(context) {
11307
+ return (id) => {
11308
+ const rows = id.matrix.length;
11309
+ if (rows === 0) return false;
11310
+ const cols = id.matrix[0].length;
11311
+ if (rows !== cols) return false;
11312
+ for (let i = 0; i < rows; i++) {
11313
+ for (let j = 0; j < i; j++) {
11314
+ if (id.matrix[i][j] !== id.matrix[j][i]) return false;
11315
+ }
11316
+ }
11317
+ return true;
11318
+ };
11319
+ }
11320
+
11321
+ function is_triangular(context) {
11322
+ return (id) => {
11323
+ const rows = id.matrix.length;
11324
+ if (rows === 0) return false;
11325
+ const cols = id.matrix[0].length;
11326
+ if (rows !== cols) return false;
11327
+ let isUpper = true;
11328
+ let isLower = true;
11329
+ for (let i = 0; i < rows; i++) {
11330
+ for (let j = 0; j < cols; j++) {
11331
+ if (i > j && id.matrix[i][j] !== 0) isUpper = false;
11332
+ if (i < j && id.matrix[i][j] !== 0) isLower = false;
11333
+ }
11334
+ }
11335
+ return isUpper || isLower;
11336
+ };
11337
+ }
11338
+
11339
+ function is_zero(context) {
11340
+ return (id) => {
11341
+ const rows = id.matrix.length;
11342
+ if (rows === 0) return true;
11343
+ const cols = id.matrix[0].length;
11344
+ for (let i = 0; i < rows; i++) {
11345
+ for (let j = 0; j < cols; j++) {
11346
+ if (id.matrix[i][j] !== 0) return false;
11347
+ }
11348
+ }
11349
+ return true;
11350
+ };
11351
+ }
11352
+
11353
+ function kron(context) {
11354
+ return (id, id2) => {
11355
+ const r1 = id.matrix.length;
11356
+ const c1 = r1 > 0 ? id.matrix[0].length : 0;
11357
+ const r2 = id2.matrix.length;
11358
+ const c2 = r2 > 0 ? id2.matrix[0].length : 0;
11359
+ const rows = r1 * r2;
11360
+ const cols = c1 * c2;
11361
+ const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
11362
+ for (let i = 0; i < r1; i++) {
11363
+ for (let j = 0; j < c1; j++) {
11364
+ const val1 = id.matrix[i][j];
11365
+ for (let k = 0; k < r2; k++) {
11366
+ for (let l = 0; l < c2; l++) {
11367
+ const val2 = id2.matrix[k][l];
11368
+ newMatrix.matrix[i * r2 + k][j * c2 + l] = val1 * val2;
11369
+ }
11370
+ }
11371
+ }
11372
+ }
11373
+ return newMatrix;
11374
+ };
11375
+ }
11376
+
11377
+ function max$1(context) {
11378
+ return (id) => {
11379
+ const rows = id.matrix.length;
11380
+ if (rows === 0) return NaN;
11381
+ const cols = id.matrix[0].length;
11382
+ let maxVal = -Infinity;
11383
+ let found = false;
11384
+ for (let i = 0; i < rows; i++) {
11385
+ for (let j = 0; j < cols; j++) {
11386
+ const val = id.matrix[i][j];
11387
+ if (!isNaN(val)) {
11388
+ if (val > maxVal) maxVal = val;
11389
+ found = true;
11390
+ }
11391
+ }
11392
+ }
11393
+ return found ? maxVal : NaN;
11394
+ };
11395
+ }
11396
+
11397
+ function median$1(context) {
11398
+ return (id) => {
11399
+ const rows = id.matrix.length;
11400
+ if (rows === 0) return NaN;
11401
+ const cols = id.matrix[0].length;
11402
+ const values = [];
11403
+ for (let i = 0; i < rows; i++) {
11404
+ for (let j = 0; j < cols; j++) {
11405
+ const val = id.matrix[i][j];
11406
+ if (!isNaN(val)) {
11407
+ values.push(val);
11408
+ }
11409
+ }
11410
+ }
11411
+ if (values.length === 0) return NaN;
11412
+ values.sort((a, b) => a - b);
11413
+ const mid = Math.floor(values.length / 2);
11414
+ if (values.length % 2 !== 0) {
11415
+ return values[mid];
11416
+ } else {
11417
+ return (values[mid - 1] + values[mid]) / 2;
11418
+ }
11419
+ };
11420
+ }
11421
+
11422
+ function min$1(context) {
11423
+ return (id) => {
11424
+ const rows = id.matrix.length;
11425
+ if (rows === 0) return NaN;
11426
+ const cols = id.matrix[0].length;
11427
+ let minVal = Infinity;
11428
+ let found = false;
11429
+ for (let i = 0; i < rows; i++) {
11430
+ for (let j = 0; j < cols; j++) {
11431
+ const val = id.matrix[i][j];
11432
+ if (!isNaN(val)) {
11433
+ if (val < minVal) minVal = val;
11434
+ found = true;
11435
+ }
11436
+ }
11437
+ }
11438
+ return found ? minVal : NaN;
11439
+ };
11440
+ }
11441
+
11442
+ function mode$1(context) {
11443
+ return (id) => {
11444
+ const rows = id.matrix.length;
11445
+ if (rows === 0) return NaN;
11446
+ const cols = id.matrix[0].length;
11447
+ const counts = /* @__PURE__ */ new Map();
11448
+ let maxCount = 0;
11449
+ let modeVal = NaN;
11450
+ for (let i = 0; i < rows; i++) {
11451
+ for (let j = 0; j < cols; j++) {
11452
+ const val = id.matrix[i][j];
11453
+ if (!isNaN(val)) {
11454
+ const count = (counts.get(val) || 0) + 1;
11455
+ counts.set(val, count);
11456
+ if (count > maxCount) {
11457
+ maxCount = count;
11458
+ modeVal = val;
11459
+ } else if (count === maxCount) {
11460
+ if (val < modeVal) modeVal = val;
11461
+ }
11462
+ }
11463
+ }
11464
+ }
11465
+ return modeVal;
11466
+ };
11467
+ }
11468
+
11469
+ function mult(context) {
11470
+ return (id, id2) => {
11471
+ const rows1 = id.matrix.length;
11472
+ const cols1 = rows1 > 0 ? id.matrix[0].length : 0;
11473
+ if (id2 instanceof PineMatrixObject) {
11474
+ const rows2 = id2.matrix.length;
11475
+ const cols2 = rows2 > 0 ? id2.matrix[0].length : 0;
11476
+ if (cols1 !== rows2) {
11477
+ return new PineMatrixObject(id.type, 0, 0, NaN, context);
11478
+ }
11479
+ const newMatrix = new PineMatrixObject(id.type, rows1, cols2, 0, context);
11480
+ for (let i = 0; i < rows1; i++) {
11481
+ for (let j = 0; j < cols2; j++) {
11482
+ let sum = 0;
11483
+ for (let k = 0; k < cols1; k++) {
11484
+ sum += id.matrix[i][k] * id2.matrix[k][j];
11485
+ }
11486
+ newMatrix.matrix[i][j] = sum;
11487
+ }
11488
+ }
11489
+ return newMatrix;
11490
+ } else if (id2 instanceof PineArrayObject || Array.isArray(id2.array || id2)) {
11491
+ const vec = id2.array || id2;
11492
+ if (cols1 !== vec.length) {
11493
+ return new PineMatrixObject(id.type, 0, 0, NaN, context);
11494
+ }
11495
+ const newMatrix = new PineMatrixObject(id.type, rows1, 1, 0, context);
11496
+ for (let i = 0; i < rows1; i++) {
11497
+ let sum = 0;
11498
+ for (let j = 0; j < cols1; j++) {
11499
+ sum += id.matrix[i][j] * vec[j];
11500
+ }
11501
+ newMatrix.matrix[i][0] = sum;
11502
+ }
11503
+ return newMatrix;
11504
+ } else {
11505
+ const scalar = id2;
11506
+ const newMatrix = new PineMatrixObject(id.type, rows1, cols1, NaN, context);
11507
+ for (let i = 0; i < rows1; i++) {
11508
+ for (let j = 0; j < cols1; j++) {
11509
+ newMatrix.matrix[i][j] = id.matrix[i][j] * scalar;
11510
+ }
11511
+ }
11512
+ return newMatrix;
11513
+ }
11514
+ };
11515
+ }
11516
+
11517
+ function pinv(context) {
11518
+ return (id) => {
11519
+ const rows = id.matrix.length;
11520
+ const cols = rows > 0 ? id.matrix[0].length : 0;
11521
+ if (rows === cols) {
11522
+ return new PineMatrixObject(id.type, rows, cols, NaN, context);
11523
+ }
11524
+ return new PineMatrixObject(id.type, cols, rows, NaN, context);
11525
+ };
11526
+ }
11527
+
11528
+ function pow$1(context) {
11529
+ return (id, power) => {
11530
+ const rows = id.matrix.length;
11531
+ const cols = rows > 0 ? id.matrix[0].length : 0;
11532
+ if (rows !== cols) {
11533
+ return new PineMatrixObject(id.type, 0, 0, NaN, context);
11534
+ }
11535
+ let result = new PineMatrixObject(id.type, rows, cols, 0, context);
11536
+ for (let i = 0; i < rows; i++) result.matrix[i][i] = 1;
11537
+ let base = new PineMatrixObject(id.type, rows, cols, NaN, context);
11538
+ for (let i = 0; i < rows; i++) base.matrix[i] = [...id.matrix[i]];
11539
+ let p = Math.floor(power);
11540
+ if (p < 0) return new PineMatrixObject(id.type, rows, cols, NaN, context);
11541
+ while (p > 0) {
11542
+ if (p % 2 === 1) {
11543
+ const temp = new PineMatrixObject(id.type, rows, cols, 0, context);
11544
+ for (let i = 0; i < rows; i++) {
11545
+ for (let j = 0; j < cols; j++) {
11546
+ let sum = 0;
11547
+ for (let k = 0; k < rows; k++) {
11548
+ sum += result.matrix[i][k] * base.matrix[k][j];
11549
+ }
11550
+ temp.matrix[i][j] = sum;
11551
+ }
11552
+ }
11553
+ result = temp;
11554
+ }
11555
+ const tempBase = new PineMatrixObject(id.type, rows, cols, 0, context);
11556
+ for (let i = 0; i < rows; i++) {
11557
+ for (let j = 0; j < cols; j++) {
11558
+ let sum = 0;
11559
+ for (let k = 0; k < rows; k++) {
11560
+ sum += base.matrix[i][k] * base.matrix[k][j];
11561
+ }
11562
+ tempBase.matrix[i][j] = sum;
11563
+ }
11564
+ }
11565
+ base = tempBase;
11566
+ p = Math.floor(p / 2);
11567
+ }
11568
+ return result;
11569
+ };
11570
+ }
11571
+
11572
+ function rank(context) {
11573
+ return (id) => {
11574
+ const rows = id.matrix.length;
11575
+ if (rows === 0) return 0;
11576
+ const cols = id.matrix[0].length;
11577
+ const mat = id.matrix.map((r2) => [...r2]);
11578
+ let r = 0;
11579
+ for (let c = 0; c < cols && r < rows; c++) {
11580
+ let pivot = r;
11581
+ while (pivot < rows && Math.abs(mat[pivot][c]) < 1e-10) pivot++;
11582
+ if (pivot < rows) {
11583
+ [mat[r], mat[pivot]] = [mat[pivot], mat[r]];
11584
+ const val = mat[r][c];
11585
+ for (let j = c; j < cols; j++) mat[r][j] /= val;
11586
+ for (let i = 0; i < rows; i++) {
11587
+ if (i !== r) {
11588
+ const factor = mat[i][c];
11589
+ for (let j = c; j < cols; j++) mat[i][j] -= factor * mat[r][j];
11590
+ }
11591
+ }
11592
+ r++;
11593
+ }
11594
+ }
11595
+ return r;
11596
+ };
11597
+ }
11598
+
11599
+ function remove_col(context) {
11600
+ return (id, column_index) => {
11601
+ const rows = id.matrix.length;
11602
+ if (rows === 0) return new PineArrayObject([], id.type, context);
11603
+ const removedValues = [];
11604
+ for (let i = 0; i < rows; i++) {
11605
+ const removed = id.matrix[i].splice(column_index, 1);
11606
+ removedValues.push(removed[0]);
11607
+ }
11608
+ return new PineArrayObject(removedValues, id.type, context);
11609
+ };
11610
+ }
11611
+
11612
+ function remove_row(context) {
11613
+ return (id, row_index) => {
11614
+ const removed = id.matrix.splice(row_index, 1);
11615
+ return new PineArrayObject(removed[0] || [], id.type, context);
11616
+ };
11617
+ }
11618
+
11619
+ function reshape(context) {
11620
+ return (id, rows, cols) => {
11621
+ const currentRows = id.matrix.length;
11622
+ const currentCols = currentRows > 0 ? id.matrix[0].length : 0;
11623
+ const newSize = rows * cols;
11624
+ const elements = [];
11625
+ for (let i = 0; i < currentRows; i++) {
11626
+ for (let j = 0; j < currentCols; j++) {
11627
+ elements.push(id.matrix[i][j]);
11628
+ }
11629
+ }
11630
+ while (elements.length < newSize) elements.push(NaN);
11631
+ if (elements.length > newSize) elements.length = newSize;
11632
+ const newMatrix = [];
11633
+ let k = 0;
11634
+ for (let i = 0; i < rows; i++) {
11635
+ const row = [];
11636
+ for (let j = 0; j < cols; j++) {
11637
+ row.push(elements[k++]);
11638
+ }
11639
+ newMatrix.push(row);
11640
+ }
11641
+ id.matrix = newMatrix;
11642
+ };
11643
+ }
11644
+
11645
+ function reverse(context) {
11646
+ return (id) => {
11647
+ id.matrix.reverse();
11648
+ for (const row of id.matrix) {
11649
+ row.reverse();
11650
+ }
11651
+ };
11652
+ }
11653
+
11654
+ function row(context) {
11655
+ return (id, row2) => {
11656
+ if (!id.matrix[row2]) return new PineArrayObject([], id.type, context);
11657
+ return new PineArrayObject([...id.matrix[row2]], id.type, context);
11658
+ };
11659
+ }
11660
+
11661
+ function rows(context) {
11662
+ return (id) => {
11663
+ return id.matrix.length;
11664
+ };
11665
+ }
11666
+
11667
+ function set(context) {
11668
+ return (id, row, col, value) => {
11669
+ if (!id.matrix[row]) return;
11670
+ id.matrix[row][col] = value;
11671
+ };
11672
+ }
11673
+
11674
+ function sort(context) {
11675
+ return (id, column = 0, order = "asc") => {
11676
+ const rows = id.matrix.length;
11677
+ if (rows === 0) return;
11678
+ id.matrix.sort((a, b) => {
11679
+ const valA = a[column];
11680
+ const valB = b[column];
11681
+ if (valA < valB) return order === "asc" ? -1 : 1;
11682
+ if (valA > valB) return order === "asc" ? 1 : -1;
11683
+ return 0;
11684
+ });
11685
+ };
11686
+ }
11687
+
11688
+ function submatrix(context) {
11689
+ return (id, from_row, to_row, from_col, to_col) => {
11690
+ const rows = to_row - from_row;
11691
+ const cols = to_col - from_col;
11692
+ const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
11693
+ for (let i = 0; i < rows; i++) {
11694
+ const sourceRow = from_row + i;
11695
+ if (sourceRow >= id.matrix.length) break;
11696
+ for (let j = 0; j < cols; j++) {
11697
+ const sourceCol = from_col + j;
11698
+ if (sourceCol >= id.matrix[sourceRow].length) break;
11699
+ newMatrix.matrix[i][j] = id.matrix[sourceRow][sourceCol];
11700
+ }
11701
+ }
11702
+ return newMatrix;
11703
+ };
11704
+ }
11705
+
11706
+ function sum$1(context) {
11707
+ return (id, id2) => {
11708
+ const rows = id.matrix.length;
11709
+ if (rows === 0) return new PineMatrixObject(id.type, 0, 0, NaN, context);
11710
+ const cols = id.matrix[0].length;
11711
+ const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
11712
+ if (id2 instanceof PineMatrixObject) {
11713
+ for (let i = 0; i < rows; i++) {
11714
+ for (let j = 0; j < cols; j++) {
11715
+ const v1 = id.matrix[i][j];
11716
+ const v2 = id2.matrix[i] && id2.matrix[i][j] !== void 0 ? id2.matrix[i][j] : NaN;
11717
+ newMatrix.matrix[i][j] = v1 + v2;
11718
+ }
11719
+ }
11720
+ } else {
11721
+ const scalar = id2;
11722
+ for (let i = 0; i < rows; i++) {
11723
+ for (let j = 0; j < cols; j++) {
11724
+ newMatrix.matrix[i][j] = id.matrix[i][j] + scalar;
11725
+ }
11726
+ }
11727
+ }
11728
+ return newMatrix;
9868
11729
  };
9869
11730
  }
9870
11731
 
9871
- function some(context) {
9872
- return (id, callback) => {
9873
- return id.array.some(callback);
11732
+ function swap_columns(context) {
11733
+ return (id, col1, col2) => {
11734
+ const rows = id.matrix.length;
11735
+ for (let i = 0; i < rows; i++) {
11736
+ const temp = id.matrix[i][col1];
11737
+ id.matrix[i][col1] = id.matrix[i][col2];
11738
+ id.matrix[i][col2] = temp;
11739
+ }
9874
11740
  };
9875
11741
  }
9876
11742
 
9877
- function sort(context) {
9878
- return (id, order = "asc") => {
9879
- id.array.sort((a, b) => order === "asc" ? a - b : b - a);
11743
+ function swap_rows(context) {
11744
+ return (id, row1, row2) => {
11745
+ const temp = id.matrix[row1];
11746
+ id.matrix[row1] = id.matrix[row2];
11747
+ id.matrix[row2] = temp;
9880
11748
  };
9881
11749
  }
9882
11750
 
9883
- function sort_indices(context) {
9884
- return (id, comparator) => {
9885
- const indices = id.array.map((_, index) => index);
9886
- indices.sort((a, b) => {
9887
- const valA = id.array[a];
9888
- const valB = id.array[b];
9889
- return comparator ? comparator(valA, valB) : valA - valB;
9890
- });
9891
- return new PineArrayObject(indices);
11751
+ function trace(context) {
11752
+ return (id) => {
11753
+ const rows = id.matrix.length;
11754
+ if (rows === 0) return 0;
11755
+ const cols = id.matrix[0].length;
11756
+ const n = Math.min(rows, cols);
11757
+ let sum = 0;
11758
+ for (let i = 0; i < n; i++) {
11759
+ sum += id.matrix[i][i];
11760
+ }
11761
+ return sum;
9892
11762
  };
9893
11763
  }
9894
11764
 
9895
- function standardize(context) {
11765
+ function transpose(context) {
9896
11766
  return (id) => {
9897
- const mean = context.array.avg(id);
9898
- const stdev = context.array.stdev(id);
9899
- if (stdev === 0) {
9900
- return new PineArrayObject(id.array.map(() => 0));
11767
+ const rows = id.matrix.length;
11768
+ if (rows === 0) return new PineMatrixObject(id.type, 0, 0, NaN, context);
11769
+ const cols = id.matrix[0].length;
11770
+ const newMatrix = new PineMatrixObject(id.type, cols, rows, NaN, context);
11771
+ for (let i = 0; i < rows; i++) {
11772
+ for (let j = 0; j < cols; j++) {
11773
+ newMatrix.matrix[j][i] = id.matrix[i][j];
11774
+ }
9901
11775
  }
9902
- return new PineArrayObject(id.array.map((x) => (x - mean) / stdev));
11776
+ return newMatrix;
9903
11777
  };
9904
11778
  }
9905
11779
 
9906
- function stdev$1(context) {
9907
- return (id, biased = true) => {
9908
- const mean = context.array.avg(id);
9909
- const deviations = id.array.map((x) => Math.pow(x - mean, 2));
9910
- const divisor = biased ? id.array.length : id.array.length - 1;
9911
- return Math.sqrt(context.array.sum(new PineArrayObject(deviations)) / divisor);
9912
- };
11780
+ var __defProp$9 = Object.defineProperty;
11781
+ var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11782
+ var __publicField$9 = (obj, key, value) => __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
11783
+ class PineMatrixObject {
11784
+ constructor(type, rows$1 = 0, cols = 0, initialValue = NaN, context) {
11785
+ this.type = type;
11786
+ this.context = context;
11787
+ __publicField$9(this, "matrix");
11788
+ __publicField$9(this, "_add_col");
11789
+ __publicField$9(this, "_add_row");
11790
+ __publicField$9(this, "_avg");
11791
+ __publicField$9(this, "_col");
11792
+ __publicField$9(this, "_columns");
11793
+ __publicField$9(this, "_concat");
11794
+ __publicField$9(this, "_copy");
11795
+ __publicField$9(this, "_det");
11796
+ __publicField$9(this, "_diff");
11797
+ __publicField$9(this, "_eigenvalues");
11798
+ __publicField$9(this, "_eigenvectors");
11799
+ __publicField$9(this, "_elements_count");
11800
+ __publicField$9(this, "_fill");
11801
+ __publicField$9(this, "_get");
11802
+ __publicField$9(this, "_inv");
11803
+ __publicField$9(this, "_is_antidiagonal");
11804
+ __publicField$9(this, "_is_antisymmetric");
11805
+ __publicField$9(this, "_is_binary");
11806
+ __publicField$9(this, "_is_diagonal");
11807
+ __publicField$9(this, "_is_identity");
11808
+ __publicField$9(this, "_is_square");
11809
+ __publicField$9(this, "_is_stochastic");
11810
+ __publicField$9(this, "_is_symmetric");
11811
+ __publicField$9(this, "_is_triangular");
11812
+ __publicField$9(this, "_is_zero");
11813
+ __publicField$9(this, "_kron");
11814
+ __publicField$9(this, "_max");
11815
+ __publicField$9(this, "_median");
11816
+ __publicField$9(this, "_min");
11817
+ __publicField$9(this, "_mode");
11818
+ __publicField$9(this, "_mult");
11819
+ __publicField$9(this, "_pinv");
11820
+ __publicField$9(this, "_pow");
11821
+ __publicField$9(this, "_rank");
11822
+ __publicField$9(this, "_remove_col");
11823
+ __publicField$9(this, "_remove_row");
11824
+ __publicField$9(this, "_reshape");
11825
+ __publicField$9(this, "_reverse");
11826
+ __publicField$9(this, "_row");
11827
+ __publicField$9(this, "_rows");
11828
+ __publicField$9(this, "_set");
11829
+ __publicField$9(this, "_sort");
11830
+ __publicField$9(this, "_submatrix");
11831
+ __publicField$9(this, "_sum");
11832
+ __publicField$9(this, "_swap_columns");
11833
+ __publicField$9(this, "_swap_rows");
11834
+ __publicField$9(this, "_trace");
11835
+ __publicField$9(this, "_transpose");
11836
+ this.matrix = [];
11837
+ if (rows$1 > 0 && cols > 0) {
11838
+ for (let i = 0; i < rows$1; i++) {
11839
+ this.matrix.push(Array(cols).fill(initialValue));
11840
+ }
11841
+ }
11842
+ this._add_col = add_col(this.context);
11843
+ this._add_row = add_row(this.context);
11844
+ this._avg = avg$1(this.context);
11845
+ this._col = col(this.context);
11846
+ this._columns = columns(this.context);
11847
+ this._concat = concat(this.context);
11848
+ this._copy = copy(this.context);
11849
+ this._det = det(this.context);
11850
+ this._diff = diff(this.context);
11851
+ this._eigenvalues = eigenvalues(this.context);
11852
+ this._eigenvectors = eigenvectors(this.context);
11853
+ this._elements_count = elements_count(this.context);
11854
+ this._fill = fill(this.context);
11855
+ this._get = get(this.context);
11856
+ this._inv = inv(this.context);
11857
+ this._is_antidiagonal = is_antidiagonal(this.context);
11858
+ this._is_antisymmetric = is_antisymmetric(this.context);
11859
+ this._is_binary = is_binary(this.context);
11860
+ this._is_diagonal = is_diagonal(this.context);
11861
+ this._is_identity = is_identity(this.context);
11862
+ this._is_square = is_square(this.context);
11863
+ this._is_stochastic = is_stochastic(this.context);
11864
+ this._is_symmetric = is_symmetric(this.context);
11865
+ this._is_triangular = is_triangular(this.context);
11866
+ this._is_zero = is_zero(this.context);
11867
+ this._kron = kron(this.context);
11868
+ this._max = max$1(this.context);
11869
+ this._median = median$1(this.context);
11870
+ this._min = min$1(this.context);
11871
+ this._mode = mode$1(this.context);
11872
+ this._mult = mult(this.context);
11873
+ this._pinv = pinv(this.context);
11874
+ this._pow = pow$1(this.context);
11875
+ this._rank = rank(this.context);
11876
+ this._remove_col = remove_col(this.context);
11877
+ this._remove_row = remove_row(this.context);
11878
+ this._reshape = reshape(this.context);
11879
+ this._reverse = reverse(this.context);
11880
+ this._row = row(this.context);
11881
+ this._rows = rows(this.context);
11882
+ this._set = set(this.context);
11883
+ this._sort = sort(this.context);
11884
+ this._submatrix = submatrix(this.context);
11885
+ this._sum = sum$1(this.context);
11886
+ this._swap_columns = swap_columns(this.context);
11887
+ this._swap_rows = swap_rows(this.context);
11888
+ this._trace = trace(this.context);
11889
+ this._transpose = transpose(this.context);
11890
+ }
11891
+ toString() {
11892
+ let result = "";
11893
+ for (let i = 0; i < this.matrix.length; i++) {
11894
+ result += result === "" ? "" : "\n";
11895
+ result += "[" + this.matrix[i].join(", ") + "]";
11896
+ }
11897
+ return result;
11898
+ }
11899
+ add_col(...args) {
11900
+ return this._add_col(this, ...args);
11901
+ }
11902
+ add_row(...args) {
11903
+ return this._add_row(this, ...args);
11904
+ }
11905
+ avg(...args) {
11906
+ return this._avg(this, ...args);
11907
+ }
11908
+ col(...args) {
11909
+ return this._col(this, ...args);
11910
+ }
11911
+ columns(...args) {
11912
+ return this._columns(this, ...args);
11913
+ }
11914
+ concat(...args) {
11915
+ return this._concat(this, ...args);
11916
+ }
11917
+ copy(...args) {
11918
+ return this._copy(this, ...args);
11919
+ }
11920
+ det(...args) {
11921
+ return this._det(this, ...args);
11922
+ }
11923
+ diff(...args) {
11924
+ return this._diff(this, ...args);
11925
+ }
11926
+ eigenvalues(...args) {
11927
+ return this._eigenvalues(this, ...args);
11928
+ }
11929
+ eigenvectors(...args) {
11930
+ return this._eigenvectors(this, ...args);
11931
+ }
11932
+ elements_count(...args) {
11933
+ return this._elements_count(this, ...args);
11934
+ }
11935
+ fill(...args) {
11936
+ return this._fill(this, ...args);
11937
+ }
11938
+ get(...args) {
11939
+ return this._get(this, ...args);
11940
+ }
11941
+ inv(...args) {
11942
+ return this._inv(this, ...args);
11943
+ }
11944
+ is_antidiagonal(...args) {
11945
+ return this._is_antidiagonal(this, ...args);
11946
+ }
11947
+ is_antisymmetric(...args) {
11948
+ return this._is_antisymmetric(this, ...args);
11949
+ }
11950
+ is_binary(...args) {
11951
+ return this._is_binary(this, ...args);
11952
+ }
11953
+ is_diagonal(...args) {
11954
+ return this._is_diagonal(this, ...args);
11955
+ }
11956
+ is_identity(...args) {
11957
+ return this._is_identity(this, ...args);
11958
+ }
11959
+ is_square(...args) {
11960
+ return this._is_square(this, ...args);
11961
+ }
11962
+ is_stochastic(...args) {
11963
+ return this._is_stochastic(this, ...args);
11964
+ }
11965
+ is_symmetric(...args) {
11966
+ return this._is_symmetric(this, ...args);
11967
+ }
11968
+ is_triangular(...args) {
11969
+ return this._is_triangular(this, ...args);
11970
+ }
11971
+ is_zero(...args) {
11972
+ return this._is_zero(this, ...args);
11973
+ }
11974
+ kron(...args) {
11975
+ return this._kron(this, ...args);
11976
+ }
11977
+ max(...args) {
11978
+ return this._max(this, ...args);
11979
+ }
11980
+ median(...args) {
11981
+ return this._median(this, ...args);
11982
+ }
11983
+ min(...args) {
11984
+ return this._min(this, ...args);
11985
+ }
11986
+ mode(...args) {
11987
+ return this._mode(this, ...args);
11988
+ }
11989
+ mult(...args) {
11990
+ return this._mult(this, ...args);
11991
+ }
11992
+ pinv(...args) {
11993
+ return this._pinv(this, ...args);
11994
+ }
11995
+ pow(...args) {
11996
+ return this._pow(this, ...args);
11997
+ }
11998
+ rank(...args) {
11999
+ return this._rank(this, ...args);
12000
+ }
12001
+ remove_col(...args) {
12002
+ return this._remove_col(this, ...args);
12003
+ }
12004
+ remove_row(...args) {
12005
+ return this._remove_row(this, ...args);
12006
+ }
12007
+ reshape(...args) {
12008
+ return this._reshape(this, ...args);
12009
+ }
12010
+ reverse(...args) {
12011
+ return this._reverse(this, ...args);
12012
+ }
12013
+ row(...args) {
12014
+ return this._row(this, ...args);
12015
+ }
12016
+ rows(...args) {
12017
+ return this._rows(this, ...args);
12018
+ }
12019
+ set(...args) {
12020
+ return this._set(this, ...args);
12021
+ }
12022
+ sort(...args) {
12023
+ return this._sort(this, ...args);
12024
+ }
12025
+ submatrix(...args) {
12026
+ return this._submatrix(this, ...args);
12027
+ }
12028
+ sum(...args) {
12029
+ return this._sum(this, ...args);
12030
+ }
12031
+ swap_columns(...args) {
12032
+ return this._swap_columns(this, ...args);
12033
+ }
12034
+ swap_rows(...args) {
12035
+ return this._swap_rows(this, ...args);
12036
+ }
12037
+ trace(...args) {
12038
+ return this._trace(this, ...args);
12039
+ }
12040
+ transpose(...args) {
12041
+ return this._transpose(this, ...args);
12042
+ }
9913
12043
  }
9914
12044
 
9915
- function sum$1(context) {
9916
- return (id) => {
9917
- return id.array.reduce((a, b) => a + (isNaN(b) ? 0 : b), 0);
12045
+ function new_fn(context) {
12046
+ return (type, rows, cols, initial_value) => {
12047
+ return new PineMatrixObject(type, rows, cols, initial_value, context);
9918
12048
  };
9919
12049
  }
9920
12050
 
9921
- function unshift(context) {
9922
- return (id, value) => {
9923
- id.array.unshift(value);
12051
+ function param$4(context) {
12052
+ return (source, index = 0) => {
12053
+ return Series.from(source).get(index);
9924
12054
  };
9925
12055
  }
9926
12056
 
9927
- function variance$1(context) {
9928
- return (id, biased = true) => {
9929
- const mean = context.array.avg(id);
9930
- const deviations = id.array.map((x) => Math.pow(x - mean, 2));
9931
- const divisor = biased ? id.array.length : id.array.length - 1;
9932
- return context.array.sum(new PineArrayObject(deviations)) / divisor;
9933
- };
12057
+ class PineMatrix {
12058
+ constructor(context) {
12059
+ this.context = context;
12060
+ this.add_col = (id, ...args) => id.add_col(...args);
12061
+ this.add_row = (id, ...args) => id.add_row(...args);
12062
+ this.avg = (id, ...args) => id.avg(...args);
12063
+ this.col = (id, ...args) => id.col(...args);
12064
+ this.columns = (id, ...args) => id.columns(...args);
12065
+ this.concat = (id, ...args) => id.concat(...args);
12066
+ this.copy = (id, ...args) => id.copy(...args);
12067
+ this.det = (id, ...args) => id.det(...args);
12068
+ this.diff = (id, ...args) => id.diff(...args);
12069
+ this.eigenvalues = (id, ...args) => id.eigenvalues(...args);
12070
+ this.eigenvectors = (id, ...args) => id.eigenvectors(...args);
12071
+ this.elements_count = (id, ...args) => id.elements_count(...args);
12072
+ this.fill = (id, ...args) => id.fill(...args);
12073
+ this.get = (id, ...args) => id.get(...args);
12074
+ this.inv = (id, ...args) => id.inv(...args);
12075
+ this.is_antidiagonal = (id, ...args) => id.is_antidiagonal(...args);
12076
+ this.is_antisymmetric = (id, ...args) => id.is_antisymmetric(...args);
12077
+ this.is_binary = (id, ...args) => id.is_binary(...args);
12078
+ this.is_diagonal = (id, ...args) => id.is_diagonal(...args);
12079
+ this.is_identity = (id, ...args) => id.is_identity(...args);
12080
+ this.is_square = (id, ...args) => id.is_square(...args);
12081
+ this.is_stochastic = (id, ...args) => id.is_stochastic(...args);
12082
+ this.is_symmetric = (id, ...args) => id.is_symmetric(...args);
12083
+ this.is_triangular = (id, ...args) => id.is_triangular(...args);
12084
+ this.is_zero = (id, ...args) => id.is_zero(...args);
12085
+ this.kron = (id, ...args) => id.kron(...args);
12086
+ this.max = (id, ...args) => id.max(...args);
12087
+ this.median = (id, ...args) => id.median(...args);
12088
+ this.min = (id, ...args) => id.min(...args);
12089
+ this.mode = (id, ...args) => id.mode(...args);
12090
+ this.mult = (id, ...args) => id.mult(...args);
12091
+ this.new = new_fn(context);
12092
+ this.param = param$4();
12093
+ this.pinv = (id, ...args) => id.pinv(...args);
12094
+ this.pow = (id, ...args) => id.pow(...args);
12095
+ this.rank = (id, ...args) => id.rank(...args);
12096
+ this.remove_col = (id, ...args) => id.remove_col(...args);
12097
+ this.remove_row = (id, ...args) => id.remove_row(...args);
12098
+ this.reshape = (id, ...args) => id.reshape(...args);
12099
+ this.reverse = (id, ...args) => id.reverse(...args);
12100
+ this.row = (id, ...args) => id.row(...args);
12101
+ this.rows = (id, ...args) => id.rows(...args);
12102
+ this.set = (id, ...args) => id.set(...args);
12103
+ this.sort = (id, ...args) => id.sort(...args);
12104
+ this.submatrix = (id, ...args) => id.submatrix(...args);
12105
+ this.sum = (id, ...args) => id.sum(...args);
12106
+ this.swap_columns = (id, ...args) => id.swap_columns(...args);
12107
+ this.swap_rows = (id, ...args) => id.swap_rows(...args);
12108
+ this.trace = (id, ...args) => id.trace(...args);
12109
+ this.transpose = (id, ...args) => id.transpose(...args);
12110
+ }
9934
12111
  }
9935
12112
 
9936
12113
  var __defProp$8 = Object.defineProperty;
9937
12114
  var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9938
12115
  var __publicField$8 = (obj, key, value) => __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
9939
- const methods$4 = {
9940
- abs: abs$1,
9941
- avg: avg$1,
9942
- clear,
9943
- concat,
9944
- copy,
9945
- covariance,
9946
- every,
9947
- fill,
9948
- first,
9949
- from,
9950
- get,
9951
- includes,
9952
- indexof,
9953
- insert,
9954
- join,
9955
- last,
9956
- lastindexof,
9957
- max: max$1,
9958
- min: min$1,
9959
- new: new_fn,
9960
- new_bool,
9961
- new_float,
9962
- new_int,
9963
- new_string,
9964
- param: param$4,
9965
- pop,
9966
- push,
9967
- range: range$1,
9968
- remove,
9969
- reverse,
9970
- set,
9971
- shift,
9972
- size,
9973
- slice,
9974
- some,
9975
- sort,
9976
- sort_indices,
9977
- standardize,
9978
- stdev: stdev$1,
9979
- sum: sum$1,
9980
- unshift,
9981
- variance: variance$1
9982
- };
9983
- class PineArray {
12116
+ class Barstate {
9984
12117
  constructor(context) {
9985
12118
  this.context = context;
9986
- __publicField$8(this, "_cache", {});
9987
- __publicField$8(this, "abs");
9988
- __publicField$8(this, "avg");
9989
- __publicField$8(this, "clear");
9990
- __publicField$8(this, "concat");
9991
- __publicField$8(this, "copy");
9992
- __publicField$8(this, "covariance");
9993
- __publicField$8(this, "every");
9994
- __publicField$8(this, "fill");
9995
- __publicField$8(this, "first");
9996
- __publicField$8(this, "from");
9997
- __publicField$8(this, "get");
9998
- __publicField$8(this, "includes");
9999
- __publicField$8(this, "indexof");
10000
- __publicField$8(this, "insert");
10001
- __publicField$8(this, "join");
10002
- __publicField$8(this, "last");
10003
- __publicField$8(this, "lastindexof");
10004
- __publicField$8(this, "max");
10005
- __publicField$8(this, "min");
10006
- __publicField$8(this, "new");
10007
- __publicField$8(this, "new_bool");
10008
- __publicField$8(this, "new_float");
10009
- __publicField$8(this, "new_int");
10010
- __publicField$8(this, "new_string");
10011
- __publicField$8(this, "param");
10012
- __publicField$8(this, "pop");
10013
- __publicField$8(this, "push");
10014
- __publicField$8(this, "range");
10015
- __publicField$8(this, "remove");
10016
- __publicField$8(this, "reverse");
10017
- __publicField$8(this, "set");
10018
- __publicField$8(this, "shift");
10019
- __publicField$8(this, "size");
10020
- __publicField$8(this, "slice");
10021
- __publicField$8(this, "some");
10022
- __publicField$8(this, "sort");
10023
- __publicField$8(this, "sort_indices");
10024
- __publicField$8(this, "standardize");
10025
- __publicField$8(this, "stdev");
10026
- __publicField$8(this, "sum");
10027
- __publicField$8(this, "unshift");
10028
- __publicField$8(this, "variance");
10029
- Object.entries(methods$4).forEach(([name, factory]) => {
10030
- this[name] = factory(context);
10031
- });
12119
+ __publicField$8(this, "_live", false);
12120
+ }
12121
+ setLive() {
12122
+ this._live = true;
12123
+ }
12124
+ get isnew() {
12125
+ return !this._live;
12126
+ }
12127
+ get islast() {
12128
+ return this.context.idx === this.context.data.close.data.length - 1;
12129
+ }
12130
+ get isfirst() {
12131
+ return this.context.idx === 0;
12132
+ }
12133
+ get ishistory() {
12134
+ return this.context.idx < this.context.data.close.data.length - 1;
12135
+ }
12136
+ get isrealtime() {
12137
+ return this.context.idx === this.context.data.close.data.length - 1;
12138
+ }
12139
+ get isconfirmed() {
12140
+ return this.context.data.closeTime[this.context.data.closeTime.length - 1] <= (/* @__PURE__ */ new Date()).getTime();
12141
+ }
12142
+ get islastconfirmedhistory() {
12143
+ return this.context.data.closeTime[this.context.data.closeTime.length - 1] <= (/* @__PURE__ */ new Date()).getTime();
10032
12144
  }
10033
12145
  }
10034
12146
 
@@ -10060,7 +12172,22 @@ ${code}
10060
12172
  maroon: "maroon",
10061
12173
  black: "black",
10062
12174
  gray: "gray",
10063
- blue: "blue"
12175
+ blue: "blue",
12176
+ yellow: "yellow",
12177
+ orange: "orange",
12178
+ purple: "purple",
12179
+ pink: "pink",
12180
+ brown: "brown",
12181
+ teal: "teal",
12182
+ cyan: "cyan",
12183
+ navy: "navy",
12184
+ indigo: "indigo",
12185
+ violet: "violet",
12186
+ magenta: "magenta",
12187
+ rose: "rose",
12188
+ gold: "gold",
12189
+ silver: "silver",
12190
+ bronze: "bronze"
10064
12191
  });
10065
12192
  }
10066
12193
  extractPlotOptions(options) {
@@ -10108,94 +12235,155 @@ ${code}
10108
12235
  }
10109
12236
  }
10110
12237
 
12238
+ const INPUT_ARGS_OPTIONS = [
12239
+ ["defval", "title", "tooltip", "inline", "group", "display"],
12240
+ ["defval", "title", "tooltip", "group", "confirm", "display"],
12241
+ ["defval", "title", "tooltip", "inline", "group", "confirm", "display"],
12242
+ ["defval", "title", "options", "tooltip", "inline", "group", "confirm", "display"],
12243
+ ["defval", "title", "minval", "maxval", "step", "tooltip", "inline", "group", "confirm", "display"]
12244
+ ];
12245
+ const INPUT_ARGS_TYPE_CHECK = {
12246
+ defval: (arg) => typeof arg !== "object",
12247
+ title: (arg) => typeof arg === "string",
12248
+ tooltip: (arg) => typeof arg === "string",
12249
+ inline: (arg) => typeof arg === "string",
12250
+ group: (arg) => typeof arg === "string",
12251
+ display: (arg) => typeof arg === "string",
12252
+ confirm: (arg) => typeof arg === "boolean",
12253
+ options: (arg) => Array.isArray(arg),
12254
+ minval: (arg) => typeof arg === "number",
12255
+ maxval: (arg) => typeof arg === "number",
12256
+ step: (arg) => typeof arg === "number"
12257
+ };
12258
+ function parseInputOptions(args) {
12259
+ const options = {};
12260
+ let options_arg = {};
12261
+ const valid = new Array(INPUT_ARGS_OPTIONS.length).fill(true);
12262
+ for (let i = 0; i < args.length; i++) {
12263
+ const arg = args[i];
12264
+ if (typeof arg === "object") {
12265
+ options_arg = arg;
12266
+ break;
12267
+ }
12268
+ const curOptions = INPUT_ARGS_OPTIONS.map((e, idx) => valid[idx] ? e[i] : void 0);
12269
+ for (let o = 0; o < curOptions.length; o++) {
12270
+ const optionName = curOptions[o];
12271
+ if (optionName === void 0) {
12272
+ valid[o] = false;
12273
+ continue;
12274
+ }
12275
+ if (INPUT_ARGS_TYPE_CHECK[optionName](arg)) {
12276
+ options[optionName] = arg;
12277
+ } else {
12278
+ valid[o] = false;
12279
+ }
12280
+ }
12281
+ }
12282
+ return { ...options_arg, ...options };
12283
+ }
12284
+
10111
12285
  function any(context) {
10112
- return (value, { title, group } = {}) => {
10113
- return Array.isArray(value) ? value[0] : value;
12286
+ return (...args) => {
12287
+ const options = parseInputOptions(args);
12288
+ return options.defval;
10114
12289
  };
10115
12290
  }
10116
12291
 
10117
12292
  function bool(context) {
10118
- return (value, { title, group } = {}) => {
10119
- return Array.isArray(value) ? value[0] : value;
12293
+ return (...args) => {
12294
+ const options = parseInputOptions(args);
12295
+ return options.defval;
10120
12296
  };
10121
12297
  }
10122
12298
 
10123
12299
  function color(context) {
10124
- return (value, { title, group } = {}) => {
10125
- return Array.isArray(value) ? value[0] : value;
12300
+ return (...args) => {
12301
+ const options = parseInputOptions(args);
12302
+ return options.defval;
10126
12303
  };
10127
12304
  }
10128
12305
 
10129
12306
  function enum_fn(context) {
10130
- return (value, { title, group } = {}) => {
10131
- return Array.isArray(value) ? value[0] : value;
12307
+ return (...args) => {
12308
+ const options = parseInputOptions(args);
12309
+ return options.defval;
10132
12310
  };
10133
12311
  }
10134
12312
 
10135
12313
  function float(context) {
10136
- return (value, { title, group } = {}) => {
10137
- return Array.isArray(value) ? value[0] : value;
12314
+ return (...args) => {
12315
+ const options = parseInputOptions(args);
12316
+ return options.defval;
10138
12317
  };
10139
12318
  }
10140
12319
 
10141
12320
  function int(context) {
10142
- return (value, { title, group } = {}) => {
10143
- return Array.isArray(value) ? value[0] : value;
12321
+ return (...args) => {
12322
+ const options = parseInputOptions(args);
12323
+ return options.defval;
10144
12324
  };
10145
12325
  }
10146
12326
 
10147
12327
  function param$3(context) {
10148
12328
  return (source, index = 0) => {
10149
12329
  const val = Series.from(source).get(index);
10150
- return [val];
12330
+ return val;
10151
12331
  };
10152
12332
  }
10153
12333
 
10154
12334
  function price(context) {
10155
- return (value, { title, group } = {}) => {
10156
- return Array.isArray(value) ? value[0] : value;
12335
+ return (...args) => {
12336
+ const options = parseInputOptions(args);
12337
+ return options.defval;
10157
12338
  };
10158
12339
  }
10159
12340
 
10160
12341
  function session(context) {
10161
- return (value, { title, group } = {}) => {
10162
- return Array.isArray(value) ? value[0] : value;
12342
+ return (...args) => {
12343
+ const options = parseInputOptions(args);
12344
+ return options.defval;
10163
12345
  };
10164
12346
  }
10165
12347
 
10166
12348
  function source(context) {
10167
- return (value, { title, group } = {}) => {
10168
- return Array.isArray(value) ? value[0] : value;
12349
+ return (...args) => {
12350
+ const options = parseInputOptions(args);
12351
+ return options.defval;
10169
12352
  };
10170
12353
  }
10171
12354
 
10172
12355
  function string(context) {
10173
- return (value, { title, group } = {}) => {
10174
- return Array.isArray(value) ? value[0] : value;
12356
+ return (...args) => {
12357
+ const options = parseInputOptions(args);
12358
+ return options.defval;
10175
12359
  };
10176
12360
  }
10177
12361
 
10178
12362
  function symbol(context) {
10179
- return (value, { title, group } = {}) => {
10180
- return Array.isArray(value) ? value[0] : value;
12363
+ return (...args) => {
12364
+ const options = parseInputOptions(args);
12365
+ return options.defval;
10181
12366
  };
10182
12367
  }
10183
12368
 
10184
12369
  function text_area(context) {
10185
- return (value, { title, group } = {}) => {
10186
- return Array.isArray(value) ? value[0] : value;
12370
+ return (...args) => {
12371
+ const options = parseInputOptions(args);
12372
+ return options.defval;
10187
12373
  };
10188
12374
  }
10189
12375
 
10190
12376
  function time(context) {
10191
- return (value, { title, group } = {}) => {
10192
- return Array.isArray(value) ? value[0] : value;
12377
+ return (...args) => {
12378
+ const options = parseInputOptions(args);
12379
+ return options.defval;
10193
12380
  };
10194
12381
  }
10195
12382
 
10196
12383
  function timeframe(context) {
10197
- return (value, { title, group } = {}) => {
10198
- return Array.isArray(value) ? value[0] : value;
12384
+ return (...args) => {
12385
+ const options = parseInputOptions(args);
12386
+ return options.defval;
10199
12387
  };
10200
12388
  }
10201
12389
 
@@ -10289,6 +12477,12 @@ ${code}
10289
12477
  };
10290
12478
  }
10291
12479
 
12480
+ function e(context) {
12481
+ return () => {
12482
+ return Math.E;
12483
+ };
12484
+ }
12485
+
10292
12486
  function exp(context) {
10293
12487
  return (source) => {
10294
12488
  return Math.exp(Series.from(source).get(0));
@@ -10357,6 +12551,18 @@ ${code}
10357
12551
  };
10358
12552
  }
10359
12553
 
12554
+ function phi(context) {
12555
+ return () => {
12556
+ return 1.618033988749895;
12557
+ };
12558
+ }
12559
+
12560
+ function pi(context) {
12561
+ return () => {
12562
+ return Math.PI;
12563
+ };
12564
+ }
12565
+
10360
12566
  function pow(context) {
10361
12567
  return (source, power) => {
10362
12568
  return Math.pow(Series.from(source).get(0), Series.from(power).get(0));
@@ -10375,6 +12581,24 @@ ${code}
10375
12581
  };
10376
12582
  }
10377
12583
 
12584
+ function round_to_mintick(context) {
12585
+ return (source) => {
12586
+ return context.precision(Math.round(Series.from(source).get(0) / context.pine.syminfo.mintick) * context.pine.syminfo.mintick);
12587
+ };
12588
+ }
12589
+
12590
+ function rphi(context) {
12591
+ return () => {
12592
+ return 0.6180339887498948;
12593
+ };
12594
+ }
12595
+
12596
+ function sign(context) {
12597
+ return (source) => {
12598
+ return Math.sign(Series.from(source).get(0));
12599
+ };
12600
+ }
12601
+
10378
12602
  function sin(context) {
10379
12603
  return (source) => {
10380
12604
  return Math.sin(Series.from(source).get(0));
@@ -10427,6 +12651,7 @@ ${code}
10427
12651
  avg,
10428
12652
  ceil,
10429
12653
  cos,
12654
+ e,
10430
12655
  exp,
10431
12656
  floor,
10432
12657
  ln,
@@ -10435,9 +12660,14 @@ ${code}
10435
12660
  max,
10436
12661
  min,
10437
12662
  param: param$2,
12663
+ phi,
12664
+ pi,
10438
12665
  pow,
10439
12666
  random,
10440
12667
  round,
12668
+ round_to_mintick,
12669
+ rphi,
12670
+ sign,
10441
12671
  sin,
10442
12672
  sqrt,
10443
12673
  sum,
@@ -10455,6 +12685,7 @@ ${code}
10455
12685
  __publicField$5(this, "avg");
10456
12686
  __publicField$5(this, "ceil");
10457
12687
  __publicField$5(this, "cos");
12688
+ __publicField$5(this, "e");
10458
12689
  __publicField$5(this, "exp");
10459
12690
  __publicField$5(this, "floor");
10460
12691
  __publicField$5(this, "ln");
@@ -10463,9 +12694,14 @@ ${code}
10463
12694
  __publicField$5(this, "max");
10464
12695
  __publicField$5(this, "min");
10465
12696
  __publicField$5(this, "param");
12697
+ __publicField$5(this, "phi");
12698
+ __publicField$5(this, "pi");
10466
12699
  __publicField$5(this, "pow");
10467
12700
  __publicField$5(this, "random");
10468
12701
  __publicField$5(this, "round");
12702
+ __publicField$5(this, "round_to_mintick");
12703
+ __publicField$5(this, "rphi");
12704
+ __publicField$5(this, "sign");
10469
12705
  __publicField$5(this, "sin");
10470
12706
  __publicField$5(this, "sqrt");
10471
12707
  __publicField$5(this, "sum");
@@ -10632,12 +12868,68 @@ ${code}
10632
12868
  };
10633
12869
  }
10634
12870
 
12871
+ function security_lower_tf(context) {
12872
+ return async (symbol, timeframe, expression, ignore_invalid_symbol = false, currency = null, ignore_invalid_timeframe = false, calc_bars_count = 0) => {
12873
+ const _symbol = symbol[0];
12874
+ const _timeframe = timeframe[0];
12875
+ const _expression = expression[0];
12876
+ const _expression_name = expression[1];
12877
+ Array.isArray(ignore_invalid_symbol) ? ignore_invalid_symbol[0] : ignore_invalid_symbol;
12878
+ const _ignore_invalid_timeframe = Array.isArray(ignore_invalid_timeframe) ? ignore_invalid_timeframe[0] : ignore_invalid_timeframe;
12879
+ if (context.isSecondaryContext) {
12880
+ return Array.isArray(_expression) ? [_expression] : _expression;
12881
+ }
12882
+ const ctxTimeframeIdx = TIMEFRAMES.indexOf(context.timeframe);
12883
+ const reqTimeframeIdx = TIMEFRAMES.indexOf(_timeframe);
12884
+ if (ctxTimeframeIdx === -1 || reqTimeframeIdx === -1) {
12885
+ if (_ignore_invalid_timeframe) return NaN;
12886
+ throw new Error("Invalid timeframe");
12887
+ }
12888
+ if (reqTimeframeIdx > ctxTimeframeIdx) {
12889
+ if (_ignore_invalid_timeframe) return NaN;
12890
+ throw new Error(`Timeframe ${_timeframe} is not lower than or equal to chart timeframe ${context.timeframe}`);
12891
+ }
12892
+ if (reqTimeframeIdx === ctxTimeframeIdx) {
12893
+ return [[_expression]];
12894
+ }
12895
+ const cacheKey = `${_symbol}_${_timeframe}_${_expression_name}_lower`;
12896
+ if (!context.cache[cacheKey]) {
12897
+ const buffer = 1e3 * 60 * 60 * 24 * 30;
12898
+ const adjustedSDate = context.sDate ? context.sDate - buffer : void 0;
12899
+ const limit = context.sDate && context.eDate ? void 0 : context.limit || 1e3;
12900
+ const pineTS = new PineTS(context.source, _symbol, _timeframe, limit, adjustedSDate, context.eDate);
12901
+ pineTS.markAsSecondary();
12902
+ const secContext2 = await pineTS.run(context.pineTSCode);
12903
+ context.cache[cacheKey] = secContext2;
12904
+ }
12905
+ const secContext = context.cache[cacheKey];
12906
+ const myOpenTime = Series.from(context.data.openTime).get(0);
12907
+ const myCloseTime = Series.from(context.data.closeTime).get(0);
12908
+ const secOpenTimes = secContext.data.openTime.data;
12909
+ const secCloseTimes = secContext.data.closeTime.data;
12910
+ const secValues = secContext.params[_expression_name];
12911
+ if (!secValues) return [];
12912
+ const result = [];
12913
+ for (let i = 0; i < secOpenTimes.length; i++) {
12914
+ const sOpen = secOpenTimes[i];
12915
+ const sClose = secCloseTimes[i];
12916
+ if (sClose <= myOpenTime) continue;
12917
+ if (sOpen >= myCloseTime) break;
12918
+ if (sOpen >= myOpenTime && sOpen < myCloseTime) {
12919
+ result.push(secValues[i]);
12920
+ }
12921
+ }
12922
+ return [result];
12923
+ };
12924
+ }
12925
+
10635
12926
  var __defProp$4 = Object.defineProperty;
10636
12927
  var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10637
12928
  var __publicField$4 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
10638
12929
  const methods$1 = {
10639
12930
  param: param$1,
10640
- security
12931
+ security,
12932
+ security_lower_tf
10641
12933
  };
10642
12934
  class PineRequest {
10643
12935
  constructor(context) {
@@ -10645,6 +12937,7 @@ ${code}
10645
12937
  __publicField$4(this, "_cache", {});
10646
12938
  __publicField$4(this, "param");
10647
12939
  __publicField$4(this, "security");
12940
+ __publicField$4(this, "security_lower_tf");
10648
12941
  Object.entries(methods$1).forEach(([name, factory]) => {
10649
12942
  this[name] = factory(context);
10650
12943
  });
@@ -12447,10 +14740,10 @@ ${code}
12447
14740
  const low0 = context.get(context.data.low, 0);
12448
14741
  const close1 = context.get(context.data.close, 1);
12449
14742
  if (isNaN(close1)) {
12450
- return handleNa ? high0 - low0 : NaN;
14743
+ return handleNa ? context.precision(high0 - low0) : NaN;
12451
14744
  }
12452
14745
  const val = Math.max(high0 - low0, Math.abs(high0 - close1), Math.abs(low0 - close1));
12453
- return val;
14746
+ return context.precision(val);
12454
14747
  };
12455
14748
  }
12456
14749
 
@@ -12942,6 +15235,189 @@ ${code}
12942
15235
  }
12943
15236
  }
12944
15237
 
15238
+ class Log {
15239
+ constructor(context) {
15240
+ this.context = context;
15241
+ }
15242
+ logFormat(message, ...args) {
15243
+ return message.replace(/{(\d+)}/g, (match, index) => args[index]);
15244
+ }
15245
+ param(source, index = 0, name) {
15246
+ return Series.from(source).get(index);
15247
+ }
15248
+ warning(message, ...args) {
15249
+ console.warn(this.logFormat(message, ...args));
15250
+ }
15251
+ error(message, ...args) {
15252
+ console.error(this.logFormat(message, ...args));
15253
+ }
15254
+ info(message, ...args) {
15255
+ console.log(this.logFormat(message, ...args));
15256
+ }
15257
+ }
15258
+
15259
+ class Str {
15260
+ constructor(context) {
15261
+ this.context = context;
15262
+ }
15263
+ param(source, index = 0, name) {
15264
+ return Series.from(source).get(index);
15265
+ }
15266
+ tostring(value) {
15267
+ return String(value);
15268
+ }
15269
+ tonumber(value) {
15270
+ return Number(value);
15271
+ }
15272
+ lower(value) {
15273
+ return String(value).toLowerCase();
15274
+ }
15275
+ upper(value) {
15276
+ return String(value).toUpperCase();
15277
+ }
15278
+ trim(value) {
15279
+ return String(value).trim();
15280
+ }
15281
+ repeat(source, repeat, separator = "") {
15282
+ return Array(repeat).fill(source).join(separator || "");
15283
+ }
15284
+ replace_all(source, target, replacement) {
15285
+ return String(source).replaceAll(target, replacement);
15286
+ }
15287
+ //occurense is the nth occurrence to replace
15288
+ replace(source, target, replacement, occurrence = 0) {
15289
+ const str = String(source);
15290
+ const tgt = String(target);
15291
+ const repl = String(replacement);
15292
+ const occ = Math.floor(Number(occurrence)) || 0;
15293
+ if (tgt === "") return str;
15294
+ let pos = 0;
15295
+ let found = 0;
15296
+ while (true) {
15297
+ const idx = str.indexOf(tgt, pos);
15298
+ if (idx === -1) return str;
15299
+ if (found === occ) {
15300
+ return str.substring(0, idx) + repl + str.substring(idx + tgt.length);
15301
+ }
15302
+ found++;
15303
+ pos = idx + tgt.length;
15304
+ }
15305
+ }
15306
+ contains(source, target) {
15307
+ return String(source).includes(target);
15308
+ }
15309
+ endswith(source, target) {
15310
+ return String(source).endsWith(target);
15311
+ }
15312
+ startswith(source, target) {
15313
+ return String(source).startsWith(target);
15314
+ }
15315
+ pos(source, target) {
15316
+ const idx = String(source).indexOf(target);
15317
+ return idx === -1 ? NaN : idx;
15318
+ }
15319
+ length(source) {
15320
+ return String(source).length;
15321
+ }
15322
+ match(source, pattern) {
15323
+ return String(source).match(new RegExp(pattern));
15324
+ }
15325
+ split(source, separator) {
15326
+ return [String(source).split(separator)];
15327
+ }
15328
+ substring(source, begin_pos, end_pos) {
15329
+ return String(source).substring(begin_pos, end_pos);
15330
+ }
15331
+ format(message, ...args) {
15332
+ return message.replace(/{(\d+)}/g, (match, index) => args[index]);
15333
+ }
15334
+ }
15335
+
15336
+ class Timeframe {
15337
+ constructor(context) {
15338
+ this.context = context;
15339
+ }
15340
+ param(source, index = 0, name) {
15341
+ return Series.from(source).get(index);
15342
+ }
15343
+ //Note : current PineTS implementation does not differentiate between main_period and period because the timeframe is always taken from the main execution context.
15344
+ //once we implement indicator() function, the main_period can be overridden by the indicator's timeframe.
15345
+ get main_period() {
15346
+ return this.context.timeframe;
15347
+ }
15348
+ get period() {
15349
+ return this.context.timeframe;
15350
+ }
15351
+ get multiplier() {
15352
+ const val = parseInt(this.context.timeframe);
15353
+ return isNaN(val) ? 1 : val;
15354
+ }
15355
+ get isdwm() {
15356
+ return ["D", "W", "M"].includes(this.context.timeframe.slice(-1));
15357
+ }
15358
+ get isdaily() {
15359
+ return this.context.timeframe.slice(-1) === "D";
15360
+ }
15361
+ get isweekly() {
15362
+ return this.context.timeframe.slice(-1) === "W";
15363
+ }
15364
+ get ismonthly() {
15365
+ return this.context.timeframe.slice(-1) === "M";
15366
+ }
15367
+ get isseconds() {
15368
+ return this.context.timeframe.slice(-1) === "S";
15369
+ }
15370
+ get isminutes() {
15371
+ return parseInt(this.context.timeframe).toString() == this.context.timeframe.trim();
15372
+ }
15373
+ get isintraday() {
15374
+ return !this.isdwm;
15375
+ }
15376
+ // public change(timeframe: string) {
15377
+ // const prevOpenTime = this.context.data.openTime.get(this.context.data.openTime.length - 2);
15378
+ // const currentOpenTime = this.context.data.openTime.get(this.context.data.openTime.length - 1);
15379
+ // }
15380
+ from_seconds(seconds) {
15381
+ if (seconds < 60) {
15382
+ const roundedSeconds = Math.ceil(seconds / 5) * 5;
15383
+ return roundedSeconds + "S";
15384
+ }
15385
+ if (seconds < 60 * 60 * 24) {
15386
+ const roundedMinutes = Math.ceil(seconds / 60);
15387
+ return roundedMinutes;
15388
+ }
15389
+ if (seconds <= 60 * 60 * 24 * 7 * 52) {
15390
+ if (seconds % (60 * 60 * 24 * 7) === 0) {
15391
+ const roundedWeeks = Math.ceil(seconds / (60 * 60 * 24 * 7));
15392
+ return roundedWeeks + "W";
15393
+ }
15394
+ const roundedHours = Math.ceil(seconds / (60 * 60 * 24));
15395
+ return roundedHours + "D";
15396
+ }
15397
+ return "12M";
15398
+ }
15399
+ in_seconds(timeframe) {
15400
+ const multiplier = parseInt(timeframe);
15401
+ const unit = timeframe.slice(-1);
15402
+ if (unit === "S") {
15403
+ return multiplier;
15404
+ }
15405
+ if (unit === "D") {
15406
+ return multiplier * 60 * 60 * 24;
15407
+ }
15408
+ if (unit === "W") {
15409
+ return multiplier * 60 * 60 * 24 * 7;
15410
+ }
15411
+ if (unit === "M") {
15412
+ return multiplier * 60 * 60 * 24 * 30;
15413
+ }
15414
+ if (!isNaN(multiplier)) {
15415
+ return multiplier * 60;
15416
+ }
15417
+ return 0;
15418
+ }
15419
+ }
15420
+
12945
15421
  var __defProp$2 = Object.defineProperty;
12946
15422
  var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12947
15423
  var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -12953,7 +15429,8 @@ ${code}
12953
15429
  timeframe,
12954
15430
  limit,
12955
15431
  sDate,
12956
- eDate
15432
+ eDate,
15433
+ fullContext
12957
15434
  }) {
12958
15435
  __publicField$2(this, "data", {
12959
15436
  open: new Series([]),
@@ -12963,7 +15440,8 @@ ${code}
12963
15440
  volume: new Series([]),
12964
15441
  hl2: new Series([]),
12965
15442
  hlc3: new Series([]),
12966
- ohlc4: new Series([])
15443
+ ohlc4: new Series([]),
15444
+ hlcc4: new Series([])
12967
15445
  });
12968
15446
  __publicField$2(this, "cache", {});
12969
15447
  __publicField$2(this, "taState", {});
@@ -12988,6 +15466,7 @@ ${code}
12988
15466
  __publicField$2(this, "limit");
12989
15467
  __publicField$2(this, "sDate");
12990
15468
  __publicField$2(this, "eDate");
15469
+ __publicField$2(this, "fullContext");
12991
15470
  __publicField$2(this, "pineTSCode");
12992
15471
  this.marketData = marketData;
12993
15472
  this.source = source;
@@ -12996,6 +15475,7 @@ ${code}
12996
15475
  this.limit = limit;
12997
15476
  this.sDate = sDate;
12998
15477
  this.eDate = eDate;
15478
+ this.fullContext = fullContext || this;
12999
15479
  const core = new Core(this);
13000
15480
  const coreFunctions = {
13001
15481
  plotchar: core.plotchar.bind(core),
@@ -13011,14 +15491,33 @@ ${code}
13011
15491
  math: new PineMath(this),
13012
15492
  request: new PineRequest(this),
13013
15493
  array: new PineArray(this),
15494
+ map: new PineMap(this),
15495
+ matrix: new PineMatrix(this),
13014
15496
  na: coreFunctions.na,
13015
15497
  plotchar: coreFunctions.plotchar,
13016
15498
  color: coreFunctions.color,
13017
15499
  plot: coreFunctions.plot,
13018
15500
  nz: coreFunctions.nz,
15501
+ syminfo: null,
15502
+ timeframe: new Timeframe(this),
15503
+ //FIXME : this is a temporary solution to get the barstate values,
15504
+ //we need to implement a better way to handle realtime states
15505
+ barstate: new Barstate(this),
13019
15506
  get bar_index() {
13020
15507
  return _this.idx;
13021
- }
15508
+ },
15509
+ get last_bar_index() {
15510
+ return _this.data.close.length - 1;
15511
+ },
15512
+ get last_bar_time() {
15513
+ return _this.data.openTime.get(_this.data.openTime.length - 1);
15514
+ },
15515
+ get timenow() {
15516
+ return (/* @__PURE__ */ new Date()).getTime();
15517
+ },
15518
+ log: new Log(this),
15519
+ str: new Str(this),
15520
+ ...types
13022
15521
  };
13023
15522
  }
13024
15523
  //#region [Runtime functions] ===========================
@@ -13038,10 +15537,10 @@ ${code}
13038
15537
  if (Array.isArray(src[0])) {
13039
15538
  value = src[0];
13040
15539
  } else {
13041
- value = this.precision(src[src.length - 1 + idx]);
15540
+ value = src[src.length - 1 + idx];
13042
15541
  }
13043
15542
  } else {
13044
- value = this.precision(src);
15543
+ value = src;
13045
15544
  }
13046
15545
  if (!trg) {
13047
15546
  return new Series([value]);
@@ -13089,9 +15588,9 @@ ${code}
13089
15588
  * @param decimals - the number of decimals to precision to
13090
15589
  * @returns the precision number
13091
15590
  */
13092
- precision(n, decimals = 10) {
13093
- if (typeof n !== "number" || isNaN(n)) return n;
13094
- return Number(n.toFixed(decimals));
15591
+ precision(value, decimals = 10) {
15592
+ const epsilon = 10 ** decimals;
15593
+ return typeof value === "number" ? Math.round(value * epsilon) / epsilon : value;
13095
15594
  }
13096
15595
  /**
13097
15596
  * This function is used to apply special transformation to internal PineTS parameters and handle them as time-series
@@ -13256,6 +15755,7 @@ ${code}
13256
15755
  __publicField$1(this, "hl2", []);
13257
15756
  __publicField$1(this, "hlc3", []);
13258
15757
  __publicField$1(this, "ohlc4", []);
15758
+ __publicField$1(this, "hlcc4", []);
13259
15759
  __publicField$1(this, "openTime", []);
13260
15760
  __publicField$1(this, "closeTime", []);
13261
15761
  //#endregion
@@ -13274,6 +15774,7 @@ ${code}
13274
15774
  });
13275
15775
  __publicField$1(this, "_transpiledCode", null);
13276
15776
  __publicField$1(this, "_isSecondaryContext", false);
15777
+ __publicField$1(this, "_syminfo");
13277
15778
  this._readyPromise = new Promise((resolve) => {
13278
15779
  this.loadMarketData(source, tickerId, timeframe, limit, sDate, eDate).then((data) => {
13279
15780
  const marketData = data;
@@ -13286,6 +15787,7 @@ ${code}
13286
15787
  const _hlc3 = marketData.map((d) => (d.high + d.low + d.close) / 3);
13287
15788
  const _hl2 = marketData.map((d) => (d.high + d.low) / 2);
13288
15789
  const _ohlc4 = marketData.map((d) => (d.high + d.low + d.open + d.close) / 4);
15790
+ const _hlcc4 = marketData.map((d) => (d.high + d.low + d.close + d.close) / 4);
13289
15791
  const _openTime = marketData.map((d) => d.openTime);
13290
15792
  const _closeTime = marketData.map((d) => d.closeTime);
13291
15793
  this.open = _open;
@@ -13296,10 +15798,23 @@ ${code}
13296
15798
  this.hl2 = _hl2;
13297
15799
  this.hlc3 = _hlc3;
13298
15800
  this.ohlc4 = _ohlc4;
15801
+ this.hlcc4 = _hlcc4;
13299
15802
  this.openTime = _openTime;
13300
15803
  this.closeTime = _closeTime;
13301
- this._ready = true;
13302
- resolve(true);
15804
+ if (source && source.getSymbolInfo) {
15805
+ source.getSymbolInfo(tickerId).then((symbolInfo2) => {
15806
+ this._syminfo = symbolInfo2;
15807
+ this._ready = true;
15808
+ resolve(true);
15809
+ }).catch((error) => {
15810
+ console.warn("Failed to get symbol info, using default values:", error);
15811
+ this._ready = true;
15812
+ resolve(true);
15813
+ });
15814
+ } else {
15815
+ this._ready = true;
15816
+ resolve(true);
15817
+ }
13303
15818
  });
13304
15819
  });
13305
15820
  }
@@ -13389,6 +15904,7 @@ ${code}
13389
15904
  }
13390
15905
  this._removeLastResult(context);
13391
15906
  processedUpToIdx = this.data.length - (newCandles + 1);
15907
+ context.pine.barstate.setLive();
13392
15908
  }
13393
15909
  }
13394
15910
  /**
@@ -13418,7 +15934,8 @@ ${code}
13418
15934
  timeframe: this.timeframe,
13419
15935
  limit: this.limit,
13420
15936
  sDate: this.sDate,
13421
- eDate: this.eDate
15937
+ eDate: this.eDate,
15938
+ fullContext
13422
15939
  });
13423
15940
  pageContext.pineTSCode = fullContext.pineTSCode;
13424
15941
  pageContext.idx = fullContext.idx;
@@ -13491,6 +16008,7 @@ ${code}
13491
16008
  this.hl2[index] = (candle.high + candle.low) / 2;
13492
16009
  this.hlc3[index] = (candle.high + candle.low + candle.close) / 3;
13493
16010
  this.ohlc4[index] = (candle.high + candle.low + candle.open + candle.close) / 4;
16011
+ this.hlcc4[index] = (candle.high + candle.low + candle.close + candle.close) / 4;
13494
16012
  this.openTime[index] = candle.openTime;
13495
16013
  this.closeTime[index] = candle.closeTime;
13496
16014
  }
@@ -13508,6 +16026,7 @@ ${code}
13508
16026
  this.hl2.push((candle.high + candle.low) / 2);
13509
16027
  this.hlc3.push((candle.high + candle.low + candle.close) / 3);
13510
16028
  this.ohlc4.push((candle.high + candle.low + candle.open + candle.close) / 4);
16029
+ this.hlcc4.push((candle.high + candle.low + candle.close + candle.close) / 4);
13511
16030
  this.openTime.push(candle.openTime);
13512
16031
  this.closeTime.push(candle.closeTime);
13513
16032
  }
@@ -13533,6 +16052,7 @@ ${code}
13533
16052
  context.data.hl2.data.pop();
13534
16053
  context.data.hlc3.data.pop();
13535
16054
  context.data.ohlc4.data.pop();
16055
+ context.data.hlcc4.data.pop();
13536
16056
  context.data.openTime.data.pop();
13537
16057
  if (context.data.closeTime) {
13538
16058
  context.data.closeTime.data.pop();
@@ -13552,6 +16072,7 @@ ${code}
13552
16072
  sDate: this.sDate,
13553
16073
  eDate: this.eDate
13554
16074
  });
16075
+ context.pine.syminfo = this._syminfo;
13555
16076
  context.pineTSCode = pineTSCode;
13556
16077
  context.isSecondaryContext = isSecondary;
13557
16078
  context.data.close = new Series([]);
@@ -13562,6 +16083,7 @@ ${code}
13562
16083
  context.data.hl2 = new Series([]);
13563
16084
  context.data.hlc3 = new Series([]);
13564
16085
  context.data.ohlc4 = new Series([]);
16086
+ context.data.hlcc4 = new Series([]);
13565
16087
  context.data.openTime = new Series([]);
13566
16088
  context.data.closeTime = new Series([]);
13567
16089
  return context;
@@ -13590,6 +16112,7 @@ ${code}
13590
16112
  context.data.hl2.data.push(this.hl2[i]);
13591
16113
  context.data.hlc3.data.push(this.hlc3[i]);
13592
16114
  context.data.ohlc4.data.push(this.ohlc4[i]);
16115
+ context.data.hlcc4.data.push(this.hlcc4[i]);
13593
16116
  context.data.openTime.data.push(this.openTime[i]);
13594
16117
  context.data.closeTime.data.push(this.closeTime[i]);
13595
16118
  const result = await transpiledFn(context);
@@ -13768,10 +16291,13 @@ ${code}
13768
16291
  }
13769
16292
  async getMarketData(tickerId, timeframe, limit, sDate, eDate) {
13770
16293
  try {
16294
+ const shouldCache = eDate !== void 0;
13771
16295
  const cacheParams = { tickerId, timeframe, limit, sDate, eDate };
13772
- const cachedData = this.cacheManager.get(cacheParams);
13773
- if (cachedData) {
13774
- return cachedData;
16296
+ if (shouldCache) {
16297
+ const cachedData = this.cacheManager.get(cacheParams);
16298
+ if (cachedData) {
16299
+ return cachedData;
16300
+ }
13775
16301
  }
13776
16302
  const interval = timeframe_to_binance[timeframe.toUpperCase()];
13777
16303
  if (!interval) {
@@ -13816,7 +16342,9 @@ ${code}
13816
16342
  ignore: item[11]
13817
16343
  };
13818
16344
  });
13819
- this.cacheManager.set(cacheParams, data);
16345
+ if (shouldCache) {
16346
+ this.cacheManager.set(cacheParams, data);
16347
+ }
13820
16348
  return data;
13821
16349
  } catch (error) {
13822
16350
  console.error("Error in binance.klines:", error);
@@ -13853,6 +16381,106 @@ ${code}
13853
16381
  }
13854
16382
  return false;
13855
16383
  }
16384
+ async getSymbolInfo(tickerId) {
16385
+ try {
16386
+ let marketType = "crypto";
16387
+ let apiUrl = BINANCE_API_URL;
16388
+ let apiSymbol = tickerId;
16389
+ let contractType = "";
16390
+ if (tickerId.endsWith(".P")) {
16391
+ marketType = "futures";
16392
+ apiSymbol = tickerId.replace(".P", "");
16393
+ apiUrl = "https://fapi.binance.com/fapi/v1";
16394
+ contractType = "Perpetual";
16395
+ } else if (tickerId.includes("_")) {
16396
+ marketType = "futures";
16397
+ apiSymbol = tickerId;
16398
+ apiUrl = "https://dapi.binance.com/dapi/v1";
16399
+ contractType = "Delivery";
16400
+ }
16401
+ const url = marketType === "crypto" ? `${apiUrl}/exchangeInfo?symbol=${apiSymbol}` : `${apiUrl}/exchangeInfo`;
16402
+ const response = await fetch(url);
16403
+ if (!response.ok) {
16404
+ throw new Error(`HTTP error! status: ${response.status}`);
16405
+ }
16406
+ const result = await response.json();
16407
+ const symbols = result.symbols;
16408
+ if (!symbols || symbols.length === 0) {
16409
+ console.error(`Symbol ${tickerId} not found`);
16410
+ return null;
16411
+ }
16412
+ const symbolData = marketType === "futures" ? symbols.find((s) => s.symbol === apiSymbol) : symbols[0];
16413
+ if (!symbolData) {
16414
+ console.error(`Symbol ${apiSymbol} not found in exchange info`);
16415
+ return null;
16416
+ }
16417
+ const priceFilter = symbolData.filters?.find((f) => f.filterType === "PRICE_FILTER");
16418
+ const lotSizeFilter = symbolData.filters?.find((f) => f.filterType === "LOT_SIZE");
16419
+ const tickSize = priceFilter ? parseFloat(priceFilter.tickSize) : 0.01;
16420
+ const minQty = lotSizeFilter ? parseFloat(lotSizeFilter.minQty) : 0;
16421
+ const pricescale = Math.round(1 / tickSize);
16422
+ const baseAsset = symbolData.baseAsset;
16423
+ const quoteAsset = symbolData.quoteAsset;
16424
+ const typeLabel = contractType ? ` ${contractType}` : "";
16425
+ const description = `${baseAsset} / ${quoteAsset}${typeLabel}`;
16426
+ const symbolInfo = {
16427
+ // Symbol Identification
16428
+ ticker: tickerId,
16429
+ // KEEP ORIGINAL including .P if present!
16430
+ tickerid: `BINANCE:${tickerId}`,
16431
+ // Also keep .P here
16432
+ prefix: "BINANCE",
16433
+ root: baseAsset,
16434
+ // Just the base asset: "BTC"
16435
+ description,
16436
+ type: marketType,
16437
+ main_tickerid: `BINANCE:${tickerId}`,
16438
+ current_contract: contractType,
16439
+ isin: "",
16440
+ // Currency & Location
16441
+ basecurrency: baseAsset,
16442
+ currency: quoteAsset,
16443
+ timezone: "Etc/UTC",
16444
+ country: "",
16445
+ // Price & Contract Info
16446
+ mintick: tickSize,
16447
+ pricescale,
16448
+ minmove: 1,
16449
+ pointvalue: symbolData.contractSize || 1,
16450
+ mincontract: minQty,
16451
+ // Session & Market
16452
+ session: "24x7",
16453
+ volumetype: "base",
16454
+ expiration_date: symbolData.deliveryDate || 0,
16455
+ // Company Data (N/A for crypto)
16456
+ employees: 0,
16457
+ industry: "",
16458
+ sector: "",
16459
+ shareholders: 0,
16460
+ shares_outstanding_float: 0,
16461
+ shares_outstanding_total: 0,
16462
+ // Analyst Ratings (N/A for crypto)
16463
+ recommendations_buy: 0,
16464
+ recommendations_buy_strong: 0,
16465
+ recommendations_date: 0,
16466
+ recommendations_hold: 0,
16467
+ recommendations_sell: 0,
16468
+ recommendations_sell_strong: 0,
16469
+ recommendations_total: 0,
16470
+ // Price Targets (N/A for crypto)
16471
+ target_price_average: 0,
16472
+ target_price_date: 0,
16473
+ target_price_estimates: 0,
16474
+ target_price_high: 0,
16475
+ target_price_low: 0,
16476
+ target_price_median: 0
16477
+ };
16478
+ return symbolInfo;
16479
+ } catch (error) {
16480
+ console.error("Error in binance.exchangeInfo:", error);
16481
+ return null;
16482
+ }
16483
+ }
13856
16484
  }
13857
16485
 
13858
16486
  class MockProvider {