@tradecanvas/core 0.7.1 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const L=require("@tradecanvas/commons");class Ee{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;const n=Math.round(e.width*t),s=Math.round(e.height*t);(this.canvas.width!==n||this.canvas.height!==s)&&(this.canvas.width=n,this.canvas.height=s),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()}}class He{constructor(e){this.container=e}layers=new Map;createLayers(){this.destroy();for(const e of[L.LayerType.Background,L.LayerType.Main,L.LayerType.Overlay,L.LayerType.UI])this.layers.set(e,new Ee(this.container,e))}getLayer(e){return this.layers.get(e)}resize(e,t){for(const n of this.layers.values())n.resize(e,t)}destroy(){for(const e of this.layers.values())e.destroy();this.layers.clear()}}class $e{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(L.LayerType.Background),this.dirtyLayers.add(L.LayerType.Main),this.dirtyLayers.add(L.LayerType.Overlay),this.dirtyLayers.add(L.LayerType.UI),this.running&&!this.animFrameId&&this.scheduleFrame()}start(){this.running=!0,this.markAllDirty()}stop(){this.running=!1,this.animFrameId&&(cancelAnimationFrame(this.animFrameId),this.animFrameId=0)}scheduleFrame(){this.animFrameId=requestAnimationFrame(()=>{if(this.animFrameId=0,!(!this.running||!this.callback)&&this.dirtyLayers.size>0){const e=this.dirtyLayers;this.dirtyLayers=this.swapLayers,this.callback(e),e.clear(),this.swapLayers=e}})}}class Ye{constructor(e){this.container=e,this.currentDpr=window.devicePixelRatio||1,this.cachedSize={width:e.clientWidth,height:e.clientHeight}}observer=null;callback=null;currentDpr=1;resizeScheduled=!1;cachedSize;getDpr(){return this.currentDpr}getContainerSize(){return this.cachedSize}readContainerSize(){const 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;const t=this.container.clientWidth,n=this.container.clientHeight;t===this.cachedSize.width&&n===this.cachedSize.height||(this.cachedSize={width:t,height:n},this.callback?.(this.cachedSize,this.currentDpr))}))}),this.observer.observe(this.container)}destroy(){this.observer?.disconnect(),this.observer=null,this.callback=null}}function D(r,e){const t=e.barWidth+e.barSpacing;return r*t-e.offset+e.chartRect.x+e.barWidth/2}function A(r,e){const{min:t,max:n}=e.priceRange;if(e.logScale&&t>0&&n>0){const o=Math.log(t),h=Math.log(n)-o;if(h===0)return e.chartRect.y+e.chartRect.height/2;const a=(Math.log(Math.max(r,Number.EPSILON))-o)/h;return e.chartRect.y+e.chartRect.height*(1-a)}const s=n-t;if(s===0)return e.chartRect.y+e.chartRect.height/2;const i=(r-t)/s;return e.chartRect.y+e.chartRect.height*(1-i)}function ee(r,e){const t=e.barWidth+e.barSpacing;return Math.round((r-e.chartRect.x-e.barWidth/2+e.offset)/t)}function V(r,e){const{min:t,max:n}=e.priceRange,s=1-(r-e.chartRect.y)/e.chartRect.height;if(e.logScale&&t>0&&n>0){const i=Math.log(t),o=Math.log(n);return Math.exp(i+(o-i)*s)}return t+(n-t)*s}function j(r,e){if(e.length===0)return-1;if(r<=e[0].time)return 0;if(r>=e[e.length-1].time)return e.length-1;let t=0,n=e.length-1;for(;t<=n;){const l=t+n>>>1;if(e[l].time===r)return l;e[l].time<r?t=l+1:n=l-1}if(t>=e.length)return e.length-1;if(n<0)return 0;const s=e[n].time,i=e[t].time,o=i!==s?(r-s)/(i-s):0;return n+o}function O(r,e){const t=e.data;return t&&t.length>0?j(r,t):r}function Ve(r,e){return D(O(r,e),e)}function _(r,e){const t=ee(r,e),n=e.data;if(n&&n.length>0){const s=Math.max(0,Math.min(n.length-1,t));return n[s].time}return t}class en{layerManager;renderLoop;dprManager;renderCtx=null;bgLayer;mainLayer;overlayLayer;uiLayer;onContainerResize=null;constructor(e){this.layerManager=new He(e),this.renderLoop=new $e,this.dprManager=new Ye(e),this.layerManager.createLayers(),this.cacheLayerRefs(),this.renderLoop.setCallback(s=>this.render(s)),this.dprManager.onResize((s,i)=>{this.layerManager.resize(s,i),this.renderLoop.markAllDirty(),this.onContainerResize?.(s)});const t=this.dprManager.getContainerSize(),n=this.dprManager.getDpr();this.layerManager.resize(t,n)}cacheLayerRefs(){this.bgLayer=this.layerManager.getLayer(L.LayerType.Background),this.mainLayer=this.layerManager.getLayer(L.LayerType.Main),this.overlayLayer=this.layerManager.getLayer(L.LayerType.Overlay),this.uiLayer=this.layerManager.getLayer(L.LayerType.UI)}setRenderContext(e){this.renderCtx=e}start(){this.renderLoop.start()}stop(){this.renderLoop.stop()}requestRender(e){e!==void 0?this.renderLoop.markDirty(e):this.renderLoop.markAllDirty()}render(e){const t=this.renderCtx;if(!t)return;const{viewport:n,theme:s,data:i}=t,o=t.numberLocale??"en-US";if(!(n.chartRect.width<=0||n.chartRect.height<=0)){if(e.has(L.LayerType.Background)&&this.bgLayer&&(this.bgLayer.clear(),t.gridRenderer?.render(this.bgLayer.ctx,n,s),t.sessionBreaks?.render(this.bgLayer.ctx,n,s,i),t.watermark?.render(this.bgLayer.ctx,n,s)),e.has(L.LayerType.Main)&&this.mainLayer){this.mainLayer.clear();const l=this.mainLayer.ctx;l.save(),l.beginPath(),l.rect(n.chartRect.x,n.chartRect.y,n.chartRect.width,n.chartRect.height),l.clip(),t.volumeRenderer?.render(l,i,n,s),t.chartRenderer?.render(l,i,n,s),t.compareRenderer?.render(l,i,n,s),t.indicatorEngine?.renderOverlays(l,n),l.restore();const h=t.panels,a=t.indicatorEngine;if(a&&h.length>0){const c=`10px ${s.font.family}`,d=a.getPanelIndicators(),f=new Map;for(const u of d)f.set(u.instanceId,u);for(const u of h){if(u.rect.width<=0||u.rect.height<=0)continue;l.save(),l.beginPath(),l.rect(u.rect.x,u.rect.y,u.rect.width,u.rect.height),l.clip();const g=20;l.fillStyle=s.background,l.fillRect(u.rect.x,u.rect.y,u.rect.width,u.rect.height),l.fillStyle=s.axisLine,l.fillRect(u.rect.x,u.rect.y,u.rect.width,3),l.fillStyle=s.textSecondary,l.font=c,l.textBaseline="top",l.textAlign="left";const m=f.get(u.instanceId);m&&l.fillText(m.descriptor.name,u.rect.x+6,u.rect.y+6),l.save(),l.beginPath(),l.rect(u.rect.x,u.rect.y+g,u.rect.width,u.rect.height-g),l.clip(),a.renderPanel(l,u.instanceId,u.viewport),l.restore(),l.restore()}}}if(e.has(L.LayerType.Overlay)&&this.overlayLayer){this.overlayLayer.clear();const l=this.overlayLayer.ctx;t.priceLimits&&this.renderPriceLimits(l,n,s,t.priceLimits),t.tradeZoneManager?.render(l,n,s),t.drawingRenderer?.render(l,n),t.tradingRenderer?.render(l,n,s),t.signalMarkerManager?.render(l,n,s),t.alertManager?.render(l,n,s),t.crosshairHandler?.render(l,n,s);const h=t.crosshairHandler?.getPosition();if(h&&t.panels.length>0)for(const a of t.panels){const c=a.rect;if(h.x>=c.x&&h.x<=c.x+c.width&&h.y>=c.y&&h.y<=c.y+c.height){const d=a.viewport;let f=h.x;const u=ee(f,d),g=Math.max(0,Math.min((t.data?.length??1)-1,Math.round(u)));f=D(g,d),l.save(),l.beginPath(),l.rect(c.x,c.y,c.width,c.height),l.clip(),l.setLineDash([4,4]),l.strokeStyle=s.crosshair,l.lineWidth=1,l.beginPath(),l.moveTo(Math.round(f)+.5,c.y),l.lineTo(Math.round(f)+.5,c.y+c.height),l.stroke(),l.beginPath(),l.moveTo(c.x,Math.round(h.y)+.5),l.lineTo(c.x+c.width,Math.round(h.y)+.5),l.stroke(),l.setLineDash([]),l.restore();break}}}if(e.has(L.LayerType.UI)&&this.uiLayer){this.uiLayer.clear();const l=this.uiLayer.ctx;if(t.priceAxis?.render(l,n,s),t.tradingRenderer?.renderAxisBadges(l,n,s),t.currentPriceLine?.render(l,n,s),t.timeAxis?.render(l,n,s,i,t.timeAxisY),t.chartLegend?.render(l,n,s,i),t.barCountdown?.render(l,n,s,i),t.panels.length>0){const h=t.crosshairHandler?.getPosition(),a=t.indicatorEngine,c=`${s.font.sizeSmall}px ${s.font.family}`;let d;if(a){const f=a.getPanelIndicators();d=new Map;for(const u of f)d.set(u.instanceId,u)}for(const f of t.panels){const u=f.viewport,g=f.rect,{min:m,max:y}=u.priceRange;if(y-m<=0||g.height<=0)continue;const p=g.x+g.width,T=u.chartRect;l.strokeStyle=s.axisLine,l.lineWidth=1,l.beginPath(),l.moveTo(p+.5,g.y),l.lineTo(p+.5,g.y+g.height),l.stroke();const v=L.computeTickStep(m,y,4),S=Math.ceil(m/v)*v,M=v<1?Math.ceil(-Math.log10(v))+1:v<10?1:0;l.font=c,l.textBaseline="middle",l.textAlign="left";for(let P=S;P<=y;P+=v){const R=A(P,u);R<T.y||R>T.y+T.height||(l.strokeStyle=s.axisLine,l.beginPath(),l.moveTo(p,Math.round(R)+.5),l.lineTo(p+4,Math.round(R)+.5),l.stroke(),l.fillStyle=s.axisLabel,l.fillText(L.formatPrice(P,M,o),p+6,R))}if(h&&h.x>=g.x&&h.x<=g.x+g.width&&h.y>=g.y&&h.y<=g.y+g.height){const P=V(h.y,u),R=L.formatPrice(P,M,o);l.font=`bold ${s.font.sizeSmall}px ${s.font.family}`;const w=l.measureText(R).width,k=Math.min(w+10,L.PRICE_AXIS_WIDTH-2);l.fillStyle=s.crosshair,l.fillRect(p+1,h.y-9,k,18),l.fillStyle=s.background,l.textBaseline="middle",l.textAlign="left",l.fillText(R,p+5,h.y)}if(h&&a){const P=ee(h.x,u),R=Math.max(0,Math.min((i?.length??1)-1,Math.round(P))),w=a.getOutput(f.instanceId);if(w?.series&&R<w.series.length){const k=w.series[R];if(k){const C=[];for(const I in k){const B=k[I];B!==void 0&&C.push(`${I}: ${B.toFixed(M)}`)}if(C.length>0){const I=d?.get(f.instanceId),B=I?l.measureText(I.descriptor.name).width+14:10;l.font=c,l.fillStyle=s.textSecondary,l.textBaseline="top",l.textAlign="left",l.fillText(C.join(" "),g.x+B,g.y+6)}}}}}}}}}renderPriceLimits(e,t,n,s){const{chartRect:i}=t,o=[{price:s.ceiling,color:s.colors?.ceiling??"#FF00FF",label:"CE"},{price:s.floor,color:s.colors?.floor??"#00FFFF",label:"FL"},{price:s.reference,color:s.colors?.reference??"#FFD700",label:"REF"}],l=`bold 9px ${n.font.family}`,h=`10px ${n.font.family}`;for(const{price:a,color:c,label:d}of o){const f=A(a,t);if(f<i.y||f>i.y+i.height)continue;e.setLineDash([8,4]),e.strokeStyle=c,e.lineWidth=1,e.beginPath(),e.moveTo(i.x,Math.round(f)+.5),e.lineTo(i.x+i.width,Math.round(f)+.5),e.stroke(),e.setLineDash([]),e.font=l,e.fillStyle=c,e.textBaseline="bottom",e.textAlign="left",e.fillText(`${d} ${a.toFixed(2)}`,i.x+4,f-2);const u=i.x+i.width+1;e.fillStyle=c,e.fillRect(u,f-7,L.PRICE_AXIS_WIDTH-2,14),e.fillStyle="#000",e.font=h,e.textBaseline="middle",e.textAlign="left",e.fillText(a.toFixed(2),u+4,f)}}destroy(){this.renderLoop.stop(),this.dprManager.destroy(),this.layerManager.destroy()}}class tn{state;dataLength=0;minBarWidth;maxBarWidth;rightMarginBars;constructor(e,t,n=2,s=30,i=5){this.minBarWidth=n,this.maxBarWidth=s,this.rightMarginBars=i;const o=this.computeChartRect(e,t);this.state={visibleRange:{from:0,to:0},priceRange:{min:0,max:1},barWidth:L.DEFAULT_BAR_WIDTH,barSpacing:L.DEFAULT_BAR_SPACING,offset:0,chartRect:o}}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(e,t){return{x:0,y:0,width:Math.max(0,e-L.PRICE_AXIS_WIDTH),height:Math.max(0,t-L.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(e,t){this.dataLength=e.length,this.dataLength!==0&&(this.clampOffset(),this.updateVisibleRange(),t&&(this.state.priceRange=L.computePriceRange(e,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(){const e=this.state.barWidth+this.state.barSpacing,t=this.rightMarginBars*e,s=this.dataLength*e-this.state.chartRect.width+t;return this.state.offset>=s-e*2}scrollToEnd(){const 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(e,t){const n=this.state.barWidth,s=L.clamp(n*(1+e),this.minBarWidth,this.maxBarWidth);if(s===n)return;const i=this.state.barWidth+this.state.barSpacing,o=(this.state.offset+t)/i;this.state.barWidth=s;const l=s+this.state.barSpacing;this.state.offset=o*l-t,this.clampOffset(),this.updateVisibleRange()}clampOffset(){if(this.state.chartRect.width<=0)return;const e=this.state.barWidth+this.state.barSpacing,t=this.dataLength*e,n=this.rightMarginBars*e,s=t-this.state.chartRect.width+n;let i,o;s>0?(i=-(this.state.chartRect.width*.5),o=s):(i=s,o=s),this.state.offset=L.clamp(this.state.offset,i,o)}updateVisibleRange(){const e=this.state.barWidth+this.state.barSpacing;if(e<=0||this.state.chartRect.width<=0)return;const t=Math.floor(this.state.offset/e),n=Math.ceil(this.state.chartRect.width/e)+1,s=Math.min(t+n,this.dataLength-1);this.state.visibleRange={from:Math.max(0,t),to:Math.max(0,s)}}}function nn(r,e){return L.sliceVisibleData(r,e.visibleRange.from,e.visibleRange.to)}class sn{render(e,t,n,s){const{from:i,to:o}=n.visibleRange,l=n.barWidth,h=l/2,a=l+n.barSpacing,c=-n.offset+n.chartRect.x+h,{min:d,max:f}=n.priceRange,u=f-d;if(u===0)return;const g=n.chartRect.y,y=n.chartRect.height/u,b=P=>P*a+c,p=P=>g+(f-P)*y,T=new Path2D,v=new Path2D,S=new Path2D,M=new Path2D;for(let P=i;P<=o&&P<t.length;P++){const R=t[P],w=b(P),k=p(R.high),C=p(R.low),I=p(R.open),B=p(R.close),F=R.close>=R.open,W=F?T:v;W.moveTo(w,k),W.lineTo(w,C);const Y=F?B:I,X=Math.max(Math.abs(B-I),1);(F?S:M).rect(w-h,Y,l,X)}e.strokeStyle=s.candleUpWick,e.lineWidth=1,e.stroke(T),e.strokeStyle=s.candleDownWick,e.stroke(v),e.fillStyle=s.candleUp,e.fill(S),e.fillStyle=s.candleDown,e.fill(M)}}class on{render(e,t,n,s){const{from:i,to:o}=n.visibleRange;if(i>o||t.length===0)return;const l=n.barWidth+n.barSpacing,h=-n.offset+n.chartRect.x+n.barWidth/2,{min:a,max:c}=n.priceRange,d=c-a;if(d===0)return;const f=n.chartRect.y,u=n.chartRect.height/d;e.beginPath(),e.strokeStyle=s.lineColor,e.lineWidth=2,e.lineJoin="round";let g=!1;for(let m=i;m<=o&&m<t.length;m++){const y=m*l+h,b=f+(c-t[m].close)*u;g?e.lineTo(y,b):(e.moveTo(y,b),g=!0)}e.stroke()}}class rn{render(e,t,n,s){const{from:i,to:o}=n.visibleRange;if(i>o||t.length===0)return;const l=n.barWidth+n.barSpacing,h=-n.offset+n.chartRect.x+n.barWidth/2,{min:a,max:c}=n.priceRange,d=c-a;if(d===0)return;const f=n.chartRect.y,u=n.chartRect.height,g=u/d,m=f+u,y=i*l+h,b=f+(c-t[i].close)*g,p=e.createLinearGradient(0,f,0,m);p.addColorStop(0,s.areaTopColor),p.addColorStop(1,s.areaBottomColor),e.beginPath(),e.moveTo(y,m),e.lineTo(y,b);let T=y;for(let v=i+1;v<=o&&v<t.length;v++)T=v*l+h,e.lineTo(T,f+(c-t[v].close)*g);e.lineTo(T,m),e.closePath(),e.fillStyle=p,e.fill(),e.beginPath(),e.strokeStyle=s.lineColor,e.lineWidth=2,e.lineJoin="round",e.moveTo(y,b);for(let v=i+1;v<=o&&v<t.length;v++)e.lineTo(v*l+h,f+(c-t[v].close)*g);e.stroke()}}class ln{render(e,t,n,s){const{from:i,to:o}=n.visibleRange,l=n.barWidth,h=l/2,a=l+n.barSpacing,c=-n.offset+n.chartRect.x+h,{min:d,max:f}=n.priceRange,u=f-d;if(u===0)return;const g=n.chartRect.y,m=n.chartRect.height/u,y=v=>v*a+c,b=v=>g+(f-v)*m,p=new Path2D,T=new Path2D;for(let v=i;v<=o&&v<t.length;v++){const S=t[v],M=y(v),R=S.close>=S.open?p:T,w=b(S.high),k=b(S.low),C=b(S.open),I=b(S.close);R.moveTo(M,w),R.lineTo(M,k),R.moveTo(M-h,C),R.lineTo(M,C),R.moveTo(M,I),R.lineTo(M+h,I)}e.lineWidth=1,e.strokeStyle=s.candleUp,e.stroke(p),e.strokeStyle=s.candleDown,e.stroke(T)}}class an{render(e,t,n,s){const{from:i,to:o}=n.visibleRange,l=n.barWidth,h=l/2,a=l+n.barSpacing,c=-n.offset+n.chartRect.x+h,{min:d,max:f}=n.priceRange,u=f-d;if(u===0)return;const g=n.chartRect.y,m=n.chartRect.height/u,y=R=>R*a+c,b=R=>g+(f-R)*m,p=new Path2D,T=new Path2D,v=[],S=[],M=new Path2D,P=new Path2D;for(let R=i;R<=o&&R<t.length;R++){const w=t[R],k=y(R),C=w.close>=w.open,I=R===0||w.close>=t[R-1].close,B=b(w.high),F=b(w.low),W=b(w.open),Y=b(w.close),X=Math.min(W,Y),N=Math.max(Math.abs(Y-W),1),ne=I?p:T;ne.moveTo(k,B),ne.lineTo(k,F),C?(I?v:S).push({x:k-h,y:X,w:l,h:N}):(I?M:P).rect(k-h,X,l,N)}e.lineWidth=1,e.strokeStyle=s.candleUp,e.stroke(p),e.strokeStyle=s.candleDown,e.stroke(T),e.fillStyle=s.candleUp,e.fill(M),e.fillStyle=s.candleDown,e.fill(P),e.lineWidth=1.5,e.strokeStyle=s.candleUp;for(const R of v)e.strokeRect(R.x,R.y,R.w,R.h);e.strokeStyle=s.candleDown;for(const R of S)e.strokeRect(R.x,R.y,R.w,R.h)}}class hn{baselinePrice=null;setBaseline(e){this.baselinePrice=e}render(e,t,n,s){const{from:i,to:o}=n.visibleRange;if(i>o||t.length===0)return;const l=n.barWidth,h=l/2,a=l+n.barSpacing,c=-n.offset+n.chartRect.x+h,{min:d,max:f}=n.priceRange,u=f-d;if(u===0)return;const g=n.chartRect.y,m=n.chartRect.height/u,y=W=>W*a+c,b=W=>g+(f-W)*m,p=this.baselinePrice??this.computeBaseline(t,i,o),T=b(p),{chartRect:v}=n,S=[],M=[];for(let W=i;W<=o&&W<t.length;W++)S.push(y(W)),M.push(b(t[W].close));if(S.length<2)return;const P=S.length-1;e.save();const R=new Path2D;R.rect(v.x,v.y,v.width,T-v.y),e.clip(R);const w=new Path2D;w.moveTo(S[0],T);for(let W=0;W<=P;W++)w.lineTo(S[W],M[W]);w.lineTo(S[P],T),w.closePath(),e.fillStyle=`${s.candleUp}20`,e.fill(w),e.restore(),e.save();const k=new Path2D;k.rect(v.x,T,v.width,v.y+v.height-T),e.clip(k);const C=new Path2D;C.moveTo(S[0],T);for(let W=0;W<=P;W++)C.lineTo(S[W],M[W]);C.lineTo(S[P],T),C.closePath(),e.fillStyle=`${s.candleDown}20`,e.fill(C),e.restore();const I=new Path2D,B=new Path2D;for(let W=1;W<=P;W++){const Y=M[W]<=T?I:B;Y.moveTo(S[W-1],M[W-1]),Y.lineTo(S[W],M[W])}e.lineWidth=2,e.strokeStyle=s.candleUp,e.stroke(I),e.strokeStyle=s.candleDown,e.stroke(B),e.setLineDash([6,4]),e.strokeStyle=s.textSecondary,e.lineWidth=1;const F=new Path2D;F.moveTo(v.x,T),F.lineTo(v.x+v.width,T),e.stroke(F),e.setLineDash([])}computeBaseline(e,t,n){let s=0,i=0;for(let o=t;o<=n&&o<e.length;o++)s+=e[o].close,i++;return i>0?s/i:0}}class cn{render(e,t,n,s){const{from:i,to:o}=n.visibleRange,l=n.barWidth,h=l/2,a=l+n.barSpacing,c=-n.offset+n.chartRect.x+h,{min:d,max:f}=n.priceRange,u=f-d;if(u===0)return;const g=n.chartRect.y,m=n.chartRect.height/u,y=M=>M*a+c,b=M=>g+(f-M)*m,p=new Path2D,T=new Path2D,v=new Path2D,S=new Path2D;for(let M=i;M<=o&&M<t.length;M++){const P=t[M],R=y(M),w=P.close>=P.open,k=b(Math.max(P.open,P.close)),C=b(Math.min(P.open,P.close)),I=Math.max(C-k,1),B=R-h;w?(p.rect(B,k,l,I),v.rect(B,k,l,I)):(T.rect(B,k,l,I),S.rect(B,k,l,I))}e.fillStyle=s.candleUp,e.fill(p),e.fillStyle=s.candleDown,e.fill(T),e.lineWidth=1,e.strokeStyle=s.candleUp,e.stroke(v),e.strokeStyle=s.candleDown,e.stroke(S)}}class dn{render(e,t,n,s){const{from:i,to:o}=n.visibleRange,l=n.barWidth,h=l/2,a=l+n.barSpacing,c=-n.offset+n.chartRect.x+h,{min:d,max:f}=n.priceRange,u=f-d;if(u===0)return;const g=n.chartRect.y,m=n.chartRect.height/u,y=v=>v*a+c,b=v=>g+(f-v)*m,p=new Path2D,T=new Path2D;for(let v=i;v<=o&&v<t.length;v++){const S=t[v],M=y(v),P=S.close>=S.open,R=b(S.high),w=b(S.low),k=P?p:T;if(k.moveTo(M,R),k.lineTo(M,w),v<o&&v+1<t.length){const C=y(v+1),I=P?w:R;k.moveTo(M,I),k.lineTo(C,I)}}e.strokeStyle=s.candleUp,e.lineWidth=3,e.stroke(p),e.strokeStyle=s.candleDown,e.lineWidth=1,e.stroke(T)}}class fn{boxSize=1;setBoxSize(e){this.boxSize=e}render(e,t,n,s){const{from:i,to:o}=n.visibleRange,l=n.barWidth,h=l/2,a=l+n.barSpacing,c=-n.offset+n.chartRect.x+h,{min:d,max:f}=n.priceRange,u=f-d;if(u===0)return;const g=n.chartRect.y,m=n.chartRect.height/u,y=P=>P*a+c,b=P=>g+(f-P)*m,p=Math.abs(this.boxSize*m),v=Math.min(h*.8,p*.8)*.5,S=new Path2D,M=new Path2D;for(let P=i;P<=o&&P<t.length;P++){const R=t[P],w=y(P),k=R.close>=R.open,C=R.high,I=R.low,B=Math.max(1,Math.round((C-I)/this.boxSize));for(let F=0;F<B;F++){const W=I+(F+.5)*this.boxSize,Y=b(W);k?(S.moveTo(w-v,Y-v),S.lineTo(w+v,Y+v),S.moveTo(w+v,Y-v),S.lineTo(w-v,Y+v)):(M.moveTo(w+v,Y),M.arc(w,Y,v,0,Math.PI*2))}}e.lineWidth=1.5,e.strokeStyle=s.candleUp,e.stroke(S),e.strokeStyle=s.candleDown,e.stroke(M)}}class un{render(e,t,n,s){const{from:i,to:o}=n.visibleRange,{min:l,max:h}=n.priceRange,a=h-l;if(a===0||i>o||t.length===0)return;const c=n.barWidth+n.barSpacing,d=-n.offset+n.chartRect.x+n.barWidth/2,f=n.chartRect.y,u=n.chartRect.height/a,g=n.barWidth;let m=0;for(let M=i;M<=o&&M<t.length;M++)t[M].volume>m&&(m=t[M].volume);m===0&&(m=1);const y=new Path2D,b=new Path2D,p=new Path2D,T=new Path2D,v=M=>M*c+d,S=M=>f+(h-M)*u;for(let M=i;M<=o&&M<t.length;M++){const P=t[M],R=v(M),w=S(P.high),k=S(P.low),C=S(P.open),I=S(P.close),B=P.close>=P.open,F=Math.max(.2,P.volume/m),W=g*F,Y=W/2,X=B?y:b;X.moveTo(R,w),X.lineTo(R,k);const N=B?I:C,ne=Math.max(Math.abs(I-C),1);(B?p:T).rect(R-Y,N,W,ne)}e.strokeStyle=s.candleUpWick,e.lineWidth=1,e.stroke(y),e.strokeStyle=s.candleDownWick,e.stroke(b),e.fillStyle=s.candleUp,e.fill(p),e.fillStyle=s.candleDown,e.fill(T)}}class gn{render(e,t,n,s){const{from:i,to:o}=n.visibleRange;if(i>o||t.length===0)return;const{min:l,max:h}=n.priceRange,a=h-l;if(a===0)return;const c=n.barWidth+n.barSpacing,d=-n.offset+n.chartRect.x+n.barWidth/2,f=n.chartRect.y,u=n.chartRect.height/a,g=T=>T*c+d,m=T=>f+(h-T)*u,y=[],b=[];for(let T=i;T<=o&&T<t.length;T++){const v=g(T);y.push({x:v,y:m(t[T].high)}),b.push({x:v,y:m(t[T].low)})}if(y.length===0)return;const p=e.createLinearGradient(0,f,0,f+n.chartRect.height);p.addColorStop(0,s.areaTopColor),p.addColorStop(1,s.areaBottomColor),e.beginPath(),e.moveTo(y[0].x,y[0].y);for(let T=1;T<y.length;T++)e.lineTo(y[T].x,y[T].y);for(let T=b.length-1;T>=0;T--)e.lineTo(b[T].x,b[T].y);e.closePath(),e.fillStyle=p,e.fill(),e.beginPath(),e.strokeStyle=s.lineColor,e.lineWidth=2,e.lineJoin="round";for(let T=i;T<=o&&T<t.length;T++){const v=g(T),S=m(t[T].close);T===i?e.moveTo(v,S):e.lineTo(v,S)}e.stroke()}}class mn{render(e,t,n,s){const{from:i,to:o}=n.visibleRange;if(i>o||t.length===0)return;const{min:l,max:h}=n.priceRange,a=h-l;if(a===0)return;const c=n.barWidth+n.barSpacing,d=-n.offset+n.chartRect.x+n.barWidth/2,f=n.chartRect.y,u=n.chartRect.height/a,g=b=>b*c+d,m=b=>f+(h-b)*u;e.beginPath(),e.strokeStyle=s.lineColor,e.lineWidth=2,e.lineJoin="miter";let y=0;for(let b=i;b<=o&&b<t.length;b++){const p=g(b),T=m(t[b].close);b===i?e.moveTo(p,T):(e.lineTo(p,y),e.lineTo(p,T)),y=T}e.stroke()}}class yn{render(e,t,n,s){const{from:i,to:o}=n.visibleRange;if(i>o||t.length===0)return;const{min:l,max:h}=n.priceRange,a=h-l;if(a===0)return;const c=n.barWidth+n.barSpacing,d=-n.offset+n.chartRect.x+n.barWidth/2,f=n.chartRect.y,u=n.chartRect.height/a,g=T=>T*c+d,m=T=>f+(h-T)*u;e.beginPath(),e.strokeStyle=s.lineColor,e.lineWidth=2,e.lineJoin="round";const y=[];for(let T=i;T<=o&&T<t.length;T++){const v=g(T),S=m(t[T].close);y.push({x:v,y:S}),T===i?e.moveTo(v,S):e.lineTo(v,S)}e.stroke();const b=Math.max(2,Math.min(4,n.barWidth*.3)),p=new Path2D;for(const{x:T,y:v}of y)p.moveTo(T+b,v),p.arc(T,v,b,0,Math.PI*2);e.fillStyle=s.lineColor,e.fill(p),e.strokeStyle=s.background,e.lineWidth=1,e.stroke(p)}}class pn{visible=!0;heightRatio=.2;setVisible(e){this.visible=e}setHeightRatio(e){this.heightRatio=Math.max(.05,Math.min(.5,e))}render(e,t,n,s){if(!this.visible||t.length===0)return;const{from:i,to:o}=n.visibleRange,{chartRect:l}=n,h=n.barWidth,a=h/2,c=h+n.barSpacing,d=-n.offset+l.x+a;let f=0;for(let p=i;p<=o&&p<t.length;p++)t[p].volume>f&&(f=t[p].volume);if(f===0)return;const u=l.height*this.heightRatio,g=l.y+l.height,m=u/f,y=new Path2D,b=new Path2D;for(let p=i;p<=o&&p<t.length;p++){const T=t[p],v=p*c+d,S=T.volume*m;(T.close>=T.open?y:b).rect(v-a,g-S,h,S)}e.globalAlpha=.35,e.fillStyle=s.candleUp,e.fill(y),e.fillStyle=s.candleDown,e.fill(b),e.globalAlpha=1}}class bn{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){const n=this.symbols.get(e);n&&(n.data=t)}setSymbolVisible(e,t){const n=this.symbols.get(e);n&&(n.visible=t)}getSymbols(){return Array.from(this.symbols.values())}clear(){this.symbols.clear()}render(e,t,n,s){if(this.symbols.size===0||t.length===0)return;const{chartRect:i}=n;e.save(),e.beginPath(),e.rect(i.x,i.y,i.width,i.height),e.clip();for(const o of this.symbols.values())!o.visible||o.data.length===0||this.renderSymbol(e,o,t,n,s);e.restore()}renderSymbol(e,t,n,s,i){const{chartRect:o}=s,l=s.barWidth+s.barSpacing,h=Math.max(0,Math.floor(s.offset/l));if(h>=n.length||h>=t.data.length)return;const a=n[h].close,c=t.data[h].close;if(a===0||c===0)return;e.strokeStyle=t.color,e.lineWidth=t.lineWidth??1.5,e.lineJoin="round",e.beginPath();let d=!1;const f=Math.min(n.length,t.data.length);for(let u=h;u<f;u++){const g=D(u,s);if(g<o.x-l)continue;if(g>o.x+o.width+l)break;let m;if(this.mode==="percent"){const y=(t.data[u].close-c)/c,b=a*(1+y);m=A(b,s)}else m=A(t.data[u].close,s);d?e.lineTo(g,m):(e.moveTo(g,m),d=!0)}if(e.stroke(),d){const u=Math.min(h+2,f-1),g=D(u,s)+4;let m;if(this.mode==="percent"){const y=(t.data[u].close-c)/c;m=A(a*(1+y),s)-4}else m=A(t.data[u].close,s)-4;e.fillStyle=t.color,e.font="bold 10px sans-serif",e.textBaseline="bottom",e.textAlign="left",e.fillText(t.label,g,m)}}}function Tn(r){if(r.length===0)return[];const e=[];let t=r[0].open,n=(r[0].open+r[0].high+r[0].low+r[0].close)/4;for(let s=0;s<r.length;s++){const i=r[s],o=(i.open+i.high+i.low+i.close)/4,l=s===0?i.open:(t+n)/2,h=Math.max(i.high,l,o),a=Math.min(i.low,l,o);e.push({time:i.time,open:l,high:h,low:a,close:o,volume:i.volume}),t=l,n=o}return e}function vn(r,e){if(r.length===0)return[];let t=e.brickSize;e.useATR&&r.length>(e.atrPeriod??14)&&(t=Mn(r,e.atrPeriod??14)),t<=0&&(t=1);const n=[];let s=Math.round(r[0].close/t)*t,i=r[0].time;for(let o=1;o<r.length;o++){const h=r[o].close-s,a=Math.floor(Math.abs(h)/t);if(a>=1){const c=h>0?1:-1;for(let d=0;d<a;d++){const f=s;s+=c*t,n.push({time:i++,open:f,high:Math.max(f,s),low:Math.min(f,s),close:s,volume:r[o].volume/a})}}}return n}function Mn(r,e){let t=0;for(let n=1;n<=Math.min(e,r.length-1);n++){const s=Math.max(r[n].high-r[n].low,Math.abs(r[n].high-r[n-1].close),Math.abs(r[n].low-r[n-1].close));t+=s}return t/e}function Sn(r,e=3){if(r.length===0)return[];const t=[];let n=0;t.push({time:n++,open:r[0].open,high:Math.max(r[0].open,r[0].close),low:Math.min(r[0].open,r[0].close),close:r[0].close,volume:r[0].volume});for(let s=1;s<r.length;s++){const i=r[s].close,o=t[t.length-1],l=o.close>=o.open,h=Math.min(e,t.length);let a=-1/0,c=1/0;for(let d=t.length-h;d<t.length;d++)a=Math.max(a,t[d].high),c=Math.min(c,t[d].low);l&&i>o.close?t.push({time:n++,open:o.close,high:i,low:o.close,close:i,volume:r[s].volume}):!l&&i<o.close?t.push({time:n++,open:o.close,high:o.close,low:i,close:i,volume:r[s].volume}):l&&i<c?t.push({time:n++,open:o.close,high:o.close,low:i,close:i,volume:r[s].volume}):!l&&i>a&&t.push({time:n++,open:o.close,high:i,low:o.close,close:i,volume:r[s].volume})}return t}function Pn(r,e=4){if(r.length<2)return r.slice();const t=[];let n=r[1].close>=r[0].close?1:-1,s=r[0].close,i=r[0].close,o=r[0].close,l=0;for(let h=1;h<r.length;h++){const a=r[h].close,c=o*(e/100);n===1?(a>s&&(s=a),s-a>=c&&(t.push({time:l++,open:o,high:s,low:o,close:s,volume:r[h].volume}),n=-1,o=s,i=a,s=a)):(a<i&&(i=a),a-i>=c&&(t.push({time:l++,open:o,high:o,low:i,close:i,volume:r[h].volume}),n=1,o=i,s=a,i=a))}return n===1?t.push({time:l,open:o,high:s,low:o,close:s,volume:0}):t.push({time:l,open:o,high:o,low:i,close:i,volume:0}),t}function Rn(r,e,t=3){if(r.length===0||e<=0)return[];const n=[];let s=0,i=0,o=0,l=0;const h=Math.round(r[0].close/e)*e;i=h,o=h,s=1;for(let a=1;a<r.length;a++){const c=r[a].close,d=Math.round(c/e)*e;s===1?d>i?i=d:i-d>=e*t&&(n.push({time:l++,open:o,high:i,low:o,close:i,volume:r[a].volume}),s=-1,o=d,i=i-e):d<o?o=d:d-o>=e*t&&(n.push({time:l++,open:i,high:i,low:o,close:o,volume:r[a].volume}),s=1,i=d,o=o+e)}return n.push({time:l,open:s===1?o:i,high:i,low:o,close:s===1?i:o,volume:0}),n}function kn(r,e){if(r.length===0||e.rangeSize<=0)return[];const t=[];let n=r[0].time,s=r[0].open,i=s,o=s,l=0;const h=(a,c)=>{for(a>i&&(i=a),a<o&&(o=a),l+=c;i-o>=e.rangeSize;){const d=a>=s,f=d?s+e.rangeSize:s-e.rangeSize;t.push({time:n++,open:s,high:d?f:s,low:d?s:f,close:f,volume:l}),s=f,i=f,o=f,l=0}};for(const a of r){const d=a.close>=a.open?[a.low,a.high,a.close]:[a.high,a.low,a.close],f=a.volume/d.length;for(const u of d)h(u,f)}return t}class H{drawLine(e,t,n,s){if(!(t.length<2)){e.beginPath(),e.strokeStyle=n,e.lineWidth=s,e.lineJoin="round",e.moveTo(t[0].x,t[0].y);for(let i=1;i<t.length;i++)e.lineTo(t[i].x,t[i].y);e.stroke()}}drawBand(e,t,n,s){if(!(t.length<2||n.length<2)){e.beginPath(),e.moveTo(t[0].x,t[0].y);for(let i=1;i<t.length;i++)e.lineTo(t[i].x,t[i].y);for(let i=n.length-1;i>=0;i--)e.lineTo(n[i].x,n[i].y);e.closePath(),e.fillStyle=s,e.fill()}}drawHistogram(e,t,n){const s=n/2;for(const i of t){e.fillStyle=i.color;const o=Math.min(i.y,i.baseY),l=Math.abs(i.y-i.baseY);e.fillRect(i.x-s,o,n,Math.max(l,1))}}valuesToPoints(e,t,n,s){const i=[],{from:o,to:l}=s.visibleRange;for(let h=o;h<=l&&h<n.length;h++){const a=e.values.get(n[h].time);a&&a[t]!==void 0&&i.push({x:D(h,s),y:A(a[t],s)})}return i}}let wn=1;class An{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){const s=this.registry.get(e);if(!s)throw new Error(`Unknown indicator: ${e}`);const i=`tc_${e}_${wn++}`,o={id:e,instanceId:i,params:{...s.descriptor.defaultConfig,...t},visible:!0},l=["#2196F3","#FF9800","#4CAF50","#E91E63","#9C27B0"],h={colors:o.style?.colors??l,lineWidths:o.style?.lineWidths??[1.5],opacity:o.style?.opacity??1},a={plugin:s,config:o,output:null,style:h};return n&&(a.output=s.calculate(n,o)),this.instances.set(i,a),i}removeIndicator(e){this.instances.delete(e)}updateIndicator(e,t,n){const s=this.instances.get(e);s&&(Object.assign(s.config.params,t),n&&(s.output=s.plugin.calculate(n,s.config)))}recalculateAll(e){for(const 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(const 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){const s=this.instances.get(t);!s?.output||!s.config.visible||s.plugin.render(e,s.output,n,s.style)}getIndicatorConfig(e){return this.instances.get(e)?.config??null}getIndicatorDescriptor(e){return this.instances.get(e)?.plugin.descriptor??null}getActiveIndicators(){const e=[];for(const[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){const n=this.instances.get(e);n&&Object.assign(n.style,t)}getOverlayPriceRange(e,t){let n=1/0,s=-1/0;for(const i of this.instances.values()){if(!i.output||!i.config.visible||i.plugin.descriptor.placement!=="overlay")continue;let o=0;for(const[,l]of i.output.values){if(o>=e&&o<=t)for(const h in l){const a=l[h];a!==void 0&&isFinite(a)&&(a<n&&(n=a),a>s&&(s=a))}o++}}return n===1/0?null:{min:n,max:s}}getPanelIndicators(){const e=[];for(const[t,n]of this.instances)n.plugin.descriptor.placement==="panel"&&e.push({instanceId:t,descriptor:n.plugin.descriptor});return e}}function q(r,e,t){const n=r.params[e];if(typeof n=="number"&&Number.isFinite(n))return n;if(typeof n=="string"){const s=Number(n);if(Number.isFinite(s))return s}return t}function E(r,e,t,n=1){const s=q(r,e,t);return Math.max(n,Math.floor(s))}class Ue extends H{descriptor={id:"sma",name:"Simple Moving Average",placement:"overlay",defaultConfig:{period:20}};calculate(e,t){const n=E(t,"period",20,1),s=new Map,i=new Array(e.length).fill(null);let o=0;for(let l=0;l<e.length;l++)if(o+=e[l].close,l>=n&&(o-=e[l-n].close),l>=n-1){const h={value:o/n};s.set(e[l].time,h),i[l]=h}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange;e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let h=!1;for(let a=o;a<=l&&a<i.length;a++){const c=i[a];if(!c||c.value===void 0)continue;const d=D(a,n),f=A(c.value,n);h?e.lineTo(d,f):(e.moveTo(d,f),h=!0)}e.stroke()}}class xe extends H{descriptor={id:"ema",name:"Exponential Moving Average",placement:"overlay",defaultConfig:{period:20}};calculate(e,t){const n=E(t,"period",20,1),s=2/(n+1),i=new Map,o=new Array(e.length).fill(null);let l=0;for(let h=0;h<e.length;h++){if(h<n-1){l+=e[h].close;continue}h===n-1?l=(l+e[h].close)/n:l=(e[h].close-l)*s+l;const a={value:l};i.set(e[h].time,a),o[h]=a}return{values:i,series:o}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange;e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let h=!1;for(let a=o;a<=l&&a<i.length;a++){const c=i[a];if(!c||c.value===void 0)continue;const d=D(a,n),f=A(c.value,n);h?e.lineTo(d,f):(e.moveTo(d,f),h=!0)}e.stroke()}}class Oe extends H{descriptor={id:"bb",name:"Bollinger Bands",placement:"overlay",defaultConfig:{period:20,stdDev:2}};calculate(e,t){const n=E(t,"period",20,1),s=q(t,"stdDev",2),i=new Map,o=new Array(e.length).fill(null);let l=0,h=0;for(let a=0;a<e.length;a++){const c=e[a].close;if(l+=c,h+=c*c,a>=n){const d=e[a-n].close;l-=d,h-=d*d}if(a>=n-1){const d=l/n,f=h/n-d*d,u=Math.sqrt(Math.max(0,f)),g={middle:d,upper:d+s*u,lower:d-s*u};i.set(e[a].time,g),o[a]=g}}return{values:i,series:o}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange;let h=0;const a=l-o+1,c=new Float64Array(a),d=new Float64Array(a),f=new Float64Array(a),u=new Float64Array(a),g=new Float64Array(a),m=new Float64Array(a);for(let y=o;y<=l&&y<i.length;y++){const b=i[y];if(!b||b.upper===void 0)continue;const p=D(y,n);c[h]=p,d[h]=A(b.upper,n),f[h]=p,u[h]=A(b.middle,n),g[h]=p,m[h]=A(b.lower,n),h++}if(!(h<2)){e.beginPath(),e.moveTo(c[0],d[0]);for(let y=1;y<h;y++)e.lineTo(c[y],d[y]);for(let y=h-1;y>=0;y--)e.lineTo(g[y],m[y]);e.closePath(),e.fillStyle=L.withAlpha(s.colors[0],.1),e.fill(),e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round",e.moveTo(c[0],d[0]);for(let y=1;y<h;y++)e.lineTo(c[y],d[y]);e.stroke(),e.beginPath(),e.strokeStyle=s.colors[1]??s.colors[0],e.moveTo(f[0],u[0]);for(let y=1;y<h;y++)e.lineTo(f[y],u[y]);e.stroke(),e.beginPath(),e.strokeStyle=s.colors[0],e.moveTo(g[0],m[0]);for(let y=1;y<h;y++)e.lineTo(g[y],m[y]);e.stroke()}}}class qe extends H{descriptor={id:"vwap",name:"Volume Weighted Average Price",placement:"overlay",defaultConfig:{}};calculate(e,t){const n=new Map,s=new Array(e.length).fill(null);let i=0,o=0;for(let l=0;l<e.length;l++){const h=(e[l].high+e[l].low+e[l].close)/3;if(i+=e[l].volume,o+=h*e[l].volume,i>0){const a={value:o/i};n.set(e[l].time,a),s[l]=a}}return{values:n,series:s}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange;e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let h=!1;for(let a=o;a<=l&&a<i.length;a++){const c=i[a];if(!c||c.value===void 0)continue;const d=D(a,n),f=A(c.value,n);h?e.lineTo(d,f):(e.moveTo(d,f),h=!0)}e.stroke()}}class ze extends H{descriptor={id:"ichimoku",name:"Ichimoku Cloud",placement:"overlay",defaultConfig:{tenkan:9,kijun:26,senkou:52,displacement:26}};calculate(e,t){const n=E(t,"tenkan",9,1),s=E(t,"kijun",26,1),i=E(t,"senkou",52,1),o=new Map,l=new Array(e.length).fill(null),h=f=>{const u=new Float64Array(e.length),g=new Float64Array(e.length);for(let m=0;m<e.length;m++){let y=-1/0,b=1/0;const p=Math.max(0,m-f+1);for(let T=p;T<=m;T++)e[T].high>y&&(y=e[T].high),e[T].low<b&&(b=e[T].low);u[m]=y,g[m]=b}return{highs:u,lows:g}},a=h(n),c=h(s),d=h(i);for(let f=0;f<e.length;f++){const u={};f>=n-1&&(u.tenkan=(a.highs[f]+a.lows[f])/2),f>=s-1&&(u.kijun=(c.highs[f]+c.lows[f])/2),u.tenkan!==void 0&&u.kijun!==void 0&&(u.senkouA=(u.tenkan+u.kijun)/2),f>=i-1&&(u.senkouB=(d.highs[f]+d.lows[f])/2),f>=s-1&&(u.chikou=e[f].close),(u.tenkan!==void 0||u.kijun!==void 0||u.senkouA!==void 0||u.senkouB!==void 0)&&(o.set(e[f].time,u),l[f]=u)}return{values:o,series:l}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange,h=l-o+1,a=new Float64Array(h),c=new Float64Array(h);let d=0;const f=new Float64Array(h),u=new Float64Array(h);let g=0;const m=new Float64Array(h),y=new Float64Array(h);let b=0;const p=new Float64Array(h),T=new Float64Array(h);let v=0;for(let M=o;M<=l&&M<i.length;M++){const P=i[M];if(!P)continue;const R=D(M,n);P.tenkan!==void 0&&(a[d]=R,c[d]=A(P.tenkan,n),d++),P.kijun!==void 0&&(f[g]=R,u[g]=A(P.kijun,n),g++),P.senkouA!==void 0&&(m[b]=R,y[b]=A(P.senkouA,n),b++),P.senkouB!==void 0&&(p[v]=R,T[v]=A(P.senkouB,n),v++)}if(b>1&&v>1){const M=Math.min(b,v);e.beginPath(),e.moveTo(m[0],y[0]);for(let P=1;P<M;P++)e.lineTo(m[P],y[P]);for(let P=M-1;P>=0;P--)e.lineTo(p[P],T[P]);e.closePath(),e.fillStyle=L.withAlpha(s.colors[2]??"#26A69A",.15),e.fill()}const S=(M,P,R,w,k)=>{if(!(R<2)){e.beginPath(),e.strokeStyle=w,e.lineWidth=k,e.lineJoin="round",e.moveTo(M[0],P[0]);for(let C=1;C<R;C++)e.lineTo(M[C],P[C]);e.stroke()}};S(a,c,d,s.colors[0],s.lineWidths[0]),S(f,u,g,s.colors[1]??"#FF9800",s.lineWidths[0]),S(m,y,b,s.colors[2]??"#26A69A",1),S(p,T,v,s.colors[3]??"#EF5350",1)}}class Xe extends H{descriptor={id:"psar",name:"Parabolic SAR",placement:"overlay",defaultConfig:{step:.02,max:.2}};calculate(e,t){const n=q(t,"step",.02),s=q(t,"max",.2),i=new Map,o=new Array(e.length).fill(null);if(e.length<2)return{values:i,series:o};let l=e[1].close>e[0].close,h=n,a=l?e[0].high:e[0].low,c=l?e[0].low:e[0].high;const d={value:c,trend:l?1:-1};i.set(e[0].time,d),o[0]=d;for(let f=1;f<e.length;f++){const u=c;c=u+h*(a-u),l?(c=Math.min(c,e[f-1].low),f>=2&&(c=Math.min(c,e[f-2].low)),e[f].low<c?(l=!1,c=a,a=e[f].low,h=n):e[f].high>a&&(a=e[f].high,h=Math.min(h+n,s))):(c=Math.max(c,e[f-1].high),f>=2&&(c=Math.max(c,e[f-2].high)),e[f].high>c?(l=!0,c=a,a=e[f].high,h=n):e[f].low<a&&(a=e[f].low,h=Math.min(h+n,s)));const g={value:c,trend:l?1:-1};i.set(e[f].time,g),o[f]=g}return{values:i,series:o}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange,h=Math.max(1.5,n.barWidth*.15);for(let a=o;a<=l&&a<i.length;a++){const c=i[a];if(!c||c.value===void 0)continue;const d=D(a,n),f=A(c.value,n);e.beginPath(),e.arc(d,f,h,0,Math.PI*2),e.fillStyle=c.trend===1?s.colors[0]:s.colors[1]??"#EF5350",e.fill()}}}class _e extends H{descriptor={id:"supertrend",name:"Supertrend",placement:"overlay",defaultConfig:{period:10,multiplier:3}};calculate(e,t){const n=E(t,"period",10,1),s=q(t,"multiplier",3),i=new Map,o=new Array(e.length).fill(null);if(e.length<n)return{values:i,series:o};const l=[e[0].high-e[0].low];for(let y=1;y<e.length;y++)l.push(Math.max(e[y].high-e[y].low,Math.abs(e[y].high-e[y-1].close),Math.abs(e[y].low-e[y-1].close)));let h=0;for(let y=0;y<n;y++)h+=l[y];h/=n;let a=0,c=0,d=0,f=0,u=0,g=0,m=1;for(let y=n-1;y<e.length;y++){y>n-1&&(h=(h*(n-1)+l[y])/n);const b=(e[y].high+e[y].low)/2,p=b+s*h,T=b-s*h;a=p<d||e[y-1].close>d?p:d,c=T>f||e[y-1].close<f?T:f,g===d?u=e[y].close<=a?a:c:u=e[y].close>=c?c:a,m=u===c?1:-1;const v={value:u,trend:m};i.set(e[y].time,v),o[y]=v,d=a,f=c,g=u}return{values:i,series:o}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange,h=s.colors[0],a=s.colors[1]??"#EF5350";let c=0,d=0,f=0;for(let u=o;u<=l&&u<i.length;u++){const g=i[u];if(!g||g.value===void 0)continue;const m=D(u,n),y=A(g.value,n),b=g.trend??1;f!==0&&(e.beginPath(),e.strokeStyle=b===1?h:a,e.lineWidth=s.lineWidths[0]+.5,e.moveTo(c,d),e.lineTo(m,y),e.stroke()),c=m,d=y,f=b}}}class je extends H{descriptor={id:"keltner",name:"Keltner Channel",placement:"overlay",defaultConfig:{emaPeriod:20,atrPeriod:10,multiplier:1.5}};calculate(e,t){const n=E(t,"emaPeriod",20,1),s=E(t,"atrPeriod",10,1),i=q(t,"multiplier",1.5),o=new Map;if(e.length<Math.max(n,s))return{values:o};const l=2/(n+1);let h=e[0].close;const a=[h];for(let g=1;g<e.length;g++)h=(e[g].close-h)*l+h,a.push(h);const c=[e[0].high-e[0].low];for(let g=1;g<e.length;g++)c.push(Math.max(e[g].high-e[g].low,Math.abs(e[g].high-e[g-1].close),Math.abs(e[g].low-e[g-1].close)));let d=0;for(let g=0;g<s;g++)d+=c[g];d/=s;const f=new Array(e.length).fill(null),u=Math.max(n,s)-1;for(let g=u;g<e.length;g++){g>u&&(d=(d*(s-1)+c[g])/s);const m={middle:a[g],upper:a[g]+i*d,lower:a[g]-i*d};o.set(e[g].time,m),f[g]=m}return{values:o,series:f}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange,h=[],a=[],c=[];for(let d=o;d<=l&&d<i.length;d++){const f=i[d];if(!f)continue;const u=D(d,n);f.upper!==void 0&&h.push({x:u,y:A(f.upper,n)}),f.middle!==void 0&&a.push({x:u,y:A(f.middle,n)}),f.lower!==void 0&&c.push({x:u,y:A(f.lower,n)})}this.drawBand(e,h,c,L.withAlpha(s.colors[0],.08)),this.drawLine(e,h,s.colors[0],s.lineWidths[0]),this.drawLine(e,a,s.colors[1]??s.colors[0],s.lineWidths[0]),this.drawLine(e,c,s.colors[0],s.lineWidths[0])}}class Ge extends H{descriptor={id:"donchian",name:"Donchian Channel",placement:"overlay",defaultConfig:{period:20}};calculate(e,t){const n=E(t,"period",20,1),s=new Map,i=new Array(e.length).fill(null);for(let o=n-1;o<e.length;o++){let l=-1/0,h=1/0;for(let c=o-n+1;c<=o;c++)e[c].high>l&&(l=e[c].high),e[c].low<h&&(h=e[c].low);const a={upper:l,middle:(l+h)/2,lower:h};s.set(e[o].time,a),i[o]=a}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange,h=[],a=[],c=[];for(let d=o;d<=l&&d<i.length;d++){const f=i[d];if(!f)continue;const u=D(d,n);f.upper!==void 0&&h.push({x:u,y:A(f.upper,n)}),f.middle!==void 0&&a.push({x:u,y:A(f.middle,n)}),f.lower!==void 0&&c.push({x:u,y:A(f.lower,n)})}this.drawBand(e,h,c,L.withAlpha(s.colors[0],.08)),this.drawLine(e,h,s.colors[0],s.lineWidths[0]),this.drawLine(e,a,s.colors[1]??"#787B86",1),this.drawLine(e,c,s.colors[0],s.lineWidths[0])}}const Ln=["s3","s2","s1","pp","r1","r2","r3"];class Ne extends H{descriptor={id:"pivots",name:"Pivot Points (Classic)",placement:"overlay",defaultConfig:{lookback:24}};calculate(e,t){const n=E(t,"lookback",24,1),s=new Map,i=new Array(e.length).fill(null);if(e.length<=n)return{values:s,series:i};for(let o=n;o<e.length;o++){let l=-1/0,h=1/0;for(let u=o-n;u<o;u++)e[u].high>l&&(l=e[u].high),e[u].low<h&&(h=e[u].low);const a=e[o-1].close,c=(l+h+a)/3,d=l-h,f={pp:c,r1:2*c-h,s1:2*c-l,r2:c+d,s2:c-d,r3:l+2*(c-h),s3:h-2*(l-c)};s.set(e[o].time,f),i[o]=f}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange,h=s.colors[0],a=s.colors[1]??"#EF5350",c=s.colors[2]??"#26A69A",d=s.lineWidths[0],f=u=>u==="pp"?h:u.startsWith("r")?a:c;for(const u of Ln){e.beginPath(),e.strokeStyle=f(u),e.lineWidth=u==="pp"?d:Math.max(1,d-1),u!=="pp"&&e.setLineDash([4,3]);let g=!1;for(let m=o;m<=l&&m<i.length;m++){const b=i[m]?.[u];if(b===void 0)continue;const p=D(m,n),T=A(b,n);g?e.lineTo(p,T):(e.moveTo(p,T),g=!0)}e.stroke(),e.setLineDash([])}}}class Je extends H{descriptor={id:"avwap",name:"Anchored VWAP",placement:"overlay",defaultConfig:{anchorTime:0}};calculate(e,t){const n=q(t,"anchorTime",0),s=new Map,i=new Array(e.length).fill(null);let o=0;if(n>0&&(o=e.findIndex(a=>a.time>=n),o<0))return{values:s,series:i};let l=0,h=0;for(let a=o;a<e.length;a++){const c=(e[a].high+e[a].low+e[a].close)/3;if(l+=e[a].volume,h+=c*e[a].volume,l>0){const d={value:h/l};s.set(e[a].time,d),i[a]=d}}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange;e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let h=!1;for(let a=o;a<=l&&a<i.length;a++){const c=i[a];if(!c||c.value===void 0)continue;const d=D(a,n),f=A(c.value,n);h?e.lineTo(d,f):(e.moveTo(d,f),h=!0)}e.stroke()}}class Ke extends H{descriptor={id:"zigzag",name:"ZigZag",placement:"overlay",defaultConfig:{deviation:5}};calculate(e,t){const n=Math.max(.01,q(t,"deviation",5))/100,s=new Map,i=new Array(e.length).fill(null);if(e.length<2)return{values:s,series:i,meta:{pivots:[]}};const o=[];let l=null,h=0,a=e[0].close;for(let c=1;c<e.length;c++){const d=e[c].high,f=e[c].low;if(l===null){d>=a*(1+n)?(o.push({idx:h,price:a,type:"low"}),l="up",h=c,a=d):f<=a*(1-n)&&(o.push({idx:h,price:a,type:"high"}),l="down",h=c,a=f);continue}l==="up"?d>a?(h=c,a=d):f<=a*(1-n)&&(o.push({idx:h,price:a,type:"high"}),l="down",h=c,a=f):f<a?(h=c,a=f):d>=a*(1+n)&&(o.push({idx:h,price:a,type:"low"}),l="up",h=c,a=d)}o.push({idx:h,price:a,type:l==="up"?"high":"low"});for(const c of o){const d={pivot:c.price};s.set(e[c.idx].time,d),i[c.idx]=d}return{values:s,series:i,meta:{pivots:o}}}render(e,t,n,s){const i=t.meta?.pivots;if(!(!i||i.length<2)){e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";for(let o=0;o<i.length;o++){const l=i[o],h=D(l.idx,n),a=A(l.price,n);o===0?e.moveTo(h,a):e.lineTo(h,a)}e.stroke()}}}class Ze extends H{descriptor={id:"lrc",name:"Linear Regression Channel",placement:"overlay",defaultConfig:{period:100,stdDev:2}};calculate(e,t){const n=E(t,"period",100,2),s=q(t,"stdDev",2),i=new Map,o=new Array(e.length).fill(null);if(e.length<n)return{values:i,series:o};for(let l=n-1;l<e.length;l++){const h=l-n+1;let a=0,c=0,d=0,f=0;for(let v=0;v<n;v++){const S=v,M=e[h+v].close;a+=S,c+=M,d+=S*M,f+=S*S}const u=n*f-a*a;if(u===0)continue;const g=(n*d-a*c)/u,m=(c-g*a)/n;let y=0;for(let v=0;v<n;v++){const S=g*v+m,M=e[h+v].close-S;y+=M*M}const b=Math.sqrt(y/n),p=g*(n-1)+m,T={middle:p,upper:p+s*b,lower:p-s*b,slope:g};i.set(e[l].time,T),o[l]=T}return{values:i,series:o}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange,h=[],a=[],c=[];for(let d=o;d<=l&&d<i.length;d++){const f=i[d];if(!f)continue;const u=D(d,n);f.middle!==void 0&&a.push({x:u,y:A(f.middle,n)}),f.upper!==void 0&&h.push({x:u,y:A(f.upper,n)}),f.lower!==void 0&&c.push({x:u,y:A(f.lower,n)})}this.drawBand(e,h,c,L.withAlpha(s.colors[0],.08)),this.drawLine(e,h,s.colors[0],s.lineWidths[0]),this.drawLine(e,a,s.colors[1]??s.colors[0],s.lineWidths[0]),this.drawLine(e,c,s.colors[0],s.lineWidths[0])}}class Qe extends H{descriptor={id:"hma",name:"Hull Moving Average",placement:"overlay",defaultConfig:{period:21}};calculate(e,t){const n=E(t,"period",21,2),s=new Map,i=new Array(e.length).fill(null);if(e.length<n)return{values:s,series:i};const o=e.map(u=>u.close),l=Math.max(2,Math.floor(n/2)),h=Math.max(2,Math.floor(Math.sqrt(n))),a=de(o,n),c=de(o,l),d=o.map((u,g)=>a[g]===null||c[g]===null?null:2*c[g]-a[g]),f=Cn(d,h);for(let u=0;u<e.length;u++){if(f[u]===null)continue;const g={value:f[u]};s.set(e[u].time,g),i[u]=g}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{from:o,to:l}=n.visibleRange;e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let h=!1;for(let a=o;a<=l&&a<i.length;a++){const c=i[a];if(!c||c.value===void 0)continue;const d=D(a,n),f=A(c.value,n);h?e.lineTo(d,f):(e.moveTo(d,f),h=!0)}e.stroke()}}function de(r,e){const t=new Array(r.length).fill(null);if(r.length<e)return t;const n=e*(e+1)/2;for(let s=e-1;s<r.length;s++){let i=0;for(let o=0;o<e;o++)i+=r[s-o]*(e-o);t[s]=i/n}return t}function Cn(r,e){const t=new Array(r.length).fill(null),n=e*(e+1)/2;for(let s=e-1;s<r.length;s++){let i=0,o=!0;for(let l=0;l<e;l++){const h=r[s-l];if(h===null){o=!1;break}i+=h*(e-l)}o&&(t[s]=i/n)}return t}class et extends H{descriptor={id:"ao",name:"Awesome Oscillator",placement:"panel",defaultConfig:{fast:5,slow:34}};calculate(e,t){const n=E(t,"fast",5,1),s=E(t,"slow",34,1),i=new Map,o=new Array(e.length).fill(null);if(e.length<s)return{values:i,series:o};const l=new Array(e.length);for(let d=0;d<e.length;d++)l[d]=(e[d].high+e[d].low)/2;let h=0,a=0,c;for(let d=0;d<e.length;d++)if(a+=l[d],d>=s&&(a-=l[d-s]),h+=l[d],d>=n&&(h-=l[d-n]),d>=s-1){const f=h/n-a/s,u=c===void 0?!0:f>=c,g={value:f,up:u?1:0};i.set(e[d].time,g),o[d]=g,c=f}return{values:i,series:o}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let b=l;b<=h&&b<i.length;b++){const p=i[b]?.value;p!==void 0&&(p<a&&(a=p),p>c&&(c=p))}if(a===1/0)return;const d=Math.max(Math.abs(a),Math.abs(c))||1,f=b=>o.y+o.height*(1-(b+d)/(2*d)),u=f(0),g=n.barWidth/2,m=s.colors[0]??"#26A69A",y=s.colors[1]??"#EF5350";for(let b=l;b<=h&&b<i.length;b++){const p=i[b];if(!p||p.value===void 0)continue;const T=D(b,n),v=f(p.value),S=Math.min(v,u),M=Math.max(Math.abs(v-u),1);e.fillStyle=p.up?m:y,e.fillRect(T-g,S,n.barWidth,M)}}}class tt extends H{descriptor={id:"chaikinOsc",name:"Chaikin Oscillator",placement:"panel",defaultConfig:{fast:3,slow:10}};calculate(e,t){const n=E(t,"fast",3,1),s=E(t,"slow",10,1),i=new Map,o=new Array(e.length).fill(null);if(e.length<s)return{values:i,series:o};const l=2/(n+1),h=2/(s+1);let a=0,c=0,d=0;for(let f=0;f<e.length;f++){const{high:u,low:g,close:m,volume:y}=e[f],b=u-g,p=b===0?0:(m-g-(u-m))/b;if(a+=p*y,f===0?(c=a,d=a):(c=(a-c)*l+c,d=(a-d)*h+d),f>=s-1){const v={value:c-d,adl:a};i.set(e[f].time,v),o[f]=v}}return{values:i,series:o}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let b=l;b<=h&&b<i.length;b++){const p=i[b]?.value;p!==void 0&&(p<a&&(a=p),p>c&&(c=p))}if(a===1/0)return;const d=Math.max(Math.abs(a),Math.abs(c))||1,f=b=>o.y+o.height*(1-(b+d)/(2*d)),u=f(0),g=n.barWidth/2,m=s.colors[0]??"#26A69A",y=s.colors[1]??"#EF5350";for(let b=l;b<=h&&b<i.length;b++){const p=i[b];if(!p||p.value===void 0)continue;const T=D(b,n),v=f(p.value),S=Math.min(v,u),M=Math.max(Math.abs(v-u),1);e.fillStyle=p.value>=0?m:y,e.fillRect(T-g,S,n.barWidth,M)}}}class nt extends H{descriptor={id:"rsi",name:"Relative Strength Index",placement:"panel",defaultConfig:{period:14}};calculate(e,t){const n=E(t,"period",14,1),s=new Map,i=new Array(e.length).fill(null);if(e.length<n+1)return{values:s,series:i};let o=0,l=0;for(let c=1;c<=n;c++){const d=e[c].close-e[c-1].close;d>0?o+=d:l-=d}o/=n,l/=n;const a={value:l===0?100:100-100/(1+o/l)};s.set(e[n].time,a),i[n]=a;for(let c=n+1;c<e.length;c++){const d=e[c].close-e[c-1].close,f=d>0?d:0,u=d<0?-d:0;o=(o*(n-1)+f)/n,l=(l*(n-1)+u)/n;const m={value:l===0?100:100-100/(1+o/l)};s.set(e[c].time,m),i[c]=m}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange,a=d=>o.y+o.height*(1-d/100);e.strokeStyle=s.colors[1]??"#787B86",e.lineWidth=1,e.setLineDash([4,4]),e.beginPath();for(const d of[30,70]){const f=a(d);e.moveTo(o.x,f),e.lineTo(o.x+o.width,f)}e.stroke(),e.setLineDash([]),e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let c=!1;for(let d=l;d<=h&&d<i.length;d++){const f=i[d];if(!f||f.value===void 0)continue;const u=D(d,n),g=a(f.value);c?e.lineTo(u,g):(e.moveTo(u,g),c=!0)}e.stroke()}}class it extends H{descriptor={id:"macd",name:"MACD",placement:"panel",defaultConfig:{fast:12,slow:26,signal:9}};calculate(e,t){const n=E(t,"fast",12,1),s=E(t,"slow",26,1),i=E(t,"signal",9,1),o=new Map,l=new Array(e.length).fill(null);if(e.length<s)return{values:o,series:l};const h=2/(n+1),a=2/(s+1),c=2/(i+1);let d=e[0].close,f=e[0].close,u=0,g=!1;for(let m=0;m<e.length;m++){const y=e[m].close;if(m===0?(d=y,f=y):(d=(y-d)*h+d,f=(y-f)*a+f),m>=s-1){const b=d-f;g?u=(b-u)*c+u:(u=b,g=!0);const p=b-u,T={macd:b,signal:u,histogram:p};o.set(e[m].time,T),l[m]=T}}return{values:o,series:l}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let w=l;w<=h&&w<i.length;w++){const k=i[w];k&&(k.macd!==void 0&&(k.macd<a&&(a=k.macd),k.macd>c&&(c=k.macd)),k.signal!==void 0&&(k.signal<a&&(a=k.signal),k.signal>c&&(c=k.signal)),k.histogram!==void 0&&(k.histogram<a&&(a=k.histogram),k.histogram>c&&(c=k.histogram)))}if(a===1/0)return;const d=c-a||1,f=w=>o.y+o.height*(1-(w-a)/d),u=f(0),g=n.barWidth/2,m=s.colors[2]??"#26A69A",y=s.colors[3]??"#EF5350",b=s.colors[0],p=s.colors[1]??"#FF9800";e.beginPath();const T=new Path2D,v=new Path2D;let S=!1,M=!1;const P=new Path2D,R=new Path2D;for(let w=l;w<=h&&w<i.length;w++){const k=i[w];if(!k)continue;const C=D(w,n);if(k.histogram!==void 0){const I=f(k.histogram),B=Math.min(I,u),F=Math.max(Math.abs(I-u),1);k.histogram>=0?T.rect(C-g,B,n.barWidth,F):v.rect(C-g,B,n.barWidth,F)}if(k.macd!==void 0){const I=f(k.macd);S?P.lineTo(C,I):(P.moveTo(C,I),S=!0)}if(k.signal!==void 0){const I=f(k.signal);M?R.lineTo(C,I):(R.moveTo(C,I),M=!0)}}e.fillStyle=m,e.fill(T),e.fillStyle=y,e.fill(v),e.strokeStyle=b,e.lineWidth=s.lineWidths[0],e.lineJoin="round",e.stroke(P),e.strokeStyle=p,e.stroke(R)}}class st extends H{descriptor={id:"stochastic",name:"Stochastic Oscillator",placement:"panel",defaultConfig:{kPeriod:14,dPeriod:3,smooth:3}};calculate(e,t){const n=E(t,"kPeriod",14,1),s=E(t,"dPeriod",3,1),i=E(t,"smooth",3,1),o=new Map,l=new Array(e.length).fill(null);if(e.length<n)return{values:o,series:l};const h=[];for(let d=0;d<e.length;d++){if(d<n-1){h.push(0);continue}let f=-1/0,u=1/0;for(let g=d-n+1;g<=d;g++)e[g].high>f&&(f=e[g].high),e[g].low<u&&(u=e[g].low);h.push(f===u?50:(e[d].close-u)/(f-u)*100)}const a=[];for(let d=0;d<h.length;d++){if(d<n-1+i-1){a.push(0);continue}let f=0;for(let u=d-i+1;u<=d;u++)f+=h[u];a.push(f/i)}const c=n-1+i-1;for(let d=c;d<a.length;d++){const f={k:a[d]};if(d>=c+s-1){let u=0;for(let g=d-s+1;g<=d;g++)u+=a[g];f.d=u/s}o.set(e[d].time,f),l[d]=f}return{values:o,series:l}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange,a=d=>o.y+o.height*(1-d/100);e.strokeStyle=s.colors[2]??"#787B86",e.lineWidth=1,e.setLineDash([4,4]);for(const d of[20,80]){const f=a(d);e.beginPath(),e.moveTo(o.x,f),e.lineTo(o.x+o.width,f),e.stroke()}e.setLineDash([]);const c=(d,f)=>{e.beginPath(),e.strokeStyle=f,e.lineWidth=s.lineWidths[0],e.lineJoin="round";let u=!1;for(let g=l;g<=h&&g<i.length;g++){const m=i[g];if(!m||m[d]===void 0)continue;const y=D(g,n),b=a(m[d]);u?e.lineTo(y,b):(e.moveTo(y,b),u=!0)}e.stroke()};c("k",s.colors[0]),c("d",s.colors[1]??"#FF9800")}}class ot extends H{descriptor={id:"atr",name:"Average True Range",placement:"panel",defaultConfig:{period:14}};calculate(e,t){const n=E(t,"period",14,1),s=new Map,i=new Array(e.length).fill(null);if(e.length<2)return{values:s,series:i};const o=[e[0].high-e[0].low];for(let a=1;a<e.length;a++){const c=Math.max(e[a].high-e[a].low,Math.abs(e[a].high-e[a-1].close),Math.abs(e[a].low-e[a-1].close));o.push(c)}let l=0;for(let a=0;a<n;a++)l+=o[a];l/=n;const h={value:l};s.set(e[n-1].time,h),i[n-1]=h;for(let a=n;a<e.length;a++){l=(l*(n-1)+o[a])/n;const c={value:l};s.set(e[a].time,c),i[a]=c}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];T&&T.value!==void 0&&(a=Math.min(a,T.value),c=Math.max(c,T.value))}if(a===1/0)return;const f=(c-a||1)*.1,u=a-f,m=c+f-u,y=p=>o.y+o.height*(1-(p-u)/m);e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let b=!1;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];if(T&&T.value!==void 0){const v=D(p,n),S=y(T.value);b?e.lineTo(v,S):(e.moveTo(v,S),b=!0)}}e.stroke()}}class rt extends H{descriptor={id:"adx",name:"Average Directional Index",placement:"panel",defaultConfig:{period:14}};calculate(e,t){const n=E(t,"period",14,1),s=new Map,i=new Array(e.length).fill(null);if(e.length<n*2)return{values:s,series:i};const o=[],l=[],h=[];for(let u=1;u<e.length;u++){const g=Math.max(e[u].high-e[u].low,Math.abs(e[u].high-e[u-1].close),Math.abs(e[u].low-e[u-1].close));o.push(g);const m=e[u].high-e[u-1].high,y=e[u-1].low-e[u].low;l.push(m>y&&m>0?m:0),h.push(y>m&&y>0?y:0)}let a=0,c=0,d=0;for(let u=0;u<n;u++)a+=o[u],c+=l[u],d+=h[u];const f=[];for(let u=n;u<o.length;u++){u>n&&(a=a-a/n+o[u],c=c-c/n+l[u],d=d-d/n+h[u]);const g=a!==0?c/a*100:0,m=a!==0?d/a*100:0,y=g+m,b=y!==0?Math.abs(g-m)/y*100:0;f.push(b);const p=u+1,T={plusDI:g,minusDI:m,dx:b};s.set(e[p].time,T),i[p]=T}if(f.length>=n){let u=0;for(let m=0;m<n;m++)u+=f[m];u/=n;const g=n*2;if(g<e.length){const m=s.get(e[g].time);m&&(m.adx=u),i[g]&&(i[g].adx=u)}for(let m=n;m<f.length;m++){u=(u*(n-1)+f[m])/n;const y=m+n+1;if(y<e.length){const b=s.get(e[y].time);b&&(b.adx=u),i[y]&&(i[y].adx=u)}}}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange,a=d=>o.y+o.height*(1-d/100),c=(d,f)=>{e.beginPath(),e.strokeStyle=f,e.lineWidth=s.lineWidths[0],e.lineJoin="round";let u=!1;for(let g=l;g<=h&&g<i.length;g++){const m=i[g];if(!m||m[d]===void 0)continue;const y=D(g,n),b=a(m[d]);u?e.lineTo(y,b):(e.moveTo(y,b),u=!0)}e.stroke()};c("adx",s.colors[0]),c("plusDI",s.colors[1]??"#26A69A"),c("minusDI",s.colors[2]??"#EF5350")}}class lt extends H{descriptor={id:"obv",name:"On Balance Volume",placement:"panel",defaultConfig:{}};calculate(e,t){const n=new Map,s=new Array(e.length).fill(null);if(e.length===0)return{values:n,series:s};let i=0;const o={value:i};n.set(e[0].time,o),s[0]=o;for(let l=1;l<e.length;l++){e[l].close>e[l-1].close?i+=e[l].volume:e[l].close<e[l-1].close&&(i-=e[l].volume);const h={value:i};n.set(e[l].time,h),s[l]=h}return{values:n,series:s}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let b=l;b<=h&&b<i.length;b++){const p=i[b];p&&p.value!==void 0&&(a=Math.min(a,p.value),c=Math.max(c,p.value))}if(a===1/0)return;const f=(c-a||1)*.1,u=a-f,g=c+f-u,m=b=>o.y+o.height*(1-(b-u)/g);e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let y=!1;for(let b=l;b<=h&&b<i.length;b++){const p=i[b];if(p&&p.value!==void 0){const T=D(b,n),v=m(p.value);y?e.lineTo(T,v):(e.moveTo(T,v),y=!0)}}e.stroke()}}class at extends H{descriptor={id:"williamsR",name:"Williams %R",placement:"panel",defaultConfig:{period:14}};calculate(e,t){const n=E(t,"period",14,1),s=new Map,i=new Array(e.length).fill(null);for(let o=n-1;o<e.length;o++){let l=-1/0,h=1/0;for(let d=o-n+1;d<=o;d++)e[d].high>l&&(l=e[d].high),e[d].low<h&&(h=e[d].low);const c={value:l===h?-50:(l-e[o].close)/(l-h)*-100};s.set(e[o].time,c),i[o]=c}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange,a=d=>o.y+o.height*(1-(d+100)/100);e.strokeStyle=s.colors[1]??"#787B86",e.lineWidth=1,e.setLineDash([4,4]);for(const d of[-20,-80]){const f=a(d);e.beginPath(),e.moveTo(o.x,f),e.lineTo(o.x+o.width,f),e.stroke()}e.setLineDash([]),e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let c=!1;for(let d=l;d<=h&&d<i.length;d++){const f=i[d];if(f&&f.value!==void 0){const u=D(d,n),g=a(f.value);c?e.lineTo(u,g):(e.moveTo(u,g),c=!0)}}e.stroke()}}class ht extends H{descriptor={id:"cci",name:"Commodity Channel Index",placement:"panel",defaultConfig:{period:20}};calculate(e,t){const n=E(t,"period",20,1),s=new Map,i=new Array(e.length).fill(null);for(let o=n-1;o<e.length;o++){let l=0;for(let g=o-n+1;g<=o;g++)l+=(e[g].high+e[g].low+e[g].close)/3;const h=l/n;let a=0;for(let g=o-n+1;g<=o;g++){const m=(e[g].high+e[g].low+e[g].close)/3;a+=Math.abs(m-h)}const c=a/n,d=(e[o].high+e[o].low+e[o].close)/3,u={value:c===0?0:(d-h)/(.015*c)};s.set(e[o].time,u),i[o]=u}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];T&&T.value!==void 0&&(a=Math.min(a,T.value),c=Math.max(c,T.value))}if(a===1/0)return;const f=(c-a||1)*.1,u=a-f,g=c+f,m=g-u,y=p=>o.y+o.height*(1-(p-u)/m);e.strokeStyle=s.colors[1]??"#787B86",e.lineWidth=1,e.setLineDash([4,4]);for(const p of[-100,0,100])if(p>=u&&p<=g){const T=y(p);e.beginPath(),e.moveTo(o.x,T),e.lineTo(o.x+o.width,T),e.stroke()}e.setLineDash([]),e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let b=!1;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];if(T&&T.value!==void 0){const v=D(p,n),S=y(T.value);b?e.lineTo(v,S):(e.moveTo(v,S),b=!0)}}e.stroke()}}class ct extends H{descriptor={id:"mfi",name:"Money Flow Index",placement:"panel",defaultConfig:{period:14}};calculate(e,t){const n=E(t,"period",14,1),s=new Map,i=new Array(e.length).fill(null);if(e.length<n+1)return{values:s,series:i};const o=e.map(h=>(h.high+h.low+h.close)/3),l=o.map((h,a)=>h*e[a].volume);for(let h=n;h<e.length;h++){let a=0,c=0;for(let u=h-n+1;u<=h;u++)o[u]>o[u-1]?a+=l[u]:o[u]<o[u-1]&&(c+=l[u]);const f={value:c===0?100:100-100/(1+a/c)};s.set(e[h].time,f),i[h]=f}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange,a=d=>o.y+o.height*(1-d/100);e.strokeStyle=s.colors[1]??"#787B86",e.lineWidth=1,e.setLineDash([4,4]);for(const d of[20,80]){const f=a(d);e.beginPath(),e.moveTo(o.x,f),e.lineTo(o.x+o.width,f),e.stroke()}e.setLineDash([]),e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let c=!1;for(let d=l;d<=h&&d<i.length;d++){const f=i[d];if(f&&f.value!==void 0){const u=D(d,n),g=a(f.value);c?e.lineTo(u,g):(e.moveTo(u,g),c=!0)}}e.stroke()}}class dt extends H{descriptor={id:"aroon",name:"Aroon",placement:"panel",defaultConfig:{period:25}};calculate(e,t){const n=E(t,"period",25,1),s=new Map,i=new Array(e.length).fill(null);for(let o=n;o<e.length;o++){let l=0,h=0,a=-1/0,c=1/0;for(let f=0;f<=n;f++){const u=o-n+f;e[u].high>a&&(a=e[u].high,l=f),e[u].low<c&&(c=e[u].low,h=f)}const d={up:l/n*100,down:h/n*100};s.set(e[o].time,d),i[o]=d}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange,a=d=>o.y+o.height*(1-d/100),c=(d,f)=>{e.beginPath(),e.strokeStyle=f,e.lineWidth=s.lineWidths[0],e.lineJoin="round";let u=!1;for(let g=l;g<=h&&g<i.length;g++){const m=i[g];if(!m||m[d]===void 0)continue;const y=D(g,n),b=a(m[d]);u?e.lineTo(y,b):(e.moveTo(y,b),u=!0)}e.stroke()};c("up",s.colors[0]),c("down",s.colors[1]??"#EF5350")}}class ft extends H{descriptor={id:"roc",name:"Rate of Change",placement:"panel",defaultConfig:{period:12}};calculate(e,t){const n=E(t,"period",12,1),s=new Map,i=new Array(e.length).fill(null);for(let o=n;o<e.length;o++){const l=e[o-n].close,a={value:l===0?0:(e[o].close-l)/l*100};s.set(e[o].time,a),i[o]=a}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];T&&T.value!==void 0&&(a=Math.min(a,T.value),c=Math.max(c,T.value))}if(a===1/0)return;const f=(c-a||1)*.1,u=a-f,g=c+f,m=g-u,y=p=>o.y+o.height*(1-(p-u)/m);if(0>=u&&0<=g){e.strokeStyle=s.colors[1]??"#787B86",e.lineWidth=1,e.setLineDash([4,4]);const p=y(0);e.beginPath(),e.moveTo(o.x,p),e.lineTo(o.x+o.width,p),e.stroke(),e.setLineDash([])}e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let b=!1;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];if(T&&T.value!==void 0){const v=D(p,n),S=y(T.value);b?e.lineTo(v,S):(e.moveTo(v,S),b=!0)}}e.stroke()}}class ut extends H{descriptor={id:"tsi",name:"True Strength Index",placement:"panel",defaultConfig:{longPeriod:25,shortPeriod:13,signalPeriod:7}};emaSmooth(e,t){const n=2/(t+1),s=[e[0]];for(let i=1;i<e.length;i++)s.push((e[i]-s[i-1])*n+s[i-1]);return s}calculate(e,t){const n=E(t,"longPeriod",25,1),s=E(t,"shortPeriod",13,1),i=E(t,"signalPeriod",7,1),o=new Map,l=new Array(e.length).fill(null);if(e.length<2)return{values:o,series:l};const h=[],a=[];for(let g=1;g<e.length;g++){const m=e[g].close-e[g-1].close;h.push(m),a.push(Math.abs(m))}const c=this.emaSmooth(this.emaSmooth(h,n),s),d=this.emaSmooth(this.emaSmooth(a,n),s),f=[];for(let g=0;g<c.length;g++)f.push(d[g]===0?0:c[g]/d[g]*100);const u=this.emaSmooth(f,i);for(let g=0;g<f.length;g++){const m=g+1;if(m<e.length){const y={tsi:f[g],signal:u[g]};o.set(e[m].time,y),l[m]=y}}return{values:o,series:l}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];T&&(T.tsi!==void 0&&(a=Math.min(a,T.tsi),c=Math.max(c,T.tsi)),T.signal!==void 0&&(a=Math.min(a,T.signal),c=Math.max(c,T.signal)))}if(a===1/0)return;const f=(c-a||1)*.1,u=a-f,g=c+f,m=g-u,y=p=>o.y+o.height*(1-(p-u)/m);0>=u&&0<=g&&(e.strokeStyle="#787B86",e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(o.x,y(0)),e.lineTo(o.x+o.width,y(0)),e.stroke(),e.setLineDash([]));const b=(p,T)=>{e.beginPath(),e.strokeStyle=T,e.lineWidth=s.lineWidths[0],e.lineJoin="round";let v=!1;for(let S=l;S<=h&&S<i.length;S++){const M=i[S];if(!M||M[p]===void 0)continue;const P=D(S,n),R=y(M[p]);v?e.lineTo(P,R):(e.moveTo(P,R),v=!0)}e.stroke()};b("tsi",s.colors[0]),b("signal",s.colors[1]??"#FF9800")}}class gt extends H{descriptor={id:"cmf",name:"Chaikin Money Flow",placement:"panel",defaultConfig:{period:20}};calculate(e,t){const n=E(t,"period",20,1),s=new Map,i=new Array(e.length).fill(null);for(let o=n-1;o<e.length;o++){let l=0,h=0;for(let c=o-n+1;c<=o;c++){const d=e[c].high-e[c].low,f=d===0?0:(e[c].close-e[c].low-(e[c].high-e[c].close))/d;l+=f*e[c].volume,h+=e[c].volume}const a={value:h===0?0:l/h};s.set(e[o].time,a),i[o]=a}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];T&&T.value!==void 0&&(a=Math.min(a,T.value),c=Math.max(c,T.value))}if(a===1/0)return;const f=(c-a||1)*.1,u=a-f,g=c+f,m=g-u,y=p=>o.y+o.height*(1-(p-u)/m);0>=u&&0<=g&&(e.strokeStyle="#787B86",e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(o.x,y(0)),e.lineTo(o.x+o.width,y(0)),e.stroke(),e.setLineDash([]));const b=n.barWidth/2;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];if(T&&T.value!==void 0){const v=D(p,n),S=y(T.value),M=y(0);e.fillStyle=T.value>=0?s.colors[0]:s.colors[1]??"#EF5350";const P=Math.min(S,M);e.fillRect(v-b,P,n.barWidth,Math.max(Math.abs(S-M),1))}}}}class mt extends H{descriptor={id:"stddev",name:"Standard Deviation",placement:"panel",defaultConfig:{period:20}};calculate(e,t){const n=E(t,"period",20,1),s=new Map,i=new Array(e.length).fill(null);for(let o=n-1;o<e.length;o++){let l=0;for(let d=o-n+1;d<=o;d++)l+=e[d].close;const h=l/n;let a=0;for(let d=o-n+1;d<=o;d++){const f=e[d].close-h;a+=f*f}const c={value:Math.sqrt(a/n)};s.set(e[o].time,c),i[o]=c}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let b=l;b<=h&&b<i.length;b++){const p=i[b];p&&p.value!==void 0&&(a=Math.min(a,p.value),c=Math.max(c,p.value))}if(a===1/0)return;const f=(c-a||1)*.1,u=a-f,g=c+f-u,m=b=>o.y+o.height*(1-(b-u)/g);e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let y=!1;for(let b=l;b<=h&&b<i.length;b++){const p=i[b];if(p&&p.value!==void 0){const T=D(b,n),v=m(p.value);y?e.lineTo(T,v):(e.moveTo(T,v),y=!0)}}e.stroke()}}class yt extends H{descriptor={id:"volumeProfile",name:"Volume Profile",placement:"panel",defaultConfig:{rows:24}};calculate(e,t){const n=E(t,"rows",24,1),s=new Map,i=new Array(e.length).fill(null);if(e.length===0)return{values:s,series:i};let o=1/0,l=-1/0;for(const d of e)d.low<o&&(o=d.low),d.high>l&&(l=d.high);const a=(l-o||1)/n,c=new Array(n).fill(0);for(const d of e){const f=(d.high+d.low+d.close)/3,u=Math.min(n-1,Math.floor((f-o)/a));c[u]+=d.volume}for(let d=0;d<n;d++){const f=o+(d+.5)*a;s.set(d,{price:f,volume:c[d],rowIndex:d})}return{values:s,series:i,meta:{minPrice:o,maxPrice:l,rows:n,rowHeight:a,bins:c,maxVolume:Math.max(...c)}}}render(e,t,n,s){const{chartRect:i}=n,o=t.meta;if(!o||o.maxVolume===0)return;const{minPrice:l,rows:h,rowHeight:a,bins:c,maxVolume:d}=o,f=i.height/h,u=i.width*.3;for(let g=0;g<h;g++){const y=(l+(g+.5)*a-n.priceRange.min)/(n.priceRange.max-n.priceRange.min),b=i.y+i.height*(1-y),p=c[g]/d*u;e.fillStyle=c[g]>d*.7?s.colors[1]??"rgba(255, 152, 0, 0.5)":s.colors[0]??"rgba(33, 150, 243, 0.3)",e.fillRect(i.x,b-f/2,p,Math.max(f-1,1))}}}class pt extends H{descriptor={id:"ad",name:"Accumulation/Distribution",placement:"panel",defaultConfig:{}};calculate(e,t){const n=new Map,s=new Array(e.length).fill(null);if(e.length===0)return{values:n,series:s};let i=0;for(let o=0;o<e.length;o++){const l=e[o].high-e[o].low,h=l===0?0:(e[o].close-e[o].low-(e[o].high-e[o].close))/l;i+=h*e[o].volume;const a={value:i};n.set(e[o].time,a),s[o]=a}return{values:n,series:s}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let b=l;b<=h&&b<i.length;b++){const p=i[b];p&&p.value!==void 0&&(a=Math.min(a,p.value),c=Math.max(c,p.value))}if(a===1/0)return;const f=(c-a||1)*.1,u=a-f,g=c+f-u,m=b=>o.y+o.height*(1-(b-u)/g);e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let y=!1;for(let b=l;b<=h&&b<i.length;b++){const p=i[b];if(p&&p.value!==void 0){const T=D(b,n),v=m(p.value);y?e.lineTo(T,v):(e.moveTo(T,v),y=!0)}}e.stroke()}}class bt extends H{descriptor={id:"vroc",name:"Volume Rate of Change",placement:"panel",defaultConfig:{period:14}};calculate(e,t){const n=E(t,"period",14,1),s=new Map,i=new Array(e.length).fill(null);for(let o=n;o<e.length;o++){const l=e[o-n].volume,a={value:l===0?0:(e[o].volume-l)/l*100};s.set(e[o].time,a),i[o]=a}return{values:s,series:i}}render(e,t,n,s){const i=t.series;if(!i)return;const{chartRect:o}=n,{from:l,to:h}=n.visibleRange;let a=1/0,c=-1/0;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];T&&T.value!==void 0&&(a=Math.min(a,T.value),c=Math.max(c,T.value))}if(a===1/0)return;const f=(c-a||1)*.1,u=a-f,g=c+f,m=g-u,y=p=>o.y+o.height*(1-(p-u)/m);0>=u&&0<=g&&(e.strokeStyle="#787B86",e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(o.x,y(0)),e.lineTo(o.x+o.width,y(0)),e.stroke(),e.setLineDash([])),e.beginPath(),e.strokeStyle=s.colors[0],e.lineWidth=s.lineWidths[0],e.lineJoin="round";let b=!1;for(let p=l;p<=h&&p<i.length;p++){const T=i[p];if(T&&T.value!==void 0){const v=D(p,n),S=y(T.value);b?e.lineTo(v,S):(e.moveTo(v,S),b=!0)}}e.stroke()}}function In(r){r.register(new Ue),r.register(new xe),r.register(new Oe),r.register(new qe),r.register(new ze),r.register(new Xe),r.register(new _e),r.register(new je),r.register(new Ge),r.register(new Ne),r.register(new Je),r.register(new Ke),r.register(new Ze),r.register(new Qe),r.register(new nt),r.register(new it),r.register(new st),r.register(new ot),r.register(new rt),r.register(new lt),r.register(new at),r.register(new ht),r.register(new ct),r.register(new dt),r.register(new ft),r.register(new ut),r.register(new gt),r.register(new mt),r.register(new yt),r.register(new pt),r.register(new bt),r.register(new et),r.register(new tt)}function Tt(r){if(typeof r!="object"||r===null)return!1;const e=r;return typeof e.requestId!="number"?!1:e.type==="result"||e.type==="pong"||e.type==="error"}class Dn{worker;fallbackPlugins;timeoutMs;nextRequestId=1;pending=new Map;messageHandler;constructor(e,t={}){this.worker=e,this.fallbackPlugins=t.fallbackPlugins??new Map,this.timeoutMs=t.timeoutMs??3e4,this.messageHandler=n=>this.handleMessage(n),this.worker&&(this.worker.addEventListener("message",this.messageHandler),this.worker.addEventListener("error",this.messageHandler))}hasWorker(){return this.worker!==null}registerFallbackPlugin(e){this.fallbackPlugins.set(e.descriptor.id,e)}async calculate(e,t,n){if(!this.worker){const o=this.fallbackPlugins.get(e);if(!o)throw new Error(`Unknown indicator: ${e}`);return o.calculate(n,t)}const s=this.nextRequestId++,i={type:"calculate",requestId:s,indicatorId:e,config:t,data:n};return new Promise((o,l)=>{const h={resolve:o,reject:l,timer:null};this.timeoutMs>0&&(h.timer=setTimeout(()=>{this.pending.delete(s),l(new Error(`Indicator worker timeout (${this.timeoutMs}ms): ${e}`))},this.timeoutMs)),this.pending.set(s,h),this.worker.postMessage(i)})}ping(){if(!this.worker)return Promise.resolve();const e=this.nextRequestId++;return new Promise((t,n)=>{const s={resolve:()=>t(),reject:n,timer:this.timeoutMs>0?setTimeout(()=>{this.pending.delete(e),n(new Error("Indicator worker ping timeout"))},this.timeoutMs):null};this.pending.set(e,s),this.worker.postMessage({type:"ping",requestId:e})})}terminate(){this.worker&&(this.worker.removeEventListener("message",this.messageHandler),this.worker.removeEventListener("error",this.messageHandler),this.worker.terminate(),this.worker=null);for(const e of this.pending.values())e.timer&&clearTimeout(e.timer),e.reject(new Error("Indicator worker terminated"));this.pending.clear()}handleMessage(e){const t=e.data;if(t!==void 0){if(!Tt(t))return;this.dispatchResponse(t);return}const n=e.message??"Indicator worker error";for(const[s,i]of this.pending)i.timer&&clearTimeout(i.timer),i.reject(new Error(n)),this.pending.delete(s)}dispatchResponse(e){const t=this.pending.get(e.requestId);if(t){if(this.pending.delete(e.requestId),t.timer&&clearTimeout(t.timer),e.type==="error"){t.reject(new Error(e.message));return}if(e.type==="pong"){t.resolve(void 0);return}t.resolve(e.output)}}}class Wn{visible=!0;setVisible(e){this.visible=e}isVisible(){return this.visible}render(e,t,n){if(!this.visible)return;const{chartRect:s,priceRange:i}=t;e.strokeStyle=n.grid,e.lineWidth=1;const o=i.max-i.min;if(o<=0)return;const l=1/o;e.beginPath();const h=L.computeTickStep(i.min,i.max,8),a=Math.ceil(i.min/h)*h;for(let m=a;m<=i.max;m+=h){const y=Math.round(s.y+s.height*(1-(m-i.min)*l))+.5;e.moveTo(s.x,y),e.lineTo(s.x+s.width,y)}const c=t.barWidth+t.barSpacing,d=Math.max(1,Math.ceil(80/c)),{from:f,to:u}=t.visibleRange,g=-t.offset+s.x+t.barWidth/2;for(let m=f;m<=u;m++){if(m%d!==0)continue;const y=Math.round(m*c+g)+.5;e.moveTo(y,s.y),e.lineTo(y,s.y+s.height)}e.stroke()}}class Bn{locale="en-US";setLocale(e){this.locale=e}render(e,t,n){const{chartRect:s,priceRange:i}=t,o=s.x+s.width,l=i.max-i.min;if(l<=0)return;const h=1/l;e.strokeStyle=n.axisLine,e.lineWidth=1,e.beginPath(),e.moveTo(o+.5,s.y),e.lineTo(o+.5,s.y+s.height),e.stroke();const a=L.computeTickStep(i.min,i.max,8),c=Math.ceil(i.min/a)*a,d=a<1?Math.ceil(-Math.log10(a))+1:2,f=`${n.font.sizeSmall}px ${n.font.family}`,u=[];for(let g=c;g<=i.max;g+=a){const m=s.y+s.height*(1-(g-i.min)*h);u.push({y:m,text:L.formatPrice(g,d,this.locale)})}e.font=f,e.textBaseline="middle",e.textAlign="left",e.fillStyle=n.axisLabelBackground;for(const{y:g}of u)e.fillRect(o+1,g-8,L.PRICE_AXIS_WIDTH-2,16);e.fillStyle=n.axisLabel;for(const{y:g,text:m}of u)e.fillText(m,o+5,g)}}const J=new Date,fe=r=>r<10?"0"+r:""+r,Fn=new Date().getTimezoneOffset(),ue=-Fn/60,En=`UTC${ue>=0?"+":""}${ue}`;class Hn{render(e,t,n,s,i){const{chartRect:o}=t,l=i??o.y+o.height;e.strokeStyle=n.axisLine,e.lineWidth=1,e.beginPath(),e.moveTo(o.x,l+.5),e.lineTo(o.x+o.width,l+.5),e.stroke();const h=t.barWidth+t.barSpacing,c=Math.max(1,Math.ceil(80/h)),{from:d,to:f}=t.visibleRange,u=-t.offset+o.x+t.barWidth/2;e.font=`${n.font.sizeSmall}px ${n.font.family}`,e.textBaseline="top",e.textAlign="center",e.fillStyle=n.axisLabel;let g=-1;for(let b=d;b<=f&&b<s.length;b++){if(b%c!==0)continue;const p=b*h+u,T=s[b].time,v=T>1e12?T:T*1e3;J.setTime(v);const S=J.getDate(),M=J.getMonth()+1,P=J.getHours(),R=J.getMinutes();let w;S!==g?(w=`${M}/${S}`,g=S):w=`${fe(P)}:${fe(R)}`,e.fillText(w,p,l+5)}const m=o.x+o.width-4,y=l+4;e.font=`${n.font.sizeSmall-1}px ${n.font.family}`,e.textAlign="right",e.textBaseline="top",e.fillStyle=n.textSecondary,e.fillText(En,m,y)}}class $n{constructor(e){this.element=e}panHandler=null;zoomHandler=null;crosshairHandler=null;drawingManager=null;tradingManager=null;viewportGetter=null;onOverlayDirty=null;boundHandlers=[];lastTouchDist=0;lastTouchMid={x:0,y:0};touchActive=!1;cachedRect=null;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(){const e=()=>this.viewportGetter?.()??null,t=g=>{this.invalidateRect();const m=this.getMousePos(g),y=e();this.tradingManager&&y&&this.tradingManager.onPointerDown(m,y)||this.drawingManager&&y&&this.drawingManager.onPointerDown(m,y)||this.panHandler?.onPointerDown(m)},n=g=>{const m=this.getMousePos(g),y=e();if(this.tradingManager&&y&&this.tradingManager.onPointerMove(m,y)){this.crosshairHandler?.onPointerMove(m),this.onOverlayDirty?.();return}if(this.drawingManager&&y&&this.drawingManager.onPointerMove(m,y)){this.crosshairHandler?.onPointerMove(m),this.onOverlayDirty?.();return}this.panHandler?.onPointerMove(m),this.crosshairHandler?.onPointerMove(m),this.onOverlayDirty?.()},s=()=>{this.tradingManager?.onPointerUp()||this.drawingManager?.onPointerUp()||this.panHandler?.onPointerUp()},i=()=>{this.panHandler?.onPointerUp(),this.drawingManager?.onPointerUp(),this.tradingManager?.onPointerUp(),this.crosshairHandler?.onPointerLeave(),this.onOverlayDirty?.()},o=g=>{g.preventDefault();const m=this.getMousePos(g);this.zoomHandler?.onWheel(g.deltaY,m)},l=g=>{this.drawingManager?.onKeyDown(g.key,g.ctrlKey||g.metaKey)&&g.preventDefault()},h=g=>{const m=e();this.tradingManager&&m&&(g.preventDefault(),this.tradingManager.onContextMenu(this.getMousePos(g),m))},a=g=>{if(g.preventDefault(),this.invalidateRect(),g.touches.length===1){const m=this.getTouchPos(g.touches[0]);this.panHandler?.onPointerDown(m),this.crosshairHandler?.onPointerMove(m),this.touchActive=!0}else g.touches.length===2&&(this.panHandler?.onPointerUp(),this.lastTouchDist=this.getTouchDistance(g.touches[0],g.touches[1]),this.lastTouchMid=this.getTouchMidpoint(g.touches[0],g.touches[1]),this.touchActive=!0)},c=g=>{if(g.preventDefault(),g.touches.length===1&&this.touchActive){const m=this.getTouchPos(g.touches[0]);this.panHandler?.onPointerMove(m),this.crosshairHandler?.onPointerMove(m)}else if(g.touches.length===2){const m=this.getTouchDistance(g.touches[0],g.touches[1]),y=this.getTouchMidpoint(g.touches[0],g.touches[1]);if(this.lastTouchDist>0){const T=(m/this.lastTouchDist-1)*.5;this.zoomHandler?.onWheel(-T*100,y)}const b=this.lastTouchMid.x-y.x;Math.abs(b)>1&&(this.panHandler?.onPointerDown(this.lastTouchMid),this.panHandler?.onPointerMove(y)),this.lastTouchDist=m,this.lastTouchMid=y}this.onOverlayDirty?.()},d=g=>{if(g.touches.length===0)this.panHandler?.onPointerUp(),this.crosshairHandler?.onPointerLeave(),this.touchActive=!1,this.lastTouchDist=0;else if(g.touches.length===1){const m=this.getTouchPos(g.touches[0]);this.panHandler?.onPointerDown(m)}},f=()=>this.invalidateRect(),u=()=>this.invalidateRect();this.element.addEventListener("mousedown",t),this.element.addEventListener("mousemove",n),document.addEventListener("mouseup",s),this.element.addEventListener("mouseleave",i),this.element.addEventListener("wheel",o,{passive:!1}),this.element.addEventListener("contextmenu",h),this.element.addEventListener("touchstart",a,{passive:!1}),this.element.addEventListener("touchmove",c,{passive:!1}),this.element.addEventListener("touchend",d),document.addEventListener("keydown",l),window.addEventListener("resize",f),this.element.addEventListener("scroll",u,{passive:!0}),this.boundHandlers.push(()=>this.element.removeEventListener("mousedown",t),()=>this.element.removeEventListener("mousemove",n),()=>document.removeEventListener("mouseup",s),()=>this.element.removeEventListener("mouseleave",i),()=>this.element.removeEventListener("wheel",o),()=>this.element.removeEventListener("contextmenu",h),()=>this.element.removeEventListener("touchstart",a),()=>this.element.removeEventListener("touchmove",c),()=>this.element.removeEventListener("touchend",d),()=>document.removeEventListener("keydown",l),()=>window.removeEventListener("resize",f),()=>this.element.removeEventListener("scroll",u))}detach(){for(const e of this.boundHandlers)e();this.boundHandlers=[],this.cachedRect=null}invalidateRect(){this.cachedRect=null}getRect(){return this.cachedRect||(this.cachedRect=this.element.getBoundingClientRect()),this.cachedRect}getMousePos(e){const t=this.getRect();return{x:e.clientX-t.left,y:e.clientY-t.top}}getTouchPos(e){const t=this.getRect();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){const n=this.getRect();return{x:(e.clientX+t.clientX)/2-n.left,y:(e.clientY+t.clientY)/2-n.top}}}class Yn{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;const t=Date.now(),n=this.lastX-e.x,s=t-this.lastTime;s>0&&(this.velocity=n/s),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;const 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),this.momentumId=0)}}class Vn{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||(this.rafId=requestAnimationFrame(()=>{this.rafId=0;const n=this.pendingDelta,s=this.pendingCenterX;this.pendingDelta=0,this.callback(n,s)}))}dispose(){this.rafId&&(cancelAnimationFrame(this.rafId),this.rafId=0)}}class Un{position=null;callback=null;data=[];magnetMode=!0;mode="magnet";pricePrecision=2;locale="en-US";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}setLocale(e){this.locale=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(e,t,n){if(!this.position||this.mode==="hidden")return;const{chartRect:s}=t;let{x:i,y:o}=this.position;if(i<s.x||i>s.x+s.width||o<s.y||o>s.y+s.height)return;let l=ee(i,t);l=Math.max(0,Math.min(this.data.length-1,l)),this.magnetMode&&l>=0&&l<this.data.length&&(i=D(l,t)),l!==this.lastCallbackBarIndex&&(this.pendingBarIndex=l,this.pendingPoint={x:i,y:o},this.lastCallbackBarIndex=l,this.scheduleCallback()),e.setLineDash([4,4]),e.strokeStyle=n.crosshair,e.lineWidth=1,e.beginPath(),e.moveTo(Math.round(i)+.5,s.y),e.lineTo(Math.round(i)+.5,s.y+s.height),e.stroke(),e.beginPath(),e.moveTo(s.x,Math.round(o)+.5),e.lineTo(s.x+s.width,Math.round(o)+.5),e.stroke(),e.setLineDash([]);const h=V(o,t),a=L.formatPrice(h,this.pricePrecision,this.locale),c=s.x+s.width+1;e.fillStyle=n.axisLabelBackground,e.fillRect(c,o-10,L.PRICE_AXIS_WIDTH-2,20),e.fillStyle=n.axisLabel,e.font=`${n.font.sizeSmall}px ${n.font.family}`,e.textBaseline="middle",e.textAlign="left",e.fillText(a,c+5,o)}scheduleCallback(){this.callbackScheduled||(this.callbackScheduled=!0,queueMicrotask(()=>{this.callbackScheduled=!1,this.flushCallback(this.pendingBarIndex,this.pendingPoint)}))}flushCallback(e,t){this.callback?.(e,t)}}class xn{callbacks;enabled=!0;constructor(e){this.callbacks=e}setEnabled(e){this.enabled=e}handleKey(e){if(!this.enabled)return!1;const n=e.shiftKey?10:1;switch(e.key){case"ArrowLeft":return this.callbacks.scrollBars(-n),!0;case"ArrowRight":return this.callbacks.scrollBars(n),!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}}}class On{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);const 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,s){if(!this.el)return;const o=t.close>=t.open?n.candleUp:n.candleDown,l=y=>y.toFixed(2),h=new Date(t.time);this.timeEl.textContent=`${h.getMonth()+1}/${h.getDate()} ${String(h.getHours()).padStart(2,"0")}:${String(h.getMinutes()).padStart(2,"0")}`,this.timeEl.style.color=n.textSecondary,this.openEl.textContent=l(t.open),this.highEl.textContent=l(t.high),this.lowEl.textContent=l(t.low),this.closeEl.textContent=l(t.close),this.openEl.style.color=o,this.highEl.style.color=o,this.lowEl.style.color=o,this.closeEl.style.color=o;const a=t.close-t.open,c=t.open!==0?a/t.open*100:0,d=a>=0?"+":"";this.changeEl.textContent=`${d}${l(a)} (${d}${c.toFixed(2)}%)`,this.changeEl.style.color=o;const f=t.volume;this.volEl.textContent=`Vol ${f>=1e6?(f/1e6).toFixed(1)+"M":f>=1e3?(f/1e3).toFixed(1)+"K":f.toFixed(0)}`,this.volEl.style.color=n.textSecondary;const u=qn(n.background);this.el.style.background=u?ge(n.background,.95):ge(n.background,.97),this.el.style.border=`1px solid ${n.axisLine}`,this.el.style.boxShadow=u?"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 g=e.x+16,m=e.y-40;g+this.measuredWidth>s.width-80&&(g=e.x-this.measuredWidth-16),m<4&&(m=4),m+this.measuredHeight>s.height&&(m=s.height-this.measuredHeight-4),this.el.style.transform=`translate(${Math.round(g)}px, ${Math.round(m)}px)`,this.el.style.left="0",this.el.style.top="0",this.visible||(this.el.style.display="block",this.visible=!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){const t=document.createElement("div");return e.appendChild(t),t}label(e){const t=document.createElement("span");return t.textContent=e,t.style.opacity="0.6",t}val(){return document.createElement("span")}}function qn(r){if(r.startsWith("#")){const t=r.slice(1),n=t.length===3?t.split("").map(h=>h+h).join(""):t.slice(0,6),s=parseInt(n.slice(0,2),16),i=parseInt(n.slice(2,4),16),o=parseInt(n.slice(4,6),16);return(.299*s+.587*i+.114*o)/255<.5}const e=r.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);if(e){const t=parseInt(e[1]),n=parseInt(e[2]),s=parseInt(e[3]);return(.299*t+.587*n+.114*s)/255<.5}return!0}function ge(r,e){if(r.startsWith("#")){const t=r.slice(1),n=t.length===3?t.split("").map(l=>l+l).join(""):t.slice(0,6),s=parseInt(n.slice(0,2),16),i=parseInt(n.slice(2,4),16),o=parseInt(n.slice(4,6),16);return`rgba(${s}, ${i}, ${o}, ${e})`}return r}class zn{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){const n={type:e,timestamp:Date.now(),payload:t},s=this.listeners.get(e);if(s)for(const i of s)i(n);if(this.tauriBridge?.enabled&&window.__TAURI__){const i=this.tauriBridge.eventPrefix??"tc-chart";window.__TAURI__.event.emit(`${i}:${e}`,n).catch(()=>{})}}enableTauriBridge(e){this.tauriBridge=e}disableTauriBridge(){this.tauriBridge=null}destroy(){this.listeners.clear(),this.tauriBridge=null}}class ${hitTestAnchor(e,t,n,s){for(let i=0;i<t.anchors.length;i++){const o=this.anchorToPixel(t.anchors[i],n);if(Math.hypot(e.x-o.x,e.y-o.y)<=s)return i}return-1}anchorToPixel(e,t){const n=Ve(e.time,t),s=A(e.price,t);return{x:n,y:s}}pixelToAnchor(e,t){const n=_(e.x,t),s=V(e.y,t);return{time:n,price:s}}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(const i of t.anchors){const o=this.anchorToPixel(i,n);e.fillRect(o.x-4,o.y-4,4*2,4*2),e.strokeRect(o.x-4,o.y-4,4*2,4*2)}}distanceToLine(e,t,n){const s=n.x-t.x,i=n.y-t.y,o=s*s+i*i;if(o===0)return Math.hypot(e.x-t.x,e.y-t.y);let l=((e.x-t.x)*s+(e.y-t.y)*i)/o;l=Math.max(0,Math.min(1,l));const h=t.x+l*s,a=t.y+l*i;return Math.hypot(e.x-h,e.y-a)}distanceToInfiniteLine(e,t,n){const s=n.x-t.x,i=n.y-t.y,o=Math.hypot(s,i);return o===0?Math.hypot(e.x-t.x,e.y-t.y):Math.abs(s*(t.y-e.y)-i*(t.x-e.x))/o}}let me=1;function se(r){return{...r,anchors:r.anchors.map(e=>({...e})),style:{...r.style},meta:r.meta?{...r.meta}:void 0}}class Xn{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};const s=this.displayDataGetter?.()??this.dataGetter();if(s.length===0)return{time:e,price:t};const i=!!(n?.data&&n.data.length>0),o=Math.max(0,Math.min(s.length-1,Math.round(i?j(e,s):e))),l=s[o],h=[l.open,l.high,l.low,l.close];let a=h[0],c=Math.abs(t-a);for(let d=1;d<h.length;d++){const f=Math.abs(t-h[d]);f<c&&(c=f,a=h[d])}if(n){const d=n.chartRect.height/(n.priceRange.max-n.priceRange.min||1);if(c*d>30)return{time:i?l.time:o,price:t}}return{time:i?l.time:o,price:a}}undo(){if(!this.undoRedo)return!1;const e=this.undoRedo.undo();return e?(this.applyUndoAction(e),!0):!1}redo(){if(!this.undoRedo)return!1;const 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){const t=this.drawings.findIndex(n=>n.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){const t=this.drawings.findIndex(n=>n.id===e.after.id);t>=0&&(this.drawings[t]=structuredClone(e.after))}break}this.selectedDrawingId=null,this.state="idle",this.requestRender?.()}lockAllDrawings(){for(const e of this.drawings)e.locked=!0;this.requestRender?.()}unlockAllDrawings(){for(const e of this.drawings)e.locked=!1;this.requestRender?.()}hideAllDrawings(){for(const e of this.drawings)e.visible=!1;this.selectedDrawingId=null,this.state="idle",this.requestRender?.()}showAllDrawings(){for(const 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){const n=this.registry.get(this.creatingDrawing.type);if(n&&this.committedAnchors<n.descriptor.requiredAnchors){const s=_(e.x,t),i=V(e.y,t);return this.previewAnchor=this.snapToOHLC(s,i,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){const e=this.drawings.find(t=>t.id===this.selectedDrawingId);e&&this.undoRedo?.push({type:"drawingModify",before:this.dragBeforeState,after:se(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(s=>s.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=this.upgradeLegacyAnchors(e),this.requestRender?.()}upgradeLegacyAnchors(e){const t=this.dataGetter?.();if(!t||t.length===0)return e;const n=1e9;return e.map(s=>({...s,anchors:s.anchors.map(i=>{if(i.time>=n)return i;const o=Math.max(0,Math.min(t.length-1,Math.round(i.time)));return{...i,time:t[o].time}})}))}removeDrawing(e){const t=this.drawings.find(n=>n.id===e);this.drawings=this.drawings.filter(n=>n.id!==e),t&&this.undoRedo?.push({type:"drawingRemove",before:structuredClone(t),after:null}),this.eventCallback?.("drawingRemove",{id:e}),this.requestRender?.()}duplicateDrawing(e){const t=this.drawings.find(i=>i.id===e);if(!t)return null;const n=structuredClone(t);n.id=`tc_drawing_${me++}`;const s=this.computeBarOffsetTime(3);return n.anchors=n.anchors.map(i=>({...i,time:i.time+s})),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(const n of this.drawings){if(!n.visible)continue;const s=this.registry.get(n.type);if(!s)continue;const i=n.id===this.selectedDrawingId;s.render(e,n,t,i)}if(this.creatingDrawing&&this.committedAnchors>0){const n=this.registry.get(this.creatingDrawing.type);if(n){const s={...this.creatingDrawing,anchors:this.previewAnchor?[...this.creatingDrawing.anchors,this.previewAnchor]:[...this.creatingDrawing.anchors]};n.render(e,s,t,!1)}}e.restore()}computeBarOffsetTime(e){const t=this.dataGetter?.();if(!t||t.length<2)return e;const n=Math.max(0,t.length-20),s=[];for(let o=n+1;o<t.length;o++)s.push(t[o].time-t[o-1].time);if(s.length===0)return e;s.sort((o,l)=>o-l);const i=s[Math.floor(s.length/2)];return e*i}handleCreationClick(e,t){if(!this.activeTool)return!1;const n=this.registry.get(this.activeTool);if(!n)return!1;const s=_(e.x,t),i=V(e.y,t),o=this.snapToOHLC(s,i,t);return this.creatingDrawing?(this.creatingDrawing.anchors.push(o),this.committedAnchors++,this.previewAnchor=null,this.committedAnchors>=n.descriptor.requiredAnchors&&this.finalizeCreation(),this.requestRender?.(),!0):(this.creatingDrawing={id:`tc_drawing_${me++}`,type:this.activeTool,anchors:[o],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){const s=this.drawings.find(i=>i.id===this.selectedDrawingId);if(s&&!s.locked){const i=this.registry.get(s.type);if(i){const o=i.hitTestAnchor(e,s,t,8);if(o>=0)return this.state="resizing",this.dragAnchorIndex=o,this.dragStartPoint=e,this.dragBeforeState=se(s),!0;if(i.hitTest(e,s,t,8))return this.state="moving",this.dragStartPoint=e,this.dragStartAnchors=s.anchors.map(l=>({...l})),this.dragBeforeState=se(s),!0}}}for(let s=this.drawings.length-1;s>=0;s--){const i=this.drawings[s];if(!i.visible)continue;const o=this.registry.get(i.type);if(o&&o.hitTest(e,i,t,8))return this.selectedDrawingId=i.id,this.state="selected",this.requestRender?.(),!0}return this.selectedDrawingId&&(this.selectedDrawingId=null,this.state="idle",this.requestRender?.()),!1}handleMove(e,t){const n=this.drawings.find(o=>o.id===this.selectedDrawingId);if(!n||!this.dragStartPoint)return!1;const s=_(e.x,t)-_(this.dragStartPoint.x,t),i=V(e.y,t)-V(this.dragStartPoint.y,t);return n.anchors=this.dragStartAnchors.map(o=>({time:o.time+s,price:o.price+i})),this.requestRender?.(),!0}handleResize(e,t){const n=this.drawings.find(s=>s.id===this.selectedDrawingId);return!n||this.dragAnchorIndex<0?!1:(n.anchors[this.dragAnchorIndex]={time:_(e.x,t),price:V(e.y,t)},this.requestRender?.(),!0)}}class _n{constructor(e){this.manager=e}render(e,t){this.manager.render(e,t)}}class vt extends ${descriptor={type:"trendLine",name:"Trend Line",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(o.x,o.y),e.stroke(),this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n);return this.distanceToLine(e,i,o)<=s}}class Mt extends ${descriptor={type:"horizontalLine",name:"Horizontal Line",requiredAnchors:1};render(e,t,n,s){if(t.anchors.length<1)return;const i=A(t.anchors[0].price,n),{chartRect:o}=n;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(o.x,i),e.lineTo(o.x+o.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),o.x+4,i-3),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<1)return!1;const i=A(t.anchors[0].price,n);return Math.abs(e.y-i)<=s&&e.x>=n.chartRect.x&&e.x<=n.chartRect.x+n.chartRect.width}}class St extends ${descriptor={type:"verticalLine",name:"Vertical Line",requiredAnchors:1};render(e,t,n,s){if(t.anchors.length<1)return;const i=this.anchorToPixel(t.anchors[0],n),{chartRect:o}=n;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,o.y),e.lineTo(i.x,o.y+o.height),e.stroke(),this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<1)return!1;const i=this.anchorToPixel(t.anchors[0],n),{chartRect:o}=n;return Math.abs(e.x-i.x)<=s&&e.y>=o.y&&e.y<=o.y+o.height}}class Pt extends ${descriptor={type:"ray",name:"Ray",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),{chartRect:l}=n,h=o.x-i.x,a=o.y-i.y,c=Math.hypot(h,a);if(c===0)return;const d=Math.hypot(l.width,l.height)*2,f=i.x+h/c*d,u=i.y+a/c*d;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(f,u),e.stroke(),this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=o.x-i.x,h=o.y-i.y;return((e.x-i.x)*l+(e.y-i.y)*h)/(l*l+h*h)<0?!1:this.distanceToInfiniteLine(e,i,o)<=s}}class Rt extends ${descriptor={type:"extendedLine",name:"Extended Line",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),{chartRect:l}=n,h=o.x-i.x,a=o.y-i.y,c=Math.hypot(h,a);if(c===0)return;const d=Math.hypot(l.width,l.height)*2,f=i.x-h/c*d,u=i.y-a/c*d,g=i.x+h/c*d,m=i.y+a/c*d;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(f,u),e.lineTo(g,m),e.stroke(),this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n);return this.distanceToInfiniteLine(e,i,o)<=s}}class kt extends ${descriptor={type:"parallelChannel",name:"Parallel Channel",requiredAnchors:3};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n);if(this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(o.x,o.y),e.stroke(),t.anchors.length>=3){const l=this.anchorToPixel(t.anchors[2],n),h=o.x-i.x,a=o.y-i.y,c=Math.hypot(h,a)||1,d=-a/c,f=h/c,u=(l.x-i.x)*d+(l.y-i.y)*f,g={x:i.x+d*u,y:i.y+f*u},m={x:o.x+d*u,y:o.y+f*u};e.beginPath(),e.moveTo(g.x,g.y),e.lineTo(m.x,m.y),e.stroke(),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(o.x,o.y),e.lineTo(m.x,m.y),e.lineTo(g.x,g.y),e.closePath(),e.fill())}this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n);if(this.distanceToLine(e,i,o)<=s)return!0;if(t.anchors.length>=3){const l=this.anchorToPixel(t.anchors[2],n),h=o.x-i.x,a=o.y-i.y,c=Math.hypot(h,a)||1,d=-a/c,f=h/c,u=(l.x-i.x)*d+(l.y-i.y)*f,g={x:i.x+d*u,y:i.y+f*u},m={x:o.x+d*u,y:o.y+f*u};if(this.distanceToLine(e,g,m)<=s)return!0}return!1}}class wt extends ${descriptor={type:"regressionChannel",name:"Regression Channel",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(o.x,o.y),e.stroke();const l=o.x-i.x,h=o.y-i.y,a=Math.hypot(l,h);if(a===0){this.resetLineStyle(e);return}const c=Math.max(10,Math.min(a*.2,100)),d=-h/a*c,f=l/a*c;e.setLineDash([4,4]),e.beginPath(),e.moveTo(i.x+d,i.y+f),e.lineTo(o.x+d,o.y+f),e.stroke(),e.beginPath(),e.moveTo(i.x-d,i.y-f),e.lineTo(o.x-d,o.y-f),e.stroke(),t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.beginPath(),e.moveTo(i.x+d,i.y+f),e.lineTo(o.x+d,o.y+f),e.lineTo(o.x-d,o.y-f),e.lineTo(i.x-d,i.y-f),e.closePath(),e.fill()),this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=Math.hypot(o.x-i.x,o.y-i.y),h=Math.max(10,Math.min(l*.2,100));return this.distanceToLine(e,i,o)<=s+h}}const K=[0,.236,.382,.5,.618,.786,1];class At extends ${descriptor={type:"fibRetracement",name:"Fibonacci Retracement",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const{chartRect:i}=n,o=t.anchors[0].price,l=t.anchors[1].price,h=o-l;e.font="11px sans-serif",e.textBaseline="middle";for(const a of K){const c=l+h*(1-a),d=A(c,n);this.applyLineStyle(e,t.style),e.globalAlpha=a===0||a===1?1:.7,e.beginPath(),e.moveTo(i.x,d),e.lineTo(i.x+i.width,d),e.stroke(),e.fillStyle=t.style.color,e.textAlign="left",e.fillText(`${(a*100).toFixed(1)}% (${c.toFixed(2)})`,i.x+4,d-2)}if(e.globalAlpha=1,t.style.fillColor){for(let a=0;a<K.length-1;a++){const c=l+h*(1-K[a]),d=l+h*(1-K[a+1]),f=A(c,n),u=A(d,n);e.fillStyle=t.style.fillColor,e.globalAlpha=.05+a*.02,e.fillRect(i.x,Math.min(f,u),i.width,Math.abs(u-f))}e.globalAlpha=1}this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const{chartRect:i}=n;if(e.x<i.x-s||e.x>i.x+i.width+s)return!1;const o=t.anchors[0].price,l=t.anchors[1].price,h=o-l;for(const a of K){const c=l+h*(1-a),d=A(c,n);if(Math.abs(e.y-d)<=s)return!0}return!1}}const ye=[0,.618,1,1.618,2,2.618,3.618,4.236];class Lt extends ${descriptor={type:"fibExtension",name:"Fibonacci Extension",requiredAnchors:3};render(e,t,n,s){if(t.anchors.length<3){if(t.anchors.length===2){const c=this.anchorToPixel(t.anchors[0],n),d=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(c.x,c.y),e.lineTo(d.x,d.y),e.stroke(),this.resetLineStyle(e)}return}const{chartRect:i}=n,o=t.anchors[0].price,l=t.anchors[1].price,h=t.anchors[2].price,a=o-l;e.font="11px sans-serif",e.textBaseline="middle",e.textAlign="left";for(const c of ye){const d=h+a*c,f=A(d,n);this.applyLineStyle(e,t.style),e.globalAlpha=c===0||c===1?1:.6,e.beginPath(),e.moveTo(i.x,f),e.lineTo(i.x+i.width,f),e.stroke(),e.fillStyle=t.style.color,e.fillText(`${(c*100).toFixed(1)}%`,i.x+4,f-2)}e.globalAlpha=1,this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<3)return!1;const{chartRect:i}=n;if(e.x<i.x-s||e.x>i.x+i.width+s)return!1;const o=t.anchors[0].price-t.anchors[1].price;for(const l of ye){const h=t.anchors[2].price+o*l,a=A(h,n);if(Math.abs(e.y-a)<=s)return!0}return!1}}class Ct extends ${descriptor={type:"rectangle",name:"Rectangle",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=Math.min(i.x,o.x),h=Math.min(i.y,o.y),a=Math.abs(o.x-i.x),c=Math.abs(o.y-i.y);t.style.fillColor&&(e.fillStyle=t.style.fillColor,e.fillRect(l,h,a,c)),this.applyLineStyle(e,t.style),e.strokeRect(l,h,a,c),this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=Math.min(i.x,o.x)-s,h=Math.min(i.y,o.y)-s,a=Math.max(i.x,o.x)+s,c=Math.max(i.y,o.y)+s;if(e.x<l||e.x>a||e.y<h||e.y>c)return!1;const d=Math.min(i.x,o.x)+s,f=Math.min(i.y,o.y)+s,u=Math.max(i.x,o.x)-s,g=Math.max(i.y,o.y)-s;return!(e.x>d&&e.x<u&&e.y>f&&e.y<g)||!!t.style.fillColor}}class It extends ${descriptor={type:"ellipse",name:"Ellipse",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=(i.x+o.x)/2,h=(i.y+o.y)/2,a=Math.abs(o.x-i.x)/2,c=Math.abs(o.y-i.y)/2;e.beginPath(),e.ellipse(l,h,Math.max(a,1),Math.max(c,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),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=(i.x+o.x)/2,h=(i.y+o.y)/2,a=Math.max(Math.abs(o.x-i.x)/2,1),c=Math.max(Math.abs(o.y-i.y)/2,1),d=(e.x-l)/a,f=(e.y-h)/c,u=Math.sqrt(d*d+f*f),g=s/Math.max(Math.min(a,c),4);return t.style.fillColor?u<=1+g:Math.abs(u-1)<=g}}class Dt extends ${descriptor={type:"triangle",name:"Triangle",requiredAnchors:3};render(e,t,n,s){if(t.anchors.length<3){if(t.anchors.length===2){const o=this.anchorToPixel(t.anchors[0],n),l=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(o.x,o.y),e.lineTo(l.x,l.y),e.stroke(),this.resetLineStyle(e)}return}const i=t.anchors.map(o=>this.anchorToPixel(o,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),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<3)return!1;const i=t.anchors.map(o=>this.anchorToPixel(o,n));for(let o=0;o<3;o++){const l=(o+1)%3;if(this.distanceToLine(e,i[o],i[l])<=s)return!0}return!1}}class Wt extends ${descriptor={type:"pitchfork",name:"Andrews' Pitchfork",requiredAnchors:3};render(e,t,n,s){if(t.anchors.length<3){if(t.anchors.length===2){const m=this.anchorToPixel(t.anchors[0],n),y=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(m.x,m.y),e.lineTo(y.x,y.y),e.stroke(),this.resetLineStyle(e)}return}const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=this.anchorToPixel(t.anchors[2],n),h={x:(o.x+l.x)/2,y:(o.y+l.y)/2},a=h.x-i.x,c=h.y-i.y,d=Math.hypot(a,c)||1,f=Math.hypot(n.chartRect.width,n.chartRect.height),u=i.x+a/d*f,g=i.y+c/d*f;this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(u,g),e.stroke(),e.beginPath(),e.moveTo(o.x,o.y),e.lineTo(o.x+a/d*f,o.y+c/d*f),e.stroke(),e.beginPath(),e.moveTo(l.x,l.y),e.lineTo(l.x+a/d*f,l.y+c/d*f),e.stroke(),this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<3)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=this.anchorToPixel(t.anchors[2],n),h={x:(o.x+l.x)/2,y:(o.y+l.y)/2};if(this.distanceToInfiniteLine(e,i,h)<=s)return!0;const a=h.x-i.x,c=h.y-i.y,d={x:o.x+a,y:o.y+c},f={x:l.x+a,y:l.y+c};return this.distanceToInfiniteLine(e,o,d)<=s||this.distanceToInfiniteLine(e,l,f)<=s}}const pe=["1","2","3","4","5","A","B","C"];class Bt extends ${descriptor={type:"elliottWave",name:"Elliott Wave",requiredAnchors:8};render(e,t,n,s){if(t.anchors.length<2)return;const i=t.anchors.map(o=>this.anchorToPixel(o,n));this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i[0].x,i[0].y);for(let o=1;o<i.length;o++)e.lineTo(i[o].x,i[o].y);e.stroke(),this.resetLineStyle(e),e.font="bold 12px sans-serif",e.fillStyle=t.style.color,e.textAlign="center";for(let o=0;o<i.length&&o<pe.length;o++){const l=o%2===0?-12:12;e.textBaseline=l<0?"bottom":"top",e.fillText(pe[o],i[o].x,i[o].y+l)}s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=t.anchors.map(o=>this.anchorToPixel(o,n));for(let o=0;o<i.length-1;o++)if(this.distanceToLine(e,i[o],i[o+1])<=s)return!0;return!1}}class Ft extends ${descriptor={type:"priceRange",name:"Price Range",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=Math.min(i.x,o.x),h=Math.abs(o.x-i.x),a=Math.min(i.y,o.y),c=Math.abs(o.y-i.y),d=t.anchors[1].price>t.anchors[0].price;e.fillStyle=d?"rgba(38, 166, 154, 0.15)":"rgba(239, 83, 80, 0.15)",e.fillRect(l,a,h,c),this.applyLineStyle(e,t.style),e.strokeRect(l,a,h,c),this.resetLineStyle(e);const f=t.anchors[1].price-t.anchors[0].price,u=t.anchors[0].price!==0?f/t.anchors[0].price*100:0,g=`${f>=0?"+":""}${f.toFixed(2)} (${u>=0?"+":""}${u.toFixed(2)}%)`;e.font="12px sans-serif",e.fillStyle=d?"#26A69A":"#EF5350",e.textAlign="center",e.textBaseline="middle",e.fillText(g,l+h/2,a+c/2),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=Math.min(i.x,o.x),h=Math.max(i.x,o.x),a=Math.min(i.y,o.y),c=Math.max(i.y,o.y);return e.x>=l-s&&e.x<=h+s&&e.y>=a-s&&e.y<=c+s}}class Et extends ${descriptor={type:"dateRange",name:"Date Range",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),{chartRect:l}=n,h=Math.min(i.x,o.x),a=Math.max(i.x,o.x);e.fillStyle="rgba(33, 150, 243, 0.08)",e.fillRect(h,l.y,a-h,l.height),this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(h,l.y),e.lineTo(h,l.y+l.height),e.stroke(),e.beginPath(),e.moveTo(a,l.y),e.lineTo(a,l.y+l.height),e.stroke(),this.resetLineStyle(e);const c=O(t.anchors[0].time,n),d=O(t.anchors[1].time,n),f=Math.max(0,Math.round(Math.abs(d-c)));e.font="12px sans-serif",e.fillStyle=t.style.color,e.textAlign="center",e.textBaseline="top",e.fillText(`${f} bars`,(h+a)/2,l.y+4),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),{chartRect:l}=n,h=Math.min(i.x,o.x),a=Math.max(i.x,o.x);return e.x>=h-s&&e.x<=a+s&&e.y>=l.y&&e.y<=l.y+l.height}}class Ht extends ${descriptor={type:"measure",name:"Measure",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=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(o.x,o.y),e.stroke(),this.resetLineStyle(e);const l=t.anchors[1].price-t.anchors[0].price,h=O(t.anchors[0].time,n),a=O(t.anchors[1].time,n),c=Math.max(0,Math.round(Math.abs(a-h))),d=t.anchors[0].price!==0?l/t.anchors[0].price*100:0,f=[`Price: ${l>=0?"+":""}${l.toFixed(2)} (${d>=0?"+":""}${d.toFixed(2)}%)`,`Bars: ${c}`],u=(i.x+o.x)/2,g=(i.y+o.y)/2;e.font="11px sans-serif",e.textAlign="center",e.textBaseline="bottom";const m=Math.max(...f.map(y=>e.measureText(y).width));e.fillStyle="rgba(0, 0, 0, 0.7)",e.fillRect(u-m/2-6,g-f.length*14-4,m+12,f.length*14+8),e.fillStyle="#FFFFFF";for(let y=0;y<f.length;y++)e.fillText(f[y],u,g-(f.length-1-y)*14);s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n);return this.distanceToLine(e,i,o)<=s}}function jn(r,e="Text"){if(typeof r.style.text=="string"&&r.style.text.length>0)return r.style.text;const t=r.meta?.text;return typeof t=="string"&&t.length>0?t:e}class $t extends ${descriptor={type:"text",name:"Text",requiredAnchors:1};render(e,t,n,s){if(t.anchors.length<1)return;const i=this.anchorToPixel(t.anchors[0],n),o=jn(t),l=t.style.fontSize||14;if(e.font=`${l}px sans-serif`,e.fillStyle=t.style.color,e.textAlign="left",e.textBaseline="top",s){const h=e.measureText(o);e.strokeStyle=t.style.color,e.lineWidth=1,e.setLineDash([3,3]),e.strokeRect(i.x-2,i.y-2,h.width+4,l+4),e.setLineDash([])}e.fillText(o,i.x,i.y),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<1)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=t.style.fontSize||14;return e.x>=i.x-s&&e.x<=i.x+80+s&&e.y>=i.y-s&&e.y<=i.y+o+s}}class Yt extends ${descriptor={type:"arrow",name:"Arrow",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n);this.applyLineStyle(e,t.style),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(o.x,o.y),e.stroke();const l=Math.atan2(o.y-i.y,o.x-i.x),h=12;e.beginPath(),e.moveTo(o.x,o.y),e.lineTo(o.x-h*Math.cos(l-Math.PI/6),o.y-h*Math.sin(l-Math.PI/6)),e.lineTo(o.x-h*Math.cos(l+Math.PI/6),o.y-h*Math.sin(l+Math.PI/6)),e.closePath(),e.fillStyle=t.style.color,e.fill(),this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n);return this.distanceToLine(e,i,o)<=s}}const be=[{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}];class Vt extends ${descriptor={type:"gannFan",name:"Gann Fan",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=o.x-i.x,h=o.y-i.y;if(Math.abs(l)<1)return;const a=h/l,c=l>0?1:-1,{chartRect:d}=n,f=c>0?d.x+d.width:d.x;this.applyLineStyle(e,t.style);for(const{label:u,ratio:g}of be){const m=a*g,y=f,b=i.y+m*(y-i.x);e.globalAlpha=g===1?1:.5,e.lineWidth=g===1?t.style.lineWidth:Math.max(.5,t.style.lineWidth*.7),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(y,b),e.stroke();const p=y-c*4,T=b;T>d.y&&T<d.y+d.height&&(e.globalAlpha=.7,e.fillStyle=t.style.color,e.font="9px sans-serif",e.textAlign=c>0?"right":"left",e.textBaseline="bottom",e.fillText(u,p,T-2))}e.globalAlpha=1,this.resetLineStyle(e),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=o.x-i.x,h=o.y-i.y;if(Math.abs(l)<1)return!1;const a=h/l,c=l>0?1:-1,{chartRect:d}=n,f=c>0?d.x+d.width:d.x;for(const{ratio:u}of be){const g=a*u,m=f,y=i.y+g*(m-i.x);if(this.distanceToLine(e,i,{x:m,y})<=s)return!0}return!1}}const oe=[0,.25,.333,.5,.667,.75,1];class Ut extends ${descriptor={type:"gannBox",name:"Gann Box",requiredAnchors:2};render(e,t,n,s){if(t.anchors.length<2)return;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=Math.min(i.x,o.x),h=Math.max(i.x,o.x),a=Math.min(i.y,o.y),c=Math.max(i.y,o.y),d=h-l,f=c-a;if(!(d<2||f<2)){this.applyLineStyle(e,t.style),e.strokeRect(l,a,d,f),e.save(),e.globalAlpha=.35;for(const u of oe){if(u===0||u===1)continue;const g=a+f*u;e.beginPath(),e.moveTo(l,g),e.lineTo(h,g),e.stroke()}for(const u of oe){if(u===0||u===1)continue;const g=l+d*u;e.beginPath(),e.moveTo(g,a),e.lineTo(g,c),e.stroke()}e.globalAlpha=.5,e.setLineDash([4,3]),e.beginPath(),e.moveTo(l,a),e.lineTo(h,c),e.stroke(),e.beginPath(),e.moveTo(h,a),e.lineTo(l,c),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(const u of oe){if(u===0||u===1)continue;const g=a+f*u;e.fillText(`${(u*100).toFixed(0)}%`,l-3,g)}e.globalAlpha=1,s&&this.renderAnchorHandles(e,t,n)}}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=Math.min(i.x,o.x),h=Math.max(i.x,o.x),a=Math.min(i.y,o.y),c=Math.max(i.y,o.y);return Math.abs(e.x-l)<=s&&e.y>=a-s&&e.y<=c+s||Math.abs(e.x-h)<=s&&e.y>=a-s&&e.y<=c+s||Math.abs(e.y-a)<=s&&e.x>=l-s&&e.x<=h+s||Math.abs(e.y-c)<=s&&e.x>=l-s&&e.x<=h+s||this.distanceToLine(e,{x:l,y:a},{x:h,y:c})<=s||this.distanceToLine(e,{x:h,y:a},{x:l,y:c})<=s}}class xt extends ${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,s){if(t.anchors.length<1||!this.dataGetter)return;const i=this.dataGetter(),o=Math.max(0,Math.round(O(t.anchors[0].time,n)));if(o>=i.length)return;const{vwap:l,upper1:h,lower1:a,upper2:c,lower2:d}=this.computeVWAP(i,o),{chartRect:f}=n,u=n.barWidth+n.barSpacing;if(this.applyLineStyle(e,t.style),e.lineWidth=t.style.lineWidth+.5,this.drawLine(e,l,o,n,f,u),this.showBands&&l.length>1&&(e.globalAlpha=.4,e.lineWidth=Math.max(.5,t.style.lineWidth*.6),e.setLineDash([4,3]),this.drawLine(e,h,o,n,f,u),this.drawLine(e,a,o,n,f,u),e.globalAlpha=.25,this.drawLine(e,c,o,n,f,u),this.drawLine(e,d,o,n,f,u),e.globalAlpha=1),this.resetLineStyle(e),l.length>0){const g=l[l.length-1],m=o+l.length-1,y=D(m,n),b=A(g,n);y>=f.x&&y<=f.x+f.width&&(e.fillStyle=t.style.color,e.font="bold 9px sans-serif",e.textAlign="left",e.textBaseline="middle",e.fillText(`AVWAP ${g.toFixed(2)}`,y+6,b))}s&&this.renderAnchorHandles(e,t,n)}drawLine(e,t,n,s,i,o){e.beginPath();let l=!1;for(let h=0;h<t.length;h++){const a=D(n+h,s);if(a<i.x-o)continue;if(a>i.x+i.width+o)break;const c=A(t[h],s);l?e.lineTo(a,c):(e.moveTo(a,c),l=!0)}e.stroke()}computeVWAP(e,t){const n=e.length-t,s=new Array(n),i=new Array(n),o=new Array(n),l=new Array(n),h=new Array(n);let a=0,c=0,d=0;for(let f=0;f<n;f++){const u=e[t+f],g=(u.high+u.low+u.close)/3,m=u.volume??1;a+=g*m,c+=m,d+=g*g*m;const y=c>0?a/c:g;s[f]=y;const b=c>0?d/c-y*y:0,p=Math.sqrt(Math.max(0,b));i[f]=y+p,o[f]=y-p,l[f]=y+2*p,h[f]=y-2*p}return{vwap:s,upper1:i,lower1:o,upper2:l,lower2:h}}hitTest(e,t,n,s){if(t.anchors.length<1||!this.dataGetter)return!1;const i=this.dataGetter(),o=Math.max(0,Math.round(O(t.anchors[0].time,n)));if(o>=i.length)return!1;const{vwap:l}=this.computeVWAP(i,o);for(let h=1;h<l.length;h++){const a=D(o+h-1,n),c=A(l[h-1],n),d=D(o+h,n),f=A(l[h],n);if(this.distanceToLine(e,{x:a,y:c},{x:d,y:f})<=s)return!0}return!1}}class Ot extends ${descriptor={type:"volumeProfileRange",name:"Volume Profile (Range)",requiredAnchors:2};dataGetter=null;numBins=40;setDataGetter(e){this.dataGetter=e}render(e,t,n,s){if(t.anchors.length<2||!this.dataGetter)return;const i=this.dataGetter(),o=Math.round(O(t.anchors[0].time,n)),l=Math.round(O(t.anchors[1].time,n)),h=Math.max(0,Math.min(o,l)),a=Math.min(i.length-1,Math.max(o,l));if(h>=i.length||a<0||h>a)return;let c=1/0,d=-1/0;for(let P=h;P<=a;P++)i[P].low<c&&(c=i[P].low),i[P].high>d&&(d=i[P].high);if(c>=d)return;const f=(d-c)/this.numBins,u=new Float64Array(this.numBins);let g=0;for(let P=h;P<=a;P++){const R=i[P],w=R.volume??1,k=Math.max(0,Math.floor((R.low-c)/f)),C=Math.min(this.numBins-1,Math.floor((R.high-c)/f)),I=C-k+1,B=w/I;for(let F=k;F<=C;F++)u[F]+=B,u[F]>g&&(g=u[F])}if(g===0)return;let m=0;for(let P=1;P<this.numBins;P++)u[P]>u[m]&&(m=P);const y=D(a,n)+n.barWidth,b=100,p=t.style.color,T=t.style.fillColor??"#FF9800";e.save();for(let P=0;P<this.numBins;P++){const w=u[P]/g*b,k=c+(P+1)*f,C=c+P*f,I=A(k,n),B=A(C,n),F=Math.abs(B-I);e.globalAlpha=.35,e.fillStyle=P===m?T:p,e.fillRect(y,Math.min(I,B),w,Math.max(1,F-.5))}const v=c+(m+.5)*f,S=A(v,n);e.globalAlpha=.7,e.strokeStyle=T,e.lineWidth=1,e.setLineDash([4,3]),e.beginPath();const M=D(h,n)-n.barWidth/2;e.moveTo(M,S),e.lineTo(y+b,S),e.stroke(),e.fillStyle=T,e.font="9px sans-serif",e.textAlign="left",e.textBaseline="bottom",e.globalAlpha=.8,e.fillText(`POC ${v.toFixed(2)}`,y+b+4,S-2),e.restore(),s&&this.renderAnchorHandles(e,t,n)}hitTest(e,t,n,s){if(t.anchors.length<2)return!1;const i=this.anchorToPixel(t.anchors[0],n),o=this.anchorToPixel(t.anchors[1],n),l=Math.min(i.x,o.x)-s,h=Math.max(i.x,o.x)+100+s,a=Math.min(i.y,o.y)-s,c=Math.max(i.y,o.y)+s;return e.x>=l&&e.x<=h&&e.y>=a&&e.y<=c}}function Gn(r){r.register(new vt),r.register(new Mt),r.register(new St),r.register(new Pt),r.register(new Rt),r.register(new kt),r.register(new wt),r.register(new At),r.register(new Lt),r.register(new Ct),r.register(new It),r.register(new Dt),r.register(new Wt),r.register(new Bt),r.register(new Ft),r.register(new Et),r.register(new Ht),r.register(new $t),r.register(new Yt),r.register(new Vt),r.register(new Ut),r.register(new xt),r.register(new Ot)}class qt{render(e,t,n,s,i,o){const l=i.orderColors?.buy??"#26A69A",h=i.orderColors?.sell??"#EF5350",{chartRect:a}=n;for(const c of t){const d=o?.orderId===c.id&&o.sourceType==="order",f=d?o.currentPrice:c.price,u=A(f,n);if(u<a.y||u>a.y+a.height)continue;const g=c.side==="buy"?l:h;e.setLineDash([6,4]),e.strokeStyle=g,e.lineWidth=1,e.beginPath(),e.moveTo(a.x,Math.round(u)+.5),e.lineTo(a.x+a.width,Math.round(u)+.5),e.stroke(),e.setLineDash([]);const m=c.label??c.type.toUpperCase();e.font=`bold 10px ${s.font.family}`;const y=e.measureText(m).width+8;e.fillStyle=g,e.fillRect(a.x+4,u-8,y,16),e.fillStyle="#FFFFFF",e.textBaseline="middle",e.textAlign="left",e.fillText(m,a.x+8,u);const b=a.x+4+y+6;if(e.fillStyle=g,e.beginPath(),c.side==="buy"?(e.moveTo(b,u+4),e.lineTo(b+5,u-2),e.lineTo(b+10,u+4)):(e.moveTo(b,u-4),e.lineTo(b+5,u+2),e.lineTo(b+10,u-4)),e.fill(),d){const p=A(o.startPrice,n);e.setLineDash([2,4]),e.strokeStyle=g,e.globalAlpha=.3,e.beginPath(),e.moveTo(a.x,Math.round(p)+.5),e.lineTo(a.x+a.width,Math.round(p)+.5),e.stroke(),e.globalAlpha=1,e.setLineDash([])}}}renderAxisBadges(e,t,n,s,i,o){const l=i.orderColors?.buy??"#26A69A",h=i.orderColors?.sell??"#EF5350",a=i.pricePrecision??2,{chartRect:c}=n,d=c.x+c.width+1;for(const f of t){const g=o?.orderId===f.id&&o.sourceType==="order"?o.currentPrice:f.price,m=A(g,n);if(m<c.y||m>c.y+c.height)continue;const y=f.side==="buy"?l:h,b=`${g.toFixed(a)} x ${f.quantity}`;e.font=`11px ${s.font.family}`;const p=e.measureText(b).width+10;e.fillStyle=y,e.fillRect(d,m-9,Math.min(p,L.PRICE_AXIS_WIDTH-2),18),e.fillStyle="#FFFFFF",e.textAlign="left",e.textBaseline="middle",e.fillText(b,d+5,m)}}hitTest(e,t,n,s){for(const i of t){if(i.draggable===!1)continue;const o=A(i.price,n);if(Math.abs(e-o)<=s)return i}return null}}function Nn(r,e,t){const n=Math.min(Math.max(r.closedQuantity??0,0),r.quantity),s=r.quantity-n,i=r.side==="buy"?1:-1,o=(e-r.entryPrice)*s*i,l=r.entryPrice===0?0:(e-r.entryPrice)/r.entryPrice*100*i;return{side:r.side,quantity:r.quantity,closedQuantity:n,openQuantity:s,entryPrice:r.entryPrice,currentPrice:e,pnl:o,pnlPct:l,precision:t}}const Jn=/\{(side|qty|closedQty|openQty|entry|price|pnl|pnlPct|pnlSign)\}/g;function Kn(r,e){return r.replace(Jn,(t,n)=>{switch(n){case"side":return e.side.toUpperCase();case"qty":return String(e.quantity);case"closedQty":return String(e.closedQuantity);case"openQty":return String(e.openQuantity);case"entry":return e.entryPrice.toFixed(e.precision);case"price":return e.currentPrice.toFixed(e.precision);case"pnl":return e.pnl.toFixed(e.precision);case"pnlPct":return`${e.pnlPct.toFixed(2)}%`;case"pnlSign":return e.pnl>=0?"+":"";default:return""}})}const Zn="{side} {qty} | P&L: {pnlSign}{pnl}";function Qn(r,e){return typeof r=="function"?r(e):Kn(r??Zn,e)}function ei(r,e,t){if(!e||e.length===0)return t;const n=[...e].sort((i,o)=>i.pnl-o.pnl);let s;for(const i of n)if(r>=i.pnl)s=i.color;else break;return s??t}class zt{render(e,t,n,s,i,o){const{chartRect:l}=s,h=o.positionColors?.profit??"#26A69A",a=o.positionColors?.loss??"#EF5350",c=o.positionColors?.entry??"#2196F3",d=o.pricePrecision??2;for(const f of t){const u=A(f.entryPrice,s);if(e.strokeStyle=c,e.lineWidth=1.5,e.setLineDash([]),e.beginPath(),e.moveTo(l.x,Math.round(u)+.5),e.lineTo(l.x+l.width,Math.round(u)+.5),e.stroke(),n!==null){const g=A(n,s),m=Nn(f,n,d),y=m.pnl,p=y>=0?h:a,T=ei(y,o.pnlThresholds,p);e.fillStyle=T,e.globalAlpha=.08;const v=Math.min(u,g),S=Math.abs(g-u);if(e.fillRect(l.x,v,l.width,S),e.globalAlpha=1,m.closedQuantity>0&&f.quantity>0){const w=m.closedQuantity/f.quantity;e.fillStyle=c,e.globalAlpha=.25,e.fillRect(l.x,v,Math.max(2,l.width*w*.04)+2,S),e.globalAlpha=1}const M=Qn(o.positionLabel,m);e.font=`bold 11px ${i.font.family}`;const P=e.measureText(M).width+12,R=l.x+8;e.fillStyle=T,e.globalAlpha=.9,e.fillRect(R,u-10,P,20),e.globalAlpha=1,e.fillStyle="#FFFFFF",e.textBaseline="middle",e.textAlign="left",e.fillText(M,R+6,u)}if(f.stopLoss!==void 0){const g=A(f.stopLoss,s);e.setLineDash([4,4]),e.strokeStyle=a,e.lineWidth=1,e.beginPath(),e.moveTo(l.x,Math.round(g)+.5),e.lineTo(l.x+l.width,Math.round(g)+.5),e.stroke(),e.font=`bold 10px ${i.font.family}`,e.fillStyle=a,e.fillRect(l.x+4,g-8,24,16),e.fillStyle="#FFFFFF",e.textAlign="left",e.textBaseline="middle",e.fillText("SL",l.x+8,g)}if(f.takeProfit!==void 0){const g=A(f.takeProfit,s);e.setLineDash([4,4]),e.strokeStyle=h,e.lineWidth=1,e.beginPath(),e.moveTo(l.x,Math.round(g)+.5),e.lineTo(l.x+l.width,Math.round(g)+.5),e.stroke(),e.font=`bold 10px ${i.font.family}`,e.fillStyle=h,e.fillRect(l.x+4,g-8,24,16),e.fillStyle="#FFFFFF",e.textAlign="left",e.textBaseline="middle",e.fillText("TP",l.x+8,g)}e.setLineDash([])}}renderAxisBadges(e,t,n,s,i){const{chartRect:o}=n,l=i.positionColors?.entry??"#2196F3",h=i.pricePrecision??2,a=o.x+o.width+1;for(const c of t){const d=A(c.entryPrice,n);e.fillStyle=l,e.fillRect(a,d-9,L.PRICE_AXIS_WIDTH-2,18),e.fillStyle="#FFFFFF",e.font=`11px ${s.font.family}`,e.textAlign="left",e.textBaseline="middle",e.fillText(c.entryPrice.toFixed(h),a+5,d)}}}class Xt{render(e,t,n,s){if(!s.depthOverlay?.enabled)return;const{chartRect:i}=n,o=s.depthOverlay.bidColor??"rgba(38,166,154,0.15)",l=s.depthOverlay.askColor??"rgba(239,83,80,0.15)",h=s.depthOverlay.maxWidth??100;let a=0;const c=[];let d=0;for(const m of t.bids)d+=m.volume,c.push({price:m.price,cumVol:d}),d>a&&(a=d);const f=[];d=0;for(const m of t.asks)d+=m.volume,f.push({price:m.price,cumVol:d}),d>a&&(a=d);if(a===0)return;const u=Math.max(1,t.bids.length>1?Math.abs(A(t.bids[0].price,n)-A(t.bids[1].price,n)):4),g=i.x+i.width;e.fillStyle=o;for(const{price:m,cumVol:y}of c){const b=A(m,n);if(b<i.y||b>i.y+i.height)continue;const p=y/a*h;e.fillRect(g-p,b-u/2,p,u)}e.fillStyle=l;for(const{price:m,cumVol:y}of f){const b=A(m,n);if(b<i.y||b>i.y+i.height)continue;const p=y/a*h;e.fillRect(g-p,b-u/2,p,u)}}}class _t{constructor(e=3){this.dragThreshold=e}dragState=null;startY=0;dragging=!1;onPointerDown(e,t,n,s,i){for(const o of t){if(o.draggable===!1)continue;const l=A(o.price,s);if(Math.abs(e.y-l)<=i)return this.dragState={orderId:o.id,sourceType:"order",startPrice:o.price,currentPrice:o.price},this.startY=e.y,this.dragging=!1,!0}for(const o of n){if(o.stopLoss!==void 0){const l=A(o.stopLoss,s);if(Math.abs(e.y-l)<=i)return this.dragState={orderId:o.id,sourceType:"stopLoss",startPrice:o.stopLoss,currentPrice:o.stopLoss},this.startY=e.y,this.dragging=!1,!0}if(o.takeProfit!==void 0){const l=A(o.takeProfit,s);if(Math.abs(e.y-l)<=i)return this.dragState={orderId:o.id,sourceType:"takeProfit",startPrice:o.takeProfit,currentPrice:o.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=V(e.y,t),!0}onPointerUp(){if(!this.dragState)return null;const 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}}class jt{menuElement=null;removeHandler=null;onItemSelect=null;show(e,t,n,s){if(this.hide(),!s.contextMenu?.enabled)return;const i=s.pricePrecision??2,o=t.toFixed(i),l=document.createElement("div");l.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}function u(e,t){let n=t.data;return n&&n.length>0?l(e,n):e}function d(e,t){return a(u(e,t),t)}function f(e,t){let n=s(e,t),r=t.data;return r&&r.length>0?r[Math.max(0,Math.min(r.length-1,n))].time:n}var p=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,u=n.numberLocale??`en-US`;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.tradeZoneManager?.render(e,r,i),n.drawingRenderer?.render(e,r),n.tradingRenderer?.render(e,r,i),n.signalMarkerManager?.render(e,r,i),n.alertManager?.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,d=`${i.font.sizeSmall}px ${i.font.family}`,f;if(a){let e=a.getPanelIndicators();f=new Map;for(let t of e)f.set(t.instanceId,t)}for(let p of n.panels){let n=p.viewport,m=p.rect,{min:h,max:g}=n.priceRange;if(g-h<=0||m.height<=0)continue;let _=m.x+m.width,v=n.chartRect;t.strokeStyle=i.axisLine,t.lineWidth=1,t.beginPath(),t.moveTo(_+.5,m.y),t.lineTo(_+.5,m.y+m.height),t.stroke();let y=(0,e.computeTickStep)(h,g,4),b=Math.ceil(h/y)*y,x=y<1?Math.ceil(-Math.log10(y))+1:+(y<10);t.font=d,t.textBaseline=`middle`,t.textAlign=`left`;for(let r=b;r<=g;r+=y){let a=o(r,n);a<v.y||a>v.y+v.height||(t.strokeStyle=i.axisLine,t.beginPath(),t.moveTo(_,Math.round(a)+.5),t.lineTo(_+4,Math.round(a)+.5),t.stroke(),t.fillStyle=i.axisLabel,t.fillText((0,e.formatPrice)(r,x,u),_+6,a))}if(r&&r.x>=m.x&&r.x<=m.x+m.width&&r.y>=m.y&&r.y<=m.y+m.height){let a=(0,e.formatPrice)(c(r.y,n),x,u);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(_+1,r.y-9,s,18),t.fillStyle=i.background,t.textBaseline=`middle`,t.textAlign=`left`,t.fillText(a,_+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(p.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(x)}`)}if(n.length>0){let e=f?.get(p.instanceId),r=e?t.measureText(e.descriptor.name).width+14:10;t.font=d,t.fillStyle=i.textSecondary,t.textBaseline=`top`,t.textAlign=`left`,t.fillText(n.join(` `),m.x+r,m.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()}},m=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;let o=this.computeChartRect(t,n);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:o}}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 h(t,n){return(0,e.sliceVisibleData)(t,n.visibleRange.from,n.visibleRange.to)}var 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,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)}},_=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()}},v=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()}},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;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)}},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,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)}},x=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}},S=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)}},C=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)}},w=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)}},T=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,{min:o,max:s}=n.priceRange,c=s-o;if(c===0||i>a||t.length===0)return;let l=n.barWidth+n.barSpacing,u=-n.offset+n.chartRect.x+n.barWidth/2,d=n.chartRect.y,f=n.chartRect.height/c,p=n.barWidth,m=0;for(let e=i;e<=a&&e<t.length;e++)t[e].volume>m&&(m=t[e].volume);m===0&&(m=1);let h=new Path2D,g=new Path2D,_=new Path2D,v=new Path2D,y=e=>e*l+u,b=e=>d+(s-e)*f;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=y(e),i=b(n.high),a=b(n.low),o=b(n.open),s=b(n.close),c=n.close>=n.open,l=p*Math.max(.2,n.volume/m),u=l/2,d=c?h:g;d.moveTo(r,i),d.lineTo(r,a);let f=c?s:o,x=Math.max(Math.abs(s-o),1);(c?_:v).rect(r-u,f,l,x)}e.strokeStyle=r.candleUpWick,e.lineWidth=1,e.stroke(h),e.strokeStyle=r.candleDownWick,e.stroke(g),e.fillStyle=r.candleUp,e.fill(_),e.fillStyle=r.candleDown,e.fill(v)}},E=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange,{min:o,max:s}=n.priceRange,c=s-o;if(c===0||i>a||t.length===0)return;let l=n.barWidth+n.barSpacing,u=-n.offset+n.chartRect.x+n.barWidth/2,d=n.chartRect.y,f=n.chartRect.height/c,p=n.barWidth,m=0;for(let e=i;e<=a&&e<t.length;e++)t[e].volume>m&&(m=t[e].volume);m===0&&(m=1);let h=new Path2D,g=new Path2D,_=new Path2D,v=new Path2D,y=e=>e*l+u,b=e=>d+(s-e)*f;for(let e=i;e<=a&&e<t.length;e++){let n=t[e],r=e>0?t[e-1]:n,i=y(e),a=b(n.high),o=b(n.low),s=n.close>=r.close,c=p*Math.max(.15,n.volume/m),l=c/2,u=Math.max(o-a,1),d=s?h:g,f=s?_:v;d.rect(i-l,a,c,u),f.rect(i-l,a,c,u)}e.fillStyle=D(r.candleUp,.55),e.fill(h),e.fillStyle=D(r.candleDown,.55),e.fill(g),e.lineWidth=1,e.strokeStyle=r.candleUpWick,e.stroke(_),e.strokeStyle=r.candleDownWick,e.stroke(v)}};function D(e,t){if(e.startsWith(`#`)){let n=Math.round(Math.max(0,Math.min(1,t))*255).toString(16).padStart(2,`0`);if(e.length===7)return`${e}${n}`;if(e.length===9)return`${e.slice(0,7)}${n}`}return e}var O=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange;if(i>a||t.length===0)return;let{min:o,max:s}=n.priceRange,c=s-o;if(c===0)return;let l=n.barWidth+n.barSpacing,u=-n.offset+n.chartRect.x+n.barWidth/2,d=n.chartRect.y,f=n.chartRect.height/c,p=e=>e*l+u,m=e=>d+(s-e)*f,h=[],g=[];for(let e=i;e<=a&&e<t.length;e++){let n=p(e);h.push({x:n,y:m(t[e].high)}),g.push({x:n,y:m(t[e].low)})}if(h.length===0)return;let _=e.createLinearGradient(0,d,0,d+n.chartRect.height);_.addColorStop(0,r.areaTopColor),_.addColorStop(1,r.areaBottomColor),e.beginPath(),e.moveTo(h[0].x,h[0].y);for(let t=1;t<h.length;t++)e.lineTo(h[t].x,h[t].y);for(let t=g.length-1;t>=0;t--)e.lineTo(g[t].x,g[t].y);e.closePath(),e.fillStyle=_,e.fill(),e.beginPath(),e.strokeStyle=r.lineColor,e.lineWidth=2,e.lineJoin=`round`;for(let n=i;n<=a&&n<t.length;n++){let r=p(n),a=m(t[n].close);n===i?e.moveTo(r,a):e.lineTo(r,a)}e.stroke()}},k=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange;if(i>a||t.length===0)return;let{min:o,max:s}=n.priceRange,c=s-o;if(c===0)return;let l=n.barWidth+n.barSpacing,u=-n.offset+n.chartRect.x+n.barWidth/2,d=n.chartRect.y,f=n.chartRect.height/c,p=e=>e*l+u,m=e=>d+(s-e)*f;e.beginPath(),e.strokeStyle=r.lineColor,e.lineWidth=2,e.lineJoin=`miter`;let h=0;for(let n=i;n<=a&&n<t.length;n++){let r=p(n),a=m(t[n].close);n===i?e.moveTo(r,a):(e.lineTo(r,h),e.lineTo(r,a)),h=a}e.stroke()}},A=class{render(e,t,n,r){let{from:i,to:a}=n.visibleRange;if(i>a||t.length===0)return;let{min:o,max:s}=n.priceRange,c=s-o;if(c===0)return;let l=n.barWidth+n.barSpacing,u=-n.offset+n.chartRect.x+n.barWidth/2,d=n.chartRect.y,f=n.chartRect.height/c,p=e=>e*l+u,m=e=>d+(s-e)*f;e.beginPath(),e.strokeStyle=r.lineColor,e.lineWidth=2,e.lineJoin=`round`;let h=[];for(let n=i;n<=a&&n<t.length;n++){let r=p(n),a=m(t[n].close);h.push({x:r,y:a}),n===i?e.moveTo(r,a):e.lineTo(r,a)}e.stroke();let g=Math.max(2,Math.min(4,n.barWidth*.3)),_=new Path2D;for(let{x:e,y:t}of h)_.moveTo(e+g,t),_.arc(e,t,g,0,Math.PI*2);e.fillStyle=r.lineColor,e.fill(_),e.strokeStyle=r.background,e.lineWidth=1,e.stroke(_)}},ee=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}},te=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 ne(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 re(e,t){if(e.length===0)return[];let n=t.brickSize;t.useATR&&e.length>(t.atrPeriod??14)&&(n=ie(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 ie(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 ae(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 oe(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 se(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}function ce(e,t){if(e.length===0||t.rangeSize<=0)return[];let n=[],r=e[0].time,i=e[0].open,a=i,o=i,s=0,c=(e,c)=>{for(e>a&&(a=e),e<o&&(o=e),s+=c;a-o>=t.rangeSize;){let c=e>=i,l=c?i+t.rangeSize:i-t.rangeSize;n.push({time:r++,open:i,high:c?l:i,low:c?i:l,close:l,volume:s}),i=l,a=l,o=l,s=0}};for(let t of e){let e=t.close>=t.open?[t.low,t.high,t.close]:[t.high,t.low,t.close],n=t.volume/e.length;for(let t of e)c(t,n)}return n}var j=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}},le=1,ue=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}_${le++}`,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}};function M(e,t,n){let r=e.params[t];if(typeof r==`number`&&Number.isFinite(r))return r;if(typeof r==`string`){let e=Number(r);if(Number.isFinite(e))return e}return n}function N(e,t,n,r=1){let i=M(e,t,n);return Math.max(r,Math.floor(i))}var de=class extends j{descriptor={id:`sma`,name:`Simple Moving Average`,placement:`overlay`,defaultConfig:{period:20}};calculate(e,t){let n=N(t,`period`,20,1),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()}},fe=class extends j{descriptor={id:`ema`,name:`Exponential Moving Average`,placement:`overlay`,defaultConfig:{period:20}};calculate(e,t){let n=N(t,`period`,20,1),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()}},pe=class extends j{descriptor={id:`bb`,name:`Bollinger Bands`,placement:`overlay`,defaultConfig:{period:20,stdDev:2}};calculate(e,t){let n=N(t,`period`,20,1),r=M(t,`stdDev`,2),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()}}},me=class extends j{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()}},he=class extends j{descriptor={id:`ichimoku`,name:`Ichimoku Cloud`,placement:`overlay`,defaultConfig:{tenkan:9,kijun:26,senkou:52,displacement:26}};calculate(e,t){let n=N(t,`tenkan`,9,1),r=N(t,`kijun`,26,1),i=N(t,`senkou`,52,1),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)}},ge=class extends j{descriptor={id:`psar`,name:`Parabolic SAR`,placement:`overlay`,defaultConfig:{step:.02,max:.2}};calculate(e,t){let n=M(t,`step`,.02),r=M(t,`max`,.2),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()}}},_e=class extends j{descriptor={id:`supertrend`,name:`Supertrend`,placement:`overlay`,defaultConfig:{period:10,multiplier:3}};calculate(e,t){let n=N(t,`period`,10,1),r=M(t,`multiplier`,3),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}}},ve=class extends j{descriptor={id:`keltner`,name:`Keltner Channel`,placement:`overlay`,defaultConfig:{emaPeriod:20,atrPeriod:10,multiplier:1.5}};calculate(e,t){let n=N(t,`emaPeriod`,20,1),r=N(t,`atrPeriod`,10,1),i=M(t,`multiplier`,1.5),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])}},ye=class extends j{descriptor={id:`donchian`,name:`Donchian Channel`,placement:`overlay`,defaultConfig:{period:20}};calculate(e,t){let n=N(t,`period`,20,1),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])}},be=[`s3`,`s2`,`s1`,`pp`,`r1`,`r2`,`r3`],xe=class extends j{descriptor={id:`pivots`,name:`Pivot Points (Classic)`,placement:`overlay`,defaultConfig:{lookback:24}};calculate(e,t){let n=N(t,`lookback`,24,1),r=new Map,i=Array(e.length).fill(null);if(e.length<=n)return{values:r,series:i};for(let t=n;t<e.length;t++){let a=-1/0,o=1/0;for(let r=t-n;r<t;r++)e[r].high>a&&(a=e[r].high),e[r].low<o&&(o=e[r].low);let s=e[t-1].close,c=(a+o+s)/3,l=a-o,u={pp:c,r1:2*c-o,s1:2*c-a,r2:c+l,s2:c-l,r3:a+2*(c-o),s3:o-2*(a-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{from:s,to:c}=n.visibleRange,l=r.colors[0],u=r.colors[1]??`#EF5350`,d=r.colors[2]??`#26A69A`,f=r.lineWidths[0],p=e=>e===`pp`?l:e.startsWith(`r`)?u:d;for(let t of be){e.beginPath(),e.strokeStyle=p(t),e.lineWidth=t===`pp`?f:Math.max(1,f-1),t!==`pp`&&e.setLineDash([4,3]);let r=!1;for(let l=s;l<=c&&l<i.length;l++){let s=i[l]?.[t];if(s===void 0)continue;let c=a(l,n),u=o(s,n);r?e.lineTo(c,u):(e.moveTo(c,u),r=!0)}e.stroke(),e.setLineDash([])}}},Se=class extends j{descriptor={id:`avwap`,name:`Anchored VWAP`,placement:`overlay`,defaultConfig:{anchorTime:0}};calculate(e,t){let n=M(t,`anchorTime`,0),r=new Map,i=Array(e.length).fill(null),a=0;if(n>0&&(a=e.findIndex(e=>e.time>=n),a<0))return{values:r,series:i};let o=0,s=0;for(let t=a;t<e.length;t++){let n=(e[t].high+e[t].low+e[t].close)/3;if(o+=e[t].volume,s+=n*e[t].volume,o>0){let n={value:s/o};r.set(e[t].time,n),i[t]=n}}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()}},Ce=class extends j{descriptor={id:`zigzag`,name:`ZigZag`,placement:`overlay`,defaultConfig:{deviation:5}};calculate(e,t){let n=Math.max(.01,M(t,`deviation`,5))/100,r=new Map,i=Array(e.length).fill(null);if(e.length<2)return{values:r,series:i,meta:{pivots:[]}};let a=[],o=null,s=0,c=e[0].close;for(let t=1;t<e.length;t++){let r=e[t].high,i=e[t].low;if(o===null){r>=c*(1+n)?(a.push({idx:s,price:c,type:`low`}),o=`up`,s=t,c=r):i<=c*(1-n)&&(a.push({idx:s,price:c,type:`high`}),o=`down`,s=t,c=i);continue}o===`up`?r>c?(s=t,c=r):i<=c*(1-n)&&(a.push({idx:s,price:c,type:`high`}),o=`down`,s=t,c=i):i<c?(s=t,c=i):r>=c*(1+n)&&(a.push({idx:s,price:c,type:`low`}),o=`up`,s=t,c=r)}a.push({idx:s,price:c,type:o===`up`?`high`:`low`});for(let t of a){let n={pivot:t.price};r.set(e[t.idx].time,n),i[t.idx]=n}return{values:r,series:i,meta:{pivots:a}}}render(e,t,n,r){let i=t.meta?.pivots;if(!(!i||i.length<2)){e.beginPath(),e.strokeStyle=r.colors[0],e.lineWidth=r.lineWidths[0],e.lineJoin=`round`;for(let t=0;t<i.length;t++){let r=i[t],s=a(r.idx,n),c=o(r.price,n);t===0?e.moveTo(s,c):e.lineTo(s,c)}e.stroke()}}},we=class extends j{descriptor={id:`lrc`,name:`Linear Regression Channel`,placement:`overlay`,defaultConfig:{period:100,stdDev:2}};calculate(e,t){let n=N(t,`period`,100,2),r=M(t,`stdDev`,2),i=new Map,a=Array(e.length).fill(null);if(e.length<n)return{values:i,series:a};for(let t=n-1;t<e.length;t++){let o=t-n+1,s=0,c=0,l=0,u=0;for(let t=0;t<n;t++){let n=t,r=e[o+t].close;s+=n,c+=r,l+=n*r,u+=n*n}let d=n*u-s*s;if(d===0)continue;let f=(n*l-s*c)/d,p=(c-f*s)/n,m=0;for(let t=0;t<n;t++){let n=f*t+p,r=e[o+t].close-n;m+=r*r}let h=Math.sqrt(m/n),g=f*(n-1)+p,_={middle:g,upper:g+r*h,lower:g-r*h,slope:f};i.set(e[t].time,_),a[t]=_}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=[],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.middle!==void 0&&d.push({x:n,y:o(t.middle,r)}),t.upper!==void 0&&u.push({x:n,y:o(t.upper,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])}},Te=class extends j{descriptor={id:`hma`,name:`Hull Moving Average`,placement:`overlay`,defaultConfig:{period:21}};calculate(e,t){let n=N(t,`period`,21,2),r=new Map,i=Array(e.length).fill(null);if(e.length<n)return{values:r,series:i};let a=e.map(e=>e.close),o=Math.max(2,Math.floor(n/2)),s=Math.max(2,Math.floor(Math.sqrt(n))),c=Ee(a,n),l=Ee(a,o),u=De(a.map((e,t)=>c[t]===null||l[t]===null?null:2*l[t]-c[t]),s);for(let t=0;t<e.length;t++){if(u[t]===null)continue;let n={value:u[t]};r.set(e[t].time,n),i[t]=n}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()}};function Ee(e,t){let n=Array(e.length).fill(null);if(e.length<t)return n;let r=t*(t+1)/2;for(let i=t-1;i<e.length;i++){let a=0;for(let n=0;n<t;n++)a+=e[i-n]*(t-n);n[i]=a/r}return n}function De(e,t){let n=Array(e.length).fill(null),r=t*(t+1)/2;for(let i=t-1;i<e.length;i++){let a=0,o=!0;for(let n=0;n<t;n++){let r=e[i-n];if(r===null){o=!1;break}a+=r*(t-n)}o&&(n[i]=a/r)}return n}var Oe=class extends j{descriptor={id:`ao`,name:`Awesome Oscillator`,placement:`panel`,defaultConfig:{fast:5,slow:34}};calculate(e,t){let n=N(t,`fast`,5,1),r=N(t,`slow`,34,1),i=new Map,a=Array(e.length).fill(null);if(e.length<r)return{values:i,series:a};let o=Array(e.length);for(let t=0;t<e.length;t++)o[t]=(e[t].high+e[t].low)/2;let s=0,c=0,l;for(let t=0;t<e.length;t++)if(c+=o[t],t>=r&&(c-=o[t-r]),s+=o[t],t>=n&&(s-=o[t-n]),t>=r-1){let o=s/n-c/r,u={value:o,up:+(l===void 0||o>=l)};i.set(e[t].time,u),a[t]=u,l=o}return{values:i,series:a}}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]?.value;t!==void 0&&(t<l&&(l=t),t>u&&(u=t))}if(l===1/0)return;let d=Math.max(Math.abs(l),Math.abs(u))||1,f=e=>o.y+o.height*(1-(e+d)/(2*d)),p=f(0),m=n.barWidth/2,h=r.colors[0]??`#26A69A`,g=r.colors[1]??`#EF5350`;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=f(r.value),c=Math.min(s,p),l=Math.max(Math.abs(s-p),1);e.fillStyle=r.up?h:g,e.fillRect(o-m,c,n.barWidth,l)}}},ke=class extends j{descriptor={id:`chaikinOsc`,name:`Chaikin Oscillator`,placement:`panel`,defaultConfig:{fast:3,slow:10}};calculate(e,t){let n=N(t,`fast`,3,1),r=N(t,`slow`,10,1),i=new Map,a=Array(e.length).fill(null);if(e.length<r)return{values:i,series:a};let o=2/(n+1),s=2/(r+1),c=0,l=0,u=0;for(let t=0;t<e.length;t++){let{high:n,low:d,close:f,volume:p}=e[t],m=n-d,h=m===0?0:(f-d-(n-f))/m;if(c+=h*p,t===0?(l=c,u=c):(l=(c-l)*o+l,u=(c-u)*s+u),t>=r-1){let n={value:l-u,adl:c};i.set(e[t].time,n),a[t]=n}}return{values:i,series:a}}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]?.value;t!==void 0&&(t<l&&(l=t),t>u&&(u=t))}if(l===1/0)return;let d=Math.max(Math.abs(l),Math.abs(u))||1,f=e=>o.y+o.height*(1-(e+d)/(2*d)),p=f(0),m=n.barWidth/2,h=r.colors[0]??`#26A69A`,g=r.colors[1]??`#EF5350`;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=f(r.value),c=Math.min(s,p),l=Math.max(Math.abs(s-p),1);e.fillStyle=r.value>=0?h:g,e.fillRect(o-m,c,n.barWidth,l)}}},Ae=class extends j{descriptor={id:`rsi`,name:`Relative Strength Index`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=N(t,`period`,14,1),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()}},je=class extends j{descriptor={id:`macd`,name:`MACD`,placement:`panel`,defaultConfig:{fast:12,slow:26,signal:9}};calculate(e,t){let n=N(t,`fast`,12,1),r=N(t,`slow`,26,1),i=N(t,`signal`,9,1),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)}},Me=class extends j{descriptor={id:`stochastic`,name:`Stochastic Oscillator`,placement:`panel`,defaultConfig:{kPeriod:14,dPeriod:3,smooth:3}};calculate(e,t){let n=N(t,`kPeriod`,14,1),r=N(t,`dPeriod`,3,1),i=N(t,`smooth`,3,1),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`)}},P=class extends j{descriptor={id:`atr`,name:`Average True Range`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=N(t,`period`,14,1),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()}},F=class extends j{descriptor={id:`adx`,name:`Average Directional Index`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=N(t,`period`,14,1),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`)}},Ne=class extends j{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()}},Pe=class extends j{descriptor={id:`williamsR`,name:`Williams %R`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=N(t,`period`,14,1),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()}},Fe=class extends j{descriptor={id:`cci`,name:`Commodity Channel Index`,placement:`panel`,defaultConfig:{period:20}};calculate(e,t){let n=N(t,`period`,20,1),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()}},Ie=class extends j{descriptor={id:`mfi`,name:`Money Flow Index`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=N(t,`period`,14,1),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()}},Le=class extends j{descriptor={id:`aroon`,name:`Aroon`,placement:`panel`,defaultConfig:{period:25}};calculate(e,t){let n=N(t,`period`,25,1),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`)}},Re=class extends j{descriptor={id:`roc`,name:`Rate of Change`,placement:`panel`,defaultConfig:{period:12}};calculate(e,t){let n=N(t,`period`,12,1),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()}},ze=class extends j{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=N(t,`longPeriod`,25,1),r=N(t,`shortPeriod`,13,1),i=N(t,`signalPeriod`,7,1),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`)}},Be=class extends j{descriptor={id:`cmf`,name:`Chaikin Money Flow`,placement:`panel`,defaultConfig:{period:20}};calculate(e,t){let n=N(t,`period`,20,1),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))}}}},Ve=class extends j{descriptor={id:`stddev`,name:`Standard Deviation`,placement:`panel`,defaultConfig:{period:20}};calculate(e,t){let n=N(t,`period`,20,1),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()}},He=class extends j{descriptor={id:`volumeProfile`,name:`Volume Profile`,placement:`panel`,defaultConfig:{rows:24}};calculate(e,t){let n=N(t,`rows`,24,1),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))}}},Ue=class extends j{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()}},We=class extends j{descriptor={id:`vroc`,name:`Volume Rate of Change`,placement:`panel`,defaultConfig:{period:14}};calculate(e,t){let n=N(t,`period`,14,1),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 Ge(e){e.register(new de),e.register(new fe),e.register(new pe),e.register(new me),e.register(new he),e.register(new ge),e.register(new _e),e.register(new ve),e.register(new ye),e.register(new xe),e.register(new Se),e.register(new Ce),e.register(new we),e.register(new Te),e.register(new Ae),e.register(new je),e.register(new Me),e.register(new P),e.register(new F),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 Be),e.register(new Ve),e.register(new He),e.register(new Ue),e.register(new We),e.register(new Oe),e.register(new ke)}function Ke(e){if(typeof e!=`object`||!e)return!1;let t=e;return typeof t.requestId==`number`?t.type===`result`||t.type===`pong`||t.type===`error`:!1}var qe=class{worker;fallbackPlugins;timeoutMs;nextRequestId=1;pending=new Map;messageHandler;constructor(e,t={}){this.worker=e,this.fallbackPlugins=t.fallbackPlugins??new Map,this.timeoutMs=t.timeoutMs??3e4,this.messageHandler=e=>this.handleMessage(e),this.worker&&(this.worker.addEventListener(`message`,this.messageHandler),this.worker.addEventListener(`error`,this.messageHandler))}hasWorker(){return this.worker!==null}registerFallbackPlugin(e){this.fallbackPlugins.set(e.descriptor.id,e)}async calculate(e,t,n){if(!this.worker){let r=this.fallbackPlugins.get(e);if(!r)throw Error(`Unknown indicator: ${e}`);return r.calculate(n,t)}let r=this.nextRequestId++,i={type:`calculate`,requestId:r,indicatorId:e,config:t,data:n};return new Promise((t,n)=>{let a={resolve:t,reject:n,timer:null};this.timeoutMs>0&&(a.timer=setTimeout(()=>{this.pending.delete(r),n(Error(`Indicator worker timeout (${this.timeoutMs}ms): ${e}`))},this.timeoutMs)),this.pending.set(r,a),this.worker.postMessage(i)})}ping(){if(!this.worker)return Promise.resolve();let e=this.nextRequestId++;return new Promise((t,n)=>{let r={resolve:()=>t(),reject:n,timer:this.timeoutMs>0?setTimeout(()=>{this.pending.delete(e),n(Error(`Indicator worker ping timeout`))},this.timeoutMs):null};this.pending.set(e,r),this.worker.postMessage({type:`ping`,requestId:e})})}terminate(){this.worker&&=(this.worker.removeEventListener(`message`,this.messageHandler),this.worker.removeEventListener(`error`,this.messageHandler),this.worker.terminate(),null);for(let e of this.pending.values())e.timer&&clearTimeout(e.timer),e.reject(Error(`Indicator worker terminated`));this.pending.clear()}handleMessage(e){let t=e.data;if(t!==void 0){if(!Ke(t))return;this.dispatchResponse(t);return}let n=e.message??`Indicator worker error`;for(let[e,t]of this.pending)t.timer&&clearTimeout(t.timer),t.reject(Error(n)),this.pending.delete(e)}dispatchResponse(e){let t=this.pending.get(e.requestId);if(t){if(this.pending.delete(e.requestId),t.timer&&clearTimeout(t.timer),e.type===`error`){t.reject(Error(e.message));return}if(e.type===`pong`){t.resolve(void 0);return}t.resolve(e.output)}}},Je=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()}},Ye=class{locale=`en-US`;setLocale(e){this.locale=e}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,this.locale)})}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)}},I=new Date,Xe=e=>e<10?`0`+e:``+e,Ze=-new Date().getTimezoneOffset()/60,Qe=`UTC${Ze>=0?`+`:``}${Ze}`,$e=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;I.setTime(a);let l=I.getDate(),u=I.getMonth()+1,p=I.getHours(),m=I.getMinutes(),h;l===f?h=`${Xe(p)}:${Xe(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(Qe,p,m)}},et=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;cachedRect=null;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=>{this.invalidateRect();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||this.tradingManager.onContextMenu(this.getMousePos(t),n)&&t.preventDefault()},c=e=>{if(e.preventDefault(),this.invalidateRect(),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)}},d=()=>this.invalidateRect(),f=()=>this.invalidateRect();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),window.addEventListener(`resize`,d),this.element.addEventListener(`scroll`,f,{passive:!0}),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),()=>window.removeEventListener(`resize`,d),()=>this.element.removeEventListener(`scroll`,f))}detach(){for(let e of this.boundHandlers)e();this.boundHandlers=[],this.cachedRect=null}invalidateRect(){this.cachedRect=null}getRect(){return this.cachedRect||=this.element.getBoundingClientRect(),this.cachedRect}getMousePos(e){let t=this.getRect();return{x:e.clientX-t.left,y:e.clientY-t.top}}getTouchPos(e){let t=this.getRect();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.getRect();return{x:(e.clientX+t.clientX)/2-n.left,y:(e.clientY+t.clientY)/2-n.top}}},tt=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)}},nt=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)}},rt=class{position=null;callback=null;data=[];magnetMode=!0;mode=`magnet`;pricePrecision=2;locale=`en-US`;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}setLocale(e){this.locale=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,this.locale),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)}},it=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}}},at=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=ot(n.background);this.el.style.background=d?st(n.background,.95):st(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 ot(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 st(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 ct=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}},L=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:d(e.time,t),y:o(e.price,t)}}pixelToAnchor(e,t){return{time:f(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}},lt=1;function R(e){return{...e,anchors:e.anchors.map(e=>({...e})),style:{...e.style},meta:e.meta?{...e.meta}:void 0}}var ut=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();if(r.length===0)return{time:e,price:t};let i=!!(n?.data&&n.data.length>0),a=Math.max(0,Math.min(r.length-1,Math.round(i?l(e,r):e))),o=r[a],s=[o.open,o.high,o.low,o.close],c=s[0],u=Math.abs(t-c);for(let e=1;e<s.length;e++){let n=Math.abs(t-s[e]);n<u&&(u=n,c=s[e])}if(n){let e=n.chartRect.height/(n.priceRange.max-n.priceRange.min||1);if(u*e>30)return{time:i?o.time:a,price:t}}return{time:i?o.time:a,price:c}}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=f(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:R(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=this.upgradeLegacyAnchors(e),this.requestRender?.()}upgradeLegacyAnchors(e){let t=this.dataGetter?.();return!t||t.length===0?e:e.map(e=>({...e,anchors:e.anchors.map(e=>{if(e.time>=1e9)return e;let n=Math.max(0,Math.min(t.length-1,Math.round(e.time)));return{...e,time:t[n].time}})}))}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);n.id=`tc_drawing_${lt++}`;let r=this.computeBarOffsetTime(3);return n.anchors=n.anchors.map(e=>({...e,time:e.time+r})),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()}computeBarOffsetTime(e){let t=this.dataGetter?.();if(!t||t.length<2)return e;let n=Math.max(0,t.length-20),r=[];for(let e=n+1;e<t.length;e++)r.push(t[e].time-t[e-1].time);return r.length===0?e:(r.sort((e,t)=>e-t),e*r[Math.floor(r.length/2)])}handleCreationClick(e,t){if(!this.activeTool)return!1;let n=this.registry.get(this.activeTool);if(!n)return!1;let r=f(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_${lt++}`,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=R(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=R(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=f(e.x,t)-f(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:f(e.x,t),price:c(e.y,t)},this.requestRender?.(),!0)}},dt=class{constructor(e){this.manager=e}render(e,t){this.manager.render(e,t)}},ft=class extends L{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}},pt=class extends L{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}},mt=class extends L{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}},ht=class extends L{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}},gt=class extends L{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}},_t=class extends L{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}},vt=class extends L{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}},z=[0,.236,.382,.5,.618,.786,1],yt=class extends L{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 z){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<z.length-1;r++){let a=s+c*(1-z[r]),l=s+c*(1-z[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 z){let i=o(s+c*(1-t),n);if(Math.abs(e.y-i)<=r)return!0}return!1}},bt=[0,.618,1,1.618,2,2.618,3.618,4.236],xt=class extends L{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 bt){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 bt){let s=o(t.anchors[2].price+a*i,n);if(Math.abs(e.y-s)<=r)return!0}return!1}},St=class extends L{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}},Ct=class extends L{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}},wt=class extends L{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}},Tt=class extends L{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}},Et=[`1`,`2`,`3`,`4`,`5`,`A`,`B`,`C`],Dt=class extends L{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<Et.length;t++){let n=t%2==0?-12:12;e.textBaseline=n<0?`bottom`:`top`,e.fillText(Et[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}},Ot=class extends L{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}},kt=class extends L{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=u(t.anchors[0].time,n),d=u(t.anchors[1].time,n),f=Math.max(0,Math.round(Math.abs(d-l)));e.font=`12px sans-serif`,e.fillStyle=t.style.color,e.textAlign=`center`,e.textBaseline=`top`,e.fillText(`${f} 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}},At=class extends L{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=u(t.anchors[0].time,n),c=u(t.anchors[1].time,n),l=Math.max(0,Math.round(Math.abs(c-s))),d=t.anchors[0].price===0?0:o/t.anchors[0].price*100,f=[`Price: ${o>=0?`+`:``}${o.toFixed(2)} (${d>=0?`+`:``}${d.toFixed(2)}%)`,`Bars: ${l}`],p=(i.x+a.x)/2,m=(i.y+a.y)/2;e.font=`11px sans-serif`,e.textAlign=`center`,e.textBaseline=`bottom`;let h=Math.max(...f.map(t=>e.measureText(t).width));e.fillStyle=`rgba(0, 0, 0, 0.7)`,e.fillRect(p-h/2-6,m-f.length*14-4,h+12,f.length*14+8),e.fillStyle=`#FFFFFF`;for(let t=0;t<f.length;t++)e.fillText(f[t],p,m-(f.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}};function jt(e,t=`Text`){if(typeof e.style.text==`string`&&e.style.text.length>0)return e.style.text;let n=e.meta?.text;return typeof n==`string`&&n.length>0?n:t}var Mt=class extends L{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=jt(t),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}},Nt=class extends L{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}},Pt=[{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}],Ft=class extends L{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 Pt){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 Pt){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}},B=[0,.25,.333,.5,.667,.75,1],It=class extends L{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 B){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 B){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 B){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}},Lt=class extends L{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(u(t.anchors[0].time,n)));if(s>=i.length)return;let{vwap:c,upper1:l,lower1:d,upper2:f,lower2:p}=this.computeVWAP(i,s),{chartRect:m}=n,h=n.barWidth+n.barSpacing;if(this.applyLineStyle(e,t.style),e.lineWidth=t.style.lineWidth+.5,this.drawLine(e,c,s,n,m,h),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,m,h),this.drawLine(e,d,s,n,m,h),e.globalAlpha=.25,this.drawLine(e,f,s,n,m,h),this.drawLine(e,p,s,n,m,h),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>=m.x&&i<=m.x+m.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(u(t.anchors[0].time,n)));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 L{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.round(u(t.anchors[0].time,n)),c=Math.round(u(t.anchors[1].time,n)),l=Math.max(0,Math.min(s,c)),d=Math.min(i.length-1,Math.max(s,c));if(l>=i.length||d<0||l>d)return;let f=1/0,p=-1/0;for(let e=l;e<=d;e++)i[e].low<f&&(f=i[e].low),i[e].high>p&&(p=i[e].high);if(f>=p)return;let m=(p-f)/this.numBins,h=new Float64Array(this.numBins),g=0;for(let e=l;e<=d;e++){let t=i[e],n=t.volume??1,r=Math.max(0,Math.floor((t.low-f)/m)),a=Math.min(this.numBins-1,Math.floor((t.high-f)/m)),o=n/(a-r+1);for(let e=r;e<=a;e++)h[e]+=o,h[e]>g&&(g=h[e])}if(g===0)return;let _=0;for(let e=1;e<this.numBins;e++)h[e]>h[_]&&(_=e);let v=a(d,n)+n.barWidth,y=t.style.color,b=t.style.fillColor??`#FF9800`;e.save();for(let t=0;t<this.numBins;t++){let r=h[t]/g*100,i=f+(t+1)*m,a=f+t*m,s=o(i,n),c=o(a,n),l=Math.abs(c-s);e.globalAlpha=.35,e.fillStyle=t===_?b:y,e.fillRect(v,Math.min(s,c),r,Math.max(1,l-.5))}let x=f+(_+.5)*m,S=o(x,n);e.globalAlpha=.7,e.strokeStyle=b,e.lineWidth=1,e.setLineDash([4,3]),e.beginPath();let C=a(l,n)-n.barWidth/2;e.moveTo(C,S),e.lineTo(v+100,S),e.stroke(),e.fillStyle=b,e.font=`9px sans-serif`,e.textAlign=`left`,e.textBaseline=`bottom`,e.globalAlpha=.8,e.fillText(`POC ${x.toFixed(2)}`,v+100+4,S-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}},zt=[1,2,3,5,8,13,21,34,55],Bt=class extends L{descriptor={type:`fibTimeZones`,name:`Fibonacci Time Zones`,requiredAnchors:2};render(e,t,n,r){if(t.anchors.length<2)return;let i=t.anchors[0],a=t.anchors[1].time-i.time;if(a===0)return;let{chartRect:o}=n;e.font=`11px sans-serif`,e.textBaseline=`top`,e.textAlign=`center`,this.applyLineStyle(e,t.style);for(let r of zt){let s=d(i.time+a*r,n);s<o.x||s>o.x+o.width||(e.globalAlpha=r===1?1:.55,e.beginPath(),e.moveTo(s,o.y),e.lineTo(s,o.y+o.height),e.stroke(),e.fillStyle=t.style.color,e.fillText(String(r),s,o.y+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=t.anchors[0],a=t.anchors[1].time-i.time;if(a===0)return!1;let{chartRect:o}=n;if(e.y<o.y||e.y>o.y+o.height)return!1;for(let t of zt){let o=d(i.time+a*t,n);if(Math.abs(e.x-o)<=r)return!0}return!1}};function Vt(e){e.register(new ft),e.register(new pt),e.register(new mt),e.register(new ht),e.register(new gt),e.register(new _t),e.register(new vt),e.register(new yt),e.register(new xt),e.register(new St),e.register(new Ct),e.register(new wt),e.register(new Tt),e.register(new Dt),e.register(new Ot),e.register(new kt),e.register(new At),e.register(new Mt),e.register(new Nt),e.register(new Ft),e.register(new It),e.register(new Lt),e.register(new Rt),e.register(new Bt)}var Ht=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}};function Ut(e,t,n){let r=Math.min(Math.max(e.closedQuantity??0,0),e.quantity),i=e.quantity-r,a=e.side===`buy`?1:-1,o=(t-e.entryPrice)*i*a,s=e.entryPrice===0?0:(t-e.entryPrice)/e.entryPrice*100*a;return{side:e.side,quantity:e.quantity,closedQuantity:r,openQuantity:i,entryPrice:e.entryPrice,currentPrice:t,pnl:o,pnlPct:s,precision:n}}var Wt=/\{(side|qty|closedQty|openQty|entry|price|pnl|pnlPct|pnlSign)\}/g;function Gt(e,t){return e.replace(Wt,(e,n)=>{switch(n){case`side`:return t.side.toUpperCase();case`qty`:return String(t.quantity);case`closedQty`:return String(t.closedQuantity);case`openQty`:return String(t.openQuantity);case`entry`:return t.entryPrice.toFixed(t.precision);case`price`:return t.currentPrice.toFixed(t.precision);case`pnl`:return t.pnl.toFixed(t.precision);case`pnlPct`:return`${t.pnlPct.toFixed(2)}%`;case`pnlSign`:return t.pnl>=0?`+`:``;default:return``}})}function Kt(e,t){return typeof e==`function`?e(t):Gt(e??`{side} {qty} | P&L: {pnlSign}{pnl}`,t)}function qt(e,t,n){if(!t||t.length===0)return n;let r=[...t].sort((e,t)=>e.pnl-t.pnl),i;for(let t of r)if(e>=t.pnl)i=t.color;else break;return i??n}var Jt=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 f of t){let t=o(f.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 p=o(n,r),m=Ut(f,n,d),h=m.pnl,g=h>=0?c:l,_=qt(h,a.pnlThresholds,g);e.fillStyle=_,e.globalAlpha=.08;let v=Math.min(t,p),y=Math.abs(p-t);if(e.fillRect(s.x,v,s.width,y),e.globalAlpha=1,m.closedQuantity>0&&f.quantity>0){let t=m.closedQuantity/f.quantity;e.fillStyle=u,e.globalAlpha=.25,e.fillRect(s.x,v,Math.max(2,s.width*t*.04)+2,y),e.globalAlpha=1}let b=Kt(a.positionLabel,m);e.font=`bold 11px ${i.font.family}`;let x=e.measureText(b).width+12,S=s.x+8;e.fillStyle=_,e.globalAlpha=.9,e.fillRect(S,t-10,x,20),e.globalAlpha=1,e.fillStyle=`#FFFFFF`,e.textBaseline=`middle`,e.textAlign=`left`,e.fillText(b,S+6,t)}if(f.stopLoss!==void 0){let t=o(f.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(f.takeProfit!==void 0){let t=o(f.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)}}},Yt=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)}}},Xt=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}},Zt=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;
@@ -11,11 +11,11 @@
11
11
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
12
12
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
13
13
  font-size: 12px;
14
- `;const h=[{label:`Buy Limit @ ${o}`,intent:{side:"buy",type:"limit",price:t}},{label:`Sell Limit @ ${o}`,intent:{side:"sell",type:"limit",price:t}},{label:`Buy Stop @ ${o}`,intent:{side:"buy",type:"stop",price:t}},{label:`Sell Stop @ ${o}`,intent:{side:"sell",type:"stop",price:t}}];for(const c of h){const d=document.createElement("div"),f=c.intent.side==="buy";d.textContent=c.label,d.style.cssText=`
14
+ `;let s=[{label:`Buy Limit @ ${a}`,intent:{side:`buy`,type:`limit`,price:t}},{label:`Sell Limit @ ${a}`,intent:{side:`sell`,type:`limit`,price:t}},{label:`Buy Stop @ ${a}`,intent:{side:`buy`,type:`stop`,price:t}},{label:`Sell Stop @ ${a}`,intent:{side:`sell`,type:`stop`,price:t}}];for(let e of s){let t=document.createElement(`div`),n=e.intent.side===`buy`;t.textContent=e.label,t.style.cssText=`
15
15
  padding: 6px 12px;
16
16
  cursor: pointer;
17
- color: ${f?"#26A69A":"#EF5350"};
17
+ color: ${n?`#26A69A`:`#EF5350`};
18
18
  transition: background 0.1s;
19
- `,d.addEventListener("mouseenter",()=>{d.style.background="#2a2e39"}),d.addEventListener("mouseleave",()=>{d.style.background="transparent"}),d.addEventListener("click",()=>{this.onItemSelect?.(c.intent),this.hide()}),l.appendChild(d)}n.appendChild(l),this.menuElement=l;const a=c=>{c instanceof KeyboardEvent&&c.key==="Escape"?this.hide():c instanceof MouseEvent&&!l.contains(c.target)&&this.hide()};document.addEventListener("mousedown",a),document.addEventListener("keydown",a),this.removeHandler=()=>{document.removeEventListener("mousedown",a),document.removeEventListener("keydown",a)}}hide(){this.menuElement?.remove(),this.menuElement=null,this.removeHandler?.(),this.removeHandler=null}destroy(){this.hide(),this.onItemSelect=null}}class ti{orders=[];positions=[];depthData=null;currentPrice=null;config;orderRenderer=new qt;positionRenderer=new zt;depthOverlay=new Xt;dragHandler;contextMenu=new jt;requestRender=null;eventCallback=null;container=null;constructor(e){this.config={...L.DEFAULT_TRADING_CONFIG,...e},this.dragHandler=new _t(this.config.dragThreshold),this.contextMenu.onItemSelect=t=>{this.eventCallback?.("orderPlace",t)}}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;const n=this.dragHandler.onPointerMove(e,t);return n&&this.requestRender?.(),n}onPointerUp(){const 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;const n=V(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()}}class ni{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)}}class z{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){const n=i=>{s(),t(i)},s=this.on(e,n);return s}off(e,t){this.listeners.get(e)?.delete(t)}emit(e,t){const n=this.listeners.get(e);if(n)for(const s of n)s(t)}removeAllListeners(){this.listeners.clear()}}class Gt extends z{currentBar=null;timeframeMs;tickRing;ringHead=0;ringCount=0;ringCapacity;constructor(e,t=1e3){super(),this.timeframeMs=L.timeframeToMs(e),this.ringCapacity=t,this.tickRing=new Array(t)}setTimeframe(e){this.timeframeMs=L.timeframeToMs(e),this.currentBar=null,this.ringHead=0,this.ringCount=0}processTick(e){const t=(this.ringHead+this.ringCount)%this.ringCapacity;this.tickRing[t]=e,this.ringCount<this.ringCapacity?this.ringCount++:this.ringHead=(this.ringHead+1)%this.ringCapacity;const 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{const n={...e,closed:!0,tickCount:1};this.emit("barClose",n),this.currentBar=null}}getCurrentBar(){return this.currentBar?{...this.currentBar}:null}getTickBuffer(){const e=new 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}}class ae extends z{config;attempt=0;timer=null;active=!1;constructor(e){super(),this.config={...L.DEFAULT_RECONNECT,...e}}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}const 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),this.timer=null)}getAttempt(){return this.attempt}isActive(){return this.active}computeDelay(){const e=this.config.baseDelay,t=Math.pow(this.config.backoffMultiplier,this.attempt-1),n=Math.min(e*t,this.config.maxDelay),s=n*.25*(Math.random()*2-1);return Math.max(0,Math.round(n+s))}}class Nt{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(e,t,n){if(!this.visible||this.price===null)return;const s=A(this.price,t),{chartRect:i}=t;if(s<i.y||s>i.y+i.height)return;const l=(this.previousClose!==null?this.price>=this.previousClose:!0)?n.candleUp??"#26A69A":n.candleDown??"#EF5350",h=Date.now()<this.flashUntil;e.setLineDash([4,3]),e.strokeStyle=l,e.lineWidth=1,e.globalAlpha=h?1:.7,e.beginPath(),e.moveTo(i.x,Math.round(s)+.5),e.lineTo(i.x+i.width,Math.round(s)+.5),e.stroke(),e.setLineDash([]),e.globalAlpha=1;const a=i.x+i.width+1,c=this.price.toFixed(this.pricePrecision);e.font=`bold 11px ${n.font.family}`;const d=e.measureText(c).width,f=Math.min(d+12,L.PRICE_AXIS_WIDTH-2);e.fillStyle=l,e.fillRect(a,s-10,f,20);const u=a-5;e.beginPath(),e.moveTo(u,s),e.lineTo(u+5,s-5),e.lineTo(u+5,s+5),e.closePath(),e.fill(),e.fillStyle="#FFFFFF",e.textBaseline="middle",e.textAlign="left",e.fillText(c,a+5,s)}}class ii extends z{config=null;adapter=null;aggregator=null;reconnector;priceLine=new Nt;state="disconnected";lastPrice=null;previousClose=null;unsubscribers=[];heartbeatTimer=null;lastMessageTime=0;constructor(){super(),this.reconnector=new ae,this.wireReconnector()}async connect(e){this.disconnect(),this.config={...L.DEFAULT_STREAM_CONFIG,...e},this.adapter=e.adapter,this.aggregator=new Gt(e.timeframe),this.reconnector.removeAllListeners(),this.reconnector.stop(),this.reconnector=new ae(e.reconnect??L.DEFAULT_RECONNECT),this.wireReconnector(),this.aggregator.on("bar",t=>{this.emit("barUpdate",t),this.updatePrice(t.close)}),this.aggregator.on("barClose",t=>{this.previousClose=t.close,this.emit("barClose",t)}),this.wireAdapter(),this.reconnector.start(),await this.doConnect()}disconnect(){this.reconnector.stop(),this.stopHeartbeat();for(const e of this.unsubscribers)e();this.unsubscribers=[],this.adapter&&(this.adapter.disconnect(),this.adapter=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{const 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){const 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(){if(!this.adapter)return;const e=this.adapter,t=o=>{const l=o.data;this.lastMessageTime=Date.now(),l==="connected"?(this.reconnector.onConnected(),this.setState("connected")):(l==="disconnected"||l==="error")&&this.reconnector.schedule(()=>this.doConnect())};e.on("connectionChange",t),this.unsubscribers.push(()=>e.off("connectionChange",t));const n=o=>{this.lastMessageTime=Date.now();const l=o.data;this.emit("tick",l),this.config?.aggregateTicks&&this.aggregator?this.aggregator.processTick(l):this.updatePrice(l.price)};e.on("tick",n),this.unsubscribers.push(()=>e.off("tick",n));const s=o=>{this.lastMessageTime=Date.now();const{bar:l,closed:h}=o.data;this.aggregator&&this.aggregator.processBar(l,h)};e.on("bar",s),this.unsubscribers.push(()=>e.off("bar",s));const i=o=>{this.emit("error",{message:o.data?.message??"Adapter error"})};e.on("error",i),this.unsubscribers.push(()=>e.off("error",i))}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),this.heartbeatTimer=null)}dispose(){this.disconnect(),this.removeAllListeners()}}function si(r){if(!Array.isArray(r)||r.length<6)return null;const e=r[0];if(typeof e!="number"||!Number.isFinite(e))return null;const t=x(r[1]),n=x(r[2]),s=x(r[3]),i=x(r[4]),o=x(r[5]);return t===null||n===null||s===null||i===null||o===null?null:{time:e,open:t,high:n,low:s,close:i,volume:o}}function oi(r){if(!ri(r))return null;const e=r.t;if(typeof e!="number"||!Number.isFinite(e))return null;const t=x(r.o),n=x(r.h),s=x(r.l),i=x(r.c),o=x(r.v);if(t===null||n===null||s===null||i===null||o===null)return null;const l=typeof r.x=="boolean"?r.x:!1;return{bar:{time:e,open:t,high:n,low:s,close:i,volume:o},closed:l}}function x(r){if(typeof r=="number")return Number.isFinite(r)?r:null;if(typeof r=="string"){const e=parseFloat(r);return Number.isFinite(e)?e:null}return null}function ri(r){return typeof r=="object"&&r!==null}const Te={"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"};class li{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(),this.ws=null),this.setState("disconnected")}getConnectionState(){return this.state}async fetchHistory(e,t,n=500){const s=Te[t]??"15m",i=`${this.restBase}/klines?symbol=${e}&interval=${s}&limit=${n}`,o=await fetch(i);if(!o.ok)throw new Error(`Binance REST error: ${o.status}`);const l=await o.json();if(!Array.isArray(l))return[];const h=[];for(const a of l){const c=si(a);c&&h.push(c)}return h}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;const e=this.config.symbol.toLowerCase(),t=Te[this.config.timeframe]??"15m",n=`${e}@kline_${t}`,s=`${this.wsBase}/${n}`;this.setState("connecting");try{this.ws=new WebSocket(s)}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=i=>{let o;try{o=JSON.parse(typeof i.data=="string"?i.data:"")}catch{return}typeof o=="object"&&o!==null&&o.e==="kline"&&this.handleKline(o.k)},this.ws.onerror=()=>{this.setState("error"),this.emitEvent("error",{message:"WebSocket error"})},this.ws.onclose=()=>{this.setState("disconnected"),this.emitEvent("connectionChange","disconnected")}}handleKline(e){const t=oi(e);if(!t)return;const{bar:n,closed:s}=t;this.emitEvent("bar",{bar:n,closed:s}),this.emitEvent("tick",{time:Date.now(),price:n.close,volume:n.volume})}setState(e){this.state=e}emitEvent(e,t){const n=this.listeners.get(e);if(n){const s={type:e,data:t,timestamp:Date.now()};for(const i of n)i(s)}}}class ai{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(e,t){const n=[],s=Date.now(),i=L.timeframeToMs(e);let o=this.options.basePrice;const l=this.options.volatility/100;for(let h=t-1;h>=0;h--){const a=s-h*i,c=(Math.random()-.48)*o*l,d=o;o+=c;const f=Math.max(d,o)+Math.random()*o*l*.3,u=Math.min(d,o)-Math.random()*o*l*.3,g=Math.floor(1e4+Math.random()*1e5);n.push({time:a,open:d,high:f,low:u,close:o,volume:g})}return this.currentPrice=o,n}startTicking(){this.stopTicking();const e=this.options.tickInterval??2e3;this.tickTimer=setInterval(()=>{if(this.state!=="connected"||!this.config)return;const t=this.options.volatility/100,n=(Math.random()-.48)*this.currentPrice*t*.3;this.currentPrice+=n;const s=Date.now(),i=L.timeframeToMs(this.config.timeframe),l={time:Math.floor(s/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:l,closed:!1}),this.emitEvent("tick",{time:s,price:this.currentPrice,volume:l.volume})},e)}stopTicking(){this.tickTimer&&(clearInterval(this.tickTimer),this.tickTimer=null)}emitEvent(e,t){const n=this.listeners.get(e);if(n){const s={type:e,data:t,timestamp:Date.now()};for(const i of n)i(s)}}}const Jt={visible:!0,showSymbol:!0,showOHLC:!0,showChange:!0,showVolume:!0,position:"top-left",fontSize:12};class hi{config={...Jt};symbol="";timeframe="";hoverBar=null;indicators=[];statusText=null;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}setStatusText(e){this.statusText=e}render(e,t,n,s){if(!this.config.visible||s.length===0)return;const i=this.hoverBar??s[s.length-1],o=s.length>1?s[s.length-2]:i,{chartRect:l}=t,h=this.config.fontSize,a=this.config.position==="top-left";let c=a?l.x+8:l.x+l.width-8,d=l.y+6;if(e.textAlign=a?"left":"right",e.textBaseline="top",this.config.showSymbol){e.font=`bold ${h+2}px ${n.font.family}`,e.fillStyle=n.text;const f=[this.symbol,this.timeframe].filter(Boolean).join(" · "),u=this.statusText?`${f} ${this.statusText}`:f;e.fillText(u,c,d),d+=h+6}else this.statusText&&(e.font=`${h}px ${n.font.family}`,e.fillStyle=n.textSecondary,e.fillText(this.statusText,c,d),d+=h+4);if(this.config.showOHLC){e.font=`${h}px ${n.font.family}`;const f=i.close>=i.open,u=y=>y.toLocaleString(void 0,{minimumFractionDigits:2,maximumFractionDigits:2}),g=[{label:"O",value:u(i.open),color:n.text},{label:"H",value:u(i.high),color:n.text},{label:"L",value:u(i.low),color:n.text},{label:"C",value:u(i.close),color:f?n.candleUp:n.candleDown}];if(this.config.showChange){const y=i.close-o.close,b=o.close!==0?y/o.close*100:0,p=y>=0?"+":"";g.push({label:"",value:`${p}${u(y)} (${p}${b.toFixed(2)}%)`,color:y>=0?n.candleUp:n.candleDown})}let m=c;if(a)for(const y of g)y.label&&(e.fillStyle=n.textSecondary,e.fillText(y.label,m,d),m+=e.measureText(y.label+" ").width),e.fillStyle=y.color,e.fillText(y.value,m,d),m+=e.measureText(y.value+" ").width;else{const y=g.map(b=>`${b.label} ${b.value}`).join(" ");e.fillStyle=n.text,e.fillText(y,c,d)}d+=h+3}if(this.config.showVolume){e.font=`${h-1}px ${n.font.family}`,e.fillStyle=n.textSecondary;const f=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 ${f}`,c,d),d+=h+2}if(this.indicators.length>0){e.font=`${h-1}px ${n.font.family}`;let f=c;for(const u of this.indicators){e.fillStyle=u.color;const g=`${u.name} ${u.value}`;e.fillText(g,f,d),f+=e.measureText(g+" ").width}}}}class ci{static toDataURL(e,t){const n=e.querySelectorAll("canvas");if(n.length===0)return"";const s=n[0],i=s.width,o=s.height,l=document.createElement("canvas");l.width=i,l.height=o;const h=l.getContext("2d");t&&(h.fillStyle=t,h.fillRect(0,0,i,o));const a=Array.from(n).sort((c,d)=>(parseInt(c.style.zIndex)||0)-(parseInt(d.style.zIndex)||0));for(const c of a)h.drawImage(c,0,0);return l.toDataURL("image/png")}static async toBlob(e,t){const n=this.toDataURL(e,t);return n?(await fetch(n)).blob():null}static download(e,t="chart.png",n){const s=this.toDataURL(e,n);if(!s)return;const i=document.createElement("a");i.href=s,i.download=t,document.body.appendChild(i),i.click(),document.body.removeChild(i)}}class di{config=null;setConfig(e){this.config=e}render(e,t,n){if(!this.config?.text)return;const{chartRect:s}=t,i=this.config.fontSize??48,o=this.config.color??n.textSecondary;e.save(),e.globalAlpha=.07,e.font=`bold ${i}px ${n.font.family}`,e.fillStyle=o,e.textAlign="center",e.textBaseline="middle",e.fillText(this.config.text,s.x+s.width/2,s.y+s.height/2),e.restore()}}class fi{visible=!0;timeframeMs=0;setVisible(e){this.visible=e}isVisible(){return this.visible}setTimeframeMs(e){this.timeframeMs=e}render(e,t,n,s){if(!this.visible||this.timeframeMs<=0||s.length===0)return;const i=s[s.length-1],h=(i.time>1e12?i.time:i.time*1e3)+this.timeframeMs-Date.now();if(h<=0)return;const a=this.formatRemaining(h),c=D(s.length-1,t),d=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(a,c,d))}formatRemaining(e){const t=Math.ceil(e/1e3),n=Math.floor(t/3600),s=Math.floor(t%3600/60),i=t%60;return n>0?`${n}:${String(s).padStart(2,"0")}:${String(i).padStart(2,"0")}`:`${s}:${String(i).padStart(2,"0")}`}}class ui{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;const t=[];let n=this.getDayKey(e[0].time);for(let s=1;s<e.length;s++){const i=this.getDayKey(e[s].time);i!==n&&(t.push(s),n=i)}return this.lastDataLength=e.length,this.cachedBreaks=t,t}getDayKey(e){const t=e>1e12?e:e*1e3,n=new Date(t);return`${n.getFullYear()}-${n.getMonth()}-${n.getDate()}`}render(e,t,n,s){if(!this.config.visible||s.length<2)return;const i=this.computeBreaks(s);if(i.length===0)return;const{chartRect:o}=t,l=this.config.color??n.axisLine,h=this.config.lineWidth??1,a=this.config.lineStyle??"dashed";e.save(),e.strokeStyle=l,e.lineWidth=h,e.globalAlpha=.4,a==="dashed"?e.setLineDash([6,4]):a==="dotted"&&e.setLineDash([2,3]),e.beginPath();for(const c of i){const d=D(c,t)-(t.barWidth+t.barSpacing)/2;if(d<o.x-1||d>o.x+o.width+1)continue;const f=Math.round(d)+.5;e.moveTo(f,o.y),e.lineTo(f,o.y+o.height)}e.stroke(),e.restore()}invalidateCache(){this.lastDataLength=0,this.cachedBreaks=[]}}let gi=1;class mi extends z{alerts=[];lastPrice=null;requestRender=null;pricePrecision=2;setRequestRender(e){this.requestRender=e}setPricePrecision(e){this.pricePrecision=e}addAlert(e,t="crossing",n,s=!1){const i=`tc_alert_${gi++}`,o={id:i,price:e,condition:t,message:n,triggered:!1,repeating:s};return this.alerts.push(o),this.emit("added",o),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(const 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{const t=this.alerts.map(n=>({id:n.id,price:n.price,condition:n.condition,message:n.message,triggered:n.triggered}));localStorage.setItem(e,JSON.stringify(t))}catch{}}loadFromStorage(e){try{const t=localStorage.getItem(e);if(!t)return;const n=JSON.parse(t);if(!Array.isArray(n))return;for(const s of n)s.price&&s.condition&&!s.triggered&&this.addAlert(s.price,s.condition,s.message)}catch{}}render(e,t,n){const{chartRect:s}=t;for(const i of this.alerts){const o=A(i.price,t);if(o<s.y||o>s.y+s.height)continue;const l=i.triggered?"#FF9800":"#FFD700";e.setLineDash([2,6]),e.strokeStyle=l,e.lineWidth=1,e.beginPath(),e.moveTo(s.x,Math.round(o)+.5),e.lineTo(s.x+s.width,Math.round(o)+.5),e.stroke(),e.setLineDash([]),e.font=`10px ${n.font.family}`,e.fillStyle=l,e.textBaseline="bottom",e.textAlign="right";const h=`🔔 ${i.price.toFixed(this.pricePrecision)}${i.message?` - ${i.message}`:""}`;e.fillText(h,s.x+s.width-4,o-2)}}}class yi extends z{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}const t=this.data[this.currentIndex];this.emit("bar",{bar:t,index:this.currentIndex,total:this.data.length}),this.currentIndex++},this.config.interval/this.config.speed))}pause(){this.timer&&(clearInterval(this.timer),this.timer=null),this.state="paused",this.emit("stateChange","paused")}resume(){this.state==="paused"&&this.play()}stop(){this.timer&&(clearInterval(this.timer),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=[]}}const Q=1;function U(r){return typeof r=="object"&&r!==null&&!Array.isArray(r)}function Z(r){return Array.isArray(r)?r:[]}function pi(r,e){return typeof r=="string"?r:e}function ie(r,e){return typeof r=="number"&&Number.isFinite(r)?r:e}function bi(r){if(!U(r)||typeof r.id!="string"||typeof r.type!="string"||!Array.isArray(r.anchors)||r.anchors.length===0||!U(r.style))return null;const e=[];for(const n of r.anchors){if(!U(n)||typeof n.time!="number"||typeof n.price!="number"||!Number.isFinite(n.time)||!Number.isFinite(n.price))return null;e.push({time:n.time,price:n.price})}const t=r.style;return typeof t.color!="string"||typeof t.lineWidth!="number"?null:{id:r.id,type:r.type,anchors:e,style:t,visible:typeof r.visible=="boolean"?r.visible:!0,locked:typeof r.locked=="boolean"?r.locked:!1,meta:U(r.meta)?r.meta:void 0}}function Ti(r){return!U(r)||typeof r.id!="string"||typeof r.side!="string"||typeof r.type!="string"||typeof r.price!="number"||typeof r.quantity!="number"?null:r}function vi(r){return!U(r)||typeof r.id!="string"||typeof r.side!="string"||typeof r.entryPrice!="number"||typeof r.quantity!="number"?null:r}function Mi(r){if(!U(r))return Si();typeof r.version=="number"&&r.version!==Q&&console.warn(`Chart state version mismatch: ${r.version} vs ${Q}`);const e=U(r.viewport)?r.viewport:{},t=[];for(const l of Z(r.indicators))U(l)&&(typeof l.id!="string"||typeof l.instanceId!="string"||U(l.params)&&t.push({id:l.id,instanceId:l.instanceId,params:l.params,position:typeof l.position=="string"?l.position:void 0}));const n=[];for(const l of Z(r.drawings)){const h=bi(l);h&&n.push(h)}const s=[];for(const l of Z(r.orders)){const h=Ti(l);h&&s.push(h)}const i=[];for(const l of Z(r.positions)){const h=vi(l);h&&i.push(h)}const o=[];for(const l of Z(r.alerts))U(l)&&typeof l.id=="string"&&o.push(l);return{version:typeof r.version=="number"?r.version:Q,timestamp:ie(r.timestamp,Date.now()),symbol:typeof r.symbol=="string"?r.symbol:void 0,timeframe:typeof r.timeframe=="string"?r.timeframe:void 0,chartType:pi(r.chartType,"candlestick"),viewport:{barWidth:ie(e.barWidth,8),barSpacing:ie(e.barSpacing,2),offset:ie(e.offset,0)},theme:typeof r.theme=="string"?r.theme:void 0,locale:typeof r.locale=="string"?r.locale:void 0,indicators:t,drawings:n,orders:s,positions:i,alerts:o}}function Si(){return{version:Q,timestamp:Date.now(),chartType:"candlestick",viewport:{barWidth:8,barSpacing:2,offset:0},indicators:[],drawings:[],orders:[],positions:[],alerts:[]}}class Pi{static capture(e,t){return{version:Q,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){const t=JSON.parse(e);return Mi(t)}static saveToStorage(e,t){localStorage.setItem(e,this.serialize(t))}static loadFromStorage(e){const t=localStorage.getItem(e);return t?this.deserialize(t):null}static downloadFile(e,t="tc-chart-state.json"){const n=new Blob([this.serialize(e)],{type:"application/json"}),s=URL.createObjectURL(n),i=document.createElement("a");i.href=s,i.download=t,document.body.appendChild(i),i.click(),document.body.removeChild(i),URL.revokeObjectURL(s)}static loadFromFile(){return new Promise((e,t)=>{const n=document.createElement("input");n.type="file",n.accept=".json",n.onchange=async()=>{const s=n.files?.[0];if(!s){t(new Error("No file selected"));return}const i=await s.text();e(this.deserialize(i))},n.click()})}}class Ri{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(){const e=this.undoStack.pop();return e?(this.redoStack.push(e),this.onChange?.(),e):null}redo(){const 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}}}class ki{static toCSV(e){const t="Time,Open,High,Low,Close,Volume",n=e.map(s=>`${new Date(s.time).toISOString()},${s.open},${s.high},${s.low},${s.close},${s.volume}`);return[t,...n].join(`
20
- `)}static toJSON(e){return JSON.stringify(e.map(t=>({time:new Date(t.time).toISOString(),open:t.open,high:t.high,low:t.low,close:t.close,volume:t.volume})),null,2)}static download(e,t,n){const s=new Blob([e],{type:n}),i=URL.createObjectURL(s),o=document.createElement("a");o.href=i,o.download=t,document.body.appendChild(o),o.click(),document.body.removeChild(o),URL.revokeObjectURL(i)}}let wi=1;class Ai extends z{markers=[];style={...L.DEFAULT_SIGNAL_STYLE};requestRender=null;dataGetter=null;setRequestRender(e){this.requestRender=e}setDataGetter(e){this.dataGetter=e}setStyle(e){this.style={...this.style,...e},this.requestRender?.()}getStyle(){return{...this.style}}addMarker(e){const t=e.id??`tc_signal_${wi++}`,n={...e,id:t};return this.markers.push(n),this.emit("added",n),this.requestRender?.(),t}removeMarker(e){this.markers=this.markers.filter(t=>t.id!==e),this.emit("removed",e),this.requestRender?.()}getMarkers(){return[...this.markers]}clearMarkers(){this.markers=[],this.emit("cleared",void 0),this.requestRender?.()}setMarkers(e){this.markers=[...e],this.requestRender?.()}render(e,t,n){const s=this.dataGetter?.();if(!s||s.length===0||this.markers.length===0)return;const{chartRect:i,visibleRange:o}=t,l=this.style,h=l.arrowSize??12;e.save(),e.beginPath(),e.rect(i.x,i.y,i.width,i.height),e.clip();for(const a of this.markers){const c=j(a.time,s);if(c<o.from-1||c>o.to+1)continue;const d=D(c,t),f=A(a.price,t);if(d<i.x-h||d>i.x+i.width+h||f<i.y-h||f>i.y+i.height+h)continue;const u=this.resolveColor(a,l),g=h*Math.max(.6,Math.min(1,a.confidence));if(this.renderArrow(e,d,f,a.direction,g,u),l.showLabel&&a.label){e.font=`10px ${n.font.family}`,e.fillStyle=u,e.textAlign="center",e.textBaseline=a.direction==="long"?"top":"bottom";const m=a.direction==="long"?f+g+3:f-g-3;e.fillText(a.label,d,m)}if(l.showConfidence&&a.confidence<1){e.font=`9px ${n.font.family}`,e.fillStyle=u,e.globalAlpha=.7,e.textAlign="center",e.textBaseline=a.direction==="long"?"top":"bottom";const m=a.direction==="long"?f+g+(a.label?14:3):f-g-(a.label?14:3);e.fillText(`${Math.round(a.confidence*100)}%`,d,m),e.globalAlpha=1}}e.restore()}resolveColor(e,t){if(e.color)return e.color;if(t.sourceColors?.[e.source])return t.sourceColors[e.source];switch(e.direction){case"long":return t.longColor??"#26A69A";case"short":return t.shortColor??"#EF5350";default:return t.neutralColor??"#9E9E9E"}}renderArrow(e,t,n,s,i,o){e.fillStyle=o,e.beginPath(),s==="long"?(e.moveTo(t,n-i),e.lineTo(t-i*.6,n),e.lineTo(t-i*.2,n),e.lineTo(t-i*.2,n+i*.4),e.lineTo(t+i*.2,n+i*.4),e.lineTo(t+i*.2,n),e.lineTo(t+i*.6,n)):s==="short"?(e.moveTo(t,n+i),e.lineTo(t-i*.6,n),e.lineTo(t-i*.2,n),e.lineTo(t-i*.2,n-i*.4),e.lineTo(t+i*.2,n-i*.4),e.lineTo(t+i*.2,n),e.lineTo(t+i*.6,n)):e.arc(t,n,i*.4,0,Math.PI*2),e.closePath(),e.fill()}}let Li=1;class Ci extends z{zones=[];style={...L.DEFAULT_TRADE_ZONE_STYLE};requestRender=null;dataGetter=null;pricePrecision=2;setRequestRender(e){this.requestRender=e}setDataGetter(e){this.dataGetter=e}setPricePrecision(e){this.pricePrecision=e}setStyle(e){this.style={...this.style,...e},this.requestRender?.()}getStyle(){return{...this.style}}addZone(e){const t=e.id??`tc_zone_${Li++}`,n={...e,id:t};return this.zones.push(n),this.emit("added",n),this.requestRender?.(),t}updateZone(e,t){const n=this.zones.findIndex(s=>s.id===e);n!==-1&&(this.zones[n]={...this.zones[n],...t},this.requestRender?.())}removeZone(e){this.zones=this.zones.filter(t=>t.id!==e),this.emit("removed",e),this.requestRender?.()}getZones(){return[...this.zones]}clearZones(){this.zones=[],this.emit("cleared",void 0),this.requestRender?.()}setZones(e){this.zones=[...e],this.requestRender?.()}render(e,t,n){const s=this.dataGetter?.();if(!s||s.length===0||this.zones.length===0)return;const{chartRect:i}=t,o=this.style;e.save(),e.beginPath(),e.rect(i.x,i.y,i.width,i.height),e.clip();for(const l of this.zones)this.renderZone(e,l,s,t,n,o);e.restore()}renderZone(e,t,n,s,i,o){const{chartRect:l,visibleRange:h}=s,a=j(t.entryTime,n),c=t.exitTime!=null?j(t.exitTime,n):n.length-1;if(c<h.from||a>h.to)return;const d=D(a,s),f=D(c,s),u=Math.max(l.x,Math.min(d,f)),g=Math.min(l.x+l.width,Math.max(d,f));if(g-u<1)return;const m=A(t.entryPrice,s),y=t.exitPrice!=null?A(t.exitPrice,s):A(t.entryPrice,s),b=Math.min(m,y),p=Math.max(m,y),T=Math.max(p-b,2),v=t.exitTime==null,S=t.pnl!=null?t.pnl>=0:t.direction==="long"?(t.exitPrice??t.entryPrice)>=t.entryPrice:(t.exitPrice??t.entryPrice)<=t.entryPrice,M=v?o.activeColor??"#2196F3":S?o.profitColor??"#26A69A":o.lossColor??"#EF5350";e.fillStyle=M,e.globalAlpha=o.fillOpacity??.12,e.fillRect(u,b,g-u,T),e.globalAlpha=1;const P=o.borderWidth??1;e.strokeStyle=M,e.lineWidth=P,e.setLineDash([]),e.beginPath(),e.moveTo(u,Math.round(m)+.5),e.lineTo(g,Math.round(m)+.5),e.stroke(),!v&&t.exitPrice!=null&&(e.setLineDash([3,3]),e.beginPath(),e.moveTo(u,Math.round(y)+.5),e.lineTo(g,Math.round(y)+.5),e.stroke(),e.setLineDash([]));const R=t.direction==="long"?"▲":"▼";if(e.font=`bold 10px ${i.font.family}`,e.fillStyle=M,e.textBaseline="bottom",e.textAlign="left",e.fillText(`${R} ${t.entryPrice.toFixed(this.pricePrecision)}`,u+4,m-3),o.showPnl&&!v&&t.pnl!=null){const w=t.pnl>=0?"+":"",k=t.pnlPercent!=null?`${w}${t.pnl.toFixed(2)} (${w}${t.pnlPercent.toFixed(2)}%)`:`${w}${t.pnl.toFixed(2)}`;e.font=`bold 10px ${i.font.family}`,e.fillStyle=M,e.textBaseline="top",e.textAlign="right";const C=e.measureText(k).width+8,I=g-C-2,B=b+2;e.globalAlpha=.85,e.fillStyle=i.background,e.fillRect(I,B,C,14),e.globalAlpha=1,e.fillStyle=M,e.fillText(k,g-4,B+1)}o.showLabel&&t.label&&(e.font=`9px ${i.font.family}`,e.fillStyle=M,e.globalAlpha=.8,e.textBaseline="top",e.textAlign="left",e.fillText(t.label,u+4,m+3),e.globalAlpha=1)}}const Kt={linear:r=>r,easeOutCubic:r=>1-Math.pow(1-r,3),easeOutQuart:r=>1-Math.pow(1-r,4),easeInOutCubic:r=>r<.5?4*r*r*r:1-Math.pow(-2*r+2,3)/2,easeOutExpo:r=>r===1?1:1-Math.pow(2,-10*r),easeOutElastic:r=>r===0||r===1?r:Math.pow(2,-10*r)*Math.sin((r*10-.75)*(2*Math.PI/3))+1,spring:r=>1-Math.pow(Math.E,-6*r)*Math.cos(8*r)};class Ii{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=>{const t=[];for(const[n,s]of this.animations){s.startTime<0&&(s.startTime=e);const i=e-s.startTime,o=Math.min(1,i/s.duration),l=s.easing(o);s.onUpdate(l),o>=1&&(s.onComplete?.(),t.push(n))}for(const n of t)this.animations.delete(n);this.animations.size>0?this.tick():this.running=!1})}stop(){this.frameId&&(cancelAnimationFrame(this.frameId),this.frameId=0),this.running=!1}dispose(){this.cancelAll()}}function ve(r){return!r||r==="dark"?L.DARK_THEME:r==="light"?L.LIGHT_THEME:r}class Di{canvas;ctx;container;theme;width=0;height=0;dpr=1;frameId=null;resizeObserver;constructor(e,t){this.container=e,this.theme=ve(t),this.canvas=document.createElement("canvas"),this.canvas.style.width="100%",this.canvas.style.height="100%",this.canvas.style.display="block",e.appendChild(this.canvas),this.ctx=this.canvas.getContext("2d"),this.dpr=window.devicePixelRatio||1,this.updateSize(),this.resizeObserver=new ResizeObserver(()=>{this.updateSize(),this.requestRender()}),this.resizeObserver.observe(e)}updateSize(){this.dpr=window.devicePixelRatio||1;const e=this.container.getBoundingClientRect();this.width=e.width||this.container.clientWidth,this.height=e.height||this.container.clientHeight,this.canvas.width=Math.max(1,Math.floor(this.width*this.dpr)),this.canvas.height=Math.max(1,Math.floor(this.height*this.dpr))}requestRender(){this.frameId===null&&(this.frameId=requestAnimationFrame(()=>{this.frameId=null,(this.width===0||this.height===0)&&this.updateSize(),!(this.width===0||this.height===0)&&(this.ctx.save(),this.ctx.scale(this.dpr,this.dpr),this.ctx.clearRect(0,0,this.width,this.height),this.renderChart(this.ctx,this.width,this.height,this.theme),this.ctx.restore())}))}setTheme(e){this.theme=ve(e),this.requestRender()}destroy(){this.frameId!==null&&cancelAnimationFrame(this.frameId),this.resizeObserver.disconnect(),this.canvas.remove()}}class Wi{pos=null;canvas;onUpdate;handleMove;handleLeave;constructor(e,t){this.canvas=e,this.onUpdate=t,this.handleMove=n=>{const s=e.getBoundingClientRect();this.pos={x:n.clientX-s.left,y:n.clientY-s.top},this.onUpdate()},this.handleLeave=()=>{this.pos=null,this.onUpdate()},e.addEventListener("pointermove",this.handleMove),e.addEventListener("pointerleave",this.handleLeave)}getPosition(){return this.pos}isActive(){return this.pos!==null}drawCrosshair(e,t,n,s){this.pos&&(e.save(),e.strokeStyle=s,e.lineWidth=.5,e.setLineDash([4,4]),e.beginPath(),e.moveTo(this.pos.x,0),e.lineTo(this.pos.x,n),e.stroke(),e.beginPath(),e.moveTo(0,this.pos.y),e.lineTo(t,this.pos.y),e.stroke(),e.restore())}destroy(){this.canvas.removeEventListener("pointermove",this.handleMove),this.canvas.removeEventListener("pointerleave",this.handleLeave)}}function Bi(r,e,t,n,s){const{data:i,mode:o="area",lineWidth:l=1.5,showLastPoint:h=!1,showMinMax:a=!1}=n;if(i.length<2||e<=0||t<=0)return;const c=n.color??s.lineColor,d=n.fillColor??c,f=2,u=e-f*2,g=t-f*2;let m=1/0,y=-1/0,b=0,p=0;for(let M=0;M<i.length;M++)i[M]<m&&(m=i[M],b=M),i[M]>y&&(y=i[M],p=M);const T=y-m||1,v=M=>f+M/(i.length-1)*u,S=M=>f+g-(M-m)/T*g;if(o==="area"){const M=r.createLinearGradient(0,f,0,t-f);M.addColorStop(0,d+(d.startsWith("#")?"40":"")),M.addColorStop(1,"transparent"),r.beginPath(),r.moveTo(v(0),S(i[0]));for(let P=1;P<i.length;P++)r.lineTo(v(P),S(i[P]));r.lineTo(v(i.length-1),t-f),r.lineTo(v(0),t-f),r.closePath(),r.fillStyle=M,r.fill()}r.beginPath(),r.moveTo(v(0),S(i[0]));for(let M=1;M<i.length;M++)r.lineTo(v(M),S(i[M]));if(r.strokeStyle=c,r.lineWidth=l,r.lineJoin="round",r.stroke(),h){const M=n.lastPointColor??c,P=v(i.length-1),R=S(i[i.length-1]);r.beginPath(),r.arc(P,R,3,0,Math.PI*2),r.fillStyle=M,r.fill()}if(a){const M=n.minPointColor??s.candleDown,P=n.maxPointColor??s.candleUp;r.beginPath(),r.arc(v(b),S(m),2.5,0,Math.PI*2),r.fillStyle=M,r.fill(),r.beginPath(),r.arc(v(p),S(y),2.5,0,Math.PI*2),r.fillStyle=P,r.fill()}}const Me=50,Se=60,re=20,Fi=.2;function Ei(r){const e=[...r].sort((s,i)=>i.price-s.price),t=[];let n=0;for(const s of e)n+=s.volume,t.push({price:s.price,cumulativeVolume:n});return t}function Hi(r){const e=[...r].sort((s,i)=>s.price-i.price),t=[];let n=0;for(const s of e)n+=s.volume,t.push({price:s.price,cumulativeVolume:n});return t}function $i(r,e){return{left:Me,right:r-Se,top:re,bottom:e-re,plotWidth:r-Me-Se,plotHeight:e-re*2}}function Yi(r,e,t,n){r.fillStyle=n,r.fillRect(0,0,e,t)}function Vi(r,e,t,n,s,i,o,l){const{left:h,right:a,top:c,bottom:d,plotWidth:f,plotHeight:u}=e;r.save(),r.strokeStyle=i.grid,r.lineWidth=.5,r.fillStyle=i.axisLabel,r.font=`${i.font.sizeSmall}px ${i.font.family}`;const g=L.computeTickStep(0,s,5);for(let p=0;p<=s;p+=g){const T=d-p/s*u;r.beginPath(),r.moveTo(h,T),r.lineTo(a,T),r.stroke(),r.textAlign="right",r.textBaseline="middle",r.fillText(l(p),h-4,T)}const m=n-t,y=L.computeTickStep(t,n,6),b=Math.ceil(t/y)*y;for(let p=b;p<=n;p+=y){const T=h+(p-t)/m*f;r.beginPath(),r.moveTo(T,c),r.lineTo(T,d),r.stroke(),r.textAlign="center",r.textBaseline="top",r.fillText(o(p),T,d+4)}r.restore()}function Pe(r,e,t,n,s,i,o,l,h){if(e.length===0)return;const{left:a,bottom:c,plotWidth:d,plotHeight:f}=t,u=m=>a+(m-n)/s*d,g=m=>c-m/i*f;if(r.beginPath(),h==="bid"){r.moveTo(u(e[0].price),c);for(let m=0;m<e.length;m++){const y=u(e[m].price),b=g(e[m].cumulativeVolume);r.lineTo(y,b),m<e.length-1&&r.lineTo(u(e[m+1].price),b)}r.lineTo(u(e[e.length-1].price),c)}else{r.moveTo(u(e[0].price),c);for(let m=0;m<e.length;m++){const y=u(e[m].price),b=g(e[m].cumulativeVolume);r.lineTo(y,b),m<e.length-1&&r.lineTo(u(e[m+1].price),b)}r.lineTo(u(e[e.length-1].price),c)}if(r.closePath(),r.fillStyle=o,r.fill(),r.beginPath(),h==="bid"){r.moveTo(u(e[0].price),g(e[0].cumulativeVolume));for(let m=0;m<e.length;m++){const y=u(e[m].price),b=g(e[m].cumulativeVolume);m>0&&r.lineTo(y,g(e[m-1].cumulativeVolume)),r.lineTo(y,b)}}else{r.moveTo(u(e[0].price),g(e[0].cumulativeVolume));for(let m=1;m<e.length;m++){const y=g(e[m-1].cumulativeVolume),b=u(e[m].price),p=g(e[m].cumulativeVolume);r.lineTo(b,y),r.lineTo(b,p)}}r.strokeStyle=l,r.lineWidth=1.5,r.stroke()}function Ui(r,e,t,n,s,i,o,l,h){const{left:a,top:c,bottom:d,plotWidth:f}=n,u=a+(e-s)/i*f;if(r.save(),r.strokeStyle=o.textSecondary,r.lineWidth=1,r.setLineDash([4,4]),r.beginPath(),r.moveTo(u,c),r.lineTo(u,d),r.stroke(),r.restore(),l){const g=`Spread: ${h(t)}`;r.save(),r.font=`${o.font.sizeSmall}px ${o.font.family}`,r.fillStyle=o.textSecondary,r.textAlign="center",r.textBaseline="bottom",r.fillText(g,u,c-2),r.restore()}}function xi(r,e,t,n,s,i,o,l,h,a,c){const{left:d,top:f,bottom:u,plotWidth:g,plotHeight:m}=s,y=e.x;if(y<d||y>s.right)return;const b=i+(y-d)/g*o;let p=0,T="";if(t.length>0&&b<=t[0].price){T="Bid";for(const k of t)if(b<=k.price)p=k.cumulativeVolume;else break}if(n.length>0&&b>=n[0].price){T="Ask";for(const k of n)if(b>=k.price)p=k.cumulativeVolume;else break}if(r.save(),r.strokeStyle=h.crosshair,r.lineWidth=.5,r.setLineDash([4,4]),r.beginPath(),r.moveTo(y,f),r.lineTo(y,u),r.stroke(),p>0){const k=u-p/l*m;r.beginPath(),r.moveTo(d,k),r.lineTo(s.right,k),r.stroke()}r.restore();const v=[`Price: ${a(b)}`,T?`${T} Vol: ${c(p)}`:""].filter(Boolean);r.save(),r.font=`${h.font.sizeSmall}px ${h.font.family}`;const M=Math.max(...v.map(k=>r.measureText(k).width))+16,P=v.length*(h.font.sizeSmall+4)+12;let R=y+10,w=e.y-P/2;R+M>s.right&&(R=y-M-10),w<f&&(w=f),w+P>u&&(w=u-P),r.fillStyle=h.axisLabelBackground,r.globalAlpha=.9,r.fillRect(R,w,M,P),r.globalAlpha=1,r.fillStyle=h.text,r.textBaseline="top",r.textAlign="left";for(let k=0;k<v.length;k++)r.fillText(v[k],R+8,w+6+k*(h.font.sizeSmall+4));r.restore()}function Oi(r){return r.toFixed(2)}function qi(r){return r>=1e6?(r/1e6).toFixed(2)+"M":r>=1e3?(r/1e3).toFixed(1)+"K":r.toFixed(0)}function zi(r,e,t,n,s,i){const{data:o}=n;if(o.bids.length===0&&o.asks.length===0||e<=0||t<=0)return;const l=n.bidColor??s.candleUp,h=n.askColor??s.candleDown,a=n.bidFillColor??l+"40",c=n.askFillColor??h+"40",d=n.midPriceLine!==!1,f=n.spreadLabel!==!1,u=n.priceFormat??Oi,g=n.volumeFormat??qi,m=Ei(o.bids),y=Hi(o.asks),b=m.length>0?m[0].price:0,p=y.length>0?y[0].price:0,T=m.length>0&&y.length>0?(b+p)/2:b||p,v=y.length>0&&m.length>0?p-b:0,S=m.length>0?m[m.length-1].price:T,M=y.length>0?y[y.length-1].price:T,R=Math.max(T-S,M-T)*(1+Fi),w=T-R,k=T+R,C=k-w,I=m.length>0?m[m.length-1].cumulativeVolume:0,B=y.length>0?y[y.length-1].cumulativeVolume:0,F=Math.max(I,B)||1,W=$i(e,t);Yi(r,e,t,s.background),Vi(r,W,w,k,F,s,u,g),Pe(r,m,W,w,C,F,a,l,"bid"),Pe(r,y,W,w,C,F,c,h,"ask"),d&&Ui(r,T,v,W,w,C,s,f,u),i&&xi(r,i,m,y,W,w,C,F,s,u,g)}const Re=70,ke=30,we=20,Ae=20,Xi=.05;function _i(r,e){return{left:Re,right:r-we,top:Ae,bottom:e-ke,plotWidth:r-Re-we,plotHeight:e-Ae-ke}}function ji(r,e){let t=1/0,n=-1/0;for(const i of r)i.value<t&&(t=i.value),i.value>n&&(n=i.value);if(e)for(const i of e)i.value<t&&(t=i.value),i.value>n&&(n=i.value);const s=(n-t)*Xi;return{valMin:t-s,valMax:n+s}}function Gi(r){const e=[];let t=-1/0;for(const n of r)n.value>t&&(t=n.value),e.push(t);return e}function Ni(r){if(r<7*864e5){const s=new Intl.DateTimeFormat(void 0,{hour:"2-digit",minute:"2-digit"});return i=>s.format(new Date(i))}if(r<315576e5){const s=new Intl.DateTimeFormat(void 0,{month:"short",day:"numeric"});return i=>s.format(new Date(i))}const n=new Intl.DateTimeFormat(void 0,{month:"short",year:"numeric"});return s=>n.format(new Date(s))}function Ji(r){return Math.abs(r)>=1e6?(r/1e6).toFixed(2)+"M":Math.abs(r)>=1e3?(r/1e3).toFixed(1)+"K":r.toFixed(2)}function Ki(r,e,t,n){r.fillStyle=n,r.fillRect(0,0,e,t)}function Zi(r,e,t,n,s,i,o,l,h){const{left:a,right:c,top:d,bottom:f,plotWidth:u,plotHeight:g}=e;r.save(),r.strokeStyle=o.grid,r.lineWidth=.5,r.fillStyle=o.axisLabel,r.font=`${o.font.sizeSmall}px ${o.font.family}`;const m=L.computeTickStep(t,n,6),y=Math.ceil(t/m)*m;for(let v=y;v<=n;v+=m){const S=f-(v-t)/(n-t)*g;r.beginPath(),r.moveTo(a,S),r.lineTo(c,S),r.stroke(),r.textAlign="right",r.textBaseline="middle",r.fillText(l(v),a-6,S)}const b=i-s,p=L.computeTickStep(s,i,6),T=Math.ceil(s/p)*p;for(let v=T;v<=i;v+=p){const S=a+(v-s)/b*u;r.beginPath(),r.moveTo(S,d),r.lineTo(S,f),r.stroke(),r.textAlign="center",r.textBaseline="top",r.fillText(h(v),S,f+4)}r.restore()}function Le(r,e,t,n,s,i,o,l,h){if(e.length<2)return;const{left:a,bottom:c,plotWidth:d,plotHeight:f}=t,u=m=>a+(m-n)/s*d,g=m=>c-(m-i)/o*f;r.save(),r.beginPath(),r.moveTo(u(e[0].time),g(e[0].value));for(let m=1;m<e.length;m++)r.lineTo(u(e[m].time),g(e[m].value));r.strokeStyle=l,r.lineWidth=h,r.lineJoin="round",r.stroke(),r.restore()}function Qi(r,e,t,n,s,i,o,l){if(e.length<2)return;const{left:h,bottom:a,plotWidth:c,plotHeight:d}=t,f=m=>h+(m-n)/s*c,u=m=>a-(m-i)/o*d,g=r.createLinearGradient(0,t.top,0,a);g.addColorStop(0,l+"40"),g.addColorStop(1,"transparent"),r.save(),r.beginPath(),r.moveTo(f(e[0].time),u(e[0].value));for(let m=1;m<e.length;m++)r.lineTo(f(e[m].time),u(e[m].value));r.lineTo(f(e[e.length-1].time),a),r.lineTo(f(e[0].time),a),r.closePath(),r.fillStyle=g,r.fill(),r.restore()}function es(r,e,t,n,s,i,o,l,h){if(e.length<2)return;const{left:a,bottom:c,plotWidth:d,plotHeight:f}=n,u=m=>a+(m-s)/i*d,g=m=>c-(m-o)/l*f;r.save(),r.fillStyle=h,r.beginPath(),r.moveTo(u(e[0].time),g(t[0]));for(let m=1;m<e.length;m++)r.lineTo(u(e[m].time),g(t[m]));for(let m=e.length-1;m>=0;m--)r.lineTo(u(e[m].time),g(e[m].value));r.closePath(),r.fill(),r.restore()}function ts(r,e,t,n,s,i,o,l,h,a){if(e.length===0)return;const{left:c,bottom:d,plotWidth:f,plotHeight:u}=t,g=e[e.length-1],m=c+(g.time-n)/s*f,y=d-(g.value-i)/o*u;r.save(),r.font=`${a.font.sizeSmall}px ${a.font.family}`,r.fillStyle=h,r.textAlign="left",r.textBaseline="middle",r.fillText(l,m+4,y),r.restore()}function ns(r,e,t,n,s,i,o,l,h,a,c,d){const{left:f,top:u,bottom:g,plotWidth:m,plotHeight:y}=s,b=e.x;if(b<f||b>s.right)return;r.save(),r.strokeStyle=a.crosshair,r.lineWidth=.5,r.setLineDash([4,4]),r.beginPath(),r.moveTo(b,u),r.lineTo(b,g),r.stroke(),r.restore();const p=i+(b-f)/m*o;let T=0,v=1/0;for(let F=0;F<t.length;F++){const W=Math.abs(t[F].time-p);W<v&&(v=W,T=F)}const S=t[T],M=[c(S.time),`Value: ${d(S.value)}`];n&&n.length>T&&M.push(`Benchmark: ${d(n[T].value)}`);const P=f+(S.time-i)/o*m,R=g-(S.value-l)/h*y;r.beginPath(),r.arc(P,R,3,0,Math.PI*2),r.fillStyle=a.lineColor,r.fill(),r.save(),r.font=`${a.font.sizeSmall}px ${a.font.family}`;const k=Math.max(...M.map(F=>r.measureText(F).width))+16,C=M.length*(a.font.sizeSmall+4)+12;let I=b+10,B=e.y-C/2;I+k>s.right&&(I=b-k-10),B<u&&(B=u),B+C>g&&(B=g-C),r.fillStyle=a.axisLabelBackground,r.globalAlpha=.9,r.fillRect(I,B,k,C),r.globalAlpha=1,r.fillStyle=a.text,r.textBaseline="top",r.textAlign="left";for(let F=0;F<M.length;F++)r.fillText(M[F],I+8,B+6+F*(a.font.sizeSmall+4));r.restore()}function is(r,e,t,n,s,i){const{data:o}=n;if(o.length<2||e<=0||t<=0)return;const l=n.lineColor??s.lineColor,h=n.lineWidth??1.5,a=n.drawdown===!0,c=n.drawdownColor??s.candleDown+"30",d=n.fillArea===!0,f=n.benchmark,u=n.benchmarkColor??s.textSecondary,g=n.benchmarkLabel??"Benchmark",m=n.valueFormat??Ji,{valMin:y,valMax:b}=ji(o,f),p=b-y||1,T=o[0].time,v=o[o.length-1].time,S=v-T||1,M=n.timeFormat??Ni(S),P=_i(e,t);if(Ki(r,e,t,s.background),Zi(r,P,y,b,T,v,s,m,M),a){const R=Gi(o);es(r,o,R,P,T,S,y,p,c)}d&&Qi(r,o,P,T,S,y,p,l),Le(r,o,P,T,S,y,p,l,h),f&&f.length>=2&&(Le(r,f,P,T,S,y,p,u,1),ts(r,f,P,T,S,y,p,g,u,s)),i&&ns(r,i,o,f,P,T,S,y,p,s,M,m)}function ss(r,e,t){if(r.length===0)return[];const n=r.length,s=Math.ceil(Math.sqrt(n)),i=Math.ceil(n/s),o=e.width/s,l=e.height/i,h=[];for(let a=0;a<n;a++){const c=a%s,d=Math.floor(a/s);h.push({id:r[a].id,rect:{x:e.x+c*o+t,y:e.y+d*l+t,width:Math.max(0,o-t*2),height:Math.max(0,l-t*2)}})}return h}function Ce(r,e){const t=r.reduce((s,i)=>s+i,0);if(t===0||e===0)return 1/0;let n=0;for(const s of r){const i=t/e,o=s/i,l=i>o?i/o:o/i;l>n&&(n=l)}return n}function le(r,e,t,n,s,i){const o=n?t.height:t.width,l=r.reduce((c,d)=>c+d,0),h=o>0?l/o:0;let a=0;for(let c=0;c<r.length;c++){const d=h>0?r[c]/h:0;let f;n?f={x:t.x+s,y:t.y+a+s,width:Math.max(0,h-s*2),height:Math.max(0,d-s*2)}:f={x:t.x+a+s,y:t.y+s,width:Math.max(0,d-s*2),height:Math.max(0,h-s*2)},i.push({id:e[c],rect:f}),a+=d}}function Zt(r,e,t,n,s){if(r.length===0)return;const i=t.width>=t.height,o=i?t.height:t.width;if(r.length===1){le(r,e,t,i,n,s);return}const l=[],h=[];let a=0;for(;a<r.length;){const c=[...l,r[a]];if(l.length===0||Ce(c,o)<=Ce(l,o))l.push(r[a]),h.push(e[a]),a++;else{le(l,h,t,i,n,s);const d=l.reduce((u,g)=>u+g,0),f=o>0?d/o:0;i?t={x:t.x+f,y:t.y,width:t.width-f,height:t.height}:t={x:t.x,y:t.y+f,width:t.width,height:t.height-f},Zt(r.slice(a),e.slice(a),t,n,s);return}}l.length>0&&le(l,h,t,i,n,s)}function os(r,e,t){if(r.length===0)return[];const n=[...r].sort((a,c)=>c.weight-a.weight),s=n.reduce((a,c)=>a+Math.max(0,c.weight),0);if(s===0)return[];const i=e.width*e.height,o=n.map(a=>Math.max(0,a.weight)/s*i),l=n.map(a=>a.id),h=[];return Zt(o,l,{...e},t,h),h}function rs(r){return(r>=0?"+":"")+r.toFixed(2)+"%"}function ls(r){return r.label}function as(r,e){const t=e.min??-5,n=e.max??5;if(r<=t)return e.negative;if(r>=n)return e.positive;if(r<0){const i=1-r/t;return L.lerpColor(e.negative,e.zero,i)}const s=r/n;return L.lerpColor(e.zero,e.positive,s)}function he(r,e,t,n,s,i){const o=Math.min(i,n/2,s/2);r.beginPath(),r.moveTo(e+o,t),r.lineTo(e+n-o,t),r.arcTo(e+n,t,e+n,t+o,o),r.lineTo(e+n,t+s-o),r.arcTo(e+n,t+s,e+n-o,t+s,o),r.lineTo(e+o,t+s),r.arcTo(e,t+s,e,t+s-o,o),r.lineTo(e,t+o),r.arcTo(e,t,e+o,t,o),r.closePath()}function hs(r,e,t,n,s,i,o,l,h,a,c){if(s<40||i<30)return;const d=t+s/2,f=n+i/2;if(r.save(),r.shadowColor="rgba(0,0,0,0.6)",r.shadowBlur=2,r.fillStyle="#FFFFFF",r.textAlign="center",l){const u=a(e),g=Math.min(o.font.sizeMedium,s/u.length*1.5,i/4);r.font="bold "+Math.max(8,g)+"px "+o.font.family,r.textBaseline=h?"bottom":"middle",r.fillText(u,d,h?f-1:f,s-8)}if(h){const u=c(e.value),g=Math.min(o.font.sizeSmall,s/u.length*1.5,i/4);r.font=Math.max(7,g)+"px "+o.font.family,r.textBaseline=l?"top":"middle",r.fillText(u,d,l?f+1:f,s-8)}r.restore()}function cs(r,e,t,n,s,i,o,l){const h=[o(e),l(e.value)];if(e.meta)for(const[m,y]of Object.entries(e.meta))h.push(m+": "+String(y));r.save(),r.font=i.font.sizeSmall+"px "+i.font.family;const c=Math.max(...h.map(m=>r.measureText(m).width))+16,d=i.font.sizeSmall+4,f=h.length*d+12;let u=t.x+14,g=t.y-f/2;u+c>n&&(u=t.x-c-14),g<0&&(g=0),g+f>s&&(g=s-f),r.fillStyle=i.axisLabelBackground,r.globalAlpha=.92,he(r,u,g,c,f,4),r.fill(),r.globalAlpha=1,r.fillStyle=i.text,r.textBaseline="top",r.textAlign="left";for(let m=0;m<h.length;m++)r.fillText(h[m],u+8,g+6+m*d);r.restore()}function ds(r,e,t,n,s,i,o,l){if(n.length===0||s.length===0||e<=0||t<=0)return null;const h=i.colorScale??{negative:"#EF5350",zero:"#424242",positive:"#26A69A"},a=i.cellRadius??4,c=i.showLabels!==!1,d=i.showValues!==!1,f=i.valueFormat??rs,u=i.labelFormat??ls,g=new Map;for(const b of n)g.set(b.id,b);let m=null,y=null;if(l)for(const b of s){const{x:p,y:T,width:v,height:S}=b.rect;if(l.x>=p&&l.x<=p+v&&l.y>=T&&l.y<=T+S){m=b.id;const M=g.get(b.id);M&&(y={cell:M,rect:b});break}}r.fillStyle=o.background,r.fillRect(0,0,e,t);for(const b of s){const p=g.get(b.id);if(!p)continue;const{x:T,y:v,width:S,height:M}=b.rect;if(S<=0||M<=0)continue;const P=as(p.value,h);he(r,T,v,S,M,a),r.fillStyle=P,r.fill(),hs(r,p,T,v,S,M,o,c,d,u,f),b.id===m&&(he(r,T,v,S,M,a),r.strokeStyle="#FFFFFF",r.lineWidth=1.5,r.stroke())}return y&&l&&cs(r,y.cell,l,e,t,o,u,f),y}const Ie=60,De=12,We=24,Be=40,fs=.7,us=1;function Qt(r){return r.type?r.type:r.value>0?"positive":r.value<0?"negative":"subtotal"}function gs(r){const e=Math.abs(r);return e>=1e6?(r/1e6).toFixed(2)+"M":e>=1e3?(r/1e3).toFixed(1)+"K":r.toFixed(2)}function ms(r,e){return{left:Ie,right:r-De,top:We,bottom:e-Be,plotWidth:r-Ie-De,plotHeight:e-We-Be}}function ys(r,e,t,n,s){const i=new Array(r.length),o=new Array(r.length),l=e.plotWidth/r.length,h=l*t,a=(l-h)/2,{left:c,bottom:d,plotHeight:f}=e;let u=0;for(let g=0;g<r.length;g++){const m=r[g],y=Qt(m);let b,p;y==="total"||y==="subtotal"?(b=0,p=m.value,u=m.value):(b=u,p=u+m.value,u=p);const T=Math.min(b,p),v=Math.max(b,p),S=d-(T-n)/s*f,M=d-(v-n)/s*f,P=Math.min(S,M),R=Math.max(Math.abs(M-S),us),w=c+g*l,k=w+a;i[g]={label:m.label,value:m.value,start:b,end:p,type:y,rectX:k,rectY:P,rectW:h,rectH:R,slotX:w,slotW:l},o[g]=u}return{bars:i,cumulatives:o}}function ps(r){if(r.length===0)return{valMin:0,valMax:1};let e=0,t=0,n=0;for(let o=0;o<r.length;o++){const l=r[o],h=Qt(l);let a,c;h==="total"||h==="subtotal"?(a=0,c=l.value,n=l.value):(a=n,c=n+l.value,n=c),a<e&&(e=a),c<e&&(e=c),a>t&&(t=a),c>t&&(t=c)}const i=(t-e||1)*.1;return{valMin:e-i,valMax:t+i}}function bs(r,e,t,n){r.fillStyle=n,r.fillRect(0,0,e,t)}function Ts(r,e,t,n,s,i){const{left:o,right:l,top:h,bottom:a,plotHeight:c}=e,d=n-t||1;r.strokeStyle=s.grid,r.lineWidth=.5,r.fillStyle=s.axisLabel,r.font=`${s.font.sizeSmall}px ${s.font.family}`,r.textAlign="right",r.textBaseline="middle";const f=L.computeTickStep(t,n,5),u=Math.ceil(t/f)*f;r.beginPath();for(let g=u;g<=n;g+=f){const m=Math.round(a-(g-t)/d*c)+.5;m<h||m>a||(r.moveTo(o,m),r.lineTo(l,m))}r.stroke();for(let g=u;g<=n;g+=f){const m=a-(g-t)/d*c;m<h-1||m>a+1||r.fillText(i(g),o-6,m)}if(t<0&&n>0){const g=Math.round(a-(0-t)/d*c)+.5;r.strokeStyle=s.axisLine,r.lineWidth=1,r.beginPath(),r.moveTo(o,g),r.lineTo(l,g),r.stroke()}}function vs(r,e,t){const n=["positive","negative","total","subtotal"];for(let s=0;s<n.length;s++){const i=n[s],o=t[i];let l=!1;r.beginPath();for(let h=0;h<e.length;h++){const a=e[h];if(a.type!==i)continue;const c=Math.round(a.rectX),d=Math.round(a.rectY),f=Math.round(a.rectW),u=Math.round(a.rectH);r.rect(c,d,f,u),l=!0}l&&(r.fillStyle=o,r.fill())}}function Ms(r,e,t,n,s,i,o){if(o==="none"||e.length<2)return;const{bottom:l,plotHeight:h}=t;r.strokeStyle=i,r.lineWidth=1,o==="dashed"?r.setLineDash([4,4]):r.setLineDash([]),r.beginPath();for(let a=0;a<e.length-1;a++){const c=e[a],d=e[a+1],f=c.end,u=Math.round(l-(f-n)/s*h)+.5,g=Math.round(c.rectX+c.rectW),m=Math.round(d.rectX);m<=g||(r.moveTo(g,u),r.lineTo(m,u))}r.stroke(),r.setLineDash([])}function Ss(r,e,t,n,s,i){r.fillStyle=n.text,r.font=`${n.font.sizeSmall}px ${n.font.family}`,r.textAlign="center";for(let o=0;o<e.length;o++){const l=e[o];let h;l.type==="total"||l.type==="subtotal"?h=s(l.value):i?h=`${l.value>=0?"+":""}${s(l.value)}`:h=s(t[o]);const a=l.rectX+l.rectW/2;l.end<l.start?(r.textBaseline="top",r.fillText(h,a,l.rectY+l.rectH+4)):(r.textBaseline="bottom",r.fillText(h,a,l.rectY-4))}}function Ps(r,e,t,n,s){r.fillStyle=n.axisLabel,r.font=`${n.font.sizeSmall}px ${n.font.family}`,r.textAlign="center",r.textBaseline="top";const i=t.bottom+6,o=e.length>0?e[0].slotW-4:0;let l=!1;for(let h=0;h<e.length;h++)if(r.measureText(s(e[h].label)).width>o){l=!0;break}if(!l){for(let h=0;h<e.length;h++){const a=e[h],c=a.slotX+a.slotW/2;r.fillText(s(a.label),c,i)}return}r.save(),r.textAlign="right",r.textBaseline="middle";for(let h=0;h<e.length;h++){const a=e[h],c=a.slotX+a.slotW/2;r.setTransform(1,0,0,1,c,i+4),r.rotate(-Math.PI/4),r.fillText(s(a.label),0,0)}r.setTransform(1,0,0,1,0,0),r.restore()}function Rs(r,e,t){if(e<t.left||e>t.right)return null;for(let n=0;n<r.length;n++){const s=r[n];if(e>=s.slotX&&e<=s.slotX+s.slotW)return n}return null}function ks(r,e,t,n,s,i,o,l){const h=e[n],a=t[n],c=Math.round(h.slotX+h.slotW/2)+.5;r.strokeStyle=o.crosshair,r.lineWidth=.5,r.setLineDash([4,4]),r.beginPath(),r.moveTo(c,i.top),r.lineTo(c,i.bottom),r.stroke(),r.setLineDash([]);const d=[h.label,`Value: ${h.value>=0&&(h.type==="positive"||h.type==="negative")?"+":""}${l(h.value)}`,`Total: ${l(a)}`];r.font=`${o.font.sizeSmall}px ${o.font.family}`;let f=0;for(let p=0;p<d.length;p++){const T=r.measureText(d[p]).width;T>f&&(f=T)}const u=f+16,g=d.length*(o.font.sizeSmall+4)+12;let m=s.x+10,y=s.y-g/2;m+u>i.right&&(m=s.x-u-10),y<i.top&&(y=i.top),y+g>i.bottom&&(y=i.bottom-g),r.fillStyle=o.axisLabelBackground,r.globalAlpha=.9,r.fillRect(m,y,u,g),r.globalAlpha=1,r.fillStyle=o.text,r.textAlign="left",r.textBaseline="top";const b=o.font.sizeSmall+4;for(let p=0;p<d.length;p++)r.fillText(d[p],m+8,y+6+p*b)}function ws(r,e,t,n,s,i){const{data:o}=n;if(!o||o.length===0||e<=0||t<=0)return{hitBar:null};const l=n.positiveColor??s.candleUp,h=n.negativeColor??s.candleDown,a=n.totalColor??s.lineColor,c=n.subtotalColor??s.textSecondary,d=n.connectorColor??s.textSecondary,f=n.connectorStyle??"dashed",u=n.barWidth??fs,g=n.showValues!==!1,m=n.showDelta!==!1,y=n.valueFormat??gs,b=n.labelFormat??(w=>w),{valMin:p,valMax:T}=ps(o),v=T-p||1,S=ms(e,t),{bars:M,cumulatives:P}=ys(o,S,u,p,v);bs(r,e,t,s.background),Ts(r,S,p,T,s,y),Ms(r,M,S,p,v,d,f),vs(r,M,{positive:l,negative:h,total:a,subtotal:c}),g&&Ss(r,M,P,s,y,m),Ps(r,M,S,s,b);const R=i?Rs(M,i.x,S):null;return i&&R!==null&&ks(r,M,P,R,i,S,s,y),{hitBar:R}}const Fe=Math.PI/180,As=135,Ls=405,Cs=.25;function Is(r){const e=Math.abs(r);return e>=1e6?(r/1e6).toFixed(2)+"M":e>=1e3?(r/1e3).toFixed(1)+"K":Number.isInteger(r)?r.toFixed(0):r.toFixed(1)}function G(r,e,t){return r<e?e:r>t?t:r}function Ds(r,e,t){const n=t.min??0,s=t.max??100,i=s-n||1,o=(t.startAngle??As)*Fe,l=(t.endAngle??Ls)*Fe,h=t.thickness??Cs,a=24,c=r/2,d=e/2+e*.05,f=Math.max(10,Math.min(r,e)/2-a),u=Math.max(2,f*(1-h));return{cx:c,cy:d,radius:f,innerRadius:u,startAngle:o,endAngle:l,angleRange:l-o,min:n,max:s,range:i}}function te(r,e){const t=G((r-e.min)/e.range,0,1);return e.startAngle+e.angleRange*t}function Ws(r,e,t,n){r.fillStyle=n,r.fillRect(0,0,e,t)}function Bs(r,e,t){r.beginPath(),r.arc(e.cx,e.cy,e.radius,e.startAngle,e.endAngle,!1),r.arc(e.cx,e.cy,e.innerRadius,e.endAngle,e.startAngle,!0),r.closePath(),r.fillStyle=t,r.fill()}function Fs(r,e,t){const n=G(t.from,e.min,e.max),s=G(t.to,e.min,e.max);if(s<=n)return;const i=te(n,e),o=te(s,e);r.beginPath(),r.arc(e.cx,e.cy,e.radius,i,o,!1),r.arc(e.cx,e.cy,e.innerRadius,o,i,!0),r.closePath(),r.fillStyle=t.color,r.fill()}function Es(r,e,t,n,s){const i=te(t,e);i<=e.startAngle||(r.globalAlpha=s,r.beginPath(),r.arc(e.cx,e.cy,e.radius,e.startAngle,i,!1),r.arc(e.cx,e.cy,e.innerRadius,i,e.startAngle,!0),r.closePath(),r.fillStyle=n,r.fill(),r.globalAlpha=1)}function Hs(r,e,t,n){const s=te(t,e),i=Math.cos(s),o=Math.sin(s),l=e.radius-2,h=e.innerRadius*.25,a=-o,c=i,d=4,f=e.cx+i*l,u=e.cy+o*l,g=e.cx+a*d-i*h,m=e.cy+c*d-o*h,y=e.cx-a*d-i*h,b=e.cy-c*d-o*h;r.beginPath(),r.moveTo(f,u),r.lineTo(g,m),r.lineTo(y,b),r.closePath(),r.fillStyle=n,r.fill(),r.beginPath(),r.arc(e.cx,e.cy,Math.max(4,e.innerRadius*.12),0,Math.PI*2),r.fill()}function $s(r,e,t,n,s){const i=Math.round(e.innerRadius*.55),o=Math.round(s.font.sizeMedium);r.fillStyle=s.text,r.textAlign="center",r.textBaseline="middle",r.font=`600 ${i}px ${s.font.family}`;const l=!!n,h=l?e.cy-o*.6:e.cy-i*.1;r.fillText(t,e.cx,h),l&&(r.fillStyle=s.textSecondary,r.font=`${o}px ${s.font.family}`,r.fillText(n,e.cx,e.cy+i*.55))}function Ys(r,e,t,n){r.fillStyle=n.textSecondary,r.font=`${n.font.sizeSmall}px ${n.font.family}`;const s=e.radius+10,i=Math.cos(e.startAngle),o=Math.sin(e.startAngle),l=Math.cos(e.endAngle),h=Math.sin(e.endAngle),a=e.cx+i*s,c=e.cy+o*s,d=e.cx+l*s,f=e.cy+h*s;r.textBaseline="middle",r.textAlign=i<-.2?"right":i>.2?"left":"center",r.fillText(t(e.min),a,c),r.textAlign=l<-.2?"right":l>.2?"left":"center",r.fillText(t(e.max),d,f)}function Vs(r,e,t,n){r.fillStyle=n.textSecondary,r.font=`${n.font.sizeSmall}px ${n.font.family}`,r.textAlign="center",r.textBaseline="middle";const s=(e.radius+e.innerRadius)/2;for(let i=0;i<t.length;i++){const o=t[i];if(!o.label)continue;const l=(G(o.from,e.min,e.max)+G(o.to,e.min,e.max))/2,h=te(l,e),a=e.cx+Math.cos(h)*s,c=e.cy+Math.sin(h)*s;r.fillText(o.label,a,c)}}function Us(r,e,t,n,s,i){if(e<=0||t<=0)return;const o=Ds(e,t,n),l=n.trackColor??s.grid,h=n.needleColor??s.text,a=n.showValue!==!1,c=n.showZoneLabels===!0,d=n.valueFormat??Is,f=G(i,o.min,o.max);Ws(r,e,t,s.background),Bs(r,o,l);const u=n.zones;if(u&&u.length>0)for(let g=0;g<u.length;g++)Fs(r,o,u[g]);Es(r,o,f,h,.12),Hs(r,o,f,h),a&&$s(r,o,d(f),n.label,s),Ys(r,o,d,s),c&&u&&u.length>0&&Vs(r,o,u,s)}exports.ADXIndicator=rt;exports.ATRIndicator=ot;exports.AccumulationDistributionIndicator=pt;exports.AlertManager=mi;exports.AnchoredVWAPIndicator=Je;exports.AnchoredVWAPTool=xt;exports.Animator=Ii;exports.AreaRenderer=rn;exports.AroonIndicator=dt;exports.ArrowTool=Yt;exports.AwesomeOscillatorIndicator=et;exports.BarCountdown=fi;exports.BarRenderer=ln;exports.BaseFinanceChart=Di;exports.BaselineRenderer=hn;exports.BinanceAdapter=li;exports.BollingerBandsIndicator=Oe;exports.CCIIndicator=ht;exports.CMFIndicator=gt;exports.CandlestickRenderer=sn;exports.CanvasLayer=Ee;exports.ChaikinOscillatorIndicator=tt;exports.ChartLegend=hi;exports.ChartStateManager=Pi;exports.CompareRenderer=bn;exports.CrosshairHandler=Un;exports.CrosshairTooltip=On;exports.CurrentPriceLine=Nt;exports.DEFAULT_LEGEND_CONFIG=Jt;exports.DPRManager=Ye;exports.DataExporter=ki;exports.DateRangeTool=Et;exports.DepthOverlay=Xt;exports.DonchianChannelIndicator=Ge;exports.DrawingBase=$;exports.DrawingManager=Xn;exports.DrawingRenderer=_n;exports.EMAIndicator=xe;exports.Easing=Kt;exports.ElliottWaveTool=Bt;exports.EllipseTool=It;exports.Emitter=z;exports.EventBus=zn;exports.ExtendedLineTool=Rt;exports.FibExtensionTool=Lt;exports.FibRetracementTool=At;exports.FinanceCrosshair=Wi;exports.GannBoxTool=Ut;exports.GannFanTool=Vt;exports.GridRenderer=Wn;exports.HLCAreaRenderer=gn;exports.HollowCandleRenderer=an;exports.HorizontalLineTool=Mt;exports.HullMAIndicator=Qe;exports.IchimokuIndicator=ze;exports.IndicatorBase=H;exports.IndicatorEngine=An;exports.IndicatorWorkerHost=Dn;exports.InteractionManager=$n;exports.KagiRenderer=dn;exports.KeltnerChannelIndicator=je;exports.KeyboardHandler=xn;exports.LayerManager=He;exports.LineRenderer=on;exports.LineWithMarkersRenderer=yn;exports.LinearRegressionChannelIndicator=Ze;exports.MACDIndicator=it;exports.MFIIndicator=ct;exports.MeasureTool=Ht;exports.MockAdapter=ai;exports.OBVIndicator=lt;exports.OrderRenderer=qt;exports.PanHandler=Yn;exports.ParabolicSARIndicator=Xe;exports.ParallelChannelTool=kt;exports.PitchforkTool=Wt;exports.PivotPointsIndicator=Ne;exports.PointAndFigureRenderer=fn;exports.PositionRenderer=zt;exports.PriceAxis=Bn;exports.PriceRangeTool=Ft;exports.ROCIndicator=ft;exports.RSIIndicator=nt;exports.RayTool=Pt;exports.ReconnectManager=ae;exports.RectangleTool=Ct;exports.RegressionChannelTool=wt;exports.RenderEngine=en;exports.RenderLoop=$e;exports.RenkoRenderer=cn;exports.ReplayManager=yi;exports.SMAIndicator=Ue;exports.Screenshot=ci;exports.SessionBreaks=ui;exports.SignalMarkerManager=Ai;exports.StdDevIndicator=mt;exports.StepLineRenderer=mn;exports.StochasticIndicator=st;exports.StreamManager=ii;exports.SupertrendIndicator=_e;exports.TSIIndicator=ut;exports.TextAnnotationTool=$t;exports.TickAggregator=Gt;exports.TimeAxis=Hn;exports.TradeZoneManager=Ci;exports.TradingContextMenu=jt;exports.TradingDragHandler=_t;exports.TradingManager=ti;exports.TradingRenderer=ni;exports.TrendLineTool=vt;exports.TriangleTool=Dt;exports.UndoRedoManager=Ri;exports.VROCIndicator=bt;exports.VWAPIndicator=qe;exports.VerticalLineTool=St;exports.Viewport=tn;exports.VolumeCandleRenderer=un;exports.VolumeProfileIndicator=yt;exports.VolumeProfileRangeTool=Ot;exports.VolumeRenderer=pn;exports.Watermark=di;exports.WilliamsRIndicator=at;exports.ZigZagIndicator=Ke;exports.ZoomHandler=Vn;exports.barIndexToX=D;exports.getIntParam=E;exports.getNumberParam=q;exports.getVisibleData=nn;exports.isWorkerResponse=Tt;exports.layoutSquarifiedTreemap=os;exports.layoutUniformGrid=ss;exports.priceToY=A;exports.registerBuiltInDrawingTools=Gn;exports.registerBuiltInIndicators=In;exports.renderDepthChart=zi;exports.renderEquityCurve=is;exports.renderGauge=Us;exports.renderHeatmap=ds;exports.renderSparkline=Bi;exports.renderWaterfall=ws;exports.resolveBarIndex=O;exports.timeToX=Ve;exports.timestampToBarIndex=j;exports.toHeikinAshi=Tn;exports.toKagi=Pn;exports.toLineBreak=Sn;exports.toPointAndFigure=Rn;exports.toRangeBars=kn;exports.toRenko=vn;exports.xToBarIndex=ee;exports.xToTime=_;exports.yToPrice=V;
21
- //# sourceMappingURL=index.cjs.map
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}},Qt=class{orders=[];positions=[];depthData=null;currentPrice=null;config;orderRenderer=new Ht;positionRenderer=new Jt;depthOverlay=new Yt;dragHandler;contextMenu=new Zt;requestRender=null;eventCallback=null;container=null;constructor(t){this.config={...e.DEFAULT_TRADING_CONFIG,...t},this.dragHandler=new Xt(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()}},$t=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)}},V=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()}},en=class extends V{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}},H=class extends V{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))}},tn=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)}},nn=class extends V{config=null;adapter=null;aggregator=null;reconnector;priceLine=new tn;state=`disconnected`;lastPrice=null;previousClose=null;unsubscribers=[];heartbeatTimer=null;lastMessageTime=0;constructor(){super(),this.reconnector=new H,this.wireReconnector()}async connect(t){this.disconnect(),this.config={...e.DEFAULT_STREAM_CONFIG,...t},this.adapter=t.adapter,this.aggregator=new en(t.timeframe),this.reconnector.removeAllListeners(),this.reconnector.stop(),this.reconnector=new H(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(){if(!this.adapter)return;let e=this.adapter,t=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())};e.on(`connectionChange`,t),this.unsubscribers.push(()=>e.off(`connectionChange`,t));let n=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)};e.on(`tick`,n),this.unsubscribers.push(()=>e.off(`tick`,n));let r=e=>{this.lastMessageTime=Date.now();let{bar:t,closed:n}=e.data;this.aggregator&&this.aggregator.processBar(t,n)};e.on(`bar`,r),this.unsubscribers.push(()=>e.off(`bar`,r));let i=e=>{this.emit(`error`,{message:e.data?.message??`Adapter error`})};e.on(`error`,i),this.unsubscribers.push(()=>e.off(`error`,i))}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()}},rn=class extends V{data;speed;index;status=`idle`;timer=null;constructor(e){if(super(),e.data.length===0)throw Error(`ReplayController requires non-empty data`);this.data=e.data,this.speed=Math.max(.01,e.speed??1),this.index=Math.max(0,Math.min(e.startIndex??0,e.data.length)),e.autoStart&&this.start()}getStatus(){return this.status}getIndex(){return this.index}getTotal(){return this.data.length}getPrefix(){return this.data.slice(0,this.index)}start(){if(this.status!==`playing`){if(this.index>=this.data.length){this.transition(`finished`);return}this.transition(`playing`),this.scheduleNext()}}pause(){this.status===`playing`&&(this.clearTimer(),this.transition(`paused`))}resume(){this.status===`paused`&&this.start()}stop(){this.clearTimer(),this.index=0,this.transition(`idle`)}step(e=1){if(!(e<=0)){this.clearTimer();for(let t=0;t<e;t++)if(!this.emitNext())return;this.status!==`finished`&&this.transition(`paused`)}}seek(e){let t=Math.max(0,Math.min(e,this.data.length));this.clearTimer(),this.index=t;let n=t>=this.data.length?`finished`:`paused`;this.transition(n)}setSpeed(e){this.speed=Math.max(.01,e),this.status===`playing`&&(this.clearTimer(),this.scheduleNext())}destroy(){this.clearTimer(),this.removeAllListeners()}scheduleNext(){let e=1e3/this.speed;this.timer=setTimeout(()=>{this.timer=null,this.status===`playing`&&this.emitNext()&&this.scheduleNext()},e)}emitNext(){if(this.index>=this.data.length)return this.transition(`finished`),this.emit(`finished`,{total:this.data.length}),!1;let e=this.data[this.index],t=this.index;return this.index++,this.emit(`bar`,{bar:e,index:t,total:this.data.length}),this.index>=this.data.length?(this.transition(`finished`),this.emit(`finished`,{total:this.data.length}),!1):!0}clearTimer(){this.timer!==null&&(clearTimeout(this.timer),this.timer=null)}transition(e){this.status!==e&&(this.status=e,this.emit(`stateChange`,{status:e,index:this.index,total:this.data.length}))}};function an(e){if(!Array.isArray(e)||e.length<6)return null;let t=e[0];if(typeof t!=`number`||!Number.isFinite(t))return null;let n=U(e[1]),r=U(e[2]),i=U(e[3]),a=U(e[4]),o=U(e[5]);return n===null||r===null||i===null||a===null||o===null?null:{time:t,open:n,high:r,low:i,close:a,volume:o}}function on(e){if(!sn(e))return null;let t=e.t;if(typeof t!=`number`||!Number.isFinite(t))return null;let n=U(e.o),r=U(e.h),i=U(e.l),a=U(e.c),o=U(e.v);if(n===null||r===null||i===null||a===null||o===null)return null;let s=typeof e.x==`boolean`?e.x:!1;return{bar:{time:t,open:n,high:r,low:i,close:a,volume:o},closed:s}}function U(e){if(typeof e==`number`)return Number.isFinite(e)?e:null;if(typeof e==`string`){let t=parseFloat(e);return Number.isFinite(t)?t:null}return null}function sn(e){return typeof e==`object`&&!!e}var cn={"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`},ln=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=cn[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}`);let o=await a.json();if(!Array.isArray(o))return[];let s=[];for(let e of o){let t=an(e);t&&s.push(t)}return s}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_${cn[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=>{let t;try{t=JSON.parse(typeof e.data==`string`?e.data:``)}catch{return}typeof t==`object`&&t&&t.e===`kline`&&this.handleKline(t.k)},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=on(e);if(!t)return;let{bar:n,closed:r}=t;this.emitEvent(`bar`,{bar:n,closed:r}),this.emitEvent(`tick`,{time:Date.now(),price:n.close,volume:n.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)}}},un=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)}}},dn={visible:!0,showSymbol:!0,showOHLC:!0,showChange:!0,showVolume:!0,position:`top-left`,fontSize:12},fn=class{config={...dn};symbol=``;timeframe=``;hoverBar=null;indicators=[];statusText=null;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}setStatusText(e){this.statusText=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(` · `),r=this.statusText?`${t} ${this.statusText}`:t;e.fillText(r,l,u),u+=s+6}else this.statusText&&(e.font=`${s}px ${n.font.family}`,e.fillStyle=n.textSecondary,e.fillText(this.statusText,l,u),u+=s+4);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}}}},pn=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)}},mn=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()}},hn=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`)}`}},gn=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=[]}},_n=1,vn=class extends V{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_${_n++}`,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)}}},yn=class extends V{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=[]}},W=1;function G(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function K(e){return Array.isArray(e)?e:[]}function bn(e,t){return typeof e==`string`?e:t}function q(e,t){return typeof e==`number`&&Number.isFinite(e)?e:t}function xn(e){if(!G(e)||typeof e.id!=`string`||typeof e.type!=`string`||!Array.isArray(e.anchors)||e.anchors.length===0||!G(e.style))return null;let t=[];for(let n of e.anchors){if(!G(n)||typeof n.time!=`number`||typeof n.price!=`number`||!Number.isFinite(n.time)||!Number.isFinite(n.price))return null;t.push({time:n.time,price:n.price})}let n=e.style;return typeof n.color!=`string`||typeof n.lineWidth!=`number`?null:{id:e.id,type:e.type,anchors:t,style:n,visible:typeof e.visible==`boolean`?e.visible:!0,locked:typeof e.locked==`boolean`?e.locked:!1,meta:G(e.meta)?e.meta:void 0}}function Sn(e){return!G(e)||typeof e.id!=`string`||typeof e.side!=`string`||typeof e.type!=`string`||typeof e.price!=`number`||typeof e.quantity!=`number`?null:e}function Cn(e){return!G(e)||typeof e.id!=`string`||typeof e.side!=`string`||typeof e.entryPrice!=`number`||typeof e.quantity!=`number`?null:e}function wn(e){if(!G(e))return Tn();typeof e.version==`number`&&e.version!==W&&console.warn(`Chart state version mismatch: ${e.version} vs ${W}`);let t=G(e.viewport)?e.viewport:{},n=[];for(let t of K(e.indicators))G(t)&&(typeof t.id!=`string`||typeof t.instanceId!=`string`||G(t.params)&&n.push({id:t.id,instanceId:t.instanceId,params:t.params,position:typeof t.position==`string`?t.position:void 0}));let r=[];for(let t of K(e.drawings)){let e=xn(t);e&&r.push(e)}let i=[];for(let t of K(e.orders)){let e=Sn(t);e&&i.push(e)}let a=[];for(let t of K(e.positions)){let e=Cn(t);e&&a.push(e)}let o=[];for(let t of K(e.alerts))G(t)&&typeof t.id==`string`&&o.push(t);return{version:typeof e.version==`number`?e.version:W,timestamp:q(e.timestamp,Date.now()),symbol:typeof e.symbol==`string`?e.symbol:void 0,timeframe:typeof e.timeframe==`string`?e.timeframe:void 0,chartType:bn(e.chartType,`candlestick`),viewport:{barWidth:q(t.barWidth,8),barSpacing:q(t.barSpacing,2),offset:q(t.offset,0)},theme:typeof e.theme==`string`?e.theme:void 0,locale:typeof e.locale==`string`?e.locale:void 0,indicators:n,drawings:r,orders:i,positions:a,alerts:o}}function Tn(){return{version:W,timestamp:Date.now(),chartType:`candlestick`,viewport:{barWidth:8,barSpacing:2,offset:0},indicators:[],drawings:[],orders:[],positions:[],alerts:[]}}var En=class{static capture(e,t){return{version:W,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){return wn(JSON.parse(e))}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()})}},Dn=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}}},On=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
+ `)}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)}},kn=1,An=class extends V{markers=[];style={...e.DEFAULT_SIGNAL_STYLE};requestRender=null;dataGetter=null;setRequestRender(e){this.requestRender=e}setDataGetter(e){this.dataGetter=e}setStyle(e){this.style={...this.style,...e},this.requestRender?.()}getStyle(){return{...this.style}}addMarker(e){let t=e.id??`tc_signal_${kn++}`,n={...e,id:t};return this.markers.push(n),this.emit(`added`,n),this.requestRender?.(),t}removeMarker(e){this.markers=this.markers.filter(t=>t.id!==e),this.emit(`removed`,e),this.requestRender?.()}getMarkers(){return[...this.markers]}clearMarkers(){this.markers=[],this.emit(`cleared`,void 0),this.requestRender?.()}setMarkers(e){this.markers=[...e],this.requestRender?.()}render(e,t,n){let r=this.dataGetter?.();if(!r||r.length===0||this.markers.length===0)return;let{chartRect:i,visibleRange:s}=t,c=this.style,u=c.arrowSize??12;e.save(),e.beginPath(),e.rect(i.x,i.y,i.width,i.height),e.clip();for(let d of this.markers){let f=l(d.time,r);if(f<s.from-1||f>s.to+1)continue;let p=a(f,t),m=o(d.price,t);if(p<i.x-u||p>i.x+i.width+u||m<i.y-u||m>i.y+i.height+u)continue;let h=this.resolveColor(d,c),g=u*Math.max(.6,Math.min(1,d.confidence));if(this.renderArrow(e,p,m,d.direction,g,h),c.showLabel&&d.label){e.font=`10px ${n.font.family}`,e.fillStyle=h,e.textAlign=`center`,e.textBaseline=d.direction===`long`?`top`:`bottom`;let t=d.direction===`long`?m+g+3:m-g-3;e.fillText(d.label,p,t)}if(c.showConfidence&&d.confidence<1){e.font=`9px ${n.font.family}`,e.fillStyle=h,e.globalAlpha=.7,e.textAlign=`center`,e.textBaseline=d.direction===`long`?`top`:`bottom`;let t=d.direction===`long`?m+g+(d.label?14:3):m-g-(d.label?14:3);e.fillText(`${Math.round(d.confidence*100)}%`,p,t),e.globalAlpha=1}}e.restore()}resolveColor(e,t){if(e.color)return e.color;if(t.sourceColors?.[e.source])return t.sourceColors[e.source];switch(e.direction){case`long`:return t.longColor??`#26A69A`;case`short`:return t.shortColor??`#EF5350`;default:return t.neutralColor??`#9E9E9E`}}renderArrow(e,t,n,r,i,a){e.fillStyle=a,e.beginPath(),r===`long`?(e.moveTo(t,n-i),e.lineTo(t-i*.6,n),e.lineTo(t-i*.2,n),e.lineTo(t-i*.2,n+i*.4),e.lineTo(t+i*.2,n+i*.4),e.lineTo(t+i*.2,n),e.lineTo(t+i*.6,n)):r===`short`?(e.moveTo(t,n+i),e.lineTo(t-i*.6,n),e.lineTo(t-i*.2,n),e.lineTo(t-i*.2,n-i*.4),e.lineTo(t+i*.2,n-i*.4),e.lineTo(t+i*.2,n),e.lineTo(t+i*.6,n)):e.arc(t,n,i*.4,0,Math.PI*2),e.closePath(),e.fill()}},jn=1,Mn=class extends V{zones=[];style={...e.DEFAULT_TRADE_ZONE_STYLE};requestRender=null;dataGetter=null;pricePrecision=2;setRequestRender(e){this.requestRender=e}setDataGetter(e){this.dataGetter=e}setPricePrecision(e){this.pricePrecision=e}setStyle(e){this.style={...this.style,...e},this.requestRender?.()}getStyle(){return{...this.style}}addZone(e){let t=e.id??`tc_zone_${jn++}`,n={...e,id:t};return this.zones.push(n),this.emit(`added`,n),this.requestRender?.(),t}updateZone(e,t){let n=this.zones.findIndex(t=>t.id===e);n!==-1&&(this.zones[n]={...this.zones[n],...t},this.requestRender?.())}removeZone(e){this.zones=this.zones.filter(t=>t.id!==e),this.emit(`removed`,e),this.requestRender?.()}getZones(){return[...this.zones]}clearZones(){this.zones=[],this.emit(`cleared`,void 0),this.requestRender?.()}setZones(e){this.zones=[...e],this.requestRender?.()}render(e,t,n){let r=this.dataGetter?.();if(!r||r.length===0||this.zones.length===0)return;let{chartRect:i}=t,a=this.style;e.save(),e.beginPath(),e.rect(i.x,i.y,i.width,i.height),e.clip();for(let i of this.zones)this.renderZone(e,i,r,t,n,a);e.restore()}renderZone(e,t,n,r,i,s){let{chartRect:c,visibleRange:u}=r,d=l(t.entryTime,n),f=t.exitTime==null?n.length-1:l(t.exitTime,n);if(f<u.from||d>u.to)return;let p=a(d,r),m=a(f,r),h=Math.max(c.x,Math.min(p,m)),g=Math.min(c.x+c.width,Math.max(p,m));if(g-h<1)return;let _=o(t.entryPrice,r),v=t.exitPrice==null?o(t.entryPrice,r):o(t.exitPrice,r),y=Math.min(_,v),b=Math.max(Math.max(_,v)-y,2),x=t.exitTime==null,S=t.pnl==null?t.direction===`long`?(t.exitPrice??t.entryPrice)>=t.entryPrice:(t.exitPrice??t.entryPrice)<=t.entryPrice:t.pnl>=0,C=x?s.activeColor??`#2196F3`:S?s.profitColor??`#26A69A`:s.lossColor??`#EF5350`;e.fillStyle=C,e.globalAlpha=s.fillOpacity??.12,e.fillRect(h,y,g-h,b),e.globalAlpha=1;let w=s.borderWidth??1;e.strokeStyle=C,e.lineWidth=w,e.setLineDash([]),e.beginPath(),e.moveTo(h,Math.round(_)+.5),e.lineTo(g,Math.round(_)+.5),e.stroke(),!x&&t.exitPrice!=null&&(e.setLineDash([3,3]),e.beginPath(),e.moveTo(h,Math.round(v)+.5),e.lineTo(g,Math.round(v)+.5),e.stroke(),e.setLineDash([]));let T=t.direction===`long`?`▲`:`▼`;if(e.font=`bold 10px ${i.font.family}`,e.fillStyle=C,e.textBaseline=`bottom`,e.textAlign=`left`,e.fillText(`${T} ${t.entryPrice.toFixed(this.pricePrecision)}`,h+4,_-3),s.showPnl&&!x&&t.pnl!=null){let n=t.pnl>=0?`+`:``,r=t.pnlPercent==null?`${n}${t.pnl.toFixed(2)}`:`${n}${t.pnl.toFixed(2)} (${n}${t.pnlPercent.toFixed(2)}%)`;e.font=`bold 10px ${i.font.family}`,e.fillStyle=C,e.textBaseline=`top`,e.textAlign=`right`;let a=e.measureText(r).width+8,o=g-a-2,s=y+2;e.globalAlpha=.85,e.fillStyle=i.background,e.fillRect(o,s,a,14),e.globalAlpha=1,e.fillStyle=C,e.fillText(r,g-4,s+1)}s.showLabel&&t.label&&(e.font=`9px ${i.font.family}`,e.fillStyle=C,e.globalAlpha=.8,e.textBaseline=`top`,e.textAlign=`left`,e.fillText(t.label,h+4,_+3),e.globalAlpha=1)}},Nn={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)},Pn=class{animations=new Map;frameId=0;running=!1;animate(e,t){this.animations.set(e,{startTime:-1,duration:t.duration,easing:t.easing??Nn.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()}};function Fn(t){return!t||t===`dark`?e.DARK_THEME:t===`light`?e.LIGHT_THEME:t}var In=class{canvas;ctx;container;theme;width=0;height=0;dpr=1;frameId=null;resizeObserver;constructor(e,t){this.container=e,this.theme=Fn(t),this.canvas=document.createElement(`canvas`),this.canvas.style.width=`100%`,this.canvas.style.height=`100%`,this.canvas.style.display=`block`,e.appendChild(this.canvas),this.ctx=this.canvas.getContext(`2d`),this.dpr=window.devicePixelRatio||1,this.updateSize(),this.resizeObserver=new ResizeObserver(()=>{this.updateSize(),this.requestRender()}),this.resizeObserver.observe(e)}updateSize(){this.dpr=window.devicePixelRatio||1;let e=this.container.getBoundingClientRect();this.width=e.width||this.container.clientWidth,this.height=e.height||this.container.clientHeight,this.canvas.width=Math.max(1,Math.floor(this.width*this.dpr)),this.canvas.height=Math.max(1,Math.floor(this.height*this.dpr))}requestRender(){this.frameId===null&&(this.frameId=requestAnimationFrame(()=>{this.frameId=null,(this.width===0||this.height===0)&&this.updateSize(),!(this.width===0||this.height===0)&&(this.ctx.save(),this.ctx.scale(this.dpr,this.dpr),this.ctx.clearRect(0,0,this.width,this.height),this.renderChart(this.ctx,this.width,this.height,this.theme),this.ctx.restore())}))}setTheme(e){this.theme=Fn(e),this.requestRender()}destroy(){this.frameId!==null&&cancelAnimationFrame(this.frameId),this.resizeObserver.disconnect(),this.canvas.remove()}},Ln=class{pos=null;canvas;onUpdate;handleMove;handleLeave;constructor(e,t){this.canvas=e,this.onUpdate=t,this.handleMove=t=>{let n=e.getBoundingClientRect();this.pos={x:t.clientX-n.left,y:t.clientY-n.top},this.onUpdate()},this.handleLeave=()=>{this.pos=null,this.onUpdate()},e.addEventListener(`pointermove`,this.handleMove),e.addEventListener(`pointerleave`,this.handleLeave)}getPosition(){return this.pos}isActive(){return this.pos!==null}drawCrosshair(e,t,n,r){this.pos&&(e.save(),e.strokeStyle=r,e.lineWidth=.5,e.setLineDash([4,4]),e.beginPath(),e.moveTo(this.pos.x,0),e.lineTo(this.pos.x,n),e.stroke(),e.beginPath(),e.moveTo(0,this.pos.y),e.lineTo(t,this.pos.y),e.stroke(),e.restore())}destroy(){this.canvas.removeEventListener(`pointermove`,this.handleMove),this.canvas.removeEventListener(`pointerleave`,this.handleLeave)}};function Rn(e,t,n,r,i){let{data:a,mode:o=`area`,lineWidth:s=1.5,showLastPoint:c=!1,showMinMax:l=!1}=r;if(a.length<2||t<=0||n<=0)return;let u=r.color??i.lineColor,d=r.fillColor??u,f=t-4,p=n-4,m=1/0,h=-1/0,g=0,_=0;for(let e=0;e<a.length;e++)a[e]<m&&(m=a[e],g=e),a[e]>h&&(h=a[e],_=e);let v=h-m||1,y=e=>2+e/(a.length-1)*f,b=e=>2+p-(e-m)/v*p;if(o===`area`){let t=e.createLinearGradient(0,2,0,n-2);t.addColorStop(0,d+(d.startsWith(`#`)?`40`:``)),t.addColorStop(1,`transparent`),e.beginPath(),e.moveTo(y(0),b(a[0]));for(let t=1;t<a.length;t++)e.lineTo(y(t),b(a[t]));e.lineTo(y(a.length-1),n-2),e.lineTo(y(0),n-2),e.closePath(),e.fillStyle=t,e.fill()}e.beginPath(),e.moveTo(y(0),b(a[0]));for(let t=1;t<a.length;t++)e.lineTo(y(t),b(a[t]));if(e.strokeStyle=u,e.lineWidth=s,e.lineJoin=`round`,e.stroke(),c){let t=r.lastPointColor??u,n=y(a.length-1),i=b(a[a.length-1]);e.beginPath(),e.arc(n,i,3,0,Math.PI*2),e.fillStyle=t,e.fill()}if(l){let t=r.minPointColor??i.candleDown,n=r.maxPointColor??i.candleUp;e.beginPath(),e.arc(y(g),b(m),2.5,0,Math.PI*2),e.fillStyle=t,e.fill(),e.beginPath(),e.arc(y(_),b(h),2.5,0,Math.PI*2),e.fillStyle=n,e.fill()}}var zn=50,Bn=60,J=20,Vn=.2;function Hn(e){let t=[...e].sort((e,t)=>t.price-e.price),n=[],r=0;for(let e of t)r+=e.volume,n.push({price:e.price,cumulativeVolume:r});return n}function Un(e){let t=[...e].sort((e,t)=>e.price-t.price),n=[],r=0;for(let e of t)r+=e.volume,n.push({price:e.price,cumulativeVolume:r});return n}function Wn(e,t){return{left:zn,right:e-Bn,top:J,bottom:t-J,plotWidth:e-zn-Bn,plotHeight:t-J*2}}function Gn(e,t,n,r){e.fillStyle=r,e.fillRect(0,0,t,n)}function Kn(t,n,r,i,a,o,s,c){let{left:l,right:u,top:d,bottom:f,plotWidth:p,plotHeight:m}=n;t.save(),t.strokeStyle=o.grid,t.lineWidth=.5,t.fillStyle=o.axisLabel,t.font=`${o.font.sizeSmall}px ${o.font.family}`;let h=(0,e.computeTickStep)(0,a,5);for(let e=0;e<=a;e+=h){let n=f-e/a*m;t.beginPath(),t.moveTo(l,n),t.lineTo(u,n),t.stroke(),t.textAlign=`right`,t.textBaseline=`middle`,t.fillText(c(e),l-4,n)}let g=i-r,_=(0,e.computeTickStep)(r,i,6),v=Math.ceil(r/_)*_;for(let e=v;e<=i;e+=_){let n=l+(e-r)/g*p;t.beginPath(),t.moveTo(n,d),t.lineTo(n,f),t.stroke(),t.textAlign=`center`,t.textBaseline=`top`,t.fillText(s(e),n,f+4)}t.restore()}function qn(e,t,n,r,i,a,o,s,c){if(t.length===0)return;let{left:l,bottom:u,plotWidth:d,plotHeight:f}=n,p=e=>l+(e-r)/i*d,m=e=>u-e/a*f;if(e.beginPath(),c===`bid`){e.moveTo(p(t[0].price),u);for(let n=0;n<t.length;n++){let r=p(t[n].price),i=m(t[n].cumulativeVolume);e.lineTo(r,i),n<t.length-1&&e.lineTo(p(t[n+1].price),i)}e.lineTo(p(t[t.length-1].price),u)}else{e.moveTo(p(t[0].price),u);for(let n=0;n<t.length;n++){let r=p(t[n].price),i=m(t[n].cumulativeVolume);e.lineTo(r,i),n<t.length-1&&e.lineTo(p(t[n+1].price),i)}e.lineTo(p(t[t.length-1].price),u)}if(e.closePath(),e.fillStyle=o,e.fill(),e.beginPath(),c===`bid`){e.moveTo(p(t[0].price),m(t[0].cumulativeVolume));for(let n=0;n<t.length;n++){let r=p(t[n].price),i=m(t[n].cumulativeVolume);n>0&&e.lineTo(r,m(t[n-1].cumulativeVolume)),e.lineTo(r,i)}}else{e.moveTo(p(t[0].price),m(t[0].cumulativeVolume));for(let n=1;n<t.length;n++){let r=m(t[n-1].cumulativeVolume),i=p(t[n].price),a=m(t[n].cumulativeVolume);e.lineTo(i,r),e.lineTo(i,a)}}e.strokeStyle=s,e.lineWidth=1.5,e.stroke()}function Jn(e,t,n,r,i,a,o,s,c){let{left:l,top:u,bottom:d,plotWidth:f}=r,p=l+(t-i)/a*f;if(e.save(),e.strokeStyle=o.textSecondary,e.lineWidth=1,e.setLineDash([4,4]),e.beginPath(),e.moveTo(p,u),e.lineTo(p,d),e.stroke(),e.restore(),s){let t=`Spread: ${c(n)}`;e.save(),e.font=`${o.font.sizeSmall}px ${o.font.family}`,e.fillStyle=o.textSecondary,e.textAlign=`center`,e.textBaseline=`bottom`,e.fillText(t,p,u-2),e.restore()}}function Yn(e,t,n,r,i,a,o,s,c,l,u){let{left:d,top:f,bottom:p,plotWidth:m,plotHeight:h}=i,g=t.x;if(g<d||g>i.right)return;let _=a+(g-d)/m*o,v=0,y=``;if(n.length>0&&_<=n[0].price){y=`Bid`;for(let e of n)if(_<=e.price)v=e.cumulativeVolume;else break}if(r.length>0&&_>=r[0].price){y=`Ask`;for(let e of r)if(_>=e.price)v=e.cumulativeVolume;else break}if(e.save(),e.strokeStyle=c.crosshair,e.lineWidth=.5,e.setLineDash([4,4]),e.beginPath(),e.moveTo(g,f),e.lineTo(g,p),e.stroke(),v>0){let t=p-v/s*h;e.beginPath(),e.moveTo(d,t),e.lineTo(i.right,t),e.stroke()}e.restore();let b=[`Price: ${l(_)}`,y?`${y} Vol: ${u(v)}`:``].filter(Boolean);e.save(),e.font=`${c.font.sizeSmall}px ${c.font.family}`;let x=Math.max(...b.map(t=>e.measureText(t).width))+16,S=b.length*(c.font.sizeSmall+4)+12,C=g+10,w=t.y-S/2;C+x>i.right&&(C=g-x-10),w<f&&(w=f),w+S>p&&(w=p-S),e.fillStyle=c.axisLabelBackground,e.globalAlpha=.9,e.fillRect(C,w,x,S),e.globalAlpha=1,e.fillStyle=c.text,e.textBaseline=`top`,e.textAlign=`left`;for(let t=0;t<b.length;t++)e.fillText(b[t],C+8,w+6+t*(c.font.sizeSmall+4));e.restore()}function Xn(e){return e.toFixed(2)}function Zn(e){return e>=1e6?(e/1e6).toFixed(2)+`M`:e>=1e3?(e/1e3).toFixed(1)+`K`:e.toFixed(0)}function Qn(e,t,n,r,i,a){let{data:o}=r;if(o.bids.length===0&&o.asks.length===0||t<=0||n<=0)return;let s=r.bidColor??i.candleUp,c=r.askColor??i.candleDown,l=r.bidFillColor??s+`40`,u=r.askFillColor??c+`40`,d=r.midPriceLine!==!1,f=r.spreadLabel!==!1,p=r.priceFormat??Xn,m=r.volumeFormat??Zn,h=Hn(o.bids),g=Un(o.asks),_=h.length>0?h[0].price:0,v=g.length>0?g[0].price:0,y=h.length>0&&g.length>0?(_+v)/2:_||v,b=g.length>0&&h.length>0?v-_:0,x=h.length>0?h[h.length-1].price:y,S=g.length>0?g[g.length-1].price:y,C=Math.max(y-x,S-y)*(1+Vn),w=y-C,T=y+C,E=T-w,D=h.length>0?h[h.length-1].cumulativeVolume:0,O=g.length>0?g[g.length-1].cumulativeVolume:0,k=Math.max(D,O)||1,A=Wn(t,n);Gn(e,t,n,i.background),Kn(e,A,w,T,k,i,p,m),qn(e,h,A,w,E,k,l,s,`bid`),qn(e,g,A,w,E,k,u,c,`ask`),d&&Jn(e,y,b,A,w,E,i,f,p),a&&Yn(e,a,h,g,A,w,E,k,i,p,m)}var $n=70,er=30,tr=20,nr=20,rr=.05;function ir(e,t){return{left:$n,right:e-tr,top:nr,bottom:t-er,plotWidth:e-$n-tr,plotHeight:t-nr-er}}function ar(e,t){let n=1/0,r=-1/0;for(let t of e)t.value<n&&(n=t.value),t.value>r&&(r=t.value);if(t)for(let e of t)e.value<n&&(n=e.value),e.value>r&&(r=e.value);let i=(r-n)*rr;return{valMin:n-i,valMax:r+i}}function or(e){let t=[],n=-1/0;for(let r of e)r.value>n&&(n=r.value),t.push(n);return t}function sr(e){let t=864e5;if(365.25*t,e<7*t){let e=new Intl.DateTimeFormat(void 0,{hour:`2-digit`,minute:`2-digit`});return t=>e.format(new Date(t))}if(e<315576e5){let e=new Intl.DateTimeFormat(void 0,{month:`short`,day:`numeric`});return t=>e.format(new Date(t))}let n=new Intl.DateTimeFormat(void 0,{month:`short`,year:`numeric`});return e=>n.format(new Date(e))}function cr(e){return Math.abs(e)>=1e6?(e/1e6).toFixed(2)+`M`:Math.abs(e)>=1e3?(e/1e3).toFixed(1)+`K`:e.toFixed(2)}function lr(e,t,n,r){e.fillStyle=r,e.fillRect(0,0,t,n)}function ur(t,n,r,i,a,o,s,c,l){let{left:u,right:d,top:f,bottom:p,plotWidth:m,plotHeight:h}=n;t.save(),t.strokeStyle=s.grid,t.lineWidth=.5,t.fillStyle=s.axisLabel,t.font=`${s.font.sizeSmall}px ${s.font.family}`;let g=(0,e.computeTickStep)(r,i,6),_=Math.ceil(r/g)*g;for(let e=_;e<=i;e+=g){let n=p-(e-r)/(i-r)*h;t.beginPath(),t.moveTo(u,n),t.lineTo(d,n),t.stroke(),t.textAlign=`right`,t.textBaseline=`middle`,t.fillText(c(e),u-6,n)}let v=o-a,y=(0,e.computeTickStep)(a,o,6),b=Math.ceil(a/y)*y;for(let e=b;e<=o;e+=y){let n=u+(e-a)/v*m;t.beginPath(),t.moveTo(n,f),t.lineTo(n,p),t.stroke(),t.textAlign=`center`,t.textBaseline=`top`,t.fillText(l(e),n,p+4)}t.restore()}function dr(e,t,n,r,i,a,o,s,c){if(t.length<2)return;let{left:l,bottom:u,plotWidth:d,plotHeight:f}=n,p=e=>l+(e-r)/i*d,m=e=>u-(e-a)/o*f;e.save(),e.beginPath(),e.moveTo(p(t[0].time),m(t[0].value));for(let n=1;n<t.length;n++)e.lineTo(p(t[n].time),m(t[n].value));e.strokeStyle=s,e.lineWidth=c,e.lineJoin=`round`,e.stroke(),e.restore()}function fr(e,t,n,r,i,a,o,s){if(t.length<2)return;let{left:c,bottom:l,plotWidth:u,plotHeight:d}=n,f=e=>c+(e-r)/i*u,p=e=>l-(e-a)/o*d,m=e.createLinearGradient(0,n.top,0,l);m.addColorStop(0,s+`40`),m.addColorStop(1,`transparent`),e.save(),e.beginPath(),e.moveTo(f(t[0].time),p(t[0].value));for(let n=1;n<t.length;n++)e.lineTo(f(t[n].time),p(t[n].value));e.lineTo(f(t[t.length-1].time),l),e.lineTo(f(t[0].time),l),e.closePath(),e.fillStyle=m,e.fill(),e.restore()}function pr(e,t,n,r,i,a,o,s,c){if(t.length<2)return;let{left:l,bottom:u,plotWidth:d,plotHeight:f}=r,p=e=>l+(e-i)/a*d,m=e=>u-(e-o)/s*f;e.save(),e.fillStyle=c,e.beginPath(),e.moveTo(p(t[0].time),m(n[0]));for(let r=1;r<t.length;r++)e.lineTo(p(t[r].time),m(n[r]));for(let n=t.length-1;n>=0;n--)e.lineTo(p(t[n].time),m(t[n].value));e.closePath(),e.fill(),e.restore()}function mr(e,t,n,r,i,a,o,s,c,l){if(t.length===0)return;let{left:u,bottom:d,plotWidth:f,plotHeight:p}=n,m=t[t.length-1],h=u+(m.time-r)/i*f,g=d-(m.value-a)/o*p;e.save(),e.font=`${l.font.sizeSmall}px ${l.font.family}`,e.fillStyle=c,e.textAlign=`left`,e.textBaseline=`middle`,e.fillText(s,h+4,g),e.restore()}function hr(e,t,n,r,i,a,o,s,c,l,u,d){let{left:f,top:p,bottom:m,plotWidth:h,plotHeight:g}=i,_=t.x;if(_<f||_>i.right)return;e.save(),e.strokeStyle=l.crosshair,e.lineWidth=.5,e.setLineDash([4,4]),e.beginPath(),e.moveTo(_,p),e.lineTo(_,m),e.stroke(),e.restore();let v=a+(_-f)/h*o,y=0,b=1/0;for(let e=0;e<n.length;e++){let t=Math.abs(n[e].time-v);t<b&&(b=t,y=e)}let x=n[y],S=[u(x.time),`Value: ${d(x.value)}`];r&&r.length>y&&S.push(`Benchmark: ${d(r[y].value)}`);let C=f+(x.time-a)/o*h,w=m-(x.value-s)/c*g;e.beginPath(),e.arc(C,w,3,0,Math.PI*2),e.fillStyle=l.lineColor,e.fill(),e.save(),e.font=`${l.font.sizeSmall}px ${l.font.family}`;let T=Math.max(...S.map(t=>e.measureText(t).width))+16,E=S.length*(l.font.sizeSmall+4)+12,D=_+10,O=t.y-E/2;D+T>i.right&&(D=_-T-10),O<p&&(O=p),O+E>m&&(O=m-E),e.fillStyle=l.axisLabelBackground,e.globalAlpha=.9,e.fillRect(D,O,T,E),e.globalAlpha=1,e.fillStyle=l.text,e.textBaseline=`top`,e.textAlign=`left`;for(let t=0;t<S.length;t++)e.fillText(S[t],D+8,O+6+t*(l.font.sizeSmall+4));e.restore()}function gr(e,t,n,r,i,a){let{data:o}=r;if(o.length<2||t<=0||n<=0)return;let s=r.lineColor??i.lineColor,c=r.lineWidth??1.5,l=r.drawdown===!0,u=r.drawdownColor??i.candleDown+`30`,d=r.fillArea===!0,f=r.benchmark,p=r.benchmarkColor??i.textSecondary,m=r.benchmarkLabel??`Benchmark`,h=r.valueFormat??cr,{valMin:g,valMax:_}=ar(o,f),v=_-g||1,y=o[0].time,b=o[o.length-1].time,x=b-y||1,S=r.timeFormat??sr(x),C=ir(t,n);lr(e,t,n,i.background),ur(e,C,g,_,y,b,i,h,S),l&&pr(e,o,or(o),C,y,x,g,v,u),d&&fr(e,o,C,y,x,g,v,s),dr(e,o,C,y,x,g,v,s,c),f&&f.length>=2&&(dr(e,f,C,y,x,g,v,p,1),mr(e,f,C,y,x,g,v,m,p,i)),a&&hr(e,a,o,f,C,y,x,g,v,i,S,h)}function _r(e,t,n){if(e.length===0)return[];let r=e.length,i=Math.ceil(Math.sqrt(r)),a=Math.ceil(r/i),o=t.width/i,s=t.height/a,c=[];for(let a=0;a<r;a++){let r=a%i,l=Math.floor(a/i);c.push({id:e[a].id,rect:{x:t.x+r*o+n,y:t.y+l*s+n,width:Math.max(0,o-n*2),height:Math.max(0,s-n*2)}})}return c}function vr(e,t){let n=e.reduce((e,t)=>e+t,0);if(n===0||t===0)return 1/0;let r=0;for(let i of e){let e=n/t,a=i/e,o=e>a?e/a:a/e;o>r&&(r=o)}return r}function Y(e,t,n,r,i,a){let o=r?n.height:n.width,s=e.reduce((e,t)=>e+t,0),c=o>0?s/o:0,l=0;for(let o=0;o<e.length;o++){let s=c>0?e[o]/c:0,u;u=r?{x:n.x+i,y:n.y+l+i,width:Math.max(0,c-i*2),height:Math.max(0,s-i*2)}:{x:n.x+l+i,y:n.y+i,width:Math.max(0,s-i*2),height:Math.max(0,c-i*2)},a.push({id:t[o],rect:u}),l+=s}}function X(e,t,n,r,i){if(e.length===0)return;let a=n.width>=n.height,o=a?n.height:n.width;if(e.length===1){Y(e,t,n,a,r,i);return}let s=[],c=[],l=0;for(;l<e.length;){let u=[...s,e[l]];if(s.length===0||vr(u,o)<=vr(s,o))s.push(e[l]),c.push(t[l]),l++;else{Y(s,c,n,a,r,i);let u=s.reduce((e,t)=>e+t,0),d=o>0?u/o:0;n=a?{x:n.x+d,y:n.y,width:n.width-d,height:n.height}:{x:n.x,y:n.y+d,width:n.width,height:n.height-d},X(e.slice(l),t.slice(l),n,r,i);return}}s.length>0&&Y(s,c,n,a,r,i)}function yr(e,t,n){if(e.length===0)return[];let r=[...e].sort((e,t)=>t.weight-e.weight),i=r.reduce((e,t)=>e+Math.max(0,t.weight),0);if(i===0)return[];let a=t.width*t.height,o=r.map(e=>Math.max(0,e.weight)/i*a),s=r.map(e=>e.id),c=[];return X(o,s,{...t},n,c),c}function br(e){return(e>=0?`+`:``)+e.toFixed(2)+`%`}function xr(e){return e.label}function Sr(t,n){let r=n.min??-5,i=n.max??5;if(t<=r)return n.negative;if(t>=i)return n.positive;if(t<0){let i=1-t/r;return(0,e.lerpColor)(n.negative,n.zero,i)}let a=t/i;return(0,e.lerpColor)(n.zero,n.positive,a)}function Z(e,t,n,r,i,a){let o=Math.min(a,r/2,i/2);e.beginPath(),e.moveTo(t+o,n),e.lineTo(t+r-o,n),e.arcTo(t+r,n,t+r,n+o,o),e.lineTo(t+r,n+i-o),e.arcTo(t+r,n+i,t+r-o,n+i,o),e.lineTo(t+o,n+i),e.arcTo(t,n+i,t,n+i-o,o),e.lineTo(t,n+o),e.arcTo(t,n,t+o,n,o),e.closePath()}function Cr(e,t,n,r,i,a,o,s,c,l,u){if(i<40||a<30)return;let d=n+i/2,f=r+a/2;if(e.save(),e.shadowColor=`rgba(0,0,0,0.6)`,e.shadowBlur=2,e.fillStyle=`#FFFFFF`,e.textAlign=`center`,s){let n=l(t),r=Math.min(o.font.sizeMedium,i/n.length*1.5,a/4);e.font=`bold `+Math.max(8,r)+`px `+o.font.family,e.textBaseline=c?`bottom`:`middle`,e.fillText(n,d,c?f-1:f,i-8)}if(c){let n=u(t.value),r=Math.min(o.font.sizeSmall,i/n.length*1.5,a/4);e.font=Math.max(7,r)+`px `+o.font.family,e.textBaseline=s?`top`:`middle`,e.fillText(n,d,s?f+1:f,i-8)}e.restore()}function wr(e,t,n,r,i,a,o,s){let c=[o(t),s(t.value)];if(t.meta)for(let[e,n]of Object.entries(t.meta))c.push(e+`: `+String(n));e.save(),e.font=a.font.sizeSmall+`px `+a.font.family;let l=Math.max(...c.map(t=>e.measureText(t).width))+16,u=a.font.sizeSmall+4,d=c.length*u+12,f=n.x+14,p=n.y-d/2;f+l>r&&(f=n.x-l-14),p<0&&(p=0),p+d>i&&(p=i-d),e.fillStyle=a.axisLabelBackground,e.globalAlpha=.92,Z(e,f,p,l,d,4),e.fill(),e.globalAlpha=1,e.fillStyle=a.text,e.textBaseline=`top`,e.textAlign=`left`;for(let t=0;t<c.length;t++)e.fillText(c[t],f+8,p+6+t*u);e.restore()}function Tr(e,t,n,r,i,a,o,s){if(r.length===0||i.length===0||t<=0||n<=0)return null;let c=a.colorScale??{negative:`#EF5350`,zero:`#424242`,positive:`#26A69A`},l=a.cellRadius??4,u=a.showLabels!==!1,d=a.showValues!==!1,f=a.valueFormat??br,p=a.labelFormat??xr,m=new Map;for(let e of r)m.set(e.id,e);let h=null,g=null;if(s)for(let e of i){let{x:t,y:n,width:r,height:i}=e.rect;if(s.x>=t&&s.x<=t+r&&s.y>=n&&s.y<=n+i){h=e.id;let t=m.get(e.id);t&&(g={cell:t,rect:e});break}}e.fillStyle=o.background,e.fillRect(0,0,t,n);for(let t of i){let n=m.get(t.id);if(!n)continue;let{x:r,y:i,width:a,height:s}=t.rect;if(a<=0||s<=0)continue;let g=Sr(n.value,c);Z(e,r,i,a,s,l),e.fillStyle=g,e.fill(),Cr(e,n,r,i,a,s,o,u,d,p,f),t.id===h&&(Z(e,r,i,a,s,l),e.strokeStyle=`#FFFFFF`,e.lineWidth=1.5,e.stroke())}return g&&s&&wr(e,g.cell,s,t,n,o,p,f),g}var Er=60,Dr=12,Or=24,kr=40,Ar=.7,jr=1;function Mr(e){return e.type?e.type:e.value>0?`positive`:e.value<0?`negative`:`subtotal`}function Nr(e){let t=Math.abs(e);return t>=1e6?(e/1e6).toFixed(2)+`M`:t>=1e3?(e/1e3).toFixed(1)+`K`:e.toFixed(2)}function Pr(e,t){return{left:Er,right:e-Dr,top:Or,bottom:t-kr,plotWidth:e-Er-Dr,plotHeight:t-Or-kr}}function Fr(e,t,n,r,i){let a=Array(e.length),o=Array(e.length),s=t.plotWidth/e.length,c=s*n,l=(s-c)/2,{left:u,bottom:d,plotHeight:f}=t,p=0;for(let t=0;t<e.length;t++){let n=e[t],m=Mr(n),h,g;m===`total`||m===`subtotal`?(h=0,g=n.value,p=n.value):(h=p,g=p+n.value,p=g);let _=Math.min(h,g),v=Math.max(h,g),y=d-(_-r)/i*f,b=d-(v-r)/i*f,x=Math.min(y,b),S=Math.max(Math.abs(b-y),jr),C=u+t*s,w=C+l;a[t]={label:n.label,value:n.value,start:h,end:g,type:m,rectX:w,rectY:x,rectW:c,rectH:S,slotX:C,slotW:s},o[t]=p}return{bars:a,cumulatives:o}}function Ir(e){if(e.length===0)return{valMin:0,valMax:1};let t=0,n=0,r=0;for(let i=0;i<e.length;i++){let a=e[i],o=Mr(a),s,c;o===`total`||o===`subtotal`?(s=0,c=a.value,r=a.value):(s=r,c=r+a.value,r=c),s<t&&(t=s),c<t&&(t=c),s>n&&(n=s),c>n&&(n=c)}let i=(n-t||1)*.1;return{valMin:t-i,valMax:n+i}}function Lr(e,t,n,r){e.fillStyle=r,e.fillRect(0,0,t,n)}function Rr(t,n,r,i,a,o){let{left:s,right:c,top:l,bottom:u,plotHeight:d}=n,f=i-r||1;t.strokeStyle=a.grid,t.lineWidth=.5,t.fillStyle=a.axisLabel,t.font=`${a.font.sizeSmall}px ${a.font.family}`,t.textAlign=`right`,t.textBaseline=`middle`;let p=(0,e.computeTickStep)(r,i,5),m=Math.ceil(r/p)*p;t.beginPath();for(let e=m;e<=i;e+=p){let n=Math.round(u-(e-r)/f*d)+.5;n<l||n>u||(t.moveTo(s,n),t.lineTo(c,n))}t.stroke();for(let e=m;e<=i;e+=p){let n=u-(e-r)/f*d;n<l-1||n>u+1||t.fillText(o(e),s-6,n)}if(r<0&&i>0){let e=Math.round(u-(0-r)/f*d)+.5;t.strokeStyle=a.axisLine,t.lineWidth=1,t.beginPath(),t.moveTo(s,e),t.lineTo(c,e),t.stroke()}}function zr(e,t,n){let r=[`positive`,`negative`,`total`,`subtotal`];for(let i=0;i<r.length;i++){let a=r[i],o=n[a],s=!1;e.beginPath();for(let n=0;n<t.length;n++){let r=t[n];if(r.type!==a)continue;let i=Math.round(r.rectX),o=Math.round(r.rectY),c=Math.round(r.rectW),l=Math.round(r.rectH);e.rect(i,o,c,l),s=!0}s&&(e.fillStyle=o,e.fill())}}function Br(e,t,n,r,i,a,o){if(o===`none`||t.length<2)return;let{bottom:s,plotHeight:c}=n;e.strokeStyle=a,e.lineWidth=1,o===`dashed`?e.setLineDash([4,4]):e.setLineDash([]),e.beginPath();for(let n=0;n<t.length-1;n++){let a=t[n],o=t[n+1],l=a.end,u=Math.round(s-(l-r)/i*c)+.5,d=Math.round(a.rectX+a.rectW),f=Math.round(o.rectX);f<=d||(e.moveTo(d,u),e.lineTo(f,u))}e.stroke(),e.setLineDash([])}function Vr(e,t,n,r,i,a){e.fillStyle=r.text,e.font=`${r.font.sizeSmall}px ${r.font.family}`,e.textAlign=`center`;for(let r=0;r<t.length;r++){let o=t[r],s;s=o.type===`total`||o.type===`subtotal`?i(o.value):a?`${o.value>=0?`+`:``}${i(o.value)}`:i(n[r]);let c=o.rectX+o.rectW/2;o.end<o.start?(e.textBaseline=`top`,e.fillText(s,c,o.rectY+o.rectH+4)):(e.textBaseline=`bottom`,e.fillText(s,c,o.rectY-4))}}function Hr(e,t,n,r,i){e.fillStyle=r.axisLabel,e.font=`${r.font.sizeSmall}px ${r.font.family}`,e.textAlign=`center`,e.textBaseline=`top`;let a=n.bottom+6,o=t.length>0?t[0].slotW-4:0,s=!1;for(let n=0;n<t.length;n++)if(e.measureText(i(t[n].label)).width>o){s=!0;break}if(!s){for(let n=0;n<t.length;n++){let r=t[n],o=r.slotX+r.slotW/2;e.fillText(i(r.label),o,a)}return}e.save(),e.textAlign=`right`,e.textBaseline=`middle`;for(let n=0;n<t.length;n++){let r=t[n],o=r.slotX+r.slotW/2;e.setTransform(1,0,0,1,o,a+4),e.rotate(-Math.PI/4),e.fillText(i(r.label),0,0)}e.setTransform(1,0,0,1,0,0),e.restore()}function Ur(e,t,n){if(t<n.left||t>n.right)return null;for(let n=0;n<e.length;n++){let r=e[n];if(t>=r.slotX&&t<=r.slotX+r.slotW)return n}return null}function Wr(e,t,n,r,i,a,o,s){let c=t[r],l=n[r],u=Math.round(c.slotX+c.slotW/2)+.5;e.strokeStyle=o.crosshair,e.lineWidth=.5,e.setLineDash([4,4]),e.beginPath(),e.moveTo(u,a.top),e.lineTo(u,a.bottom),e.stroke(),e.setLineDash([]);let d=[c.label,`Value: ${c.value>=0&&(c.type===`positive`||c.type===`negative`)?`+`:``}${s(c.value)}`,`Total: ${s(l)}`];e.font=`${o.font.sizeSmall}px ${o.font.family}`;let f=0;for(let t=0;t<d.length;t++){let n=e.measureText(d[t]).width;n>f&&(f=n)}let p=f+16,m=d.length*(o.font.sizeSmall+4)+12,h=i.x+10,g=i.y-m/2;h+p>a.right&&(h=i.x-p-10),g<a.top&&(g=a.top),g+m>a.bottom&&(g=a.bottom-m),e.fillStyle=o.axisLabelBackground,e.globalAlpha=.9,e.fillRect(h,g,p,m),e.globalAlpha=1,e.fillStyle=o.text,e.textAlign=`left`,e.textBaseline=`top`;let _=o.font.sizeSmall+4;for(let t=0;t<d.length;t++)e.fillText(d[t],h+8,g+6+t*_)}function Gr(e,t,n,r,i,a){let{data:o}=r;if(!o||o.length===0||t<=0||n<=0)return{hitBar:null};let s=r.positiveColor??i.candleUp,c=r.negativeColor??i.candleDown,l=r.totalColor??i.lineColor,u=r.subtotalColor??i.textSecondary,d=r.connectorColor??i.textSecondary,f=r.connectorStyle??`dashed`,p=r.barWidth??Ar,m=r.showValues!==!1,h=r.showDelta!==!1,g=r.valueFormat??Nr,_=r.labelFormat??(e=>e),{valMin:v,valMax:y}=Ir(o),b=y-v||1,x=Pr(t,n),{bars:S,cumulatives:C}=Fr(o,x,p,v,b);Lr(e,t,n,i.background),Rr(e,x,v,y,i,g),Br(e,S,x,v,b,d,f),zr(e,S,{positive:s,negative:c,total:l,subtotal:u}),m&&Vr(e,S,C,i,g,h),Hr(e,S,x,i,_);let w=a?Ur(S,a.x,x):null;return a&&w!==null&&Wr(e,S,C,w,a,x,i,g),{hitBar:w}}var Kr=Math.PI/180,qr=135,Jr=405,Yr=.25;function Xr(e){let t=Math.abs(e);return t>=1e6?(e/1e6).toFixed(2)+`M`:t>=1e3?(e/1e3).toFixed(1)+`K`:Number.isInteger(e)?e.toFixed(0):e.toFixed(1)}function Q(e,t,n){return e<t?t:e>n?n:e}function Zr(e,t,n){let r=n.min??0,i=n.max??100,a=i-r||1,o=(n.startAngle??qr)*Kr,s=(n.endAngle??Jr)*Kr,c=n.thickness??Yr,l=e/2,u=t/2+t*.05,d=Math.max(10,Math.min(e,t)/2-24);return{cx:l,cy:u,radius:d,innerRadius:Math.max(2,d*(1-c)),startAngle:o,endAngle:s,angleRange:s-o,min:r,max:i,range:a}}function $(e,t){let n=Q((e-t.min)/t.range,0,1);return t.startAngle+t.angleRange*n}function Qr(e,t,n,r){e.fillStyle=r,e.fillRect(0,0,t,n)}function $r(e,t,n){e.beginPath(),e.arc(t.cx,t.cy,t.radius,t.startAngle,t.endAngle,!1),e.arc(t.cx,t.cy,t.innerRadius,t.endAngle,t.startAngle,!0),e.closePath(),e.fillStyle=n,e.fill()}function ei(e,t,n){let r=Q(n.from,t.min,t.max),i=Q(n.to,t.min,t.max);if(i<=r)return;let a=$(r,t),o=$(i,t);e.beginPath(),e.arc(t.cx,t.cy,t.radius,a,o,!1),e.arc(t.cx,t.cy,t.innerRadius,o,a,!0),e.closePath(),e.fillStyle=n.color,e.fill()}function ti(e,t,n,r,i){let a=$(n,t);a<=t.startAngle||(e.globalAlpha=i,e.beginPath(),e.arc(t.cx,t.cy,t.radius,t.startAngle,a,!1),e.arc(t.cx,t.cy,t.innerRadius,a,t.startAngle,!0),e.closePath(),e.fillStyle=r,e.fill(),e.globalAlpha=1)}function ni(e,t,n,r){let i=$(n,t),a=Math.cos(i),o=Math.sin(i),s=t.radius-2,c=t.innerRadius*.25,l=-o,u=a,d=t.cx+a*s,f=t.cy+o*s,p=t.cx+l*4-a*c,m=t.cy+u*4-o*c,h=t.cx-l*4-a*c,g=t.cy-u*4-o*c;e.beginPath(),e.moveTo(d,f),e.lineTo(p,m),e.lineTo(h,g),e.closePath(),e.fillStyle=r,e.fill(),e.beginPath(),e.arc(t.cx,t.cy,Math.max(4,t.innerRadius*.12),0,Math.PI*2),e.fill()}function ri(e,t,n,r,i){let a=Math.round(t.innerRadius*.55),o=Math.round(i.font.sizeMedium);e.fillStyle=i.text,e.textAlign=`center`,e.textBaseline=`middle`,e.font=`600 ${a}px ${i.font.family}`;let s=!!r,c=s?t.cy-o*.6:t.cy-a*.1;e.fillText(n,t.cx,c),s&&(e.fillStyle=i.textSecondary,e.font=`${o}px ${i.font.family}`,e.fillText(r,t.cx,t.cy+a*.55))}function ii(e,t,n,r){e.fillStyle=r.textSecondary,e.font=`${r.font.sizeSmall}px ${r.font.family}`;let i=t.radius+10,a=Math.cos(t.startAngle),o=Math.sin(t.startAngle),s=Math.cos(t.endAngle),c=Math.sin(t.endAngle),l=t.cx+a*i,u=t.cy+o*i,d=t.cx+s*i,f=t.cy+c*i;e.textBaseline=`middle`,e.textAlign=a<-.2?`right`:a>.2?`left`:`center`,e.fillText(n(t.min),l,u),e.textAlign=s<-.2?`right`:s>.2?`left`:`center`,e.fillText(n(t.max),d,f)}function ai(e,t,n,r){e.fillStyle=r.textSecondary,e.font=`${r.font.sizeSmall}px ${r.font.family}`,e.textAlign=`center`,e.textBaseline=`middle`;let i=(t.radius+t.innerRadius)/2;for(let r=0;r<n.length;r++){let a=n[r];if(!a.label)continue;let o=$((Q(a.from,t.min,t.max)+Q(a.to,t.min,t.max))/2,t),s=t.cx+Math.cos(o)*i,c=t.cy+Math.sin(o)*i;e.fillText(a.label,s,c)}}function oi(e,t,n,r,i,a){if(t<=0||n<=0)return;let o=Zr(t,n,r),s=r.trackColor??i.grid,c=r.needleColor??i.text,l=r.showValue!==!1,u=r.showZoneLabels===!0,d=r.valueFormat??Xr,f=Q(a,o.min,o.max);Qr(e,t,n,i.background),$r(e,o,s);let p=r.zones;if(p&&p.length>0)for(let t=0;t<p.length;t++)ei(e,o,p[t]);ti(e,o,f,c,.12),ni(e,o,f,c),l&&ri(e,o,d(f),r.label,i),ii(e,o,d,i),u&&p&&p.length>0&&ai(e,o,p,i)}exports.ADXIndicator=F,exports.ATRIndicator=P,exports.AccumulationDistributionIndicator=Ue,exports.AlertManager=vn,exports.AnchoredVWAPIndicator=Se,exports.AnchoredVWAPTool=Lt,exports.Animator=Pn,exports.AreaRenderer=v,exports.AroonIndicator=Le,exports.ArrowTool=Nt,exports.AwesomeOscillatorIndicator=Oe,exports.BarCountdown=hn,exports.BarRenderer=y,exports.BaseFinanceChart=In,exports.BaselineRenderer=x,exports.BinanceAdapter=ln,exports.BollingerBandsIndicator=pe,exports.CCIIndicator=Fe,exports.CMFIndicator=Be,exports.CandlestickRenderer=g,exports.CanvasLayer=t,exports.ChaikinOscillatorIndicator=ke,exports.ChartLegend=fn,exports.ChartStateManager=En,exports.CompareRenderer=te,exports.CrosshairHandler=rt,exports.CrosshairTooltip=at,exports.CurrentPriceLine=tn,exports.DEFAULT_LEGEND_CONFIG=dn,exports.DPRManager=i,exports.DataExporter=On,exports.DateRangeTool=kt,exports.DepthOverlay=Yt,exports.DonchianChannelIndicator=ye,exports.DrawingBase=L,exports.DrawingManager=ut,exports.DrawingRenderer=dt,exports.EMAIndicator=fe,exports.Easing=Nn,exports.ElliottWaveTool=Dt,exports.EllipseTool=Ct,exports.Emitter=V,exports.EquivolumeRenderer=E,exports.EventBus=ct,exports.ExtendedLineTool=gt,exports.FibExtensionTool=xt,exports.FibRetracementTool=yt,exports.FibTimeZonesTool=Bt,exports.FinanceCrosshair=Ln,exports.GannBoxTool=It,exports.GannFanTool=Ft,exports.GridRenderer=Je,exports.HLCAreaRenderer=O,exports.HollowCandleRenderer=b,exports.HorizontalLineTool=pt,exports.HullMAIndicator=Te,exports.IchimokuIndicator=he,exports.IndicatorBase=j,exports.IndicatorEngine=ue,exports.IndicatorWorkerHost=qe,exports.InteractionManager=et,exports.KagiRenderer=C,exports.KeltnerChannelIndicator=ve,exports.KeyboardHandler=it,exports.LayerManager=n,exports.LineRenderer=_,exports.LineWithMarkersRenderer=A,exports.LinearRegressionChannelIndicator=we,exports.MACDIndicator=je,exports.MFIIndicator=Ie,exports.MeasureTool=At,exports.MockAdapter=un,exports.OBVIndicator=Ne,exports.OrderRenderer=Ht,exports.PanHandler=tt,exports.ParabolicSARIndicator=ge,exports.ParallelChannelTool=_t,exports.PitchforkTool=Tt,exports.PivotPointsIndicator=xe,exports.PointAndFigureRenderer=w,exports.PositionRenderer=Jt,exports.PriceAxis=Ye,exports.PriceRangeTool=Ot,exports.ROCIndicator=Re,exports.RSIIndicator=Ae,exports.RayTool=ht,exports.ReconnectManager=H,exports.RectangleTool=St,exports.RegressionChannelTool=vt,exports.RenderEngine=p,exports.RenderLoop=r,exports.RenkoRenderer=S,exports.ReplayController=rn,exports.ReplayManager=yn,exports.SMAIndicator=de,exports.Screenshot=pn,exports.SessionBreaks=gn,exports.SignalMarkerManager=An,exports.StdDevIndicator=Ve,exports.StepLineRenderer=k,exports.StochasticIndicator=Me,exports.StreamManager=nn,exports.SupertrendIndicator=_e,exports.TSIIndicator=ze,exports.TextAnnotationTool=Mt,exports.TickAggregator=en,exports.TimeAxis=$e,exports.TradeZoneManager=Mn,exports.TradingContextMenu=Zt,exports.TradingDragHandler=Xt,exports.TradingManager=Qt,exports.TradingRenderer=$t,exports.TrendLineTool=ft,exports.TriangleTool=wt,exports.UndoRedoManager=Dn,exports.VROCIndicator=We,exports.VWAPIndicator=me,exports.VerticalLineTool=mt,exports.Viewport=m,exports.VolumeCandleRenderer=T,exports.VolumeProfileIndicator=He,exports.VolumeProfileRangeTool=Rt,exports.VolumeRenderer=ee,exports.Watermark=mn,exports.WilliamsRIndicator=Pe,exports.ZigZagIndicator=Ce,exports.ZoomHandler=nt,exports.barIndexToX=a,exports.getIntParam=N,exports.getNumberParam=M,exports.getVisibleData=h,exports.isWorkerResponse=Ke,exports.layoutSquarifiedTreemap=yr,exports.layoutUniformGrid=_r,exports.priceToY=o,exports.registerBuiltInDrawingTools=Vt,exports.registerBuiltInIndicators=Ge,exports.renderDepthChart=Qn,exports.renderEquityCurve=gr,exports.renderGauge=oi,exports.renderHeatmap=Tr,exports.renderSparkline=Rn,exports.renderWaterfall=Gr,exports.resolveBarIndex=u,exports.timeToX=d,exports.timestampToBarIndex=l,exports.toHeikinAshi=ne,exports.toKagi=oe,exports.toLineBreak=ae,exports.toPointAndFigure=se,exports.toRangeBars=ce,exports.toRenko=re,exports.xToBarIndex=s,exports.xToTime=f,exports.yToPrice=c;
21
+ //# sourceMappingURL=index.cjs.map