pinets 0.1.1 → 0.1.33

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.
@@ -15,5 +15,5 @@
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 z from"acorn";import*as x from"acorn-walk";import*as K from"astring";var U=Object.defineProperty,H=(r,e,t)=>e in r?U(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,E=(r,e,t)=>H(r,typeof e!="symbol"?e+"":e,t);class Q{constructor(){E(this,"scopes",[]),E(this,"scopeTypes",[]),E(this,"scopeCounts",new Map),E(this,"contextBoundVars",new Set),E(this,"arrayPatternElements",new Set),E(this,"rootParams",new Set),E(this,"varKinds",new Map),E(this,"loopVars",new Set),E(this,"loopVarNames",new Map),E(this,"paramIdCounter",0),E(this,"tempVarCounter",0),this.pushScope("glb")}get nextParamIdArg(){return{type:"Identifier",name:`'p${this.paramIdCounter++}'`}}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,t=!1){this.contextBoundVars.add(e),t&&this.rootParams.add(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,t){this.loopVars.add(e),this.loopVarNames.set(e,t)}getLoopVariableName(e){return this.loopVarNames.get(e)}isLoopVariable(e){return this.loopVars.has(e)}addVariable(e,t){if(this.isContextBound(e))return e;const n=this.scopes[this.scopes.length-1],i=this.scopeTypes[this.scopeTypes.length-1],o=this.scopeCounts.get(i)||1,a=`${i}${o}_${e}`;return n.set(e,a),this.varKinds.set(a,t),a}getVariable(e){if(this.loopVars.has(e)){const t=this.loopVarNames.get(e);if(t)return[t,"let"]}if(this.isContextBound(e))return[e,"let"];for(let t=this.scopes.length-1;t>=0;t--){const n=this.scopes[t];if(n.has(e)){const i=n.get(e),o=this.varKinds.get(i)||"let";return[i,o]}}return[e,"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 f="$",P={type:"Identifier",name:"undefined"};function C(r,e){if(r.computed&&r.property.type==="Identifier"){if(e.isLoopVariable(r.property.name))return;if(!e.isContextBound(r.property.name)){const[t,n]=e.getVariable(r.property.name);r.property={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:t},computed:!1},r.property={type:"MemberExpression",object:r.property,property:{type:"Literal",value:0},computed:!0}}}if(r.computed&&r.object.type==="Identifier"){if(e.isLoopVariable(r.object.name))return;if(!e.isContextBound(r.object.name)){const[t,n]=e.getVariable(r.object.name);r.object={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:t},computed:!1}}if(r.property.type==="MemberExpression"){const t=r.property;t._indexTransformed||(C(t,e),t._indexTransformed=!0)}}}function j(r,e,t){if(r.object&&r.object.type==="Identifier"&&r.object.name==="Math")return;const n=t.getCurrentScopeType()=="if",i=t.getCurrentScopeType()=="els",o=t.getCurrentScopeType()=="for";!n&&!i&&!o&&r.object&&r.object.type==="Identifier"&&t.isContextBound(r.object.name)&&!t.isRootParam(r.object.name)||r._indexTransformed||(C(r,t),r._indexTransformed=!0)}function B(r,e){r.declarations.forEach(t=>{const n=t.init&&t.init.type==="MemberExpression"&&t.init.object&&(t.init.object.name==="context"||t.init.object.name===f||t.init.object.name==="context2"),i=t.init&&t.init.type==="MemberExpression"&&t.init.object?.object&&(t.init.object.object.name==="context"||t.init.object.object.name===f||t.init.object.object.name==="context2"),o=t.init&&t.init.type==="ArrowFunctionExpression";if(n){t.id.name&&e.addContextBoundVar(t.id.name),t.id.properties&&t.id.properties.forEach(p=>{p.key.name&&e.addContextBoundVar(p.key.name)}),t.init.object.name=f;return}if(i){t.id.name&&e.addContextBoundVar(t.id.name),t.id.properties&&t.id.properties.forEach(p=>{p.key.name&&e.addContextBoundVar(p.key.name)}),t.init.object.object.name=f;return}o&&t.init.params.forEach(p=>{p.type==="Identifier"&&e.addContextBoundVar(p.name)});const a=e.addVariable(t.id.name,r.kind),s=r.kind;t.init&&!o&&(t.init.type==="CallExpression"&&t.init.callee.type==="MemberExpression"&&t.init.callee.object&&t.init.callee.object.type==="Identifier"&&e.isContextBound(t.init.callee.object.name)?M(t.init,e):x.recursive(t.init,{parent:t.init},{Identifier(p,u){p.parent=u.parent,v(p,e);const b=p.parent&&p.parent.type==="BinaryExpression",l=p.parent&&p.parent.type==="ConditionalExpression";p.type==="Identifier"&&(b||l)&&Object.assign(p,{type:"MemberExpression",object:{type:"Identifier",name:p.name},property:{type:"Literal",value:0},computed:!0})},CallExpression(p,u,b){p.callee.type==="Identifier"&&(p.callee.parent=p),p.arguments.forEach(l=>{l.type==="Identifier"&&(l.parent=p)}),M(p,e),p.arguments.forEach(l=>b(l,{parent:p}))},BinaryExpression(p,u,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,u,b){p.object.type==="Identifier"&&(p.object.parent=p),p.property.type==="Identifier"&&(p.property.parent=p),C(p,e),p.object&&b(p.object,{parent:p})}}));const m={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:s},computed:!1},property:{type:"Identifier",name:a},computed:!1},c=e.isArrayPatternElement(t.id.name),y=!c&&t.init&&t.init.type==="MemberExpression"&&t.init.computed&&t.init.property&&(t.init.property.type==="Literal"||t.init.property.type==="MemberExpression");t.init?.property?.type==="MemberExpression"&&(t.init.property._indexTransformed||(C(t.init.property,e),t.init.property._indexTransformed=!0));const h={type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:m,right:t.init?o||c?t.init:{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:"init"},computed:!1},arguments:y?[m,t.init.object,t.init.property]:[m,t.init]}:{type:"Identifier",name:"undefined"}}};c&&(h.expression.right.object.property.name+="?.[0]"),o&&(e.pushScope("fn"),x.recursive(t.init.body,e,{BlockStatement(p,u,b){p.body.forEach(l=>b(l,u))},IfStatement(p,u,b){u.pushScope("if"),b(p.consequent,u),p.alternate&&(u.pushScope("els"),b(p.alternate,u),u.popScope()),u.popScope()},VariableDeclaration(p,u){B(p,u)},Identifier(p,u){v(p,u)},AssignmentExpression(p,u){T(p,u)}}),e.popScope()),Object.assign(r,h)})}function v(r,e){if(r.name!==f){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("`")||e.isLoopVariable(r.name)||e.isContextBound(r.name)&&!e.isRootParam(r.name))return;const t=r.parent&&r.parent.type==="MemberExpression"&&r.parent.object===r&&e.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 i=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee&&r.parent.callee.type==="MemberExpression"&&e.isContextBound(r.parent.callee.object.name),o=r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed,a=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"&&e.isContextBound(r.parent.parent.callee.object.name),s=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee===r;if(t||n||i||a||s){if(s)return;const[h,p]=e.getVariable(r.name);Object.assign(r,{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:p},computed:!1},property:{type:"Identifier",name:h},computed:!1});return}const[m,c]=e.getVariable(r.name),y={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:c},computed:!1},property:{type:"Identifier",name:m},computed:!1};!(r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed&&r.parent.object===r)&&!o?Object.assign(r,{type:"MemberExpression",object:y,property:{type:"Literal",value:0},computed:!0}):Object.assign(r,y)}}function T(r,e){if(r.left.type==="Identifier"){const[t,n]=e.getVariable(r.left.name),i={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:t},computed:!1};r.left={type:"MemberExpression",object:i,property:{type:"Literal",value:0},computed:!0}}x.recursive(r.right,{parent:r.right,inNamespaceCall:!1},{Identifier(t,n,i){t.parent=n.parent,v(t,e);const o=t.parent&&t.parent.type==="BinaryExpression";(t.parent&&t.parent.type==="ConditionalExpression"||o)&&(t.type==="MemberExpression"?C(t,e):t.type==="Identifier"&&S(t))},MemberExpression(t,n,i){C(t,e),t.object&&i(t.object,{parent:t,inNamespaceCall:n.inNamespaceCall})},CallExpression(t,n,i){const o=t.callee&&t.callee.type==="MemberExpression"&&t.callee.object&&t.callee.object.type==="Identifier"&&e.isContextBound(t.callee.object.name);M(t,e),t.arguments.forEach(a=>i(a,{parent:t,inNamespaceCall:o||n.inNamespaceCall}))}})}function X(r,e,t=!1){r.params.forEach(n=>{n.type==="Identifier"&&e.addContextBoundVar(n.name,t)})}function D(r,e){const t=e.getCurrentScopeType();if(r.argument){if(r.argument.type==="ArrayExpression")r.argument.elements=r.argument.elements.map(n=>{if(n.type==="Identifier"){if(e.isContextBound(n.name)&&!e.isRootParam(n.name))return{type:"MemberExpression",object:n,property:{type:"Literal",value:0},computed:!0};const[i,o]=e.getVariable(n.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:i},computed:!1},property:{type:"Literal",value:0},computed:!0}}else if(n.type==="MemberExpression")return n.computed&&n.object.type==="Identifier"&&e.isContextBound(n.object.name)&&!e.isRootParam(n.object.name)||j(n,"",e),n;return n}),r.argument={type:"ArrayExpression",elements:[r.argument]};else if(r.argument.type==="BinaryExpression")x.recursive(r.argument,e,{Identifier(n,i){v(n,i),n.type==="Identifier"&&S(n)},MemberExpression(n){j(n,"",e)}});else if(r.argument.type==="ObjectExpression")r.argument.properties=r.argument.properties.map(n=>{if(n.shorthand){const[i,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:f},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:i},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return n});else if(r.argument.type==="Identifier"){const[n,i]=e.getVariable(r.argument.name);r.argument={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:i},computed:!1},property:{type:"Identifier",name:n},computed:!1},r.argument={type:"MemberExpression",object:r.argument,property:{type:"Literal",value:0},computed:!0}}t==="fn"&&(r.argument={type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:"precision"}},arguments:[r.argument]})}}function N(r,e){if(r.type==="Identifier"){if(e.isLoopVariable(r.name))return r;if(e.isRootParam(r.name)){const[i,o]=e.getVariable(r.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:i},computed:!1}}if(e.isContextBound(r.name))return r;const[t,n]=e.getVariable(r.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:f},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:t},computed:!1}}return r}function _(r,e,t=""){return r.type==="BinaryExpression"?L(r,e,t):r.type==="MemberExpression"?{type:"MemberExpression",object:r.object.type==="Identifier"?N(r.object,e):r.object,property:r.property,computed:r.computed}:r.type==="Identifier"?e.isLoopVariable(r.name)||r.parent&&r.parent.type==="MemberExpression"&&r.parent.property===r?r:{type:"MemberExpression",object:N(r,e),property:{type:"Literal",value:0},computed:!0}:r}function L(r,e,t){const n=_(r.left,e,t),i=_(r.right,e,t),o={type:"BinaryExpression",operator:r.operator,left:n,right:i,start:r.start,end:r.end};return x.recursive(o,e,{CallExpression(a,s){a._transformed||M(a,s)},MemberExpression(a){j(a,"",e)}}),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[o,P,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function $(r,e,t){if(r.type==="BinaryExpression")return L(r,t,e);if(r.type==="MemberExpression"&&r.computed&&r.property){const n=r.object.type==="Identifier"&&t.isContextBound(r.object.name)&&!t.isRootParam(r.object.name)?r.object:N(r.object,t),i=r.property.type==="Identifier"&&!t.isContextBound(r.property.name)&&!t.isLoopVariable(r.property.name)?N(r.property,t):r.property;return{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:e},property:{type:"Identifier",name:"param"},computed:!1},arguments:[n,i,t.nextParamIdArg],_transformed:!0,_isParamCall:!0}}return r.type==="ObjectExpression"&&(r.properties=r.properties.map(n=>{if(n.value.name){const[i,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:f},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:i},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return n})),r.type==="Identifier"&&t.isContextBound(r.name)&&!t.isRootParam(r.name)?{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:e},property:{type:"Identifier",name:"param"},computed:!1},arguments:[r,P,t.nextParamIdArg],_transformed:!0,_isParamCall:!0}:(r?.type==="CallExpression"&&M(r,t),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:e},property:{type:"Identifier",name:"param"},computed:!1},arguments:[r.type==="Identifier"?N(r,t):r,P,t.nextParamIdArg],_transformed:!0,_isParamCall:!0})}function M(r,e){if(!r._transformed){if(r.callee&&r.callee.type==="MemberExpression"&&r.callee.object&&r.callee.object.type==="Identifier"&&(e.isContextBound(r.callee.object.name)||r.callee.object.name==="math"||r.callee.object.name==="ta")){const t=r.callee.object.name;r.arguments=r.arguments.map(n=>n._isParamCall?n:$(n,t,e)),r._transformed=!0}else r.callee&&r.callee.type==="Identifier"&&(r.arguments=r.arguments.map(t=>t._isParamCall?t:$(t,f,e)),r._transformed=!0);r.arguments.forEach(t=>{x.recursive(t,e,{CallExpression(n,i){n._transformed||M(n,i)},MemberExpression(n){j(n,"",e)}})})}}function Y(r,e){r.params.forEach(t=>{t.type==="Identifier"&&e.addContextBoundVar(t.name,!1)}),r.body&&r.body.type==="BlockStatement"&&(e.pushScope("fn"),x.recursive(r.body,e,{BlockStatement(t,n,i){t.body.forEach(o=>i(o,n))},ReturnStatement(t,n){D(t,n)},VariableDeclaration(t,n){B(t,n)},Identifier(t,n){v(t,n)},CallExpression(t,n){M(t,n),t.arguments.forEach(i=>{i.type==="BinaryExpression"&&x.recursive(i,n,{CallExpression(o,a){M(o,a)},MemberExpression(o){j(o,"",n)}})})},MemberExpression(t){j(t,"",e)},AssignmentExpression(t,n){T(t,n)},ForStatement(t,n,i){O(t,n,i)},IfStatement(t,n,i){F(t,n,i)},BinaryExpression(t,n,i){x.recursive(t,n,{CallExpression(o,a){M(o,a)},MemberExpression(o){j(o,"",n)}})}}),e.popScope())}function S(r,e){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 O(r,e,t){if(r.init&&r.init.type==="VariableDeclaration"){const n=r.init.declarations[0],i=n.id.name;e.addLoopVariable(i,i),r.init={type:"VariableDeclaration",kind:r.init.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:i},init:n.init}]},n.init&&x.recursive(n.init,e,{Identifier(o,a){e.isLoopVariable(o.name)||(e.pushScope("for"),v(o,a),e.popScope())},MemberExpression(o){e.pushScope("for"),j(o,"",e),e.popScope()}})}r.test&&x.recursive(r.test,e,{Identifier(n,i){!e.isLoopVariable(n.name)&&!n.computed&&(e.pushScope("for"),v(n,i),n.type==="Identifier"&&(n.computed=!0,S(n)),e.popScope())},MemberExpression(n){e.pushScope("for"),j(n,"",e),e.popScope()}}),r.update&&x.recursive(r.update,e,{Identifier(n,i){e.isLoopVariable(n.name)||(e.pushScope("for"),v(n,i),e.popScope())}}),e.pushScope("for"),t(r.body,e),e.popScope()}function G(r,e){x.recursive(r,e,{MemberExpression(t){j(t,"",e)},CallExpression(t,n){M(t,n)},Identifier(t,n){v(t,n);const i=e.getCurrentScopeType()==="if";e.isContextBound(t.name)&&!e.isRootParam(t.name)&&i&&S(t)}})}function F(r,e,t){r.test&&(e.pushScope("if"),G(r.test,e),e.popScope()),e.pushScope("if"),t(r.consequent,e),e.popScope(),r.alternate&&(e.pushScope("els"),t(r.alternate,e),e.popScope())}function J(r){x.recursive(r,null,{VariableDeclaration(e,t,n){e.declarations&&e.declarations.length>0&&e.declarations.forEach(i=>{if(i.init&&i.init.type==="ArrowFunctionExpression"&&i.init.start!==0){const o={type:"FunctionDeclaration",id:i.id,params:i.init.params,body:i.init.body.type==="BlockStatement"?i.init.body:{type:"BlockStatement",body:[{type:"ReturnStatement",argument:i.init.body}]},async:i.init.async,generator:!1};Object.assign(e,o)}}),e.body&&e.body.body&&e.body.body.forEach(i=>n(i,t))}})}function Z(r,e){x.simple(r,{VariableDeclaration(t){t.declarations.forEach(n=>{const i=n.init&&n.init.type==="MemberExpression"&&n.init.object&&(n.init.object.name==="context"||n.init.object.name===f||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===f||n.init.object.object.name==="context2");(i||o)&&(n.id.name&&e.addContextBoundVar(n.id.name),n.id.properties&&n.id.properties.forEach(a=>{a.key.name&&e.addContextBoundVar(a.key.name)}))})}})}function ee(r){let e=typeof r=="function"?r.toString():r;const t=z.parse(e.trim(),{ecmaVersion:"latest",sourceType:"module"});J(t);const n=new Q;let i;Z(t,n),x.simple(t,{FunctionDeclaration(a){Y(a,n)},ArrowFunctionExpression(a){const s=a.start===0;s&&a.params&&a.params.length>0&&(i=a.params[0].name,a.params[0].name=f),X(a,n,s)},VariableDeclaration(a){a.declarations.forEach(s=>{if(s.id.type==="ArrayPattern"){const m=n.generateTempVar(),c={type:"VariableDeclaration",kind:a.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:m},init:s.init}]};s.id.elements?.forEach(h=>{h.type==="Identifier"&&n.addArrayPatternElement(h.name)});const y=s.id.elements.map((h,p)=>({type:"VariableDeclaration",kind:a.kind,declarations:[{type:"VariableDeclarator",id:h,init:{type:"MemberExpression",object:{type:"Identifier",name:m},property:{type:"Literal",value:p},computed:!0}}]}));Object.assign(a,{type:"BlockStatement",body:[c,...y]})}})},ForStatement(a){}}),x.recursive(t,n,{BlockStatement(a,s,m){a.body.forEach(c=>m(c,s))},ReturnStatement(a,s){D(a,s)},VariableDeclaration(a,s){B(a,s)},Identifier(a,s){v(a,s)},CallExpression(a,s){M(a,s)},MemberExpression(a){j(a,i,n)},AssignmentExpression(a,s){T(a,s)},FunctionDeclaration(a,s){},ForStatement(a,s,m){O(a,s,m)},IfStatement(a,s,m){F(a,s,m)}});const o=K.generate(t);return new Function("",`return ${o}`)(this)}var te=Object.defineProperty,re=(r,e,t)=>e in r?te(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,g=(r,e,t)=>re(r,typeof e!="symbol"?e+"":e,t);class Oe{constructor(e,t,n,i,o,a){this.source=e,this.tickerId=t,this.timeframe=n,this.limit=i,this.sDate=o,this.eDate=a,g(this,"data",[]),g(this,"open",[]),g(this,"high",[]),g(this,"low",[]),g(this,"close",[]),g(this,"volume",[]),g(this,"hl2",[]),g(this,"hlc3",[]),g(this,"ohlc4",[]),g(this,"openTime",[]),g(this,"closeTime",[]),g(this,"_periods"),g(this,"_readyPromise",null),g(this,"_ready",!1),this._readyPromise=new Promise(s=>{this.loadMarketData(e,t,n,i,o,a).then(m=>{const c=m.reverse();this._periods=c.length,this.data=c;const y=c.map(d=>d.open),h=c.map(d=>d.close),p=c.map(d=>d.high),u=c.map(d=>d.low),b=c.map(d=>d.volume),l=c.map(d=>(d.high+d.low+d.close)/3),I=c.map(d=>(d.high+d.low)/2),w=c.map(d=>(d.high+d.low+d.open+d.close)/4),q=c.map(d=>d.openTime),W=c.map(d=>d.closeTime);this.open=y,this.close=h,this.high=p,this.low=u,this.volume=b,this.hl2=I,this.hlc3=l,this.ohlc4=w,this.openTime=q,this.closeTime=W,this._ready=!0,s(!0)})})}get periods(){return this._periods}async loadMarketData(e,t,n,i,o,a){return Array.isArray(e)?e:e.getMarketData(t,n,i,o,a)}async ready(){if(this._ready)return!0;if(!this._readyPromise)throw new Error("PineTS is not ready");return this._readyPromise}async run(e,t){await this.ready(),t||(t=this._periods);const n=new _e(this.data);n.timeframe=this.timeframe;let i=ee.bind(this)(e);const o=["const","var","let","params"];for(let a=this._periods-t,s=t-1;a<this._periods;a++,s--){n.idx=a,n.data.close=this.close.slice(s),n.data.open=this.open.slice(s),n.data.high=this.high.slice(s),n.data.low=this.low.slice(s),n.data.volume=this.volume.slice(s),n.data.hl2=this.hl2.slice(s),n.data.hlc3=this.hlc3.slice(s),n.data.ohlc4=this.ohlc4.slice(s),n.data.openTime=this.openTime.slice(s),n.data.closeTime=this.closeTime.slice(s);const m=await i(n);if(typeof m=="object"){typeof n.result!="object"&&(n.result={});for(let c in m){n.result[c]===void 0&&(n.result[c]=[]);const y=Array.isArray(m[c])?m[c][0]:m[c];n.result[c].push(y)}}else Array.isArray(n.result)||(n.result=[]),n.result.push(m);for(let c of o)for(let y in n[c])if(Array.isArray(n[c][y])){const h=n[c][y][0];n[c][y].unshift(h)}}return n}}var ne=Object.defineProperty,ie=(r,e,t)=>e in r?ne(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,oe=(r,e,t)=>ie(r,typeof e!="symbol"?e+"":e,t);class ae{constructor(e){this.context=e,oe(this,"color",{white:"white",lime:"lime",green:"green",red:"red",maroon:"maroon",black:"black",gray:"gray",blue:"blue"})}extractPlotOptions(e){const t={};for(let n in e)Array.isArray(e[n])?t[n]=e[n][0]:t[n]=e[n];return t}indicator(e,t,n){}plotchar(e,t,n){this.context.plots[t]||(this.context.plots[t]={data:[],options:this.extractPlotOptions(n),title:t}),this.context.plots[t].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:e[0],options:this.extractPlotOptions(n)})}plot(e,t,n){this.context.plots[t]||(this.context.plots[t]={data:[],options:this.extractPlotOptions(n),title:t}),this.context.plots[t].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:e[0],options:this.extractPlotOptions(n)})}na(e){return Array.isArray(e)?isNaN(e[0]):isNaN(e)}nz(e,t=0){return Array.isArray(e)?isNaN(e[0])?t:e[0]:isNaN(e)?t:e}}class se{constructor(e){this.context=e}param(e,t=0){return Array.isArray(e)?[e[t]]:[e]}any(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}int(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}float(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}bool(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}string(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}timeframe(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}time(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}price(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}session(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}source(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}symbol(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}text_area(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}enum(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}color(e,{title:t,group:n}={}){return Array.isArray(e)?e[0]:e}}var pe=Object.defineProperty,ce=(r,e,t)=>e in r?pe(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,le=(r,e,t)=>ce(r,typeof e!="symbol"?e+"":e,t);class me{constructor(e){this.context=e,le(this,"_cache",{})}param(e,t=0){return Array.isArray(e)?e[t]:e}abs(e){return Math.abs(e)}pow(e,t){return Math.pow(e,t)}sqrt(e){return Math.sqrt(e)}log(e){return Math.log(e)}ln(e){return Math.log(e)}exp(e){return Math.exp(e)}floor(e){return Math.floor(e)}ceil(e){return Math.ceil(e)}round(e){return Math.round(e)}random(){return Math.random()}max(...e){return Math.max(...e)}min(...e){return Math.min(...e)}sin(e){return Math.sin(e)}cos(e){return Math.cos(e)}tan(e){return Math.tan(e)}asin(e){return Math.asin(e)}acos(e){return Math.acos(e)}atan(e){return Math.atan(e)}avg(...e){return e.reduce((t,n)=>{const i=Array.isArray(t)?t[0]:t,o=Array.isArray(n)?n[0]:n;return i+o},0)/e.length}}var ye=Object.defineProperty,ue=(r,e,t)=>e in r?ye(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,he=(r,e,t)=>ue(r,typeof e!="symbol"?e+"":e,t);class de{constructor(e){this.context=e,he(this,"_cache",{})}param(e,t,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(e)?(t?this.context.params[n]=e.slice(t):this.context.params[n]=e.slice(0),[e[t],n]):(this.context.params[n][0]=e,[e,n])}async security(e,t,n){throw new Error("Not implemented")}}class fe{constructor(e){this.context=e}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(e,t,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(e)?t?(this.context.params[n]=e.slice(t),this.context.params[n]):(this.context.params[n]=e.slice(0),this.context.params[n]):(this.context.params[n][0]=e,this.context.params[n])}ema(e,t){const n=Array.isArray(t)?t[0]:t,i=be(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}sma(e,t){const n=Array.isArray(t)?t[0]:t,i=k(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}vwma(e,t){const n=Array.isArray(t)?t[0]:t,i=this.context.data.volume,o=Ae(e.slice(0).reverse(),i.slice(0).reverse(),n),a=this.context.idx;return this.context.precision(o[a])}wma(e,t){const n=Array.isArray(t)?t[0]:t,i=V(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}hma(e,t){const n=Array.isArray(t)?t[0]:t,i=Ee(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}rma(e,t){const n=Array.isArray(t)?t[0]:t,i=ge(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}change(e,t=1){const n=Array.isArray(t)?t[0]:t,i=je(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}rsi(e,t){const n=Array.isArray(t)?t[0]:t,i=xe(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}atr(e){const t=Array.isArray(e)?e[0]:e,n=this.context.data.high.slice().reverse(),i=this.context.data.low.slice().reverse(),o=this.context.data.close.slice().reverse(),a=R(n,i,o,t),s=this.context.idx;return this.context.precision(a[s])}mom(e,t){const n=Array.isArray(t)?t[0]:t,i=Me(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}roc(e,t){const n=Array.isArray(t)?t[0]:t,i=Ie(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}dev(e,t){const n=Array.isArray(t)?t[0]:t,i=ve(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}variance(e,t){const n=Array.isArray(t)?t[0]:t,i=we(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}highest(e,t){const n=Array.isArray(t)?t[0]:t,i=Ce(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}lowest(e,t){const n=Array.isArray(t)?t[0]:t,i=Ne(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}median(e,t){const n=Array.isArray(t)?t[0]:t,i=Ve(e.slice(0).reverse(),n),o=this.context.idx;return this.context.precision(i[o])}stdev(e,t,n=!0){const i=Array.isArray(t)?t[0]:t,o=Array.isArray(n)?n[0]:n,a=Se(e.slice(0).reverse(),i,o),s=this.context.idx;return this.context.precision(a[s])}linreg(e,t,n){const i=Array.isArray(t)?t[0]:t,o=Array.isArray(n)?n[0]:n,a=Pe(e.slice(0).reverse(),i,o),s=this.context.idx;return this.context.precision(a[s])}supertrend(e,t){const n=Array.isArray(e)?e[0]:e,i=Array.isArray(t)?t[0]:t,o=this.context.data.high.slice().reverse(),a=this.context.data.low.slice().reverse(),s=this.context.data.close.slice().reverse(),[m,c]=Be(o,a,s,n,i),y=this.context.idx;return[[this.context.precision(m[y]),c[y]]]}}function R(r,e,t,n){const i=new Array(r.length);i[0]=r[0]-e[0];for(let s=1;s<r.length;s++){const m=r[s]-e[s],c=Math.abs(r[s]-t[s-1]),y=Math.abs(e[s]-t[s-1]);i[s]=Math.max(m,c,y)}const o=new Array(r.length).fill(NaN);let a=0;for(let s=0;s<n;s++)a+=i[s];o[n-1]=a/n;for(let s=n;s<i.length;s++)o[s]=(o[s-1]*(n-1)+i[s])/n;return o}function be(r,e){const t=new Array(r.length).fill(NaN),n=2/(e+1);let i=0;for(let o=0;o<e;o++)i+=r[o]||0;t[e-1]=i/e;for(let o=e;o<r.length;o++)t[o]=r[o]*n+t[o-1]*(1-n);return t}function xe(r,e){const t=new Array(r.length).fill(NaN),n=new Array(r.length).fill(0),i=new Array(r.length).fill(0);for(let s=1;s<r.length;s++){const m=r[s]-r[s-1];n[s]=m>0?m:0,i[s]=m<0?-m:0}let o=0,a=0;for(let s=1;s<=e;s++)o+=n[s],a+=i[s];o/=e,a/=e,t[e]=a===0?100:100-100/(1+o/a);for(let s=e+1;s<r.length;s++)o=(o*(e-1)+n[s])/e,a=(a*(e-1)+i[s])/e,t[s]=a===0?100:100-100/(1+o/a);return t}function ge(r,e){const t=new Array(r.length).fill(NaN),n=1/e;let i=0;for(let o=0;o<e;o++)i+=r[o]||0;t[e-1]=i/e;for(let o=e;o<r.length;o++){const a=r[o]||0;t[o]=a*n+t[o-1]*(1-n)}return t}function k(r,e){const t=new Array(r.length).fill(NaN);for(let n=e-1;n<r.length;n++){let i=0;for(let o=0;o<e;o++)i+=r[n-o]||0;t[n]=i/e}return t}function Ae(r,e,t){const n=new Array(r.length).fill(NaN);for(let i=t-1;i<r.length;i++){let o=0,a=0;for(let s=0;s<t;s++)o+=e[i-s],a+=r[i-s]*e[i-s];n[i]=a/o}return n}function Ee(r,e){const t=Math.floor(e/2),n=V(r,t),i=V(r,e),o=n.map((s,m)=>2*s-i[m]),a=Math.floor(Math.sqrt(e));return V(o,a)}function V(r,e){const t=new Array(r.length);for(let n=e-1;n<r.length;n++){let i=0,o=0;for(let a=0;a<e;a++)i+=r[n-a]*(e-a),o+=e-a;t[n]=i/o}for(let n=0;n<e-1;n++)t[n]=NaN;return t}function je(r,e=1){const t=new Array(r.length).fill(NaN);for(let n=e;n<r.length;n++)t[n]=r[n]-r[n-e];return t}function Me(r,e){const t=new Array(r.length).fill(NaN);for(let n=e;n<r.length;n++)t[n]=r[n]-r[n-e];return t}function Ie(r,e){const t=new Array(r.length).fill(NaN);for(let n=e;n<r.length;n++)t[n]=(r[n]-r[n-e])/r[n-e]*100;return t}function ve(r,e){const t=new Array(r.length).fill(NaN),n=k(r,e);for(let i=e-1;i<r.length;i++){let o=0;for(let a=0;a<e;a++)o+=Math.abs(r[i-a]-n[i]);t[i]=o/e}return t}function we(r,e){const t=new Array(r.length).fill(NaN);for(let n=e-1;n<r.length;n++){let i=0,o=0;for(let s=0;s<e;s++)i+=r[n-s],o+=r[n-s]*r[n-s];const a=i/e;t[n]=o/e-a*a}return t}function Ce(r,e){const t=new Array(r.length).fill(NaN);for(let n=e-1;n<r.length;n++){let i=-1/0;for(let o=0;o<e;o++){const a=r[n-o];isNaN(a)?i=i===-1/0?NaN:i:i=Math.max(i,a)}t[n]=i}return t}function Ne(r,e){const t=new Array(r.length).fill(NaN);for(let n=e-1;n<r.length;n++){let i=1/0;for(let o=0;o<e;o++){const a=r[n-o];isNaN(a)?i=i===1/0?NaN:i:i=Math.min(i,a)}t[n]=i}return t}function Ve(r,e){const t=new Array(r.length).fill(NaN);for(let n=e-1;n<r.length;n++){const i=r.slice(n-e+1,n+1).slice().sort((a,s)=>a-s),o=Math.floor(e/2);t[n]=e%2===0?(i[o-1]+i[o])/2:i[o]}return t}function Se(r,e,t=!0){const n=new Array(r.length).fill(NaN),i=k(r,e);for(let o=e-1;o<r.length;o++){let a=0;for(let m=0;m<e;m++)a+=Math.pow(r[o-m]-i[o],2);const s=t?e:e-1;n[o]=Math.sqrt(a/s)}return n}function Pe(r,e,t){const n=r.length,i=new Array(n).fill(NaN);for(let o=e-1;o<n;o++){let a=0,s=0,m=0,c=0;const y=e;for(let b=0;b<e;b++){const l=b,I=r[o-e+1+b];a+=l,s+=I,m+=l*I,c+=l*l}const h=y*c-a*a;if(h===0){i[o]=NaN;continue}const p=(y*m-a*s)/h,u=(s-p*a)/y+p*(e-1-t);i[o]=u}return i}function Be(r,e,t,n,i){const o=r.length,a=new Array(o).fill(NaN),s=new Array(o).fill(0),m=R(r,e,t,i),c=new Array(o).fill(NaN),y=new Array(o).fill(NaN);for(let l=0;l<o;l++){const I=(r[l]+e[l])/2,w=m[l];isNaN(w)||(c[l]=I+n*w,y[l]=I-n*w)}let h=c[i],p=y[i],u=t[i]<=h?h:p,b=t[i]<=h?-1:1;a[i]=u,s[i]=b;for(let l=i+1;l<o;l++){let I=c[l];I<h||t[l-1]>h?c[l]=I:c[l]=h;let w=y[l];w>p||t[l-1]<p?y[l]=w:y[l]=p,u===h?t[l]>c[l]?(s[l]=1,a[l]=y[l]):(s[l]=-1,a[l]=c[l]):t[l]<y[l]?(s[l]=-1,a[l]=c[l]):(s[l]=1,a[l]=y[l]),h=c[l],p=y[l],u=a[l]}return[a,s]}var Te=Object.defineProperty,ke=(r,e,t)=>e in r?Te(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,A=(r,e,t)=>ke(r,typeof e!="symbol"?e+"":e,t);class _e{constructor(e){this.marketData=e,A(this,"data",{open:[],high:[],low:[],close:[],volume:[],hl2:[],hlc3:[],ohlc4:[]}),A(this,"math"),A(this,"ta"),A(this,"input"),A(this,"request"),A(this,"core"),A(this,"idx",0),A(this,"params",{}),A(this,"const",{}),A(this,"var",{}),A(this,"let",{}),A(this,"result"),A(this,"plots",{}),A(this,"timeframe",""),this.math=new me(this),this.ta=new fe(this),this.input=new se(this),this.request=new de(this);const t=new ae(this);this.core={plotchar:t.plotchar.bind(t),na:t.na.bind(t),color:t.color,plot:t.plot.bind(t),nz:t.nz.bind(t)}}init(e,t,n=0){return e?!Array.isArray(t)||Array.isArray(t[0])?e[0]=Array.isArray(t?.[0])?t[0]:this.precision(t):e[0]=this.precision(t[t.length-this.idx-1+n]):Array.isArray(t)?e=[this.precision(t[t.length-this.idx-1+n])]:e=[this.precision(t)],e}precision(e,t=10){return typeof e!="number"||isNaN(e)?e:Number(e.toFixed(t))}param(e,t,n){return typeof e=="string"||!Array.isArray(e)&&typeof e=="object"?e:(this.params[n]||(this.params[n]=[]),Array.isArray(e)?t?(this.params[n]=e.slice(t),this.params[n]):(this.params[n]=e.slice(0),this.params[n]):(this.params[n][0]=e,this.params[n]))}}const $e="https://api.binance.com/api/v3",De={1:"1m",3:"3m",5:"5m",15:"15m",30:"30m",45:null,60:"1h",120:"2h",180:null,240:"4h","1D":"1d",D:"1d","1W":"1w",W:"1w","1M":"1M",M:"1M"};class Le{async getMarketData(e,t,n,i,o){try{const a=De[t.toUpperCase()];if(!a)return console.error(`Unsupported timeframe: ${t}`),[];let s=`${$e}/klines?symbol=${e}&interval=${a}`;n&&(s+=`&limit=${n}`),i&&(s+=`&startTime=${i}`),o&&(s+=`&endTime=${o}`);const m=await fetch(s);if(!m.ok)throw new Error(`HTTP error! status: ${m.status}`);return(await m.json()).map(c=>({openTime:parseInt(c[0])/1e3,open:parseFloat(c[1]),high:parseFloat(c[2]),low:parseFloat(c[3]),close:parseFloat(c[4]),volume:parseFloat(c[5]),closeTime:parseInt(c[6])/1e3,quoteAssetVolume:parseFloat(c[7]),numberOfTrades:parseInt(c[8]),takerBuyBaseAssetVolume:parseFloat(c[9]),takerBuyQuoteAssetVolume:parseFloat(c[10]),ignore:c[11]}))}catch(a){return console.error("Error in binance.klines:",a),[]}}}const Fe={Binance:new Le};export{_e as Context,Oe as PineTS,Fe as Provider};
18
+ import*as X from"acorn";import*as g from"acorn-walk";import*as Y from"astring";var G=Object.defineProperty,J=(n,e,t)=>e in n?G(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,M=(n,e,t)=>J(n,typeof e!="symbol"?e+"":e,t);class Z{constructor(){M(this,"scopes",[]),M(this,"scopeTypes",[]),M(this,"scopeCounts",new Map),M(this,"contextBoundVars",new Set),M(this,"arrayPatternElements",new Set),M(this,"rootParams",new Set),M(this,"varKinds",new Map),M(this,"loopVars",new Set),M(this,"loopVarNames",new Map),M(this,"paramIdCounter",0),M(this,"cacheIdCounter",0),M(this,"tempVarCounter",0),this.pushScope("glb")}get nextParamIdArg(){return{type:"Identifier",name:`'p${this.paramIdCounter++}'`}}get nextCacheIdArg(){return{type:"Identifier",name:`'cache_${this.cacheIdCounter++}'`}}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,t=!1){this.contextBoundVars.add(e),t&&this.rootParams.add(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,t){this.loopVars.add(e),this.loopVarNames.set(e,t)}getLoopVariableName(e){return this.loopVarNames.get(e)}isLoopVariable(e){return this.loopVars.has(e)}addVariable(e,t){if(this.isContextBound(e))return e;const r=this.scopes[this.scopes.length-1],a=this.scopeTypes[this.scopeTypes.length-1],i=this.scopeCounts.get(a)||1,s=`${a}${i}_${e}`;return r.set(e,s),this.varKinds.set(s,t),s}getVariable(e){if(this.loopVars.has(e)){const t=this.loopVarNames.get(e);if(t)return[t,"let"]}if(this.isContextBound(e))return[e,"let"];for(let t=this.scopes.length-1;t>=0;t--){const r=this.scopes[t];if(r.has(e)){const a=r.get(e),i=this.varKinds.get(a)||"let";return[a,i]}}return[e,"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="$",B={type:"Identifier",name:"undefined"};function C(n,e){if(n.computed&&n.property.type==="Identifier"){if(e.isLoopVariable(n.property.name))return;if(!e.isContextBound(n.property.name)){const[t,r]=e.getVariable(n.property.name);n.property={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:r},computed:!1},property:{type:"Identifier",name:t},computed:!1},n.property={type:"MemberExpression",object:n.property,property:{type:"Literal",value:0},computed:!0}}}if(n.computed&&n.object.type==="Identifier"){if(e.isLoopVariable(n.object.name))return;if(!e.isContextBound(n.object.name)){const[t,r]=e.getVariable(n.object.name);n.object={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:r},computed:!1},property:{type:"Identifier",name:t},computed:!1}}if(n.property.type==="MemberExpression"){const t=n.property;t._indexTransformed||(C(t,e),t._indexTransformed=!0)}}}function N(n,e,t){if(n.object&&n.object.type==="Identifier"&&n.object.name==="Math")return;const r=t.getCurrentScopeType()=="if",a=t.getCurrentScopeType()=="els",i=t.getCurrentScopeType()=="for";!r&&!a&&!i&&n.object&&n.object.type==="Identifier"&&t.isContextBound(n.object.name)&&!t.isRootParam(n.object.name)||n._indexTransformed||(C(n,t),n._indexTransformed=!0)}function O(n,e){n.declarations.forEach(t=>{t.init.name=="na"&&(t.init.name="NaN");const r=t.init&&t.init.type==="MemberExpression"&&t.init.object&&(t.init.object.name==="context"||t.init.object.name===b||t.init.object.name==="context2"),a=t.init&&t.init.type==="MemberExpression"&&t.init.object?.object&&(t.init.object.object.name==="context"||t.init.object.object.name===b||t.init.object.object.name==="context2"),i=t.init&&t.init.type==="ArrowFunctionExpression";if(r){t.id.name&&e.addContextBoundVar(t.id.name),t.id.properties&&t.id.properties.forEach(p=>{p.key.name&&e.addContextBoundVar(p.key.name)}),t.init.object.name=b;return}if(a){t.id.name&&e.addContextBoundVar(t.id.name),t.id.properties&&t.id.properties.forEach(p=>{p.key.name&&e.addContextBoundVar(p.key.name)}),t.init.object.object.name=b;return}i&&t.init.params.forEach(p=>{p.type==="Identifier"&&e.addContextBoundVar(p.name)});const s=e.addVariable(t.id.name,n.kind),o=n.kind;t.init&&!i&&(t.init.type==="CallExpression"&&t.init.callee.type==="MemberExpression"&&t.init.callee.object&&t.init.callee.object.type==="Identifier"&&e.isContextBound(t.init.callee.object.name)?I(t.init,e):g.recursive(t.init,{parent:t.init},{Identifier(p,u){p.parent=u.parent,w(p,e);const f=p.parent&&p.parent.type==="BinaryExpression",m=p.parent&&p.parent.type==="ConditionalExpression";p.type==="Identifier"&&(f||m)&&Object.assign(p,{type:"MemberExpression",object:{type:"Identifier",name:p.name},property:{type:"Literal",value:0},computed:!0})},CallExpression(p,u,f){p.callee.type==="Identifier"&&(p.callee.parent=p),p.arguments.forEach(m=>{m.type==="Identifier"&&(m.parent=p)}),I(p,e),p.arguments.forEach(m=>f(m,{parent:p}))},BinaryExpression(p,u,f){p.left.type==="Identifier"&&(p.left.parent=p),p.right.type==="Identifier"&&(p.right.parent=p),f(p.left,{parent:p}),f(p.right,{parent:p})},MemberExpression(p,u,f){p.object.type==="Identifier"&&(p.object.parent=p),p.property.type==="Identifier"&&(p.property.parent=p),C(p,e),p.object&&f(p.object,{parent:p})}}));const c={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:s},computed:!1},l=e.isArrayPatternElement(t.id.name),y=!l&&t.init&&t.init.type==="MemberExpression"&&t.init.computed&&t.init.property&&(t.init.property.type==="Literal"||t.init.property.type==="MemberExpression");t.init?.property?.type==="MemberExpression"&&(t.init.property._indexTransformed||(C(t.init.property,e),t.init.property._indexTransformed=!0));const h={type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:c,right:t.init?i||l?t.init:{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"init"},computed:!1},arguments:y?[c,t.init.object,t.init.property]:[c,t.init]}:{type:"Identifier",name:"undefined"}}};if(l){h.expression.right.object.property.name+=`?.[0][${t.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]}}i&&(e.pushScope("fn"),g.recursive(t.init.body,e,{BlockStatement(p,u,f){p.body.forEach(m=>f(m,u))},IfStatement(p,u,f){u.pushScope("if"),f(p.consequent,u),p.alternate&&(u.pushScope("els"),f(p.alternate,u),u.popScope()),u.popScope()},VariableDeclaration(p,u){O(p,u)},Identifier(p,u){w(p,u)},AssignmentExpression(p,u){L(p,u)}}),e.popScope()),Object.assign(n,h)})}function w(n,e){if(n.name!==b){if(n.name==="Math"||n.name==="NaN"||n.name==="undefined"||n.name==="Infinity"||n.name==="null"||n.name.startsWith("'")&&n.name.endsWith("'")||n.name.startsWith('"')&&n.name.endsWith('"')||n.name.startsWith("`")&&n.name.endsWith("`")||e.isLoopVariable(n.name)||e.isContextBound(n.name)&&!e.isRootParam(n.name))return;const t=n.parent&&n.parent.type==="MemberExpression"&&n.parent.object===n&&e.isContextBound(n.name),r=n.parent&&n.parent.type==="CallExpression"&&n.parent.callee&&n.parent.callee.type==="MemberExpression"&&n.parent.callee.property.name==="param";n.parent&&n.parent.type==="AssignmentExpression"&&n.parent.left;const a=n.parent&&n.parent.type==="CallExpression"&&n.parent.callee&&n.parent.callee.type==="MemberExpression"&&e.isContextBound(n.parent.callee.object.name),i=n.parent&&n.parent.type==="MemberExpression"&&n.parent.computed,s=n.parent&&n.parent.type==="MemberExpression"&&n.parent.computed&&n.parent.property===n&&n.parent.parent&&n.parent.parent.type==="CallExpression"&&n.parent.parent.callee&&n.parent.parent.callee.type==="MemberExpression"&&e.isContextBound(n.parent.parent.callee.object.name),o=n.parent&&n.parent.type==="CallExpression"&&n.parent.callee===n;if(t||r||a||s||o){if(o)return;const[h,p]=e.getVariable(n.name);Object.assign(n,{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]=e.getVariable(n.name),y={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:l},computed:!1},property:{type:"Identifier",name:c},computed:!1};!(n.parent&&n.parent.type==="MemberExpression"&&n.parent.computed&&n.parent.object===n)&&!i?Object.assign(n,{type:"MemberExpression",object:y,property:{type:"Literal",value:0},computed:!0}):Object.assign(n,y)}}function L(n,e){if(n.left.type==="Identifier"){const[t,r]=e.getVariable(n.left.name),a={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:r},computed:!1},property:{type:"Identifier",name:t},computed:!1};n.left={type:"MemberExpression",object:a,property:{type:"Literal",value:0},computed:!0}}g.recursive(n.right,{parent:n.right,inNamespaceCall:!1},{Identifier(t,r,a){t.name=="na"&&(t.name="NaN"),t.parent=r.parent,w(t,e);const i=t.parent&&t.parent.type==="BinaryExpression",s=t.parent&&t.parent.type==="ConditionalExpression",o=e.isContextBound(t.name)&&!e.isRootParam(t.name),c=t.parent&&t.parent.type==="MemberExpression"&&t.parent.computed&&t.parent.object===t,l=t.parent&&t.parent._isParamCall,y=t.parent&&t.parent.type==="MemberExpression",h=t.name==="NaN";(o||s||i)&&(t.type==="MemberExpression"?C(t,e):t.type==="Identifier"&&!y&&!c&&!l&&!h&&$(t))},MemberExpression(t,r,a){C(t,e),t.object&&a(t.object,{parent:t,inNamespaceCall:r.inNamespaceCall})},CallExpression(t,r,a){const i=t.callee&&t.callee.type==="MemberExpression"&&t.callee.object&&t.callee.object.type==="Identifier"&&e.isContextBound(t.callee.object.name);I(t,e),t.arguments.forEach(s=>a(s,{parent:t,inNamespaceCall:i||r.inNamespaceCall}))}})}function ee(n,e,t=!1){n.params.forEach(r=>{r.type==="Identifier"&&e.addContextBoundVar(r.name,t)})}function W(n,e){const t=e.getCurrentScopeType();if(n.argument){if(n.argument.type==="ArrayExpression")n.argument.elements=n.argument.elements.map(r=>{if(r.type==="Identifier"){if(e.isContextBound(r.name)&&!e.isRootParam(r.name))return{type:"MemberExpression",object:r,property:{type:"Literal",value:0},computed:!0};const[a,i]=e.getVariable(r.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:i},computed:!1},property:{type:"Identifier",name:a},computed:!1},property:{type:"Literal",value:0},computed:!0}}else if(r.type==="MemberExpression")return r.computed&&r.object.type==="Identifier"&&e.isContextBound(r.object.name)&&!e.isRootParam(r.object.name)||N(r,"",e),r;return r}),n.argument={type:"ArrayExpression",elements:[n.argument]};else if(n.argument.type==="BinaryExpression")g.recursive(n.argument,e,{Identifier(r,a){w(r,a),r.type==="Identifier"&&$(r)},MemberExpression(r){N(r,"",e)}});else if(n.argument.type==="ObjectExpression")n.argument.properties=n.argument.properties.map(r=>{if(r.shorthand){const[a,i]=e.getVariable(r.value.name);return{type:"Property",key:{type:"Identifier",name:r.key.name},value:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:i},computed:!1},property:{type:"Identifier",name:a},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return r});else if(n.argument.type==="Identifier"){const[r,a]=e.getVariable(n.argument.name);n.argument={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:a},computed:!1},property:{type:"Identifier",name:r},computed:!1},n.argument={type:"MemberExpression",object:n.argument,property:{type:"Literal",value:0},computed:!0}}t==="fn"&&(n.argument={type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"precision"}},arguments:[n.argument]})}}function V(n,e){if(n.type==="Identifier"){if(n.name==="na")return n.name="NaN",n;if(e.isLoopVariable(n.name))return n;if(e.isRootParam(n.name)){const[a,i]=e.getVariable(n.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:i},computed:!1},property:{type:"Identifier",name:a},computed:!1}}if(e.isContextBound(n.name))return n;const[t,r]=e.getVariable(n.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:r},computed:!1},property:{type:"Identifier",name:t},computed:!1}}return n}function U(n,e,t){const r=k(n.argument,e,t);return{type:"UnaryExpression",operator:n.operator,prefix:n.prefix,argument:r,start:n.start,end:n.end}}function k(n,e,t=""){switch(n.type){case"BinaryExpression":return z(n,e,t);case"MemberExpression":return{type:"MemberExpression",object:n.object.type==="Identifier"?V(n.object,e):n.object,property:n.property,computed:n.computed};case"Identifier":return e.isLoopVariable(n.name)||n.parent&&n.parent.type==="MemberExpression"&&n.parent.property===n?n:{type:"MemberExpression",object:V(n,e),property:{type:"Literal",value:0},computed:!0};case"UnaryExpression":return U(n,e,t)}return n}function z(n,e,t){const r=k(n.left,e,t),a=k(n.right,e,t),i={type:"BinaryExpression",operator:n.operator,left:r,right:a,start:n.start,end:n.end};return g.recursive(i,e,{CallExpression(s,o){s._transformed||I(s,o)},MemberExpression(s){N(s,"",e)}}),i}function te(n,e,t){const r=k(n.left,e,t),a=k(n.right,e,t),i={type:"LogicalExpression",operator:n.operator,left:r,right:a,start:n.start,end:n.end};return g.recursive(i,e,{CallExpression(s,o){s._transformed||I(s,o)}}),i}function re(n,e,t){return g.recursive(n,{parent:n,inNamespaceCall:!1},{Identifier(r,a,i){if(r.name=="NaN")return;if(r.name=="na"){r.name="NaN";return}r.parent=a.parent,w(r,e);const s=r.parent&&r.parent.type==="BinaryExpression";(r.parent&&r.parent.type==="ConditionalExpression"||s)&&(r.type==="MemberExpression"?C(r,e):r.type==="Identifier"&&$(r))},MemberExpression(r,a,i){C(r,e),r.object&&i(r.object,{parent:r,inNamespaceCall:a.inNamespaceCall})},CallExpression(r,a,i){const s=r.callee&&r.callee.type==="MemberExpression"&&r.callee.object&&r.callee.object.type==="Identifier"&&e.isContextBound(r.callee.object.name);I(r,e),r.arguments.forEach(o=>i(o,{parent:r,inNamespaceCall:s||a.inNamespaceCall}))}}),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"}},arguments:[n,B,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function F(n,e,t){switch(n?.type){case"BinaryExpression":n=z(n,t,e);break;case"LogicalExpression":n=te(n,t,e);break;case"ConditionalExpression":return re(n,t,e);case"UnaryExpression":n=U(n,t,e);break}if(n.type==="MemberExpression"&&n.computed&&n.property){const r=n.object.type==="Identifier"&&t.isContextBound(n.object.name)&&!t.isRootParam(n.object.name)?n.object:V(n.object,t),a=n.property.type==="Identifier"&&!t.isContextBound(n.property.name)&&!t.isLoopVariable(n.property.name)?V(n.property,t):n.property;return{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:e},property:{type:"Identifier",name:"param"},computed:!1},arguments:[r,a,t.nextParamIdArg],_transformed:!0,_isParamCall:!0}}if(n.type==="ObjectExpression"&&(n.properties=n.properties.map(r=>{if(r.value.name){const[a,i]=t.getVariable(r.value.name);return{type:"Property",key:{type:"Identifier",name:r.key.name},value:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:i},computed:!1},property:{type:"Identifier",name:a},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return r})),n.type==="Identifier"){if(n.name==="na")return n.name="NaN",n;if(t.isContextBound(n.name)&&!t.isRootParam(n.name))return{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:e},property:{type:"Identifier",name:"param"},computed:!1},arguments:[n,B,t.nextParamIdArg],_transformed:!0,_isParamCall:!0}}return n?.type==="CallExpression"&&I(n,t),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:e},property:{type:"Identifier",name:"param"},computed:!1},arguments:[n.type==="Identifier"?V(n,t):n,B,t.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function I(n,e,t){if(!n._transformed){if(n.callee&&n.callee.type==="MemberExpression"&&n.callee.object&&n.callee.object.type==="Identifier"&&(e.isContextBound(n.callee.object.name)||n.callee.object.name==="math"||n.callee.object.name==="ta")){const r=n.callee.object.name;n.arguments=n.arguments.map(a=>a._isParamCall?a:F(a,r,e)),n._transformed=!0}else n.callee&&n.callee.type==="Identifier"&&(n.arguments=n.arguments.map(r=>r._isParamCall?r:F(r,b,e)),n._transformed=!0);n.arguments.forEach(r=>{g.recursive(r,e,{Identifier(a,i,s){a.parent=i.parent,w(a,e);const o=a.parent&&a.parent.type==="BinaryExpression";(a.parent&&a.parent.type==="ConditionalExpression"||o)&&(a.type==="MemberExpression"?C(a,e):a.type==="Identifier"&&$(a))},CallExpression(a,i,s){a._transformed||I(a,i)},MemberExpression(a,i,s){N(a,"",e),a.object&&s(a.object,{parent:a,inNamespaceCall:i.inNamespaceCall})}})})}}function ne(n,e){n.params.forEach(t=>{t.type==="Identifier"&&e.addContextBoundVar(t.name,!1)}),n.body&&n.body.type==="BlockStatement"&&(e.pushScope("fn"),g.recursive(n.body,e,{BlockStatement(t,r,a){t.body.forEach(i=>a(i,r))},ReturnStatement(t,r){W(t,r)},VariableDeclaration(t,r){O(t,r)},Identifier(t,r){w(t,r)},CallExpression(t,r){I(t,r),t.arguments.forEach(a=>{a.type==="BinaryExpression"&&g.recursive(a,r,{CallExpression(i,s){I(i,s)},MemberExpression(i){N(i,"",r)}})})},MemberExpression(t){N(t,"",e)},AssignmentExpression(t,r){L(t,r)},ForStatement(t,r,a){K(t,r,a)},IfStatement(t,r,a){H(t,r,a)},BinaryExpression(t,r,a){g.recursive(t,r,{CallExpression(i,s){I(i,s)},MemberExpression(i){N(i,"",r)}})}}),e.popScope())}function $(n,e){Object.assign(n,{type:"MemberExpression",object:{type:"Identifier",name:n.name,start:n.start,end:n.end},property:{type:"Literal",value:0},computed:!0,_indexTransformed:!0})}function K(n,e,t){if(n.init&&n.init.type==="VariableDeclaration"){const r=n.init.declarations[0],a=r.id.name;e.addLoopVariable(a,a),n.init={type:"VariableDeclaration",kind:n.init.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:a},init:r.init}]},r.init&&g.recursive(r.init,e,{Identifier(i,s){e.isLoopVariable(i.name)||(e.pushScope("for"),w(i,s),e.popScope())},MemberExpression(i){e.pushScope("for"),N(i,"",e),e.popScope()}})}n.test&&g.recursive(n.test,e,{Identifier(r,a){!e.isLoopVariable(r.name)&&!r.computed&&(e.pushScope("for"),w(r,a),r.type==="Identifier"&&(r.computed=!0,$(r)),e.popScope())},MemberExpression(r){e.pushScope("for"),N(r,"",e),e.popScope()}}),n.update&&g.recursive(n.update,e,{Identifier(r,a){e.isLoopVariable(r.name)||(e.pushScope("for"),w(r,a),e.popScope())}}),e.pushScope("for"),t(n.body,e),e.popScope()}function ae(n,e){g.recursive(n,e,{MemberExpression(t){N(t,"",e)},CallExpression(t,r){I(t,r)},Identifier(t,r){w(t,r);const a=e.getCurrentScopeType()==="if";e.isContextBound(t.name)&&!e.isRootParam(t.name)&&a&&$(t)}})}function H(n,e,t){n.test&&(e.pushScope("if"),ae(n.test,e),e.popScope()),e.pushScope("if"),t(n.consequent,e),e.popScope(),n.alternate&&(e.pushScope("els"),t(n.alternate,e),e.popScope())}function ie(n){g.recursive(n,null,{VariableDeclaration(e,t,r){e.declarations&&e.declarations.length>0&&e.declarations.forEach(a=>{if(a.init&&a.init.type==="ArrowFunctionExpression"&&a.init.start!==0){const i={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,i)}}),e.body&&e.body.body&&e.body.body.forEach(a=>r(a,t))}})}function se(n,e){g.simple(n,{VariableDeclaration(t){t.declarations.forEach(r=>{const a=r.init&&r.init.type==="MemberExpression"&&r.init.object&&(r.init.object.name==="context"||r.init.object.name===b||r.init.object.name==="context2"),i=r.init&&r.init.type==="MemberExpression"&&r.init.object?.object&&(r.init.object.object.name==="context"||r.init.object.object.name===b||r.init.object.object.name==="context2");(a||i)&&(r.id.name&&e.addContextBoundVar(r.id.name),r.id.properties&&r.id.properties.forEach(s=>{s.key.name&&e.addContextBoundVar(s.key.name)}))})}})}function oe(n){let e=typeof n=="function"?n.toString():n;const t=X.parse(e.trim(),{ecmaVersion:"latest",sourceType:"module"});ie(t);const r=new Z;let a;se(t,r),g.simple(t,{FunctionDeclaration(s){ne(s,r)},ArrowFunctionExpression(s){const o=s.start===0;o&&s.params&&s.params.length>0&&(a=s.params[0].name,s.params[0].name=b),ee(s,r,o)},VariableDeclaration(s){s.declarations.forEach(o=>{if(o.id.type==="ArrayPattern"){const c=r.generateTempVar(),l={type:"VariableDeclaration",kind:s.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:c},init:o.init}]};o.id.elements?.forEach(h=>{h.type==="Identifier"&&r.addArrayPatternElement(h.name)});const y=o.id.elements.map((h,p)=>({type:"VariableDeclaration",kind:s.kind,declarations:[{type:"VariableDeclarator",id:h,init:{type:"MemberExpression",object:{type:"Identifier",name:c},property:{type:"Literal",value:p},computed:!0}}]}));Object.assign(s,{type:"BlockStatement",body:[l,...y]})}})},ForStatement(s){}}),g.recursive(t,r,{BlockStatement(s,o,c){s.body.forEach(l=>c(l,o))},ReturnStatement(s,o){W(s,o)},VariableDeclaration(s,o){O(s,o)},Identifier(s,o){w(s,o)},CallExpression(s,o){I(s,o)},MemberExpression(s){N(s,a,r)},AssignmentExpression(s,o){L(s,o)},FunctionDeclaration(s,o){},ForStatement(s,o,c){K(s,o,c)},IfStatement(s,o,c){H(s,o,c)}});const i=Y.generate(t);return new Function("",`return ${i}`)(this)}var pe=Object.defineProperty,ce=(n,e,t)=>e in n?pe(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,A=(n,e,t)=>ce(n,typeof e!="symbol"?e+"":e,t);class le{constructor(e,t,r,a,i,s){this.source=e,this.tickerId=t,this.timeframe=r,this.limit=a,this.sDate=i,this.eDate=s,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(o=>{this.loadMarketData(e,t,r,a,i,s).then(c=>{const l=c.reverse();this._periods=l.length,this.data=l;const y=l.map(d=>d.open),h=l.map(d=>d.close),p=l.map(d=>d.high),u=l.map(d=>d.low),f=l.map(d=>d.volume),m=l.map(d=>(d.high+d.low+d.close)/3),v=l.map(d=>(d.high+d.low)/2),j=l.map(d=>(d.high+d.low+d.open+d.close)/4),P=l.map(d=>d.openTime),S=l.map(d=>d.closeTime);this.open=y,this.close=h,this.high=p,this.low=u,this.volume=f,this.hl2=v,this.hlc3=m,this.ohlc4=j,this.openTime=P,this.closeTime=S,this._ready=!0,o(!0)})})}get periods(){return this._periods}async loadMarketData(e,t,r,a,i,s){return Array.isArray(e)?e:e.getMarketData(t,r,a,i,s)}async ready(){if(this._ready)return!0;if(!this._readyPromise)throw new Error("PineTS is not ready");return this._readyPromise}async run(e,t,r){await this.ready(),t||(t=this._periods);const a=new Ye({marketData:this.data,source:this.source,tickerId:this.tickerId,timeframe:this.timeframe,limit:this.limit,sDate:this.sDate,eDate:this.eDate});a.pineTSCode=e,a.useTACache=r;let i=oe.bind(this)(e);const s=["const","var","let","params"];for(let o=this._periods-t,c=t-1;o<this._periods;o++,c--){a.idx=o,a.data.close=this.close.slice(c),a.data.open=this.open.slice(c),a.data.high=this.high.slice(c),a.data.low=this.low.slice(c),a.data.volume=this.volume.slice(c),a.data.hl2=this.hl2.slice(c),a.data.hlc3=this.hlc3.slice(c),a.data.ohlc4=this.ohlc4.slice(c),a.data.openTime=this.openTime.slice(c),a.data.closeTime=this.closeTime.slice(c);const l=await i(a);if(typeof l=="object"){typeof a.result!="object"&&(a.result={});for(let y in l){a.result[y]===void 0&&(a.result[y]=[]);const h=Array.isArray(l[y])?l[y][0]:l[y];a.result[y].push(h)}}else Array.isArray(a.result)||(a.result=[]),a.result.push(l);for(let y of s)for(let h in a[y])if(Array.isArray(a[y][h])){const p=a[y][h][0];a[y][h].unshift(p)}}return a}}var me=Object.defineProperty,ye=(n,e,t)=>e in n?me(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,he=(n,e,t)=>ye(n,typeof e!="symbol"?e+"":e,t);class ue{constructor(e){this.context=e,he(this,"color",{param:(t,r=0)=>Array.isArray(t)?t[r]:t,rgb:(t,r,a,i)=>i?`rgba(${t}, ${r}, ${a}, ${i})`:`rgb(${t}, ${r}, ${a})`,new:(t,r)=>{if(t&&t.startsWith("#")){const a=t.slice(1),i=parseInt(a.slice(0,2),16),s=parseInt(a.slice(2,4),16),o=parseInt(a.slice(4,6),16);return r?`rgba(${i}, ${s}, ${o}, ${r})`:`rgb(${i}, ${s}, ${o})`}return r?`rgba(${t}, ${r})`:t},white:"white",lime:"lime",green:"green",red:"red",maroon:"maroon",black:"black",gray:"gray",blue:"blue"})}extractPlotOptions(e){const t={};for(let r in e)Array.isArray(e[r])?t[r]=e[r][0]:t[r]=e[r];return t}indicator(e,t,r){}plotchar(e,t,r){this.context.plots[t]||(this.context.plots[t]={data:[],options:this.extractPlotOptions(r),title:t}),this.context.plots[t].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:e[0],options:{...this.extractPlotOptions(r),style:"char"}})}plot(e,t,r){this.context.plots[t]||(this.context.plots[t]={data:[],options:this.extractPlotOptions(r),title:t}),this.context.plots[t].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:e[0],options:this.extractPlotOptions(r)})}na(e){return Array.isArray(e)?isNaN(e[0]):isNaN(e)}nz(e,t=0){const r=Array.isArray(e)?e[0]:e,a=Array.isArray(e)?t[0]:t;return isNaN(r)?a:r}}class de{constructor(e){this.context=e}param(e,t=0){return Array.isArray(e)?[e[t]]:[e]}any(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}int(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}float(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}bool(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}string(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}timeframe(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}time(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}price(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}session(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}source(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}symbol(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}text_area(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}enum(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}color(e,{title:t,group:r}={}){return Array.isArray(e)?e[0]:e}}var fe=Object.defineProperty,xe=(n,e,t)=>e in n?fe(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,be=(n,e,t)=>xe(n,typeof e!="symbol"?e+"":e,t);class ge{constructor(e){this.context=e,be(this,"_cache",{})}param(e,t,r){return this.context.params[r]||(this.context.params[r]=[]),Array.isArray(e)?t?(this.context.params[r]=e.slice(t),this.context.params[r].length=e.length,this.context.params[r]):(this.context.params[r]=e.slice(0),this.context.params[r]):(this.context.params[r][0]=e,this.context.params[r])}abs(e){return Math.abs(e[0])}pow(e,t){return Math.pow(e[0],t[0])}sqrt(e){return Math.sqrt(e[0])}log(e){return Math.log(e[0])}ln(e){return Math.log(e[0])}exp(e){return Math.exp(e[0])}floor(e){return Math.floor(e[0])}ceil(e){return Math.ceil(e[0])}round(e){return Math.round(e[0])}random(){return Math.random()}max(...e){const t=e.map(r=>Array.isArray(r)?r[0]:r);return Math.max(...t)}min(...e){const t=e.map(r=>Array.isArray(r)?r[0]:r);return Math.min(...t)}sum(e,t){const r=Array.isArray(t)?t[0]:t;return Array.isArray(e)?e.slice(0,r).reduce((a,i)=>a+i,0):e}sin(e){return Math.sin(e[0])}cos(e){return Math.cos(e[0])}tan(e){return Math.tan(e[0])}acos(e){return Math.acos(e[0])}asin(e){return Math.asin(e[0])}atan(e){return Math.atan(e[0])}avg(...e){const t=e.map(r=>Array.isArray(r)?r[0]:r);return t.reduce((r,a)=>{const i=Array.isArray(r)?r[0]:r,s=Array.isArray(a)?a[0]:a;return i+s},0)/t.length}}var Ae=Object.defineProperty,Ee=(n,e,t)=>e in n?Ae(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,ve=(n,e,t)=>Ee(n,typeof e!="symbol"?e+"":e,t);const R=["1","3","5","15","30","45","60","120","180","240","D","W","M"];class je{constructor(e){this.context=e,ve(this,"_cache",{})}param(e,t,r){return this.context.params[r]||(this.context.params[r]=[]),Array.isArray(e)?(t?this.context.params[r]=e.slice(t):this.context.params[r]=e.slice(0),[e[t],r]):(this.context.params[r][0]=e,[e,r])}async security(e,t,r,a=!1,i=!1,s=!1,o=null,c=null){const l=e[0],y=t[0],h=r[0],p=r[1],u=R.indexOf(this.context.timeframe),f=R.indexOf(y);if(u==-1||f==-1)throw new Error("Invalid timeframe");if(u>f)throw new Error("Only higher timeframes are supported for now");if(u===f)return h;const m=this.context.data.openTime[0],v=this.context.data.closeTime[0];if(this.context.cache[p]){const S=this.context.cache[p],d=this._findSecContextIdx(m,v,S.data.openTime,S.data.closeTime,i);return d==-1?NaN:S.params[p][d]}const j=await new le(this.context.source,l,y,this.context.limit||1e3,this.context.sDate,this.context.eDate).run(this.context.pineTSCode);this.context.cache[p]=j;const P=this._findSecContextIdx(m,v,j.data.openTime,j.data.closeTime,i);return P==-1?NaN:j.params[p][P]}_findSecContextIdx(e,t,r,a,i=!1){for(let s=0;s<r.length;s++)if(r[s]<=e&&t<=a[s])return s+(i?1:2);return-1}}class Ie{constructor(e){this.context=e}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(e,t,r){return this.context.params[r]||(this.context.params[r]=[]),Array.isArray(e)?t?(this.context.params[r]=e.slice(t),this.context.params[r].length=e.length,this.context.params[r]):(this.context.params[r]=e.slice(0),this.context.params[r]):(this.context.params[r][0]=e,this.context.params[r])}ema(e,t){const r=Array.isArray(t)?t[0]:t,a=Me(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}sma(e,t,r){const a=Array.isArray(t)?t[0]:t,i=e.slice(0).reverse();if(this.context.useTACache&&r){this.context.cache[r]||(this.context.cache[r]={});const c=this.context.cache[r];if(c){const l=Ce(i,a,c),y=this.context.idx;return this.context.precision(l[y])}}const s=D(i,a),o=this.context.idx;return this.context.precision(s[o])}vwma(e,t){const r=Array.isArray(t)?t[0]:t,a=this.context.data.volume,i=$e(e.slice(0).reverse(),a.slice(0).reverse(),r),s=this.context.idx;return this.context.precision(i[s])}wma(e,t){const r=Array.isArray(t)?t[0]:t,a=T(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}hma(e,t){const r=Array.isArray(t)?t[0]:t,a=Se(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}rma(e,t){const r=Array.isArray(t)?t[0]:t,a=Ne(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}change(e,t=1){const r=Array.isArray(t)?t[0]:t,a=Ve(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}rsi(e,t){const r=Array.isArray(t)?t[0]:t,a=we(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}atr(e){const t=Array.isArray(e)?e[0]:e,r=this.context.data.high.slice().reverse(),a=this.context.data.low.slice().reverse(),i=this.context.data.close.slice().reverse(),s=Q(r,a,i,t),o=this.context.idx;return this.context.precision(s[o])}mom(e,t){const r=Array.isArray(t)?t[0]:t,a=ke(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}roc(e,t){const r=Array.isArray(t)?t[0]:t,a=Pe(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}dev(e,t){const r=Array.isArray(t)?t[0]:t,a=Te(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}variance(e,t){const r=Array.isArray(t)?t[0]:t,a=De(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}highest(e,t){const r=Array.isArray(t)?t[0]:t,a=Be(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}lowest(e,t){const r=Array.isArray(t)?t[0]:t,a=_e(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}median(e,t){const r=Array.isArray(t)?t[0]:t,a=Oe(e.slice(0).reverse(),r),i=this.context.idx;return this.context.precision(a[i])}stdev(e,t,r=!0){const a=Array.isArray(t)?t[0]:t,i=Array.isArray(r)?r[0]:r,s=Le(e.slice(0).reverse(),a,i),o=this.context.idx;return this.context.precision(s[o])}linreg(e,t,r){const a=Array.isArray(t)?t[0]:t,i=Array.isArray(r)?r[0]:r,s=Fe(e.slice(0).reverse(),a,i),o=this.context.idx;return this.context.precision(s[o])}supertrend(e,t){const r=Array.isArray(e)?e[0]:e,a=Array.isArray(t)?t[0]:t,i=this.context.data.high.slice().reverse(),s=this.context.data.low.slice().reverse(),o=this.context.data.close.slice().reverse(),[c,l]=Re(i,s,o,r,a),y=this.context.idx;return[[this.context.precision(c[y]),l[y]]]}crossover(e,t){const r=Array.isArray(e)?e[0]:e,a=Array.isArray(t)?t[0]:t,i=Array.isArray(e)?e[1]:this.context.data.series[e][1],s=Array.isArray(t)?t[1]:this.context.data.series[t][1];return i<s&&r>a}crossunder(e,t){const r=Array.isArray(e)?e[0]:e,a=Array.isArray(t)?t[0]:t,i=Array.isArray(e)?e[1]:this.context.data.series[e][1],s=Array.isArray(t)?t[1]:this.context.data.series[t][1];return i>s&&r<a}pivothigh(e,t,r){r==null&&(r=t,t=e,e=this.context.data.high);const a=Array.isArray(t)?t[0]:t,i=Array.isArray(r)?r[0]:r,s=qe(e.slice(0).reverse(),a,i),o=this.context.idx;return this.context.precision(s[o])}pivotlow(e,t,r){r==null&&(r=t,t=e,e=this.context.data.low);const a=Array.isArray(t)?t[0]:t,i=Array.isArray(r)?r[0]:r,s=We(e.slice(0).reverse(),a,i),o=this.context.idx;return this.context.precision(s[o])}}function Q(n,e,t,r){const a=new Array(n.length);a[0]=n[0]-e[0];for(let o=1;o<n.length;o++){const c=n[o]-e[o],l=Math.abs(n[o]-t[o-1]),y=Math.abs(e[o]-t[o-1]);a[o]=Math.max(c,l,y)}const i=new Array(n.length).fill(NaN);let s=0;for(let o=0;o<r;o++)s+=a[o];i[r-1]=s/r;for(let o=r;o<a.length;o++)i[o]=(i[o-1]*(r-1)+a[o])/r;return i}function Me(n,e){const t=new Array(n.length).fill(NaN),r=2/(e+1);let a=0;for(let i=0;i<e;i++)a+=n[i]||0;t[e-1]=a/e;for(let i=e;i<n.length;i++)t[i]=n[i]*r+t[i-1]*(1-r);return t}function we(n,e){const t=new Array(n.length).fill(NaN),r=new Array(n.length).fill(0),a=new Array(n.length).fill(0);for(let o=1;o<n.length;o++){const c=n[o]-n[o-1];r[o]=c>0?c:0,a[o]=c<0?-c:0}let i=0,s=0;for(let o=1;o<=e;o++)i+=r[o],s+=a[o];i/=e,s/=e,t[e]=s===0?100:100-100/(1+i/s);for(let o=e+1;o<n.length;o++)i=(i*(e-1)+r[o])/e,s=(s*(e-1)+a[o])/e,t[o]=s===0?100:100-100/(1+i/s);return t}function Ne(n,e){const t=new Array(n.length).fill(NaN),r=1/e;let a=0;for(let i=0;i<e;i++)a+=n[i]||0;t[e-1]=a/e;for(let i=e;i<n.length;i++){const s=n[i]||0;t[i]=s*r+t[i-1]*(1-r)}return t}function Ce(n,e,t){const r=t.previousResult||new Array(n.length).fill(NaN),a=t.lastProcessedIndex||-1;let i=t.previousSum||0;if(a===-1||n.length!==a+1){i=0;for(let s=0;s<e;s++)i+=n[s]||0;r[e-1]=i/e;for(let s=0;s<e-1;s++)r[s]=NaN;for(let s=e;s<n.length;s++)i=i-(n[s-e]||0)+(n[s]||0),r[s]=i/e}else if(n.length===a+2){const s=n.length-1;i=i-(n[s-e]||0)+(n[s]||0),r[s]=i/e}else return D(n,e);return t.previousSum=i,t.lastProcessedIndex=n.length-1,t.previousResult=r,r}function D(n,e){const t=new Array(n.length).fill(NaN);for(let r=e-1;r<n.length;r++){let a=0;for(let i=0;i<e;i++)a+=n[r-i]||0;t[r]=a/e}return t}function $e(n,e,t){const r=new Array(n.length).fill(NaN);for(let a=t-1;a<n.length;a++){let i=0,s=0;for(let o=0;o<t;o++)i+=e[a-o],s+=n[a-o]*e[a-o];r[a]=s/i}return r}function Se(n,e){const t=Math.floor(e/2),r=T(n,t),a=T(n,e),i=r.map((o,c)=>2*o-a[c]),s=Math.floor(Math.sqrt(e));return T(i,s)}function T(n,e){const t=new Array(n.length);for(let r=e-1;r<n.length;r++){let a=0,i=0;for(let s=0;s<e;s++)a+=n[r-s]*(e-s),i+=e-s;t[r]=a/i}for(let r=0;r<e-1;r++)t[r]=NaN;return t}function Ve(n,e=1){const t=new Array(n.length).fill(NaN);for(let r=e;r<n.length;r++)t[r]=n[r]-n[r-e];return t}function ke(n,e){const t=new Array(n.length).fill(NaN);for(let r=e;r<n.length;r++)t[r]=n[r]-n[r-e];return t}function Pe(n,e){const t=new Array(n.length).fill(NaN);for(let r=e;r<n.length;r++)t[r]=(n[r]-n[r-e])/n[r-e]*100;return t}function Te(n,e){const t=new Array(n.length).fill(NaN),r=D(n,e);for(let a=e-1;a<n.length;a++){let i=0;for(let s=0;s<e;s++)i+=Math.abs(n[a-s]-r[a]);t[a]=i/e}return t}function De(n,e){const t=new Array(n.length).fill(NaN);for(let r=e-1;r<n.length;r++){let a=0,i=0;for(let o=0;o<e;o++)a+=n[r-o],i+=n[r-o]*n[r-o];const s=a/e;t[r]=i/e-s*s}return t}function Be(n,e){const t=new Array(n.length).fill(NaN);for(let r=e-1;r<n.length;r++){let a=-1/0;for(let i=0;i<e;i++){const s=n[r-i];isNaN(s)?a=a===-1/0?NaN:a:a=Math.max(a,s)}t[r]=a}return t}function _e(n,e){const t=new Array(n.length).fill(NaN);for(let r=e-1;r<n.length;r++){let a=1/0;for(let i=0;i<e;i++){const s=n[r-i];isNaN(s)||s===void 0?a=a===1/0?NaN:a:a=Math.min(a,s)}t[r]=a}return t}function Oe(n,e){const t=new Array(n.length).fill(NaN);for(let r=e-1;r<n.length;r++){const a=n.slice(r-e+1,r+1).slice().sort((s,o)=>s-o),i=Math.floor(e/2);t[r]=e%2===0?(a[i-1]+a[i])/2:a[i]}return t}function Le(n,e,t=!0){const r=new Array(n.length).fill(NaN),a=D(n,e);for(let i=e-1;i<n.length;i++){let s=0;for(let c=0;c<e;c++)s+=Math.pow(n[i-c]-a[i],2);const o=t?e:e-1;r[i]=Math.sqrt(s/o)}return r}function Fe(n,e,t){const r=n.length,a=new Array(r).fill(NaN);for(let i=e-1;i<r;i++){let s=0,o=0,c=0,l=0;const y=e;for(let f=0;f<e;f++){const m=f,v=n[i-e+1+f];s+=m,o+=v,c+=m*v,l+=m*m}const h=y*l-s*s;if(h===0){a[i]=NaN;continue}const p=(y*c-s*o)/h,u=(o-p*s)/y+p*(e-1-t);a[i]=u}return a}function Re(n,e,t,r,a){const i=n.length,s=new Array(i).fill(NaN),o=new Array(i).fill(0),c=Q(n,e,t,a),l=new Array(i).fill(NaN),y=new Array(i).fill(NaN);for(let m=0;m<i;m++){const v=(n[m]+e[m])/2,j=c[m];isNaN(j)||(l[m]=v+r*j,y[m]=v-r*j)}let h=l[a],p=y[a],u=t[a]<=h?h:p,f=t[a]<=h?-1:1;s[a]=u,o[a]=f;for(let m=a+1;m<i;m++){let v=l[m];v<h||t[m-1]>h?l[m]=v:l[m]=h;let j=y[m];j>p||t[m-1]<p?y[m]=j:y[m]=p,u===h?t[m]>l[m]?(o[m]=1,s[m]=y[m]):(o[m]=-1,s[m]=l[m]):t[m]<y[m]?(o[m]=-1,s[m]=l[m]):(o[m]=1,s[m]=y[m]),h=l[m],p=y[m],u=s[m]}return[s,o]}function qe(n,e,t){const r=new Array(n.length).fill(NaN);for(let a=e+t;a<n.length;a++){const i=n[a-t];let s=!0;for(let o=1;o<=e;o++)if(n[a-t-o]>=i){s=!1;break}if(s){for(let o=1;o<=t;o++)if(n[a-t+o]>=i){s=!1;break}}s&&(r[a]=i)}return r}function We(n,e,t){const r=new Array(n.length).fill(NaN);for(let a=e+t;a<n.length;a++){const i=n[a-t];let s=!0;for(let o=1;o<=e;o++)if(n[a-t-o]<=i){s=!1;break}if(s){for(let o=1;o<=t;o++)if(n[a-t+o]<=i){s=!1;break}}s&&(r[a]=i)}return r}var Ue=Object.defineProperty,ze=(n,e,t)=>e in n?Ue(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,Ke=(n,e,t)=>ze(n,typeof e!="symbol"?e+"":e,t);class E{constructor(e){this.array=e}}class He{constructor(e){this.context=e,Ke(this,"_cache",{})}param(e,t=0){return Array.isArray(e)?e[t]:e}get(e,t){return e.array[t]}set(e,t,r){e.array[t]=r}push(e,t){e.array.push(t)}sum(e){return e.array.reduce((t,r)=>t+(isNaN(r)?0:r),0)}avg(e){return this.sum(e)/e.array.length}min(e,t=0){return[...e.array].sort((r,a)=>r-a)[t]??this.context.NA}max(e,t=0){return[...e.array].sort((r,a)=>a-r)[t]??this.context.NA}size(e){return e.array.length}new_bool(e,t=!1){return new E(Array(e).fill(t))}new_float(e,t=NaN){return new E(Array(e).fill(t))}new_int(e,t=0){return new E(Array(e).fill(Math.round(t)))}new_string(e,t=""){return new E(Array(e).fill(t))}new(e,t){return new E(Array(e).fill(t))}slice(e,t,r){const a=r!==void 0?r+1:void 0;return new E(e.array.slice(t,a))}reverse(e){e.array.reverse()}includes(e,t){return e.array.includes(t)}indexof(e,t){return e.array.indexOf(t)}lastindexof(e,t){return e.array.lastIndexOf(t)}stdev(e,t=!0){const r=this.avg(e),a=e.array.map(s=>Math.pow(s-r,2)),i=t?e.array.length:e.array.length-1;return Math.sqrt(this.sum(new E(a))/i)}variance(e,t=!0){const r=this.avg(e),a=e.array.map(s=>Math.pow(s-r,2)),i=t?e.array.length:e.array.length-1;return this.sum(new E(a))/i}covariance(e,t,r=!0){if(e.array.length!==t.array.length||e.array.length<2)return NaN;const a=r?e.array.length:e.array.length-1,i=this.avg(e),s=this.avg(t);let o=0;for(let c=0;c<e.array.length;c++)o+=(e.array[c]-i)*(t.array[c]-s);return o/a}first(e){return e.array.length>0?e.array[0]:this.context.NA}last(e){return e.array.length>0?e.array[e.array.length-1]:this.context.NA}clear(e){e.array.length=0}join(e,t=","){return e.array.join(t)}abs(e){return new E(e.array.map(t=>Math.abs(t)))}concat(e,t){return e.array.push(...t.array),e}copy(e){return new E([...e.array])}every(e,t){return e.array.every(t)}fill(e,t,r=0,a){const i=e.array.length,s=a!==void 0?Math.min(a,i):i;for(let o=r;o<s;o++)e.array[o]=t}from(e){return new E([...e])}insert(e,t,r){e.array.splice(t,0,r)}pop(e){return e.array.pop()}range(e){return this.max(e)-this.min(e)}remove(e,t){return t>=0&&t<e.array.length?e.array.splice(t,1)[0]:this.context.NA}shift(e){return e.array.shift()}sort(e,t="asc"){e.array.sort((r,a)=>t==="asc"?r-a:a-r)}sort_indices(e,t){const r=e.array.map((a,i)=>i);return r.sort((a,i)=>{const s=e.array[a],o=e.array[i];return t?t(s,o):s-o}),new E(r)}standardize(e){const t=this.avg(e),r=this.stdev(e);return r===0?new E(e.array.map(()=>0)):new E(e.array.map(a=>(a-t)/r))}unshift(e,t){e.array.unshift(t)}some(e,t){return e.array.some(t)}}var Qe=Object.defineProperty,Xe=(n,e,t)=>e in n?Qe(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,x=(n,e,t)=>Xe(n,typeof e!="symbol"?e+"":e,t);class Ye{constructor({marketData:e,source:t,tickerId:r,timeframe:a,limit:i,sDate:s,eDate:o}){x(this,"data",{open:[],high:[],low:[],close:[],volume:[],hl2:[],hlc3:[],ohlc4:[]}),x(this,"cache",{}),x(this,"useTACache",!1),x(this,"NA",NaN),x(this,"math"),x(this,"ta"),x(this,"input"),x(this,"request"),x(this,"array"),x(this,"core"),x(this,"lang"),x(this,"idx",0),x(this,"params",{}),x(this,"const",{}),x(this,"var",{}),x(this,"let",{}),x(this,"result"),x(this,"plots",{}),x(this,"marketData"),x(this,"source"),x(this,"tickerId"),x(this,"timeframe",""),x(this,"limit"),x(this,"sDate"),x(this,"eDate"),x(this,"pineTSCode"),this.marketData=e,this.source=t,this.tickerId=r,this.timeframe=a,this.limit=i,this.sDate=s,this.eDate=o,this.math=new ge(this),this.ta=new Ie(this),this.input=new de(this),this.request=new je(this),this.array=new He(this);const c=new ue(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,t,r=0){return e?!Array.isArray(t)||Array.isArray(t[0])?e[0]=Array.isArray(t?.[0])?t[0]:this.precision(t):e[0]=this.precision(t[t.length-this.idx-1+r]):Array.isArray(t)?e=[this.precision(t[t.length-this.idx-1+r])]:e=[this.precision(t)],e}precision(e,t=10){return typeof e!="number"||isNaN(e)?e:Number(e.toFixed(t))}param(e,t,r){return typeof e=="string"||!Array.isArray(e)&&typeof e=="object"?e:(this.params[r]||(this.params[r]=[]),Array.isArray(e)?t?(this.params[r]=e.slice(t),this.params[r].length=e.length,this.params[r]):(this.params[r]=e.slice(0),this.params[r]):(this.params[r][0]=e,this.params[r]))}}var Ge=Object.defineProperty,Je=(n,e,t)=>e in n?Ge(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,_=(n,e,t)=>Je(n,typeof e!="symbol"?e+"":e,t);const Ze="https://api.binance.com/api/v3",q={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 et{constructor(e=5*60*1e3){_(this,"cache"),_(this,"cacheDuration"),this.cache=new Map,this.cacheDuration=e}generateKey(e){return Object.entries(e).filter(([t,r])=>r!==void 0).map(([t,r])=>`${t}:${r}`).join("|")}get(e){const t=this.generateKey(e),r=this.cache.get(t);return r?Date.now()-r.timestamp>this.cacheDuration?(this.cache.delete(t),null):r.data:null}set(e,t){const r=this.generateKey(e);this.cache.set(r,{data:t,timestamp:Date.now()})}clear(){this.cache.clear()}cleanup(){const e=Date.now();for(const[t,r]of this.cache.entries())e-r.timestamp>this.cacheDuration&&this.cache.delete(t)}}class tt{constructor(){_(this,"cacheManager"),this.cacheManager=new et(5*60*1e3)}async getMarketDataInterval(e,t,r,a){try{const i=q[t.toUpperCase()];if(!i)return console.error(`Unsupported timeframe: ${t}`),[];const s={"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 o=[],c=r;const l=a,y=s[i];if(!y)return console.error(`Duration not defined for interval: ${i}`),[];for(;c<l;){const h=Math.min(c+1e3*y,l),p=await this.getMarketData(e,t,1e3,c,h);if(p.length===0||(o=o.concat(p),c=p[p.length-1].closeTime+1,p.length<1e3))break}return o}catch(i){return console.error("Error in getMarketDataInterval:",i),[]}}async getMarketData(e,t,r,a,i){try{const s={tickerId:e,timeframe:t,limit:r,sDate:a,eDate:i},o=this.cacheManager.get(s);if(o)return console.log("cache hit",e,t,r,a,i),o;const c=q[t.toUpperCase()];if(!c)return console.error(`Unsupported timeframe: ${t}`),[];let l=`${Ze}/klines?symbol=${e}&interval=${c}`;if(!r&&a&&i)return this.getMarketDataInterval(e,t,a,i);r&&(l+=`&limit=${r}`),a&&(l+=`&startTime=${a}`),i&&(l+=`&endTime=${i}`);const y=await fetch(l);if(!y.ok)throw new Error(`HTTP error! status: ${y.status}`);const h=(await y.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(s,h),h}catch(s){return console.error("Error in binance.klines:",s),[]}}}const rt={Binance:new tt};export{Ye as Context,le as PineTS,rt as Provider};
@@ -2,14 +2,20 @@ import { Input } from '@pinets/namespaces/Input';
2
2
  import PineMath from '@pinets/namespaces/PineMath';
3
3
  import { PineRequest } from '@pinets/namespaces/PineRequest';
4
4
  import TechnicalAnalysis from '@pinets/namespaces/TechnicalAnalysis';
5
+ import { PineArray } from './namespaces/PineArray';
6
+ import { IProvider } from './marketData/IProvider';
5
7
  export declare class Context {
6
- marketData: any;
7
8
  data: any;
9
+ cache: any;
10
+ useTACache: boolean;
11
+ NA: any;
8
12
  math: PineMath;
9
13
  ta: TechnicalAnalysis;
10
14
  input: Input;
11
15
  request: PineRequest;
16
+ array: PineArray;
12
17
  core: any;
18
+ lang: any;
13
19
  idx: number;
14
20
  params: any;
15
21
  const: any;
@@ -17,8 +23,23 @@ export declare class Context {
17
23
  let: any;
18
24
  result: any;
19
25
  plots: any;
26
+ marketData: any;
27
+ source: IProvider | any[];
28
+ tickerId: string;
20
29
  timeframe: string;
21
- constructor(marketData: any);
30
+ limit: number;
31
+ sDate: number;
32
+ eDate: number;
33
+ pineTSCode: Function | String;
34
+ constructor({ marketData, source, tickerId, timeframe, limit, sDate, eDate, }: {
35
+ marketData: any;
36
+ source: IProvider | any[];
37
+ tickerId?: string;
38
+ timeframe?: string;
39
+ limit?: number;
40
+ sDate?: number;
41
+ eDate?: number;
42
+ });
22
43
  /**
23
44
  * this function is used to initialize the target variable with the source array
24
45
  * this array will represent a time series and its values will be shifted at runtime in order to mimic Pine script behavior
@@ -33,6 +54,7 @@ export declare class Context {
33
54
  * by default it is set to 10 decimals which is the same as pine script
34
55
  * @param n - the number to be precision
35
56
  * @param decimals - the number of decimals to precision to
57
+
36
58
  * @returns the precision number
37
59
  */
38
60
  precision(n: number, decimals?: number): number;
@@ -28,6 +28,6 @@ export declare class PineTS {
28
28
  constructor(source: IProvider | any[], tickerId?: string, timeframe?: string, limit?: number, sDate?: number, eDate?: number);
29
29
  private loadMarketData;
30
30
  ready(): Promise<any>;
31
- run(fn: Function | String, n?: number): Promise<Context>;
31
+ run(pineTSCode: Function | String, n?: number, useTACache?: boolean): Promise<Context>;
32
32
  }
33
33
  export default PineTS;
@@ -1,4 +1,7 @@
1
1
  import { IProvider } from '@pinets/marketData/IProvider';
2
2
  export declare class BinanceProvider implements IProvider {
3
+ private cacheManager;
4
+ constructor();
5
+ getMarketDataInterval(tickerId: string, timeframe: string, sDate: number, eDate: number): Promise<any>;
3
6
  getMarketData(tickerId: string, timeframe: string, limit?: number, sDate?: number, eDate?: number): Promise<any>;
4
7
  }
@@ -1,6 +1,9 @@
1
1
  export declare class Core {
2
2
  private context;
3
3
  color: {
4
+ param: (source: any, index?: number) => any;
5
+ rgb: (r: number, g: number, b: number, a?: number) => string;
6
+ new: (color: string, a?: number) => string;
4
7
  white: string;
5
8
  lime: string;
6
9
  green: string;
@@ -0,0 +1,62 @@
1
+ declare class PineArrayObject {
2
+ array: any;
3
+ constructor(array: any);
4
+ }
5
+ /**
6
+ * This class implements an array port of PineScript's array functions.
7
+ */
8
+ export declare class PineArray {
9
+ private context;
10
+ private _cache;
11
+ constructor(context: any);
12
+ param(source: any, index?: number): any;
13
+ /**
14
+ * This function simulates PineScript's array.get() function
15
+ * @param id - the array object to get the value from
16
+ * @param index - the index of the value to get
17
+ * @returns the value at the given index
18
+ */
19
+ get(id: PineArrayObject, index: number): any;
20
+ set(id: PineArrayObject, index: number, value: any): void;
21
+ push(id: PineArrayObject, value: any): void;
22
+ sum(id: PineArrayObject): number;
23
+ avg(id: PineArrayObject): number;
24
+ min(id: PineArrayObject, nth?: number): number;
25
+ max(id: PineArrayObject, nth?: number): number;
26
+ size(id: PineArrayObject): number;
27
+ new_bool(size: number, initial_value?: boolean): PineArrayObject;
28
+ new_float(size: number, initial_value?: number): PineArrayObject;
29
+ new_int(size: number, initial_value?: number): PineArrayObject;
30
+ new_string(size: number, initial_value?: string): PineArrayObject;
31
+ new<T>(size: number, initial_value: T): PineArrayObject;
32
+ slice(id: PineArrayObject, start: number, end?: number): PineArrayObject;
33
+ reverse(id: PineArrayObject): void;
34
+ includes(id: PineArrayObject, value: any): boolean;
35
+ indexof(id: PineArrayObject, value: any): number;
36
+ lastindexof(id: PineArrayObject, value: any): number;
37
+ stdev(id: PineArrayObject, biased?: boolean): number;
38
+ variance(id: PineArrayObject, biased?: boolean): number;
39
+ covariance(arr1: PineArrayObject, arr2: PineArrayObject, biased?: boolean): number;
40
+ first(id: PineArrayObject): any;
41
+ last(id: PineArrayObject): any;
42
+ clear(id: PineArrayObject): void;
43
+ join(id: PineArrayObject, separator?: string): string;
44
+ /** Array Manipulation Functions */
45
+ abs(id: PineArrayObject): PineArrayObject;
46
+ concat(id: PineArrayObject, other: PineArrayObject): PineArrayObject;
47
+ copy(id: PineArrayObject): PineArrayObject;
48
+ every(id: PineArrayObject, callback: (val: any) => boolean): boolean;
49
+ fill(id: PineArrayObject, value: any, start?: number, end?: number): void;
50
+ from(source: any[]): PineArrayObject;
51
+ insert(id: PineArrayObject, index: number, value: any): void;
52
+ pop(id: PineArrayObject): any;
53
+ range(id: PineArrayObject): number;
54
+ remove(id: PineArrayObject, index: number): any;
55
+ shift(id: PineArrayObject): any;
56
+ sort(id: PineArrayObject, order?: 'asc' | 'desc'): void;
57
+ sort_indices(id: PineArrayObject, comparator?: (a: any, b: any) => number): PineArrayObject;
58
+ standardize(id: PineArrayObject): PineArrayObject;
59
+ unshift(id: PineArrayObject, value: any): void;
60
+ some(id: PineArrayObject, callback: (val: any) => boolean): boolean;
61
+ }
62
+ export {};
File without changes
@@ -2,25 +2,26 @@ export declare class PineMath {
2
2
  private context;
3
3
  private _cache;
4
4
  constructor(context: any);
5
- param(source: any, index?: number): any;
6
- abs(n: number): number;
7
- pow(a: number, b: number): number;
8
- sqrt(a: number): number;
9
- log(a: number): number;
10
- ln(a: number): number;
11
- exp(a: number): number;
12
- floor(a: number): number;
13
- ceil(a: number): number;
14
- round(a: number): number;
5
+ param(source: any, index: any, name?: string): any;
6
+ abs(source: number[]): number;
7
+ pow(source: number[], power: number[]): number;
8
+ sqrt(source: number[]): number;
9
+ log(source: number[]): number;
10
+ ln(source: number[]): number;
11
+ exp(source: number[]): number;
12
+ floor(source: number[]): number;
13
+ ceil(source: number[]): number;
14
+ round(source: number[]): number;
15
15
  random(): number;
16
- max(...args: any[]): number;
17
- min(...args: any[]): number;
18
- sin(a: number): number;
19
- cos(a: number): number;
20
- tan(a: number): number;
21
- asin(a: number): number;
22
- acos(a: number): number;
23
- atan(a: number): number;
24
- avg(...args: any[]): number;
16
+ max(...source: number[]): number;
17
+ min(...source: number[]): number;
18
+ sum(source: number[], length: number): number;
19
+ sin(source: number[]): number;
20
+ cos(source: number[]): number;
21
+ tan(source: number[]): number;
22
+ acos(source: number[]): number;
23
+ asin(source: number[]): number;
24
+ atan(source: number[]): number;
25
+ avg(...sources: number[][]): number;
25
26
  }
26
27
  export default PineMath;
@@ -3,5 +3,6 @@ export declare class PineRequest {
3
3
  private _cache;
4
4
  constructor(context: any);
5
5
  param(source: any, index: any, name?: string): any[];
6
- security(symbol: any, timeframe: any, expression: any): Promise<void>;
6
+ security(symbol: any, timeframe: any, expression: any, gaps?: boolean, lookahead?: boolean, ignore_invalid_symbol?: boolean, currency?: any, calc_bars_count?: any): Promise<any>;
7
+ private _findSecContextIdx;
7
8
  }
@@ -4,7 +4,7 @@ export declare class TechnicalAnalysis {
4
4
  get tr(): any;
5
5
  param(source: any, index: any, name?: string): any;
6
6
  ema(source: any, _period: any): any;
7
- sma(source: any, _period: any): any;
7
+ sma(source: any, _period: any, _cacheId?: any): any;
8
8
  vwma(source: any, _period: any): any;
9
9
  wma(source: any, _period: any): any;
10
10
  hma(source: any, _period: any): any;
@@ -22,5 +22,9 @@ export declare class TechnicalAnalysis {
22
22
  stdev(source: any, _length: any, _bias?: boolean): any;
23
23
  linreg(source: any, _length: any, _offset: any): any;
24
24
  supertrend(_factor: any, _atrPeriod: any): any[][];
25
+ crossover(source1: any, source2: any): boolean;
26
+ crossunder(source1: any, source2: any): boolean;
27
+ pivothigh(source: any, _leftbars: any, _rightbars: any): any;
28
+ pivotlow(source: any, _leftbars: any, _rightbars: any): any;
25
29
  }
26
30
  export default TechnicalAnalysis;
@@ -9,8 +9,10 @@ export declare class ScopeManager {
9
9
  private loopVars;
10
10
  private loopVarNames;
11
11
  private paramIdCounter;
12
+ private cacheIdCounter;
12
13
  private tempVarCounter;
13
14
  get nextParamIdArg(): any;
15
+ get nextCacheIdArg(): any;
14
16
  constructor();
15
17
  pushScope(type: string): void;
16
18
  popScope(): void;
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "pinets",
3
- "version": "0.1.1",
3
+ "version": "0.1.33",
4
4
  "description": "",
5
- "main": "dist/pinets.min.es.js",
5
+ "main": "dist/pinets.dev.es.js",
6
6
  "types": "dist/types/index.d.ts",
7
7
  "files": [
8
8
  "dist"