pinets 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/README.md +35 -18
  2. package/dist/pinets.dev.browser.js +3189 -2524
  3. package/dist/pinets.dev.cjs +3389 -2556
  4. package/dist/pinets.dev.cjs.map +1 -1
  5. package/dist/pinets.dev.es.js +2937 -2117
  6. package/dist/pinets.dev.es.js.map +1 -1
  7. package/dist/pinets.min.browser.js +11 -12
  8. package/dist/pinets.min.cjs +11 -12
  9. package/dist/pinets.min.es.js +1 -2
  10. package/dist/types/Context.class.d.ts +17 -6
  11. package/dist/types/PineTS.class.d.ts +78 -4
  12. package/dist/types/Series.d.ts +10 -0
  13. package/dist/types/TimeSeries.class.d.ts +49 -0
  14. package/dist/types/TimeSeries.d.ts +15 -0
  15. package/dist/types/marketData/Mock/MockProvider.class.d.ts +77 -0
  16. package/dist/types/marketData/Provider.class.d.ts +6 -3
  17. package/dist/types/namespaces/array/PineArrayObject.d.ts +5 -0
  18. package/dist/types/namespaces/array/array.index.d.ts +135 -0
  19. package/dist/types/namespaces/array/methods/abs.d.ts +2 -0
  20. package/dist/types/namespaces/array/methods/avg.d.ts +2 -0
  21. package/dist/types/namespaces/array/methods/clear.d.ts +2 -0
  22. package/dist/types/namespaces/array/methods/concat.d.ts +2 -0
  23. package/dist/types/namespaces/array/methods/copy.d.ts +2 -0
  24. package/dist/types/namespaces/array/methods/covariance.d.ts +2 -0
  25. package/dist/types/namespaces/array/methods/every.d.ts +2 -0
  26. package/dist/types/namespaces/array/methods/fill.d.ts +2 -0
  27. package/dist/types/namespaces/array/methods/first.d.ts +2 -0
  28. package/dist/types/namespaces/array/methods/from.d.ts +2 -0
  29. package/dist/types/namespaces/array/methods/get.d.ts +2 -0
  30. package/dist/types/namespaces/array/methods/includes.d.ts +2 -0
  31. package/dist/types/namespaces/array/methods/indexof.d.ts +2 -0
  32. package/dist/types/namespaces/array/methods/insert.d.ts +2 -0
  33. package/dist/types/namespaces/array/methods/join.d.ts +2 -0
  34. package/dist/types/namespaces/array/methods/last.d.ts +2 -0
  35. package/dist/types/namespaces/array/methods/lastindexof.d.ts +2 -0
  36. package/dist/types/namespaces/array/methods/max.d.ts +2 -0
  37. package/dist/types/namespaces/array/methods/min.d.ts +2 -0
  38. package/dist/types/namespaces/array/methods/new.d.ts +2 -0
  39. package/dist/types/namespaces/array/methods/new_bool.d.ts +2 -0
  40. package/dist/types/namespaces/array/methods/new_float.d.ts +2 -0
  41. package/dist/types/namespaces/array/methods/new_int.d.ts +2 -0
  42. package/dist/types/namespaces/array/methods/new_string.d.ts +2 -0
  43. package/dist/types/namespaces/array/methods/param.d.ts +1 -0
  44. package/dist/types/namespaces/array/methods/pop.d.ts +2 -0
  45. package/dist/types/namespaces/array/methods/push.d.ts +2 -0
  46. package/dist/types/namespaces/array/methods/range.d.ts +2 -0
  47. package/dist/types/namespaces/array/methods/remove.d.ts +2 -0
  48. package/dist/types/namespaces/array/methods/reverse.d.ts +2 -0
  49. package/dist/types/namespaces/array/methods/set.d.ts +2 -0
  50. package/dist/types/namespaces/array/methods/shift.d.ts +2 -0
  51. package/dist/types/namespaces/array/methods/size.d.ts +2 -0
  52. package/dist/types/namespaces/array/methods/slice.d.ts +2 -0
  53. package/dist/types/namespaces/array/methods/some.d.ts +2 -0
  54. package/dist/types/namespaces/array/methods/sort.d.ts +2 -0
  55. package/dist/types/namespaces/array/methods/sort_indices.d.ts +2 -0
  56. package/dist/types/namespaces/array/methods/standardize.d.ts +2 -0
  57. package/dist/types/namespaces/array/methods/stdev.d.ts +2 -0
  58. package/dist/types/namespaces/array/methods/sum.d.ts +2 -0
  59. package/dist/types/namespaces/array/methods/unshift.d.ts +2 -0
  60. package/dist/types/namespaces/array/methods/variance.d.ts +2 -0
  61. package/dist/types/namespaces/input/input.index.d.ts +53 -0
  62. package/dist/types/namespaces/input/methods/any.d.ts +2 -0
  63. package/dist/types/namespaces/input/methods/bool.d.ts +2 -0
  64. package/dist/types/namespaces/input/methods/color.d.ts +2 -0
  65. package/dist/types/namespaces/input/methods/enum.d.ts +2 -0
  66. package/dist/types/namespaces/input/methods/float.d.ts +2 -0
  67. package/dist/types/namespaces/input/methods/int.d.ts +2 -0
  68. package/dist/types/namespaces/input/methods/param.d.ts +1 -0
  69. package/dist/types/namespaces/input/methods/price.d.ts +2 -0
  70. package/dist/types/namespaces/input/methods/session.d.ts +2 -0
  71. package/dist/types/namespaces/input/methods/source.d.ts +2 -0
  72. package/dist/types/namespaces/input/methods/string.d.ts +2 -0
  73. package/dist/types/namespaces/input/methods/symbol.d.ts +2 -0
  74. package/dist/types/namespaces/input/methods/text_area.d.ts +2 -0
  75. package/dist/types/namespaces/input/methods/time.d.ts +2 -0
  76. package/dist/types/namespaces/input/methods/timeframe.d.ts +2 -0
  77. package/dist/types/namespaces/input/types.d.ts +4 -0
  78. package/dist/types/namespaces/math/math.index.d.ts +77 -0
  79. package/dist/types/namespaces/math/methods/__eq.d.ts +1 -0
  80. package/dist/types/namespaces/math/methods/abs.d.ts +1 -0
  81. package/dist/types/namespaces/math/methods/acos.d.ts +1 -0
  82. package/dist/types/namespaces/math/methods/asin.d.ts +1 -0
  83. package/dist/types/namespaces/math/methods/atan.d.ts +1 -0
  84. package/dist/types/namespaces/math/methods/avg.d.ts +1 -0
  85. package/dist/types/namespaces/math/methods/ceil.d.ts +1 -0
  86. package/dist/types/namespaces/math/methods/cos.d.ts +1 -0
  87. package/dist/types/namespaces/math/methods/exp.d.ts +1 -0
  88. package/dist/types/namespaces/math/methods/floor.d.ts +1 -0
  89. package/dist/types/namespaces/math/methods/ln.d.ts +1 -0
  90. package/dist/types/namespaces/math/methods/log.d.ts +1 -0
  91. package/dist/types/namespaces/math/methods/log10.d.ts +1 -0
  92. package/dist/types/namespaces/math/methods/max.d.ts +1 -0
  93. package/dist/types/namespaces/math/methods/min.d.ts +1 -0
  94. package/dist/types/namespaces/math/methods/param.d.ts +1 -0
  95. package/dist/types/namespaces/math/methods/pow.d.ts +1 -0
  96. package/dist/types/namespaces/math/methods/random.d.ts +1 -0
  97. package/dist/types/namespaces/math/methods/round.d.ts +1 -0
  98. package/dist/types/namespaces/math/methods/sin.d.ts +1 -0
  99. package/dist/types/namespaces/math/methods/sqrt.d.ts +1 -0
  100. package/dist/types/namespaces/math/methods/sum.d.ts +1 -0
  101. package/dist/types/namespaces/math/methods/tan.d.ts +1 -0
  102. package/dist/types/namespaces/request/methods/param.d.ts +1 -0
  103. package/dist/types/namespaces/request/methods/security.d.ts +1 -0
  104. package/dist/types/namespaces/request/request.index.d.ts +14 -0
  105. package/dist/types/namespaces/request/utils/TIMEFRAMES.d.ts +1 -0
  106. package/dist/types/namespaces/request/utils/findSecContextIdx.d.ts +1 -0
  107. package/dist/types/namespaces/ta/getters/tr.d.ts +1 -0
  108. package/dist/types/namespaces/ta/methods/atr.d.ts +1 -0
  109. package/dist/types/namespaces/ta/methods/change.d.ts +1 -0
  110. package/dist/types/namespaces/ta/methods/crossover.d.ts +1 -0
  111. package/dist/types/namespaces/ta/methods/crossunder.d.ts +1 -0
  112. package/dist/types/namespaces/ta/methods/dev.d.ts +1 -0
  113. package/dist/types/namespaces/ta/methods/ema.d.ts +1 -0
  114. package/dist/types/namespaces/ta/methods/highest.d.ts +1 -0
  115. package/dist/types/namespaces/ta/methods/hma.d.ts +1 -0
  116. package/dist/types/namespaces/ta/methods/linreg.d.ts +1 -0
  117. package/dist/types/namespaces/ta/methods/lowest.d.ts +1 -0
  118. package/dist/types/namespaces/ta/methods/median.d.ts +1 -0
  119. package/dist/types/namespaces/ta/methods/mom.d.ts +1 -0
  120. package/dist/types/namespaces/ta/methods/param.d.ts +2 -0
  121. package/dist/types/namespaces/ta/methods/pivothigh.d.ts +1 -0
  122. package/dist/types/namespaces/ta/methods/pivotlow.d.ts +1 -0
  123. package/dist/types/namespaces/ta/methods/rma.d.ts +1 -0
  124. package/dist/types/namespaces/ta/methods/roc.d.ts +1 -0
  125. package/dist/types/namespaces/ta/methods/rsi.d.ts +1 -0
  126. package/dist/types/namespaces/ta/methods/sma.d.ts +1 -0
  127. package/dist/types/namespaces/ta/methods/stdev.d.ts +1 -0
  128. package/dist/types/namespaces/ta/methods/supertrend.d.ts +1 -0
  129. package/dist/types/namespaces/ta/methods/variance.d.ts +1 -0
  130. package/dist/types/namespaces/ta/methods/vwma.d.ts +1 -0
  131. package/dist/types/namespaces/ta/methods/wma.d.ts +1 -0
  132. package/dist/types/namespaces/ta/ta.index.d.ts +84 -0
  133. package/dist/types/namespaces/ta/utils/pivothigh.d.ts +1 -0
  134. package/dist/types/namespaces/ta/utils/pivotlow.d.ts +1 -0
  135. package/dist/types/transpiler/analysis/AnalysisPass.d.ts +5 -0
  136. package/dist/types/transpiler/analysis/ScopeManager.d.ts +36 -0
  137. package/dist/types/transpiler/transformers/ExpressionTransformer.d.ts +7 -0
  138. package/dist/types/transpiler/transformers/MainTransformer.d.ts +3 -0
  139. package/dist/types/transpiler/transformers/StatementTransformer.d.ts +9 -0
  140. package/dist/types/transpiler/utils/ASTFactory.d.ts +18 -0
  141. package/package.json +12 -3
@@ -15,5 +15,4 @@
15
15
  * You should have received a copy of the GNU Affero General Public License
16
16
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
17
  */
