@tradecanvas/core 0.1.1 โ†’ 0.1.3

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/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@tradecanvas/commons`);var t=class{canvas;ctx;dpr=1;constructor(e,t){this.canvas=document.createElement(`canvas`),this.canvas.style.position=`absolute`,this.canvas.style.top=`0`,this.canvas.style.left=`0`,this.canvas.style.zIndex=String(t),this.canvas.style.pointerEvents=`none`,this.ctx=this.canvas.getContext(`2d`),e.appendChild(this.canvas)}resize(e,t){this.dpr=t;let n=Math.round(e.width*t),r=Math.round(e.height*t);(this.canvas.width!==n||this.canvas.height!==r)&&(this.canvas.width=n,this.canvas.height=r),this.canvas.style.width=`${e.width}px`,this.canvas.style.height=`${e.height}px`,this.ctx.setTransform(t,0,0,t,0,0)}clear(){this.ctx.setTransform(1,0,0,1,0,0),this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.setTransform(this.dpr,0,0,this.dpr,0,0)}getSize(){return{width:this.canvas.width/this.dpr,height:this.canvas.height/this.dpr}}destroy(){this.canvas.remove()}},n=class{layers=new Map;constructor(e){this.container=e}createLayers(){this.destroy();for(let n of[e.LayerType.Background,e.LayerType.Main,e.LayerType.Overlay,e.LayerType.UI])this.layers.set(n,new t(this.container,n))}getLayer(e){return this.layers.get(e)}resize(e,t){for(let n of this.layers.values())n.resize(e,t)}destroy(){for(let e of this.layers.values())e.destroy();this.layers.clear()}},r=class{animFrameId=0;running=!1;dirtyLayers=new Set;swapLayers=new Set;callback=null;setCallback(e){this.callback=e}markDirty(e){this.dirtyLayers.add(e),this.running&&!this.animFrameId&&this.scheduleFrame()}markAllDirty(){this.dirtyLayers.add(e.LayerType.Background),this.dirtyLayers.add(e.LayerType.Main),this.dirtyLayers.add(e.LayerType.Overlay),this.dirtyLayers.add(e.LayerType.UI),this.running&&!this.animFrameId&&this.scheduleFrame()}start(){this.running=!0,this.markAllDirty()}stop(){this.running=!1,this.animFrameId&&=(cancelAnimationFrame(this.animFrameId),0)}scheduleFrame(){this.animFrameId=requestAnimationFrame(()=>{if(this.animFrameId=0,!(!this.running||!this.callback)&&this.dirtyLayers.size>0){let e=this.dirtyLayers;this.dirtyLayers=this.swapLayers,this.callback(e),e.clear(),this.swapLayers=e}})}},i=class{observer=null;callback=null;currentDpr=1;resizeScheduled=!1;cachedSize;constructor(e){this.container=e,this.currentDpr=window.devicePixelRatio||1,this.cachedSize={width:e.clientWidth,height:e.clientHeight}}getDpr(){return this.currentDpr}getContainerSize(){return this.cachedSize}readContainerSize(){let e=this.container.clientWidth,t=this.container.clientHeight;return(e!==this.cachedSize.width||t!==this.cachedSize.height)&&(this.cachedSize={width:e,height:t}),this.cachedSize}onResize(e){this.callback=e,this.observer=new ResizeObserver(()=>{this.resizeScheduled||(this.resizeScheduled=!0,requestAnimationFrame(()=>{this.resizeScheduled=!1,this.currentDpr=window.devicePixelRatio||1;let e=this.container.clientWidth,t=this.container.clientHeight;e===this.cachedSize.width&&t===this.cachedSize.height||(this.cachedSize={width:e,height:t},this.callback?.(this.cachedSize,this.currentDpr))}))}),this.observer.observe(this.container)}destroy(){this.observer?.disconnect(),this.observer=null,this.callback=null}};function a(e,t){return e*(t.barWidth+t.barSpacing)-t.offset+t.chartRect.x+t.barWidth/2}function o(e,t){let{min:n,max:r}=t.priceRange;if(t.logScale&&n>0&&r>0){let i=Math.log(n),a=Math.log(r)-i;if(a===0)return t.chartRect.y+t.chartRect.height/2;let o=(Math.log(Math.max(e,2**-52))-i)/a;return t.chartRect.y+t.chartRect.height*(1-o)}let i=r-n;if(i===0)return t.chartRect.y+t.chartRect.height/2;let a=(e-n)/i;return t.chartRect.y+t.chartRect.height*(1-a)}function s(e,t){let n=t.barWidth+t.barSpacing;return Math.round((e-t.chartRect.x-t.barWidth/2+t.offset)/n)}function c(e,t){let{min:n,max:r}=t.priceRange,i=1-(e-t.chartRect.y)/t.chartRect.height;if(t.logScale&&n>0&&r>0){let e=Math.log(n);return Math.exp(e+(Math.log(r)-e)*i)}return n+(r-n)*i}function l(e,t){if(t.length===0)return-1;if(e<=t[0].time)return 0;if(e>=t[t.length-1].time)return t.length-1;let n=0,r=t.length-1;for(;n<=r;){let i=n+r>>>1;if(t[i].time===e)return i;t[i].time<e?n=i+1:r=i-1}if(n>=t.length)return t.length-1;if(r<0)return 0;let i=t[r].time,a=t[n].time,o=a===i?0:(e-i)/(a-i);return r+o}var u=class{layerManager;renderLoop;dprManager;renderCtx=null;bgLayer;mainLayer;overlayLayer;uiLayer;onContainerResize=null;constructor(e){this.layerManager=new n(e),this.renderLoop=new r,this.dprManager=new i(e),this.layerManager.createLayers(),this.cacheLayerRefs(),this.renderLoop.setCallback(e=>this.render(e)),this.dprManager.onResize((e,t)=>{this.layerManager.resize(e,t),this.renderLoop.markAllDirty(),this.onContainerResize?.(e)});let t=this.dprManager.getContainerSize(),a=this.dprManager.getDpr();this.layerManager.resize(t,a)}cacheLayerRefs(){this.bgLayer=this.layerManager.getLayer(e.LayerType.Background),this.mainLayer=this.layerManager.getLayer(e.LayerType.Main),this.overlayLayer=this.layerManager.getLayer(e.LayerType.Overlay),this.uiLayer=this.layerManager.getLayer(e.LayerType.UI)}setRenderContext(e){this.renderCtx=e}start(){this.renderLoop.start()}stop(){this.renderLoop.stop()}requestRender(e){e===void 0?this.renderLoop.markAllDirty():this.renderLoop.markDirty(e)}render(t){let n=this.renderCtx;if(!n)return;let{viewport:r,theme:i,data:l}=n;if(!(r.chartRect.width<=0||r.chartRect.height<=0)){if(t.has(e.LayerType.Background)&&this.bgLayer&&(this.bgLayer.clear(),n.gridRenderer?.render(this.bgLayer.ctx,r,i),n.sessionBreaks?.render(this.bgLayer.ctx,r,i,l),n.watermark?.render(this.bgLayer.ctx,r,i)),t.has(e.LayerType.Main)&&this.mainLayer){this.mainLayer.clear();let e=this.mainLayer.ctx;e.save(),e.beginPath(),e.rect(r.chartRect.x,r.chartRect.y,r.chartRect.width,r.chartRect.height),e.clip(),n.volumeRenderer?.render(e,l,r,i),n.chartRenderer?.render(e,l,r,i),n.compareRenderer?.render(e,l,r,i),n.indicatorEngine?.renderOverlays(e,r),e.restore();let t=n.panels,a=n.indicatorEngine;if(a&&t.length>0){let n=`10px ${i.font.family}`,r=a.getPanelIndicators(),o=new Map;for(let e of r)o.set(e.instanceId,e);for(let r of t){if(r.rect.width<=0||r.rect.height<=0)continue;e.save(),e.beginPath(),e.rect(r.rect.x,r.rect.y,r.rect.width,r.rect.height),e.clip(),e.fillStyle=i.background,e.fillRect(r.rect.x,r.rect.y,r.rect.width,r.rect.height),e.fillStyle=i.axisLine,e.fillRect(r.rect.x,r.rect.y,r.rect.width,3),e.fillStyle=i.textSecondary,e.font=n,e.textBaseline=`top`,e.textAlign=`left`;let t=o.get(r.instanceId);t&&e.fillText(t.descriptor.name,r.rect.x+6,r.rect.y+6),e.save(),e.beginPath(),e.rect(r.rect.x,r.rect.y+20,r.rect.width,r.rect.height-20),e.clip(),a.renderPanel(e,r.instanceId,r.viewport),e.restore(),e.restore()}}}if(t.has(e.LayerType.Overlay)&&this.overlayLayer){this.overlayLayer.clear();let e=this.overlayLayer.ctx;n.priceLimits&&this.renderPriceLimits(e,r,i,n.priceLimits),n.drawingRenderer?.render(e,r),n.tradingRenderer?.render(e,r,i),n.crosshairHandler?.render(e,r,i);let t=n.crosshairHandler?.getPosition();if(t&&n.panels.length>0)for(let r of n.panels){let o=r.rect;if(t.x>=o.x&&t.x<=o.x+o.width&&t.y>=o.y&&t.y<=o.y+o.height){let c=r.viewport,l=t.x,u=s(l,c);l=a(Math.max(0,Math.min((n.data?.length??1)-1,Math.round(u))),c),e.save(),e.beginPath(),e.rect(o.x,o.y,o.width,o.height),e.clip(),e.setLineDash([4,4]),e.strokeStyle=i.crosshair,e.lineWidth=1,e.beginPath(),e.moveTo(Math.round(l)+.5,o.y),e.lineTo(Math.round(l)+.5,o.y+o.height),e.stroke(),e.beginPath(),e.moveTo(o.x,Math.round(t.y)+.5),e.lineTo(o.x+o.width,Math.round(t.y)+.5),e.stroke(),e.setLineDash([]),e.restore();break}}}if(t.has(e.LayerType.UI)&&this.uiLayer){this.uiLayer.clear();let t=this.uiLayer.ctx;if(n.priceAxis?.render(t,r,i),n.tradingRenderer?.renderAxisBadges(t,r,i),n.currentPriceLine?.render(t,r,i),n.timeAxis?.render(t,r,i,l,n.timeAxisY),n.chartLegend?.render(t,r,i,l),n.barCountdown?.render(t,r,i,l),n.panels.length>0){let r=n.crosshairHandler?.getPosition(),a=n.indicatorEngine,u=`${i.font.sizeSmall}px ${i.font.family}`,d;if(a){let e=a.getPanelIndicators();d=new Map;for(let t of e)d.set(t.instanceId,t)}for(let f of n.panels){let n=f.viewport,p=f.rect,{min:m,max:h}=n.priceRange;if(h-m<=0||p.height<=0)continue;let g=p.x+p.width,_=n.chartRect;t.strokeStyle=i.axisLine,t.lineWidth=1,t.beginPath(),t.moveTo(g+.5,p.y),t.lineTo(g+.5,p.y+p.height),t.stroke();let v=(0,e.computeTickStep)(m,h,4),y=Math.ceil(m/v)*v,b=v<1?Math.ceil(-Math.log10(v))+1:+(v<10);t.font=u,t.textBaseline=`middle`,t.textAlign=`left`;for(let r=y;r<=h;r+=v){let a=o(r,n);a<_.y||a>_.y+_.height||(t.strokeStyle=i.axisLine,t.beginPath(),t.moveTo(g,Math.round(a)+.5),t.lineTo(g+4,Math.round(a)+.5),t.stroke(),t.fillStyle=i.axisLabel,t.fillText((0,e.formatPrice)(r,b),g+6,a))}if(r&&r.x>=p.x&&r.x<=p.x+p.width&&r.y>=p.y&&r.y<=p.y+p.height){let a=(0,e.formatPrice)(c(r.y,n),b);t.font=`bold ${i.font.sizeSmall}px ${i.font.family}`;let o=t.measureText(a).width,s=Math.min(o+10,e.PRICE_AXIS_WIDTH-2);t.fillStyle=i.crosshair,t.fillRect(g+1,r.y-9,s,18),t.fillStyle=i.background,t.textBaseline=`middle`,t.textAlign=`left`,t.fillText(a,g+5,r.y)}if(r&&a){let e=s(r.x,n),o=Math.max(0,Math.min((l?.length??1)-1,Math.round(e))),c=a.getOutput(f.instanceId);if(c?.series&&o<c.series.length){let e=c.series[o];if(e){let n=[];for(let t in e){let r=e[t];r!==void 0&&n.push(`${t}: ${r.toFixed(b)}`)}if(n.length>0){let e=d?.get(f.instanceId),r=e?t.measureText(e.descriptor.name).width+14:10;t.font=u,t.fillStyle=i.textSecondary,t.textBaseline=`top`,t.textAlign=`left`,t.fillText(n.join(` `),p.x+r,p.y+6)}}}}}}}}}renderPriceLimits(t,n,r,i){let{chartRect:a}=n,s=[{price:i.ceiling,color:i.colors?.ceiling??`#FF00FF`,label:`CE`},{price:i.floor,color:i.colors?.floor??`#00FFFF`,label:`FL`},{price:i.reference,color:i.colors?.reference??`#FFD700`,label:`REF`}],c=`bold 9px ${r.font.family}`,l=`10px ${r.font.family}`;for(let{price:r,color:i,label:u}of s){let s=o(r,n);if(s<a.y||s>a.y+a.height)continue;t.setLineDash([8,4]),t.strokeStyle=i,t.lineWidth=1,t.beginPath(),t.moveTo(a.x,Math.round(s)+.5),t.lineTo(a.x+a.width,Math.round(s)+.5),t.stroke(),t.setLineDash([]),t.font=c,t.fillStyle=i,t.textBaseline=`bottom`,t.textAlign=`left`,t.fillText(`${u} ${r.toFixed(2)}`,a.x+4,s-2);let d=a.x+a.width+1;t.fillStyle=i,t.fillRect(d,s-7,e.PRICE_AXIS_WIDTH-2,14),t.fillStyle=`#000`,t.font=l,t.textBaseline=`middle`,t.textAlign=`left`,t.fillText(r.toFixed(2),d+4,s)}}destroy(){this.renderLoop.stop(),this.dprManager.destroy(),this.layerManager.destroy()}},d=class{state;dataLength=0;minBarWidth;maxBarWidth;rightMarginBars;constructor(t,n,r=2,i=30,a=5){this.minBarWidth=r,this.maxBarWidth=i,this.rightMarginBars=a,this.state={visibleRange:{from:0,to:0},priceRange:{min:0,max:1},barWidth:e.DEFAULT_BAR_WIDTH,barSpacing:e.DEFAULT_BAR_SPACING,offset:0,chartRect:this.computeChartRect(t,n)}}getState(){return{visibleRange:{...this.state.visibleRange},priceRange:{...this.state.priceRange},barWidth:this.state.barWidth,barSpacing:this.state.barSpacing,offset:this.state.offset,chartRect:{...this.state.chartRect},logScale:this.state.logScale}}setLogScale(e){this.state.logScale=e}isLogScale(){return this.state.logScale??!1}setRightMargin(e){this.rightMarginBars=e}computeChartRect(t,n){return{x:0,y:0,width:Math.max(0,t-e.PRICE_AXIS_WIDTH),height:Math.max(0,n-e.TIME_AXIS_HEIGHT)}}resize(e,t){this.state.chartRect=this.computeChartRect(e,t),this.clampOffset()}setChartRect(e){this.state.chartRect={x:e.x,y:e.y,width:Math.max(0,e.width),height:Math.max(0,e.height)},this.clampOffset(),this.updateVisibleRange()}updateData(t,n){this.dataLength=t.length,this.dataLength!==0&&(this.clampOffset(),this.updateVisibleRange(),n&&(this.state.priceRange=(0,e.computePriceRange)(t,this.state.visibleRange.from,Math.min(this.state.visibleRange.to,this.dataLength-1),.08)))}scrollBy(e){this.state.offset+=e,this.clampOffset(),this.updateVisibleRange()}isAtEnd(){let e=this.state.barWidth+this.state.barSpacing,t=this.rightMarginBars*e,n=this.dataLength*e-this.state.chartRect.width+t;return this.state.offset>=n-e*2}scrollToEnd(){let e=this.state.barWidth+this.state.barSpacing,t=this.rightMarginBars*e,n=this.dataLength*e;this.state.offset=n-this.state.chartRect.width+t,this.updateVisibleRange()}zoom(t,n){let r=this.state.barWidth,i=(0,e.clamp)(r*(1+t),this.minBarWidth,this.maxBarWidth);if(i===r)return;let a=this.state.barWidth+this.state.barSpacing,o=(this.state.offset+n)/a;this.state.barWidth=i;let s=i+this.state.barSpacing;this.state.offset=o*s-n,this.clampOffset(),this.updateVisibleRange()}clampOffset(){if(this.state.chartRect.width<=0)return;let t=this.state.barWidth+this.state.barSpacing,n=this.dataLength*t,r=this.rightMarginBars*t,i=n-this.state.chartRect.width+r,a,o;i>0?(a=-(this.state.chartRect.width*.5),o=i):(a=i,o=i),this.state.offset=(0,e.clamp)(this.state.offset,a,o)}updateVisibleRange(){let e=this.state.barWidth+this.state.barSpacing;if(e<=0||this.state.chartRect.width<=0)return;let t=Math.floor(this.state.offset/e),n=Math.ceil(this.state.chartRect.width/e)+1,r=Math.min(t+n,this.dataLength-1);this.state.visibleRange={from:Math.max(0,t),to:Math.max(0,r)}}};function f(t,n){return(0,e.sliceVisibleData)(t,n.visibleRange.from,n.visibleRange.to)}var p=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D,y=new Path2D,b=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=g(n.high),a=g(n.low),c=g(n.open),l=g(n.close),u=n.close>=n.open,d=u?_:v;d.moveTo(r,i),d.lineTo(r,a);let f=u?l:c,p=Math.max(Math.abs(l-c),1);(u?y:b).rect(r-s,f,o,p)}e.strokeStyle=r.candleUpWick,e.lineWidth=1,e.stroke(_),e.strokeStyle=r.candleDownWick,e.stroke(v),e.fillStyle=r.candleUp,e.fill(y),e.fillStyle=r.candleDown,e.fill(b)}},m=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange;if(i>a||t.length===0)return;let o=n.barWidth+n.barSpacing,s=-n.offset+n.chartRect.x+n.barWidth/2,{min:c,max:l}=n.priceRange,u=l-c;if(u===0)return;let d=n.chartRect.y,f=n.chartRect.height/u;e.beginPath(),e.strokeStyle=r.lineColor,e.lineWidth=2,e.lineJoin=`round`;let p=!1;for(let n=i;n<=a&&n<t.length;n++){let r=n*o+s,i=d+(l-t[n].close)*f;p?e.lineTo(r,i):(e.moveTo(r,i),p=!0)}e.stroke()}},h=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange;if(i>a||t.length===0)return;let o=n.barWidth+n.barSpacing,s=-n.offset+n.chartRect.x+n.barWidth/2,{min:c,max:l}=n.priceRange,u=l-c;if(u===0)return;let d=n.chartRect.y,f=n.chartRect.height,p=f/u,m=d+f,h=i*o+s,g=d+(l-t[i].close)*p,_=e.createLinearGradient(0,d,0,m);_.addColorStop(0,r.areaTopColor),_.addColorStop(1,r.areaBottomColor),e.beginPath(),e.moveTo(h,m),e.lineTo(h,g);let v=h;for(let n=i+1;n<=a&&n<t.length;n++)v=n*o+s,e.lineTo(v,d+(l-t[n].close)*p);e.lineTo(v,m),e.closePath(),e.fillStyle=_,e.fill(),e.beginPath(),e.strokeStyle=r.lineColor,e.lineWidth=2,e.lineJoin=`round`,e.moveTo(h,g);for(let n=i+1;n<=a&&n<t.length;n++)e.lineTo(n*o+s,d+(l-t[n].close)*p);e.stroke()}},g=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open?_:v,a=g(n.high),o=g(n.low),c=g(n.open),l=g(n.close);i.moveTo(r,a),i.lineTo(r,o),i.moveTo(r-s,c),i.lineTo(r,c),i.moveTo(r,l),i.lineTo(r+s,l)}e.lineWidth=1,e.strokeStyle=r.candleUp,e.stroke(_),e.strokeStyle=r.candleDown,e.stroke(v)}},_=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D,y=[],b=[],x=new Path2D,S=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open,a=e===0||n.close>=t[e-1].close,c=g(n.high),l=g(n.low),u=g(n.open),d=g(n.close),f=Math.min(u,d),p=Math.max(Math.abs(d-u),1),m=a?_:v;m.moveTo(r,c),m.lineTo(r,l),i?(a?y:b).push({x:r-s,y:f,w:o,h:p}):(a?x:S).rect(r-s,f,o,p)}e.lineWidth=1,e.strokeStyle=r.candleUp,e.stroke(_),e.strokeStyle=r.candleDown,e.stroke(v),e.fillStyle=r.candleUp,e.fill(x),e.fillStyle=r.candleDown,e.fill(S),e.lineWidth=1.5,e.strokeStyle=r.candleUp;for(let t of y)e.strokeRect(t.x,t.y,t.w,t.h);e.strokeStyle=r.candleDown;for(let t of b)e.strokeRect(t.x,t.y,t.w,t.h)}},v=class{baselinePrice=null;setBaseline(e){this.baselinePrice=e}render(e,t,n,r){let{from:i,to:a}=n.visibleRange;if(i>a||t.length===0)return;let o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=g(this.baselinePrice??this.computeBaseline(t,i,a)),{chartRect:v}=n,y=[],b=[];for(let e=i;e<=a&&e<t.length;e++)y.push(h(e)),b.push(g(t[e].close));if(y.length<2)return;let x=y.length-1;e.save();let S=new Path2D;S.rect(v.x,v.y,v.width,_-v.y),e.clip(S);let C=new Path2D;C.moveTo(y[0],_);for(let e=0;e<=x;e++)C.lineTo(y[e],b[e]);C.lineTo(y[x],_),C.closePath(),e.fillStyle=`${r.candleUp}20`,e.fill(C),e.restore(),e.save();let w=new Path2D;w.rect(v.x,_,v.width,v.y+v.height-_),e.clip(w);let T=new Path2D;T.moveTo(y[0],_);for(let e=0;e<=x;e++)T.lineTo(y[e],b[e]);T.lineTo(y[x],_),T.closePath(),e.fillStyle=`${r.candleDown}20`,e.fill(T),e.restore();let E=new Path2D,D=new Path2D;for(let e=1;e<=x;e++){let t=b[e]<=_?E:D;t.moveTo(y[e-1],b[e-1]),t.lineTo(y[e],b[e])}e.lineWidth=2,e.strokeStyle=r.candleUp,e.stroke(E),e.strokeStyle=r.candleDown,e.stroke(D),e.setLineDash([6,4]),e.strokeStyle=r.textSecondary,e.lineWidth=1;let O=new Path2D;O.moveTo(v.x,_),O.lineTo(v.x+v.width,_),e.stroke(O),e.setLineDash([])}computeBaseline(e,t,n){let r=0,i=0;for(let a=t;a<=n&&a<e.length;a++)r+=e[a].close,i++;return i>0?r/i:0}},y=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D,y=new Path2D,b=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open,a=g(Math.max(n.open,n.close)),c=g(Math.min(n.open,n.close)),l=Math.max(c-a,1),u=r-s;i?(_.rect(u,a,o,l),y.rect(u,a,o,l)):(v.rect(u,a,o,l),b.rect(u,a,o,l))}e.fillStyle=r.candleUp,e.fill(_),e.fillStyle=r.candleDown,e.fill(v),e.lineWidth=1,e.strokeStyle=r.candleUp,e.stroke(y),e.strokeStyle=r.candleDown,e.stroke(b)}},b=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open,o=g(n.high),s=g(n.low),c=i?_:v;if(c.moveTo(r,o),c.lineTo(r,s),e<a&&e+1<t.length){let t=h(e+1),n=i?s:o;c.moveTo(r,n),c.lineTo(t,n)}}e.strokeStyle=r.candleUp,e.lineWidth=3,e.stroke(_),e.strokeStyle=r.candleDown,e.lineWidth=1,e.stroke(v)}},x=class{boxSize=1;setBoxSize(e){this.boxSize=e}render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=Math.abs(this.boxSize*m),v=Math.min(s*.8,_*.8)*.5,y=new Path2D,b=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open,a=n.high,o=n.low,s=Math.max(1,Math.round((a-o)/this.boxSize));for(let e=0;e<s;e++){let t=g(o+(e+.5)*this.boxSize);i?(y.moveTo(r-v,t-v),y.lineTo(r+v,t+v),y.moveTo(r+v,t-v),y.lineTo(r-v,t+v)):(b.moveTo(r+v,t),b.arc(r,t,v,0,Math.PI*2))}}e.lineWidth=1.5,e.strokeStyle=r.candleUp,e.stroke(y),e.strokeStyle=r.candleDown,e.stroke(b)}},S=class{visible=!0;heightRatio=.2;setVisible(e){this.visible=e}setHeightRatio(e){this.heightRatio=Math.max(.05,Math.min(.5,e))}render(e,t,n,r){if(!this.visible||t.length===0)return;let{from:i,to:a}=n.visibleRange,{chartRect:o}=n,s=n.barWidth,c=s/2,l=s+n.barSpacing,u=-n.offset+o.x+c,d=0;for(let e=i;e<=a&&e<t.length;e++)t[e].volume>d&&(d=t[e].volume);if(d===0)return;let f=o.height*this.heightRatio,p=o.y+o.height,m=f/d,h=new Path2D,g=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=e*l+u,i=n.volume*m;(n.close>=n.open?h:g).rect(r-c,p-i,s,i)}e.globalAlpha=.35,e.fillStyle=r.candleUp,e.fill(h),e.fillStyle=r.candleDown,e.fill(g),e.globalAlpha=1}},C=class{symbols=new Map;mode=`percent`;setMode(e){this.mode=e}addSymbol(e){this.symbols.set(e.id,e)}removeSymbol(e){this.symbols.delete(e)}setSymbolData(e,t){let n=this.symbols.get(e);n&&(n.data=t)}setSymbolVisible(e,t){let n=this.symbols.get(e);n&&(n.visible=t)}getSymbols(){return Array.from(this.symbols.values())}clear(){this.symbols.clear()}render(e,t,n,r){if(this.symbols.size===0||t.length===0)return;let{chartRect:i}=n;e.save(),e.beginPath(),e.rect(i.x,i.y,i.width,i.height),e.clip();for(let i of this.symbols.values())!i.visible||i.data.length===0||this.renderSymbol(e,i,t,n,r);e.restore()}renderSymbol(e,t,n,r,i){let{chartRect:s}=r,c=r.barWidth+r.barSpacing,l=Math.max(0,Math.floor(r.offset/c));if(l>=n.length||l>=t.data.length)return;let u=n[l].close,d=t.data[l].close;if(u===0||d===0)return;e.strokeStyle=t.color,e.lineWidth=t.lineWidth??1.5,e.lineJoin=`round`,e.beginPath();let f=!1,p=Math.min(n.length,t.data.length);for(let n=l;n<p;n++){let i=a(n,r);if(i<s.x-c)continue;if(i>s.x+s.width+c)break;let l;l=this.mode===`percent`?o(u*(1+(t.data[n].close-d)/d),r):o(t.data[n].close,r),f?e.lineTo(i,l):(e.moveTo(i,l),f=!0)}if(e.stroke(),f){let n=Math.min(l+2,p-1),i=a(n,r)+4,s;s=this.mode===`percent`?o(u*(1+(t.data[n].close-d)/d),r)-4:o(t.data[n].close,r)-4,e.fillStyle=t.color,e.font=`bold 10px sans-serif`,e.textBaseline=`bottom`,e.textAlign=`left`,e.fillText(t.label,i,s)}}};function w(e){if(e.length===0)return[];let t=[],n=e[0].open,r=(e[0].open+e[0].high+e[0].low+e[0].close)/4;for(let i=0;i<e.length;i++){let a=e[i],o=(a.open+a.high+a.low+a.close)/4,s=i===0?a.open:(n+r)/2,c=Math.max(a.high,s,o),l=Math.min(a.low,s,o);t.push({time:a.time,open:s,high:c,low:l,close:o,volume:a.volume}),n=s,r=o}return t}function T(e,t){if(e.length===0)return[];let n=t.brickSize;t.useATR&&e.length>(t.atrPeriod??14)&&(n=E(e,t.atrPeriod??14)),n<=0&&(n=1);let r=[],i=Math.round(e[0].close/n)*n,a=e[0].time;for(let t=1;t<e.length;t++){let o=e[t].close-i,s=Math.floor(Math.abs(o)/n);if(s>=1){let c=o>0?1:-1;for(let o=0;o<s;o++){let o=i;i+=c*n,r.push({time:a++,open:o,high:Math.max(o,i),low:Math.min(o,i),close:i,volume:e[t].volume/s})}}}return r}function E(e,t){let n=0;for(let r=1;r<=Math.min(t,e.length-1);r++){let t=Math.max(e[r].high-e[r].low,Math.abs(e[r].high-e[r-1].close),Math.abs(e[r].low-e[r-1].close));n+=t}return n/t}function D(e,t=3){if(e.length===0)return[];let n=[],r=0;n.push({time:r++,open:e[0].open,high:Math.max(e[0].open,e[0].close),low:Math.min(e[0].open,e[0].close),close:e[0].close,volume:e[0].volume});for(let i=1;i<e.length;i++){let a=e[i].close,o=n[n.length-1],s=o.close>=o.open,c=Math.min(t,n.length),l=-1/0,u=1/0;for(let e=n.length-c;e<n.length;e++)l=Math.max(l,n[e].high),u=Math.min(u,n[e].low);s&&a>o.close?n.push({time:r++,open:o.close,high:a,low:o.close,close:a,volume:e[i].volume}):!s&&a<o.close||s&&a<u?n.push({time:r++,open:o.close,high:o.close,low:a,close:a,volume:e[i].volume}):!s&&a>l&&n.push({time:r++,open:o.close,high:a,low:o.close,close:a,volume:e[i].volume})}return n}function O(e,t=4){if(e.length<2)return e.slice();let n=[],r=e[1].close>=e[0].close?1:-1,i=e[0].close,a=e[0].close,o=e[0].close,s=0;for(let c=1;c<e.length;c++){let l=e[c].close,u=t/100*o;r===1?(l>i&&(i=l),i-l>=u&&(n.push({time:s++,open:o,high:i,low:o,close:i,volume:e[c].volume}),r=-1,o=i,a=l,i=l)):(l<a&&(a=l),l-a>=u&&(n.push({time:s++,open:o,high:o,low:a,close:a,volume:e[c].volume}),r=1,o=a,i=l,a=l))}return r===1?n.push({time:s,open:o,high:i,low:o,close:i,volume:0}):n.push({time:s,open:o,high:o,low:a,close:a,volume:0}),n}function ee(e,t,n=3){if(e.length===0||t<=0)return[];let r=[],i=0,a=0,o=0,s=0,c=Math.round(e[0].close/t)*t;a=c,o=c,i=1;for(let c=1;c<e.length;c++){let l=e[c].close,u=Math.round(l/t)*t;i===1?u>a?a=u:a-u>=t*n&&(r.push({time:s++,open:o,high:a,low:o,close:a,volume:e[c].volume}),i=-1,o=u,a-=t):u<o?o=u:u-o>=t*n&&(r.push({time:s++,open:a,high:a,low:o,close:o,volume:e[c].volume}),i=1,a=u,o+=t)}return r.push({time:s,open:i===1?o:a,high:a,low:o,close:i===1?a:o,volume:0}),r}var k=class{drawLine(e,t,n,r){if(!(t.length<2)){e.beginPath(),e.strokeStyle=n,e.lineWidth=r,e.lineJoin=`round`,e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length;n++)e.lineTo(t[n].x,t[n].y);e.stroke()}}drawBand(e,t,n,r){if(!(t.length<2||n.length<2)){e.beginPath(),e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length;n++)e.lineTo(t[n].x,t[n].y);for(let t=n.length-1;t>=0;t--)e.lineTo(n[t].x,n[t].y);e.closePath(),e.fillStyle=r,e.fill()}}drawHistogram(e,t,n){let r=n/2;for(let i of t){e.fillStyle=i.color;let t=Math.min(i.y,i.baseY),a=Math.abs(i.y-i.baseY);e.fillRect(i.x-r,t,n,Math.max(a,1))}}valuesToPoints(e,t,n,r){let i=[],{from:s,to:c}=r.visibleRange;for(let l=s;l<=c&&l<n.length;l++){let s=e.values.get(n[l].time);s&&s[t]!==void 0&&i.push({x:a(l,r),y:o(s[t],r)})}return i}},te=1,ne=class{registry=new Map;instances=new Map;register(e){this.registry.set(e.descriptor.id,e)}getAvailableIndicators(){return Array.from(this.registry.values()).map(e=>e.descriptor)}addIndicator(e,t={},n){let r=this.registry.get(e);if(!r)throw Error(`Unknown indicator: ${e}`);let i=`${e}_${te++}`,a={id:e,instanceId:i,params:{...r.descriptor.defaultConfig,...t},visible:!0},o={plugin:r,config:a,output:null,style:{colors:a.style?.colors??[`#2196F3`,`#FF9800`,`#4CAF50`,`#E91E63`,`#9C27B0`],lineWidths:a.style?.lineWidths??[1.5],opacity:a.style?.opacity??1}};return n&&(o.output=r.calculate(n,a)),this.instances.set(i,o),i}removeIndicator(e){this.instances.delete(e)}updateIndicator(e,t,n){let r=this.instances.get(e);r&&(Object.assign(r.config.params,t),n&&(r.output=r.plugin.calculate(n,r.config)))}recalculateAll(e){for(let t of this.instances.values())t.output=t.plugin.calculate(e,t.config)}getOutput(e){return this.instances.get(e)?.output??null}renderOverlays(e,t){for(let n of this.instances.values())!n.output||!n.config.visible||n.plugin.descriptor.placement===`overlay`&&n.plugin.render(e,n.output,t,n.style)}renderPanel(e,t,n){let r=this.instances.get(t);!r?.output||!r.config.visible||r.plugin.render(e,r.output,n,r.style)}getIndicatorConfig(e){return this.instances.get(e)?.config??null}getIndicatorDescriptor(e){return this.instances.get(e)?.plugin.descriptor??null}getActiveIndicators(){let e=[];for(let[t,n]of this.instances)e.push({instanceId:t,id:n.config.id,params:{...n.config.params},descriptor:n.plugin.descriptor});return e}updateIndicatorStyle(e,t){let n=this.instances.get(e);n&&Object.assign(n.style,t)}getPanelIndicators(){let e=[];for(let[t,n]of this.instances)n.plugin.descriptor.placement===`panel`&&e.push({instanceId:t,descriptor:n.plugin.descriptor});return e}},re=class extends k{descriptor={id:`sma`,name:`Simple Moving Average`,placement:`overlay`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null),a=0;for(let t=0;t<e.length;t++)if(a+=e[t].close,t>=n&&(a-=e[t-n].close),t>=n-1){let o={value:a/n};r.set(e[t].time,o),i[t]=o}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange;e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let l=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(!r||r.value===void 0)continue;let s=a(t,n),c=o(r.value,n);l?e.lineTo(s,c):(e.moveTo(s,c),l=!0)}e.stroke()}},ie=class extends k{descriptor={id:`ema`,name:`Exponential Moving Average`,placement:`overlay`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=2/(n+1),i=new Map,a=Array(e.length).fill(null),o=0;for(let t=0;t<e.length;t++){if(t<n-1){o+=e[t].close;continue}o=t===n-1?(o+e[t].close)/n:(e[t].close-o)*r+o;let s={value:o};i.set(e[t].time,s),a[t]=s}return{values:i,series:a}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange;e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let l=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(!r||r.value===void 0)continue;let s=a(t,n),c=o(r.value,n);l?e.lineTo(s,c):(e.moveTo(s,c),l=!0)}e.stroke()}},ae=class extends k{descriptor={id:`bb`,name:`Bollinger Bands`,placement:`overlay`,defaultConfig:{period:20,stdDev:2}};calculate(e,t){let n=t.params.period,r=t.params.stdDev,i=new Map,a=Array(e.length).fill(null),o=0,s=0;for(let t=0;t<e.length;t++){let c=e[t].close;if(o+=c,s+=c*c,t>=n){let r=e[t-n].close;o-=r,s-=r*r}if(t>=n-1){let c=o/n,l=s/n-c*c,u=Math.sqrt(Math.max(0,l)),d={middle:c,upper:c+r*u,lower:c-r*u};i.set(e[t].time,d),a[t]=d}}return{values:i,series:a}}render(t,n,r,i){let s=n.series;if(!s)return;let{from:c,to:l}=r.visibleRange,u=0,d=l-c+1,f=new Float64Array(d),p=new Float64Array(d),m=new Float64Array(d),h=new Float64Array(d),g=new Float64Array(d),_=new Float64Array(d);for(let e=c;e<=l&&e<s.length;e++){let t=s[e];if(!t||t.upper===void 0)continue;let n=a(e,r);f[u]=n,p[u]=o(t.upper,r),m[u]=n,h[u]=o(t.middle,r),g[u]=n,_[u]=o(t.lower,r),u++}if(!(u<2)){t.beginPath(),t.moveTo(f[0],p[0]);for(let e=1;e<u;e++)t.lineTo(f[e],p[e]);for(let e=u-1;e>=0;e--)t.lineTo(g[e],_[e]);t.closePath(),t.fillStyle=(0,e.withAlpha)(i.colors[0],.1),t.fill(),t.beginPath(),t.strokeStyle=i.colors[0],t.lineWidth=i.lineWidths[0],t.lineJoin=`round`,t.moveTo(f[0],p[0]);for(let e=1;e<u;e++)t.lineTo(f[e],p[e]);t.stroke(),t.beginPath(),t.strokeStyle=i.colors[1]??i.colors[0],t.moveTo(m[0],h[0]);for(let e=1;e<u;e++)t.lineTo(m[e],h[e]);t.stroke(),t.beginPath(),t.strokeStyle=i.colors[0],t.moveTo(g[0],_[0]);for(let e=1;e<u;e++)t.lineTo(g[e],_[e]);t.stroke()}}},oe=class extends k{descriptor={id:`vwap`,name:`Volume Weighted Average Price`,placement:`overlay`,defaultConfig:{}};calculate(e,t){let n=new Map,r=Array(e.length).fill(null),i=0,a=0;for(let t=0;t<e.length;t++){let o=(e[t].high+e[t].low+e[t].close)/3;if(i+=e[t].volume,a+=o*e[t].volume,i>0){let o={value:a/i};n.set(e[t].time,o),r[t]=o}}return{values:n,series:r}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange;e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let l=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(!r||r.value===void 0)continue;let s=a(t,n),c=o(r.value,n);l?e.lineTo(s,c):(e.moveTo(s,c),l=!0)}e.stroke()}},se=class extends k{descriptor={id:`ichimoku`,name:`Ichimoku Cloud`,placement:`overlay`,defaultConfig:{tenkan:9,kijun:26,senkou:52,displacement:26}};calculate(e,t){let n=t.params.tenkan,r=t.params.kijun,i=t.params.senkou,a=new Map,o=Array(e.length).fill(null),s=t=>{let n=new Float64Array(e.length),r=new Float64Array(e.length);for(let i=0;i<e.length;i++){let a=-1/0,o=1/0,s=Math.max(0,i-t+1);for(let t=s;t<=i;t++)e[t].high>a&&(a=e[t].high),e[t].low<o&&(o=e[t].low);n[i]=a,r[i]=o}return{highs:n,lows:r}},c=s(n),l=s(r),u=s(i);for(let t=0;t<e.length;t++){let s={};t>=n-1&&(s.tenkan=(c.highs[t]+c.lows[t])/2),t>=r-1&&(s.kijun=(l.highs[t]+l.lows[t])/2),s.tenkan!==void 0&&s.kijun!==void 0&&(s.senkouA=(s.tenkan+s.kijun)/2),t>=i-1&&(s.senkouB=(u.highs[t]+u.lows[t])/2),t>=r-1&&(s.chikou=e[t].close),(s.tenkan!==void 0||s.kijun!==void 0||s.senkouA!==void 0||s.senkouB!==void 0)&&(a.set(e[t].time,s),o[t]=s)}return{values:a,series:o}}render(t,n,r,i){let s=n.series;if(!s)return;let{from:c,to:l}=r.visibleRange,u=l-c+1,d=new Float64Array(u),f=new Float64Array(u),p=0,m=new Float64Array(u),h=new Float64Array(u),g=0,_=new Float64Array(u),v=new Float64Array(u),y=0,b=new Float64Array(u),x=new Float64Array(u),S=0;for(let e=c;e<=l&&e<s.length;e++){let t=s[e];if(!t)continue;let n=a(e,r);t.tenkan!==void 0&&(d[p]=n,f[p]=o(t.tenkan,r),p++),t.kijun!==void 0&&(m[g]=n,h[g]=o(t.kijun,r),g++),t.senkouA!==void 0&&(_[y]=n,v[y]=o(t.senkouA,r),y++),t.senkouB!==void 0&&(b[S]=n,x[S]=o(t.senkouB,r),S++)}if(y>1&&S>1){let n=Math.min(y,S);t.beginPath(),t.moveTo(_[0],v[0]);for(let e=1;e<n;e++)t.lineTo(_[e],v[e]);for(let e=n-1;e>=0;e--)t.lineTo(b[e],x[e]);t.closePath(),t.fillStyle=(0,e.withAlpha)(i.colors[2]??`#26A69A`,.15),t.fill()}let C=(e,n,r,i,a)=>{if(!(r<2)){t.beginPath(),t.strokeStyle=i,t.lineWidth=a,t.lineJoin=`round`,t.moveTo(e[0],n[0]);for(let i=1;i<r;i++)t.lineTo(e[i],n[i]);t.stroke()}};C(d,f,p,i.colors[0],i.lineWidths[0]),C(m,h,g,i.colors[1]??`#FF9800`,i.lineWidths[0]),C(_,v,y,i.colors[2]??`#26A69A`,1),C(b,x,S,i.colors[3]??`#EF5350`,1)}},A=class extends k{descriptor={id:`psar`,name:`Parabolic SAR`,placement:`overlay`,defaultConfig:{step:.02,max:.2}};calculate(e,t){let n=t.params.step,r=t.params.max,i=new Map,a=Array(e.length).fill(null);if(e.length<2)return{values:i,series:a};let o=e[1].close>e[0].close,s=n,c=o?e[0].high:e[0].low,l=o?e[0].low:e[0].high,u={value:l,trend:o?1:-1};i.set(e[0].time,u),a[0]=u;for(let t=1;t<e.length;t++){let u=l;l=u+s*(c-u),o?(l=Math.min(l,e[t-1].low),t>=2&&(l=Math.min(l,e[t-2].low)),e[t].low<l?(o=!1,l=c,c=e[t].low,s=n):e[t].high>c&&(c=e[t].high,s=Math.min(s+n,r))):(l=Math.max(l,e[t-1].high),t>=2&&(l=Math.max(l,e[t-2].high)),e[t].high>l?(o=!0,l=c,c=e[t].high,s=n):e[t].low<c&&(c=e[t].low,s=Math.min(s+n,r)));let d={value:l,trend:o?1:-1};i.set(e[t].time,d),a[t]=d}return{values:i,series:a}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange,l=Math.max(1.5,n.barWidth*.15);for(let t=s;t<=c&&t<i.length;t++){let s=i[t];if(!s||s.value===void 0)continue;let c=a(t,n),u=o(s.value,n);e.beginPath(),e.arc(c,u,l,0,Math.PI*2),e.fillStyle=s.trend===1?r.colors[0]:r.colors[1]??`#EF5350`,e.fill()}}},j=class extends k{descriptor={id:`supertrend`,name:`Supertrend`,placement:`overlay`,defaultConfig:{period:10,multiplier:3}};calculate(e,t){let n=t.params.period,r=t.params.multiplier,i=new Map,a=Array(e.length).fill(null);if(e.length<n)return{values:i,series:a};let o=[e[0].high-e[0].low];for(let t=1;t<e.length;t++)o.push(Math.max(e[t].high-e[t].low,Math.abs(e[t].high-e[t-1].close),Math.abs(e[t].low-e[t-1].close)));let s=0;for(let e=0;e<n;e++)s+=o[e];s/=n;let c=0,l=0,u=0,d=0,f=0,p=0,m=1;for(let t=n-1;t<e.length;t++){t>n-1&&(s=(s*(n-1)+o[t])/n);let h=(e[t].high+e[t].low)/2,g=h+r*s,_=h-r*s;c=g<u||e[t-1].close>u?g:u,l=_>d||e[t-1].close<d?_:d,f=p===u?e[t].close<=c?c:l:e[t].close>=l?l:c,m=f===l?1:-1;let v={value:f,trend:m};i.set(e[t].time,v),a[t]=v,u=c,d=l,p=f}return{values:i,series:a}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange,l=r.colors[0],u=r.colors[1]??`#EF5350`,d=0,f=0,p=0;for(let t=s;t<=c&&t<i.length;t++){let s=i[t];if(!s||s.value===void 0)continue;let c=a(t,n),m=o(s.value,n),h=s.trend??1;p!==0&&(e.beginPath(),e.strokeStyle=h===1?l:u,e.lineWidth=r.lineWidths[0]+.5,e.moveTo(d,f),e.lineTo(c,m),e.stroke()),d=c,f=m,p=h}}},M=class extends k{descriptor={id:`keltner`,name:`Keltner Channel`,placement:`overlay`,defaultConfig:{emaPeriod:20,atrPeriod:10,multiplier:1.5}};calculate(e,t){let n=t.params.emaPeriod,r=t.params.atrPeriod,i=t.params.multiplier,a=new Map;if(e.length<Math.max(n,r))return{values:a};let o=2/(n+1),s=e[0].close,c=[s];for(let t=1;t<e.length;t++)s=(e[t].close-s)*o+s,c.push(s);let l=[e[0].high-e[0].low];for(let t=1;t<e.length;t++)l.push(Math.max(e[t].high-e[t].low,Math.abs(e[t].high-e[t-1].close),Math.abs(e[t].low-e[t-1].close)));let u=0;for(let e=0;e<r;e++)u+=l[e];u/=r;let d=Array(e.length).fill(null),f=Math.max(n,r)-1;for(let t=f;t<e.length;t++){t>f&&(u=(u*(r-1)+l[t])/r);let n={middle:c[t],upper:c[t]+i*u,lower:c[t]-i*u};a.set(e[t].time,n),d[t]=n}return{values:a,series:d}}render(t,n,r,i){let s=n.series;if(!s)return;let{from:c,to:l}=r.visibleRange,u=[],d=[],f=[];for(let e=c;e<=l&&e<s.length;e++){let t=s[e];if(!t)continue;let n=a(e,r);t.upper!==void 0&&u.push({x:n,y:o(t.upper,r)}),t.middle!==void 0&&d.push({x:n,y:o(t.middle,r)}),t.lower!==void 0&&f.push({x:n,y:o(t.lower,r)})}this.drawBand(t,u,f,(0,e.withAlpha)(i.colors[0],.08)),this.drawLine(t,u,i.colors[0],i.lineWidths[0]),this.drawLine(t,d,i.colors[1]??i.colors[0],i.lineWidths[0]),this.drawLine(t,f,i.colors[0],i.lineWidths[0])}},N=class extends k{descriptor={id:`donchian`,name:`Donchian Channel`,placement:`overlay`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=-1/0,o=1/0;for(let r=t-n+1;r<=t;r++)e[r].high>a&&(a=e[r].high),e[r].low<o&&(o=e[r].low);let s={upper:a,middle:(a+o)/2,lower:o};r.set(e[t].time,s),i[t]=s}return{values:r,series:i}}render(t,n,r,i){let s=n.series;if(!s)return;let{from:c,to:l}=r.visibleRange,u=[],d=[],f=[];for(let e=c;e<=l&&e<s.length;e++){let t=s[e];if(!t)continue;let n=a(e,r);t.upper!==void 0&&u.push({x:n,y:o(t.upper,r)}),t.middle!==void 0&&d.push({x:n,y:o(t.middle,r)}),t.lower!==void 0&&f.push({x:n,y:o(t.lower,r)})}this.drawBand(t,u,f,(0,e.withAlpha)(i.colors[0],.08)),this.drawLine(t,u,i.colors[0],i.lineWidths[0]),this.drawLine(t,d,i.colors[1]??`#787B86`,1),this.drawLine(t,f,i.colors[0],i.lineWidths[0])}},ce=class extends k{descriptor={id:`rsi`,name:`Relative Strength Index`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);if(e.length<n+1)return{values:r,series:i};let a=0,o=0;for(let t=1;t<=n;t++){let n=e[t].close-e[t-1].close;n>0?a+=n:o-=n}a/=n,o/=n;let s={value:o===0?100:100-100/(1+a/o)};r.set(e[n].time,s),i[n]=s;for(let t=n+1;t<e.length;t++){let s=e[t].close-e[t-1].close,c=s>0?s:0,l=s<0?-s:0;a=(a*(n-1)+c)/n,o=(o*(n-1)+l)/n;let u={value:o===0?100:100-100/(1+a/o)};r.set(e[t].time,u),i[t]=u}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100);e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]),e.beginPath();for(let t of[30,70]){let n=l(t);e.moveTo(o.x,n),e.lineTo(o.x+o.width,n)}e.stroke(),e.setLineDash([]),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(!r||r.value===void 0)continue;let o=a(t,n),s=l(r.value);u?e.lineTo(o,s):(e.moveTo(o,s),u=!0)}e.stroke()}},P=class extends k{descriptor={id:`macd`,name:`MACD`,placement:`panel`,defaultConfig:{fast:12,slow:26,signal:9}};calculate(e,t){let n=t.params.fast,r=t.params.slow,i=t.params.signal,a=new Map,o=Array(e.length).fill(null);if(e.length<r)return{values:a,series:o};let s=2/(n+1),c=2/(r+1),l=2/(i+1),u=e[0].close,d=e[0].close,f=0,p=!1;for(let t=0;t<e.length;t++){let n=e[t].close;if(t===0?(u=n,d=n):(u=(n-u)*s+u,d=(n-d)*c+d),t>=r-1){let n=u-d;p?f=(n-f)*l+f:(f=n,p=!0);let r=n-f,i={macd:n,signal:f,histogram:r};a.set(e[t].time,i),o[t]=i}}return{values:a,series:o}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&(t.macd!==void 0&&(t.macd<l&&(l=t.macd),t.macd>u&&(u=t.macd)),t.signal!==void 0&&(t.signal<l&&(l=t.signal),t.signal>u&&(u=t.signal)),t.histogram!==void 0&&(t.histogram<l&&(l=t.histogram),t.histogram>u&&(u=t.histogram)))}if(l===1/0)return;let d=u-l||1,f=e=>o.y+o.height*(1-(e-l)/d),p=f(0),m=n.barWidth/2,h=r.colors[2]??`#26A69A`,g=r.colors[3]??`#EF5350`,_=r.colors[0],v=r.colors[1]??`#FF9800`;e.beginPath();let y=new Path2D,b=new Path2D,x=!1,S=!1,C=new Path2D,w=new Path2D;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];if(!t)continue;let r=a(e,n);if(t.histogram!==void 0){let e=f(t.histogram),i=Math.min(e,p),a=Math.max(Math.abs(e-p),1);t.histogram>=0?y.rect(r-m,i,n.barWidth,a):b.rect(r-m,i,n.barWidth,a)}if(t.macd!==void 0){let e=f(t.macd);x?C.lineTo(r,e):(C.moveTo(r,e),x=!0)}if(t.signal!==void 0){let e=f(t.signal);S?w.lineTo(r,e):(w.moveTo(r,e),S=!0)}}e.fillStyle=h,e.fill(y),e.fillStyle=g,e.fill(b),e.strokeStyle=_,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`,e.stroke(C),e.strokeStyle=v,e.stroke(w)}},F=class extends k{descriptor={id:`stochastic`,name:`Stochastic Oscillator`,placement:`panel`,defaultConfig:{kPeriod:14,dPeriod:3,smooth:3}};calculate(e,t){let n=t.params.kPeriod,r=t.params.dPeriod,i=t.params.smooth,a=new Map,o=Array(e.length).fill(null);if(e.length<n)return{values:a,series:o};let s=[];for(let t=0;t<e.length;t++){if(t<n-1){s.push(0);continue}let r=-1/0,i=1/0;for(let a=t-n+1;a<=t;a++)e[a].high>r&&(r=e[a].high),e[a].low<i&&(i=e[a].low);s.push(r===i?50:(e[t].close-i)/(r-i)*100)}let c=[];for(let e=0;e<s.length;e++){if(e<n-1+i-1){c.push(0);continue}let t=0;for(let n=e-i+1;n<=e;n++)t+=s[n];c.push(t/i)}let l=n-1+i-1;for(let t=l;t<c.length;t++){let n={k:c[t]};if(t>=l+r-1){let e=0;for(let n=t-r+1;n<=t;n++)e+=c[n];n.d=e/r}a.set(e[t].time,n),o[t]=n}return{values:a,series:o}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100);e.strokeStyle=r.colors[2]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);for(let t of[20,80]){let n=l(t);e.beginPath(),e.moveTo(o.x,n),e.lineTo(o.x+o.width,n),e.stroke()}e.setLineDash([]);let u=(t,o)=>{e.beginPath(),e.strokeStyle=o,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let r=s;r<=c&&r<i.length;r++){let o=i[r];if(!o||o[t]===void 0)continue;let s=a(r,n),c=l(o[t]);u?e.lineTo(s,c):(e.moveTo(s,c),u=!0)}e.stroke()};u(`k`,r.colors[0]),u(`d`,r.colors[1]??`#FF9800`)}},I=class extends k{descriptor={id:`atr`,name:`Average True Range`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);if(e.length<2)return{values:r,series:i};let a=[e[0].high-e[0].low];for(let t=1;t<e.length;t++){let n=Math.max(e[t].high-e[t].low,Math.abs(e[t].high-e[t-1].close),Math.abs(e[t].low-e[t-1].close));a.push(n)}let o=0;for(let e=0;e<n;e++)o+=a[e];o/=n;let s={value:o};r.set(e[n-1].time,s),i[n-1]=s;for(let t=n;t<e.length;t++){o=(o*(n-1)+a[t])/n;let s={value:o};r.set(e[t].time,s),i[t]=s}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d-f,m=e=>o.y+o.height*(1-(e-f)/p);e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let h=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=m(r.value);h?e.lineTo(i,o):(e.moveTo(i,o),h=!0)}}e.stroke()}},L=class extends k{descriptor={id:`adx`,name:`Average Directional Index`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);if(e.length<n*2)return{values:r,series:i};let a=[],o=[],s=[];for(let t=1;t<e.length;t++){let n=Math.max(e[t].high-e[t].low,Math.abs(e[t].high-e[t-1].close),Math.abs(e[t].low-e[t-1].close));a.push(n);let r=e[t].high-e[t-1].high,i=e[t-1].low-e[t].low;o.push(r>i&&r>0?r:0),s.push(i>r&&i>0?i:0)}let c=0,l=0,u=0;for(let e=0;e<n;e++)c+=a[e],l+=o[e],u+=s[e];let d=[];for(let t=n;t<a.length;t++){t>n&&(c=c-c/n+a[t],l=l-l/n+o[t],u=u-u/n+s[t]);let f=c===0?0:l/c*100,p=c===0?0:u/c*100,m=f+p,h=m===0?0:Math.abs(f-p)/m*100;d.push(h);let g=t+1,_={plusDI:f,minusDI:p,dx:h};r.set(e[g].time,_),i[g]=_}if(d.length>=n){let t=0;for(let e=0;e<n;e++)t+=d[e];t/=n;let a=n*2;if(a<e.length){let n=r.get(e[a].time);n&&(n.adx=t),i[a]&&(i[a].adx=t)}for(let a=n;a<d.length;a++){t=(t*(n-1)+d[a])/n;let o=a+n+1;if(o<e.length){let n=r.get(e[o].time);n&&(n.adx=t),i[o]&&(i[o].adx=t)}}}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100),u=(t,o)=>{e.beginPath(),e.strokeStyle=o,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let r=s;r<=c&&r<i.length;r++){let o=i[r];if(!o||o[t]===void 0)continue;let s=a(r,n),c=l(o[t]);u?e.lineTo(s,c):(e.moveTo(s,c),u=!0)}e.stroke()};u(`adx`,r.colors[0]),u(`plusDI`,r.colors[1]??`#26A69A`),u(`minusDI`,r.colors[2]??`#EF5350`)}},R=class extends k{descriptor={id:`obv`,name:`On Balance Volume`,placement:`panel`,defaultConfig:{}};calculate(e,t){let n=new Map,r=Array(e.length).fill(null);if(e.length===0)return{values:n,series:r};let i=0,a={value:i};n.set(e[0].time,a),r[0]=a;for(let t=1;t<e.length;t++){e[t].close>e[t-1].close?i+=e[t].volume:e[t].close<e[t-1].close&&(i-=e[t].volume);let a={value:i};n.set(e[t].time,a),r[t]=a}return{values:n,series:r}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d-f,m=e=>o.y+o.height*(1-(e-f)/p);e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let h=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=m(r.value);h?e.lineTo(i,o):(e.moveTo(i,o),h=!0)}}e.stroke()}},z=class extends k{descriptor={id:`williamsR`,name:`Williams %R`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=-1/0,o=1/0;for(let r=t-n+1;r<=t;r++)e[r].high>a&&(a=e[r].high),e[r].low<o&&(o=e[r].low);let s={value:a===o?-50:(a-e[t].close)/(a-o)*-100};r.set(e[t].time,s),i[t]=s}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-(e+100)/100);e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);for(let t of[-20,-80]){let n=l(t);e.beginPath(),e.moveTo(o.x,n),e.lineTo(o.x+o.width,n),e.stroke()}e.setLineDash([]),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=l(r.value);u?e.lineTo(i,o):(e.moveTo(i,o),u=!0)}}e.stroke()}},B=class extends k{descriptor={id:`cci`,name:`Commodity Channel Index`,placement:`panel`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=0;for(let r=t-n+1;r<=t;r++)a+=(e[r].high+e[r].low+e[r].close)/3;let o=a/n,s=0;for(let r=t-n+1;r<=t;r++){let t=(e[r].high+e[r].low+e[r].close)/3;s+=Math.abs(t-o)}let c=s/n,l=(e[t].high+e[t].low+e[t].close)/3,u={value:c===0?0:(l-o)/(.015*c)};r.set(e[t].time,u),i[t]=u}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);for(let t of[-100,0,100])if(t>=f&&t<=p){let n=h(t);e.beginPath(),e.moveTo(o.x,n),e.lineTo(o.x+o.width,n),e.stroke()}e.setLineDash([]),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let g=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=h(r.value);g?e.lineTo(i,o):(e.moveTo(i,o),g=!0)}}e.stroke()}},V=class extends k{descriptor={id:`mfi`,name:`Money Flow Index`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);if(e.length<n+1)return{values:r,series:i};let a=e.map(e=>(e.high+e.low+e.close)/3),o=a.map((t,n)=>t*e[n].volume);for(let t=n;t<e.length;t++){let s=0,c=0;for(let e=t-n+1;e<=t;e++)a[e]>a[e-1]?s+=o[e]:a[e]<a[e-1]&&(c+=o[e]);let l={value:c===0?100:100-100/(1+s/c)};r.set(e[t].time,l),i[t]=l}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100);e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);for(let t of[20,80]){let n=l(t);e.beginPath(),e.moveTo(o.x,n),e.lineTo(o.x+o.width,n),e.stroke()}e.setLineDash([]),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=l(r.value);u?e.lineTo(i,o):(e.moveTo(i,o),u=!0)}}e.stroke()}},H=class extends k{descriptor={id:`aroon`,name:`Aroon`,placement:`panel`,defaultConfig:{period:25}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n;t<e.length;t++){let a=0,o=0,s=-1/0,c=1/0;for(let r=0;r<=n;r++){let i=t-n+r;e[i].high>s&&(s=e[i].high,a=r),e[i].low<c&&(c=e[i].low,o=r)}let l={up:a/n*100,down:o/n*100};r.set(e[t].time,l),i[t]=l}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100),u=(t,o)=>{e.beginPath(),e.strokeStyle=o,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let r=s;r<=c&&r<i.length;r++){let o=i[r];if(!o||o[t]===void 0)continue;let s=a(r,n),c=l(o[t]);u?e.lineTo(s,c):(e.moveTo(s,c),u=!0)}e.stroke()};u(`up`,r.colors[0]),u(`down`,r.colors[1]??`#EF5350`)}},U=class extends k{descriptor={id:`roc`,name:`Rate of Change`,placement:`panel`,defaultConfig:{period:12}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n;t<e.length;t++){let a=e[t-n].close,o={value:a===0?0:(e[t].close-a)/a*100};r.set(e[t].time,o),i[t]=o}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);if(0>=f&&0<=p){e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);let t=h(0);e.beginPath(),e.moveTo(o.x,t),e.lineTo(o.x+o.width,t),e.stroke(),e.setLineDash([])}e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let g=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=h(r.value);g?e.lineTo(i,o):(e.moveTo(i,o),g=!0)}}e.stroke()}},W=class extends k{descriptor={id:`tsi`,name:`True Strength Index`,placement:`panel`,defaultConfig:{longPeriod:25,shortPeriod:13,signalPeriod:7}};emaSmooth(e,t){let n=2/(t+1),r=[e[0]];for(let t=1;t<e.length;t++)r.push((e[t]-r[t-1])*n+r[t-1]);return r}calculate(e,t){let n=t.params.longPeriod,r=t.params.shortPeriod,i=t.params.signalPeriod,a=new Map,o=Array(e.length).fill(null);if(e.length<2)return{values:a,series:o};let s=[],c=[];for(let t=1;t<e.length;t++){let n=e[t].close-e[t-1].close;s.push(n),c.push(Math.abs(n))}let l=this.emaSmooth(this.emaSmooth(s,n),r),u=this.emaSmooth(this.emaSmooth(c,n),r),d=[];for(let e=0;e<l.length;e++)d.push(u[e]===0?0:l[e]/u[e]*100);let f=this.emaSmooth(d,i);for(let t=0;t<d.length;t++){let n=t+1;if(n<e.length){let r={tsi:d[t],signal:f[t]};a.set(e[n].time,r),o[n]=r}}return{values:a,series:o}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&(t.tsi!==void 0&&(l=Math.min(l,t.tsi),u=Math.max(u,t.tsi)),t.signal!==void 0&&(l=Math.min(l,t.signal),u=Math.max(u,t.signal)))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);0>=f&&0<=p&&(e.strokeStyle=`#787B86`,e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(o.x,h(0)),e.lineTo(o.x+o.width,h(0)),e.stroke(),e.setLineDash([]));let g=(t,o)=>{e.beginPath(),e.strokeStyle=o,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let l=!1;for(let r=s;r<=c&&r<i.length;r++){let o=i[r];if(!o||o[t]===void 0)continue;let s=a(r,n),c=h(o[t]);l?e.lineTo(s,c):(e.moveTo(s,c),l=!0)}e.stroke()};g(`tsi`,r.colors[0]),g(`signal`,r.colors[1]??`#FF9800`)}},G=class extends k{descriptor={id:`cmf`,name:`Chaikin Money Flow`,placement:`panel`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=0,o=0;for(let r=t-n+1;r<=t;r++){let t=e[r].high-e[r].low,n=t===0?0:(e[r].close-e[r].low-(e[r].high-e[r].close))/t;a+=n*e[r].volume,o+=e[r].volume}let s={value:o===0?0:a/o};r.set(e[t].time,s),i[t]=s}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);0>=f&&0<=p&&(e.strokeStyle=`#787B86`,e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(o.x,h(0)),e.lineTo(o.x+o.width,h(0)),e.stroke(),e.setLineDash([]));let g=n.barWidth/2;for(let t=s;t<=c&&t<i.length;t++){let o=i[t];if(o&&o.value!==void 0){let i=a(t,n),s=h(o.value),c=h(0);e.fillStyle=o.value>=0?r.colors[0]:r.colors[1]??`#EF5350`;let l=Math.min(s,c);e.fillRect(i-g,l,n.barWidth,Math.max(Math.abs(s-c),1))}}}},le=class extends k{descriptor={id:`stddev`,name:`Standard Deviation`,placement:`panel`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=0;for(let r=t-n+1;r<=t;r++)a+=e[r].close;let o=a/n,s=0;for(let r=t-n+1;r<=t;r++){let t=e[r].close-o;s+=t*t}let c={value:Math.sqrt(s/n)};r.set(e[t].time,c),i[t]=c}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d-f,m=e=>o.y+o.height*(1-(e-f)/p);e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let h=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=m(r.value);h?e.lineTo(i,o):(e.moveTo(i,o),h=!0)}}e.stroke()}},ue=class extends k{descriptor={id:`volumeProfile`,name:`Volume Profile`,placement:`panel`,defaultConfig:{rows:24}};calculate(e,t){let n=t.params.rows,r=new Map,i=Array(e.length).fill(null);if(e.length===0)return{values:r,series:i};let a=1/0,o=-1/0;for(let t of e)t.low<a&&(a=t.low),t.high>o&&(o=t.high);let s=(o-a||1)/n,c=Array(n).fill(0);for(let t of e){let e=(t.high+t.low+t.close)/3,r=Math.min(n-1,Math.floor((e-a)/s));c[r]+=t.volume}for(let e=0;e<n;e++){let t=a+(e+.5)*s;r.set(e,{price:t,volume:c[e],rowIndex:e})}return{values:r,series:i,meta:{minPrice:a,maxPrice:o,rows:n,rowHeight:s,bins:c,maxVolume:Math.max(...c)}}}render(e,t,n,r){let{chartRect:i}=n,a=t.meta;if(!a||a.maxVolume===0)return;let{minPrice:o,rows:s,rowHeight:c,bins:l,maxVolume:u}=a,d=i.height/s,f=i.width*.3;for(let t=0;t<s;t++){let a=(o+(t+.5)*c-n.priceRange.min)/(n.priceRange.max-n.priceRange.min),s=i.y+i.height*(1-a),p=l[t]/u*f;e.fillStyle=l[t]>u*.7?r.colors[1]??`rgba(255, 152, 0, 0.5)`:r.colors[0]??`rgba(33, 150, 243, 0.3)`,e.fillRect(i.x,s-d/2,p,Math.max(d-1,1))}}},de=class extends k{descriptor={id:`ad`,name:`Accumulation/Distribution`,placement:`panel`,defaultConfig:{}};calculate(e,t){let n=new Map,r=Array(e.length).fill(null);if(e.length===0)return{values:n,series:r};let i=0;for(let t=0;t<e.length;t++){let a=e[t].high-e[t].low,o=a===0?0:(e[t].close-e[t].low-(e[t].high-e[t].close))/a;i+=o*e[t].volume;let s={value:i};n.set(e[t].time,s),r[t]=s}return{values:n,series:r}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d-f,m=e=>o.y+o.height*(1-(e-f)/p);e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let h=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=m(r.value);h?e.lineTo(i,o):(e.moveTo(i,o),h=!0)}}e.stroke()}},fe=class extends k{descriptor={id:`vroc`,name:`Volume Rate of Change`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n;t<e.length;t++){let a=e[t-n].volume,o={value:a===0?0:(e[t].volume-a)/a*100};r.set(e[t].time,o),i[t]=o}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);0>=f&&0<=p&&(e.strokeStyle=`#787B86`,e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(o.x,h(0)),e.lineTo(o.x+o.width,h(0)),e.stroke(),e.setLineDash([])),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let g=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=h(r.value);g?e.lineTo(i,o):(e.moveTo(i,o),g=!0)}}e.stroke()}};function pe(e){e.register(new re),e.register(new ie),e.register(new ae),e.register(new oe),e.register(new se),e.register(new A),e.register(new j),e.register(new M),e.register(new N),e.register(new ce),e.register(new P),e.register(new F),e.register(new I),e.register(new L),e.register(new R),e.register(new z),e.register(new B),e.register(new V),e.register(new H),e.register(new U),e.register(new W),e.register(new G),e.register(new le),e.register(new ue),e.register(new de),e.register(new fe)}var me=class{visible=!0;setVisible(e){this.visible=e}isVisible(){return this.visible}render(t,n,r){if(!this.visible)return;let{chartRect:i,priceRange:a}=n;t.strokeStyle=r.grid,t.lineWidth=1;let o=a.max-a.min;if(o<=0)return;let s=1/o;t.beginPath();let c=(0,e.computeTickStep)(a.min,a.max,8),l=Math.ceil(a.min/c)*c;for(let e=l;e<=a.max;e+=c){let n=Math.round(i.y+i.height*(1-(e-a.min)*s))+.5;t.moveTo(i.x,n),t.lineTo(i.x+i.width,n)}let u=n.barWidth+n.barSpacing,d=Math.max(1,Math.ceil(80/u)),{from:f,to:p}=n.visibleRange,m=-n.offset+i.x+n.barWidth/2;for(let e=f;e<=p;e++){if(e%d!==0)continue;let n=Math.round(e*u+m)+.5;t.moveTo(n,i.y),t.lineTo(n,i.y+i.height)}t.stroke()}},he=class{render(t,n,r){let{chartRect:i,priceRange:a}=n,o=i.x+i.width,s=a.max-a.min;if(s<=0)return;let c=1/s;t.strokeStyle=r.axisLine,t.lineWidth=1,t.beginPath(),t.moveTo(o+.5,i.y),t.lineTo(o+.5,i.y+i.height),t.stroke();let l=(0,e.computeTickStep)(a.min,a.max,8),u=Math.ceil(a.min/l)*l,d=l<1?Math.ceil(-Math.log10(l))+1:2,f=`${r.font.sizeSmall}px ${r.font.family}`,p=[];for(let t=u;t<=a.max;t+=l){let n=i.y+i.height*(1-(t-a.min)*c);p.push({y:n,text:(0,e.formatPrice)(t,d)})}t.font=f,t.textBaseline=`middle`,t.textAlign=`left`,t.fillStyle=r.axisLabelBackground;for(let{y:n}of p)t.fillRect(o+1,n-8,e.PRICE_AXIS_WIDTH-2,16);t.fillStyle=r.axisLabel;for(let{y:e,text:n}of p)t.fillText(n,o+5,e)}},K=new Date,ge=e=>e<10?`0`+e:``+e,_e=-new Date().getTimezoneOffset()/60,ve=`UTC${_e>=0?`+`:``}${_e}`,ye=class{render(e,t,n,r,i){let{chartRect:a}=t,o=i??a.y+a.height;e.strokeStyle=n.axisLine,e.lineWidth=1,e.beginPath(),e.moveTo(a.x,o+.5),e.lineTo(a.x+a.width,o+.5),e.stroke();let s=t.barWidth+t.barSpacing,c=Math.max(1,Math.ceil(80/s)),{from:l,to:u}=t.visibleRange,d=-t.offset+a.x+t.barWidth/2;e.font=`${n.font.sizeSmall}px ${n.font.family}`,e.textBaseline=`top`,e.textAlign=`center`,e.fillStyle=n.axisLabel;let f=-1;for(let t=l;t<=u&&t<r.length;t++){if(t%c!==0)continue;let n=t*s+d,i=r[t].time,a=i>0xe8d4a51000?i:i*1e3;K.setTime(a);let l=K.getDate(),u=K.getMonth()+1,p=K.getHours(),m=K.getMinutes(),h;l===f?h=`${ge(p)}:${ge(m)}`:(h=`${u}/${l}`,f=l),e.fillText(h,n,o+5)}let p=a.x+a.width-4,m=o+4;e.font=`${n.font.sizeSmall-1}px ${n.font.family}`,e.textAlign=`right`,e.textBaseline=`top`,e.fillStyle=n.textSecondary,e.fillText(ve,p,m)}},be=class{panHandler=null;zoomHandler=null;crosshairHandler=null;drawingManager=null;tradingManager=null;viewportGetter=null;onOverlayDirty=null;boundHandlers=[];lastTouchDist=0;lastTouchMid={x:0,y:0};touchActive=!1;constructor(e){this.element=e}setOverlayDirtyCallback(e){this.onOverlayDirty=e}setPanHandler(e){this.panHandler=e}setZoomHandler(e){this.zoomHandler=e}setCrosshairHandler(e){this.crosshairHandler=e}setDrawingManager(e,t){this.drawingManager=e,this.viewportGetter=t}setTradingManager(e,t){this.tradingManager=e,this.viewportGetter=t}attach(){let e=()=>this.viewportGetter?.()??null,t=t=>{let n=this.getMousePos(t),r=e();this.tradingManager&&r&&this.tradingManager.onPointerDown(n,r)||this.drawingManager&&r&&this.drawingManager.onPointerDown(n,r)||this.panHandler?.onPointerDown(n)},n=t=>{let n=this.getMousePos(t),r=e();if(this.tradingManager&&r&&this.tradingManager.onPointerMove(n,r)){this.crosshairHandler?.onPointerMove(n),this.onOverlayDirty?.();return}if(this.drawingManager&&r&&this.drawingManager.onPointerMove(n,r)){this.crosshairHandler?.onPointerMove(n),this.onOverlayDirty?.();return}this.panHandler?.onPointerMove(n),this.crosshairHandler?.onPointerMove(n),this.onOverlayDirty?.()},r=()=>{this.tradingManager?.onPointerUp()||this.drawingManager?.onPointerUp()||this.panHandler?.onPointerUp()},i=()=>{this.panHandler?.onPointerUp(),this.drawingManager?.onPointerUp(),this.tradingManager?.onPointerUp(),this.crosshairHandler?.onPointerLeave(),this.onOverlayDirty?.()},a=e=>{e.preventDefault();let t=this.getMousePos(e);this.zoomHandler?.onWheel(e.deltaY,t)},o=e=>{this.drawingManager?.onKeyDown(e.key,e.ctrlKey||e.metaKey)&&e.preventDefault()},s=t=>{let n=e();this.tradingManager&&n&&(t.preventDefault(),this.tradingManager.onContextMenu(this.getMousePos(t),n))},c=e=>{if(e.preventDefault(),e.touches.length===1){let t=this.getTouchPos(e.touches[0]);this.panHandler?.onPointerDown(t),this.crosshairHandler?.onPointerMove(t),this.touchActive=!0}else e.touches.length===2&&(this.panHandler?.onPointerUp(),this.lastTouchDist=this.getTouchDistance(e.touches[0],e.touches[1]),this.lastTouchMid=this.getTouchMidpoint(e.touches[0],e.touches[1]),this.touchActive=!0)},l=e=>{if(e.preventDefault(),e.touches.length===1&&this.touchActive){let t=this.getTouchPos(e.touches[0]);this.panHandler?.onPointerMove(t),this.crosshairHandler?.onPointerMove(t)}else if(e.touches.length===2){let t=this.getTouchDistance(e.touches[0],e.touches[1]),n=this.getTouchMidpoint(e.touches[0],e.touches[1]);if(this.lastTouchDist>0){let e=(t/this.lastTouchDist-1)*.5;this.zoomHandler?.onWheel(-e*100,n)}let r=this.lastTouchMid.x-n.x;Math.abs(r)>1&&(this.panHandler?.onPointerDown(this.lastTouchMid),this.panHandler?.onPointerMove(n)),this.lastTouchDist=t,this.lastTouchMid=n}this.onOverlayDirty?.()},u=e=>{if(e.touches.length===0)this.panHandler?.onPointerUp(),this.crosshairHandler?.onPointerLeave(),this.touchActive=!1,this.lastTouchDist=0;else if(e.touches.length===1){let t=this.getTouchPos(e.touches[0]);this.panHandler?.onPointerDown(t)}};this.element.addEventListener(`mousedown`,t),this.element.addEventListener(`mousemove`,n),document.addEventListener(`mouseup`,r),this.element.addEventListener(`mouseleave`,i),this.element.addEventListener(`wheel`,a,{passive:!1}),this.element.addEventListener(`contextmenu`,s),this.element.addEventListener(`touchstart`,c,{passive:!1}),this.element.addEventListener(`touchmove`,l,{passive:!1}),this.element.addEventListener(`touchend`,u),document.addEventListener(`keydown`,o),this.boundHandlers.push(()=>this.element.removeEventListener(`mousedown`,t),()=>this.element.removeEventListener(`mousemove`,n),()=>document.removeEventListener(`mouseup`,r),()=>this.element.removeEventListener(`mouseleave`,i),()=>this.element.removeEventListener(`wheel`,a),()=>this.element.removeEventListener(`contextmenu`,s),()=>this.element.removeEventListener(`touchstart`,c),()=>this.element.removeEventListener(`touchmove`,l),()=>this.element.removeEventListener(`touchend`,u),()=>document.removeEventListener(`keydown`,o))}detach(){for(let e of this.boundHandlers)e();this.boundHandlers=[]}getMousePos(e){let t=this.element.getBoundingClientRect();return{x:e.clientX-t.left,y:e.clientY-t.top}}getTouchPos(e){let t=this.element.getBoundingClientRect();return{x:e.clientX-t.left,y:e.clientY-t.top}}getTouchDistance(e,t){return Math.hypot(t.clientX-e.clientX,t.clientY-e.clientY)}getTouchMidpoint(e,t){let n=this.element.getBoundingClientRect();return{x:(e.clientX+t.clientX)/2-n.left,y:(e.clientY+t.clientY)/2-n.top}}},xe=class{dragging=!1;lastX=0;lastTime=0;velocity=0;momentumId=0;callback;friction=.92;constructor(e){this.callback=e}onPointerDown(e){this.dragging=!0,this.lastX=e.x,this.lastTime=Date.now(),this.velocity=0,this.stopMomentum()}onPointerMove(e){if(!this.dragging)return;let t=Date.now(),n=this.lastX-e.x,r=t-this.lastTime;r>0&&(this.velocity=n/r),this.lastX=e.x,this.lastTime=t,this.callback(n)}onPointerUp(){this.dragging&&(this.dragging=!1,Math.abs(this.velocity)>.1&&this.startMomentum())}startMomentum(){this.stopMomentum();let e=this.velocity*16,t=()=>{if(e*=this.friction,Math.abs(e)<.5){this.momentumId=0;return}this.callback(e),this.momentumId=requestAnimationFrame(t)};this.momentumId=requestAnimationFrame(t)}stopMomentum(){this.momentumId&&=(cancelAnimationFrame(this.momentumId),0)}},Se=class{callback;sensitivity;pendingDelta=0;pendingCenterX=0;rafId=0;constructor(e,t=.001){this.callback=e,this.sensitivity=t}onWheel(e,t){this.pendingDelta+=-e*this.sensitivity,this.pendingCenterX=t.x,this.rafId||=requestAnimationFrame(()=>{this.rafId=0;let e=this.pendingDelta,t=this.pendingCenterX;this.pendingDelta=0,this.callback(e,t)})}dispose(){this.rafId&&=(cancelAnimationFrame(this.rafId),0)}},Ce=class{position=null;callback=null;data=[];magnetMode=!0;mode=`magnet`;pricePrecision=2;pendingBarIndex=null;pendingPoint=null;callbackScheduled=!1;lastCallbackBarIndex=-1;setCallback(e){this.callback=e}setData(e){this.data=e}setMagnetMode(e){this.magnetMode=e,this.mode=e?`magnet`:`normal`}setMode(e){this.mode=e,this.magnetMode=e===`magnet`}getMode(){return this.mode}setPricePrecision(e){this.pricePrecision=e}getPosition(){return this.position}onPointerMove(e){this.position=e}onPointerLeave(){this.position=null,this.pendingBarIndex=null,this.pendingPoint=null,this.lastCallbackBarIndex=-1,this.flushCallback(null,null)}render(t,n,r){if(!this.position||this.mode===`hidden`)return;let{chartRect:i}=n,{x:o,y:l}=this.position;if(o<i.x||o>i.x+i.width||l<i.y||l>i.y+i.height)return;let u=s(o,n);u=Math.max(0,Math.min(this.data.length-1,u)),this.magnetMode&&u>=0&&u<this.data.length&&(o=a(u,n)),u!==this.lastCallbackBarIndex&&(this.pendingBarIndex=u,this.pendingPoint={x:o,y:l},this.lastCallbackBarIndex=u,this.scheduleCallback()),t.setLineDash([4,4]),t.strokeStyle=r.crosshair,t.lineWidth=1,t.beginPath(),t.moveTo(Math.round(o)+.5,i.y),t.lineTo(Math.round(o)+.5,i.y+i.height),t.stroke(),t.beginPath(),t.moveTo(i.x,Math.round(l)+.5),t.lineTo(i.x+i.width,Math.round(l)+.5),t.stroke(),t.setLineDash([]);let d=(0,e.formatPrice)(c(l,n),this.pricePrecision),f=i.x+i.width+1;t.fillStyle=r.axisLabelBackground,t.fillRect(f,l-10,e.PRICE_AXIS_WIDTH-2,20),t.fillStyle=r.axisLabel,t.font=`${r.font.sizeSmall}px ${r.font.family}`,t.textBaseline=`middle`,t.textAlign=`left`,t.fillText(d,f+5,l)}scheduleCallback(){this.callbackScheduled||(this.callbackScheduled=!0,queueMicrotask(()=>{this.callbackScheduled=!1,this.flushCallback(this.pendingBarIndex,this.pendingPoint)}))}flushCallback(e,t){this.callback?.(e,t)}},we=class{callbacks;enabled=!0;constructor(e){this.callbacks=e}setEnabled(e){this.enabled=e}handleKey(e){if(!this.enabled)return!1;let t=e.shiftKey?10:1;switch(e.key){case`ArrowLeft`:return this.callbacks.scrollBars(-t),!0;case`ArrowRight`:return this.callbacks.scrollBars(t),!0;case`ArrowUp`:case`+`:case`=`:return this.callbacks.zoom(.1),!0;case`ArrowDown`:case`-`:return this.callbacks.zoom(-.1),!0;case`Home`:return this.callbacks.goToStart(),!0;case`End`:return this.callbacks.goToEnd(),!0;case` `:return this.callbacks.fitContent(),!0;default:return!1}}},Te=class{el=null;visible=!1;timeEl;openEl;highEl;lowEl;closeEl;changeEl;volEl;measuredWidth=155;measuredHeight=95;measured=!1;create(e){if(this.el)return;this.el=document.createElement(`div`),Object.assign(this.el.style,{position:`absolute`,display:`none`,padding:`6px 10px`,borderRadius:`4px`,fontSize:`11px`,fontFamily:`-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif`,lineHeight:`1.5`,pointerEvents:`none`,zIndex:`100`,whiteSpace:`nowrap`,willChange:`transform`}),this.timeEl=this.addLine(this.el);let t=this.addLine(this.el),n=this.addLine(this.el);this.changeEl=this.addLine(this.el),this.volEl=this.addLine(this.el),t.append(this.label(`O `),this.openEl=this.val(),this.label(` H `),this.highEl=this.val()),n.append(this.label(`L `),this.lowEl=this.val(),this.label(` C `),this.closeEl=this.val()),e.appendChild(this.el)}show(e,t,n,r){if(!this.el)return;let i=t.close>=t.open?n.candleUp:n.candleDown,a=e=>e.toFixed(2),o=new Date(t.time);this.timeEl.textContent=`${o.getMonth()+1}/${o.getDate()} ${String(o.getHours()).padStart(2,`0`)}:${String(o.getMinutes()).padStart(2,`0`)}`,this.timeEl.style.color=n.textSecondary,this.openEl.textContent=a(t.open),this.highEl.textContent=a(t.high),this.lowEl.textContent=a(t.low),this.closeEl.textContent=a(t.close),this.openEl.style.color=i,this.highEl.style.color=i,this.lowEl.style.color=i,this.closeEl.style.color=i;let s=t.close-t.open,c=t.open===0?0:s/t.open*100,l=s>=0?`+`:``;this.changeEl.textContent=`${l}${a(s)} (${l}${c.toFixed(2)}%)`,this.changeEl.style.color=i;let u=t.volume;this.volEl.textContent=`Vol ${u>=1e6?(u/1e6).toFixed(1)+`M`:u>=1e3?(u/1e3).toFixed(1)+`K`:u.toFixed(0)}`,this.volEl.style.color=n.textSecondary;let d=Ee(n.background);this.el.style.background=d?De(n.background,.95):De(n.background,.97),this.el.style.border=`1px solid ${n.axisLine}`,this.el.style.boxShadow=d?`0 4px 12px rgba(0,0,0,0.5)`:`0 4px 12px rgba(0,0,0,0.15)`,this.el.style.color=n.text,this.el.style.backdropFilter=`blur(8px)`,!this.measured&&this.visible&&(this.measuredWidth=this.el.offsetWidth,this.measuredHeight=this.el.offsetHeight,this.measured=!0);let f=e.x+16,p=e.y-40;f+this.measuredWidth>r.width-80&&(f=e.x-this.measuredWidth-16),p<4&&(p=4),p+this.measuredHeight>r.height&&(p=r.height-this.measuredHeight-4),this.el.style.transform=`translate(${Math.round(f)}px, ${Math.round(p)}px)`,this.el.style.left=`0`,this.el.style.top=`0`,this.visible||=(this.el.style.display=`block`,!0)}hide(){this.el&&this.visible&&(this.el.style.display=`none`,this.visible=!1)}isVisible(){return this.visible}destroy(){this.el?.remove(),this.el=null,this.visible=!1,this.measured=!1}addLine(e){let t=document.createElement(`div`);return e.appendChild(t),t}label(e){let t=document.createElement(`span`);return t.textContent=e,t.style.opacity=`0.6`,t}val(){return document.createElement(`span`)}};function Ee(e){if(e.startsWith(`#`)){let t=e.slice(1),n=t.length===3?t.split(``).map(e=>e+e).join(``):t.slice(0,6),r=parseInt(n.slice(0,2),16),i=parseInt(n.slice(2,4),16),a=parseInt(n.slice(4,6),16);return(.299*r+.587*i+.114*a)/255<.5}let t=e.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);if(t){let e=parseInt(t[1]),n=parseInt(t[2]),r=parseInt(t[3]);return(.299*e+.587*n+.114*r)/255<.5}return!0}function De(e,t){if(e.startsWith(`#`)){let n=e.slice(1),r=n.length===3?n.split(``).map(e=>e+e).join(``):n.slice(0,6);return`rgba(${parseInt(r.slice(0,2),16)}, ${parseInt(r.slice(2,4),16)}, ${parseInt(r.slice(4,6),16)}, ${t})`}return e}var Oe=class{listeners=new Map;tauriBridge=null;on(e,t){let n=this.listeners.get(e);n||(n=new Set,this.listeners.set(e,n)),n.add(t)}off(e,t){this.listeners.get(e)?.delete(t)}emit(e,t){let n={type:e,timestamp:Date.now(),payload:t},r=this.listeners.get(e);if(r)for(let e of r)e(n);if(this.tauriBridge?.enabled&&window.__TAURI__){let t=this.tauriBridge.eventPrefix??`chart`;window.__TAURI__.event.emit(`${t}:${e}`,n).catch(()=>{})}}enableTauriBridge(e){this.tauriBridge=e}disableTauriBridge(){this.tauriBridge=null}destroy(){this.listeners.clear(),this.tauriBridge=null}},q=class{hitTestAnchor(e,t,n,r){for(let i=0;i<t.anchors.length;i++){let a=this.anchorToPixel(t.anchors[i],n);if(Math.hypot(e.x-a.x,e.y-a.y)<=r)return i}return-1}anchorToPixel(e,t){return{x:a(e.time,t),y:o(e.price,t)}}pixelToAnchor(e,t){return{time:s(e.x,t),price:c(e.y,t)}}applyLineStyle(e,t){switch(e.strokeStyle=t.color,e.lineWidth=t.lineWidth,t.lineStyle){case`dashed`:e.setLineDash([6,4]);break;case`dotted`:e.setLineDash([2,2]);break;default:e.setLineDash([]);break}}resetLineStyle(e){e.setLineDash([])}renderAnchorHandles(e,t,n){e.fillStyle=`#FFFFFF`,e.strokeStyle=t.style.color,e.lineWidth=1;for(let r of t.anchors){let t=this.anchorToPixel(r,n);e.fillRect(t.x-4,t.y-4,8,8),e.strokeRect(t.x-4,t.y-4,8,8)}}distanceToLine(e,t,n){let r=n.x-t.x,i=n.y-t.y,a=r*r+i*i;if(a===0)return Math.hypot(e.x-t.x,e.y-t.y);let o=((e.x-t.x)*r+(e.y-t.y)*i)/a;o=Math.max(0,Math.min(1,o));let s=t.x+o*r,c=t.y+o*i;return Math.hypot(e.x-s,e.y-c)}distanceToInfiniteLine(e,t,n){let r=n.x-t.x,i=n.y-t.y,a=Math.hypot(r,i);return a===0?Math.hypot(e.x-t.x,e.y-t.y):Math.abs(r*(t.y-e.y)-i*(t.x-e.x))/a}},ke=1,Ae=class{registry=new Map;drawings=[];state=`idle`;activeTool=null;activeStyle={color:`#2196F3`,lineWidth:1,lineStyle:`solid`,fillColor:`rgba(33, 150, 243, 0.1)`,fillOpacity:.1,fontSize:12};creatingDrawing=null;committedAnchors=0;previewAnchor=null;selectedDrawingId=null;dragAnchorIndex=-1;dragStartPoint=null;dragStartAnchors=[];dragBeforeState=null;eventCallback=null;requestRender=null;undoRedo=null;magnetMode=`none`;dataGetter=null;displayDataGetter=null;register(e){this.registry.set(e.descriptor.type,e)}setEventCallback(e){this.eventCallback=e}setRequestRender(e){this.requestRender=e}setUndoRedoManager(e){this.undoRedo=e}setMagnetMode(e){this.magnetMode=e}getMagnetMode(){return this.magnetMode}setDataGetter(e){this.dataGetter=e}setDisplayDataGetter(e){this.displayDataGetter=e}snapToOHLC(e,t,n){if(this.magnetMode===`none`||!this.dataGetter)return{time:e,price:t};let r=this.displayDataGetter?.()??this.dataGetter(),i=Math.round(e);if(i<0||i>=r.length)return{time:e,price:t};let a=r[i],o=[a.open,a.high,a.low,a.close],s=o[0],c=Math.abs(t-s);for(let e=1;e<o.length;e++){let n=Math.abs(t-o[e]);n<c&&(c=n,s=o[e])}if(n){let e=n.chartRect.height/(n.priceRange.max-n.priceRange.min||1);if(c*e>30)return{time:i,price:t}}return{time:i,price:s}}undo(){if(!this.undoRedo)return!1;let e=this.undoRedo.undo();return e?(this.applyUndoAction(e),!0):!1}redo(){if(!this.undoRedo)return!1;let e=this.undoRedo.redo();return e?(this.applyRedoAction(e),!0):!1}applyUndoAction(e){switch(e.type){case`drawingCreate`:e.after&&(this.drawings=this.drawings.filter(t=>t.id!==e.after.id));break;case`drawingRemove`:e.before&&this.drawings.push(e.before);break;case`drawingModify`:if(e.before){let t=this.drawings.findIndex(t=>t.id===e.before.id);t>=0&&(this.drawings[t]=structuredClone(e.before))}break}this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()}applyRedoAction(e){switch(e.type){case`drawingCreate`:e.after&&this.drawings.push(structuredClone(e.after));break;case`drawingRemove`:e.before&&(this.drawings=this.drawings.filter(t=>t.id!==e.before.id));break;case`drawingModify`:if(e.after){let t=this.drawings.findIndex(t=>t.id===e.after.id);t>=0&&(this.drawings[t]=structuredClone(e.after))}break}this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()}lockAllDrawings(){for(let e of this.drawings)e.locked=!0;this.requestRender?.()}unlockAllDrawings(){for(let e of this.drawings)e.locked=!1;this.requestRender?.()}hideAllDrawings(){for(let e of this.drawings)e.visible=!1;this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()}showAllDrawings(){for(let e of this.drawings)e.visible=!0;this.requestRender?.()}setActiveTool(e){this.activeTool=e,this.state=e?`creating`:`idle`,this.creatingDrawing=null,this.committedAnchors=0,this.previewAnchor=null}getActiveTool(){return this.activeTool}setStyle(e){Object.assign(this.activeStyle,e)}onPointerDown(e,t){return this.state===`creating`&&this.activeTool?this.handleCreationClick(e,t):this.state===`idle`||this.state===`selected`?this.handleSelectionClick(e,t):!1}onPointerMove(e,t){if(this.state===`creating`&&this.creatingDrawing){let n=this.registry.get(this.creatingDrawing.type);if(n&&this.committedAnchors<n.descriptor.requiredAnchors){let n=s(e.x,t),r=c(e.y,t);return this.previewAnchor=this.snapToOHLC(n,r,t),this.requestRender?.(),!0}}return this.state===`moving`&&this.selectedDrawingId&&this.dragStartPoint?this.handleMove(e,t):this.state===`resizing`&&this.selectedDrawingId&&this.dragAnchorIndex>=0?this.handleResize(e,t):!1}onPointerUp(){if(this.state===`moving`||this.state===`resizing`){if(this.dragBeforeState&&this.selectedDrawingId){let e=this.drawings.find(e=>e.id===this.selectedDrawingId);e&&this.undoRedo?.push({type:`drawingModify`,before:this.dragBeforeState,after:structuredClone(e)})}return this.state=`selected`,this.dragStartPoint=null,this.dragStartAnchors=[],this.dragAnchorIndex=-1,this.dragBeforeState=null,!0}return!1}onKeyDown(e,t=!1){if(t&&(e===`d`||e===`D`)&&this.state===`selected`&&this.selectedDrawingId&&this.duplicateDrawing(this.selectedDrawingId)||t&&(e===`z`||e===`Z`)&&this.undo()||t&&(e===`y`||e===`Y`)&&this.redo())return!0;if(e===`Escape`){if(this.state===`creating`)return this.setActiveTool(null),this.requestRender?.(),!0;if(this.state===`selected`)return this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.(),!0}return(e===`Delete`||e===`Backspace`)&&this.state===`selected`&&this.selectedDrawingId?this.drawings.find(e=>e.id===this.selectedDrawingId)?.locked?!1:(this.removeDrawing(this.selectedDrawingId),this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.(),!0):!1}getDrawings(){return this.drawings}setDrawings(e){this.drawings=e,this.requestRender?.()}removeDrawing(e){let t=this.drawings.find(t=>t.id===e);this.drawings=this.drawings.filter(t=>t.id!==e),t&&this.undoRedo?.push({type:`drawingRemove`,before:structuredClone(t),after:null}),this.eventCallback?.(`drawingRemove`,{id:e}),this.requestRender?.()}duplicateDrawing(e){let t=this.drawings.find(t=>t.id===e);if(!t)return null;let n=structuredClone(t);return n.id=`drawing_${ke++}`,n.anchors=n.anchors.map(e=>({...e,time:e.time+3})),n.locked=!1,this.drawings.push(n),this.undoRedo?.push({type:`drawingCreate`,before:null,after:structuredClone(n)}),this.selectedDrawingId=n.id,this.state=`selected`,this.eventCallback?.(`drawingCreate`,{drawing:n}),this.requestRender?.(),n.id}clearDrawings(){this.drawings=[],this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()}getSelectedDrawingId(){return this.selectedDrawingId}render(e,t){e.save(),e.beginPath(),e.rect(t.chartRect.x,t.chartRect.y,t.chartRect.width,t.chartRect.height),e.clip();for(let n of this.drawings){if(!n.visible)continue;let r=this.registry.get(n.type);if(!r)continue;let i=n.id===this.selectedDrawingId;r.render(e,n,t,i)}if(this.creatingDrawing&&this.committedAnchors>0){let n=this.registry.get(this.creatingDrawing.type);if(n){let r={...this.creatingDrawing,anchors:this.previewAnchor?[...this.creatingDrawing.anchors,this.previewAnchor]:[...this.creatingDrawing.anchors]};n.render(e,r,t,!1)}}e.restore()}handleCreationClick(e,t){if(!this.activeTool)return!1;let n=this.registry.get(this.activeTool);if(!n)return!1;let r=s(e.x,t),i=c(e.y,t),a=this.snapToOHLC(r,i,t);return this.creatingDrawing?(this.creatingDrawing.anchors.push(a),this.committedAnchors++,this.previewAnchor=null,this.committedAnchors>=n.descriptor.requiredAnchors&&this.finalizeCreation(),this.requestRender?.(),!0):(this.creatingDrawing={id:`drawing_${ke++}`,type:this.activeTool,anchors:[a],style:{...this.activeStyle},visible:!0,locked:!1},this.committedAnchors=1,this.previewAnchor=null,n.descriptor.requiredAnchors===1&&this.finalizeCreation(),this.requestRender?.(),!0)}finalizeCreation(){this.creatingDrawing&&(this.drawings.push(this.creatingDrawing),this.selectedDrawingId=this.creatingDrawing.id,this.undoRedo?.push({type:`drawingCreate`,before:null,after:structuredClone(this.creatingDrawing)}),this.eventCallback?.(`drawingCreate`,{drawing:this.creatingDrawing}),this.creatingDrawing=null,this.committedAnchors=0,this.previewAnchor=null,this.activeTool=null,this.state=`selected`)}handleSelectionClick(e,t){if(this.selectedDrawingId){let n=this.drawings.find(e=>e.id===this.selectedDrawingId);if(n&&!n.locked){let r=this.registry.get(n.type);if(r){let i=r.hitTestAnchor(e,n,t,8);if(i>=0)return this.state=`resizing`,this.dragAnchorIndex=i,this.dragStartPoint=e,this.dragBeforeState=structuredClone(n),!0;if(r.hitTest(e,n,t,8))return this.state=`moving`,this.dragStartPoint=e,this.dragStartAnchors=n.anchors.map(e=>({...e})),this.dragBeforeState=structuredClone(n),!0}}}for(let n=this.drawings.length-1;n>=0;n--){let r=this.drawings[n];if(!r.visible)continue;let i=this.registry.get(r.type);if(i&&i.hitTest(e,r,t,8))return this.selectedDrawingId=r.id,this.state=`selected`,this.requestRender?.(),!0}return this.selectedDrawingId&&(this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()),!1}handleMove(e,t){let n=this.drawings.find(e=>e.id===this.selectedDrawingId);if(!n||!this.dragStartPoint)return!1;let r=s(e.x,t)-s(this.dragStartPoint.x,t),i=c(e.y,t)-c(this.dragStartPoint.y,t);return n.anchors=this.dragStartAnchors.map(e=>({time:e.time+r,price:e.price+i})),this.requestRender?.(),!0}handleResize(e,t){let n=this.drawings.find(e=>e.id===this.selectedDrawingId);return!n||this.dragAnchorIndex<0?!1:(n.anchors[this.dragAnchorIndex]={time:s(e.x,t),price:c(e.y,t)},this.requestRender?.(),!0)}},je=class{constructor(e){this.manager=e}render(e,t){this.manager.render(e,t)}},Me=class extends q{descriptor={type:`trendLine`,name:`Trend Line`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);return this.distanceToLine(e,i,a)<=r}},Ne=class extends q{descriptor={type:`horizontalLine`,name:`Horizontal Line`,requiredAnchors:1};render(e,t,n,r){if(t.anchors.length<1)return;let i=o(t.anchors[0].price,n),{chartRect:a}=n;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(a.x,i),e.lineTo(a.x+a.width,i),e.stroke(),this.resetLineStyle(e),e.fillStyle=t.style.color,e.font=`11px sans-serif`,e.textBaseline=`bottom`,e.fillText(t.anchors[0].price.toFixed(2),a.x+4,i-3),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<1)return!1;let i=o(t.anchors[0].price,n);return Math.abs(e.y-i)<=r&&e.x>=n.chartRect.x&&e.x<=n.chartRect.x+n.chartRect.width}},Pe=class extends q{descriptor={type:`verticalLine`,name:`Vertical Line`,requiredAnchors:1};render(e,t,n,r){if(t.anchors.length<1)return;let i=this.anchorToPixel(t.anchors[0],n),{chartRect:a}=n;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,a.y),e.lineTo(i.x,a.y+a.height),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<1)return!1;let i=this.anchorToPixel(t.anchors[0],n),{chartRect:a}=n;return Math.abs(e.x-i.x)<=r&&e.y>=a.y&&e.y<=a.y+a.height}},Fe=class extends q{descriptor={type:`ray`,name:`Ray`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),{chartRect:o}=n,s=a.x-i.x,c=a.y-i.y,l=Math.hypot(s,c);if(l===0)return;let u=Math.hypot(o.width,o.height)*2,d=i.x+s/l*u,f=i.y+c/l*u;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(d,f),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=a.x-i.x,s=a.y-i.y;return((e.x-i.x)*o+(e.y-i.y)*s)/(o*o+s*s)<0?!1:this.distanceToInfiniteLine(e,i,a)<=r}},Ie=class extends q{descriptor={type:`extendedLine`,name:`Extended Line`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),{chartRect:o}=n,s=a.x-i.x,c=a.y-i.y,l=Math.hypot(s,c);if(l===0)return;let u=Math.hypot(o.width,o.height)*2,d=i.x-s/l*u,f=i.y-c/l*u,p=i.x+s/l*u,m=i.y+c/l*u;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(d,f),e.lineTo(p,m),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);return this.distanceToInfiniteLine(e,i,a)<=r}},Le=class extends q{descriptor={type:`parallelChannel`,name:`Parallel Channel`,requiredAnchors:3};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);if(this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke(),t.anchors.length>=3){let r=this.anchorToPixel(t.anchors[2],n),o=a.x-i.x,s=a.y-i.y,c=Math.hypot(o,s)||1,l=-s/c,u=o/c,d=(r.x-i.x)*l+(r.y-i.y)*u,f={x:i.x+l*d,y:i.y+u*d},p={x:a.x+l*d,y:a.y+u*d};e.beginPath(),e.moveTo(f.x,f.y),e.lineTo(p.x,p.y),e.stroke(),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.lineTo(p.x,p.y),e.lineTo(f.x,f.y),e.closePath(),e.fill())}this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);if(this.distanceToLine(e,i,a)<=r)return!0;if(t.anchors.length>=3){let o=this.anchorToPixel(t.anchors[2],n),s=a.x-i.x,c=a.y-i.y,l=Math.hypot(s,c)||1,u=-c/l,d=s/l,f=(o.x-i.x)*u+(o.y-i.y)*d,p={x:i.x+u*f,y:i.y+d*f},m={x:a.x+u*f,y:a.y+d*f};if(this.distanceToLine(e,p,m)<=r)return!0}return!1}},Re=class extends q{descriptor={type:`regressionChannel`,name:`Regression Channel`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke();let o=a.x-i.x,s=a.y-i.y,c=Math.hypot(o,s);if(c===0){this.resetLineStyle(e);return}let l=Math.max(10,Math.min(c*.2,100)),u=-s/c*l,d=o/c*l;e.setLineDash([4,4]),e.beginPath(),e.moveTo(i.x+u,i.y+d),e.lineTo(a.x+u,a.y+d),e.stroke(),e.beginPath(),e.moveTo(i.x-u,i.y-d),e.lineTo(a.x-u,a.y-d),e.stroke(),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.beginPath(),e.moveTo(i.x+u,i.y+d),e.lineTo(a.x+u,a.y+d),e.lineTo(a.x-u,a.y-d),e.lineTo(i.x-u,i.y-d),e.closePath(),e.fill()),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.hypot(a.x-i.x,a.y-i.y),s=Math.max(10,Math.min(o*.2,100));return this.distanceToLine(e,i,a)<=r+s}},J=[0,.236,.382,.5,.618,.786,1],ze=class extends q{descriptor={type:`fibRetracement`,name:`Fibonacci Retracement`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let{chartRect:i}=n,a=t.anchors[0].price,s=t.anchors[1].price,c=a-s;e.font=`11px sans-serif`,e.textBaseline=`middle`;for(let r of J){let a=s+c*(1-r),l=o(a,n);this.applyLineStyle(e,t.style),e.globalAlpha=r===0||r===1?1:.7,e.beginPath(),e.moveTo(i.x,l),e.lineTo(i.x+i.width,l),e.stroke(),e.fillStyle=t.style.color,e.textAlign=`left`,e.fillText(`${(r*100).toFixed(1)}% (${a.toFixed(2)})`,i.x+4,l-2)}if(e.globalAlpha=1,t.style.fillColor){for(let r=0;r<J.length-1;r++){let a=s+c*(1-J[r]),l=s+c*(1-J[r+1]),u=o(a,n),d=o(l,n);e.fillStyle=t.style.fillColor,e.globalAlpha=.05+r*.02,e.fillRect(i.x,Math.min(u,d),i.width,Math.abs(d-u))}e.globalAlpha=1}this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let{chartRect:i}=n;if(e.x<i.x-r||e.x>i.x+i.width+r)return!1;let a=t.anchors[0].price,s=t.anchors[1].price,c=a-s;for(let t of J){let i=o(s+c*(1-t),n);if(Math.abs(e.y-i)<=r)return!0}return!1}},Be=[0,.618,1,1.618,2,2.618,3.618,4.236],Ve=class extends q{descriptor={type:`fibExtension`,name:`Fibonacci Extension`,requiredAnchors:3};render(e,t,n,r){if(t.anchors.length<3){if(t.anchors.length===2){let r=this.anchorToPixel(t.anchors[0],n),i=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(r.x,r.y),e.lineTo(i.x,i.y),e.stroke(),this.resetLineStyle(e)}return}let{chartRect:i}=n,a=t.anchors[0].price,s=t.anchors[1].price,c=t.anchors[2].price,l=a-s;e.font=`11px sans-serif`,e.textBaseline=`middle`,e.textAlign=`left`;for(let r of Be){let a=o(c+l*r,n);this.applyLineStyle(e,t.style),e.globalAlpha=r===0||r===1?1:.6,e.beginPath(),e.moveTo(i.x,a),e.lineTo(i.x+i.width,a),e.stroke(),e.fillStyle=t.style.color,e.fillText(`${(r*100).toFixed(1)}%`,i.x+4,a-2)}e.globalAlpha=1,this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<3)return!1;let{chartRect:i}=n;if(e.x<i.x-r||e.x>i.x+i.width+r)return!1;let a=t.anchors[0].price-t.anchors[1].price;for(let i of Be){let s=o(t.anchors[2].price+a*i,n);if(Math.abs(e.y-s)<=r)return!0}return!1}},He=class extends q{descriptor={type:`rectangle`,name:`Rectangle`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.min(i.y,a.y),c=Math.abs(a.x-i.x),l=Math.abs(a.y-i.y);t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.fillRect(o,s,c,l)),this.applyLineStyle(e,t.style),e.strokeRect(o,s,c,l),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x)-r,s=Math.min(i.y,a.y)-r,c=Math.max(i.x,a.x)+r,l=Math.max(i.y,a.y)+r;if(e.x<o||e.x>c||e.y<s||e.y>l)return!1;let u=Math.min(i.x,a.x)+r,d=Math.min(i.y,a.y)+r,f=Math.max(i.x,a.x)-r,p=Math.max(i.y,a.y)-r;return!(e.x>u&&e.x<f&&e.y>d&&e.y<p)||!!t.style.fillColor}},Ue=class extends q{descriptor={type:`ellipse`,name:`Ellipse`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=(i.x+a.x)/2,s=(i.y+a.y)/2,c=Math.abs(a.x-i.x)/2,l=Math.abs(a.y-i.y)/2;e.beginPath(),e.ellipse(o,s,Math.max(c,1),Math.max(l,1),0,0,Math.PI*2),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.fill()),this.applyLineStyle(e,t.style),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=(i.x+a.x)/2,s=(i.y+a.y)/2,c=Math.max(Math.abs(a.x-i.x)/2,1),l=Math.max(Math.abs(a.y-i.y)/2,1),u=(e.x-o)/c,d=(e.y-s)/l,f=Math.sqrt(u*u+d*d),p=r/Math.max(Math.min(c,l),4);return t.style.fillColor?f<=1+p:Math.abs(f-1)<=p}},We=class extends q{descriptor={type:`triangle`,name:`Triangle`,requiredAnchors:3};render(e,t,n,r){if(t.anchors.length<3){if(t.anchors.length===2){let r=this.anchorToPixel(t.anchors[0],n),i=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(r.x,r.y),e.lineTo(i.x,i.y),e.stroke(),this.resetLineStyle(e)}return}let i=t.anchors.map(e=>this.anchorToPixel(e,n));e.beginPath(),e.moveTo(i[0].x,i[0].y),e.lineTo(i[1].x,i[1].y),e.lineTo(i[2].x,i[2].y),e.closePath(),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.fill()),this.applyLineStyle(e,t.style),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<3)return!1;let i=t.anchors.map(e=>this.anchorToPixel(e,n));for(let t=0;t<3;t++){let n=(t+1)%3;if(this.distanceToLine(e,i[t],i[n])<=r)return!0}return!1}},Ge=class extends q{descriptor={type:`pitchfork`,name:`Andrews' Pitchfork`,requiredAnchors:3};render(e,t,n,r){if(t.anchors.length<3){if(t.anchors.length===2){let r=this.anchorToPixel(t.anchors[0],n),i=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(r.x,r.y),e.lineTo(i.x,i.y),e.stroke(),this.resetLineStyle(e)}return}let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=this.anchorToPixel(t.anchors[2],n),s={x:(a.x+o.x)/2,y:(a.y+o.y)/2},c=s.x-i.x,l=s.y-i.y,u=Math.hypot(c,l)||1,d=Math.hypot(n.chartRect.width,n.chartRect.height),f=i.x+c/u*d,p=i.y+l/u*d;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(f,p),e.stroke(),e.beginPath(),e.moveTo(a.x,a.y),e.lineTo(a.x+c/u*d,a.y+l/u*d),e.stroke(),e.beginPath(),e.moveTo(o.x,o.y),e.lineTo(o.x+c/u*d,o.y+l/u*d),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<3)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=this.anchorToPixel(t.anchors[2],n),s={x:(a.x+o.x)/2,y:(a.y+o.y)/2};if(this.distanceToInfiniteLine(e,i,s)<=r)return!0;let c=s.x-i.x,l=s.y-i.y,u={x:a.x+c,y:a.y+l},d={x:o.x+c,y:o.y+l};return this.distanceToInfiniteLine(e,a,u)<=r||this.distanceToInfiniteLine(e,o,d)<=r}},Ke=[`1`,`2`,`3`,`4`,`5`,`A`,`B`,`C`],qe=class extends q{descriptor={type:`elliottWave`,name:`Elliott Wave`,requiredAnchors:8};render(e,t,n,r){if(t.anchors.length<2)return;let i=t.anchors.map(e=>this.anchorToPixel(e,n));this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i[0].x,i[0].y);for(let t=1;t<i.length;t++)e.lineTo(i[t].x,i[t].y);e.stroke(),this.resetLineStyle(e),e.font=`bold 12px sans-serif`,e.fillStyle=t.style.color,e.textAlign=`center`;for(let t=0;t<i.length&&t<Ke.length;t++){let n=t%2==0?-12:12;e.textBaseline=n<0?`bottom`:`top`,e.fillText(Ke[t],i[t].x,i[t].y+n)}r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=t.anchors.map(e=>this.anchorToPixel(e,n));for(let t=0;t<i.length-1;t++)if(this.distanceToLine(e,i[t],i[t+1])<=r)return!0;return!1}},Je=class extends q{descriptor={type:`priceRange`,name:`Price Range`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.abs(a.x-i.x),c=Math.min(i.y,a.y),l=Math.abs(a.y-i.y),u=t.anchors[1].price>t.anchors[0].price;e.fillStyle=u?`rgba(38, 166, 154, 0.15)`:`rgba(239, 83, 80, 0.15)`,e.fillRect(o,c,s,l),this.applyLineStyle(e,t.style),e.strokeRect(o,c,s,l),this.resetLineStyle(e);let d=t.anchors[1].price-t.anchors[0].price,f=t.anchors[0].price===0?0:d/t.anchors[0].price*100,p=`${d>=0?`+`:``}${d.toFixed(2)} (${f>=0?`+`:``}${f.toFixed(2)}%)`;e.font=`12px sans-serif`,e.fillStyle=u?`#26A69A`:`#EF5350`,e.textAlign=`center`,e.textBaseline=`middle`,e.fillText(p,o+s/2,c+l/2),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.max(i.x,a.x),c=Math.min(i.y,a.y),l=Math.max(i.y,a.y);return e.x>=o-r&&e.x<=s+r&&e.y>=c-r&&e.y<=l+r}},Ye=class extends q{descriptor={type:`dateRange`,name:`Date Range`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),{chartRect:o}=n,s=Math.min(i.x,a.x),c=Math.max(i.x,a.x);e.fillStyle=`rgba(33, 150, 243, 0.08)`,e.fillRect(s,o.y,c-s,o.height),this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(s,o.y),e.lineTo(s,o.y+o.height),e.stroke(),e.beginPath(),e.moveTo(c,o.y),e.lineTo(c,o.y+o.height),e.stroke(),this.resetLineStyle(e);let l=Math.abs(t.anchors[1].time-t.anchors[0].time);e.font=`12px sans-serif`,e.fillStyle=t.style.color,e.textAlign=`center`,e.textBaseline=`top`,e.fillText(`${l} bars`,(s+c)/2,o.y+4),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),{chartRect:o}=n,s=Math.min(i.x,a.x),c=Math.max(i.x,a.x);return e.x>=s-r&&e.x<=c+r&&e.y>=o.y&&e.y<=o.y+o.height}},Xe=class extends q{descriptor={type:`measure`,name:`Measure`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.setLineDash([4,4]),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke(),this.resetLineStyle(e);let o=t.anchors[1].price-t.anchors[0].price,s=Math.abs(t.anchors[1].time-t.anchors[0].time),c=t.anchors[0].price===0?0:o/t.anchors[0].price*100,l=[`Price: ${o>=0?`+`:``}${o.toFixed(2)} (${c>=0?`+`:``}${c.toFixed(2)}%)`,`Bars: ${s}`],u=(i.x+a.x)/2,d=(i.y+a.y)/2;e.font=`11px sans-serif`,e.textAlign=`center`,e.textBaseline=`bottom`;let f=Math.max(...l.map(t=>e.measureText(t).width));e.fillStyle=`rgba(0, 0, 0, 0.7)`,e.fillRect(u-f/2-6,d-l.length*14-4,f+12,l.length*14+8),e.fillStyle=`#FFFFFF`;for(let t=0;t<l.length;t++)e.fillText(l[t],u,d-(l.length-1-t)*14);r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);return this.distanceToLine(e,i,a)<=r}},Ze=class extends q{descriptor={type:`text`,name:`Text`,requiredAnchors:1};render(e,t,n,r){if(t.anchors.length<1)return;let i=this.anchorToPixel(t.anchors[0],n),a=t.style.text||t.meta?.text||`Text`,o=t.style.fontSize||14;if(e.font=`${o}px sans-serif`,e.fillStyle=t.style.color,e.textAlign=`left`,e.textBaseline=`top`,r){let n=e.measureText(a);e.strokeStyle=t.style.color,e.lineWidth=1,e.setLineDash([3,3]),e.strokeRect(i.x-2,i.y-2,n.width+4,o+4),e.setLineDash([])}e.fillText(a,i.x,i.y),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<1)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=t.style.fontSize||14;return e.x>=i.x-r&&e.x<=i.x+80+r&&e.y>=i.y-r&&e.y<=i.y+a+r}},Qe=class extends q{descriptor={type:`arrow`,name:`Arrow`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke();let o=Math.atan2(a.y-i.y,a.x-i.x);e.beginPath(),e.moveTo(a.x,a.y),e.lineTo(a.x-12*Math.cos(o-Math.PI/6),a.y-12*Math.sin(o-Math.PI/6)),e.lineTo(a.x-12*Math.cos(o+Math.PI/6),a.y-12*Math.sin(o+Math.PI/6)),e.closePath(),e.fillStyle=t.style.color,e.fill(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);return this.distanceToLine(e,i,a)<=r}},$e=[{label:`8x1`,ratio:8},{label:`4x1`,ratio:4},{label:`3x1`,ratio:3},{label:`2x1`,ratio:2},{label:`1x1`,ratio:1},{label:`1x2`,ratio:.5},{label:`1x3`,ratio:1/3},{label:`1x4`,ratio:.25},{label:`1x8`,ratio:.125}],et=class extends q{descriptor={type:`gannFan`,name:`Gann Fan`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=a.x-i.x,s=a.y-i.y;if(Math.abs(o)<1)return;let c=s/o,l=o>0?1:-1,{chartRect:u}=n,d=l>0?u.x+u.width:u.x;this.applyLineStyle(e,t.style);for(let{label:n,ratio:r}of $e){let a=c*r,o=d,s=i.y+a*(o-i.x);e.globalAlpha=r===1?1:.5,e.lineWidth=r===1?t.style.lineWidth:Math.max(.5,t.style.lineWidth*.7),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(o,s),e.stroke();let f=o-l*4,p=s;p>u.y&&p<u.y+u.height&&(e.globalAlpha=.7,e.fillStyle=t.style.color,e.font=`9px sans-serif`,e.textAlign=l>0?`right`:`left`,e.textBaseline=`bottom`,e.fillText(n,f,p-2))}e.globalAlpha=1,this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=a.x-i.x,s=a.y-i.y;if(Math.abs(o)<1)return!1;let c=s/o,l=o>0?1:-1,{chartRect:u}=n,d=l>0?u.x+u.width:u.x;for(let{ratio:t}of $e){let n=c*t,a=d,o=i.y+n*(a-i.x);if(this.distanceToLine(e,i,{x:a,y:o})<=r)return!0}return!1}},Y=[0,.25,.333,.5,.667,.75,1],tt=class extends q{descriptor={type:`gannBox`,name:`Gann Box`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.max(i.x,a.x),c=Math.min(i.y,a.y),l=Math.max(i.y,a.y),u=s-o,d=l-c;if(!(u<2||d<2)){this.applyLineStyle(e,t.style),e.strokeRect(o,c,u,d),e.save(),e.globalAlpha=.35;for(let t of Y){if(t===0||t===1)continue;let n=c+d*t;e.beginPath(),e.moveTo(o,n),e.lineTo(s,n),e.stroke()}for(let t of Y){if(t===0||t===1)continue;let n=o+u*t;e.beginPath(),e.moveTo(n,c),e.lineTo(n,l),e.stroke()}e.globalAlpha=.5,e.setLineDash([4,3]),e.beginPath(),e.moveTo(o,c),e.lineTo(s,l),e.stroke(),e.beginPath(),e.moveTo(s,c),e.lineTo(o,l),e.stroke(),e.restore(),this.resetLineStyle(e),e.fillStyle=t.style.color,e.font=`8px sans-serif`,e.globalAlpha=.6,e.textBaseline=`bottom`,e.textAlign=`right`;for(let t of Y){if(t===0||t===1)continue;let n=c+d*t;e.fillText(`${(t*100).toFixed(0)}%`,o-3,n)}e.globalAlpha=1,r&&this.renderAnchorHandles(e,t,n)}}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.max(i.x,a.x),c=Math.min(i.y,a.y),l=Math.max(i.y,a.y);return Math.abs(e.x-o)<=r&&e.y>=c-r&&e.y<=l+r||Math.abs(e.x-s)<=r&&e.y>=c-r&&e.y<=l+r||Math.abs(e.y-c)<=r&&e.x>=o-r&&e.x<=s+r||Math.abs(e.y-l)<=r&&e.x>=o-r&&e.x<=s+r||this.distanceToLine(e,{x:o,y:c},{x:s,y:l})<=r||this.distanceToLine(e,{x:s,y:c},{x:o,y:l})<=r}},nt=class extends q{descriptor={type:`anchoredVWAP`,name:`Anchored VWAP`,requiredAnchors:1};dataGetter=null;showBands=!0;setDataGetter(e){this.dataGetter=e}setShowBands(e){this.showBands=e}render(e,t,n,r){if(t.anchors.length<1||!this.dataGetter)return;let i=this.dataGetter(),s=Math.max(0,Math.round(t.anchors[0].time));if(s>=i.length)return;let{vwap:c,upper1:l,lower1:u,upper2:d,lower2:f}=this.computeVWAP(i,s),{chartRect:p}=n,m=n.barWidth+n.barSpacing;if(this.applyLineStyle(e,t.style),e.lineWidth=t.style.lineWidth+.5,this.drawLine(e,c,s,n,p,m),this.showBands&&c.length>1&&(e.globalAlpha=.4,e.lineWidth=Math.max(.5,t.style.lineWidth*.6),e.setLineDash([4,3]),this.drawLine(e,l,s,n,p,m),this.drawLine(e,u,s,n,p,m),e.globalAlpha=.25,this.drawLine(e,d,s,n,p,m),this.drawLine(e,f,s,n,p,m),e.globalAlpha=1),this.resetLineStyle(e),c.length>0){let r=c[c.length-1],i=a(s+c.length-1,n),l=o(r,n);i>=p.x&&i<=p.x+p.width&&(e.fillStyle=t.style.color,e.font=`bold 9px sans-serif`,e.textAlign=`left`,e.textBaseline=`middle`,e.fillText(`AVWAP ${r.toFixed(2)}`,i+6,l))}r&&this.renderAnchorHandles(e,t,n)}drawLine(e,t,n,r,i,s){e.beginPath();let c=!1;for(let l=0;l<t.length;l++){let u=a(n+l,r);if(u<i.x-s)continue;if(u>i.x+i.width+s)break;let d=o(t[l],r);c?e.lineTo(u,d):(e.moveTo(u,d),c=!0)}e.stroke()}computeVWAP(e,t){let n=e.length-t,r=Array(n),i=Array(n),a=Array(n),o=Array(n),s=Array(n),c=0,l=0,u=0;for(let d=0;d<n;d++){let n=e[t+d],f=(n.high+n.low+n.close)/3,p=n.volume??1;c+=f*p,l+=p,u+=f*f*p;let m=l>0?c/l:f;r[d]=m;let h=l>0?u/l-m*m:0,g=Math.sqrt(Math.max(0,h));i[d]=m+g,a[d]=m-g,o[d]=m+2*g,s[d]=m-2*g}return{vwap:r,upper1:i,lower1:a,upper2:o,lower2:s}}hitTest(e,t,n,r){if(t.anchors.length<1||!this.dataGetter)return!1;let i=this.dataGetter(),s=Math.max(0,Math.round(t.anchors[0].time));if(s>=i.length)return!1;let{vwap:c}=this.computeVWAP(i,s);for(let t=1;t<c.length;t++){let i=a(s+t-1,n),l=o(c[t-1],n),u=a(s+t,n),d=o(c[t],n);if(this.distanceToLine(e,{x:i,y:l},{x:u,y:d})<=r)return!0}return!1}},rt=class extends q{descriptor={type:`volumeProfileRange`,name:`Volume Profile (Range)`,requiredAnchors:2};dataGetter=null;numBins=40;setDataGetter(e){this.dataGetter=e}render(e,t,n,r){if(t.anchors.length<2||!this.dataGetter)return;let i=this.dataGetter(),s=Math.max(0,Math.min(Math.round(t.anchors[0].time),Math.round(t.anchors[1].time))),c=Math.min(i.length-1,Math.max(Math.round(t.anchors[0].time),Math.round(t.anchors[1].time)));if(s>=i.length||c<0||s>c)return;let l=1/0,u=-1/0;for(let e=s;e<=c;e++)i[e].low<l&&(l=i[e].low),i[e].high>u&&(u=i[e].high);if(l>=u)return;let d=(u-l)/this.numBins,f=new Float64Array(this.numBins),p=0;for(let e=s;e<=c;e++){let t=i[e],n=t.volume??1,r=Math.max(0,Math.floor((t.low-l)/d)),a=Math.min(this.numBins-1,Math.floor((t.high-l)/d)),o=n/(a-r+1);for(let e=r;e<=a;e++)f[e]+=o,f[e]>p&&(p=f[e])}if(p===0)return;let m=0;for(let e=1;e<this.numBins;e++)f[e]>f[m]&&(m=e);let h=a(c,n)+n.barWidth,g=t.style.color,_=t.style.fillColor??`#FF9800`;e.save();for(let t=0;t<this.numBins;t++){let r=f[t]/p*100,i=l+(t+1)*d,a=l+t*d,s=o(i,n),c=o(a,n),u=Math.abs(c-s);e.globalAlpha=.35,e.fillStyle=t===m?_:g,e.fillRect(h,Math.min(s,c),r,Math.max(1,u-.5))}let v=l+(m+.5)*d,y=o(v,n);e.globalAlpha=.7,e.strokeStyle=_,e.lineWidth=1,e.setLineDash([4,3]),e.beginPath();let b=a(s,n)-n.barWidth/2;e.moveTo(b,y),e.lineTo(h+100,y),e.stroke(),e.fillStyle=_,e.font=`9px sans-serif`,e.textAlign=`left`,e.textBaseline=`bottom`,e.globalAlpha=.8,e.fillText(`POC ${v.toFixed(2)}`,h+100+4,y-2),e.restore(),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x)-r,s=Math.max(i.x,a.x)+100+r,c=Math.min(i.y,a.y)-r,l=Math.max(i.y,a.y)+r;return e.x>=o&&e.x<=s&&e.y>=c&&e.y<=l}};function it(e){e.register(new Me),e.register(new Ne),e.register(new Pe),e.register(new Fe),e.register(new Ie),e.register(new Le),e.register(new Re),e.register(new ze),e.register(new Ve),e.register(new He),e.register(new Ue),e.register(new We),e.register(new Ge),e.register(new qe),e.register(new Je),e.register(new Ye),e.register(new Xe),e.register(new Ze),e.register(new Qe),e.register(new et),e.register(new tt),e.register(new nt),e.register(new rt)}var at=class{render(e,t,n,r,i,a){let s=i.orderColors?.buy??`#26A69A`,c=i.orderColors?.sell??`#EF5350`,{chartRect:l}=n;for(let i of t){let t=a?.orderId===i.id&&a.sourceType===`order`,u=o(t?a.currentPrice:i.price,n);if(u<l.y||u>l.y+l.height)continue;let d=i.side===`buy`?s:c;e.setLineDash([6,4]),e.strokeStyle=d,e.lineWidth=1,e.beginPath(),e.moveTo(l.x,Math.round(u)+.5),e.lineTo(l.x+l.width,Math.round(u)+.5),e.stroke(),e.setLineDash([]);let f=i.label??i.type.toUpperCase();e.font=`bold 10px ${r.font.family}`;let p=e.measureText(f).width+8;e.fillStyle=d,e.fillRect(l.x+4,u-8,p,16),e.fillStyle=`#FFFFFF`,e.textBaseline=`middle`,e.textAlign=`left`,e.fillText(f,l.x+8,u);let m=l.x+4+p+6;if(e.fillStyle=d,e.beginPath(),i.side===`buy`?(e.moveTo(m,u+4),e.lineTo(m+5,u-2),e.lineTo(m+10,u+4)):(e.moveTo(m,u-4),e.lineTo(m+5,u+2),e.lineTo(m+10,u-4)),e.fill(),t){let t=o(a.startPrice,n);e.setLineDash([2,4]),e.strokeStyle=d,e.globalAlpha=.3,e.beginPath(),e.moveTo(l.x,Math.round(t)+.5),e.lineTo(l.x+l.width,Math.round(t)+.5),e.stroke(),e.globalAlpha=1,e.setLineDash([])}}}renderAxisBadges(t,n,r,i,a,s){let c=a.orderColors?.buy??`#26A69A`,l=a.orderColors?.sell??`#EF5350`,u=a.pricePrecision??2,{chartRect:d}=r,f=d.x+d.width+1;for(let a of n){let n=s?.orderId===a.id&&s.sourceType===`order`?s.currentPrice:a.price,p=o(n,r);if(p<d.y||p>d.y+d.height)continue;let m=a.side===`buy`?c:l,h=`${n.toFixed(u)} x ${a.quantity}`;t.font=`11px ${i.font.family}`;let g=t.measureText(h).width+10;t.fillStyle=m,t.fillRect(f,p-9,Math.min(g,e.PRICE_AXIS_WIDTH-2),18),t.fillStyle=`#FFFFFF`,t.textAlign=`left`,t.textBaseline=`middle`,t.fillText(h,f+5,p)}}hitTest(e,t,n,r){for(let i of t){if(i.draggable===!1)continue;let t=o(i.price,n);if(Math.abs(e-t)<=r)return i}return null}},ot=class{render(e,t,n,r,i,a){let{chartRect:s}=r,c=a.positionColors?.profit??`#26A69A`,l=a.positionColors?.loss??`#EF5350`,u=a.positionColors?.entry??`#2196F3`,d=a.pricePrecision??2;for(let a of t){let t=o(a.entryPrice,r);if(e.strokeStyle=u,e.lineWidth=1.5,e.setLineDash([]),e.beginPath(),e.moveTo(s.x,Math.round(t)+.5),e.lineTo(s.x+s.width,Math.round(t)+.5),e.stroke(),n!==null){let u=o(n,r),f=(n-a.entryPrice)*a.quantity*(a.side===`buy`?1:-1),p=f>=0;e.fillStyle=p?c:l,e.globalAlpha=.08;let m=Math.min(t,u),h=Math.abs(u-t);e.fillRect(s.x,m,s.width,h),e.globalAlpha=1;let g=`${a.side.toUpperCase()} ${a.quantity} | P&L: ${f>=0?`+`:``}${f.toFixed(d)}`;e.font=`bold 11px ${i.font.family}`;let _=e.measureText(g).width+12,v=s.x+8;e.fillStyle=p?c:l,e.globalAlpha=.9,e.fillRect(v,t-10,_,20),e.globalAlpha=1,e.fillStyle=`#FFFFFF`,e.textBaseline=`middle`,e.textAlign=`left`,e.fillText(g,v+6,t)}if(a.stopLoss!==void 0){let t=o(a.stopLoss,r);e.setLineDash([4,4]),e.strokeStyle=l,e.lineWidth=1,e.beginPath(),e.moveTo(s.x,Math.round(t)+.5),e.lineTo(s.x+s.width,Math.round(t)+.5),e.stroke(),e.font=`bold 10px ${i.font.family}`,e.fillStyle=l,e.fillRect(s.x+4,t-8,24,16),e.fillStyle=`#FFFFFF`,e.textAlign=`left`,e.textBaseline=`middle`,e.fillText(`SL`,s.x+8,t)}if(a.takeProfit!==void 0){let t=o(a.takeProfit,r);e.setLineDash([4,4]),e.strokeStyle=c,e.lineWidth=1,e.beginPath(),e.moveTo(s.x,Math.round(t)+.5),e.lineTo(s.x+s.width,Math.round(t)+.5),e.stroke(),e.font=`bold 10px ${i.font.family}`,e.fillStyle=c,e.fillRect(s.x+4,t-8,24,16),e.fillStyle=`#FFFFFF`,e.textAlign=`left`,e.textBaseline=`middle`,e.fillText(`TP`,s.x+8,t)}e.setLineDash([])}}renderAxisBadges(t,n,r,i,a){let{chartRect:s}=r,c=a.positionColors?.entry??`#2196F3`,l=a.pricePrecision??2,u=s.x+s.width+1;for(let a of n){let n=o(a.entryPrice,r);t.fillStyle=c,t.fillRect(u,n-9,e.PRICE_AXIS_WIDTH-2,18),t.fillStyle=`#FFFFFF`,t.font=`11px ${i.font.family}`,t.textAlign=`left`,t.textBaseline=`middle`,t.fillText(a.entryPrice.toFixed(l),u+5,n)}}},st=class{render(e,t,n,r){if(!r.depthOverlay?.enabled)return;let{chartRect:i}=n,a=r.depthOverlay.bidColor??`rgba(38,166,154,0.15)`,s=r.depthOverlay.askColor??`rgba(239,83,80,0.15)`,c=r.depthOverlay.maxWidth??100,l=0,u=[],d=0;for(let e of t.bids)d+=e.volume,u.push({price:e.price,cumVol:d}),d>l&&(l=d);let f=[];d=0;for(let e of t.asks)d+=e.volume,f.push({price:e.price,cumVol:d}),d>l&&(l=d);if(l===0)return;let p=Math.max(1,t.bids.length>1?Math.abs(o(t.bids[0].price,n)-o(t.bids[1].price,n)):4),m=i.x+i.width;e.fillStyle=a;for(let{price:t,cumVol:r}of u){let a=o(t,n);if(a<i.y||a>i.y+i.height)continue;let s=r/l*c;e.fillRect(m-s,a-p/2,s,p)}e.fillStyle=s;for(let{price:t,cumVol:r}of f){let a=o(t,n);if(a<i.y||a>i.y+i.height)continue;let s=r/l*c;e.fillRect(m-s,a-p/2,s,p)}}},ct=class{dragState=null;startY=0;dragging=!1;constructor(e=3){this.dragThreshold=e}onPointerDown(e,t,n,r,i){for(let n of t){if(n.draggable===!1)continue;let t=o(n.price,r);if(Math.abs(e.y-t)<=i)return this.dragState={orderId:n.id,sourceType:`order`,startPrice:n.price,currentPrice:n.price},this.startY=e.y,this.dragging=!1,!0}for(let t of n){if(t.stopLoss!==void 0){let n=o(t.stopLoss,r);if(Math.abs(e.y-n)<=i)return this.dragState={orderId:t.id,sourceType:`stopLoss`,startPrice:t.stopLoss,currentPrice:t.stopLoss},this.startY=e.y,this.dragging=!1,!0}if(t.takeProfit!==void 0){let n=o(t.takeProfit,r);if(Math.abs(e.y-n)<=i)return this.dragState={orderId:t.id,sourceType:`takeProfit`,startPrice:t.takeProfit,currentPrice:t.takeProfit},this.startY=e.y,this.dragging=!1,!0}}return!1}onPointerMove(e,t){if(!this.dragState)return!1;if(!this.dragging){if(Math.abs(e.y-this.startY)<this.dragThreshold)return!0;this.dragging=!0}return this.dragState.currentPrice=c(e.y,t),!0}onPointerUp(){if(!this.dragState)return null;let e=this.dragState;return this.dragState=null,this.dragging?(this.dragging=!1,{sourceType:e.sourceType,id:e.orderId,newPrice:e.currentPrice,previousPrice:e.startPrice}):null}getDragState(){return this.dragState}isActive(){return this.dragState!==null}},lt=class{menuElement=null;removeHandler=null;onItemSelect=null;show(e,t,n,r){if(this.hide(),!r.contextMenu?.enabled)return;let i=r.pricePrecision??2,a=t.toFixed(i),o=document.createElement(`div`);o.style.cssText=`
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@tradecanvas/commons`);var t=class{canvas;ctx;dpr=1;constructor(e,t){this.canvas=document.createElement(`canvas`),this.canvas.style.position=`absolute`,this.canvas.style.top=`0`,this.canvas.style.left=`0`,this.canvas.style.zIndex=String(t),this.canvas.style.pointerEvents=`none`,this.ctx=this.canvas.getContext(`2d`),e.appendChild(this.canvas)}resize(e,t){this.dpr=t;let n=Math.round(e.width*t),r=Math.round(e.height*t);(this.canvas.width!==n||this.canvas.height!==r)&&(this.canvas.width=n,this.canvas.height=r),this.canvas.style.width=`${e.width}px`,this.canvas.style.height=`${e.height}px`,this.ctx.setTransform(t,0,0,t,0,0)}clear(){this.ctx.setTransform(1,0,0,1,0,0),this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.setTransform(this.dpr,0,0,this.dpr,0,0)}getSize(){return{width:this.canvas.width/this.dpr,height:this.canvas.height/this.dpr}}destroy(){this.canvas.remove()}},n=class{layers=new Map;constructor(e){this.container=e}createLayers(){this.destroy();for(let n of[e.LayerType.Background,e.LayerType.Main,e.LayerType.Overlay,e.LayerType.UI])this.layers.set(n,new t(this.container,n))}getLayer(e){return this.layers.get(e)}resize(e,t){for(let n of this.layers.values())n.resize(e,t)}destroy(){for(let e of this.layers.values())e.destroy();this.layers.clear()}},r=class{animFrameId=0;running=!1;dirtyLayers=new Set;swapLayers=new Set;callback=null;setCallback(e){this.callback=e}markDirty(e){this.dirtyLayers.add(e),this.running&&!this.animFrameId&&this.scheduleFrame()}markAllDirty(){this.dirtyLayers.add(e.LayerType.Background),this.dirtyLayers.add(e.LayerType.Main),this.dirtyLayers.add(e.LayerType.Overlay),this.dirtyLayers.add(e.LayerType.UI),this.running&&!this.animFrameId&&this.scheduleFrame()}start(){this.running=!0,this.markAllDirty()}stop(){this.running=!1,this.animFrameId&&=(cancelAnimationFrame(this.animFrameId),0)}scheduleFrame(){this.animFrameId=requestAnimationFrame(()=>{if(this.animFrameId=0,!(!this.running||!this.callback)&&this.dirtyLayers.size>0){let e=this.dirtyLayers;this.dirtyLayers=this.swapLayers,this.callback(e),e.clear(),this.swapLayers=e}})}},i=class{observer=null;callback=null;currentDpr=1;resizeScheduled=!1;cachedSize;constructor(e){this.container=e,this.currentDpr=window.devicePixelRatio||1,this.cachedSize={width:e.clientWidth,height:e.clientHeight}}getDpr(){return this.currentDpr}getContainerSize(){return this.cachedSize}readContainerSize(){let e=this.container.clientWidth,t=this.container.clientHeight;return(e!==this.cachedSize.width||t!==this.cachedSize.height)&&(this.cachedSize={width:e,height:t}),this.cachedSize}onResize(e){this.callback=e,this.observer=new ResizeObserver(()=>{this.resizeScheduled||(this.resizeScheduled=!0,requestAnimationFrame(()=>{this.resizeScheduled=!1,this.currentDpr=window.devicePixelRatio||1;let e=this.container.clientWidth,t=this.container.clientHeight;e===this.cachedSize.width&&t===this.cachedSize.height||(this.cachedSize={width:e,height:t},this.callback?.(this.cachedSize,this.currentDpr))}))}),this.observer.observe(this.container)}destroy(){this.observer?.disconnect(),this.observer=null,this.callback=null}};function a(e,t){return e*(t.barWidth+t.barSpacing)-t.offset+t.chartRect.x+t.barWidth/2}function o(e,t){let{min:n,max:r}=t.priceRange;if(t.logScale&&n>0&&r>0){let i=Math.log(n),a=Math.log(r)-i;if(a===0)return t.chartRect.y+t.chartRect.height/2;let o=(Math.log(Math.max(e,2**-52))-i)/a;return t.chartRect.y+t.chartRect.height*(1-o)}let i=r-n;if(i===0)return t.chartRect.y+t.chartRect.height/2;let a=(e-n)/i;return t.chartRect.y+t.chartRect.height*(1-a)}function s(e,t){let n=t.barWidth+t.barSpacing;return Math.round((e-t.chartRect.x-t.barWidth/2+t.offset)/n)}function c(e,t){let{min:n,max:r}=t.priceRange,i=1-(e-t.chartRect.y)/t.chartRect.height;if(t.logScale&&n>0&&r>0){let e=Math.log(n);return Math.exp(e+(Math.log(r)-e)*i)}return n+(r-n)*i}function l(e,t){if(t.length===0)return-1;if(e<=t[0].time)return 0;if(e>=t[t.length-1].time)return t.length-1;let n=0,r=t.length-1;for(;n<=r;){let i=n+r>>>1;if(t[i].time===e)return i;t[i].time<e?n=i+1:r=i-1}if(n>=t.length)return t.length-1;if(r<0)return 0;let i=t[r].time,a=t[n].time,o=a===i?0:(e-i)/(a-i);return r+o}var u=class{layerManager;renderLoop;dprManager;renderCtx=null;bgLayer;mainLayer;overlayLayer;uiLayer;onContainerResize=null;constructor(e){this.layerManager=new n(e),this.renderLoop=new r,this.dprManager=new i(e),this.layerManager.createLayers(),this.cacheLayerRefs(),this.renderLoop.setCallback(e=>this.render(e)),this.dprManager.onResize((e,t)=>{this.layerManager.resize(e,t),this.renderLoop.markAllDirty(),this.onContainerResize?.(e)});let t=this.dprManager.getContainerSize(),a=this.dprManager.getDpr();this.layerManager.resize(t,a)}cacheLayerRefs(){this.bgLayer=this.layerManager.getLayer(e.LayerType.Background),this.mainLayer=this.layerManager.getLayer(e.LayerType.Main),this.overlayLayer=this.layerManager.getLayer(e.LayerType.Overlay),this.uiLayer=this.layerManager.getLayer(e.LayerType.UI)}setRenderContext(e){this.renderCtx=e}start(){this.renderLoop.start()}stop(){this.renderLoop.stop()}requestRender(e){e===void 0?this.renderLoop.markAllDirty():this.renderLoop.markDirty(e)}render(t){let n=this.renderCtx;if(!n)return;let{viewport:r,theme:i,data:l}=n;if(!(r.chartRect.width<=0||r.chartRect.height<=0)){if(t.has(e.LayerType.Background)&&this.bgLayer&&(this.bgLayer.clear(),n.gridRenderer?.render(this.bgLayer.ctx,r,i),n.sessionBreaks?.render(this.bgLayer.ctx,r,i,l),n.watermark?.render(this.bgLayer.ctx,r,i)),t.has(e.LayerType.Main)&&this.mainLayer){this.mainLayer.clear();let e=this.mainLayer.ctx;e.save(),e.beginPath(),e.rect(r.chartRect.x,r.chartRect.y,r.chartRect.width,r.chartRect.height),e.clip(),n.volumeRenderer?.render(e,l,r,i),n.chartRenderer?.render(e,l,r,i),n.compareRenderer?.render(e,l,r,i),n.indicatorEngine?.renderOverlays(e,r),e.restore();let t=n.panels,a=n.indicatorEngine;if(a&&t.length>0){let n=`10px ${i.font.family}`,r=a.getPanelIndicators(),o=new Map;for(let e of r)o.set(e.instanceId,e);for(let r of t){if(r.rect.width<=0||r.rect.height<=0)continue;e.save(),e.beginPath(),e.rect(r.rect.x,r.rect.y,r.rect.width,r.rect.height),e.clip(),e.fillStyle=i.background,e.fillRect(r.rect.x,r.rect.y,r.rect.width,r.rect.height),e.fillStyle=i.axisLine,e.fillRect(r.rect.x,r.rect.y,r.rect.width,3),e.fillStyle=i.textSecondary,e.font=n,e.textBaseline=`top`,e.textAlign=`left`;let t=o.get(r.instanceId);t&&e.fillText(t.descriptor.name,r.rect.x+6,r.rect.y+6),e.save(),e.beginPath(),e.rect(r.rect.x,r.rect.y+20,r.rect.width,r.rect.height-20),e.clip(),a.renderPanel(e,r.instanceId,r.viewport),e.restore(),e.restore()}}}if(t.has(e.LayerType.Overlay)&&this.overlayLayer){this.overlayLayer.clear();let e=this.overlayLayer.ctx;n.priceLimits&&this.renderPriceLimits(e,r,i,n.priceLimits),n.drawingRenderer?.render(e,r),n.tradingRenderer?.render(e,r,i),n.crosshairHandler?.render(e,r,i);let t=n.crosshairHandler?.getPosition();if(t&&n.panels.length>0)for(let r of n.panels){let o=r.rect;if(t.x>=o.x&&t.x<=o.x+o.width&&t.y>=o.y&&t.y<=o.y+o.height){let c=r.viewport,l=t.x,u=s(l,c);l=a(Math.max(0,Math.min((n.data?.length??1)-1,Math.round(u))),c),e.save(),e.beginPath(),e.rect(o.x,o.y,o.width,o.height),e.clip(),e.setLineDash([4,4]),e.strokeStyle=i.crosshair,e.lineWidth=1,e.beginPath(),e.moveTo(Math.round(l)+.5,o.y),e.lineTo(Math.round(l)+.5,o.y+o.height),e.stroke(),e.beginPath(),e.moveTo(o.x,Math.round(t.y)+.5),e.lineTo(o.x+o.width,Math.round(t.y)+.5),e.stroke(),e.setLineDash([]),e.restore();break}}}if(t.has(e.LayerType.UI)&&this.uiLayer){this.uiLayer.clear();let t=this.uiLayer.ctx;if(n.priceAxis?.render(t,r,i),n.tradingRenderer?.renderAxisBadges(t,r,i),n.currentPriceLine?.render(t,r,i),n.timeAxis?.render(t,r,i,l,n.timeAxisY),n.chartLegend?.render(t,r,i,l),n.barCountdown?.render(t,r,i,l),n.panels.length>0){let r=n.crosshairHandler?.getPosition(),a=n.indicatorEngine,u=`${i.font.sizeSmall}px ${i.font.family}`,d;if(a){let e=a.getPanelIndicators();d=new Map;for(let t of e)d.set(t.instanceId,t)}for(let f of n.panels){let n=f.viewport,p=f.rect,{min:m,max:h}=n.priceRange;if(h-m<=0||p.height<=0)continue;let g=p.x+p.width,_=n.chartRect;t.strokeStyle=i.axisLine,t.lineWidth=1,t.beginPath(),t.moveTo(g+.5,p.y),t.lineTo(g+.5,p.y+p.height),t.stroke();let v=(0,e.computeTickStep)(m,h,4),y=Math.ceil(m/v)*v,b=v<1?Math.ceil(-Math.log10(v))+1:+(v<10);t.font=u,t.textBaseline=`middle`,t.textAlign=`left`;for(let r=y;r<=h;r+=v){let a=o(r,n);a<_.y||a>_.y+_.height||(t.strokeStyle=i.axisLine,t.beginPath(),t.moveTo(g,Math.round(a)+.5),t.lineTo(g+4,Math.round(a)+.5),t.stroke(),t.fillStyle=i.axisLabel,t.fillText((0,e.formatPrice)(r,b),g+6,a))}if(r&&r.x>=p.x&&r.x<=p.x+p.width&&r.y>=p.y&&r.y<=p.y+p.height){let a=(0,e.formatPrice)(c(r.y,n),b);t.font=`bold ${i.font.sizeSmall}px ${i.font.family}`;let o=t.measureText(a).width,s=Math.min(o+10,e.PRICE_AXIS_WIDTH-2);t.fillStyle=i.crosshair,t.fillRect(g+1,r.y-9,s,18),t.fillStyle=i.background,t.textBaseline=`middle`,t.textAlign=`left`,t.fillText(a,g+5,r.y)}if(r&&a){let e=s(r.x,n),o=Math.max(0,Math.min((l?.length??1)-1,Math.round(e))),c=a.getOutput(f.instanceId);if(c?.series&&o<c.series.length){let e=c.series[o];if(e){let n=[];for(let t in e){let r=e[t];r!==void 0&&n.push(`${t}: ${r.toFixed(b)}`)}if(n.length>0){let e=d?.get(f.instanceId),r=e?t.measureText(e.descriptor.name).width+14:10;t.font=u,t.fillStyle=i.textSecondary,t.textBaseline=`top`,t.textAlign=`left`,t.fillText(n.join(` `),p.x+r,p.y+6)}}}}}}}}}renderPriceLimits(t,n,r,i){let{chartRect:a}=n,s=[{price:i.ceiling,color:i.colors?.ceiling??`#FF00FF`,label:`CE`},{price:i.floor,color:i.colors?.floor??`#00FFFF`,label:`FL`},{price:i.reference,color:i.colors?.reference??`#FFD700`,label:`REF`}],c=`bold 9px ${r.font.family}`,l=`10px ${r.font.family}`;for(let{price:r,color:i,label:u}of s){let s=o(r,n);if(s<a.y||s>a.y+a.height)continue;t.setLineDash([8,4]),t.strokeStyle=i,t.lineWidth=1,t.beginPath(),t.moveTo(a.x,Math.round(s)+.5),t.lineTo(a.x+a.width,Math.round(s)+.5),t.stroke(),t.setLineDash([]),t.font=c,t.fillStyle=i,t.textBaseline=`bottom`,t.textAlign=`left`,t.fillText(`${u} ${r.toFixed(2)}`,a.x+4,s-2);let d=a.x+a.width+1;t.fillStyle=i,t.fillRect(d,s-7,e.PRICE_AXIS_WIDTH-2,14),t.fillStyle=`#000`,t.font=l,t.textBaseline=`middle`,t.textAlign=`left`,t.fillText(r.toFixed(2),d+4,s)}}destroy(){this.renderLoop.stop(),this.dprManager.destroy(),this.layerManager.destroy()}},d=class{state;dataLength=0;minBarWidth;maxBarWidth;rightMarginBars;constructor(t,n,r=2,i=30,a=5){this.minBarWidth=r,this.maxBarWidth=i,this.rightMarginBars=a,this.state={visibleRange:{from:0,to:0},priceRange:{min:0,max:1},barWidth:e.DEFAULT_BAR_WIDTH,barSpacing:e.DEFAULT_BAR_SPACING,offset:0,chartRect:this.computeChartRect(t,n)}}getState(){return{visibleRange:{...this.state.visibleRange},priceRange:{...this.state.priceRange},barWidth:this.state.barWidth,barSpacing:this.state.barSpacing,offset:this.state.offset,chartRect:{...this.state.chartRect},logScale:this.state.logScale}}setLogScale(e){this.state.logScale=e}isLogScale(){return this.state.logScale??!1}setRightMargin(e){this.rightMarginBars=e}computeChartRect(t,n){return{x:0,y:0,width:Math.max(0,t-e.PRICE_AXIS_WIDTH),height:Math.max(0,n-e.TIME_AXIS_HEIGHT)}}resize(e,t){this.state.chartRect=this.computeChartRect(e,t),this.clampOffset()}setChartRect(e){this.state.chartRect={x:e.x,y:e.y,width:Math.max(0,e.width),height:Math.max(0,e.height)},this.clampOffset(),this.updateVisibleRange()}updateData(t,n){this.dataLength=t.length,this.dataLength!==0&&(this.clampOffset(),this.updateVisibleRange(),n&&(this.state.priceRange=(0,e.computePriceRange)(t,this.state.visibleRange.from,Math.min(this.state.visibleRange.to,this.dataLength-1),.08)))}setPriceRange(e,t){this.state.priceRange={min:e,max:t}}scrollBy(e){this.state.offset+=e,this.clampOffset(),this.updateVisibleRange()}isAtEnd(){let e=this.state.barWidth+this.state.barSpacing,t=this.rightMarginBars*e,n=this.dataLength*e-this.state.chartRect.width+t;return this.state.offset>=n-e*2}scrollToEnd(){let e=this.state.barWidth+this.state.barSpacing,t=this.rightMarginBars*e,n=this.dataLength*e;this.state.offset=n-this.state.chartRect.width+t,this.updateVisibleRange()}zoom(t,n){let r=this.state.barWidth,i=(0,e.clamp)(r*(1+t),this.minBarWidth,this.maxBarWidth);if(i===r)return;let a=this.state.barWidth+this.state.barSpacing,o=(this.state.offset+n)/a;this.state.barWidth=i;let s=i+this.state.barSpacing;this.state.offset=o*s-n,this.clampOffset(),this.updateVisibleRange()}clampOffset(){if(this.state.chartRect.width<=0)return;let t=this.state.barWidth+this.state.barSpacing,n=this.dataLength*t,r=this.rightMarginBars*t,i=n-this.state.chartRect.width+r,a,o;i>0?(a=-(this.state.chartRect.width*.5),o=i):(a=i,o=i),this.state.offset=(0,e.clamp)(this.state.offset,a,o)}updateVisibleRange(){let e=this.state.barWidth+this.state.barSpacing;if(e<=0||this.state.chartRect.width<=0)return;let t=Math.floor(this.state.offset/e),n=Math.ceil(this.state.chartRect.width/e)+1,r=Math.min(t+n,this.dataLength-1);this.state.visibleRange={from:Math.max(0,t),to:Math.max(0,r)}}};function f(t,n){return(0,e.sliceVisibleData)(t,n.visibleRange.from,n.visibleRange.to)}var p=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D,y=new Path2D,b=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=g(n.high),a=g(n.low),c=g(n.open),l=g(n.close),u=n.close>=n.open,d=u?_:v;d.moveTo(r,i),d.lineTo(r,a);let f=u?l:c,p=Math.max(Math.abs(l-c),1);(u?y:b).rect(r-s,f,o,p)}e.strokeStyle=r.candleUpWick,e.lineWidth=1,e.stroke(_),e.strokeStyle=r.candleDownWick,e.stroke(v),e.fillStyle=r.candleUp,e.fill(y),e.fillStyle=r.candleDown,e.fill(b)}},m=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange;if(i>a||t.length===0)return;let o=n.barWidth+n.barSpacing,s=-n.offset+n.chartRect.x+n.barWidth/2,{min:c,max:l}=n.priceRange,u=l-c;if(u===0)return;let d=n.chartRect.y,f=n.chartRect.height/u;e.beginPath(),e.strokeStyle=r.lineColor,e.lineWidth=2,e.lineJoin=`round`;let p=!1;for(let n=i;n<=a&&n<t.length;n++){let r=n*o+s,i=d+(l-t[n].close)*f;p?e.lineTo(r,i):(e.moveTo(r,i),p=!0)}e.stroke()}},h=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange;if(i>a||t.length===0)return;let o=n.barWidth+n.barSpacing,s=-n.offset+n.chartRect.x+n.barWidth/2,{min:c,max:l}=n.priceRange,u=l-c;if(u===0)return;let d=n.chartRect.y,f=n.chartRect.height,p=f/u,m=d+f,h=i*o+s,g=d+(l-t[i].close)*p,_=e.createLinearGradient(0,d,0,m);_.addColorStop(0,r.areaTopColor),_.addColorStop(1,r.areaBottomColor),e.beginPath(),e.moveTo(h,m),e.lineTo(h,g);let v=h;for(let n=i+1;n<=a&&n<t.length;n++)v=n*o+s,e.lineTo(v,d+(l-t[n].close)*p);e.lineTo(v,m),e.closePath(),e.fillStyle=_,e.fill(),e.beginPath(),e.strokeStyle=r.lineColor,e.lineWidth=2,e.lineJoin=`round`,e.moveTo(h,g);for(let n=i+1;n<=a&&n<t.length;n++)e.lineTo(n*o+s,d+(l-t[n].close)*p);e.stroke()}},g=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open?_:v,a=g(n.high),o=g(n.low),c=g(n.open),l=g(n.close);i.moveTo(r,a),i.lineTo(r,o),i.moveTo(r-s,c),i.lineTo(r,c),i.moveTo(r,l),i.lineTo(r+s,l)}e.lineWidth=1,e.strokeStyle=r.candleUp,e.stroke(_),e.strokeStyle=r.candleDown,e.stroke(v)}},_=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D,y=[],b=[],x=new Path2D,S=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open,a=e===0||n.close>=t[e-1].close,c=g(n.high),l=g(n.low),u=g(n.open),d=g(n.close),f=Math.min(u,d),p=Math.max(Math.abs(d-u),1),m=a?_:v;m.moveTo(r,c),m.lineTo(r,l),i?(a?y:b).push({x:r-s,y:f,w:o,h:p}):(a?x:S).rect(r-s,f,o,p)}e.lineWidth=1,e.strokeStyle=r.candleUp,e.stroke(_),e.strokeStyle=r.candleDown,e.stroke(v),e.fillStyle=r.candleUp,e.fill(x),e.fillStyle=r.candleDown,e.fill(S),e.lineWidth=1.5,e.strokeStyle=r.candleUp;for(let t of y)e.strokeRect(t.x,t.y,t.w,t.h);e.strokeStyle=r.candleDown;for(let t of b)e.strokeRect(t.x,t.y,t.w,t.h)}},v=class{baselinePrice=null;setBaseline(e){this.baselinePrice=e}render(e,t,n,r){let{from:i,to:a}=n.visibleRange;if(i>a||t.length===0)return;let o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=g(this.baselinePrice??this.computeBaseline(t,i,a)),{chartRect:v}=n,y=[],b=[];for(let e=i;e<=a&&e<t.length;e++)y.push(h(e)),b.push(g(t[e].close));if(y.length<2)return;let x=y.length-1;e.save();let S=new Path2D;S.rect(v.x,v.y,v.width,_-v.y),e.clip(S);let C=new Path2D;C.moveTo(y[0],_);for(let e=0;e<=x;e++)C.lineTo(y[e],b[e]);C.lineTo(y[x],_),C.closePath(),e.fillStyle=`${r.candleUp}20`,e.fill(C),e.restore(),e.save();let w=new Path2D;w.rect(v.x,_,v.width,v.y+v.height-_),e.clip(w);let T=new Path2D;T.moveTo(y[0],_);for(let e=0;e<=x;e++)T.lineTo(y[e],b[e]);T.lineTo(y[x],_),T.closePath(),e.fillStyle=`${r.candleDown}20`,e.fill(T),e.restore();let E=new Path2D,D=new Path2D;for(let e=1;e<=x;e++){let t=b[e]<=_?E:D;t.moveTo(y[e-1],b[e-1]),t.lineTo(y[e],b[e])}e.lineWidth=2,e.strokeStyle=r.candleUp,e.stroke(E),e.strokeStyle=r.candleDown,e.stroke(D),e.setLineDash([6,4]),e.strokeStyle=r.textSecondary,e.lineWidth=1;let O=new Path2D;O.moveTo(v.x,_),O.lineTo(v.x+v.width,_),e.stroke(O),e.setLineDash([])}computeBaseline(e,t,n){let r=0,i=0;for(let a=t;a<=n&&a<e.length;a++)r+=e[a].close,i++;return i>0?r/i:0}},y=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D,y=new Path2D,b=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open,a=g(Math.max(n.open,n.close)),c=g(Math.min(n.open,n.close)),l=Math.max(c-a,1),u=r-s;i?(_.rect(u,a,o,l),y.rect(u,a,o,l)):(v.rect(u,a,o,l),b.rect(u,a,o,l))}e.fillStyle=r.candleUp,e.fill(_),e.fillStyle=r.candleDown,e.fill(v),e.lineWidth=1,e.strokeStyle=r.candleUp,e.stroke(y),e.strokeStyle=r.candleDown,e.stroke(b)}},b=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=new Path2D,v=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open,o=g(n.high),s=g(n.low),c=i?_:v;if(c.moveTo(r,o),c.lineTo(r,s),e<a&&e+1<t.length){let t=h(e+1),n=i?s:o;c.moveTo(r,n),c.lineTo(t,n)}}e.strokeStyle=r.candleUp,e.lineWidth=3,e.stroke(_),e.strokeStyle=r.candleDown,e.lineWidth=1,e.stroke(v)}},x=class{boxSize=1;setBoxSize(e){this.boxSize=e}render(e,t,n,r){let{from:i,to:a}=n.visibleRange,o=n.barWidth,s=o/2,c=o+n.barSpacing,l=-n.offset+n.chartRect.x+s,{min:u,max:d}=n.priceRange,f=d-u;if(f===0)return;let p=n.chartRect.y,m=n.chartRect.height/f,h=e=>e*c+l,g=e=>p+(d-e)*m,_=Math.abs(this.boxSize*m),v=Math.min(s*.8,_*.8)*.5,y=new Path2D,b=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=h(e),i=n.close>=n.open,a=n.high,o=n.low,s=Math.max(1,Math.round((a-o)/this.boxSize));for(let e=0;e<s;e++){let t=g(o+(e+.5)*this.boxSize);i?(y.moveTo(r-v,t-v),y.lineTo(r+v,t+v),y.moveTo(r+v,t-v),y.lineTo(r-v,t+v)):(b.moveTo(r+v,t),b.arc(r,t,v,0,Math.PI*2))}}e.lineWidth=1.5,e.strokeStyle=r.candleUp,e.stroke(y),e.strokeStyle=r.candleDown,e.stroke(b)}},S=class{visible=!0;heightRatio=.2;setVisible(e){this.visible=e}setHeightRatio(e){this.heightRatio=Math.max(.05,Math.min(.5,e))}render(e,t,n,r){if(!this.visible||t.length===0)return;let{from:i,to:a}=n.visibleRange,{chartRect:o}=n,s=n.barWidth,c=s/2,l=s+n.barSpacing,u=-n.offset+o.x+c,d=0;for(let e=i;e<=a&&e<t.length;e++)t[e].volume>d&&(d=t[e].volume);if(d===0)return;let f=o.height*this.heightRatio,p=o.y+o.height,m=f/d,h=new Path2D,g=new Path2D;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=e*l+u,i=n.volume*m;(n.close>=n.open?h:g).rect(r-c,p-i,s,i)}e.globalAlpha=.35,e.fillStyle=r.candleUp,e.fill(h),e.fillStyle=r.candleDown,e.fill(g),e.globalAlpha=1}},C=class{symbols=new Map;mode=`percent`;setMode(e){this.mode=e}addSymbol(e){this.symbols.set(e.id,e)}removeSymbol(e){this.symbols.delete(e)}setSymbolData(e,t){let n=this.symbols.get(e);n&&(n.data=t)}setSymbolVisible(e,t){let n=this.symbols.get(e);n&&(n.visible=t)}getSymbols(){return Array.from(this.symbols.values())}clear(){this.symbols.clear()}render(e,t,n,r){if(this.symbols.size===0||t.length===0)return;let{chartRect:i}=n;e.save(),e.beginPath(),e.rect(i.x,i.y,i.width,i.height),e.clip();for(let i of this.symbols.values())!i.visible||i.data.length===0||this.renderSymbol(e,i,t,n,r);e.restore()}renderSymbol(e,t,n,r,i){let{chartRect:s}=r,c=r.barWidth+r.barSpacing,l=Math.max(0,Math.floor(r.offset/c));if(l>=n.length||l>=t.data.length)return;let u=n[l].close,d=t.data[l].close;if(u===0||d===0)return;e.strokeStyle=t.color,e.lineWidth=t.lineWidth??1.5,e.lineJoin=`round`,e.beginPath();let f=!1,p=Math.min(n.length,t.data.length);for(let n=l;n<p;n++){let i=a(n,r);if(i<s.x-c)continue;if(i>s.x+s.width+c)break;let l;l=this.mode===`percent`?o(u*(1+(t.data[n].close-d)/d),r):o(t.data[n].close,r),f?e.lineTo(i,l):(e.moveTo(i,l),f=!0)}if(e.stroke(),f){let n=Math.min(l+2,p-1),i=a(n,r)+4,s;s=this.mode===`percent`?o(u*(1+(t.data[n].close-d)/d),r)-4:o(t.data[n].close,r)-4,e.fillStyle=t.color,e.font=`bold 10px sans-serif`,e.textBaseline=`bottom`,e.textAlign=`left`,e.fillText(t.label,i,s)}}};function w(e){if(e.length===0)return[];let t=[],n=e[0].open,r=(e[0].open+e[0].high+e[0].low+e[0].close)/4;for(let i=0;i<e.length;i++){let a=e[i],o=(a.open+a.high+a.low+a.close)/4,s=i===0?a.open:(n+r)/2,c=Math.max(a.high,s,o),l=Math.min(a.low,s,o);t.push({time:a.time,open:s,high:c,low:l,close:o,volume:a.volume}),n=s,r=o}return t}function T(e,t){if(e.length===0)return[];let n=t.brickSize;t.useATR&&e.length>(t.atrPeriod??14)&&(n=E(e,t.atrPeriod??14)),n<=0&&(n=1);let r=[],i=Math.round(e[0].close/n)*n,a=e[0].time;for(let t=1;t<e.length;t++){let o=e[t].close-i,s=Math.floor(Math.abs(o)/n);if(s>=1){let c=o>0?1:-1;for(let o=0;o<s;o++){let o=i;i+=c*n,r.push({time:a++,open:o,high:Math.max(o,i),low:Math.min(o,i),close:i,volume:e[t].volume/s})}}}return r}function E(e,t){let n=0;for(let r=1;r<=Math.min(t,e.length-1);r++){let t=Math.max(e[r].high-e[r].low,Math.abs(e[r].high-e[r-1].close),Math.abs(e[r].low-e[r-1].close));n+=t}return n/t}function D(e,t=3){if(e.length===0)return[];let n=[],r=0;n.push({time:r++,open:e[0].open,high:Math.max(e[0].open,e[0].close),low:Math.min(e[0].open,e[0].close),close:e[0].close,volume:e[0].volume});for(let i=1;i<e.length;i++){let a=e[i].close,o=n[n.length-1],s=o.close>=o.open,c=Math.min(t,n.length),l=-1/0,u=1/0;for(let e=n.length-c;e<n.length;e++)l=Math.max(l,n[e].high),u=Math.min(u,n[e].low);s&&a>o.close?n.push({time:r++,open:o.close,high:a,low:o.close,close:a,volume:e[i].volume}):!s&&a<o.close||s&&a<u?n.push({time:r++,open:o.close,high:o.close,low:a,close:a,volume:e[i].volume}):!s&&a>l&&n.push({time:r++,open:o.close,high:a,low:o.close,close:a,volume:e[i].volume})}return n}function O(e,t=4){if(e.length<2)return e.slice();let n=[],r=e[1].close>=e[0].close?1:-1,i=e[0].close,a=e[0].close,o=e[0].close,s=0;for(let c=1;c<e.length;c++){let l=e[c].close,u=t/100*o;r===1?(l>i&&(i=l),i-l>=u&&(n.push({time:s++,open:o,high:i,low:o,close:i,volume:e[c].volume}),r=-1,o=i,a=l,i=l)):(l<a&&(a=l),l-a>=u&&(n.push({time:s++,open:o,high:o,low:a,close:a,volume:e[c].volume}),r=1,o=a,i=l,a=l))}return r===1?n.push({time:s,open:o,high:i,low:o,close:i,volume:0}):n.push({time:s,open:o,high:o,low:a,close:a,volume:0}),n}function ee(e,t,n=3){if(e.length===0||t<=0)return[];let r=[],i=0,a=0,o=0,s=0,c=Math.round(e[0].close/t)*t;a=c,o=c,i=1;for(let c=1;c<e.length;c++){let l=e[c].close,u=Math.round(l/t)*t;i===1?u>a?a=u:a-u>=t*n&&(r.push({time:s++,open:o,high:a,low:o,close:a,volume:e[c].volume}),i=-1,o=u,a-=t):u<o?o=u:u-o>=t*n&&(r.push({time:s++,open:a,high:a,low:o,close:o,volume:e[c].volume}),i=1,a=u,o+=t)}return r.push({time:s,open:i===1?o:a,high:a,low:o,close:i===1?a:o,volume:0}),r}var k=class{drawLine(e,t,n,r){if(!(t.length<2)){e.beginPath(),e.strokeStyle=n,e.lineWidth=r,e.lineJoin=`round`,e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length;n++)e.lineTo(t[n].x,t[n].y);e.stroke()}}drawBand(e,t,n,r){if(!(t.length<2||n.length<2)){e.beginPath(),e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length;n++)e.lineTo(t[n].x,t[n].y);for(let t=n.length-1;t>=0;t--)e.lineTo(n[t].x,n[t].y);e.closePath(),e.fillStyle=r,e.fill()}}drawHistogram(e,t,n){let r=n/2;for(let i of t){e.fillStyle=i.color;let t=Math.min(i.y,i.baseY),a=Math.abs(i.y-i.baseY);e.fillRect(i.x-r,t,n,Math.max(a,1))}}valuesToPoints(e,t,n,r){let i=[],{from:s,to:c}=r.visibleRange;for(let l=s;l<=c&&l<n.length;l++){let s=e.values.get(n[l].time);s&&s[t]!==void 0&&i.push({x:a(l,r),y:o(s[t],r)})}return i}},te=1,ne=class{registry=new Map;instances=new Map;register(e){this.registry.set(e.descriptor.id,e)}getAvailableIndicators(){return Array.from(this.registry.values()).map(e=>e.descriptor)}addIndicator(e,t={},n){let r=this.registry.get(e);if(!r)throw Error(`Unknown indicator: ${e}`);let i=`tc_${e}_${te++}`,a={id:e,instanceId:i,params:{...r.descriptor.defaultConfig,...t},visible:!0},o={plugin:r,config:a,output:null,style:{colors:a.style?.colors??[`#2196F3`,`#FF9800`,`#4CAF50`,`#E91E63`,`#9C27B0`],lineWidths:a.style?.lineWidths??[1.5],opacity:a.style?.opacity??1}};return n&&(o.output=r.calculate(n,a)),this.instances.set(i,o),i}removeIndicator(e){this.instances.delete(e)}updateIndicator(e,t,n){let r=this.instances.get(e);r&&(Object.assign(r.config.params,t),n&&(r.output=r.plugin.calculate(n,r.config)))}recalculateAll(e){for(let t of this.instances.values())t.output=t.plugin.calculate(e,t.config)}getOutput(e){return this.instances.get(e)?.output??null}renderOverlays(e,t){for(let n of this.instances.values())!n.output||!n.config.visible||n.plugin.descriptor.placement===`overlay`&&n.plugin.render(e,n.output,t,n.style)}renderPanel(e,t,n){let r=this.instances.get(t);!r?.output||!r.config.visible||r.plugin.render(e,r.output,n,r.style)}getIndicatorConfig(e){return this.instances.get(e)?.config??null}getIndicatorDescriptor(e){return this.instances.get(e)?.plugin.descriptor??null}getActiveIndicators(){let e=[];for(let[t,n]of this.instances)e.push({instanceId:t,id:n.config.id,params:{...n.config.params},descriptor:n.plugin.descriptor});return e}updateIndicatorStyle(e,t){let n=this.instances.get(e);n&&Object.assign(n.style,t)}getOverlayPriceRange(e,t){let n=1/0,r=-1/0;for(let i of this.instances.values()){if(!i.output||!i.config.visible||i.plugin.descriptor.placement!==`overlay`)continue;let a=0;for(let[,o]of i.output.values){if(a>=e&&a<=t)for(let e in o){let t=o[e];t!==void 0&&isFinite(t)&&(t<n&&(n=t),t>r&&(r=t))}a++}}return n===1/0?null:{min:n,max:r}}getPanelIndicators(){let e=[];for(let[t,n]of this.instances)n.plugin.descriptor.placement===`panel`&&e.push({instanceId:t,descriptor:n.plugin.descriptor});return e}},re=class extends k{descriptor={id:`sma`,name:`Simple Moving Average`,placement:`overlay`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null),a=0;for(let t=0;t<e.length;t++)if(a+=e[t].close,t>=n&&(a-=e[t-n].close),t>=n-1){let o={value:a/n};r.set(e[t].time,o),i[t]=o}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange;e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let l=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(!r||r.value===void 0)continue;let s=a(t,n),c=o(r.value,n);l?e.lineTo(s,c):(e.moveTo(s,c),l=!0)}e.stroke()}},ie=class extends k{descriptor={id:`ema`,name:`Exponential Moving Average`,placement:`overlay`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=2/(n+1),i=new Map,a=Array(e.length).fill(null),o=0;for(let t=0;t<e.length;t++){if(t<n-1){o+=e[t].close;continue}o=t===n-1?(o+e[t].close)/n:(e[t].close-o)*r+o;let s={value:o};i.set(e[t].time,s),a[t]=s}return{values:i,series:a}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange;e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let l=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(!r||r.value===void 0)continue;let s=a(t,n),c=o(r.value,n);l?e.lineTo(s,c):(e.moveTo(s,c),l=!0)}e.stroke()}},ae=class extends k{descriptor={id:`bb`,name:`Bollinger Bands`,placement:`overlay`,defaultConfig:{period:20,stdDev:2}};calculate(e,t){let n=t.params.period,r=t.params.stdDev,i=new Map,a=Array(e.length).fill(null),o=0,s=0;for(let t=0;t<e.length;t++){let c=e[t].close;if(o+=c,s+=c*c,t>=n){let r=e[t-n].close;o-=r,s-=r*r}if(t>=n-1){let c=o/n,l=s/n-c*c,u=Math.sqrt(Math.max(0,l)),d={middle:c,upper:c+r*u,lower:c-r*u};i.set(e[t].time,d),a[t]=d}}return{values:i,series:a}}render(t,n,r,i){let s=n.series;if(!s)return;let{from:c,to:l}=r.visibleRange,u=0,d=l-c+1,f=new Float64Array(d),p=new Float64Array(d),m=new Float64Array(d),h=new Float64Array(d),g=new Float64Array(d),_=new Float64Array(d);for(let e=c;e<=l&&e<s.length;e++){let t=s[e];if(!t||t.upper===void 0)continue;let n=a(e,r);f[u]=n,p[u]=o(t.upper,r),m[u]=n,h[u]=o(t.middle,r),g[u]=n,_[u]=o(t.lower,r),u++}if(!(u<2)){t.beginPath(),t.moveTo(f[0],p[0]);for(let e=1;e<u;e++)t.lineTo(f[e],p[e]);for(let e=u-1;e>=0;e--)t.lineTo(g[e],_[e]);t.closePath(),t.fillStyle=(0,e.withAlpha)(i.colors[0],.1),t.fill(),t.beginPath(),t.strokeStyle=i.colors[0],t.lineWidth=i.lineWidths[0],t.lineJoin=`round`,t.moveTo(f[0],p[0]);for(let e=1;e<u;e++)t.lineTo(f[e],p[e]);t.stroke(),t.beginPath(),t.strokeStyle=i.colors[1]??i.colors[0],t.moveTo(m[0],h[0]);for(let e=1;e<u;e++)t.lineTo(m[e],h[e]);t.stroke(),t.beginPath(),t.strokeStyle=i.colors[0],t.moveTo(g[0],_[0]);for(let e=1;e<u;e++)t.lineTo(g[e],_[e]);t.stroke()}}},oe=class extends k{descriptor={id:`vwap`,name:`Volume Weighted Average Price`,placement:`overlay`,defaultConfig:{}};calculate(e,t){let n=new Map,r=Array(e.length).fill(null),i=0,a=0;for(let t=0;t<e.length;t++){let o=(e[t].high+e[t].low+e[t].close)/3;if(i+=e[t].volume,a+=o*e[t].volume,i>0){let o={value:a/i};n.set(e[t].time,o),r[t]=o}}return{values:n,series:r}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange;e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let l=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(!r||r.value===void 0)continue;let s=a(t,n),c=o(r.value,n);l?e.lineTo(s,c):(e.moveTo(s,c),l=!0)}e.stroke()}},se=class extends k{descriptor={id:`ichimoku`,name:`Ichimoku Cloud`,placement:`overlay`,defaultConfig:{tenkan:9,kijun:26,senkou:52,displacement:26}};calculate(e,t){let n=t.params.tenkan,r=t.params.kijun,i=t.params.senkou,a=new Map,o=Array(e.length).fill(null),s=t=>{let n=new Float64Array(e.length),r=new Float64Array(e.length);for(let i=0;i<e.length;i++){let a=-1/0,o=1/0,s=Math.max(0,i-t+1);for(let t=s;t<=i;t++)e[t].high>a&&(a=e[t].high),e[t].low<o&&(o=e[t].low);n[i]=a,r[i]=o}return{highs:n,lows:r}},c=s(n),l=s(r),u=s(i);for(let t=0;t<e.length;t++){let s={};t>=n-1&&(s.tenkan=(c.highs[t]+c.lows[t])/2),t>=r-1&&(s.kijun=(l.highs[t]+l.lows[t])/2),s.tenkan!==void 0&&s.kijun!==void 0&&(s.senkouA=(s.tenkan+s.kijun)/2),t>=i-1&&(s.senkouB=(u.highs[t]+u.lows[t])/2),t>=r-1&&(s.chikou=e[t].close),(s.tenkan!==void 0||s.kijun!==void 0||s.senkouA!==void 0||s.senkouB!==void 0)&&(a.set(e[t].time,s),o[t]=s)}return{values:a,series:o}}render(t,n,r,i){let s=n.series;if(!s)return;let{from:c,to:l}=r.visibleRange,u=l-c+1,d=new Float64Array(u),f=new Float64Array(u),p=0,m=new Float64Array(u),h=new Float64Array(u),g=0,_=new Float64Array(u),v=new Float64Array(u),y=0,b=new Float64Array(u),x=new Float64Array(u),S=0;for(let e=c;e<=l&&e<s.length;e++){let t=s[e];if(!t)continue;let n=a(e,r);t.tenkan!==void 0&&(d[p]=n,f[p]=o(t.tenkan,r),p++),t.kijun!==void 0&&(m[g]=n,h[g]=o(t.kijun,r),g++),t.senkouA!==void 0&&(_[y]=n,v[y]=o(t.senkouA,r),y++),t.senkouB!==void 0&&(b[S]=n,x[S]=o(t.senkouB,r),S++)}if(y>1&&S>1){let n=Math.min(y,S);t.beginPath(),t.moveTo(_[0],v[0]);for(let e=1;e<n;e++)t.lineTo(_[e],v[e]);for(let e=n-1;e>=0;e--)t.lineTo(b[e],x[e]);t.closePath(),t.fillStyle=(0,e.withAlpha)(i.colors[2]??`#26A69A`,.15),t.fill()}let C=(e,n,r,i,a)=>{if(!(r<2)){t.beginPath(),t.strokeStyle=i,t.lineWidth=a,t.lineJoin=`round`,t.moveTo(e[0],n[0]);for(let i=1;i<r;i++)t.lineTo(e[i],n[i]);t.stroke()}};C(d,f,p,i.colors[0],i.lineWidths[0]),C(m,h,g,i.colors[1]??`#FF9800`,i.lineWidths[0]),C(_,v,y,i.colors[2]??`#26A69A`,1),C(b,x,S,i.colors[3]??`#EF5350`,1)}},A=class extends k{descriptor={id:`psar`,name:`Parabolic SAR`,placement:`overlay`,defaultConfig:{step:.02,max:.2}};calculate(e,t){let n=t.params.step,r=t.params.max,i=new Map,a=Array(e.length).fill(null);if(e.length<2)return{values:i,series:a};let o=e[1].close>e[0].close,s=n,c=o?e[0].high:e[0].low,l=o?e[0].low:e[0].high,u={value:l,trend:o?1:-1};i.set(e[0].time,u),a[0]=u;for(let t=1;t<e.length;t++){let u=l;l=u+s*(c-u),o?(l=Math.min(l,e[t-1].low),t>=2&&(l=Math.min(l,e[t-2].low)),e[t].low<l?(o=!1,l=c,c=e[t].low,s=n):e[t].high>c&&(c=e[t].high,s=Math.min(s+n,r))):(l=Math.max(l,e[t-1].high),t>=2&&(l=Math.max(l,e[t-2].high)),e[t].high>l?(o=!0,l=c,c=e[t].high,s=n):e[t].low<c&&(c=e[t].low,s=Math.min(s+n,r)));let d={value:l,trend:o?1:-1};i.set(e[t].time,d),a[t]=d}return{values:i,series:a}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange,l=Math.max(1.5,n.barWidth*.15);for(let t=s;t<=c&&t<i.length;t++){let s=i[t];if(!s||s.value===void 0)continue;let c=a(t,n),u=o(s.value,n);e.beginPath(),e.arc(c,u,l,0,Math.PI*2),e.fillStyle=s.trend===1?r.colors[0]:r.colors[1]??`#EF5350`,e.fill()}}},j=class extends k{descriptor={id:`supertrend`,name:`Supertrend`,placement:`overlay`,defaultConfig:{period:10,multiplier:3}};calculate(e,t){let n=t.params.period,r=t.params.multiplier,i=new Map,a=Array(e.length).fill(null);if(e.length<n)return{values:i,series:a};let o=[e[0].high-e[0].low];for(let t=1;t<e.length;t++)o.push(Math.max(e[t].high-e[t].low,Math.abs(e[t].high-e[t-1].close),Math.abs(e[t].low-e[t-1].close)));let s=0;for(let e=0;e<n;e++)s+=o[e];s/=n;let c=0,l=0,u=0,d=0,f=0,p=0,m=1;for(let t=n-1;t<e.length;t++){t>n-1&&(s=(s*(n-1)+o[t])/n);let h=(e[t].high+e[t].low)/2,g=h+r*s,_=h-r*s;c=g<u||e[t-1].close>u?g:u,l=_>d||e[t-1].close<d?_:d,f=p===u?e[t].close<=c?c:l:e[t].close>=l?l:c,m=f===l?1:-1;let v={value:f,trend:m};i.set(e[t].time,v),a[t]=v,u=c,d=l,p=f}return{values:i,series:a}}render(e,t,n,r){let i=t.series;if(!i)return;let{from:s,to:c}=n.visibleRange,l=r.colors[0],u=r.colors[1]??`#EF5350`,d=0,f=0,p=0;for(let t=s;t<=c&&t<i.length;t++){let s=i[t];if(!s||s.value===void 0)continue;let c=a(t,n),m=o(s.value,n),h=s.trend??1;p!==0&&(e.beginPath(),e.strokeStyle=h===1?l:u,e.lineWidth=r.lineWidths[0]+.5,e.moveTo(d,f),e.lineTo(c,m),e.stroke()),d=c,f=m,p=h}}},M=class extends k{descriptor={id:`keltner`,name:`Keltner Channel`,placement:`overlay`,defaultConfig:{emaPeriod:20,atrPeriod:10,multiplier:1.5}};calculate(e,t){let n=t.params.emaPeriod,r=t.params.atrPeriod,i=t.params.multiplier,a=new Map;if(e.length<Math.max(n,r))return{values:a};let o=2/(n+1),s=e[0].close,c=[s];for(let t=1;t<e.length;t++)s=(e[t].close-s)*o+s,c.push(s);let l=[e[0].high-e[0].low];for(let t=1;t<e.length;t++)l.push(Math.max(e[t].high-e[t].low,Math.abs(e[t].high-e[t-1].close),Math.abs(e[t].low-e[t-1].close)));let u=0;for(let e=0;e<r;e++)u+=l[e];u/=r;let d=Array(e.length).fill(null),f=Math.max(n,r)-1;for(let t=f;t<e.length;t++){t>f&&(u=(u*(r-1)+l[t])/r);let n={middle:c[t],upper:c[t]+i*u,lower:c[t]-i*u};a.set(e[t].time,n),d[t]=n}return{values:a,series:d}}render(t,n,r,i){let s=n.series;if(!s)return;let{from:c,to:l}=r.visibleRange,u=[],d=[],f=[];for(let e=c;e<=l&&e<s.length;e++){let t=s[e];if(!t)continue;let n=a(e,r);t.upper!==void 0&&u.push({x:n,y:o(t.upper,r)}),t.middle!==void 0&&d.push({x:n,y:o(t.middle,r)}),t.lower!==void 0&&f.push({x:n,y:o(t.lower,r)})}this.drawBand(t,u,f,(0,e.withAlpha)(i.colors[0],.08)),this.drawLine(t,u,i.colors[0],i.lineWidths[0]),this.drawLine(t,d,i.colors[1]??i.colors[0],i.lineWidths[0]),this.drawLine(t,f,i.colors[0],i.lineWidths[0])}},N=class extends k{descriptor={id:`donchian`,name:`Donchian Channel`,placement:`overlay`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=-1/0,o=1/0;for(let r=t-n+1;r<=t;r++)e[r].high>a&&(a=e[r].high),e[r].low<o&&(o=e[r].low);let s={upper:a,middle:(a+o)/2,lower:o};r.set(e[t].time,s),i[t]=s}return{values:r,series:i}}render(t,n,r,i){let s=n.series;if(!s)return;let{from:c,to:l}=r.visibleRange,u=[],d=[],f=[];for(let e=c;e<=l&&e<s.length;e++){let t=s[e];if(!t)continue;let n=a(e,r);t.upper!==void 0&&u.push({x:n,y:o(t.upper,r)}),t.middle!==void 0&&d.push({x:n,y:o(t.middle,r)}),t.lower!==void 0&&f.push({x:n,y:o(t.lower,r)})}this.drawBand(t,u,f,(0,e.withAlpha)(i.colors[0],.08)),this.drawLine(t,u,i.colors[0],i.lineWidths[0]),this.drawLine(t,d,i.colors[1]??`#787B86`,1),this.drawLine(t,f,i.colors[0],i.lineWidths[0])}},ce=class extends k{descriptor={id:`rsi`,name:`Relative Strength Index`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);if(e.length<n+1)return{values:r,series:i};let a=0,o=0;for(let t=1;t<=n;t++){let n=e[t].close-e[t-1].close;n>0?a+=n:o-=n}a/=n,o/=n;let s={value:o===0?100:100-100/(1+a/o)};r.set(e[n].time,s),i[n]=s;for(let t=n+1;t<e.length;t++){let s=e[t].close-e[t-1].close,c=s>0?s:0,l=s<0?-s:0;a=(a*(n-1)+c)/n,o=(o*(n-1)+l)/n;let u={value:o===0?100:100-100/(1+a/o)};r.set(e[t].time,u),i[t]=u}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100);e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]),e.beginPath();for(let t of[30,70]){let n=l(t);e.moveTo(o.x,n),e.lineTo(o.x+o.width,n)}e.stroke(),e.setLineDash([]),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(!r||r.value===void 0)continue;let o=a(t,n),s=l(r.value);u?e.lineTo(o,s):(e.moveTo(o,s),u=!0)}e.stroke()}},P=class extends k{descriptor={id:`macd`,name:`MACD`,placement:`panel`,defaultConfig:{fast:12,slow:26,signal:9}};calculate(e,t){let n=t.params.fast,r=t.params.slow,i=t.params.signal,a=new Map,o=Array(e.length).fill(null);if(e.length<r)return{values:a,series:o};let s=2/(n+1),c=2/(r+1),l=2/(i+1),u=e[0].close,d=e[0].close,f=0,p=!1;for(let t=0;t<e.length;t++){let n=e[t].close;if(t===0?(u=n,d=n):(u=(n-u)*s+u,d=(n-d)*c+d),t>=r-1){let n=u-d;p?f=(n-f)*l+f:(f=n,p=!0);let r=n-f,i={macd:n,signal:f,histogram:r};a.set(e[t].time,i),o[t]=i}}return{values:a,series:o}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&(t.macd!==void 0&&(t.macd<l&&(l=t.macd),t.macd>u&&(u=t.macd)),t.signal!==void 0&&(t.signal<l&&(l=t.signal),t.signal>u&&(u=t.signal)),t.histogram!==void 0&&(t.histogram<l&&(l=t.histogram),t.histogram>u&&(u=t.histogram)))}if(l===1/0)return;let d=u-l||1,f=e=>o.y+o.height*(1-(e-l)/d),p=f(0),m=n.barWidth/2,h=r.colors[2]??`#26A69A`,g=r.colors[3]??`#EF5350`,_=r.colors[0],v=r.colors[1]??`#FF9800`;e.beginPath();let y=new Path2D,b=new Path2D,x=!1,S=!1,C=new Path2D,w=new Path2D;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];if(!t)continue;let r=a(e,n);if(t.histogram!==void 0){let e=f(t.histogram),i=Math.min(e,p),a=Math.max(Math.abs(e-p),1);t.histogram>=0?y.rect(r-m,i,n.barWidth,a):b.rect(r-m,i,n.barWidth,a)}if(t.macd!==void 0){let e=f(t.macd);x?C.lineTo(r,e):(C.moveTo(r,e),x=!0)}if(t.signal!==void 0){let e=f(t.signal);S?w.lineTo(r,e):(w.moveTo(r,e),S=!0)}}e.fillStyle=h,e.fill(y),e.fillStyle=g,e.fill(b),e.strokeStyle=_,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`,e.stroke(C),e.strokeStyle=v,e.stroke(w)}},F=class extends k{descriptor={id:`stochastic`,name:`Stochastic Oscillator`,placement:`panel`,defaultConfig:{kPeriod:14,dPeriod:3,smooth:3}};calculate(e,t){let n=t.params.kPeriod,r=t.params.dPeriod,i=t.params.smooth,a=new Map,o=Array(e.length).fill(null);if(e.length<n)return{values:a,series:o};let s=[];for(let t=0;t<e.length;t++){if(t<n-1){s.push(0);continue}let r=-1/0,i=1/0;for(let a=t-n+1;a<=t;a++)e[a].high>r&&(r=e[a].high),e[a].low<i&&(i=e[a].low);s.push(r===i?50:(e[t].close-i)/(r-i)*100)}let c=[];for(let e=0;e<s.length;e++){if(e<n-1+i-1){c.push(0);continue}let t=0;for(let n=e-i+1;n<=e;n++)t+=s[n];c.push(t/i)}let l=n-1+i-1;for(let t=l;t<c.length;t++){let n={k:c[t]};if(t>=l+r-1){let e=0;for(let n=t-r+1;n<=t;n++)e+=c[n];n.d=e/r}a.set(e[t].time,n),o[t]=n}return{values:a,series:o}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100);e.strokeStyle=r.colors[2]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);for(let t of[20,80]){let n=l(t);e.beginPath(),e.moveTo(o.x,n),e.lineTo(o.x+o.width,n),e.stroke()}e.setLineDash([]);let u=(t,o)=>{e.beginPath(),e.strokeStyle=o,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let r=s;r<=c&&r<i.length;r++){let o=i[r];if(!o||o[t]===void 0)continue;let s=a(r,n),c=l(o[t]);u?e.lineTo(s,c):(e.moveTo(s,c),u=!0)}e.stroke()};u(`k`,r.colors[0]),u(`d`,r.colors[1]??`#FF9800`)}},I=class extends k{descriptor={id:`atr`,name:`Average True Range`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);if(e.length<2)return{values:r,series:i};let a=[e[0].high-e[0].low];for(let t=1;t<e.length;t++){let n=Math.max(e[t].high-e[t].low,Math.abs(e[t].high-e[t-1].close),Math.abs(e[t].low-e[t-1].close));a.push(n)}let o=0;for(let e=0;e<n;e++)o+=a[e];o/=n;let s={value:o};r.set(e[n-1].time,s),i[n-1]=s;for(let t=n;t<e.length;t++){o=(o*(n-1)+a[t])/n;let s={value:o};r.set(e[t].time,s),i[t]=s}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d-f,m=e=>o.y+o.height*(1-(e-f)/p);e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let h=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=m(r.value);h?e.lineTo(i,o):(e.moveTo(i,o),h=!0)}}e.stroke()}},L=class extends k{descriptor={id:`adx`,name:`Average Directional Index`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);if(e.length<n*2)return{values:r,series:i};let a=[],o=[],s=[];for(let t=1;t<e.length;t++){let n=Math.max(e[t].high-e[t].low,Math.abs(e[t].high-e[t-1].close),Math.abs(e[t].low-e[t-1].close));a.push(n);let r=e[t].high-e[t-1].high,i=e[t-1].low-e[t].low;o.push(r>i&&r>0?r:0),s.push(i>r&&i>0?i:0)}let c=0,l=0,u=0;for(let e=0;e<n;e++)c+=a[e],l+=o[e],u+=s[e];let d=[];for(let t=n;t<a.length;t++){t>n&&(c=c-c/n+a[t],l=l-l/n+o[t],u=u-u/n+s[t]);let f=c===0?0:l/c*100,p=c===0?0:u/c*100,m=f+p,h=m===0?0:Math.abs(f-p)/m*100;d.push(h);let g=t+1,_={plusDI:f,minusDI:p,dx:h};r.set(e[g].time,_),i[g]=_}if(d.length>=n){let t=0;for(let e=0;e<n;e++)t+=d[e];t/=n;let a=n*2;if(a<e.length){let n=r.get(e[a].time);n&&(n.adx=t),i[a]&&(i[a].adx=t)}for(let a=n;a<d.length;a++){t=(t*(n-1)+d[a])/n;let o=a+n+1;if(o<e.length){let n=r.get(e[o].time);n&&(n.adx=t),i[o]&&(i[o].adx=t)}}}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100),u=(t,o)=>{e.beginPath(),e.strokeStyle=o,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let r=s;r<=c&&r<i.length;r++){let o=i[r];if(!o||o[t]===void 0)continue;let s=a(r,n),c=l(o[t]);u?e.lineTo(s,c):(e.moveTo(s,c),u=!0)}e.stroke()};u(`adx`,r.colors[0]),u(`plusDI`,r.colors[1]??`#26A69A`),u(`minusDI`,r.colors[2]??`#EF5350`)}},R=class extends k{descriptor={id:`obv`,name:`On Balance Volume`,placement:`panel`,defaultConfig:{}};calculate(e,t){let n=new Map,r=Array(e.length).fill(null);if(e.length===0)return{values:n,series:r};let i=0,a={value:i};n.set(e[0].time,a),r[0]=a;for(let t=1;t<e.length;t++){e[t].close>e[t-1].close?i+=e[t].volume:e[t].close<e[t-1].close&&(i-=e[t].volume);let a={value:i};n.set(e[t].time,a),r[t]=a}return{values:n,series:r}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d-f,m=e=>o.y+o.height*(1-(e-f)/p);e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let h=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=m(r.value);h?e.lineTo(i,o):(e.moveTo(i,o),h=!0)}}e.stroke()}},z=class extends k{descriptor={id:`williamsR`,name:`Williams %R`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=-1/0,o=1/0;for(let r=t-n+1;r<=t;r++)e[r].high>a&&(a=e[r].high),e[r].low<o&&(o=e[r].low);let s={value:a===o?-50:(a-e[t].close)/(a-o)*-100};r.set(e[t].time,s),i[t]=s}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-(e+100)/100);e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);for(let t of[-20,-80]){let n=l(t);e.beginPath(),e.moveTo(o.x,n),e.lineTo(o.x+o.width,n),e.stroke()}e.setLineDash([]),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=l(r.value);u?e.lineTo(i,o):(e.moveTo(i,o),u=!0)}}e.stroke()}},B=class extends k{descriptor={id:`cci`,name:`Commodity Channel Index`,placement:`panel`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=0;for(let r=t-n+1;r<=t;r++)a+=(e[r].high+e[r].low+e[r].close)/3;let o=a/n,s=0;for(let r=t-n+1;r<=t;r++){let t=(e[r].high+e[r].low+e[r].close)/3;s+=Math.abs(t-o)}let c=s/n,l=(e[t].high+e[t].low+e[t].close)/3,u={value:c===0?0:(l-o)/(.015*c)};r.set(e[t].time,u),i[t]=u}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);for(let t of[-100,0,100])if(t>=f&&t<=p){let n=h(t);e.beginPath(),e.moveTo(o.x,n),e.lineTo(o.x+o.width,n),e.stroke()}e.setLineDash([]),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let g=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=h(r.value);g?e.lineTo(i,o):(e.moveTo(i,o),g=!0)}}e.stroke()}},V=class extends k{descriptor={id:`mfi`,name:`Money Flow Index`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);if(e.length<n+1)return{values:r,series:i};let a=e.map(e=>(e.high+e.low+e.close)/3),o=a.map((t,n)=>t*e[n].volume);for(let t=n;t<e.length;t++){let s=0,c=0;for(let e=t-n+1;e<=t;e++)a[e]>a[e-1]?s+=o[e]:a[e]<a[e-1]&&(c+=o[e]);let l={value:c===0?100:100-100/(1+s/c)};r.set(e[t].time,l),i[t]=l}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100);e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);for(let t of[20,80]){let n=l(t);e.beginPath(),e.moveTo(o.x,n),e.lineTo(o.x+o.width,n),e.stroke()}e.setLineDash([]),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=l(r.value);u?e.lineTo(i,o):(e.moveTo(i,o),u=!0)}}e.stroke()}},H=class extends k{descriptor={id:`aroon`,name:`Aroon`,placement:`panel`,defaultConfig:{period:25}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n;t<e.length;t++){let a=0,o=0,s=-1/0,c=1/0;for(let r=0;r<=n;r++){let i=t-n+r;e[i].high>s&&(s=e[i].high,a=r),e[i].low<c&&(c=e[i].low,o=r)}let l={up:a/n*100,down:o/n*100};r.set(e[t].time,l),i[t]=l}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=e=>o.y+o.height*(1-e/100),u=(t,o)=>{e.beginPath(),e.strokeStyle=o,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let u=!1;for(let r=s;r<=c&&r<i.length;r++){let o=i[r];if(!o||o[t]===void 0)continue;let s=a(r,n),c=l(o[t]);u?e.lineTo(s,c):(e.moveTo(s,c),u=!0)}e.stroke()};u(`up`,r.colors[0]),u(`down`,r.colors[1]??`#EF5350`)}},U=class extends k{descriptor={id:`roc`,name:`Rate of Change`,placement:`panel`,defaultConfig:{period:12}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n;t<e.length;t++){let a=e[t-n].close,o={value:a===0?0:(e[t].close-a)/a*100};r.set(e[t].time,o),i[t]=o}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);if(0>=f&&0<=p){e.strokeStyle=r.colors[1]??`#787B86`,e.lineWidth=1,e.setLineDash([4,4]);let t=h(0);e.beginPath(),e.moveTo(o.x,t),e.lineTo(o.x+o.width,t),e.stroke(),e.setLineDash([])}e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let g=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=h(r.value);g?e.lineTo(i,o):(e.moveTo(i,o),g=!0)}}e.stroke()}},W=class extends k{descriptor={id:`tsi`,name:`True Strength Index`,placement:`panel`,defaultConfig:{longPeriod:25,shortPeriod:13,signalPeriod:7}};emaSmooth(e,t){let n=2/(t+1),r=[e[0]];for(let t=1;t<e.length;t++)r.push((e[t]-r[t-1])*n+r[t-1]);return r}calculate(e,t){let n=t.params.longPeriod,r=t.params.shortPeriod,i=t.params.signalPeriod,a=new Map,o=Array(e.length).fill(null);if(e.length<2)return{values:a,series:o};let s=[],c=[];for(let t=1;t<e.length;t++){let n=e[t].close-e[t-1].close;s.push(n),c.push(Math.abs(n))}let l=this.emaSmooth(this.emaSmooth(s,n),r),u=this.emaSmooth(this.emaSmooth(c,n),r),d=[];for(let e=0;e<l.length;e++)d.push(u[e]===0?0:l[e]/u[e]*100);let f=this.emaSmooth(d,i);for(let t=0;t<d.length;t++){let n=t+1;if(n<e.length){let r={tsi:d[t],signal:f[t]};a.set(e[n].time,r),o[n]=r}}return{values:a,series:o}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&(t.tsi!==void 0&&(l=Math.min(l,t.tsi),u=Math.max(u,t.tsi)),t.signal!==void 0&&(l=Math.min(l,t.signal),u=Math.max(u,t.signal)))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);0>=f&&0<=p&&(e.strokeStyle=`#787B86`,e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(o.x,h(0)),e.lineTo(o.x+o.width,h(0)),e.stroke(),e.setLineDash([]));let g=(t,o)=>{e.beginPath(),e.strokeStyle=o,e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let l=!1;for(let r=s;r<=c&&r<i.length;r++){let o=i[r];if(!o||o[t]===void 0)continue;let s=a(r,n),c=h(o[t]);l?e.lineTo(s,c):(e.moveTo(s,c),l=!0)}e.stroke()};g(`tsi`,r.colors[0]),g(`signal`,r.colors[1]??`#FF9800`)}},G=class extends k{descriptor={id:`cmf`,name:`Chaikin Money Flow`,placement:`panel`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=0,o=0;for(let r=t-n+1;r<=t;r++){let t=e[r].high-e[r].low,n=t===0?0:(e[r].close-e[r].low-(e[r].high-e[r].close))/t;a+=n*e[r].volume,o+=e[r].volume}let s={value:o===0?0:a/o};r.set(e[t].time,s),i[t]=s}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);0>=f&&0<=p&&(e.strokeStyle=`#787B86`,e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(o.x,h(0)),e.lineTo(o.x+o.width,h(0)),e.stroke(),e.setLineDash([]));let g=n.barWidth/2;for(let t=s;t<=c&&t<i.length;t++){let o=i[t];if(o&&o.value!==void 0){let i=a(t,n),s=h(o.value),c=h(0);e.fillStyle=o.value>=0?r.colors[0]:r.colors[1]??`#EF5350`;let l=Math.min(s,c);e.fillRect(i-g,l,n.barWidth,Math.max(Math.abs(s-c),1))}}}},le=class extends k{descriptor={id:`stddev`,name:`Standard Deviation`,placement:`panel`,defaultConfig:{period:20}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n-1;t<e.length;t++){let a=0;for(let r=t-n+1;r<=t;r++)a+=e[r].close;let o=a/n,s=0;for(let r=t-n+1;r<=t;r++){let t=e[r].close-o;s+=t*t}let c={value:Math.sqrt(s/n)};r.set(e[t].time,c),i[t]=c}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d-f,m=e=>o.y+o.height*(1-(e-f)/p);e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let h=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=m(r.value);h?e.lineTo(i,o):(e.moveTo(i,o),h=!0)}}e.stroke()}},ue=class extends k{descriptor={id:`volumeProfile`,name:`Volume Profile`,placement:`panel`,defaultConfig:{rows:24}};calculate(e,t){let n=t.params.rows,r=new Map,i=Array(e.length).fill(null);if(e.length===0)return{values:r,series:i};let a=1/0,o=-1/0;for(let t of e)t.low<a&&(a=t.low),t.high>o&&(o=t.high);let s=(o-a||1)/n,c=Array(n).fill(0);for(let t of e){let e=(t.high+t.low+t.close)/3,r=Math.min(n-1,Math.floor((e-a)/s));c[r]+=t.volume}for(let e=0;e<n;e++){let t=a+(e+.5)*s;r.set(e,{price:t,volume:c[e],rowIndex:e})}return{values:r,series:i,meta:{minPrice:a,maxPrice:o,rows:n,rowHeight:s,bins:c,maxVolume:Math.max(...c)}}}render(e,t,n,r){let{chartRect:i}=n,a=t.meta;if(!a||a.maxVolume===0)return;let{minPrice:o,rows:s,rowHeight:c,bins:l,maxVolume:u}=a,d=i.height/s,f=i.width*.3;for(let t=0;t<s;t++){let a=(o+(t+.5)*c-n.priceRange.min)/(n.priceRange.max-n.priceRange.min),s=i.y+i.height*(1-a),p=l[t]/u*f;e.fillStyle=l[t]>u*.7?r.colors[1]??`rgba(255, 152, 0, 0.5)`:r.colors[0]??`rgba(33, 150, 243, 0.3)`,e.fillRect(i.x,s-d/2,p,Math.max(d-1,1))}}},de=class extends k{descriptor={id:`ad`,name:`Accumulation/Distribution`,placement:`panel`,defaultConfig:{}};calculate(e,t){let n=new Map,r=Array(e.length).fill(null);if(e.length===0)return{values:n,series:r};let i=0;for(let t=0;t<e.length;t++){let a=e[t].high-e[t].low,o=a===0?0:(e[t].close-e[t].low-(e[t].high-e[t].close))/a;i+=o*e[t].volume;let s={value:i};n.set(e[t].time,s),r[t]=s}return{values:n,series:r}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d-f,m=e=>o.y+o.height*(1-(e-f)/p);e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let h=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=m(r.value);h?e.lineTo(i,o):(e.moveTo(i,o),h=!0)}}e.stroke()}},fe=class extends k{descriptor={id:`vroc`,name:`Volume Rate of Change`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=t.params.period,r=new Map,i=Array(e.length).fill(null);for(let t=n;t<e.length;t++){let a=e[t-n].volume,o={value:a===0?0:(e[t].volume-a)/a*100};r.set(e[t].time,o),i[t]=o}return{values:r,series:i}}render(e,t,n,r){let i=t.series;if(!i)return;let{chartRect:o}=n,{from:s,to:c}=n.visibleRange,l=1/0,u=-1/0;for(let e=s;e<=c&&e<i.length;e++){let t=i[e];t&&t.value!==void 0&&(l=Math.min(l,t.value),u=Math.max(u,t.value))}if(l===1/0)return;let d=(u-l||1)*.1,f=l-d,p=u+d,m=p-f,h=e=>o.y+o.height*(1-(e-f)/m);0>=f&&0<=p&&(e.strokeStyle=`#787B86`,e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(o.x,h(0)),e.lineTo(o.x+o.width,h(0)),e.stroke(),e.setLineDash([])),e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;let g=!1;for(let t=s;t<=c&&t<i.length;t++){let r=i[t];if(r&&r.value!==void 0){let i=a(t,n),o=h(r.value);g?e.lineTo(i,o):(e.moveTo(i,o),g=!0)}}e.stroke()}};function pe(e){e.register(new re),e.register(new ie),e.register(new ae),e.register(new oe),e.register(new se),e.register(new A),e.register(new j),e.register(new M),e.register(new N),e.register(new ce),e.register(new P),e.register(new F),e.register(new I),e.register(new L),e.register(new R),e.register(new z),e.register(new B),e.register(new V),e.register(new H),e.register(new U),e.register(new W),e.register(new G),e.register(new le),e.register(new ue),e.register(new de),e.register(new fe)}var me=class{visible=!0;setVisible(e){this.visible=e}isVisible(){return this.visible}render(t,n,r){if(!this.visible)return;let{chartRect:i,priceRange:a}=n;t.strokeStyle=r.grid,t.lineWidth=1;let o=a.max-a.min;if(o<=0)return;let s=1/o;t.beginPath();let c=(0,e.computeTickStep)(a.min,a.max,8),l=Math.ceil(a.min/c)*c;for(let e=l;e<=a.max;e+=c){let n=Math.round(i.y+i.height*(1-(e-a.min)*s))+.5;t.moveTo(i.x,n),t.lineTo(i.x+i.width,n)}let u=n.barWidth+n.barSpacing,d=Math.max(1,Math.ceil(80/u)),{from:f,to:p}=n.visibleRange,m=-n.offset+i.x+n.barWidth/2;for(let e=f;e<=p;e++){if(e%d!==0)continue;let n=Math.round(e*u+m)+.5;t.moveTo(n,i.y),t.lineTo(n,i.y+i.height)}t.stroke()}},he=class{render(t,n,r){let{chartRect:i,priceRange:a}=n,o=i.x+i.width,s=a.max-a.min;if(s<=0)return;let c=1/s;t.strokeStyle=r.axisLine,t.lineWidth=1,t.beginPath(),t.moveTo(o+.5,i.y),t.lineTo(o+.5,i.y+i.height),t.stroke();let l=(0,e.computeTickStep)(a.min,a.max,8),u=Math.ceil(a.min/l)*l,d=l<1?Math.ceil(-Math.log10(l))+1:2,f=`${r.font.sizeSmall}px ${r.font.family}`,p=[];for(let t=u;t<=a.max;t+=l){let n=i.y+i.height*(1-(t-a.min)*c);p.push({y:n,text:(0,e.formatPrice)(t,d)})}t.font=f,t.textBaseline=`middle`,t.textAlign=`left`,t.fillStyle=r.axisLabelBackground;for(let{y:n}of p)t.fillRect(o+1,n-8,e.PRICE_AXIS_WIDTH-2,16);t.fillStyle=r.axisLabel;for(let{y:e,text:n}of p)t.fillText(n,o+5,e)}},K=new Date,ge=e=>e<10?`0`+e:``+e,_e=-new Date().getTimezoneOffset()/60,ve=`UTC${_e>=0?`+`:``}${_e}`,ye=class{render(e,t,n,r,i){let{chartRect:a}=t,o=i??a.y+a.height;e.strokeStyle=n.axisLine,e.lineWidth=1,e.beginPath(),e.moveTo(a.x,o+.5),e.lineTo(a.x+a.width,o+.5),e.stroke();let s=t.barWidth+t.barSpacing,c=Math.max(1,Math.ceil(80/s)),{from:l,to:u}=t.visibleRange,d=-t.offset+a.x+t.barWidth/2;e.font=`${n.font.sizeSmall}px ${n.font.family}`,e.textBaseline=`top`,e.textAlign=`center`,e.fillStyle=n.axisLabel;let f=-1;for(let t=l;t<=u&&t<r.length;t++){if(t%c!==0)continue;let n=t*s+d,i=r[t].time,a=i>0xe8d4a51000?i:i*1e3;K.setTime(a);let l=K.getDate(),u=K.getMonth()+1,p=K.getHours(),m=K.getMinutes(),h;l===f?h=`${ge(p)}:${ge(m)}`:(h=`${u}/${l}`,f=l),e.fillText(h,n,o+5)}let p=a.x+a.width-4,m=o+4;e.font=`${n.font.sizeSmall-1}px ${n.font.family}`,e.textAlign=`right`,e.textBaseline=`top`,e.fillStyle=n.textSecondary,e.fillText(ve,p,m)}},be=class{panHandler=null;zoomHandler=null;crosshairHandler=null;drawingManager=null;tradingManager=null;viewportGetter=null;onOverlayDirty=null;boundHandlers=[];lastTouchDist=0;lastTouchMid={x:0,y:0};touchActive=!1;constructor(e){this.element=e}setOverlayDirtyCallback(e){this.onOverlayDirty=e}setPanHandler(e){this.panHandler=e}setZoomHandler(e){this.zoomHandler=e}setCrosshairHandler(e){this.crosshairHandler=e}setDrawingManager(e,t){this.drawingManager=e,this.viewportGetter=t}setTradingManager(e,t){this.tradingManager=e,this.viewportGetter=t}attach(){let e=()=>this.viewportGetter?.()??null,t=t=>{let n=this.getMousePos(t),r=e();this.tradingManager&&r&&this.tradingManager.onPointerDown(n,r)||this.drawingManager&&r&&this.drawingManager.onPointerDown(n,r)||this.panHandler?.onPointerDown(n)},n=t=>{let n=this.getMousePos(t),r=e();if(this.tradingManager&&r&&this.tradingManager.onPointerMove(n,r)){this.crosshairHandler?.onPointerMove(n),this.onOverlayDirty?.();return}if(this.drawingManager&&r&&this.drawingManager.onPointerMove(n,r)){this.crosshairHandler?.onPointerMove(n),this.onOverlayDirty?.();return}this.panHandler?.onPointerMove(n),this.crosshairHandler?.onPointerMove(n),this.onOverlayDirty?.()},r=()=>{this.tradingManager?.onPointerUp()||this.drawingManager?.onPointerUp()||this.panHandler?.onPointerUp()},i=()=>{this.panHandler?.onPointerUp(),this.drawingManager?.onPointerUp(),this.tradingManager?.onPointerUp(),this.crosshairHandler?.onPointerLeave(),this.onOverlayDirty?.()},a=e=>{e.preventDefault();let t=this.getMousePos(e);this.zoomHandler?.onWheel(e.deltaY,t)},o=e=>{this.drawingManager?.onKeyDown(e.key,e.ctrlKey||e.metaKey)&&e.preventDefault()},s=t=>{let n=e();this.tradingManager&&n&&(t.preventDefault(),this.tradingManager.onContextMenu(this.getMousePos(t),n))},c=e=>{if(e.preventDefault(),e.touches.length===1){let t=this.getTouchPos(e.touches[0]);this.panHandler?.onPointerDown(t),this.crosshairHandler?.onPointerMove(t),this.touchActive=!0}else e.touches.length===2&&(this.panHandler?.onPointerUp(),this.lastTouchDist=this.getTouchDistance(e.touches[0],e.touches[1]),this.lastTouchMid=this.getTouchMidpoint(e.touches[0],e.touches[1]),this.touchActive=!0)},l=e=>{if(e.preventDefault(),e.touches.length===1&&this.touchActive){let t=this.getTouchPos(e.touches[0]);this.panHandler?.onPointerMove(t),this.crosshairHandler?.onPointerMove(t)}else if(e.touches.length===2){let t=this.getTouchDistance(e.touches[0],e.touches[1]),n=this.getTouchMidpoint(e.touches[0],e.touches[1]);if(this.lastTouchDist>0){let e=(t/this.lastTouchDist-1)*.5;this.zoomHandler?.onWheel(-e*100,n)}let r=this.lastTouchMid.x-n.x;Math.abs(r)>1&&(this.panHandler?.onPointerDown(this.lastTouchMid),this.panHandler?.onPointerMove(n)),this.lastTouchDist=t,this.lastTouchMid=n}this.onOverlayDirty?.()},u=e=>{if(e.touches.length===0)this.panHandler?.onPointerUp(),this.crosshairHandler?.onPointerLeave(),this.touchActive=!1,this.lastTouchDist=0;else if(e.touches.length===1){let t=this.getTouchPos(e.touches[0]);this.panHandler?.onPointerDown(t)}};this.element.addEventListener(`mousedown`,t),this.element.addEventListener(`mousemove`,n),document.addEventListener(`mouseup`,r),this.element.addEventListener(`mouseleave`,i),this.element.addEventListener(`wheel`,a,{passive:!1}),this.element.addEventListener(`contextmenu`,s),this.element.addEventListener(`touchstart`,c,{passive:!1}),this.element.addEventListener(`touchmove`,l,{passive:!1}),this.element.addEventListener(`touchend`,u),document.addEventListener(`keydown`,o),this.boundHandlers.push(()=>this.element.removeEventListener(`mousedown`,t),()=>this.element.removeEventListener(`mousemove`,n),()=>document.removeEventListener(`mouseup`,r),()=>this.element.removeEventListener(`mouseleave`,i),()=>this.element.removeEventListener(`wheel`,a),()=>this.element.removeEventListener(`contextmenu`,s),()=>this.element.removeEventListener(`touchstart`,c),()=>this.element.removeEventListener(`touchmove`,l),()=>this.element.removeEventListener(`touchend`,u),()=>document.removeEventListener(`keydown`,o))}detach(){for(let e of this.boundHandlers)e();this.boundHandlers=[]}getMousePos(e){let t=this.element.getBoundingClientRect();return{x:e.clientX-t.left,y:e.clientY-t.top}}getTouchPos(e){let t=this.element.getBoundingClientRect();return{x:e.clientX-t.left,y:e.clientY-t.top}}getTouchDistance(e,t){return Math.hypot(t.clientX-e.clientX,t.clientY-e.clientY)}getTouchMidpoint(e,t){let n=this.element.getBoundingClientRect();return{x:(e.clientX+t.clientX)/2-n.left,y:(e.clientY+t.clientY)/2-n.top}}},xe=class{dragging=!1;lastX=0;lastTime=0;velocity=0;momentumId=0;callback;friction=.92;constructor(e){this.callback=e}onPointerDown(e){this.dragging=!0,this.lastX=e.x,this.lastTime=Date.now(),this.velocity=0,this.stopMomentum()}onPointerMove(e){if(!this.dragging)return;let t=Date.now(),n=this.lastX-e.x,r=t-this.lastTime;r>0&&(this.velocity=n/r),this.lastX=e.x,this.lastTime=t,this.callback(n)}onPointerUp(){this.dragging&&(this.dragging=!1,Math.abs(this.velocity)>.1&&this.startMomentum())}startMomentum(){this.stopMomentum();let e=this.velocity*16,t=()=>{if(e*=this.friction,Math.abs(e)<.5){this.momentumId=0;return}this.callback(e),this.momentumId=requestAnimationFrame(t)};this.momentumId=requestAnimationFrame(t)}stopMomentum(){this.momentumId&&=(cancelAnimationFrame(this.momentumId),0)}},Se=class{callback;sensitivity;pendingDelta=0;pendingCenterX=0;rafId=0;constructor(e,t=.001){this.callback=e,this.sensitivity=t}onWheel(e,t){this.pendingDelta+=-e*this.sensitivity,this.pendingCenterX=t.x,this.rafId||=requestAnimationFrame(()=>{this.rafId=0;let e=this.pendingDelta,t=this.pendingCenterX;this.pendingDelta=0,this.callback(e,t)})}dispose(){this.rafId&&=(cancelAnimationFrame(this.rafId),0)}},Ce=class{position=null;callback=null;data=[];magnetMode=!0;mode=`magnet`;pricePrecision=2;pendingBarIndex=null;pendingPoint=null;callbackScheduled=!1;lastCallbackBarIndex=-1;setCallback(e){this.callback=e}setData(e){this.data=e}setMagnetMode(e){this.magnetMode=e,this.mode=e?`magnet`:`normal`}setMode(e){this.mode=e,this.magnetMode=e===`magnet`}getMode(){return this.mode}setPricePrecision(e){this.pricePrecision=e}getPosition(){return this.position}onPointerMove(e){this.position=e}onPointerLeave(){this.position=null,this.pendingBarIndex=null,this.pendingPoint=null,this.lastCallbackBarIndex=-1,this.flushCallback(null,null)}render(t,n,r){if(!this.position||this.mode===`hidden`)return;let{chartRect:i}=n,{x:o,y:l}=this.position;if(o<i.x||o>i.x+i.width||l<i.y||l>i.y+i.height)return;let u=s(o,n);u=Math.max(0,Math.min(this.data.length-1,u)),this.magnetMode&&u>=0&&u<this.data.length&&(o=a(u,n)),u!==this.lastCallbackBarIndex&&(this.pendingBarIndex=u,this.pendingPoint={x:o,y:l},this.lastCallbackBarIndex=u,this.scheduleCallback()),t.setLineDash([4,4]),t.strokeStyle=r.crosshair,t.lineWidth=1,t.beginPath(),t.moveTo(Math.round(o)+.5,i.y),t.lineTo(Math.round(o)+.5,i.y+i.height),t.stroke(),t.beginPath(),t.moveTo(i.x,Math.round(l)+.5),t.lineTo(i.x+i.width,Math.round(l)+.5),t.stroke(),t.setLineDash([]);let d=(0,e.formatPrice)(c(l,n),this.pricePrecision),f=i.x+i.width+1;t.fillStyle=r.axisLabelBackground,t.fillRect(f,l-10,e.PRICE_AXIS_WIDTH-2,20),t.fillStyle=r.axisLabel,t.font=`${r.font.sizeSmall}px ${r.font.family}`,t.textBaseline=`middle`,t.textAlign=`left`,t.fillText(d,f+5,l)}scheduleCallback(){this.callbackScheduled||(this.callbackScheduled=!0,queueMicrotask(()=>{this.callbackScheduled=!1,this.flushCallback(this.pendingBarIndex,this.pendingPoint)}))}flushCallback(e,t){this.callback?.(e,t)}},we=class{callbacks;enabled=!0;constructor(e){this.callbacks=e}setEnabled(e){this.enabled=e}handleKey(e){if(!this.enabled)return!1;let t=e.shiftKey?10:1;switch(e.key){case`ArrowLeft`:return this.callbacks.scrollBars(-t),!0;case`ArrowRight`:return this.callbacks.scrollBars(t),!0;case`ArrowUp`:case`+`:case`=`:return this.callbacks.zoom(.1),!0;case`ArrowDown`:case`-`:return this.callbacks.zoom(-.1),!0;case`Home`:return this.callbacks.goToStart(),!0;case`End`:return this.callbacks.goToEnd(),!0;case` `:return this.callbacks.fitContent(),!0;default:return!1}}},Te=class{el=null;visible=!1;timeEl;openEl;highEl;lowEl;closeEl;changeEl;volEl;measuredWidth=155;measuredHeight=95;measured=!1;create(e){if(this.el)return;this.el=document.createElement(`div`),Object.assign(this.el.style,{position:`absolute`,display:`none`,padding:`6px 10px`,borderRadius:`4px`,fontSize:`11px`,fontFamily:`-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif`,lineHeight:`1.5`,pointerEvents:`none`,zIndex:`100`,whiteSpace:`nowrap`,willChange:`transform`}),this.timeEl=this.addLine(this.el);let t=this.addLine(this.el),n=this.addLine(this.el);this.changeEl=this.addLine(this.el),this.volEl=this.addLine(this.el),t.append(this.label(`O `),this.openEl=this.val(),this.label(` H `),this.highEl=this.val()),n.append(this.label(`L `),this.lowEl=this.val(),this.label(` C `),this.closeEl=this.val()),e.appendChild(this.el)}show(e,t,n,r){if(!this.el)return;let i=t.close>=t.open?n.candleUp:n.candleDown,a=e=>e.toFixed(2),o=new Date(t.time);this.timeEl.textContent=`${o.getMonth()+1}/${o.getDate()} ${String(o.getHours()).padStart(2,`0`)}:${String(o.getMinutes()).padStart(2,`0`)}`,this.timeEl.style.color=n.textSecondary,this.openEl.textContent=a(t.open),this.highEl.textContent=a(t.high),this.lowEl.textContent=a(t.low),this.closeEl.textContent=a(t.close),this.openEl.style.color=i,this.highEl.style.color=i,this.lowEl.style.color=i,this.closeEl.style.color=i;let s=t.close-t.open,c=t.open===0?0:s/t.open*100,l=s>=0?`+`:``;this.changeEl.textContent=`${l}${a(s)} (${l}${c.toFixed(2)}%)`,this.changeEl.style.color=i;let u=t.volume;this.volEl.textContent=`Vol ${u>=1e6?(u/1e6).toFixed(1)+`M`:u>=1e3?(u/1e3).toFixed(1)+`K`:u.toFixed(0)}`,this.volEl.style.color=n.textSecondary;let d=Ee(n.background);this.el.style.background=d?De(n.background,.95):De(n.background,.97),this.el.style.border=`1px solid ${n.axisLine}`,this.el.style.boxShadow=d?`0 4px 12px rgba(0,0,0,0.5)`:`0 4px 12px rgba(0,0,0,0.15)`,this.el.style.color=n.text,this.el.style.backdropFilter=`blur(8px)`,!this.measured&&this.visible&&(this.measuredWidth=this.el.offsetWidth,this.measuredHeight=this.el.offsetHeight,this.measured=!0);let f=e.x+16,p=e.y-40;f+this.measuredWidth>r.width-80&&(f=e.x-this.measuredWidth-16),p<4&&(p=4),p+this.measuredHeight>r.height&&(p=r.height-this.measuredHeight-4),this.el.style.transform=`translate(${Math.round(f)}px, ${Math.round(p)}px)`,this.el.style.left=`0`,this.el.style.top=`0`,this.visible||=(this.el.style.display=`block`,!0)}hide(){this.el&&this.visible&&(this.el.style.display=`none`,this.visible=!1)}isVisible(){return this.visible}destroy(){this.el?.remove(),this.el=null,this.visible=!1,this.measured=!1}addLine(e){let t=document.createElement(`div`);return e.appendChild(t),t}label(e){let t=document.createElement(`span`);return t.textContent=e,t.style.opacity=`0.6`,t}val(){return document.createElement(`span`)}};function Ee(e){if(e.startsWith(`#`)){let t=e.slice(1),n=t.length===3?t.split(``).map(e=>e+e).join(``):t.slice(0,6),r=parseInt(n.slice(0,2),16),i=parseInt(n.slice(2,4),16),a=parseInt(n.slice(4,6),16);return(.299*r+.587*i+.114*a)/255<.5}let t=e.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);if(t){let e=parseInt(t[1]),n=parseInt(t[2]),r=parseInt(t[3]);return(.299*e+.587*n+.114*r)/255<.5}return!0}function De(e,t){if(e.startsWith(`#`)){let n=e.slice(1),r=n.length===3?n.split(``).map(e=>e+e).join(``):n.slice(0,6);return`rgba(${parseInt(r.slice(0,2),16)}, ${parseInt(r.slice(2,4),16)}, ${parseInt(r.slice(4,6),16)}, ${t})`}return e}var Oe=class{listeners=new Map;tauriBridge=null;on(e,t){let n=this.listeners.get(e);n||(n=new Set,this.listeners.set(e,n)),n.add(t)}off(e,t){this.listeners.get(e)?.delete(t)}emit(e,t){let n={type:e,timestamp:Date.now(),payload:t},r=this.listeners.get(e);if(r)for(let e of r)e(n);if(this.tauriBridge?.enabled&&window.__TAURI__){let t=this.tauriBridge.eventPrefix??`tc-chart`;window.__TAURI__.event.emit(`${t}:${e}`,n).catch(()=>{})}}enableTauriBridge(e){this.tauriBridge=e}disableTauriBridge(){this.tauriBridge=null}destroy(){this.listeners.clear(),this.tauriBridge=null}},q=class{hitTestAnchor(e,t,n,r){for(let i=0;i<t.anchors.length;i++){let a=this.anchorToPixel(t.anchors[i],n);if(Math.hypot(e.x-a.x,e.y-a.y)<=r)return i}return-1}anchorToPixel(e,t){return{x:a(e.time,t),y:o(e.price,t)}}pixelToAnchor(e,t){return{time:s(e.x,t),price:c(e.y,t)}}applyLineStyle(e,t){switch(e.strokeStyle=t.color,e.lineWidth=t.lineWidth,t.lineStyle){case`dashed`:e.setLineDash([6,4]);break;case`dotted`:e.setLineDash([2,2]);break;default:e.setLineDash([]);break}}resetLineStyle(e){e.setLineDash([])}renderAnchorHandles(e,t,n){e.fillStyle=`#FFFFFF`,e.strokeStyle=t.style.color,e.lineWidth=1;for(let r of t.anchors){let t=this.anchorToPixel(r,n);e.fillRect(t.x-4,t.y-4,8,8),e.strokeRect(t.x-4,t.y-4,8,8)}}distanceToLine(e,t,n){let r=n.x-t.x,i=n.y-t.y,a=r*r+i*i;if(a===0)return Math.hypot(e.x-t.x,e.y-t.y);let o=((e.x-t.x)*r+(e.y-t.y)*i)/a;o=Math.max(0,Math.min(1,o));let s=t.x+o*r,c=t.y+o*i;return Math.hypot(e.x-s,e.y-c)}distanceToInfiniteLine(e,t,n){let r=n.x-t.x,i=n.y-t.y,a=Math.hypot(r,i);return a===0?Math.hypot(e.x-t.x,e.y-t.y):Math.abs(r*(t.y-e.y)-i*(t.x-e.x))/a}},ke=1,Ae=class{registry=new Map;drawings=[];state=`idle`;activeTool=null;activeStyle={color:`#2196F3`,lineWidth:1,lineStyle:`solid`,fillColor:`rgba(33, 150, 243, 0.1)`,fillOpacity:.1,fontSize:12};creatingDrawing=null;committedAnchors=0;previewAnchor=null;selectedDrawingId=null;dragAnchorIndex=-1;dragStartPoint=null;dragStartAnchors=[];dragBeforeState=null;eventCallback=null;requestRender=null;undoRedo=null;magnetMode=`none`;dataGetter=null;displayDataGetter=null;register(e){this.registry.set(e.descriptor.type,e)}setEventCallback(e){this.eventCallback=e}setRequestRender(e){this.requestRender=e}setUndoRedoManager(e){this.undoRedo=e}setMagnetMode(e){this.magnetMode=e}getMagnetMode(){return this.magnetMode}setDataGetter(e){this.dataGetter=e}setDisplayDataGetter(e){this.displayDataGetter=e}snapToOHLC(e,t,n){if(this.magnetMode===`none`||!this.dataGetter)return{time:e,price:t};let r=this.displayDataGetter?.()??this.dataGetter(),i=Math.round(e);if(i<0||i>=r.length)return{time:e,price:t};let a=r[i],o=[a.open,a.high,a.low,a.close],s=o[0],c=Math.abs(t-s);for(let e=1;e<o.length;e++){let n=Math.abs(t-o[e]);n<c&&(c=n,s=o[e])}if(n){let e=n.chartRect.height/(n.priceRange.max-n.priceRange.min||1);if(c*e>30)return{time:i,price:t}}return{time:i,price:s}}undo(){if(!this.undoRedo)return!1;let e=this.undoRedo.undo();return e?(this.applyUndoAction(e),!0):!1}redo(){if(!this.undoRedo)return!1;let e=this.undoRedo.redo();return e?(this.applyRedoAction(e),!0):!1}applyUndoAction(e){switch(e.type){case`drawingCreate`:e.after&&(this.drawings=this.drawings.filter(t=>t.id!==e.after.id));break;case`drawingRemove`:e.before&&this.drawings.push(e.before);break;case`drawingModify`:if(e.before){let t=this.drawings.findIndex(t=>t.id===e.before.id);t>=0&&(this.drawings[t]=structuredClone(e.before))}break}this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()}applyRedoAction(e){switch(e.type){case`drawingCreate`:e.after&&this.drawings.push(structuredClone(e.after));break;case`drawingRemove`:e.before&&(this.drawings=this.drawings.filter(t=>t.id!==e.before.id));break;case`drawingModify`:if(e.after){let t=this.drawings.findIndex(t=>t.id===e.after.id);t>=0&&(this.drawings[t]=structuredClone(e.after))}break}this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()}lockAllDrawings(){for(let e of this.drawings)e.locked=!0;this.requestRender?.()}unlockAllDrawings(){for(let e of this.drawings)e.locked=!1;this.requestRender?.()}hideAllDrawings(){for(let e of this.drawings)e.visible=!1;this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()}showAllDrawings(){for(let e of this.drawings)e.visible=!0;this.requestRender?.()}setActiveTool(e){this.activeTool=e,this.state=e?`creating`:`idle`,this.creatingDrawing=null,this.committedAnchors=0,this.previewAnchor=null}getActiveTool(){return this.activeTool}setStyle(e){Object.assign(this.activeStyle,e)}onPointerDown(e,t){return this.state===`creating`&&this.activeTool?this.handleCreationClick(e,t):this.state===`idle`||this.state===`selected`?this.handleSelectionClick(e,t):!1}onPointerMove(e,t){if(this.state===`creating`&&this.creatingDrawing){let n=this.registry.get(this.creatingDrawing.type);if(n&&this.committedAnchors<n.descriptor.requiredAnchors){let n=s(e.x,t),r=c(e.y,t);return this.previewAnchor=this.snapToOHLC(n,r,t),this.requestRender?.(),!0}}return this.state===`moving`&&this.selectedDrawingId&&this.dragStartPoint?this.handleMove(e,t):this.state===`resizing`&&this.selectedDrawingId&&this.dragAnchorIndex>=0?this.handleResize(e,t):!1}onPointerUp(){if(this.state===`moving`||this.state===`resizing`){if(this.dragBeforeState&&this.selectedDrawingId){let e=this.drawings.find(e=>e.id===this.selectedDrawingId);e&&this.undoRedo?.push({type:`drawingModify`,before:this.dragBeforeState,after:structuredClone(e)})}return this.state=`selected`,this.dragStartPoint=null,this.dragStartAnchors=[],this.dragAnchorIndex=-1,this.dragBeforeState=null,!0}return!1}onKeyDown(e,t=!1){if(t&&(e===`d`||e===`D`)&&this.state===`selected`&&this.selectedDrawingId&&this.duplicateDrawing(this.selectedDrawingId)||t&&(e===`z`||e===`Z`)&&this.undo()||t&&(e===`y`||e===`Y`)&&this.redo())return!0;if(e===`Escape`){if(this.state===`creating`)return this.setActiveTool(null),this.requestRender?.(),!0;if(this.state===`selected`)return this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.(),!0}return(e===`Delete`||e===`Backspace`)&&this.state===`selected`&&this.selectedDrawingId?this.drawings.find(e=>e.id===this.selectedDrawingId)?.locked?!1:(this.removeDrawing(this.selectedDrawingId),this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.(),!0):!1}getDrawings(){return this.drawings}setDrawings(e){this.drawings=e,this.requestRender?.()}removeDrawing(e){let t=this.drawings.find(t=>t.id===e);this.drawings=this.drawings.filter(t=>t.id!==e),t&&this.undoRedo?.push({type:`drawingRemove`,before:structuredClone(t),after:null}),this.eventCallback?.(`drawingRemove`,{id:e}),this.requestRender?.()}duplicateDrawing(e){let t=this.drawings.find(t=>t.id===e);if(!t)return null;let n=structuredClone(t);return n.id=`tc_drawing_${ke++}`,n.anchors=n.anchors.map(e=>({...e,time:e.time+3})),n.locked=!1,this.drawings.push(n),this.undoRedo?.push({type:`drawingCreate`,before:null,after:structuredClone(n)}),this.selectedDrawingId=n.id,this.state=`selected`,this.eventCallback?.(`drawingCreate`,{drawing:n}),this.requestRender?.(),n.id}clearDrawings(){this.drawings=[],this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()}getSelectedDrawingId(){return this.selectedDrawingId}render(e,t){e.save(),e.beginPath(),e.rect(t.chartRect.x,t.chartRect.y,t.chartRect.width,t.chartRect.height),e.clip();for(let n of this.drawings){if(!n.visible)continue;let r=this.registry.get(n.type);if(!r)continue;let i=n.id===this.selectedDrawingId;r.render(e,n,t,i)}if(this.creatingDrawing&&this.committedAnchors>0){let n=this.registry.get(this.creatingDrawing.type);if(n){let r={...this.creatingDrawing,anchors:this.previewAnchor?[...this.creatingDrawing.anchors,this.previewAnchor]:[...this.creatingDrawing.anchors]};n.render(e,r,t,!1)}}e.restore()}handleCreationClick(e,t){if(!this.activeTool)return!1;let n=this.registry.get(this.activeTool);if(!n)return!1;let r=s(e.x,t),i=c(e.y,t),a=this.snapToOHLC(r,i,t);return this.creatingDrawing?(this.creatingDrawing.anchors.push(a),this.committedAnchors++,this.previewAnchor=null,this.committedAnchors>=n.descriptor.requiredAnchors&&this.finalizeCreation(),this.requestRender?.(),!0):(this.creatingDrawing={id:`tc_drawing_${ke++}`,type:this.activeTool,anchors:[a],style:{...this.activeStyle},visible:!0,locked:!1},this.committedAnchors=1,this.previewAnchor=null,n.descriptor.requiredAnchors===1&&this.finalizeCreation(),this.requestRender?.(),!0)}finalizeCreation(){this.creatingDrawing&&(this.drawings.push(this.creatingDrawing),this.selectedDrawingId=this.creatingDrawing.id,this.undoRedo?.push({type:`drawingCreate`,before:null,after:structuredClone(this.creatingDrawing)}),this.eventCallback?.(`drawingCreate`,{drawing:this.creatingDrawing}),this.creatingDrawing=null,this.committedAnchors=0,this.previewAnchor=null,this.activeTool=null,this.state=`selected`)}handleSelectionClick(e,t){if(this.selectedDrawingId){let n=this.drawings.find(e=>e.id===this.selectedDrawingId);if(n&&!n.locked){let r=this.registry.get(n.type);if(r){let i=r.hitTestAnchor(e,n,t,8);if(i>=0)return this.state=`resizing`,this.dragAnchorIndex=i,this.dragStartPoint=e,this.dragBeforeState=structuredClone(n),!0;if(r.hitTest(e,n,t,8))return this.state=`moving`,this.dragStartPoint=e,this.dragStartAnchors=n.anchors.map(e=>({...e})),this.dragBeforeState=structuredClone(n),!0}}}for(let n=this.drawings.length-1;n>=0;n--){let r=this.drawings[n];if(!r.visible)continue;let i=this.registry.get(r.type);if(i&&i.hitTest(e,r,t,8))return this.selectedDrawingId=r.id,this.state=`selected`,this.requestRender?.(),!0}return this.selectedDrawingId&&(this.selectedDrawingId=null,this.state=`idle`,this.requestRender?.()),!1}handleMove(e,t){let n=this.drawings.find(e=>e.id===this.selectedDrawingId);if(!n||!this.dragStartPoint)return!1;let r=s(e.x,t)-s(this.dragStartPoint.x,t),i=c(e.y,t)-c(this.dragStartPoint.y,t);return n.anchors=this.dragStartAnchors.map(e=>({time:e.time+r,price:e.price+i})),this.requestRender?.(),!0}handleResize(e,t){let n=this.drawings.find(e=>e.id===this.selectedDrawingId);return!n||this.dragAnchorIndex<0?!1:(n.anchors[this.dragAnchorIndex]={time:s(e.x,t),price:c(e.y,t)},this.requestRender?.(),!0)}},je=class{constructor(e){this.manager=e}render(e,t){this.manager.render(e,t)}},Me=class extends q{descriptor={type:`trendLine`,name:`Trend Line`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);return this.distanceToLine(e,i,a)<=r}},Ne=class extends q{descriptor={type:`horizontalLine`,name:`Horizontal Line`,requiredAnchors:1};render(e,t,n,r){if(t.anchors.length<1)return;let i=o(t.anchors[0].price,n),{chartRect:a}=n;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(a.x,i),e.lineTo(a.x+a.width,i),e.stroke(),this.resetLineStyle(e),e.fillStyle=t.style.color,e.font=`11px sans-serif`,e.textBaseline=`bottom`,e.fillText(t.anchors[0].price.toFixed(2),a.x+4,i-3),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<1)return!1;let i=o(t.anchors[0].price,n);return Math.abs(e.y-i)<=r&&e.x>=n.chartRect.x&&e.x<=n.chartRect.x+n.chartRect.width}},Pe=class extends q{descriptor={type:`verticalLine`,name:`Vertical Line`,requiredAnchors:1};render(e,t,n,r){if(t.anchors.length<1)return;let i=this.anchorToPixel(t.anchors[0],n),{chartRect:a}=n;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,a.y),e.lineTo(i.x,a.y+a.height),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<1)return!1;let i=this.anchorToPixel(t.anchors[0],n),{chartRect:a}=n;return Math.abs(e.x-i.x)<=r&&e.y>=a.y&&e.y<=a.y+a.height}},Fe=class extends q{descriptor={type:`ray`,name:`Ray`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),{chartRect:o}=n,s=a.x-i.x,c=a.y-i.y,l=Math.hypot(s,c);if(l===0)return;let u=Math.hypot(o.width,o.height)*2,d=i.x+s/l*u,f=i.y+c/l*u;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(d,f),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=a.x-i.x,s=a.y-i.y;return((e.x-i.x)*o+(e.y-i.y)*s)/(o*o+s*s)<0?!1:this.distanceToInfiniteLine(e,i,a)<=r}},Ie=class extends q{descriptor={type:`extendedLine`,name:`Extended Line`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),{chartRect:o}=n,s=a.x-i.x,c=a.y-i.y,l=Math.hypot(s,c);if(l===0)return;let u=Math.hypot(o.width,o.height)*2,d=i.x-s/l*u,f=i.y-c/l*u,p=i.x+s/l*u,m=i.y+c/l*u;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(d,f),e.lineTo(p,m),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);return this.distanceToInfiniteLine(e,i,a)<=r}},Le=class extends q{descriptor={type:`parallelChannel`,name:`Parallel Channel`,requiredAnchors:3};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);if(this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke(),t.anchors.length>=3){let r=this.anchorToPixel(t.anchors[2],n),o=a.x-i.x,s=a.y-i.y,c=Math.hypot(o,s)||1,l=-s/c,u=o/c,d=(r.x-i.x)*l+(r.y-i.y)*u,f={x:i.x+l*d,y:i.y+u*d},p={x:a.x+l*d,y:a.y+u*d};e.beginPath(),e.moveTo(f.x,f.y),e.lineTo(p.x,p.y),e.stroke(),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.lineTo(p.x,p.y),e.lineTo(f.x,f.y),e.closePath(),e.fill())}this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);if(this.distanceToLine(e,i,a)<=r)return!0;if(t.anchors.length>=3){let o=this.anchorToPixel(t.anchors[2],n),s=a.x-i.x,c=a.y-i.y,l=Math.hypot(s,c)||1,u=-c/l,d=s/l,f=(o.x-i.x)*u+(o.y-i.y)*d,p={x:i.x+u*f,y:i.y+d*f},m={x:a.x+u*f,y:a.y+d*f};if(this.distanceToLine(e,p,m)<=r)return!0}return!1}},Re=class extends q{descriptor={type:`regressionChannel`,name:`Regression Channel`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke();let o=a.x-i.x,s=a.y-i.y,c=Math.hypot(o,s);if(c===0){this.resetLineStyle(e);return}let l=Math.max(10,Math.min(c*.2,100)),u=-s/c*l,d=o/c*l;e.setLineDash([4,4]),e.beginPath(),e.moveTo(i.x+u,i.y+d),e.lineTo(a.x+u,a.y+d),e.stroke(),e.beginPath(),e.moveTo(i.x-u,i.y-d),e.lineTo(a.x-u,a.y-d),e.stroke(),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.beginPath(),e.moveTo(i.x+u,i.y+d),e.lineTo(a.x+u,a.y+d),e.lineTo(a.x-u,a.y-d),e.lineTo(i.x-u,i.y-d),e.closePath(),e.fill()),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.hypot(a.x-i.x,a.y-i.y),s=Math.max(10,Math.min(o*.2,100));return this.distanceToLine(e,i,a)<=r+s}},J=[0,.236,.382,.5,.618,.786,1],ze=class extends q{descriptor={type:`fibRetracement`,name:`Fibonacci Retracement`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let{chartRect:i}=n,a=t.anchors[0].price,s=t.anchors[1].price,c=a-s;e.font=`11px sans-serif`,e.textBaseline=`middle`;for(let r of J){let a=s+c*(1-r),l=o(a,n);this.applyLineStyle(e,t.style),e.globalAlpha=r===0||r===1?1:.7,e.beginPath(),e.moveTo(i.x,l),e.lineTo(i.x+i.width,l),e.stroke(),e.fillStyle=t.style.color,e.textAlign=`left`,e.fillText(`${(r*100).toFixed(1)}% (${a.toFixed(2)})`,i.x+4,l-2)}if(e.globalAlpha=1,t.style.fillColor){for(let r=0;r<J.length-1;r++){let a=s+c*(1-J[r]),l=s+c*(1-J[r+1]),u=o(a,n),d=o(l,n);e.fillStyle=t.style.fillColor,e.globalAlpha=.05+r*.02,e.fillRect(i.x,Math.min(u,d),i.width,Math.abs(d-u))}e.globalAlpha=1}this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let{chartRect:i}=n;if(e.x<i.x-r||e.x>i.x+i.width+r)return!1;let a=t.anchors[0].price,s=t.anchors[1].price,c=a-s;for(let t of J){let i=o(s+c*(1-t),n);if(Math.abs(e.y-i)<=r)return!0}return!1}},Be=[0,.618,1,1.618,2,2.618,3.618,4.236],Ve=class extends q{descriptor={type:`fibExtension`,name:`Fibonacci Extension`,requiredAnchors:3};render(e,t,n,r){if(t.anchors.length<3){if(t.anchors.length===2){let r=this.anchorToPixel(t.anchors[0],n),i=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(r.x,r.y),e.lineTo(i.x,i.y),e.stroke(),this.resetLineStyle(e)}return}let{chartRect:i}=n,a=t.anchors[0].price,s=t.anchors[1].price,c=t.anchors[2].price,l=a-s;e.font=`11px sans-serif`,e.textBaseline=`middle`,e.textAlign=`left`;for(let r of Be){let a=o(c+l*r,n);this.applyLineStyle(e,t.style),e.globalAlpha=r===0||r===1?1:.6,e.beginPath(),e.moveTo(i.x,a),e.lineTo(i.x+i.width,a),e.stroke(),e.fillStyle=t.style.color,e.fillText(`${(r*100).toFixed(1)}%`,i.x+4,a-2)}e.globalAlpha=1,this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<3)return!1;let{chartRect:i}=n;if(e.x<i.x-r||e.x>i.x+i.width+r)return!1;let a=t.anchors[0].price-t.anchors[1].price;for(let i of Be){let s=o(t.anchors[2].price+a*i,n);if(Math.abs(e.y-s)<=r)return!0}return!1}},He=class extends q{descriptor={type:`rectangle`,name:`Rectangle`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.min(i.y,a.y),c=Math.abs(a.x-i.x),l=Math.abs(a.y-i.y);t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.fillRect(o,s,c,l)),this.applyLineStyle(e,t.style),e.strokeRect(o,s,c,l),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x)-r,s=Math.min(i.y,a.y)-r,c=Math.max(i.x,a.x)+r,l=Math.max(i.y,a.y)+r;if(e.x<o||e.x>c||e.y<s||e.y>l)return!1;let u=Math.min(i.x,a.x)+r,d=Math.min(i.y,a.y)+r,f=Math.max(i.x,a.x)-r,p=Math.max(i.y,a.y)-r;return!(e.x>u&&e.x<f&&e.y>d&&e.y<p)||!!t.style.fillColor}},Ue=class extends q{descriptor={type:`ellipse`,name:`Ellipse`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=(i.x+a.x)/2,s=(i.y+a.y)/2,c=Math.abs(a.x-i.x)/2,l=Math.abs(a.y-i.y)/2;e.beginPath(),e.ellipse(o,s,Math.max(c,1),Math.max(l,1),0,0,Math.PI*2),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.fill()),this.applyLineStyle(e,t.style),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=(i.x+a.x)/2,s=(i.y+a.y)/2,c=Math.max(Math.abs(a.x-i.x)/2,1),l=Math.max(Math.abs(a.y-i.y)/2,1),u=(e.x-o)/c,d=(e.y-s)/l,f=Math.sqrt(u*u+d*d),p=r/Math.max(Math.min(c,l),4);return t.style.fillColor?f<=1+p:Math.abs(f-1)<=p}},We=class extends q{descriptor={type:`triangle`,name:`Triangle`,requiredAnchors:3};render(e,t,n,r){if(t.anchors.length<3){if(t.anchors.length===2){let r=this.anchorToPixel(t.anchors[0],n),i=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(r.x,r.y),e.lineTo(i.x,i.y),e.stroke(),this.resetLineStyle(e)}return}let i=t.anchors.map(e=>this.anchorToPixel(e,n));e.beginPath(),e.moveTo(i[0].x,i[0].y),e.lineTo(i[1].x,i[1].y),e.lineTo(i[2].x,i[2].y),e.closePath(),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.fill()),this.applyLineStyle(e,t.style),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<3)return!1;let i=t.anchors.map(e=>this.anchorToPixel(e,n));for(let t=0;t<3;t++){let n=(t+1)%3;if(this.distanceToLine(e,i[t],i[n])<=r)return!0}return!1}},Ge=class extends q{descriptor={type:`pitchfork`,name:`Andrews' Pitchfork`,requiredAnchors:3};render(e,t,n,r){if(t.anchors.length<3){if(t.anchors.length===2){let r=this.anchorToPixel(t.anchors[0],n),i=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(r.x,r.y),e.lineTo(i.x,i.y),e.stroke(),this.resetLineStyle(e)}return}let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=this.anchorToPixel(t.anchors[2],n),s={x:(a.x+o.x)/2,y:(a.y+o.y)/2},c=s.x-i.x,l=s.y-i.y,u=Math.hypot(c,l)||1,d=Math.hypot(n.chartRect.width,n.chartRect.height),f=i.x+c/u*d,p=i.y+l/u*d;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(f,p),e.stroke(),e.beginPath(),e.moveTo(a.x,a.y),e.lineTo(a.x+c/u*d,a.y+l/u*d),e.stroke(),e.beginPath(),e.moveTo(o.x,o.y),e.lineTo(o.x+c/u*d,o.y+l/u*d),e.stroke(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<3)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=this.anchorToPixel(t.anchors[2],n),s={x:(a.x+o.x)/2,y:(a.y+o.y)/2};if(this.distanceToInfiniteLine(e,i,s)<=r)return!0;let c=s.x-i.x,l=s.y-i.y,u={x:a.x+c,y:a.y+l},d={x:o.x+c,y:o.y+l};return this.distanceToInfiniteLine(e,a,u)<=r||this.distanceToInfiniteLine(e,o,d)<=r}},Ke=[`1`,`2`,`3`,`4`,`5`,`A`,`B`,`C`],qe=class extends q{descriptor={type:`elliottWave`,name:`Elliott Wave`,requiredAnchors:8};render(e,t,n,r){if(t.anchors.length<2)return;let i=t.anchors.map(e=>this.anchorToPixel(e,n));this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i[0].x,i[0].y);for(let t=1;t<i.length;t++)e.lineTo(i[t].x,i[t].y);e.stroke(),this.resetLineStyle(e),e.font=`bold 12px sans-serif`,e.fillStyle=t.style.color,e.textAlign=`center`;for(let t=0;t<i.length&&t<Ke.length;t++){let n=t%2==0?-12:12;e.textBaseline=n<0?`bottom`:`top`,e.fillText(Ke[t],i[t].x,i[t].y+n)}r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=t.anchors.map(e=>this.anchorToPixel(e,n));for(let t=0;t<i.length-1;t++)if(this.distanceToLine(e,i[t],i[t+1])<=r)return!0;return!1}},Je=class extends q{descriptor={type:`priceRange`,name:`Price Range`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.abs(a.x-i.x),c=Math.min(i.y,a.y),l=Math.abs(a.y-i.y),u=t.anchors[1].price>t.anchors[0].price;e.fillStyle=u?`rgba(38, 166, 154, 0.15)`:`rgba(239, 83, 80, 0.15)`,e.fillRect(o,c,s,l),this.applyLineStyle(e,t.style),e.strokeRect(o,c,s,l),this.resetLineStyle(e);let d=t.anchors[1].price-t.anchors[0].price,f=t.anchors[0].price===0?0:d/t.anchors[0].price*100,p=`${d>=0?`+`:``}${d.toFixed(2)} (${f>=0?`+`:``}${f.toFixed(2)}%)`;e.font=`12px sans-serif`,e.fillStyle=u?`#26A69A`:`#EF5350`,e.textAlign=`center`,e.textBaseline=`middle`,e.fillText(p,o+s/2,c+l/2),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.max(i.x,a.x),c=Math.min(i.y,a.y),l=Math.max(i.y,a.y);return e.x>=o-r&&e.x<=s+r&&e.y>=c-r&&e.y<=l+r}},Ye=class extends q{descriptor={type:`dateRange`,name:`Date Range`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),{chartRect:o}=n,s=Math.min(i.x,a.x),c=Math.max(i.x,a.x);e.fillStyle=`rgba(33, 150, 243, 0.08)`,e.fillRect(s,o.y,c-s,o.height),this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(s,o.y),e.lineTo(s,o.y+o.height),e.stroke(),e.beginPath(),e.moveTo(c,o.y),e.lineTo(c,o.y+o.height),e.stroke(),this.resetLineStyle(e);let l=Math.abs(t.anchors[1].time-t.anchors[0].time);e.font=`12px sans-serif`,e.fillStyle=t.style.color,e.textAlign=`center`,e.textBaseline=`top`,e.fillText(`${l} bars`,(s+c)/2,o.y+4),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),{chartRect:o}=n,s=Math.min(i.x,a.x),c=Math.max(i.x,a.x);return e.x>=s-r&&e.x<=c+r&&e.y>=o.y&&e.y<=o.y+o.height}},Xe=class extends q{descriptor={type:`measure`,name:`Measure`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.setLineDash([4,4]),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke(),this.resetLineStyle(e);let o=t.anchors[1].price-t.anchors[0].price,s=Math.abs(t.anchors[1].time-t.anchors[0].time),c=t.anchors[0].price===0?0:o/t.anchors[0].price*100,l=[`Price: ${o>=0?`+`:``}${o.toFixed(2)} (${c>=0?`+`:``}${c.toFixed(2)}%)`,`Bars: ${s}`],u=(i.x+a.x)/2,d=(i.y+a.y)/2;e.font=`11px sans-serif`,e.textAlign=`center`,e.textBaseline=`bottom`;let f=Math.max(...l.map(t=>e.measureText(t).width));e.fillStyle=`rgba(0, 0, 0, 0.7)`,e.fillRect(u-f/2-6,d-l.length*14-4,f+12,l.length*14+8),e.fillStyle=`#FFFFFF`;for(let t=0;t<l.length;t++)e.fillText(l[t],u,d-(l.length-1-t)*14);r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);return this.distanceToLine(e,i,a)<=r}},Ze=class extends q{descriptor={type:`text`,name:`Text`,requiredAnchors:1};render(e,t,n,r){if(t.anchors.length<1)return;let i=this.anchorToPixel(t.anchors[0],n),a=t.style.text||t.meta?.text||`Text`,o=t.style.fontSize||14;if(e.font=`${o}px sans-serif`,e.fillStyle=t.style.color,e.textAlign=`left`,e.textBaseline=`top`,r){let n=e.measureText(a);e.strokeStyle=t.style.color,e.lineWidth=1,e.setLineDash([3,3]),e.strokeRect(i.x-2,i.y-2,n.width+4,o+4),e.setLineDash([])}e.fillText(a,i.x,i.y),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<1)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=t.style.fontSize||14;return e.x>=i.x-r&&e.x<=i.x+80+r&&e.y>=i.y-r&&e.y<=i.y+a+r}},Qe=class extends q{descriptor={type:`arrow`,name:`Arrow`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(a.x,a.y),e.stroke();let o=Math.atan2(a.y-i.y,a.x-i.x);e.beginPath(),e.moveTo(a.x,a.y),e.lineTo(a.x-12*Math.cos(o-Math.PI/6),a.y-12*Math.sin(o-Math.PI/6)),e.lineTo(a.x-12*Math.cos(o+Math.PI/6),a.y-12*Math.sin(o+Math.PI/6)),e.closePath(),e.fillStyle=t.style.color,e.fill(),this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n);return this.distanceToLine(e,i,a)<=r}},$e=[{label:`8x1`,ratio:8},{label:`4x1`,ratio:4},{label:`3x1`,ratio:3},{label:`2x1`,ratio:2},{label:`1x1`,ratio:1},{label:`1x2`,ratio:.5},{label:`1x3`,ratio:1/3},{label:`1x4`,ratio:.25},{label:`1x8`,ratio:.125}],et=class extends q{descriptor={type:`gannFan`,name:`Gann Fan`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=a.x-i.x,s=a.y-i.y;if(Math.abs(o)<1)return;let c=s/o,l=o>0?1:-1,{chartRect:u}=n,d=l>0?u.x+u.width:u.x;this.applyLineStyle(e,t.style);for(let{label:n,ratio:r}of $e){let a=c*r,o=d,s=i.y+a*(o-i.x);e.globalAlpha=r===1?1:.5,e.lineWidth=r===1?t.style.lineWidth:Math.max(.5,t.style.lineWidth*.7),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(o,s),e.stroke();let f=o-l*4,p=s;p>u.y&&p<u.y+u.height&&(e.globalAlpha=.7,e.fillStyle=t.style.color,e.font=`9px sans-serif`,e.textAlign=l>0?`right`:`left`,e.textBaseline=`bottom`,e.fillText(n,f,p-2))}e.globalAlpha=1,this.resetLineStyle(e),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=a.x-i.x,s=a.y-i.y;if(Math.abs(o)<1)return!1;let c=s/o,l=o>0?1:-1,{chartRect:u}=n,d=l>0?u.x+u.width:u.x;for(let{ratio:t}of $e){let n=c*t,a=d,o=i.y+n*(a-i.x);if(this.distanceToLine(e,i,{x:a,y:o})<=r)return!0}return!1}},Y=[0,.25,.333,.5,.667,.75,1],tt=class extends q{descriptor={type:`gannBox`,name:`Gann Box`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.max(i.x,a.x),c=Math.min(i.y,a.y),l=Math.max(i.y,a.y),u=s-o,d=l-c;if(!(u<2||d<2)){this.applyLineStyle(e,t.style),e.strokeRect(o,c,u,d),e.save(),e.globalAlpha=.35;for(let t of Y){if(t===0||t===1)continue;let n=c+d*t;e.beginPath(),e.moveTo(o,n),e.lineTo(s,n),e.stroke()}for(let t of Y){if(t===0||t===1)continue;let n=o+u*t;e.beginPath(),e.moveTo(n,c),e.lineTo(n,l),e.stroke()}e.globalAlpha=.5,e.setLineDash([4,3]),e.beginPath(),e.moveTo(o,c),e.lineTo(s,l),e.stroke(),e.beginPath(),e.moveTo(s,c),e.lineTo(o,l),e.stroke(),e.restore(),this.resetLineStyle(e),e.fillStyle=t.style.color,e.font=`8px sans-serif`,e.globalAlpha=.6,e.textBaseline=`bottom`,e.textAlign=`right`;for(let t of Y){if(t===0||t===1)continue;let n=c+d*t;e.fillText(`${(t*100).toFixed(0)}%`,o-3,n)}e.globalAlpha=1,r&&this.renderAnchorHandles(e,t,n)}}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x),s=Math.max(i.x,a.x),c=Math.min(i.y,a.y),l=Math.max(i.y,a.y);return Math.abs(e.x-o)<=r&&e.y>=c-r&&e.y<=l+r||Math.abs(e.x-s)<=r&&e.y>=c-r&&e.y<=l+r||Math.abs(e.y-c)<=r&&e.x>=o-r&&e.x<=s+r||Math.abs(e.y-l)<=r&&e.x>=o-r&&e.x<=s+r||this.distanceToLine(e,{x:o,y:c},{x:s,y:l})<=r||this.distanceToLine(e,{x:s,y:c},{x:o,y:l})<=r}},nt=class extends q{descriptor={type:`anchoredVWAP`,name:`Anchored VWAP`,requiredAnchors:1};dataGetter=null;showBands=!0;setDataGetter(e){this.dataGetter=e}setShowBands(e){this.showBands=e}render(e,t,n,r){if(t.anchors.length<1||!this.dataGetter)return;let i=this.dataGetter(),s=Math.max(0,Math.round(t.anchors[0].time));if(s>=i.length)return;let{vwap:c,upper1:l,lower1:u,upper2:d,lower2:f}=this.computeVWAP(i,s),{chartRect:p}=n,m=n.barWidth+n.barSpacing;if(this.applyLineStyle(e,t.style),e.lineWidth=t.style.lineWidth+.5,this.drawLine(e,c,s,n,p,m),this.showBands&&c.length>1&&(e.globalAlpha=.4,e.lineWidth=Math.max(.5,t.style.lineWidth*.6),e.setLineDash([4,3]),this.drawLine(e,l,s,n,p,m),this.drawLine(e,u,s,n,p,m),e.globalAlpha=.25,this.drawLine(e,d,s,n,p,m),this.drawLine(e,f,s,n,p,m),e.globalAlpha=1),this.resetLineStyle(e),c.length>0){let r=c[c.length-1],i=a(s+c.length-1,n),l=o(r,n);i>=p.x&&i<=p.x+p.width&&(e.fillStyle=t.style.color,e.font=`bold 9px sans-serif`,e.textAlign=`left`,e.textBaseline=`middle`,e.fillText(`AVWAP ${r.toFixed(2)}`,i+6,l))}r&&this.renderAnchorHandles(e,t,n)}drawLine(e,t,n,r,i,s){e.beginPath();let c=!1;for(let l=0;l<t.length;l++){let u=a(n+l,r);if(u<i.x-s)continue;if(u>i.x+i.width+s)break;let d=o(t[l],r);c?e.lineTo(u,d):(e.moveTo(u,d),c=!0)}e.stroke()}computeVWAP(e,t){let n=e.length-t,r=Array(n),i=Array(n),a=Array(n),o=Array(n),s=Array(n),c=0,l=0,u=0;for(let d=0;d<n;d++){let n=e[t+d],f=(n.high+n.low+n.close)/3,p=n.volume??1;c+=f*p,l+=p,u+=f*f*p;let m=l>0?c/l:f;r[d]=m;let h=l>0?u/l-m*m:0,g=Math.sqrt(Math.max(0,h));i[d]=m+g,a[d]=m-g,o[d]=m+2*g,s[d]=m-2*g}return{vwap:r,upper1:i,lower1:a,upper2:o,lower2:s}}hitTest(e,t,n,r){if(t.anchors.length<1||!this.dataGetter)return!1;let i=this.dataGetter(),s=Math.max(0,Math.round(t.anchors[0].time));if(s>=i.length)return!1;let{vwap:c}=this.computeVWAP(i,s);for(let t=1;t<c.length;t++){let i=a(s+t-1,n),l=o(c[t-1],n),u=a(s+t,n),d=o(c[t],n);if(this.distanceToLine(e,{x:i,y:l},{x:u,y:d})<=r)return!0}return!1}},rt=class extends q{descriptor={type:`volumeProfileRange`,name:`Volume Profile (Range)`,requiredAnchors:2};dataGetter=null;numBins=40;setDataGetter(e){this.dataGetter=e}render(e,t,n,r){if(t.anchors.length<2||!this.dataGetter)return;let i=this.dataGetter(),s=Math.max(0,Math.min(Math.round(t.anchors[0].time),Math.round(t.anchors[1].time))),c=Math.min(i.length-1,Math.max(Math.round(t.anchors[0].time),Math.round(t.anchors[1].time)));if(s>=i.length||c<0||s>c)return;let l=1/0,u=-1/0;for(let e=s;e<=c;e++)i[e].low<l&&(l=i[e].low),i[e].high>u&&(u=i[e].high);if(l>=u)return;let d=(u-l)/this.numBins,f=new Float64Array(this.numBins),p=0;for(let e=s;e<=c;e++){let t=i[e],n=t.volume??1,r=Math.max(0,Math.floor((t.low-l)/d)),a=Math.min(this.numBins-1,Math.floor((t.high-l)/d)),o=n/(a-r+1);for(let e=r;e<=a;e++)f[e]+=o,f[e]>p&&(p=f[e])}if(p===0)return;let m=0;for(let e=1;e<this.numBins;e++)f[e]>f[m]&&(m=e);let h=a(c,n)+n.barWidth,g=t.style.color,_=t.style.fillColor??`#FF9800`;e.save();for(let t=0;t<this.numBins;t++){let r=f[t]/p*100,i=l+(t+1)*d,a=l+t*d,s=o(i,n),c=o(a,n),u=Math.abs(c-s);e.globalAlpha=.35,e.fillStyle=t===m?_:g,e.fillRect(h,Math.min(s,c),r,Math.max(1,u-.5))}let v=l+(m+.5)*d,y=o(v,n);e.globalAlpha=.7,e.strokeStyle=_,e.lineWidth=1,e.setLineDash([4,3]),e.beginPath();let b=a(s,n)-n.barWidth/2;e.moveTo(b,y),e.lineTo(h+100,y),e.stroke(),e.fillStyle=_,e.font=`9px sans-serif`,e.textAlign=`left`,e.textBaseline=`bottom`,e.globalAlpha=.8,e.fillText(`POC ${v.toFixed(2)}`,h+100+4,y-2),e.restore(),r&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,r){if(t.anchors.length<2)return!1;let i=this.anchorToPixel(t.anchors[0],n),a=this.anchorToPixel(t.anchors[1],n),o=Math.min(i.x,a.x)-r,s=Math.max(i.x,a.x)+100+r,c=Math.min(i.y,a.y)-r,l=Math.max(i.y,a.y)+r;return e.x>=o&&e.x<=s&&e.y>=c&&e.y<=l}};function it(e){e.register(new Me),e.register(new Ne),e.register(new Pe),e.register(new Fe),e.register(new Ie),e.register(new Le),e.register(new Re),e.register(new ze),e.register(new Ve),e.register(new He),e.register(new Ue),e.register(new We),e.register(new Ge),e.register(new qe),e.register(new Je),e.register(new Ye),e.register(new Xe),e.register(new Ze),e.register(new Qe),e.register(new et),e.register(new tt),e.register(new nt),e.register(new rt)}var at=class{render(e,t,n,r,i,a){let s=i.orderColors?.buy??`#26A69A`,c=i.orderColors?.sell??`#EF5350`,{chartRect:l}=n;for(let i of t){let t=a?.orderId===i.id&&a.sourceType===`order`,u=o(t?a.currentPrice:i.price,n);if(u<l.y||u>l.y+l.height)continue;let d=i.side===`buy`?s:c;e.setLineDash([6,4]),e.strokeStyle=d,e.lineWidth=1,e.beginPath(),e.moveTo(l.x,Math.round(u)+.5),e.lineTo(l.x+l.width,Math.round(u)+.5),e.stroke(),e.setLineDash([]);let f=i.label??i.type.toUpperCase();e.font=`bold 10px ${r.font.family}`;let p=e.measureText(f).width+8;e.fillStyle=d,e.fillRect(l.x+4,u-8,p,16),e.fillStyle=`#FFFFFF`,e.textBaseline=`middle`,e.textAlign=`left`,e.fillText(f,l.x+8,u);let m=l.x+4+p+6;if(e.fillStyle=d,e.beginPath(),i.side===`buy`?(e.moveTo(m,u+4),e.lineTo(m+5,u-2),e.lineTo(m+10,u+4)):(e.moveTo(m,u-4),e.lineTo(m+5,u+2),e.lineTo(m+10,u-4)),e.fill(),t){let t=o(a.startPrice,n);e.setLineDash([2,4]),e.strokeStyle=d,e.globalAlpha=.3,e.beginPath(),e.moveTo(l.x,Math.round(t)+.5),e.lineTo(l.x+l.width,Math.round(t)+.5),e.stroke(),e.globalAlpha=1,e.setLineDash([])}}}renderAxisBadges(t,n,r,i,a,s){let c=a.orderColors?.buy??`#26A69A`,l=a.orderColors?.sell??`#EF5350`,u=a.pricePrecision??2,{chartRect:d}=r,f=d.x+d.width+1;for(let a of n){let n=s?.orderId===a.id&&s.sourceType===`order`?s.currentPrice:a.price,p=o(n,r);if(p<d.y||p>d.y+d.height)continue;let m=a.side===`buy`?c:l,h=`${n.toFixed(u)} x ${a.quantity}`;t.font=`11px ${i.font.family}`;let g=t.measureText(h).width+10;t.fillStyle=m,t.fillRect(f,p-9,Math.min(g,e.PRICE_AXIS_WIDTH-2),18),t.fillStyle=`#FFFFFF`,t.textAlign=`left`,t.textBaseline=`middle`,t.fillText(h,f+5,p)}}hitTest(e,t,n,r){for(let i of t){if(i.draggable===!1)continue;let t=o(i.price,n);if(Math.abs(e-t)<=r)return i}return null}},ot=class{render(e,t,n,r,i,a){let{chartRect:s}=r,c=a.positionColors?.profit??`#26A69A`,l=a.positionColors?.loss??`#EF5350`,u=a.positionColors?.entry??`#2196F3`,d=a.pricePrecision??2;for(let a of t){let t=o(a.entryPrice,r);if(e.strokeStyle=u,e.lineWidth=1.5,e.setLineDash([]),e.beginPath(),e.moveTo(s.x,Math.round(t)+.5),e.lineTo(s.x+s.width,Math.round(t)+.5),e.stroke(),n!==null){let u=o(n,r),f=(n-a.entryPrice)*a.quantity*(a.side===`buy`?1:-1),p=f>=0;e.fillStyle=p?c:l,e.globalAlpha=.08;let m=Math.min(t,u),h=Math.abs(u-t);e.fillRect(s.x,m,s.width,h),e.globalAlpha=1;let g=`${a.side.toUpperCase()} ${a.quantity} | P&L: ${f>=0?`+`:``}${f.toFixed(d)}`;e.font=`bold 11px ${i.font.family}`;let _=e.measureText(g).width+12,v=s.x+8;e.fillStyle=p?c:l,e.globalAlpha=.9,e.fillRect(v,t-10,_,20),e.globalAlpha=1,e.fillStyle=`#FFFFFF`,e.textBaseline=`middle`,e.textAlign=`left`,e.fillText(g,v+6,t)}if(a.stopLoss!==void 0){let t=o(a.stopLoss,r);e.setLineDash([4,4]),e.strokeStyle=l,e.lineWidth=1,e.beginPath(),e.moveTo(s.x,Math.round(t)+.5),e.lineTo(s.x+s.width,Math.round(t)+.5),e.stroke(),e.font=`bold 10px ${i.font.family}`,e.fillStyle=l,e.fillRect(s.x+4,t-8,24,16),e.fillStyle=`#FFFFFF`,e.textAlign=`left`,e.textBaseline=`middle`,e.fillText(`SL`,s.x+8,t)}if(a.takeProfit!==void 0){let t=o(a.takeProfit,r);e.setLineDash([4,4]),e.strokeStyle=c,e.lineWidth=1,e.beginPath(),e.moveTo(s.x,Math.round(t)+.5),e.lineTo(s.x+s.width,Math.round(t)+.5),e.stroke(),e.font=`bold 10px ${i.font.family}`,e.fillStyle=c,e.fillRect(s.x+4,t-8,24,16),e.fillStyle=`#FFFFFF`,e.textAlign=`left`,e.textBaseline=`middle`,e.fillText(`TP`,s.x+8,t)}e.setLineDash([])}}renderAxisBadges(t,n,r,i,a){let{chartRect:s}=r,c=a.positionColors?.entry??`#2196F3`,l=a.pricePrecision??2,u=s.x+s.width+1;for(let a of n){let n=o(a.entryPrice,r);t.fillStyle=c,t.fillRect(u,n-9,e.PRICE_AXIS_WIDTH-2,18),t.fillStyle=`#FFFFFF`,t.font=`11px ${i.font.family}`,t.textAlign=`left`,t.textBaseline=`middle`,t.fillText(a.entryPrice.toFixed(l),u+5,n)}}},st=class{render(e,t,n,r){if(!r.depthOverlay?.enabled)return;let{chartRect:i}=n,a=r.depthOverlay.bidColor??`rgba(38,166,154,0.15)`,s=r.depthOverlay.askColor??`rgba(239,83,80,0.15)`,c=r.depthOverlay.maxWidth??100,l=0,u=[],d=0;for(let e of t.bids)d+=e.volume,u.push({price:e.price,cumVol:d}),d>l&&(l=d);let f=[];d=0;for(let e of t.asks)d+=e.volume,f.push({price:e.price,cumVol:d}),d>l&&(l=d);if(l===0)return;let p=Math.max(1,t.bids.length>1?Math.abs(o(t.bids[0].price,n)-o(t.bids[1].price,n)):4),m=i.x+i.width;e.fillStyle=a;for(let{price:t,cumVol:r}of u){let a=o(t,n);if(a<i.y||a>i.y+i.height)continue;let s=r/l*c;e.fillRect(m-s,a-p/2,s,p)}e.fillStyle=s;for(let{price:t,cumVol:r}of f){let a=o(t,n);if(a<i.y||a>i.y+i.height)continue;let s=r/l*c;e.fillRect(m-s,a-p/2,s,p)}}},ct=class{dragState=null;startY=0;dragging=!1;constructor(e=3){this.dragThreshold=e}onPointerDown(e,t,n,r,i){for(let n of t){if(n.draggable===!1)continue;let t=o(n.price,r);if(Math.abs(e.y-t)<=i)return this.dragState={orderId:n.id,sourceType:`order`,startPrice:n.price,currentPrice:n.price},this.startY=e.y,this.dragging=!1,!0}for(let t of n){if(t.stopLoss!==void 0){let n=o(t.stopLoss,r);if(Math.abs(e.y-n)<=i)return this.dragState={orderId:t.id,sourceType:`stopLoss`,startPrice:t.stopLoss,currentPrice:t.stopLoss},this.startY=e.y,this.dragging=!1,!0}if(t.takeProfit!==void 0){let n=o(t.takeProfit,r);if(Math.abs(e.y-n)<=i)return this.dragState={orderId:t.id,sourceType:`takeProfit`,startPrice:t.takeProfit,currentPrice:t.takeProfit},this.startY=e.y,this.dragging=!1,!0}}return!1}onPointerMove(e,t){if(!this.dragState)return!1;if(!this.dragging){if(Math.abs(e.y-this.startY)<this.dragThreshold)return!0;this.dragging=!0}return this.dragState.currentPrice=c(e.y,t),!0}onPointerUp(){if(!this.dragState)return null;let e=this.dragState;return this.dragState=null,this.dragging?(this.dragging=!1,{sourceType:e.sourceType,id:e.orderId,newPrice:e.currentPrice,previousPrice:e.startPrice}):null}getDragState(){return this.dragState}isActive(){return this.dragState!==null}},lt=class{menuElement=null;removeHandler=null;onItemSelect=null;show(e,t,n,r){if(this.hide(),!r.contextMenu?.enabled)return;let i=r.pricePrecision??2,a=t.toFixed(i),o=document.createElement(`div`);o.style.cssText=`
2
2
  position: absolute;
3
3
  left: ${e.x}px;
4
4
  top: ${e.y}px;
@@ -16,6 +16,6 @@ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require
16
16
  cursor: pointer;
17
17
  color: ${n?`#26A69A`:`#EF5350`};
18
18
  transition: background 0.1s;
19
- `,t.addEventListener(`mouseenter`,()=>{t.style.background=`#2a2e39`}),t.addEventListener(`mouseleave`,()=>{t.style.background=`transparent`}),t.addEventListener(`click`,()=>{this.onItemSelect?.(e.intent),this.hide()}),o.appendChild(t)}n.appendChild(o),this.menuElement=o;let c=e=>{(e instanceof KeyboardEvent&&e.key===`Escape`||e instanceof MouseEvent&&!o.contains(e.target))&&this.hide()};document.addEventListener(`mousedown`,c),document.addEventListener(`keydown`,c),this.removeHandler=()=>{document.removeEventListener(`mousedown`,c),document.removeEventListener(`keydown`,c)}}hide(){this.menuElement?.remove(),this.menuElement=null,this.removeHandler?.(),this.removeHandler=null}destroy(){this.hide(),this.onItemSelect=null}},ut=class{orders=[];positions=[];depthData=null;currentPrice=null;config;orderRenderer=new at;positionRenderer=new ot;depthOverlay=new st;dragHandler;contextMenu=new lt;requestRender=null;eventCallback=null;container=null;constructor(t){this.config={...e.DEFAULT_TRADING_CONFIG,...t},this.dragHandler=new ct(this.config.dragThreshold),this.contextMenu.onItemSelect=e=>{this.eventCallback?.(`orderPlace`,e)}}setContainer(e){this.container=e}setRequestRender(e){this.requestRender=e}setEventCallback(e){this.eventCallback=e}setOrders(e){this.orders=e,this.requestRender?.()}setPositions(e){this.positions=e,this.requestRender?.()}setDepthData(e){this.depthData=e,this.requestRender?.()}setCurrentPrice(e){this.currentPrice=e,this.requestRender?.()}setConfig(e){Object.assign(this.config,e),this.requestRender?.()}onPointerDown(e,t){return this.config.enabled?this.dragHandler.onPointerDown(e,this.orders,this.positions,t,8):!1}onPointerMove(e,t){if(!this.dragHandler.isActive())return!1;let n=this.dragHandler.onPointerMove(e,t);return n&&this.requestRender?.(),n}onPointerUp(){let e=this.dragHandler.onPointerUp();return e?(e.sourceType===`order`?this.eventCallback?.(`orderModify`,{orderId:e.id,newPrice:e.newPrice,previousPrice:e.previousPrice}):this.eventCallback?.(`positionModify`,{positionId:e.id,[e.sourceType]:e.newPrice}),this.requestRender?.(),!0):!1}onContextMenu(e,t){if(!this.config.enabled||!this.config.contextMenu?.enabled||!this.container)return!1;let n=c(e.y,t);return this.contextMenu.show(e,n,this.container,this.config),!0}render(e,t,n){this.config.enabled&&(this.depthData&&this.depthOverlay.render(e,this.depthData,t,this.config),this.positions.length>0&&this.positionRenderer.render(e,this.positions,this.currentPrice,t,n,this.config),this.orders.length>0&&this.orderRenderer.render(e,this.orders,t,n,this.config,this.dragHandler.getDragState()))}renderAxisBadges(e,t,n){this.config.enabled&&(this.positions.length>0&&this.positionRenderer.renderAxisBadges(e,this.positions,t,n,this.config),this.orders.length>0&&this.orderRenderer.renderAxisBadges(e,this.orders,t,n,this.config,this.dragHandler.getDragState()))}destroy(){this.contextMenu.destroy()}},dt=class{constructor(e){this.manager=e}render(e,t,n){this.manager.render(e,t,n)}renderAxisBadges(e,t,n){this.manager.renderAxisBadges(e,t,n)}},X=class{listeners=new Map;on(e,t){let n=this.listeners.get(e);return n||(n=new Set,this.listeners.set(e,n)),n.add(t),()=>n.delete(t)}once(e,t){let n=this.on(e,e=>{n(),t(e)});return n}off(e,t){this.listeners.get(e)?.delete(t)}emit(e,t){let n=this.listeners.get(e);if(n)for(let e of n)e(t)}removeAllListeners(){this.listeners.clear()}},ft=class extends X{currentBar=null;timeframeMs;tickRing;ringHead=0;ringCount=0;ringCapacity;constructor(t,n=1e3){super(),this.timeframeMs=(0,e.timeframeToMs)(t),this.ringCapacity=n,this.tickRing=Array(n)}setTimeframe(t){this.timeframeMs=(0,e.timeframeToMs)(t),this.currentBar=null,this.ringHead=0,this.ringCount=0}processTick(e){let t=(this.ringHead+this.ringCount)%this.ringCapacity;this.tickRing[t]=e,this.ringCount<this.ringCapacity?this.ringCount++:this.ringHead=(this.ringHead+1)%this.ringCapacity;let n=this.alignToTimeframe(e.time);!this.currentBar||this.currentBar.time!==n?(this.currentBar&&(this.currentBar.closed=!0,this.emit(`barClose`,this.currentBar)),this.currentBar={time:n,open:e.price,high:e.price,low:e.price,close:e.price,volume:e.volume,closed:!1,tickCount:1}):(this.currentBar.high=Math.max(this.currentBar.high,e.price),this.currentBar.low=Math.min(this.currentBar.low,e.price),this.currentBar.close=e.price,this.currentBar.volume+=e.volume,this.currentBar.tickCount++),this.emit(`bar`,this.currentBar)}processBar(e,t){if(t&&this.currentBar&&this.currentBar.time===e.time)this.currentBar={...e,closed:!0,tickCount:this.currentBar.tickCount+1},this.emit(`barClose`,this.currentBar),this.currentBar=null;else if(!t)this.currentBar={...e,closed:!1,tickCount:(this.currentBar?.tickCount??0)+1},this.emit(`bar`,this.currentBar);else{let t={...e,closed:!0,tickCount:1};this.emit(`barClose`,t),this.currentBar=null}}getCurrentBar(){return this.currentBar?{...this.currentBar}:null}getTickBuffer(){let e=Array(this.ringCount);for(let t=0;t<this.ringCount;t++)e[t]=this.tickRing[(this.ringHead+t)%this.ringCapacity];return e}reset(){this.currentBar=null,this.ringHead=0,this.ringCount=0}alignToTimeframe(e){return Math.floor(e/this.timeframeMs)*this.timeframeMs}},Z=class extends X{config;attempt=0;timer=null;active=!1;constructor(t){super(),this.config={...e.DEFAULT_RECONNECT,...t}}schedule(e){if(!this.config.enabled||!this.active)return;if(this.attempt++,this.attempt>this.config.maxRetries){this.emit(`giveUp`,{attempts:this.attempt-1}),this.active=!1;return}let t=this.computeDelay();this.emit(`attempt`,{attempt:this.attempt,delay:t}),this.timer=setTimeout(()=>{this.timer=null,this.active&&e()},t)}onConnected(){this.attempt=0,this.cancel(),this.emit(`success`,void 0)}start(){this.active=!0}stop(){this.active=!1,this.cancel(),this.attempt=0}cancel(){this.timer&&=(clearTimeout(this.timer),null)}getAttempt(){return this.attempt}isActive(){return this.active}computeDelay(){let e=this.config.baseDelay,t=this.config.backoffMultiplier**(this.attempt-1),n=Math.min(e*t,this.config.maxDelay),r=n*.25*(Math.random()*2-1);return Math.max(0,Math.round(n+r))}},Q=class{price=null;previousClose=null;visible=!0;flashUntil=0;pricePrecision=2;setPrice(e,t){this.price!==null&&e!==this.price&&(this.flashUntil=Date.now()+300),this.previousClose=t??this.price??e,this.price=e}setVisible(e){this.visible=e}setPricePrecision(e){this.pricePrecision=e}getPrice(){return this.price}render(t,n,r){if(!this.visible||this.price===null)return;let i=o(this.price,n),{chartRect:a}=n;if(i<a.y||i>a.y+a.height)return;let s=this.previousClose===null||this.price>=this.previousClose?r.candleUp??`#26A69A`:r.candleDown??`#EF5350`,c=Date.now()<this.flashUntil;t.setLineDash([4,3]),t.strokeStyle=s,t.lineWidth=1,t.globalAlpha=c?1:.7,t.beginPath(),t.moveTo(a.x,Math.round(i)+.5),t.lineTo(a.x+a.width,Math.round(i)+.5),t.stroke(),t.setLineDash([]),t.globalAlpha=1;let l=a.x+a.width+1,u=this.price.toFixed(this.pricePrecision);t.font=`bold 11px ${r.font.family}`;let d=t.measureText(u).width,f=Math.min(d+12,e.PRICE_AXIS_WIDTH-2);t.fillStyle=s,t.fillRect(l,i-10,f,20);let p=l-5;t.beginPath(),t.moveTo(p,i),t.lineTo(p+5,i-5),t.lineTo(p+5,i+5),t.closePath(),t.fill(),t.fillStyle=`#FFFFFF`,t.textBaseline=`middle`,t.textAlign=`left`,t.fillText(u,l+5,i)}},pt=class extends X{config=null;adapter=null;aggregator=null;reconnector;priceLine=new Q;state=`disconnected`;lastPrice=null;previousClose=null;unsubscribers=[];heartbeatTimer=null;lastMessageTime=0;constructor(){super(),this.reconnector=new Z,this.wireReconnector()}async connect(t){this.disconnect(),this.config={...e.DEFAULT_STREAM_CONFIG,...t},this.adapter=t.adapter,this.aggregator=new ft(t.timeframe),this.reconnector.removeAllListeners(),this.reconnector.stop(),this.reconnector=new Z(t.reconnect??e.DEFAULT_RECONNECT),this.wireReconnector(),this.aggregator.on(`bar`,e=>{this.emit(`barUpdate`,e),this.updatePrice(e.close)}),this.aggregator.on(`barClose`,e=>{this.previousClose=e.close,this.emit(`barClose`,e)}),this.wireAdapter(),this.reconnector.start(),await this.doConnect()}disconnect(){this.reconnector.stop(),this.stopHeartbeat();for(let e of this.unsubscribers)e();this.unsubscribers=[],this.adapter&&=(this.adapter.disconnect(),null),this.aggregator?.reset(),this.aggregator?.removeAllListeners(),this.aggregator=null,this.config=null,this.lastPrice=null,this.previousClose=null,this.setState(`disconnected`)}async switchTo(e,t){!this.config||!this.adapter||(this.adapter.disconnect(),this.aggregator?.reset(),this.aggregator?.setTimeframe(t),this.lastPrice=null,this.previousClose=null,this.config.symbol=e,this.config.timeframe=t,await this.doConnect())}getConnectionState(){return this.state}getConnectionInfo(){return{state:this.state,reconnectAttempt:this.reconnector.getAttempt(),lastMessageTime:this.lastMessageTime}}getLastPrice(){return this.lastPrice}async doConnect(){if(!(!this.config||!this.adapter)){this.setState(`connecting`);try{let e=await this.adapter.fetchHistory(this.config.symbol,this.config.timeframe,this.config.historyLimit??500);e.length>0&&(this.previousClose=e[e.length-1].close,this.updatePrice(e[e.length-1].close)),this.emit(`snapshot`,e),this.adapter.connect({symbol:this.config.symbol,timeframe:this.config.timeframe}),this.startHeartbeat()}catch(e){let t=e instanceof Error?e.message:String(e);this.emit(`error`,{message:t,code:`CONNECT_FAILED`}),this.setState(`error`),this.reconnector.schedule(()=>this.doConnect())}}}wireReconnector(){this.reconnector.on(`attempt`,({attempt:e,delay:t})=>{this.setState(`reconnecting`),this.emit(`connectionChange`,{state:`reconnecting`,reconnectAttempt:e,lastMessageTime:this.lastMessageTime})}),this.reconnector.on(`giveUp`,({attempts:e})=>{this.setState(`error`),this.emit(`error`,{message:`Reconnection failed after ${e} attempts`,code:`RECONNECT_EXHAUSTED`})})}wireAdapter(){this.adapter&&(this.adapter.on(`connectionChange`,(e=>{let t=e.data;this.lastMessageTime=Date.now(),t===`connected`?(this.reconnector.onConnected(),this.setState(`connected`)):(t===`disconnected`||t===`error`)&&this.reconnector.schedule(()=>this.doConnect())})),this.adapter.on(`tick`,(e=>{this.lastMessageTime=Date.now();let t=e.data;this.emit(`tick`,t),this.config?.aggregateTicks&&this.aggregator?this.aggregator.processTick(t):this.updatePrice(t.price)})),this.adapter.on(`bar`,(e=>{this.lastMessageTime=Date.now();let{bar:t,closed:n}=e.data;this.aggregator&&this.aggregator.processBar(t,n)})),this.adapter.on(`error`,(e=>{this.emit(`error`,{message:e.data?.message??`Adapter error`})})))}updatePrice(e){e!==this.lastPrice&&(this.lastPrice=e,this.priceLine.setPrice(e,this.previousClose??void 0),this.emit(`priceChange`,{price:e,previousClose:this.previousClose}))}setState(e){this.state!==e&&(this.state=e,this.emit(`connectionChange`,{state:e,reconnectAttempt:this.reconnector.getAttempt(),lastMessageTime:this.lastMessageTime}))}startHeartbeat(){this.stopHeartbeat(),this.heartbeatTimer=setInterval(()=>{Date.now()-this.lastMessageTime>6e4&&this.state===`connected`&&(this.emit(`error`,{message:`Heartbeat timeout`,code:`HEARTBEAT_TIMEOUT`}),this.adapter?.disconnect(),this.reconnector.schedule(()=>this.doConnect()))},3e4)}stopHeartbeat(){this.heartbeatTimer&&=(clearInterval(this.heartbeatTimer),null)}dispose(){this.disconnect(),this.removeAllListeners()}},mt={"1s":`1s`,"1m":`1m`,"3m":`3m`,"5m":`5m`,"15m":`15m`,"30m":`30m`,"1h":`1h`,"2h":`2h`,"4h":`4h`,"6h":`6h`,"8h":`8h`,"12h":`12h`,"1d":`1d`,"3d":`3d`,"1w":`1w`,"1M":`1M`},ht=class{name=`binance`;ws=null;state=`disconnected`;listeners=new Map;config=null;restBase;wsBase;constructor(e){this.restBase=e?.restBase??`https://api.binance.com/api/v3`,this.wsBase=e?.wsBase??`wss://stream.binance.com:9443/ws`}connect(e){this.config=e,this.connectWs()}disconnect(){this.ws&&=(this.ws.onclose=null,this.ws.close(),null),this.setState(`disconnected`)}getConnectionState(){return this.state}async fetchHistory(e,t,n=500){let r=mt[t]??`15m`,i=`${this.restBase}/klines?symbol=${e}&interval=${r}&limit=${n}`,a=await fetch(i);if(!a.ok)throw Error(`Binance REST error: ${a.status}`);return(await a.json()).map(e=>({time:e[0],open:parseFloat(e[1]),high:parseFloat(e[2]),low:parseFloat(e[3]),close:parseFloat(e[4]),volume:parseFloat(e[5])}))}on(e,t){let n=this.listeners.get(e);n||(n=new Set,this.listeners.set(e,n)),n.add(t)}off(e,t){this.listeners.get(e)?.delete(t)}dispose(){this.disconnect(),this.listeners.clear()}connectWs(){if(!this.config)return;let e=`${this.config.symbol.toLowerCase()}@kline_${mt[this.config.timeframe]??`15m`}`,t=`${this.wsBase}/${e}`;this.setState(`connecting`);try{this.ws=new WebSocket(t)}catch{this.setState(`error`),this.emitEvent(`error`,{message:`WebSocket creation failed`}),this.emitEvent(`connectionChange`,`error`);return}this.ws.onopen=()=>{this.setState(`connected`),this.emitEvent(`connectionChange`,`connected`)},this.ws.onmessage=e=>{try{let t=JSON.parse(e.data);t.e===`kline`&&this.handleKline(t.k)}catch{}},this.ws.onerror=()=>{this.setState(`error`),this.emitEvent(`error`,{message:`WebSocket error`})},this.ws.onclose=()=>{this.setState(`disconnected`),this.emitEvent(`connectionChange`,`disconnected`)}}handleKline(e){let t={time:e.t,open:parseFloat(e.o),high:parseFloat(e.h),low:parseFloat(e.l),close:parseFloat(e.c),volume:parseFloat(e.v)},n=e.x;this.emitEvent(`bar`,{bar:t,closed:n}),this.emitEvent(`tick`,{time:Date.now(),price:t.close,volume:t.volume})}setState(e){this.state=e}emitEvent(e,t){let n=this.listeners.get(e);if(n){let r={type:e,data:t,timestamp:Date.now()};for(let e of n)e(r)}}},gt=class{name=`mock`;state=`disconnected`;listeners=new Map;config=null;options;tickTimer=null;currentPrice;constructor(e){this.options={basePrice:100,volatility:1.5,tickInterval:2e3,historySize:500,...e},this.currentPrice=this.options.basePrice}connect(e){this.config=e,this.state=`connected`,this.emitEvent(`connectionChange`,`connected`),this.startTicking()}disconnect(){this.stopTicking(),this.state=`disconnected`,this.emitEvent(`connectionChange`,`disconnected`)}getConnectionState(){return this.state}async fetchHistory(e,t,n){return this.options.historyGenerator?this.options.historyGenerator(e,t,n??this.options.historySize):this.generateHistory(t,n??this.options.historySize)}on(e,t){let n=this.listeners.get(e);n||(n=new Set,this.listeners.set(e,n)),n.add(t)}off(e,t){this.listeners.get(e)?.delete(t)}dispose(){this.disconnect(),this.listeners.clear()}generateHistory(t,n){let r=[],i=Date.now(),a=(0,e.timeframeToMs)(t),o=this.options.basePrice,s=this.options.volatility/100;for(let e=n-1;e>=0;e--){let t=i-e*a,n=(Math.random()-.48)*o*s,c=o;o+=n;let l=Math.max(c,o)+Math.random()*o*s*.3,u=Math.min(c,o)-Math.random()*o*s*.3,d=Math.floor(1e4+Math.random()*1e5);r.push({time:t,open:c,high:l,low:u,close:o,volume:d})}return this.currentPrice=o,r}startTicking(){this.stopTicking();let t=this.options.tickInterval??2e3;this.tickTimer=setInterval(()=>{if(this.state!==`connected`||!this.config)return;let t=this.options.volatility/100,n=(Math.random()-.48)*this.currentPrice*t*.3;this.currentPrice+=n;let r=Date.now(),i=(0,e.timeframeToMs)(this.config.timeframe),a={time:Math.floor(r/i)*i,open:this.currentPrice-n,high:Math.max(this.currentPrice,this.currentPrice-n)+Math.abs(n)*.2,low:Math.min(this.currentPrice,this.currentPrice-n)-Math.abs(n)*.2,close:this.currentPrice,volume:Math.floor(100+Math.random()*5e3)};this.emitEvent(`bar`,{bar:a,closed:!1}),this.emitEvent(`tick`,{time:r,price:this.currentPrice,volume:a.volume})},t)}stopTicking(){this.tickTimer&&=(clearInterval(this.tickTimer),null)}emitEvent(e,t){let n=this.listeners.get(e);if(n){let r={type:e,data:t,timestamp:Date.now()};for(let e of n)e(r)}}},_t={visible:!0,showSymbol:!0,showOHLC:!0,showChange:!0,showVolume:!0,position:`top-left`,fontSize:12},vt=class{config={..._t};symbol=``;timeframe=``;hoverBar=null;indicators=[];setConfig(e){Object.assign(this.config,e)}setSymbol(e){this.symbol=e}setTimeframe(e){this.timeframe=e}setChartType(e){}setHoverBar(e){this.hoverBar=e}setIndicatorValues(e){this.indicators=e}render(e,t,n,r){if(!this.config.visible||r.length===0)return;let i=this.hoverBar??r[r.length-1],a=r.length>1?r[r.length-2]:i,{chartRect:o}=t,s=this.config.fontSize,c=this.config.position===`top-left`,l=c?o.x+8:o.x+o.width-8,u=o.y+6;if(e.textAlign=c?`left`:`right`,e.textBaseline=`top`,this.config.showSymbol){e.font=`bold ${s+2}px ${n.font.family}`,e.fillStyle=n.text;let t=[this.symbol,this.timeframe].filter(Boolean).join(` ยท `);e.fillText(t,l,u),u+=s+6}if(this.config.showOHLC){e.font=`${s}px ${n.font.family}`;let t=i.close>=i.open,r=e=>e.toLocaleString(void 0,{minimumFractionDigits:2,maximumFractionDigits:2}),o=[{label:`O`,value:r(i.open),color:n.text},{label:`H`,value:r(i.high),color:n.text},{label:`L`,value:r(i.low),color:n.text},{label:`C`,value:r(i.close),color:t?n.candleUp:n.candleDown}];if(this.config.showChange){let e=i.close-a.close,t=a.close===0?0:e/a.close*100,s=e>=0?`+`:``;o.push({label:``,value:`${s}${r(e)} (${s}${t.toFixed(2)}%)`,color:e>=0?n.candleUp:n.candleDown})}let d=l;if(c)for(let t of o)t.label&&(e.fillStyle=n.textSecondary,e.fillText(t.label,d,u),d+=e.measureText(t.label+` `).width),e.fillStyle=t.color,e.fillText(t.value,d,u),d+=e.measureText(t.value+` `).width;else{let t=o.map(e=>`${e.label} ${e.value}`).join(` `);e.fillStyle=n.text,e.fillText(t,l,u)}u+=s+3}if(this.config.showVolume){e.font=`${s-1}px ${n.font.family}`,e.fillStyle=n.textSecondary;let t=i.volume>=1e6?`${(i.volume/1e6).toFixed(2)}M`:i.volume>=1e3?`${(i.volume/1e3).toFixed(2)}K`:i.volume.toFixed(0);e.fillText(`Vol ${t}`,l,u),u+=s+2}if(this.indicators.length>0){e.font=`${s-1}px ${n.font.family}`;let t=l;for(let n of this.indicators){e.fillStyle=n.color;let r=`${n.name} ${n.value}`;e.fillText(r,t,u),t+=e.measureText(r+` `).width}}}},yt=class{static toDataURL(e,t){let n=e.querySelectorAll(`canvas`);if(n.length===0)return``;let r=n[0],i=r.width,a=r.height,o=document.createElement(`canvas`);o.width=i,o.height=a;let s=o.getContext(`2d`);t&&(s.fillStyle=t,s.fillRect(0,0,i,a));let c=Array.from(n).sort((e,t)=>(parseInt(e.style.zIndex)||0)-(parseInt(t.style.zIndex)||0));for(let e of c)s.drawImage(e,0,0);return o.toDataURL(`image/png`)}static async toBlob(e,t){let n=this.toDataURL(e,t);return n?(await fetch(n)).blob():null}static download(e,t=`chart.png`,n){let r=this.toDataURL(e,n);if(!r)return;let i=document.createElement(`a`);i.href=r,i.download=t,document.body.appendChild(i),i.click(),document.body.removeChild(i)}},bt=class{config=null;setConfig(e){this.config=e}render(e,t,n){if(!this.config?.text)return;let{chartRect:r}=t,i=this.config.fontSize??48,a=this.config.color??n.textSecondary;e.save(),e.globalAlpha=.07,e.font=`bold ${i}px ${n.font.family}`,e.fillStyle=a,e.textAlign=`center`,e.textBaseline=`middle`,e.fillText(this.config.text,r.x+r.width/2,r.y+r.height/2),e.restore()}},xt=class{visible=!0;timeframeMs=0;setVisible(e){this.visible=e}isVisible(){return this.visible}setTimeframeMs(e){this.timeframeMs=e}render(e,t,n,r){if(!this.visible||this.timeframeMs<=0||r.length===0)return;let i=r[r.length-1],o=(i.time>0xe8d4a51000?i.time:i.time*1e3)+this.timeframeMs-Date.now();if(o<=0)return;let s=this.formatRemaining(o),c=a(r.length-1,t),l=t.chartRect.y+t.chartRect.height-4;c<t.chartRect.x||c>t.chartRect.x+t.chartRect.width||(e.font=`${n.font.sizeSmall}px ${n.font.family}`,e.textBaseline=`bottom`,e.textAlign=`center`,e.fillStyle=n.textSecondary,e.fillText(s,c,l))}formatRemaining(e){let t=Math.ceil(e/1e3),n=Math.floor(t/3600),r=Math.floor(t%3600/60),i=t%60;return n>0?`${n}:${String(r).padStart(2,`0`)}:${String(i).padStart(2,`0`)}`:`${r}:${String(i).padStart(2,`0`)}`}},St=class{config={visible:!1};cachedBreaks=[];lastDataLength=0;setConfig(e){Object.assign(this.config,e)}isVisible(){return this.config.visible}setVisible(e){this.config.visible=e}computeBreaks(e){if(e.length<2)return[];if(e.length===this.lastDataLength&&this.cachedBreaks.length>0)return this.cachedBreaks;let t=[],n=this.getDayKey(e[0].time);for(let r=1;r<e.length;r++){let i=this.getDayKey(e[r].time);i!==n&&(t.push(r),n=i)}return this.lastDataLength=e.length,this.cachedBreaks=t,t}getDayKey(e){let t=e>0xe8d4a51000?e:e*1e3,n=new Date(t);return`${n.getFullYear()}-${n.getMonth()}-${n.getDate()}`}render(e,t,n,r){if(!this.config.visible||r.length<2)return;let i=this.computeBreaks(r);if(i.length===0)return;let{chartRect:o}=t,s=this.config.color??n.axisLine,c=this.config.lineWidth??1,l=this.config.lineStyle??`dashed`;e.save(),e.strokeStyle=s,e.lineWidth=c,e.globalAlpha=.4,l===`dashed`?e.setLineDash([6,4]):l===`dotted`&&e.setLineDash([2,3]),e.beginPath();for(let n of i){let r=a(n,t)-(t.barWidth+t.barSpacing)/2;if(r<o.x-1||r>o.x+o.width+1)continue;let i=Math.round(r)+.5;e.moveTo(i,o.y),e.lineTo(i,o.y+o.height)}e.stroke(),e.restore()}invalidateCache(){this.lastDataLength=0,this.cachedBreaks=[]}},Ct=1,wt=class extends X{alerts=[];lastPrice=null;requestRender=null;pricePrecision=2;setRequestRender(e){this.requestRender=e}setPricePrecision(e){this.pricePrecision=e}addAlert(e,t=`crossing`,n,r=!1){let i=`alert_${Ct++}`,a={id:i,price:e,condition:t,message:n,triggered:!1,repeating:r};return this.alerts.push(a),this.emit(`added`,a),this.requestRender?.(),i}removeAlert(e){this.alerts=this.alerts.filter(t=>t.id!==e),this.emit(`removed`,e),this.requestRender?.()}getAlerts(){return[...this.alerts]}clearAlerts(){this.alerts=[],this.requestRender?.()}checkPrice(e){if(this.lastPrice===null){this.lastPrice=e;return}for(let t of this.alerts){if(t.triggered&&!t.repeating)continue;let n=!1;switch(t.condition){case`crossingUp`:n=this.lastPrice<t.price&&e>=t.price;break;case`crossingDown`:n=this.lastPrice>t.price&&e<=t.price;break;case`crossing`:n=this.lastPrice<t.price&&e>=t.price||this.lastPrice>t.price&&e<=t.price;break;case`greaterThan`:n=e>t.price;break;case`lessThan`:n=e<t.price;break}n&&(t.triggered=!0,this.emit(`triggered`,{...t}))}this.lastPrice=e}saveToStorage(e){try{let t=this.alerts.map(e=>({id:e.id,price:e.price,condition:e.condition,message:e.message,triggered:e.triggered}));localStorage.setItem(e,JSON.stringify(t))}catch{}}loadFromStorage(e){try{let t=localStorage.getItem(e);if(!t)return;let n=JSON.parse(t);if(!Array.isArray(n))return;for(let e of n)e.price&&e.condition&&!e.triggered&&this.addAlert(e.price,e.condition,e.message)}catch{}}render(e,t,n){let{chartRect:r}=t;for(let i of this.alerts){let a=o(i.price,t);if(a<r.y||a>r.y+r.height)continue;let s=i.triggered?`#FF9800`:`#FFD700`;e.setLineDash([2,6]),e.strokeStyle=s,e.lineWidth=1,e.beginPath(),e.moveTo(r.x,Math.round(a)+.5),e.lineTo(r.x+r.width,Math.round(a)+.5),e.stroke(),e.setLineDash([]),e.font=`10px ${n.font.family}`,e.fillStyle=s,e.textBaseline=`bottom`,e.textAlign=`right`;let c=`๐Ÿ”” ${i.price.toFixed(this.pricePrecision)}${i.message?` - ${i.message}`:``}`;e.fillText(c,r.x+r.width-4,a-2)}}},Tt=class extends X{data=[];currentIndex=0;timer=null;state=`stopped`;config={speed:1,interval:500};load(e){this.stop(),this.data=e,this.currentIndex=0}play(e){this.data.length!==0&&(e&&Object.assign(this.config,e),e?.startIndex!==void 0&&(this.currentIndex=e.startIndex),this.state=`playing`,this.emit(`stateChange`,`playing`),this.timer=setInterval(()=>{if(this.currentIndex>=this.data.length){this.stop(),this.emit(`complete`,void 0);return}let e=this.data[this.currentIndex];this.emit(`bar`,{bar:e,index:this.currentIndex,total:this.data.length}),this.currentIndex++},this.config.interval/this.config.speed))}pause(){this.timer&&=(clearInterval(this.timer),null),this.state=`paused`,this.emit(`stateChange`,`paused`)}resume(){this.state===`paused`&&this.play()}stop(){this.timer&&=(clearInterval(this.timer),null),this.state=`stopped`,this.currentIndex=0,this.emit(`stateChange`,`stopped`)}seekTo(e){this.currentIndex=Math.max(0,Math.min(e,this.data.length-1))}setSpeed(e){this.config.speed=e,this.state===`playing`&&(this.pause(),this.play())}getState(){return this.state}getProgress(){return{current:this.currentIndex,total:this.data.length,percent:this.data.length>0?this.currentIndex/this.data.length*100:0}}dispose(){this.stop(),this.removeAllListeners(),this.data=[]}},$=1,Et=class{static capture(e,t){return{version:$,timestamp:Date.now(),chartType:t?.chartType??`candlestick`,symbol:t?.symbol,timeframe:t?.timeframe,viewport:{barWidth:8,barSpacing:2,offset:0},theme:e.getTheme().name,indicators:e.getIndicators?.()??[],drawings:e.getDrawings(),orders:e.getOrders?.()??[],positions:e.getPositions?.()??[],alerts:e.getAlerts?.()??[]}}static serialize(e){return JSON.stringify(e)}static deserialize(e){let t=JSON.parse(e);return t.version!==$&&console.warn(`Chart state version mismatch: ${t.version} vs ${$}`),t}static saveToStorage(e,t){localStorage.setItem(e,this.serialize(t))}static loadFromStorage(e){let t=localStorage.getItem(e);return t?this.deserialize(t):null}static downloadFile(e,t=`chart-state.json`){let n=new Blob([this.serialize(e)],{type:`application/json`}),r=URL.createObjectURL(n),i=document.createElement(`a`);i.href=r,i.download=t,document.body.appendChild(i),i.click(),document.body.removeChild(i),URL.revokeObjectURL(r)}static loadFromFile(){return new Promise((e,t)=>{let n=document.createElement(`input`);n.type=`file`,n.accept=`.json`,n.onchange=async()=>{let r=n.files?.[0];if(!r){t(Error(`No file selected`));return}let i=await r.text();e(this.deserialize(i))},n.click()})}},Dt=class{undoStack=[];redoStack=[];maxHistory=50;onChange=null;setOnChange(e){this.onChange=e}push(e){this.undoStack.push(e),this.undoStack.length>this.maxHistory&&this.undoStack.shift(),this.redoStack.length=0,this.onChange?.()}canUndo(){return this.undoStack.length>0}canRedo(){return this.redoStack.length>0}undo(){let e=this.undoStack.pop();return e?(this.redoStack.push(e),this.onChange?.(),e):null}redo(){let e=this.redoStack.pop();return e?(this.undoStack.push(e),this.onChange?.(),e):null}clear(){this.undoStack.length=0,this.redoStack.length=0}getState(){return{canUndo:this.undoStack.length>0,canRedo:this.redoStack.length>0,undoCount:this.undoStack.length,redoCount:this.redoStack.length}}},Ot=class{static toCSV(e){return[`Time,Open,High,Low,Close,Volume`,...e.map(e=>`${new Date(e.time).toISOString()},${e.open},${e.high},${e.low},${e.close},${e.volume}`)].join(`
19
+ `,t.addEventListener(`mouseenter`,()=>{t.style.background=`#2a2e39`}),t.addEventListener(`mouseleave`,()=>{t.style.background=`transparent`}),t.addEventListener(`click`,()=>{this.onItemSelect?.(e.intent),this.hide()}),o.appendChild(t)}n.appendChild(o),this.menuElement=o;let c=e=>{(e instanceof KeyboardEvent&&e.key===`Escape`||e instanceof MouseEvent&&!o.contains(e.target))&&this.hide()};document.addEventListener(`mousedown`,c),document.addEventListener(`keydown`,c),this.removeHandler=()=>{document.removeEventListener(`mousedown`,c),document.removeEventListener(`keydown`,c)}}hide(){this.menuElement?.remove(),this.menuElement=null,this.removeHandler?.(),this.removeHandler=null}destroy(){this.hide(),this.onItemSelect=null}},ut=class{orders=[];positions=[];depthData=null;currentPrice=null;config;orderRenderer=new at;positionRenderer=new ot;depthOverlay=new st;dragHandler;contextMenu=new lt;requestRender=null;eventCallback=null;container=null;constructor(t){this.config={...e.DEFAULT_TRADING_CONFIG,...t},this.dragHandler=new ct(this.config.dragThreshold),this.contextMenu.onItemSelect=e=>{this.eventCallback?.(`orderPlace`,e)}}setContainer(e){this.container=e}setRequestRender(e){this.requestRender=e}setEventCallback(e){this.eventCallback=e}setOrders(e){this.orders=e,this.requestRender?.()}setPositions(e){this.positions=e,this.requestRender?.()}setDepthData(e){this.depthData=e,this.requestRender?.()}setCurrentPrice(e){this.currentPrice=e,this.requestRender?.()}setConfig(e){Object.assign(this.config,e),this.requestRender?.()}onPointerDown(e,t){return this.config.enabled?this.dragHandler.onPointerDown(e,this.orders,this.positions,t,8):!1}onPointerMove(e,t){if(!this.dragHandler.isActive())return!1;let n=this.dragHandler.onPointerMove(e,t);return n&&this.requestRender?.(),n}onPointerUp(){let e=this.dragHandler.onPointerUp();return e?(e.sourceType===`order`?this.eventCallback?.(`orderModify`,{orderId:e.id,newPrice:e.newPrice,previousPrice:e.previousPrice}):this.eventCallback?.(`positionModify`,{positionId:e.id,[e.sourceType]:e.newPrice}),this.requestRender?.(),!0):!1}onContextMenu(e,t){if(!this.config.enabled||!this.config.contextMenu?.enabled||!this.container)return!1;let n=c(e.y,t);return this.contextMenu.show(e,n,this.container,this.config),!0}render(e,t,n){this.config.enabled&&(this.depthData&&this.depthOverlay.render(e,this.depthData,t,this.config),this.positions.length>0&&this.positionRenderer.render(e,this.positions,this.currentPrice,t,n,this.config),this.orders.length>0&&this.orderRenderer.render(e,this.orders,t,n,this.config,this.dragHandler.getDragState()))}renderAxisBadges(e,t,n){this.config.enabled&&(this.positions.length>0&&this.positionRenderer.renderAxisBadges(e,this.positions,t,n,this.config),this.orders.length>0&&this.orderRenderer.renderAxisBadges(e,this.orders,t,n,this.config,this.dragHandler.getDragState()))}destroy(){this.contextMenu.destroy()}},dt=class{constructor(e){this.manager=e}render(e,t,n){this.manager.render(e,t,n)}renderAxisBadges(e,t,n){this.manager.renderAxisBadges(e,t,n)}},X=class{listeners=new Map;on(e,t){let n=this.listeners.get(e);return n||(n=new Set,this.listeners.set(e,n)),n.add(t),()=>n.delete(t)}once(e,t){let n=this.on(e,e=>{n(),t(e)});return n}off(e,t){this.listeners.get(e)?.delete(t)}emit(e,t){let n=this.listeners.get(e);if(n)for(let e of n)e(t)}removeAllListeners(){this.listeners.clear()}},ft=class extends X{currentBar=null;timeframeMs;tickRing;ringHead=0;ringCount=0;ringCapacity;constructor(t,n=1e3){super(),this.timeframeMs=(0,e.timeframeToMs)(t),this.ringCapacity=n,this.tickRing=Array(n)}setTimeframe(t){this.timeframeMs=(0,e.timeframeToMs)(t),this.currentBar=null,this.ringHead=0,this.ringCount=0}processTick(e){let t=(this.ringHead+this.ringCount)%this.ringCapacity;this.tickRing[t]=e,this.ringCount<this.ringCapacity?this.ringCount++:this.ringHead=(this.ringHead+1)%this.ringCapacity;let n=this.alignToTimeframe(e.time);!this.currentBar||this.currentBar.time!==n?(this.currentBar&&(this.currentBar.closed=!0,this.emit(`barClose`,this.currentBar)),this.currentBar={time:n,open:e.price,high:e.price,low:e.price,close:e.price,volume:e.volume,closed:!1,tickCount:1}):(this.currentBar.high=Math.max(this.currentBar.high,e.price),this.currentBar.low=Math.min(this.currentBar.low,e.price),this.currentBar.close=e.price,this.currentBar.volume+=e.volume,this.currentBar.tickCount++),this.emit(`bar`,this.currentBar)}processBar(e,t){if(t&&this.currentBar&&this.currentBar.time===e.time)this.currentBar={...e,closed:!0,tickCount:this.currentBar.tickCount+1},this.emit(`barClose`,this.currentBar),this.currentBar=null;else if(!t)this.currentBar={...e,closed:!1,tickCount:(this.currentBar?.tickCount??0)+1},this.emit(`bar`,this.currentBar);else{let t={...e,closed:!0,tickCount:1};this.emit(`barClose`,t),this.currentBar=null}}getCurrentBar(){return this.currentBar?{...this.currentBar}:null}getTickBuffer(){let e=Array(this.ringCount);for(let t=0;t<this.ringCount;t++)e[t]=this.tickRing[(this.ringHead+t)%this.ringCapacity];return e}reset(){this.currentBar=null,this.ringHead=0,this.ringCount=0}alignToTimeframe(e){return Math.floor(e/this.timeframeMs)*this.timeframeMs}},Z=class extends X{config;attempt=0;timer=null;active=!1;constructor(t){super(),this.config={...e.DEFAULT_RECONNECT,...t}}schedule(e){if(!this.config.enabled||!this.active)return;if(this.attempt++,this.attempt>this.config.maxRetries){this.emit(`giveUp`,{attempts:this.attempt-1}),this.active=!1;return}let t=this.computeDelay();this.emit(`attempt`,{attempt:this.attempt,delay:t}),this.timer=setTimeout(()=>{this.timer=null,this.active&&e()},t)}onConnected(){this.attempt=0,this.cancel(),this.emit(`success`,void 0)}start(){this.active=!0}stop(){this.active=!1,this.cancel(),this.attempt=0}cancel(){this.timer&&=(clearTimeout(this.timer),null)}getAttempt(){return this.attempt}isActive(){return this.active}computeDelay(){let e=this.config.baseDelay,t=this.config.backoffMultiplier**(this.attempt-1),n=Math.min(e*t,this.config.maxDelay),r=n*.25*(Math.random()*2-1);return Math.max(0,Math.round(n+r))}},Q=class{price=null;previousClose=null;visible=!0;flashUntil=0;pricePrecision=2;setPrice(e,t){this.price!==null&&e!==this.price&&(this.flashUntil=Date.now()+300),this.previousClose=t??this.price??e,this.price=e}setVisible(e){this.visible=e}setPricePrecision(e){this.pricePrecision=e}getPrice(){return this.price}render(t,n,r){if(!this.visible||this.price===null)return;let i=o(this.price,n),{chartRect:a}=n;if(i<a.y||i>a.y+a.height)return;let s=this.previousClose===null||this.price>=this.previousClose?r.candleUp??`#26A69A`:r.candleDown??`#EF5350`,c=Date.now()<this.flashUntil;t.setLineDash([4,3]),t.strokeStyle=s,t.lineWidth=1,t.globalAlpha=c?1:.7,t.beginPath(),t.moveTo(a.x,Math.round(i)+.5),t.lineTo(a.x+a.width,Math.round(i)+.5),t.stroke(),t.setLineDash([]),t.globalAlpha=1;let l=a.x+a.width+1,u=this.price.toFixed(this.pricePrecision);t.font=`bold 11px ${r.font.family}`;let d=t.measureText(u).width,f=Math.min(d+12,e.PRICE_AXIS_WIDTH-2);t.fillStyle=s,t.fillRect(l,i-10,f,20);let p=l-5;t.beginPath(),t.moveTo(p,i),t.lineTo(p+5,i-5),t.lineTo(p+5,i+5),t.closePath(),t.fill(),t.fillStyle=`#FFFFFF`,t.textBaseline=`middle`,t.textAlign=`left`,t.fillText(u,l+5,i)}},pt=class extends X{config=null;adapter=null;aggregator=null;reconnector;priceLine=new Q;state=`disconnected`;lastPrice=null;previousClose=null;unsubscribers=[];heartbeatTimer=null;lastMessageTime=0;constructor(){super(),this.reconnector=new Z,this.wireReconnector()}async connect(t){this.disconnect(),this.config={...e.DEFAULT_STREAM_CONFIG,...t},this.adapter=t.adapter,this.aggregator=new ft(t.timeframe),this.reconnector.removeAllListeners(),this.reconnector.stop(),this.reconnector=new Z(t.reconnect??e.DEFAULT_RECONNECT),this.wireReconnector(),this.aggregator.on(`bar`,e=>{this.emit(`barUpdate`,e),this.updatePrice(e.close)}),this.aggregator.on(`barClose`,e=>{this.previousClose=e.close,this.emit(`barClose`,e)}),this.wireAdapter(),this.reconnector.start(),await this.doConnect()}disconnect(){this.reconnector.stop(),this.stopHeartbeat();for(let e of this.unsubscribers)e();this.unsubscribers=[],this.adapter&&=(this.adapter.disconnect(),null),this.aggregator?.reset(),this.aggregator?.removeAllListeners(),this.aggregator=null,this.config=null,this.lastPrice=null,this.previousClose=null,this.setState(`disconnected`)}async switchTo(e,t){!this.config||!this.adapter||(this.adapter.disconnect(),this.aggregator?.reset(),this.aggregator?.setTimeframe(t),this.lastPrice=null,this.previousClose=null,this.config.symbol=e,this.config.timeframe=t,await this.doConnect())}getConnectionState(){return this.state}getConnectionInfo(){return{state:this.state,reconnectAttempt:this.reconnector.getAttempt(),lastMessageTime:this.lastMessageTime}}getLastPrice(){return this.lastPrice}async doConnect(){if(!(!this.config||!this.adapter)){this.setState(`connecting`);try{let e=await this.adapter.fetchHistory(this.config.symbol,this.config.timeframe,this.config.historyLimit??500);e.length>0&&(this.previousClose=e[e.length-1].close,this.updatePrice(e[e.length-1].close)),this.emit(`snapshot`,e),this.adapter.connect({symbol:this.config.symbol,timeframe:this.config.timeframe}),this.startHeartbeat()}catch(e){let t=e instanceof Error?e.message:String(e);this.emit(`error`,{message:t,code:`CONNECT_FAILED`}),this.setState(`error`),this.reconnector.schedule(()=>this.doConnect())}}}wireReconnector(){this.reconnector.on(`attempt`,({attempt:e,delay:t})=>{this.setState(`reconnecting`),this.emit(`connectionChange`,{state:`reconnecting`,reconnectAttempt:e,lastMessageTime:this.lastMessageTime})}),this.reconnector.on(`giveUp`,({attempts:e})=>{this.setState(`error`),this.emit(`error`,{message:`Reconnection failed after ${e} attempts`,code:`RECONNECT_EXHAUSTED`})})}wireAdapter(){this.adapter&&(this.adapter.on(`connectionChange`,(e=>{let t=e.data;this.lastMessageTime=Date.now(),t===`connected`?(this.reconnector.onConnected(),this.setState(`connected`)):(t===`disconnected`||t===`error`)&&this.reconnector.schedule(()=>this.doConnect())})),this.adapter.on(`tick`,(e=>{this.lastMessageTime=Date.now();let t=e.data;this.emit(`tick`,t),this.config?.aggregateTicks&&this.aggregator?this.aggregator.processTick(t):this.updatePrice(t.price)})),this.adapter.on(`bar`,(e=>{this.lastMessageTime=Date.now();let{bar:t,closed:n}=e.data;this.aggregator&&this.aggregator.processBar(t,n)})),this.adapter.on(`error`,(e=>{this.emit(`error`,{message:e.data?.message??`Adapter error`})})))}updatePrice(e){e!==this.lastPrice&&(this.lastPrice=e,this.priceLine.setPrice(e,this.previousClose??void 0),this.emit(`priceChange`,{price:e,previousClose:this.previousClose}))}setState(e){this.state!==e&&(this.state=e,this.emit(`connectionChange`,{state:e,reconnectAttempt:this.reconnector.getAttempt(),lastMessageTime:this.lastMessageTime}))}startHeartbeat(){this.stopHeartbeat(),this.heartbeatTimer=setInterval(()=>{Date.now()-this.lastMessageTime>6e4&&this.state===`connected`&&(this.emit(`error`,{message:`Heartbeat timeout`,code:`HEARTBEAT_TIMEOUT`}),this.adapter?.disconnect(),this.reconnector.schedule(()=>this.doConnect()))},3e4)}stopHeartbeat(){this.heartbeatTimer&&=(clearInterval(this.heartbeatTimer),null)}dispose(){this.disconnect(),this.removeAllListeners()}},mt={"1s":`1s`,"1m":`1m`,"3m":`3m`,"5m":`5m`,"15m":`15m`,"30m":`30m`,"1h":`1h`,"2h":`2h`,"4h":`4h`,"6h":`6h`,"8h":`8h`,"12h":`12h`,"1d":`1d`,"3d":`3d`,"1w":`1w`,"1M":`1M`},ht=class{name=`binance`;ws=null;state=`disconnected`;listeners=new Map;config=null;restBase;wsBase;constructor(e){this.restBase=e?.restBase??`https://api.binance.com/api/v3`,this.wsBase=e?.wsBase??`wss://stream.binance.com:9443/ws`}connect(e){this.config=e,this.connectWs()}disconnect(){this.ws&&=(this.ws.onclose=null,this.ws.close(),null),this.setState(`disconnected`)}getConnectionState(){return this.state}async fetchHistory(e,t,n=500){let r=mt[t]??`15m`,i=`${this.restBase}/klines?symbol=${e}&interval=${r}&limit=${n}`,a=await fetch(i);if(!a.ok)throw Error(`Binance REST error: ${a.status}`);return(await a.json()).map(e=>({time:e[0],open:parseFloat(e[1]),high:parseFloat(e[2]),low:parseFloat(e[3]),close:parseFloat(e[4]),volume:parseFloat(e[5])}))}on(e,t){let n=this.listeners.get(e);n||(n=new Set,this.listeners.set(e,n)),n.add(t)}off(e,t){this.listeners.get(e)?.delete(t)}dispose(){this.disconnect(),this.listeners.clear()}connectWs(){if(!this.config)return;let e=`${this.config.symbol.toLowerCase()}@kline_${mt[this.config.timeframe]??`15m`}`,t=`${this.wsBase}/${e}`;this.setState(`connecting`);try{this.ws=new WebSocket(t)}catch{this.setState(`error`),this.emitEvent(`error`,{message:`WebSocket creation failed`}),this.emitEvent(`connectionChange`,`error`);return}this.ws.onopen=()=>{this.setState(`connected`),this.emitEvent(`connectionChange`,`connected`)},this.ws.onmessage=e=>{try{let t=JSON.parse(e.data);t.e===`kline`&&this.handleKline(t.k)}catch{}},this.ws.onerror=()=>{this.setState(`error`),this.emitEvent(`error`,{message:`WebSocket error`})},this.ws.onclose=()=>{this.setState(`disconnected`),this.emitEvent(`connectionChange`,`disconnected`)}}handleKline(e){let t={time:e.t,open:parseFloat(e.o),high:parseFloat(e.h),low:parseFloat(e.l),close:parseFloat(e.c),volume:parseFloat(e.v)},n=e.x;this.emitEvent(`bar`,{bar:t,closed:n}),this.emitEvent(`tick`,{time:Date.now(),price:t.close,volume:t.volume})}setState(e){this.state=e}emitEvent(e,t){let n=this.listeners.get(e);if(n){let r={type:e,data:t,timestamp:Date.now()};for(let e of n)e(r)}}},gt=class{name=`mock`;state=`disconnected`;listeners=new Map;config=null;options;tickTimer=null;currentPrice;constructor(e){this.options={basePrice:100,volatility:1.5,tickInterval:2e3,historySize:500,...e},this.currentPrice=this.options.basePrice}connect(e){this.config=e,this.state=`connected`,this.emitEvent(`connectionChange`,`connected`),this.startTicking()}disconnect(){this.stopTicking(),this.state=`disconnected`,this.emitEvent(`connectionChange`,`disconnected`)}getConnectionState(){return this.state}async fetchHistory(e,t,n){return this.options.historyGenerator?this.options.historyGenerator(e,t,n??this.options.historySize):this.generateHistory(t,n??this.options.historySize)}on(e,t){let n=this.listeners.get(e);n||(n=new Set,this.listeners.set(e,n)),n.add(t)}off(e,t){this.listeners.get(e)?.delete(t)}dispose(){this.disconnect(),this.listeners.clear()}generateHistory(t,n){let r=[],i=Date.now(),a=(0,e.timeframeToMs)(t),o=this.options.basePrice,s=this.options.volatility/100;for(let e=n-1;e>=0;e--){let t=i-e*a,n=(Math.random()-.48)*o*s,c=o;o+=n;let l=Math.max(c,o)+Math.random()*o*s*.3,u=Math.min(c,o)-Math.random()*o*s*.3,d=Math.floor(1e4+Math.random()*1e5);r.push({time:t,open:c,high:l,low:u,close:o,volume:d})}return this.currentPrice=o,r}startTicking(){this.stopTicking();let t=this.options.tickInterval??2e3;this.tickTimer=setInterval(()=>{if(this.state!==`connected`||!this.config)return;let t=this.options.volatility/100,n=(Math.random()-.48)*this.currentPrice*t*.3;this.currentPrice+=n;let r=Date.now(),i=(0,e.timeframeToMs)(this.config.timeframe),a={time:Math.floor(r/i)*i,open:this.currentPrice-n,high:Math.max(this.currentPrice,this.currentPrice-n)+Math.abs(n)*.2,low:Math.min(this.currentPrice,this.currentPrice-n)-Math.abs(n)*.2,close:this.currentPrice,volume:Math.floor(100+Math.random()*5e3)};this.emitEvent(`bar`,{bar:a,closed:!1}),this.emitEvent(`tick`,{time:r,price:this.currentPrice,volume:a.volume})},t)}stopTicking(){this.tickTimer&&=(clearInterval(this.tickTimer),null)}emitEvent(e,t){let n=this.listeners.get(e);if(n){let r={type:e,data:t,timestamp:Date.now()};for(let e of n)e(r)}}},_t={visible:!0,showSymbol:!0,showOHLC:!0,showChange:!0,showVolume:!0,position:`top-left`,fontSize:12},vt=class{config={..._t};symbol=``;timeframe=``;hoverBar=null;indicators=[];setConfig(e){Object.assign(this.config,e)}setSymbol(e){this.symbol=e}setTimeframe(e){this.timeframe=e}setChartType(e){}setHoverBar(e){this.hoverBar=e}setIndicatorValues(e){this.indicators=e}render(e,t,n,r){if(!this.config.visible||r.length===0)return;let i=this.hoverBar??r[r.length-1],a=r.length>1?r[r.length-2]:i,{chartRect:o}=t,s=this.config.fontSize,c=this.config.position===`top-left`,l=c?o.x+8:o.x+o.width-8,u=o.y+6;if(e.textAlign=c?`left`:`right`,e.textBaseline=`top`,this.config.showSymbol){e.font=`bold ${s+2}px ${n.font.family}`,e.fillStyle=n.text;let t=[this.symbol,this.timeframe].filter(Boolean).join(` ยท `);e.fillText(t,l,u),u+=s+6}if(this.config.showOHLC){e.font=`${s}px ${n.font.family}`;let t=i.close>=i.open,r=e=>e.toLocaleString(void 0,{minimumFractionDigits:2,maximumFractionDigits:2}),o=[{label:`O`,value:r(i.open),color:n.text},{label:`H`,value:r(i.high),color:n.text},{label:`L`,value:r(i.low),color:n.text},{label:`C`,value:r(i.close),color:t?n.candleUp:n.candleDown}];if(this.config.showChange){let e=i.close-a.close,t=a.close===0?0:e/a.close*100,s=e>=0?`+`:``;o.push({label:``,value:`${s}${r(e)} (${s}${t.toFixed(2)}%)`,color:e>=0?n.candleUp:n.candleDown})}let d=l;if(c)for(let t of o)t.label&&(e.fillStyle=n.textSecondary,e.fillText(t.label,d,u),d+=e.measureText(t.label+` `).width),e.fillStyle=t.color,e.fillText(t.value,d,u),d+=e.measureText(t.value+` `).width;else{let t=o.map(e=>`${e.label} ${e.value}`).join(` `);e.fillStyle=n.text,e.fillText(t,l,u)}u+=s+3}if(this.config.showVolume){e.font=`${s-1}px ${n.font.family}`,e.fillStyle=n.textSecondary;let t=i.volume>=1e6?`${(i.volume/1e6).toFixed(2)}M`:i.volume>=1e3?`${(i.volume/1e3).toFixed(2)}K`:i.volume.toFixed(0);e.fillText(`Vol ${t}`,l,u),u+=s+2}if(this.indicators.length>0){e.font=`${s-1}px ${n.font.family}`;let t=l;for(let n of this.indicators){e.fillStyle=n.color;let r=`${n.name} ${n.value}`;e.fillText(r,t,u),t+=e.measureText(r+` `).width}}}},yt=class{static toDataURL(e,t){let n=e.querySelectorAll(`canvas`);if(n.length===0)return``;let r=n[0],i=r.width,a=r.height,o=document.createElement(`canvas`);o.width=i,o.height=a;let s=o.getContext(`2d`);t&&(s.fillStyle=t,s.fillRect(0,0,i,a));let c=Array.from(n).sort((e,t)=>(parseInt(e.style.zIndex)||0)-(parseInt(t.style.zIndex)||0));for(let e of c)s.drawImage(e,0,0);return o.toDataURL(`image/png`)}static async toBlob(e,t){let n=this.toDataURL(e,t);return n?(await fetch(n)).blob():null}static download(e,t=`chart.png`,n){let r=this.toDataURL(e,n);if(!r)return;let i=document.createElement(`a`);i.href=r,i.download=t,document.body.appendChild(i),i.click(),document.body.removeChild(i)}},bt=class{config=null;setConfig(e){this.config=e}render(e,t,n){if(!this.config?.text)return;let{chartRect:r}=t,i=this.config.fontSize??48,a=this.config.color??n.textSecondary;e.save(),e.globalAlpha=.07,e.font=`bold ${i}px ${n.font.family}`,e.fillStyle=a,e.textAlign=`center`,e.textBaseline=`middle`,e.fillText(this.config.text,r.x+r.width/2,r.y+r.height/2),e.restore()}},xt=class{visible=!0;timeframeMs=0;setVisible(e){this.visible=e}isVisible(){return this.visible}setTimeframeMs(e){this.timeframeMs=e}render(e,t,n,r){if(!this.visible||this.timeframeMs<=0||r.length===0)return;let i=r[r.length-1],o=(i.time>0xe8d4a51000?i.time:i.time*1e3)+this.timeframeMs-Date.now();if(o<=0)return;let s=this.formatRemaining(o),c=a(r.length-1,t),l=t.chartRect.y+t.chartRect.height-4;c<t.chartRect.x||c>t.chartRect.x+t.chartRect.width||(e.font=`${n.font.sizeSmall}px ${n.font.family}`,e.textBaseline=`bottom`,e.textAlign=`center`,e.fillStyle=n.textSecondary,e.fillText(s,c,l))}formatRemaining(e){let t=Math.ceil(e/1e3),n=Math.floor(t/3600),r=Math.floor(t%3600/60),i=t%60;return n>0?`${n}:${String(r).padStart(2,`0`)}:${String(i).padStart(2,`0`)}`:`${r}:${String(i).padStart(2,`0`)}`}},St=class{config={visible:!1};cachedBreaks=[];lastDataLength=0;setConfig(e){Object.assign(this.config,e)}isVisible(){return this.config.visible}setVisible(e){this.config.visible=e}computeBreaks(e){if(e.length<2)return[];if(e.length===this.lastDataLength&&this.cachedBreaks.length>0)return this.cachedBreaks;let t=[],n=this.getDayKey(e[0].time);for(let r=1;r<e.length;r++){let i=this.getDayKey(e[r].time);i!==n&&(t.push(r),n=i)}return this.lastDataLength=e.length,this.cachedBreaks=t,t}getDayKey(e){let t=e>0xe8d4a51000?e:e*1e3,n=new Date(t);return`${n.getFullYear()}-${n.getMonth()}-${n.getDate()}`}render(e,t,n,r){if(!this.config.visible||r.length<2)return;let i=this.computeBreaks(r);if(i.length===0)return;let{chartRect:o}=t,s=this.config.color??n.axisLine,c=this.config.lineWidth??1,l=this.config.lineStyle??`dashed`;e.save(),e.strokeStyle=s,e.lineWidth=c,e.globalAlpha=.4,l===`dashed`?e.setLineDash([6,4]):l===`dotted`&&e.setLineDash([2,3]),e.beginPath();for(let n of i){let r=a(n,t)-(t.barWidth+t.barSpacing)/2;if(r<o.x-1||r>o.x+o.width+1)continue;let i=Math.round(r)+.5;e.moveTo(i,o.y),e.lineTo(i,o.y+o.height)}e.stroke(),e.restore()}invalidateCache(){this.lastDataLength=0,this.cachedBreaks=[]}},Ct=1,wt=class extends X{alerts=[];lastPrice=null;requestRender=null;pricePrecision=2;setRequestRender(e){this.requestRender=e}setPricePrecision(e){this.pricePrecision=e}addAlert(e,t=`crossing`,n,r=!1){let i=`tc_alert_${Ct++}`,a={id:i,price:e,condition:t,message:n,triggered:!1,repeating:r};return this.alerts.push(a),this.emit(`added`,a),this.requestRender?.(),i}removeAlert(e){this.alerts=this.alerts.filter(t=>t.id!==e),this.emit(`removed`,e),this.requestRender?.()}getAlerts(){return[...this.alerts]}clearAlerts(){this.alerts=[],this.requestRender?.()}checkPrice(e){if(this.lastPrice===null){this.lastPrice=e;return}for(let t of this.alerts){if(t.triggered&&!t.repeating)continue;let n=!1;switch(t.condition){case`crossingUp`:n=this.lastPrice<t.price&&e>=t.price;break;case`crossingDown`:n=this.lastPrice>t.price&&e<=t.price;break;case`crossing`:n=this.lastPrice<t.price&&e>=t.price||this.lastPrice>t.price&&e<=t.price;break;case`greaterThan`:n=e>t.price;break;case`lessThan`:n=e<t.price;break}n&&(t.triggered=!0,this.emit(`triggered`,{...t}))}this.lastPrice=e}saveToStorage(e){try{let t=this.alerts.map(e=>({id:e.id,price:e.price,condition:e.condition,message:e.message,triggered:e.triggered}));localStorage.setItem(e,JSON.stringify(t))}catch{}}loadFromStorage(e){try{let t=localStorage.getItem(e);if(!t)return;let n=JSON.parse(t);if(!Array.isArray(n))return;for(let e of n)e.price&&e.condition&&!e.triggered&&this.addAlert(e.price,e.condition,e.message)}catch{}}render(e,t,n){let{chartRect:r}=t;for(let i of this.alerts){let a=o(i.price,t);if(a<r.y||a>r.y+r.height)continue;let s=i.triggered?`#FF9800`:`#FFD700`;e.setLineDash([2,6]),e.strokeStyle=s,e.lineWidth=1,e.beginPath(),e.moveTo(r.x,Math.round(a)+.5),e.lineTo(r.x+r.width,Math.round(a)+.5),e.stroke(),e.setLineDash([]),e.font=`10px ${n.font.family}`,e.fillStyle=s,e.textBaseline=`bottom`,e.textAlign=`right`;let c=`๐Ÿ”” ${i.price.toFixed(this.pricePrecision)}${i.message?` - ${i.message}`:``}`;e.fillText(c,r.x+r.width-4,a-2)}}},Tt=class extends X{data=[];currentIndex=0;timer=null;state=`stopped`;config={speed:1,interval:500};load(e){this.stop(),this.data=e,this.currentIndex=0}play(e){this.data.length!==0&&(e&&Object.assign(this.config,e),e?.startIndex!==void 0&&(this.currentIndex=e.startIndex),this.state=`playing`,this.emit(`stateChange`,`playing`),this.timer=setInterval(()=>{if(this.currentIndex>=this.data.length){this.stop(),this.emit(`complete`,void 0);return}let e=this.data[this.currentIndex];this.emit(`bar`,{bar:e,index:this.currentIndex,total:this.data.length}),this.currentIndex++},this.config.interval/this.config.speed))}pause(){this.timer&&=(clearInterval(this.timer),null),this.state=`paused`,this.emit(`stateChange`,`paused`)}resume(){this.state===`paused`&&this.play()}stop(){this.timer&&=(clearInterval(this.timer),null),this.state=`stopped`,this.currentIndex=0,this.emit(`stateChange`,`stopped`)}seekTo(e){this.currentIndex=Math.max(0,Math.min(e,this.data.length-1))}setSpeed(e){this.config.speed=e,this.state===`playing`&&(this.pause(),this.play())}getState(){return this.state}getProgress(){return{current:this.currentIndex,total:this.data.length,percent:this.data.length>0?this.currentIndex/this.data.length*100:0}}dispose(){this.stop(),this.removeAllListeners(),this.data=[]}},$=1,Et=class{static capture(e,t){return{version:$,timestamp:Date.now(),chartType:t?.chartType??`candlestick`,symbol:t?.symbol,timeframe:t?.timeframe,viewport:{barWidth:8,barSpacing:2,offset:0},theme:e.getTheme().name,indicators:e.getIndicators?.()??[],drawings:e.getDrawings(),orders:e.getOrders?.()??[],positions:e.getPositions?.()??[],alerts:e.getAlerts?.()??[]}}static serialize(e){return JSON.stringify(e)}static deserialize(e){let t=JSON.parse(e);return t.version!==$&&console.warn(`Chart state version mismatch: ${t.version} vs ${$}`),t}static saveToStorage(e,t){localStorage.setItem(e,this.serialize(t))}static loadFromStorage(e){let t=localStorage.getItem(e);return t?this.deserialize(t):null}static downloadFile(e,t=`tc-chart-state.json`){let n=new Blob([this.serialize(e)],{type:`application/json`}),r=URL.createObjectURL(n),i=document.createElement(`a`);i.href=r,i.download=t,document.body.appendChild(i),i.click(),document.body.removeChild(i),URL.revokeObjectURL(r)}static loadFromFile(){return new Promise((e,t)=>{let n=document.createElement(`input`);n.type=`file`,n.accept=`.json`,n.onchange=async()=>{let r=n.files?.[0];if(!r){t(Error(`No file selected`));return}let i=await r.text();e(this.deserialize(i))},n.click()})}},Dt=class{undoStack=[];redoStack=[];maxHistory=50;onChange=null;setOnChange(e){this.onChange=e}push(e){this.undoStack.push(e),this.undoStack.length>this.maxHistory&&this.undoStack.shift(),this.redoStack.length=0,this.onChange?.()}canUndo(){return this.undoStack.length>0}canRedo(){return this.redoStack.length>0}undo(){let e=this.undoStack.pop();return e?(this.redoStack.push(e),this.onChange?.(),e):null}redo(){let e=this.redoStack.pop();return e?(this.undoStack.push(e),this.onChange?.(),e):null}clear(){this.undoStack.length=0,this.redoStack.length=0}getState(){return{canUndo:this.undoStack.length>0,canRedo:this.redoStack.length>0,undoCount:this.undoStack.length,redoCount:this.redoStack.length}}},Ot=class{static toCSV(e){return[`Time,Open,High,Low,Close,Volume`,...e.map(e=>`${new Date(e.time).toISOString()},${e.open},${e.high},${e.low},${e.close},${e.volume}`)].join(`
20
20
  `)}static toJSON(e){return JSON.stringify(e.map(e=>({time:new Date(e.time).toISOString(),open:e.open,high:e.high,low:e.low,close:e.close,volume:e.volume})),null,2)}static download(e,t,n){let r=new Blob([e],{type:n}),i=URL.createObjectURL(r),a=document.createElement(`a`);a.href=i,a.download=t,document.body.appendChild(a),a.click(),document.body.removeChild(a),URL.revokeObjectURL(i)}},kt={linear:e=>e,easeOutCubic:e=>1-(1-e)**3,easeOutQuart:e=>1-(1-e)**4,easeInOutCubic:e=>e<.5?4*e*e*e:1-(-2*e+2)**3/2,easeOutExpo:e=>e===1?1:1-2**(-10*e),easeOutElastic:e=>e===0||e===1?e:2**(-10*e)*Math.sin((e*10-.75)*(2*Math.PI/3))+1,spring:e=>1-Math.E**(-6*e)*Math.cos(8*e)},At=class{animations=new Map;frameId=0;running=!1;animate(e,t){this.animations.set(e,{startTime:-1,duration:t.duration,easing:t.easing??kt.easeOutCubic,onUpdate:t.onUpdate,onComplete:t.onComplete}),this.running||(this.running=!0,this.tick())}cancel(e){this.animations.delete(e),this.animations.size===0&&this.stop()}cancelAll(){this.animations.clear(),this.stop()}isAnimating(e){return e?this.animations.has(e):this.animations.size>0}tick(){this.frameId=requestAnimationFrame(e=>{let t=[];for(let[n,r]of this.animations){r.startTime<0&&(r.startTime=e);let i=e-r.startTime,a=Math.min(1,i/r.duration),o=r.easing(a);r.onUpdate(o),a>=1&&(r.onComplete?.(),t.push(n))}for(let e of t)this.animations.delete(e);this.animations.size>0?this.tick():this.running=!1})}stop(){this.frameId&&=(cancelAnimationFrame(this.frameId),0),this.running=!1}dispose(){this.cancelAll()}};exports.ADXIndicator=L,exports.ATRIndicator=I,exports.AccumulationDistributionIndicator=de,exports.AlertManager=wt,exports.AnchoredVWAPTool=nt,exports.Animator=At,exports.AreaRenderer=h,exports.AroonIndicator=H,exports.ArrowTool=Qe,exports.BarCountdown=xt,exports.BarRenderer=g,exports.BaselineRenderer=v,exports.BinanceAdapter=ht,exports.BollingerBandsIndicator=ae,exports.CCIIndicator=B,exports.CMFIndicator=G,exports.CandlestickRenderer=p,exports.CanvasLayer=t,exports.ChartLegend=vt,exports.ChartStateManager=Et,exports.CompareRenderer=C,exports.CrosshairHandler=Ce,exports.CrosshairTooltip=Te,exports.CurrentPriceLine=Q,exports.DEFAULT_LEGEND_CONFIG=_t,exports.DPRManager=i,exports.DataExporter=Ot,exports.DateRangeTool=Ye,exports.DepthOverlay=st,exports.DonchianChannelIndicator=N,exports.DrawingBase=q,exports.DrawingManager=Ae,exports.DrawingRenderer=je,exports.EMAIndicator=ie,exports.Easing=kt,exports.ElliottWaveTool=qe,exports.EllipseTool=Ue,exports.Emitter=X,exports.EventBus=Oe,exports.ExtendedLineTool=Ie,exports.FibExtensionTool=Ve,exports.FibRetracementTool=ze,exports.GannBoxTool=tt,exports.GannFanTool=et,exports.GridRenderer=me,exports.HollowCandleRenderer=_,exports.HorizontalLineTool=Ne,exports.IchimokuIndicator=se,exports.IndicatorBase=k,exports.IndicatorEngine=ne,exports.InteractionManager=be,exports.KagiRenderer=b,exports.KeltnerChannelIndicator=M,exports.KeyboardHandler=we,exports.LayerManager=n,exports.LineRenderer=m,exports.MACDIndicator=P,exports.MFIIndicator=V,exports.MeasureTool=Xe,exports.MockAdapter=gt,exports.OBVIndicator=R,exports.OrderRenderer=at,exports.PanHandler=xe,exports.ParabolicSARIndicator=A,exports.ParallelChannelTool=Le,exports.PitchforkTool=Ge,exports.PointAndFigureRenderer=x,exports.PositionRenderer=ot,exports.PriceAxis=he,exports.PriceRangeTool=Je,exports.ROCIndicator=U,exports.RSIIndicator=ce,exports.RayTool=Fe,exports.ReconnectManager=Z,exports.RectangleTool=He,exports.RegressionChannelTool=Re,exports.RenderEngine=u,exports.RenderLoop=r,exports.RenkoRenderer=y,exports.ReplayManager=Tt,exports.SMAIndicator=re,exports.Screenshot=yt,exports.SessionBreaks=St,exports.StdDevIndicator=le,exports.StochasticIndicator=F,exports.StreamManager=pt,exports.SupertrendIndicator=j,exports.TSIIndicator=W,exports.TextAnnotationTool=Ze,exports.TickAggregator=ft,exports.TimeAxis=ye,exports.TradingContextMenu=lt,exports.TradingDragHandler=ct,exports.TradingManager=ut,exports.TradingRenderer=dt,exports.TrendLineTool=Me,exports.TriangleTool=We,exports.UndoRedoManager=Dt,exports.VROCIndicator=fe,exports.VWAPIndicator=oe,exports.VerticalLineTool=Pe,exports.Viewport=d,exports.VolumeProfileIndicator=ue,exports.VolumeProfileRangeTool=rt,exports.VolumeRenderer=S,exports.Watermark=bt,exports.WilliamsRIndicator=z,exports.ZoomHandler=Se,exports.barIndexToX=a,exports.getVisibleData=f,exports.priceToY=o,exports.registerBuiltInDrawingTools=it,exports.registerBuiltInIndicators=pe,exports.timestampToBarIndex=l,exports.toHeikinAshi=w,exports.toKagi=O,exports.toLineBreak=D,exports.toPointAndFigure=ee,exports.toRenko=T,exports.xToBarIndex=s,exports.yToPrice=c;
21
21
  //# sourceMappingURL=index.cjs.map