pinets 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/pinets.dev.browser.js +22 -13
- package/dist/pinets.dev.cjs +22 -13
- package/dist/pinets.dev.cjs.map +1 -1
- package/dist/pinets.dev.es.js +22 -13
- package/dist/pinets.dev.es.js.map +1 -1
- package/dist/pinets.min.browser.js +10 -10
- package/dist/pinets.min.cjs +10 -10
- package/dist/pinets.min.es.js +1 -1
- package/package.json +1 -1
package/dist/pinets.min.es.js
CHANGED
|
@@ -16,4 +16,4 @@
|
|
|
16
16
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
import*as K from"acorn";import*as g from"acorn-walk";import*as z from"astring";var H=Object.defineProperty,Q=(r,t,e)=>t in r?H(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,j=(r,t,e)=>Q(r,typeof t!="symbol"?t+"":t,e);class J{constructor(){j(this,"scopes",[]),j(this,"scopeTypes",[]),j(this,"scopeCounts",new Map),j(this,"contextBoundVars",new Set),j(this,"arrayPatternElements",new Set),j(this,"rootParams",new Set),j(this,"varKinds",new Map),j(this,"loopVars",new Set),j(this,"loopVarNames",new Map),j(this,"paramIdCounter",0),j(this,"cacheIdCounter",0),j(this,"tempVarCounter",0),j(this,"taCallIdCounter",0),this.pushScope("glb")}get nextParamIdArg(){return{type:"Identifier",name:`'p${this.paramIdCounter++}'`}}get nextCacheIdArg(){return{type:"Identifier",name:`'cache_${this.cacheIdCounter++}'`}}getNextTACallId(){return{type:"Literal",value:`_ta${this.taCallIdCounter++}`}}pushScope(t){this.scopes.push(new Map),this.scopeTypes.push(t),this.scopeCounts.set(t,(this.scopeCounts.get(t)||0)+1)}popScope(){this.scopes.pop(),this.scopeTypes.pop()}getCurrentScopeType(){return this.scopeTypes[this.scopeTypes.length-1]}getCurrentScopeCount(){return this.scopeCounts.get(this.getCurrentScopeType())||1}addContextBoundVar(t,e=!1){this.contextBoundVars.add(t),e&&this.rootParams.add(t)}removeContextBoundVar(t){this.contextBoundVars.has(t)&&(this.contextBoundVars.delete(t),this.rootParams.has(t)&&this.rootParams.delete(t))}addArrayPatternElement(t){this.arrayPatternElements.add(t)}isContextBound(t){return this.contextBoundVars.has(t)}isArrayPatternElement(t){return this.arrayPatternElements.has(t)}isRootParam(t){return this.rootParams.has(t)}addLoopVariable(t,e){this.loopVars.add(t),this.loopVarNames.set(t,e)}getLoopVariableName(t){return this.loopVarNames.get(t)}isLoopVariable(t){return this.loopVars.has(t)}addVariable(t,e){if(this.isContextBound(t))return t;const n=this.scopes[this.scopes.length-1],a=this.scopeTypes[this.scopeTypes.length-1],o=this.scopeCounts.get(a)||1,i=`${a}${o}_${t}`;return n.set(t,i),this.varKinds.set(i,e),i}getVariable(t){if(this.loopVars.has(t)){const e=this.loopVarNames.get(t);if(e)return[e,"let"]}if(this.isContextBound(t))return[t,"let"];for(let e=this.scopes.length-1;e>=0;e--){const n=this.scopes[e];if(n.has(t)){const a=n.get(t),o=this.varKinds.get(a)||"let";return[a,o]}}return[t,"let"]}generateTempVar(){return`temp_${++this.tempVarCounter}`}}//!!!Warning!!! this code is not clean, it was initially written as a PoC then used as transpiler for PineTS
|
|
19
|
-
const b="$",k={type:"Identifier",name:"undefined"};function N(r,t){if(r.computed&&r.property.type==="Identifier"){if(t.isLoopVariable(r.property.name))return;if(!t.isContextBound(r.property.name)){const[e,n]=t.getVariable(r.property.name);r.property={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1},r.property={type:"MemberExpression",object:r.property,property:{type:"Literal",value:0},computed:!0}}}if(r.computed&&r.object.type==="Identifier"){if(t.isLoopVariable(r.object.name))return;if(!t.isContextBound(r.object.name)){const[e,n]=t.getVariable(r.object.name);r.object={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1}}if(r.property.type==="MemberExpression"){const e=r.property;e._indexTransformed||(N(e,t),e._indexTransformed=!0)}}}function I(r,t,e){if(r.object&&r.object.type==="Identifier"&&r.object.name==="Math")return;const n=e.getCurrentScopeType()=="if",a=e.getCurrentScopeType()=="els",o=e.getCurrentScopeType()=="for";!n&&!a&&!o&&r.object&&r.object.type==="Identifier"&&e.isContextBound(r.object.name)&&!e.isRootParam(r.object.name)||r._indexTransformed||(N(r,e),r._indexTransformed=!0)}function D(r,t){r.declarations.forEach(e=>{e.init.name=="na"&&(e.init.name="NaN");const n=e.init&&e.init.type==="MemberExpression"&&e.init.object&&(e.init.object.name==="context"||e.init.object.name===b||e.init.object.name==="context2"),a=e.init&&e.init.type==="MemberExpression"&&e.init.object?.object&&(e.init.object.object.name==="context"||e.init.object.object.name===b||e.init.object.object.name==="context2"),o=e.init&&e.init.type==="ArrowFunctionExpression";if(n){e.id.name&&t.addContextBoundVar(e.id.name),e.id.properties&&e.id.properties.forEach(p=>{p.key.name&&t.addContextBoundVar(p.key.name)}),e.init.object.name=b;return}if(a){e.id.name&&t.addContextBoundVar(e.id.name),e.id.properties&&e.id.properties.forEach(p=>{p.key.name&&t.addContextBoundVar(p.key.name)}),e.init.object.object.name=b;return}o&&e.init.params.forEach(p=>{p.type==="Identifier"&&t.addContextBoundVar(p.name)});const i=t.addVariable(e.id.name,r.kind),s=r.kind;e.init&&!o&&(e.init.type==="CallExpression"&&e.init.callee.type==="MemberExpression"&&e.init.callee.object&&e.init.callee.object.type==="Identifier"&&t.isContextBound(e.init.callee.object.name)?S(e.init,t):g.recursive(e.init,{parent:e.init},{Identifier(p,u){p.parent=u.parent,E(p,t);const y=p.parent&&p.parent.type==="BinaryExpression",x=p.parent&&p.parent.type==="ConditionalExpression";p.type==="Identifier"&&(y||x)&&Object.assign(p,{type:"MemberExpression",object:{type:"Identifier",name:p.name},property:{type:"Literal",value:0},computed:!0})},CallExpression(p,u,y){p.callee.type==="Identifier"&&(p.callee.parent=p),p.arguments.forEach(x=>{x.type==="Identifier"&&(x.parent=p)}),S(p,t),p.arguments.forEach(x=>y(x,{parent:p}))},BinaryExpression(p,u,y){p.left.type==="Identifier"&&(p.left.parent=p),p.right.type==="Identifier"&&(p.right.parent=p),y(p.left,{parent:p}),y(p.right,{parent:p})},MemberExpression(p,u,y){p.object.type==="Identifier"&&(p.object.parent=p),p.property.type==="Identifier"&&(p.property.parent=p),N(p,t),p.object&&y(p.object,{parent:p})}}));const c={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:s},computed:!1},property:{type:"Identifier",name:i},computed:!1},l=t.isArrayPatternElement(e.id.name),m=!l&&e.init&&e.init.type==="MemberExpression"&&e.init.computed&&e.init.property&&(e.init.property.type==="Literal"||e.init.property.type==="MemberExpression");e.init?.property?.type==="MemberExpression"&&(e.init.property._indexTransformed||(N(e.init.property,t),e.init.property._indexTransformed=!0));const h={type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:c,right:e.init?o||l?e.init:{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"init"},computed:!1},arguments:m?[c,e.init.object,e.init.property]:[c,e.init]}:{type:"Identifier",name:"undefined"}}};if(l){h.expression.right.object.property.name+=`?.[0][${e.init.property.value}]`;const p=h.expression.right.object;h.expression.right={type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"init"},computed:!1},arguments:[c,p]}}o&&(t.pushScope("fn"),g.recursive(e.init.body,t,{BlockStatement(p,u,y){p.body.forEach(x=>y(x,u))},IfStatement(p,u,y){u.pushScope("if"),y(p.consequent,u),p.alternate&&(u.pushScope("els"),y(p.alternate,u),u.popScope()),u.popScope()},VariableDeclaration(p,u){D(p,u)},Identifier(p,u){E(p,u)},AssignmentExpression(p,u){L(p,u)}}),t.popScope()),Object.assign(r,h)})}function E(r,t){if(r.name!==b){if(r.name==="Math"||r.name==="NaN"||r.name==="undefined"||r.name==="Infinity"||r.name==="null"||r.name.startsWith("'")&&r.name.endsWith("'")||r.name.startsWith('"')&&r.name.endsWith('"')||r.name.startsWith("`")&&r.name.endsWith("`")||t.isLoopVariable(r.name)||t.isContextBound(r.name)&&!t.isRootParam(r.name))return;const e=r.parent&&r.parent.type==="MemberExpression"&&r.parent.object===r&&t.isContextBound(r.name),n=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee&&r.parent.callee.type==="MemberExpression"&&r.parent.callee.property.name==="param";r.parent&&r.parent.type==="AssignmentExpression"&&r.parent.left;const a=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee&&r.parent.callee.type==="MemberExpression"&&t.isContextBound(r.parent.callee.object.name),o=r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed,i=r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed&&r.parent.property===r&&r.parent.parent&&r.parent.parent.type==="CallExpression"&&r.parent.parent.callee&&r.parent.parent.callee.type==="MemberExpression"&&t.isContextBound(r.parent.parent.callee.object.name),s=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee===r;if(e||n||a||i||s){if(s)return;const[h,p]=t.getVariable(r.name);Object.assign(r,{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:p},computed:!1},property:{type:"Identifier",name:h},computed:!1});return}const[c,l]=t.getVariable(r.name),m={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:l},computed:!1},property:{type:"Identifier",name:c},computed:!1};!(r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed&&r.parent.object===r)&&!o?Object.assign(r,{type:"MemberExpression",object:m,property:{type:"Literal",value:0},computed:!0}):Object.assign(r,m)}}function L(r,t){if(r.left.type==="Identifier"){const[e,n]=t.getVariable(r.left.name),a={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1};r.left={type:"MemberExpression",object:a,property:{type:"Literal",value:0},computed:!0}}g.recursive(r.right,{parent:r.right,inNamespaceCall:!1},{Identifier(e,n,a){e.name=="na"&&(e.name="NaN"),e.parent=n.parent,E(e,t);const o=e.parent&&e.parent.type==="BinaryExpression",i=e.parent&&e.parent.type==="ConditionalExpression",s=t.isContextBound(e.name)&&!t.isRootParam(e.name),c=e.parent&&e.parent.type==="MemberExpression"&&e.parent.computed&&e.parent.object===e,l=e.parent&&e.parent._isParamCall,m=e.parent&&e.parent.type==="MemberExpression",h=e.name==="NaN";(s||i||o)&&(e.type==="MemberExpression"?N(e,t):e.type==="Identifier"&&!m&&!c&&!l&&!h&&$(e))},MemberExpression(e,n,a){N(e,t),e.object&&a(e.object,{parent:e,inNamespaceCall:n.inNamespaceCall})},CallExpression(e,n,a){const o=e.callee&&e.callee.type==="MemberExpression"&&e.callee.object&&e.callee.object.type==="Identifier"&&t.isContextBound(e.callee.object.name);S(e,t),e.arguments.forEach(i=>a(i,{parent:e,inNamespaceCall:o||n.inNamespaceCall}))}})}function X(r,t,e=!1){r.params.forEach(n=>{n.type==="Identifier"&&t.addContextBoundVar(n.name,e)})}function F(r,t){const e=t.getCurrentScopeType();r.argument&&(r.argument.type==="ArrayExpression"?(r.argument.elements=r.argument.elements.map(n=>{if(n.type==="Identifier"){if(t.isContextBound(n.name)&&!t.isRootParam(n.name))return{type:"MemberExpression",object:n,property:{type:"Literal",value:0},computed:!0};const[a,o]=t.getVariable(n.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1},property:{type:"Literal",value:0},computed:!0}}else if(n.type==="MemberExpression")return n.computed&&n.object.type==="Identifier"&&t.isContextBound(n.object.name)&&!t.isRootParam(n.object.name)||I(n,"",t),n;return n}),r.argument={type:"ArrayExpression",elements:[r.argument]}):r.argument.type==="BinaryExpression"?g.recursive(r.argument,t,{Identifier(n,a){E(n,a),n.type==="Identifier"&&$(n)},MemberExpression(n){I(n,"",t)}}):r.argument.type==="ObjectExpression"?r.argument.properties=r.argument.properties.map(n=>{if(n.shorthand){const[a,o]=t.getVariable(n.value.name);return{type:"Property",key:{type:"Identifier",name:n.key.name},value:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return n}):r.argument.type==="Identifier"&&(E(r.argument,t),r.argument.type==="Identifier"&&$(r.argument)),e==="fn"&&(r.argument.type==="Identifier"&&t.isContextBound(r.argument.name)&&!t.isRootParam(r.argument.name)?r.argument={type:"MemberExpression",object:r.argument,property:{type:"Literal",value:0},computed:!0}:r.argument.type==="MemberExpression"?r.argument.object.type==="Identifier"&&t.isContextBound(r.argument.object.name)&&!t.isRootParam(r.argument.object.name)&&(r.argument._indexTransformed||(N(r.argument,t),r.argument._indexTransformed=!0)):(r.argument.type==="BinaryExpression"||r.argument.type==="LogicalExpression"||r.argument.type==="ConditionalExpression"||r.argument.type==="CallExpression")&&g.recursive(r.argument,t,{Identifier(n,a){E(n,a),n.type==="Identifier"&&!n._arrayAccessed&&($(n),n._arrayAccessed=!0)},MemberExpression(n){I(n,"",t)},CallExpression(n,a){S(n,a)}}),r.argument={type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"precision"}},arguments:[r.argument]}))}function V(r,t){if(r.type==="Identifier"){if(r.name==="na")return r.name="NaN",r;if(t.isLoopVariable(r.name))return r;if(t.isRootParam(r.name)){const[a,o]=t.getVariable(r.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1}}if(t.isContextBound(r.name))return r;const[e,n]=t.getVariable(r.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1}}return r}function U(r,t,e){const n=B(r.argument,t,e);return{type:"UnaryExpression",operator:r.operator,prefix:r.prefix,argument:n,start:r.start,end:r.end}}function B(r,t,e=""){switch(r.type){case"BinaryExpression":return W(r,t,e);case"MemberExpression":return{type:"MemberExpression",object:r.object.type==="Identifier"?V(r.object,t):r.object,property:r.property,computed:r.computed};case"Identifier":return t.isLoopVariable(r.name)||r.parent&&r.parent.type==="MemberExpression"&&r.parent.property===r?r:{type:"MemberExpression",object:V(r,t),property:{type:"Literal",value:0},computed:!0};case"UnaryExpression":return U(r,t,e)}return r}function W(r,t,e){const n=B(r.left,t,e),a=B(r.right,t,e),o={type:"BinaryExpression",operator:r.operator,left:n,right:a,start:r.start,end:r.end};return g.recursive(o,t,{CallExpression(i,s){i._transformed||S(i,s)},MemberExpression(i){I(i,"",t)}}),o}function Y(r,t,e){const n=B(r.left,t,e),a=B(r.right,t,e),o={type:"LogicalExpression",operator:r.operator,left:n,right:a,start:r.start,end:r.end};return g.recursive(o,t,{CallExpression(i,s){i._transformed||S(i,s)}}),o}function Z(r,t,e){return g.recursive(r,{parent:r,inNamespaceCall:!1},{Identifier(n,a,o){if(n.name=="NaN")return;if(n.name=="na"){n.name="NaN";return}n.parent=a.parent,E(n,t);const i=n.parent&&n.parent.type==="BinaryExpression";(n.parent&&n.parent.type==="ConditionalExpression"||i)&&(n.type==="MemberExpression"?N(n,t):n.type==="Identifier"&&$(n))},MemberExpression(n,a,o){N(n,t),n.object&&o(n.object,{parent:n,inNamespaceCall:a.inNamespaceCall})},CallExpression(n,a,o){const i=n.callee&&n.callee.type==="MemberExpression"&&n.callee.object&&n.callee.object.type==="Identifier"&&t.isContextBound(n.callee.object.name);S(n,t),n.arguments.forEach(s=>o(s,{parent:n,inNamespaceCall:i||a.inNamespaceCall}))}}),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:e},property:{type:"Identifier",name:"param"}},arguments:[r,k,t.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function O(r,t,e){switch(r?.type){case"BinaryExpression":r=W(r,e,t);break;case"LogicalExpression":r=Y(r,e,t);break;case"ConditionalExpression":return Z(r,e,t);case"UnaryExpression":r=U(r,e,t);break}if(r.type==="MemberExpression"&&r.computed&&r.property){const n=r.object.type==="Identifier"&&e.isContextBound(r.object.name)&&!e.isRootParam(r.object.name)?r.object:V(r.object,e),a=r.property.type==="Identifier"&&!e.isContextBound(r.property.name)&&!e.isLoopVariable(r.property.name)?V(r.property,e):r.property;return{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[n,a,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}if(r.type==="ObjectExpression"&&(r.properties=r.properties.map(n=>{if(n.value.name){const[a,o]=e.getVariable(n.value.name);return{type:"Property",key:{type:"Identifier",name:n.key.name},value:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return n})),r.type==="Identifier"){if(r.name==="na")return r.name="NaN",r;if(e.isContextBound(r.name)&&!e.isRootParam(r.name))return{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[r,k,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}return r?.type==="CallExpression"&&S(r,e),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[r.type==="Identifier"?V(r,e):r,k,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function S(r,t,e){if(!r._transformed){if(r.callee&&r.callee.type==="MemberExpression"&&r.callee.object&&r.callee.object.type==="Identifier"&&(t.isContextBound(r.callee.object.name)||r.callee.object.name==="math"||r.callee.object.name==="ta")){const n=r.callee.object.name;r.arguments=r.arguments.map(a=>a._isParamCall?a:O(a,n,t)),n==="ta"&&r.arguments.push(t.getNextTACallId()),r._transformed=!0}else r.callee&&r.callee.type==="Identifier"&&(r.arguments=r.arguments.map(n=>n._isParamCall?n:O(n,b,t)),r._transformed=!0);r.arguments.forEach(n=>{g.recursive(n,t,{Identifier(a,o,i){a.parent=o.parent,E(a,t);const s=a.parent&&a.parent.type==="BinaryExpression";(a.parent&&a.parent.type==="ConditionalExpression"||s)&&(a.type==="MemberExpression"?N(a,t):a.type==="Identifier"&&$(a))},CallExpression(a,o,i){a._transformed||S(a,o)},MemberExpression(a,o,i){I(a,"",t),a.object&&i(a.object,{parent:a,inNamespaceCall:o.inNamespaceCall})}})})}}function tt(r,t){const e=[];r.params.forEach(n=>{n.type==="Identifier"&&(t.addContextBoundVar(n.name,!1),e.push(n.name))}),r.body&&r.body.type==="BlockStatement"&&(t.pushScope("fn"),g.recursive(r.body,t,{BlockStatement(n,a,o){n.body.forEach(i=>o(i,a))},ReturnStatement(n,a){F(n,a)},VariableDeclaration(n,a){D(n,a)},Identifier(n,a){E(n,a)},CallExpression(n,a){S(n,a),n.arguments.forEach(o=>{o.type==="BinaryExpression"&&g.recursive(o,a,{CallExpression(i,s){S(i,s)},MemberExpression(i){I(i,"",a)}})})},MemberExpression(n){I(n,"",t)},AssignmentExpression(n,a){L(n,a)},ForStatement(n,a,o){q(n,a,o)},IfStatement(n,a,o){G(n,a,o)},BinaryExpression(n,a,o){g.recursive(n,a,{CallExpression(i,s){S(i,s)},MemberExpression(i){I(i,"",a)}})}}),t.popScope())}function $(r,t){Object.assign(r,{type:"MemberExpression",object:{type:"Identifier",name:r.name,start:r.start,end:r.end},property:{type:"Literal",value:0},computed:!0,_indexTransformed:!0})}function q(r,t,e){if(r.init&&r.init.type==="VariableDeclaration"){const n=r.init.declarations[0],a=n.id.name;t.addLoopVariable(a,a),r.init={type:"VariableDeclaration",kind:r.init.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:a},init:n.init}]},n.init&&g.recursive(n.init,t,{Identifier(o,i){t.isLoopVariable(o.name)||(t.pushScope("for"),E(o,i),t.popScope())},MemberExpression(o){t.pushScope("for"),I(o,"",t),t.popScope()}})}r.test&&g.recursive(r.test,t,{Identifier(n,a){!t.isLoopVariable(n.name)&&!n.computed&&(t.pushScope("for"),E(n,a),n.type==="Identifier"&&(n.computed=!0,$(n)),t.popScope())},MemberExpression(n){t.pushScope("for"),I(n,"",t),t.popScope()}}),r.update&&g.recursive(r.update,t,{Identifier(n,a){t.isLoopVariable(n.name)||(t.pushScope("for"),E(n,a),t.popScope())}}),t.pushScope("for"),e(r.body,t),t.popScope()}function et(r,t){g.recursive(r,t,{MemberExpression(e){I(e,"",t)},CallExpression(e,n){S(e,n)},Identifier(e,n){E(e,n);const a=t.getCurrentScopeType()==="if";t.isContextBound(e.name)&&!t.isRootParam(e.name)&&a&&$(e)}})}function G(r,t,e){r.test&&(t.pushScope("if"),et(r.test,t),t.popScope()),t.pushScope("if"),e(r.consequent,t),t.popScope(),r.alternate&&(t.pushScope("els"),e(r.alternate,t),t.popScope())}function nt(r){g.recursive(r,null,{VariableDeclaration(t,e,n){t.declarations&&t.declarations.length>0&&t.declarations.forEach(a=>{if(a.init&&a.init.type==="ArrowFunctionExpression"&&a.init.start!==0){const o={type:"FunctionDeclaration",id:a.id,params:a.init.params,body:a.init.body.type==="BlockStatement"?a.init.body:{type:"BlockStatement",body:[{type:"ReturnStatement",argument:a.init.body}]},async:a.init.async,generator:!1};Object.assign(t,o)}}),t.body&&t.body.body&&t.body.body.forEach(a=>n(a,e))}})}function rt(r,t){g.simple(r,{VariableDeclaration(e){e.declarations.forEach(n=>{const a=n.init&&n.init.type==="MemberExpression"&&n.init.object&&(n.init.object.name==="context"||n.init.object.name===b||n.init.object.name==="context2"),o=n.init&&n.init.type==="MemberExpression"&&n.init.object?.object&&(n.init.object.object.name==="context"||n.init.object.object.name===b||n.init.object.object.name==="context2");(a||o)&&(n.id.name&&t.addContextBoundVar(n.id.name),n.id.properties&&n.id.properties.forEach(i=>{i.key.name&&t.addContextBoundVar(i.key.name)}))})}})}function at(r){let t=typeof r=="function"?r.toString():r;const e=K.parse(t.trim(),{ecmaVersion:"latest",sourceType:"module"});nt(e);const n=new J;let a;rt(e,n),g.simple(e,{FunctionDeclaration(i){tt(i,n)},ArrowFunctionExpression(i){const s=i.start===0;s&&i.params&&i.params.length>0&&(a=i.params[0].name,i.params[0].name=b),X(i,n,s)},VariableDeclaration(i){i.declarations.forEach(s=>{if(s.id.type==="ArrayPattern"){const c=n.generateTempVar(),l={type:"VariableDeclaration",kind:i.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:c},init:s.init}]};s.id.elements?.forEach(h=>{h.type==="Identifier"&&n.addArrayPatternElement(h.name)});const m=s.id.elements.map((h,p)=>({type:"VariableDeclaration",kind:i.kind,declarations:[{type:"VariableDeclarator",id:h,init:{type:"MemberExpression",object:{type:"Identifier",name:c},property:{type:"Literal",value:p},computed:!0}}]}));Object.assign(i,{type:"BlockStatement",body:[l,...m]})}})},ForStatement(i){}}),g.recursive(e,n,{BlockStatement(i,s,c){i.body.forEach(l=>c(l,s))},ReturnStatement(i,s){F(i,s)},VariableDeclaration(i,s){D(i,s)},Identifier(i,s){E(i,s)},CallExpression(i,s){S(i,s)},MemberExpression(i){I(i,a,n)},AssignmentExpression(i,s){L(i,s)},FunctionDeclaration(i,s){},ForStatement(i,s,c){q(i,s,c)},IfStatement(i,s,c){G(i,s,c)}}),it(e);const o=z.generate(e);return new Function("",`return ${o}`)(this)}function it(r){g.simple(r,{BinaryExpression(t){if(t.operator==="=="||t.operator==="==="){const e=t.left,n=t.right;Object.assign(t,{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:"math"},property:{type:"Identifier",name:"__eq"},computed:!1},arguments:[e,n],_transformed:!0})}}})}var ot=Object.defineProperty,st=(r,t,e)=>t in r?ot(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,A=(r,t,e)=>st(r,typeof t!="symbol"?t+"":t,e);const pt=5e3;class ct{constructor(t,e,n,a,o,i){this.source=t,this.tickerId=e,this.timeframe=n,this.limit=a,this.sDate=o,this.eDate=i,A(this,"data",[]),A(this,"open",[]),A(this,"high",[]),A(this,"low",[]),A(this,"close",[]),A(this,"volume",[]),A(this,"hl2",[]),A(this,"hlc3",[]),A(this,"ohlc4",[]),A(this,"openTime",[]),A(this,"closeTime",[]),A(this,"_periods"),A(this,"_readyPromise",null),A(this,"_ready",!1),this._readyPromise=new Promise(s=>{this.loadMarketData(t,e,n,a,o,i).then(c=>{const l=c.reverse().slice(0,pt);this._periods=l.length,this.data=l;const m=l.map(d=>d.open),h=l.map(d=>d.close),p=l.map(d=>d.high),u=l.map(d=>d.low),y=l.map(d=>d.volume),x=l.map(d=>(d.high+d.low+d.close)/3),w=l.map(d=>(d.high+d.low)/2),M=l.map(d=>(d.high+d.low+d.open+d.close)/4),_=l.map(d=>d.openTime),C=l.map(d=>d.closeTime);this.open=m,this.close=h,this.high=p,this.low=u,this.volume=y,this.hl2=w,this.hlc3=x,this.ohlc4=M,this.openTime=_,this.closeTime=C,this._ready=!0,s(!0)})})}get periods(){return this._periods}async loadMarketData(t,e,n,a,o,i){return Array.isArray(t)?t:t.getMarketData(e,n,a,o,i)}async ready(){if(this._ready)return!0;if(!this._readyPromise)throw new Error("PineTS is not ready");return this._readyPromise}async run(t,e,n){await this.ready(),e||(e=this._periods);const a=new Vt({marketData:this.data,source:this.source,tickerId:this.tickerId,timeframe:this.timeframe,limit:this.limit,sDate:this.sDate,eDate:this.eDate});a.pineTSCode=t,a.useTACache=n;let o=at.bind(this)(t);const i=["const","var","let","params"];for(let s=this._periods-e,c=e-1;s<this._periods;s++,c--){a.idx=s,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 o(a);if(typeof l=="object"){typeof a.result!="object"&&(a.result={});for(let m in l){a.result[m]===void 0&&(a.result[m]=[]);const h=Array.isArray(l[m])?l[m][0]:l[m];a.result[m].push(h)}}else Array.isArray(a.result)||(a.result=[]),a.result.push(l);for(let m of i)for(let h in a[m])if(Array.isArray(a[m][h])){const p=a[m][h][0];a[m][h].unshift(p)}}return a}}var lt=Object.defineProperty,mt=(r,t,e)=>t in r?lt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,ht=(r,t,e)=>mt(r,typeof t!="symbol"?t+"":t,e);class ut{constructor(t){this.context=t,ht(this,"color",{param:(e,n=0)=>Array.isArray(e)?e[n]:e,rgb:(e,n,a,o)=>o?`rgba(${e}, ${n}, ${a}, ${o})`:`rgb(${e}, ${n}, ${a})`,new:(e,n)=>{if(e&&e.startsWith("#")){const a=e.slice(1),o=parseInt(a.slice(0,2),16),i=parseInt(a.slice(2,4),16),s=parseInt(a.slice(4,6),16);return n?`rgba(${o}, ${i}, ${s}, ${n})`:`rgb(${o}, ${i}, ${s})`}return n?`rgba(${e}, ${n})`:e},white:"white",lime:"lime",green:"green",red:"red",maroon:"maroon",black:"black",gray:"gray",blue:"blue"})}extractPlotOptions(t){const e={};for(let n in t)Array.isArray(t[n])?e[n]=t[n][0]:e[n]=t[n];return e}indicator(t,e,n){}plotchar(t,e,n){this.context.plots[e]||(this.context.plots[e]={data:[],options:this.extractPlotOptions(n),title:e}),this.context.plots[e].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:t[0],options:{...this.extractPlotOptions(n),style:"char"}})}plot(t,e,n){this.context.plots[e]||(this.context.plots[e]={data:[],options:this.extractPlotOptions(n),title:e}),this.context.plots[e].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:t[0],options:this.extractPlotOptions(n)})}na(t){return Array.isArray(t)?isNaN(t[0]):isNaN(t)}nz(t,e=0){const n=Array.isArray(t)?t[0]:t,a=Array.isArray(t)?e[0]:e;return isNaN(n)?a:n}}class yt{constructor(t){this.context=t}param(t,e=0){return Array.isArray(t)?[t[e]]:[t]}any(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}int(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}float(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}bool(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}string(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}timeframe(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}time(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}price(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}session(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}source(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}symbol(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}text_area(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}enum(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}color(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}}var dt=Object.defineProperty,xt=(r,t,e)=>t in r?dt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,ft=(r,t,e)=>xt(r,typeof t!="symbol"?t+"":t,e);class bt{constructor(t){this.context=t,ft(this,"_cache",{})}param(t,e,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(t)?e?(this.context.params[n]=t.slice(e),this.context.params[n].length=t.length,this.context.params[n]):(this.context.params[n]=t.slice(0),this.context.params[n]):(this.context.params[n][0]=t,this.context.params[n])}__eq(t,e){return Math.abs(t-e)<1e-8}abs(t){return Math.abs(t[0])}pow(t,e){return Math.pow(t[0],e[0])}sqrt(t){return Math.sqrt(t[0])}log(t){return Math.log(t[0])}ln(t){return Math.log(t[0])}exp(t){return Math.exp(t[0])}floor(t){return Math.floor(t[0])}ceil(t){return Math.ceil(t[0])}round(t){return Math.round(t[0])}random(){return Math.random()}max(...t){const e=t.map(n=>Array.isArray(n)?n[0]:n);return Math.max(...e)}min(...t){const e=t.map(n=>Array.isArray(n)?n[0]:n);return Math.min(...e)}sum(t,e){const n=Array.isArray(e)?e[0]:e;return Array.isArray(t)?t.slice(0,n).reduce((a,o)=>a+o,0):t}sin(t){return Math.sin(t[0])}cos(t){return Math.cos(t[0])}tan(t){return Math.tan(t[0])}acos(t){return Math.acos(t[0])}asin(t){return Math.asin(t[0])}atan(t){return Math.atan(t[0])}avg(...t){const e=t.map(n=>Array.isArray(n)?n[0]:n);return e.reduce((n,a)=>{const o=Array.isArray(n)?n[0]:n,i=Array.isArray(a)?a[0]:a;return o+i},0)/e.length}}var gt=Object.defineProperty,wt=(r,t,e)=>t in r?gt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,At=(r,t,e)=>wt(r,typeof t!="symbol"?t+"":t,e);const R=["1","3","5","15","30","45","60","120","180","240","D","W","M"];class vt{constructor(t){this.context=t,At(this,"_cache",{})}param(t,e,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(t)?(e?this.context.params[n]=t.slice(e):this.context.params[n]=t.slice(0),[t[e],n]):(this.context.params[n][0]=t,[t,n])}async security(t,e,n,a=!1,o=!1,i=!1,s=null,c=null){const l=t[0],m=e[0],h=n[0],p=n[1],u=R.indexOf(this.context.timeframe),y=R.indexOf(m);if(u==-1||y==-1)throw new Error("Invalid timeframe");if(u>y)throw new Error("Only higher timeframes are supported for now");if(u===y)return h;const x=this.context.data.openTime[0],w=this.context.data.closeTime[0];if(this.context.cache[p]){const C=this.context.cache[p],d=this._findSecContextIdx(x,w,C.data.openTime,C.data.closeTime,o);return d==-1?NaN:C.params[p][d]}const M=await new ct(this.context.source,l,m,this.context.limit||1e3,this.context.sDate,this.context.eDate).run(this.context.pineTSCode);this.context.cache[p]=M;const _=this._findSecContextIdx(x,w,M.data.openTime,M.data.closeTime,o);return _==-1?NaN:M.params[p][_]}_findSecContextIdx(t,e,n,a,o=!1){for(let i=0;i<n.length;i++)if(n[i]<=t&&e<=a[i])return i+(o?1:2);return-1}}class Et{constructor(t){this.context=t}get tr(){return this.context.math.max(this.context.data.high[0]-this.context.data.low[0],this.context.math.abs(this.context.data.high[0]-this.context.data.close[1]),this.context.math.abs(this.context.data.low[0]-this.context.data.close[1]))}param(t,e,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(t)?e?(this.context.params[n]=t.slice(e),this.context.params[n].length=t.length,this.context.params[n]):(this.context.params[n]=t.slice(0),this.context.params[n]):(this.context.params[n][0]=t,this.context.params[n])}ema(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`ema_${a}`;this.context.taState[o]||(this.context.taState[o]={prevEma:null,initSum:0,initCount:0});const i=this.context.taState[o],s=t[0];if(i.initCount<a)return i.initSum+=s,i.initCount++,i.initCount===a?(i.prevEma=i.initSum/a,this.context.precision(i.prevEma)):NaN;const c=2/(a+1),l=s*c+i.prevEma*(1-c);return i.prevEma=l,this.context.precision(l)}sma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`sma_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[],sum:0});const i=this.context.taState[o],s=t[0]||0;if(i.window.unshift(s),i.sum+=s,i.window.length<a)return NaN;if(i.window.length>a){const l=i.window.pop();i.sum-=l}const c=i.sum/a;return this.context.precision(c)}vwma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`vwma_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[],volumeWindow:[]});const i=this.context.taState[o],s=t[0],c=this.context.data.volume[0];if(i.window.unshift(s),i.volumeWindow.unshift(c),i.window.length<a)return NaN;i.window.length>a&&(i.window.pop(),i.volumeWindow.pop());let l=0,m=0;for(let p=0;p<a;p++)l+=i.window[p]*i.volumeWindow[p],m+=i.volumeWindow[p];const h=l/m;return this.context.precision(h)}wma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`wma_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();let c=0,l=0;for(let h=0;h<a;h++){const p=a-h;c+=i.window[h]*p,l+=p}const m=c/l;return this.context.precision(m)}hma(t,e,n){const a=Array.isArray(e)?e[0]:e,o=Math.floor(a/2),i=Math.floor(Math.sqrt(a)),s=this.wma(t,o,n?`${n}_wma1`:void 0),c=this.wma(t,a,n?`${n}_wma2`:void 0);if(isNaN(s)||isNaN(c))return NaN;this.context.taState||(this.context.taState={});const l=n||`hma_raw_${a}`;this.context.taState[l]||(this.context.taState[l]=[]);const m=2*s-c;this.context.taState[l].unshift(m);const h=n?`${n}_hma_final`:`hma_final_${a}`;this.context.taState[h]||(this.context.taState[h]={window:[]});const p=this.context.taState[h];if(p.window.unshift(m),p.window.length<i)return NaN;p.window.length>i&&p.window.pop();let u=0,y=0;for(let w=0;w<i;w++){const M=i-w;u+=p.window[w]*M,y+=M}const x=u/y;return this.context.precision(x)}rma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`rma_${a}`;this.context.taState[o]||(this.context.taState[o]={prevRma:null,initSum:0,initCount:0});const i=this.context.taState[o],s=t[0]||0;if(i.initCount<a)return i.initSum+=s,i.initCount++,i.initCount===a?(i.prevRma=i.initSum/a,this.context.precision(i.prevRma)):NaN;const c=1/a,l=s*c+i.prevRma*(1-c);return i.prevRma=l,this.context.precision(l)}change(t,e=1,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`change_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<=a)return NaN;i.window.length>a+1&&i.window.pop();const c=s-i.window[a];return this.context.precision(c)}rsi(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`rsi_${a}`;this.context.taState[o]||(this.context.taState[o]={prevValue:null,avgGain:0,avgLoss:0,initGains:[],initLosses:[]});const i=this.context.taState[o],s=t[0];if(i.prevValue!==null){const c=s-i.prevValue,l=c>0?c:0,m=c<0?-c:0;if(i.initGains.length<a){if(i.initGains.push(l),i.initLosses.push(m),i.initGains.length===a){i.avgGain=i.initGains.reduce((u,y)=>u+y,0)/a,i.avgLoss=i.initLosses.reduce((u,y)=>u+y,0)/a,i.prevValue=s;const p=i.avgLoss===0?100:100-100/(1+i.avgGain/i.avgLoss);return this.context.precision(p)}return i.prevValue=s,NaN}i.avgGain=(i.avgGain*(a-1)+l)/a,i.avgLoss=(i.avgLoss*(a-1)+m)/a;const h=i.avgLoss===0?100:100-100/(1+i.avgGain/i.avgLoss);return i.prevValue=s,this.context.precision(h)}return i.prevValue=s,NaN}atr(t,e){const n=Array.isArray(t)?t[0]:t;this.context.taState||(this.context.taState={});const a=e||`atr_${n}`;this.context.taState[a]||(this.context.taState[a]={prevAtr:null,initSum:0,initCount:0,prevClose:null});const o=this.context.taState[a],i=this.context.data.high[0],s=this.context.data.low[0],c=this.context.data.close[0];let l;if(o.prevClose!==null){const h=i-s,p=Math.abs(i-o.prevClose),u=Math.abs(s-o.prevClose);l=Math.max(h,p,u)}else l=i-s;if(o.prevClose=c,o.initCount<n)return o.initSum+=l,o.initCount++,o.initCount===n?(o.prevAtr=o.initSum/n,this.context.precision(o.prevAtr)):NaN;const m=(o.prevAtr*(n-1)+l)/n;return o.prevAtr=m,this.context.precision(m)}mom(t,e,n){const a=Array.isArray(e)?e[0]:e;return this.change(t,a)}roc(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`roc_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<=a)return NaN;i.window.length>a+1&&i.window.pop();const c=i.window[a],l=(s-c)/c*100;return this.context.precision(l)}dev(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`dev_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[],sum:0});const i=this.context.taState[o],s=t[0]||0;if(i.window.unshift(s),i.sum+=s,i.window.length<a)return NaN;if(i.window.length>a){const h=i.window.pop();i.sum-=h}const c=i.sum/a;let l=0;for(let h=0;h<a;h++)l+=Math.abs(i.window[h]-c);const m=l/a;return this.context.precision(m)}variance(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`variance_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();let c=0,l=0;for(let p=0;p<a;p++)c+=i.window[p],l+=i.window[p]*i.window[p];const m=c/a,h=l/a-m*m;return this.context.precision(h)}highest(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`highest_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=Math.max(...i.window.filter(l=>!isNaN(l)));return this.context.precision(c)}lowest(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`lowest_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=i.window.filter(m=>!isNaN(m)&&m!==void 0),l=c.length>0?Math.min(...c):NaN;return this.context.precision(l)}median(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`median_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=i.window.slice().sort((h,p)=>h-p),l=Math.floor(a/2),m=a%2===0?(c[l-1]+c[l])/2:c[l];return this.context.precision(m)}stdev(t,e,n=!0,a){const o=Array.isArray(e)?e[0]:e,i=Array.isArray(n)?n[0]:n;this.context.taState||(this.context.taState={});const s=a||`stdev_${o}_${i}`;this.context.taState[s]||(this.context.taState[s]={window:[],sum:0});const c=this.context.taState[s],l=t[0];if(c.window.unshift(l),c.sum+=l,c.window.length<o)return NaN;if(c.window.length>o){const y=c.window.pop();c.sum-=y}const m=c.sum/o;let h=0;for(let y=0;y<o;y++)h+=Math.pow(c.window[y]-m,2);const p=i?o:o-1,u=Math.sqrt(h/p);return this.context.precision(u)}linreg(t,e,n,a){const o=Array.isArray(e)?e[0]:e,i=Array.isArray(n)?n[0]:n;this.context.taState||(this.context.taState={});const s=a||`linreg_${o}_${i}`;this.context.taState[s]||(this.context.taState[s]={window:[]});const c=this.context.taState[s],l=t[0];if(c.window.unshift(l),c.window.length<o)return NaN;c.window.length>o&&c.window.pop();let m=0,h=0,p=0,u=0;const y=o;for(let _=0;_<o;_++){const C=o-1-_,d=c.window[_];m+=C,h+=d,p+=C*d,u+=C*C}const x=y*u-m*m;if(x===0)return NaN;const w=(y*p-m*h)/x,M=(h-w*m)/y+w*(o-1-i);return this.context.precision(M)}supertrend(t,e,n){const a=Array.isArray(t)?t[0]:t,o=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const i=`supertrend_${a}_${o}`;this.context.taState[i]||(this.context.taState[i]={prevUpperBand:null,prevLowerBand:null,prevSupertrend:null,prevDirection:null});const s=this.context.taState[i],c=this.context.data.high[0],l=this.context.data.low[0],m=this.context.data.close[0],h=this.atr(o,n?`${n}_atr`:void 0);if(isNaN(h))return[[NaN,0]];const p=(c+l)/2;let u=p+a*h,y=p-a*h;s.prevUpperBand!==null&&(u<s.prevUpperBand||this.context.data.close[1]>s.prevUpperBand?u=u:u=s.prevUpperBand,y>s.prevLowerBand||this.context.data.close[1]<s.prevLowerBand?y=y:y=s.prevLowerBand);let x,w;return s.prevSupertrend===null?(x=m<=u?-1:1,w=x===-1?u:y):s.prevSupertrend===s.prevUpperBand?m>u?(x=1,w=y):(x=-1,w=u):m<y?(x=-1,w=u):(x=1,w=y),s.prevUpperBand=u,s.prevLowerBand=y,s.prevSupertrend=w,s.prevDirection=x,[[this.context.precision(w),x]]}crossover(t,e){const n=Array.isArray(t)?t[0]:t,a=Array.isArray(e)?e[0]:e,o=Array.isArray(t)?t[1]:this.context.data.series[t][1],i=Array.isArray(e)?e[1]:this.context.data.series[e][1];return o<i&&n>a}crossunder(t,e){const n=Array.isArray(t)?t[0]:t,a=Array.isArray(e)?e[0]:e,o=Array.isArray(t)?t[1]:this.context.data.series[t][1],i=Array.isArray(e)?e[1]:this.context.data.series[e][1];return o>i&&n<a}pivothigh(t,e,n){n==null&&(n=e,e=t,t=this.context.data.high);const a=Array.isArray(e)?e[0]:e,o=Array.isArray(n)?n[0]:n,i=St(t.slice(0).reverse(),a,o),s=this.context.idx;return this.context.precision(i[s])}pivotlow(t,e,n){n==null&&(n=e,e=t,t=this.context.data.low);const a=Array.isArray(e)?e[0]:e,o=Array.isArray(n)?n[0]:n,i=jt(t.slice(0).reverse(),a,o),s=this.context.idx;return this.context.precision(i[s])}}function St(r,t,e){const n=new Array(r.length).fill(NaN);for(let a=t+e;a<r.length;a++){const o=r[a-e];let i=!0;for(let s=1;s<=t;s++)if(r[a-e-s]>=o){i=!1;break}if(i){for(let s=1;s<=e;s++)if(r[a-e+s]>=o){i=!1;break}}i&&(n[a]=o)}return n}function jt(r,t,e){const n=new Array(r.length).fill(NaN);for(let a=t+e;a<r.length;a++){const o=r[a-e];let i=!0;for(let s=1;s<=t;s++)if(r[a-e-s]<=o){i=!1;break}if(i){for(let s=1;s<=e;s++)if(r[a-e+s]<=o){i=!1;break}}i&&(n[a]=o)}return n}var It=Object.defineProperty,Mt=(r,t,e)=>t in r?It(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,Ct=(r,t,e)=>Mt(r,typeof t!="symbol"?t+"":t,e);class v{constructor(t){this.array=t}}class Nt{constructor(t){this.context=t,Ct(this,"_cache",{})}param(t,e=0){return Array.isArray(t)?t[e]:t}get(t,e){return t.array[e]}set(t,e,n){t.array[e]=n}push(t,e){t.array.push(e)}sum(t){return t.array.reduce((e,n)=>e+(isNaN(n)?0:n),0)}avg(t){return this.sum(t)/t.array.length}min(t,e=0){return[...t.array].sort((n,a)=>n-a)[e]??this.context.NA}max(t,e=0){return[...t.array].sort((n,a)=>a-n)[e]??this.context.NA}size(t){return t.array.length}new_bool(t,e=!1){return new v(Array(t).fill(e))}new_float(t,e=NaN){return new v(Array(t).fill(e))}new_int(t,e=0){return new v(Array(t).fill(Math.round(e)))}new_string(t,e=""){return new v(Array(t).fill(e))}new(t,e){return new v(Array(t).fill(e))}slice(t,e,n){const a=n!==void 0?n+1:void 0;return new v(t.array.slice(e,a))}reverse(t){t.array.reverse()}includes(t,e){return t.array.includes(e)}indexof(t,e){return t.array.indexOf(e)}lastindexof(t,e){return t.array.lastIndexOf(e)}stdev(t,e=!0){const n=this.avg(t),a=t.array.map(i=>Math.pow(i-n,2)),o=e?t.array.length:t.array.length-1;return Math.sqrt(this.sum(new v(a))/o)}variance(t,e=!0){const n=this.avg(t),a=t.array.map(i=>Math.pow(i-n,2)),o=e?t.array.length:t.array.length-1;return this.sum(new v(a))/o}covariance(t,e,n=!0){if(t.array.length!==e.array.length||t.array.length<2)return NaN;const a=n?t.array.length:t.array.length-1,o=this.avg(t),i=this.avg(e);let s=0;for(let c=0;c<t.array.length;c++)s+=(t.array[c]-o)*(e.array[c]-i);return s/a}first(t){return t.array.length>0?t.array[0]:this.context.NA}last(t){return t.array.length>0?t.array[t.array.length-1]:this.context.NA}clear(t){t.array.length=0}join(t,e=","){return t.array.join(e)}abs(t){return new v(t.array.map(e=>Math.abs(e)))}concat(t,e){return t.array.push(...e.array),t}copy(t){return new v([...t.array])}every(t,e){return t.array.every(e)}fill(t,e,n=0,a){const o=t.array.length,i=a!==void 0?Math.min(a,o):o;for(let s=n;s<i;s++)t.array[s]=e}from(t){return new v([...t])}insert(t,e,n){t.array.splice(e,0,n)}pop(t){return t.array.pop()}range(t){return this.max(t)-this.min(t)}remove(t,e){return e>=0&&e<t.array.length?t.array.splice(e,1)[0]:this.context.NA}shift(t){return t.array.shift()}sort(t,e="asc"){t.array.sort((n,a)=>e==="asc"?n-a:a-n)}sort_indices(t,e){const n=t.array.map((a,o)=>o);return n.sort((a,o)=>{const i=t.array[a],s=t.array[o];return e?e(i,s):i-s}),new v(n)}standardize(t){const e=this.avg(t),n=this.stdev(t);return n===0?new v(t.array.map(()=>0)):new v(t.array.map(a=>(a-e)/n))}unshift(t,e){t.array.unshift(e)}some(t,e){return t.array.some(e)}}var _t=Object.defineProperty,$t=(r,t,e)=>t in r?_t(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,f=(r,t,e)=>$t(r,typeof t!="symbol"?t+"":t,e);class Vt{constructor({marketData:t,source:e,tickerId:n,timeframe:a,limit:o,sDate:i,eDate:s}){f(this,"data",{open:[],high:[],low:[],close:[],volume:[],hl2:[],hlc3:[],ohlc4:[]}),f(this,"cache",{}),f(this,"taState",{}),f(this,"useTACache",!1),f(this,"NA",NaN),f(this,"math"),f(this,"ta"),f(this,"input"),f(this,"request"),f(this,"array"),f(this,"core"),f(this,"lang"),f(this,"idx",0),f(this,"params",{}),f(this,"const",{}),f(this,"var",{}),f(this,"let",{}),f(this,"result"),f(this,"plots",{}),f(this,"marketData"),f(this,"source"),f(this,"tickerId"),f(this,"timeframe",""),f(this,"limit"),f(this,"sDate"),f(this,"eDate"),f(this,"pineTSCode"),this.marketData=t,this.source=e,this.tickerId=n,this.timeframe=a,this.limit=o,this.sDate=i,this.eDate=s,this.math=new bt(this),this.ta=new Et(this),this.input=new yt(this),this.request=new vt(this),this.array=new Nt(this);const c=new ut(this);this.core={plotchar:c.plotchar.bind(c),na:c.na.bind(c),color:c.color,plot:c.plot.bind(c),nz:c.nz.bind(c)}}init(t,e,n=0){return t?!Array.isArray(e)||Array.isArray(e[0])?t[0]=Array.isArray(e?.[0])?e[0]:this.precision(e):t[0]=this.precision(e[e.length-this.idx-1+n]):Array.isArray(e)?t=[this.precision(e[e.length-this.idx-1+n])]:t=[this.precision(e)],t}precision(t,e=10){return typeof t!="number"||isNaN(t)?t:Number(t.toFixed(e))}param(t,e,n){return typeof t=="string"||!Array.isArray(t)&&typeof t=="object"?t:(this.params[n]||(this.params[n]=[]),Array.isArray(t)?e?(this.params[n]=t.slice(e),this.params[n].length=t.length,this.params[n]):(this.params[n]=t.slice(0),this.params[n]):(this.params[n][0]=t,this.params[n]))}}var Bt=Object.defineProperty,Pt=(r,t,e)=>t in r?Bt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,T=(r,t,e)=>Pt(r,typeof t!="symbol"?t+"":t,e);const kt="https://api.binance.com/api/v3",P={1:"1m",3:"3m",5:"5m",15:"15m",30:"30m",45:null,60:"1h",120:"2h",180:null,240:"4h","4H":"4h","1D":"1d",D:"1d","1W":"1w",W:"1w","1M":"1M",M:"1M"};class Tt{constructor(t=5*60*1e3){T(this,"cache"),T(this,"cacheDuration"),this.cache=new Map,this.cacheDuration=t}generateKey(t){return Object.entries(t).filter(([e,n])=>n!==void 0).map(([e,n])=>`${e}:${n}`).join("|")}get(t){const e=this.generateKey(t),n=this.cache.get(e);return n?Date.now()-n.timestamp>this.cacheDuration?(this.cache.delete(e),null):n.data:null}set(t,e){const n=this.generateKey(t);this.cache.set(n,{data:e,timestamp:Date.now()})}clear(){this.cache.clear()}cleanup(){const t=Date.now();for(const[e,n]of this.cache.entries())t-n.timestamp>this.cacheDuration&&this.cache.delete(e)}}class Dt{constructor(){T(this,"cacheManager"),this.cacheManager=new Tt(5*60*1e3)}async getMarketDataInterval(t,e,n,a){try{const o=P[e.toUpperCase()];if(!o)return console.error(`Unsupported timeframe: ${e}`),[];const i={"1m":60*1e3,"3m":3*60*1e3,"5m":5*60*1e3,"15m":15*60*1e3,"30m":30*60*1e3,"1h":60*60*1e3,"2h":2*60*60*1e3,"4h":4*60*60*1e3,"1d":24*60*60*1e3,"1w":7*24*60*60*1e3,"1M":30*24*60*60*1e3};let s=[],c=n;const l=a,m=i[o];if(!m)return console.error(`Duration not defined for interval: ${o}`),[];for(;c<l;){const h=Math.min(c+1e3*m,l),p=await this.getMarketData(t,e,1e3,c,h);if(p.length===0)break;s=s.concat(p),c=p[p.length-1].closeTime+1}return s}catch(o){return console.error("Error in getMarketDataInterval:",o),[]}}async getMarketData(t,e,n,a,o){try{const i={tickerId:t,timeframe:e,limit:n,sDate:a,eDate:o},s=this.cacheManager.get(i);if(s)return console.log("cache hit",t,e,n,a,o),s;const c=P[e.toUpperCase()];if(!c)return console.error(`Unsupported timeframe: ${e}`),[];if(this.shouldPaginate(e,n,a,o)&&a&&o){const p=await this.getMarketDataInterval(t,e,a,o),u=n?p.slice(0,n):p;return this.cacheManager.set(i,u),u}let l=`${kt}/klines?symbol=${t}&interval=${c}`;n&&(l+=`&limit=${Math.min(n,1e3)}`),a&&(l+=`&startTime=${a}`),o&&(l+=`&endTime=${o}`);const m=await fetch(l);if(!m.ok)throw new Error(`HTTP error! status: ${m.status}`);const h=(await m.json()).map(p=>({openTime:parseInt(p[0]),open:parseFloat(p[1]),high:parseFloat(p[2]),low:parseFloat(p[3]),close:parseFloat(p[4]),volume:parseFloat(p[5]),closeTime:parseInt(p[6]),quoteAssetVolume:parseFloat(p[7]),numberOfTrades:parseInt(p[8]),takerBuyBaseAssetVolume:parseFloat(p[9]),takerBuyQuoteAssetVolume:parseFloat(p[10]),ignore:p[11]}));return this.cacheManager.set(i,h),h}catch(i){return console.error("Error in binance.klines:",i),[]}}shouldPaginate(t,e,n,a){if(e&&e>1e3)return!0;if(n&&a){const o=P[t.toUpperCase()],i={"1m":60*1e3,"3m":3*60*1e3,"5m":5*60*1e3,"15m":15*60*1e3,"30m":30*60*1e3,"1h":60*60*1e3,"2h":2*60*60*1e3,"4h":4*60*60*1e3,"1d":24*60*60*1e3,"1w":7*24*60*60*1e3,"1M":30*24*60*60*1e3}[o];if(i)return Math.ceil((a-n)/i)>1e3}return!1}}const Lt={Binance:new Dt};export{Vt as Context,ct as PineTS,Lt as Provider};
|
|
19
|
+
const b="$",k={type:"Identifier",name:"undefined"};function N(r,t){if(r.computed&&r.property.type==="Identifier"){if(t.isLoopVariable(r.property.name))return;if(!t.isContextBound(r.property.name)){const[e,n]=t.getVariable(r.property.name);r.property={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1},r.property={type:"MemberExpression",object:r.property,property:{type:"Literal",value:0},computed:!0}}}if(r.computed&&r.object.type==="Identifier"){if(t.isLoopVariable(r.object.name))return;if(!t.isContextBound(r.object.name)){const[e,n]=t.getVariable(r.object.name);r.object={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1}}if(r.property.type==="MemberExpression"){const e=r.property;e._indexTransformed||(N(e,t),e._indexTransformed=!0)}}}function I(r,t,e){if(r.object&&r.object.type==="Identifier"&&r.object.name==="Math")return;const n=e.getCurrentScopeType()=="if",a=e.getCurrentScopeType()=="els",o=e.getCurrentScopeType()=="for";!n&&!a&&!o&&r.object&&r.object.type==="Identifier"&&e.isContextBound(r.object.name)&&!e.isRootParam(r.object.name)||r._indexTransformed||(N(r,e),r._indexTransformed=!0)}function D(r,t){r.declarations.forEach(e=>{e.init.name=="na"&&(e.init.name="NaN");const n=e.init&&e.init.type==="MemberExpression"&&e.init.object&&(e.init.object.name==="context"||e.init.object.name===b||e.init.object.name==="context2"),a=e.init&&e.init.type==="MemberExpression"&&e.init.object?.object&&(e.init.object.object.name==="context"||e.init.object.object.name===b||e.init.object.object.name==="context2"),o=e.init&&e.init.type==="ArrowFunctionExpression";if(n){e.id.name&&t.addContextBoundVar(e.id.name),e.id.properties&&e.id.properties.forEach(p=>{p.key.name&&t.addContextBoundVar(p.key.name)}),e.init.object.name=b;return}if(a){e.id.name&&t.addContextBoundVar(e.id.name),e.id.properties&&e.id.properties.forEach(p=>{p.key.name&&t.addContextBoundVar(p.key.name)}),e.init.object.object.name=b;return}o&&e.init.params.forEach(p=>{p.type==="Identifier"&&t.addContextBoundVar(p.name)});const i=t.addVariable(e.id.name,r.kind),s=r.kind;e.init&&!o&&(e.init.type==="CallExpression"&&e.init.callee.type==="MemberExpression"&&e.init.callee.object&&e.init.callee.object.type==="Identifier"&&t.isContextBound(e.init.callee.object.name)?S(e.init,t):g.recursive(e.init,{parent:e.init},{Identifier(p,u){p.parent=u.parent,E(p,t);const y=p.parent&&p.parent.type==="BinaryExpression",x=p.parent&&p.parent.type==="ConditionalExpression";p.type==="Identifier"&&(y||x)&&Object.assign(p,{type:"MemberExpression",object:{type:"Identifier",name:p.name},property:{type:"Literal",value:0},computed:!0})},CallExpression(p,u,y){p.callee.type==="Identifier"&&(p.callee.parent=p),p.arguments.forEach(x=>{x.type==="Identifier"&&(x.parent=p)}),S(p,t),p.arguments.forEach(x=>y(x,{parent:p}))},BinaryExpression(p,u,y){p.left.type==="Identifier"&&(p.left.parent=p),p.right.type==="Identifier"&&(p.right.parent=p),y(p.left,{parent:p}),y(p.right,{parent:p})},MemberExpression(p,u,y){p.object.type==="Identifier"&&(p.object.parent=p),p.property.type==="Identifier"&&(p.property.parent=p),N(p,t),p.object&&y(p.object,{parent:p})}}));const c={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:s},computed:!1},property:{type:"Identifier",name:i},computed:!1},l=t.isArrayPatternElement(e.id.name),m=!l&&e.init&&e.init.type==="MemberExpression"&&e.init.computed&&e.init.property&&(e.init.property.type==="Literal"||e.init.property.type==="MemberExpression");e.init?.property?.type==="MemberExpression"&&(e.init.property._indexTransformed||(N(e.init.property,t),e.init.property._indexTransformed=!0));const h={type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:c,right:e.init?o||l?e.init:{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"init"},computed:!1},arguments:m?[c,e.init.object,e.init.property]:[c,e.init]}:{type:"Identifier",name:"undefined"}}};if(l){h.expression.right.object.property.name+=`?.[0][${e.init.property.value}]`;const p=h.expression.right.object;h.expression.right={type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"init"},computed:!1},arguments:[c,p]}}o&&(t.pushScope("fn"),g.recursive(e.init.body,t,{BlockStatement(p,u,y){p.body.forEach(x=>y(x,u))},IfStatement(p,u,y){u.pushScope("if"),y(p.consequent,u),p.alternate&&(u.pushScope("els"),y(p.alternate,u),u.popScope()),u.popScope()},VariableDeclaration(p,u){D(p,u)},Identifier(p,u){E(p,u)},AssignmentExpression(p,u){L(p,u)}}),t.popScope()),Object.assign(r,h)})}function E(r,t){if(r.name!==b){if(r.name==="Math"||r.name==="NaN"||r.name==="undefined"||r.name==="Infinity"||r.name==="null"||r.name.startsWith("'")&&r.name.endsWith("'")||r.name.startsWith('"')&&r.name.endsWith('"')||r.name.startsWith("`")&&r.name.endsWith("`")||t.isLoopVariable(r.name)||t.isContextBound(r.name)&&!t.isRootParam(r.name))return;const e=r.parent&&r.parent.type==="MemberExpression"&&r.parent.object===r&&t.isContextBound(r.name),n=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee&&r.parent.callee.type==="MemberExpression"&&r.parent.callee.property.name==="param";r.parent&&r.parent.type==="AssignmentExpression"&&r.parent.left;const a=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee&&r.parent.callee.type==="MemberExpression"&&t.isContextBound(r.parent.callee.object.name),o=r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed,i=r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed&&r.parent.property===r&&r.parent.parent&&r.parent.parent.type==="CallExpression"&&r.parent.parent.callee&&r.parent.parent.callee.type==="MemberExpression"&&t.isContextBound(r.parent.parent.callee.object.name),s=r.parent&&r.parent.type==="CallExpression"&&r.parent.callee===r;if(e||n||a||i||s){if(s)return;const[h,p]=t.getVariable(r.name);Object.assign(r,{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:p},computed:!1},property:{type:"Identifier",name:h},computed:!1});return}const[c,l]=t.getVariable(r.name),m={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:l},computed:!1},property:{type:"Identifier",name:c},computed:!1};!(r.parent&&r.parent.type==="MemberExpression"&&r.parent.computed&&r.parent.object===r)&&!o?Object.assign(r,{type:"MemberExpression",object:m,property:{type:"Literal",value:0},computed:!0}):Object.assign(r,m)}}function L(r,t){if(r.left.type==="Identifier"){const[e,n]=t.getVariable(r.left.name),a={type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1};r.left={type:"MemberExpression",object:a,property:{type:"Literal",value:0},computed:!0}}g.recursive(r.right,{parent:r.right,inNamespaceCall:!1},{Identifier(e,n,a){e.name=="na"&&(e.name="NaN"),e.parent=n.parent,E(e,t);const o=e.parent&&e.parent.type==="BinaryExpression",i=e.parent&&e.parent.type==="ConditionalExpression",s=t.isContextBound(e.name)&&!t.isRootParam(e.name),c=e.parent&&e.parent.type==="MemberExpression"&&e.parent.computed&&e.parent.object===e,l=e.parent&&e.parent._isParamCall,m=e.parent&&e.parent.type==="MemberExpression",h=e.name==="NaN";(s||i||o)&&(e.type==="MemberExpression"?N(e,t):e.type==="Identifier"&&!m&&!c&&!l&&!h&&$(e))},MemberExpression(e,n,a){N(e,t),e.object&&a(e.object,{parent:e,inNamespaceCall:n.inNamespaceCall})},CallExpression(e,n,a){const o=e.callee&&e.callee.type==="MemberExpression"&&e.callee.object&&e.callee.object.type==="Identifier"&&t.isContextBound(e.callee.object.name);S(e,t),e.arguments.forEach(i=>a(i,{parent:e,inNamespaceCall:o||n.inNamespaceCall}))}})}function X(r,t,e=!1){r.params.forEach(n=>{n.type==="Identifier"&&t.addContextBoundVar(n.name,e)})}function F(r,t){const e=t.getCurrentScopeType();r.argument&&(r.argument.type==="ArrayExpression"?(r.argument.elements=r.argument.elements.map(n=>{if(n.type==="Identifier"){if(t.isContextBound(n.name)&&!t.isRootParam(n.name))return{type:"MemberExpression",object:n,property:{type:"Literal",value:0},computed:!0};const[a,o]=t.getVariable(n.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1},property:{type:"Literal",value:0},computed:!0}}else if(n.type==="MemberExpression")return n.computed&&n.object.type==="Identifier"&&t.isContextBound(n.object.name)&&!t.isRootParam(n.object.name)||I(n,"",t),n;return n}),r.argument={type:"ArrayExpression",elements:[r.argument]}):r.argument.type==="BinaryExpression"?g.recursive(r.argument,t,{Identifier(n,a){E(n,a),n.type==="Identifier"&&$(n)},MemberExpression(n){I(n,"",t)}}):r.argument.type==="ObjectExpression"?r.argument.properties=r.argument.properties.map(n=>{if(n.shorthand){const[a,o]=t.getVariable(n.value.name);return{type:"Property",key:{type:"Identifier",name:n.key.name},value:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return n}):r.argument.type==="Identifier"&&(E(r.argument,t),r.argument.type==="Identifier"&&$(r.argument)),e==="fn"&&(r.argument.type==="Identifier"&&t.isContextBound(r.argument.name)&&!t.isRootParam(r.argument.name)?r.argument={type:"MemberExpression",object:r.argument,property:{type:"Literal",value:0},computed:!0}:r.argument.type==="MemberExpression"?r.argument.object.type==="Identifier"&&t.isContextBound(r.argument.object.name)&&!t.isRootParam(r.argument.object.name)&&(r.argument._indexTransformed||(N(r.argument,t),r.argument._indexTransformed=!0)):(r.argument.type==="BinaryExpression"||r.argument.type==="LogicalExpression"||r.argument.type==="ConditionalExpression"||r.argument.type==="CallExpression")&&g.recursive(r.argument,t,{Identifier(n,a){E(n,a),n.type==="Identifier"&&!n._arrayAccessed&&($(n),n._arrayAccessed=!0)},MemberExpression(n){I(n,"",t)},CallExpression(n,a){S(n,a)}}),r.argument={type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:"precision"}},arguments:[r.argument]}))}function V(r,t){if(r.type==="Identifier"){if(r.name==="na")return r.name="NaN",r;if(t.isLoopVariable(r.name))return r;if(t.isRootParam(r.name)){const[a,o]=t.getVariable(r.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1}}if(t.isContextBound(r.name))return r;const[e,n]=t.getVariable(r.name);return{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:n},computed:!1},property:{type:"Identifier",name:e},computed:!1}}return r}function U(r,t,e){const n=B(r.argument,t,e);return{type:"UnaryExpression",operator:r.operator,prefix:r.prefix,argument:n,start:r.start,end:r.end}}function B(r,t,e=""){switch(r.type){case"BinaryExpression":return W(r,t,e);case"MemberExpression":return{type:"MemberExpression",object:r.object.type==="Identifier"?V(r.object,t):r.object,property:r.property,computed:r.computed};case"Identifier":return t.isLoopVariable(r.name)||r.parent&&r.parent.type==="MemberExpression"&&r.parent.property===r?r:{type:"MemberExpression",object:V(r,t),property:{type:"Literal",value:0},computed:!0};case"UnaryExpression":return U(r,t,e)}return r}function W(r,t,e){const n=B(r.left,t,e),a=B(r.right,t,e),o={type:"BinaryExpression",operator:r.operator,left:n,right:a,start:r.start,end:r.end};return g.recursive(o,t,{CallExpression(i,s){i._transformed||S(i,s)},MemberExpression(i){I(i,"",t)}}),o}function Y(r,t,e){const n=B(r.left,t,e),a=B(r.right,t,e),o={type:"LogicalExpression",operator:r.operator,left:n,right:a,start:r.start,end:r.end};return g.recursive(o,t,{CallExpression(i,s){i._transformed||S(i,s)}}),o}function Z(r,t,e){return g.recursive(r,{parent:r,inNamespaceCall:!1},{Identifier(n,a,o){if(n.name=="NaN")return;if(n.name=="na"){n.name="NaN";return}n.parent=a.parent,E(n,t);const i=n.parent&&n.parent.type==="BinaryExpression";(n.parent&&n.parent.type==="ConditionalExpression"||i)&&(n.type==="MemberExpression"?N(n,t):n.type==="Identifier"&&$(n))},MemberExpression(n,a,o){N(n,t),n.object&&o(n.object,{parent:n,inNamespaceCall:a.inNamespaceCall})},CallExpression(n,a,o){const i=n.callee&&n.callee.type==="MemberExpression"&&n.callee.object&&n.callee.object.type==="Identifier"&&t.isContextBound(n.callee.object.name);S(n,t),n.arguments.forEach(s=>o(s,{parent:n,inNamespaceCall:i||a.inNamespaceCall}))}}),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:e},property:{type:"Identifier",name:"param"}},arguments:[r,k,t.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function O(r,t,e){switch(r?.type){case"BinaryExpression":r=W(r,e,t);break;case"LogicalExpression":r=Y(r,e,t);break;case"ConditionalExpression":return Z(r,e,t);case"UnaryExpression":r=U(r,e,t);break}if(r.type==="MemberExpression"&&r.computed&&r.property){const n=r.object.type==="Identifier"&&e.isContextBound(r.object.name)&&!e.isRootParam(r.object.name)?r.object:V(r.object,e),a=r.property.type==="Identifier"&&!e.isContextBound(r.property.name)&&!e.isLoopVariable(r.property.name)?V(r.property,e):r.property;return{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[n,a,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}if(r.type==="ObjectExpression"&&(r.properties=r.properties.map(n=>{if(n.value.name){const[a,o]=e.getVariable(n.value.name);return{type:"Property",key:{type:"Identifier",name:n.key.name},value:{type:"MemberExpression",object:{type:"MemberExpression",object:{type:"Identifier",name:b},property:{type:"Identifier",name:o},computed:!1},property:{type:"Identifier",name:a},computed:!1},kind:"init",method:!1,shorthand:!1,computed:!1}}return n})),r.type==="Identifier"){if(r.name==="na")return r.name="NaN",r;if(e.isContextBound(r.name)&&!e.isRootParam(r.name))return{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[r,k,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}return r?.type==="CallExpression"&&S(r,e),{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:t},property:{type:"Identifier",name:"param"},computed:!1},arguments:[r.type==="Identifier"?V(r,e):r,k,e.nextParamIdArg],_transformed:!0,_isParamCall:!0}}function S(r,t,e){if(!r._transformed){if(r.callee&&r.callee.type==="MemberExpression"&&r.callee.object&&r.callee.object.type==="Identifier"&&(t.isContextBound(r.callee.object.name)||r.callee.object.name==="math"||r.callee.object.name==="ta")){const n=r.callee.object.name;r.arguments=r.arguments.map(a=>a._isParamCall?a:O(a,n,t)),n==="ta"&&r.arguments.push(t.getNextTACallId()),r._transformed=!0}else r.callee&&r.callee.type==="Identifier"&&(r.arguments=r.arguments.map(n=>n._isParamCall?n:O(n,b,t)),r._transformed=!0);r.arguments.forEach(n=>{g.recursive(n,t,{Identifier(a,o,i){a.parent=o.parent,E(a,t);const s=a.parent&&a.parent.type==="BinaryExpression";(a.parent&&a.parent.type==="ConditionalExpression"||s)&&(a.type==="MemberExpression"?N(a,t):a.type==="Identifier"&&$(a))},CallExpression(a,o,i){a._transformed||S(a,o)},MemberExpression(a,o,i){I(a,"",t),a.object&&i(a.object,{parent:a,inNamespaceCall:o.inNamespaceCall})}})})}}function tt(r,t){const e=[];r.params.forEach(n=>{n.type==="Identifier"&&(t.addContextBoundVar(n.name,!1),e.push(n.name))}),r.body&&r.body.type==="BlockStatement"&&(t.pushScope("fn"),g.recursive(r.body,t,{BlockStatement(n,a,o){n.body.forEach(i=>o(i,a))},ReturnStatement(n,a){F(n,a)},VariableDeclaration(n,a){D(n,a)},Identifier(n,a){E(n,a)},CallExpression(n,a){S(n,a),n.arguments.forEach(o=>{o.type==="BinaryExpression"&&g.recursive(o,a,{CallExpression(i,s){S(i,s)},MemberExpression(i){I(i,"",a)}})})},MemberExpression(n){I(n,"",t)},AssignmentExpression(n,a){L(n,a)},ForStatement(n,a,o){q(n,a,o)},IfStatement(n,a,o){G(n,a,o)},BinaryExpression(n,a,o){g.recursive(n,a,{CallExpression(i,s){S(i,s)},MemberExpression(i){I(i,"",a)}})}}),t.popScope())}function $(r,t){Object.assign(r,{type:"MemberExpression",object:{type:"Identifier",name:r.name,start:r.start,end:r.end},property:{type:"Literal",value:0},computed:!0,_indexTransformed:!0})}function q(r,t,e){if(r.init&&r.init.type==="VariableDeclaration"){const n=r.init.declarations[0],a=n.id.name;t.addLoopVariable(a,a),r.init={type:"VariableDeclaration",kind:r.init.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:a},init:n.init}]},n.init&&g.recursive(n.init,t,{Identifier(o,i){t.isLoopVariable(o.name)||(t.pushScope("for"),E(o,i),t.popScope())},MemberExpression(o){t.pushScope("for"),I(o,"",t),t.popScope()}})}r.test&&g.recursive(r.test,t,{Identifier(n,a){!t.isLoopVariable(n.name)&&!n.computed&&(t.pushScope("for"),E(n,a),n.type==="Identifier"&&(n.computed=!0,$(n)),t.popScope())},MemberExpression(n){t.pushScope("for"),I(n,"",t),t.popScope()}}),r.update&&g.recursive(r.update,t,{Identifier(n,a){t.isLoopVariable(n.name)||(t.pushScope("for"),E(n,a),t.popScope())}}),t.pushScope("for"),e(r.body,t),t.popScope()}function et(r,t){g.recursive(r,t,{MemberExpression(e){I(e,"",t)},CallExpression(e,n){S(e,n)},Identifier(e,n){E(e,n);const a=t.getCurrentScopeType()==="if";t.isContextBound(e.name)&&!t.isRootParam(e.name)&&a&&$(e)}})}function G(r,t,e){r.test&&(t.pushScope("if"),et(r.test,t),t.popScope()),t.pushScope("if"),e(r.consequent,t),t.popScope(),r.alternate&&(t.pushScope("els"),e(r.alternate,t),t.popScope())}function nt(r){g.recursive(r,null,{VariableDeclaration(t,e,n){t.declarations&&t.declarations.length>0&&t.declarations.forEach(a=>{if(a.init&&a.init.type==="ArrowFunctionExpression"&&a.init.start!==0){const o={type:"FunctionDeclaration",id:a.id,params:a.init.params,body:a.init.body.type==="BlockStatement"?a.init.body:{type:"BlockStatement",body:[{type:"ReturnStatement",argument:a.init.body}]},async:a.init.async,generator:!1};Object.assign(t,o)}}),t.body&&t.body.body&&t.body.body.forEach(a=>n(a,e))}})}function rt(r,t){g.simple(r,{VariableDeclaration(e){e.declarations.forEach(n=>{const a=n.init&&n.init.type==="MemberExpression"&&n.init.object&&(n.init.object.name==="context"||n.init.object.name===b||n.init.object.name==="context2"),o=n.init&&n.init.type==="MemberExpression"&&n.init.object?.object&&(n.init.object.object.name==="context"||n.init.object.object.name===b||n.init.object.object.name==="context2");(a||o)&&(n.id.name&&t.addContextBoundVar(n.id.name),n.id.properties&&n.id.properties.forEach(i=>{i.key.name&&t.addContextBoundVar(i.key.name)}))})}})}function at(r){let t=typeof r=="function"?r.toString():r;const e=K.parse(t.trim(),{ecmaVersion:"latest",sourceType:"module"});nt(e);const n=new J;let a;rt(e,n),g.simple(e,{FunctionDeclaration(i){tt(i,n)},ArrowFunctionExpression(i){const s=i.start===0;s&&i.params&&i.params.length>0&&(a=i.params[0].name,i.params[0].name=b),X(i,n,s)},VariableDeclaration(i){i.declarations.forEach(s=>{if(s.id.type==="ArrayPattern"){const c=n.generateTempVar(),l={type:"VariableDeclaration",kind:i.kind,declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:c},init:s.init}]};s.id.elements?.forEach(h=>{h.type==="Identifier"&&n.addArrayPatternElement(h.name)});const m=s.id.elements.map((h,p)=>({type:"VariableDeclaration",kind:i.kind,declarations:[{type:"VariableDeclarator",id:h,init:{type:"MemberExpression",object:{type:"Identifier",name:c},property:{type:"Literal",value:p},computed:!0}}]}));Object.assign(i,{type:"BlockStatement",body:[l,...m]})}})},ForStatement(i){}}),g.recursive(e,n,{BlockStatement(i,s,c){i.body.forEach(l=>c(l,s))},ReturnStatement(i,s){F(i,s)},VariableDeclaration(i,s){D(i,s)},Identifier(i,s){E(i,s)},CallExpression(i,s){S(i,s)},MemberExpression(i){I(i,a,n)},AssignmentExpression(i,s){L(i,s)},FunctionDeclaration(i,s){},ForStatement(i,s,c){q(i,s,c)},IfStatement(i,s,c){G(i,s,c)}}),it(e);const o=z.generate(e);return new Function("",`return ${o}`)(this)}function it(r){g.simple(r,{BinaryExpression(t){if(t.operator==="=="||t.operator==="==="){const e=t.left,n=t.right;Object.assign(t,{type:"CallExpression",callee:{type:"MemberExpression",object:{type:"Identifier",name:"$.math"},property:{type:"Identifier",name:"__eq"},computed:!1},arguments:[e,n],_transformed:!0})}}})}var ot=Object.defineProperty,st=(r,t,e)=>t in r?ot(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,A=(r,t,e)=>st(r,typeof t!="symbol"?t+"":t,e);const pt=5e3;class ct{constructor(t,e,n,a,o,i){this.source=t,this.tickerId=e,this.timeframe=n,this.limit=a,this.sDate=o,this.eDate=i,A(this,"data",[]),A(this,"open",[]),A(this,"high",[]),A(this,"low",[]),A(this,"close",[]),A(this,"volume",[]),A(this,"hl2",[]),A(this,"hlc3",[]),A(this,"ohlc4",[]),A(this,"openTime",[]),A(this,"closeTime",[]),A(this,"_periods"),A(this,"_readyPromise",null),A(this,"_ready",!1),this._readyPromise=new Promise(s=>{this.loadMarketData(t,e,n,a,o,i).then(c=>{const l=c.slice(0,pt);this._periods=l.length,this.data=l;const m=l.map(d=>d.open),h=l.map(d=>d.close),p=l.map(d=>d.high),u=l.map(d=>d.low),y=l.map(d=>d.volume),x=l.map(d=>(d.high+d.low+d.close)/3),w=l.map(d=>(d.high+d.low)/2),M=l.map(d=>(d.high+d.low+d.open+d.close)/4),_=l.map(d=>d.openTime),C=l.map(d=>d.closeTime);this.open=m,this.close=h,this.high=p,this.low=u,this.volume=y,this.hl2=w,this.hlc3=x,this.ohlc4=M,this.openTime=_,this.closeTime=C,this._ready=!0,s(!0)})})}get periods(){return this._periods}async loadMarketData(t,e,n,a,o,i){return Array.isArray(t)?t:t.getMarketData(e,n,a,o,i)}async ready(){if(this._ready)return!0;if(!this._readyPromise)throw new Error("PineTS is not ready");return this._readyPromise}async run(t,e,n){await this.ready(),e||(e=this._periods);const a=new Vt({marketData:this.data,source:this.source,tickerId:this.tickerId,timeframe:this.timeframe,limit:this.limit,sDate:this.sDate,eDate:this.eDate});a.pineTSCode=t,a.useTACache=n;let o=at.bind(this)(t);a.data.close=[],a.data.open=[],a.data.high=[],a.data.low=[],a.data.volume=[],a.data.hl2=[],a.data.hlc3=[],a.data.ohlc4=[],a.data.openTime=[],a.data.closeTime=[];const i=["const","var","let","params"];for(let s=this._periods-e;s<this._periods;s++){a.idx=s,a.data.close.unshift(this.close[s]),a.data.open.unshift(this.open[s]),a.data.high.unshift(this.high[s]),a.data.low.unshift(this.low[s]),a.data.volume.unshift(this.volume[s]),a.data.hl2.unshift(this.hl2[s]),a.data.hlc3.unshift(this.hlc3[s]),a.data.ohlc4.unshift(this.ohlc4[s]),a.data.openTime.unshift(this.openTime[s]);const c=await o(a);if(typeof c=="object"){typeof a.result!="object"&&(a.result={});for(let l in c){a.result[l]===void 0&&(a.result[l]=[]);const m=Array.isArray(c[l])?c[l][0]:c[l];a.result[l].push(m)}}else Array.isArray(a.result)||(a.result=[]),a.result.push(c);for(let l of i)for(let m in a[l])if(Array.isArray(a[l][m])){const h=a[l][m][0];a[l][m].unshift(h)}}return a}}var lt=Object.defineProperty,mt=(r,t,e)=>t in r?lt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,ht=(r,t,e)=>mt(r,typeof t!="symbol"?t+"":t,e);class ut{constructor(t){this.context=t,ht(this,"color",{param:(e,n=0)=>Array.isArray(e)?e[n]:e,rgb:(e,n,a,o)=>o?`rgba(${e}, ${n}, ${a}, ${o})`:`rgb(${e}, ${n}, ${a})`,new:(e,n)=>{if(e&&e.startsWith("#")){const a=e.slice(1),o=parseInt(a.slice(0,2),16),i=parseInt(a.slice(2,4),16),s=parseInt(a.slice(4,6),16);return n?`rgba(${o}, ${i}, ${s}, ${n})`:`rgb(${o}, ${i}, ${s})`}return n?`rgba(${e}, ${n})`:e},white:"white",lime:"lime",green:"green",red:"red",maroon:"maroon",black:"black",gray:"gray",blue:"blue"})}extractPlotOptions(t){const e={};for(let n in t)Array.isArray(t[n])?e[n]=t[n][0]:e[n]=t[n];return e}indicator(t,e,n){}plotchar(t,e,n){this.context.plots[e]||(this.context.plots[e]={data:[],options:this.extractPlotOptions(n),title:e}),this.context.plots[e].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:t[0],options:{...this.extractPlotOptions(n),style:"char"}})}plot(t,e,n){this.context.plots[e]||(this.context.plots[e]={data:[],options:this.extractPlotOptions(n),title:e}),this.context.plots[e].data.push({time:this.context.marketData[this.context.marketData.length-this.context.idx-1].openTime,value:t[0],options:this.extractPlotOptions(n)})}na(t){return Array.isArray(t)?isNaN(t[0]):isNaN(t)}nz(t,e=0){const n=Array.isArray(t)?t[0]:t,a=Array.isArray(t)?e[0]:e;return isNaN(n)?a:n}}class yt{constructor(t){this.context=t}param(t,e=0){return Array.isArray(t)?[t[e]]:[t]}any(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}int(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}float(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}bool(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}string(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}timeframe(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}time(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}price(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}session(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}source(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}symbol(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}text_area(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}enum(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}color(t,{title:e,group:n}={}){return Array.isArray(t)?t[0]:t}}var dt=Object.defineProperty,xt=(r,t,e)=>t in r?dt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,ft=(r,t,e)=>xt(r,typeof t!="symbol"?t+"":t,e);class bt{constructor(t){this.context=t,ft(this,"_cache",{})}param(t,e,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(t)?e?(this.context.params[n]=t.slice(e),this.context.params[n].length=t.length,this.context.params[n]):(this.context.params[n]=t.slice(0),this.context.params[n]):(this.context.params[n][0]=t,this.context.params[n])}__eq(t,e){return Math.abs(t-e)<1e-8}abs(t){return Math.abs(t[0])}pow(t,e){return Math.pow(t[0],e[0])}sqrt(t){return Math.sqrt(t[0])}log(t){return Math.log(t[0])}ln(t){return Math.log(t[0])}exp(t){return Math.exp(t[0])}floor(t){return Math.floor(t[0])}ceil(t){return Math.ceil(t[0])}round(t){return Math.round(t[0])}random(){return Math.random()}max(...t){const e=t.map(n=>Array.isArray(n)?n[0]:n);return Math.max(...e)}min(...t){const e=t.map(n=>Array.isArray(n)?n[0]:n);return Math.min(...e)}sum(t,e){const n=Array.isArray(e)?e[0]:e;return Array.isArray(t)?t.slice(0,n).reduce((a,o)=>a+o,0):t}sin(t){return Math.sin(t[0])}cos(t){return Math.cos(t[0])}tan(t){return Math.tan(t[0])}acos(t){return Math.acos(t[0])}asin(t){return Math.asin(t[0])}atan(t){return Math.atan(t[0])}avg(...t){const e=t.map(n=>Array.isArray(n)?n[0]:n);return e.reduce((n,a)=>{const o=Array.isArray(n)?n[0]:n,i=Array.isArray(a)?a[0]:a;return o+i},0)/e.length}}var gt=Object.defineProperty,wt=(r,t,e)=>t in r?gt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,At=(r,t,e)=>wt(r,typeof t!="symbol"?t+"":t,e);const R=["1","3","5","15","30","45","60","120","180","240","D","W","M"];class vt{constructor(t){this.context=t,At(this,"_cache",{})}param(t,e,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(t)?(e?this.context.params[n]=t.slice(e):this.context.params[n]=t.slice(0),[t[e],n]):(this.context.params[n][0]=t,[t,n])}async security(t,e,n,a=!1,o=!1,i=!1,s=null,c=null){const l=t[0],m=e[0],h=n[0],p=n[1],u=R.indexOf(this.context.timeframe),y=R.indexOf(m);if(u==-1||y==-1)throw new Error("Invalid timeframe");if(u>y)throw new Error("Only higher timeframes are supported for now");if(u===y)return h;const x=this.context.data.openTime[0],w=this.context.data.closeTime[0];if(this.context.cache[p]){const C=this.context.cache[p],d=this._findSecContextIdx(x,w,C.data.openTime,C.data.closeTime,o);return d==-1?NaN:C.params[p][d]}const M=await new ct(this.context.source,l,m,this.context.limit||1e3,this.context.sDate,this.context.eDate).run(this.context.pineTSCode);this.context.cache[p]=M;const _=this._findSecContextIdx(x,w,M.data.openTime,M.data.closeTime,o);return _==-1?NaN:M.params[p][_]}_findSecContextIdx(t,e,n,a,o=!1){for(let i=0;i<n.length;i++)if(n[i]<=t&&e<=a[i])return i+(o?1:2);return-1}}class Et{constructor(t){this.context=t}get tr(){return this.context.math.max(this.context.data.high[0]-this.context.data.low[0],this.context.math.abs(this.context.data.high[0]-this.context.data.close[1]),this.context.math.abs(this.context.data.low[0]-this.context.data.close[1]))}param(t,e,n){return this.context.params[n]||(this.context.params[n]=[]),Array.isArray(t)?e?(this.context.params[n]=t.slice(e),this.context.params[n].length=t.length,this.context.params[n]):(this.context.params[n]=t.slice(0),this.context.params[n]):(this.context.params[n][0]=t,this.context.params[n])}ema(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`ema_${a}`;this.context.taState[o]||(this.context.taState[o]={prevEma:null,initSum:0,initCount:0});const i=this.context.taState[o],s=t[0];if(i.initCount<a)return i.initSum+=s,i.initCount++,i.initCount===a?(i.prevEma=i.initSum/a,this.context.precision(i.prevEma)):NaN;const c=2/(a+1),l=s*c+i.prevEma*(1-c);return i.prevEma=l,this.context.precision(l)}sma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`sma_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[],sum:0});const i=this.context.taState[o],s=t[0]||0;if(i.window.unshift(s),i.sum+=s,i.window.length<a)return NaN;if(i.window.length>a){const l=i.window.pop();i.sum-=l}const c=i.sum/a;return this.context.precision(c)}vwma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`vwma_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[],volumeWindow:[]});const i=this.context.taState[o],s=t[0],c=this.context.data.volume[0];if(i.window.unshift(s),i.volumeWindow.unshift(c),i.window.length<a)return NaN;i.window.length>a&&(i.window.pop(),i.volumeWindow.pop());let l=0,m=0;for(let p=0;p<a;p++)l+=i.window[p]*i.volumeWindow[p],m+=i.volumeWindow[p];const h=l/m;return this.context.precision(h)}wma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`wma_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();let c=0,l=0;for(let h=0;h<a;h++){const p=a-h;c+=i.window[h]*p,l+=p}const m=c/l;return this.context.precision(m)}hma(t,e,n){const a=Array.isArray(e)?e[0]:e,o=Math.floor(a/2),i=Math.floor(Math.sqrt(a)),s=this.wma(t,o,n?`${n}_wma1`:void 0),c=this.wma(t,a,n?`${n}_wma2`:void 0);if(isNaN(s)||isNaN(c))return NaN;this.context.taState||(this.context.taState={});const l=n||`hma_raw_${a}`;this.context.taState[l]||(this.context.taState[l]=[]);const m=2*s-c;this.context.taState[l].unshift(m);const h=n?`${n}_hma_final`:`hma_final_${a}`;this.context.taState[h]||(this.context.taState[h]={window:[]});const p=this.context.taState[h];if(p.window.unshift(m),p.window.length<i)return NaN;p.window.length>i&&p.window.pop();let u=0,y=0;for(let w=0;w<i;w++){const M=i-w;u+=p.window[w]*M,y+=M}const x=u/y;return this.context.precision(x)}rma(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`rma_${a}`;this.context.taState[o]||(this.context.taState[o]={prevRma:null,initSum:0,initCount:0});const i=this.context.taState[o],s=t[0]||0;if(i.initCount<a)return i.initSum+=s,i.initCount++,i.initCount===a?(i.prevRma=i.initSum/a,this.context.precision(i.prevRma)):NaN;const c=1/a,l=s*c+i.prevRma*(1-c);return i.prevRma=l,this.context.precision(l)}change(t,e=1,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`change_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<=a)return NaN;i.window.length>a+1&&i.window.pop();const c=s-i.window[a];return this.context.precision(c)}rsi(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`rsi_${a}`;this.context.taState[o]||(this.context.taState[o]={prevValue:null,avgGain:0,avgLoss:0,initGains:[],initLosses:[]});const i=this.context.taState[o],s=t[0];if(i.prevValue!==null){const c=s-i.prevValue,l=c>0?c:0,m=c<0?-c:0;if(i.initGains.length<a){if(i.initGains.push(l),i.initLosses.push(m),i.initGains.length===a){i.avgGain=i.initGains.reduce((u,y)=>u+y,0)/a,i.avgLoss=i.initLosses.reduce((u,y)=>u+y,0)/a,i.prevValue=s;const p=i.avgLoss===0?100:100-100/(1+i.avgGain/i.avgLoss);return this.context.precision(p)}return i.prevValue=s,NaN}i.avgGain=(i.avgGain*(a-1)+l)/a,i.avgLoss=(i.avgLoss*(a-1)+m)/a;const h=i.avgLoss===0?100:100-100/(1+i.avgGain/i.avgLoss);return i.prevValue=s,this.context.precision(h)}return i.prevValue=s,NaN}atr(t,e){const n=Array.isArray(t)?t[0]:t;this.context.taState||(this.context.taState={});const a=e||`atr_${n}`;this.context.taState[a]||(this.context.taState[a]={prevAtr:null,initSum:0,initCount:0,prevClose:null});const o=this.context.taState[a],i=this.context.data.high[0],s=this.context.data.low[0],c=this.context.data.close[0];let l;if(o.prevClose!==null){const h=i-s,p=Math.abs(i-o.prevClose),u=Math.abs(s-o.prevClose);l=Math.max(h,p,u)}else l=i-s;if(o.prevClose=c,o.initCount<n)return o.initSum+=l,o.initCount++,o.initCount===n?(o.prevAtr=o.initSum/n,this.context.precision(o.prevAtr)):NaN;const m=(o.prevAtr*(n-1)+l)/n;return o.prevAtr=m,this.context.precision(m)}mom(t,e,n){const a=Array.isArray(e)?e[0]:e;return this.change(t,a)}roc(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`roc_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<=a)return NaN;i.window.length>a+1&&i.window.pop();const c=i.window[a],l=(s-c)/c*100;return this.context.precision(l)}dev(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`dev_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[],sum:0});const i=this.context.taState[o],s=t[0]||0;if(i.window.unshift(s),i.sum+=s,i.window.length<a)return NaN;if(i.window.length>a){const h=i.window.pop();i.sum-=h}const c=i.sum/a;let l=0;for(let h=0;h<a;h++)l+=Math.abs(i.window[h]-c);const m=l/a;return this.context.precision(m)}variance(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`variance_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();let c=0,l=0;for(let p=0;p<a;p++)c+=i.window[p],l+=i.window[p]*i.window[p];const m=c/a,h=l/a-m*m;return this.context.precision(h)}highest(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`highest_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=Math.max(...i.window.filter(l=>!isNaN(l)));return this.context.precision(c)}lowest(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`lowest_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=i.window.filter(m=>!isNaN(m)&&m!==void 0),l=c.length>0?Math.min(...c):NaN;return this.context.precision(l)}median(t,e,n){const a=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const o=n||`median_${a}`;this.context.taState[o]||(this.context.taState[o]={window:[]});const i=this.context.taState[o],s=t[0];if(i.window.unshift(s),i.window.length<a)return NaN;i.window.length>a&&i.window.pop();const c=i.window.slice().sort((h,p)=>h-p),l=Math.floor(a/2),m=a%2===0?(c[l-1]+c[l])/2:c[l];return this.context.precision(m)}stdev(t,e,n=!0,a){const o=Array.isArray(e)?e[0]:e,i=Array.isArray(n)?n[0]:n;this.context.taState||(this.context.taState={});const s=a||`stdev_${o}_${i}`;this.context.taState[s]||(this.context.taState[s]={window:[],sum:0});const c=this.context.taState[s],l=t[0];if(c.window.unshift(l),c.sum+=l,c.window.length<o)return NaN;if(c.window.length>o){const y=c.window.pop();c.sum-=y}const m=c.sum/o;let h=0;for(let y=0;y<o;y++)h+=Math.pow(c.window[y]-m,2);const p=i?o:o-1,u=Math.sqrt(h/p);return this.context.precision(u)}linreg(t,e,n,a){const o=Array.isArray(e)?e[0]:e,i=Array.isArray(n)?n[0]:n;this.context.taState||(this.context.taState={});const s=a||`linreg_${o}_${i}`;this.context.taState[s]||(this.context.taState[s]={window:[]});const c=this.context.taState[s],l=t[0];if(c.window.unshift(l),c.window.length<o)return NaN;c.window.length>o&&c.window.pop();let m=0,h=0,p=0,u=0;const y=o;for(let _=0;_<o;_++){const C=o-1-_,d=c.window[_];m+=C,h+=d,p+=C*d,u+=C*C}const x=y*u-m*m;if(x===0)return NaN;const w=(y*p-m*h)/x,M=(h-w*m)/y+w*(o-1-i);return this.context.precision(M)}supertrend(t,e,n){const a=Array.isArray(t)?t[0]:t,o=Array.isArray(e)?e[0]:e;this.context.taState||(this.context.taState={});const i=`supertrend_${a}_${o}`;this.context.taState[i]||(this.context.taState[i]={prevUpperBand:null,prevLowerBand:null,prevSupertrend:null,prevDirection:null});const s=this.context.taState[i],c=this.context.data.high[0],l=this.context.data.low[0],m=this.context.data.close[0],h=this.atr(o,n?`${n}_atr`:void 0);if(isNaN(h))return[[NaN,0]];const p=(c+l)/2;let u=p+a*h,y=p-a*h;s.prevUpperBand!==null&&(u<s.prevUpperBand||this.context.data.close[1]>s.prevUpperBand?u=u:u=s.prevUpperBand,y>s.prevLowerBand||this.context.data.close[1]<s.prevLowerBand?y=y:y=s.prevLowerBand);let x,w;return s.prevSupertrend===null?(x=m<=u?-1:1,w=x===-1?u:y):s.prevSupertrend===s.prevUpperBand?m>u?(x=1,w=y):(x=-1,w=u):m<y?(x=-1,w=u):(x=1,w=y),s.prevUpperBand=u,s.prevLowerBand=y,s.prevSupertrend=w,s.prevDirection=x,[[this.context.precision(w),x]]}crossover(t,e){const n=Array.isArray(t)?t[0]:t,a=Array.isArray(e)?e[0]:e,o=Array.isArray(t)?t[1]:this.context.data.series[t][1],i=Array.isArray(e)?e[1]:this.context.data.series[e][1];return o<i&&n>a}crossunder(t,e){const n=Array.isArray(t)?t[0]:t,a=Array.isArray(e)?e[0]:e,o=Array.isArray(t)?t[1]:this.context.data.series[t][1],i=Array.isArray(e)?e[1]:this.context.data.series[e][1];return o>i&&n<a}pivothigh(t,e,n){n==null&&(n=e,e=t,t=this.context.data.high);const a=Array.isArray(e)?e[0]:e,o=Array.isArray(n)?n[0]:n,i=St(t.slice(0).reverse(),a,o),s=this.context.idx;return this.context.precision(i[s])}pivotlow(t,e,n){n==null&&(n=e,e=t,t=this.context.data.low);const a=Array.isArray(e)?e[0]:e,o=Array.isArray(n)?n[0]:n,i=jt(t.slice(0).reverse(),a,o),s=this.context.idx;return this.context.precision(i[s])}}function St(r,t,e){const n=new Array(r.length).fill(NaN);for(let a=t+e;a<r.length;a++){const o=r[a-e];let i=!0;for(let s=1;s<=t;s++)if(r[a-e-s]>=o){i=!1;break}if(i){for(let s=1;s<=e;s++)if(r[a-e+s]>=o){i=!1;break}}i&&(n[a]=o)}return n}function jt(r,t,e){const n=new Array(r.length).fill(NaN);for(let a=t+e;a<r.length;a++){const o=r[a-e];let i=!0;for(let s=1;s<=t;s++)if(r[a-e-s]<=o){i=!1;break}if(i){for(let s=1;s<=e;s++)if(r[a-e+s]<=o){i=!1;break}}i&&(n[a]=o)}return n}var It=Object.defineProperty,Mt=(r,t,e)=>t in r?It(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,Ct=(r,t,e)=>Mt(r,typeof t!="symbol"?t+"":t,e);class v{constructor(t){this.array=t}}class Nt{constructor(t){this.context=t,Ct(this,"_cache",{})}param(t,e=0){return Array.isArray(t)?t[e]:t}get(t,e){return t.array[e]}set(t,e,n){t.array[e]=n}push(t,e){t.array.push(e)}sum(t){return t.array.reduce((e,n)=>e+(isNaN(n)?0:n),0)}avg(t){return this.sum(t)/t.array.length}min(t,e=0){return[...t.array].sort((n,a)=>n-a)[e]??this.context.NA}max(t,e=0){return[...t.array].sort((n,a)=>a-n)[e]??this.context.NA}size(t){return t.array.length}new_bool(t,e=!1){return new v(Array(t).fill(e))}new_float(t,e=NaN){return new v(Array(t).fill(e))}new_int(t,e=0){return new v(Array(t).fill(Math.round(e)))}new_string(t,e=""){return new v(Array(t).fill(e))}new(t,e){return new v(Array(t).fill(e))}slice(t,e,n){const a=n!==void 0?n+1:void 0;return new v(t.array.slice(e,a))}reverse(t){t.array.reverse()}includes(t,e){return t.array.includes(e)}indexof(t,e){return t.array.indexOf(e)}lastindexof(t,e){return t.array.lastIndexOf(e)}stdev(t,e=!0){const n=this.avg(t),a=t.array.map(i=>Math.pow(i-n,2)),o=e?t.array.length:t.array.length-1;return Math.sqrt(this.sum(new v(a))/o)}variance(t,e=!0){const n=this.avg(t),a=t.array.map(i=>Math.pow(i-n,2)),o=e?t.array.length:t.array.length-1;return this.sum(new v(a))/o}covariance(t,e,n=!0){if(t.array.length!==e.array.length||t.array.length<2)return NaN;const a=n?t.array.length:t.array.length-1,o=this.avg(t),i=this.avg(e);let s=0;for(let c=0;c<t.array.length;c++)s+=(t.array[c]-o)*(e.array[c]-i);return s/a}first(t){return t.array.length>0?t.array[0]:this.context.NA}last(t){return t.array.length>0?t.array[t.array.length-1]:this.context.NA}clear(t){t.array.length=0}join(t,e=","){return t.array.join(e)}abs(t){return new v(t.array.map(e=>Math.abs(e)))}concat(t,e){return t.array.push(...e.array),t}copy(t){return new v([...t.array])}every(t,e){return t.array.every(e)}fill(t,e,n=0,a){const o=t.array.length,i=a!==void 0?Math.min(a,o):o;for(let s=n;s<i;s++)t.array[s]=e}from(t){return new v([...t])}insert(t,e,n){t.array.splice(e,0,n)}pop(t){return t.array.pop()}range(t){return this.max(t)-this.min(t)}remove(t,e){return e>=0&&e<t.array.length?t.array.splice(e,1)[0]:this.context.NA}shift(t){return t.array.shift()}sort(t,e="asc"){t.array.sort((n,a)=>e==="asc"?n-a:a-n)}sort_indices(t,e){const n=t.array.map((a,o)=>o);return n.sort((a,o)=>{const i=t.array[a],s=t.array[o];return e?e(i,s):i-s}),new v(n)}standardize(t){const e=this.avg(t),n=this.stdev(t);return n===0?new v(t.array.map(()=>0)):new v(t.array.map(a=>(a-e)/n))}unshift(t,e){t.array.unshift(e)}some(t,e){return t.array.some(e)}}var _t=Object.defineProperty,$t=(r,t,e)=>t in r?_t(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,f=(r,t,e)=>$t(r,typeof t!="symbol"?t+"":t,e);class Vt{constructor({marketData:t,source:e,tickerId:n,timeframe:a,limit:o,sDate:i,eDate:s}){f(this,"data",{open:[],high:[],low:[],close:[],volume:[],hl2:[],hlc3:[],ohlc4:[]}),f(this,"cache",{}),f(this,"taState",{}),f(this,"useTACache",!1),f(this,"NA",NaN),f(this,"math"),f(this,"ta"),f(this,"input"),f(this,"request"),f(this,"array"),f(this,"core"),f(this,"lang"),f(this,"idx",0),f(this,"params",{}),f(this,"const",{}),f(this,"var",{}),f(this,"let",{}),f(this,"result"),f(this,"plots",{}),f(this,"marketData"),f(this,"source"),f(this,"tickerId"),f(this,"timeframe",""),f(this,"limit"),f(this,"sDate"),f(this,"eDate"),f(this,"pineTSCode"),this.marketData=t,this.source=e,this.tickerId=n,this.timeframe=a,this.limit=o,this.sDate=i,this.eDate=s,this.math=new bt(this),this.ta=new Et(this),this.input=new yt(this),this.request=new vt(this),this.array=new Nt(this);const c=new ut(this);this.core={plotchar:c.plotchar.bind(c),na:c.na.bind(c),color:c.color,plot:c.plot.bind(c),nz:c.nz.bind(c)}}init(t,e,n=0){return t?!Array.isArray(e)||Array.isArray(e[0])?t[0]=Array.isArray(e?.[0])?e[0]:this.precision(e):t[0]=this.precision(e[e.length-this.idx-1+n]):Array.isArray(e)?t=[this.precision(e[e.length-this.idx-1+n])]:t=[this.precision(e)],t}precision(t,e=10){return typeof t!="number"||isNaN(t)?t:Number(t.toFixed(e))}param(t,e,n){return typeof t=="string"||!Array.isArray(t)&&typeof t=="object"?t:(this.params[n]||(this.params[n]=[]),Array.isArray(t)?e?(this.params[n]=t.slice(e),this.params[n].length=t.length,this.params[n]):(this.params[n]=t.slice(0),this.params[n]):(this.params[n][0]=t,this.params[n]))}}var Bt=Object.defineProperty,Pt=(r,t,e)=>t in r?Bt(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,T=(r,t,e)=>Pt(r,typeof t!="symbol"?t+"":t,e);const kt="https://api.binance.com/api/v3",P={1:"1m",3:"3m",5:"5m",15:"15m",30:"30m",45:null,60:"1h",120:"2h",180:null,240:"4h","4H":"4h","1D":"1d",D:"1d","1W":"1w",W:"1w","1M":"1M",M:"1M"};class Tt{constructor(t=5*60*1e3){T(this,"cache"),T(this,"cacheDuration"),this.cache=new Map,this.cacheDuration=t}generateKey(t){return Object.entries(t).filter(([e,n])=>n!==void 0).map(([e,n])=>`${e}:${n}`).join("|")}get(t){const e=this.generateKey(t),n=this.cache.get(e);return n?Date.now()-n.timestamp>this.cacheDuration?(this.cache.delete(e),null):n.data:null}set(t,e){const n=this.generateKey(t);this.cache.set(n,{data:e,timestamp:Date.now()})}clear(){this.cache.clear()}cleanup(){const t=Date.now();for(const[e,n]of this.cache.entries())t-n.timestamp>this.cacheDuration&&this.cache.delete(e)}}class Dt{constructor(){T(this,"cacheManager"),this.cacheManager=new Tt(5*60*1e3)}async getMarketDataInterval(t,e,n,a){try{const o=P[e.toUpperCase()];if(!o)return console.error(`Unsupported timeframe: ${e}`),[];const i={"1m":60*1e3,"3m":3*60*1e3,"5m":5*60*1e3,"15m":15*60*1e3,"30m":30*60*1e3,"1h":60*60*1e3,"2h":2*60*60*1e3,"4h":4*60*60*1e3,"1d":24*60*60*1e3,"1w":7*24*60*60*1e3,"1M":30*24*60*60*1e3};let s=[],c=n;const l=a,m=i[o];if(!m)return console.error(`Duration not defined for interval: ${o}`),[];for(;c<l;){const h=Math.min(c+1e3*m,l),p=await this.getMarketData(t,e,1e3,c,h);if(p.length===0)break;s=s.concat(p),c=p[p.length-1].closeTime+1}return s}catch(o){return console.error("Error in getMarketDataInterval:",o),[]}}async getMarketData(t,e,n,a,o){try{const i={tickerId:t,timeframe:e,limit:n,sDate:a,eDate:o},s=this.cacheManager.get(i);if(s)return console.log("cache hit",t,e,n,a,o),s;const c=P[e.toUpperCase()];if(!c)return console.error(`Unsupported timeframe: ${e}`),[];if(this.shouldPaginate(e,n,a,o)&&a&&o){const p=await this.getMarketDataInterval(t,e,a,o),u=n?p.slice(0,n):p;return this.cacheManager.set(i,u),u}let l=`${kt}/klines?symbol=${t}&interval=${c}`;n&&(l+=`&limit=${Math.min(n,1e3)}`),a&&(l+=`&startTime=${a}`),o&&(l+=`&endTime=${o}`);const m=await fetch(l);if(!m.ok)throw new Error(`HTTP error! status: ${m.status}`);const h=(await m.json()).map(p=>({openTime:parseInt(p[0]),open:parseFloat(p[1]),high:parseFloat(p[2]),low:parseFloat(p[3]),close:parseFloat(p[4]),volume:parseFloat(p[5]),closeTime:parseInt(p[6]),quoteAssetVolume:parseFloat(p[7]),numberOfTrades:parseInt(p[8]),takerBuyBaseAssetVolume:parseFloat(p[9]),takerBuyQuoteAssetVolume:parseFloat(p[10]),ignore:p[11]}));return this.cacheManager.set(i,h),h}catch(i){return console.error("Error in binance.klines:",i),[]}}shouldPaginate(t,e,n,a){if(e&&e>1e3)return!0;if(n&&a){const o=P[t.toUpperCase()],i={"1m":60*1e3,"3m":3*60*1e3,"5m":5*60*1e3,"15m":15*60*1e3,"30m":30*60*1e3,"1h":60*60*1e3,"2h":2*60*60*1e3,"4h":4*60*60*1e3,"1d":24*60*60*1e3,"1w":7*24*60*60*1e3,"1M":30*24*60*60*1e3}[o];if(i)return Math.ceil((a-n)/i)>1e3}return!1}}const Lt={Binance:new Dt};export{Vt as Context,ct as PineTS,Lt as Provider};
|