18
- import*as K from"acorn";import*as g from"acorn-walk";import*as z from"astring";var H=Object.defineProperty,Q=(r,t,e)=>t in r?H(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,j=(r,t,e)=>Q(r,typeof t!="symbol"?t+"":t,e);class J{constructor(){j(this,"scopes",[]),j(this,"scopeTypes",[]),j(this,"scopeCounts",new Map),j(this,"contextBoundVars",new Set),j(this,"arrayPatternElements",new Set),j(this,"rootParams",new Set),j(this,"varKinds",new Map),j(this,"loopVars",new Set),j(this,"loopVarNames",new Map),j(this,"paramIdCounter",0),j(this,"cacheIdCounter",0),j(this,"tempVarCounter",0),j(this,"taCallIdCounter",0),this.pushScope("glb")}get nextParamIdArg(){return{type:"Identifier",name:`'p${this.paramIdCounter++}'`}}get nextCacheIdArg(){return{type:"Identifier",name:`'cache_${this.cacheIdCounter++}'`}}getNextTACallId(){return{type:"Literal",value:`_ta${this.taCallIdCounter++}`}}pushScope(t){this.scopes.push(new Map),this.scopeTypes.push(t),this.scopeCounts.set(t,(this.scopeCounts.get(t)||0)+1)}popScope(){this.scopes.pop(),this.scopeTypes.pop()}getCurrentScopeType(){return this.scopeTypes[this.scopeTypes.length-1]}getCurrentScopeCount(){return this.scopeCounts.get(this.getCurrentScopeType())||1}addContextBoundVar(t,e=!1){this.contextBoundVars.add(t),e&&this.rootParams.add(t)}removeContextBoundVar(t){this.contextBoundVars.has(t)&&(this.contextBoundVars.delete(t),this.rootParams.has(t)&&this.rootParams.delete(t))}addArrayPatternElement(t){this.arrayPatternElements.add(t)}isContextBound(t){return this.contextBoundVars.has(t)}isArrayPatternElement(t){return this.arrayPatternElements.has(t)}isRootParam(t){return this.rootParams.has(t)}addLoopVariable(t,e){this.loopVars.add(t),this.loopVarNames.set(t,e)}getLoopVariableName(t){return this.loopVarNames.get(t)}isLoopVariable(t){return this.loopVars.has(t)}addVariable(t,e){if(this.isContextBound(t))return t;const n=this.scopes[this.scopes.length-1],a=this.scopeTypes[this.scopeTypes.length-1],o=this.scopeCounts.get(a)||1,i=`${a}${o}_${t}`;return n.set(t,i),this.varKinds.set(i,e),i}getVariable(t){if(this.loopVars.has(t)){const e=this.loopVarNames.get(t);if(e)return[e,"let"]}if(this.isContextBound(t))return[t,"let"];for(let e=this.scopes.length-1;e>=0;e--){const n=this.scopes[e];if(n.has(t)){const a=n.get(t),o=this.varKinds.get(a)||"let";return[a,o]}}return[t,"let"]}generateTempVar(){return`temp_${++this.tempVarCounter}`}}//!!!Warning!!! this code is not clean, it was initially written as a PoC then used as transpiler for PineTS
19
- const b="$",k={type:"Identifier",name:"undefined"};function N(r,t){if(r.computed&&r.property.type==="Identifier"){if(t.isLoopVariable(r.property.name))return;if(!t.isContextBound(r.property.name)){const[e,n]=t.getVariable(r.property.name);r.property={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1},r.property={type:"MemberExpression",object:r.property,property:{type:"Literal",value:0},computed:!0}}}if(r.computed&&r.object.type==="Identifier"){if(t.isLoopVariable(r.object.name))return;if(!t.isContextBound(r.object.name)){const[e,n]=t.getVariable(r.object.name);r.object={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1}}if(r.property.type==="MemberExpression"){const e=r.property;e._indexTransformed||(N(e,t),e._indexTransformed=!0)}}}function I(r,t,e){if(r.object&&r.object.type==="Identifier"&&r.object.name==="Math")return;const n=e.getCurrentScopeType()=="if",a=e.getCurrentScopeType()=="els",o=e.getCurrentScopeType()=="for";!n&&!a&&!o&&r.object&&r.object.type==="Identifier"&&e.isContextBound(r.object.name)&&!e.isRootParam(r.object.name)||r._indexTransformed||(N(r,e),r._indexTransformed=!0)}function D(r,t){r.declarations.forEach(e=>{e.init.name=="na"&&(e.init.name="NaN");const n=e.init&&e.init.type==="MemberExpression"&&e.init.object&&(e.init.object.name==="context"||e.init.object.name===b||e.init.object.name==="context2"),a=e.init&&e.init.type==="MemberExpression"&&e.init.object?.object&&(e.init.object.object.name==="context"||e.init.object.object.name===b||e.init.object.object.name==="context2"),o=e.init&&e.init.type==="ArrowFunctionExpression";if(n){e.id.name&&t.addContextBoundVar(e.id.name),e.id.properties&&e.id.properties.forEach(p=>{p.key.name&&t.addContextBoundVar(p.key.name)}),e.init.object.name=b;return}if(a){e.id.name&&t.addContextBoundVar(e.id.name),e.id.properties&&e.id.properties.forEach(p=>{p.key.name&&t.addContextBoundVar(p.key.name)}),e.init.object.object.name=b;return}o&&e.init.params.forEach(p=>{p.type==="Identifier"&&t.addContextBoundVar(p.name)});const i=t.addVariable(e.id.name,r.kind),s=r.kind;e.init&&!o&&(e.init.type==="CallExpression"&&e.init.callee.type==="MemberExpression"&&e.init.callee.object&&e.init.callee.object.type==="Identifier"&&t.isContextBound(e.init.callee.object.name)?S(e.init,t):g.recursive(e.init,{parent:e.init},{Identifier(p,u){p.parent=u.parent,E(p,t);const y=p.parent&&p.parent.type==="BinaryExpression",x=p.parent&&p.parent.type==="ConditionalExpression";p.type==="Identifier"&&(y||x)&&Object.assign(p,{type:"MemberExpression",object:{type:"Identifier",name:p.name},property:{type:"Literal",value:0},computed:!0})},CallExpression(p,u,y){p.callee.type==="Identifier"&&(p.callee.parent=p),p.arguments.forEach(x=>{x.type==="Identifier"&&(x.parent=p)}),S(p,t),p.arguments.forEach(x=>y(x,{parent:p}))},BinaryExpression(p,u,y){p.left.type==="Identifier"&&(p.left.parent=p),p.right.type==="Identifier"&&(p.right.parent=p),y(p.left,{parent:p}),y(p.right,{parent:p})},MemberExpression(p,u,y){p.object.type==="Identifier"&&(p.object.parent=p),p.property.type==="Identifier"&&(p.property.parent=p),N(p,t),p.object&&y(p.object,{parent:p})}}));const c={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:s},computed:!1},property:{type:"Identifier",name:i},computed:!1},l=t.isArrayPatternElement(e.id.name),m=!l&&e.init&&e.init.type==="MemberExpression"&&e.init.computed&&e.init.property&&(e.init.property.type==="Literal"||e.init.property.type==="MemberExpression");e.init?.property?.type==="MemberExpression"&&(e.init.property._indexTransformed||(N(e.init.property,t),e.init.property._indexTransformed=!0));const h={type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:c,right:e.init?o||l?e.init:{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"init"},computed:!1},arguments:m?[c,e.init.object,e.init.property]:[c,e.init]}:{type:"Identifier",name:"undefined"}}};if(l){h.expression.right.object.property.name+=`?.[0][${e.init.property.value}]`;const p=h.expression.right.object;h.expression.right={type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"init"},computed:!1},arguments:[c,p]}}o&&(t.pushScope("fn"),g.recursive(e.init.body,t,{BlockStatement(p,u,y){p.body.forEach(x=>y(x,u))},IfStatement(p,u,y){u.pushScope("if"),y(p.consequent,u),p.alternate&&(u.pushScope("els"),y(p.alternate,u),u.popScope()),u.popScope()},VariableDeclaration(p,u){D(p,u)},Identifier(p,u){E(p,u)},AssignmentExpression(p,u){L(p,u)}}),t.popScope()),Object.assign(r,h)})}function E(r,t){if(r.name!==b){if(r.name==="Math"||r.name==="NaN"||r.name==="undefined"||r.name==="Infinity"||r.name==="null"||r.name.startsWith("'")&&r.name.endsWith("'")||r.name.startsWith('"')&&r.name.endsWith('"')||r.name.startsWith("`")&&r.name.endsWith("`")||t.isLoopVariable(r.name)||t.isContextBound(r.name)&&!t.isRootParam(r.name))return;const e=r.parent&&r.parent.type==="MemberExpression"&&r.parent.object===r&&t.isContextBound(r.name),n=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee&&r.parent.callee.type==="MemberExpression"&&r.parent.callee.property.name==="param";r.parent&&r.parent.type==="AssignmentExpression"&&r.parent.left;const a=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee&&r.parent.callee.type==="MemberExpression"&&t.isContextBound(r.parent.callee.object.name),o=r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed,i=r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed&&r.parent.property===r&&r.parent.parent&&r.parent.parent.type==="CallExpression"&&r.parent.parent.callee&&r.parent.parent.callee.type==="MemberExpression"&&t.isContextBound(r.parent.parent.callee.object.name),s=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee===r;if(e||n||a||i||s){if(s)return;const[h,p]=t.getVariable(r.name);Object.assign(r,{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:p},computed:!1},property:{type:"Identifier",name:h},computed:!1});return}const[c,l]=t.getVariable(r.name),m={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:l},computed:!1},property:{type:"Identifier",name:c},computed:!1};!(r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed&&r.parent.object===r)&&!o?Object.assign(r,{type:"MemberExpression",object:m,property:{type:"Literal",value:0},computed:!0}):Object.assign(r,m)}}function L(r,t){if(r.left.type==="Identifier"){const[e,n]=t.getVariable(r.left.name),a={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1};r.left={type:"MemberExpression",object:a,property:{type:"Literal",value:0},computed:!0}}g.recursive(r.right,{parent:r.right,inNamespaceCall:!1},{Identifier(e,n,a){e.name=="na"&&(e.name="NaN"),e.parent=n.parent,E(e,t);const o=e.parent&&e.parent.type==="BinaryExpression",i=e.parent&&e.parent.type==="ConditionalExpression",s=t.isContextBound(e.name)&&!t.isRootParam(e.name),c=e.parent&&e.parent.type==="MemberExpression"&&e.parent.computed&&e.parent.object===e,l=e.parent&&e.parent._isParamCall,m=e.parent&&e.parent.type==="MemberExpression",h=e.name==="NaN";(s||i||o)&&(e.type==="MemberExpression"?N(e,t):e.type==="Identifier"&&!m&&!c&&!l&&!h&&$(e))},MemberExpression(e,n,a){N(e,t),e.object&&a(e.object,{parent:e,inNamespaceCall:n.inNamespaceCall})},CallExpression(e,n,a){const o=e.callee&&e.callee.type==="MemberExpression"&&e.callee.object&&e.callee.object.type==="Identifier"&&t.isContextBound(e.callee.object.name);S(e,t),e.arguments.forEach(i=>a(i,{parent:e,inNamespaceCall:o||n.inNamespaceCall}))}})}function X(r,t,e=!1){r.params.forEach(n=>{n.type==="Identifier"&&t.addContextBoundVar(n.name,e)})}function F(r,t){const e=t.getCurrentScopeType();r.argument&&(r.argument.type==="ArrayExpression"?(r.argument.elements=r.argument.elements.map(n=>{if(n.type==="Identifier"){if(t.isContextBound(n.name)&&!t.isRootParam(n.name))return{type:"MemberExpression",object:n,property:{type:"Literal",value:0},computed:!0};const[a,o]=t.getVariable(n.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1},property:{type:"Literal",value:0},computed:!0}}else if(n.type==="MemberExpression")return n.computed&&n.object.type==="Identifier"&&t.isContextBound(n.object.name)&&!t.isRootParam(n.object.name)||I(n,"",t),n;return n}),r.argument={type:"ArrayExpression",elements:[r.argument]}):r.argument.type==="BinaryExpression"?g.recursive(r.argument,t,{Identifier(n,a){E(n,a),n.type==="Identifier"&&$(n)},MemberExpression(n){I(n,"",t)}}):r.argument.type==="ObjectExpression"?r.argument.properties=r.argument.properties.map(n=>{if(n.shorthand){const[a,o]=t.getVariable(n.value.name);return{type:"Property",key:{type:"Identifier",name:n.key.name},value:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return n}):r.argument.type==="Identifier"&&(E(r.argument,t),r.argument.type==="Identifier"&&$(r.argument)),e==="fn"&&(r.argument.type==="Identifier"&&t.isContextBound(r.argument.name)&&!t.isRootParam(r.argument.name)?r.argument={type:"MemberExpression",object:r.argument,property:{type:"Literal",value:0},computed:!0}:r.argument.type==="MemberExpression"?r.argument.object.type==="Identifier"&&t.isContextBound(r.argument.object.name)&&!t.isRootParam(r.argument.object.name)&&(r.argument._indexTransformed||(N(r.argument,t),r.argument._indexTransformed=!0)):(r.argument.type==="BinaryExpression"||r.argument.type==="LogicalExpression"||r.argument.type==="ConditionalExpression"||r.argument.type==="CallExpression")&&g.recursive(r.argument,t,{Identifier(n,a){E(n,a),n.type==="Identifier"&&!n._arrayAccessed&&($(n),n._arrayAccessed=!0)},MemberExpression(n){I(n,"",t)},CallExpression(n,a){S(n,a)}}),r.argument={type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"precision"}},arguments:[r.argument]}))}function V(r,t){if(r.type==="Identifier"){if(r.name==="na")return r.name="NaN",r;if(t.isLoopVariable(r.name))return r;if(t.isRootParam(r.name)){const[a,o]=t.getVariable(r.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1}}if(t.isContextBound(r.name))return r;const[e,n]=t.getVariable(r.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1}}return r}function U(r,t,e){const n=B(r.argument,t,e);return{type:"UnaryExpression",operator:r.operator,prefix:r.prefix,argument:n,start:r.start,end:r.end}}function B(r,t,e=""){switch(r.type){case"BinaryExpression":return W(r,t,e);case"MemberExpression":return{type:"MemberExpression",object:r.object.type==="Identifier"?V(r.object,t):r.object,property:r.property,computed:r.computed};case"Identifier":return t.isLoopVariable(r.name)||r.parent&&r.parent.type==="MemberExpression"&&r.parent.property===r?r:{type:"MemberExpression",object:V(r,t),property:{type:"Literal",value:0},computed:!0};case"UnaryExpression":return U(r,t,e)}return r}function W(r,t,e){const n=B(r.left,t,e),a=B(r.right,t,e),o={type:"BinaryExpression",operator:r.operator,left:n,right:a,start:r.start,end:r.end};return g.recursive(o,t,{CallExpression(i,s){i._transformed||S(i,s)},MemberExpression(i){I(i,"",t)}}),o}function Y(r,t,e){const n=B(r.left,t,e),a=B(r.right,t,e),o={type:"LogicalExpression",operator:r.operator,left:n,right:a,start:r.start,end:r.end};return g.recursive(o,t,{CallExpression(i,s){i._transformed||S(i,s)}}),o}function Z(r,t,e){return g.recursive(r,{parent:r,inNamespaceCall:!1},{Identifier(n,a,o){if(n.name=="NaN")return;if(n.name=="na"){n.name="NaN";return}n.parent=a.parent,E(n,t);const i=n.parent&&n.parent.type==="BinaryExpression";(n.parent&&n.parent.type==="ConditionalExpression"||i)&&(n.type==="MemberExpression"?N(n,t):n.type==="Identifier"&&$(n))},MemberExpression(n,a,o){N(n,t),n.object&&o(n.object,{parent:n,inNamespaceCall:a.inNamespaceCall})},CallExpression(n,a,o){const i=n.callee&&n.callee.type==="MemberExpression"&&n.callee.object&&n.callee.object.type==="Identifier"&&t.isContextBound(n.callee.object.name);S(n,t),n.arguments.forEach(s=>o(s,{parent:n,inNamespaceCall:i||a.inNamespaceCall}))}}),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:e},property:{type:"Identifier",name:"param"}},arguments:[r,k,t.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function O(r,t,e){switch(r?.type){case"BinaryExpression":r=W(r,e,t);break;case"LogicalExpression":r=Y(r,e,t);break;case"ConditionalExpression":return Z(r,e,t);case"UnaryExpression":r=U(r,e,t);break}if(r.type==="MemberExpression"&&r.computed&&r.property){const n=r.object.type==="Identifier"&&e.isContextBound(r.object.name)&&!e.isRootParam(r.object.name)?r.object:V(r.object,e),a=r.property.type==="Identifier"&&!e.isContextBound(r.property.name)&&!e.isLoopVariable(r.property.name)?V(r.property,e):r.property;return{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[n,a,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}if(r.type==="ObjectExpression"&&(r.properties=r.properties.map(n=>{if(n.value.name){const[a,o]=e.getVariable(n.value.name);return{type:"Property",key:{type:"Identifier",name:n.key.name},value:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return n})),r.type==="Identifier"){if(r.name==="na")return r.name="NaN",r;if(e.isContextBound(r.name)&&!e.isRootParam(r.name))return{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[r,k,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}return r?.type==="CallExpression"&&S(r,e),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[r.type==="Identifier"?V(r,e):r,k,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function S(r,t,e){if(!r._transformed){if(r.callee&&r.callee.type==="MemberExpression"&&r.callee.object&&r.callee.object.type==="Identifier"&&(t.isContextBound(r.callee.object.name)||r.callee.object.name==="math"||r.callee.object.name==="ta")){const n=r.callee.object.name;r.arguments=r.arguments.map(a=>a._isParamCall?a:O(a,n,t)),n==="ta"&&r.arguments.push(t.getNextTACallId()),r._transformed=!0}else r.callee&&r.callee.type==="Identifier"&&(r.arguments=r.arguments.map(n=>n._isParamCall?n:O(n,b,t)),r._transformed=!0);r.arguments.forEach(n=>{g.recursive(n,t,{Identifier(a,o,i){a.parent=o.parent,E(a,t);const s=a.parent&&a.parent.type==="BinaryExpression";(a.parent&&a.parent.type==="ConditionalExpression"||s)&&(a.type==="MemberExpression"?N(a,t):a.type==="Identifier"&&$(a))},CallExpression(a,o,i){a._transformed||S(a,o)},MemberExpression(a,o,i){I(a,"",t),a.object&&i(a.object,{parent:a,inNamespaceCall:o.inNamespaceCall})}})})}}function tt(r,t){const e=[];r.params.forEach(n=>{n.type==="Identifier"&&(t.addContextBoundVar(n.name,!1),e.push(n.name))}),r.body&&r.body.type==="BlockStatement"&&(t.pushScope("fn"),g.recursive(r.body,t,{BlockStatement(n,a,o){n.body.forEach(i=>o(i,a))},ReturnStatement(n,a){F(n,a)},VariableDeclaration(n,a){D(n,a)},Identifier(n,a){E(n,a)},CallExpression(n,a){S(n,a),n.arguments.forEach(o=>{o.type==="BinaryExpression"&&g.recursive(o,a,{CallExpression(i,s){S(i,s)},MemberExpression(i){I(i,"",a)}})})},MemberExpression(n){I(n,"",t)},AssignmentExpression(n,a){L(n,a)},ForStatement(n,a,o){q(n,a,o)},IfStatement(n,a,o){G(n,a,o)},BinaryExpression(n,a,o){g.recursive(n,a,{CallExpression(i,s){S(i,s)},MemberExpression(i){I(i,"",a)}})}}),t.popScope())}function $(r,t){Object.assign(r,{type:"MemberExpression",object:{type:"Identifier",name:r.name,start:r.start,end:r.end},property:{type:"Literal",value:0},computed:!0,_indexTransformed:!0})}function q(r,t,e){if(r.init&&r.init.type==="VariableDeclaration"){const n=r.init.declarations[0],a=n.id.name;t.addLoopVariable(a,a),r.init={type:"VariableDeclaration",kind:r.init.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:a},init:n.init}]},n.init&&g.recursive(n.init,t,{Identifier(o,i){t.isLoopVariable(o.name)||(t.pushScope("for"),E(o,i),t.popScope())},MemberExpression(o){t.pushScope("for"),I(o,"",t),t.popScope()}})}r.test&&g.recursive(r.test,t,{Identifier(n,a){!t.isLoopVariable(n.name)&&!n.computed&&(t.pushScope("for"),E(n,a),n.type==="Identifier"&&(n.computed=!0,$(n)),t.popScope())},MemberExpression(n){t.pushScope("for"),I(n,"",t),t.popScope()}}),r.update&&g.recursive(r.update,t,{Identifier(n,a){t.isLoopVariable(n.name)||(t.pushScope("for"),E(n,a),t.popScope())}}),t.pushScope("for"),e(r.body,t),t.popScope()}function et(r,t){g.recursive(r,t,{MemberExpression(e){I(e,"",t)},CallExpression(e,n){S(e,n)},Identifier(e,n){E(e,n);const a=t.getCurrentScopeType()==="if";t.isContextBound(e.name)&&!t.isRootParam(e.name)&&a&&$(e)}})}function G(r,t,e){r.test&&(t.pushScope("if"),et(r.test,t),t.popScope()),t.pushScope("if"),e(r.consequent,t),t.popScope(),r.alternate&&(t.pushScope("els"),e(r.alternate,t),t.popScope())}function nt(r){g.recursive(r,null,{VariableDeclaration(t,e,n){t.declarations&&t.declarations.length>0&&t.declarations.forEach(a=>{if(a.init&&a.init.type==="ArrowFunctionExpression"&&a.init.start!==0){const o={type:"FunctionDeclaration",id:a.id,params:a.init.params,body:a.init.body.type==="BlockStatement"?a.init.body:{type:"BlockStatement",body:[{type:"ReturnStatement",argument:a.init.body}]},async:a.init.async,generator:!1};Object.assign(t,o)}}),t.body&&t.body.body&&t.body.body.forEach(a=>n(a,e))}})}function rt(r,t){g.simple(r,{VariableDeclaration(e){e.declarations.forEach(n=>{const a=n.init&&n.init.type==="MemberExpression"&&n.init.object&&(n.init.object.name==="context"||n.init.object.name===b||n.init.object.name==="context2"),o=n.init&&n.init.type==="MemberExpression"&&n.init.object?.object&&(n.init.object.object.name==="context"||n.init.object.object.name===b||n.init.object.object.name==="context2");(a||o)&&(n.id.name&&t.addContextBoundVar(n.id.name),n.id.properties&&n.id.properties.forEach(i=>{i.key.name&&t.addContextBoundVar(i.key.name)}))})}})}function at(r){let t=typeof r=="function"?r.toString():r;const e=K.parse(t.trim(),{ecmaVersion:"latest",sourceType:"module"});nt(e);const n=new J;let a;rt(e,n),g.simple(e,{FunctionDeclaration(i){tt(i,n)},ArrowFunctionExpression(i){const s=i.start===0;s&&i.params&&i.params.length>0&&(a=i.params[0].name,i.params[0].name=b),X(i,n,s)},VariableDeclaration(i){i.declarations.forEach(s=>{if(s.id.type==="ArrayPattern"){const c=n.generateTempVar(),l={type:"VariableDeclaration",kind:i.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:c},init:s.init}]};s.id.elements?.forEach(h=>{h.type==="Identifier"&&n.addArrayPatternElement(h.name)});const m=s.id.elements.map((h,p)=>({type:"VariableDeclaration",kind:i.kind,declarations:[{type:"VariableDeclarator",id:h,init:{type:"MemberExpression",object:{type:"Identifier",name:c},property:{type:"Literal",value:p},computed:!0}}]}));Object.assign(i,{type:"BlockStatement",body:[l,...m]})}})},ForStatement(i){}}),g.recursive(e,n,{BlockStatement(i,s,c){i.body.forEach(l=>c(l,s))},ReturnStatement(i,s){F(i,s)},VariableDeclaration(i,s){D(i,s)},Identifier(i,s){E(i,s)},CallExpression(i,s){S(i,s)},MemberExpression(i){I(i,a,n)},AssignmentExpression(i,s){L(i,s)},FunctionDeclaration(i,s){},ForStatement(i,s,c){q(i,s,c)},IfStatement(i,s,c){G(i,s,c)}}),it(e);const o=z.generate(e);return new Function("",`return ${o}`)(this)}function it(r){g.simple(r,{BinaryExpression(t){if(t.operator==="=="||t.operator==="==="){const e=t.left,n=t.right;Object.assign(t,{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:"$.math"},property:{type:"Identifier",name:"__eq"},computed:!1},arguments:[e,n],_transformed:!0})}}})}var ot=Object.defineProperty,st=(r,t,e)=>t in r?ot(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,A=(r,t,e)=>st(r,typeof t!="symbol"?t+"":t,e);const pt=5e3;class ct{constructor(t,e,n,a,o,i){this.source=t,this.tickerId=e,this.timeframe=n,this.limit=a,this.sDate=o,this.eDate=i,A(this,"data",[]),A(this,"open",[]),A(this,"high",[]),A(this,"low",[]),A(this,"close",[]),A(this,"volume",[]),A(this,"hl2",[]),A(this,"hlc3",[]),A(this,"ohlc4",[]),A(this,"openTime",[]),A(this,"closeTime",[]),A(this,"_periods"),A(this,"_readyPromise",null),A(this,"_ready",!1),this._readyPromise=new Promise(s=>{this.loadMarketData(t,e,n,a,o,i).then(c=>{const l=c.slice(0,pt);this._periods=l.length,this.data=l;const m=l.map(d=>d.open),h=l.map(d=>d.close),p=l.map(d=>d.high),u=l.map(d=>d.low),y=l.map(d=>d.volume),x=l.map(d=>(d.high+d.low+d.close)/3),w=l.map(d=>(d.high+d.low)/2),M=l.map(d=>(d.high+d.low+d.open+d.close)/4),_=l.map(d=>d.openTime),C=l.map(d=>d.closeTime);this.open=m,this.close=h,this.high=p,this.low=u,this.volume=y,this.hl2=w,this.hlc3=x,this.ohlc4=M,this.openTime=_,this.closeTime=C,this._ready=!0,s(!0)})})}get periods(){return this._periods}async loadMarketData(t,e,n,a,o,i){return Array.isArray(t)?t:t.getMarketData(e,n,a,o,i)}async ready(){if(this._ready)return!0;if(!this._readyPromise)throw new Error("PineTS is not ready");return this._readyPromise}async run(t,e,n){await this.ready(),e||(e=this._periods);const a=new Vt({marketData:this.data,source:this.source,tickerId:this.tickerId,timeframe:this.timeframe,limit:this.limit,sDate:this.sDate,eDate:this.eDate});a.pineTSCode=t,a.useTACache=n;let o=at.bind(this)(t);a.data.close=[],a.data.open=[],a.data.high=[],a.data.low=[],a.data.volume=[],a.data.hl2=[],a.data.hlc3=[],a.data.ohlc4=[],a.data.openTime=[],a.data.closeTime=[];const i=["const","var","let","params"];for(let s=this._periods-e;s<this._periods;s++){a.idx=s,a.data.close.unshift(this.close[s]),a.data.open.unshift(this.open[s]),a.data.high.unshift(this.high[s]),a.data.low.unshift(this.low[s]),a.data.volume.unshift(this.volume[s]),a.data.hl2.unshift(this.hl2[s]),a.data.hlc3.unshift(this.hlc3[s]),a.data.ohlc4.unshift(this.ohlc4[s]),a.data.openTime.unshift(this.openTime[s]);const c=await o(a);if(typeof c=="object"){typeof a.result!="object"&&(a.result={});for(let l in c){a.result[l]===void 0&&(a.result[l]=[]);const m=Array.isArray(c[l])?c[l][0]:c[l];a.result[l].push(m)}}else Array.isArray(a.result)||(a.result=[]),a.result.push(c);for(let l of i)for(let m in a[l])if(Array.isArray(a[l][m])){const h=a[l][m][0];a[l][m].unshift(h)}}return a}}var lt=Object.defineProperty,mt=(r,t,e)=>t in r?lt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,ht=(r,t,e)=>mt(r,typeof t!="symbol"?t+"":t,e);class ut{constructor(t){this.context=t,ht(this,"color",{param:(e,n=0)=>Array.isArray(e)?e[n]:e,rgb:(e,n,a,o)=>o?`rgba(${e}, ${n}, ${a}, ${o})`:`rgb(${e}, ${n}, ${a})`,new:(e,n)=>{if(e&&e.startsWith("#")){const a=e.slice(1),o=parseInt(a.slice(0,2),16),i=parseInt(a.slice(2,4),16),s=parseInt(a.slice(4,6),16);return n?`rgba(${o}, ${i}, ${s}, ${n})`:`rgb(${o}, ${i}, ${s})`}return n?`rgba(${e}, ${n})`:e},white:"white",lime:"lime",green:"green",red:"red",maroon:"maroon",black:"black",gray:"gray",blue:"blue"})}extractPlotOptions(t){const e={};for(let n in t)Array.isArray(t[n])?e[n]=t[n][0]:e[n]=t[n];return e}indicator(t,e,n){}plotchar(t,e,n){this.context.plots[e]||(this.context.plots[e]={data:[],options:this.extractPlotOptions(n),title:e}),this.context.plots[e].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:t[0],options:{...this.extractPlotOptions(n),style:"char"}})}plot(t,e,n){this.context.plots[e]||(this.context.plots[e]={data:[],options:this.extractPlotOptions(n),title:e}),this.context.plots[e].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:t[0],options:this.extractPlotOptions(n)})}na(t){return Array.isArray(t)?isNaN(t[0]):isNaN(t)}nz(t,e=0){const n=Array.isArray(t)?t[0]:t,a=Array.isArray(t)?e[0]:e;return isNaN(n)?a:n}}class yt{constructor(t){this.context=t}param(t,e=0){return Array.isArray(t)?[t[e]]:[t]}any(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}int(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}float(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}bool(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}string(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}timeframe(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}time(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}price(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}session(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}source(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}symbol(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}text_area(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}enum(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}color(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}}var dt=Object.defineProperty,xt=(r,t,e)=>t in r?dt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,ft=(r,t,e)=>xt(r,typeof t!="symbol"?t+"":t,e);class bt{constructor(t){this.context=t,ft(this,"_cache",{})}param(t,e,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(t)?e?(this.context.params[n]=t.slice(e),this.context.params[n].length=t.length,this.context.params[n]):(this.context.params[n]=t.slice(0),this.context.params[n]):(this.context.params[n][0]=t,this.context.params[n])}__eq(t,e){return Math.abs(t-e)<1e-8}abs(t){return Math.abs(t[0])}pow(t,e){return Math.pow(t[0],e[0])}sqrt(t){return Math.sqrt(t[0])}log(t){return Math.log(t[0])}ln(t){return Math.log(t[0])}exp(t){return Math.exp(t[0])}floor(t){return Math.floor(t[0])}ceil(t){return Math.ceil(t[0])}round(t){return Math.round(t[0])}random(){return Math.random()}max(...t){const e=t.map(n=>Array.isArray(n)?n[0]:n);return Math.max(...e)}min(...t){const e=t.map(n=>Array.isArray(n)?n[0]:n);return Math.min(...e)}sum(t,e){const n=Array.isArray(e)?e[0]:e;return Array.isArray(t)?t.slice(0,n).reduce((a,o)=>a+o,0):t}sin(t){return Math.sin(t[0])}cos(t){return Math.cos(t[0])}tan(t){return Math.tan(t[0])}acos(t){return Math.acos(t[0])}asin(t){return Math.asin(t[0])}atan(t){return Math.atan(t[0])}avg(...t){const e=t.map(n=>Array.isArray(n)?n[0]:n);return e.reduce((n,a)=>{const o=Array.isArray(n)?n[0]:n,i=Array.isArray(a)?a[0]:a;return o+i},0)/e.length}}var gt=Object.defineProperty,wt=(r,t,e)=>t in r?gt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,At=(r,t,e)=>wt(r,typeof t!="symbol"?t+"":t,e);const R=["1","3","5","15","30","45","60","120","180","240","D","W","M"];class vt{constructor(t){this.context=t,At(this,"_cache",{})}param(t,e,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(t)?(e?this.context.params[n]=t.slice(e):this.context.params[n]=t.slice(0),[t[e],n]):(this.context.params[n][0]=t,[t,n])}async security(t,e,n,a=!1,o=!1,i=!1,s=null,c=null){const l=t[0],m=e[0],h=n[0],p=n[1],u=R.indexOf(this.context.timeframe),y=R.indexOf(m);if(u==-1||y==-1)throw new Error("Invalid timeframe");if(u>y)throw new Error("Only higher timeframes are supported for now");if(u===y)return h;const x=this.context.data.openTime[0],w=this.context.data.closeTime[0];if(this.context.cache[p]){const C=this.context.cache[p],d=this._findSecContextIdx(x,w,C.data.openTime,C.data.closeTime,o);return d==-1?NaN:C.params[p][d]}const M=await new ct(this.context.source,l,m,this.context.limit||1e3,this.context.sDate,this.context.eDate).run(this.context.pineTSCode);this.context.cache[p]=M;const _=this._findSecContextIdx(x,w,M.data.openTime,M.data.closeTime,o);return _==-1?NaN:M.params[p][_]}_findSecContextIdx(t,e,n,a,o=!1){for(let i=0;i<n.length;i++)if(n[i]<=t&&e<=a[i])return i+(o?1:2);return-1}}class Et{constructor(t){this.context=t}get tr(){return this.context.math.max(this.context.data.high[0]-this.context.data.low[0],this.context.math.abs(this.context.data.high[0]-this.context.data.close[1]),this.context.math.abs(this.context.data.low[0]-this.context.data.close[1]))}param(t,e,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(t)?e?(this.context.params[n]=t.slice(e),this.context.params[n].length=t.length,this.context.params[n]):(this.context.params[n]=t.slice(0),this.context.params[n]):(this.context.params[n][0]=t,this.context.params[n])}ema(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`ema_${a}`;this.context.taState[o]||(this.context.taState[o]={prevEma:null,initSum:0,initCount:0});const i=this.context.taState[o],s=t[0];if(i.initCount<a)return i.initSum+=s,i.initCount++,i.initCount===a?(i.prevEma=i.initSum/a,this.context.precision(i.prevEma)):NaN;const c=2/(a+1),l=s*c+i.prevEma*(1-c);return i.prevEma=l,this.context.precision(l)}sma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`sma_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[],sum:0});const i=this.context.taState[o],s=t[0]||0;if(i.window.unshift(s),i.sum+=s,i.window.length<a)return NaN;if(i.window.length>a){const l=i.window.pop();i.sum-=l}const c=i.sum/a;return this.context.precision(c)}vwma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`vwma_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[],volumeWindow:[]});const i=this.context.taState[o],s=t[0],c=this.context.data.volume[0];if(i.window.unshift(s),i.volumeWindow.unshift(c),i.window.length<a)return NaN;i.window.length>a&&(i.window.pop(),i.volumeWindow.pop());let l=0,m=0;for(let p=0;p<a;p++)l+=i.window[p]*i.volumeWindow[p],m+=i.volumeWindow[p];const h=l/m;return this.context.precision(h)}wma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`wma_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();let c=0,l=0;for(let h=0;h<a;h++){const p=a-h;c+=i.window[h]*p,l+=p}const m=c/l;return this.context.precision(m)}hma(t,e,n){const a=Array.isArray(e)?e[0]:e,o=Math.floor(a/2),i=Math.floor(Math.sqrt(a)),s=this.wma(t,o,n?`${n}_wma1`:void 0),c=this.wma(t,a,n?`${n}_wma2`:void 0);if(isNaN(s)||isNaN(c))return NaN;this.context.taState||(this.context.taState={});const l=n||`hma_raw_${a}`;this.context.taState[l]||(this.context.taState[l]=[]);const m=2*s-c;this.context.taState[l].unshift(m);const h=n?`${n}_hma_final`:`hma_final_${a}`;this.context.taState[h]||(this.context.taState[h]={window:[]});const p=this.context.taState[h];if(p.window.unshift(m),p.window.length<i)return NaN;p.window.length>i&&p.window.pop();let u=0,y=0;for(let w=0;w<i;w++){const M=i-w;u+=p.window[w]*M,y+=M}const x=u/y;return this.context.precision(x)}rma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`rma_${a}`;this.context.taState[o]||(this.context.taState[o]={prevRma:null,initSum:0,initCount:0});const i=this.context.taState[o],s=t[0]||0;if(i.initCount<a)return i.initSum+=s,i.initCount++,i.initCount===a?(i.prevRma=i.initSum/a,this.context.precision(i.prevRma)):NaN;const c=1/a,l=s*c+i.prevRma*(1-c);return i.prevRma=l,this.context.precision(l)}change(t,e=1,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`change_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<=a)return NaN;i.window.length>a+1&&i.window.pop();const c=s-i.window[a];return this.context.precision(c)}rsi(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`rsi_${a}`;this.context.taState[o]||(this.context.taState[o]={prevValue:null,avgGain:0,avgLoss:0,initGains:[],initLosses:[]});const i=this.context.taState[o],s=t[0];if(i.prevValue!==null){const c=s-i.prevValue,l=c>0?c:0,m=c<0?-c:0;if(i.initGains.length<a){if(i.initGains.push(l),i.initLosses.push(m),i.initGains.length===a){i.avgGain=i.initGains.reduce((u,y)=>u+y,0)/a,i.avgLoss=i.initLosses.reduce((u,y)=>u+y,0)/a,i.prevValue=s;const p=i.avgLoss===0?100:100-100/(1+i.avgGain/i.avgLoss);return this.context.precision(p)}return i.prevValue=s,NaN}i.avgGain=(i.avgGain*(a-1)+l)/a,i.avgLoss=(i.avgLoss*(a-1)+m)/a;const h=i.avgLoss===0?100:100-100/(1+i.avgGain/i.avgLoss);return i.prevValue=s,this.context.precision(h)}return i.prevValue=s,NaN}atr(t,e){const n=Array.isArray(t)?t[0]:t;this.context.taState||(this.context.taState={});const a=e||`atr_${n}`;this.context.taState[a]||(this.context.taState[a]={prevAtr:null,initSum:0,initCount:0,prevClose:null});const o=this.context.taState[a],i=this.context.data.high[0],s=this.context.data.low[0],c=this.context.data.close[0];let l;if(o.prevClose!==null){const h=i-s,p=Math.abs(i-o.prevClose),u=Math.abs(s-o.prevClose);l=Math.max(h,p,u)}else l=i-s;if(o.prevClose=c,o.initCount<n)return o.initSum+=l,o.initCount++,o.initCount===n?(o.prevAtr=o.initSum/n,this.context.precision(o.prevAtr)):NaN;const m=(o.prevAtr*(n-1)+l)/n;return o.prevAtr=m,this.context.precision(m)}mom(t,e,n){const a=Array.isArray(e)?e[0]:e;return this.change(t,a)}roc(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`roc_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<=a)return NaN;i.window.length>a+1&&i.window.pop();const c=i.window[a],l=(s-c)/c*100;return this.context.precision(l)}dev(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`dev_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[],sum:0});const i=this.context.taState[o],s=t[0]||0;if(i.window.unshift(s),i.sum+=s,i.window.length<a)return NaN;if(i.window.length>a){const h=i.window.pop();i.sum-=h}const c=i.sum/a;let l=0;for(let h=0;h<a;h++)l+=Math.abs(i.window[h]-c);const m=l/a;return this.context.precision(m)}variance(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`variance_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();let c=0,l=0;for(let p=0;p<a;p++)c+=i.window[p],l+=i.window[p]*i.window[p];const m=c/a,h=l/a-m*m;return this.context.precision(h)}highest(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`highest_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=Math.max(...i.window.filter(l=>!isNaN(l)));return this.context.precision(c)}lowest(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`lowest_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=i.window.filter(m=>!isNaN(m)&&m!==void 0),l=c.length>0?Math.min(...c):NaN;return this.context.precision(l)}median(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`median_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=i.window.slice().sort((h,p)=>h-p),l=Math.floor(a/2),m=a%2===0?(c[l-1]+c[l])/2:c[l];return this.context.precision(m)}stdev(t,e,n=!0,a){const o=Array.isArray(e)?e[0]:e,i=Array.isArray(n)?n[0]:n;this.context.taState||(this.context.taState={});const s=a||`stdev_${o}_${i}`;this.context.taState[s]||(this.context.taState[s]={window:[],sum:0});const c=this.context.taState[s],l=t[0];if(c.window.unshift(l),c.sum+=l,c.window.length<o)return NaN;if(c.window.length>o){const y=c.window.pop();c.sum-=y}const m=c.sum/o;let h=0;for(let y=0;y<o;y++)h+=Math.pow(c.window[y]-m,2);const p=i?o:o-1,u=Math.sqrt(h/p);return this.context.precision(u)}linreg(t,e,n,a){const o=Array.isArray(e)?e[0]:e,i=Array.isArray(n)?n[0]:n;this.context.taState||(this.context.taState={});const s=a||`linreg_${o}_${i}`;this.context.taState[s]||(this.context.taState[s]={window:[]});const c=this.context.taState[s],l=t[0];if(c.window.unshift(l),c.window.length<o)return NaN;c.window.length>o&&c.window.pop();let m=0,h=0,p=0,u=0;const y=o;for(let _=0;_<o;_++){const C=o-1-_,d=c.window[_];m+=C,h+=d,p+=C*d,u+=C*C}const x=y*u-m*m;if(x===0)return NaN;const w=(y*p-m*h)/x,M=(h-w*m)/y+w*(o-1-i);return this.context.precision(M)}supertrend(t,e,n){const a=Array.isArray(t)?t[0]:t,o=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const i=`supertrend_${a}_${o}`;this.context.taState[i]||(this.context.taState[i]={prevUpperBand:null,prevLowerBand:null,prevSupertrend:null,prevDirection:null});const s=this.context.taState[i],c=this.context.data.high[0],l=this.context.data.low[0],m=this.context.data.close[0],h=this.atr(o,n?`${n}_atr`:void 0);if(isNaN(h))return[[NaN,0]];const p=(c+l)/2;let u=p+a*h,y=p-a*h;s.prevUpperBand!==null&&(u<s.prevUpperBand||this.context.data.close[1]>s.prevUpperBand?u=u:u=s.prevUpperBand,y>s.prevLowerBand||this.context.data.close[1]<s.prevLowerBand?y=y:y=s.prevLowerBand);let x,w;return s.prevSupertrend===null?(x=m<=u?-1:1,w=x===-1?u:y):s.prevSupertrend===s.prevUpperBand?m>u?(x=1,w=y):(x=-1,w=u):m<y?(x=-1,w=u):(x=1,w=y),s.prevUpperBand=u,s.prevLowerBand=y,s.prevSupertrend=w,s.prevDirection=x,[[this.context.precision(w),x]]}crossover(t,e){const n=Array.isArray(t)?t[0]:t,a=Array.isArray(e)?e[0]:e,o=Array.isArray(t)?t[1]:this.context.data.series[t][1],i=Array.isArray(e)?e[1]:this.context.data.series[e][1];return o<i&&n>a}crossunder(t,e){const n=Array.isArray(t)?t[0]:t,a=Array.isArray(e)?e[0]:e,o=Array.isArray(t)?t[1]:this.context.data.series[t][1],i=Array.isArray(e)?e[1]:this.context.data.series[e][1];return o>i&&n<a}pivothigh(t,e,n){n==null&&(n=e,e=t,t=this.context.data.high);const a=Array.isArray(e)?e[0]:e,o=Array.isArray(n)?n[0]:n,i=St(t.slice(0).reverse(),a,o),s=this.context.idx;return this.context.precision(i[s])}pivotlow(t,e,n){n==null&&(n=e,e=t,t=this.context.data.low);const a=Array.isArray(e)?e[0]:e,o=Array.isArray(n)?n[0]:n,i=jt(t.slice(0).reverse(),a,o),s=this.context.idx;return this.context.precision(i[s])}}function St(r,t,e){const n=new Array(r.length).fill(NaN);for(let a=t+e;a<r.length;a++){const o=r[a-e];let i=!0;for(let s=1;s<=t;s++)if(r[a-e-s]>=o){i=!1;break}if(i){for(let s=1;s<=e;s++)if(r[a-e+s]>=o){i=!1;break}}i&&(n[a]=o)}return n}function jt(r,t,e){const n=new Array(r.length).fill(NaN);for(let a=t+e;a<r.length;a++){const o=r[a-e];let i=!0;for(let s=1;s<=t;s++)if(r[a-e-s]<=o){i=!1;break}if(i){for(let s=1;s<=e;s++)if(r[a-e+s]<=o){i=!1;break}}i&&(n[a]=o)}return n}var It=Object.defineProperty,Mt=(r,t,e)=>t in r?It(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,Ct=(r,t,e)=>Mt(r,typeof t!="symbol"?t+"":t,e);class v{constructor(t){this.array=t}}class Nt{constructor(t){this.context=t,Ct(this,"_cache",{})}param(t,e=0){return Array.isArray(t)?t[e]:t}get(t,e){return t.array[e]}set(t,e,n){t.array[e]=n}push(t,e){t.array.push(e)}sum(t){return t.array.reduce((e,n)=>e+(isNaN(n)?0:n),0)}avg(t){return this.sum(t)/t.array.length}min(t,e=0){return[...t.array].sort((n,a)=>n-a)[e]??this.context.NA}max(t,e=0){return[...t.array].sort((n,a)=>a-n)[e]??this.context.NA}size(t){return t.array.length}new_bool(t,e=!1){return new v(Array(t).fill(e))}new_float(t,e=NaN){return new v(Array(t).fill(e))}new_int(t,e=0){return new v(Array(t).fill(Math.round(e)))}new_string(t,e=""){return new v(Array(t).fill(e))}new(t,e){return new v(Array(t).fill(e))}slice(t,e,n){const a=n!==void 0?n+1:void 0;return new v(t.array.slice(e,a))}reverse(t){t.array.reverse()}includes(t,e){return t.array.includes(e)}indexof(t,e){return t.array.indexOf(e)}lastindexof(t,e){return t.array.lastIndexOf(e)}stdev(t,e=!0){const n=this.avg(t),a=t.array.map(i=>Math.pow(i-n,2)),o=e?t.array.length:t.array.length-1;return Math.sqrt(this.sum(new v(a))/o)}variance(t,e=!0){const n=this.avg(t),a=t.array.map(i=>Math.pow(i-n,2)),o=e?t.array.length:t.array.length-1;return this.sum(new v(a))/o}covariance(t,e,n=!0){if(t.array.length!==e.array.length||t.array.length<2)return NaN;const a=n?t.array.length:t.array.length-1,o=this.avg(t),i=this.avg(e);let s=0;for(let c=0;c<t.array.length;c++)s+=(t.array[c]-o)*(e.array[c]-i);return s/a}first(t){return t.array.length>0?t.array[0]:this.context.NA}last(t){return t.array.length>0?t.array[t.array.length-1]:this.context.NA}clear(t){t.array.length=0}join(t,e=","){return t.array.join(e)}abs(t){return new v(t.array.map(e=>Math.abs(e)))}concat(t,e){return t.array.push(...e.array),t}copy(t){return new v([...t.array])}every(t,e){return t.array.every(e)}fill(t,e,n=0,a){const o=t.array.length,i=a!==void 0?Math.min(a,o):o;for(let s=n;s<i;s++)t.array[s]=e}from(t){return new v([...t])}insert(t,e,n){t.array.splice(e,0,n)}pop(t){return t.array.pop()}range(t){return this.max(t)-this.min(t)}remove(t,e){return e>=0&&e<t.array.length?t.array.splice(e,1)[0]:this.context.NA}shift(t){return t.array.shift()}sort(t,e="asc"){t.array.sort((n,a)=>e==="asc"?n-a:a-n)}sort_indices(t,e){const n=t.array.map((a,o)=>o);return n.sort((a,o)=>{const i=t.array[a],s=t.array[o];return e?e(i,s):i-s}),new v(n)}standardize(t){const e=this.avg(t),n=this.stdev(t);return n===0?new v(t.array.map(()=>0)):new v(t.array.map(a=>(a-e)/n))}unshift(t,e){t.array.unshift(e)}some(t,e){return t.array.some(e)}}var _t=Object.defineProperty,$t=(r,t,e)=>t in r?_t(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,f=(r,t,e)=>$t(r,typeof t!="symbol"?t+"":t,e);class Vt{constructor({marketData:t,source:e,tickerId:n,timeframe:a,limit:o,sDate:i,eDate:s}){f(this,"data",{open:[],high:[],low:[],close:[],volume:[],hl2:[],hlc3:[],ohlc4:[]}),f(this,"cache",{}),f(this,"taState",{}),f(this,"useTACache",!1),f(this,"NA",NaN),f(this,"math"),f(this,"ta"),f(this,"input"),f(this,"request"),f(this,"array"),f(this,"core"),f(this,"lang"),f(this,"idx",0),f(this,"params",{}),f(this,"const",{}),f(this,"var",{}),f(this,"let",{}),f(this,"result"),f(this,"plots",{}),f(this,"marketData"),f(this,"source"),f(this,"tickerId"),f(this,"timeframe",""),f(this,"limit"),f(this,"sDate"),f(this,"eDate"),f(this,"pineTSCode"),this.marketData=t,this.source=e,this.tickerId=n,this.timeframe=a,this.limit=o,this.sDate=i,this.eDate=s,this.math=new bt(this),this.ta=new Et(this),this.input=new yt(this),this.request=new vt(this),this.array=new Nt(this);const c=new ut(this);this.core={plotchar:c.plotchar.bind(c),na:c.na.bind(c),color:c.color,plot:c.plot.bind(c),nz:c.nz.bind(c)}}init(t,e,n=0){return t?!Array.isArray(e)||Array.isArray(e[0])?t[0]=Array.isArray(e?.[0])?e[0]:this.precision(e):t[0]=this.precision(e[e.length-this.idx-1+n]):Array.isArray(e)?t=[this.precision(e[e.length-this.idx-1+n])]:t=[this.precision(e)],t}precision(t,e=10){return typeof t!="number"||isNaN(t)?t:Number(t.toFixed(e))}param(t,e,n){return typeof t=="string"||!Array.isArray(t)&&typeof t=="object"?t:(this.params[n]||(this.params[n]=[]),Array.isArray(t)?e?(this.params[n]=t.slice(e),this.params[n].length=t.length,this.params[n]):(this.params[n]=t.slice(0),this.params[n]):(this.params[n][0]=t,this.params[n]))}}var Bt=Object.defineProperty,Pt=(r,t,e)=>t in r?Bt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,T=(r,t,e)=>Pt(r,typeof t!="symbol"?t+"":t,e);const kt="https://api.binance.com/api/v3",P={1:"1m",3:"3m",5:"5m",15:"15m",30:"30m",45:null,60:"1h",120:"2h",180:null,240:"4h","4H":"4h","1D":"1d",D:"1d","1W":"1w",W:"1w","1M":"1M",M:"1M"};class Tt{constructor(t=5*60*1e3){T(this,"cache"),T(this,"cacheDuration"),this.cache=new Map,this.cacheDuration=t}generateKey(t){return Object.entries(t).filter(([e,n])=>n!==void 0).map(([e,n])=>`${e}:${n}`).join("|")}get(t){const e=this.generateKey(t),n=this.cache.get(e);return n?Date.now()-n.timestamp>this.cacheDuration?(this.cache.delete(e),null):n.data:null}set(t,e){const n=this.generateKey(t);this.cache.set(n,{data:e,timestamp:Date.now()})}clear(){this.cache.clear()}cleanup(){const t=Date.now();for(const[e,n]of this.cache.entries())t-n.timestamp>this.cacheDuration&&this.cache.delete(e)}}class Dt{constructor(){T(this,"cacheManager"),this.cacheManager=new Tt(5*60*1e3)}async getMarketDataInterval(t,e,n,a){try{const o=P[e.toUpperCase()];if(!o)return console.error(`Unsupported timeframe: ${e}`),[];const i={"1m":60*1e3,"3m":3*60*1e3,"5m":5*60*1e3,"15m":15*60*1e3,"30m":30*60*1e3,"1h":60*60*1e3,"2h":2*60*60*1e3,"4h":4*60*60*1e3,"1d":24*60*60*1e3,"1w":7*24*60*60*1e3,"1M":30*24*60*60*1e3};let s=[],c=n;const l=a,m=i[o];if(!m)return console.error(`Duration not defined for interval: ${o}`),[];for(;c<l;){const h=Math.min(c+1e3*m,l),p=await this.getMarketData(t,e,1e3,c,h);if(p.length===0)break;s=s.concat(p),c=p[p.length-1].closeTime+1}return s}catch(o){return console.error("Error in getMarketDataInterval:",o),[]}}async getMarketData(t,e,n,a,o){try{const i={tickerId:t,timeframe:e,limit:n,sDate:a,eDate:o},s=this.cacheManager.get(i);if(s)return console.log("cache hit",t,e,n,a,o),s;const c=P[e.toUpperCase()];if(!c)return console.error(`Unsupported timeframe: ${e}`),[];if(this.shouldPaginate(e,n,a,o)&&a&&o){const p=await this.getMarketDataInterval(t,e,a,o),u=n?p.slice(0,n):p;return this.cacheManager.set(i,u),u}let l=`${kt}/klines?symbol=${t}&interval=${c}`;n&&(l+=`&limit=${Math.min(n,1e3)}`),a&&(l+=`&startTime=${a}`),o&&(l+=`&endTime=${o}`);const m=await fetch(l);if(!m.ok)throw new Error(`HTTP error! status: ${m.status}`);const h=(await m.json()).map(p=>({openTime:parseInt(p[0]),open:parseFloat(p[1]),high:parseFloat(p[2]),low:parseFloat(p[3]),close:parseFloat(p[4]),volume:parseFloat(p[5]),closeTime:parseInt(p[6]),quoteAssetVolume:parseFloat(p[7]),numberOfTrades:parseInt(p[8]),takerBuyBaseAssetVolume:parseFloat(p[9]),takerBuyQuoteAssetVolume:parseFloat(p[10]),ignore:p[11]}));return this.cacheManager.set(i,h),h}catch(i){return console.error("Error in binance.klines:",i),[]}}shouldPaginate(t,e,n,a){if(e&&e>1e3)return!0;if(n&&a){const o=P[t.toUpperCase()],i={"1m":60*1e3,"3m":3*60*1e3,"5m":5*60*1e3,"15m":15*60*1e3,"30m":30*60*1e3,"1h":60*60*1e3,"2h":2*60*60*1e3,"4h":4*60*60*1e3,"1d":24*60*60*1e3,"1w":7*24*60*60*1e3,"1M":30*24*60*60*1e3}[o];if(i)return Math.ceil((a-n)/i)>1e3}return!1}}const Lt={Binance:new Dt};export{Vt as Context,ct as PineTS,Lt as Provider};
18
+ import*as re from"acorn";import*as ne from"astring";import*as j from"acorn-walk";import*as R from"fs";import*as F from"path";import{fileURLToPath as ae}from"url";var ie=Object.defineProperty,oe=(t,e,r)=>e in t?ie(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,_=(t,e,r)=>oe(t,typeof e!="symbol"?e+"":e,r);class se{constructor(){_(this,"scopes",[]),_(this,"scopeTypes",[]),_(this,"scopeCounts",new Map),_(this,"contextBoundVars",new Set),_(this,"arrayPatternElements",new Set),_(this,"rootParams",new Set),_(this,"varKinds",new Map),_(this,"loopVars",new Set),_(this,"loopVarNames",new Map),_(this,"paramIdCounter",0),_(this,"cacheIdCounter",0),_(this,"tempVarCounter",0),_(this,"taCallIdCounter",0),this.pushScope("glb")}get nextParamIdArg(){return{type:"Identifier",name:`'p${this.paramIdCounter++}'`}}get nextCacheIdArg(){return{type:"Identifier",name:`'cache_${this.cacheIdCounter++}'`}}getNextTACallId(){return{type:"Literal",value:`_ta${this.taCallIdCounter++}`}}pushScope(e){this.scopes.push(new Map),this.scopeTypes.push(e),this.scopeCounts.set(e,(this.scopeCounts.get(e)||0)+1)}popScope(){this.scopes.pop(),this.scopeTypes.pop()}getCurrentScopeType(){return this.scopeTypes[this.scopeTypes.length-1]}getCurrentScopeCount(){return this.scopeCounts.get(this.getCurrentScopeType())||1}addContextBoundVar(e,r=!1){this.contextBoundVars.add(e),r&&this.rootParams.add(e)}removeContextBoundVar(e){this.contextBoundVars.has(e)&&(this.contextBoundVars.delete(e),this.rootParams.has(e)&&this.rootParams.delete(e))}addArrayPatternElement(e){this.arrayPatternElements.add(e)}isContextBound(e){return this.contextBoundVars.has(e)}isArrayPatternElement(e){return this.arrayPatternElements.has(e)}isRootParam(e){return this.rootParams.has(e)}addLoopVariable(e,r){this.loopVars.add(e),this.loopVarNames.set(e,r)}getLoopVariableName(e){return this.loopVarNames.get(e)}isLoopVariable(e){return this.loopVars.has(e)}addVariable(e,r){if(this.isContextBound(e))return e;const n=this.scopes[this.scopes.length-1],a=this.scopeTypes[this.scopeTypes.length-1],o=this.scopeCounts.get(a)||1,i=`${a}${o}_${e}`;return n.set(e,i),this.varKinds.set(i,r),i}getVariable(e){if(this.loopVars.has(e)){const r=this.loopVarNames.get(e);if(r)return[r,"let"]}if(this.isContextBound(e))return[e,"let"];for(let r=this.scopes.length-1;r>=0;r--){const n=this.scopes[r];if(n.has(e)){const a=n.get(e),o=this.varKinds.get(a)||"let";return[a,o]}}return[e,"let"]}generateTempVar(){return`temp_${++this.tempVarCounter}`}}const A="$",y={createIdentifier(t){return{type:"Identifier",name:t}},createLiteral(t){return{type:"Literal",value:t}},createMemberExpression(t,e,r=!1){return{type:"MemberExpression",object:t,property:e,computed:r}},createContextIdentifier(){return this.createIdentifier(A)},createContextVariableReference(t,e){const r=this.createContextIdentifier(),n=this.createIdentifier(t),a=this.createIdentifier(e);return this.createMemberExpression(this.createMemberExpression(r,n,!1),a,!1)},createContextVariableAccess0(t,e){const r=this.createContextVariableReference(t,e);return this.createArrayAccess(r,0)},createArrayAccess(t,e){const r=typeof e=="number"?this.createLiteral(e):e;return this.createMemberExpression(t,r,!0)},createCallExpression(t,e){return{type:"CallExpression",callee:t,arguments:e}},createAssignmentExpression(t,e,r="="){return{type:"AssignmentExpression",operator:r,left:t,right:e}},createExpressionStatement(t){return{type:"ExpressionStatement",expression:t}},createInitCall(t,e,r){const n=this.createMemberExpression(this.createContextIdentifier(),this.createIdentifier("init"),!1),a=[t,e];return r&&a.push(r),this.createCallExpression(n,a)},createGetCall(t,e){const r=this.createMemberExpression(this.createContextIdentifier(),this.createIdentifier("get"),!1),n=typeof e=="number"?this.createLiteral(e):e;return this.createCallExpression(r,[t,n])},createSetCall(t,e){const r=this.createMemberExpression(this.createContextIdentifier(),this.createIdentifier("set"),!1);return this.createCallExpression(r,[t,e])},createMathEqCall(t,e){const r=this.createMemberExpression(this.createContextIdentifier(),this.createIdentifier("math"),!1),n=this.createMemberExpression(r,this.createIdentifier("__eq"),!1);return this.createCallExpression(n,[t,e])},createWrapperFunction(t){return{type:"FunctionDeclaration",id:null,params:[this.createIdentifier("context")],body:{type:"BlockStatement",body:[{type:"ReturnStatement",argument:t}]}}}};function ce(t){j.recursive(t,null,{VariableDeclaration(e,r,n){e.declarations&&e.declarations.length>0&&e.declarations.forEach(a=>{if(a.init&&a.init.type==="ArrowFunctionExpression"&&a.init.start!==0){const o={type:"FunctionDeclaration",id:a.id,params:a.init.params,body:a.init.body.type==="BlockStatement"?a.init.body:{type:"BlockStatement",body:[{type:"ReturnStatement",argument:a.init.body}]},async:a.init.async,generator:!1};Object.assign(e,o)}}),e.body&&e.body.body&&e.body.body.forEach(a=>n(a,r))}})}function pe(t,e){j.simple(t,{VariableDeclaration(r){r.declarations.forEach(n=>{const a=n.init&&n.init.type==="MemberExpression"&&n.init.object&&(n.init.object.name==="context"||n.init.object.name===A||n.init.object.name==="context2"),o=n.init&&n.init.type==="MemberExpression"&&n.init.object?.object&&(n.init.object.object.name==="context"||n.init.object.object.name===A||n.init.object.object.name==="context2");(a||o)&&(n.id.name&&e.addContextBoundVar(n.id.name),n.id.properties&&n.id.properties.forEach(i=>{i.key.name&&e.addContextBoundVar(i.key.name)}))})}})}function le(t,e,r=!1){t.params.forEach(n=>{n.type==="Identifier"&&e.addContextBoundVar(n.name,r)})}function ue(t,e){t.params.forEach(r=>{r.type==="Identifier"&&e.addContextBoundVar(r.name,!1)})}function me(t,e){let r;return j.simple(t,{FunctionDeclaration(n){ue(n,e)},ArrowFunctionExpression(n){const a=n.start===0;a&&n.params&&n.params.length>0&&(r=n.params[0].name,n.params[0].name=A),le(n,e,a)},VariableDeclaration(n){n.declarations.forEach(a=>{if(a.id.type==="ArrayPattern"){const o=e.generateTempVar(),i={type:"VariableDeclaration",kind:n.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:o},init:a.init}]};a.id.elements?.forEach(c=>{c.type==="Identifier"&&e.addArrayPatternElement(c.name)});const s=a.id.elements.map((c,u)=>({type:"VariableDeclaration",kind:n.kind,declarations:[{type:"VariableDeclarator",id:c,init:{type:"MemberExpression",object:{type:"Identifier",name:o},property:{type:"Literal",value:u},computed:!0}}]}));Object.assign(n,{type:"BlockStatement",body:[i,...s]})}})},ForStatement(n){}}),r}const U={type:"Identifier",name:"undefined"};function k(t,e){if(t.computed&&t.property.type==="Identifier"){if(e.isLoopVariable(t.property.name))return;if(!e.isContextBound(t.property.name)){const[r,n]=e.getVariable(t.property.name);t.property=y.createContextVariableReference(n,r),t.property=y.createGetCall(t.property,0)}}if(t.computed&&t.object.type==="Identifier"){if(e.isLoopVariable(t.object.name))return;if(!e.isContextBound(t.object.name)){const[r,n]=e.getVariable(t.object.name);t.object=y.createContextVariableReference(n,r)}if(t.property.type==="MemberExpression"){const r=t.property;r._indexTransformed||(k(r,e),r._indexTransformed=!0)}}}function B(t,e){const r=y.createGetCall(y.createIdentifier(t.name),0);t.start!==void 0&&(r.start=t.start),t.end!==void 0&&(r.end=t.end),r._indexTransformed=!0,Object.assign(t,r)}function $(t,e){if(t.name!==A){if(t.name==="Math"||t.name==="NaN"||t.name==="undefined"||t.name==="Infinity"||t.name==="null"||t.name.startsWith("'")&&t.name.endsWith("'")||t.name.startsWith('"')&&t.name.endsWith('"')||t.name.startsWith("`")&&t.name.endsWith("`")||e.isLoopVariable(t.name)||e.isContextBound(t.name)&&!e.isRootParam(t.name))return;const r=t.parent&&t.parent.type==="MemberExpression"&&t.parent.object===t&&e.isContextBound(t.name),n=t.parent&&t.parent.type==="CallExpression"&&t.parent.callee&&t.parent.callee.type==="MemberExpression"&&t.parent.callee.property.name==="param";t.parent&&t.parent.type==="AssignmentExpression"&&t.parent.left;const a=t.parent&&t.parent.type==="CallExpression"&&t.parent.callee&&t.parent.callee.type==="MemberExpression"&&e.isContextBound(t.parent.callee.object.name),o=t.parent&&t.parent.type==="MemberExpression"&&t.parent.computed,i=t.parent&&t.parent.type==="MemberExpression"&&t.parent.computed&&t.parent.property===t&&t.parent.parent&&t.parent.parent.type==="CallExpression"&&t.parent.parent.callee&&t.parent.parent.callee.type==="MemberExpression"&&e.isContextBound(t.parent.parent.callee.object.name),s=t.parent&&t.parent.type==="CallExpression"&&t.parent.callee===t;if(r||n||a||i||s){if(s)return;const[f,m]=e.getVariable(t.name),p=y.createContextVariableReference(m,f);Object.assign(t,p);return}const[c,u]=e.getVariable(t.name),h=y.createContextVariableReference(u,c);if(!(t.parent&&t.parent.type==="MemberExpression"&&t.parent.computed&&t.parent.object===t)&&!o){const f=y.createGetCall(h,0);Object.assign(t,f)}else Object.assign(t,h)}}function T(t,e,r){if(t.object&&t.object.type==="Identifier"&&t.object.name==="Math")return;const n=r.getCurrentScopeType()=="if",a=r.getCurrentScopeType()=="els",o=r.getCurrentScopeType()=="for";if(!n&&!a&&!o&&t.object&&t.object.type==="Identifier"&&r.isContextBound(t.object.name)&&!r.isRootParam(t.object.name)&&!t.computed)return;t._indexTransformed||(k(t,r),t._indexTransformed=!0);const i=t.object&&t.object.type==="MemberExpression"&&t.object.object&&t.object.object.type==="MemberExpression"&&t.object.object.object&&t.object.object.object.name===A,s=t.object&&t.object.type==="Identifier"&&r.isContextBound(t.object.name);if(t.computed&&(i||s)){if(t.parent&&t.parent.type==="AssignmentExpression"&&t.parent.left===t)return;const c=y.createGetCall(t.object,t.property);t.start&&(c.start=t.start),t.end&&(c.end=t.end),Object.assign(t,c)}}function L(t,e){if(t.type==="Identifier"){if(t.name==="na")return t.name="NaN",t;if(e.isLoopVariable(t.name))return t;if(e.isRootParam(t.name)){const[a,o]=e.getVariable(t.name);return y.createContextVariableReference(o,a)}if(e.isContextBound(t.name))return t;const[r,n]=e.getVariable(t.name);return y.createContextVariableReference(n,r)}return t}function O(t,e,r=""){switch(t.type){case"BinaryExpression":return Y(t,e,r);case"MemberExpression":return{type:"MemberExpression",object:t.object.type==="Identifier"?L(t.object,e):t.object,property:t.property,computed:t.computed};case"Identifier":{if(e.isLoopVariable(t.name)||t.parent&&t.parent.type==="MemberExpression"&&t.parent.property===t)return t;const n=L(t,e);return y.createGetCall(n,0)}case"UnaryExpression":return Z(t,e,r)}return t}function Y(t,e,r){const n=O(t.left,e,r),a=O(t.right,e,r),o={type:"BinaryExpression",operator:t.operator,left:n,right:a,start:t.start,end:t.end};return j.recursive(o,e,{CallExpression(i,s){i._transformed||D(i,s)},MemberExpression(i){T(i,"",e)}}),o}function he(t,e,r){const n=O(t.left,e,r),a=O(t.right,e,r),o={type:"LogicalExpression",operator:t.operator,left:n,right:a,start:t.start,end:t.end};return j.recursive(o,e,{CallExpression(i,s){i._transformed||D(i,s)}}),o}function fe(t,e,r){return j.recursive(t,{parent:t,inNamespaceCall:!1},{Identifier(n,a,o){if(n.name=="NaN")return;if(n.name=="na"){n.name="NaN";return}n.parent=a.parent,$(n,e);const i=n.parent&&n.parent.type==="BinaryExpression";(n.parent&&n.parent.type==="ConditionalExpression"||i)&&(n.type==="MemberExpression"?k(n,e):n.type==="Identifier"&&(n.parent&&n.parent.type==="CallExpression"&&n.parent.callee&&n.parent.callee.object&&n.parent.callee.object.name===A&&n.parent.callee.property.name==="get"||B(n)))},MemberExpression(n,a,o){k(n,e),n.object&&o(n.object,{parent:n,inNamespaceCall:a.inNamespaceCall})},CallExpression(n,a,o){const i=n.callee&&n.callee.type==="MemberExpression"&&n.callee.object&&n.callee.object.type==="Identifier"&&e.isContextBound(n.callee.object.name);D(n,e),n.arguments.forEach(s=>o(s,{parent:n,inNamespaceCall:i||a.inNamespaceCall}))}}),{type:"CallExpression",callee:y.createMemberExpression(y.createIdentifier(r),y.createIdentifier("param")),arguments:[t,U,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function Z(t,e,r){const n=O(t.argument,e,r);return{type:"UnaryExpression",operator:t.operator,prefix:t.prefix,argument:n,start:t.start,end:t.end}}function K(t,e,r){switch(t?.type){case"BinaryExpression":t=Y(t,r,e);break;case"LogicalExpression":t=he(t,r,e);break;case"ConditionalExpression":return fe(t,r,e);case"UnaryExpression":t=Z(t,r,e);break}if(t.type==="MemberExpression"&&t.computed&&t.property){const n=t.object.type==="Identifier"&&r.isContextBound(t.object.name)&&!r.isRootParam(t.object.name)?t.object:L(t.object,r),a=t.property.type==="Identifier"&&!r.isContextBound(t.property.name)&&!r.isLoopVariable(t.property.name)?L(t.property,r):t.property;return{type:"CallExpression",callee:y.createMemberExpression(y.createIdentifier(e),y.createIdentifier("param")),arguments:[n,a,r.nextParamIdArg],_transformed:!0,_isParamCall:!0}}if(t.type==="ObjectExpression"&&(t.properties=t.properties.map(n=>{if(n.value.name){const[a,o]=r.getVariable(n.value.name);return{type:"Property",key:{type:"Identifier",name:n.key.name},value:y.createContextVariableReference(o,a),kind:"init",method:!1,shorthand:!1,computed:!1}}return n})),t.type==="Identifier"){if(t.name==="na")return t.name="NaN",t;if(r.isContextBound(t.name)&&!r.isRootParam(t.name))return{type:"CallExpression",callee:y.createMemberExpression(y.createIdentifier(e),y.createIdentifier("param")),arguments:[t,U,r.nextParamIdArg],_transformed:!0,_isParamCall:!0}}return t?.type==="CallExpression"&&D(t,r),{type:"CallExpression",callee:y.createMemberExpression(y.createIdentifier(e),y.createIdentifier("param")),arguments:[t.type==="Identifier"?L(t,r):t,U,r.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function D(t,e,r){if(!t._transformed){if(t.callee&&t.callee.type==="MemberExpression"&&t.callee.object&&t.callee.object.type==="Identifier"&&(e.isContextBound(t.callee.object.name)||t.callee.object.name==="math"||t.callee.object.name==="ta")){if(t.callee.object.name===A&&["get","init","param"].includes(t.callee.property.name))return;const n=t.callee.object.name;t.arguments=t.arguments.map(a=>a._isParamCall?a:K(a,n,e)),n==="ta"&&t.arguments.push(e.getNextTACallId()),t._transformed=!0}else t.callee&&t.callee.type==="Identifier"&&(t.arguments=t.arguments.map(n=>n._isParamCall?n:K(n,A,e)),t._transformed=!0);t.arguments.forEach(n=>{j.recursive(n,e,{Identifier(a,o,i){a.parent=o.parent,$(a,e);const s=a.parent&&a.parent.type==="BinaryExpression";(a.parent&&a.parent.type==="ConditionalExpression"||s)&&(a.type==="MemberExpression"?k(a,e):a.type==="Identifier"&&(a.parent&&a.parent.type==="CallExpression"&&a.parent.callee&&a.parent.callee.object&&a.parent.callee.object.name===A&&a.parent.callee.property.name==="get"||B(a)))},CallExpression(a,o,i){a._transformed||D(a,o)},MemberExpression(a,o,i){T(a,"",e),a.object&&i(a.object,{parent:a,inNamespaceCall:o.inNamespaceCall})}})})}}function ee(t,e){let r=null;if(t.left.type==="Identifier"){const[n,a]=e.getVariable(t.left.name);r=y.createContextVariableReference(a,n)}else if(t.left.type==="MemberExpression"&&t.left.computed&&t.left.object.type==="Identifier"){const n=t.left.object.name,[a,o]=e.getVariable(n),i=a!==n,s=e.isContextBound(n);(i||s)&&!e.isLoopVariable(n)&&t.left.property.type==="Literal"&&t.left.property.value===0&&(r=y.createContextVariableReference(o,a))}if(j.recursive(t.right,{parent:t.right,inNamespaceCall:!1},{Identifier(n,a,o){n.name=="na"&&(n.name="NaN"),n.parent=a.parent,$(n,e);const i=n.parent&&n.parent.type==="BinaryExpression",s=n.parent&&n.parent.type==="ConditionalExpression",c=e.isContextBound(n.name)&&!e.isRootParam(n.name),u=n.parent&&n.parent.type==="MemberExpression"&&n.parent.computed&&n.parent.object===n,h=n.parent&&n.parent._isParamCall,f=n.parent&&n.parent.type==="MemberExpression",m=n.name==="NaN",p=n.parent&&n.parent.type==="CallExpression"&&n.parent.callee&&n.parent.callee.object&&n.parent.callee.object.name===A&&n.parent.callee.property.name==="get";(c||s||i)&&(n.type==="MemberExpression"?k(n,e):n.type==="Identifier"&&!f&&!u&&!h&&!m&&!p&&B(n))},MemberExpression(n,a,o){T(n,"",e),n.type==="CallExpression"?n.arguments.forEach(i=>o(i,{parent:n,inNamespaceCall:a.inNamespaceCall})):n.object&&o(n.object,{parent:n,inNamespaceCall:a.inNamespaceCall})},CallExpression(n,a,o){const i=n.callee&&n.callee.type==="MemberExpression"&&n.callee.object&&n.callee.object.type==="Identifier"&&e.isContextBound(n.callee.object.name);D(n,e),n.arguments.forEach(s=>o(s,{parent:n,inNamespaceCall:i||a.inNamespaceCall}))}}),r){let n=t.right;if(t.operator!=="="){const o=t.operator.replace("=",""),i=y.createGetCall(r,0);n={type:"BinaryExpression",operator:o,left:i,right:t.right,start:t.start,end:t.end}}const a=y.createSetCall(r,n);t.start&&(a.start=t.start),t.end&&(a.end=t.end),Object.assign(t,a)}}function te(t,e){t.declarations.forEach(r=>{r.init.name=="na"&&(r.init.name="NaN");const n=r.init&&r.init.type==="MemberExpression"&&r.init.object&&(r.init.object.name==="context"||r.init.object.name===A||r.init.object.name==="context2"),a=r.init&&r.init.type==="MemberExpression"&&r.init.object?.object&&(r.init.object.object.name==="context"||r.init.object.object.name===A||r.init.object.object.name==="context2"),o=r.init&&r.init.type==="ArrowFunctionExpression";if(n){r.id.name&&e.addContextBoundVar(r.id.name),r.id.properties&&r.id.properties.forEach(p=>{p.key.name&&e.addContextBoundVar(p.key.name)}),r.init.object.name=A;return}if(a){r.id.name&&e.addContextBoundVar(r.id.name),r.id.properties&&r.id.properties.forEach(p=>{p.key.name&&e.addContextBoundVar(p.key.name)}),r.init.object.object.name=A;return}o&&r.init.params.forEach(p=>{p.type==="Identifier"&&e.addContextBoundVar(p.name)});const i=e.addVariable(r.id.name,t.kind),s=t.kind;r.init&&!o&&(r.init.type==="CallExpression"&&r.init.callee.type==="MemberExpression"&&r.init.callee.object&&r.init.callee.object.type==="Identifier"&&e.isContextBound(r.init.callee.object.name)?D(r.init,e):j.recursive(r.init,{parent:r.init},{Identifier(p,d){p.parent=d.parent,$(p,e);const b=p.parent&&p.parent.type==="BinaryExpression",x=p.parent&&p.parent.type==="ConditionalExpression",S=p.parent&&p.parent.type==="CallExpression"&&p.parent.callee&&p.parent.callee.object&&p.parent.callee.object.name===A&&p.parent.callee.property.name==="get";p.type==="Identifier"&&(b||x)&&!S&&B(p)},CallExpression(p,d,b){p.callee.type==="Identifier"&&(p.callee.parent=p),p.arguments.forEach(x=>{x.type==="Identifier"&&(x.parent=p)}),D(p,e),p.arguments.forEach(x=>b(x,{parent:p}))},BinaryExpression(p,d,b){p.left.type==="Identifier"&&(p.left.parent=p),p.right.type==="Identifier"&&(p.right.parent=p),b(p.left,{parent:p}),b(p.right,{parent:p})},MemberExpression(p,d,b){p.object&&p.object.type==="Identifier"&&(p.object.parent=p),p.property&&p.property.type==="Identifier"&&(p.property.parent=p),T(p,"",e),p.type==="CallExpression"?p.arguments.forEach(x=>b(x,{parent:p})):p.object&&b(p.object,{parent:p})}}));const c=y.createContextVariableReference(s,i),u=e.isArrayPatternElement(r.id.name),h=!u&&r.init&&r.init.type==="MemberExpression"&&r.init.computed&&r.init.property&&(r.init.property.type==="Literal"||r.init.property.type==="MemberExpression");r.init?.property?.type==="MemberExpression"&&(r.init.property._indexTransformed||(k(r.init.property,e),r.init.property._indexTransformed=!0));let f;r.init?o||u?f=r.init:f=y.createInitCall(c,h?r.init.object:r.init,h?r.init.property:void 0):f=y.createIdentifier("undefined");const m=y.createExpressionStatement(y.createAssignmentExpression(c,f));if(u){m.expression.right.object.property.name+=`?.[0][${r.init.property.value}]`;const p=m.expression.right.object;m.expression.right=y.createCallExpression(y.createMemberExpression(y.createContextIdentifier(),y.createIdentifier("init")),[c,p])}o&&(e.pushScope("fn"),j.recursive(r.init.body,e,{BlockStatement(p,d,b){p.body.forEach(x=>b(x,d))},IfStatement(p,d,b){d.pushScope("if"),b(p.consequent,d),p.alternate&&(d.pushScope("els"),b(p.alternate,d),d.popScope()),d.popScope()},VariableDeclaration(p,d){te(p,d)},Identifier(p,d){$(p,d)},AssignmentExpression(p,d){ee(p,d)}}),e.popScope()),Object.assign(t,m)})}function de(t,e,r){if(t.init&&t.init.type==="VariableDeclaration"){const n=t.init.declarations[0],a=n.id.name;e.addLoopVariable(a,a),t.init={type:"VariableDeclaration",kind:t.init.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:a},init:n.init}]},n.init&&j.recursive(n.init,e,{Identifier(o,i){e.isLoopVariable(o.name)||(e.pushScope("for"),$(o,i),e.popScope())},MemberExpression(o){e.pushScope("for"),T(o,"",e),e.popScope()}})}t.test&&j.recursive(t.test,e,{Identifier(n,a){!e.isLoopVariable(n.name)&&!n.computed&&(e.pushScope("for"),$(n,a),n.type==="Identifier"&&(n.computed=!0,B(n)),e.popScope())},MemberExpression(n){e.pushScope("for"),T(n,"",e),e.popScope()}}),t.update&&j.recursive(t.update,e,{Identifier(n,a){e.isLoopVariable(n.name)||(e.pushScope("for"),$(n,a),e.popScope())}}),e.pushScope("for"),r(t.body,e),e.popScope()}function ye(t,e){j.recursive(t,e,{MemberExpression(r){T(r,"",e)},CallExpression(r,n){D(r,n)},Identifier(r,n){$(r,n);const a=e.getCurrentScopeType()==="if";e.isContextBound(r.name)&&!e.isRootParam(r.name)&&a&&B(r)}})}function ge(t,e,r){t.test&&(e.pushScope("if"),ye(t.test,e),e.popScope()),e.pushScope("if"),r(t.consequent,e),e.popScope(),t.alternate&&(e.pushScope("els"),r(t.alternate,e),e.popScope())}function be(t,e){const r=e.getCurrentScopeType();if(t.argument&&(t.argument.type==="ArrayExpression"?(t.argument.elements=t.argument.elements.map(n=>{if(n.type==="Identifier"){if(e.isContextBound(n.name)&&!e.isRootParam(n.name))return y.createArrayAccess(n,0);const[a,o]=e.getVariable(n.name);return y.createContextVariableAccess0(o,a)}else if(n.type==="MemberExpression")return n.computed&&n.object.type==="Identifier"&&e.isContextBound(n.object.name)&&!e.isRootParam(n.object.name)||T(n,"",e),n;return n}),t.argument={type:"ArrayExpression",elements:[t.argument]}):t.argument.type==="BinaryExpression"?j.recursive(t.argument,e,{Identifier(n,a){$(n,a),n.type==="Identifier"&&B(n)},MemberExpression(n){T(n,"",e)}}):t.argument.type==="ObjectExpression"?t.argument.properties=t.argument.properties.map(n=>{if(n.shorthand){const[a,o]=e.getVariable(n.value.name);return{type:"Property",key:y.createIdentifier(n.key.name),value:y.createContextVariableReference(o,a),kind:"init",method:!1,shorthand:!1,computed:!1}}return n}):t.argument.type==="Identifier"&&($(t.argument,e),t.argument.type==="Identifier"&&B(t.argument)),r==="fn")){t.argument.type==="Identifier"&&e.isContextBound(t.argument.name)&&!e.isRootParam(t.argument.name)?t.argument=y.createArrayAccess(t.argument,0):t.argument.type==="MemberExpression"?t.argument.object.type==="Identifier"&&e.isContextBound(t.argument.object.name)&&!e.isRootParam(t.argument.object.name)&&(t.argument._indexTransformed||(k(t.argument,e),t.argument._indexTransformed=!0)):(t.argument.type==="BinaryExpression"||t.argument.type==="LogicalExpression"||t.argument.type==="ConditionalExpression"||t.argument.type==="CallExpression")&&j.recursive(t.argument,e,{Identifier(a,o){$(a,o),a.type==="Identifier"&&!a._arrayAccessed&&(B(a),a._arrayAccessed=!0)},MemberExpression(a){T(a,"",e)},CallExpression(a,o){D(a,o)}});const n=y.createCallExpression(y.createMemberExpression(y.createContextIdentifier(),y.createIdentifier("precision")),[t.argument]);t.argument=n}}function we(t,e,r){t.body&&t.body.type==="BlockStatement"&&(e.pushScope("fn"),r(t.body,e),e.popScope())}function xe(t){j.simple(t,{BinaryExpression(e){if(e.operator==="=="||e.operator==="==="){const r=e.left,n=e.right,a=y.createMathEqCall(r,n);a._transformed=!0,Object.assign(e,a)}}})}function ve(t,e,r){j.recursive(t,e,{BlockStatement(n,a,o){n.body.forEach(i=>o(i,a))},ReturnStatement(n,a){be(n,a)},VariableDeclaration(n,a){te(n,a)},Identifier(n,a){$(n,a)},CallExpression(n,a){D(n,a)},MemberExpression(n,a){T(n,r,a)},AssignmentExpression(n,a){ee(n,a)},FunctionDeclaration(n,a,o){we(n,a,o)},ForStatement(n,a,o){de(n,a,o)},IfStatement(n,a,o){ge(n,a,o)}})}function Ce(t){let e=typeof t=="function"?t.toString():t;const r=re.parse(e.trim(),{ecmaVersion:"latest",sourceType:"module"});ce(r);const n=new se;pe(r,n);const a=me(r,n)||"";ve(r,n,a),xe(r);const o=ne.generate(r);return new Function("",`return ${o}`)(this)}class l{constructor(e,r=0){this.data=e,this.offset=r}get(e){const r=this.data.length-1-(this.offset+e);return r<0||r>=this.data.length?NaN:this.data[r]}set(e,r){const n=this.data.length-1-(this.offset+e);n>=0&&n<this.data.length&&(this.data[n]=r)}get length(){return this.data.length}toArray(){return this.data}static from(e){return e instanceof l?e:Array.isArray(e)?new l(e):new l([e])}}var Ee=Object.defineProperty,Se=(t,e,r)=>e in t?Ee(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,je=(t,e,r)=>Se(t,typeof e!="symbol"?e+"":e,r);class Ae{constructor(e){this.context=e,je(this,"color",{param:(r,n=0)=>l.from(r).get(n),rgb:(r,n,a,o)=>o?`rgba(${r}, ${n}, ${a}, ${o})`:`rgb(${r}, ${n}, ${a})`,new:(r,n)=>{if(r&&r.startsWith("#")){const a=r.slice(1),o=parseInt(a.slice(0,2),16),i=parseInt(a.slice(2,4),16),s=parseInt(a.slice(4,6),16);return n?`rgba(${o}, ${i}, ${s}, ${n})`:`rgb(${o}, ${i}, ${s})`}return n?`rgba(${r}, ${n})`:r},white:"white",lime:"lime",green:"green",red:"red",maroon:"maroon",black:"black",gray:"gray",blue:"blue"})}extractPlotOptions(e){const r={};for(let n in e)r[n]=l.from(e[n]).get(0);return r}indicator(e,r,n){}plotchar(e,r,n){this.context.plots[r]||(this.context.plots[r]={data:[],options:this.extractPlotOptions(n),title:r});const a=l.from(e).get(0);this.context.plots[r].data.push({time:this.context.marketData[this.context.idx].openTime,value:a,options:{...this.extractPlotOptions(n),style:"char"}})}plot(e,r,n){this.context.plots[r]||(this.context.plots[r]={data:[],options:this.extractPlotOptions(n),title:r});const a=l.from(e).get(0);this.context.plots[r].data.push({time:this.context.marketData[this.context.idx].openTime,value:a,options:this.extractPlotOptions(n)})}na(e){return isNaN(l.from(e).get(0))}nz(e,r=0){const n=l.from(e).get(0),a=l.from(r).get(0);return isNaN(n)?a:n}}function Me(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Ie(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Ne(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function _e(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function $e(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Ve(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Te(t){return(e,r=0)=>[l.from(e).get(r)]}function De(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Pe(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Be(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function ke(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Le(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Oe(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Re(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}function Fe(t){return(e,{title:r,group:n}={})=>Array.isArray(e)?e[0]:e}var qe=Object.defineProperty,We=(t,e,r)=>e in t?qe(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,M=(t,e,r)=>We(t,typeof e!="symbol"?e+"":e,r);const Ge={any:Me,bool:Ie,color:Ne,enum:_e,float:$e,int:Ve,param:Te,price:De,session:Pe,source:Be,string:ke,symbol:Le,text_area:Oe,time:Re,timeframe:Fe};class Ue{constructor(e){this.context=e,M(this,"any"),M(this,"bool"),M(this,"color"),M(this,"enum"),M(this,"float"),M(this,"int"),M(this,"param"),M(this,"price"),M(this,"session"),M(this,"source"),M(this,"string"),M(this,"symbol"),M(this,"text_area"),M(this,"time"),M(this,"timeframe"),Object.entries(Ge).forEach(([r,n])=>{this[r]=n(e)})}}function ze(t){return e=>Math.abs(l.from(e).get(0))}function Ke(t){return e=>Math.acos(l.from(e).get(0))}function He(t){return e=>Math.asin(l.from(e).get(0))}function Xe(t){return e=>Math.atan(l.from(e).get(0))}function Je(t){return(...e)=>{const r=e.map(n=>l.from(n).get(0));return r.reduce((n,a)=>n+a,0)/r.length}}function Qe(t){return e=>Math.ceil(l.from(e).get(0))}function Ye(t){return e=>Math.cos(l.from(e).get(0))}function Ze(t){return e=>Math.exp(l.from(e).get(0))}function et(t){return e=>Math.floor(l.from(e).get(0))}function tt(t){return e=>Math.log(l.from(e).get(0))}function rt(t){return e=>Math.log(l.from(e).get(0))}function nt(t){return e=>Math.log10(l.from(e).get(0))}function at(t){return(...e)=>{const r=e.map(n=>l.from(n).get(0));return Math.max(...r)}}function it(t){return(...e)=>{const r=e.map(n=>l.from(n).get(0));return Math.min(...r)}}function ot(t){return(e,r,n)=>typeof e=="string"?e:e instanceof l?r?new l(e.data,e.offset+r):e:!Array.isArray(e)&&typeof e=="object"?e:(t.params[n]||(t.params[n]=[]),Array.isArray(e)?new l(e,r||0):(t.params[n].length===0?t.params[n].push(e):t.params[n][t.params[n].length-1]=e,new l(t.params[n],0)))}function st(t){return(e,r)=>Math.pow(l.from(e).get(0),l.from(r).get(0))}function ct(t){return()=>Math.random()}function pt(t){return e=>Math.round(l.from(e).get(0))}function lt(t){return e=>Math.sin(l.from(e).get(0))}function ut(t){return e=>Math.sqrt(l.from(e).get(0))}function mt(t){return(e,r)=>{const n=l.from(r).get(0),a=l.from(e);let o=0;for(let i=0;i<n;i++){const s=a.get(i);isNaN(s)||(o+=s)}return o}}function ht(t){return e=>Math.tan(l.from(e).get(0))}function ft(t){return(e,r)=>{const n=l.from(e).get(0),a=l.from(r).get(0);return isNaN(n)&&isNaN(a)?!0:isNaN(n)||isNaN(a)?!1:Math.abs(n-a)<1e-8}}var dt=Object.defineProperty,yt=(t,e,r)=>e in t?dt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,E=(t,e,r)=>yt(t,typeof e!="symbol"?e+"":e,r);const gt={abs:ze,acos:Ke,asin:He,atan:Xe,avg:Je,ceil:Qe,cos:Ye,exp:Ze,floor:et,ln:tt,log:rt,log10:nt,max:at,min:it,param:ot,pow:st,random:ct,round:pt,sin:lt,sqrt:ut,sum:mt,tan:ht,__eq:ft};class bt{constructor(e){this.context=e,E(this,"_cache",{}),E(this,"abs"),E(this,"acos"),E(this,"asin"),E(this,"atan"),E(this,"avg"),E(this,"ceil"),E(this,"cos"),E(this,"exp"),E(this,"floor"),E(this,"ln"),E(this,"log"),E(this,"log10"),E(this,"max"),E(this,"min"),E(this,"param"),E(this,"pow"),E(this,"random"),E(this,"round"),E(this,"sin"),E(this,"sqrt"),E(this,"sum"),E(this,"tan"),E(this,"__eq"),Object.entries(gt).forEach(([r,n])=>{this[r]=n(e)})}}function wt(t){return(e,r,n)=>(t.params[n]||(t.params[n]=[]),e instanceof l||Array.isArray(e)?[l.from(e).get(r||0),n]:(t.params[n].length===0?t.params[n].push(e):t.params[n][t.params[n].length-1]=e,[e,n]))}const H=["1","3","5","15","30","45","60","120","180","240","D","W","M"];function X(t,e,r,n,a=!1){for(let o=0;o<r.length;o++)if(r[o]<=t&&e<=n[o])return o+(a?1:2);return-1}function xt(t){return async(e,r,n,a=!1,o=!1,i=!1,s=null,c=null)=>{const u=e[0],h=r[0],f=n[0],m=n[1],p=H.indexOf(t.timeframe),d=H.indexOf(h);if(p==-1||d==-1)throw new Error("Invalid timeframe");if(p>d)throw new Error("Only higher timeframes are supported for now");if(p===d)return f;const b=t.data.openTime[0],x=t.data.closeTime[0];if(t.cache[m]){const P=t.cache[m],w=X(b,x,P.data.openTime,P.data.closeTime,o);return w==-1?NaN:P.params[m][w]}const S=await new Zr(t.source,u,h,t.limit||1e3,t.sDate,t.eDate).run(t.pineTSCode);t.cache[m]=S;const V=X(b,x,S.data.openTime,S.data.closeTime,o);return V==-1?NaN:S.params[m][V]}}var vt=Object.defineProperty,Ct=(t,e,r)=>e in t?vt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,W=(t,e,r)=>Ct(t,typeof e!="symbol"?e+"":e,r);const Et={param:wt,security:xt};class St{constructor(e){this.context=e,W(this,"_cache",{}),W(this,"param"),W(this,"security"),Object.entries(Et).forEach(([r,n])=>{this[r]=n(e)})}}function jt(t){return()=>{const e=t.get(t.data.high,0),r=t.get(t.data.low,0),n=t.get(t.data.close,1);return isNaN(n)?e-r:Math.max(e-r,Math.abs(e-n),Math.abs(r-n))}}function At(t){return(e,r)=>{const n=l.from(e).get(0);t.taState||(t.taState={});const a=r||`atr_${n}`;t.taState[a]||(t.taState[a]={prevAtr:null,initSum:0,initCount:0,prevClose:null});const o=t.taState[a],i=t.get(t.data.high,0),s=t.get(t.data.low,0),c=t.get(t.data.close,0);let u;if(o.prevClose!==null){const f=i-s,m=Math.abs(i-o.prevClose),p=Math.abs(s-o.prevClose);u=Math.max(f,m,p)}else u=i-s;if(o.prevClose=c,o.initCount<n)return o.initSum+=u,o.initCount++,o.initCount===n?(o.prevAtr=o.initSum/n,t.precision(o.prevAtr)):NaN;const h=(o.prevAtr*(n-1)+u)/n;return o.prevAtr=h,t.precision(h)}}function Mt(t){return(e,r=1,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`change_${a}`;t.taState[o]||(t.taState[o]={window:[]});const i=t.taState[o],s=l.from(e).get(0);if(i.window.unshift(s),i.window.length<=a)return NaN;i.window.length>a+1&&i.window.pop();const c=s-i.window[a];return t.precision(c)}}function It(t){return(e,r)=>{const n=l.from(e),a=l.from(r),o=n.get(0),i=a.get(0),s=n.get(1),c=a.get(1);return s<c&&o>i}}function Nt(t){return(e,r)=>{const n=l.from(e),a=l.from(r),o=n.get(0),i=a.get(0),s=n.get(1),c=a.get(1);return s>c&&o<i}}function _t(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`dev_${a}`;t.taState[o]||(t.taState[o]={window:[],sum:0});const i=t.taState[o],s=l.from(e).get(0)||0;if(i.window.unshift(s),i.sum+=s,i.window.length<a)return NaN;if(i.window.length>a){const f=i.window.pop();i.sum-=f}const c=i.sum/a;let u=0;for(let f=0;f<a;f++)u+=Math.abs(i.window[f]-c);const h=u/a;return t.precision(h)}}function $t(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`ema_${a}`;t.taState[o]||(t.taState[o]={prevEma:null,initSum:0,initCount:0});const i=t.taState[o],s=l.from(e).get(0);if(i.initCount<a)return i.initSum+=s,i.initCount++,i.initCount===a?(i.prevEma=i.initSum/a,t.precision(i.prevEma)):NaN;const c=2/(a+1),u=s*c+i.prevEma*(1-c);return i.prevEma=u,t.precision(u)}}function Vt(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`highest_${a}`;t.taState[o]||(t.taState[o]={window:[]});const i=t.taState[o],s=l.from(e).get(0);if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=Math.max(...i.window.filter(u=>!isNaN(u)));return t.precision(c)}}function Tt(t){return(e,r,n)=>{const a=l.from(r).get(0),o=Math.floor(a/2),i=Math.floor(Math.sqrt(a)),s=t.ta.wma,c=s(e,o,n?`${n}_wma1`:void 0),u=s(e,a,n?`${n}_wma2`:void 0);if(isNaN(c)||isNaN(u))return NaN;t.taState||(t.taState={});const h=n||`hma_raw_${a}`;t.taState[h]||(t.taState[h]=[]);const f=2*c-u;t.taState[h].unshift(f);const m=n?`${n}_hma_final`:`hma_final_${a}`;t.taState[m]||(t.taState[m]={window:[]});const p=t.taState[m];if(p.window.unshift(f),p.window.length<i)return NaN;p.window.length>i&&p.window.pop();let d=0,b=0;for(let S=0;S<i;S++){const V=i-S;d+=p.window[S]*V,b+=V}const x=d/b;return t.precision(x)}}function Dt(t){return(e,r,n,a)=>{const o=l.from(r).get(0),i=l.from(n).get(0);t.taState||(t.taState={});const s=a||`linreg_${o}_${i}`;t.taState[s]||(t.taState[s]={window:[]});const c=t.taState[s],u=l.from(e).get(0);if(c.window.unshift(u),c.window.length<o)return NaN;c.window.length>o&&c.window.pop();let h=0,f=0,m=0,p=0;const d=o;for(let V=0;V<o;V++){const P=o-1-V,w=c.window[V];h+=P,f+=w,m+=P*w,p+=P*P}const b=d*p-h*h;if(b===0)return NaN;const x=(d*m-h*f)/b,S=(f-x*h)/d+x*(o-1-i);return t.precision(S)}}function Pt(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`lowest_${a}`;t.taState[o]||(t.taState[o]={window:[]});const i=t.taState[o],s=l.from(e).get(0);if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=i.window.filter(h=>!isNaN(h)&&h!==void 0),u=c.length>0?Math.min(...c):NaN;return t.precision(u)}}function Bt(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`median_${a}`;t.taState[o]||(t.taState[o]={window:[]});const i=t.taState[o],s=l.from(e).get(0);if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=i.window.slice().sort((f,m)=>f-m),u=Math.floor(a/2),h=a%2===0?(c[u-1]+c[u])/2:c[u];return t.precision(h)}}function kt(t){return(e,r,n)=>{const a=l.from(r).get(0);return t.ta.change(e,a)}}function Lt(t){return(e,r,n)=>e instanceof l?r?new l(e.data,e.offset+r):e:(t.params[n]||(t.params[n]=[]),Array.isArray(e)?new l(e,r||0):(t.params[n].length===0?t.params[n].push(e):t.params[n][t.params[n].length-1]=e,new l(t.params[n],0)))}function Ot(t,e,r){const n=new Array(t.length).fill(NaN);for(let a=e+r;a<t.length;a++){const o=t[a-r];let i=!0;for(let s=1;s<=e;s++)if(t[a-r-s]>=o){i=!1;break}if(i){for(let s=1;s<=r;s++)if(t[a-r+s]>=o){i=!1;break}}i&&(n[a]=o)}return n}function Rt(t){return(e,r,n)=>{n==null&&(n=r,r=e,e=t.data.high);const a=l.from(r).get(0),o=l.from(n).get(0),i=l.from(e).toArray(),s=Ot(i,a,o),c=t.idx;return t.precision(s[c])}}function Ft(t,e,r){const n=new Array(t.length).fill(NaN);for(let a=e+r;a<t.length;a++){const o=t[a-r];let i=!0;for(let s=1;s<=e;s++)if(t[a-r-s]<=o){i=!1;break}if(i){for(let s=1;s<=r;s++)if(t[a-r+s]<=o){i=!1;break}}i&&(n[a]=o)}return n}function qt(t){return(e,r,n)=>{n==null&&(n=r,r=e,e=t.data.low);const a=l.from(r).get(0),o=l.from(n).get(0),i=l.from(e).toArray(),s=Ft(i,a,o),c=t.idx;return t.precision(s[c])}}function Wt(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`rma_${a}`;t.taState[o]||(t.taState[o]={prevRma:null,initSum:0,initCount:0});const i=t.taState[o],s=l.from(e).get(0)||0;if(i.initCount<a)return i.initSum+=s,i.initCount++,i.initCount===a?(i.prevRma=i.initSum/a,t.precision(i.prevRma)):NaN;const c=1/a,u=s*c+i.prevRma*(1-c);return i.prevRma=u,t.precision(u)}}function Gt(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`roc_${a}`;t.taState[o]||(t.taState[o]={window:[]});const i=t.taState[o],s=l.from(e).get(0);if(i.window.unshift(s),i.window.length<=a)return NaN;i.window.length>a+1&&i.window.pop();const c=i.window[a],u=(s-c)/c*100;return t.precision(u)}}function Ut(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`rsi_${a}`;t.taState[o]||(t.taState[o]={prevValue:null,avgGain:0,avgLoss:0,initGains:[],initLosses:[]});const i=t.taState[o],s=l.from(e).get(0);if(i.prevValue!==null){const c=s-i.prevValue,u=c>0?c:0,h=c<0?-c:0;if(i.initGains.length<a){if(i.initGains.push(u),i.initLosses.push(h),i.initGains.length===a){i.avgGain=i.initGains.reduce((p,d)=>p+d,0)/a,i.avgLoss=i.initLosses.reduce((p,d)=>p+d,0)/a,i.prevValue=s;const m=i.avgLoss===0?100:100-100/(1+i.avgGain/i.avgLoss);return t.precision(m)}return i.prevValue=s,NaN}i.avgGain=(i.avgGain*(a-1)+u)/a,i.avgLoss=(i.avgLoss*(a-1)+h)/a;const f=i.avgLoss===0?100:100-100/(1+i.avgGain/i.avgLoss);return i.prevValue=s,t.precision(f)}return i.prevValue=s,NaN}}function zt(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`sma_${a}`;t.taState[o]||(t.taState[o]={window:[],sum:0});const i=t.taState[o],s=l.from(e).get(0)||0;if(i.window.unshift(s),i.sum+=s,i.window.length<a)return NaN;if(i.window.length>a){const u=i.window.pop();i.sum-=u}const c=i.sum/a;return t.precision(c)}}function Kt(t){return(e,r,n=!0,a)=>{const o=l.from(r).get(0),i=l.from(n).get(0);t.taState||(t.taState={});const s=a||`stdev_${o}_${i}`;t.taState[s]||(t.taState[s]={window:[],sum:0});const c=t.taState[s],u=l.from(e).get(0);if(c.window.unshift(u),c.sum+=u,c.window.length<o)return NaN;if(c.window.length>o){const d=c.window.pop();c.sum-=d}const h=c.sum/o;let f=0;for(let d=0;d<o;d++)f+=Math.pow(c.window[d]-h,2);const m=i?o:o-1,p=Math.sqrt(f/m);return t.precision(p)}}function Ht(t){return(e,r,n)=>{const a=l.from(e).get(0),o=l.from(r).get(0);t.taState||(t.taState={});const i=`supertrend_${a}_${o}`;t.taState[i]||(t.taState[i]={prevUpperBand:null,prevLowerBand:null,prevSupertrend:null,prevDirection:null});const s=t.taState[i],c=t.get(t.data.high,0),u=t.get(t.data.low,0),h=t.get(t.data.close,0),f=t.get(t.data.close,1),m=t.ta.atr(o,n?`${n}_atr`:void 0);if(isNaN(m))return[[NaN,0]];const p=(c+u)/2;let d=p+a*m,b=p-a*m;s.prevUpperBand!==null&&(d<s.prevUpperBand||f>s.prevUpperBand?d=d:d=s.prevUpperBand,b>s.prevLowerBand||f<s.prevLowerBand?b=b:b=s.prevLowerBand);let x,S;return s.prevSupertrend===null?(x=h<=d?-1:1,S=x===-1?d:b):s.prevSupertrend===s.prevUpperBand?h>d?(x=1,S=b):(x=-1,S=d):h<b?(x=-1,S=d):(x=1,S=b),s.prevUpperBand=d,s.prevLowerBand=b,s.prevSupertrend=S,s.prevDirection=x,[[t.precision(S),x]]}}function Xt(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`variance_${a}`;t.taState[o]||(t.taState[o]={window:[]});const i=t.taState[o],s=l.from(e).get(0);if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();let c=0,u=0;for(let m=0;m<a;m++)c+=i.window[m],u+=i.window[m]*i.window[m];const h=c/a,f=u/a-h*h;return t.precision(f)}}function Jt(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`vwma_${a}`;t.taState[o]||(t.taState[o]={window:[],volumeWindow:[]});const i=t.taState[o],s=l.from(e).get(0),c=t.get(t.data.volume,0);if(i.window.unshift(s),i.volumeWindow.unshift(c),i.window.length<a)return NaN;i.window.length>a&&(i.window.pop(),i.volumeWindow.pop());let u=0,h=0;for(let m=0;m<a;m++)u+=i.window[m]*i.volumeWindow[m],h+=i.volumeWindow[m];const f=u/h;return t.precision(f)}}function Qt(t){return(e,r,n)=>{const a=l.from(r).get(0);t.taState||(t.taState={});const o=n||`wma_${a}`;t.taState[o]||(t.taState[o]={window:[]});const i=t.taState[o],s=l.from(e).get(0);if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();let c=0,u=0;for(let f=0;f<a;f++){const m=a-f;c+=i.window[f]*m,u+=m}const h=c/u;return t.precision(h)}}var Yt=Object.defineProperty,Zt=(t,e,r)=>e in t?Yt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,C=(t,e,r)=>Zt(t,typeof e!="symbol"?e+"":e,r);const er={tr:jt},tr={atr:At,change:Mt,crossover:It,crossunder:Nt,dev:_t,ema:$t,highest:Vt,hma:Tt,linreg:Dt,lowest:Pt,median:Bt,mom:kt,param:Lt,pivothigh:Rt,pivotlow:qt,rma:Wt,roc:Gt,rsi:Ut,sma:zt,stdev:Kt,supertrend:Ht,variance:Xt,vwma:Jt,wma:Qt};class rr{constructor(e){this.context=e,C(this,"tr"),C(this,"atr"),C(this,"change"),C(this,"crossover"),C(this,"crossunder"),C(this,"dev"),C(this,"ema"),C(this,"highest"),C(this,"hma"),C(this,"linreg"),C(this,"lowest"),C(this,"median"),C(this,"mom"),C(this,"param"),C(this,"pivothigh"),C(this,"pivotlow"),C(this,"rma"),C(this,"roc"),C(this,"rsi"),C(this,"sma"),C(this,"stdev"),C(this,"supertrend"),C(this,"variance"),C(this,"vwma"),C(this,"wma"),Object.entries(er).forEach(([r,n])=>{Object.defineProperty(this,r,{get:n(e),enumerable:!0})}),Object.entries(tr).forEach(([r,n])=>{this[r]=n(e)})}}class N{constructor(e){this.array=e}toString(){return"PineArrayObject:"+this.array.toString()}}function nr(t){return e=>new N(e.array.map(r=>Math.abs(r)))}function ar(t){return e=>t.array.sum(e)/e.array.length}function ir(t){return e=>{e.array.length=0}}function or(t){return(e,r)=>(e.array.push(...r.array),e)}function sr(t){return e=>new N([...e.array])}function cr(t){return(e,r,n=!0)=>{if(e.array.length!==r.array.length||e.array.length<2)return NaN;const a=n?e.array.length:e.array.length-1,o=t.array.avg(e),i=t.array.avg(r);let s=0;for(let c=0;c<e.array.length;c++)s+=(e.array[c]-o)*(r.array[c]-i);return s/a}}function pr(t){return(e,r)=>e.array.every(r)}function lr(t){return(e,r,n=0,a)=>{const o=e.array.length,i=a!==void 0?Math.min(a,o):o;for(let s=n;s<i;s++)e.array[s]=r}}function ur(t){return e=>e.array.length>0?e.array[0]:t.NA}function mr(t){return e=>new N([...e])}function hr(t){return(e,r)=>e.array[r]}function fr(t){return(e,r)=>e.array.includes(r)}function dr(t){return(e,r)=>e.array.indexOf(r)}function yr(t){return(e,r,n)=>{e.array.splice(r,0,n)}}function gr(t){return(e,r=",")=>e.array.join(r)}function br(t){return e=>e.array.length>0?e.array[e.array.length-1]:t.NA}function wr(t){return(e,r)=>e.array.lastIndexOf(r)}function xr(t){return(e,r=0)=>[...e.array].sort((n,a)=>a-n)[r]??t.NA}function vr(t){return(e,r=0)=>[...e.array].sort((n,a)=>n-a)[r]??t.NA}function Cr(t){return(e,r)=>new N(Array(e).fill(r))}function Er(t){return(e,r=!1)=>new N(Array(e).fill(r))}function Sr(t){return(e,r=NaN)=>new N(Array(e).fill(r))}function jr(t){return(e,r=0)=>new N(Array(e).fill(Math.round(r)))}function Ar(t){return(e,r="")=>new N(Array(e).fill(r))}function Mr(t){return(e,r=0)=>l.from(e).get(r)}function Ir(t){return e=>e.array.pop()}function Nr(t){return(e,r)=>{e.array.push(r)}}function _r(t){return e=>t.array.max(e)-t.array.min(e)}function $r(t){return(e,r)=>r>=0&&r<e.array.length?e.array.splice(r,1)[0]:t.NA}function Vr(t){return e=>{e.array.reverse()}}function Tr(t){return(e,r,n)=>{e.array[r]=n}}function Dr(t){return e=>e.array.shift()}function Pr(t){return e=>e.array.length}function Br(t){return(e,r,n)=>{const a=n!==void 0?n+1:void 0;return new N(e.array.slice(r,a))}}function kr(t){return(e,r)=>e.array.some(r)}function Lr(t){return(e,r="asc")=>{e.array.sort((n,a)=>r==="asc"?n-a:a-n)}}function Or(t){return(e,r)=>{const n=e.array.map((a,o)=>o);return n.sort((a,o)=>{const i=e.array[a],s=e.array[o];return r?r(i,s):i-s}),new N(n)}}function Rr(t){return e=>{const r=t.array.avg(e),n=t.array.stdev(e);return n===0?new N(e.array.map(()=>0)):new N(e.array.map(a=>(a-r)/n))}}function Fr(t){return(e,r=!0)=>{const n=t.array.avg(e),a=e.array.map(i=>Math.pow(i-n,2)),o=r?e.array.length:e.array.length-1;return Math.sqrt(t.array.sum(new N(a))/o)}}function qr(t){return e=>e.array.reduce((r,n)=>r+(isNaN(n)?0:n),0)}function Wr(t){return(e,r)=>{e.array.unshift(r)}}function Gr(t){return(e,r=!0)=>{const n=t.array.avg(e),a=e.array.map(i=>Math.pow(i-n,2)),o=r?e.array.length:e.array.length-1;return t.array.sum(new N(a))/o}}var Ur=Object.defineProperty,zr=(t,e,r)=>e in t?Ur(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,g=(t,e,r)=>zr(t,typeof e!="symbol"?e+"":e,r);const Kr={abs:nr,avg:ar,clear:ir,concat:or,copy:sr,covariance:cr,every:pr,fill:lr,first:ur,from:mr,get:hr,includes:fr,indexof:dr,insert:yr,join:gr,last:br,lastindexof:wr,max:xr,min:vr,new:Cr,new_bool:Er,new_float:Sr,new_int:jr,new_string:Ar,param:Mr,pop:Ir,push:Nr,range:_r,remove:$r,reverse:Vr,set:Tr,shift:Dr,size:Pr,slice:Br,some:kr,sort:Lr,sort_indices:Or,standardize:Rr,stdev:Fr,sum:qr,unshift:Wr,variance:Gr};class Hr{constructor(e){this.context=e,g(this,"_cache",{}),g(this,"abs"),g(this,"avg"),g(this,"clear"),g(this,"concat"),g(this,"copy"),g(this,"covariance"),g(this,"every"),g(this,"fill"),g(this,"first"),g(this,"from"),g(this,"get"),g(this,"includes"),g(this,"indexof"),g(this,"insert"),g(this,"join"),g(this,"last"),g(this,"lastindexof"),g(this,"max"),g(this,"min"),g(this,"new"),g(this,"new_bool"),g(this,"new_float"),g(this,"new_int"),g(this,"new_string"),g(this,"param"),g(this,"pop"),g(this,"push"),g(this,"range"),g(this,"remove"),g(this,"reverse"),g(this,"set"),g(this,"shift"),g(this,"size"),g(this,"slice"),g(this,"some"),g(this,"sort"),g(this,"sort_indices"),g(this,"standardize"),g(this,"stdev"),g(this,"sum"),g(this,"unshift"),g(this,"variance"),Object.entries(Kr).forEach(([r,n])=>{this[r]=n(e)})}}var Xr=Object.defineProperty,Jr=(t,e,r)=>e in t?Xr(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,v=(t,e,r)=>Jr(t,typeof e!="symbol"?e+"":e,r);class J{constructor({marketData:e,source:r,tickerId:n,timeframe:a,limit:o,sDate:i,eDate:s}){v(this,"data",{open:[],high:[],low:[],close:[],volume:[],hl2:[],hlc3:[],ohlc4:[]}),v(this,"cache",{}),v(this,"taState",{}),v(this,"NA",NaN),v(this,"math"),v(this,"ta"),v(this,"input"),v(this,"request"),v(this,"array"),v(this,"core"),v(this,"lang"),v(this,"idx",0),v(this,"params",{}),v(this,"const",{}),v(this,"var",{}),v(this,"let",{}),v(this,"result"),v(this,"plots",{}),v(this,"marketData"),v(this,"source"),v(this,"tickerId"),v(this,"timeframe",""),v(this,"limit"),v(this,"sDate"),v(this,"eDate"),v(this,"pineTSCode"),this.marketData=e,this.source=r,this.tickerId=n,this.timeframe=a,this.limit=o,this.sDate=i,this.eDate=s,this.math=new bt(this),this.ta=new rr(this),this.input=new Ue(this),this.request=new St(this),this.array=new Hr(this);const c=new Ae(this);this.core={plotchar:c.plotchar.bind(c),na:c.na.bind(c),color:c.color,plot:c.plot.bind(c),nz:c.nz.bind(c)}}init(e,r,n=0){return r instanceof l&&(r=r.get(0)),e?!Array.isArray(r)||Array.isArray(r[0])?e[e.length-1]=Array.isArray(r?.[0])?r[0]:this.precision(r):e[e.length-1]=this.precision(r[r.length-1+n]):Array.isArray(r)?e=[this.precision(r[r.length-1+n])]:e=[this.precision(r)],e}precision(e,r=10){return typeof e!="number"||isNaN(e)?e:Number(e.toFixed(r))}param(e,r,n){return typeof e=="string"?e:e instanceof l?r?new l(e.data,e.offset+r):e:!Array.isArray(e)&&typeof e=="object"?e:(this.params[n]||(this.params[n]=[]),Array.isArray(e)?new l(e,r||0):(this.params[n].length===0?this.params[n].push(e):this.params[n][this.params[n].length-1]=e,new l(this.params[n],0)))}get(e,r){if(e instanceof l)return e.get(r);if(Array.isArray(e)){const n=e.length-1-r;return n<0||n>=e.length?NaN:e[n]}return e}set(e,r){if(e instanceof l){e.set(0,r);return}if(Array.isArray(e)){e.length>0?e[e.length-1]=r:e.push(r);return}}}var Qr=Object.defineProperty,Yr=(t,e,r)=>e in t?Qr(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,I=(t,e,r)=>Yr(t,typeof e!="symbol"?e+"":e,r);class Zr{constructor(e,r,n,a,o,i){this.source=e,this.tickerId=r,this.timeframe=n,this.limit=a,this.sDate=o,this.eDate=i,I(this,"data",[]),I(this,"open",[]),I(this,"high",[]),I(this,"low",[]),I(this,"close",[]),I(this,"volume",[]),I(this,"hl2",[]),I(this,"hlc3",[]),I(this,"ohlc4",[]),I(this,"openTime",[]),I(this,"closeTime",[]),I(this,"_readyPromise",null),I(this,"_ready",!1),I(this,"_transpiledCode",null),this._readyPromise=new Promise(s=>{this.loadMarketData(e,r,n,a,o,i).then(c=>{const u=c;this.data=u;const h=u.map(w=>w.open),f=u.map(w=>w.close),m=u.map(w=>w.high),p=u.map(w=>w.low),d=u.map(w=>w.volume),b=u.map(w=>(w.high+w.low+w.close)/3),x=u.map(w=>(w.high+w.low)/2),S=u.map(w=>(w.high+w.low+w.open+w.close)/4),V=u.map(w=>w.openTime),P=u.map(w=>w.closeTime);this.open=h,this.close=f,this.high=m,this.low=p,this.volume=d,this.hl2=x,this.hlc3=b,this.ohlc4=S,this.openTime=V,this.closeTime=P,this._ready=!0,s(!0)})})}get transpiledCode(){return this._transpiledCode}async loadMarketData(e,r,n,a,o,i){return Array.isArray(e)?e:e.getMarketData(r,n,a,o,i)}async ready(){if(this._ready)return!0;if(!this._readyPromise)throw new Error("PineTS is not ready");return this._readyPromise}run(e,r,n){if(n&&n>0){const a=typeof this.eDate>"u"&&!Array.isArray(this.source);return this._runPaginated(e,r,n,a)}else return this._runComplete(e,r)}async _runComplete(e,r){await this.ready(),r||(r=this.data.length);const n=this._initializeContext(e);return this._transpiledCode=this._transpileCode(e),await this._executeIterations(n,this._transpiledCode,this.data.length-r,this.data.length),n}async*_runPaginated(e,r,n,a=!1){await this.ready(),r||(r=this.data.length);const o=this._initializeContext(e);this._transpiledCode=this._transpileCode(e);let i=this.data.length-r;for(;;){const s=this.data.length-i;if(s>0){const h=Math.min(s,n),f=this._getResultLength(o.result);await this._executeIterations(o,this._transpiledCode,i,i+h),i+=h,yield this._createPageContext(o,f);continue}if(!a||Array.isArray(this.source))break;const{newCandles:c,updatedLastCandle:u}=await this._updateMarketData();if(c===0&&!u){yield null;continue}this._removeLastResult(o),i=this.data.length-(c+1)}}_getResultLength(e){if(Array.isArray(e))return e.length;if(typeof e=="object"&&e!==null){const r=Object.keys(e);if(r.length>0&&Array.isArray(e[r[0]]))return e[r[0]].length}return 0}_createPageContext(e,r){const n=new J({marketData:this.data,source:this.source,tickerId:this.tickerId,timeframe:this.timeframe,limit:this.limit,sDate:this.sDate,eDate:this.eDate});if(n.pineTSCode=e.pineTSCode,n.idx=e.idx,Array.isArray(e.result))n.result=e.result.slice(r);else if(typeof e.result=="object"&&e.result!==null){n.result={};for(let a in e.result)Array.isArray(e.result[a])?n.result[a]=e.result[a].slice(r):n.result[a]=e.result[a]}else n.result=e.result;return n.plots={...e.plots},n}async _updateMarketData(e){if(Array.isArray(this.source))return{newCandles:0,updatedLastCandle:!1};const r=this.source,n=this.data.length-1,a=this.data[n].openTime;try{const o=await r.getMarketData(this.tickerId,this.timeframe,void 0,a,e);if(!o||o.length===0)return{newCandles:0,updatedLastCandle:!1};let i=!1,s=0;for(let c=0;c<o.length;c++){const u=o[c];u.openTime===a?(this._replaceCandle(n,u),i=!0):u.openTime>a&&(this._appendCandle(u),s++)}return{newCandles:s,updatedLastCandle:i}}catch(o){return console.error("Error updating market data:",o),{newCandles:0,updatedLastCandle:!1}}}_replaceCandle(e,r){this.data[e]=r,this.open[e]=r.open,this.close[e]=r.close,this.high[e]=r.high,this.low[e]=r.low,this.volume[e]=r.volume,this.hl2[e]=(r.high+r.low)/2,this.hlc3[e]=(r.high+r.low+r.close)/3,this.ohlc4[e]=(r.high+r.low+r.open+r.close)/4,this.openTime[e]=r.openTime,this.closeTime[e]=r.closeTime}_appendCandle(e){this.data.push(e),this.open.push(e.open),this.close.push(e.close),this.high.push(e.high),this.low.push(e.low),this.volume.push(e.volume),this.hl2.push((e.high+e.low)/2),this.hlc3.push((e.high+e.low+e.close)/3),this.ohlc4.push((e.high+e.low+e.open+e.close)/4),this.openTime.push(e.openTime),this.closeTime.push(e.closeTime)}_removeLastResult(e){if(Array.isArray(e.result))e.result.pop();else if(typeof e.result=="object"&&e.result!==null)for(let r in e.result)Array.isArray(e.result[r])&&e.result[r].pop();e.data.close.pop(),e.data.open.pop(),e.data.high.pop(),e.data.low.pop(),e.data.volume.pop(),e.data.hl2.pop(),e.data.hlc3.pop(),e.data.ohlc4.pop(),e.data.openTime.pop(),e.data.closeTime&&e.data.closeTime.pop()}_initializeContext(e){const r=new J({marketData:this.data,source:this.source,tickerId:this.tickerId,timeframe:this.timeframe,limit:this.limit,sDate:this.sDate,eDate:this.eDate});return r.pineTSCode=e,r.data.close=[],r.data.open=[],r.data.high=[],r.data.low=[],r.data.volume=[],r.data.hl2=[],r.data.hlc3=[],r.data.ohlc4=[],r.data.openTime=[],r.data.closeTime=[],r}_transpileCode(e){return Ce.bind(this)(e)}async _executeIterations(e,r,n,a){const o=["const","var","let","params"];for(let i=n;i<a;i++){e.idx=i,e.data.close.push(this.close[i]),e.data.open.push(this.open[i]),e.data.high.push(this.high[i]),e.data.low.push(this.low[i]),e.data.volume.push(this.volume[i]),e.data.hl2.push(this.hl2[i]),e.data.hlc3.push(this.hlc3[i]),e.data.ohlc4.push(this.ohlc4[i]),e.data.openTime.push(this.openTime[i]);const s=await r(e);if(typeof s=="object"){typeof e.result!="object"&&(e.result={});for(let c in s){e.result[c]===void 0&&(e.result[c]=[]);const u=Array.isArray(s[c])?s[c][s[c].length-1]:s[c];e.result[c].push(u)}}else Array.isArray(e.result)||(e.result=[]),e.result.push(s);for(let c of o)for(let u in e[c])if(Array.isArray(e[c][u])){const h=e[c][u],f=h[h.length-1];h.push(f)}}}}var en=Object.defineProperty,tn=(t,e,r)=>e in t?en(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,z=(t,e,r)=>tn(t,typeof e!="symbol"?e+"":e,r);const rn="https://api.binance.com/api/v3",G={1:"1m",3:"3m",5:"5m",15:"15m",30:"30m",45:null,60:"1h",120:"2h",180:null,240:"4h","4H":"4h","1D":"1d",D:"1d","1W":"1w",W:"1w","1M":"1M",M:"1M"};class nn{constructor(e=5*60*1e3){z(this,"cache"),z(this,"cacheDuration"),this.cache=new Map,this.cacheDuration=e}generateKey(e){return Object.entries(e).filter(([r,n])=>n!==void 0).map(([r,n])=>`${r}:${n}`).join("|")}get(e){const r=this.generateKey(e),n=this.cache.get(r);return n?Date.now()-n.timestamp>this.cacheDuration?(this.cache.delete(r),null):n.data:null}set(e,r){const n=this.generateKey(e);this.cache.set(n,{data:r,timestamp:Date.now()})}clear(){this.cache.clear()}cleanup(){const e=Date.now();for(const[r,n]of this.cache.entries())e-n.timestamp>this.cacheDuration&&this.cache.delete(r)}}class an{constructor(){z(this,"cacheManager"),this.cacheManager=new nn(5*60*1e3)}async getMarketDataInterval(e,r,n,a){try{const o=G[r.toUpperCase()];if(!o)return console.error(`Unsupported timeframe: ${r}`),[];const i={"1m":60*1e3,"3m":3*60*1e3,"5m":5*60*1e3,"15m":15*60*1e3,"30m":30*60*1e3,"1h":60*60*1e3,"2h":2*60*60*1e3,"4h":4*60*60*1e3,"1d":24*60*60*1e3,"1w":7*24*60*60*1e3,"1M":30*24*60*60*1e3};let s=[],c=n;const u=a,h=i[o];if(!h)return console.error(`Duration not defined for interval: ${o}`),[];for(;c<u;){const f=Math.min(c+1e3*h,u),m=await this.getMarketData(e,r,1e3,c,f);if(m.length===0)break;s=s.concat(m),c=m[m.length-1].closeTime+1}return s}catch(o){return console.error("Error in getMarketDataInterval:",o),[]}}async getMarketData(e,r,n,a,o){try{const i={tickerId:e,timeframe:r,limit:n,sDate:a,eDate:o},s=this.cacheManager.get(i);if(s)return console.log("cache hit",e,r,n,a,o),s;const c=G[r.toUpperCase()];if(!c)return console.error(`Unsupported timeframe: ${r}`),[];if(this.shouldPaginate(r,n,a,o)&&a&&o){const m=await this.getMarketDataInterval(e,r,a,o),p=n?m.slice(0,n):m;return this.cacheManager.set(i,p),p}let u=`${rn}/klines?symbol=${e}&interval=${c}`;n&&(u+=`&limit=${Math.min(n,1e3)}`),a&&(u+=`&startTime=${a}`),o&&(u+=`&endTime=${o}`);const h=await fetch(u);if(!h.ok)throw new Error(`HTTP error! status: ${h.status}`);const f=(await h.json()).map(m=>({openTime:parseInt(m[0]),open:parseFloat(m[1]),high:parseFloat(m[2]),low:parseFloat(m[3]),close:parseFloat(m[4]),volume:parseFloat(m[5]),closeTime:parseInt(m[6]),quoteAssetVolume:parseFloat(m[7]),numberOfTrades:parseInt(m[8]),takerBuyBaseAssetVolume:parseFloat(m[9]),takerBuyQuoteAssetVolume:parseFloat(m[10]),ignore:m[11]}));return this.cacheManager.set(i,f),f}catch(i){return console.error("Error in binance.klines:",i),[]}}shouldPaginate(e,r,n,a){if(r&&r>1e3)return!0;if(n&&a){const o=G[e.toUpperCase()],i={"1m":60*1e3,"3m":3*60*1e3,"5m":5*60*1e3,"15m":15*60*1e3,"30m":30*60*1e3,"1h":60*60*1e3,"2h":2*60*60*1e3,"4h":4*60*60*1e3,"1d":24*60*60*1e3,"1w":7*24*60*60*1e3,"1M":30*24*60*60*1e3}[o];if(i)return Math.ceil((a-n)/i)>1e3}return!1}}var on=Object.defineProperty,sn=(t,e,r)=>e in t?on(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,Q=(t,e,r)=>sn(t,typeof e!="symbol"?e+"":e,r);const cn=ae(import.meta.url),pn=F.dirname(cn);class ln{constructor(e){if(Q(this,"dataCache",new Map),Q(this,"dataDirectory"),e)this.dataDirectory=e;else{const r=F.resolve(pn,"../../../");this.dataDirectory=F.join(r,"tests","compatibility","_data")}}getDataFileName(e,r,n,a){return n&&a?`${e}-${r}-${n}-${a}.json`:null}loadDataFromFile(e){const r=`file:${e}`;if(this.dataCache.has(r))return this.dataCache.get(r);const n=F.join(this.dataDirectory,e);if(!R.existsSync(n))throw new Error(`Mock data file not found: ${n}`);const a=R.readFileSync(n,"utf-8"),o=JSON.parse(a);return this.dataCache.set(r,o),o}findDataFile(e,r,n,a){if(!R.existsSync(this.dataDirectory))return null;const o=R.readdirSync(this.dataDirectory).filter(c=>c.endsWith(".json"));if(n&&a){const c=`${e}-${r}-${n}-${a}.json`;if(o.includes(c))return c}const i=new RegExp(`^${e}-${r}-(\\d+)-(\\d+)\\.json$`),s=o.filter(c=>i.test(c)).map(c=>{const u=c.match(i);return{file:c,startTime:parseInt(u[1]),endTime:parseInt(u[2])}}).sort((c,u)=>u.endTime-c.endTime);if(s.length===0)return null;if(n&&a){const c=s.find(u=>u.startTime<=n&&u.endTime>=a);if(c)return c.file}return s[0].file}filterData(e,r,n,a){let o=e;return(r||n)&&(o=e.filter(i=>{const s=!r||i.openTime>=r,c=!n||i.openTime<=n;return s&&c})),o.sort((i,s)=>i.openTime-s.openTime),a&&a>0&&(o=o.slice(0,a)),o}normalizeTimeframe(e){return{1:"1m",3:"3m",5:"5m",15:"15m",30:"30m",60:"1h",120:"2h",240:"4h","4H":"4h","1D":"1d",D:"1d","1W":"1w",W:"1w","1M":"1M",M:"1M"}[e.toUpperCase()]||e.toLowerCase()}async getMarketData(e,r,n,a,o){try{const i=this.normalizeTimeframe(r),s=this.findDataFile(e,i,a,o);if(!s)return console.warn(`No mock data file found for ${e} ${i}. Searched in: ${this.dataDirectory}`),[];const c=this.loadDataFromFile(s);return this.filterData(c,a,o,n)}catch(i){return console.error("Error in MockProvider.getMarketData:",i),[]}}clearCache(){this.dataCache.clear()}setDataDirectory(e){this.dataDirectory=e,this.clearCache()}}const un=typeof process<"u"&&process.versions&&process.versions.node;let q=null;if(un)try{q=new ln}catch{q=null}const hn={Binance:new an,...q?{Mock:q}:{}};export{J as Context,Zr as PineTS,hn as Provider};
@@ -1,14 +1,13 @@
1
- import { Input } from '@pinets/namespaces/Input';
2
- import PineMath from '@pinets/namespaces/PineMath';
3
- import { PineRequest } from '@pinets/namespaces/PineRequest';
4
- import TechnicalAnalysis from '@pinets/namespaces/TechnicalAnalysis';
5
- import { PineArray } from './namespaces/PineArray';
1
+ import { Input } from '@pinets/namespaces/input/input.index';
2
+ import PineMath from '@pinets/namespaces/math/math.index';
3
+ import { PineRequest } from '@pinets/namespaces/request/request.index';
4
+ import TechnicalAnalysis from '@pinets/namespaces/ta/ta.index';
5
+ import { PineArray } from './namespaces/array/array.index';
6
6
  import { IProvider } from './marketData/IProvider';
7
7
  export declare class Context {
8
8
  data: any;
9
9
  cache: any;
10
10
  taState: any;
11
- useTACache: boolean;
12
11
  NA: any;
13
12
  math: PineMath;
14
13
  ta: TechnicalAnalysis;
@@ -67,5 +66,17 @@ export declare class Context {
67
66
  * @returns the current value of the param
68
67
  */
69
68
  param(source: any, index: any, name?: string): any;
69
+ /**
70
+ * Access a series value with Pine Script semantics (reverse order)
71
+ * @param source - The source series or array
72
+ * @param index - The lookback index (0 = current value)
73
+ */
74
+ get(source: any, index: number): any;
75
+ /**
76
+ * Set the current value of a series (index 0)
77
+ * @param target - The target series or array
78
+ * @param value - The value to set
79
+ */
80
+ set(target: any, value: any): void;
70
81
  }
71
82
  export default Context;
@@ -1,4 +1,4 @@
1
- import { Context } from '@pinets/index';
1
+ import { Context } from './Context.class';
2
2
  import { IProvider } from '@pinets/marketData/IProvider';
3
3
  export declare class PineTS {
4
4
  private source;
@@ -18,13 +18,87 @@ export declare class PineTS {
18
18
  ohlc4: any;
19
19
  openTime: any;
20
20
  closeTime: any;
21
- private _periods;
22
- get periods(): number;
23
21
  private _readyPromise;
24
22
  private _ready;
23
+ private _transpiledCode;
24
+ get transpiledCode(): Function | String;
25
25
  constructor(source: IProvider | any[], tickerId?: string, timeframe?: string, limit?: number, sDate?: number, eDate?: number);
26
26
  private loadMarketData;
27
27
  ready(): Promise<any>;
28
- run(pineTSCode: Function | String, n?: number, useTACache?: boolean): Promise<Context>;
28
+ /**
29
+ * Run the Pine Script code and return the resulting context.
30
+ * @param pineTSCode
31
+ * @param periods
32
+ * @returns Promise<Context>
33
+ */
34
+ run(pineTSCode: Function | String, periods?: number): Promise<Context>;
35
+ /**
36
+ * Run the Pine Script code with pagination, yielding results page by page.
37
+ * @param pineTSCode
38
+ * @param periods
39
+ * @param pageSize
40
+ * @returns AsyncGenerator<Context>
41
+ */
42
+ run(pineTSCode: Function | String, periods: number | undefined, pageSize: number): AsyncGenerator<Context>;
43
+ /**
44
+ * Run the script completely and return the final context (backward compatible behavior)
45
+ * @private
46
+ */
47
+ private _runComplete;
48
+ /**
49
+ * Run the script with pagination, yielding results page by page
50
+ * Each page contains only the new results for that page, not cumulative results
51
+ * Uses a unified loop that handles both historical and live streaming data
52
+ * @private
53
+ */
54
+ private _runPaginated;
55
+ /**
56
+ * Get the length of the result (works for arrays and objects)
57
+ * @private
58
+ */
59
+ private _getResultLength;
60
+ /**
61
+ * Create a context containing only the new results for the current page
62
+ * @private
63
+ */
64
+ private _createPageContext;
65
+ /**
66
+ * Update market data from the last known candle to now (or eDate if provided)
67
+ * Intelligently replaces the last candle if it's still open, or appends new candles
68
+ * @param eDate - Optional end date, defaults to now
69
+ * @returns Object containing: { newCandles: number, updatedLastCandle: boolean }
70
+ * @private
71
+ */
72
+ private _updateMarketData;
73
+ /**
74
+ * Replace a candle at a specific index with new data
75
+ * @private
76
+ */
77
+ private _replaceCandle;
78
+ /**
79
+ * Append a new candle to the end of market data arrays
80
+ * @private
81
+ */
82
+ private _appendCandle;
83
+ /**
84
+ * Remove the last result from context (for updating an open candle)
85
+ * @private
86
+ */
87
+ private _removeLastResult;
88
+ /**
89
+ * Initialize a new context for running Pine Script code
90
+ * @private
91
+ */
92
+ private _initializeContext;
93
+ /**
94
+ * Transpile the Pine Script code
95
+ * @private
96
+ */
97
+ private _transpileCode;
98
+ /**
99
+ * Execute iterations from startIdx to endIdx, updating the context
100
+ * @private
101
+ */
102
+ private _executeIterations;
29
103
  }
30
104
  export default PineTS;
@@ -0,0 +1,10 @@
1
+ export declare class Series {
2
+ data: any[];
3
+ offset: number;
4
+ constructor(data: any[], offset?: number);
5
+ get(index: number): any;
6
+ set(index: number, value: any): void;
7
+ get length(): number;
8
+ toArray(): any[];
9
+ static from(source: any): Series;
10
+ }
@@ -0,0 +1,49 @@
1
+ import Context from '../Context.class';
2
+ /**
3
+ * A lightweight wrapper around arrays to provide Pine Script-like "series" access behavior.
4
+ * Pine Script series are indexed relative to the current bar:
5
+ * - series[0] is the current value
6
+ * - series[1] is the previous value
7
+ *
8
+ * This class maps this reversed, relative indexing to the underlying chronological array
9
+ * using the Context's current index.
10
+ */
11
+ export declare class TimeSeries<T = any> {
12
+ private readonly _data;
13
+ private readonly _ctx;
14
+ constructor(_data: T[], _ctx: Context);
15
+ /**
16
+ * Get the value at the specified offset from the current bar.
17
+ * @param offset The lookback offset (0 = current, 1 = previous, etc.)
18
+ */
19
+ get(offset: number): T;
20
+ /**
21
+ * Set the value at the specified offset from the current bar.
22
+ * Usually used for setting current value (offset 0).
23
+ */
24
+ set(offset: number, value: T): T;
25
+ /**
26
+ * Get the length of the series *so far* (up to current index)
27
+ * Note: This mimics the behavior where future data isn't "visible" yet
28
+ */
29
+ get length(): number;
30
+ /**
31
+ * Returns the underlying raw data array.
32
+ * Useful for performance-critical bulk operations that understand the indexing.
33
+ */
34
+ get data(): T[];
35
+ /**
36
+ * Creates a copy of the visible series as a reversed array (Pine Script standard format for bulk ops).
37
+ * Warning: This is O(N) and should be used sparingly.
38
+ */
39
+ toArray(): T[];
40
+ /**
41
+ * Push a value to the underlying array.
42
+ * Used when constructing user-defined series dynamically.
43
+ */
44
+ push(value: T): number;
45
+ /**
46
+ * Update the underlying array reference (e.g. for re-assignment)
47
+ */
48
+ replaceData(newData: T[]): void;
49
+ }
@@ -0,0 +1,15 @@
1
+ export interface IContext {
2
+ idx: number;
3
+ NA: any;
4
+ }
5
+ export declare class TimeSeries<T = any> {
6
+ private _data;
7
+ private context;
8
+ constructor(context: IContext, data?: T[]);
9
+ get(offset: number): T;
10
+ set(value: T, offset?: number): void;
11
+ push(value: T): void;
12
+ pop(): T | undefined;
13
+ get data(): T[];
14
+ get length(): number;
15
+ }
@@ -0,0 +1,77 @@
1
+ import { IProvider } from '@pinets/marketData/IProvider';
2
+ interface Kline {
3
+ openTime: number;
4
+ open: number;
5
+ high: number;
6
+ low: number;
7
+ close: number;
8
+ volume: number;
9
+ closeTime: number;
10
+ quoteAssetVolume: number;
11
+ numberOfTrades: number;
12
+ takerBuyBaseAssetVolume: number;
13
+ takerBuyQuoteAssetVolume: number;
14
+ ignore: number | string;
15
+ }
16
+ /**
17
+ * Mock Market Data Provider for Unit Tests
18
+ *
19
+ * This provider reads market data from pre-fetched JSON files instead of making API calls.
20
+ * It's designed to be used in unit tests to provide consistent, offline test data.
21
+ *
22
+ * Usage:
23
+ * ```typescript
24
+ * const mockProvider = new MockProvider();
25
+ * const data = await mockProvider.getMarketData('BTCUSDC', '1h', 100, startTime, endTime);
26
+ * ```
27
+ *
28
+ * The provider looks for JSON files in the tests/compatibility/_data directory
29
+ * with the naming pattern: {SYMBOL}-{TIMEFRAME}-{START_TIME}-{END_TIME}.json
30
+ *
31
+ * Example: BTCUSDC-1h-1704067200000-1763683199000.json
32
+ */
33
+ export declare class MockProvider implements IProvider {
34
+ private dataCache;
35
+ private readonly dataDirectory;
36
+ constructor(dataDirectory?: string);
37
+ /**
38
+ * Generates a cache key for the data file
39
+ */
40
+ private getDataFileName;
41
+ /**
42
+ * Loads data from JSON file
43
+ */
44
+ private loadDataFromFile;
45
+ /**
46
+ * Finds the best matching data file for the given parameters
47
+ */
48
+ private findDataFile;
49
+ /**
50
+ * Filters data based on date range and limit
51
+ */
52
+ private filterData;
53
+ /**
54
+ * Normalizes timeframe to match file naming convention
55
+ */
56
+ private normalizeTimeframe;
57
+ /**
58
+ * Implements IProvider.getMarketData
59
+ *
60
+ * @param tickerId - Symbol (e.g., 'BTCUSDC')
61
+ * @param timeframe - Timeframe (e.g., '1h', '60', 'D')
62
+ * @param limit - Optional limit on number of candles to return
63
+ * @param sDate - Optional start date (timestamp in milliseconds)
64
+ * @param eDate - Optional end date (timestamp in milliseconds)
65
+ * @returns Promise<Kline[]> - Array of candle data
66
+ */
67
+ getMarketData(tickerId: string, timeframe: string, limit?: number, sDate?: number, eDate?: number): Promise<Kline[]>;
68
+ /**
69
+ * Clears the data cache
70
+ */
71
+ clearCache(): void;
72
+ /**
73
+ * Sets a custom data directory
74
+ */
75
+ setDataDirectory(directory: string): void;
76
+ }
77
+ export {};
@@ -1,4 +1,7 @@
1
- import { BinanceProvider } from './Binance/BinanceProvider.class';
2
- export declare const Provider: {
3
- Binance: BinanceProvider;
1
+ import { IProvider } from './IProvider';
2
+ type TProvider = {
3
+ [key: string]: IProvider;
4
4
  };
5
+ export declare const Provider: TProvider;
6
+ export declare function registerProvider(name: string, provider: IProvider): void;
7
+ export {};
@@ -0,0 +1,5 @@
1
+ export declare class PineArrayObject {
2
+ array: any;
3
+ constructor(array: any);
4
+ toString(): string;
5
+ }