UNPKG

openalgo-charts

Version:

From-scratch, dependency-free canvas charting engine for OpenAlgo: professional-grade interactive rendering, advanced on-chart trading, and a one-call widget tier that adds the toolbar, drawing rail, dialogs and shortcuts.

3 lines • 615 kB
const e="2.6.0";function t(){return e}const i={None:0,Cursor:1,Light:2,Full:3};class s{constructor(e=i.None){Object.defineProperty(this,"_globalLevel",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_panes",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_timeScaleOps",{enumerable:!0,configurable:!0,writable:!0,value:[]}),this._globalLevel=e}get globalLevel(){return this._globalLevel}invalidateGlobal(e){e>this._globalLevel&&(this._globalLevel=e)}invalidatePane(e,t){var i,s;this._panes.set(e,(i=this._panes.get(e),s=t,void 0===i?{...s}:{level:Math.max(i.level,s.level),autoScale:i.autoScale||s.autoScale}))}paneInvalidation(e){return this._panes.get(e)}panes(){return this._panes}addTimeScaleOp(e){this._timeScaleOps.push(e)}timeScaleOps(){return this._timeScaleOps}isEmpty(){return this._globalLevel===i.None&&0===this._panes.size&&0===this._timeScaleOps.length}merge(e){this.invalidateGlobal(e._globalLevel);for(const[t,i]of e._panes)this.invalidatePane(t,i);e._timeScaleOps.length>0&&(this._timeScaleOps=this._timeScaleOps.concat(e._timeScaleOps))}}const r=()=>{};class n{constructor(e,t,i=r){Object.defineProperty(this,"_onFrame",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_schedule",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_cancel",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_handle",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._onFrame=e,this._schedule=t,this._cancel=i}get scheduled(){return null!==this._handle}requestFrame(){if(null!==this._handle)return;this._handle=-1;const e=this._schedule(()=>{this._handle=null,this._onFrame()});null!==this._handle&&(this._handle=e)}stop(){null!==this._handle&&(this._cancel(this._handle),this._handle=null)}}function o(e,t,i){return e<t?t:e>i?i:e}function a(e,t,i){return e+(t-e)*i}function l(e,t){return t<=0?e:Math.round(e/t)*t}function h(e,t,i){return{width:Math.round(e*i),height:Math.round(t*i)}}function c(e,t){return Math.round(e*t)/t}class d{constructor(e,t){Object.defineProperty(this,"element",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"ctx",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_mediaWidth",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_mediaHeight",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_dpr",{enumerable:!0,configurable:!0,writable:!0,value:1}),Object.defineProperty(this,"_deviceWidth",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_deviceHeight",{enumerable:!0,configurable:!0,writable:!0,value:0}),this.element=e.createElement("canvas");const i=this.element.getContext("2d");if(null===i)throw new Error("openalgo-charts: 2D canvas context is not available");this.ctx=i;const s=this.element.style;s.position="absolute",s.top="0",s.left="0",s.width="100%",s.height="100%",s.zIndex=String(t)}get mediaWidth(){return this._mediaWidth}get mediaHeight(){return this._mediaHeight}get pixelRatio(){return this._dpr}resize(e,t,i){if(e===this._mediaWidth&&t===this._mediaHeight&&i===this._dpr)return;this._mediaWidth=e,this._mediaHeight=t,this._dpr=i;const s=h(e,t,i);this.element.width=this._snapsTo(this._deviceWidth,e*i)??s.width,this.element.height=this._snapsTo(this._deviceHeight,t*i)??s.height,this.element.style.width=`${e}px`,this.element.style.height=`${t}px`}setDeviceSize(e,t){const i=this._reported(e,this._mediaWidth*this._dpr),s=this._reported(t,this._mediaHeight*this._dpr);return null===i||null===s?(this.forgetDeviceSize(),!1):(this._deviceWidth=i,this._deviceHeight=s,(this.element.width!==i||this.element.height!==s)&&(this.element.width=i,this.element.height=s,!0))}forgetDeviceSize(){this._deviceWidth=0,this._deviceHeight=0}_reported(e,t){return Number.isInteger(e)&&e>0&&Math.abs(e-t)<=1?e:null}_snapsTo(e,t){return e>0&&Math.abs(e-t)<1?e:null}clearBitmap(){this.ctx.setTransform(1,0,0,1,0,0),this.ctx.clearRect(0,0,this.element.width,this.element.height)}}function u(e){const t=Math.floor(Math.log10(e)),i=e/Math.pow(10,t);for(const e of[1,2,2.5,5])if(e>i*(1+1e-9))return e*Math.pow(10,t);return 10*Math.pow(10,t)}function _(e,t,i){const s=Math.ceil(e/i)*i,r=Math.floor(t/i)*i;return Math.round((r-s)/i)+1}function p(e,t,i=6){if(!isFinite(e)||!isFinite(t)||t<=e)return[Number.isFinite(e)?e:0];let s=function(e){if(e<=0)return 0;const t=Math.floor(Math.log10(e)),i=e/Math.pow(10,t);let s;return s=i<1.5?1:i<3?2:i<7?5:10,s*Math.pow(10,t)}((t-e)/Math.max(1,i-1));if(!(s>0))return[e];for(let r=0;r<20&&_(e,t,s)>i;r++)s=u(s);const r=Math.ceil(e/s)*s,n=Math.floor(t/s)*s,o=[],a=Math.round((n-r)/s)+1;for(let e=0;e<a&&e<1e3;e++){const t=r+e*s;o.push(Math.abs(t)<s/1e6?0:t)}return o}function f(e){return e<=0||!isFinite(e)?2:e>=1?0:Math.min(8,Math.ceil(-Math.log10(e)))}const m=["linear","logarithmic","percentage","indexed-to-100"];function b(e){return"percentage"===e||"indexed-to-100"===e}const g={marginTop:.1,marginBottom:.1,minMove:0,minPrecision:0,mode:"linear",inverted:!1};function v(e,t,i,s){if(!isFinite(e)||!isFinite(t))return{min:0,max:1};if(t<=e){const i=Math.abs(t)>0?.05*Math.abs(t):.5;return{min:e-i,max:t+i}}const r=(t-e)/Math.max(1-i-s,.01);return{min:e-r*s,max:t+r*i}}class y{constructor(e={}){Object.defineProperty(this,"_options",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_height",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_min",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_max",{enumerable:!0,configurable:!0,writable:!0,value:1}),Object.defineProperty(this,"_autoScale",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"_scaled",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_baseline",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_fixedRange",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_fixedRangeOwner",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_manualRangeOverride",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_priceFormatter",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._options={...g,...e}}get options(){return this._options}setOptions(e){this._options={...this._options,...e},b(this._options.mode)||(this._baseline=null)}setHeight(e){this._height=e}get height(){return this._height}setPriceRange(e){this._manualRangeOverride=!0,this._writePriceRange(e)}setComputedRange(e){this._writePriceRange(e)}_writePriceRange(e){this._min=e.min,this._max=e.max,this._scaled=!0}get scaled(){return this._scaled}setBaseline(e){this._baseline=e}get baseline(){return this._baseline}priceRange(){return{min:this._min,max:this._max}}get autoScale(){return this._autoScale}setAutoScale(e){this._manualRangeOverride=!e,e&&null!==this._fixedRange?this._writePriceRange(this._fixedRange):this._autoScale=e}setFixedRange(e){this._fixedRangeOwner=null,this._fixedRange=null===e?null:{...e},null!==e&&(this._manualRangeOverride=!0,this._autoScale=!1,this._writePriceRange(e))}setOwnedFixedRange(e,t){return this._fixedRangeOwner===e&&this._manualRangeOverride?(this._fixedRange={...t},!0):!(this._manualRangeOverride||!(this._fixedRangeOwner===e||null===this._fixedRange&&this._autoScale))&&(this._fixedRangeOwner=e,this._fixedRange={...t},this._autoScale=!1,this._writePriceRange(t),!0)}clearOwnedFixedRange(e){return!!this.ownsFixedRange(e)&&(this._fixedRangeOwner=null,this._fixedRange=null,this._manualRangeOverride||(this._autoScale=!0),!0)}ownsFixedRange(e){return this._fixedRangeOwner===e&&null!==this._fixedRange}ownedFixedRangeState(e){return this.ownsFixedRange(e)?{manual:this._manualRangeOverride}:null}hasConfiguration(){return this._manualRangeOverride||null!==this._fixedRange||null!==this._priceFormatter||Object.keys(g).some(e=>this._options[e]!==g[e])}get fixedRange(){return null===this._fixedRange?null:{...this._fixedRange}}reset(){this._autoScale&&(this._min=0,this._max=1,this._scaled=!1,this._baseline=null)}scaleAroundCenter(e){this._manualRangeOverride=!0;const t=(this._min+this._max)/2,i=(this._max-this._min)/2*e;this._min=t-i,this._max=t+i,this._autoScale=!1}scaleAtY(e,t){if(!this._scaled||this._height<=0||!Number.isFinite(t)||t<=0)return;const i=this._t(this._min),s=this._t(this._max),r=this._t(this.yToPrice(e)),n=this._tInv(r+(i-r)*t),o=this._tInv(r+(s-r)*t);Number.isFinite(n)&&Number.isFinite(o)&&o>n&&(this.setPriceRange({min:n,max:o}),this._autoScale=!1)}panByPixels(e){if(this._height<=0||0===e)return;const t=this._t(this._min),i=this._t(this._max),s=i-t;if(!(s>0))return;const r=s*((this._options.inverted?-e:e)/this._height);this._min=this._tInv(t+r),this._max=this._tInv(i+r),this._manualRangeOverride=!0,this._autoScale=!1}autoscale(e,t,i=1){if(null!==this._fixedRange)return this._writePriceRange(this._fixedRange),!1;const s=v(e,t,this._options.marginTop,this._options.marginBottom),r=this._t(this._min),n=this._t(this._max),a=this._t(s.min),l=this._t(s.max),h=n-r,c=l-a,d=Math.max(Math.abs((a-r)/h),Math.abs((l-n)/h))*this._height;if(!this._scaled||i>=1||!Number.isFinite(i)||!(h>0)||!(c>0)||d<.1)return this._writePriceRange(s),!1;const u=o(i,0,1),_=(1-u)/h+u/c,p=((1-u)*r/h+u*a/c)/_;return this._writePriceRange({min:this._tInv(p),max:this._tInv(p+1/_)}),!0}_rebase(){const e=this._options.mode;if("percentage"!==e&&"indexed-to-100"!==e)return null;const t=this._baseline;return null!==t&&isFinite(t)&&t>0?t:null}_t(e){if("logarithmic"===this._options.mode)return Math.log10(Math.max(1e-10,e));const t=this._rebase();if(null===t)return e;const i=e/t*100;return"percentage"===this._options.mode?i-100:i}_tInv(e){if("logarithmic"===this._options.mode)return Math.pow(10,e);const t=this._rebase();return null===t?e:("percentage"===this._options.mode?e+100:e)*t/100}priceToY(e){const t=this._t(this._min),i=this._t(this._max)-t;if(i<=0)return this._height/2;const s=(this._t(e)-t)/i;return this._options.inverted?this._height*s:this._height*(1-s)}yToPrice(e){const t=this._t(this._min),i=this._t(this._max)-t,s=this._options.inverted?e/this._height:1-e/this._height;return this._tInv(t+s*i)}precision(){if(null!==this._rebase())return Math.max(2,f((this._t(this._max)-this._t(this._min))/100));if(this._options.minMove>0)return f(this._options.minMove);const e=f((this._max-this._min)/100),t=Math.max(Math.abs(this._min),Math.abs(this._max));return!Number.isFinite(t)||t>=1e4?e:Math.max(this._options.minPrecision,e)}ticks(e=6){return null===this._rebase()?p(this._min,this._max,e):p(this._t(this._min),this._t(this._max),e).map(e=>this._tInv(e))}snapToTick(e){const t=this._options.minMove;return t<=0?e:Math.round(e/t)*t}setPriceFormatter(e){this._priceFormatter=e}format(e){if(null!==this._rebase()){const t=this.precision(),i=this._t(e);if("indexed-to-100"===this._options.mode)return i.toFixed(t);const s=Number(i.toFixed(t));return`${s>0?"+":""}${(0===s?0:i).toFixed(t)}%`}return null!==this._priceFormatter?this._priceFormatter(e):e.toFixed(this.precision())}clampY(e){return o(e,0,this._height)}}const w={barSpacing:8,minBarSpacing:1,maxBarSpacing:80,rightOffset:4};class x{constructor(e={}){Object.defineProperty(this,"_barSpacing",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_rightOffset",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_minBarSpacing",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_maxBarSpacing",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_width",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_baseIndex",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_onChange",{enumerable:!0,configurable:!0,writable:!0,value:null});const t={...w,...e};this._barSpacing=t.barSpacing,this._rightOffset=t.rightOffset,this._minBarSpacing=t.minBarSpacing,this._maxBarSpacing=t.maxBarSpacing}setWidth(e){this._width=e}get width(){return this._width}get barSpacing(){return this._barSpacing}setBarSpacing(e){const t=this.visibleRange();this._barSpacing=this.constrainBarSpacing(e),this._notifyChange(t)}constrainBarSpacing(e){return o(e,this._minBarSpacing,this._maxBarSpacing)}get rightOffset(){return this._rightOffset}setRightOffset(e){const t=this.visibleRange();this._rightOffset=e,this._notifyChange(t)}setBaseIndex(e){this._baseIndex=e}_rightEdgeIndex(){return this._baseIndex+this._rightOffset}indexToX(e){return this._width-(this._rightEdgeIndex()-e)*this._barSpacing}xToIndex(e){return this._rightEdgeIndex()-(this._width-e)/this._barSpacing}visibleRange(){const e=this._rightEdgeIndex();return{from:e-this._width/this._barSpacing,to:e}}getVisibleLogicalRange(){return this.visibleRange()}setVisibleLogicalRange(e){if(this._width<=0)return;const t=e.to-e.from;if(!(t>0))return;const i=this.visibleRange();this._barSpacing=this.constrainBarSpacing(this._width/t),this._rightOffset=e.to-this._baseIndex,this._onChange?.(i)}setChangeHandler(e){this._onChange=e}_notifyChange(e){const t=this.visibleRange();e.from===t.from&&e.to===t.to||this._onChange?.(e)}scrollByPixels(e){this.setRightOffset(this._rightOffset-e/this._barSpacing)}zoomAtX(e,t){const i=this.visibleRange(),s=this.xToIndex(e),r=this._barSpacing;if(this._barSpacing=this.constrainBarSpacing(r*t),this._barSpacing===r)return;const n=s+(this._width-e)/this._barSpacing;this._rightOffset=n-this._baseIndex,this._notifyChange(i)}fitContent(e){if(e<=0||this._width<=0)return;const t=this.visibleRange();this._baseIndex=e-1,this._rightOffset=w.rightOffset;const i=this._width/(e+this._rightOffset);this._barSpacing=this.constrainBarSpacing(i),this._notifyChange(t)}}const S=86400;function P(e){return e.sort((e,t)=>e-t),e[e.length-1>>1]}function O(e,t,i){let s=0;for(;s<i;){const r=s+i+1>>1;e[r]<=t?s=r:i=r-1}return s}function k(e){const t=e.slots,i=t.length-1;let s=NaN;if(i<4096)try{s=e.next()}catch{s=null}null!==s&&s>t[i]?t.push(s):(e.next=null,null!==s&&Number.isNaN(s)&&i>0&&(e.step=(t[i]-t[0])/i))}function I(e,t){const i=e.slots;for(;i.length<=t&&e.next;)k(e);const s=i.length-1;return t<=s?i[t]:i[s]+(t-s)*e.step}const M=[];function T(e,t,i,s,r){const n=Math.max(0,Math.floor(i)),o=Math.min(e.baseIndex,Math.ceil(s));if(o<n||0===t.length)return!1;const a=e.indexToTime(n),l=e.indexToTime(o);if(void 0===a||void 0===l)return!1;let h=0,c=t.length;for(;h<c;){const e=h+c>>1;t[e].time<a?h=e+1:c=e}return r.start=h,r.lastTime=l,!0}const C={start:0,lastTime:0};function j(e,t,i){const s=e.seriesBars(t);if(T(e,s,i,i,C))for(let t=C.start;t<s.length;t++){const i=s[t];if(i.time>C.lastTime)return;if(void 0!==e.timeToIndex(i.time))return i}}class A{constructor(){Object.defineProperty(this,"_series",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_sortedTimes",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_indexByTime",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_timeRefs",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_nextId",{enumerable:!0,configurable:!0,writable:!0,value:1}),Object.defineProperty(this,"_version",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_calendar",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_future",{enumerable:!0,configurable:!0,writable:!0,value:null})}setSessionCalendar(e){this._calendar=e,this._future=null}get sessionCalendar(){return this._calendar}createSeries(){const e=this._nextId++;return this._series.set(e,{bars:[]}),e}removeSeries(e){const t=this._series.get(e);void 0!==t&&(this._replaceBars(t,[]),this._series.delete(e))}setSeriesData(e,t){const i=this._series.get(e);if(void 0===i)throw new Error(`openalgo-charts: unknown series ${e}`);this._replaceBars(i,function(e){let t=!0;for(let i=1;i<e.length&&t;i++)t=e[i].time>e[i-1].time;if(t)return e.slice();const i=e.slice().sort((e,t)=>e.time-t.time);let s=0;for(let e=0;e<i.length;e++)s>0&&i[e].time===i[s-1].time?i[s-1]=i[e]:i[s++]=i[e];return i.length=s,i}(t))}addBars(e,t){const i=this._series.get(e);if(void 0===i)throw new Error(`openalgo-charts: unknown series ${e}`);if(0===t.length)return;const s=new Map;for(const e of i.bars)s.set(e.time,e);for(const e of t)s.set(e.time,e);this._replaceBars(i,Array.from(s.values()).sort((e,t)=>e.time-t.time))}update(e,t){const i=this._series.get(e);if(void 0===i)throw new Error(`openalgo-charts: unknown series ${e}`);const s=i.bars,r=s[s.length-1];if(void 0===r||t.time>r.time){s.push(t);const e=this._sortedTimes.length,i=e>0?this._sortedTimes[e-1]:void 0,r=this._retain(t.time);return void 0===i||t.time>i?(this._appendTime(t.time),"append"):r?(this._rebuild([t.time],!1),"insert"):"replace"}if(t.time===r.time)return s[s.length-1]=t,"replace";const n=function(e,t){let i=0,s=e.length-1;for(;i<=s;){const r=i+s>>1,n=e[r].time;if(n===t)return r;if(n<t)i=r+1;else{if(!(n>t))break;s=r-1}}return e.findIndex(e=>e.time===t)}(s,t.time);return n>=0?(s[n]=t,"replace"):(this.addBars(e,[t]),"insert")}_appendTime(e){this._indexByTime.has(e)||(this._indexByTime.set(e,this._sortedTimes.length),this._sortedTimes.push(e),this._version++)}get length(){return this._sortedTimes.length}get baseIndex(){return this._sortedTimes.length-1}indexToTime(e){return this._sortedTimes[e]}timeToIndex(e){return this._indexByTime.get(e)}indexToTimeFloat(e){const t=this._sortedTimes,i=t.length;if(0===i)return NaN;if(1===i)return t[0];if(e<=0)return t[0]+e*(t[1]-t[0]);if(e>=i-1){const t=this._plan(),s=e-(i-1),r=Math.floor(s),n=I(t,r);return s===r?n:n+(s-r)*(I(t,r+1)-n)}const s=Math.floor(e);return t[s]+(e-s)*(t[s+1]-t[s])}timeToIndexFloat(e){const t=this._sortedTimes,i=t.length;if(0===i)return NaN;if(1===i)return 0;if(e<=t[0]){const i=t[1]-t[0];return i>0?(e-t[0])/i:0}if(e>=t[i-1]){const t=this._plan(),s=t.slots;for(;t.next&&s[s.length-1]<=e;)k(t);const r=s.length-1;if(e>=s[r])return i-1+r+(e-s[r])/t.step;const n=O(s,e,r);return i-1+n+(e-s[n])/(s[n+1]-s[n])}const s=O(t,e,i-1);return s+(e-t[s])/(t[s+1]-t[s])}_plan(){const e=this._sortedTimes,t=Math.max(0,e.length-1-64);let i=this._future;if(null!==i&&i.version!==this._version&&i.tail.length===e.length-t&&i.tail.every((i,s)=>i===e[t+s])&&(i.version=this._version),null===i||i.version!==this._version){const s=e.slice(t),r=P(function(e){const t=[];for(let i=1;i<e.length;i++)t.push(e[i]-e[i-1]);return t}(s));let n=null;try{this._calendar&&(n=function(e,t,i){let s=0;const r=e=>++s>512?void 0:t.sessionFrom(e);let n=r(e[0]);if(!n)return null;const o=[],a=[n.open];let l=n.open-e[0];for(let t=1;t<e.length;t++){if(e[t]<n.close)o.push(e[t]-e[t-1]);else if(!(n=r(e[t])))return null;a.push(n.open),l=Math.max(l,n.open-e[t])}const h=e[e.length-1];let c=n,d=h;if(o.length>0){const t=P(o);if(l>=t)return null;const i=(e,i)=>{const s=((e-i)%t+t)%t;return s>1e-6&&t-s>1e-6?s-t:0},s=new Map;for(let t=0;t<e.length;t++)s.set(a[t]%S,i(e[t],a[t]));const n=i(h,c.open);return()=>{let e=d+t;for(;e>=c.close;){const t=r(c.close);if(!t)return null===t?null:NaN;c=t;const i=t.open+(s.get(t.open%S)??n);i>d&&(e=i)}return d=e}}if(i>=172800||l>=S)return null;const u=h-c.open;return()=>{for(const e=c.date;c.date===e;){const e=r(c.close);if(!e)return null===e?null:NaN;c=e}const e=c.open+u;return e>d?d=e:null}}(s,this._calendar,r))}catch{}i=this._future={version:this._version,tail:s,slots:[s[s.length-1]],next:n,step:r}}return i}seriesBars(e){return this._series.get(e)?.bars??M}indexedBars(e){const t=this._series.get(e);if(void 0===t)return[];const i=[];for(const e of t.bars){const t=this._indexByTime.get(e.time);void 0!==t&&i.push({index:t,bar:e})}return i}visibleBars(e,t,i){const s=this._series.get(e);if(void 0===s)return[];const r=s.bars;if(!T(this,r,t,i,C))return[];const n=[];for(let e=C.start;e<r.length;e++){const t=r[e].time;if(t>C.lastTime)break;const i=this._indexByTime.get(t);void 0!==i&&n.push({index:i,bar:r[e]})}return n}lastIndexedBar(e){const t=this._series.get(e);if(void 0===t||0===t.bars.length)return null;const i=t.bars[t.bars.length-1],s=this._indexByTime.get(i.time);return void 0===s?null:{index:s,bar:i}}_retain(e){const t=this._timeRefs.get(e)??0;return this._timeRefs.set(e,t+1),0===t}_replaceBars(e,t){const i=e.bars;e.bars=t;let s=0;for(;s<i.length&&s<t.length&&i[s].time===t[s].time;)s++;let r=null;for(let e=s;e<t.length;e++)this._retain(t[e].time)&&(r??(r=[])).push(t[e].time);let n=!1;for(let e=s;e<i.length;e++){const t=i[e].time,s=this._timeRefs.get(t)??0;s<=1?(this._timeRefs.delete(t),n=!0):this._timeRefs.set(t,s-1)}if(null===r&&!n)return;const o=this._sortedTimes.length>0?this._sortedTimes[this._sortedTimes.length-1]:-1/0;if(!n&&null!==r&&r[0]>o)for(const e of r)this._appendTime(e);else this._rebuild(r??[],n)}_rebuild(e=[],t=!0){const i=this._sortedTimes;if(e.some(Number.isNaN)||i.some(Number.isNaN)){const e=Array.from(this._timeRefs.keys()).sort((e,t)=>e-t);this._indexByTime.clear();for(let t=0;t<e.length;t++)this._indexByTime.set(e[t],t);return this._sortedTimes=e,void this._version++}const s=[];let r=0,n=0;for(;r<i.length||n<e.length;)if(n>=e.length||r<i.length&&i[r]<e[n]){const e=i[r++];!t||this._timeRefs.has(e)?s.push(e):this._indexByTime.delete(e)}else s.push(e[n++]);let o=0;for(;o<s.length&&o<i.length&&s[o]===i[o];)o++;for(let e=o;e<s.length;e++)this._indexByTime.set(s[e],e);this._sortedTimes=s,this._version++}}function L(e){return"string"==typeof e&&("right"===e||"left"===e||""===e||e.startsWith("overlay:"))}const N=["left","right","hidden"];function R(e){return"left"===e||"right"===e?e:"hidden"}class F{constructor(){Object.defineProperty(this,"_configured",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_placements",{enumerable:!0,configurable:!0,writable:!0,value:new Map([["right",{side:"right",order:0}]])})}register(e){L(e)&&!this._placements.has(e)&&this._placements.set(e,this.get(e))}get(e){const t=this._placements.get(e);if(t)return{...t};const i=R(e);let s=0;for(const e of this._placements.values())e.side===i&&s++;return{side:i,order:s}}set(e,t,i){if(!L(e)||!N.includes(t)||void 0!==i&&(!Number.isSafeInteger(i)||i<0))return!1;const s=this.get(e),r=this._sideIds(t).filter(t=>t!==e),n=Math.min(i??(s.side===t?s.order:r.length),r.length);return(s.side!==t||s.order!==n)&&(this.register(e),r.splice(n,0,e),s.side!==t&&this._reorder(s.side,this._sideIds(s.side).filter(t=>t!==e)),this._reorder(t,r),!0)}entries(){return Array.from(this._placements,([e,t])=>({scaleId:e,...t}))}configured(e){return this._configured.has(e)}restore(e){this._configured.clear();for(const t of e.keys())this._configured.add(t);for(const t of e.keys())this.register(t);const t={left:0,right:0,hidden:0};for(const i of this._placements.keys()){const s=R(i),r={side:s,order:t[s]++};this._placements.set(i,{...e.get(i)??r})}for(const e of N)this._reorder(e,this._sideIds(e))}_sideIds(e){return this.entries().filter(t=>t.side===e).sort((e,t)=>e.order-t.order).map(e=>e.scaleId)}_reorder(e,t){t.forEach((t,i)=>{const s=this._placements.get(t);s?.side===e&&s.order===i||this._configured.add(t),this._placements.set(t,{side:e,order:i})})}}function z(e,t){return"dashed"===e?[4*t,4*t]:"dotted"===e?[1*t,3*t]:[]}function E(e,t,i){const s=z(e.gridStyle,i);return{color:e.grid,lineWidth:t?.lineWidth??1,dash:s,vert:{color:t?.vertColor??e.grid,dash:void 0===t?.vertStyle?s:z(t.vertStyle,i)},horz:{color:t?.horzColor??e.grid,dash:void 0===t?.horzStyle?s:z(t.horzStyle,i)}}}function B(e,t,i,s,r,n,o,a){if(0!==t.length){e.strokeStyle=s?.color??r.color,e.setLineDash(s?.dash??r.dash??[]),e.beginPath();for(const s of t){const t=Math.round(c(s,n)*n);i?(e.moveTo(t,0),e.lineTo(t,a)):(e.moveTo(0,t),e.lineTo(o,t))}e.stroke()}}const $=10,D=14;function W(e,t){const i=void 0===t?.fontSize?e.axisFontSize??11:o(Math.round(t.fontSize),10,14);return{textColor:t?.textColor??e.axisText,lineColor:t?.lineColor??e.axisLine,font:`${i}px system-ui, sans-serif`}}function q(e){const t={};return void 0!==e?.top&&(t.marginTop=o(e.top,0,49)/100),void 0!==e?.bottom&&(t.marginBottom=o(e.bottom,0,49)/100),t}const V={upColor:"#26a69a",downColor:"#ef5350",borderUpColor:"#26a69a",borderDownColor:"#ef5350",wickUpColor:"#26a69a",wickDownColor:"#ef5350",borderVisible:!0,wickVisible:!0};function H(e,t,i={}){return{...V,upColor:e.upColor??t.upColor,downColor:e.downColor??t.downColor,borderUpColor:e.borderUpColor??t.upColor,borderDownColor:e.borderDownColor??t.downColor,wickUpColor:e.wickUpColor??t.wickUpColor,wickDownColor:e.wickDownColor??t.wickDownColor,borderVisible:e.borderVisible??V.borderVisible,bodyVisible:e.bodyVisible??!0,wickVisible:e.wickVisible??V.wickVisible,colorByPreviousClose:e.colorByPreviousClose,...i}}function G(e,t){const i=Math.floor(e*t*.8);let s=Math.max(1,i);if(s>=2){s%2!=Math.max(1,Math.floor(t))%2&&(s-=1)}return Math.max(1,s)}function U(e,t,i){return i.wickVisible?!0===i.hollow||!1===i.bodyVisible||e>t||i.borderVisible&&e>=3||i.upColor!==i.wickUpColor||i.downColor!==i.wickDownColor?"full":"wick":"full"}function K(e,t,i,s=1){const r=G(t,i),n=Math.max(1,Math.floor(i)),o=Math.round(e*i),a=Y(r,s);return{cx:o,bodyX:o-Math.floor(a/2),bodyW:a,wickX:o-Math.floor(n/2),wickW:n}}function Y(e,t){const i=Math.max(.05,Math.min(1,t));return 1===i?e:Math.max(1,Math.round(e*i))}function X(e,t,i,s,r,n=V){const o=G(s,r),a=Math.max(1,Math.floor(r)),l=n.widthScale?null:U(o,a,n);for(let s=0;s<t.length;s++){const{x:h,bar:c}=t[s],d=s>0?t[s-1].bar.close:t[s].prevClose,u=!0===n.colorByPreviousClose&&void 0!==d&&Number.isFinite(d)?c.close>=d:c.close>=c.open,_=Math.round(i(c.open)*r),p=Math.round(i(c.close)*r),f=Math.round(i(c.high)*r),m=Math.round(i(c.low)*r),b=Math.round(h*r),g=n.widthScale?Y(o,n.widthScale(c)):o,v=Math.floor(g/2),y=c.color,w=y??(u?n.upColor:n.downColor),x=c.wickColor??y??(u?n.wickUpColor:n.wickDownColor),S=c.borderColor??y??(u?n.borderUpColor:n.borderDownColor);if(n.wickVisible&&(e.fillStyle=x,e.fillRect(b-(a>>1),f,a,Math.max(1,m-f))),"wick"===(l??U(g,a,n))&&x===w)continue;const P=Math.min(_,p),O=Math.max(1,Math.abs(p-_));if(n.hollow&&u)e.strokeStyle=n.borderVisible?S:w,e.lineWidth=Math.max(1,a),e.strokeRect(b-v+.5,P+.5,g-1,O-1);else{const t=!1!==n.bodyVisible;t&&(e.fillStyle=w,e.fillRect(b-v,P,g,O)),n.borderVisible&&(!t||g>=3)&&(e.strokeStyle=S,e.lineWidth=1,e.strokeRect(b-v+.5,P+.5,g-1,O-1))}}}function J(e,t,i,s,r,n,o=!1){const a=Math.max(1,Math.floor(G(s,r)/2)),l=Math.max(1,Math.floor(r));e.save();for(let s=0;s<t.length;s++){const h=t[s],c=h.bar,d=Math.round(h.x*r),u=Math.round(i(c.open)*r),_=Math.round(i(c.close)*r),p=Math.round(i(c.high)*r),f=Math.round(i(c.low)*r),m=s>0?t[s-1].bar.close:h.prevClose,b=!0===n.colorByPreviousClose&&void 0!==m&&Number.isFinite(m)?h.bar.close>=m:c.close>=c.open;e.fillStyle=h.bar.color??(b?n.upColor??"#26a69a":n.downColor??"#ef5350"),e.fillRect(d-Math.floor(l/2),p,l,Math.max(1,f-p)),o||(e.fillRect(d-a,u,a,l),e.fillRect(d,_,a,l))}e.restore()}function Z(e){const t=e.trim();if(t.startsWith("#")){const e=t.slice(1);if(3===e.length||4===e.length){const[t,i,s,r]=[...e].map(e=>parseInt(e+e,16));return[t,i,s].some(Number.isNaN)?null:{r:t,g:i,b:s,a:4===e.length?r/255:1}}if(6===e.length||8===e.length){const t=parseInt(e.slice(0,6),16);if(Number.isNaN(t))return null;const i=8===e.length?parseInt(e.slice(6,8),16)/255:1;return{r:t>>16&255,g:t>>8&255,b:255&t,a:Number.isNaN(i)?1:i}}return null}const i=/^rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*(?:,\s*([\d.]+)\s*)?\)$/i.exec(t);return null===i?null:{r:Number(i[1]),g:Number(i[2]),b:Number(i[3]),a:void 0===i[4]?1:Number(i[4])}}function Q(e){const t=Z(e);return(null!==t?te(t):.5)>.45?"#10131a":"#ffffff"}const ee=e=>{const t=e/255;return t<=.03928?t/12.92:Math.pow((t+.055)/1.055,2.4)};function te(e){return.2126*ee(e.r)+.7152*ee(e.g)+.0722*ee(e.b)}function ie(e,t){const i=Z(e);return null===i?e:`rgba(${i.r},${i.g},${i.b},${t})`}function se(e){if("transparent"===e.trim().toLowerCase())return!0;const t=Z(e);return null!==t&&t.a<=0}function re(e,t){const i=Z(e);if(null===i)return e;const s=t>=0?255:0,r=Math.min(1,Math.abs(t)),n=e=>Math.round(e+(s-e)*r);return`rgba(${n(i.r)},${n(i.g)},${n(i.b)},${i.a})`}function ne(e,t,i,s,r,n){const o=e.roundRect;"function"==typeof o?o.call(e,t,i,s,r,Math.max(0,Math.min(n,s/2,r/2))):e.rect(t,i,s,r)}const oe=new WeakMap;function ae(e,t,i,s){let r=oe.get(e);void 0===r&&(r=new Map,oe.set(e,r));const n=`${Math.round(t)}|${i}|${s}`;let o=r.get(n);return void 0===o&&(o=e.createLinearGradient(0,0,0,t),o.addColorStop(0,i),o.addColorStop(1,s),r.set(n,o)),o}function le(e,t,i,s,r){const n=Z(s),o=Z(r);if(null===n||null===o)return s;const a=(e-t)/(i-t),l=a>0?a<1?a:1:0;return`rgba(${Math.round(n.r+(o.r-n.r)*l)},${Math.round(n.g+(o.g-n.g)*l)},${Math.round(n.b+(o.b-n.b)*l)},${n.a+(o.a-n.a)*l})`}const he={xs:[],ys:[],s:0,n:0,a:-1,b:-1},ce={xs:[],ys:[],s:0,n:0,a:-1,b:-1},de={xs:[],ys:[],s:0,n:0,a:-1,b:-1},ue={xs:[],ys:[],s:0,n:0,a:-1,b:-1},_e=[];function pe(e,t){e.s=0,e.a=e.b=-1,e.n=t,e.xs.length>16384&&4*t<e.xs.length&&(e.xs.length=e.ys.length=t)}function fe(e,t,i,s){const r=e.xs,n=e.ys;for(let e=0;e<t.length;e++){const o=t[e],a=o.bar;r[e]=o.x,n[e]=i(0===s?a.close:1===s?a.high:a.low)}pe(e,t.length)}function me(e,t,i,s,r){const n=t.length;if(0===n)return;const o=t[0].edgeX,a=t[n-1].edgeX;if(void 0===o&&void 0===a)return;if(1===n)return void(e.n=0);const l=e.xs,h=e.ys;if(void 0!==o){const t=o-r,n=l[1],a=h[1],c=l[0],d=h[0];if(n<t||!Number.isFinite(d)||!Number.isFinite(a))e.s=i?2:1,h[0]=NaN;else{const r=Math.hypot(c-n,d-a);let o=c<t?r*(n-t)/(n-c):r;const u=i&&Number.isFinite(h[2]),_=u?Math.abs(h[2]-a):0;if(s>0&&(o=Math.ceil((o+_)/s)*s-_),o!==r){const e=o/r;l[0]=n+(c-n)*e,h[0]=a+(d-a)*e}e.a=u?2:1}}if(void 0!==a){const t=a+r,s=i?e.n-2:e.n-1,n=l[s-1],o=h[s-1],c=l[s],d=h[s];n>t||!Number.isFinite(o)||!Number.isFinite(d)?(e.n=s,h[s]=NaN):(c>t&&(h[s]=o+(t-n)/(c-n)*(d-o),l[s]=t,e.n=s+1),e.b=s-1)}}function be(e,t){return"dashed"===e?[6*t,4*t]:"dotted"===e?[1*t,3*t]:[]}function ge(e,t,i,s,r=!1){const n=t.n,o=t.xs,a=t.ys,l=t.a,h=t.b<0?-1:t.b+1;if(n<=t.s)return;const c=e.strokeStyle;e.beginPath();let d,u=-1,_=!1,p=0;for(let f=t.s;f<n;f++){const t=o[f],n=a[f];if(!Number.isFinite(t)||!Number.isFinite(n)){u=-1;continue}if(u<0){e.moveTo(t*i,n*i),u=f,p=0;continue}const m=void 0===s?d:s[f];m!==d&&(_&&(e.stroke(),e.beginPath(),e.moveTo(o[u]*i,a[u]*i),_=!1,p=0),e.strokeStyle=m??c,d=m),r&&_&&f===h&&(e.stroke(),e.beginPath(),e.moveTo(o[u]*i,a[u]*i),e.lineDashOffset=p*i,_=!1),e.lineTo(t*i,n*i),r&&(p+=Math.hypot(t-o[u],n-a[u])),u=f,_=!0,r&&f===l&&(e.stroke(),e.beginPath(),e.moveTo(t*i,n*i),_=!1,p=0)}e.stroke()}function ve(e,t,i,s,r){const n=he;fe(n,t,i,0);let o=n;r.step&&function(e,t){const i=e.xs,s=e.ys,r=t.xs,n=t.ys;let o=0;for(let t=0;t<e.n;t++)t>0&&(r[o]=i[t],n[o++]=s[t-1]),r[o]=i[t],n[o++]=s[t];pe(t,o)}(n,o=ce);const a=function(e,t){let i=!1;for(let t=0;t<e.length;t++)if(void 0!==e[t].bar.color){i=!0;break}if(!i)return;let s=0;for(let i=0;i<e.length;i++){const r=e[i].bar.color;t&&s>0&&(_e[s++]=r),_e[s++]=r}return _e.length=s,_e}(t,!0===r.step),l=r.lineWidth??1.5,h=be(r.lineStyle,s);if(me(o,t,!0===r.step,function(e,t){let i=0;for(let t=0;t<e.length;t++)i+=e[t];return i>0?(e.length%2==1?2*i:i)/t:0}(h,s),8+l),e.save(),e.strokeStyle=r.color??"#4f8cff",e.lineWidth=Math.max(1,l*s),e.lineJoin="round",e.lineCap="round",e.setLineDash(h),r.markersOnly||ge(e,o,s,a,h.length>0),e.setLineDash([]),r.markers||r.markersOnly){const i=(r.markerRadius??2)*s,o=r.color??"#4f8cff";e.fillStyle=o;for(let r=0;r<t.length;r++){const l=n.xs[r],h=n.ys[r];Number.isFinite(l)&&Number.isFinite(h)&&void 0===t[r].edgeX&&(void 0!==a&&(e.fillStyle=t[r].bar.color??o),e.beginPath(),e.arc(l*s,h*s,i,0,2*Math.PI),e.fill())}}e.restore()}const ye="rgba(79,140,255,0.15)";function we(e,t,i,s,r){void 0!==i&&(e.save(),e.strokeStyle=i,e.lineWidth=Math.max(1,Math.round((s.lineWidth??1.5)*r)),ge(e,t,r),e.restore())}const xe={color:"#3a4666",base:0};function Se(e,t){return{...e,upColor:e.upColor??t.upColor,downColor:e.downColor??t.downColor}}function Pe(e,t,i){return{...e,color:e.color??t.lineColor,...i}}const Oe=e=>({min:e.low,max:e.high}),ke=e=>({min:e.close,max:e.close}),Ie=(e,t)=>{const i=t.base??0;return{min:Math.min(i,e.close),max:Math.max(i,e.close)}},Me=new Map,Te=new Set(["point-figure","kagi"]);function Ce(e,t){Me.set(e,t)}function je(e){const t=Me.get(e);if(void 0===t){if(Te.has(e))throw new Error(`openalgo-charts: series type "${e}" needs the transform tier, import 'openalgo-charts/transform' first`);throw new Error(`openalgo-charts: unknown series type "${e}"`)}return t}function Ae(){return Array.from(Me.keys())}function Le(e,t,i=.5,s=1){const r=e*t;if(r<=0)return 1;const n=i*Math.max(1,s);return r>=n?1:Math.max(1,Math.ceil(n/r))}function Ne(e){const t=e[0];let i=t.high,s=t.low,r=t.volume??0,n=void 0!==t.volume,o=t.oi;for(let t=1;t<e.length;t++){const a=e[t];a.high>i&&(i=a.high),a.low<s&&(s=a.low),void 0!==a.volume&&(r+=a.volume,n=!0),void 0!==a.oi&&(o=a.oi)}const a={time:t.time,open:t.open,high:i,low:s,close:e[e.length-1].close};return n&&(a.volume=r),void 0!==o&&(a.oi=o),a}function Re(e,t){if(t<=1)return e.slice();const i=[];for(let s=0;s<e.length;s+=t)i.push(Ne(e.slice(s,s+t)));return i}function Fe(e,t){if(t<=1)return e.map(e=>({x:e.x,bar:e.bar}));const i=[];for(let s=0;s<e.length;s+=t){const r=e.slice(s,s+t),n=r.reduce((e,t)=>e+t.x,0)/r.length;i.push({x:n,bar:Ne(r.map(e=>e.bar))})}return i}Ce("candlestick",{defaultStyle:{},isPriceSeries:!0,draw:(e,t,i,s,r,n,o)=>X(e,t,i,s,r,H(n,o.theme)),extents:Oe}),Ce("hollow-candle",{defaultStyle:{},isPriceSeries:!0,draw:(e,t,i,s,r,n,o)=>X(e,t,i,s,r,H(n,o.theme,{hollow:!0})),extents:Oe}),Ce("volume-candle",{defaultStyle:{},isPriceSeries:!0,draw:(e,t,i,s,r,n,o)=>{const a=o.maxVolume;X(e,t,i,s,r,H(n,o.theme,{widthScale:e=>a>0?(e.volume??0)/a:1}))},extents:Oe}),Ce("bar",{defaultStyle:{},isPriceSeries:!0,draw:(e,t,i,s,r,n,o)=>J(e,t,i,s,r,Se(n,o.theme)),extents:Oe}),Ce("high-low",{defaultStyle:{},isPriceSeries:!0,draw:(e,t,i,s,r,n,o)=>J(e,t,i,s,r,Se(n,o.theme),!0),extents:Oe}),Ce("line",{defaultStyle:{lineWidth:1.5},isPriceSeries:!0,connectsBars:!0,draw:(e,t,i,s,r,n,o)=>ve(e,t,i,r,Pe(n,o.theme)),extents:ke}),Ce("line-markers",{defaultStyle:{lineWidth:1.5,markers:!0},isPriceSeries:!0,connectsBars:!0,draw:(e,t,i,s,r,n,o)=>ve(e,t,i,r,Pe(n,o.theme,{markers:!0})),extents:ke}),Ce("step",{defaultStyle:{lineWidth:1.5,step:!0},isPriceSeries:!0,connectsBars:!0,draw:(e,t,i,s,r,n,o)=>ve(e,t,i,r,Pe(n,o.theme,{step:!0})),extents:ke}),Ce("area",{defaultStyle:{lineWidth:1.5},isPriceSeries:!0,connectsBars:!0,draw:(e,t,i,s,r,n,o)=>function(e,t,i,s,r,n){const o=he;fe(o,t,i,0),me(o,t,!1,0,8+(n.lineWidth??1.5));const a=o.s,l=o.n,h=o.xs,c=o.ys;if(l<=a)return;const d=r*s;e.save(),e.beginPath(),e.moveTo(h[a]*s,d);for(let t=a;t<l;t++)e.lineTo(h[t]*s,c[t]*s);e.lineTo(h[l-1]*s,d),e.closePath(),e.fillStyle=ae(e,d,n.areaTopColor??"rgba(79,140,255,0.40)",n.areaBottomColor??"rgba(79,140,255,0.00)"),e.fill(),e.restore(),ve(e,t,i,s,{color:n.color??"#4f8cff",lineWidth:n.lineWidth??1.5,lineStyle:n.lineStyle})}(e,t,i,r,o.plotHeight,function(e,t){return{...e,color:e.color??t.lineColor,areaTopColor:e.areaTopColor??t.areaTopColor,areaBottomColor:e.areaBottomColor??t.areaBottomColor}}(n,o.theme)),extents:ke}),Ce("hlc-area",{defaultStyle:{lineWidth:1.5},isPriceSeries:!0,connectsBars:!0,draw:(e,t,i,s,r,n,o)=>function(e,t,i,s,r){if(0===t.length)return;const n=de,o=ue,a=8+(r.lineWidth??1.5);fe(n,t,i,1),fe(o,t,i,2),me(n,t,!1,0,a),me(o,t,!1,0,a);const l=n.s,h=n.n;e.save(),e.beginPath(),h>l&&e.moveTo(n.xs[l]*s,n.ys[l]*s);for(let t=l;t<h;t++)e.lineTo(n.xs[t]*s,n.ys[t]*s);for(let t=o.n-1;t>=o.s;t--)e.lineTo(o.xs[t]*s,o.ys[t]*s);e.closePath(),e.fillStyle=r.areaTopColor??ye,e.fill(),e.restore(),we(e,n,r.highColor,r,s),we(e,o,r.lowColor,r,s),ve(e,t,i,s,{color:r.closeColor??"#4f8cff",lineWidth:r.lineWidth??1.5})}(e,t,i,r,function(e,t){return{...e,closeColor:e.closeColor??t.lineColor}}(n,o.theme)),extents:Oe}),Ce("baseline",{defaultStyle:{baseValue:0,lineWidth:1.5},isPriceSeries:!0,connectsBars:!0,draw:(e,t,i,s,r,n,o)=>function(e,t,i,s,r){const n=i(r.baseValue??0)*s,o=he;fe(o,t,i,0),me(o,t,!1,0,8+(r.lineWidth??1.5));const a=o.s,l=o.n,h=o.xs,c=o.ys;if(l<=a)return;const d=h[a]*s,u=h[l-1]*s,_=()=>{e.beginPath(),e.moveTo(d,n);for(let t=a;t<l;t++)e.lineTo(h[t]*s,c[t]*s);e.lineTo(u,n),e.closePath()},p=r.areaTopColor??"rgba(38,166,154,0.20)",f=r.areaBottomColor??"rgba(239,83,80,0.20)",m=1e5;e.save(),e.beginPath(),e.rect(d,n-m,u-d,m),e.clip(),_(),e.fillStyle=ae(e,n,p,"rgba(0,0,0,0)"),e.fill(),e.restore(),e.save(),e.beginPath(),e.rect(d,n,u-d,m),e.clip(),_(),e.fillStyle=f,e.fill(),e.restore(),e.save();for(let t=a+1;t<l;t++){const i=c[t-1],o=c[t],a=(i+o)/2<=n/s;e.strokeStyle=a?r.topColor??"#26a69a":r.bottomColor??"#ef5350",e.lineWidth=Math.max(1,Math.round((r.lineWidth??1.5)*s)),e.beginPath(),e.moveTo(h[t-1]*s,i*s),e.lineTo(h[t]*s,o*s),e.stroke()}e.restore()}(e,t,i,r,function(e,t){return{...e,topColor:e.topColor??t.baselineTopLine,bottomColor:e.bottomColor??t.baselineBottomLine,areaTopColor:e.areaTopColor??t.baselineTopFill,areaBottomColor:e.areaBottomColor??t.baselineBottomFill}}(n,o.theme)),extents:(e,t)=>{const i=t.baseValue??0;return{min:Math.min(i,e.close),max:Math.max(i,e.close)}}}),Ce("column",{defaultStyle:{base:0},isPriceSeries:!1,draw:(e,t,i,s,r,n,o)=>function(e,t,i,s,r,n){const o=G(s,r),a=Math.floor(o/2),l=Math.round(i(n.base??0)*r);e.save();for(const s of t){const t=Math.round(s.x*r),h=Math.round(i(s.bar.close)*r),c=s.bar.close>=s.bar.open;e.fillStyle=s.bar.color??n.color??(c?n.upColor??"#26a69a":n.downColor??"#ef5350");const d=Math.min(l,h);e.fillRect(t-a,d,o,Math.max(1,Math.abs(l-h)))}e.restore()}(e,t,i,s,r,Se(n,o.theme)),extents:Ie}),Ce("histogram",{defaultStyle:{base:0,color:xe.color},isPriceSeries:!1,draw:(e,t,i,s,r,n)=>{!function(e,t,i,s,r,n=xe){const o=G(s,r),a=Math.floor(o/2),l=Math.round(i(n.base)*r);let h="";for(const{x:s,bar:c}of t){const t=c.color??n.color;t!==h&&(e.fillStyle=t,h=t);const d=Math.round(s*r),u=Math.round(i(c.close)*r),_=Math.min(l,u),p=Math.max(1,Math.abs(l-u));e.fillRect(d-a,_,o,p)}}(e,t,i,s,r,{color:n.color??xe.color,base:n.base??0})},extents:Ie});const ze=new Map([["candlestick","ohlc"],["hollow-candle","ohlc"],["volume-candle","ohlc"],["bar","ohlc"],["high-low","ohlc"],["hlc-area","band"],["line","line"],["line-markers","line"],["step","line"],["area","line"],["baseline","line"],["column","column"],["histogram","column"]]);function Ee(e){return ze.get(e)??null}function Be(e,t){return"ohlc"===e?Math.max(1,Math.floor(t)):1}function $e(e,t=1,i="ohlc"){const s=Number.isFinite(t)&&t>1?t:1;return Math.max(1,Math.round(Be(i,e)*s))}function De(e){let t="ohlc",i=1,s=1,r=1,n=NaN;const o=[];let a,l,h,c,d=0,u=!1,_=0,p=NaN,f=NaN,m=NaN,b=NaN,g=0,v=!1,y=null,w=0,x=null,S=0,P=null,O=0,k=0,I=null,M=0,T=0,C=0,j=null,A=0,L=0,N=null,R=0,F=0,z=0,E=0;const B=[],$=[],D=[];let W=0,q=!1;const V=(e,t,i)=>{let s=W;for(;s>0&&B[s-1]>e;)s--;if(!(s>0&&B[s-1]===e)){for(let e=W;e>s;e--)B[e]=B[e-1],$[e]=$[e-1],D[e]=D[e-1];B[s]=e,$[s]=t,D[s]=i,W++}},H=()=>{if(null===y)return;const i=y;if(y=null,"band"===t){W=0,V(z,w,i),V(k,O,P),V(T,M,I),V(L,A,j),V(F,R,N),V(E,S,x);for(let t=0;t<W;t++)e($[t],D[t]);return}const s=k<=T,r=s?P:I,n=s?O:M,o=s?I:P,a=s?M:O;if("column"===t)return e(n,r),void(o!==r&&e(a,o));e(w,i),r!==i&&e(n,r),o!==i&&o!==r&&e(a,o),x!==i&&x!==r&&x!==o&&e(S,x)},G=()=>{"ohlc"===t?(()=>{if(!u)return;u=!1;let t=o[d];void 0===t&&(t={time:0,open:0,high:0,low:0,close:0,volume:void 0,oi:void 0,color:void 0,wickColor:void 0,borderColor:void 0},o.push(t)),d++,t.time=_,t.open=p,t.high=f,t.low=m,t.close=b,t.volume=v?g:void 0,t.oi=a,t.color=l,t.wickColor=h,t.borderColor=c,e((n*s+(s-r>>1)+(r>>1))/i,t)})():H()};return{begin(e,o,a){t=e,i=o,s=$e(o,a,e),r=Be(e,o),n=NaN,d=0,u=!1,y=null,q=!1,C=0},push(o,d){const B=Math.floor((Math.round(o*i)-(r>>1)+(r-1>>1))/s);B!==n&&(G(),n=B),"ohlc"===t?(e=>{if(!Number.isFinite(e.close))return;const t=e.high,i=e.low;u?(t!=t||f>=t||(f=t),i!=i||m<=i||(m=i)):(u=!0,_=e.time,p=e.open,f=t,m=i,g=0,v=!1,a=void 0),b=e.close,void 0!==e.volume&&(g+=e.volume,v=!0),void 0!==e.oi&&(a=e.oi),l=e.color,h=e.wickColor,c=e.borderColor})(d):((i,s)=>{const r=s.close;if(!Number.isFinite(r)){if("column"===t)return;return H(),void(q||(q=!0,e(i,s)))}q=!1;const n=C++;if(null===y)y=s,w=i,z=n,P=s,O=i,k=n,I=s,M=i,T=n,j=s,A=i,L=n,N=s,R=i,F=n;else if(r<P.close&&(P=s,O=i,k=n),r>I.close&&(I=s,M=i,T=n),"band"===t){const e=s.high,t=s.low;e!=e||j.high>=e||(j=s,A=i,L=n),t!=t||N.low<=t||(N=s,R=i,F=n)}x=s,S=i,E=n})(o,d)},end(){G(),n=NaN}}}const We={time:0,open:NaN,high:NaN,low:NaN,close:NaN},qe={start:0,lastTime:0};function Ve(){const e=[],t=[];let i=0,s=-1,r=null;const n=(s,r)=>{const n=i++;let o=t[n];void 0===o&&(o={x:0,bar:We,prevClose:void 0,edgeX:void 0},t.push(o)),o.x=s,o.bar=r,o.prevClose=void 0,o.edgeX=void 0,e[n]=o},o=(t,s,r,o,a)=>{const l=t.timeToIndex(s.time);void 0!==l&&(n(o.indexToX(l+r),s),e[i-1].edgeX=a)},a=De(n);return{items:e,priceToY:e=>r.priceToY(e),firstIndex:()=>s,build(l,h,c,d,u,_,p,f,m=!1){r=p,i=0,s=-1;const b=l.seriesBars(h);if(T(l,b,c,d,qe)){const e=qe.lastTime;m&&qe.start>0&&o(l,b[qe.start-1],u,_,0),null!==f&&a.begin(f.kind,f.dpr,f.factor);let t=qe.start;for(;t<b.length;t++){const i=b[t];if(i.time>e)break;const r=l.timeToIndex(i.time);if(void 0===r)continue;s<0&&(s=r);const o=_.indexToX(r+u);null!==f?a.push(o,i):n(o,i)}null!==f&&a.end(),m&&t<b.length&&o(l,b[t],u,_,_.width)}if(e.length!==i){for(let s=i;s<e.length;s++)t[s].bar=We;e.length=i}return t.length>4096&&4*i<t.length&&(t.length=Math.max(4096,2*i)),e}}}const He={left:1/0,top:1/0,right:-1/0,bottom:-1/0},Ge={left:-1/0,top:-1/0,right:1/0,bottom:1/0};let Ue=0;function Ke(){const e=[],t=[],i=[],s=[],r=[];let n=0,o=0,a=!1,l=!1,h=!1;const c=(e,t)=>e.length===t.length&&e.every((e,i)=>Object.is(e,t[i])),d=e=>{const i=n++;Object.is(t[i],e)||(t[i]=e,a=!0)};return{begin(){n=0,o=0,a=!1,d(Ue)},number:d,ref(e){const t=o++;t<i.length&&Object.is(i[t],e)||(i[t]=e,a=!0)},scale(e){d(e.priceToY(1)),d(e.priceToY(1e3)),d(e.height),d(m.indexOf(e.options.mode)),d(e.options.inverted?1:0)},end(){n!==t.length&&(t.length=n,a=!0),o!==i.length&&(i.length=o,a=!0),a&&(e.length=0,h=!1,l=c(t,s)&&c(i,r))},painted(){h&&(e.length=0,h=!1),s.length=0,s.push(...t),r.length=0,r.push(...i),l=!0},kept:t=>e[t],measure(t,i,s){const r=void 0===i.hitBounds?Ge:i.hitBounds(s)??He;return e[t]=r,l||(h=!0),r},clear(){e.length=0}}}function Ye(e,t,i){return!(t<e.left||t>e.right||i<e.top||i>e.bottom)}const Xe=19800;function Je(e){return Math.floor(e/1e3)}function Ze(e){const t=e.trim(),i=/^(\d{4})-(\d{2})-(\d{2})(?:[ T](\d{2}):(\d{2})(?::(\d{2}))?)?/.exec(t);if(null===i)throw new Error(`openalgo-charts: unparseable IST time string "${e}"`);const s=Number(i[1]),r=Number(i[2]),n=Number(i[3]),o=void 0!==i[4]?Number(i[4]):0,a=void 0!==i[5]?Number(i[5]):0,l=void 0!==i[6]?Number(i[6]):0,h=Date.UTC(s,r-1,n,o,a,l);return Math.floor(h/1e3)-Xe}function Qe(e){const t=new Date(1e3*(e+Xe));return{year:t.getUTCFullYear(),month:t.getUTCMonth()+1,day:t.getUTCDate(),hour:t.getUTCHours(),minute:t.getUTCMinutes(),second:t.getUTCSeconds(),weekday:t.getUTCDay()}}const et=e=>e<10?`0${e}`:String(e);function tt(e){const t=Qe(e);return`${et(t.hour)}:${et(t.minute)}`}function it(e){const t=Qe(e);return`${et(t.hour)}:${et(t.minute)}:${et(t.second)}`}function st(e){const t=Qe(e);return`${t.year}-${et(t.month)}-${et(t.day)}`}const rt=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];function nt(e){const t=Qe(e);return`${et(t.day)} ${rt[t.month-1]}`}const ot=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];function at(e){const t=Qe(e);let i=`${ot[t.weekday]} ${et(t.day)} ${rt[t.month-1]} '${String(t.year).slice(-2)}`;return 0===t.hour&&0===t.minute&&0===t.second||(i+=` ${et(t.hour)}:${et(t.minute)}`,0!==t.second&&(i+=`:${et(t.second)}`)),i}function lt(e,t){const i=Qe(e),s=Qe(t);return i.year!==s.year||i.month!==s.month||i.day!==s.day}const ht="Asia/Kolkata",ct=new Map,dt={Sun:0,Mon:1,Tue:2,Wed:3,Thu:4,Fri:5,Sat:6};function ut(e){let t=ct.get(e);if(void 0===t){let i;try{i=new Intl.DateTimeFormat("en-US",{timeZone:e,hourCycle:"h23",year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit",weekday:"short"})}catch{throw new Error(`openalgo-charts: unknown IANA time zone "${e}"`)}t={format:i,parts:new Map,lastSecond:Number.NaN,lastParts:null},ct.set(e,t)}return t}function _t(e){if(ct.has(e))return!0;try{return ut(e),!0}catch{return!1}}function pt(e,t){const i=ut(t);if(e===i.lastSecond&&null!==i.lastParts)return i.lastParts;let s=i.parts.get(e);return void 0===s&&(s=function(e,t){let i=1970,s=1,r=1,n=0,o=0,a=0,l=4;for(const h of e.formatToParts(new Date(1e3*t)))switch(h.type){case"year":i=Number(h.value);break;case"month":s=Number(h.value);break;case"day":r=Number(h.value);break;case"hour":n=Number(h.value)%24;break;case"minute":o=Number(h.value);break;case"second":a=Number(h.value);break;case"weekday":l=dt[h.value]??0}return{year:i,month:s,day:r,hour:n,minute:o,second:a,weekday:l,dayIndex:Math.floor(Date.UTC(i,s-1,r)/864e5)}}(i.format,e),i.parts.size>=4096&&i.parts.clear(),i.parts.set(e,s)),i.lastSecond=e,i.lastParts=s,s}function ft(e,t=ht){const i=pt(e,t);return{year:i.year,month:i.month,day:i.day,hour:i.hour,minute:i.minute,second:i.second,weekday:i.weekday}}function mt(e,t=ht){return pt(e,t).dayIndex}function bt(e,t=ht){return Math.floor((pt(e,t).dayIndex+3)/7)}function gt(e,t=ht){const i=pt(e,t);return Math.floor(Date.UTC(i.year,i.month-1,i.day,i.hour,i.minute,i.second)/1e3)-e}function vt(e,t,i=ht){return pt(e,i).dayIndex!==pt(t,i).dayIndex}function yt(e,t,i=ht){return Math.floor((pt(e,i).dayIndex+3)/7)!==Math.floor((pt(t,i).dayIndex+3)/7)}function wt(e,t,i=ht){const s=pt(e,i),r=pt(t,i);return s.year!==r.year||s.month!==r.month}function xt(e,t,i=ht){const s=pt(e,i),r=pt(t,i);return s.year!==r.year||Math.floor((s.month-1)/3)!==Math.floor((r.month-1)/3)}function St(e,t,i=ht){return pt(e,i).year!==pt(t,i).year}function Pt(e,t,i,s=ht){switch(i){case"week":return yt(e,t,s);case"month":return wt(e,t,s);case"quarter":return xt(e,t,s);case"year":return St(e,t,s);default:return vt(e,t,s)}}function Ot(e,t=ht){const i=e.trim(),s=/^(\d{4})-(\d{2})-(\d{2})(?:[ T](\d{2}):(\d{2})(?::(\d{2}))?)?/.exec(i);if(null===s)throw new Error(`openalgo-charts: unparseable time string "${e}"`);return kt(Number(s[1]),Number(s[2]),Number(s[3]),void 0!==s[4]?Number(s[4]):0,void 0!==s[5]?Number(s[5]):0,void 0!==s[6]?Number(s[6]):0,t)}function kt(e,t,i,s=0,r=0,n=0,o=ht){const a=Math.floor(Date.UTC(e,t-1,i,s,r,n)/1e3),l=a-gt(a,o);return a-gt(l,o)}function It(e,t=ht){const i=pt(e,t);return kt(i.year,i.month,i.day,0,0,0,t)}function Mt(e,t=ht){const i=pt(e,t),s=(i.weekday+6)%7,r=new Date(Date.UTC(i.year,i.month-1,i.day-s));return kt(r.getUTCFullYear(),r.getUTCMonth()+1,r.getUTCDate(),0,0,0,t)}function Tt(e,t=ht){const i=pt(e,t);return kt(i.year,i.month,1,0,0,0,t)}function Ct(e,t=ht){const i=pt(e,t);return`${et(i.hour)}:${et(i.minute)}`}function jt(e,t=ht){const i=pt(e,t);return`${et(i.hour)}:${et(i.minute)}:${et(i.second)}`}function At(e,t=ht){const i=pt(e,t);return`${et(i.day)} ${rt[i.month-1]}`}function Lt(e,t=ht){const i=pt(e,t);return`${i.year}-${et(i.month)}-${et(i.day)}`}function Nt(e,t=ht){const i=pt(e,t);let s=`${ot[i.weekday]} ${et(i.day)} ${rt[i.month-1]} '${String(i.year).slice(-2)}`;return 0===i.hour&&0===i.minute&&0===i.second||(s+=` ${et(i.hour)}:${et(i.minute)}`,0!==i.second&&(s+=`:${et(i.second)}`)),s}function Rt(e){const t=function(e){const t=[];for(let i=1;i<e.length;i++){const s=e[i]-e[i-1];s>0&&t.push(s)}return 0===t.length?0:(t.sort((e,t)=>e-t),t[t.length>>1])}(e);if(t<=0||t>=86400)return null;const i=Math.max(4*t,14400),s=[];for(let t=1;t<e.length;t++)e[t]-e[t-1]>=i&&s.push(t);if(0===s.length)return null;const r=[e[0],...s.map(t=>e[t])],n=[];for(let e=1;e<r.length;e++)n.push(r[e]-r[e-1]);return n.sort((e,t)=>e-t),n[n.length>>1]>129600?null:s}function Ft(e,t=ht){const i=new Array(e.length).fill(!1),s=Rt(e);if(null===s){const s=t===ht?lt:(e,i)=>vt(e,i,t);for(let t=1;t<e.length;t++)i[t]=s(e[t-1],e[t]);return i}for(const e of s)i[e]=!0;return i}function zt(e,t){const i=new Array(e.length).fill(!1),s=Rt(e);if(null===s){for(let s=1;s<e.length;s++)i[s]=t(e[s-1],e[s]);return i}let r=e[0];for(const n of s)t(r,e[n])&&(i[n]=!0),r=e[n];return i}const Et=/^\s*(\d{2})(\d{2})\s*-\s*(\d{2})(\d{2})(?:\s*:\s*([1-7]+))?\s*$/;function Bt(e,t){const i=Number(e),s=Number(t);return i>23||s>59?-1:60*i+s}function $t(e){const t=Et.exec(e);if(null===t)return null;const i=Bt(t[1],t[2]),s=Bt(t[3],t[4]);return i<0||s<0?null:void 0===t[5]?{start:i,end:s}:{start:i,end:s,days:[...t[5]].map(Number)}}function Dt(e,t){const i=60*e.hour+e.minute,s=t.end<=t.start,r=i<t.end;if(!(s?i>=t.start||r:i>=t.start&&r))return!1;if(void 0===t.days)return!0;const n=s&&r?(e.weekday+6)%7:e.weekday;return t.days.includes(n+1)}function Wt(e,t,i=ht){return Dt(pt(e,i),t)}function qt(e,t,i=ht){const s=new Array(e.length).fill(!1),r="string"==typeof t?$t(t):t;if(null===r)return s;for(let t=0;t<e.length;t++)s[t]=Dt(pt(e[t],i),r);return s}const Vt={textColor:"#8b91a7",lineColor:"#2a3046",font:"11px system-ui, sans-serif"};const Ht=90,Gt=30,Ut=10;function Kt(e,t=0){const i=new Array(e.length).fill(!1),s=e.map((e,t)=>t);s.sort((t,i)=>e[i].priority-e[t].priority||t-i);const r=[];for(const n of s){const s=e[n];if(!Number.isFinite(s.y)||!Number.isFinite(s.height))continue;let o=!0;for(const e of r)if(Math.abs(e.y-s.y)<(e.height+s.height)/2+t){o=!1;break}o&&(i[n]=!0,r.push(s))}return i}function Yt(e,t,i){const s=8*i;return e>=s&&e<=t-s}function Xt(e,t,i,s,r=Vt,n,o="right"){const a=t.ticks((l=i.plotHeight,!Number.isFinite(l)||l<=0?2:Math.max(2,Math.min(30,Math.round(l/32)))));var l;const h=function(e,t,i,s){return void 0===s||0===s.length?null:Kt([...e.map(e=>({y:Math.round(t.priceToY(e)*i),height:16*i,priority:Ut})),...s],2*i).slice(0,e.length)}(a,t,s,n),c="left"===o,d=Math.round((c?i.plotLeft:i.plotWidth)*s),u=c?d-.5:d+.5,_=c?d-6*s:d+6*s;e.save(),e.strokeStyle=r.lineColor,e.fillStyle=r.textColor,e.font=_i(r.font,s),e.textAlign=c?"right":"left",e.textBaseline="middle",e.lineWidth=1,e.beginPath(),e.moveTo(u,0),e.lineTo(u,Math.round(i.plotHeight*s)),e.stroke();for(let r=0;r<a.length;r++){if(null!==h&&!h[r])continue;const n=a[r],o=Math.round(t.priceToY(n)*s);Yt(o,i.plotHeight*s,s)&&e.fillText(t.format(n),_,o)}e.restore()}function Jt(e,t,i,s){const r=s?"timeWithSeconds":"time";if(i===ht){const i=Qe(e),s=Qe(t);return i.year!==s.year?"year":i.month!==s.month?"month":i.day!==s.day?"day":r}return St(e,t,i)?"year":wt(e,t,i)?"month":vt(e,t,i)?"day":r}function Zt(e,t){const i=Qt(e);return i!=i||i!==Qt(t)}function Qt(e){const t=Math.trunc(1e3*(e+Xe));return Math.abs(t)<=864e13?Math.floor(t/864e5):NaN}function ei(e,t,i){const s=[];for(let r=e;r<=t;r+=i)s.push(r);return s}const ti={up:"#26a69a",down:"#ef5350",text:"#0d0e12"};function ii(e,t=64){const i=e.baseIndex,s=[];let r;for(let n=Math.max(0,i-t);n<=i;n++){const t=e.indexToTime(n);void 0!==t&&(void 0!==r&&t-r>0&&s.push(t-r),r=t)}return 0===s.length?0:(s.sort((e,t)=>e-t),s[s.length>>1])}function si(e,t,i){if(!Number.isFinite(t)||t<=0)return null;if(!Number.isFinite(e)||!Number.isFinite(i))return null;const s=i-e;return s<0?t-s:t-s%t}const ri=e=>e<10?`0${e}`:String(e);function ni(e){if(null===e||!Number.isFinite(e))return"--:--:--";const t=Math.max(0,Math.ceil(e));return`${ri(Math.floor(t/3600))}:${ri(Math.floor(t%3600/60))}:${ri(t%60)}`}function oi(e,t=!1){return(t?28:16)*e}function ai(e,t,i,s){return!Number.isFinite(e)||e<0||e>t?null:"right"===s?e:t<i?null:Math.max(i/2,Math.min(t-i/2,e))}function li(e,t,i,s,r,n,o,a){const l=Math.round(r.plotLeft*n)-1;if(!(l>0))return;const h=Math.min(6*n,l/4),c=s.map(t=>(e.font=t.font,e.measureText(t.text).width)),d=Math.min(l,Math.max(...c)+2*h),u=-1-d;e.fillStyle=o,e.fillRect(u,t-i/2,d,i),e.fillStyle=a;for(let r=0;r<s.length;r++){const o=s[r],a=Number(/(^|[^\d.])(\d{1,5}(?:\.\d{1,4})?)px/.exec(o.font)?.[2]),l=a>0?(i/s.length-2*n)/a:1,_=c[r]>0?(d-2*h)/c[r]:1;e.font=_i(o.font,Math.min(1,_,l)),e.fillText(o.text,u+h,t+o.offset)}}function hi(e,t,i,s,r,n,o,a){const l=e.measureText(n).width;e.fillStyle=o,e.fillRect(t+1,i-s/2,l+2*r,s),e.fillStyle=a,e.fillText(n,t+1+r,i)}function ci(e,t,i,s,r,n,o=Vt,a="right"){if(!Number.isFinite(i))return;const l=ai(Math.round(t.priceToY(i)*n),r.plotHeight*n,oi(n),a);null!==l&&(e.save(),e.font=_i(o.font,n),e.textAlign="left",e.textBaseline="middle","left"===a?li(e,l,oi(n),[{text:t.format(i),font:e.font,offset:0}],r,n,s,Q(s)):hi(e,Math.round(r.plotWidth*n),l,oi(n),6*n,t.format(i),s,Q(s)),e.restore())}function di(e,t,i,s,r,n,o=Vt,{colors:a=ti,countdown:l,side:h="right"}={}){const c=Math.round(t.priceToY(i)*n);if(!Number.isFinite(c)||c<0||c>r.plotHeight*n)return;const d=s?a.up:a.down,u=Math.round(r.plotWidth*n);e.save();const _=t.format(i),p=void 0!==l&&!0===l.visible,f=_i(o.font,n);e.font=f;const m=6*n,b=oi(n,p);if(e.textAlign="left",e.textBaseline="middle","left"===h){const t=ai(c,r.plotHeight*n,b,h);if(null!==t){const i=[{text:_,font:f,offset:p?-b/4:0}];p&&i.push({text:ni(si(l.lastBarTime,l.intervalSec,l.now())),font:_i(o.font,.9*n),offset:b/4}),li(e,t,b,i,r,n,d,a.text)}}else if(p){const t=ni(si(l.lastBarTime,l.intervalSec,l.now())),i=_i(o.font,.9*n),s=e.measureText(_).width;e.font=i;const r=e.measureText(t).width;e.fillStyle=d,e.fillRect(u+1,c-b/2,Math.max(s,r)+2*m,b),e.fillStyle=a.text,e.font=f,e.fillText(_,u+1+m,c-b/4),e.font=i,e.fillText(t,u+1+m,c+b/4)}else hi(e,u,c,b,m,_,d,a.text);e.restore()}function ui(e,t,i,s,r=Vt){if(!0!==s.visible)return;if(t.timeAxisHeight<=0||t.priceAxisWidth<=0)return;const n=s.now();if(!Number.isFinite(n))return;const o=Math.floor(n),a=void 0!==s.timezone&&s.timezone!==ht&&_t(s.timezone)?s.timezone:ht,l=a===ht,h=l?it(o):jt(o,a),c=l?Xe:gt(o,a),d=Math.round((t.plotWidth+t.priceAxisWidth/2)*i),u=Math.round(t.plotHeight*i),_=t.timeAxisHeight*i,p=!1!==s.showOffset&&t.timeAxisHeight>=20;e.save(),e.fillStyle=r.textColor,e.textAlign="center",e.textBaseline="middle",e.font=_i(r.font,i),p?(e.fillText(h,d,u+.3*_),e.font=_i(r.font,.82*i),e.fillText(function(e){const t=Math.round(e/60);if(0===t)return"UTC";const i=Math.abs(t),s=i%60;return`UTC${t<0?"-":"+"}${Math.floor(i/60)}${0===s?"":`:${ri(s)}`}`}(c),d,u+.74*_)):e.fillText(h,d,u+_/2),e.restore()}function _i(e,t){return e.replace(/(^|[^\d.])(\d{1,5}(?:\.\d{1,4})?)px/,(e,i,s)=>`${i}${Number(s)*t}px`)}function pi(e,t,i){return{color:t?.color??e.crosshair,width:t?.width??e.crosshairWidth??1,dash:z(t?.style??e.crosshairStyle??"dashed",i)}}function fi(e,t,i,s,r,n,o,a=1,l=[4*n,4*n]){if(e.save(),e.strokeStyle=o,e.lineWidth=Math.max(1,a),e.setLineDash(l),t>=0&&t<=s){const i=Math.round(t*n)+.5;e.beginPath(),e.moveTo(i,0),e.lineTo(i,Math.round(r*n)),e.stroke()}if(null!==i&&i>=0&&i<=r){const t=Math.round(i*n)+.5;e.beginPath(),e.moveTo(0,t),e.lineTo(Math.round(s*n),t),e.stroke()}e.setLineDash([]),e.restore()}const mi="ui-sans-serif, system-ui, sans-serif",bi={},gi={};function vi(e){const t=e.iconSize;return"number"==typeof t&&Number.isFinite(t)?Math.max(12,Math.min(28,t)):16}function yi(e){return Math.max(18,vi(e)+2)}const wi=e=>e.parts.reduce((e,t)=>e+t.w+t.gap,0);function xi(e,t,i){return"ohlc"===t?!1!==e.chartValues:"change"===t?!1!==e.barChange:"volume"===t?!1!==e.volume:"openInterest"===t?!0===e.openInterest&&!1!==i:!1!==e.lastValueLabel}const Si=["hide","settings","close"];function Pi(e,t,i,s,r,n,o){const a=.36*r*n,l=Math.max(1,Math.round(1.4*r*n/16));switch(e.save(),e.strokeStyle=e.fillStyle,e.lineWidth=l,e.lineCap="round",e.lineJoin="round",e.beginPath(),t){case"up":e.moveTo(i,s+a),e.lineTo(i,s-a),e.moveTo(i-.62*a,s-.35*a),e.lineTo(i,s-a),e.lineTo(i+.62*a,s-.35*a);break;case"down":e.moveTo(i,s-a),e.lineTo(i,s+a),e.moveTo(i-.62*a,s+.35*a),e.lineTo(i,s+a),e.lineTo(i+.62*a,s+.35*a);break;case"hide":e.moveTo(i-a,s),e.quadraticCurveTo(i,s-1.15*a,i+a,s),e.quadraticCurveTo(i,s+1.15*a,i-a,s),e.stroke(),e.beginPath(),e.arc(i,s,.34*a,0,2*Math.PI),!0===o.hidden&&(e.moveTo(i-a,s+.85*a),e.lineTo(i+a,s-.85*a));break;case"maximize":!0===o.maximized?(e.moveTo(i-a,s-.25*a),e.lineTo(i-.25*a,s-.25*a),e.lineTo(i-.25*a,s-a),e.moveTo(i+a,s+.25*a),e.lineTo(i+.25*a,s+.25*a),e.lineTo(i+.25*a,s+a)):(e.moveTo(i-a,s-.4*a),e.lineTo(i-a,s-a),e.lineTo(i-.4*a,s-a),e.moveTo(i+.4*a,s-a),e.lineTo(i+a,s-a),e.lineTo(i+a,s-.4*a),e.moveTo(i+a,s+.4*a),e.lineTo(i+a,s+a),e.lineTo(i+.4*a,s+a),e.moveTo(i-.4*a,s+a),e.lineTo(i-a,s+a),e.lineTo(i-a,s+.4*a));break;case"collapse":{const t=!0===o.collapsed?.6*a:0;e.moveTo(i-a,s-.7*a),e.lineTo(i+a,s-.7*a),e.moveTo(i-.6*a,s+.6*a-t),e.lineTo(i,s+t),e.lineTo(i+.6*a,s+.6*a-t);break}case"settings":e.arc(i,s,.46*a,0,2*Math.PI);for(let t=0;t<8;t++){const r=t*Math.PI/4,n=Math.cos(r),o=Math.sin(r);e.moveTo(i+n*a*.68,s+o*a*.68),e.lineTo(i+n*a,s+o*a)}break;case"source":{const t=.62*a,r=.34*a,n=.28*a;for(const o of[-1,1]){const l=i+o*t;e.moveTo(l-o*r,s-a),e.quadraticCurveTo(l,s-a,l,s-.55*a),e.lineTo(l,s-.2*a),e.quadraticCurveTo(l,s,l+o*n,s),e.quadraticCurveTo(l,s,l,s+.2*a),e.lineTo(l,s+.55*a),e.quadraticCurveTo(l,s+a,l-o*r,s+a)}break}case"close":e.moveTo(i-.8*a,s-.55*a),e.lineTo(i+.8*a,s-.55*a),e.moveTo(i-.3*a,s-.55*a),e.lineTo(i-.3*a,s-.85*a),e.lineTo(i+.3*a,s-.85*a),e.lineTo(i+.3*a,s-.55*a),e.moveTo(i-.6*a,s-.55*a),e.lineTo(i-.45*a,s+.85*a),e.lineTo(i+.45*a,s+.85*a),e.lineTo(i+.6*a,s-.55*a)}e.stroke(),e.restore()}class Oi{constructor(e){Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_values",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_buttons",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_width",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_plotWidth",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_plotHeight",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_suppressed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._opts={font:11,left:8,top:6,row:0,...e}}attached(e){this._host=e}detached(){this._host=null}zOrder(){return"top"}autoscaleInfo(){return null}setSuppressed(e){this._suppressed!==e&&(this._suppressed=e,e&&this._clearGeometry(),this._host?.requestUpdate())}_clearGeometry(){this._buttons=[],this._width=this._plotWidth=this._plotHeight=0}setValue(e,t){this.setValues(""===e?[]:[{text:e,color:t}])}setValues(e){e.length===this._values.length&&e.every((e,t)=>e.text===this._values[t].text&&e.label===this._values[t].label&&e.color===this._values[t].color&&e.field===this._values[t].field&&e.priority===this._values[t].priority)||(this._values=e.map(e=>({...e})),this._host?.requestUpdate())}setOptions(e){const t=this._opts.statusLine;this._opts={...this._opts,...e},void 0!==e.statusLine&&(this._opts.statusLine={...t,...e.statusLine}),!1===this._opts.visible&&this._clearGeometry(),this._host?.requestUpdate()}_status(){const e=this._opts.status;return void 0===e?gi:("function"==typeof e?e():e)??gi}options(){return this._opts}draw(e,t){const i=this._opts;if(this._suppressed||!1===i.visible)return void this._clearGeometry();const s=t.dpr,r=(i.font??11)*s,n=vi(i),o=yi(i),a=((i.top??6)+(i.row??0)*o)*s,l=a+o*s/2,h=(i.left??8)*s;let c=h;const d=!0===i.hidden,u=i.statusLine??bi,_=this._status(),p=ie(t.theme.axisText,.55);this._plotWidth=Math.max(0,t.plotWidth),this._plotHeight=Math.max(0,t.plotHeight);const f=Math.max(0,(this._plotWidth-4)*s-h),m="string"==typeof t.hoverId&&t.hoverId.startsWith(`${i.id}::`),b=Math.max(0,Math.floor((f/s-6)/(n+2))),g=m&&b>0?(i.actions??Si).slice(-b):[],v=g.length?(g.length*(n+2)+6)*s:0;e.save(),e.beginPath(),e.rect(0,0,this._plotWidth*s,t.plotHeight*s),e.clip(),e.textBaseline="middle",e.textAlign="left",e.globalAlpha=d?.45:1;const y=[],w=(t,i,n,o)=>(e.font=`${n?"600 ":""}${r}px ${mi}`,{k:"text",text:t,color:i,bold:n,w:e.measureText(t).width,gap:o*s}),x=(e,t,i=1)=>{const s=[];void 0!==e.label&&""!==e.label&&s.push(w(e.label,p,!1,3)),s.push(w(e.text,e.color??t,!1,6)),y.push({parts:s,priority:Number.isFinite(e.priority)?e.priority:i,reading:i>0})};let S;if(void 0!==_.logo&&!1!==u.logo&&y.push({parts:[{k:"logo",img:_.logo,w:12*s,gap:5*s}],priority:-1}),void 0!==i.color&&y.push({parts:[{k:"dot",color:i.color,w:6*s,gap:5*s}],priority:-1}),!1!==u.title){const e=u.titleMode??"symbol",s="description"===e?_.description:"ticker"===e?_.ticker:void 0;S=w(s??i.title,t.theme.axisText,!0,6),y.push({parts:[S],priority:0})}void 0!==i.params&&""!==i.params&&y.push({parts:[w(i.params,p,!1,6)],priority:-3}),void 0!==_.marketStatus&&!1!==u.marketStatus&&x(_.marketStatus,p,-4);const P=i.valueColor??i.color??t.theme.axisText;for(const e of this._values)xi(u,e.field,i.hasOpenInterest)&&x(e,P);void 0!==_.lastDayChange&&!1!==u.lastDayChange&&x(_.lastDayChange,P,-2),e.font=`600 ${r}px ${mi}`;const O=function(e,t,i,s){if(t.reduce((e,t)=>e+wi(t),0)<=s)return t.flatMap(e=>e.parts);const r=t.filter(e=>!e.parts.includes(i)).sort((e,t)=>t.priority-e.priority);if("text"===i?.k){const t=r.find(e=>e.reading),n=t?wi(t):0,o=n<=.75*s?n:0,a=Math.max(0,s-o-i.gap);if(i.w>a){const t=Array.from(i.text);let s=0,r=t.length;for(;s<r;){const i=Math.ceil((s+r)/2);e.measureText(t.slice(0,i).join("")+"...").width<=a?s=i:r=i-1}i.text=e.measureText("...").width<=a?t.slice(0,s).join("")+"...":"",i.w=e.measureText(i.text).width,i.text||(i.gap=0)}s-=i.w+i.gap}const n=new Set;for(const e of r){const t=wi(e);t<=s&&(n.add(e),s-=t)}return t.filter(e=>e.parts.includes(i)||n.has(e)).flatMap(e=>e.parts)}(e,y,S,Math.max(0,f-v)),k=O[O.length-1];if(!0===u.background&&void 0!==k){const i=4*s,r=O.reduce((e,t)=>e+t.w+t.gap,0)-k.gap+2*i;e.globalAlpha=1,e.fillStyle=ie(u.backgroundColor??t.theme.background,u.backgroundOpacity??.8),e.beginPath(),e.roundRect(h-i,l-o/2*s,r,o*s,4*s),e.fill(),e.globalAlpha=d?.45:1}for(const t of O)"text"===t.k?(e.font=`${t.bold?"600 ":""}${r}px ${mi}`,e.fillStyle=t.color,e.fillText(t.text,c,l)):"dot"===t.k?(e.fillStyle=t.color,e.beginPath(),e.arc(c+3*s,l,3*s,0,2*Math.PI),e.fill()):e.drawImage(t.img,c,l-t.w/2,t.w,t.w),c+=t.w+t.gap;if(this._buttons=[],g.length>0){const r=(g.length*(n+2)+6)*s;e.globalAlpha=1,e.fillStyle=ie(t.theme.background,.82),e.beginPath(),e.roundRect(c-3*s,l-o/2*s,r,o*s,5*s),e.fill(),c+=2*s;for(const r of g){const o=`${i.id}::${r}`,h=c;this._buttons.push({id:o,x:h/s,y:a/s});const u=t.hoverId===o;u&&(e.fillStyle=ie(t.theme.axisText,.16),e.beginPath(),e.roundRect(h,l-n/2*s,n*s,n*s,4*s),e.fill()),e.globalAlpha=d?.5:u?1:.75,e.fillStyle="close"===r&&u?"#ff6b6b":t.theme.axisText,Pi(e,r,h+n/2*s,l,n,s,i),e.globalAlpha=1,c+=(n+2)*s}}this._width=c/s,e.restore()}hitTest(e,t){if(this._suppressed||!1===this._opts.visible)return null;if(e<0||e>=this._plotWidth||t<0||t>=this._plotHeight)return null;const i=this._opts,s=yi(i),r=(i.top??6)+(i.row??0)*s;if(t<r||t>r+s)return null;const n=vi(i);for(const t of this._buttons)if(e>=t.x&&e<=t.x+n)return{externalId:t.id,zOrder:"top",distance:0,cursor:"pointer"};const o=Math.max(this._width,(i.left??8)+40)+130;return e>=(i.left??8)-4&&e<=o?{externalId:`${i.id}::row`,zOrder:"top",distance:0,cursor:"default"}:null}}function ki(e){return"canvas2d"===e.kind||void 0===e.device?null:e.device.lost?"context-lost":e.device.available?null:"unavailable"}const Ii=new Map;function Mi(e,t){Ii.set(e,t)}function Ti(e){return"canvas2d"!==e&&Ii.delete(e)}function Ci(){return Array.from(Ii.keys())}function ji(e="canvas2d"){const t=Ii.get("canvas2d");if(void 0===t)throw new Error("openalgo-charts: no canvas2d render backend is registered");if("auto"===e){const e=Ii.get("webgl2");return void 0===e?t:()=>e()??t()}const i=Ii.get(e);if(void 0===i)throw new Error(`openalgo-charts: render backend "${e}" is not registered; import the tier that provides it first`);return"canvas2d"===e?i:()=>i()??t()}function Ai(e="canvas2d"){const t=ji(e)();if(null===t)throw new Error("openalgo-charts: the canvas2d render backend factory returned null");return t}class Li{constructor(){Object.defineProperty(this,"kind",{enumerable:!0,configurable:!0,writable:!0,value:"canvas2d"}),Object.defineProperty(this,"_canvas",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_ctx",{enumerable:!0,configurable:!0,writable:!0,value:null})}mount(e,t){if(this._canvas=e,this._ctx=t??e.getContext("2d"),null===this._ctx)throw new Error("openalgo-charts: 2D canvas context is not available")}resize(e,t,i){}beginFrame(e){e&&null!==this._ctx&&null!==this._canvas&&(this._ctx.setTransform(1,0,0,1,0,0),this._ctx.clearRect(0,0,this._canvas.width,this._canvas.height))}drawSeries(e,t,i,s,r,n,o){null!==this._ctx&&e.draw(this._ctx,t,i,s,r,n,o)}endFrame(){}overlay2d(){return this._ctx}destroy(){this._ctx=null,this._canvas=null}}function Ni(e,t){if("string"==typeof e.color)return e.color;const i=t?e.upColor:e.downColor;return"string"==typeof i?i:"string"==typeof e.closeColor?e.closeColor:void 0}Mi("canvas2d",()=>new Li);const Ri={bottom:0,normal:2,top:3},Fi={start:0,lastTime:0},zi={kind:"ohlc",dpr:1,factor:1},Ei=new Set(Ae().map(je));class Bi{constructor(e,t=new Li){Object.defineProperty(this,"element",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"base",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"top",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_separator",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_backend",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_rightScale",{enumerable:!0,configurable:!0,writable:!0,value:new y}),Object.defineProperty(this,"_leftScale",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_overlayScales",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_axisLayout",{enumerable:!0,configurable:!0,writable:!0,value:new F}),Object.defineProperty(this,"_ratioLocks",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"weight",{enumerable:!0,configurable:!0,writable:!0,value:1}),Object.defineProperty(this,"_series",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_primitives",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_primitiveScales",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_stackAbove",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_source",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_width",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_height",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_drawItems",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_hitBoxes",{enumerable:!0,configurable:!0,writable:!0,value:Ke()}),Object.defineProperty(this,"_hitEpoch",{enumerable:!0,configurable:!0,writable:!0,value:0}),this.element=e.createElement("div"),this.element.style.position="relative",this.element.style.width="100%",this.element.style.flex="1 1 auto",this.element.style.overflow="hidden",this.element.style.borderTopStyle="solid",this.element.style.borderTopWidth="0px",this.element.style.boxSizing="border-box",this.base=new d(e,0),this.top=new d(e,1),this.element.appendChild(this.base.element),this.element.appendChild(this.top.element);const i=e.createElement("div"),s=i.style;s.position="absolute",s.left="0",s.top="0",s.width="100%",s.height="0px",s.zIndex="2",s.pointerEvents="none",s.display="none",this._separator=i,this.element.appendChild(i),this._backend=t,t.mount(this.base.element,this.base.ctx)}get backend(){return this._backend}setBackend(e){e!==this._backend&&(this._backend.destroy(),this._backend=e,e.mount(this.base.element,this.base.ctx),this._width>0&&this._height>0&&e.resize(this._width,this._height,this.base.pixelRatio))}get backendDegradation(){return ki(this._backend)}get priceScale(){return this._rightScale}addSeries(e){this._scaleFor(e.scaleId),this._series.push(e),this._hitEpoch++}_scaleFor(e){if(this._axisLayout.register(e),"left"===e)return this._leftScale??(this._leftScale=new y);if(""===e||e.startsWith("overlay:")){let t=this._overlayScales.get(e);return t||(t=new y,this._overlayScales.set(e,t)),t}return this._rightScale}scaleFor(e){return this._scaleFor(e)}usesScale(e){if(this._series.some(t=>t.scaleId===e))return!0;for(const t of this._primitiveScales.values())if(t===e)return!0;return!1}axisPlacement(e){return this._axisLayout.get(e)}setAxisPlacement(e,t,i){return!!this._axisLayout.set(e,t,i)&&(this._hitEpoch++,this._retainPlacedScales(),!0)}restoreAxisPlacements(e){this._axisLayout.restore(e),this._hitEpoch++,this._retainPlacedScales()}_retainPlacedScales(){for(const{scaleId:e}of this._axisLayout.entries())this._axisLayout.configured(e)&&this._scaleFor(e)}visibleAxes(e=!0){const t=e&&0===this._series.length&&0===this._primitiveScales.size;return this._axisLayout.entries().filter(e=>"hidden"!==e.side&&(this.usesScale(e.scaleId)||t&&"right"===e.scaleId)).sort((e,t)=>e.side.localeCompare(t.side)||e.order-t.order)}axisSlots(e){if(!0===e.collapsed)return[];const t=this._layout(e),i={left:0,right:0};return this.visibleAxes(e.emptyPriceAxis).map(s=>{const r=e.axisColumnWidth??("left"===s.side?t.plotLeft:t.priceAxisWidth),n=i[s.side]++;return{...s,width:r,x:"left"===s.side?-(n+1)*r:t.plotWidth+n*r}}).filter(e=>e.width>0)}moveSeriesScale(e,t){if(e===t||!this.usesScale(e)||this.usesScale(t))return!1;this._hitEpoch++;const i=this._scaleFor(e),s=this._scaleFor(t);this._axisLayout.set(t,t),this._axisLayout.set(e,e),"left"===t?(this._leftScale=i,this._rightScale=s):(this._rightScale=i,this._leftScale=s);for(const i of this._series)i.scaleId===e&&(i.scaleId=t);for(const[i,s]of this._primitiveScales)s===e&&this._primitiveScales.set(i,t);s.setFixedRange(null),s.setAutoScale(!0),s.reset();const r=this._ratioLocks.get(e);return void 0!==r&&(this._ratioLocks.delete(e),this._ratioLocks.set(t,r)),!0}ratioLocked(e){return this._ratioLocks.has(e)}setRatioLock(e,t,i,s){if(!t)return this._ratioLocks.delete(e),!0;const r=this._scaleFor(e);return!!(r.scaled&&i>0&&s>0)&&(r.setAutoScale(!1),this._ratioLocks.set(e,{barSpacing:i,height:s}),!0)}clearRatioLocks(){this._ratioLocks.clear()}scaleOf(e){return this._scaleFor(e.scaleId)}scales(){const e=[this.priceScale];return null!==this._leftScale&&e.push(this._leftScale),e.push(...this._overlayScales.values()),e}scaleStates(){const e=[["right",this.priceScale]];return null!==this._leftScale&&e.push(["left",this._leftScale]),e.push(...this._overlayScales),Object.fromEntries(e.map(([e,t])=>{const i=t.options,s={marginTop:i.marginTop,marginBottom:i.marginBottom,minMove:i.minMove,minPrecision:i.minPrecision,mode:i.mode,inverted:i.inverted,autoScale:t.autoScale,fixedRange:t.fixedRange,placement:this._axisLayout.get(e)};t.autoScale||(s.range=t.priceRange());const r=this._ratioLocks.get(e);return r&&!t.autoScale&&(s.ratioLock={...r}),[e,s]}))}axisScales(){return this._axisLayout.entries().filter(e=>"hidden"!==e.side).map(e=>this._scaleFor(e.scaleId))}hasLeftScale(){return null!==this._leftScale&&this.usesScale("left")}removeSeries(e){const t=this._series.indexOf(e);if(t<0)return!1;if(this._series.splice(t,1),this._drawItems.delete(e),this._hitEpoch++,!this.usesScale(e.scaleId)){const t=this._scaleFor(e.scaleId);t.reset(),""===e.scaleId||"hidden"!==this._axisLayout.get(e.scaleId).side||this._axisLayout.configured(e.scaleId)||t.hasConfiguration()||this._ratioLocks.has(e.scaleId)||this._overlayScales.delete(e.scaleId)}return!0}series(){return this._series}reorderSeries(e){const t=new Set(e),i=e.filter(e=>this._series.includes(e));this._hitEpoch++;let s=0;for(let e=0;e<this._series.length;e++)t.has(this._series[e])&&(this._series[e]=i[s++])}setSourceSeries(e){this._source=e,this._hitEpoch++}_shownSource(){const e=this._source;return null!==e&&!1!==e.style.visible&&je(e.type).isPriceSeries&&this._series.includes(e)?e:void 0}moveSeries(e,t){const i=this._series.indexOf(e);if(i<0||e===t)return;this._hitEpoch++,this._series.splice(i,1);const s=null===t?-1:this._series.indexOf(t);this._series.splice(s<0?this._series.length:s,0,e)}reorderPrimitives(e){const t=new Set(e),i=e.filter(e=>this._primitives.includes(e));this._hitEpoch++;let s=0;for(let e=0;e<this._primitives.length;e++)t.has(this._primitives[e])&&(this._primitives[e]=i[s++])}transferPrimitive(e,t){const i=this._primitives.indexOf(e);if(i<0||t===this||t._destroyed||t.hasPrimitive(e))return!1;const s=this._primitiveScales.get(e);void 0!==s&&t._scaleFor(s);const r=this._stackAbove.get(e);return this._primitives.splice(i,1),this._primitiveScales.delete(e),this._stackAbove.delete(e),this._hitEpoch++,t._hitEpoch++,t._primitives.push(e),void 0!==s&&t._primitiveScales.set(e,s),void 0!==r&&t._stackAbove.set(e,r),!0}setPrimitiveStackAbove(e,t){return!(this._destroyed||!this.hasPrimitive(e))&&(null===t?this._stackAbove.delete(e):this._stackAbove.set(e,t),!0)}primitiveStackAbove(e){return this._stackAbove.get(e)??null}_slotted(e,t){if(0===this._stackAbove.size||void 0===t.stackSlot)return null;let i=null;for(const s of e){const e=this._stackAbove.get(s),r=void 0===e?void 0:t.stackSlot(e);void 0!==r&&this._series.includes(r)&&(i??(i=new Map)).set(s,r)}return i}paintsBelowSeries(e,t,i){const s=this._stackAbove.get(e),r=void 0===s?void 0:i.stackSlot?.(s),n=void 0===r?-1:this._series.indexOf(r);return n>=0?n<this._series.indexOf(t):"bottom"===e.zOrder()}bindPrimitiveScale(e,t){return!(this._destroyed||!this.hasPrimitive(e))&&(!(null!==t&&!L(t))&&(this.primitiveScaleId(e)!==t&&(this._hitEpoch++,null===t?this._primitiveScales.delete(e):(this._scaleFor(t),this._primitiveScales.set(e,t)),!0)))}primitiveScaleId(e){return this._primitiveScales.get(e)??null}primitives(){return this._primitives}addPrimitive(e,t){this._primitives.push(e),this._hitEpoch++,e.attached?.(void 0===e.hitBounds?t:function(e){return{requestUpdate:()=>{Ue++,e.requestUpdate()}}}(t))}hasPrimitive(e){return this._primitives.includes(e)}removePrimitive(e){const t=this._primitives.indexOf(e);return!(t<0)&&(this._primitives.splice(t,1),this._primitiveScales.delete(e),this._stackAbove.delete(e),this._hitEpoch++,e.detached?.(),!0)}destroy(){this._destroyed=!0,this._primitiveScales.clear(),this._stackAbove.clear(),this._drawItems.clear(),this._hitBoxes.clear();for(const e of this._primitives)e.detached?.();this._primitives.length=0,this._backend.destroy(),this.element.remove()}_primitiveContext(e){const t=this._layout(e),i=this.axisSlots(e).find(e=>"right"===e.scaleId);return{timeScale:e.timeScale,priceScale:this.priceScale,readoutPriceScale:this._readoutScale(),dataLayer:e.dataLayer,plotWidth:t.plotWidth,plotHeight:t.plotHeight,priceAxisWidth:i?.width??0,priceAxisSide:i?.side??"hidden",priceAxisOffset:i?i.x+("left"===i.side?i.width:0):void 0,dpr:e.dpr,theme:e.theme,hoverId:e.hoverId??null,hoverKey:e.hoverKey??null,dragId:e.dragId??null,bars:()=>{for(const t of null!==this._source&&this._series.includes(this._source)?[this._source,...this._series]:this._series)if(je(t.type).isPriceSeries)return e.dataLayer.seriesBars(t.dataId);return[]}}}_boundPrimitiveContext(e,t,i){const s=this._primitiveScales.get(e);if(void 0===s)return t;const r=this.axisSlots(i).find(e=>e.scaleId===s);return{...t,priceScale:this._scaleFor(s),priceAxisSide:r?.side??"hidden",priceAxisWidth:r?.width??0,priceAxisOffset:r?r.x+("left"===r.side?r.width:0):void 0}}_live(e){return!0===e.collapsed?this._primitives.filter(e=>e instanceof Oi):this._primitives}hitTestPrimitives(e,t,i,s){const r=this._primitiveContext(i),n=this._live(i),o=this._slotted(n,i);this._checkHitBoxes(i);let a=null,l=0;for(let h=0;h<n.length;h++){const c=n[h];if(!c.hitTest||c===s)continue;let d,u=this._hitBoxes.kept(h);if(void 0===u&&(u=this._hitBoxes.measure(h,c,d=this._boundPrimitiveContext(c,r,i))),!Ye(u,e,t))continue;d??(d=this._boundPrimitiveContext(c,r,i));let _=c.hitTest(e,t,d);if(null===_)continue;this._primitiveScales.has(c)&&(_={..._,priceScale:d.priceScale});const p=_.paintedBy??c,f=o?.get(p);void 0!==f&&(_={..._,paintedBy:p});const m=void 0!==f?1+(this._series.indexOf(f)+1)/(this._series.length+1):Ri[p===c?_.zOrder:p.zOrder()],b=+(m>=2)-+(l>=2);if((null===a||b>0||0===b&&(_.distance<a.distance||_.distance===a.distance&&m>l))&&(a=_,l=m,0===a.distance&&l>=Ri.top))break}return a}_paintedHitBoxes(e){this._checkHitBoxes(e),this._hitBoxes.painted()}_checkHitBoxes(e){const t=this._hitBoxes,i=e.timeScale,s=e.dataLayer;t.begin(),t.number(this._hitEpoch),t.number(e.dpr),t.number(this._width),t.number(this._height),t.number(e.priceAxisWidth),t.number(e.leftAxisWidth??0),t.number(e.axisColumnWidth??-1),t.number(e.showTimeAxis?e.timeAxisHeight:-1),t.number(!0===e.collapsed?1:0),t.number(!1===e.emptyPriceAxis?0:1),t.number(i.barSpacing),t.number(i.rightOffset),t.number(i.width),t.number(s.length),t.number(s.indexToTime(0)??NaN),t.number(s.indexToTime(s.baseIndex)??NaN),t.scale(this._rightScale),null!==this._leftScale&&t.scale(this._leftScale);for(const e of this._overlayScales.values())t.scale(e);t.ref(e.hoverId??null),t.ref(e.hoverKey??null),t.ref(e.dragId??null),t.ref(e.theme),t.ref(s.sessionCalendar),t.ref(this._readoutScale()),t.ref(this._rightScale),t.end()}setSeparator(e,t){const i=function(e){return!(e>0)||!Number.isFinite(e)||Number.isInteger(e)}(t)?e:null,s=null===i?e:null,r=this.element.style;r.borderTopWidth=null===i?"0px":"1px",r.borderTopColor=i??"transparent";const n=this._separator.style;n.display=null===s?"none":"",n.height=`${null===s?0:function(e){return e>0&&Number.isFinite(e)?Math.max(1,Math.floor(e))/e:1}(t)}px`,n.background=s??"transparent"}resize(e,t,i){this._width=e,this._height=t,this.base.resize(e,t,i),this.top.resize(e,t,i),this._backend.resize(e,t,i)}setLayoutSize(e,t){this._width=e,this._height=t}setScaleHeights(e){this.priceScale.setHeight(e),this._leftScale?.setHeight(e);for(const t of this._overlayScales.values())t.setHeight(e)}_layout(e){const t=e.leftAxisWidth??0;return{plotWidth:Math.max(0,this._width-e.priceAxisWidth-t),plotHeight:Math.max(0,this._height-(e.showTimeAxis?e.timeAxisHeight:0)),priceAxisWidth:e.priceAxisWidth,timeAxisHeight:e.showTimeAxis?e.timeAxisHeight:0,plotLeft:t}}autoscale(e,t=1){let i=!1;const s=this._layout(e),r=e.timeScale.visibleRange();i=this._autoscaleScale(this.priceScale,"right",e,s.plotHeight,r,t)||i,this._leftScale&&(i=this._autoscaleScale(this._leftScale,"left",e,s.plotHeight,r,t)||i);for(const[n,o]of this._overlayScales)i=this._autoscaleScale(o,n,e,s.plotHeight,r,t)||i;this._ratioLocks.size>0&&this._applyRatioLocks(e.timeScale.barSpacing,s.plotHeight);for(const e of this._primitives)e.afterAutoscale?.();return i}_autoscaleScale(e,t,i,s,r,n){const o=!0===i.priceOnlyAutoScale&&this._series.some(e=>e.dataId===i.primaryDataId&&e.scaleId===t),a=e=>e.scaleId===t&&(!o||e.dataId===i.primaryDataId);e.setHeight(s);const l=e.options.mode;if("percentage"!==l&&"indexed-to-100"!==l||e.setBaseline(this._firstVisibleValue(a,i,r,o)),!e.autoScale)return!1;let h=1/0,c=-1/0;for(const e of this._series){if(!1===e.style.visible||!a(e))continue;const t=je(e.type),s=e.style.barOffset??0,n=i.dataLayer,o=n.seriesBars(e.dataId),l=Fi;if(T(n,o,r.from-s,r.to-s,l))for(let i=l.start;i<o.length;i++){const s=o[i];if(s.time>l.lastTime)break;if(void 0===n.timeToIndex(s.time))continue;const r=t.extents(s,e.style);r.min<h&&(h=r.min),r.max>c&&(c=r.max)}}for(const e of this._primitives){if(o)break;if((this._primitiveScales.get(e)??"right")!==t)continue;const i=e.autoscaleInfo?.();i&&(i.min<h&&(h=i.min),i.max>c&&(c=i.max))}return h<=c&&e.autoscale(h,c,n)}_applyRatioLocks(e,t){if(e>0&&t>0)for(const[i,s]of this._ratioLocks){const r=t/e/(s.height/s.barSpacing);if(s.barSpacing=e,s.height=t,!isFinite(r)||r<=0||Math.abs(r-1)<1e-9)continue;const n=this._scaleFor(i);n.scaled&&this._scaleSpan(n,r)}}_scaleSpan(e,t){const i=e.height;if(!(i>0))return;const s=i/2*t,r=e.yToPrice(i/2-s),n=e.yToPrice(i/2+s);isFinite(r)&&isFinite(n)&&r!==n&&e.setPriceRange({min:Math.min(r,n),max:Math.max(r,n)})}_firstVisibleValue(e,t,i,s=!1){const r=this._shownSource();for(const n of r?[r,...this._series]:this._series){if(!1===n.style.visible||!e(n))continue;const r=s?n.style.barOffset??0:0,o=t.dataLayer,a=o.seriesBars(n.dataId),l=Fi;if(T(o,a,i.from-r,i.to-r,l))for(let e=l.start;e<a.length;e++){const t=a[e];if(t.time>l.lastTime)break;if(void 0!==o.timeToIndex(t.time)&&isFinite(t.close))return t.close}}return null}paintBase(e,t){const i=this._layout(e),s=e.dpr,r=t??this._backend.overlay2d()??this.base.ctx;void 0===t&&this._backend.beginFrame(!0);const n=!0!==e.collapsed,o=this._live(e),a=W(e.theme,e.canvasOptions?.scales);!1!==e.paintBackground&&"transparent"!==e.theme.background&&(r.fillStyle=e.theme.background,r.fillRect(0,0,Math.round(this._width*s),Math.round(this._height*s))),r.save(),i.plotLeft>0&&r.translate(Math.round(i.plotLeft*s),0);const l=e.canvasOptions?.grid,h=function(e,t,i){const s=Math.max(1,i.spacing),r=[];if(!1!==i.vertLines)for(let t=s;t<e;t+=s)r.push(t);const n=[];if(!1!==i.horzLines)for(let e=s;e<t;e+=s)n.push(e);return{verticals:r,horizontals:n}}(i.plotWidth,i.plotHeight,{...l,spacing:l?.spacing??60,vertLines:e.showVertGrid&&n,horzLines:e.showHorzGrid&&n});(h.verticals.length>0||h.horizontals.length>0)&&function(e,t,i,s,r,n){e.save(),e.lineWidth=Math.max(1,Math.round(n.lineWidth*r));const o=Math.round(i*r),a=Math.round(s*r);B(e,t.verticals,!0,n.vert,n,r,o,a),B(e,t.horizontals,!1,n.horz,n,r,o,a),e.setLineDash([]),e.restore()}(r,h,i.plotWidth,i.plotHeight,s,E(e.theme,l,s)),r.save(),r.beginPath(),r.rect(0,0,Math.round(i.plotWidth*s),Math.round(i.plotHeight*s)),r.clip();const c=this._primitiveContext(e),d=this._slotted(o,e);for(const t of o)"bottom"!==t.zOrder()||d?.has(t)||t.draw(r,this._boundPrimitiveContext(t,c,e));const{readout:u,lastEntry:_,valueTags:p}=this._paintSeriesPass(r,e,i,n,c,d,t);void 0===t&&this._backend.endFrame(),r.restore();const f=this.axisSlots(e),m={up:e.theme.lastPriceUp,down:e.theme.lastPriceDown,text:e.theme.lastPriceText},b=_;null!==b&&b.showLine&&function(e,t,i,s,r,n,o=ti){const a=Math.round(t.priceToY(i)*n);if(!Number.isFinite(a)||a<0||a>r.plotHeight*n)return;const l=s?o.up:o.down,h=Math.round(r.plotWidth*n);e.save(),e.strokeStyle=l,e.lineWidth=Math.max(1,Math.round(n)),e.setLineDash([3*n,3*n]),e.beginPath(),e.moveTo(0,a+.5),e.lineTo(h,a+.5),e.stroke(),e.setLineDash([]),e.restore()}(r,u,b.close,b.up,i,s,m);const g=null!==this._source&&this._series.includes(this._source)?e.barCountdown:void 0,v=e=>null!==b&&b.showTag&&u===e;this._paintAxisLadders(r,i,s,f,p,b,v,g,a);for(const t of o)"normal"!==t.zOrder()||d?.has(t)||t.draw(r,this._boundPrimitiveContext(t,c,e));for(const e of f)null!==b&&v(this._scaleFor(e.scaleId))&&this._inAxisSlot(r,e,i,s,(t,i)=>di(r,t,b.close,b.up,i,s,a,{colors:m,countdown:g,side:e.side}));e.showTimeAxis&&(!function(e,t,i,s,r,n=Vt,o,a){const l=t.visibleRange(),h=Math.max(0,Math.floor(l.from)),c=Math.min(i.baseIndex,Math.ceil(l.to));if(c<h)return;const d=Math.max(1,Math.round(80/Math.max(1,t.barSpacing))),u=Math.round(s.plotHeight*r);let _=Number.POSITIVE_INFINITY;{let e=i.indexToTime(h),t=Number.POSITIVE_INFINITY;for(let s=h+1;s<=c;s++){const r=i.indexToTime(s);if(void 0!==r&&void 0!==e){const i=r-e;i>0&&Math.floor(r/60)!==Math.floor(e/60)?_=Math.min(_,i):i>0&&(t=Math.min(t,i))}void 0!==r&&(e=r)}Number.isFinite(_)||(_=t)}const p=_*d,f=Number.isFinite(p)&&p<60,m=void 0!==a&&a!==ht&&_t(a)?a:ht,b=m===ht,g=b?Zt:(e,t)=>vt(e,t,m),v=b?nt:e=>At(e,m),y=b?tt:e=>Ct(e,m),w=b?it:e=>jt(e,m);e.save(),e.fillStyle=n.textColor,e.strokeStyle=n.lineColor,e.font=_i(n.font,r),e.textAlign="center",e.textBaseline="top",e.lineWidth=1,e.beginPath(),e.moveTo(0,u+.5),e.lineTo(Math.round(s.plotWidth*r),u+.5),e.stroke();const x=new Set;{let e=i.indexToTime(h);for(let t=h+1;t<=c;t++){const s=i.indexToTime(t);void 0!==s&&(void 0!==e&&g(e,s)&&x.add(t),e=s)}}const S=[...new Set([...ei(h,c,d),...x])].sort((e,t)=>e-t),P=[];let O;for(const n of S){const a=i.indexToTime(n);if(void 0===a)continue;const l=Math.round(t.indexToX(n)*r);if(l<0||l>s.plotWidth*r){O=a;continue}let h;h=o?o(a,void 0===O?"day":Jt(O,a,m,f)):void 0===O||g(O,a)?v(a):f?w(a):y(a),P.push({x:l,text:h,half:e.measureText(h).width/2,boundary:x.has(n)}),O=a}const k=4*r,I=[],M=e=>I.every(t=>e.x+e.half+k<=t.x-t.half||e.x-e.half-k>=t.x+t.half);for(const e of P)e.boundary&&M(e)&&I.push(e);for(const e of P)!e.boundary&&M(e)&&I.push(e);I.sort((e,t)=>e.x-t.x);for(const t of I)e.fillText(t.text,t.x,u+4*r);e.restore()}(r,e.timeScale,e.dataLayer,i,s,a,e.timeFormatter,e.timezone),void 0!==e.sessionClock&&ui(r,{...i,priceAxisWidth:Math.min(i.priceAxisWidth,e.axisColumnWidth??i.priceAxisWidth)},s,e.sessionClock,a)),r.restore(),void 0===t&&this._paintedHitBoxes(e)}_paintSeriesPass(e,t,i,s,r,n,o){const a=t.dpr,l=t.timeScale.visibleRange(),h=this._readoutScale(),c=this._shownSource(),d=void 0!==c&&this._scaleFor(c.scaleId)===h?c:this._series.find(e=>!1!==e.style.visible&&je(e.type).isPriceSeries&&this._scaleFor(e.scaleId)===h);let u=null;const _=[],p=t.timeScale.barSpacing,f=$e(a,t.conflationFactor),m=t.conflate&&function(e,t,i){return e>0&&e*t<i}(p,a,f),b=zi;b.dpr=a,b.factor=t.conflationFactor;for(const h of this._series){if(!1===h.style.visible||!s){this._paintSlot(n,h,e,r,t,o);continue}const c=this._scaleFor(h.scaleId),g=je(h.type),v=h.style.barOffset??0,y=m&&Ei.has(g)?Ee(h.type):null;null!==y&&(b.kind=y);let w=this._drawItems.get(h);void 0===w&&this._drawItems.set(h,w=Ve());const x=w.priceToY,S=w.build(t.dataLayer,h.dataId,l.from-v,l.to-v,v,t.timeScale,c,null===y?null:b,!0===g.connectsBars),P="ohlc"===y?Math.max(p,f/a):p;if(!0===h.style.colorByPreviousClose&&S.length>0){const e=w.firstIndex()-1,i=j(t.dataLayer,h.dataId,e);void 0!==i&&(S[0].prevClose=i.close)}let O=0;for(let e=0;e<S.length;e++){const t=S[e].bar.volume??0;t>O&&(O=t)}const k={plotHeight:i.plotHeight,maxVolume:O,theme:t.theme};void 0===o?this._backend.drawSeries(g,S,x,P,a,h.style,k):g.draw(e,S,x,P,a,h.style,k);const I=t.dataLayer.lastIndexedBar(h.dataId);if(null!==I){const e=Ni(h.style,I.bar.close>=I.bar.open);h===d&&null===u?u={close:I.bar.close,up:I.bar.close>=I.bar.open,showLine:!1!==h.style.priceLineVisible,showTag:!1!==h.style.lastValueVisible&&(void 0===e||!se(e))}:!1!==h.style.lastValueVisible&&void 0!==e&&!se(e)&&Number.isFinite(I.bar.close)&&_.push({price:I.bar.close,color:e,scaleId:h.scaleId})}this._paintSlot(n,h,e,r,t,o)}return{readout:h,lastEntry:u,valueTags:_}}_paintAxisLadders(e,t,i,s,r,n,o,a,l){for(const h of s)this._inAxisSlot(e,h,t,i,(s,c)=>{const{side:d,width:u,scaleId:_}=h,p=r.filter(e=>e.scaleId===_),f=[];if(null!==n&&o(s)){const e=oi(i,!0===a?.visible),r=ai(Math.round(s.priceToY(n.close)*i),t.plotHeight*i,e,d);null!==r&&f.push({y:r,height:e,priority:Ht})}const m=f.length;for(const e of p){const r=oi(i),n=ai(Math.round(s.priceToY(e.price)*i),t.plotHeight*i,r,d);f.push({y:n??NaN,height:r,priority:Gt})}const b=f.length>0?Kt(f,2*i):[],g=f.length>0?f.filter((e,t)=>b[t]):void 0;"left"===d?(e.save(),e.translate(-Math.round(u*i),0),Xt(e,s,c,i,l,g,"left"),e.restore()):Xt(e,s,c,i,l,g);for(let t=0;t<p.length;t++)b[m+t]&&ci(e,s,p[t].price,p[t].color,c,i,l,d)})}_inAxisSlot(e,t,i,s,r){const{side:n,width:o}=t,a=this._scaleFor(t.scaleId);if(!a.scaled)return;e.save();const l=Math.round(t.x*s),h=Math.round((t.x+o)*s);e.beginPath(),e.rect(l,0,h-l,Math.round(i.plotHeight*s)),e.clip(),e.translate("left"===n?h:l-Math.round(i.plotWidth*s),0),r(a,{...i,priceAxisWidth:o,plotLeft:o}),e.restore()}_paintSlot(e,t,i,s,r,n){if(null===e)return;let o=!1;for(const[a,l]of e)l===t&&(o||void 0!==n||this._backend.endFrame(),o=!0,a.draw(i,this._boundPrimitiveContext(a,s,r)))}paintTop(e,t,i){void 0===i&&this.top.clearBitmap();const s=this._layout(t),r=i??this.top.ctx,n=t.dpr;r.save(),s.plotLeft>0&&r.translate(Math.round(s.plotLeft*n),0);const o=this._primitiveContext(t),a=this._live(t),l=0===this._stackAbove.size?null:this._slotted(a.filter(e=>"top"===e.zOrder()&&this._stackAbove.has(e)),t);for(const e of a)"top"!==e.zOrder()||l?.has(e)||e.draw(r,this._boundPrimitiveContext(e,o,t));if(null!==e){const i=pi(t.theme,t.canvasOptions?.crosshair,n);!0!==t.collapsed&&fi(r,e.x,e.yLocal,s.plotWidth,s.plotHeight,n,i.color,i.width,i.dash);const o=t.theme.crosshairLabelBackground??i.color,a=!1!==t.theme.crosshairLabelVisible,l=W(t.theme,t.canvasOptions?.scales);if(a&&null!==e.yLocal){const i=this._readoutScale(),a=this.axisSlots(t).find(e=>this._scaleFor(e.scaleId)===i);if(a){const h=i.format(i.yToPrice(e.yLocal)),c=Math.round(a.x*n),d=Math.round((a.x+a.width)*n),u="left"===a.side?d-function(e,t,i,s=Vt){e.save(),e.font=_i(s.font,i);const r=e.measureText(t).width+12*i+1;return e.restore(),r}(r,h,n,l):c;r.save(),r.beginPath(),r.rect(c,0,d-c,Math.round(s.plotHeight*n)),r.clip(),function(e,t,i,s,r,n,o,a,l=Vt){e.save(),e.font=_i(l.font,r);const h=6*r,c=16*r,d=e.measureText(t).width;e.fillStyle=n,e.textBaseline="middle",e.fillRect(i+1,s-c/2,d+2*h,c),e.fillStyle=o,e.textAlign="left",e.fillText(t,i+1+h,s),e.restore()}(r,h,u,e.yLocal*n,n,o,t.theme.lastPriceText,0,l),r.restore()}}if(a&&e.showTimeTag&&e.x>=0&&e.x<=s.plotWidth){const i=Math.round(t.timeScale.xToIndex(e.x)),a=t.dataLayer.indexToTime(i);if(void 0!==a){!function(e,t,i,s,r,n,o=Vt){e.save(),e.font=_i(o.font,r);const a=7*r,l=18*r,h=e.measureText(t).width+2*a,c=Math.max(0,i-h/2),d=s+1*r,u=(n.radius??4)*r;e.beginPath(),ne(e,c,d,h,l,u),void 0!==n.backplate&&(e.fillStyle=n.backplate,e.fill()),e.fillStyle=n.background,e.fill(),void 0!==n.border&&(e.strokeStyle=n.border,e.lineWidth=Math.max(1,Math.round(r)),e.stroke()),e.fillStyle=n.textColor,e.textAlign="center",e.textBaseline="middle",e.fillText(t,c+h/2,d+l/2),e.restore()}(r,t.timeFormatter?t.timeFormatter(a):Nt(a,t.timezone??ht),e.x*n,s.plotHeight*n,n,{background:o,textColor:t.theme.lastPriceText,backplate:t.theme.background},l)}}}r.restore(),void 0===i&&this._paintedHitBoxes(t)}_readoutScale(){const e=this._shownSource();if(void 0!==e)return this._scaleFor(e.scaleId);for(const e of this._series)if(!1!==e.style.visible&&je(e.type).isPriceSeries)return this._scaleFor(e.scaleId);return this._rightScale}readoutScale(){return this._readoutScale()}priceToY(e){return this._readoutScale().priceToY(e)}yToPrice(e){return this._readoutScale().yToPrice(e)}}const $i={friction:.0055,minSpeed:.02,triggerSpeed:.08};class Di{constructor(e,t={}){Object.defineProperty(this,"_v0",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_k",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_minSpeed",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_duration",{enumerable:!0,configurable:!0,writable:!0,value:void 0});const i={...$i,...t};this._v0=e,this._k=i.friction,this._minSpeed=i.minSpeed;const s=Math.abs(this._v0)>0?this._minSpeed/Math.abs(this._v0):1;this._duration=s>=1?0:-Math.log(s)/this._k}static shouldAnimate(e,t={}){const i=t.triggerSpeed??$i.triggerSpeed;return Math.abs(e)>=i}distanceAt(e){const t=Math.min(Math.max(0,e),this._duration);return this._v0/this._k*(1-Math.exp(-this._k*t))}get durationMs(){return this._duration}finished(e){return e>=this._duration}}const Wi={tau:55,stopFraction:.995,minLogStep:.02};class qi{constructor(e,t={}){Object.defineProperty(this,"_base",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_target",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_epoch",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_tau",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_duration",{enumerable:!0,configurable:!0,writable:!0,value:void 0});const i={...Wi,...t};this._target=e,this._tau=i.tau,this._duration=-Math.log(1-i.stopFraction)*i.tau}static leadFraction(e={}){const t=e.tau??Wi.tau;return 1-Math.exp(-16.666666666666668/t)}static shouldAnimate(e,t={}){const i=t.minLogStep??Wi.minLogStep;return Math.abs(e)>=i}add(e,t,i){this._base=t,this._target+=e,this._epoch=i}appliedAt(e){const t=e-this._epoch;return t<=0?this._base:t>=this._duration?this._target:this._base+(this._target-this._base)*(1-Math.exp(-t/this._tau))}get totalLogFactor(){return this._target}get durationMs(){return this._duration}finished(e){return e-this._epoch>=this._duration}}class Vi{constructor(e,t){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_kinetic",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_zoomGlide",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_zoomFocus",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_zoomFrames",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_zoomGlideStart",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_zoomGlideApplied",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_animAutoscale",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_autoscaleTime",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_autoscaleFrames",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_navigationEpoch",{enumerable:!0,configurable:!0,writable:!0,value:0}),this._host=e,this._animAutoscale=t}_step(){return this._stepZoomGlide(),this._stepKinetic(),null!==this._zoomGlide||null!==this._kinetic}_applyZoom(e,t){!1!==this._host._navigation.zoomEnabled&&(this._beginAutoscaleMotion(),this._host._mutateTimeScale(()=>this._host._timeScale.zoomAtX(e,Math.exp(t))),this._host._maybeLoadHistory(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._emitViewport("zoom"))}_remainingZoom(){return null===this._zoomGlide?0:this._zoomGlide.totalLogFactor-this._zoomGlideApplied}_startZoomGlide(e,t){!1!==this._host._navigation.zoomEnabled&&(this._zoomGlide=new qi(t),this._zoomFocus=e,this._zoomGlideStart=this._host._now(),this._zoomGlideApplied=0,this._zoomFrames=0,this._host._loop.requestFrame())}_stepZoomGlide(){const e=this._zoomGlide;if(null===e)return;if(this._host._destroyed||!1===this._host._navigation.zoomEnabled)return void(this._zoomGlide=null);const t=this._host._now()-this._zoomGlideStart,i=e.appliedAt(t),s=i-this._zoomGlideApplied;if(this._zoomGlideApplied=i,0!==s&&this._applyZoom(this._zoomFocus,s),this._zoomGlide===e&&!this._host._destroyed&&(e.finished(t)||++this._zoomFrames>=600)){const t=e.totalLogFactor-this._zoomGlideApplied;0!==t&&this._applyZoom(this._zoomFocus,t),this._zoomGlide===e&&(this._zoomGlide=null)}}_beginAutoscaleMotion(){this._animAutoscale&&null===this._autoscaleTime&&(this._autoscaleTime=this._host._now()-16,this._autoscaleFrames=0)}_autoscaleFraction(){const e=this._host._now(),t=null===this._autoscaleTime||++this._autoscaleFrames>=90?1:1-Math.exp(-Math.max(1,e-this._autoscaleTime)/80);return null!==this._autoscaleTime&&(this._autoscaleTime=e),t}_stopNavigationMotion(){this._navigationEpoch++,this._stopZoomGlide(),this._stopKinetic(),this._autoscaleTime=null}_stopDisabled(e,t){t&&(this._navigationEpoch++,this._stopZoomGlide()),e&&this._stopKinetic(),this._autoscaleTime=null}_stopZoomGlide(){this._zoomGlide=null}_startKinetic(e){if(!1===this._host._navigation.panEnabled)return;this._stopKinetic();const t=new Di(e);t.durationMs<=0||(this._kinetic={anim:t,start:this._host._now(),travelled:0,frames:0},this._host._loop.requestFrame())}_stepKinetic(){const e=this._kinetic;if(null===e)return;if(this._host._destroyed||!1===this._host._navigation.panEnabled)return void(this._kinetic=null);const t=this._host._now()-e.start,s=e.anim.distanceAt(t),r=s-e.travelled;e.travelled=s,this._beginAutoscaleMotion(),this._host._mutateTimeScale(()=>this._host._timeScale.setRightOffset(this._host._timeScale.rightOffset-r/this._host._timeScale.barSpacing)),this._host._maybeLoadHistory(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._emitViewport("pan"),this._kinetic===e&&(e.anim.finished(t)||++e.frames>=600)&&(this._kinetic=null)}_stopKinetic(){this._kinetic=null}}class Hi{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_resizeObserver",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_deviceObserver",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_ratioQuery",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_ratioView",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._host=e}_observeSize(){if("undefined"!=typeof ResizeObserver&&(this._resizeObserver=new ResizeObserver(e=>{const t=e[0];t&&(this._host.applySize(t.contentRect.width,t.contentRect.height),this._host._paintNow())}),this._resizeObserver.observe(this._host._container),"undefined"!=typeof ResizeObserverEntry&&"devicePixelContentBoxSize"in ResizeObserverEntry.prototype)){this._deviceObserver=new ResizeObserver(e=>this._onDevicePixels(e));for(const e of this._host._panes)this._observeCanvases(e,!0)}}_observeCanvases(e,t){const i=this._deviceObserver;if(null!==i)for(const s of[e.base,e.top])t?i.observe(s.element,{box:"device-pixel-content-box"}):i.unobserve(s.element)}_onDevicePixels(e){if(this._host._destroyed||this._host._destroying)return;let t=!1;for(const i of e){const e=this._canvasLayerOf(i.target),s=i.devicePixelContentBoxSize?.[0],r=i.contentBoxSize?.[0];null!==e&&void 0!==s&&void 0!==r&&(Math.abs(r.inlineSize-e.mediaWidth)>.05||Math.abs(r.blockSize-e.mediaHeight)>.05?e.forgetDeviceSize():e.setDeviceSize(s.inlineSize,s.blockSize)&&(t=!0))}t&&(this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._paintNow())}_canvasLayerOf(e){for(const t of this._host._panes){if(t.base.element===e)return t.base;if(t.top.element===e)return t.top}return null}_view(){const e=this._host._doc.defaultView;return e||("undefined"==typeof window?null:window)}_watchPixelRatio(){this._unwatchPixelRatio();const e=this._view();if(null===e||this._host._destroying||this._host._destroyed)return;if("function"==typeof e.addEventListener&&(e.addEventListener("resize",this._host._checkPixelRatio),this._ratioView=e),"function"!=typeof e.matchMedia)return;const t=e.matchMedia(`(resolution: ${e.devicePixelRatio||1}dppx)`);"function"==typeof t?.addEventListener&&(t.addEventListener("change",this._host._onPixelRatio),this._ratioQuery=t)}_unwatchPixelRatio(){this._ratioQuery?.removeEventListener("change",this._host._onPixelRatio),this._ratioQuery=null,this._ratioView?.removeEventListener("resize",this._host._checkPixelRatio),this._ratioView=null}_onPixelRatio(){this._host._destroyed||this._host._destroying||(this._watchPixelRatio(),this._checkPixelRatio())}_checkPixelRatio(){this._host._destroyed||this._host._destroying||this._host._pixelRatio()===this._host._layoutRatio||(this._host._layout._relayout(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._paintNow())}}function Gi(e){const t=Math.abs(e);return t>=1e9?(e/1e9).toFixed(2)+"B":t>=1e6?(e/1e6).toFixed(2)+"M":t>=1e3?(e/1e3).toFixed(2)+"K":String(Math.round(e))}const Ui={background:"#0d0e12",grid:"#161a26",axisText:"#8b91a7",axisLine:"#2a3046",paneSeparator:"#1e2334",crosshair:"#6b7280",upColor:"#26a69a",downColor:"#ef5350",wickUpColor:"#26a69a",wickDownColor:"#ef5350",lineColor:"#4f8cff",areaTopColor:"rgba(79,140,255,0.40)",areaBottomColor:"rgba(79,140,255,0.00)",baselineTopLine:"#26a69a",baselineTopFill:"rgba(38,166,154,0.20)",baselineBottomLine:"#ef5350",baselineBottomFill:"rgba(239,83,80,0.20)",lastPriceUp:"#26a69a",lastPriceDown:"#ef5350",lastPriceText:"#0d0e12",buy:"#26a69a",sell:"#ef5350",profit:"#26a69a",loss:"#ef5350"},Ki={background:"#ffffff",grid:"#eef1f6",axisText:"#5b6472",axisLine:"#d4dae3",paneSeparator:"#e6eaf0",crosshair:"#9aa3b2",upColor:"#089981",downColor:"#e0473e",wickUpColor:"#089981",wickDownColor:"#e0473e",lineColor:"#2962ff",areaTopColor:"rgba(41,98,255,0.30)",areaBottomColor:"rgba(41,98,255,0.00)",baselineTopLine:"#089981",baselineTopFill:"rgba(8,153,129,0.18)",baselineBottomLine:"#e0473e",baselineBottomFill:"rgba(224,71,62,0.18)",lastPriceUp:"#089981",lastPriceDown:"#e0473e",lastPriceText:"#ffffff",buy:"#089981",sell:"#e0473e",profit:"#089981",loss:"#e0473e"},Yi=Ki;function Xi(e,t=!1){if(void 0===e)return;if(null===e||"object"!=typeof e||Array.isArray(e))throw new TypeError("Series options must be an object");if(void 0!==e.confirmation&&"auto"!==e.confirmation&&"forming"!==e.confirmation&&"confirmed"!==e.confirmation)throw new TypeError("Series confirmation must be auto, forming or confirmed");const i=e.source;if(t&&void 0!==i&&"live"!==i&&"history"!==i)throw new TypeError("Series update source must be live or history")}class Ji{constructor(e){Object.defineProperty(this,"_state",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_tailTime",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._state=Object.freeze({sourceId:e,revision:0,historyRevision:0,provenance:"history",change:"reset"})}snapshot(){return this._state}record(e,t,i){const s="reset"===e||"prepend"===e||"correction"===e||"history"===i?.source,r="correction"===e?void 0:i?.confirmation,n="auto"===r?void 0:r??("reset"!==e&&t===this._tailTime?this._state.confirmation:void 0);this._tailTime=t,this._state=Object.freeze({sourceId:this._state.sourceId,revision:this._state.revision+1,historyRevision:this._state.historyRevision+(s?1:0),provenance:s?"history":"live",change:e,...void 0===t||void 0===n?{}:{confirmation:n,confirmationSource:"provider"}})}contextChanged(){this.record("reset",this._tailTime)}}const Zi=new WeakMap;function Qi(e){const t=Zi.get(e)?.snapshot().confirmation;return void 0===t?void 0:{confirmation:t}}class es extends Error{constructor(e){super(e),this.name="IndicatorInputError"}}function ts(e){if(null===e||"object"!=typeof e)return!1;const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function is(e){return Object.values(Object.getOwnPropertyDescriptors(e)).every(e=>"value"in e)}function ss(e,t){for(const i of e){if(!["symbol","session","multiline","price","timestamp"].includes(i.type))continue;const s=e=>{throw new es(`Invalid study input "${i.key}": ${e}`)},r=Object.getOwnPropertyDescriptor(t,i.key);r&&!("value"in r)&&s("expected a data property");const n=r?r.value:i.default;if("price"===i.type||"timestamp"===i.type){const{min:t,max:r,step:o}=i;(void 0!==t&&!Number.isFinite(t)||void 0!==r&&!Number.isFinite(r)||void 0!==t&&void 0!==r&&t>r||void 0!==o&&(!Number.isFinite(o)||o<=0))&&s("invalid numeric constraints");const a=e=>"number"==typeof e&&Number.isFinite(e)&&(void 0===t||e>=t)&&(void 0===r||e<=r);a(i.default)&&a(n)||s("expected a finite number within the declared bounds");const l=i.pick;if(void 0!==l&&"boolean"!=typeof l){"price"===i.type&&null!==l&&"object"==typeof l||s("invalid pick target");const e=l;ts(e)||s("invalid pick target");const t=Object.getOwnPropertyDescriptors(e);Object.values(t).some(e=>!("value"in e))&&s("invalid pick target");const r=t.paneIndex?.value,n=t.priceScaleId?.value;void 0!==r&&(!Number.isSafeInteger(r)||r<0)&&s("invalid pick pane"),void 0===n||L(n)||s("invalid pick scale")}if("price"===i.type){const{timeKey:t,anchor:r}=i;void 0===t||"string"==typeof t&&t!==i.key&&"timestamp"===e.find(e=>e.key===t)?.type&&!e.some(e=>e!==i&&"price"===e.type&&e.timeKey===t)||s("timeKey must name a declared timestamp input no other price pairs with"),void 0!==r&&("boolean"!=typeof r||r&&void 0===t)&&s("an anchor needs a boolean and a timeKey")}}else if("string"==typeof i.default&&"string"==typeof n||s("expected text"),"session"!==i.type||$t(i.default)&&$t(n)||s("expected a session such as 0930-1600:23456"),"symbol"===i.type&&void 0!==i.exchangeKey){"string"==typeof i.exchangeKey&&i.exchangeKey&&i.exchangeKey!==i.key||s("invalid exchange field");const r=Object.getOwnPropertyDescriptor(t,i.exchangeKey);!r||"value"in r&&"string"==typeof r.value||s(`expected text in "${i.exchangeKey}"`);const n=e.find(e=>e.key===i.exchangeKey);n&&"string"!=typeof n.default&&s("exchange field must default to text")}}}const rs=[{label:"Solid",value:"solid"},{label:"Dashed",value:"dashed"},{label:"Dotted",value:"dotted"}];function ns(e){return{color:e.colorKey??`${e.key}:color`,width:`${e.key}:width`,lineStyle:`${e.key}:lineStyle`,opacity:`${e.key}:opacity`,type:`${e.key}:type`}}function os(e){const t=[];for(const i of e.plots){const s=ns(i),r=e.inputs.find(e=>e.key===i.colorKey),n="string"==typeof r?.default?r.default:i.style?.color??"#4f8cff";t.push({key:s.color,type:"color",label:i.title,default:n,group:i.title}),t.push({key:s.opacity,type:"number",label:"Opacity",default:100,min:0,max:100,step:1,group:i.title}),t.push({key:s.width,type:"number",label:"Thickness",default:i.style?.lineWidth??1.5,min:.5,max:8,step:.5,group:i.title}),t.push({key:s.lineStyle,type:"select",label:"Line style",default:i.style?.lineStyle??"solid",options:rs,group:i.title}),t.push({key:s.type,type:"select",label:"Plot style",default:i.type,options:as,group:i.title})}return t}const as=[{label:"Line",value:"line"},{label:"Line with markers",value:"line-markers"},{label:"Step line",value:"step"},{label:"Area",value:"area"},{label:"Histogram",value:"histogram"},{label:"Columns",value:"column"}],ls=[{label:"Close",value:"close"},{label:"Open",value:"open"},{label:"High",value:"high"},{label:"Low",value:"low"},{label:"HL2",value:"hl2"},{label:"HLC3",value:"hlc3"},{label:"OHLC4",value:"ohlc4"}],hs=new Map;function cs(e){ss(e.inputs,{}),hs.set(e.id,e)}function ds(e){const t=hs.get(e);if(void 0===t)throw new Error(`openalgo-charts: unknown indicator "${e}", did you import 'openalgo-charts/indicators'?`);return t}function us(e){return hs.has(e)}function _s(){return Array.from(hs.values())}function ps(e){ss(e.inputs,{});const t=Object.fromEntries(e.inputs.map(e=>[e.key,e.default]));for(const i of e.inputs)"symbol"!==i.type||void 0===i.exchangeKey||Object.prototype.hasOwnProperty.call(t,i.exchangeKey)||Object.defineProperty(t,i.exchangeKey,{value:"",enumerable:!0,writable:!0,configurable:!0});return t}function fs(e,t){switch(t){case"open":return e.open;case"high":return e.high;case"low":return e.low;case"hl2":return(e.high+e.low)/2;case"hlc3":return(e.high+e.low+e.close)/3;case"ohlc4":return(e.open+e.high+e.low+e.close)/4;case"volume":return e.volume??0;default:return e.close}}function ms(e,t,i){if("string"!=typeof t){if(!ts(t))throw new es("Invalid study source reference");const s=Object.getOwnPropertyDescriptors(t);if(3!==Reflect.ownKeys(s).length||"indicator"!==s.kind?.value||"string"!=typeof s.instanceId?.value||!s.instanceId.value.trim()||"string"!=typeof s.plotKey?.value||!s.plotKey.value.trim())throw new es("Invalid study source reference");if(!i?.resolveSource)throw new es("Study source requires a calculation resolver");const r=i.resolveSource(t);if(!Array.isArray(r)||r.length!==e.length)throw new es("Study source length must align with source bars");return r.slice()}const s=new Array(e.length);for(let i=0;i<e.length;i++)s[i]=fs(e[i],t);return s}function bs(e,t){const i=[...new Set(t.filter(e=>void 0!==e))],s=1===i.length?null:new AbortController,r=s?.signal??i[0];return new Promise((t,n)=>{let o=!1;const a=new Map,l=e=>{o||(o=!0,(()=>{for(const[e,t]of a)e.removeEventListener("abort",t);a.clear()})(),e())},h=e=>{l(()=>{n(e.reason),s?.abort(e.reason)})},c=i.find(e=>e.aborted);if(c)h(c);else{for(const e of i){const t=()=>h(e);a.set(e,t),e.addEventListener("abort",t,{once:!0})}try{Promise.resolve(e(r)).then(e=>l(()=>t(e)),e=>l(()=>n(e)))}catch(e){l(()=>n(e))}}})}const gs=new Set(["everyUpdate","oncePerBar","onBarClose","once"]);function vs(e,t){const i={};for(const s of Object.keys(e))Object.defineProperty(i,s,{value:Object.freeze(e[s].slice(0,t)),enumerable:!0});return Object.freeze(i)}function ys(e,t,i,s){return s===e.length-1?{bars:e,values:t,settings:i,index:s}:{bars:Object.freeze(e.slice(0,s+1)),values:vs(t,s+1),settings:i,index:s}}function ws(e,t){return t?e=>t.bars[t.sourceIndex[e]].time:t=>e[t].time}class xs{constructor(e){Object.defineProperty(this,"_entries",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_legacyCalculations",{enumerable:!0,configurable:!0,writable:!0,value:new WeakSet}),Object.defineProperty(this,"_initialized",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_native",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_sourceId",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_historyRevision",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_revision",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_replaying",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_epoch",{enumerable:!0,configurable:!0,writable:!0,value:0});const t=new Set;for(const i of e){const e=i.frequency;if(void 0!==e){if(!gs.has(e))throw new TypeError("Indicator alert frequency is invalid");if("string"!=typeof i.id||""===i.id.trim())throw new TypeError("Explicit indicator alert id must be nonempty");if(t.has(i.id))throw new TypeError("Explicit indicator alert ids must be unique");t.add(i.id),this._entries.push({spec:Object.freeze({...i,frequency:e}),onceSpent:!1,perBarTime:-1/0,closedTime:-1/0,failedCloses:new Set,busy:!1})}}}evaluate(e,t){if(0===this._entries.length||!e.current())return;const i=e.calculation.execution,s=void 0!==i,r="replay"===i?.provenance,n=!this._initialized||this._native!==s||s&&(i.sourceId!==this._sourceId||i.historyRevision!==this._historyRevision||r!==this._replaying);if(s){if(!n&&void 0!==this._revision&&i.revision<=this._revision)return}else{if(this._legacyCalculations.has(e.calculation))return;this._legacyCalculations.add(e.calculation)}const o=++this._epoch;this._initialized=!0,this._native=s,this._sourceId=i?.sourceId,this._historyRevision=i?.historyRevision,this._revision=i?.revision,this._replaying=r;const a=e.sampled?.bars??e.bars,l=ws(e.bars,e.sampled),h=a[a.length-(e.calculation.barState.isConfirmed?1:2)]?.time??-1/0,c=e.refresh||"refresh"===i?.change,d=s?"live"===i.provenance:e.tailOnly;if(n||!c&&!d)return void this._seed(h);if(c||!d||0===e.bars.length)return;const u=Object.freeze(e.bars.map(e=>Object.freeze({...e}))),_=vs(e.values),p=Object.freeze({...e.settings}),f=()=>this._epoch===o&&e.current();if(!f())return;let m,b=!1;const g=u.length-1,v=Math.min(e.from,g),y=[];for(let t=v;t<=g;t++)(t>v?l(t)!==l(t-1):v===e.from||l(t)===a[a.length-1].time)&&y.push(t);for(const e of this._entries){if(!f())return;if(e.busy)continue;const i="onBarClose"===e.spec.frequency,s=i?this._closeIndices(e,l,g,h):y;for(const r of s){if(!f())return;if("once"===e.spec.frequency&&e.onceSpent)break;const s=u[r].time,n=l(r);if("oncePerBar"===e.spec.frequency&&n<=e.perBarTime)continue;const o=ys(u,_,p,r);let a=!1;e.busy=!0;try{if(!f())return;const l=e.spec.when(o);if(!f())return;if(!l){i&&this._judgeClose(e,n);continue}const h="function"==typeof e.spec.message?e.spec.message(o):e.spec.message??e.spec.title;if(!f())return;const c={alertId:e.spec.id,title:e.spec.title,message:h,time:s,index:r};if("once"===e.spec.frequency&&(e.onceSpent=!0),"oncePerBar"===e.spec.frequency&&(e.perBarTime=n),i&&this._judgeClose(e,n),a=!0,t(c),!f())return}catch(t){if(!f())return;i&&!a&&e.failedCloses.add(n),b||(b=!0,m=t)}finally{e.busy=!1}}}if(b&&f())throw m}redate(e,t){for(const i of this._entries)i.perBarTime===e&&(i.perBarTime=t),i.closedTime===e&&(i.closedTime=t)}_seed(e){for(const t of this._entries)t.perBarTime=e,t.closedTime=e,t.failedCloses.clear()}_judgeClose(e,t){e.closedTime=Math.max(e.closedTime,t),e.failedCloses.delete(t)}_closeIndices(e,t,i,s){const r=[],n=new Set;for(let o=0,a=0;o<=i&&(a=t(o))<=s;o++)(a>e.closedTime||e.failedCloses.has(a))&&!n.has(a)&&r.push(o),n.add(a);for(const t of e.failedCloses)n.has(t)||e.failedCloses.delete(t);return r}}function Ss(e){return"string"==typeof e&&e.trim().length>0}function Ps(e){if("object"!=typeof e||null===e)return!1;const t=Object.getOwnPropertyDescriptor(e,"kind");return void 0!==t&&"value"in t&&"indicator"===t.value}function Os(e){if(!ts(e))throw new TypeError("Study source reference must be a plain object.");const t=Reflect.ownKeys(e);if(3!==t.length||!t.every(e=>"kind"===e||"instanceId"===e||"plotKey"===e))throw new TypeError("Study source reference requires only kind, instanceId and plotKey.");const i=Object.getOwnPropertyDescriptor(e,"kind"),s=Object.getOwnPropertyDescriptor(e,"instanceId"),r=Object.getOwnPropertyDescriptor(e,"plotKey");if(!(i&&s&&r&&"value"in i&&"value"in s&&"value"in r))throw new TypeError("Study source reference fields must be own data properties.");if("indicator"!==i.value||!Ss(s.value)||!Ss(r.value))throw new TypeError("Study source reference requires nonempty instanceId and plotKey strings.");return{kind:"indicator",instanceId:s.value,plotKey:r.value}}function ks(e){if(!ts(e))throw new TypeError("Indicator settings must be a plain object.");const t={};for(const i of Object.keys(e)){const s=Object.getOwnPropertyDescriptor(e,i);if(!("value"in s))throw new TypeError(`Indicator settings property "${i}" must be a data property.`);const r=s.value;Object.defineProperty(t,i,{value:Ps(r)?Os(r):r,writable:!0,configurable:!0,enumerable:!0})}return t}function Is(e,t){let i=e.length;for(e.push(t);i>0;){const s=i-1>>>1;if(e[s]<=t)break;e[i]=e[s],i=s}e[i]=t}function Ms(e){const t=e[0],i=e.pop();if(0===e.length)return t;let s=0;for(;2*s+1<e.length;){let t=2*s+1;if(t+1<e.length&&e[t+1]<e[t]&&t++,e[t]>=i)break;e[s]=e[t],s=t}return e[s]=i,t}function Ts(e){const t=new Map;for(let i=0;i<e.length;i++){const s=e[i].id;if(!Ss(s))throw new TypeError("Indicator instance id must be a nonempty string.");if(t.has(s))throw new TypeError(`Duplicate indicator instance id "${s}".`);t.set(s,i)}const i=new Map,s=e.map(()=>[]),r=e.map(()=>0);for(let n=0;n<e.length;n++){const o=e[n],a=ks(o.settings);for(const e of Object.keys(a))if(Ps(a[e])&&!o.descriptor.inputs.some(t=>t.key===e&&"source"===t.type&&!0===t.allowStudyOutputs))throw new TypeError(`Indicator "${o.id}" input "${e}" does not support study sources.`);const l=[],h=new Set;for(const i of o.descriptor.inputs){if("source"!==i.type)continue;const s=Object.getOwnPropertyDescriptor(a,i.key),r=s?s.value:i.default;if("string"==typeof r)continue;const n=Os(r);if(!0!==i.allowStudyOutputs)throw new TypeError(`Indicator "${o.id}" input "${i.key}" does not support study sources.`);if(n.instanceId===o.id)throw new TypeError(`Indicator "${o.id}" cannot reference itself (self dependency).`);const c=t.get(n.instanceId);if(void 0!==c){const t=e[c].descriptor.plots.filter(e=>e.key===n.plotKey);if(1!==t.length)throw new TypeError(`Study source plot "${n.plotKey}" must identify one declared plot on "${n.instanceId}".`);if(void 0!==t[0].ohlc)throw new TypeError(`Study source plot "${n.plotKey}" must be scalar, without an OHLC mapping.`);h.add(c)}l.push({inputKey:i.key,source:n,available:void 0!==c})}i.set(o.id,l),r[n]=h.size;for(const e of h)s[e].push(n)}const n=[],o=[];for(let t=0;t<e.length;t++)0===r[t]&&Is(n,t);for(;n.length>0;){const t=Ms(n);o.push(e[t].id);for(const e of s[t])r[e]--,0===r[e]&&Is(n,e)}if(o.length!==e.length)throw new TypeError("Study source references contain a dependency cycle.");return{order:o,dependencies:i}}const Cs=["removable","configurable","movable","listed"];function js(e){if(null===e||"object"!=typeof e||Array.isArray(e))throw new TypeError("Invalid indicator policy");const t={};for(const i of Cs){const s=e[i];if(void 0!==s){if("boolean"!=typeof s)throw new TypeError(`Invalid indicator policy flag: ${i}`);s||(t[i]=!1)}}return Object.freeze(t)}function As(e){if(void 0!==e.fontSize&&("number"!=typeof e.fontSize||!Number.isFinite(e.fontSize)||e.fontSize<=0))throw new TypeError("Text fontSize must be a positive finite number");if(void 0!==e.fontFamily&&("string"!=typeof e.fontFamily||""===e.fontFamily.trim()))throw new TypeError("Text fontFamily must be a nonempty CSS font-family list");for(const t of["bold","italic"])if(void 0!==e[t]&&"boolean"!=typeof e[t])throw new TypeError(`Text ${t} must be boolean`);if(void 0!==e.textAlign&&!["left","center","right"].includes(e.textAlign))throw new TypeError("Text textAlign must be left, center or right")}function Ls(e){return void 0!==e.fontSize||void 0!==e.fontFamily||void 0!==e.bold||void 0!==e.italic||void 0!==e.textAlign}function Ns(e,t,i,s=!1){return`${e?.italic?"italic ":""}${e?.bold??s?"600 ":""}${function(e){const t=String(e),i=t.indexOf("e");if(i<0)return t;const s=t.slice(0,i),r=Number(t.slice(i+1)),n=s.replace(".",""),o=(s.indexOf(".")<0?s.length:s.indexOf("."))+r;return o<=0?`0.${"0".repeat(-o)}${n}`:o>=n.length?n+"0".repeat(o-n.length):`${n.slice(0,o)}.${n.slice(o)}`}(t)}px ${e?.fontFamily??i}`}const Rs={tiny:6,small:9,medium:12,big:16};function Fs(e){return Rs[e]}function zs(e,t){return Math.max(4,Math.min(Rs[e],Math.floor(t)))}function Es(e,t,i,s,r,n){const o=r/2;switch(e.fillStyle=n,e.strokeStyle=n,e.beginPath(),t){case"arrowUp":case"triangleUp":e.moveTo(i,s-o),e.lineTo(i+o,s+o),e.lineTo(i-o,s+o),e.closePath(),e.fill();break;case"arrowDown":case"triangleDown":e.moveTo(i,s+o),e.lineTo(i+o,s-o),e.lineTo(i-o,s-o),e.closePath(),e.fill();break;case"circle":e.arc(i,s,o,0,2*Math.PI),e.fill();break;case"square":e.fillRect(i-o,s-o,r,r);break;case"diamond":e.moveTo(i,s-o),e.lineTo(i+o,s),e.lineTo(i,s+o),e.lineTo(i-o,s),e.closePath(),e.fill();break;case"flag":e.fillRect(i-1,s-o,Math.max(1,r/8),r),e.fillRect(i,s-o,o,.8*o);break;case"cross":{const t=Math.max(1,Math.round(r/6));e.fillRect(i-o,s-Math.floor(t/2),r,t),e.fillRect(i-Math.floor(t/2),s-o,t,r);break}case"xcross":e.lineWidth=Math.max(1,Math.round(r/6)),e.moveTo(i-o,s-o),e.lineTo(i+o,s+o),e.moveTo(i+o,s-o),e.lineTo(i-o,s+o),e.stroke()}}const Bs=1.35;function $s(e,t,i,s,r){const n=.5*r,o=s.indexOf("\n")<0?void 0:s.split("\n");let a=e.measureText(void 0===o?s:o[0]).width;if(void 0!==o)for(let t=1;t<o.length;t++)a=Math.max(a,e.measureText(o[t]).width);const l=r*Bs,h=a+2*n,c=r+.64*r+(void 0===o?0:l*(o.length-1)),d=.42*r;return{lines:o,lh:l,w:h,h:c,tail:d,textW:a,top:t?i+d:i-d-c}}function Ds(e,t,i,s,r,n,o){e.font=`600 ${o}px system-ui, sans-serif`;Ws(e,t,i,s,r,n,o,$s(e,t,s,r,o))}function Ws(e,t,i,s,r,n,o,a,l){const{w:h,h:c,top:d,tail:u,lines:_,lh:p,textW:f}=a;e.fillStyle=n,e.beginPath(),ne(e,i-h/2,d,h,c,Math.min(.3*o,c/2)),e.fill();const m=t?d+1:d+c-1;e.beginPath(),e.moveTo(i,s),e.lineTo(i-u,m),e.lineTo(i+u,m),e.closePath(),e.fill(),e.fillStyle=l?.textColor??Q(n),e.textAlign=l?.textAlign??"center",e.textBaseline="middle";const b="left"===l?.textAlign?i-f/2:"right"===l?.textAlign?i+f/2:i;if(void 0===_)return void e.fillText(r,b,d+c/2);const g=d+c/2-p*(_.length-1)/2;for(let t=0;t<_.length;t++)e.fillText(_[t],b,g+p*t)}function qs(e,t){let i=0,s=e.length-1;for(;i<=s;){let r=i+s>>1;const n=e[r].time;if(n===t){for(;r+1<e.length&&e[r+1].time===t;)r++;return e[r]}n<t?i=r+1:s=r-1}}const Vs="system-ui, sans-serif",Hs={aboveBar:-1,paneBottom:-1,belowBar:1,paneTop:1},Gs=e=>"labelUp"===e.shape||"labelDown"===e.shape,Us=e=>Ls(e)||void 0!==e.textColor,Ks=e=>e.fontSize??Math.max(9,Fs(e.size));function Ys(e,t,i,s){return Gs(e)?(t+i)/2:"text"===e.shape?t/2:Math.max(t,s)/2}function Xs(e,t,i,s,r,n,o){const a=Ys(e,n,r,s),l=function(e){let t=0;for(let i=e.indexOf("\n");i>=0;i=e.indexOf("\n",i+1))t++;return t}(e.text??"");if(Gs(e)){const s=r+.64*r+(0===l?0:r*Bs*l),n=.42*r;return"labelUp"===e.shape?{l:t-a,r:t+a,t:i,b:i+n+s}:{l:t-a,r:t+a,t:i-n-s,b:i}}const h=r+l*r*Bs,c=o?i+s:i-s-h;return"text"===e.shape?{l:t-a,r:t+a,t:c,b:c+h}:{l:t-a,r:t+a,t:Math.min(i-s/2,c),b:Math.max(i+s/2,c+h)}}function Js(e,t,i,s,r,n){if(e.length>32){let t=0;for(const i of e)i.r>r&&(e[t++]=i);e.length=t}let o=0;for(let r=!0;r;){r=!1;for(const a of e)if(!(a.r<=i.l||a.l>=i.r||a.b<=i.t+o||a.t>=i.b+o)&&(o=t<0?a.t-s-i.b:a.b+s-i.t,r=!0,Math.abs(o)>n)){o=0,r=!1;break}}return i.t+=o,i.b+=o,e.push(i),o}function Zs(e,t,i,s,r,n){return"paneTop"===e.position?i/2+4*n.dpr+s:"paneBottom"===e.position?n.plotHeight*n.dpr-i/2-4*n.dpr-s:"atPrice"===e.position&&void 0!==e.price?r.priceToY(e.price)*n.dpr:void 0!==t&&"aboveBar"===e.position?r.priceToY(t.high)*n.dpr-i-s:void 0!==t&&"belowBar"===e.position?r.priceToY(t.low)*n.dpr+i+s:void 0!==t?r.priceToY((t.open+t.close)/2)*n.dpr:NaN}function Qs(e,t,i,s,r,n,o,a,l,h,c,d){const u=l?r+n:r-n;let _,p=0;if(h){a&&(e.font=Ns(i,o,Vs,c));let h=s-n/2,d=s+n/2,f=r-n/2,m=r+n/2;if(a&&void 0!==i.text)if(c)_=$s(e,"labelUp"===i.shape,r,i.text,o),h=s-_.w/2,d=s+_.w/2,f=Math.min(r,_.top),m=Math.max(r,_.top+_.h);else{const t=i.text.split("\n");for(const i of t)p=Math.max(p,e.measureText(i).width);const r=o+(t.length-1)*o*Bs;h=Math.min(h,s-p/2),d=Math.max(d,s+p/2),f=Math.min(f,l?u:u-r),m=Math.max(m,l?u+r:u)}if(c||[h,d,f,m].every(Number.isFinite)||(a=!1,h=s-n/2,d=s+n/2,f=r-n/2,m=r+n/2),![h,d,f,m].every(Number.isFinite)||d<0||h>t.plotWidth*t.dpr||m<0||f>t.plotHeight*t.dpr)return;e.save(),e.beginPath(),e.rect(0,0,t.plotWidth*t.dpr,t.plotHeight*t.dpr),e.clip()}const f=h?{width:t.plotWidth,height:t.plotHeight}:void 0;if("labelUp"===i.shape||"labelDown"===i.shape)return void 0!==i.text&&(_?Ws(e,"labelUp"===i.shape,s,r,i.text,i.color,o,_,i):Ds(e,"labelUp"===i.shape,s,r,i.text,i.color,o)),void 0!==i.id&&d.push({id:i.id,x:s/t.dpr,y:r/t.dpr,clip:f}),void(h&&e.restore());if(Es(e,i.shape,s,r,n,i.color),a&&void 0!==i.text){e.fillStyle=i.textColor??i.color,e.font=Ns(i,o,Vs),e.textAlign=i.textAlign??"center",e.textBaseline=l?"top":"bottom";const t="left"===i.textAlign?s-p/2:"right"===i.textAlign?s+p/2:s;if(i.text.indexOf("\n")<0)e.fillText(i.text,t,u);else{const s=i.text.split("\n"),r=o*Bs;for(let i=0;i<s.length;i++)e.fillText(s[l?i:s.length-1-i],t,l?u+r*i:u-r*i)}}void 0!==i.id&&d.push({id:i.id,x:s/t.dpr,y:r/t.dpr,clip:f}),h&&e.restore()}class er{constructor(e,t,i){Object.defineProperty(this,"_seriesId",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_fallbackBars",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_priceScale",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_markers",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lastPositions",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_widths",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_widthDpr",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_fontWidths",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_anyStyled",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._seriesId=e,this._fallbackBars=t,this._priceScale=i}attached(e){this._host=e}detached(){this._host=null,this._lastPositions=[]}zOrder(){return"normal"}setMarkers(e){for(const t of e)if(As(t),void 0!==t.textColor&&("string"!=typeof t.textColor||""===t.textColor.trim()))throw new TypeError("Marker textColor must be a nonempty color string");this._markers=e.slice().sort((e,t)=>e.time-t.time),this._lastPositions=[],this._widths=new Map,this._anyStyled=this._markers.some(Us),this._host?.requestUpdate()}_textWidth(e,t,i,s){this._widthDpr!==s&&(this._widths=new Map,this._widthDpr=s);let r=this._widths.get(t);if(void 0!==r)return r;const n=Ns(t,i,Vs,Gs(t)),o=t.text??"",a=n+"\n"+o;if(r=this._fontWidths.get(a),void 0===r){e.font=n,r=0;for(const t of o.split("\n"))r=Math.max(r,e.measureText(t).width);this._fontWidths.size>=512&&this._fontWidths.clear(),this._fontWidths.set(a,r)}return this._widths.set(t,r),r}draw(e,t){if(this._lastPositions=[],0===this._markers.length)return;const i=function(e,t){let i,s;return r=>{const n=qs(e,r);if(void 0!==n&&Number.isFinite(n.close))return n;if(void 0===t)return;i??(i=t());const o=qs(i,r);if(void 0!==o)return o;if(void 0===s){s=new Map;for(const e of i)s.set(e.time,e)}return s.get(r)}}(t.dataLayer.seriesBars(this._seriesId),this._fallbackBars),s=this._priceScale?.()??t.priceScale,r=t.timeScale.visibleRange(),n=new Map,o=this._markers,a=t.dpr,l=e=>{if(void 0===e.text||void 0===Hs[e.position])return NaN;const t=Ks(e)*a;return Number.isFinite(t)&&t>0?t:NaN};e.save();const h=function(e,t,i,s,r,n){const o=i.dpr;let a=e.length;for(let t=0;t<e.length;t++){const r=e[t],l=i.dataLayer.timeToIndex(r.time);if(void 0!==l){if(Us(r)){const e=Ks(r)*o,t=void 0!==r.text&&Number.isFinite(e)&&e>0?n(r,e):0;if(i.timeScale.indexToX(l)*o+Math.max(t,16*o)<0)continue}else if(l<s.from-1)continue;a=t;break}}if(a===e.length)return null;for(;a>0&&e[a-1].time===e[a].time;)a--;const l=Math.max(0,256*Math.floor((a-256)/256));let h=0;for(let o=l;o<e.length;o++){const a=e[o],l=i.dataLayer.timeToIndex(a.time);if(void 0===l)continue;if(!Us(a)&&l>s.to+1){if(t)continue;break}const c=r(a);c>0&&(h=Math.max(h,n(a,c)))}let c=l,d=-1/0;for(let t=l;t<a;t++){const s=e[t],a=i.dataLayer.timeToIndex(s.time);if(void 0===a)continue;const u=i.timeScale.indexToX(a)*o;u-h>=d&&(t===l||e[t-1].time!==s.time)&&(c=t);const _=r(s);_>0&&(d=Math.max(d,u+n(s,_)))}if(a>c){const t=i.dataLayer.timeToIndex(e[a].time);void 0!==t&&i.timeScale.indexToX(t)*o-h>=d&&(c=a)}return{start:c,widest:h}}(o,this._anyStyled,t,r,l,(i,s)=>Ys(i,this._textWidth(e,i,s,a),s,zs(i.size,t.timeScale.barSpacing)*a));if(null===h)return void e.restore();const{start:c,widest:d}=h,u=2*a,_={[-1]:[],1:[]};for(let h=c;h<o.length;h++){const c=o[h],p=Us(c),f=t.dataLayer.timeToIndex(c.time);if(void 0===f)continue;if(!p&&f>r.to+1){if(this._anyStyled)continue;break}const m=l(c),b=!p&&f<r.from-1;if(b&&!(m>0))continue;const g="paneTop"===c.position||"paneBottom"===c.position?void 0:i(c.time),v=zs(c.size,t.timeScale.barSpacing)*t.dpr,y=t.timeScale.indexToX(f)*t.dpr,w=n.get(c.time)??0;let x=Zs(c,g,v,(v+4*t.dpr)*w,s,t);if(!Number.isFinite(x)||!Number.isFinite(y))continue;n.set(c.time,w+1);const S=Ks(c)*t.dpr,P="labelUp"===c.shape||"labelDown"===c.shape,O=Number.isFinite(S)&&S>0;if(P&&!O)continue;const k=O&&void 0!==c.text,I="belowBar"===c.position||"paneTop"===c.position;if(m>0){const t=Hs[c.position],i=Xs(c,y,x,v,m,this._textWidth(e,c,m,a),I);Number.isFinite(i.l)&&Number.isFinite(i.r)&&Number.isFinite(i.t)&&Number.isFinite(i.b)&&(x+=Js(_[t],t,i,u,y-d,5*(i.b-i.t+u)))}b||Qs(e,t,c,y,x,v,S,k,I,p,P,this._lastPositions)}e.restore()}hitTest(e,t){let i=null;for(const s of this._lastPositions){if(s.clip&&(e<0||t<0||e>s.clip.width||t>s.clip.height))continue;const r=Math.hypot(s.x-e,s.y-t);r<=8&&(null===i||r<i.distance)&&(i={externalId:s.id,zOrder:"normal",distance:r,cursor:"pointer"})}return i}}function tr(e,t){return e===t||void 0!==e&&void 0!==t&&e.topValue===t.topValue&&e.bottomValue===t.bottomValue&&e.topColor===t.topColor&&e.bottomColor===t.bottomColor}class ir{constructor(e){Object.defineProperty(this,"_points",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_visible",{enumerable:!0,configurable:!0,writable:!0,value:!0}),this._opts={opacity:.12,...e}}attached(e){this._host=e}detached(){this._host=null}zOrder(){return"bottom"}autoscaleInfo(){return null}setPoints(e){this._points=e.slice(),this._host?.requestUpdate()}setOptions(e){this._opts={...this._opts,...e},this._host?.requestUpdate()}setVisible(e){this._visible=e,this._host?.requestUpdate()}draw(e,t){if(!this._visible||this._points.length<2)return;const i=t.dpr,s=e=>t.timeScale.indexToX(e)*i,r=e=>t.priceScale.priceToY(e)*i,n=new Map;let o;const a=t=>{let i=t.topValue,s=t.bottomValue;if(void 0===i||void 0===s){if(void 0===o){let e=1/0,t=-1/0;for(const i of this._points)for(const s of[i.a,i.b])null!==s&&Number.isFinite(s)&&(e=Math.min(e,s),t=Math.max(t,s));o={min:e,max:t}}i??(i=o.max),s??(s=o.min)}const a=r(i),l=a+(r(s)-a||1),h=JSON.stringify([a,l,t.topColor,t.bottomColor]);let c=n.get(h);return void 0===c&&(c=e.createLinearGradient(0,a,0,l),c.addColorStop(0,t.topColor),c.addColorStop(1,t.bottomColor),n.set(h,c)),c};e.save(),e.globalAlpha=this._opts.opacity??.12;let l=null;const h=()=>{if(null!==l&&l.xs.length>=2){e.beginPath(),e.moveTo(l.xs[0],l.top[0]);for(let t=1;t<l.xs.length;t++)e.lineTo(l.xs[t],l.top[t]);for(let t=l.xs.length-1;t>=0;t--)e.lineTo(l.xs[t],l.bot[t]);e.closePath(),void 0===l.gradient||void 0!==l.color?(e.fillStyle=l.color??(l.up?this._opts.colorUp:this._opts.colorDown),e.fill()):(e.fillStyle=a(l.gradient),e.fill())}l=null};for(let e=0;e<this._points.length;e++){const t=this._points[e];if(null===t.a||null===t.b||!Number.isFinite(t.a)||!Number.isFinite(t.b)){h();continue}const i=t.a>=t.b,n=s(t.index),o=r(t.a),a=r(t.b),c=t.gradient??this._opts.gradient;if(null===l||l.up!==i||l.color===t.color&&(void 0!==t.color||tr(l.gradient,c))){if(null!==l&&l.up!==i){const n=this._points[e-1];if(void 0!==n&&null!==n.a&&null!==n.b){const e=n.a-n.b,o=t.a-t.b,a=e===o?0:e/(e-o),d=s(n.index+(t.index-n.index)*a),u=r(n.a+(t.a-n.a)*a);l.xs.push(d),l.top.push(u),l.bot.push(u),h(),l={up:i,color:t.color,gradient:c,xs:[d],top:[u],bot:[u]}}else h()}null===l&&(l={up:i,color:t.color,gradient:c,xs:[],top:[],bot:[]}),l.xs.push(n),l.top.push(o),l.bot.push(a)}else l.xs.push(n),l.top.push(o),l.bot.push(a),h(),l={up:i,color:t.color,gradient:c,xs:[n],top:[o],bot:[a]}}h(),e.restore()}}function sr(e,t,i){const s=[];for(let i=0;i<e.length;i++){const r=s[s.length-1];r&&r[0]===e[i]&&r[1]===t[i]||s.push([e[i],t[i]])}if(i&&s.length>1&&s[0][0]===s[s.length-1][0]&&s[0][1]===s[s.length-1][1]&&s.pop(),s.length<2)return null;const r=[];if(s.length>2){const e=s.length;for(let t=0;t<(i?e:e-1);t++){const n=s[t],o=s[(t+1)%e],a=s[i?(t+e-1)%e:Math.max(0,t-1)],l=s[i?(t+2)%e:Math.min(e-1,t+2)],h=[n[0]+(o[0]-a[0])/6,n[1]+(o[1]-a[1])/6,o[0]-(l[0]-n[0])/6,o[1]-(l[1]-n[1])/6,o[0],o[1]];if(!h.every(Number.isFinite))return null;r.push(h)}}return{points:s,segments:r}}function rr(e,t,i,s,r,n,o,a,l,h){const c=(l?.fontSize??11)*t;if(!Number.isFinite(c)||c<=0)return null;e.font=Ns(l,c,"ui-sans-serif, system-ui, sans-serif"),e.textBaseline="middle",e.textAlign=l?.textAlign??"left";const d=r.split("\n");let u=0;for(const t of d)u=Math.max(u,e.measureText(t).width);const _=5*t,p=3*t,f=1.35*c,m=u+2*_,b=f*d.length+2*p,g="center"===a?i-m/2:"right"===a?i-m:i,v="top"===l?.verticalAlign?s:"bottom"===l?.verticalAlign?s-b:s-b/2;if(h){if(![g,v,m,b].every(Number.isFinite)||g+m<0||g>h.w||v+b<0||v>h.h)return null;e.save(),e.beginPath(),e.rect(0,0,h.w,h.h),e.clip()}e.beginPath(),ne(e,g,v,m,b,3*t),e.fillStyle=n,e.fill(),e.fillStyle=o??Q(n);const y="center"===l?.textAlign?g+m/2:"right"===l?.textAlign?g+m-_:g+_;for(let t=0;t<d.length;t++)e.fillText(d[t],y,v+p+f*(t+.5));return h&&e.restore(),{x:g,y:v,w:m,h:b}}class nr{constructor(e){Object.defineProperty(this,"_items",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_visible",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"_priceScale",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_hits",{enumerable:!0,configurable:!0,writable:!0,value:[]}),this._priceScale=e}attached(e){this._host=e}detached(){this._host=null,this._hits=[]}zOrder(){return"normal"}autoscaleInfo(){return null}setItems(e){for(const t of e)if("label"===t.kind||"box"===t.kind){if(As(t),void 0!==t.verticalAlign&&!["top","middle","bottom"].includes(t.verticalAlign))throw new TypeError("Drawing verticalAlign must be top, middle or bottom");if("box"===t.kind&&void 0!==t.align&&!["left","center","right"].includes(t.align))throw new TypeError("Box align must be left, center or right")}this._items=e,this._hits=[],this._host?.requestUpdate()}setVisible(e){this._visible=e,this._hits=[],this._host?.requestUpdate()}draw(e,t){if(this._hits=[],!this._visible||0===this._items.length)return;const i=t.dpr,s=t.plotWidth*i,r=t.plotHeight*i,n=t.theme.axisText,o=200*i,a=e=>t.timeScale.indexToX(t.dataLayer.timeToIndexFloat(e.time))*i,l=this._priceScale?.(t)??t.priceScale,h=e=>l.priceToY(e.price)*i,c=(e,t,i,n)=>Math.max(e,i)<0||Math.min(e,i)>s||Math.max(t,n)<0||Math.min(t,n)>r;e.save(),e.lineJoin="round",e.lineCap="round";for(const t of this._items){if("label"===t.kind){const l=Ls(t)||void 0!==t.verticalAlign,c=a(t.at),d=h(t.at);if(!l&&(c<-o||c>s+o||d<-o||d>r+o))continue;const u=rr(e,i,c,d,t.text,t.color??n,t.textColor,t.align??"center",t,l?{w:s,h:r}:void 0);u&&this._recordHit(t,u,i,l?{w:s,h:r}:void 0);continue}const l=t.color??n;if(e.lineWidth=Math.max(1,Math.round((t.lineWidth??1)*i)),"polyline"===t.kind){const i=t.points;if(i.length<2)continue;const n=new Array(i.length),o=new Array(i.length);let d=1/0,u=-1/0,_=1/0,p=-1/0,f=!0;for(let e=0;e<i.length;e++){if(!Number.isFinite(i[e].time)||!Number.isFinite(i[e].price)){f=!1;break}const t=a(i[e]),s=h(i[e]);if(!Number.isFinite(t)||!Number.isFinite(s)){f=!1;break}n[e]=t,o[e]=s,t<d&&(d=t),t>u&&(u=t),s<_&&(_=s),s>p&&(p=s)}if(!f)continue;const m="smooth"===t.curve,b=m?sr(n,o,!0===t.closed):null;if(m&&null===b)continue;if(b)for(const e of b.segments)for(let t=0;t<e.length;t+=2)d=Math.min(d,e[t]),u=Math.max(u,e[t]),_=Math.min(_,e[t+1]),p=Math.max(p,e[t+1]);const g=m?e.lineWidth/2:0;if(c(d-g,_-g,u+g,p+g))continue;if(m&&(e.save(),e.beginPath(),e.rect(0,0,s,r),e.clip()),e.beginPath(),e.moveTo(n[0],o[0]),b&&b.segments.length>0)for(const t of b.segments)e.bezierCurveTo(...t);else if(b)e.lineTo(b.points[1][0],b.points[1][1]);else for(let t=1;t<i.length;t++)e.lineTo(n[t],o[t]);!0===t.closed&&e.closePath(),void 0!==t.fillColor&&(e.globalAlpha=t.opacity??.12,e.fillStyle=t.fillColor,e.fill(),e.globalAlpha=1),e.setLineDash([]),e.strokeStyle=l,e.stroke(),m&&e.restore();continue}let d=a(t.from),u=h(t.from),_=a(t.to),p=h(t.to);if("box"===t.kind){const n=Ls(t)||void 0!==t.align||void 0!==t.verticalAlign;if(!n&&c(d,u,_,p))continue;if(n&&![d,u,_,p].every(Number.isFinite))continue;const o=Math.min(d,_),a=Math.min(u,p),h=Math.abs(_-d),f=Math.abs(p-u);if(n&&(!Number.isFinite(h)||!Number.isFinite(f)))continue;if(n&&(e.save(),e.beginPath(),e.rect(0,0,s,r),e.clip()),void 0!==t.fillColor&&(e.globalAlpha=t.opacity??.12,e.fillStyle=t.fillColor,e.fillRect(o,a,h,f),e.globalAlpha=1),e.setLineDash([]),e.strokeStyle=l,e.strokeRect(o,a,h,f),this._recordHit(t,{x:o,y:a,w:h,h:f},i,n?{w:s,h:r}:void 0),void 0!==t.text&&""!==t.text){const c="left"===t.align?o:"right"===t.align?o+h:o+h/2,d="top"===t.verticalAlign?a:"bottom"===t.verticalAlign?a+f:a+f/2;rr(e,i,c,d,t.text,l,t.textColor,t.align??"center",t,n?{w:s,h:r}:void 0)}n&&e.restore();continue}if(!0===t.extendLeft||!0===t.extendRight){const e=_-d;if(0===e)u=0,p=r;else{const i=(p-u)/e,r=d<=_;!0===t.extendLeft&&(r?(u-=i*d,d=0):(p-=i*_,_=0)),!0===t.extendRight&&(r?(p+=i*(s-_),_=s):(u+=i*(s-d),d=s))}}c(d,u,_,p)||(e.setLineDash(be(t.lineStyle,i)),e.strokeStyle=l,e.beginPath(),e.moveTo(d,u),e.lineTo(_,p),e.stroke())}e.setLineDash([]),this._drawTooltip(e,t,i,s,r,n),e.restore()}_recordHit(e,t,i,s){const r=e.id??e.tooltip;if(void 0===r)return;const n=s?Math.max(0,t.x):t.x,o=s?Math.max(0,t.y):t.y,a=s?Math.min(s.w,t.x+t.w):t.x+t.w,l=s?Math.min(s.h,t.y+t.h):t.y+t.h;s&&(a<=n||l<=o)||this._hits.push({id:r,tooltip:e.tooltip,x:n/i,y:o/i,w:(a-n)/i,h:(l-o)/i,clipped:void 0!==s})}_drawTooltip(e,t,i,s,r,n){const o=t.hoverId;if(null==o)return;const a=this._hits.find(e=>e.id===o&&void 0!==e.tooltip);if(void 0===a?.tooltip)return;const l=8*i,h=11*i*1.35*a.tooltip.split("\n").length+6*i,c=a.y*i-l-h>=0?a.y*i-l-h/2:(a.y+a.h)*i+l+h/2,d=Math.min(Math.max((a.x+a.w/2)*i,0),s);c<-h||c>r+h||rr(e,i,d,c,a.tooltip,n,void 0,"center",void 0,a.clipped?{w:s,h:r}:void 0)}hitTest(e,t){for(let i=this._hits.length-1;i>=0;i--){const s=this._hits[i];if(e>=s.x&&e<=s.x+s.w&&t>=s.y&&t<=s.y+s.h)return{externalId:s.id,zOrder:"normal",distance:0,cursor:void 0===s.tooltip?"pointer":"default"}}return null}}class or{constructor(){Object.defineProperty(this,"_colors",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_anchor",{enumerable:!0,configurable:!0,writable:!0,value:NaN}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_visible",{enumerable:!0,configurable:!0,writable:!0,value:!0})}attached(e){this._host=e}detached(){this._host=null}zOrder(){return"bottom"}autoscaleInfo(){return null}setColors(e,t){this._colors=e,this._anchor=t.length>0?t[0].time:NaN,this._host?.requestUpdate()}setVisible(e){this._visible=e,this._host?.requestUpdate()}draw(e,t){const i=this._colors,s=i.length;if(!this._visible||0===s)return;const r=Math.round(t.dataLayer.timeToIndexFloat(this._anchor)),n=t.timeScale.visibleRange(),o=Math.max(0,Math.floor(n.from-r)),a=Math.min(s-1,Math.ceil(n.to-r)),l=t.dpr,h=t.plotWidth*l,c=t.plotHeight*l,d=e=>Math.min(h,Math.max(0,Math.round(t.timeScale.indexToX(r+e-.5)*l)));e.save();for(let t=o,s=o;t<=a;t++){const r=i[t];if(!(t<a&&i[t+1]===r)){if(r){const i=d(s);e.fillStyle=r,e.fillRect(i,0,d(t+1)-i,c)}s=t+1}}e.restore()}}function ar(e,t){return e===t?0!==e||1/e==1/t:e!=e&&t!=t}function lr(e,t){const i=t[ns(e).opacity];return"number"==typeof i&&Number.isFinite(i)?i:100}function hr(e,t){if("string"!=typeof e||t>=100)return e;const i=Z(e);return null===i?e:`rgba(${i.r},${i.g},${i.b},${i.a*Math.max(0,t)/100})`}class cr{constructor(){Object.defineProperty(this,"_times",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_ordered",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"_kept",{enumerable:!0,configurable:!0,writable:!0,value:-1}),Object.defineProperty(this,"_pass",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_records",{enumerable:!0,configurable:!0,writable:!0,value:new WeakMap})}begin(e){const t=this._times,i=t.length,s=e.length;let r=this._ordered&&s>=i;for(let s=0;s<i&&r;s++)r=e[s].time===t[s];r||(t.length=0,this._ordered=!0);for(let i=t.length;i<s;i++){const s=e[i].time;i>0&&!(s>t[i-1])&&(this._ordered=!1),t.push(s)}this._kept=r&&this._ordered?i:-1,this._pass++}clear(e){this._records.delete(e),e.setData([])}writeValues(e,t,i,s,r,n){if(void 0===i)return void this.clear(e);const o=s.length,{colorBy:a,colorParts:l}=t,h=void 0!==a||void 0!==l,c=h?lr(t,n):100,[d,u]=this._claim(e,1,1),_=d.cols[0],p=d.colors[0],f=[];let m=u<0;for(let e=0;e<o;e++){const t=i[e],s=null==t?NaN:t;let o;h&&Number.isFinite(s)&&(o=hr(l?.({value:s,index:e,values:r,settings:n})?.body??a?.({value:s,index:e,values:r,settings:n}),c)),!m&&(e>=u||!ar(_[e],s)||h&&p[e]!==o)&&f.push(e)>8&&(m=!0),_[e]=s,h&&(p[e]=o)}this._commit(e,d,m,f,o,e=>{const t={time:s[e].time,value:_[e]};return void 0!==p[e]&&(t.color=p[e]),t})}writeCandles(e,t,i,s,r,n,o){const a=s.length,l=[i.open,i.high,i.low,i.close].map(e=>{const i=r[e];if(void 0===i||i.length!==a)throw new Error(`openalgo-charts: ${o} plot "${t.key}" ohlc column "${e}" must be ${a} values`);return i}),{colorBy:h,colorParts:c}=t,d=void 0!==h||void 0!==c?lr(t,n):100,[u,_]=this._claim(e,4,3),[p,f,m,b]=u.cols,[g,v,y]=u.colors,w=[];let x=_<0;for(let e=0;e<a;e++){const t=l[3][e],i=null===t?NaN:t,s=l[0][e]??NaN,o=l[1][e]??NaN,a=l[2][e]??NaN;let u,S,P;if(Number.isFinite(i)){u=h?.({value:i,index:e,values:r,settings:n});const t=c?.({value:i,index:e,values:r,settings:n});void 0!==t&&(void 0!==t.body&&(u=t.body),S=hr(t.wick,d),P=hr(t.border,d)),u=hr(u,d)}x||!(e>=_)&&ar(p[e],s)&&ar(f[e],o)&&ar(m[e],a)&&ar(b[e],i)&&g[e]===u&&v[e]===S&&y[e]===P||!(w.push(e)>8)||(x=!0),p[e]=s,f[e]=o,m[e]=a,b[e]=i,g[e]=u,v[e]=S,y[e]=P}this._commit(e,u,x,w,a,e=>{const t={time:s[e].time,open:p[e],high:f[e],low:m[e],close:b[e]};return void 0!==g[e]&&(t.color=g[e]),void 0!==v[e]&&(t.wickColor=v[e]),void 0!==y[e]&&(t.borderColor=y[e]),t})}_claim(e,t,i){let s=this._records.get(e);const r=void 0!==s&&s.pass===this._pass-1?this._kept:-1;if(void 0===s)s={pass:-1,cols:Array.from({length:t},()=>[]),colors:Array.from({length:i},()=>[])},this._records.set(e,s);else if(r<0)for(const e of[...s.cols,...s.colors])e.length=0;return[s,r]}_commit(e,t,i,s,r,n){if(i){const t=new Array(r);for(let e=0;e<r;e++)t[e]=n(e);e.setData(t)}else if(0===r)e.setData([]);else{for(const t of s)e.update(n(t));s[s.length-1]!==r-1&&e.update(n(r-1))}t.pass=this._pass}}function dr(e){if("chart"!==e&&"underlying"!==e)throw new TypeError(`openalgo-charts: a study bar source is 'chart' or 'underlying', not ${String(e)}`);return e}function ur(e,t,i){let s=0,r=e;for(;s<r;){const e=s+r>>1;t(e)<i?s=e+1:r=e}return s}class _r extends Error{constructor(e){super(`openalgo-charts: unknown interval "${e}". Register it with registerInterval() or use a built-in token (1s, 1m, 5m, 1h, D, W).`),Object.defineProperty(this,"code",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.name="UnknownIntervalError",this.code=e}}const pr=/^(\d*)\s*([smhdw])$/i,fr={s:1,m:60,h:3600,d:86400,w:604800};const mr=new Map;function br(e){return e.trim().toLowerCase()}function gr(e){const t=br(e.code);if(""===t)throw new Error("openalgo-charts: interval code must not be empty");return function(e){if("interval"===e.mode&&!(e.seconds>0))throw new Error("openalgo-charts: interval bucketing needs seconds > 0");if("ticks"===e.mode&&!(e.count>=1))throw new Error("openalgo-charts: tick bucketing needs count >= 1");if("volume"===e.mode&&!(e.perBar>0))throw new Error("openalgo-charts: volume bucketing needs perBar > 0");if("calendar"===e.mode&&void 0!==e.count&&!(e.count>=1))throw new Error("openalgo-charts: calendar bucketing needs count >= 1")}(e.bucketing),mr.set(t,e),()=>{mr.get(t)===e&&mr.delete(t)}}function vr(e){return mr.delete(br(e))}function yr(){return Array.from(mr.values())}function wr(e){const t=e.trim(),i=mr.get(t.toLowerCase());if(void 0!==i)return i;const s=function(e){if(/^\d*\s*M$/.test(e))return null;const t=pr.exec(e);return null===t?null:{mode:"interval",seconds:(""===t[1]?1:Number(t[1]))*fr[t[2].toLowerCase()]}}(t);return null===s?null:{code:t,bucketing:s}}function xr(e){return null!==wr(e)}function Sr(e){const t=wr(e);if(null===t)throw new _r(e);return t}function Pr(e){return"interval"===e.mode||"calendar"===e.mode}const Or={month:1,quarter:3,year:12};function kr(e){return Or[e.unit]*Math.max(1,Math.floor(e.count??1))}function Ir(e,t){return e.timezone??t??ht}function Mr(e,t,i){const s=ft(t,i),r=kr(e);return Math.floor((12*s.year+(s.month-1))/r)*r}function Tr(e,t){return kt(Math.floor(e/12),e%12+1,1,0,0,0,t)}function Cr(e,t,i){if("interval"===e.mode){const i=e.anchorSec??0;return i+Math.floor((t-i)/e.seconds)*e.seconds}if("calendar"!==e.mode)return t;const s=Ir(e,i);return Tr(Mr(e,t,s),s)}function jr(e,t,i){if("interval"===e.mode)return Cr(e,t)+e.seconds;if("calendar"!==e.mode)return null;const s=Ir(e,i);return Tr(Mr(e,t,s)+kr(e),s)}function Ar(e){const t=wr(e);if(null===t)return null;const i=t.bucketing;if("ticks"===i.mode)return{multiplier:i.count,unit:"tick"};if("volume"===i.mode)return{multiplier:i.perBar,unit:"other"};if("calendar"===i.mode)return{multiplier:kr(i),unit:"M"};const s=i.seconds;return s%604800==0?{multiplier:s/604800,unit:"W"}:s%86400==0?{multiplier:s/86400,unit:"D"}:s%3600==0?{multiplier:s/3600,unit:"h"}:s%60==0?{multiplier:s/60,unit:"m"}:{multiplier:s,unit:"s"}}function Lr(e){const t=wr(e)?.bucketing;return void 0!==t&&"interval"===t.mode?t.seconds:0}function Nr(e){const t=Lr(e);return t>0&&t%60!=0}function Rr(e){const t=Lr(e);return t>0&&t<86400}function Fr(e){return 86400===Lr(e)}function zr(e){return"ticks"===wr(e)?.bucketing.mode}function Er(e,t,i){if(!ts(t))throw new TypeError("Invalid indicator plot price scale map");const s=new Set(e.plots.map(e=>e.key));return Reflect.ownKeys(t).map(e=>{const r=Object.getOwnPropertyDescriptor(t,e);if(!("string"==typeof e&&s.has(e)&&r.enumerable&&"value"in r&&(L(r.value)||i&&null===r.value)))throw new TypeError("Invalid indicator plot price scale assignment");return[e,r.value]})}function Br(e,t){return void 0===t?{}:Object.fromEntries(Er(e,t,!1))}function $r(e,t,i){return(Object.prototype.hasOwnProperty.call(i,e.key)?i[e.key]:void 0)??(!0===e.overlay?null:t)??e.priceScaleId??"right"}function Dr(e,t,i){const s=e.plots.find(e=>!0!==e.overlay);return s?$r(s,t,i):t??"right"}function Wr(e,t,i,s,r,n=0){const o=e.plots.find(e=>e.key===t.between[0]),a=e.plots.find(e=>e.key===t.between[1]),l=!0===t.overlay?n:r;if(o&&(!0===o.overlay?n:r)!==l||a&&(!0===a.overlay?n:r)!==l)return null;const h=!0===t.overlay?"right":Dr(e,i,s),c=o?$r(o,i,s):h;return c===(a?$r(a,i,s):h)?c:null}function qr(e,t,i,s,r=0){if(void 0!==t&&!L(t))throw new TypeError("Invalid indicator price scale");const n=Br(e,i);if((void 0!==t||0!==Object.keys(n).length)&&(e.fills??[]).some(i=>null===Wr(e,i,t??null,n,s,r)))throw new RangeError("Indicator fill endpoints must share their pane and price scale")}function Vr(e){const t={};for(const i of os(e))t[i.key]=i.default;return t}function Hr(e,t){if(void 0!==t&&t>0)return e.toFixed(f(t));const i=Math.abs(e);return i>=1e9?`${(e/1e9).toFixed(2)}B`:i>=1e6?`${(e/1e6).toFixed(2)}M`:i>=1e3?e.toFixed(0):i>=1?e.toFixed(2):e.toPrecision(3)}let Gr=1,Ur=1,Kr=!1;class Yr extends es{}class Xr{constructor(e,t,i={},s,r,n,o,a,l,h,c=!1){Object.defineProperty(this,"id",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"indicatorId",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"paneIndex",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_d",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_ownPane",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_settings",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_scaleOverride",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_plotScaleOverrides",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_policy",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_zoned",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_series",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_plotTypes",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_fills",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_levels",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_levelSig",{enumerable:!0,configurable:!0,writable:!0,value:""}),Object.defineProperty(this,"_values",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"_writes",{enumerable:!0,configurable:!0,writable:!0,value:new cr}),Object.defineProperty(this,"_barCount",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_firstTime",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_lastTime",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_removed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_store",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"_attachedPrimitives",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_detach",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lifetime",{enumerable:!0,configurable:!0,writable:!0,value:new AbortController}),Object.defineProperty(this,"_dataStatus",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_dataRetry",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_dataListeners",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_legend",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_markers",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_markerSeries",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_markerLayers",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_table",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_tables",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_draws",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_drawLayers",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_background",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_bgLayers",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_alertTime",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_alertRead",{enumerable:!0,configurable:!0,writable:!0,value:-1/0}),Object.defineProperty(this,"_alertPolicy",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_calculationEpoch",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_live",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_sourceId",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_sourceRevision",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_sourceHistoryRevision",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_sourceLastTime",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_visible",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"_constructed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_calcFailed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_lifecycleStatus",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_generation",{enumerable:!0,configurable:!0,writable:!0,value:Ur++}),Object.defineProperty(this,"_outputRevision",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_outputHistoryRevision",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_outputSource",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_studySnapshots",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_dependencyUnavailable",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_alertNeedsSeed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_outputPending",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_publishedBarColors",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_barSource",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_restored",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_sampled",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._restored=c,this._host=e,this._d=t,this._barSource=void 0===h?"chart":dr(h),this._policy=void 0===l?Object.freeze({}):js(l),this._scaleOverride=o??null,this._plotScaleOverrides=Br(t,a),this.indicatorId=t.id,this.name=t.name,this._alertPolicy=new xs(t.alerts??[]);let d=r;if(void 0===d)do{d=`${t.id}-${Gr++}`}while(n?.has(d));this.id=d,this._settings=this._validatedSettings({...ps(t),...Vr(t),...ks(i)}),void 0!==s?(this.paneIndex=s,this._ownPane=!1):"onchart"===t.placement?(this.paneIndex=this._pricePane(),this._ownPane=!1):(this.paneIndex=e.nextPaneIndex(),this._ownPane=!0),qr(t,o,void 0===a?void 0:this._plotScaleOverrides,this.paneIndex,this._pricePane());for(const i of t.plots){const t=this._plotType(i);this._plotTypes.set(i.key,t),this._series.set(i.key,e.addIndicatorSeries(t,this._plotPane(i),this._plotStyle(i),this._plotScale(i),i.priceFormat))}for(const i of t.fills??[]){const t=new ir({colorUp:this._fillColor(i,!0),colorDown:this._fillColor(i,!1),opacity:i.opacity??.12});this._fills.push(t),e.addIndicatorFill(t,!0===i.overlay?this._pricePane():this.paneIndex);const s=this._fillScale(i,this._scaleOverride);null!==s&&e.bindIndicatorPrimitiveScale?.(t,s)}this._legend=e.addIndicatorLegend({id:`indicator:${this.id}`,title:t.name,params:this._paramSummary(),color:this._legendColor(),row:e.legendRowsOn(this.paneIndex),paneIndex:this.paneIndex,hasSource:!0===t.hasSource}),this._applyRange();try{this.recompute()}catch(e){throw this.remove({force:!0}),e}this._constructed=!0,this._attach()}_plotPane(e){return!0===e.overlay?this._pricePane():this.paneIndex}_pricePane(){return this._host.primaryPaneIndex?.()??0}_plotScale(e,t=this._scaleOverride,i=this._plotScaleOverrides){return $r(e,t,i)}_localScale(e=this._scaleOverride,t=this._plotScaleOverrides){return Dr(this._d,e,t)}_fillScale(e,t,i=this.paneIndex,s=this._plotScaleOverrides){return Wr(this._d,e,t,s,i,this._pricePane())}_targetKey({plot:e,overlay:t}){if(void 0===e)return!0===t?null:void 0;if(!0===t||!this._d.plots.some(t=>t.key===e))throw new Error("Indicator output target must name one declared plot or the price pane");return e}_targets(){return[null,...this._d.plots.map(e=>e.key)]}_route(e,t){const i=[],s=new Map(this._targets().map(e=>[e,[]]));for(const r of e){const e=this._targetKey(r);(void 0===e||t?.(e)?i:s.get(e)).push(r)}for(const[e,t]of s)0===t.length&&s.delete(e);return[s.size>0?i:e,s]}_overlayTarget(e){return null===e||this._d.plots.some(t=>t.key===e&&!0===t.overlay)}_drawTarget(e,t=this._scaleOverride,i=this._plotScaleOverrides){const s=this._d.plots.find(t=>t.key===e);return s?[this._plotPane(s),this._plotScale(s,t,i)]:[this._pricePane(),null]}priceScaleId(){return this._scaleOverride}plotPriceScaleId(e){const t=this._d.plots.find(t=>t.key===e);return t?this._plotScale(t):null}plotPriceScaleIds(){return{...this._plotScaleOverrides}}canRelocate(e){return(this._d.fills??[]).every(t=>null!==this._fillScale(t,this._scaleOverride,e))}adoptPriceScale(e){this._scaleOverride=e,this._plotScaleOverrides=this._overlayScaleOverrides()}_overlayScaleOverrides(){return Object.fromEntries(this._d.plots.filter(e=>!0===e.overlay&&Object.prototype.hasOwnProperty.call(this._plotScaleOverrides,e.key)).map(e=>[e.key,this._plotScaleOverrides[e.key]]))}policy(){return this._policy}barSource(){return this._barSource}setBarSource(e,t){const i=dr(e);return!(this._removed||i===this._barSource||!this._allows("configurable",t))&&(this._barSource=i,this._barCount=0,this._requestRecompute(!0),this._host.emit?.("objects:change",{}),!0)}setPolicy(e){const t=null===e?Object.freeze({}):js(e);this._removed||(this._policy=t,this._host.policyChanged?.())}_allows(e,t){return!0===t?.force||!1!==this._policy[e]}setPriceScale(e,t){if(this._removed||!this._allows("configurable",t)||null!==e&&!L(e))return!1;const i=this._overlayScaleOverrides();return(e!==this._scaleOverride||Object.keys(i).length!==Object.keys(this._plotScaleOverrides).length)&&this._assignPriceScales(e,i,!1)}setPlotPriceScales(e,t){if(this._removed||!this._allows("configurable",t))return!1;let i;try{i=Er(this._d,e,!0)}catch{return!1}const s=new Map(Object.entries(this._plotScaleOverrides));let r=!1;for(const[e,t]of i)null===t?r=s.delete(e)||r:s.get(e)!==t&&(s.set(e,t),r=!0);return r&&this._assignPriceScales(this._scaleOverride,Object.fromEntries(s),!0)}_assignPriceScales(e,t,i){const s=this._d.plots.filter(e=>i||!0!==e.overlay),r=new Map;for(const i of s){const s=this._plotScale(i,e,t);if(s===this._plotScale(i))continue;const n=this._plotPane(i),o=r.get(n)??new Set;o.add(s),r.set(n,o)}const n=s.flatMap(i=>{const s=this._plotScale(i,e,t);if(!r.get(this._plotPane(i))?.has(s))return[];const n=this._series.get(i.key);return n?[{api:n,scaleId:s}]:[]}),o=[];for(let s=0;s<this._fills.length;s++){const r=this._fillScale(this._d.fills[s],e,this.paneIndex,t);if(null===r)return!1;!i&&!0===this._d.fills[s].overlay||r===this._fillScale(this._d.fills[s],this._scaleOverride)||o.push({primitive:this._fills[s],scaleId:r})}const a=this._localScale(e,t);if(a!==this._localScale())for(const e of[...this._levels,this._draws,...this._attachedPrimitives])null!==e&&o.push({primitive:e,scaleId:a});for(const[i,s]of[...this._drawLayers,...this._bgLayers]){const r=this._drawTarget(i,e,t)[1];null!==r&&r!==this._drawTarget(i)[1]&&o.push({primitive:s,scaleId:r})}const l=this._settings,h=this._plotScaleOverrides,c=this._scaleOverride,d=this.paneIndex,u=this._host.setIndicatorRange||this._ownPane?this._d.range?.(this._descriptorSettings())??null:null;if(this._removed||l!==this._settings||h!==this._plotScaleOverrides||c!==this._scaleOverride||d!==this.paneIndex)return!1;const _=this._host.assignIndicatorScale?.(this.id,n,o,()=>{this._scaleOverride=e,this._plotScaleOverrides=t,this._applyRange(u)})??!1;return _&&this.updateLegendValues(this._host.legendIndex?.()),_}_paramSummary(){const e=[];for(const t of this._d.inputs){if("color"===t.type||"boolean"===t.type)continue;const i=this._settings[t.key];if(null!=i&&""!==i)if("source"===t.type&&t.allowStudyOutputs&&"object"==typeof i){const t=i;e.push(`${t.instanceId}/${t.plotKey}`)}else e.push(String(i))}return e.join(" ")}_legendColor(){for(const e of this._d.plots){const t=e.colorKey;if(void 0!==t&&"string"==typeof this._settings[t])return this._settings[t];if("string"==typeof e.style?.color)return e.style.color}}visible(){return this._visible}setVisible(e){if(this._removed||e===this._visible)return;this._visible=e;for(const t of this._d.plots)this._series.get(t.key)?.applyOptions({visible:e&&!1!==t.style?.visible});for(const t of this._fills)t.setVisible(e);const t=this._host.sourceBars();this._applyLevels(t,this._descriptorSettings()),this._syncMarkers(t);try{this._syncTable(t)}catch(e){this._calcFailed=!0,this._publishStatus({state:"error",error:e})}this._syncBarColors(t),this._draws?.setVisible(e);for(const t of[...this._drawLayers.values(),...this._bgLayers.values()])t.setVisible(e);this._background?.setVisible(e),this._legend?.setOptions({hidden:!e}),this._host.emit?.("objects:change",{})}shiftPane(e){this.paneIndex+=e}renderResources(){const e=this._d.plots.flatMap(e=>{const t=this._series.get(e.key);return t?[{api:t,overlay:!0===e.overlay}]:[]}),t=this._fills.map((e,t)=>({primitive:e,overlay:!0===this._d.fills?.[t].overlay}));for(const{table:e,overlay:i}of this._tables.values())t.push({primitive:e,overlay:i});Kr&&(e.length=0,t.length=0);const i=(...i)=>{for(const s of i)null===s||Kr||t.push({primitive:s,overlay:s===this._markers&&(this._markerSeries===this._host.primarySeries?.()||e.some(e=>e.api===this._markerSeries&&e.overlay))})},s=e=>{for(const i of this._targets()){const s=e(i);s&&t.push({primitive:s,overlay:this._overlayTarget(i)})}};return i(this._legend,...this._levels,this._markers),s(e=>this._markerLayers.get(e)?.[0]),i(this._table,this._draws),s(e=>this._drawLayers.get(e)),i(this._background),s(e=>this._bgLayers.get(e)),i(...this._attachedPrimitives),{series:e,primitives:t}}relocate(e){this._ownPane&&!this._host.setIndicatorRange&&this._host.setPaneRange(this.paneIndex,null),this.paneIndex=e,this._applyRange(),this._syncMarkers(this._host.sourceBars())}refreshBarColors(){Kr||(this._calcFailed?this.republishBarColors():this._syncBarColors(this._host.sourceBars()))}republishBarColors(){this._d.barColors&&this._host.setBarColors?.(this._publishedBarColors,this.id)}legend(){return this._legend}updateLegendValues(e){if(null===this._legend)return;const t=this._barCount,i=void 0===e||e<0||e>=t?t-1:e;if(i<0)return void this._legend.setValues([]);const s=[];for(const e of this._d.plots){const t=this._values[e.ohlc?.close??e.key]?.[i-(e.offset??0)];if(null==t||!Number.isFinite(t))continue;const r=this._plotPane(e),n=this._plotColor(e);if(void 0!==n&&se(n))continue;const o=this._host.formatPrice?.(r,t,this._series.get(e.key))??Hr(t,this._host.tickSize?.(r));s.push({text:o,color:n})}this._legend.setValues(s)}_fillColor(e,t){const i=t?e.colorUpKey:e.colorDownKey,s=void 0!==i?this._settings[i]:void 0;if("string"==typeof s)return s;return(t?e.colorUp:e.colorDown)??(t?"#26a69a":"#ef5350")}_syncFills(e){const t=this._d.fills??[],i=this._descriptorSettings();for(let s=0;s<t.length;s++){const r=this._fills[s];if(void 0===r)continue;const n=t[s],o=this._values[n.between[0]],a=this._values[n.between[1]];if(r.setOptions({colorUp:this._fillColor(n,!0),colorDown:this._fillColor(n,!1),opacity:n.opacity??.12,gradient:"function"==typeof n.gradient?n.gradient({bars:e,values:this._values,settings:i}):n.gradient}),void 0===o||void 0===a){r.setPoints([]);continue}const l=this._d.plots.find(e=>e.key===n.between[0])?.offset??0,h=[];for(let e=0;e<this._barCount;e++){const t=o[e]??null,s=a[e]??null,r={index:e,a:t,b:s,values:this._values,settings:i},c=n.colorBy?.(r),d=n.gradientBy?.(r);h.push({index:e+l,a:t,b:s,color:c,gradient:d})}r.setPoints(h)}}_syncMarkers(e){if(this._dependencyUnavailable)return;if(void 0===this._d.markers)return;const t=this._sampled,i=this._visible?this._d.markers({bars:e,values:this._values,settings:this._descriptorSettings()}).flatMap(i=>{const s=null===t?i.time:function(e,t,i,s){const r=ur(t.length,e=>t[e].time,e);if(t[r]?.time===e)return e;const n=ur(i.length,e=>i[e].time,e);return t[ur(s.length,e=>s[e],n)]?.time??null}(i.time,e,t.bars,t.sourceIndex);return null===s?[]:[s===i.time?i:{...i,time:s}]}):[],s=this._host.primarySeries?.()??void 0,r=this.paneIndex===this._pricePane(),n=("price"===this._d.markerAnchor&&r?s:void 0)??this._series.get(this._d.plots[0]?.key??""),o=e=>null===e?s:this._series.get(e),[a,l]=this._route(i,e=>o(e)===n&&!(!r&&this._overlayTarget(e)));l.size>0&&new er(0).setMarkers(i);let h=!1;null!==this._markers&&n!==this._markerSeries&&(this._host.removeIndicatorMarkers(this._markers),this._markers=null),null===this._markers&&a.length>0&&void 0!==n&&(this._markerSeries=n,this._markers=n.createMarkers(()=>{const e=this._host.primarySeries?.();return this.paneIndex===this._pricePane()&&null!=e&&n.priceScale()===e.priceScale()?this._host.sourceBars():[]})),this._markers?.setMarkers(a);for(const[e,[t,i]]of this._markerLayers)i!==o(e)||this._visible&&!l.has(e)?(this._host.removeIndicatorMarkers(t),this._markerLayers.delete(e)):this._visible||t.setMarkers([]);for(const[e,t]of l){let i=this._markerLayers.get(e);const s=o(e);if(void 0===i&&void 0!==s){const t=this._d.plots.find(t=>t.key===e);h=!0,this._markerLayers.set(e,i=[s.createMarkers(t&&(()=>{const e=this._host.primarySeries?.();return this._plotPane(t)===this._pricePane()&&null!=e&&s.priceScale()===e.priceScale()?this._host.sourceBars():[]})),s])}i?.[0].setMarkers(t)}this._restack(h)}_syncTable(e){if(this._dependencyUnavailable)return;if(void 0!==this._d.tables)return void this._syncTables(e);if(void 0===this._d.table)return;const t=this._visible?this._d.table({bars:e,values:this._values,settings:this._descriptorSettings()}):null,i=t?.rows??[];if(null===this._table){if(0===i.length)return;this._table=this._host.addIndicatorTable(this.paneIndex)}void 0!==t?.options&&this._table.setOptions(t.options),this._table.setRows(i)}_syncTables(e){if(!this._visible){for(const{table:e}of this._tables.values())e.setRows([]);return}const t=this._d.tables({bars:e,values:this._values,settings:this._descriptorSettings()}),i=new Set;for(const e of t){if("string"!=typeof e.id||""===e.id.trim()||i.has(e.id))throw new Error("Indicator table IDs must be nonempty and unique");i.add(e.id)}for(const[e,{table:t}]of this._tables)i.has(e)||(this._host.removeIndicatorTable(t),this._tables.delete(e));for(const e of t){const t=!0===e.overlay;let i=this._tables.get(e.id);void 0!==i&&i.overlay!==t&&(this._host.removeIndicatorTable(i.table),this._tables.delete(e.id),i=void 0),void 0===i&&(i={table:this._host.addIndicatorTable(t?this._pricePane():this.paneIndex),overlay:t},this._tables.set(e.id,i)),void 0!==e.options&&i.table.setOptions(e.options),i.table.setRows(e.rows)}}_syncDraws(e){if(void 0===this._d.draws)return;const t=this._d.draws({bars:e,values:this._values,settings:this._descriptorSettings()}),[i,s]=this._route(t);s.size>0&&(new nr).setItems(t),null===this._draws&&i.length>0&&void 0!==this._host.addIndicatorPrimitive&&(this._draws=new nr,this._draws.setVisible(this._visible),this._host.addIndicatorPrimitive(this._draws,this.paneIndex),this._host.bindIndicatorPrimitiveScale?.(this._draws,this._localScale())),this._draws?.setItems(i),this._syncRouted(this._drawLayers,s,(e,t)=>new nr(null===t?e=>e.readoutPriceScale:void 0),(e,t)=>e.setItems(t))}_syncRouted(e,t,i,s){let r=!1;for(const[i,s]of e)t.has(i)||(this._host.removeIndicatorPrimitive?.(s),e.delete(i));for(const[n,o]of t){let t=e.get(n);if(void 0===t){const[s,a]=this._drawTarget(n),l=this._host.addIndicatorPrimitive&&i(o,a);if(!l)continue;r=!0,e.set(n,t=l),t.setVisible(this._visible),this._host.addIndicatorPrimitive(t,s),null!==a&&this._host.bindIndicatorPrimitiveScale?.(t,a)}s(t,o)}this._restack(r)}_restack(e){if(e&&this._constructed){Kr=!0;try{this._host.resourcesChanged?.()}finally{Kr=!1}}}_syncBackground(e){if(void 0===this._d.background)return;const t=this._d.background({bars:e,values:this._values,settings:this._descriptorSettings()}),i=t.some(e=>"object"==typeof e&&null!==e);if(i)for(const e of t)if(!Array.isArray(e?.colors))throw new Error("Indicator background must return colours or a list of columns");const[s,r]=i?this._route(t):[[{colors:t}],new Map];for(const e of[s,...r.values()])if(e.length>1)throw new Error("Indicator background allows one column per target");const n=s[0]?.colors??[];null===this._background&&n.length>0&&void 0!==this._host.addIndicatorPrimitive&&(this._background=new or,this._background.setVisible(this._visible),this._host.addIndicatorPrimitive(this._background,this.paneIndex)),this._background?.setColors(n,e),this._syncRouted(this._bgLayers,r,([e])=>e.colors.length>0?new or:null,(t,[i])=>t.setColors(i.colors,e))}_syncBarColors(e){if(void 0===this._d.barColors)return;const t=this._visible&&!this._dependencyUnavailable?this._d.barColors({bars:e,values:this._values,settings:this._descriptorSettings()}):null;this._publishedBarColors=t,this._host.setBarColors?.(t,this.id)}_syncAlerts(e,t,i,s,r,n){const o=this._d.alerts,a=e.length;if(void 0===o||!n())return;const l=this._alertTime,h=ws(e,this._sampled);a>0&&(this._alertTime=e[a-1].time);let c=a;if(i&&!r)for(;c>0&&e[c-1].time>l;)c--;else this._alertRead=a>0?h(a-1):-1/0;let d,u=!1;for(let i=c;i<a;i++){if(h(i)<=this._alertRead)continue;this._alertRead=h(i);const s=ys(e,this._values,t,i);for(const t of o)if(void 0===t.frequency){if(!n())return;try{const r=t.when(s);if(!n())return;if(!r)continue;const o="function"==typeof t.message?t.message(s):t.message??t.title;if(!n())return;this._host.emit?.("indicator:alert",{indicatorId:this.indicatorId,instanceId:this.id,alertId:t.id,title:t.title,message:o,time:e[i].time,index:i})}catch(e){if(!n())return;u||(u=!0,d=e)}}}if(n()){try{this._alertPolicy.evaluate({bars:e,values:this._values,settings:t,calculation:s,tailOnly:i,refresh:r,current:n,from:c,sampled:this._sampled},e=>{this._host.emit?.("indicator:alert",{...e,indicatorId:this.indicatorId,instanceId:this.id})})}catch(e){u||(u=!0,d=e)}if(u&&n())throw d}}_calcContext(e,t,i){const s=e.length,r=()=>this._host.now?.()??Date.now()/1e3,n=this._host.interval?.(),o=this._host.timezone?.()??ht,a=s>1?e[s-1].time-e[s-2].time:0;let l=0===s,h=0===s?"empty":"unknown";if(s>0){const t=e[s-1].time;if(void 0===n)l=a<=0||r()>=t+a,h="clock";else{const e=wr(n)?.bucketing,i="interval"===e?.mode?e.seconds>0?t+e.seconds:null:void 0===e?null:jr(e,t,o);l=null!==i&&r()>=i,null!==i&&(h="clock")}}s>0&&void 0!==i?.confirmation&&(l="confirmed"===i.confirmation,h=i.confirmationSource??"provider");const c=null===this._sourceRevision||i?.sourceId!==this._sourceId,d=void 0!==i&&i.revision!==this._sourceRevision,u=void 0===i?this._live:!c&&d&&"live"===i.provenance;return{...void 0===i?{}:{execution:{sourceId:i.sourceId,provenance:"replay"===i.provenance?"replay":u?"live":"history",change:c?"initial":d?i.change:"refresh",revision:i.revision,historyRevision:i.historyRevision,confirmationSource:h}},barState:{isNew:void 0===i?t:u&&s>0&&(void 0===this._sourceLastTime||e[s-1].time>this._sourceLastTime),isConfirmed:l,isRealtime:u,lastIndex:s-1},symbol:this._host.symbol?.(),interval:n,timezone:o,now:r,tickSize:this._host.tickSize?.(this._pricePane())||void 0}}_plotType(e){const t=this._settings[ns(e).type];return"string"==typeof t&&""!==t?t:e.type}_plotColor(e){const t=this._settings[ns(e).color];return"string"==typeof t?t:"string"==typeof e.style?.color?e.style.color:void 0}_plotStyle(e){const t=ns(e),i={...e.style??{},title:e.title,visible:this._visible&&!1!==e.style?.visible},s=this._plotColor(e),r=(n=this._settings[t.opacity],o=100,"number"==typeof n&&Number.isFinite(n)?n:o);var n,o;void 0!==s&&(i.color=r>=100?s:ie(s,r/100));const a=this._settings[t.width];"number"==typeof a&&a>0&&(i.lineWidth=a);const l=this._settings[t.lineStyle];return"string"==typeof l&&(i.lineStyle=l),void 0!==e.offset&&0!==e.offset&&(i.barOffset=e.offset),i}_attach(){this._dataRetry=null;const e=this._d.attach?.({dataContext:()=>this._host.dataContext?.(),subscribeDataChanges:e=>this._host.subscribeDataChanges?.(e)??(()=>{}),signal:this._lifetime.signal,setDataStatus:e=>{this._lifecycleStatus=Object.freeze({...e}),this._publishStatus(this._lifecycleStatus)},setDataRetry:e=>{this._removed||(this._dataRetry=e)},requestBars:e=>{const t=this._host.requestBars;return void 0===t?Promise.reject(new Error("openalgo-charts: this chart has no bars provider; call chart.setBarsProvider(...) to serve other instruments")):bs(i=>t.call(this._host,{...e,signal:i}),[e.signal,this._lifetime.signal])},requestSnapshot:e=>bs(t=>{const i=this._host.requestSnapshot;if(!i)throw new Error("Requested snapshots are unsupported by this host");return i.call(this._host,{...e,signal:t})},[e.signal,this._lifetime.signal]),requestState:this._host.requestState?()=>this._host.requestState():void 0,subscribeRequestChanges:this._host.subscribeRequestChanges?e=>this._host.subscribeRequestChanges(e):void 0,settings:()=>this._descriptorSettings(),bars:()=>("underlying"===this._barSource?this._host.underlyingBars?.()?.bars:void 0)??this._host.sourceBars(),requestRecompute:()=>{this._removed||(this._barCount=0,this._requestRecompute(!0))},store:this._store,symbol:()=>this._host.symbol?.(),interval:()=>this._host.interval?.(),timezone:()=>this._host.timezone?.()??ht,now:()=>this._host.now?.()??Date.now()/1e3,paneIndex:()=>this.paneIndex,addPrimitive:e=>{this._attachedPrimitives.add(e),this._host.addIndicatorPrimitive?.(e,this.paneIndex),this._host.bindIndicatorPrimitiveScale?.(e,this._localScale())},removePrimitive:e=>{this._attachedPrimitives.delete(e),this._host.removeIndicatorPrimitive?.(e)},emit:(e,t)=>{this._host.emit?.(e,t)}});this._detach="function"==typeof e?e:null}_publishStatus(e){if(this._removed)return;const t=this._dataStatus;if(t?.state!==e.state||"error"===e.state&&("error"!==t.state||t.error!==e.error)){this._dataStatus=Object.freeze({...e}),this._outputRevision++,this._outputHistoryRevision++,this._host.indicatorOutputChanged?.(this.id,!0);for(const e of this._dataListeners)e(this._dataStatus);this._host.emit?.("indicator:data-status",{id:this.id,indicatorId:this.indicatorId,status:this._dataStatus})}}dataStatus(){return this._dataStatus}subscribeDataStatus(e){return this._removed?()=>{}:(this._dataListeners.add(e),null!==this._dataStatus&&e(this._dataStatus),()=>{this._dataListeners.delete(e)})}retryData(){this._removed||this._dataRetry?.()}dependencyNode(){return{id:this.id,descriptor:this._d,settings:this._settings}}invalidateStudyOutput(){this._outputPending=!0}_validatedSettings(e){const t=ks(e);ss(this._d.inputs,t),Ts([{id:this.id,descriptor:this._d,settings:t}]),this._host.validateIndicatorSettings?.(this.id,this._d,t);for(const e of this._d.inputs)"source"===e.type&&"object"==typeof t[e.key]&&null!==t[e.key]&&Object.freeze(t[e.key]);return t}studyOutput(e){const t=this._d.plots.find(t=>t.key===e);if(t&&!t.ohlc)return{generation:this._generation,revision:this._outputRevision,historyRevision:this._outputHistoryRevision,source:this._outputSource,values:this._values[e]??[],available:!this._removed&&this._outputRevision>0&&!this._outputPending&&!this._calcFailed&&!this._dependencyUnavailable&&(null===this._lifecycleStatus||"ready"===this._lifecycleStatus.state)}}_studyBindings(e,t){const i=Ts([this.dependencyNode()]).dependencies.get(this.id)??[],s=new Map,r=new Map;let n=this._studySnapshots.size===i.length;for(const{inputKey:o,source:a}of i){const i=this._host.studyOutput?.(a);if(!i?.available||i.values.length!==e.length||void 0!==t&&(i.source?.sourceId!==t.sourceId||i.source.revision!==t.revision||i.source.historyRevision!==t.historyRevision))throw new Yr(`Study input ${o} is unavailable: ${a.instanceId}/${a.plotKey}`);s.set(o,i),r.set(o,Object.freeze(i.values.slice()));const l=this._studySnapshots.get(o);(!l||l.generation!==i.generation||l.historyRevision!==i.historyRevision||l.revision>i.revision)&&(n=!1)}return{snapshots:s,canTail:n,current:()=>i.every(({inputKey:e,source:t})=>{const i=s.get(e),r=this._host.studyOutput?.(t);return!0===r?.available&&i.generation===r.generation&&i.revision===r.revision&&i.historyRevision===r.historyRevision}),resolve:e=>{const t=i.find(t=>t.source.instanceId===e.instanceId&&t.source.plotKey===e.plotKey);if(!t)throw new es("Study source must be declared by an opted-in input");return r.get(t.inputKey)}}}_requestRecompute(e){this._host.indicatorRecompute?this._host.indicatorRecompute(this.id,e,()=>this.recompute(e)):this.recompute(e)}_clearUnavailableOutput(e){this._dependencyUnavailable=!0,this._alertNeedsSeed=!0,this._studySnapshots.clear();const t=new Set([...Object.keys(this._values),...this._d.plots.map(e=>e.key)]);this._values=Object.fromEntries([...t].map(t=>[t,new Array(e.length).fill(null)])),this._barCount=0,this._outputHistoryRevision++;for(const e of this._series.values())this._writes.clear(e);for(const e of this._fills)e.setPoints([]);this._markers?.setMarkers([]);for(const[e]of this._markerLayers.values())e.setMarkers([]);this._table?.setRows([]);for(const{table:e}of this._tables.values())e.setRows([]);this._draws?.setItems([]);for(const e of this._drawLayers.values())e.setItems([]);for(const t of[this._background,...this._bgLayers.values()])t?.setColors([],e);for(const e of this._levels)this._host.removeIndicatorLevel(e);this._levels=[],this._publishedBarColors=null,this._host.setBarColors?.(null,this.id),this.updateLegendValues()}settings(){return ks(this._settings)}_descriptorSettings(){const e=this._host.timezone?.()??ht;if(e===ht)return this._settings;const t=this._zoned;if(null!==t&&t.zone===e&&t.base===this._settings)return t.merged;const i={...this._settings,timezone:e};return this._zoned={zone:e,base:this._settings,merged:i},i}series(e){return this._series.get(e)}values(){return this._host.flushIndicators?.(),this._values}setSettings(e,t){if(this._removed||!this._allows("configurable",t))return!1;this._settings=this._validatedSettings({...this._settings,...ks(e)}),this._outputPending=!0,this._host.indicatorOutputChanged?.(this.id,!0);for(const e of this._d.plots){const t=this._plotType(e);if(t!==this._plotTypes.get(e.key)){const i=this._series.get(e.key);if(i&&this._host.setIndicatorSeriesType?.(i,t)){i.applyOptions(this._plotStyle(e)),this._plotTypes.set(e.key,t);continue}this._series.get(e.key)?.remove(),this._series.set(e.key,this._host.addIndicatorSeries(t,this._plotPane(e),this._plotStyle(e),this._plotScale(e),e.priceFormat)),this._plotTypes.set(e.key,t);continue}this._series.get(e.key)?.applyOptions(this._plotStyle(e))}return this._legend?.setOptions({params:this._paramSummary(),color:this._legendColor()}),this._applyRange(),this._values={},this._barCount=0,this._requestRecompute(!0),this._detach?.(),this._attach(),this._host.resourcesChanged?.(),this._host.emit?.("objects:change",{}),!0}recompute(e=!1){if(this._removed)return;const t=++this._calculationEpoch,i=this._settings,s=this._host.sourceState?.(),r=this._host.sourceBars(),n="underlying"===this._barSource?this._host.underlyingBars?.()??null:null,o=n?.bars??r,a=n?.sourceIndex??null;let l;const h=()=>{if(this._removed||t!==this._calculationEpoch||i!==this._settings)return!1;if(l&&!l.current())return!1;const e=this._host.sourceState?.();return void 0===s?void 0===e:void 0!==e&&s.sourceId===e.sourceId&&s.revision===e.revision&&s.historyRevision===e.historyRevision&&s.provenance===e.provenance};try{l=this._studyBindings(o,s),this._recompute(e,o,null===a?o:r,a,s,l,h)}catch(e){if(!h())return;if(!(this._constructed||e instanceof Yr||this._restored&&e instanceof es))throw e;return e instanceof Yr&&this._clearUnavailableOutput(null===a?o:r),this._calcFailed=!0,this._publishStatus({state:"error",error:e}),void this._host.indicatorOutputChanged?.(this.id,!0)}h()&&(this._calcFailed&&(this._calcFailed=!1,this._publishStatus(this._lifecycleStatus??{state:"ready"})),h()&&this._host.indicatorOutputChanged?.(this.id,e))}_recompute(e,t,i,s,r,n,o){const a=i.length,l=this._descriptorSettings();let h=null;const c=this._barCount>0&&a>=this._barCount?i[this._barCount-1].time:NaN,d=(c===this._lastTime||void 0!==r&&c>this._lastTime)&&(1===this._barCount||i[0].time===this._firstTime),u=d&&a>this._barCount,_=d&&(void 0===r||r.sourceId===this._sourceId&&r.revision!==this._sourceRevision&&r.historyRevision===this._sourceHistoryRevision&&"live"===r.provenance);void 0===r&&_&&(this._live=!0),_&&c>this._lastTime&&this._redate(c,t===i);const p=this._calcContext(t,u,r);p.resolveSource=n.resolve,"chart"===this._barSource&&null!==(this._host.underlyingBars?.()??null)&&(p.transformed=!0);let f=!1;if(_&&a<=this._barCount+1&&null===s&&n.canTail&&void 0!==this._d.calcTail){const e=this._barCount-1,i=this._d.calcTail(t,l,e,this._values,this._store,p);null!==i&&(h=function(e,t,i,s){const r={};if(Object.keys(e).length!==Object.keys(t).length)return null;for(const n of Object.keys(t)){const o=e[n],a=t[n];if(void 0===o||void 0===a)return null;if(o.length!==i+1||a.length!==s-i)return null;const l=new Array(s);for(let e=0;e<i;e++)l[e]=o[e]??null;for(let e=i;e<s;e++)l[e]=a[e-i]??null;r[n]=l}return r}(this._values,i,e,a),f=!0)}if(null===h&&(h=this._d.calc(t,l,this._store,p)),o()){null!==s&&(h=function(e,t){const i={};for(const s of Object.keys(e)){const r=e[s];i[s]=t.map(e=>r[e]??null)}return i}(h,s)),this._sampled=null===s?null:{bars:t,sourceIndex:s},this._values=h,this._outputPending=!1,this._dependencyUnavailable=!1,this._studySnapshots=n.snapshots,this._outputRevision++,f||this._outputHistoryRevision++,this._outputSource=r?{...r}:void 0,this._barCount=a,this._firstTime=a>0?i[0].time:0,this._lastTime=a>0?i[a-1].time:0,void 0!==r&&(this._sourceId=r.sourceId,this._sourceRevision=r.revision,this._sourceHistoryRevision=r.historyRevision,this._sourceLastTime=t[t.length-1]?.time),this._writes.begin(i);for(const e of this._d.plots){const t=this._series.get(e.key);void 0!==t&&(void 0!==e.ohlc?this._writes.writeCandles(t,e,e.ohlc,i,h,l,this.indicatorId):this._writes.writeValues(t,e,h[e.key],i,h,l))}if(this._syncFills(i),this._syncMarkers(i),this._syncTable(i),this._syncDraws(i),this._syncBackground(i),this._syncBarColors(i),this._applyLevels(i,l),this._alertNeedsSeed){this._alertNeedsSeed=!1;const e={...p,execution:p.execution?{...p.execution,provenance:"history"}:void 0};this._syncAlerts(i,l,!1,e,!1,o)}else this._syncAlerts(i,l,_,p,e,o);o()&&this.updateLegendValues(this._host.legendIndex?.())}}_redate(e,t){this._alertTime===this._lastTime&&(this._alertTime=e),t&&(this._sourceLastTime=e,this._alertPolicy.redate(this._lastTime,e))}_applyLevels(e,t){if(void 0===this._d.levels)return;const i={...t,settings:t,bars:e,values:this._values},s=this._visible?this._d.levels(i):[];let r="";for(const e of s)r+=`${e.price}|${e.color??""}|${e.title??""}|${e.dashed??""}|${e.lineWidth??""}|${e.lineStyle??""};`;if(r===this._levelSig)return;this._levelSig=r;for(const e of this._levels)this._host.removeIndicatorLevel(e);this._levels=[];let n=0;for(const e of s){const t=e.lineStyle??(!1===e.dashed?"solid":"dashed");this._levels.push(this._host.addIndicatorLevel({price:e.price,color:e.color??"#8892a6",dashed:"dashed"===t,lineWidth:e.lineWidth??1,lineStyle:t,label:e.title??"",id:`${this.id}:level:${n++}`},this.paneIndex)),this._host.bindIndicatorPrimitiveScale?.(this._levels[this._levels.length-1],this._localScale())}}_applyRange(e){if(this._host.setIndicatorRange){const t=this._d.plots.filter(e=>!0!==e.overlay).flatMap(e=>{const t=this._series.get(e.key);return t?[t]:[]});return void this._host.setIndicatorRange(this.id,this.paneIndex,this._localScale(),void 0===e?this._d.range?.(this._descriptorSettings())??null:e,t)}this._ownPane&&this._host.setPaneRange(this.paneIndex,void 0===e?this._d.range?.(this._descriptorSettings())??null:e)}remove(e){if(this._removed||!this._allows("removable",e))return!1;this._removed=!0,this._lifetime.abort(),this._dataRetry=null,this._dataListeners.clear();try{this._detach?.()}catch{}this._detach=null;for(const e of this._attachedPrimitives)this._host.removeIndicatorPrimitive?.(e);this._attachedPrimitives.clear(),null!==this._legend&&(this._host.removeIndicatorLegend(this._legend),this._legend=null);for(const e of this._levels)this._host.removeIndicatorLevel(e);this._levels=[];for(const e of this._series.values())e.remove();this._series.clear();for(const e of this._fills)this._host.removeIndicatorFill(e);this._fills.length=0,null!==this._markers&&(this._host.removeIndicatorMarkers(this._markers),this._markers=null);for(const[e]of this._markerLayers.values())this._host.removeIndicatorMarkers(e);this._markerLayers.clear(),null!==this._table&&(this._host.removeIndicatorTable(this._table),this._table=null);for(const{table:e}of this._tables.values())this._host.removeIndicatorTable(e);this._tables.clear(),null!==this._draws&&(this._host.removeIndicatorPrimitive?.(this._draws),this._draws=null);for(const e of[...this._drawLayers.values(),...this._bgLayers.values()])this._host.removeIndicatorPrimitive?.(e);return this._drawLayers.clear(),this._bgLayers.clear(),null!==this._background&&(this._host.removeIndicatorPrimitive?.(this._background),this._background=null),this._host.setIndicatorRange?.(this.id,this.paneIndex,this._localScale(),null,[]),void 0!==this._d.barColors&&this._host.setBarColors?.(null,this.id),this._ownPane&&!this._host.setIndicatorRange&&this._host.setPaneRange(this.paneIndex,null),this._host.indicatorRemoved?.(this.id,!this._constructed&&this._ownPane?this.paneIndex:void 0),!0}}const Jr=["crossing","crossingUp","crossingDown","greaterThan","lessThan","enteringRange","leavingRange"];function Zr(e,t,i,s,r,n=s,o=r){if(!Number.isFinite(i))return!1;if("greaterThan"===e)return i>s;if("lessThan"===e)return i<s;if(void 0===t||!Number.isFinite(t))return!1;const a=t<=n&&i>s,l=t>=n&&i<s;if("crossing"===e)return a||l;if("crossingUp"===e)return a;if("crossingDown"===e)return l;const h=t>=n&&t<=o,c=i>=s&&i<=r;return"enteringRange"===e?!h&&c:h&&!c}function Qr(e,t,i,s,r,n,o=r,a=n){if(![t,i,s].every(Number.isFinite))return;const l=t<=o&&s>r,h=t>=o&&i<r;switch(e){case"crossing":return l||h?r:void 0;case"crossingUp":return l?r:void 0;case"crossingDown":return h?r:void 0;case"greaterThan":return s>r?s:void 0;case"lessThan":return i<r?i:void 0;case"enteringRange":return t<o&&s>=r?r:t>a&&i<=n?n:void 0;case"leavingRange":if(t<o||t>a)return;return i<r?r:s>n?n:void 0;case"matches":return}}const en=["session","adjustment","currency","unit"],tn={session:["regular","extended"],adjustment:["adjusted","raw"]};function sn(e){if(null==e)return;const t=e;if("object"!=typeof e||Array.isArray(e)||Object.keys(t).some(e=>!en.includes(e)))throw new TypeError("Invalid data variant");const i={};for(const e of en){const s=t[e],r=tn[e];if(void 0!==s){if(r?!r.includes(s):"string"!=typeof s||!s.trim()||s.length>32)throw new TypeError(`Invalid data ${e}: ${String(s)}`);i[e]=s}}return Object.keys(i).length?Object.freeze(i):void 0}function rn(e){const t=sn(e);return t?JSON.stringify(en.map(e=>t[e]??null)):""}function nn(e,t){const i=sn(t);return en.find(t=>void 0!==i?.[t]&&!(t=>e?.["currency"===t?"currencies":`${t}s`])(t)?.includes(i[t]))??null}function on(e,t){const i=new Error(`This source does not serve ${e} ${String(t?.[e])}`);return i.name="DataVariantUnsupportedError",i}const an=Symbol.for("openalgo-charts.data-context.passing");function ln(e,t){const i=sn(t?.variant),s=t&&{...t};s&&(delete s.variant,i&&(s.variant=i)),e.setDataContext(s),s&&rn(e.getDataContext()?.variant)!==rn(i)&&(e.setDataContext({...s,interval:void 0===s.interval?"":void 0,[an]:!0}),e.setDataContext(s))}const hn=e=>"string"==typeof e&&e.trim().length>0;function cn(e){if(!hn(e.id))throw new Error("Alert id must be nonempty");const t=e.source;if(!t||!["price","indicator","barCondition","drawing"].includes(t.kind))throw new Error("Unknown alert source");if("barCondition"===t.kind&&!hn(t.id))throw new Error("Invalid bar condition id");if(!("indicator"!==t.kind||hn(t.instanceId)&&hn(t.plotKey)))throw new Error("Invalid indicator source");if(!("drawing"!==t.kind||hn(t.drawingId)&&(void 0===t.level||hn(t.level))&&(void 0===t.input||t.input&&hn(t.input.instanceId)&&hn(t.input.plotKey))))throw new Error("Invalid drawing source");if("barCondition"===t.kind?"matches"!==e.condition:!Jr.includes(e.condition))throw new Error("Invalid source condition");if("price"===t.kind||"indicator"===t.kind){const i="price"===t.kind?t.price:t.value,s="price"===t.kind?t.upperPrice:t.upperValue;if(!Number.isFinite(i)||void 0!==s&&!Number.isFinite(s))throw new Error("Alert bounds must be finite");if(("enteringRange"===e.condition||"leavingRange"===e.condition)&&(void 0===s||s<i))throw new Error("Alert range requires ordered finite bounds")}if("onBarClose"!==e.policy&&"onTouch"!==e.policy)throw new Error("Unknown alert policy");if("once"!==e.repeat&&"everyTime"!==e.repeat)throw new Error("Unknown alert repeat");if(!["armed","triggered","disabled","expired"].includes(e.state))throw new Error("Unknown alert state");if(!Number.isFinite(e.cooldownSeconds)||e.cooldownSeconds<0)throw new Error("Invalid alert cooldown");if("string"!=typeof e.title||void 0!==e.message&&"string"!=typeof e.message)throw new Error("Alert title and message must be text");const i=un(e.scope);for(const e of["symbol","exchange","interval"])if(void 0!==i[e]&&"string"!=typeof i[e])throw new Error("Invalid alert scope");if(void 0!==i.variant)try{sn(un(i.variant))}catch{throw new Error("Invalid alert scope variant")}for(const t of["expiresAt","lastTriggeredAt","lastTriggeredTime","lastClosedTime","lastTouchedTime"])if(void 0!==e[t]&&!Number.isFinite(e[t]))throw new Error(`Invalid alert ${t}`)}function dn(e){return{...e,source:"drawing"===e.source.kind?{...e.source,...e.source.input?{input:{...e.source.input}}:{}}:{...e.source},scope:{...e.scope,...e.scope.variant?{variant:{...e.scope.variant}}:{}}}}function un(e){if(null===e||"object"!=typeof e||Array.isArray(e)||![Object.prototype,null].includes(Object.getPrototypeOf(e)))throw new Error("Alert document needs plain JSON records");for(const t of Reflect.ownKeys(e)){const i=Object.getOwnPropertyDescriptor(e,t);if("string"!=typeof t||!("value"in i)||!i.enumerable)throw new Error("Alert document needs JSON data properties")}return e}function _n(e){if(!Array.isArray(e)||e.length>1e5||Reflect.ownKeys(e).length!==e.length+1)throw new Error("Alert document array is not JSON data");return Array.from({length:e.length},(t,i)=>{const s=Object.getOwnPropertyDescriptor(e,String(i));if(!s||!("value"in s))throw new Error("Alert document array needs JSON data values");return s.value})}function pn(e){const t=new Set;let i=0,s=0;const r=(e,n)=>{if(++i>1e5||n>32)throw new Error("Alert payload exceeds JSON limits");if(null===e||"boolean"==typeof e)return e;if("string"==typeof e){if(s+=e.length,s>5242880)throw new Error("Alert payload exceeds JSON size limit");return e}if("number"==typeof e&&Number.isFinite(e))return e;if("object"!=typeof e)throw new Error("Alert payload must be finite JSON data");if(t.has(e))throw new Error("Alert payload contains a cycle");let o;if(t.add(e),Array.isArray(e))o=_n(e).map(e=>r(e,n+1));else{const t=un(e),i={};for(const e of Object.keys(t)){if(s+=e.length,s>5242880)throw new Error("Alert payload exceeds JSON size limit");Object.defineProperty(i,e,{value:r(t[e],n+1),enumerable:!0,writable:!0,configurable:!0})}o=i}return t.delete(e),o};return r(e,0)}function fn(e){if("string"==typeof e){if(e.length>5242880)throw new Error("Alert document exceeds JSON size limit");e=JSON.parse(e)}const t=Array.isArray(e)?{version:1,alerts:e}:un(e);if(1!==t.version||!Array.isArray(t.alerts)||t.alerts.length>1e4)throw new Error("Unsupported alert document");const i=new Set,s=_n(t.alerts).map(e=>{const t=un(e),s=un(t.source);void 0!==s.input&&un(s.input);const r=(e,i)=>void 0===t[e]?i:t[e],n=un(r("scope",{})),o={id:t.id,source:{...s},condition:r("condition","barCondition"===s.kind?"matches":"crossing"),policy:r("policy","onBarClose"),repeat:r("repeat","once"),state:r("state","armed"),title:r("title","Chart alert"),cooldownSeconds:r("cooldownSeconds",0),scope:pn(Object.fromEntries(Object.entries(n).filter(([,e])=>void 0!==e)))};for(const e of["message","expiresAt","lastTriggeredAt","lastTriggeredTime","lastClosedTime","lastTouchedTime"])void 0!==t[e]&&Object.assign(o,{[e]:t[e]});if(cn(o),i.has(o.id))throw new Error(`Duplicate alert id: ${o.id}`);return i.add(o.id),o.source=pn(Object.fromEntries(Object.entries(o.source).filter(([,e])=>void 0!==e))),void 0!==t.payload&&(o.payload=pn(t.payload)),o});return{version:1,alerts:s}}function mn(e,t){if(void 0!==e)for(const i of[...e])try{i(t)}catch(e){globalThis.reportError?.(e)}}function bn(e,t){return e.add(t),()=>{e.delete(t)}}const gn=["Mod","Ctrl","Meta","Alt","Shift"],vn=new Set(gn),yn=new Set(["Shift","Control","Alt","Meta","OS","ShiftLeft","ShiftRight","ControlLeft","ControlRight","AltLeft","AltRight","MetaLeft","MetaRight","OSLeft","OSRight"]);function wn(){return"undefined"!=typeof navigator&&/mac|iphone|ipad/i.test(navigator.platform||navigator.userAgent||"")}function xn(e){const t=e.split("+").map(e=>e.trim()).filter(e=>e.length>0);if(0===t.length)return null;const i=t[t.length-1];if(!/^[A-Za-z0-9]+$/.test(i)||vn.has(i))return null;const s=[];for(const e of t.slice(0,-1)){if(!vn.has(e))return null;s.includes(e)||s.push(e)}return s.sort((e,t)=>gn.indexOf(e)-gn.indexOf(t)),{mods:s,key:i}}function Sn(e){const t=xn(e);return null===t?"":[...t.mods,t.key].join("+")}function Pn(e){return""!==Sn(e)}const On={ArrowLeft:"←",ArrowRight:"→",ArrowUp:"↑",ArrowDown:"↓",Equal:"=",Minus:"-",NumpadAdd:"+",NumpadSubtract:"-",Escape:"Esc",Space:"Space"};function kn(e,t=wn()){const i=xn(e);if(null===i)return"";const s=t?{Mod:"⌘",Ctrl:"⌃",Meta:"⌘",Alt:"⌥",Shift:"⇧"}:{Mod:"Ctrl",Ctrl:"Ctrl",Meta:"Win",Alt:"Alt",Shift:"Shift"};var r;return[...i.mods.map(e=>s[e]),(r=i.key,void 0!==On[r]?On[r]:/^Key[A-Z]$/.test(r)?r.slice(3):/^Digit[0-9]$/.test(r)?r.slice(5):r)].join(t?" ":" + ")}function In(e,t=wn()){const i=e.code??e.key??"";if(""===i||yn.has(i))return"";const s=[];return(t?e.metaKey:e.ctrlKey)&&s.push("Mod"),t&&e.ctrlKey&&s.push("Ctrl"),!t&&e.metaKey&&s.push("Meta"),e.altKey&&s.push("Alt"),e.shiftKey&&s.push("Shift"),Sn([...s,i].join("+"))}const Mn=new Set(["Mod+KeyW","Mod+KeyT","Mod+KeyN","Mod+KeyQ","Mod+KeyR","Mod+KeyL","Mod+Shift+KeyT","Mod+Shift+KeyN","Mod+Shift+KeyW"]);function Tn(e){return Mn.has(Sn(e))}const Cn=[{command:"panLeft",label:"Pan left",combos:["ArrowLeft"]},{command:"panRight",label:"Pan right",combos:["ArrowRight"]},{command:"panLeftFast",label:"Pan left (fast)",combos:["Mod+ArrowLeft"]},{command:"panRightFast",label:"Pan right (fast)",combos:["Mod+ArrowRight"]},{command:"panUp",label:"Pan up",combos:["ArrowUp"]},{command:"panDown",label:"Pan down",combos:["ArrowDown"]},{command:"zoomIn",label:"Zoom in",combos:["Equal","Shift+Equal","NumpadAdd"]},{command:"zoomOut",label:"Zoom out",combos:["Minus","NumpadSubtract"]},{command:"resetScale",label:"Reset view",combos:["Home","Digit0"]},{command:"fitContent",label:"Fit all bars",combos:["Alt+KeyF"]},{command:"screenshot",label:"Screenshot (PNG)",combos:["Alt+Shift+KeyS"]},{command:"toggleGridVert",label:"Toggle vertical grid",combos:["Alt+KeyV"]},{command:"toggleGridHorz",label:"Toggle horizontal grid",combos:["Alt+KeyH"]},{command:"toggleCrosshairMagnet",label:"Toggle crosshair magnet",combos:["Alt+KeyM"]}],jn={panLeftFast:["Alt+ArrowLeft"],panRightFast:["Alt+ArrowRight"],screenshot:["Mod+Shift+KeyS"]},An=new Set(Cn.map(e=>e.command)),Ln=e=>Array.isArray(e)?e:[e];class Nn{constructor(e={}){Object.defineProperty(this,"scope",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_isMac",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_persist",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_storageKey",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_preset",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_overrides",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"_disabled",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_custom",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_entries",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_reverse",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_listeners",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_changes",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),this.scope=e.scope??"hover",this._isMac=e.isMac??wn(),this._persist=e.persist??!1,this._storageKey=e.storageKey??"openalgo-charts:shortcuts",this._preset=e.preset??"default";const t=e=>{for(const[t,i]of Object.entries(e??{})){if(null===i){this._disabled.add(t);continue}const e=Ln(i).map(Sn).filter(e=>""!==e&&!Tn(e));this._overrides[t]=e}};t(e.overrides);for(const t of e.disabledCommands??[])this._disabled.add(t);for(const t of e.customShortcuts??[])this._custom.set(t.command,t);const i=this._load();if(null!==i){void 0!==i.preset&&(this._preset=i.preset),t(i.overrides);for(const e of i.disabled??[])this._disabled.add(e)}this._rebuild()}_presetCombos(e){return"alt"===this._preset?jn[e]:void 0}_effectiveCombos(e,t){return this._disabled.has(e)?[]:void 0!==this._overrides[e]?this._overrides[e]:this._presetCombos(e)??t}_rebuild(){this._entries=new Map;for(const e of Cn)this._entries.set(e.command,{command:e.command,label:e.label,combos:this._effectiveCombos(e.command,e.combos)});for(const e of this._custom.values())this._entries.set(e.command,{command:e.command,label:e.label??e.command,combos:this._effectiveCombos(e.command,Ln(e.combos).map(Sn).filter(e=>""!==e))});this._reverse=new Map;for(const e of this._entries.values())for(const t of e.combos)this._reverse.has(t)||this._reverse.set(t,e.command)}resolve(e){const t=In(e,this._isMac);return""===t?null:this._reverse.get(t)??null}handleKey(e){return this._reverse.get(Sn(e))??null}runCustom(e){const t=this._custom.get(e);return void 0!==t&&(t.onTrigger(),this.emitTrigger(e),!0)}emitTrigger(e,t=""){const i={command:e,combo:t,isCustom:this._custom.has(e)};mn(this._listeners,i)}on(e){return this._listeners.add(e),()=>this._listeners.delete(e)}onChange(e){return this._changes.add(e),()=>{this._changes.delete(e)}}setBinding(e,t){const i=Ln(t).map(Sn).filter(e=>""!==e&&!Tn(e));return 0!==i.length&&(this._overrides[e]=i,this._disabled.delete(e),this._after(),!0)}disable(e){this._disabled.add(e),delete this._overrides[e],this._after()}resetBinding(e){delete this._overrides[e],this._disabled.delete(e),this._after()}resetAll(){this._overrides={},this._disabled.clear(),this._after()}setPreset(e){this._preset=e,this._after()}addCustom(e){this._custom.set(e.command,e),this._after()}list(){return[...this._entries.values()].map(e=>({command:e.command,label:e.label,combos:e.combos,isCustom:this._custom.has(e.command),isDisabled:this._disabled.has(e.command)}))}state(){return{preset:this._preset,overrides:this._overrides,disabled:[...this._disabled]}}_after(){this._rebuild(),this._save();for(const e of[...this._changes])e()}_save(){if(this._persist&&"undefined"!=typeof localStorage)try{localStorage.setItem(this._storageKey,JSON.stringify(this.state()))}catch{}}_load(){if(!this._persist||"undefined"==typeof localStorage)return null;try{const e=localStorage.getItem(this._storageKey);return null===e?null:JSON.parse(e)}catch{return null}}static shouldIgnore(e){const t=e;if(null==t)return!1;if(!0===t.isContentEditable)return!0;const i=(t.tagName??"").toUpperCase();return"INPUT"===i||"TEXTAREA"===i||"SELECT"===i}}function Rn(e,t,i,s,r,n,o,a){if("hidden"===t.priceAxisSide||!(t.priceAxisWidth>0))return;const l=t.dpr,h=6*l,c="left"===t.priceAxisSide;if(c||void 0!==t.priceAxisOffset){const d=t.priceAxisOffset??0,u=Math.round(d*l),_=Math.round((d+(c?-t.priceAxisWidth:t.priceAxisWidth))*l),p=a||void 0!==t.priceAxisOffset,f=(p?Math.abs(_-u):Math.round(t.priceAxisWidth*l))-1,m=t.plotHeight*l;if(Number.isFinite(u)&&Number.isFinite(_)&&f>0&&m>=o){e.save(),p&&(e.beginPath(),e.rect(Math.min(u,_),0,f+1,m),e.clip());const t=Math.min(h,f/4),a=e.measureText(s).width,d=Math.min(f,a+2*t),b=c?u-1-d:u+1,g=Math.max(o/2,Math.min(m-o/2,i));e.fillStyle=r,e.fillRect(b,g-o/2,d,o),e.fillStyle=n,e.font=`500 ${11*l*(a>0?Math.min(1,(d-2*t)/a):1)}px system-ui, sans-serif`,e.fillText(s,b+t,g),e.restore(),e.font=`500 ${11*l}px system-ui, sans-serif`}}else{const a=Math.round(t.plotWidth*l);e.fillStyle=r,e.fillRect(a+1,i-o/2,e.measureText(s).width+2*h,o),e.fillStyle=n,e.fillText(s,a+1+h,i)}}class Fn{constructor(e){Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_ghostPrice",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_group",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._opts=e}attached(e){this._host=e}detached(){this._host=null}get price(){return this._opts.price}setPrice(e){this._opts.price=e,this._host?.requestUpdate()}setLeftLabel(e){this._opts.leftLabel=e,this._host?.requestUpdate()}setOptions(e){this._opts={...this._opts,...e},this._host?.requestUpdate()}setDragGhost(e){this._ghostPrice=e,this._host?.requestUpdate()}options(){return this._opts}zOrder(){return"normal"}autoscaleInfo(){return{min:this._opts.price,max:this._opts.price}}draw(e,t){this._group=null;const i=Math.round(t.priceScale.priceToY(this._opts.price)*t.dpr)+.5,s="left"===t.priceAxisSide?i-.5:i;if(!Number.isFinite(i)||s<0||s>t.plotHeight*t.dpr)return;const r=t.dpr,n=this._opts.lineWidth??1,o=Math.round(t.plotWidth*r),a=Math.max(0,Math.min(1,this._opts.extentFromRight??1)),l=Math.round(t.plotWidth*(1-a)*r),h=this._opts.color,c=void 0!==this._opts.cursor||!0===this._opts.closeButton,d=t.dragId===this._opts.id,u=c&&!d&&(t.hoverId===this._opts.id||t.hoverId===`${this._opts.id}::close`),_=t.hoverId===`${this._opts.id}::close`;if(e.save(),null!==this._ghostPrice&&d){const i=Math.round(t.priceScale.priceToY(this._ghostPrice)*r)+.5;i>=0&&i<=t.plotHeight*r&&(e.strokeStyle=ie(h,.35),e.lineWidth=Math.max(1,Math.round(r)),e.setLineDash([2*r,3*r]),e.beginPath(),e.moveTo(l,i),e.lineTo(o,i),e.stroke(),e.setLineDash([]))}d&&(e.strokeStyle=ie(h,.18),e.lineWidth=Math.max(5*r,Math.round(n*r)+4*r),e.beginPath(),e.moveTo(l,i),e.lineTo(o,i),e.stroke()),e.strokeStyle=u||d?re(h,.1):h;const p=Math.max(1,Math.round(n*r));e.lineWidth=u||d?p+Math.round(r):p,e.setLineDash(z(this._opts.lineStyle??(!0===this._opts.dashed?"dashed":"solid"),r)),e.beginPath(),e.moveTo(l,i),e.lineTo(o,i),e.stroke(),e.setLineDash([]),e.font=`500 ${11*r}px system-ui, sans-serif`,e.textBaseline="middle",e.textAlign="left";const f=18*r,m=6*r,b=3*r;Rn(e,t,i,this._opts.label??t.priceScale.format(this._opts.price),d||u?re(h,.12):h,Q(h),f,!1);if(void 0!==this._opts.badge||void 0!==this._opts.qty||void 0!==this._opts.leftLabel&&""!==this._opts.leftLabel||!0===this._opts.closeButton){const s="transparent"===t.theme.background,n=s?ie(h,.14):t.theme.background,o=s?t.theme.axisText:Q(t.theme.background),c=ie(t.theme.axisText,u||d?.75:.5),p=[];void 0!==this._opts.badge&&p.push({text:this._opts.badge,fill:d?re(h,.2):u?re(h,.12):h,textColor:Q(h),border:re(h,-.25)}),void 0!==this._opts.qty&&p.push({text:String(this._opts.qty),fill:n,textColor:o,border:c}),void 0!==this._opts.leftLabel&&""!==this._opts.leftLabel&&p.push({text:this._opts.leftLabel,fill:n,textColor:o,border:c}),!0===this._opts.closeButton&&p.push({close:!0,fill:_?h:n,textColor:_?Q(h):o,border:_?h:c}),this._group=function(e,t,i,s,r){const{height:n,padX:o,radius:a,gap:l,dpr:h}=r,c=s.map(t=>!0===t.close?20*h:e.measureText(t.text??"").width+2*o),d=c.reduce((e,t)=>e+t,0)+l*Math.max(0,s.length-1);void 0!==r.backplate&&(e.beginPath(),ne(e,t,i-n/2,d,n,a),e.fillStyle=r.backplate,e.fill());let u=Number.POSITIVE_INFINITY,_=t;for(let t=0;t<s.length;t++){const r=s[t],d=c[t];if(e.beginPath(),ne(e,_,i-n/2,d,n,a),e.fillStyle=r.fill,e.fill(),void 0!==r.border&&(e.strokeStyle=r.border,e.lineWidth=Math.max(1,Math.round(h)),e.stroke()),!0===r.close){u=_,e.strokeStyle=r.textColor,e.lineWidth=Math.max(1,Math.round(1.4*h));const t=_+d/2,s=3.5*h;e.beginPath(),e.moveTo(t-s,i-s),e.lineTo(t+s,i+s),e.moveTo(t+s,i-s),e.lineTo(t-s,i+s),e.stroke()}else e.fillStyle=r.textColor,e.textAlign="left",e.textBaseline="middle",e.fillText(r.text??"",_+o,i);_+=d+l}return{x0:t/h,x1:(_-l)/h,closeX0:u/h}}(e,l+(1===a?6*r:0),i,p,{height:f,padX:m,radius:b,gap:2*r,backplate:s?void 0:t.theme.background,dpr:r})}e.restore()}hitTest(e,t,i){if(e<0||e>i.plotWidth)return null;const s=i.priceScale.priceToY(this._opts.price),r=Math.abs(t-s),n=this._group;return null!==n&&r<=10&&e>=n.x0&&e<=n.x1?this._opts.closeButton&&e>=n.closeX0?{externalId:`${this._opts.id}::close`,zOrder:"normal",distance:r,cursor:"pointer"}:{externalId:this._opts.id,zOrder:"normal",distance:r,cursor:this._opts.cursor}:r>4?null:{externalId:this._opts.id,zOrder:"normal",distance:r,cursor:this._opts.cursor}}}const zn={long:"#2f6df6",short:"#ef5350",order:"#3b82f6",tp:"#26a69a",sl:"#ef5350",buy:"#26a69a",sell:"#ef5350"},En="::close";function Bn(e,t){const i=e.length;if(0===i)return;let s=0,r=i-1;for(;s<r;){const i=s+r+1>>1,n=e.indexToTime(i);void 0!==n&&n<=t?s=i:r=i-1}const n=e.indexToTime(s);if(void 0!==n){if(s+1<i){const i=e.indexToTime(s+1);if(void 0!==i&&Math.abs(i-t)<Math.abs(n-t))return s+1}return s}}function $n(e,t,i,s,r,n){const o=5*n;e.fillStyle=r,e.beginPath(),"buy"===s?(e.moveTo(t,i-1.5*o),e.lineTo(t-o,i),e.lineTo(t+o,i)):(e.moveTo(t,i+1.5*o),e.lineTo(t-o,i),e.lineTo(t+o,i)),e.closePath(),e.fill()}function Dn(e,t,i,s,r,n){const o=9*n;e.fillStyle=s,e.beginPath(),e.arc(t,i,o,0,2*Math.PI),e.fill(),""!==r&&(e.fillStyle=Q(s),e.font=`500 ${9*n}px system-ui, sans-serif`,e.textAlign="center",e.textBaseline="middle",e.fillText(r,t,i+.5*n))}function Wn(e,t,i,s,r,n){const o=String(s);e.font=`500 ${9*n}px system-ui, sans-serif`;const a=e.measureText(o).width+10*n,l=15*n;e.fillStyle=r,e.beginPath(),ne(e,Math.round(t-a/2),Math.round(i-l/2),Math.round(a),Math.round(l),3*n),e.fill(),e.fillStyle=Q(r),e.textAlign="center",e.textBaseline="middle",e.fillText(o,t,i+.5*n)}class qn{constructor(e){Object.defineProperty(this,"_trades",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_colors",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._colors=e}attached(e){this._host=e}detached(){this._host=null}zOrder(){return"top"}autoscaleInfo(){return null}setTrades(e){this._trades=e,this._host?.requestUpdate()}setColors(e){this._colors=e,this._host?.requestUpdate()}draw(e,t){if(0===this._trades.length)return;const i=t.dpr,s=new Map;e.save();for(const r of this._trades){const n=Bn(t.dataLayer,Math.floor(r.timestamp/1e3));if(void 0===n)continue;const o=Math.round(t.timeScale.indexToX(n)*i),a=r.color??("buy"===r.side?this._colors.buy:this._colors.sell),l=r.variant??"chevron";if("count"===l){const e=`${n}:${r.side}`;let t=s.get(e);void 0===t&&(t={index:n,sumPS:0,sumS:0,n:0,color:a},s.set(e,t)),t.sumPS+=r.price*r.size,t.sumS+=r.size,t.n+=1;continue}const h=Math.round(t.priceScale.priceToY(r.price)*i);"bubble"===l?Dn(e,o,h,a,r.label??("buy"===r.side?"B":"S"),i):$n(e,o,h,r.side,a,i)}for(const r of s.values()){const s=r.sumS>0?r.sumPS/r.sumS:0;Wn(e,Math.round(t.timeScale.indexToX(r.index)*i),Math.round(t.priceScale.priceToY(s)*i),r.n,r.color,i)}e.restore()}}class Vn{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_positions",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_orders",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_trades",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_listeners",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_dragPrev",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_colors",{enumerable:!0,configurable:!0,writable:!0,value:{...zn}}),Object.defineProperty(this,"_markers",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_ticks",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._host=e,this._ticks=e.tickSchedule?.()??null,e.subscribeClick(e=>this._onClick(e)),e.subscribeDrag((e,t)=>this._onDrag(e,t),(e,t)=>this._onDragEnd(e,t))}on(e,t){let i=this._listeners.get(e);return void 0===i&&(i=new Set,this._listeners.set(e,i)),i.add(t),()=>{this._listeners.get(e)?.delete(t)}}off(e,t){this._listeners.get(e)?.delete(t)}_emit(e,t){mn(this._listeners.get(e),t),this._host.emit?.(e,t)}setSettings(e){void 0!==e.longColor&&(this._colors.long=e.longColor),void 0!==e.shortColor&&(this._colors.short=e.shortColor),void 0!==e.orderColor&&(this._colors.order=e.orderColor),void 0!==e.tpColor&&(this._colors.tp=e.tpColor),void 0!==e.slColor&&(this._colors.sl=e.slColor),void 0!==e.buyColor&&(this._colors.buy=e.buyColor),void 0!==e.sellColor&&(this._colors.sell=e.sellColor),this.setPositions(this.getPositions()),this.setOrders(this.getOrders()),this._markers?.setColors(this._colors)}getSettings(){return{...this._colors}}setTickSchedule(e){if(null!=e&&"function"!=typeof e.round)throw new TypeError("chart.trading.setTickSchedule takes a schedule built with new TickSchedule(bands), or null");this._ticks=e??null}setPositions(e){this._sync(this._positions,e,"pos")}setOrders(e){this._sync(this._orders,e,"ord")}setTrades(e){this._trades.clear();for(const t of e)this._trades.set(t.id,t);this._renderTrades()}addTrade(e){this._trades.set(e.id,e),this._renderTrades()}upsertOrder(e){const t=this.getOrders().filter(t=>t.id!==e.id);t.push(e),this.setOrders(t)}removeOrder(e){const t=this.getOrders().filter(t=>t.id!==e&&t.parentId!==e);this.setOrders(t)}syncState(e){void 0!==e.positions&&this.setPositions(e.positions),void 0!==e.orders&&this.setOrders(e.orders),void 0!==e.trades&&this.setTrades(e.trades)}updatePositionPnl(e,...t){const i=this._positions.get(e);if(void 0===i)return;const[s,r]="string"==typeof t[0]?t:t.slice(1);"string"==typeof s&&(i.entity.pnlText=s),"string"==typeof r&&(i.entity.pnlPercent=r),i.line.setLeftLabel(this._positionPill(i.entity))}getPositions(){return[...this._positions.values()].map(e=>e.entity)}getOrders(){return[...this._orders.values()].map(e=>e.entity)}getTrades(){return[...this._trades.values()]}clear(){for(const e of this._positions.values())this._host.removePrimitive(e.line);for(const e of this._orders.values())this._host.removePrimitive(e.line);this._positions.clear(),this._orders.clear(),this._trades.clear(),null!==this._markers&&(this._host.removePrimitive(this._markers),this._markers=null)}_renderTrades(){null===this._markers&&(this._markers=new qn(this._colors),this._host.addPrimitive(this._markers)),this._markers.setTrades(this.getTrades())}_sync(e,t,i){const s=new Set;for(const r of t){s.add(r.id);const t="pos"===i?this._positionOpts(r):this._orderOpts(r),n=this._sig(t),o=e.get(r.id);if(void 0!==o&&o.sig===n)o.entity=r,o.line.setPrice(t.price),void 0!==t.leftLabel&&o.line.setLeftLabel(t.leftLabel);else{void 0!==o&&this._host.removePrimitive(o.line);const i=new Fn(t);this._host.addPrimitive(i),e.set(r.id,{entity:r,line:i,sig:n})}}for(const[t,i]of e)s.has(t)||(this._host.removePrimitive(i.line),e.delete(t))}_sig(e){return`${e.color}|${e.dashed}|${!0===e.closeButton}|${e.cursor??""}|${void 0!==e.leftLabel}|${e.badge??""}|${e.qty??""}`}_positionPill(e){return void 0===e.pnlText?"":`${e.pnlText}${void 0!==e.pnlPercent?` (${e.pnlPercent})`:""}`}_positionOpts(e){const t="line-only"===e.variant;return{price:e.entryPrice,color:e.color??("long"===e.side?this._colors.long:this._colors.short),lineWidth:2,dashed:!1,id:`pos:${e.id}`,badge:t?void 0:e.side.toUpperCase(),qty:t?void 0:e.size,leftLabel:t?void 0:this._positionPill(e),closeButton:!t&&!0!==e.readOnly,extentFromRight:.3}}_orderOpts(e){const t="line-only"===e.variant,i=!t&&(e.draggable??!0!==e.readOnly),s=e.color??("tp"===e.bracketRole?this._colors.tp:"sl"===e.bracketRole?this._colors.sl:this._colors.order);return{price:e.price,color:s,lineWidth:e.lineWidth??1,dashed:"solid"!==(e.lineStyle??"solid"),id:`ord:${e.id}`,badge:t?void 0:(e.bracketRole??e.side).toUpperCase(),qty:t?void 0:e.size,leftLabel:t||void 0!==e.bracketRole?void 0:e.type.replace("_"," ").toUpperCase(),closeButton:!t&&!0!==e.readOnly,extentFromRight:.3,cursor:i?"ns-resize":void 0}}_onClick(e){if(e.endsWith(En)){const t=e.slice(0,-7);if(t.startsWith("ord:")){const e=t.slice(4);this._orders.has(e)&&this._emit("trading:order_cancel",{orderId:e})}else if(t.startsWith("pos:")){const e=t.slice(4);this._positions.has(e)&&this._emit("trading:position_close",{positionId:e})}return}if(e.startsWith("ord:")){const t=this._orders.get(e.slice(4));void 0!==t&&this._emit("trading:order_click",{order:t.entity})}else if(e.startsWith("pos:")){const t=this._positions.get(e.slice(4));void 0!==t&&this._emit("trading:position_click",{position:t.entity})}}_onDrag(e,t){if(!e.startsWith("ord:"))return;const i=this._ticks?.round(t)??t,s=this._orders.get(e.slice(4));if(void 0===s)return;const r=e.slice(4);this._dragPrev.has(r)||(this._dragPrev.set(r,s.entity.price),s.line.setDragGhost(s.entity.price)),s.line.setPrice(i)}_onDragEnd(e,t){if(!e.startsWith("ord:"))return;const i=this._ticks?.round(t)??t,s=e.slice(4),r=this._orders.get(s);if(void 0===r)return;const n=this._dragPrev.get(s)??r.entity.price;this._dragPrev.delete(s),r.line.setDragGhost(null),r.entity.price=i,r.line.setPrice(i),void 0!==r.entity.bracketRole?this._emit("trading:bracket_modify",{parentId:r.entity.parentId,bracketRole:r.entity.bracketRole,newPrice:i}):this._emit("trading:order_modify",{orderId:s,newPrice:i,previousPrice:n})}}const Hn=new WeakMap,Gn=new WeakMap;function Un(e){Gn.set(e,{}),Hn.get(e)?.()}function Kn(e,t,i){return Yn(e,t,i)}function Yn(e,t,i,s){if("price"!==t&&"time"!==t&&"point"!==t)throw new TypeError("Invalid pick kind");const r={};Gn.set(e,r);let n=!1,o=!1;const a=[],l=s=>{if(n){n=!1,Hn.get(e)===h&&Hn.delete(e);try{e.emit("pick:end",{kind:t,value:s}),null===s||o||Gn.get(e)!==r||i(s)}finally{for(const e of a.splice(0))e()}}},h=Object.assign(()=>{o=!0,l(null)},{active:()=>n});for(const t of["destroy","state:restore:start","data:context","paneRemoved","paneMoved"])a.push(e.on(t,h));if(a.push(e.on("data:update",e=>{"reset"===e?.kind&&h()})),Hn.get(e)?.(),o||Gn.get(e)!==r){for(const e of a.splice(0))e();return h}n=!0;const c=t=>{const i=e.dataLayer;return i.indexToTime(Math.round(i.timeToIndexFloat(t)))??t};return a.push(e.on("click",e=>{if(!n)return;const i=e,r=s?s(e):"price"===t?i.price:"time"===t?i.time:{time:i.time,price:i.price};if(null===r)return;if("object"==typeof r){const e=Number.isFinite(r.time)?c(r.time):NaN,{price:t}=r;return void(Number.isFinite(e)&&null!==t&&Number.isFinite(t)&&l({time:e,price:t}))}const o=Number.isFinite(r)&&"time"===t?c(r):r;Number.isFinite(o)&&l(o)})),Hn.set(e,h),e.emit("pick:start",{kind:t}),h}const Xn={earnings:"#f0a020",dividend:"#26a69a",split:"#4f8cff",news:"#9aa0b4"};function Jn(e){return Array.isArray(e)?e.map(Jn):ts(e)?Object.fromEntries(Object.entries(e).map(([e,t])=>[e,Jn(t)])):e}function Zn(e){return{...e,..."object"==typeof e.details?{details:Jn(e.details)}:{}}}class Qn{constructor(e={}){Object.defineProperty(this,"_entries",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_groups",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_options",{enumerable:!0,configurable:!0,writable:!0,value:{clustering:!1,clusterRadius:18}}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_positions",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_hits",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_providedIds",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_duplicateIds",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_width",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_height",{enumerable:!0,configurable:!0,writable:!0,value:0}),this.setOptions(e)}attached(e){this._host=e}detached(){this._host=null,this._invalidate()}zOrder(){return"normal"}setEvents(e){const t=e.map(e=>{if(!Number.isFinite(e.time))throw new Error("openalgo-charts: event time must be finite");return Zn(e)}).sort((e,t)=>e.time-t.time),i=new Map;this._entries=t.map(e=>{const t=void 0===e.id?JSON.stringify([e.time,e.type,e.label,e.group??null]):JSON.stringify(e.id),s=i.get(t)??0;return i.set(t,s+1),{event:e,key:`${t}:${s}`}}),this._providedIds=new Set(t.flatMap(e=>void 0===e.id?[]:[e.id])),this._duplicateIds=new Set([...this._providedIds].filter(e=>(i.get(JSON.stringify(e))??0)>1)),this._invalidate()}events(){return this._entries.map(e=>Zn(e.event))}setGroups(e){const t=new Map;for(const i of e){if(!i.id||t.has(i.id))throw new Error("openalgo-charts: event group IDs must be nonempty and unique");t.set(i.id,{...i})}const i=new Set;for(const e of t.keys()){const s=new Set;let r=e;for(;void 0!==r&&!i.has(r);){if(s.has(r))throw new Error("openalgo-charts: event group cycle");const e=t.get(r);if(void 0===e)throw new Error("openalgo-charts: event group parent is missing");s.add(r),r=e.parentId}for(const e of s)i.add(e)}this._groups=t,this._invalidate()}groups(){return Array.from(this._groups.values(),e=>({...e}))}setGroupVisible(e,t){const i=this._groups.get(e);if(void 0===i)throw new Error(`openalgo-charts: unknown event group ${e}`);i.visible=t,this._invalidate()}isGroupVisible(e){let t=this._groups.get(e);for(;void 0!==t;){if(!1===t.visible)return!1;t=void 0===t.parentId?void 0:this._groups.get(t.parentId)}return!0}setOptions(e){const t=e.clusterRadius??this._options.clusterRadius;if(!Number.isFinite(t)||t<1||t>200)throw new Error("openalgo-charts: event clusterRadius must be from 1 to 200");this._options={clustering:e.clustering??this._options.clustering,clusterRadius:t},this._invalidate()}options(){return{...this._options}}detailsForHit(e){const t=this._hits.get(e);return void 0===t?null:{id:t.id,cluster:t.entries.length>1,events:t.entries.map(e=>Zn(e.event))}}draw(e,t){if(this._positions=[],this._hits.clear(),this._width=t.plotWidth,this._height=t.plotHeight,!(0!==this._entries.length&&t.dpr>0&&Number.isFinite(t.dpr)&&t.plotWidth>0&&t.plotHeight>0))return;const i=[];for(const e of this._entries){if(void 0!==e.event.group&&!this.isGroupVisible(e.event.group))continue;const s=t.dataLayer.timeToIndexFloat(e.event.time),r=t.timeScale.indexToX(s);if(!Number.isFinite(r))continue;const n=i[i.length-1];this._options.clustering&&void 0!==n&&r-n.firstX<=this._options.clusterRadius?(n.entries.push(e),n.x+=(r-n.x)/n.entries.length):i.push({x:r,firstX:r,entries:[e]})}const s=t.plotHeight-8-4;e.save(),e.beginPath(),e.rect(0,0,t.plotWidth*t.dpr,t.plotHeight*t.dpr),e.clip(),e.textAlign="center",e.textBaseline="middle",e.font=10*t.dpr+"px system-ui, sans-serif";for(const r of i){if(r.x+8<0||r.x-8>t.plotWidth||s+8<0)continue;const i=r.entries[0].event,n=r.entries.length>1;let o=i.id;if(n||void 0===o||this._duplicateIds.has(o))for(o=`${n?"event-cluster:":"event:"}${JSON.stringify(r.entries.map(e=>e.key).sort())}`;this._providedIds.has(o);)o=":"+o;e.fillStyle=i.color??Xn[i.type]??"#9aa0b4",e.beginPath(),e.arc(r.x*t.dpr,s*t.dpr,8*t.dpr,0,2*Math.PI),e.fill(),e.fillStyle="#0d0e12";const a=n?r.entries.length>99?"99+":String(r.entries.length):i.label.slice(0,2);e.fillText(a,r.x*t.dpr,s*t.dpr);const l={id:o,x:r.x,y:s,r:8,entries:r.entries};this._positions.push(l),this._hits.set(o,l)}e.restore()}hitTest(e,t){if(e<0||t<0||e>this._width||t>this._height)return null;let i=null;for(const s of this._positions){const r=Math.hypot(s.x-e,s.y-t);r<=s.r&&(null===i||r<=i.distance)&&(i={externalId:s.id,zOrder:"normal",distance:r,cursor:"pointer"})}return i}_invalidate(){this._positions=[],this._hits.clear(),this._host?.requestUpdate()}}const eo={zoomOut:"Zoom out",zoomIn:"Zoom in",resetScale:"Reset view",panLeftBar:"Move left",panRightBar:"Move right"},to={id:"timenav",buttons:["zoomOut","zoomIn",null,"resetScale",null,"panLeftBar","panRightBar"],size:26,gap:4,groupGap:16,bottomMargin:10,revealHeight:64,fadeSeconds:.12,labels:eo,hints:{},showTooltip:!0,font:11,radius:6,zOrder:"top"};class io{constructor(e={},t){Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_boxes",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_pointer",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_opacity",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_lastFrameMs",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_now",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_revealTop",{enumerable:!0,configurable:!0,writable:!0,value:1/0}),this._opts={...to,...e,labels:{...to.labels,...e.labels}},this._now=t??(()=>"undefined"!=typeof performance?performance.now():0)}attached(e){this._host=e}detached(){this._host=null}zOrder(){return this._opts.zOrder??"top"}options(){return this._opts}setOptions(e){this._opts={...this._opts,...e,labels:{...this._opts.labels,...e.labels}},this._host?.requestUpdate()}setPointer(e){const t=this._pointer;this._pointer=e,(null===t!=(null===e)||this._opacity>0||this._targetOpacity()>0)&&this._host?.requestUpdate()}_revealed(){return null!==this._pointer&&this._pointer.y>=this._revealTop}_targetOpacity(){return this._revealed()?1:0}animating(){return Math.abs(this._targetOpacity()-this._opacity)>.001}draw(e,t){const i=this._opts,s=t.dpr;this._revealTop=Math.max(0,t.plotHeight-i.revealHeight);const r=this._targetOpacity(),n=this._now(),o=null===this._lastFrameMs?0:Math.max(0,(n-this._lastFrameMs)/1e3);if(this._lastFrameMs=n,i.fadeSeconds<=0||o<=0)i.fadeSeconds<=0&&(this._opacity=r);else{const e=o/i.fadeSeconds;this._opacity+=Math.sign(r-this._opacity)*Math.min(e,Math.abs(r-this._opacity))}if(this._opacity<=.001)return this._boxes=[],void(this._opacity=0);const a=i.size;let l=0;for(let e=0;e<i.buttons.length;e++){null!==i.buttons[e]?(l+=a,e<i.buttons.length-1&&null!==i.buttons[e+1]&&(l+=i.gap)):l+=i.groupGap}const h=(t.plotWidth-l)/2,c=t.plotHeight-i.bottomMargin-a;this._boxes=[];let d=h;for(let e=0;e<i.buttons.length;e++){const t=i.buttons[e];null!==t?(this._boxes.push({action:t,x:d,y:c,w:a,h:a}),d+=a,e<i.buttons.length-1&&null!==i.buttons[e+1]&&(d+=i.gap)):d+=i.groupGap}const u=this._hoveredAction();e.save(),e.globalAlpha=this._opacity;for(const i of this._boxes)this._button(e,i,i.action===u,t,s);i.showTooltip&&null!==u&&this._tooltip(e,u,t,s),e.restore()}_hoveredAction(){const e=this._pointer;if(null===e)return null;for(const t of this._boxes)if(e.x>=t.x&&e.x<=t.x+t.w&&e.y>=t.y&&e.y<=t.y+t.h)return t.action;return null}_button(e,t,i,s,r){const n=this._opts,o=t.x*r,a=t.y*r,l=t.w*r,h=t.h*r,c=Math.min(n.radius*r,l/2,h/2);e.beginPath(),e.roundRect(o,a,l,h,c),e.fillStyle=so(s.theme.background,s.theme.axisText,i?.28:.12),e.fill(),e.strokeStyle=ie(s.theme.axisLine,i?1:.8),e.lineWidth=Math.max(1,Math.round(r)),e.stroke(),e.strokeStyle=s.theme.axisText,e.fillStyle=s.theme.axisText,e.lineWidth=Math.max(1,Math.round(1.5*r)),e.lineCap="round",e.lineJoin="round";const d=o+l/2,u=a+h/2,_=.22*Math.min(l,h);switch(e.beginPath(),t.action){case"zoomOut":e.moveTo(d-_,u),e.lineTo(d+_,u);break;case"zoomIn":e.moveTo(d-_,u),e.lineTo(d+_,u),e.moveTo(d,u-_),e.lineTo(d,u+_);break;case"resetScale":{const t=_*Math.SQRT1_2;e.arc(d,u,_,.75*-Math.PI,.75*Math.PI),e.moveTo(d-t,u-2*t),e.lineTo(d-t,u-t),e.lineTo(d,u-t);break}case"panLeftBar":e.moveTo(d+.55*_,u-_),e.lineTo(d-.5*_,u),e.lineTo(d+.55*_,u+_);break;case"panRightBar":e.moveTo(d-.55*_,u-_),e.lineTo(d+.5*_,u),e.lineTo(d-.55*_,u+_)}e.stroke()}_tooltip(e,t,i,s){const r=this._opts,n=this._boxes.find(e=>e.action===t);if(void 0===n)return;const o=r.labels[t]??eo[t],a=r.hints[t];e.font=r.font*s+"px ui-sans-serif, system-ui, sans-serif",e.textBaseline="middle",e.textAlign="left";const l=7*s,h=8*s,c=e.measureText(o).width,d=2*l+c+(void 0===a?0:e.measureText(a).width+h+8*s),u=22*s;let _=(n.x+n.w/2)*s-d/2;_=Math.max(2*s,Math.min(_,i.plotWidth*s-d-2*s));const p=n.y*s-u-6*s;if(e.beginPath(),e.roundRect(_,p,d,u,5*s),e.fillStyle=so(i.theme.background,i.theme.axisText,.22),e.fill(),e.strokeStyle=ie(i.theme.axisLine,.9),e.lineWidth=Math.max(1,Math.round(s)),e.stroke(),e.fillStyle=i.theme.axisText,e.fillText(o,_+l,p+u/2),void 0!==a){const t=_+l+c+h,r=e.measureText(a).width+8*s;e.beginPath(),e.roundRect(t-4*s,p+4*s,r,u-8*s,3*s),e.fillStyle=so(i.theme.background,i.theme.axisText,.42),e.fill(),e.fillStyle=i.theme.axisText,e.fillText(a,t,p+u/2)}}hitTest(e,t){if(this._opacity<.5)return null;for(const i of this._boxes)if(e>=i.x&&e<=i.x+i.w&&t>=i.y&&t<=i.y+i.h)return{externalId:`${this._opts.id}::${i.action}`,zOrder:"top",distance:0,cursor:"pointer"};return null}}function so(e,t,i){const s=Z(e),r=Z(t);if(null===s||null===r)return e;const n=i<0?0:i>1?1:i,o=(e,t)=>Math.round(e+(t-e)*n);return`rgb(${o(s.r,r.r)},${o(s.g,r.g)},${o(s.b,r.b)})`}function ro(e){return void 0===e?{x:7,y:4}:"number"==typeof e?{x:e,y:e}:{x:e.x,y:e.y}}function no(e,t,i,s,r,n){const o=r-t-i,a=n-t-s;switch(e){case"top-left":return{x:t,y:t,w:i,h:s};case"top-right":return{x:o,y:t,w:i,h:s};case"bottom-left":return{x:t,y:a,w:i,h:s};case"center":return{x:(r-i)/2,y:(n-s)/2,w:i,h:s};default:return{x:o,y:a,w:i,h:s}}}class oo{constructor(e={}){Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_reveal",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_lastFrameMs",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_labelW",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_now",{enumerable:!0,configurable:!0,writable:!0,value:"undefined"!=typeof performance?()=>performance.now():()=>0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_img",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_ready",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_autoHeight",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_tintCanvas",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_tintKey",{enumerable:!0,configurable:!0,writable:!0,value:""}),this._autoHeight=void 0===e.height,this._opts={position:e.position??"bottom-right",margin:e.margin??12,height:e.height??28,opacity:e.opacity??.7,zOrder:e.zOrder??"top",src:e.src,image:e.image,tint:e.tint,label:e.label,id:e.id??"watermark",revealSeconds:e.revealSeconds??.18,labelColor:e.labelColor,fontSize:e.fontSize??12,background:e.background,borderColor:e.borderColor,radius:e.radius??6,padding:ro(e.padding),href:e.href,utmMedium:e.utmMedium??"oac-link",utmCampaign:e.utmCampaign??"oac-chart"},e.image&&(this._img=e.image,this._ready=!0)}attached(e){if(this._host=e,!this._ready&&this._opts.src&&"undefined"!=typeof Image){const e=new Image;e.onload=()=>{this._img=e,this._ready=!0,this._host?.requestUpdate()},e.decoding="async",e.src=this._opts.src}}detached(){this._host=null}zOrder(){return this._opts.zOrder}autoscaleInfo(){return null}setOptions(e){const{padding:t,...i}=e;this._opts={...this._opts,...i},void 0!==t&&(this._opts.padding=ro(t)),void 0!==e.height&&(this._autoHeight=!1),void 0===e.label&&void 0===e.fontSize||(this._labelW=0),this._tintCanvas=null,e.image?(this._img=e.image,this._ready=!0):void 0!==e.src&&(this._ready=!1,this._img=null,this.attached(this._host)),this._host?.requestUpdate()}_rect(e){if(!(this._builtin()||this._ready&&null!==this._img))return null;const t=this._img?.naturalWidth??this._img?.width??1,i=this._img?.naturalHeight??this._img?.height??1;if(!t||!i)return null;const s=this._opts,r=Math.max(0,s.margin,s.padding.x,s.padding.y),n=e.plotWidth-2*r,o=e.plotHeight-2*r;if(n<=0||o<=0)return null;const a=this._builtin()&&this._autoHeight&&e.plotWidth<480?24:s.height,l=Math.max(0,Math.min(a,o,n*i/t));if(!Number.isFinite(l)||l<=0)return null;const h=l*(t/i),c=Math.min(n,h+this._labelW*this._reveal);return{...no(s.position,r,c,l,e.plotWidth,e.plotHeight),logoW:h}}_builtin(){return void 0===this._opts.src&&void 0===this._opts.image}href(){const e=this._opts.href;if(void 0===e||e.includes("?"))return e;const t="undefined"!=typeof location?location:void 0,i=[`utm_medium=${encodeURIComponent(this._opts.utmMedium)}`,`utm_campaign=${encodeURIComponent(this._opts.utmCampaign)}`];return t?.host&&i.push(`utm_source=${t.host}${t.pathname}`),`${e}${e.includes("?")?"&":"?"}${i.join("&")}`}hitTest(e,t,i){if(void 0===this._opts.label&&void 0===this._opts.href)return null;const s=this._rect(i);if(null===s)return null;const r=Math.max(this._opts.padding.x,4,(44-s.w)/2),n=Math.max(this._opts.padding.y,4,(44-s.h)/2),o=Math.min(i.plotWidth,s.w+2*r),a=Math.min(i.plotHeight,s.h+2*n),l=Math.max(0,Math.min(i.plotWidth-o,s.x-r)),h=Math.max(0,Math.min(i.plotHeight-a,s.y-n));return e>=l&&e<=l+o&&t>=h&&t<=h+a?{externalId:this._opts.id,zOrder:this._opts.zOrder,distance:0,cursor:void 0!==this._opts.href?"pointer":"default"}:null}draw(e,t){if(!(this._builtin()||this._ready&&null!==this._img))return;const i=this._opts,s=t.dpr;void 0!==i.label&&0===this._labelW&&(e.save(),e.font=`600 ${i.fontSize*s}px system-ui, sans-serif`,this._labelW=e.measureText(i.label).width/s+8,e.restore()),this._advanceReveal(t);const r=this._rect(t);if(null===r)return;const n=Math.round(r.x*s),o=Math.round(r.y*s),a=Math.round(r.logoW*s),l=Math.round(r.h*s);e.save();const h=i.padding,c=Math.round((r.x-h.x)*s),d=Math.round((r.y-h.y)*s),u=Math.round((r.x+r.w+h.x)*s)-c,_=Math.round((r.y+r.h+h.y)*s)-d,p=t.theme,f=i.background??(p?ie(p.background,.82):"none"),m=i.borderColor??p?.axisLine??"none";"none"!==f&&(e.beginPath(),ne(e,c,d,u,_,Math.round(i.radius*s)),e.fillStyle=f,e.fill(),"none"!==m&&(e.strokeStyle=m,e.lineWidth=Math.max(1,Math.round(s)),e.stroke())),e.globalAlpha=Math.max(0,Math.min(1,i.opacity));const b=this._ink(t),g=void 0!==i.label?b:i.tint;if(this._builtin())e.fillStyle=b,function(e,t,i,s){e.save(),e.translate(t,i),e.scale(s/397,s/397),e.translate(-315.1,-217.65),e.beginPath(),e.moveTo(367.5,255.5),e.bezierCurveTo(371.3,251.3,374.8,247.3,378.7,242.8),e.bezierCurveTo(398.1,262.7,416.4,281.4,435.2,300.6),e.bezierCurveTo(461.6,273.7,487.4,247.4,513.6,220.6),e.bezierCurveTo(540.7,247.4,566.9,273.2,592.9,298.9),e.bezierCurveTo(611.4,280.3,629.8,262,648.4,243.3),e.bezierCurveTo(654.6,249.9,660.3,256.1,665.9,262.2),e.bezierCurveTo(648.3,280.2,630.3,298.7,612.5,316.9),e.bezierCurveTo(645.6,350.8,679,384.9,712.1,418.6),e.bezierCurveTo(684.7,446.7,656.7,475.5,628.4,504.5),e.bezierCurveTo(647.5,523.2,667.4,542.7,686.8,561.8),e.bezierCurveTo(680.8,568.3,675,574.7,668.8,581.5),e.bezierCurveTo(648.3,561.4,628.3,541.7,608.1,521.8),e.bezierCurveTo(577.2,551.9,546.6,581.7,516,611.7),e.bezierCurveTo(488.3,584.7,461,558,433.6,531.3),e.bezierCurveTo(415.9,548.5,398.2,565.6,380.1,583.1),e.bezierCurveTo(374.3,576.6,368.7,570.4,363.5,564.6),e.bezierCurveTo(379.4,548,396.1,530.5,412.9,512.9),e.bezierCurveTo(380.7,481.9,348,450.4,315.1,418.7),e.bezierCurveTo(349.2,385,383,351.6,416.6,318.3),e.bezierCurveTo(398.1,299.3,379.8,280.6,360.9,261.3),e.bezierCurveTo(362.8,259.7,365,257.7,367.5,255.5),e.moveTo(540.5,406),e.bezierCurveTo(552,417.1,563.4,428.1,575,439.2),e.bezierCurveTo(588.1,426.5,601.7,413.3,615.9,399.5),e.bezierCurveTo(621.9,406.1,627.8,412.6,633.8,419.2),e.bezierCurveTo(620.3,431.7,606.4,444.8,592.9,457.3),e.bezierCurveTo(600,464.3,606.9,471.2,613.8,478.1),e.bezierCurveTo(633.5,458.1,653.4,437.7,673.2,417.5),e.bezierCurveTo(620.7,365.5,567.7,313.2,514.8,260.8),e.bezierCurveTo(494.6,280.6,474.6,300.2,454.3,320),e.bezierCurveTo(461.7,327,468.8,333.8,477.5,342.1),e.bezierCurveTo(489.4,329.1,501.6,315.8,514.3,301.9),e.bezierCurveTo(521,308.7,526.7,314.6,531.9,319.9),e.bezierCurveTo(519.2,333.3,506.8,346.5,494.7,359.4),e.bezierCurveTo(509.5,374.4,524.7,390,540.5,406),e.moveTo(575.3,514.8),e.bezierCurveTo(581.4,508.6,587.6,502.3,593.4,496.4),e.bezierCurveTo(586.7,489.5,580.1,482.5,573.2,475.3),e.bezierCurveTo(557.6,490.1,542.3,504.7,526.9,519.3),e.bezierCurveTo(520.9,513.3,515.2,507.5,509.4,501.6),e.bezierCurveTo(524.7,487,540,472.3,555.6,457.4),e.bezierCurveTo(528.5,430.5,501.6,403.8,474.5,377),e.bezierCurveTo(461.3,389.6,448,402.3,434.6,415.2),e.bezierCurveTo(455,435.2,475.1,454.9,494.9,474.3),e.bezierCurveTo(482.1,487.6,469.4,500.8,456.5,514.1),e.bezierCurveTo(476.3,533.2,496.5,552.7,516.8,572.4),e.bezierCurveTo(536.3,553.1,555.6,534.2,575.3,514.8),e.moveTo(413.2,474.3),e.bezierCurveTo(420.1,481,427,487.7,434.6,495.1),e.bezierCurveTo(441.3,487.6,447.8,480.3,453.9,473.5),e.bezierCurveTo(435.5,454.7,416.3,435.1,397.1,415.5),e.bezierCurveTo(416.9,396.5,436.7,377.7,456.9,358.4),e.bezierCurveTo(449.8,351.8,442.7,345.3,435.6,338.7),e.bezierCurveTo(409,364.8,381.8,391.4,354.7,418),e.bezierCurveTo(374.3,436.9,393.4,455.4,413.2,474.3),e.closePath(),e.fill(),e.restore()}(e,n,o,l);else{const t=g?this._tinted(e,a,l,g):this._img;g&&t?e.drawImage(t,n,o):this._img&&e.drawImage(this._img,n,o,a,l)}if(void 0!==i.label&&this._reveal>.001){const t=(r.w-r.logoW)*s;e.beginPath(),e.rect(n+a,o,t,l),e.clip(),e.globalAlpha=this._reveal,e.font=`600 ${Math.round(i.fontSize*s)}px system-ui, -apple-system, sans-serif`,e.textBaseline="middle",e.textAlign="left",e.fillStyle=b,e.fillText(i.label,Math.round(n+a+6*s),Math.round(o+l/2))}e.restore()}_ink(e){const t=this._opts,i=e.theme;return t.labelColor??t.tint??i?.axisText??"#9aa3b2"}_advanceReveal(e){if(void 0===this._opts.label)return;const t=e.hoverId===this._opts.id?1:0,i=this._now(),s=null===this._lastFrameMs?0:Math.max(0,(i-this._lastFrameMs)/1e3);this._lastFrameMs=i;const r=this._opts.revealSeconds;if(r<=0)this._reveal=t;else if(this._reveal!==t){const e=s/r;this._reveal+=Math.sign(t-this._reveal)*Math.min(e,Math.abs(t-this._reveal))}Math.abs(t-this._reveal)>.001?this._host?.requestUpdate():this._reveal=t}_tinted(e,t,i,s){const r=`${t}x${i}:${s}`;if(this._tintCanvas&&this._tintKey===r)return this._tintCanvas;const n=e.canvas,o=n?.ownerDocument;let a=null;if(o&&"function"==typeof o.createElement&&(a=o.createElement("canvas")),!a)return null;a.width=t,a.height=i;const l=a.getContext("2d");return l&&null!==this._img?(l.clearRect(0,0,t,i),l.drawImage(this._img,0,0,t,i),l.globalCompositeOperation="source-in",l.fillStyle=s,l.fillRect(0,0,t,i),this._tintCanvas=a,this._tintKey=r,a):null}}const ao=2;function lo(e){if(!ts(e))throw new Error("Invalid pane scale object");if(!is(e))throw new Error("Pane scale accessors are not supported");return e}function ho(e,t,i=-Number.MAX_VALUE,s=Number.MAX_VALUE){if("number"!=typeof e||!Number.isFinite(e)||e<i||e>s)throw new Error(`Invalid ${t}`);return e}function co(e){const t=lo(e),i=ho(t.min,"scale range minimum");return{min:i,max:ho(t.max,"scale range maximum",i)}}function uo(e,t){const i=lo(e),s=(e,s)=>void 0===i[e]&&t?s:i[e],r=s("mode","linear"),n=s("inverted",!1),o=s("autoScale",!0);if(!m.includes(r)||"boolean"!=typeof n||"boolean"!=typeof o)throw new Error("Invalid price scale mode or flags");const a={marginTop:ho(s("marginTop",.1),"top scale margin"),marginBottom:ho(s("marginBottom",.1),"bottom scale margin"),minMove:ho(s("minMove",0),"scale tick",0),mode:r,inverted:n,autoScale:o};if(void 0!==i.minPrecision&&(a.minPrecision=ho(i.minPrecision,"scale precision",0,100),!Number.isInteger(a.minPrecision)))throw new Error("Scale precision must be an integer");if(void 0!==i.range&&(a.range=co(i.range)),void 0!==i.fixedRange&&(a.fixedRange=null===i.fixedRange?null:co(i.fixedRange)),void 0!==i.indicatorRange){const e=lo(i.indicatorRange);if("string"!=typeof e.instanceId||!e.instanceId.trim()||"boolean"!=typeof e.manual||!a.fixedRange)throw new Error("Invalid indicator range ownership");a.indicatorRange={instanceId:e.instanceId,manual:e.manual}}if(void 0!==i.ratioLock){const e=lo(i.ratioLock);if(o||!a.range)throw new Error("A scale ratio lock requires a manual range");a.ratioLock={barSpacing:ho(e.barSpacing,"ratio bar spacing",Number.MIN_VALUE),height:ho(e.height,"ratio height",Number.MIN_VALUE)}}if(void 0!==i.placement){const e=lo(i.placement);if(!Object.prototype.hasOwnProperty.call(e,"side")||!Object.prototype.hasOwnProperty.call(e,"order")||!["left","right","hidden"].includes(e.side)||"number"!=typeof e.order||!Number.isSafeInteger(e.order)||e.order<0)throw new Error("Invalid price axis placement side or order");a.placement={side:e.side,order:e.order}}return a}function _o(e,t=!1){const i=lo(e),s={weight:ho(i.weight,"pane weight",Number.MIN_VALUE),priceScale:uo(i.priceScale,t)};if(void 0!==i.scales){const e=lo(i.scales);s.scales={};for(const[t,i]of Object.entries(e)){if(!L(t)||"right"===t)throw new Error("Invalid secondary price scale id");s.scales[t]=uo(i,!1)}}if(void 0!==i.collapsed){if("boolean"!=typeof i.collapsed)throw new Error("Invalid pane collapse flag");s.collapsed=i.collapsed}return s}class po{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_restoreGeneration",{enumerable:!0,configurable:!0,writable:!0,value:0}),this._host=e}getState(){const e=Ts(this._host._indicators.map(e=>e.dependencyNode())).dependencies,t=this._host._panes.map(e=>{const t=e.scaleStates();for(const[i,s]of this._host._indicatorRanges){if(s.pane!==e)continue;const r=e.scaleFor(s.scaleId),n=this._host._ownedScaleRanges.get(r),o=n?r.ownedFixedRangeState(n):null;o&&t[s.scaleId]&&!t[s.scaleId].indicatorRange&&(t[s.scaleId].indicatorRange={instanceId:i,manual:o.manual})}const{right:i,...s}=t,r={weight:e.weight,priceScale:i};return Object.keys(s).length&&(r.scales=s),this._host._collapsed.has(e)&&(r.collapsed=!0),r}),i=[];this._host._panes.forEach((e,t)=>{for(const s of e.series()){const e=Object.fromEntries(Object.entries(s.style).filter(([,e])=>void 0!==e)),r=this._host._series._transformOf(s.dataId);i.push({type:s.type,style:e,paneIndex:t,priceScaleId:s.scaleId,...void 0===r?{}:{transform:r}})}});const s=this._host._primaryIndex(),r={version:s>0?2:1,timezone:this._host._timezone,viewport:{...this._host.getVisibleLogicalRange()},barSpacing:this._host._timeScale.barSpacing,navigation:this._host.navigationOptions(),grid:this._host.gridOptions(),canvas:this._host.canvasOptions(),statusLine:this._host.statusLineOptions(),watermark:this._host.watermarkOptions(),trading:{...this._host._tradingSettings},axisChrome:{sessionClock:this._host._axisChrome.sessionClock,barCountdown:this._host._axisChrome.barCountdown},events:this._host.eventOptions(),crosshairMode:this._host._crosshairMode,crosshairSnapToBar:this._host._crosshairSnapToBar,priceOnlyAutoScale:this._host._priceOnlyAutoScale,indicatorLegendCollapsed:this._host._indicatorLegendCollapsed,panes:t,...s>0?{primaryPane:s}:{},series:i,indicators:this._host._indicators.map(t=>({indicatorId:t.indicatorId,instanceId:t.id,settings:t.settings(),paneIndex:t.paneIndex,visible:t.visible(),...e.get(t.id)?.length?{studyInputs:e.get(t.id).map(e=>e.inputKey)}:{},...null===t.priceScaleId()?{}:{priceScaleId:t.priceScaleId()},...Object.keys(t.plotPriceScaleIds()).length?{plotPriceScaleIds:t.plotPriceScaleIds()}:{},...Object.keys(t.policy()).length?{policy:{...t.policy()}}:{},..."underlying"===t.barSource()?{barSource:"underlying"}:{}})),..."string"==typeof this._host._sourceAbove?{sourceAbove:this._host._sourceAbove}:{}};return void 0!==this._host._drawingState&&(r.drawings=this._host._drawingState),void 0!==this._host._alertState&&(r.alerts=fn(this._host._alertState)),r}restoreState(e,t={}){const s=e;if(null===s||"object"!=typeof s||"number"!=typeof s.version)return{applied:!1,series:[],indicators:0,reason:"not a chart state object"};if(s.version>2)return{applied:!1,series:[],indicators:0,reason:`state version ${s.version} is newer than 2`};let r,n,o,a;const l=new Map,h=new Set;try{const e=Object.getOwnPropertyDescriptor(s,"priceOnlyAutoScale");if(e&&(!("value"in e)||void 0!==e.value&&"boolean"!=typeof e.value))throw new Error("Invalid price-only autoscale preference");const i=Object.getOwnPropertyDescriptor(s,"indicatorLegendCollapsed");if(i&&(!("value"in i)||void 0!==i.value&&"boolean"!=typeof i.value))throw new Error("Invalid indicator legend preference");const c=e=>ts(e)&&is(e);if(!c(t))throw new Error("Invalid chart restore options");if(void 0!==t.preserveScaleFormats){const e=t.preserveScaleFormats;if(!Array.isArray(e))throw new Error("Invalid preserved scale formats");const i=Object.getOwnPropertyDescriptors(e);if(Reflect.ownKeys(i).some(t=>"length"!==t&&("string"!=typeof t||!/^(0|[1-9]\d*)$/.test(t)||Number(t)>=e.length||!("value"in i[t]))))throw new Error("Invalid preserved scale formats");for(let t=0;t<e.length;t++){const e=i[t]?.value;if(!c(e)||"number"!=typeof e.paneIndex||!Number.isInteger(e.paneIndex)||e.paneIndex<0||!L(e.scaleId))throw new Error("Invalid preserved scale selector");const s=this._host._panes[e.paneIndex];if(!s||!Object.prototype.hasOwnProperty.call(s.scaleStates(),e.scaleId))throw new Error("Preserved scale must already exist");const r=l.get(s)??new Set;r.add(e.scaleId),l.set(s,r)}}if(void 0!==s.panes){if(!Array.isArray(s.panes))throw new Error("Invalid pane list");n=s.panes.map(e=>_o(e,!0))}const d=Object.getOwnPropertyDescriptor(s,"primaryPane");if(d&&(!("value"in d)||void 0!==d.value)){const e=d.value;if("number"!=typeof e||!Number.isSafeInteger(e)||e<0||!(e<(n?.length??0)))throw new Error("Invalid primary pane slot");if(s.version<2)throw new Error("A moved price pane needs chart version 2");if(e>0&&!this._host._movablePrimaryPane)throw new Error("A moved price pane needs movablePrimaryPane");o=e}if(void 0!==s.alerts&&(r=fn(s.alerts)),void 0!==s.sourceAbove&&("string"!=typeof s.sourceAbove||!s.sourceAbove.trim()))throw new Error("Invalid source placement");if(void 0!==s.indicators){if(!Array.isArray(s.indicators))throw new Error("Invalid indicator list");for(const e of s.indicators){if("plotPriceScaleIds"in e){const t=Object.getOwnPropertyDescriptor(e,"plotPriceScaleIds");if(!t?.enumerable||!("value"in t))throw new Error("Invalid indicator plot price scale map field")}if(void 0!==e.priceScaleId&&!L(e.priceScaleId))throw new Error("Invalid indicator price scale");if(void 0!==e.barSource&&dr(e.barSource),void 0!==e.instanceId){if("string"!=typeof e.instanceId||!e.instanceId.trim()||h.has(e.instanceId))throw new Error("Invalid or duplicate indicator instance id");h.add(e.instanceId)}}this._host._reserveAlertStudyIds(r,h);const e=new Set([...h,...this._host._indicatorReservedIds,...this._host._indicators.map(e=>e.id)]),t=s.indicators.map(e=>({...e,settings:ks(e.settings??{}),...void 0===e.policy?{}:{policy:js(e.policy)}}));for(const i of t)for(const t of Object.values(i.settings))t&&"object"==typeof t&&"kind"in t&&"indicator"===t.kind&&"instanceId"in t&&"string"==typeof t.instanceId&&(e.add(t.instanceId),h.add(t.instanceId));let i=0;for(const s of t){if(void 0===s.instanceId){do{s.instanceId="restored-study-"+ ++i}while(e.has(s.instanceId));e.add(s.instanceId)}h.add(s.instanceId)}const l=new Map(t.filter(e=>us(e.indicatorId)).map(e=>[e.instanceId,ds(e.indicatorId)])),c=t.flatMap(e=>{const t=l.get(e.instanceId);return t&&void 0!==e.plotPriceScaleIds&&(e.plotPriceScaleIds=Br(t,e.plotPriceScaleIds)),t&&(ss(t.inputs,e.settings),qr(t,e.priceScaleId,e.plotPriceScaleIds,e.paneIndex,void 0===n?this._host._primaryIndex():o??0)),t?[{id:e.instanceId,descriptor:t,settings:e.settings}]:[]});a={specs:t,order:Ts(c).order,descriptors:l}}}catch(e){return{applied:!1,series:[],indicators:0,reason:e instanceof Error?e.message:"Invalid saved alerts or identities"}}this._host._reserveAlertStudyIds(r,h);for(const e of h)this._host._indicatorReservedIds.add(e);const c=++this._restoreGeneration,d=this._host._priceOnlyAutoScale,u=this._host._indicatorLegendCollapsed;this._host._emit("state:restore:start",{});const _=this._host._timeScale.visibleRange();try{if(c!==this._restoreGeneration)return{applied:!1,series:[],indicators:0,reason:"superseded by a newer chart restore"};const e=this._host._withinLayoutChange(()=>this._host._mutateTimeScale(()=>this._restoreState(s,r,h,n,o??0,a,l)));return!e.applied||c!==this._restoreGeneration||d===this._host._priceOnlyAutoScale&&u===this._host._indicatorLegendCollapsed||this._host._emit("objects:change",{}),e}finally{l.clear(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._emitViewportIfMoved(_),this._host._emit("state:restore:end",{})}}_restoreState(e,t,s,r,n,o,a){if(r)for(const e of this._host._panes)e.clearRatioLocks();e.grid&&this._host.setGridOptions(e.grid),e.canvas&&this._host.setCanvasOptions(e.canvas),e.statusLine&&this._host.setStatusLineOptions(e.statusLine),e.watermark&&this._host.setWatermarkOptions(e.watermark),e.trading&&this._host.setTradingSettings(e.trading),e.axisChrome&&this._host.setAxisChromeOptions(e.axisChrome),e.navigation&&"object"==typeof e.navigation&&this._host._input._patchNavigation(e.navigation),e.events&&this._host.setEventOptions(e.events),e.crosshairMode&&(this._host._crosshairMode=e.crosshairMode),"boolean"==typeof e.crosshairSnapToBar&&(this._host._crosshairSnapToBar=e.crosshairSnapToBar);const l=Object.getOwnPropertyDescriptor(e,"priceOnlyAutoScale");l&&"boolean"==typeof l.value&&(this._host._priceOnlyAutoScale=l.value);const h=Object.getOwnPropertyDescriptor(e,"indicatorLegendCollapsed");if(h&&"boolean"==typeof h.value&&(this._host._indicatorLegendCollapsed=h.value),this._host._legendStack._restackLegends(),"string"==typeof e.timezone&&_t(e.timezone)&&this._host.setTimezone(e.timezone),r){for(let e=0;e<r.length;e++)this._host._layout._ensurePane(e);this._host.setPrimaryPaneIndex(n),r.forEach((e,t)=>{this._host._panes[t].weight=e.weight})}(r||o)&&(this._host._panes.forEach((e,t)=>{e!==this._host._primaryPane&&r?.[t]?.collapsed?this._host._collapsed.add(e):this._host._collapsed.delete(e)}),this._host._layout._relayout(),this._host._primitives._rehomeAnchored());let c=0;if(o){this._host._indicatorRefreshes.clear();for(const e of this._host._indicators.splice(0))e.remove({force:!0});const t=new Map(o.specs.map(e=>[e.instanceId,e]));for(const e of o.order){const i=t.get(e),r=o.descriptors.get(e),n=new Xr(this._host._indicatorHost(a),r,i.settings,i.paneIndex,i.instanceId,s,i.priceScaleId,i.plotPriceScaleIds,i.policy,i.barSource,!0);s.add(n.id),this._host._indicators.push(n),!1===i.visible&&n.setVisible(!1),c+=1}const i=new Map(o.specs.map((e,t)=>[e.instanceId,t]));this._host._indicators.sort((e,t)=>i.get(e.id)-i.get(t.id)),this._host._sourceAbove=e.sourceAbove,this._host._studies._reorderIndicatorResources()}const d=[];r&&r.forEach((e,t)=>{const i=this._host._panes[t];if(void 0===i)return;const s=[["right",e.priceScale],...Object.entries(e.scales??{})];i.restoreAxisPlacements(new Map(s.flatMap(([e,t])=>t.placement?[[e,t.placement]]:[])));for(const[e,t]of s){const s=i.scaleFor(e),r={marginTop:t.marginTop,marginBottom:t.marginBottom,minMove:t.minMove,mode:t.mode,inverted:t.inverted};void 0!==t.minPrecision&&(r.minPrecision=t.minPrecision),s.setOptions("right"===e&&void 0===t.minPrecision?this._host._scales._scalePatchFor(i,r):r);const n=t.indicatorRange?this._host._indicatorRanges.get(t.indicatorRange.instanceId):void 0;n&&n.pane===i&&n.scaleId===e&&t.fixedRange?.min===n.range.min&&t.fixedRange?.max===n.range.max?(s.ownsFixedRange(n.token)||(s.setFixedRange(null),s.setAutoScale(!0),s.setOwnedFixedRange(n.token,n.range),this._host._ownedScaleRanges.set(s,n.token)),s.setAutoScale(!0),t.indicatorRange.manual&&(s.setAutoScale(!1),t.range&&s.setPriceRange(t.range))):(void 0!==t.fixedRange&&s.setFixedRange(t.fixedRange),s.setAutoScale(t.autoScale),!t.autoScale&&t.range&&s.setPriceRange(t.range)),t.ratioLock&&d.push({pane:i,id:e,reference:t.ratioLock})}}),this._host._drawingState=e.drawings,this._host._emit("drawings:restore",e.drawings??[]);for(let e=this._host._panes.length-1;e>=0;e--){const t=this._host._panes[e];!this._host._indicators.some(t=>t.paneIndex===e)&&this._host._layout._holdsOnlyFurniture(t)&&this._host.removePane(e)}this._host._alertState=t,this._host._layout._recomputeAxisColumns(),void 0!==e.barSpacing&&this._host._timeScale.setBarSpacing(e.barSpacing),e.viewport&&this._host._dataLayer.length>0&&this._host.setVisibleLogicalRange(e.viewport);for(const{pane:e,id:t,reference:i}of d)this._host._panes.includes(e)&&e.setRatioLock(t,!0,i.barSpacing,i.height);return this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._emit("alerts:restore",t??{version:1,alerts:[]}),this._host._emit("objects:change",{}),{applied:!0,series:e.series??[],indicators:c}}}const fo=new WeakMap,mo=new WeakMap;function bo(e){return fo.get(e)}function go(e){return fo.has(e)}function vo(e,t){t?fo.set(e,Object.freeze({...t})):fo.delete(e);for(const t of mo.get(e)??[])t()}function yo(e,t){let i=mo.get(e);return i||(i=new Set,mo.set(e,i)),i.add(t),()=>{i.delete(t),i.size||mo.delete(e)}}const wo={position:"bottom-right",margin:8,cellWidth:64,cellHeight:18,fontSize:11,borderWidth:1};function xo(e,t,i){const s=Math.floor(.62*e);return 1===t?s:Math.max(0,Math.min(s,Math.floor((e-2*Math.round(4*i))/(1.2*t))))}function So(e,t,i,s){return Math.min(Math.round(e*i),xo(t,s,i))}function Po(e,t){return`${!0===e.italic?"italic ":""}${!0===e.bold?"600 ":""}${t}px ${e.fontFamily??"system-ui, sans-serif"}`}function Oo(e){return e.text.split(/\r\n|\r|\n/)}function ko(e,t){let i=0;for(const s of t)i=Math.max(i,e.measureText(s).width);return i}function Io(e,t,i,s,r,n,o){const a=e=>Math.round(e*o),l=xo(s,i.length,o);let h;if(void 0===t.fontSize&&"auto"===n.fontSize){const s=1===i.length,n=s?a(6):Math.min(a(6),l);h=s?Math.max(n,l):l,e.font=Po(t,h);const o=s?Math.max(1,a(1)):1;for(;h>n&&ko(e,i)>r;)h=s?h-o:Math.max(n,h-o),e.font=Po(t,h)}else h=So(t.fontSize??("auto"===n.fontSize?11:n.fontSize),s,o,i.length);return h}const Mo=28;function To(e,t,i,s,r,n){const[o,a]=e.split("-");return{x:"left"===a?t:"right"===a?r-i-t:(r-i)/2,y:"top"===o?t:"bottom"===o?n-s-t:(n-s)/2}}let Co=0;class jo{constructor(e={}){Object.defineProperty(this,"_rows",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_grid",{enumerable:!0,configurable:!0,writable:!0,value:{cols:0,cells:[]}}),Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_rect",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_hitId",{enumerable:!0,configurable:!0,writable:!0,value:"table-tooltip:"+ ++Co}),Object.defineProperty(this,"_revision",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_hits",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_size",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._opts={...wo,...e}}attached(e){this._host=e}detached(){this._host=null,this._clearHits()}zOrder(){return"top"}options(){return this._opts}setOptions(e){this._opts={...this._opts,...e},this._clearHits(),this._host?.requestUpdate()}setRows(e){const t=function(e){let t=0;for(const i of e)t=Math.max(t,i.length);const i=new Map,s=[];for(let r=0;r<e.length;r++)for(let n=0;n<e[r].length;n++){const o=e[r][n];if(void 0!==o.tooltip&&"string"!=typeof o.tooltip)throw new TypeError("Table tooltip must be text");const a=void 0===o.rowSpan?1:o.rowSpan,l=void 0===o.colSpan?1:o.colSpan;if(!Number.isSafeInteger(a)||a<1||!Number.isSafeInteger(l)||l<1)throw new RangeError("Table spans must be positive safe integers");if(a>e.length-r||l>t-n)throw new RangeError("Table span exceeds the grid bounds");if(a>1||l>1)for(let e=r;e<r+a;e++){let t=i.get(e);if(void 0===t&&(t=[],i.set(e,t)),t.some(e=>n<e.to&&e.from<n+l))throw new RangeError("Table spans overlap");t.push({from:n,to:n+l})}else if(i.get(r)?.some(e=>e.from<=n&&n<e.to))continue;s.push({cell:o,row:r,col:n,rowSpan:a,colSpan:l})}return{cols:t,cells:s}}(e);this._rows=e,this._grid=t,this._clearHits(),this._host?.requestUpdate()}rows(){return this._rows}_clearHits(){this._rect=null,this._hits=[],this._size=null,this._revision++}_sameSize(e){return this._size?.width===e.plotWidth&&this._size.height===e.plotHeight&&this._size.dpr===e.dpr}draw(e,t){this._sameSize(t)||this._clearHits(),this._size={width:t.plotWidth,height:t.plotHeight,dpr:t.dpr},this._rect=null,this._hits=[];const i=this._rows;if(0===i.length)return;const s=this._opts,r=t.dpr,{cols:n,cells:o}=this._grid;if(0===n)return;const a=i.map((e,t)=>{const i=s.rowWeights?.[t];return"number"==typeof i&&i>0?i:1}),l=a.reduce((e,t)=>e+t,0);let h=s.cellHeight,c=h*l;void 0!==s.heightPercent&&s.heightPercent>0&&(c=t.plotHeight*s.heightPercent/100,h=c/l);const d=[];for(let e=0,t=0;e<i.length;e++)d.push(t),t+=h*a[e];const u=a.map(e=>Math.round(h*e*r)),_=o.map(e=>1===e.rowSpan?u[e.row]:Math.round((d[e.row+e.rowSpan]??c)*r)-Math.round(d[e.row]*r));e.save();const p=function(e,t){const i=[];let s=0;for(let r=0;r<t;r++)i.push(s),s+="number"==typeof e?e:e[r]??e[e.length-1]??0;return{x:i,total:s}}("auto"===s.cellWidth?function(e,t,i,s,r,n){const o=Array(i).fill(Mo),a=({cell:t},i)=>{if(""===t.text)return 0;const s=Oo(t),o=t.fontSize??r.fontSize,a="auto"===o?Math.round(11*n):So(o,i,n,s.length);return a<=0?0:(e.font=Po(t,a),ko(e,s)/n+10)};for(let e=0;e<t.length;e++){const i=t[e];1===i.colSpan&&(o[i.col]=Math.max(o[i.col],a(i,s[e])))}for(let e=0;e<t.length;e++){const i=t[e];if(1===i.colSpan)continue;let r=0;for(let e=i.col;e<i.col+i.colSpan;e++)r+=o[e];const n=Math.max(0,a(i,s[e])-r)/i.colSpan;for(let e=i.col;e<i.col+i.colSpan;e++)o[e]+=n}return o}(e,o,n,_,s,r):s.cellWidth,n);let f=p.x,m=p.total;if(void 0!==s.widthPercent&&s.widthPercent>0&&m>0){const e=t.plotWidth*s.widthPercent/100,i=e/m;f=f.map(e=>e*i),m=e}const b=To(s.position,s.margin,m,c,t.plotWidth,t.plotHeight);this._rect={x:b.x,y:b.y,w:m,h:c};const g=e=>Math.round(e*r),v=g(b.x),y=g(b.y);void 0!==s.background&&(e.fillStyle=s.background,e.fillRect(v,y,g(m),g(c))),e.textBaseline="middle";for(let i=0;i<o.length;i++){const{cell:n,row:a,col:l,colSpan:h}=o[i],c=y+g(d[a]),u=_[i],p=v+g(f[l]),b=f[l+h]??m,w=1===h?g(b-f[l]):g(b)-g(f[l]);if(n.tooltip){const e=Math.max(0,p/r),i=Math.max(0,c/r),s=Math.min(t.plotWidth,(p+w)/r),o=Math.min(t.plotHeight,(c+u)/r);s>e&&o>i&&this._hits.push({x:e,y:i,w:s-e,h:o-i,key:`${this._hitId}:${this._revision}:${a}:${l}`,tooltip:n.tooltip})}if(void 0!==n.bgColor&&(e.fillStyle=n.bgColor,e.fillRect(p,c,w,u)),void 0!==s.borderColor&&s.borderWidth>0&&(e.strokeStyle=s.borderColor,e.lineWidth=Math.max(1,g(s.borderWidth)),e.strokeRect(p,c,w,u)),""===n.text)continue;const x=g(4);e.save(),e.beginPath(),e.rect(p,c,w,u),e.clip();const S=Oo(n),P=Io(e,n,S,u,w-2*x,s,r);if(P<=0){e.restore();continue}e.font=Po(n,P),e.fillStyle=n.textColor??(void 0!==n.bgColor?Q(n.bgColor):t.theme.axisText);const O=n.align??"center";e.textAlign=O;const k="left"===O?p+x:"right"===O?p+w-x:p+w/2,I=1.2*P,M=S.length*I,T=Math.min(x,Math.max(0,(u-M)/2)),C="top"===n.verticalAlign?c+T+I/2:"bottom"===n.verticalAlign?c+u-T-M+I/2:c+u/2-(S.length-1)*I/2;for(let t=0;t<S.length;t++)e.fillText(S[t],k,C+t*I);e.restore()}void 0!==s.frameColor&&(s.frameWidth??1)>0&&(e.strokeStyle=s.frameColor,e.lineWidth=Math.max(1,g(s.frameWidth??1)),e.strokeRect(v,y,g(m),g(c))),this._drawTooltip(e,t),e.restore()}_drawTooltip(e,t){if(t.hoverId!==(this._opts.id??this._hitId)||!t.hoverKey)return;const i=this._hits.find(e=>e.key===t.hoverKey),s=t.dpr,r=t.plotWidth*s,n=t.plotHeight*s;if(!i||r<=10*s||n<=6*s)return;e.save(),e.font=11*s+"px system-ui, sans-serif",e.textAlign="left",e.textBaseline="middle";const o=5*s,a=3*s,l=11*s*1.35,h=function(e,t,i,s){const r=[];for(const n of t.split(/\r\n|\r|\n/)){const t=Array.from(n);0===t.length&&r.push("");for(let n=0;n<t.length&&r.length<s;){let s=0,o=t.length-n;for(;s<o;){const r=Math.ceil((s+o)/2);e.measureText(t.slice(n,n+r).join("")).width<=i?s=r:o=r-1}let a=Math.max(1,s);if(n+a<t.length)for(let e=a-1;e>0;e--)if(/\s/.test(t[n+e])){a=e+1;break}r.push(t.slice(n,n+a).join("")),n+=a}if(r.length>=s)break}return r}(e,i.tooltip,r-2*o,Math.ceil((n-2*a)/l)),c=Math.min(r,ko(e,h)+2*o),d=h.length*l+2*a,u=Math.max(0,Math.min((i.x+i.w/2)*s-c/2,r-c)),_=i.y*s-8*s-d,p=Math.max(0,Math.min(_>=0?_:(i.y+i.h)*s+8*s,n-d));e.beginPath(),e.rect(0,0,r,n),e.clip(),e.beginPath(),ne(e,u,p,c,d,Math.min(3*s,c/2,d/2)),e.fillStyle=t.theme.axisText,e.fill(),e.fillStyle=Q(t.theme.axisText);for(let t=0;t<h.length;t++)e.fillText(h[t],u+o,p+a+(t+.5)*l);e.restore()}hitTest(e,t,i){if(void 0!==i&&!this._sameSize(i))return this._clearHits(),null;if(!Number.isFinite(e)||!Number.isFinite(t)||!this._size||e<0||t<0||e>this._size.width||t>this._size.height)return null;const s=this._rect;if(null===s)return null;for(const i of this._hits)if(e>=i.x&&e<i.x+i.w&&t>=i.y&&t<i.y+i.h)return{externalId:this._opts.id??this._hitId,hoverKey:i.key,zOrder:"top",distance:0,cursor:"default"};return void 0===this._opts.id||e<s.x||e>s.x+s.w||t<s.y||t>s.y+s.h?null:{externalId:this._opts.id,zOrder:"top",distance:0,cursor:"default"}}}const Ao=["#f5a623","#26a69a","#ab47bc","#ef5350","#26c6da","#8bc34a","#ff7043","#5c6bc0"];class Lo{constructor(e,t){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_chart",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_recomputing",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_indicatorWork",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_indicatorProcessed",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_barColorOwner",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_barColors",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_barColorBase",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_barColorAnchor",{enumerable:!0,configurable:!0,writable:!0,value:0}),this._chart=e,this._host=t}addIndicator(e,t,i){const s=i.instanceId;if(void 0!==s&&("string"!=typeof s||!s.trim()))throw new TypeError("Invalid indicator instance id");if(void 0!==s&&this._host._indicators.some(e=>e.id===s))throw new Error(`Indicator instance id already in use: ${s}`);if(void 0!==i.priceScaleId&&!L(i.priceScaleId))throw new TypeError("Invalid indicator price scale");const r=void 0===i.policy?void 0:js(i.policy),n=void 0===i.barSource?void 0:dr(i.barSource),o=ds(e),a=ks(t);ss(o.inputs,a);const l=void 0===i.plotPriceScaleIds?void 0:Br(o,i.plotPriceScaleIds);qr(o,i.priceScaleId,l,i.paneIndex??("onchart"===o.placement?this._host._primaryIndex():this._host._panes.length),this._host._primaryIndex()),this._flushIndicators();const h=this._host._series._sharedAxis(),c=new Set([...this._host._indicatorReservedIds,...this._host._indicators.map(e=>e.id)]);for(const e of Ts(this._host._indicators.map(e=>e.dependencyNode())).dependencies.values())for(const t of e)c.add(t.source.instanceId);const d=new Xr(this._indicatorHost(),o,this._distinctColors(o,a),i.paneIndex,s,c,i.priceScaleId,l,r,n);return this._host._indicators.push(d),this._invalidateStudies([d.id],h),this._host._legendStack._restackLegends(),this._host._indicatorReservedIds.add(d.id),this._queueIndicatorDependents(d.id,!0),this._host._emit("objects:change",{}),d}_distinctColors(e,t){const i=this._host._indicators.filter(t=>t.indicatorId===e.id).length;if(0===i)return t;const s={...t},r=e.plots;for(let e=0;e<r.length;e++){const t=ns(r[e]).color;void 0===s[t]&&(s[t]=Ao[(i*r.length+e)%Ao.length])}return s}indicators(){return this._flushIndicators(),this._host._indicators}moveIndicator(e,t,s){const r=this._host._indicators.find(t=>t.id===e);if(this._host.isDestroyed||!r||!this._host._policyAllows(r,"movable",s)||!Number.isInteger(t)||t<0||t>this._host._panes.length||r.paneIndex===t||!r.canRelocate(t))return!1;const n=r.paneIndex,o=t===this._host._panes.length;this._host._layout._ensurePane(t);const a=this._host._panes[t],l=r.renderResources();for(const{api:e,overlay:t}of l.series){if(t)continue;const i=this._host._seriesOwners.get(e),s=this._host._seriesRecords.get(e);if(!i||!s||i.pane===a)continue;const r=i.pane.scaleOf(s).options;i.pane.removeSeries(s),a.addSeries(s),i.pane=a,o&&1===a.series().filter(e=>e.scaleId===s.scaleId).length&&a.scaleOf(s).setOptions(r),this._host._series._applySeriesPriceFormat(a.scaleOf(s),i.priceFormat),void 0!==s.style.precision&&this._host._series._applyPrecision(a.scaleOf(s),s.style.precision)}for(const{primitive:e,overlay:t}of l.primitives)t||this._host._panes.find(t=>t.hasPrimitive(e))?.transferPrimitive(e,a);r.relocate(t),this._host._primitives._reanchorSource(),this._host._legendStack._syncLegendPanes(),this._host._emit("objects:change",{});const h=this._host._panes[n];return this._host._layout._holdsOnlyFurniture(h)&&this._host.removePane(n),this._reorderIndicatorResources(),this._host._layout._recomputeAxisColumns(),this._host._layout._relayout(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._emit("objects:change",{}),!0}reorderIndicator(e,t,s){if(-1!==t&&1!==t)return!1;const r=this._host._indicators.findIndex(t=>t.id===e);if(this._host.isDestroyed||r<0||!this._host._policyAllows(this._host._indicators[r],"movable",s))return!1;const n=this._host._indicators[r].paneIndex;let o=r+t;for(;o>=0&&o<this._host._indicators.length&&this._host._indicators[o].paneIndex!==n;)o+=t;return!(o<0||o>=this._host._indicators.length)&&([this._host._indicators[r],this._host._indicators[o]]=[this._host._indicators[o],this._host._indicators[r]],this._reorderIndicatorResources(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._emit("objects:change",{}),!0)}_reorderIndicatorResources(){for(const e of this._host._indicators)e.refreshBarColors();const e=this._host._indicators.map(e=>e.renderResources()),t=e.flatMap(e=>e.series.flatMap(({api:e})=>{const t=this._host._seriesRecords.get(e);return t?[t]:[]})),i=e.flatMap(e=>e.primitives.map(e=>e.primitive));for(const e of this._host._panes)e.reorderSeries(t),e.reorderPrimitives(i);const s=this._host._indicators.flatMap(e=>e.legend()?[e.legend()]:[]),r=new Set(s);let n=0;for(const e of this._host._legends)r.has(e.legend)&&(e.legend=s[n++]);this._host._primitives._placeSource(),this._host._legendStack._syncLegendPanes()}removeIndicator(e,t){const i=this._host._indicators.find(t=>t.id===e);return void 0!==i&&i.remove(t)}_forgetIndicator(e,t){const i=this._host._indicators.findIndex(t=>t.id===e);if(i<0){const e=void 0===t?void 0:this._host._panes[t];return void(void 0!==t&&void 0!==e&&this._host._layout._holdsOnlyFurniture(e)&&this._host.removePane(t))}const{indicatorId:s,paneIndex:r}=this._host._indicators[i];this._host._indicators.splice(i,1),this._host._primitives._reanchorSource(),this._host._legendStack._restackLegends(),this._host._indicatorReservedIds.add(e),this._host._indicatorRefreshes.delete(e),this._queueIndicatorDependents(e,!0),this._host._emit("indicatorRemoved",{instanceId:e,indicatorId:s,paneIndex:r});const n=this._host._panes[r];void 0!==n&&n!==this._host._primaryPane&&0===n.series().length&&this._host.removePane(r)}_indicatorHost(e){return{assignIndicatorScale:(e,t,i,s)=>this._assignIndicatorScale(e,t,i,s),bindIndicatorPrimitiveScale:(e,t)=>{const s=this._host._panes.find(t=>t.hasPrimitive(e));s?.bindPrimitiveScale(e,t),this._host._layout._recomputeAxisColumns();const r=void 0===s?-1:this._host._panes.indexOf(s);this._host.invalidate(e=>r<0?e.invalidateGlobal(i.Full):e.invalidatePane(r,{level:i.Full,autoScale:!0}))},setIndicatorRange:(e,t,i,s,r)=>{const n=this._host._indicatorRanges.get(e),o=this._host._panes[t];s&&o?this._host._indicatorRanges.set(e,{pane:o,scaleId:i,range:s,series:r,token:n?.token??{}}):this._host._indicatorRanges.delete(e),this._reconcileIndicatorRanges()},legendIndex:()=>this._host._readoutIndex(),indicatorRemoved:(e,t)=>this._forgetIndicator(e,t),flushIndicators:()=>this._flushIndicators(),validateIndicatorSettings:(e,t,i)=>{const s=this._host._indicators.filter(t=>t.id!==e).map(e=>e.dependencyNode());s.push({id:e,descriptor:t,settings:i}),Ts(s)},studyOutput:e=>this._host._indicators.find(t=>t.id===e.instanceId)?.studyOutput(e.plotKey),indicatorOutputChanged:(e,t)=>this._queueIndicatorDependents(e,t),indicatorRecompute:(e,t,i)=>{if(!this._host._indicators.some(t=>t.id===e))return void i();if(this._recomputing)return i(),this._indicatorWork?.delete(e),void this._indicatorProcessed?.add(e);this._host._indicatorRefreshes.set(e,t||!0===this._host._indicatorRefreshes.get(e)),this._queueIndicatorDependents(e,t);const s=this._host._indicatorsDirty;this._flushIndicators(),s&&(this._host._indicatorsDirty=!0,this._host._loop.requestFrame())},resourcesChanged:()=>this._reorderIndicatorResources(),formatPrice:(e,t,i)=>(i?.priceScale()??this._host._panes[e]?.priceScale)?.format(t),policyChanged:()=>{this._host._legendStack._restackLegends(),this._host._emit("objects:change",{})},addIndicatorLegend:e=>{const t=["hide","settings","close"];!0===e.hasSource&&t.splice(t.indexOf("settings")+1,0,"source");const i=new Oi({...e,actions:t});return this._host._legendActions.set(i,[t,i.options().actions]),this._host._studyLegends.add(i),this._host._primitives._addPrimitive(e.paneIndex,i),i},removeIndicatorLegend:e=>{this._host._studyLegends.delete(e),this._host.removePrimitive(e),this._host._legendStack._restackLegends()},legendRowsOn:e=>this._host._legends.filter(t=>t.paneIndex===e).length,primarySeries:()=>this._host.primarySeries(),setIndicatorSeriesType:(e,t)=>this._host._series._setSeriesType(e,t,!1),addIndicatorSeries:(t,i,s,r,n)=>this._host._series._createSeries(t,{paneIndex:i,style:s,priceScaleId:r,priceFormat:n},!1,e),addIndicatorLevel:(e,t)=>{const i={price:e.price,color:e.color,lineWidth:e.lineWidth,lineStyle:e.lineStyle,leftLabel:e.label,id:e.id};return this._host.addPriceLine(i,t)},removeIndicatorLevel:e=>this._host.removePrimitive(e),addIndicatorFill:(e,t)=>this._host._primitives._addPrimitive(t,e),removeIndicatorFill:e=>this._host.removePrimitive(e),removeIndicatorMarkers:e=>this._host.removePrimitive(e),addIndicatorPrimitive:(e,t)=>this._host._primitives._addPrimitive(t,e),removeIndicatorPrimitive:e=>this._host.removePrimitive(e),addIndicatorTable:e=>{const t=new jo;return this._host._primitives._addPrimitive(e,t),t},removeIndicatorTable:e=>this._host.removePrimitive(e),sourceBars:()=>null===this._host._firstDataId.value?[]:this._host._dataLayer.seriesBars(this._host._firstDataId.value),underlyingBars:()=>this._host._series._underlying(this._host._firstDataId.value),sourceState:()=>{const e=this._host._seriesProvenance.get(this._host._firstDataId.value??-1)?.snapshot(),t=bo(this._chart);return e&&t?{...e,provenance:"replay",confirmation:t.forming?"forming":"confirmed",confirmationSource:"replay"}:e},nextPaneIndex:()=>this._host._panes.length,primaryPaneIndex:()=>this._host._primaryIndex(),timezone:()=>this._host._timezone,now:()=>this._host._wallClock(),symbol:()=>this._host._dataContext?.symbol,interval:()=>this._host._dataContext?.interval,dataContext:()=>this._host._dataContext,requestBars:e=>{const t=this._host._barsProvider;return null===t?Promise.reject(new Error("openalgo-charts: this chart has no bars provider; call chart.setBarsProvider(...) to serve other instruments")):bs(i=>"function"==typeof t?t({...e,signal:i}):t.requestBars({...e,signal:i}),[e.signal,this._host._barsRequests.signal])},requestSnapshot:e=>{const t=this._host._barsProvider;return bs(i=>{if("object"!=typeof t||void 0===t?.requestSnapshot)throw new Error("Requested snapshots are unsupported by this provider");const s=bo(this._chart);if(s&&void 0===s.asOf)throw new Error("Requested snapshots require an availability clock during replay");if(void 0!==e.asOf&&!Number.isFinite(e.asOf))throw new RangeError("Requested availability time must be finite");const r=void 0===s?.asOf?e.asOf:Math.min(e.asOf??1/0,s.asOf);return t.requestSnapshot({...e,...void 0===r?{}:{asOf:r},signal:i})},[e.signal,this._host._barsRequests.signal])},requestState:()=>({source:this._host._seriesProvenance.get(this._host._firstDataId.value??-1)?.snapshot(),providerRevision:this._host._barsProviderRevision,dataRevision:this._host._requestedDataRevision,supportsSnapshots:this._host.hasSnapshotProvider(),replay:bo(this._chart)}),subscribeRequestChanges:e=>{const t=["data:context","data:range","data:requests"].map(t=>this._host.on(t,e));return t.push(yo(this._chart,e)),()=>{for(const e of t)e()}},subscribeDataChanges:e=>{const t=this._host.on("data:context",()=>e("context")),i=this._host.on("data:range",()=>e("range"));return()=>{t(),i()}},tickSize:e=>{const t=this._host._panes[e]??this._host._primaryPane,i=t?.priceScale.options.minMove??0;return i>0?i:void 0},setBarColors:(e,t)=>this._setBarColors(e,t),emit:(e,t)=>this._host._bus.emit(e,t),setPaneRange:(e,t)=>{const i=this._host._panes[e];if(void 0===i)return;const s=i===this._host._primaryPane||this._host._indicators.filter(t=>t.paneIndex===e).length>1;i.priceScale.setFixedRange(s?null:t),null===t&&i.priceScale.setAutoScale(!0)}}}_assignIndicatorScale(e,t,s,r){if(this._host._destroyed||!this._host._indicators.some(t=>t.id===e))return!1;for(const e of t)if(!L(e.scaleId)||null===this._host.seriesType(e.api))return!1;for(const e of s)if(!L(e.scaleId)||!this._host._panes.some(t=>t.hasPrimitive(e.primitive)))return!1;this._host._scaleMutationDepth++;try{for(const{api:e,scaleId:i}of t){const t=this._host._seriesRecords.get(e),s=this._host._seriesOwners.get(e),r=s.pane.scaleFor(i);t.scaleId=i,this._host._series._applySeriesPriceFormat(r,s.priceFormat),void 0!==t.style.precision&&this._host._series._applyPrecision(r,t.style.precision)}for(const{primitive:e,scaleId:t}of s)this._host._panes.find(t=>t.hasPrimitive(e)).bindPrimitiveScale(e,t);r(),this._host._layout._recomputeAxisColumns()}finally{this._host._scaleMutationDepth--,this._host.invalidate(e=>e.invalidateGlobal(i.Full))}return this._host._emit("objects:change",{}),!0}_reconcileIndicatorRanges(){const e=new Map;for(const t of this._host._indicatorRanges.values()){if(!this._host._panes.includes(t.pane))continue;const i=t.pane.scaleFor(t.scaleId);if(e.has(i))continue;const s=[...this._host._indicatorRanges.values()].filter(e=>e.pane===t.pane&&e.scaleId===t.scaleId&&e.range.min===t.range.min&&e.range.max===t.range.max),r=new Set(s.flatMap(e=>e.series.flatMap(e=>{const t=this._host._seriesRecords.get(e);return t?[t]:[]})));t.pane.series().some(e=>e.scaleId===t.scaleId&&!r.has(e))||e.set(i,t)}for(const[t,i]of this._host._ownedScaleRanges)e.has(t)||(t.clearOwnedFixedRange(i),this._host._ownedScaleRanges.delete(t));for(const[t,i]of e){const e=this._host._ownedScaleRanges.get(t)??i.token;t.setOwnedFixedRange(e,i.range)?this._host._ownedScaleRanges.set(t,e):t.ownsFixedRange(e)||this._host._ownedScaleRanges.delete(t)}}_setBarColors(e,t){if(null===e){if(this._barColorOwner!==t)return;this._barColorOwner=null}else this._barColorOwner=t;this._barColors=e,this._applyBarColors()}_applyBarColors(){const e=this._host._firstDataId.value;if(null===e)return;const t=this._host._dataLayer.seriesBars(e),s=t.length,r=this._barColorBase;(s<r.length||r.length>0&&t[0].time!==this._barColorAnchor)&&(r.length=0),0===r.length&&(this._barColorAnchor=s>0?t[0].time:0);for(let e=r.length;e<s;e++)r[e]=t[e].color;const n=this._barColors,o=new Array(s);let a=!1;for(let e=0;e<s;e++){const i=t[e],s=n?.[e]??r[e];s!==i.color?(o[e]={...i,color:s},a=!0):o[e]=i}if(!a)return;this._host._dataLayer.setSeriesData(e,o);const l=this._host._panes.findIndex(t=>t.series().some(t=>t.dataId===e));this._host.invalidate(e=>{l<0?e.invalidateGlobal(i.Light):e.invalidatePane(l,{level:i.Light,autoScale:!1})})}_invalidateIndicators(){this._host._emit("data:range",{}),0!==this._host._indicators.length&&(this._host._indicatorsDirty=!0,this._host._loop.requestFrame())}_flushIndicators(){if(!this._host._indicatorsDirty&&0===this._host._indicatorRefreshes.size)return;if(this._recomputing)return;if(0===this._host._indicators.length)return this._host._indicatorsDirty=!1,void this._host._indicatorRefreshes.clear();const e=this._host._indicatorsDirty,t=this._host._series._sharedAxis(),i=Ts(this._host._indicators.map(e=>e.dependencyNode())).order,s=new Map(this._host._indicatorRefreshes);if(this._host._indicatorRefreshes.clear(),e)for(const e of i)s.has(e)||s.set(e,!1);this._host._indicatorsDirty=!1,this._recomputing=!0,this._indicatorWork=s;const r=new Set;this._indicatorProcessed=r;try{for(const e of i){if(!s.has(e)||r.has(e))continue;const t=this._host._indicators.find(t=>t.id===e);t&&(r.add(e),t.recompute(s.get(e)))}for(const e of this._host._indicators)e.republishBarColors()}finally{this._indicatorWork=null,this._indicatorProcessed=null,this._recomputing=!1,this._invalidateStudies(r,t)}}_invalidateStudies(e,t){const i=new Set;for(const t of e){const e=this._host._indicators.find(e=>e.id===t)?.renderResources();for(const{api:t}of e?.series??[]){const e=this._host._seriesOwners.get(t)?.pane;void 0!==e&&i.add(e)}for(const{primitive:t}of e?.primitives??[]){const e=this._host._panes.find(e=>e.hasPrimitive(t));void 0!==e&&i.add(e)}}this._host._series._invalidateWrite(i,t)}_queueIndicatorDependents(e,t){const i=Ts(this._host._indicators.map(e=>e.dependencyNode())),s=[e],r=new Set(s);for(let e=0;e<s.length;e++)for(const[n,o]of i.dependencies){if(r.has(n)||!o.some(t=>t.source.instanceId===s[e]))continue;r.add(n),s.push(n),this._host._indicators.find(e=>e.id===n)?.invalidateStudyOutput();const i=this._indicatorWork&&!this._indicatorProcessed?.has(n)?this._indicatorWork:this._host._indicatorRefreshes;i.set(n,t||!0===i.get(n))}this._host._indicatorRefreshes.size>0&&this._host._loop.requestFrame()}}function No(e){return Math.max(-2,Math.min(2,-e*Math.log(1.1)/100))}function Ro(e,t){return{dist:Math.hypot(e.x-t.x,e.y-t.y),cx:(e.x+t.x)/2,cy:(e.y+t.y)/2}}const Fo="indicator-legend::toggle";class zo{constructor(){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_bounds",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_options",{enumerable:!0,configurable:!0,writable:!0,value:{count:0,collapsed:!1,left:8,top:6,height:18}})}attached(e){this._host=e}detached(){this._host=null,this._bounds=null}zOrder(){return"top"}setOptions(e){Object.keys(e).every(t=>e[t]===this._options[t])||(this._options=e,this._bounds=null,this._host?.requestUpdate())}draw(e,t){this._bounds=null;const i=this._options,s=t.dpr,r=i.height;if(0===i.count||t.plotWidth<18||i.top<0||i.top+r>t.plotHeight)return;e.save(),e.font=`500 ${11*s}px ui-sans-serif, system-ui, sans-serif`;let n=`Indicators ${i.count}`;e.measureText(n).width/s+24>t.plotWidth&&(n=String(i.count));const o=Math.min(t.plotWidth,e.measureText(n).width/s+24),a=Math.max(0,Math.min(i.left,t.plotWidth-o)),l=i.top;this._bounds={x:a,y:l,width:o,height:r},e.beginPath(),e.rect(a*s,l*s,o*s,r*s),e.clip(),e.fillStyle=ie(t.theme.background,.9),e.beginPath(),e.roundRect(a*s,l*s,o*s,r*s,3*s),e.fill(),e.strokeStyle=t.theme.axisText,e.lineWidth=s;const h=(a+8)*s,c=(l+r/2)*s,d=3*s;e.beginPath(),i.collapsed?(e.moveTo(h-d/2,c-d),e.lineTo(h+d/2,c),e.lineTo(h-d/2,c+d)):(e.moveTo(h-d,c-d/2),e.lineTo(h,c+d/2),e.lineTo(h+d,c-d/2)),e.stroke(),e.fillStyle=t.theme.axisText,e.textBaseline="middle",e.textAlign="left",e.fillText(n,(a+16)*s,c),e.restore()}hitTest(e,t){const i=this._bounds;return i&&e>=i.x&&e<i.x+i.width&&t>=i.y&&t<i.y+i.height?{externalId:Fo,zOrder:"top",distance:0,cursor:"pointer"}:null}}function Eo(e){const t=e.pointerType;return{modifiers:{shift:!0===e.shiftKey,alt:!0===e.altKey,ctrl:!0===e.ctrlKey,meta:!0===e.metaKey},pointerType:"touch"===t||"pen"===t?t:"mouse",pressure:Bo(e)}}function Bo(e){const t=e.pressure;return"number"==typeof t&&Number.isFinite(t)?Math.min(1,Math.max(0,t)):0!==(e.buttons??0)?.5:0}class $o{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_pointerInside",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_keyTarget",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_cursorPane",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_cursor",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_dragging",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_navigationCancelled",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_dragStartX",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_dragStartY",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_lastDragY",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_endedPointers",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_pointers",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_pinch",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_pinchPane",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_dragStartOffset",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_indicatorTogglePress",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lastPressOnIndicatorToggle",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_previousPressOnIndicatorToggle",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_clickCbs",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_crosshairCbs",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_readoutTime",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_pointerMoved",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_placementMode",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_downPane",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_downX",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_downLocalY",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_downPressure",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_dragId",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_dragPriceScale",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_dragCancelOnEscape",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_hoverId",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_hoverKey",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_hoverOnBase",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_overlayFrozen",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_timeNavButtons",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_dragCbs",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_axisDrag",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_axisDragScale",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_dragMoved",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_dragFrom",{enumerable:!0,configurable:!0,writable:!0,value:{time:0,price:0}}),Object.defineProperty(this,"_paneResize",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_axisStartCoord",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_axisStartMin",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_axisStartMax",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_axisStartSpacing",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_brandingPress",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._host=e}_attachInput(){if("undefined"==typeof window)return;const e=this._host._container;e.addEventListener("pointerdown",this._host._onPointerDown),e.addEventListener("pointermove",this._host._onPointerMove),e.addEventListener("pointerup",this._host._onPointerUpNative),e.addEventListener("pointercancel",this._host._onPointerCancel),e.addEventListener("lostpointercapture",this._host._onLostPointerCapture),e.addEventListener("pointerleave",this._host._onPointerLeave),e.addEventListener("wheel",this._host._onWheel,{passive:!1}),e.addEventListener("dblclick",this._host._onDblClick),e.addEventListener("pointerenter",this._host._onPointerEnter),e.addEventListener("contextmenu",this._host._onContextMenu);const t="function"==typeof this._host._doc.addEventListener?this._host._doc:e;t.addEventListener("keydown",this._host._onKeyDown),this._keyTarget=t}_detachInput(){if("undefined"==typeof window)return;const e=this._host._container;e.removeEventListener("pointerdown",this._host._onPointerDown),e.removeEventListener("pointermove",this._host._onPointerMove),e.removeEventListener("pointerup",this._host._onPointerUpNative),e.removeEventListener("pointercancel",this._host._onPointerCancel),e.removeEventListener("lostpointercapture",this._host._onLostPointerCapture),e.removeEventListener("pointerleave",this._host._onPointerLeave),e.removeEventListener("wheel",this._host._onWheel),e.removeEventListener("dblclick",this._host._onDblClick),e.removeEventListener("pointerenter",this._host._onPointerEnter),e.removeEventListener("contextmenu",this._host._onContextMenu),this._keyTarget?.removeEventListener("keydown",this._host._onKeyDown),this._keyTarget=null}_onPointerEnter(){this._pointerInside=!0}_onContextMenu(e){if(e.defaultPrevented)return;const t=this._localPoint(e),i=this._host._panes[t.pane];if(void 0!==i)if(this._host._bus.has("contextmenu"))this._host._emit("contextmenu",this._contextMenuEvent(e,t));else{this._cursor=null,this._cursorPane=null;try{const e=i.top.ctx;e.save(),e.globalCompositeOperation="destination-over",e.drawImage(i.base.element,0,0),e.restore(),this._overlayFrozen=!0}catch{}}}_contextMenuEvent(e,t){const i=t.x-this._host._leftAxisWidth,s=i>=0&&t.x<this._host._width-this._host._rightAxisWidth,r=s?Math.round(this._host._timeScale.xToIndex(i)):null;return s&&this._host._ensureScaled(t.pane),{paneIndex:t.pane,point:{x:t.x,y:t.y},price:s?this._priceAt(t.pane,t.localY):null,time:null===r?null:this._host._dataLayer.indexToTime(r)??null,index:r,target:this._contextTarget(t,s,r),preventDefault:()=>e.preventDefault()}}_contextTarget(e,t,i){const s=e.pane===this._host._bottomPaneIndex(),r=e.x>=this._host._width-this._host._rightAxisWidth;if(s&&e.localY>=e.paneHeight-this._host._timeAxisHeight&&!r)return{kind:"time-scale",id:null};if(!t){const t=this._axisAt(e.pane,e.x);return t?{kind:"price-scale",id:null,side:t.side,scaleId:t.scaleId}:{kind:"empty",id:null}}const n=this._host._panes[e.pane],o=this._host._renderContext(e.pane),a=this._hitAt(e.pane,e.x,e.localY),l=null===i||this._host._layout._collapsedShown(e.pane)?null:this._seriesAt(e.pane,i,e.localY);if(null!=a&&(null===l||void 0===a.paintedBy||!n.paintsBelowSeries(a.paintedBy,l,o))){const e=a.externalId;if(e.startsWith("draw:"))return{kind:"drawing",id:e};if(e.startsWith("indicator:")){const t=e.lastIndexOf("::"),i=e.slice(10,t<0?void 0:t);return{kind:"indicator",id:e,instanceId:i}}return e.endsWith("::row")?{kind:"legend",id:e}:{kind:"primitive",id:e}}if(null===l)return{kind:"empty",id:null};for(const e of this._host._indicators)for(const t of ds(e.indicatorId).plots){const i=e.series(t.key);if(i&&this._host._seriesRecords.get(i)===l)return{kind:"indicator",id:`indicator:${e.id}`,instanceId:e.id,plotKey:t.key}}return{kind:"series",id:null,seriesType:l.type}}_axisAt(e,t){return this._host.priceAxisLayout(e).find(e=>t>=e.x&&t<e.x+e.width)}_seriesAt(e,t,i){const s=this._host._panes[e];if(void 0===s)return null;const r=s.series();for(let e=r.length-1;e>=0;e--){const n=r[e];if(!1===n.style.visible)continue;const o=j(this._host._dataLayer,n.dataId,t);if(void 0===o)continue;const a=je(n.type).extents(o,n.style);if(!isFinite(a.min)||!isFinite(a.max))continue;const l=s.scaleOf(n),h=l.priceToY(a.max),c=l.priceToY(a.min);if(i>=Math.min(h,c)-3&&i<=Math.max(h,c)+3)return n}return null}_priceAt(e,t){return this._host._layout._collapsedShown(e)?null:this._host._panes[e]?.yToPrice(t)??null}_unfreezeOverlay(){this._overlayFrozen&&(this._overlayFrozen=!1,this._host.invalidate(e=>e.invalidateGlobal(i.Cursor)))}_localPoint(e){const t=this._host._container.getBoundingClientRect();return this._project(e.clientX-t.left,e.clientY-t.top,this._host._paneLayout())}_project(e,t,i){let s=0;for(let e=0;e<i.length;e++)t>=i[e].top&&(s=e);const r=i[s]??{top:0,height:this._host._height};return{x:e,y:t,pane:s,localY:t-r.top,paneHeight:r.height}}_dragSamples(e){const t="function"==typeof e.getCoalescedEvents?e.getCoalescedEvents():[],i=t.length>0?t:[e],s=this._host._container.getBoundingClientRect(),r=this._host._paneLayout(),n=[];for(const e of i){const t=this._project(e.clientX-s.left,e.clientY-s.top,r);n.push({x:t.x,y:null===this._dragId?t.localY:t.y-(r[this._downPane]?.top??0),pressure:Bo(e)})}return n}_clickInfo(e){const t=Eo(e);return t.pressure=this._downPressure,{...t,shiftKey:t.modifiers.shift,ctrlKey:t.modifiers.ctrl,metaKey:t.modifiers.meta}}_onPointerDown(e){if(this._unfreezeOverlay(),("mouse"===e.pointerType||"pen"===e.pointerType)&&0!==e.button)return;this._previousPressOnIndicatorToggle=this._lastPressOnIndicatorToggle,this._lastPressOnIndicatorToggle=!1,this._endedPointers.delete(e.pointerId),0===this._pointers.size&&(this._navigationCancelled=!1),this._host._motion._stopKinetic(),this._host._motion._stopZoomGlide();const t=this._localPoint(e);this._pointers.set(e.pointerId,{x:t.x,y:t.y,pane:t.pane});try{this._host._container.setPointerCapture?.(e.pointerId)}catch{}if(this._navigationCancelled)return;if(this._pointers.size>=2)return void this._beginPinch();this._downPane=t.pane,this._downX=t.x,this._downLocalY=t.localY,this._downPressure=Bo(e);const i=this._host._layout._dividerAt(t.y);if(null!==i)return void this._beginDividerResize(i,t);const s=Math.max(0,this._host._width-this._host._rightAxisWidth),r=this._host._leftAxisWidth>0&&t.x<this._host._leftAxisWidth,n=t.x>=s||r,o=t.pane===this._host._bottomPaneIndex()&&t.localY>=t.paneHeight-this._host._timeAxisHeight;if(n)return void this._beginPriceAxisDrag(t);if(o)return!1===this._host._navigation.zoomEnabled?void(this._axisDrag="empty"):(this._axisDrag="time",this._axisStartCoord=t.x,this._axisStartSpacing=this._host._timeScale.barSpacing,void(this._dragging=!1));if(this._host._legendStack._indicatorLegendHit(t.pane,t.x,t.localY))return this._lastPressOnIndicatorToggle=!0,this._indicatorTogglePress={pointerId:e.pointerId,moved:!1},this._dragging=!1,void(this._pointerMoved=!1);const a=this._host._panes[t.pane]?.hitTestPrimitives(t.x-this._host._leftAxisWidth,t.localY,this._host._renderContext(t.pane),this._host._branding);return null!==this._host._branding&&!a&&this._brandingHit(t.pane,t.x,t.localY)?(this._brandingPress={pointerId:e.pointerId,mark:this._host._branding,moved:!1},this._dragging=!1,void(this._pointerMoved=!1)):this._placementMode?(this._dragging=!1,void(this._pointerMoved=!1)):void(a&&(!0===a.draggable||"ns-resize"===a.cursor&&this._dragCbs.size>0)?this._beginPrimitiveDrag(e,t,a):(this._dragging=!1!==this._host._navigation.panEnabled,this._setHover(a??null),this._pointerMoved=!1,this._dragStartX=t.x,this._dragStartY=t.y,this._lastDragY=t.y,this._dragStartOffset=this._host._timeScale.rightOffset,this._host._lastDragX=t.x,this._host._lastDragT=this._host._now(),this._host._dragVelocity=0))}_onPointerMove(e){if(this._unfreezeOverlay(),this._pointerInside=!0,this._pointers.size>0&&!this._pointers.has(e.pointerId))return;if(("mouse"===e.pointerType||"pen"===e.pointerType)&&!(1&e.buttons)&&(this._pointers.has(e.pointerId)||this._dragging||null!==this._dragId||null!==this._axisDrag||null!==this._brandingPress||null!==this._indicatorTogglePress))return null!==this._brandingPress&&(this._brandingPress.moved=!0),null!==this._indicatorTogglePress&&(this._indicatorTogglePress.moved=!0),this._host._onPointerUp(e),void this._endedPointers.add(e.pointerId);const t=this._localPoint(e);if(this._pointers.has(e.pointerId)&&this._pointers.set(e.pointerId,{x:t.x,y:t.y,pane:t.pane}),!this._navigationCancelled)if(null===this._pinch){if("empty"!==this._axisDrag)if(null===this._indicatorTogglePress)if(null===this._brandingPress){if("price"===this._axisDrag){const e=t.localY-this._axisStartCoord,s=Math.exp(.005*e),r=(this._axisStartMin+this._axisStartMax)/2,n=(this._axisStartMax-this._axisStartMin)/2*s,o=this._axisDragScale??this._host._panes[this._downPane].priceScale;return o.setPriceRange({min:r-n,max:r+n}),o.setAutoScale(!1),void this._host.invalidate(e=>e.invalidatePane(this._downPane,{level:i.Light,autoScale:!1}))}if(null!==this._paneResize){const e=this._paneResize,s=e.aHeight+e.bHeight,r=e.aWeight+e.bWeight,n=Math.min(24,s/4),o=Math.max(n,Math.min(s-n,e.aHeight+(t.y-e.startY)));return this._host._panes[e.a].weight=o/s*r,this._host._panes[e.b].weight=r-this._host._panes[e.a].weight,this._host._layout._relayout(),void this._host.invalidate(e=>e.invalidateGlobal(i.Full))}if("time"===this._axisDrag){this._host._motion._beginAutoscaleMotion();const e=t.x-this._axisStartCoord;return this._host._mutateTimeScale(()=>this._host._timeScale.setBarSpacing(this._axisStartSpacing*Math.exp(.005*-e))),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),void this._host._emitViewport("zoom")}(this._placementMode||!this._dragging)&&this._pointers.size>0&&this._movedFromPress(t.x,t.localY)&&(this._pointerMoved=!0),null===this._dragId?this._dragging?this._movePan(e,t):this._updateCursor(t.pane,t.x,t.localY,t.y,e):this._movePrimitiveDrag(e,t)}else!this._movedFromPress(t.x,t.localY)&&t.pane===this._downPane&&("mouse"!==e.pointerType||1&e.buttons)||(this._brandingPress.moved=!0);else(this._movedFromPress(t.x,t.localY)||t.pane!==this._downPane)&&(this._indicatorTogglePress.moved=!0)}else this._updatePinch()}_onPointerUp(e){if(this._pointers.size>0&&!this._pointers.has(e.pointerId))return;const t=this._indicatorTogglePress?.pointerId===e.pointerId?this._indicatorTogglePress:null;t&&(this._indicatorTogglePress=null),this._pointers.delete(e.pointerId);try{this._host._container.releasePointerCapture?.(e.pointerId)}catch{}if(this._endedPointers.has(e.pointerId))return void this._endedPointers.delete(e.pointerId);if(this._navigationCancelled)return 0===this._pointers.size&&(this._navigationCancelled=!1),void this._endedPointers.add(e.pointerId);if(t){const i=this._localPoint(e);return this._endedPointers.add(e.pointerId),void(!t.moved&&i.pane===this._downPane&&Math.abs(i.x-this._downX)<=3&&Math.abs(i.localY-this._downLocalY)<=3&&this._host._legendStack._indicatorLegendHit(i.pane,i.x,i.localY)&&this._host._handleLegendAction(Fo))}if(this._brandingPress?.pointerId===e.pointerId){const t=this._brandingPress;this._brandingPress=null;const i=this._localPoint(e);if(!t.moved&&t.mark===this._host._branding&&i.pane===this._downPane&&Math.abs(i.x-this._downX)<=3&&Math.abs(i.localY-this._downLocalY)<=3&&this._brandingHit(i.pane,i.x,i.localY)){const e=t.mark.href();e&&/^https?:\/\//i.test(e)&&this._host._doc.defaultView?.open(e,"_blank","noopener,noreferrer")}return void this._endedPointers.add(e.pointerId)}if(null!==this._pinch)return void(0===this._pointers.size&&(this._pinch=null,this._dragging=!1));if(null!==this._paneResize)return this._paneResize=null,void this._host._emit("paneResized",{paneIndex:this._downPane});if(null!==this._axisDrag)return this._axisDrag=null,void(this._axisDragScale=null);if(null!==this._dragId)return void this._endPrimitiveDrag(e);const i=this._dragging;this._dragging=!1,this._placementMode&&this._pointerMoved?this._endPlacement(e,i):this._pointerMoved?(i&&this._setHover(null),i&&!1!==this._host._navigation.panEnabled&&"touch"===e.pointerType&&"pointercancel"!==e.type&&Di.shouldAnimate(this._host._dragVelocity)&&this._host._startKinetic(this._host._dragVelocity)):this._endClick(e,i)}_movedFromPress(e,t){return Math.abs(e-this._downX)>3||Math.abs(t-this._downLocalY)>3}_beginDividerResize(e,t){const i=this._host._paneLayout(),[s,r]=e;this._paneResize={a:s,b:r,startY:t.y,aWeight:this._host._panes[s].weight,bWeight:this._host._panes[r].weight,aHeight:i[s].height,bHeight:i[r].height},this._dragging=!1}_beginPriceAxisDrag(e){const t=this._axisAt(e.pane,e.x);if(this._dragging=!1,!t||!1===this._host._navigation.zoomEnabled)return void(this._axisDrag="empty");this._axisDrag="price",this._axisDragScale=this._host._panes[e.pane].scaleFor(t.scaleId),this._axisStartCoord=e.localY;const i=this._axisDragScale.priceRange();this._axisStartMin=i.min,this._axisStartMax=i.max,this._dragging=!1}_beginPrimitiveDrag(e,t,s){this._dragId=s.externalId,this._dragPriceScale=s.priceScale??null,this._dragCancelOnEscape=!0===s.cancelOnEscape,this._dragMoved=!1,this._host._ensureScaled(t.pane),this._dragFrom={time:this._host._xToTime(t.x),price:this._dragPriceScale?.yToPrice(t.localY)??this._host._panes[t.pane].yToPrice(t.localY)},this._setHover(s),this._cursor=null,this._cursorPane=null,this._readoutTime=null,this._host.invalidate(e=>e.invalidateGlobal(i.Cursor)),this._dragging=!1,this._pointerMoved=!1;const r={id:s.externalId,...this._dragFrom,paneIndex:this._downPane,point:{x:t.x,y:t.localY},...Eo(e)};this._host._emit("drag:start",r)}_movePrimitiveDrag(e,t){const i=t.y-(this._host._paneLayout()[this._downPane]?.top??0);this._movedFromPress(t.x,i)&&(this._dragMoved=!0);const s=this._dragPriceScale?.yToPrice(i)??this._host._panes[this._downPane].yToPrice(i),r=this._host._xToTime(t.x);mn(this._dragCbs,{end:!1,id:this._dragId,price:s,time:r});const n={id:this._dragId,price:s,time:r,paneIndex:this._downPane,fromPrice:this._dragFrom.price,fromTime:this._dragFrom.time,point:{x:t.x,y:i},samples:this._dragSamples(e),...Eo(e)};this._host._emit("drag",n)}_movePan(e,t){this._host._motion._beginAutoscaleMotion();const s=t.x-this._dragStartX;if((Math.abs(s)>3||Math.abs(t.y-this._dragStartY)>3)&&(this._pointerMoved=!0),this._pointerMoved&&null!==this._hoverId&&this._setHover(null),this._host._mutateTimeScale(()=>this._host._timeScale.setRightOffset(this._dragStartOffset-s/this._host._timeScale.barSpacing)),"touch"===e.pointerType||"both"===this._host._navigation.mousePan){const i=this._host._layout._collapsedShown(this._downPane)?void 0:this._host._panes[this._downPane]?.priceScale,s=t.y-this._dragStartY;i&&("touch"===e.pointerType||!i.autoScale||Math.abs(s)>3)&&i.panByPixels("touch"!==e.pointerType&&i.autoScale?s:t.y-this._lastDragY)}this._lastDragY=t.y;const r=this._host._now(),n=r-this._host._lastDragT;if(n>0){const e=(t.x-this._host._lastDragX)/n,i=Math.exp(-n/50);this._host._dragVelocity=this._host._dragVelocity*i+e*(1-i)}this._host._lastDragX=t.x,this._host._lastDragT=r,this._host._maybeLoadHistory(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._emitViewport("pan")}_endPrimitiveDrag(e){const t=this._localPoint(e),s=t.y-(this._host._paneLayout()[this._downPane]?.top??0),r=this._dragPriceScale?.yToPrice(s)??this._host._panes[this._downPane].yToPrice(s),n=this._host._xToTime(t.x);mn(this._dragCbs,{end:!0,id:this._dragId,price:r,time:n});const o={id:this._dragId,price:r,time:n,paneIndex:this._downPane,point:{x:t.x,y:s},...Eo(e)};if(this._host._emit("drag:end",o),!this._dragMoved){const t=this._dragId;mn(this._clickCbs,t);const i={id:t,price:r,time:n,paneIndex:this._downPane,point:{x:this._downX,y:this._downLocalY},...this._clickInfo(e)};this._host._emit("click",i)}this._dragId=null,this._dragPriceScale=null;const a="touch"===e.pointerType?null:this._hitAt(t.pane,t.x,t.localY);this._setHover(a),this._host.invalidate(e=>e.invalidateGlobal(i.Light))}_endPlacement(e,t){t&&this._setHover(null);const i=this._localPoint(e);this._host._ensureScaled(this._downPane);const s=this._clickInfo(e),r={id:null,price:this._priceAt(this._downPane,this._downLocalY),time:this._host._xToTime(this._downX),paneIndex:this._downPane,point:{x:this._downX,y:this._downLocalY},...s};this._host._emit("click",r);const n={id:null,price:this._priceAt(this._downPane,i.localY),time:this._host._xToTime(i.x),paneIndex:this._downPane,point:{x:i.x,y:i.localY},viaDrag:!0,...s};this._host._emit("click",n)}_endClick(e,t){const i=this._hitAt(this._downPane,this._downX,this._downLocalY);if(t&&this._setHover("touch"===e.pointerType?null:i??null),i&&this._host._handleLegendAction(i.externalId))return;i&&mn(this._clickCbs,i.externalId),this._host._ensureScaled(this._downPane);const s={id:i?.externalId??null,price:this._priceAt(this._downPane,this._downLocalY),time:this._host._xToTime(this._downX),paneIndex:this._downPane,point:{x:this._downX,y:this._downLocalY},...this._clickInfo(e)};this._host._emit("click",s)}_onPointerUpNative(e){("mouse"!==e.pointerType&&"pen"!==e.pointerType||0===e.button)&&this._host._onPointerUp(e)}_onPointerCancel(e){this._cancelPrimitiveDrag("pointercancel"),this._pointers.has(e.pointerId)&&(this._pointerMoved=!0),this._brandingPress?.pointerId===e.pointerId&&(this._brandingPress.moved=!0),this._indicatorTogglePress?.pointerId===e.pointerId&&(this._indicatorTogglePress.moved=!0),this._host._onPointerUp(e)}_onLostPointerCapture(e){if(this._indicatorTogglePress?.pointerId===e.pointerId)return this._indicatorTogglePress.moved=!0,void this._host._onPointerUp(e);this._pointers.has(e.pointerId)&&null===this._dragId&&null===this._paneResize&&null===this._brandingPress&&((null!==this._pinch||this._navigationCancelled)&&(this._navigationCancelled=!0,this._pinch=null),this._dragging=!1,this._axisDrag=null,this._axisDragScale=null,this._host._dragVelocity=0,this._pointerMoved=!0,this._pointers.delete(e.pointerId),0===this._pointers.size&&(this._navigationCancelled=!1),this._endedPointers.add(e.pointerId),this._setHover(null))}_brandingHit(e,t,i){const s=this._host._panes[e];if(null===this._host._branding||!s?.hasPrimitive(this._host._branding))return null;const r=e===this._host._bottomPaneIndex();return this._host._branding.hitTest(t-this._host._leftAxisWidth,i,{timeScale:this._host._timeScale,priceScale:s.priceScale,dataLayer:this._host._dataLayer,plotWidth:this._host._width-this._host._leftAxisWidth-this._host._rightAxisWidth,plotHeight:(this._host._paneLayout()[e]?.height??0)-(r?this._host._timeAxisHeight:0),priceAxisWidth:this._host._rightAxisWidth,dpr:this._host._pixelRatio(),theme:this._host._theme})}_hitAt(e,t,i){return this._host._panes[e]?.hitTestPrimitives(t-this._host._leftAxisWidth,i,this._host._renderContext(e),this._host._branding)??this._brandingHit(e,t,i)}_onPointerLeave(){if(this._pointerInside=!1,this._host._feedTimeNav(null),null===this._dragId&&this._setHover(null),null!==this._cursor){this._cursor=null,this._cursorPane=null,this._host.invalidate(e=>e.invalidateGlobal(i.Cursor));for(const e of this._host._indicators)e.updateLegendValues();const e={time:null,index:null,price:null,bar:null,point:null,paneIndex:null};mn(this._crosshairCbs,e),this._host._emit("crosshair:readout",e),this._host._emit("crosshair:move",e)}}_onWheel(e){const t=function(e,t,i){const s=e.deltaMode,r=e.deltaX??0,n=e.deltaY??0;return{x:Number.isFinite(r)?r*(1===s?16:2===s?t:1):0,y:Number.isFinite(n)?n*(1===s?16:2===s?i:1):0}}(e,this._host._width,this._host._height);if(0===t.x&&0===t.y)return;const s=this._localPoint(e),r=this._host._leftAxisWidth>0&&s.x<this._host._leftAxisWidth,n=this._host._rightAxisWidth>0&&s.x>=this._host._width-this._host._rightAxisWidth,o=!r&&!n&&!e.ctrlKey&&!e.metaKey&&(e.shiftKey||Math.abs(t.x)>Math.abs(t.y));if(o?!1===this._host._navigation.panEnabled:!1===this._host._navigation.zoomEnabled)return;if(!o&&0===t.y)return;if(this._unfreezeOverlay(),e.preventDefault(),this._host._motion._stopKinetic(),r||n){if(0===t.y)return;const e=this._axisAt(s.pane,s.x);if(!e)return;this._host._motion._stopZoomGlide();return this._host._panes[s.pane].scaleFor(e.scaleId).scaleAtY(s.localY,Math.exp(-No(t.y))),void this._host.invalidate(e=>e.invalidateGlobal(i.Full))}if(o)return this._host._motion._stopZoomGlide(),this._host._motion._beginAutoscaleMotion(),this._host._mutateTimeScale(()=>this._host._timeScale.scrollByPixels(-(e.shiftKey&&0===t.x?t.y:t.x))),this._host._maybeLoadHistory(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),void this._host._emitViewport("pan");if(0===t.y)return;const a="right"!==this._host._zoomAnchor||e.ctrlKey||e.metaKey?Math.max(0,Math.min(this._host._timeScale.width,s.x-this._host._leftAxisWidth)):this._host._timeScale.width,l=this._host._motion._remainingZoom(),h=this._host._timeScale.barSpacing,c=this._host._timeScale.constrainBarSpacing(h*Math.exp(l+No(t.y))),d=Math.log(c/h);if(this._host._motion._stopZoomGlide(),0===d)return;if(!this._host._animZoom||!qi.shouldAnimate(d))return void this._host._motion._applyZoom(a,d);const u=d*qi.leadFraction(),_=this._host._motion._navigationEpoch;this._host._motion._applyZoom(a,u),this._host._destroyed||_!==this._host._motion._navigationEpoch||this._host._motion._startZoomGlide(a,d-u)}_onDblClick(e){if(this._lastPressOnIndicatorToggle||this._previousPressOnIndicatorToggle)return;const t=this._localPoint(e);if(this._host._legendStack._indicatorLegendHit(t.pane,t.x,t.localY))return;if(this._brandingHit(t.pane,t.x,t.localY)&&!this._host._panes[t.pane]?.hitTestPrimitives(t.x-this._host._leftAxisWidth,t.localY,this._host._renderContext(t.pane),this._host._branding))return;const i={paneIndex:t.pane,x:t.x,y:t.y,handled:!1};this._host._emit("dblclick",i),this._placementMode||i.handled||("reset"===this._host._doubleClick&&!1!==this._host._navigation.zoomEnabled?this._host.resetScale():"maximize"===this._host._doubleClick&&this._host.maximizePane(t.pane))}_beginPinch(){this._cancelPrimitiveDrag("pinch"),this._brandingPress=null,this._indicatorTogglePress=null;const e=[...this._pointers.values()];this._pinch=Ro(e[0],e[1]),this._pinchPane=e[0].pane,this._dragging=!1,this._axisDrag=null,this._axisDragScale=null,this._dragId=null,this._pointerMoved=!0,this._dragPriceScale=null,this._setHover(null)}_updatePinch(){const e=[...this._pointers.values()];if(e.length<2||null===this._pinch)return;const t=Ro(e[0],e[1]),s=function(e,t){return{factor:e.dist>0?t.dist/e.dist:1,dx:t.cx-e.cx,dy:t.cy-e.cy}}(this._pinch,t);this._pinch=t;const r=!1!==this._host._navigation.zoomEnabled&&1!==s.factor,n=!1!==this._host._navigation.panEnabled&&(0!==s.dx||0!==s.dy);(r||n)&&(this._host._motion._beginAutoscaleMotion(),this._host._mutateTimeScale(()=>{r&&this._host._timeScale.zoomAtX(t.cx,s.factor),n&&this._host._timeScale.setRightOffset(this._host._timeScale.rightOffset-s.dx/this._host._timeScale.barSpacing)}),n&&0!==s.dy&&!this._host._layout._collapsedShown(this._pinchPane)&&this._host._panes[this._pinchPane]?.priceScale.panByPixels(s.dy),this._host._maybeLoadHistory(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._emitViewport(r?"zoom":"pan"))}_cancelPrimitiveDrag(e){null!==this._dragId&&(this._dragMoved=!0,this._host._emit("drag:cancel",{id:this._dragId,paneIndex:this._downPane,reason:e}))}_onKeyDown(e){if(this._unfreezeOverlay(),"Escape"===e.key&&this._dragCancelOnEscape&&null!==this._dragId&&!Nn.shouldIgnore(e.target)){this._cancelPrimitiveDrag("escape"),this._dragId=null,this._dragPriceScale=null,this._dragging=!1,this._pointerMoved=!0;for(const e of this._pointers.keys())this._endedPointers.add(e);return this._pointers.clear(),this._setHover(null),this._host.invalidate(e=>e.invalidateGlobal(i.Light)),void e.preventDefault()}const t=this._host._shortcuts;if(null===t||Nn.shouldIgnore(e.target)||!this._shortcutsActive())return;const s=t.resolve(e);if(null===s)return;if(!this._navigationAllowed(s))return;let r=this._runShortcut(s);r||(r=t.runCustom(s)),r&&(e.preventDefault(),t.emitTrigger(s),this._host._maybeLoadHistory(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._updateAccessibleSummary())}_hovered(){return!!this._pointerInside&&(!1!==this._host._container.isConnected||(this._pointerInside=!1,!1))}_shortcutsActive(){if(null===this._host._shortcuts)return!1;if("global"===this._host._shortcuts.scope||this._hovered())return!0;const e=this._host._doc.activeElement;return null!==e&&(e===this._host._container||!0===this._host._container.contains?.(e))}_runShortcut(e){if(!this._navigationAllowed(e))return!1;const t=this._host._timeScale,i=e=>{this._host._motion._stopZoomGlide(),this._host._motion._beginAutoscaleMotion();const i=t.visibleRange();return this._host._mutateTimeScale(()=>t.setRightOffset(t.rightOffset+e)),this._host._emitViewportIfMoved(i),!0},s=e=>{this._host._motion._stopZoomGlide(),this._host._motion._beginAutoscaleMotion();const i=t.visibleRange();return this._host._mutateTimeScale(()=>t.zoomAtX(this._host._width/2,e)),this._host._emitViewportIfMoved(i),!0};switch(e){case"panLeftBar":return i(-1);case"panRightBar":return i(1);case"panLeft":return i(-2);case"panRight":return i(2);case"panLeftFast":return i(-10);case"panRightFast":return i(10);case"panUp":return this._host._primaryPane.priceScale.panByPixels(20),!0;case"panDown":return this._host._primaryPane.priceScale.panByPixels(-20),!0;case"zoomIn":return s(1.1);case"zoomOut":return s(1/1.1);case"resetScale":return this._host.resetScale(),!0;case"fitContent":return this._host.fitContent(),!0;case"screenshot":return this._host.downloadScreenshot(),!0;case"toggleGridVert":return this._host.setGridOptions({vertLines:!this._host._gridVert}),!0;case"toggleGridHorz":return this._host.setGridOptions({horzLines:!this._host._gridHorz}),!0;case"toggleCrosshairMagnet":return this._host.applyOptions({crosshairMode:"magnet"===this._host._crosshairMode?"normal":"magnet"}),!0;default:return!1}}_setHover(e){const t=e?.externalId??null,s=e?.hoverKey??t;if(this._host._container.style.cursor=this._dragging&&"pointer"!==e?.cursor?"grabbing":e?.cursor??"",t===this._hoverId&&s===this._hoverKey)return;const r=t!==this._hoverId,n=null!==e&&"top"!==e.zOrder,o=n||this._hoverOnBase?i.Light:i.Cursor;this._hoverId=t,this._hoverKey=s,this._hoverOnBase=n,this._host.invalidate(e=>e.invalidateGlobal(o)),r&&this._host._emit("hover",{id:t})}_updateCursor(e,t,s,r,n){const o=this._host._width-this._host._rightAxisWidth,a=t-this._host._leftAxisWidth,l=Math.max(0,o-this._host._leftAxisWidth);if(a<0||a>l)return void this._host._onPointerLeave();const h=this._host._panes[e],c=this._hitAt(e,t,s);if(null===c&&null!==this._host._layout._dividerAt(r))return this._setHover(null),void(this._host._container.style.cursor="row-resize");this._setHover(c),this._host._feedTimeNav(e!==this._host._timeNavPane||this._brandingHit(e,t,s)?null:{x:a,y:s});let d=s;const u=Math.round(this._host._timeScale.xToIndex(a));let _=null;if(null!==this._host._firstDataId.value){const t=j(this._host._dataLayer,this._host._firstDataId.value,u);if(void 0!==t&&(_=t,"magnet"===this._host._crosshairMode&&e===this._host._layout._firstPaneSlot())){const e=function(e,t){const i=[t.open,t.high,t.low,t.close];let s=i[0],r=Math.abs(e-s);for(let t=1;t<i.length;t++){const n=Math.abs(e-i[t]);n<r&&(r=n,s=i[t])}return s}(h.yToPrice(s),_);d=h.priceToY(e)}}this._cursorPane=e,this._cursor={x:a,y:d},this._readoutTime=_?.time??null;for(const e of this._host._indicators)e.updateLegendValues(u);if(this._host.invalidate(e=>e.invalidateGlobal(i.Cursor)),this._crosshairCbs.size>0||this._host._bus.has("crosshair:move")||this._host._bus.has("crosshair:readout")){const i={time:this._host._dataLayer.indexToTime(u)??null,index:u,price:this._priceAt(e,s),bar:_,point:{x:t,y:r},paneIndex:e,pressed:this._pointers.size>0,...Eo(n),...this._pointers.size>0?{samples:this._dragSamples(n)}:{}};mn(this._crosshairCbs,i),this._host._emit("crosshair:readout",i),this._host._emit("crosshair:move",i)}}_patchNavigation(e){const t=this._host._navigation.panEnabled,i=this._host._navigation.zoomEnabled;for(const t of["panEnabled","zoomEnabled"]){const i=Object.getOwnPropertyDescriptor(e,t);i&&"value"in i&&"boolean"==typeof i.value&&(this._host._navigation[t]=i.value)}const s=!1!==t&&!1===this._host._navigation.panEnabled,r=!1!==i&&!1===this._host._navigation.zoomEnabled;(s||r)&&(this._host._motion._stopDisabled(s,r),(null!==this._pinch||s&&this._dragging||r&&("price"===this._axisDrag||"time"===this._axisDrag))&&(this._navigationCancelled=!0,this._dragging=!1,this._axisDrag=null,this._axisDragScale=null,this._pinch=null,this._pointerMoved=!0,this._host._dragVelocity=0,this._setHover(null))),t===this._host._navigation.panEnabled&&i===this._host._navigation.zoomEnabled||this._syncNavigatorPolicy(),"horizontal"!==e.mousePan&&"both"!==e.mousePan||(this._host._navigation.mousePan=e.mousePan);const n=e.defaultVisibleBars;"number"==typeof n&&Number.isFinite(n)&&n>=0&&(this._host._navigation.defaultVisibleBars=Math.min(1e5,Math.floor(n)),void 0===e.defaultBarSpacing&&delete this._host._navigation.defaultBarSpacing);const o=e.defaultBarSpacing;"number"==typeof o&&Number.isFinite(o)&&o>=0&&(0===o?delete this._host._navigation.defaultBarSpacing:this._host._navigation.defaultBarSpacing=o)}_navigationAllowed(e){switch(e){case"panLeftBar":case"panRightBar":case"panLeft":case"panRight":case"panLeftFast":case"panRightFast":case"panUp":case"panDown":return!1!==this._host._navigation.panEnabled;case"zoomIn":case"zoomOut":case"resetScale":case"fitContent":return!1!==this._host._navigation.zoomEnabled;default:return!0}}_syncNavigatorPolicy(){if(null===this._host._timeNav)return;if(this._timeNavButtons.every(e=>null===e||this._navigationAllowed(e)))return void this._host._timeNav.setOptions({buttons:[...this._timeNavButtons]});const e=[];let t=!1;for(const i of this._timeNavButtons)null!==i?this._navigationAllowed(i)&&(t&&e.length>0&&e.push(null),e.push(i),t=!1):t=!0;this._host._timeNav.setOptions({buttons:e})}}const Do=["up","down","collapse","maximize"];function Wo(e=[],t){const i=e.filter(e=>!Do.includes(e));return t&&i.splice(i.includes("close")?i.indexOf("close"):i.length,0,...Do),i}class qo{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_indicatorLegendToggle",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_indicatorLegendRow",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_legendOffset",{enumerable:!0,configurable:!0,writable:!0,value:{top:6,left:8}}),this._host=e}_syncLegendPanes(){for(const e of this._host._legends)e.paneIndex=this._host._panes.findIndex(t=>t.hasPrimitive(e.legend));this._restackLegends()}_restackLegends(){const e=new Map,t=this._host._primitives._priceCornerIndex(),i=this._host._indicators.length,s=new Map;for(const{legend:e,paneIndex:t}of this._host._legends)this._host._studyLegends.has(e)&&!s.has(t)&&s.set(t,e);const r=e=>s.get(e.paneIndex)===e.legend&&this._host._layout._collapsedShown(e.paneIndex),n=new Map(this._host._indicators.map(e=>[e.legend(),e.policy()]));let o=!1;for(const a of[...this._host._legends.filter(r),...this._host._legends.filter(e=>!r(e))]){let l=e.get(a.paneIndex)??0;const h=this._host._studyLegends.has(a.legend),c=h&&this._host._indicatorLegendCollapsed&&!r(a);a.legend.setSuppressed(c||l>0&&this._host._layout._collapsedShown(a.paneIndex)),i>0&&a.paneIndex===t&&h&&!o&&(this._indicatorLegendRow=l++,o=!0);const d=this._host._panes[a.paneIndex],u=this._host._collapsed.has(d);if(h){const e=this._host._legendActions.get(a.legend),t=a.legend.options().actions,i=void 0===e||e[1]!==t?t??[]:e[0],r=n.get(a.legend),o=Wo(i.filter(e=>!("close"===e&&!1===r?.removable||"settings"===e&&!1===r?.configurable)),void 0!==d&&d!==this._host._primaryPane&&s.get(a.paneIndex)===a.legend);a.legend.setOptions({row:l,collapsed:u,actions:o}),this._host._legendActions.set(a.legend,[i,o])}else a.legend.setOptions({row:l});e.set(a.paneIndex,l+(c||!1===a.legend.options().visible?0:1))}if(o||(this._indicatorLegendRow=e.get(t)??0),i>0&&null===this._indicatorLegendToggle)this._indicatorLegendToggle=new zo,this._host.addPrimitive(this._indicatorLegendToggle,{anchor:"primary-pane"});else if(0===i&&null!==this._indicatorLegendToggle){const e=this._indicatorLegendToggle;this._indicatorLegendToggle=null,this._host.removePrimitive(e)}this._syncLegendOffsets()}_syncLegendOffsets(){const e=this._host._primitives._priceCornerIndex(),t=this._host._primaryIndex(),i=yi({iconSize:this._host._legendIconSize}),s=this._legendOffset.top+this._indicatorLegendRow*i;let r=s;for(const t of this._host._legends){const i=t.legend.options();this._host._studyLegends.has(t.legend)||t.paneIndex!==e||!1===i.visible||(i.row??0)>=this._indicatorLegendRow||(r=Math.max(r,(i.top??6)+((i.row??0)+1)*yi(i)))}for(const i of this._host._legends){if(!this._host._studyLegends.has(i.legend))continue;const n=i.paneIndex===e||i.paneIndex===t;i.legend.setOptions(n?{top:this._legendOffset.top+(this._indicatorLegendToggle?r-s:0),left:this._legendOffset.left}:{top:6,left:8})}this._indicatorLegendToggle?.setOptions({count:this._host._indicators.length,collapsed:this._host._indicatorLegendCollapsed,left:this._legendOffset.left,top:r,height:i})}_handleLegendAction(e){if(e===Fo)return this._host.setIndicatorLegendCollapsed(!this._host._indicatorLegendCollapsed),!0;const t=e.lastIndexOf("::");if(t<0)return!1;const s=e.slice(t+2);if(null!==this._host._timeNav&&e.startsWith(`${this._host._timeNav.options().id}::`))return this._host._runShortcut(s)&&this._host.invalidate(e=>e.invalidateGlobal(i.Full)),!0;if("row"===s&&!e.startsWith("indicator:"))return!0;if(!e.startsWith("indicator:"))return!1;const r=e.slice(10,t);if("row"===s)return!0;const n=this._host._indicators.find(e=>e.id===r);if(void 0===n)return!1;const o=n.paneIndex;switch(s){case"close":return this._host.removeIndicator(r),!0;case"hide":return n.setVisible(!n.visible()),!0;case"up":return this._host.movePane(o,-1),!0;case"down":return this._host.movePane(o,1),!0;case"collapse":return this._host.setPaneCollapsed(o,!this._host.paneCollapsed(o)),!0;case"maximize":return this._host.maximizePane(o),!0;case"settings":return!1!==n.policy().configurable&&this._host._emit("indicatorSettings",{instanceId:r,indicatorId:n.indicatorId,paneIndex:o}),!0;case"source":return this._host._emit("indicatorSource",{instanceId:r,indicatorId:n.indicatorId,paneIndex:o}),!0;default:return!1}}_indicatorLegendHit(e,t,i){return null!==this._indicatorLegendToggle&&!0===this._host._panes[e]?.hasPrimitive(this._indicatorLegendToggle)&&null!==this._indicatorLegendToggle.hitTest(t-this._host._leftAxisWidth,i)}}class Vo{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_maximizedPane",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._host=e}_mappedPane(e){return this._host._ensureScaled(e),this._collapsedShown(e)?null:this._host._panes[e]??null}_ensurePane(e){if(!Number.isSafeInteger(e)||e<0)throw new RangeError("Invalid pane index");const t=[];for(;this._host._panes.length<=e;)this._addPane(0===this._host._panes.length?1:.32),t.push(this._host._panes.length-1);if(0!==t.length){this._relayout(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._primitives._rehomeAnchored();for(const e of t)this._host._emit("paneAdded",{paneIndex:e})}}_addPane(e=1){const t=new Bi(this._host._doc,this._host._newBackend());if(t.weight=e,0===this._host._panes.length)this._host._pricePanes.add(t),this._host._primaryPane=t;else for(const e of t.scales())e.setOptions({minPrecision:2});return t.priceScale.setPriceFormatter(this._host._priceFormatter),this._host._priceScaleOptions&&t.priceScale.setOptions(this._host._scales._scalePatchFor(t,this._host._priceScaleOptions)),this._host._panes.push(t),this._host._container.appendChild(t.element),this._host._pixels._observeCanvases(t,!0),t}_remeasure(){if(0===this._host._panes.length)return;const e=this._host._container.clientWidth,t=this._host._container.clientHeight;e>0&&t>0&&(this._host.applySize(e,t),this._host._paintNow())}_relayout(e=!1){this._measureAxisColumns(),e||(this._host._syncTimeNavPane(),this._host._legendStack._restackLegends());const t=this._host._pixelRatio();e||(this._host._layoutRatio=t);const i=this._paneLayout(),s=this._bottomPaneIndex();this._host._panes.forEach((r,n)=>{const o=i[n].height;e?r.setLayoutSize(this._host._width,o):(r.element.style.display=this._layoutWeight(n)>0?"":"none",r.element.style.flex=`0 0 ${o}px`,r.resize(this._host._width,o,t)),r.setScaleHeights(Math.max(0,o-(n===s?this._host._timeAxisHeight:0)))}),e||this._syncSeparators(),this._host._timeScale.setWidth(Math.max(0,this._host._width-this._host._rightAxisWidth-this._host._leftAxisWidth))}_syncSeparators(){const e=this._ratioForLayout(),t=this._topPaneIndex();this._host._panes.forEach((i,s)=>i.setSeparator(s===t?null:this._host._theme.paneSeparator,e))}_ratioForLayout(){return this._host._layoutRatio>0?this._host._layoutRatio:this._host._pixelRatio()}_recomputeAxisColumns(){const e=[this._host._leftAxisWidth,this._host._rightAxisWidth,this._host._axisColumnWidth];this._measureAxisColumns(),e[0]===this._host._leftAxisWidth&&e[1]===this._host._rightAxisWidth&&e[2]===this._host._axisColumnWidth||(this._relayout(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)))}_measureAxisColumns(){let e=0,t=0;this._host._emptyPriceAxis=!this._host._panes.some(e=>e.series().length>0||e.primitives().some(t=>null!==e.primitiveScaleId(t)));for(const i of this._host._panes){const s=i.visibleAxes(this._host._emptyPriceAxis);e=Math.max(e,s.filter(e=>"left"===e.side).length),t=Math.max(t,s.filter(e=>"right"===e.side).length)}this._host._axisColumnWidth=Math.max(0,Math.min(this._host._priceAxisWidth,this._host._width/(e+t+1))),this._host._leftAxisWidth=e*this._host._axisColumnWidth,this._host._rightAxisWidth=t*this._host._axisColumnWidth}_layoutWeight(e){const t=this._host._panes[e];return void 0===t?0:null===this._maximizedPane?t.weight:e===this._maximizedPane?1:0}_topPaneIndex(){for(let e=0;e<this._host._panes.length;e++)if(this._layoutWeight(e)>0)return e;return 0}_bottomPaneIndex(e=!1){for(let t=this._host._panes.length-1;t>=0;t--)if(this._layoutWeight(t)>0&&(!e||!this._collapsedShown(t)))return t;return this._host._panes.length-1}_collapsedShown(e){return null===this._maximizedPane&&this._host._collapsed.has(this._host._panes[e])}_dividerAt(e){const t=this._paneLayout(),i=e=>this._layoutWeight(e)>0&&!this._collapsedShown(e);for(let s=0;s<t.length-1;s++){if(Math.abs(e-t[s].top-t[s].height)>Vo.DIVIDER_GRAB)continue;let r=s,n=s+1;for(;r>=0&&!i(r);)r--;for(;n<t.length&&!i(n);)n++;if(r>=0&&n<t.length)return[r,n]}return null}removePane(e,t){if(!Number.isInteger(e)||e<0||e>=this._host._panes.length||this._host._panes[e]===this._host._primaryPane)return!1;if(this._host._indicators.some(i=>i.paneIndex===e&&!this._host._policyAllows(i,"removable",t)))return!1;if(this._host._eventPane===e){const e=this._host._primaryIndex();null!==this._host._eventMarkers&&(this._host.removePrimitive(this._host._eventMarkers),this._host._primitives._addPrimitive(e,this._host._eventMarkers),this._host._emit("events:change",void 0)),this._host._eventPane=e}this._host._eventPane>e&&(this._host._eventPane-=1);for(let t=this._host._indicators.length-1;t>=0;t--){if(this._host._indicators[t].paneIndex!==e)continue;const[i]=this._host._indicators.splice(t,1);i.remove({force:!0})}const s=this._host._panes[e];for(const e of[...s.series()])s.removeSeries(e),this._host._dataLayer.removeSeries(e.dataId),this._host._seriesProvenance.delete(e.dataId),this._host._firstDataId.value===e.dataId&&(this._host._firstDataId.value=null);this._host._pixels._observeCanvases(s,!1),s.destroy(),this._host._panes.splice(e,1),null!==this._maximizedPane&&(this._maximizedPane===e?this._maximizedPane=null:this._maximizedPane>e&&(this._maximizedPane-=1));for(const t of this._host._indicators)t.paneIndex>e&&t.shiftPane(-1);return this._host._timeScale.setBaseIndex(this._host._dataLayer.baseIndex),this._recomputeAxisColumns(),this._relayout(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._primitives._rehomeAnchored(),this._host._legendStack._syncLegendPanes(),this._remapSavedDrawings(t=>t===e?null:t>e?t-1:t),this._host._emit("paneRemoved",{paneIndex:e}),!0}_remapSavedDrawings(e){const t=this._host._drawingState,i=Array.isArray(t)?t:Array.isArray(t?.drawings)?t.drawings:null;if(null===i)return;const s=i.flatMap(t=>{const i=t?.paneIndex??0;if("object"!=typeof t||null===t||!Number.isInteger(i))return[t];const s=e(i);return null===s?[]:s===i?[t]:[{...t,paneIndex:s}]});this._host._drawingState=Array.isArray(t)?s:{...t,drawings:s}}movePane(e,t){const s=e+t;if(-1!==t&&1!==t||!Number.isInteger(e)||e<0||s<0||e>=this._host._panes.length||s>=this._host._panes.length)return!1;const r=this._host._panes;if(!this._host._movablePrimaryPane&&(r[e]===this._host._primaryPane||r[s]===this._host._primaryPane))return!1;this._remapSavedDrawings(t=>t===e?s:t===s?e:t),[r[e],r[s]]=[r[s],r[e]],this._host._eventPane===e?this._host._eventPane=s:this._host._eventPane===s&&(this._host._eventPane=e),null!==this._host._eventMarkers&&this._host._emit("events:change",void 0),this._maximizedPane===e?this._maximizedPane=s:this._maximizedPane===s&&(this._maximizedPane=e);for(const i of this._host._indicators)i.paneIndex===e?i.shiftPane(t):i.paneIndex===s&&i.shiftPane(-t);for(const e of r)this._host._container.appendChild(e.element);return this._relayout(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._primitives._rehomeAnchored(),this._host._legendStack._syncLegendPanes(),this._host._emit("paneMoved",{from:e,to:s}),!0}setPrimaryPaneIndex(e){if(this._host._destroyed||!this._host._movablePrimaryPane||!Number.isInteger(e)||e<0||e>=this._host._panes.length)return!1;let t=this._host._primaryIndex();if(t===e)return!1;for(let i=0;t!==e;i++){if(i>2*this._host._panes.length||!this._host.movePane(t,e>t?1:-1))return!1;t=this._host._primaryIndex()}return!0}_firstPaneSlot(){const e=null===this._host._firstPane?-1:this._host._panes.indexOf(this._host._firstPane);return e<0?this._host._primaryIndex():e}maximizePane(e){return!(!Number.isInteger(e)||e<0||e>=this._host._panes.length)&&(this._maximizedPane=this._maximizedPane===e?null:e,this._relayout(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._primitives._rehomeAnchored(),this._host._emit("paneMaximized",{paneIndex:this._maximizedPane}),!0)}_holdsOnlyFurniture(e){return e!==this._host._primaryPane&&0===e.series().length&&e.primitives().every(e=>e===this._host._timeNav||this._host._anchored.some(t=>t.primitive===e))}setPaneCollapsed(e,t){const s=this._host._panes[e];if(!Number.isInteger(e)||void 0===s||s===this._host._primaryPane||"boolean"!=typeof t||this._host._collapsed.has(s)===t)return!1;t?this._host._collapsed.add(s):this._host._collapsed.delete(s);const r=t&&this._maximizedPane===e;return r&&(this._maximizedPane=null),this._relayout(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._primitives._rehomeAnchored(),r&&this._host._emit("paneMaximized",{paneIndex:null}),this._host._emit("paneCollapsed",{paneIndex:e,collapsed:t}),!0}_paneLayout(){return function(e,t){const i=e[e.length-1];if(!(t>0&&Number.isFinite(t)&&void 0!==i))return e.map(e=>({...e}));const s=i.top+i.height;let r=e[0].top;return e.map((i,n)=>{const o=n===e.length-1?s:Math.round((i.top+i.height)*t)/t,a={top:r,height:Math.max(0,o-r)};return r=Math.max(r,o),a})}(this._paneShares(),this._ratioForLayout())}_paneShares(){const e=this._bottomPaneIndex(),t=yi({iconSize:this._host._legendIconSize})+12,i=this._host._panes.map((i,s)=>this._collapsedShown(s)?t+(s===e?this._host._timeAxisHeight:0):0);let s=0,r=0;i.forEach((e,t)=>{s+=e,e||(r+=this._layoutWeight(t))});const n=s>this._host._height?this._host._height/s:1,o=Math.max(0,this._host._height-s);let a=0;return i.map((e,t)=>{const i={top:a,height:e?e*n:r>0?o*this._layoutWeight(t)/r:0};return a+=i.height,i})}}Object.defineProperty(Vo,"DIVIDER_GRAB",{enumerable:!0,configurable:!0,writable:!0,value:4});const Ho=new Map;function Go(e){const t=Ho.get(e);if(void 0===t)throw new Error(`openalgo-charts: unknown series transform "${e}"; import 'openalgo-charts/transform' first`);return t}function Uo(){return Array.from(Ho.keys())}function Ko(e){return!("open"in e)&&!("value"in e)}function Yo(e){if("open"in e)return e;if("value"in e){const t=e.value,i={time:e.time,open:t,high:t,low:t,close:t};return void 0!==e.color&&(i.color=e.color),i}return{time:e.time,open:NaN,high:NaN,low:NaN,close:NaN}}const Xo=e=>void 0===e.options?{type:e.type}:{type:e.type,options:{...e.options}};let Jo=null;class Zo{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_transforms",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),this._host=e}_setSeriesType(e,t,s){if(null===this._host.seriesType(e))return!1;const r=this._host._seriesRecords.get(e),n=this._host._seriesOwners.get(e),o=je(t);if(r.type===t)return!1;const a=r.style.precision,l={...r.style};for(const e of Object.keys(n.inheritedStyle))l[e]===n.inheritedStyle[e]&&delete l[e];const h={};for(const e of Object.keys(o.defaultStyle))Object.prototype.hasOwnProperty.call(l,e)||Object.assign(h,{[e]:o.defaultStyle[e]});for(const e of Object.keys(r.style))delete r.style[e];if(Object.assign(r.style,h,l),n.inheritedStyle=h,r.type=t,r.style.precision!==a){const e=n.pane.scaleOf(r);this._applyPrecision(e,r.style.precision),void 0===r.style.precision&&this._applySeriesPriceFormat(e,n.priceFormat)}return this._host.invalidate(e=>e.invalidateGlobal(i.Full)),s&&this._host._emit("objects:change",{}),!0}_setSeriesTransform(e,t,i){return null!==this._host.seriesType(e)&&(null!==Jo?Jo.set(this,e,t,i):(null!==t&&this._newTransform(t),!1))}_seriesTransform(e){const t=null===this._host.seriesType(e)?void 0:this._transforms.get(this._host._seriesRecords.get(e).dataId)?.spec;return void 0===t?null:Xo(t)}_transformOf(e){const t=this._transforms.get(e)?.spec;return void 0===t?void 0:Xo(t)}_underlying(e){const t=null===e?void 0:this._transforms.get(e)?.run;return void 0===t?null:{bars:t.source(),sourceIndex:t.sourceIndex()}}_newTransform(e){const t={...e.options},i=Go(e.type).create(t);return{spec:Object.keys(t).length>0?{type:e.type,options:t}:{type:e.type},run:i}}_createSeries(e,t,s,r){const n=void 0===t.transform?void 0:this._newTransform(t.transform),o=t.paneIndex??this._host._primaryIndex();this._host._layout._ensurePane(o);const a=this._host._dataLayer.createSeries();void 0!==n&&this._transforms.set(a,n);const l=new Ji(a);this._host._seriesProvenance.set(a,l);const h=function(e,t,i,s="right"){return{dataId:e,type:t,style:{...je(t).defaultStyle,...i},scaleId:s}}(a,e,t.style,t.priceScaleId??"right");s&&je(e).isPriceSeries&&this._host._scales._claimPricePane(this._host._panes[o]);const c=s&&null===this._host._firstDataId.value&&je(e).isPriceSeries;c&&(this._host._firstDataId.value=a,this._host._firstPane=this._host._panes[o]),this._host._panes[o].addSeries(h),this._host._layout._recomputeAxisColumns();const d={...je(e).defaultStyle};for(const e of Object.keys(t.style??{}))delete d[e];const u={pane:this._host._panes[o],priceFormat:t.priceFormat,inheritedStyle:d,indicatorOwned:!s},_=u.pane.scaleOf(h),p=!0===r?.get(u.pane)?.has(h.scaleId);this._applySeriesPriceFormat(_,t.priceFormat,p),p||void 0===h.style.precision||this._applyPrecision(_,h.style.precision);const f={setData:(e,t)=>{const i=this._transforms.get(a)?.run;i?.setData(e.map(Yo)),this._setData(a,i?.elements()??e.map(Yo),t,u)},prependData:e=>this._prependData(a,e.map(Yo)),update:(e,t)=>this._updateBar(a,Yo(e),t,u),getData:()=>this._transforms.get(a)?.run.source().slice()??this._host._dataLayer.indexedBars(a).map(e=>e.bar),applyOptions:e=>{for(const t of Object.keys(e))delete u.inheritedStyle[t];Object.assign(h.style,e),"precision"in e&&this._applyPrecision(u.pane.scaleOf(h),e.precision),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._primary?.record===h&&this._host._emit("objects:change",{})},remove:()=>{const e=this._host._primary?.record===h;u.pane.removeSeries(h),this._host._dataLayer.removeSeries(a),this._host._seriesProvenance.delete(a),this._transforms.delete(a),this._host._firstDataId.value===a&&(this._host._firstDataId.value=null),this._host._primary?.record===h&&(this._host._primary=null,u.pane.setSourceSeries(null)),u.indicatorOwned||this._host._studies._reconcileIndicatorRanges(),this._host._timeScale.setBaseIndex(this._host._dataLayer.baseIndex),this._host._layout._recomputeAxisColumns(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),e&&(this._host._emit("data:update",{kind:"reset"}),this._host._emit("objects:change",{}))},priceScale:()=>u.pane.scaleOf(h),createMarkers:e=>{const t=new er(a,e,()=>u.pane.scaleOf(h));return this._host._primitives._addPrimitive(this._host._panes.indexOf(u.pane),t),t}};var m,b;return this._host._seriesRecords.set(f,h),m=f,b=l,Zi.set(m,b),this._host._seriesOwners.set(f,u),u.indicatorOwned||this._host._studies._reconcileIndicatorRanges(),c&&(this._host._primary={api:f,record:h},this._host._panes[o].setSourceSeries(h),void 0!==this._host._sourceAbove&&this._host._primitives._placeSource(),this._host._emit("objects:change",{})),f}_applySeriesPriceFormat(e,t,i=!1){if(t)if("custom"===t.type)i||e.setPriceFormatter(t.formatter);else if("volume"===t.type)i||e.setPriceFormatter(Gi);else if("percent"===t.type){const s=t.precision??2;i||e.setPriceFormatter(e=>`${e.toFixed(s)}%`)}else{i||e.setPriceFormatter(this._host._priceFormatter);const s=t.minMove??(void 0!==t.precision?Math.pow(10,-t.precision):void 0);void 0!==s&&e.setOptions({minMove:s})}}_applyPrecision(e,t){if(void 0===t||!isFinite(t))return void e.setPriceFormatter(this._host._priceFormatter);const i=o(Math.round(t),0,8);e.setPriceFormatter(e=>e.toFixed(i))}_sharedAxis(){const e=this._host._dataLayer,t=this._host._timeScale;return[e.length,e.indexToTime(0)??0,e.indexToTime(e.baseIndex)??0,t.visibleRange().to,t.barSpacing,t.width]}_invalidateWrite(e,t){const s=this._sharedAxis(),r=[...e].map(e=>this._host._panes.indexOf(e));if(r.includes(-1)||s.some((e,i)=>e!==t[i]))return void this._host.invalidate(e=>e.invalidateGlobal(i.Full));if(0===r.length)return;const n=this._clockedPanes();this._host.invalidate(e=>{for(const t of r)e.invalidatePane(t,{level:i.Full,autoScale:!0});for(const t of n)e.invalidatePane(t,{level:i.Light,autoScale:!1})})}_clockedPanes(){const e=this._host._axisChrome,t=[];return!0===e.barCountdown&&this._host._panes.forEach((e,i)=>{e.series().some(e=>!1!==e.style.visible&&je(e.type).isPriceSeries)&&t.push(i)}),e.sessionClock&&t.push(this._host._bottomPaneIndex()),t}_updateBar(e,t,i,s){Xi(i,!0);const r=this._transforms.get(e)?.run;if(void 0!==r)return void Jo.tick(this,e,r,t,i,s);const n=this._sharedAxis(),o=this._host._dataLayer.seriesBars(e),a=o[o.length-1]?.time,l=void 0===a||t.time>a?"append":t.time===a?"replace":"correction",h=this._host._timeScale.rightOffset>=0,c=this._host._dataLayer.update(e,t);this._host._seriesProvenance.get(e)?.record(l,Math.max(a??t.time,t.time),i),this._host._timeScale.setBaseIndex(this._host._dataLayer.baseIndex),"append"!==c||h||this._host._mutateTimeScale(()=>this._host._timeScale.setRightOffset(this._host._timeScale.rightOffset-1)),e===this._host._firstDataId.value&&this._host._studies._invalidateIndicators(),this._invalidateWrite([s.pane],n),this._host._updateAccessibleSummary(),e===this._host._firstDataId.value&&this._host._emit("data:update",{kind:"update",time:t.time})}_setData(e,t,s,r){Xi(s);const n=this._sharedAxis();e===this._host._firstDataId.value&&this._host._motion._stopNavigationMotion(),this._host._dataLayer.setSeriesData(e,t);const o=this._host._dataLayer.seriesBars(e);this._host._seriesProvenance.get(e)?.record("reset",o[o.length-1]?.time,s),e===this._host._firstDataId.value&&(this._host._studies._invalidateIndicators(),this._host._studies._flushIndicators()),this._host._timeScale.setBaseIndex(this._host._dataLayer.baseIndex),!this._host._hasFitContent&&this._host._dataLayer.length>0&&(this._host._timeScale.setWidth(Math.max(0,this._host._width-this._host._rightAxisWidth-this._host._leftAxisWidth)),this._host._hasFitContent=this._host._fitDefaultView()),r.indicatorOwned?this._invalidateWrite([r.pane],n):this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._updateAccessibleSummary(),e===this._host._firstDataId.value&&this._host._emit("data:update",{kind:"reset"})}_prependData(e,t){const s=this._transforms.get(e)?.run;s?.prepend(t),void 0===s?this._host._dataLayer.addBars(e,t):this._host._dataLayer.setSeriesData(e,s.elements());const r=this._host._dataLayer.seriesBars(e);this._host._seriesProvenance.get(e)?.record("prepend",r[r.length-1]?.time),this._host._timeScale.setBaseIndex(this._host._dataLayer.baseIndex),e===this._host._firstDataId.value&&this._host._studies._invalidateIndicators(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._updateAccessibleSummary(),e===this._host._firstDataId.value&&this._host._emit("data:update",{kind:"prepend"})}}class Qo{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._host=e}_scalePatchFor(e,t){if(void 0===t.minMove||this._host._pricePanes.has(e))return t;const i={...t};return delete i.minMove,i.minPrecision=2,i}_claimPricePane(e){if(this._host._pricePanes.has(e))return;this._host._pricePanes.add(e);const t=this._host._priceScaleOptions?.minMove;for(const i of e.axisScales())i.setOptions(void 0!==t?{minMove:t,minPrecision:0}:{minPrecision:0})}priceAxisState(e,t){const i=this._host._panes[e];if(void 0===i||!L(t))return null;const s=i.scaleFor(t),r="left"===t?"right":"left";return{paneIndex:e,scaleId:t,side:"left"===i.axisPlacement(t).side?"left":"right",active:i.usesScale(t),autoFit:s.autoScale,inverted:s.options.inverted,mode:s.options.mode,scaled:s.scaled,lockRatio:i.ratioLocked(t),movable:("right"===t||"left"===t)&&this._canMovePriceAxis(e,t,r)}}_canMovePriceAxis(e,t,i){const s=this._host._panes[e];if(!s||t===i||!s.usesScale(t)||s.usesScale(i))return!1;for(const e of this._host._indicators){const i=e.renderResources(),r=i.series.filter(e=>this._host._seriesOwners.get(e.api)?.pane===s),n=i.primitives.filter(e=>s.hasPrimitive(e.primitive)&&null!==s.primitiveScaleId(e.primitive)),o=r.filter(e=>this._host._seriesRecords.get(e.api)?.scaleId===t),a=n.filter(e=>s.primitiveScaleId(e.primitive)===t);if(o.length||a.length){if(o.some(e=>e.overlay)||a.some(e=>e.overlay))return!1;if(r.some(e=>!e.overlay&&this._host._seriesRecords.get(e.api)?.scaleId!==t)||n.some(e=>!e.overlay&&s.primitiveScaleId(e.primitive)!==t))return!1}}return!0}_ensureScaledFor(e,t){const i=this._host._panes[e];void 0===i||i.scaleFor(t).scaled||i.autoscale(this._host._renderContext(e))}}class ea{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_eventVisible",{enumerable:!0,configurable:!0,writable:!0,value:{}}),this._host=e}setEvents(e,t){const i=this._ensureEventMarkers();i.setEvents(e),this._host._events=i.events();const s=t??this._host._primaryIndex();s!==this._host._eventPane&&(this._host.removePrimitive(i),this._addPrimitive(s,i)),this._host._eventPane=s,this._syncEvents()}_ensureEventMarkers(){return null===this._host._eventMarkers&&(this._host._eventMarkers=new Qn,this._host._eventPane=this._host._primaryIndex(),this._addPrimitive(this._host._eventPane,this._host._eventMarkers),this._host.on("click",e=>{const t=e;if(!t.id||t.viaDrag||t.paneIndex!==this._host._eventPane)return;const i=this._host._eventMarkers?.detailsForHit(t.id);i&&this._host._emit("event:click",{...i,point:{x:t.point.x,y:t.point.y+(this._host._paneLayout()[t.paneIndex]?.top??0)},paneIndex:t.paneIndex})})),this._host._eventMarkers}_syncEvents(){if(null===this._host._eventMarkers&&0===this._host._events.length)return;const e=this._eventVisible;this._ensureEventMarkers().setEvents(this._host._events.filter(t=>!1!==e[t.type])),this._host._emit("events:change",void 0)}seriesStack(e){const t=this._host._panes[e];if(void 0===t)return[];const i=this._stackOwners(t,e),s=[];for(const e of t.series()){const t=i.get(e);void 0===t||s.includes(t)||s.push(t)}return s}moveInSeriesStack(e,t,s,r){if(this._host.isDestroyed||e===t||"above"!==s&&"below"!==s)return!1;const n=e.startsWith("indicator:")?this._host._indicators.find(t=>"indicator:"+t.id===e):void 0,o="source:primary"===e&&null!==this._host._primary?this._host._seriesOwners.get(this._host._primary.api)?.pane:void 0,a=n?n.paneIndex:o?this._host._panes.indexOf(o):-1,l=a<0?[]:this._host.seriesStack(a);if(!l.includes(e)||!l.includes(t)||n&&!this._host._policyAllows(n,"movable",r))return!1;const h=l.filter(t=>t!==e);if(h.splice(h.indexOf(t)+("above"===s?1:0),0,e),h.every((e,t)=>e===l[t]))return!1;const c=h.flatMap(e=>this._host._indicators.filter(t=>"indicator:"+t.id===e)),d=new Set(c);let u=0;for(let e=0;e<this._host._indicators.length;e++)d.has(this._host._indicators[e])&&(this._host._indicators[e]=c[u++]);const _=h.indexOf("source:primary");return _>=0&&(this._host._sourceAbove=0===_?null:h[_-1].slice(10)),this._host._studies._reorderIndicatorResources(),this._host.invalidate(e=>e.invalidateGlobal(i.Full)),this._host._emit("objects:change",{}),!0}setPrimitiveStackAbove(e,t){const s=this._host._panes.findIndex(t=>t.hasPrimitive(e));return!(s<0||null!==t&&"string"!=typeof t)&&(this._host._panes[s].primitiveStackAbove(e)===t||(this._host._panes[s].setPrimitiveStackAbove(e,t),this._host.invalidate(e=>e.invalidatePane(s,{level:i.Light,autoScale:!1}))),!0)}_stackOwners(e,t){const i=new Map;null!==this._host._primary&&this._host._seriesOwners.get(this._host._primary.api)?.pane===e&&i.set(this._host._primary.record,"source:primary");for(const s of this._host._indicators)if(s.paneIndex===t)for(const{api:t}of s.renderResources().series){const r=this._host._seriesRecords.get(t);void 0!==r&&this._host._seriesOwners.get(t)?.pane===e&&i.set(r,"indicator:"+s.id)}return i}_stackSlot(e,t){const i=this._host._panes[e];if(void 0===i)return;const s=this._stackOwners(i,e);let r;for(const e of i.series())s.get(e)===t&&(r=e);return r}_placeSource(){const e=this._host._sourceAbove,t=null===this._host._primary?void 0:this._host._seriesOwners.get(this._host._primary.api);if(void 0===e||void 0===t)return;const i=t.pane,s=this._host._panes.indexOf(i),r=this._host._primary.record,n=this._stackOwners(i,s);n.delete(r);const o=i.series(),a=o.indexOf(r),l=null===e?void 0:this._stackSlot(s,"indicator:"+e);void 0===l&&(this._host._sourceAbove=null);const h=void 0===l?-1:o.indexOf(l),c=o.findIndex((e,t)=>t>h&&e!==r&&n.has(e));a>h&&(c<0||a<c)||i.moveSeries(r,void 0===l?o[c]:o[h+1]??null)}_reanchorSource(){const e=null===this._host._primary?void 0:this._host._seriesOwners.get(this._host._primary.api)?.pane;if("string"!=typeof this._host._sourceAbove||void 0===e)return;const t=this._host.seriesStack(this._host._panes.indexOf(e)),i=t.indexOf("source:primary");this._host._sourceAbove=i>0?t[i-1].slice(10):null}_anchorTarget(e){return"chart-bottom"===e?this._host._bottomPaneIndex(!0):"primary-pane"===e?this._priceCornerIndex():this._host._layout._topPaneIndex()}_priceCornerIndex(){const e=this._host._primaryIndex();return this._host._layout._layoutWeight(e)>0?e:this._host._layout._topPaneIndex()}_rehomeAnchored(){if(0!==this._host._anchored.length)for(const e of this._host._anchored){const t=this._anchorTarget(e.anchor),i=this._host._panes.findIndex(t=>t.hasPrimitive(e.primitive));if(i===t)continue;i>=0&&this._host._panes[i].removePrimitive(e.primitive);const s=this._host._legends.findIndex(t=>t.legend===e.primitive);s>=0&&this._host._legends.splice(s,1),this._addPrimitive(t,e.primitive)}}_addPrimitive(e,t){this._host._layout._ensurePane(e);const s={requestUpdate:()=>{const e=this._host._panes.findIndex(e=>e.hasPrimitive(t)),s="top"===t.zOrder()&&null==this._host._panes[e]?.primitiveStackAbove(t);this._host.invalidate(t=>t.invalidatePane(e,{level:s?i.Cursor:i.Light,autoScale:!1}))}};if(this._host._panes[e].addPrimitive(t,s),t instanceof Oi){if(this._host._legends.push({legend:t,paneIndex:e}),t.setOptions({hasOpenInterest:this._host.hasOpenInterest}),Object.keys(this._host._statusLine).length>0){const e=t.options().statusLine;t.setOptions({statusLine:{...this._host._statusLine,...e}})}void 0!==this._host._legendIconSize&&t.setOptions({iconSize:this._host._legendIconSize}),this._host._legendStack._restackLegends()}this._host.invalidate(t=>t.invalidatePane(e,{level:i.Light,autoScale:!1}))}removePrimitive(e){const t=this._host._anchored.findIndex(t=>t.primitive===e);t>=0&&this._host._anchored.splice(t,1);const s=this._host._legends.findIndex(t=>t.legend===e);s>=0&&this._host._legends.splice(s,1);for(let t=0;t<this._host._panes.length;t++)if(this._host._panes[t].removePrimitive(e))return s>=0&&this._host._legendStack._restackLegends(),this._host._layout._recomputeAxisColumns(),void this._host.invalidate(e=>e.invalidatePane(t,{level:i.Light,autoScale:!1}))}}class ta{constructor(e,t,i,s,r){Object.defineProperty(this,"id",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"x0",{enumerable:!0,configurable:!0,writable:!0,value:t}),Object.defineProperty(this,"y0",{enumerable:!0,configurable:!0,writable:!0,value:i}),Object.defineProperty(this,"x1",{enumerable:!0,configurable:!0,writable:!0,value:s}),Object.defineProperty(this,"y1",{enumerable:!0,configurable:!0,writable:!0,value:r}),Object.defineProperty(this,"stops",{enumerable:!0,configurable:!0,writable:!0,value:[]})}addColorStop(e,t){this.stops.push({offset:e,color:t})}toString(){return`url(#${this.id})`}}const ia=2*Math.PI;function sa(e){if(!Number.isFinite(e))return"0";const t=100*e,i=Number.isFinite(t)?Math.round(t)/100:e;return String(0===i?0:i)}function ra(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function na(e){return/[ .,:;'|!iIljtfr[\]()-]/.test(e)?.3:/[mwMW@%]/.test(e)?.9:/[A-Z]/.test(e)?.68:.55}function oa(e){const t=e.trim().split(/\s+/);let i=null,s=null,r=10,n=0;for(;n<t.length;n++){const e=t[n],o=/^(\d+(?:\.\d+)?)(px|pt)(?:\/\S+)?$/.exec(e);if(null!==o){r=parseFloat(o[1])*("pt"===o[2]?4/3:1),n++;break}"italic"===e||"oblique"===e?s=e:/^(\d{3}|bold|bolder|lighter)$/.test(e)&&(i=e)}return{size:r,family:t.slice(n).join(" ")||"sans-serif",weight:i,style:s}}const aa={top:"text-before-edge",hanging:"hanging",middle:"central",ideographic:"ideographic",bottom:"text-after-edge"},la={center:"middle",right:"end",end:"end"};class ha{constructor(e,t,i={}){Object.defineProperty(this,"width",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"height",{enumerable:!0,configurable:!0,writable:!0,value:t}),Object.defineProperty(this,"fillStyle",{enumerable:!0,configurable:!0,writable:!0,value:"#000"}),Object.defineProperty(this,"strokeStyle",{enumerable:!0,configurable:!0,writable:!0,value:"#000"}),Object.defineProperty(this,"lineWidth",{enumerable:!0,configurable:!0,writable:!0,value:1}),Object.defineProperty(this,"lineCap",{enumerable:!0,configurable:!0,writable:!0,value:"butt"}),Object.defineProperty(this,"lineJoin",{enumerable:!0,configurable:!0,writable:!0,value:"miter"}),Object.defineProperty(this,"miterLimit",{enumerable:!0,configurable:!0,writable:!0,value:10}),Object.defineProperty(this,"globalAlpha",{enumerable:!0,configurable:!0,writable:!0,value:1}),Object.defineProperty(this,"font",{enumerable:!0,configurable:!0,writable:!0,value:"10px sans-serif"}),Object.defineProperty(this,"textAlign",{enumerable:!0,configurable:!0,writable:!0,value:"start"}),Object.defineProperty(this,"textBaseline",{enumerable:!0,configurable:!0,writable:!0,value:"alphabetic"}),Object.defineProperty(this,"shadowColor",{enumerable:!0,configurable:!0,writable:!0,value:"rgba(0, 0, 0, 0)"}),Object.defineProperty(this,"shadowBlur",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"shadowOffsetX",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"shadowOffsetY",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"globalCompositeOperation",{enumerable:!0,configurable:!0,writable:!0,value:"source-over"}),Object.defineProperty(this,"imageSmoothingEnabled",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"canvas",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"unsupported",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_out",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_frames",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_gradients",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_clips",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_rootOpen",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_path",{enumerable:!0,configurable:!0,writable:!0,value:""}),Object.defineProperty(this,"_hasPoint",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_dash",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"lineDashOffset",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_ids",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_strict",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_background",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_fontCache",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._strict=!0===i.strict,this._background=i.background}asCanvasContext(){return this}save(){this._frames.push({kind:"save",open:0,state:this._snapshot()})}restore(){let e=this._frames.length-1;for(;e>=0&&"save"!==this._frames[e].kind;)e--;if(!(e<0)){if(e!==this._frames.length-1&&this._strict)throw new Error("SvgContext: restore() reached across an open pushGroup()");this._closeTo(e)}}pushGroup(e,t={}){let i="<g";for(const t in e)i+=` ${t}="${ra(String(e[t]))}"`;const{translate:s,clip:r}=t;if(void 0!==s&&(i+=` transform="translate(${sa(s.x)} ${sa(s.y)})"`),void 0!==r){i+=` clip-path="url(#${this._clipId(`<rect x="${sa(r.x)}" y="${sa(r.y)}" width="${sa(r.width)}" height="${sa(r.height)}"/>`)})"`}this._out.push(i+">"),this._frames.push({kind:"group",open:1,state:null})}popGroup(){let e=this._frames.length-1;for(;e>=0&&"group"!==this._frames[e].kind;)e--;if(e<0){if(this._strict)throw new Error("SvgContext: popGroup() without a pushGroup()")}else{if(e!==this._frames.length-1&&this._strict)throw new Error("SvgContext: popGroup() reached across an open save()");this._closeTo(e)}}translate(e,t){0===e&&0===t||this._openGroup(`transform="translate(${sa(e)} ${sa(t)})"`)}scale(e,t){1===e&&1===t||this._openGroup(`transform="scale(${sa(e)} ${sa(t)})"`)}rotate(e){0!==e&&this._openGroup(`transform="rotate(${sa(180*e/Math.PI)})"`)}setLineDash(e){this._dash=e.slice()}getLineDash(){return this._dash.slice()}beginPath(){this._path="",this._hasPoint=!1}closePath(){""!==this._path&&(this._path+="Z")}moveTo(e,t){this._path+=`M${sa(e)} ${sa(t)}`,this._hasPoint=!0}lineTo(e,t){this._path+=`${this._hasPoint?"L":"M"}${sa(e)} ${sa(t)}`,this._hasPoint=!0}quadraticCurveTo(e,t,i,s){this._hasPoint||this.moveTo(e,t),this._path+=`Q${sa(e)} ${sa(t)} ${sa(i)} ${sa(s)}`}bezierCurveTo(e,t,i,s,r,n){this._hasPoint||this.moveTo(e,t),this._path+=`C${sa(e)} ${sa(t)} ${sa(i)} ${sa(s)} ${sa(r)} ${sa(n)}`}rect(e,t,i,s){this._path+=`M${sa(e)} ${sa(t)}h${sa(i)}v${sa(s)}h${sa(-i)}Z`,this._hasPoint=!0}roundRect(e,t,i,s,r=0){const n=Array.isArray(r)?r:[r];let[o,a,l,h]=[n[0]??0,n[1]??n[0]??0,n[2]??n[0]??0,n[3]??n[1]??n[0]??0];const c=Math.min(1,i/Math.max(1e-9,o+a,h+l),s/Math.max(1e-9,o+h,a+l));o*=c,a*=c,l*=c,h*=c;const d=(e,t,i)=>e>0?`A${sa(e)} ${sa(e)} 0 0 1 ${sa(t)} ${sa(i)}`:"";this._path+=`M${sa(e+o)} ${sa(t)}H${sa(e+i-a)}${d(a,e+i,t+a)}V${sa(t+s-l)}${d(l,e+i-l,t+s)}H${sa(e+h)}${d(h,e,t+s-h)}V${sa(t+o)}${d(o,e+o,t)}Z`,this._hasPoint=!0}arc(e,t,i,s,r,n=!1){this.ellipse(e,t,i,i,0,s,r,n)}ellipse(e,t,i,s,r,n,o,a=!1){const l=Math.cos(r),h=Math.sin(r),c=r=>{const n=i*Math.cos(r),o=s*Math.sin(r);return[e+n*l-o*h,t+n*h+o*l]};let d,u=!1;a?(u=n-o>=ia,d=-((n-o)%ia+ia)%ia):(u=o-n>=ia,d=((o-n)%ia+ia)%ia);const[_,p]=c(n);this._path+=`${this._hasPoint?"L":"M"}${sa(_)} ${sa(p)}`,this._hasPoint=!0;const f=sa(180*r/Math.PI),m=(e,t,r)=>{const[n,o]=c(e);this._path+=`A${sa(i)} ${sa(s)} ${f} ${t?1:0} ${r?1:0} ${sa(n)} ${sa(o)}`};if(u){const e=a?-Math.PI:Math.PI;m(n+e,!1,!a),m(n+2*e,!1,!a)}else 0!==d&&m(n+d,Math.abs(d)>Math.PI,d>0)}fill(e,t){if("object"==typeof e)return void this._unsupported("fill(Path2D)");if(""===this._path)return;const i=this._paint(this.fillStyle);null!==i&&this._out.push(`<path d="${this._path}" fill="${i}"${"evenodd"===e?' fill-rule="evenodd"':""}${this._opacity()}/>`)}stroke(e){if(void 0!==e)return void this._unsupported("stroke(Path2D)");if(""===this._path)return;const t=this._strokeAttrs();null!==t&&this._out.push(`<path d="${this._path}" fill="none"${t}${this._opacity()}/>`)}clip(e){if("object"==typeof e)return void this._unsupported("clip(Path2D)");if(""===this._path)return;const t=this._clipId(`<path d="${this._path}"${"evenodd"===e?' clip-rule="evenodd"':""}/>`);this._openGroup(`clip-path="url(#${t})"`)}fillRect(e,t,i,s){const r=this._paint(this.fillStyle);null!==r&&this._rect(e,t,i,s,` fill="${r}"`)}strokeRect(e,t,i,s){const r=this._strokeAttrs();null!==r&&this._rect(e,t,i,s,` fill="none"${r}`)}clearRect(e,t,i,s){void 0!==this._background&&this._rect(e,t,i,s,` fill="${ra(this._background)}"`)}fillText(e,t,i){if(""===e)return;const s=this._paint(this.fillStyle);if(null===s)return;const r=this._font();let n=` font-family="${ra(r.family)}" font-size="${sa(r.size)}"`;null!==r.weight&&(n+=` font-weight="${r.weight}"`),null!==r.style&&(n+=` font-style="${r.style}"`);const o=la[this.textAlign];void 0!==o&&(n+=` text-anchor="${o}"`);const a=aa[this.textBaseline];void 0!==a&&(n+=` dominant-baseline="${a}"`),this._out.push(`<text x="${sa(t)}" y="${sa(i)}"${n} fill="${s}"${this._opacity()}>${ra(e)}</text>`)}measureText(e){const t=this._font();let i=0;for(const t of e)i+=na(t);const s=null!==t.weight&&("bold"===t.weight||"bolder"===t.weight||parseInt(t.weight,10)>=600),r=i*t.size*(s?1.05:1);return{width:r,actualBoundingBoxLeft:0,actualBoundingBoxRight:r,actualBoundingBoxAscent:.8*t.size,actualBoundingBoxDescent:.2*t.size,fontBoundingBoxAscent:.8*t.size,fontBoundingBoxDescent:.2*t.size}}createLinearGradient(e,t,i,s){const r=new ta("g"+ ++this._ids,e,t,i,s);return this._gradients.push(r),r}drawImage(e,...t){const i=this._imageHref(e);if(null===i)return void this._unsupported("drawImage");const s=e,r=s.naturalWidth??("number"==typeof s.width?s.width:0),n=s.naturalHeight??("number"==typeof s.height?s.height:0),o=this._opacity();if(t.length>=8){const[e,s,a,l,h,c,d,u]=t;return void this._out.push(`<svg x="${sa(h)}" y="${sa(c)}" width="${sa(d)}" height="${sa(u)}" viewBox="${sa(e)} ${sa(s)} ${sa(a)} ${sa(l)}" preserveAspectRatio="none"><image href="${ra(i)}" width="${sa(r)}" height="${sa(n)}" preserveAspectRatio="none"${o}/></svg>`)}const[a,l]=t,h=t.length>=4?t[2]:r,c=t.length>=4?t[3]:n;this._out.push(`<image href="${ra(i)}" x="${sa(a)}" y="${sa(l)}" width="${sa(h)}" height="${sa(c)}" preserveAspectRatio="none"${o}/>`)}setTransform(){this._unsupported("setTransform")}resetTransform(){this._unsupported("resetTransform")}getTransform(){this._unsupported("getTransform")}createRadialGradient(){this._unsupported("createRadialGradient")}createConicGradient(){this._unsupported("createConicGradient")}createPattern(){return this._unsupported("createPattern"),null}getImageData(){this._unsupported("getImageData")}putImageData(){this._unsupported("putImageData")}createImageData(){this._unsupported("createImageData")}strokeText(){this._unsupported("strokeText")}drawFocusIfNeeded(){this._unsupported("drawFocusIfNeeded")}isPointInPath(){return this._unsupported("isPointInPath"),!1}isPointInStroke(){return this._unsupported("isPointInStroke"),!1}toString(){const e=sa(this.width),t=sa(this.height);let i="";for(const e of this._clips)i+=e;for(const e of this._gradients){i+=`<linearGradient id="${e.id}" gradientUnits="userSpaceOnUse" x1="${sa(e.x0)}" y1="${sa(e.y0)}" x2="${sa(e.x1)}" y2="${sa(e.y1)}">`;for(const t of e.stops)i+=`<stop offset="${sa(t.offset)}" stop-color="${ra(t.color)}"/>`;i+="</linearGradient>"}let s="";for(let e=this._frames.length-1;e>=0;e--)s+="</g>".repeat(this._frames[e].open);return s+="</g>".repeat(this._rootOpen),`<svg xmlns="http://www.w3.org/2000/svg" width="${e}" height="${t}" viewBox="0 0 ${e} ${t}" xml:space="preserve">`+(""===i?"":`<defs>${i}</defs>`)+this._out.join("")+s+"</svg>"}_snapshot(){return{fillStyle:this.fillStyle,strokeStyle:this.strokeStyle,lineWidth:this.lineWidth,lineCap:this.lineCap,lineJoin:this.lineJoin,miterLimit:this.miterLimit,globalAlpha:this.globalAlpha,font:this.font,textAlign:this.textAlign,textBaseline:this.textBaseline,dash:this._dash.slice(),dashOffset:this.lineDashOffset}}_apply(e){this.fillStyle=e.fillStyle,this.strokeStyle=e.strokeStyle,this.lineWidth=e.lineWidth,this.lineCap=e.lineCap,this.lineJoin=e.lineJoin,this.miterLimit=e.miterLimit,this.globalAlpha=e.globalAlpha,this.font=e.font,this.textAlign=e.textAlign,this.textBaseline=e.textBaseline,this._dash=e.dash,this.lineDashOffset=e.dashOffset}_closeTo(e){for(;this._frames.length>e;){const e=this._frames.pop();this._out.push("</g>".repeat(e.open)),null!==e.state&&this._apply(e.state)}}_openGroup(e){this._out.push(`<g ${e}>`);const t=this._frames[this._frames.length-1];void 0!==t?t.open++:this._rootOpen++}_clipId(e){const t="c"+ ++this._ids;return this._clips.push(`<clipPath id="${t}">${e}</clipPath>`),t}_rect(e,t,i,s,r){0!==i&&0!==s&&(i<0&&(e+=i,i=-i),s<0&&(t+=s,s=-s),this._out.push(`<rect x="${sa(e)}" y="${sa(t)}" width="${sa(i)}" height="${sa(s)}"${r}${this._opacity()}/>`))}_paint(e){return this.globalAlpha<=0?null:"string"!=typeof e?e instanceof ta?e.toString():null:"transparent"===e?null:ra(e)}_strokeAttrs(){if(!(this.lineWidth>0))return null;const e=this._paint(this.strokeStyle);if(null===e)return null;let t=` stroke="${e}" stroke-width="${sa(this.lineWidth)}"`;return"butt"!==this.lineCap&&(t+=` stroke-linecap="${this.lineCap}"`),"miter"!==this.lineJoin&&(t+=` stroke-linejoin="${this.lineJoin}"`),this._dash.some(e=>e>0)&&(t+=` stroke-dasharray="${this._dash.map(sa).join(" ")}"`,0!==this.lineDashOffset&&(t+=` stroke-dashoffset="${sa(this.lineDashOffset)}"`)),t}_opacity(){return this.globalAlpha<1?` opacity="${sa(this.globalAlpha)}"`:""}_font(){return null!==this._fontCache&&this._fontCache.font===this.font||(this._fontCache={font:this.font,parsed:oa(this.font)}),this._fontCache.parsed}_imageHref(e){const t=e;if("string"==typeof t.src&&""!==t.src)return t.src;if("function"==typeof t.toDataURL)try{return t.toDataURL.call(e)}catch{return null}return null}_unsupported(e){if(this._strict)throw new Error(`SvgContext: ${e} has no SVG form and cannot be exported`);this.unsupported.includes(e)||this.unsupported.push(e)}}const ca='600 {size}px system-ui, -apple-system, "Segoe UI", sans-serif';class da{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._opts={text:e.text,fontSize:e.fontSize??64,opacity:e.opacity??.08,zOrder:e.zOrder??"bottom",id:e.id??"text-watermark",color:e.color,font:e.font}}zOrder(){return this._opts.zOrder}attached(e){this._host=e}detached(){this._host=null}autoscaleInfo(){return null}hitTest(){return null}setOptions(e){this._opts={...this._opts,...e},this._host?.requestUpdate()}draw(e,t){const i=this._opts;if(""===i.text)return;const s=t.dpr,r=t.plotWidth*s,n=t.plotHeight*s;if(r<=0||n<=0)return;e.save(),e.beginPath(),e.rect(0,0,r,n),e.clip(),e.globalAlpha=i.opacity,e.fillStyle=i.color??"#9aa4b2",e.textAlign="center",e.textBaseline="middle";const o=Math.min(i.fontSize*s,.8*n),a=(i.font??ca).replace("{size}",String(Math.round(o)));e.font=a;const l=e.measureText(i.text).width,h=.8*r;if(l>h&&l>0){const t=Math.max(.1,o*(h/l));e.font=(i.font??ca).replace("{size}",String(t))}e.fillText(i.text,r/2,n/2),e.restore()}}class ua{constructor(e){Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_brandingOptions",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_watermark",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_watermarkOptions",{enumerable:!0,configurable:!0,writable:!0,value:{visible:!1,text:"",color:"#9aa4b2",opacity:.08,fontSize:64}}),this._host=e}setBranding(e){null!==this._host._branding&&this._host.removePrimitive(this._host._branding),this._host._branding=null,this._brandingOptions=!1!==e&&{position:"bottom-left",margin:14,opacity:1,padding:8,label:"Chart by OpenAlgo",href:"https://openalgo.in",id:"chart-branding",...!0===e?{}:e},!1!==this._brandingOptions&&("object"==typeof this._brandingOptions.padding&&(this._brandingOptions.padding={...this._brandingOptions.padding}),this._host._branding=new oo(this._brandingOptions),this._host.addPrimitive(this._host._branding,{anchor:"chart-bottom"})),this._host.invalidate(e=>e.invalidateGlobal(i.Light)),this._host._emit("branding:changed",this._host.brandingOptions())}setWatermarkOptions(e){const t="boolean"==typeof e?{visible:e}:e;if(null===t||"object"!=typeof t)return;const i=this._watermarkOptions;"boolean"==typeof t.visible&&(i.visible=t.visible);for(const e of["text","color","font","id"])"string"==typeof t[e]&&(i[e]=t[e]);"number"==typeof t.opacity&&Number.isFinite(t.opacity)&&(i.opacity=Math.max(0,Math.min(1,t.opacity))),"number"==typeof t.fontSize&&Number.isFinite(t.fontSize)&&(i.fontSize=Math.max(10,Math.min(200,t.fontSize))),"bottom"!==t.zOrder&&"normal"!==t.zOrder&&"top"!==t.zOrder||(i.zOrder=t.zOrder),this._syncWatermark(),this._host._layoutChanged("setWatermarkOptions")}_syncWatermark(){const e=this._watermarkOptions;if(!e.visible)return null!==this._watermark&&this._host.removePrimitive(this._watermark),void(this._watermark=null);const t=e.text?.trim()?e.text:[this._host._dataContext?.symbol,this._host._dataContext?.interval].filter(Boolean).join(" ");null===this._watermark?(this._watermark=new da({...e,text:t}),this._host.addPrimitive(this._watermark,{anchor:"primary-pane"})):this._watermark.setOptions({...e,text:t}),this._host.invalidate(e=>e.invalidateGlobal(i.Light))}takeScreenshot(){const e=this._host._pixelRatio(),t=this._host._doc.createElement("canvas");t.width=Math.max(1,Math.round(this._host._width*e)),t.height=Math.max(1,Math.round(this._host._height*e));const i=t.getContext("2d");if(null===i)return t;i.fillStyle=this._host._theme.background,i.fillRect(0,0,t.width,t.height);const s=this._host._paneLayout();for(let t=0;t<this._host._panes.length;t++){if(this._host._layout._layoutWeight(t)<=0)continue;const r=Math.round((s[t]?.top??0)*e);for(const e of[this._host._panes[t].base,this._host._panes[t].top])e.element.width>0&&e.element.height>0&&i.drawImage(e.element,0,r)}return t}exportSVG(e){if(void 0!==e.dpr&&1!==e.dpr)throw new RangeError("exportSVG: dpr must be 1, SVG has no device pixels");const t=Math.max(1,Math.round(e.width??this._host._width)),s=Math.max(1,Math.round(e.height??this._host._height)),r=!1!==e.background,n=new ha(t,s,{background:r?this._host._theme.background:void 0}),o=n.asCanvasContext();this._host._studies._flushIndicators();const a=this._host._width,l=this._host._height,h=this._host._layoutRatio,c=t!==a||s!==l||1!==this._host._layout._ratioForLayout();this._host._layoutRatio=1,c&&(this._host._width=t,this._host._height=s,this._host._layout._relayout(!0));try{r&&"transparent"!==this._host._theme.background&&(n.fillStyle=this._host._theme.background,n.fillRect(0,0,t,s));const e=this._host._paneLayout(),i=this._host._layout._topPaneIndex();for(let s=0;s<this._host._panes.length;s++){if(this._host._layout._layoutWeight(s)<=0)continue;const a=this._host._panes[s],l={...this._host._renderContext(s),dpr:1,hoverId:null,hoverKey:null,dragId:null,paintBackground:r},h=s===i,c=e[s].top+(h?0:1),d=e[s].height-(h?0:1);h||(n.fillStyle=this._host._theme.paneSeparator,n.fillRect(0,e[s].top,t,1)),n.pushGroup({"data-pane":s},{translate:{x:0,y:c},clip:{x:0,y:0,width:t,height:d}}),a.autoscale(l),a.paintBase(l,o),a.paintTop(null,l,o),n.popGroup()}}finally{this._host._layoutRatio=h,c&&(this._host._width=a,this._host._height=l,this._host._layout._relayout(!0),this._host.invalidate(e=>e.invalidateGlobal(i.Full)))}return n.toString()}applyOptions(e){this._host._withinLayoutChange(()=>{e.theme&&this._host.setTheme(e.theme),e.grid&&this._host.setGridOptions(e.grid),e.canvas&&this._host.setCanvasOptions(e.canvas),e.statusLine&&this._host.setStatusLineOptions(e.statusLine),void 0!==e.legendIconSize&&this._host.setLegendIconSize(e.legendIconSize),e.priceScale&&this._host.setPriceScaleOptions(e.priceScale),void 0!==e.priceFormatter&&this._host.setPriceFormatter(e.priceFormatter),"timeFormatter"in e&&this._host.setTimeFormatter(e.timeFormatter),void 0!==e.timezone&&this._host.setTimezone(e.timezone),e.crosshairMode&&(this._host._crosshairMode=e.crosshairMode),"boolean"==typeof e.crosshairSnapToBar&&(this._host._crosshairSnapToBar=e.crosshairSnapToBar,this._host.invalidate(e=>e.invalidateGlobal(i.Cursor)))}),this._host._layoutChanged("applyOptions")}}class _a{constructor(){Object.defineProperty(this,"_listeners",{enumerable:!0,configurable:!0,writable:!0,value:new Map})}on(e,t){let i=this._listeners.get(e);return void 0===i&&(i=new Set,this._listeners.set(e,i)),i.add(t),()=>this.off(e,t)}once(e,t){const i=s=>{this.off(e,i),t(s)};return this.on(e,i)}off(e,t){void 0===t?this._listeners.delete(e):this._listeners.get(e)?.delete(t)}emit(e,t){mn(this._listeners.get(e),t)}has(e){return(this._listeners.get(e)?.size??0)>0}clear(){this._listeners.clear()}}function pa(e){if(!_t(e))throw new Error(`openalgo-charts: unknown IANA time zone "${e}"`);return e}function fa(){return"undefined"!=typeof window&&window.devicePixelRatio?window.devicePixelRatio:1}class ma{constructor(e,t={}){Object.defineProperty(this,"_container",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_doc",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_pixelRatio",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_theme",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_panes",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_primaryPane",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_loop",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_raf",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_rafInjected",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_remeasureHandle",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_dataLayer",{enumerable:!0,configurable:!0,writable:!0,value:new A}),Object.defineProperty(this,"_timeScale",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_timeScaleMutationDepth",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_priceAxisWidth",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_timeAxisHeight",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_pending",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_framing",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_scaleMutationDepth",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_pixels",{enumerable:!0,configurable:!0,writable:!0,value:new Hi(this)}),Object.defineProperty(this,"_layoutRatio",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_width",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_height",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_hasFitContent",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_layoutDepth",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_crosshairMode",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_crosshairSnapToBar",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_priceOnlyAutoScale",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_indicatorLegendCollapsed",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_shortcuts",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_trading",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_tickSchedule",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_input",{enumerable:!0,configurable:!0,writable:!0,value:new $o(this)}),Object.defineProperty(this,"_now",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_conflate",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_conflationFactor",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_backendFactory",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_rendererKind",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_requestedRenderer",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_gridVert",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"_gridHorz",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"_canvas",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"_statusLine",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"_legendIconSize",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_axisChrome",{enumerable:!0,configurable:!0,writable:!0,value:{sessionClock:!1,barCountdown:!1}}),Object.defineProperty(this,"_sessionClockForm",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_wallClock",{enumerable:!0,configurable:!0,writable:!0,value:()=>Date.now()/1e3}),Object.defineProperty(this,"_tradingSettings",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"_events",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_eventMarkers",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_eventPane",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_primitives",{enumerable:!0,configurable:!0,writable:!0,value:new ea(this)}),Object.defineProperty(this,"_navigation",{enumerable:!0,configurable:!0,writable:!0,value:{mousePan:"both",defaultVisibleBars:0,panEnabled:!0,zoomEnabled:!0}}),Object.defineProperty(this,"_liveRegion",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lastDragX",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_lastDragT",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_dragVelocity",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_motion",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_animZoom",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_zoomAnchor",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_doubleClick",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_movablePrimaryPane",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_firstDataId",{enumerable:!0,configurable:!0,writable:!0,value:{value:null}}),Object.defineProperty(this,"_primary",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_seriesRecords",{enumerable:!0,configurable:!0,writable:!0,value:new WeakMap}),Object.defineProperty(this,"_seriesProvenance",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_indicators",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_sourceAbove",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_legendActions",{enumerable:!0,configurable:!0,writable:!0,value:new WeakMap}),Object.defineProperty(this,"_persistence",{enumerable:!0,configurable:!0,writable:!0,value:new po(this)}),Object.defineProperty(this,"_indicatorRanges",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_ownedScaleRanges",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_seriesOwners",{enumerable:!0,configurable:!0,writable:!0,value:new WeakMap}),Object.defineProperty(this,"_series",{enumerable:!0,configurable:!0,writable:!0,value:new Zo(this)}),Object.defineProperty(this,"_dataContext",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_barsProvider",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_barsRequests",{enumerable:!0,configurable:!0,writable:!0,value:new AbortController}),Object.defineProperty(this,"_barsProviderRevision",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_requestedDataRevision",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_studies",{enumerable:!0,configurable:!0,writable:!0,value:new Lo(this,this)}),Object.defineProperty(this,"_indicatorsDirty",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_indicatorRefreshes",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_indicatorReservedIds",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_drawingState",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_alertState",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_layout",{enumerable:!0,configurable:!0,writable:!0,value:new Vo(this)}),Object.defineProperty(this,"_collapsed",{enumerable:!0,configurable:!0,writable:!0,value:new WeakSet}),Object.defineProperty(this,"_legends",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_studyLegends",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_legendStack",{enumerable:!0,configurable:!0,writable:!0,value:new qo(this)}),Object.defineProperty(this,"_firstPane",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_historyLoader",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_loadingHistory",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_priceFormatter",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_priceScaleOptions",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_pricePanes",{enumerable:!0,configurable:!0,writable:!0,value:new WeakSet}),Object.defineProperty(this,"_scales",{enumerable:!0,configurable:!0,writable:!0,value:new Qo(this)}),Object.defineProperty(this,"_timeFormatter",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_timezone",{enumerable:!0,configurable:!0,writable:!0,value:ht}),Object.defineProperty(this,"_leftAxisWidth",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_rightAxisWidth",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_axisColumnWidth",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_emptyPriceAxis",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"_timeNav",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_schedulingTimeNav",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_timeNavPane",{enumerable:!0,configurable:!0,writable:!0,value:-1}),Object.defineProperty(this,"_branding",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_appearance",{enumerable:!0,configurable:!0,writable:!0,value:new ua(this)}),Object.defineProperty(this,"_bus",{enumerable:!0,configurable:!0,writable:!0,value:new _a}),Object.defineProperty(this,"_anchored",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_onPixelRatio",{enumerable:!0,configurable:!0,writable:!0,value:()=>this._pixels._onPixelRatio()}),Object.defineProperty(this,"_checkPixelRatio",{enumerable:!0,configurable:!0,writable:!0,value:()=>this._pixels._checkPixelRatio()}),Object.defineProperty(this,"_onPointerEnter",{enumerable:!0,configurable:!0,writable:!0,value:()=>this._input._onPointerEnter()}),Object.defineProperty(this,"_onContextMenu",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onContextMenu(e)}),Object.defineProperty(this,"_onPointerDown",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onPointerDown(e)}),Object.defineProperty(this,"_onPointerMove",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onPointerMove(e)}),Object.defineProperty(this,"_onPointerUp",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onPointerUp(e)}),Object.defineProperty(this,"_onPointerUpNative",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onPointerUpNative(e)}),Object.defineProperty(this,"_onPointerCancel",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onPointerCancel(e)}),Object.defineProperty(this,"_onLostPointerCapture",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onLostPointerCapture(e)}),Object.defineProperty(this,"_onPointerLeave",{enumerable:!0,configurable:!0,writable:!0,value:()=>this._input._onPointerLeave()}),Object.defineProperty(this,"_onWheel",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onWheel(e)}),Object.defineProperty(this,"_onDblClick",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onDblClick(e)}),Object.defineProperty(this,"_onKeyDown",{enumerable:!0,configurable:!0,writable:!0,value:e=>this._input._onKeyDown(e)}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_destroying",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._timeScale=new x(t.timeScale),this._container=e,this._doc=t.document??e.ownerDocument,this._pixelRatio=t.pixelRatio??fa,this._theme=t.theme??Yi,this._priceAxisWidth=t.priceAxisWidth??56,this._rightAxisWidth=this._priceAxisWidth,void 0!==t.legendOffset?.top&&(this._legendStack._legendOffset.top=t.legendOffset.top),void 0!==t.legendOffset?.left&&(this._legendStack._legendOffset.left=t.legendOffset.left),this._timeAxisHeight=t.timeAxisHeight??22,this._crosshairMode=t.crosshairMode??"normal",this._crosshairSnapToBar=!0===t.crosshairSnapToBar,this._priceOnlyAutoScale=!0===t.priceOnlyAutoScale,this._indicatorLegendCollapsed=!0===t.indicatorLegendCollapsed,Object.assign(this._axisChrome,t.axisChrome),"object"==typeof t.axisChrome?.sessionClock&&(this._sessionClockForm={...t.axisChrome.sessionClock}),void 0!==t.axisChrome?.clock&&(this._wallClock=t.axisChrome.clock);const s=t.shortcuts;this._shortcuts=!1===s?null:s instanceof Nn?s:new Nn(s??{}),this._now=t.now??(()=>"undefined"!=typeof performance?performance.now():0),this._animZoom=t.animZoom??!0,this._barsProvider=t.barsProvider??null,this._motion=new Vi(this,t.animAutoscale??this._animZoom),this._zoomAnchor=t.zoomAnchor??"cursor",this._doubleClick=t.doubleClick??"reset",this._movablePrimaryPane=!0===t.movablePrimaryPane,this._conflate=t.conflate??!0,this._conflationFactor=t.conflationFactor??1,this._backendFactory=t.renderBackend??ji(t.renderer??"canvas2d"),this._requestedRenderer=void 0===t.renderBackend?t.renderer??"canvas2d":null,this._priceFormatter=t.priceFormatter??null,this._priceScaleOptions=t.priceScale??null,this._timeFormatter=t.timeFormatter,void 0!==t.timezone&&(this._timezone=pa(t.timezone)),this._gridVert=t.grid?.vertLines??!0,this._gridHorz=t.grid?.horzLines??!0,Object.assign(this._canvas,t.canvas),t.grid&&(this._canvas.grid={...this._canvas.grid,...t.grid}),Object.assign(this._statusLine,t.statusLine),"number"==typeof t.legendIconSize&&Number.isFinite(t.legendIconSize)&&(this._legendIconSize=t.legendIconSize);const o=q(this._canvas.margins);void 0===o.marginTop&&void 0===o.marginBottom||(this._priceScaleOptions={...this._priceScaleOptions,...o}),this._input._patchNavigation(t.navigation??{});const a=t.timeNavigator??!0;if(!1!==a){const e=!0===a?void 0:a;this._timeNav=new io({...e,hints:this._navHints(e)},this._now),this._input._timeNavButtons=[...this._timeNav.options().buttons],this._input._syncNavigatorPolicy(),void 0===e?.hints&&null!==this._shortcuts&&this.on("destroy",this._shortcuts.onChange(()=>this._timeNav?.setOptions({hints:this._navHints()})))}const l="function"==typeof getComputedStyle?getComputedStyle(e).position:e.style.position;l&&"static"!==l||(e.style.position="relative"),e.style.display="flex",e.style.flexDirection="column",e.style.background=this._theme.background,e.style.touchAction="none",e.getAttribute("role")||e.setAttribute("role","application"),e.setAttribute("aria-label",t.ariaLabel??"Interactive financial chart"),e.hasAttribute("tabindex")||(e.tabIndex=0);const h=this._doc.createElement("div");h.setAttribute("aria-live","polite");const c=h.style;var d;c.position="absolute",c.width="1px",c.height="1px",c.overflow="hidden",c.clip="rect(0 0 0 0)",c.whiteSpace="nowrap",c.border="0",c.padding="0",c.margin="-1px",e.appendChild(h),this._liveRegion=h,this._raf=(d=t.raf)?{schedule:d.schedule,cancel:d.cancel??r}:"function"==typeof requestAnimationFrame?{schedule:e=>requestAnimationFrame(e),cancel:e=>cancelAnimationFrame(e)}:{schedule:e=>setTimeout(e,16),cancel:e=>clearTimeout(e)},this._rafInjected=void 0!==t.raf,this._loop=new n(()=>this._onFrame(),this._raf.schedule,this._raf.cancel),this._layout._addPane(),this.setBranding(t.branding??!0),this.setWatermarkOptions(t.watermark??!1),this._pixels._observeSize(),this._pixels._watchPixelRatio(),this._input._attachInput(),this._timeScale.setChangeHandler(e=>{if(this._timeScaleMutationDepth>0||this._destroyed)return;this._motion._stopNavigationMotion();const t=this._timeScale.visibleRange();t.from===e.from&&t.to===e.to||(this.invalidate(e=>e.invalidateGlobal(i.Full)),this._emitViewportIfMoved(e))}),this.applySize(e.clientWidth,e.clientHeight),this._remeasureHandle=this._raf.schedule(()=>{this._remeasureHandle=null,this._layout._remeasure()}),"function"==typeof queueMicrotask&&queueMicrotask(()=>this._emit("ready",{}))}setHistoryLoader(e){this._historyLoader=e}historyLoadComplete(){this._loadingHistory=!1}get dataLayer(){return this._dataLayer}setSessionCalendar(e){this._destroyed||(this._dataLayer.setSessionCalendar(e),this.invalidate(e=>e.invalidateGlobal(i.Full)))}primaryBars(){const e=this._firstDataId.value;return null===e?[]:this._dataLayer.seriesBars(e)}get timeScale(){return this._timeScale}setVisibleLogicalRange(e){this._motion._stopNavigationMotion(),this._timeScale.setVisibleLogicalRange(e)}getVisibleLogicalRange(){return this._timeScale.visibleRange()}fitContent(){this._motion._stopNavigationMotion(),this._dataLayer.length<=0||this._timeScale.fitContent(this._dataLayer.length)}navigationOptions(){return{...this._navigation}}setNavigationOptions(e){const t=this._navigation.defaultVisibleBars,i=this._navigation.defaultBarSpacing,s=this._navigation.panEnabled,r=this._navigation.zoomEnabled;this._input._patchNavigation(e),t===this._navigation.defaultVisibleBars&&i===this._navigation.defaultBarSpacing||this.resetScale(),s===this._navigation.panEnabled&&r===this._navigation.zoomEnabled||this._emit("objects:change",{}),this._layoutChanged("setNavigationOptions")}_fitDefaultView(){const e=this._dataLayer.length;return!(e<=0)&&this._timeScale.width>0&&(this._mutateTimeScale(()=>{if(this._timeScale.fitContent(e),void 0!==this._navigation.defaultBarSpacing)this._timeScale.setBarSpacing(this._navigation.defaultBarSpacing);else if(this._navigation.defaultVisibleBars>0){const t=Math.min(e,this._navigation.defaultVisibleBars);this._timeScale.setBarSpacing(this._timeScale.width/(t+this._timeScale.rightOffset))}}),!0)}get shortcuts(){return this._shortcuts}get trading(){return null===this._trading&&(this._trading=new Vn(this),this._trading.setSettings(this._tradingSettings)),this._trading}hasTrading(){return null!==this._trading}tradingSettings(){if(null!==this._trading)return this._trading.getSettings();const e={...zn};for(const[t,i]of Object.entries(this._tradingSettings))"string"==typeof i&&(e[t.slice(0,-5)]=i);return e}setTradingSettings(e){Object.assign(this._tradingSettings,e),this._trading?.setSettings(e),this._layoutChanged("setTradingSettings")}_layoutChanged(e){this._layoutDepth>0||this._destroyed||this._emit("layout:change",{setter:e})}_withinLayoutChange(e){this._layoutDepth++;try{return e()}finally{this._layoutDepth--}}addSeries(e,t={}){return this._series._createSeries(e,t,!0)}seriesType(e){const t=this._seriesRecords.get(e),i=this._seriesOwners.get(e);return!this._destroyed&&t&&i&&this._panes.includes(i.pane)&&i.pane.series().includes(t)?t.type:null}seriesStyle(e){return null===this.seriesType(e)?null:Object.freeze({...this._seriesRecords.get(e).style})}setSeriesPriceScale(e,t){if(!L(t)||null===this.seriesType(e))return!1;const s=this._seriesRecords.get(e),r=this._seriesOwners.get(e);if(r.indicatorOwned||s.scaleId===t)return!1;const n=r.pane.scaleFor(t);return s.scaleId=t,this._studies._reconcileIndicatorRanges(),this._series._applySeriesPriceFormat(n,r.priceFormat),void 0!==s.style.precision&&this._series._applyPrecision(n,s.style.precision),this._layout._recomputeAxisColumns(),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._emit("objects:change",{}),!0}setSeriesType(e,t){return this._series._setSeriesType(e,t,!0)}setSeriesTransform(e,t){return this._series._setSeriesTransform(e,t,!0)}seriesTransform(e){return this._series._seriesTransform(e)}primarySeries(){return this._primary?.api??null}primarySeriesInfo(){const e=this._primary;return null===e?null:{type:e.record.type,style:e.record.style}}addPriceLine(e,t){const i=new Fn(e);return this._primitives._addPrimitive(t??this._primaryIndex(),i),i}addEventMarkers(e,t={}){const i=new Qn(t);return this._primitives._addPrimitive(e??this._primaryIndex(),i),i}setEvents(e,t){this._primitives.setEvents(e,t)}eventMarkers(){return this._eventMarkers}setEventMarkerOptions(e){this._primitives._ensureEventMarkers().setOptions(e)}setEventGroups(e){this._primitives._ensureEventMarkers().setGroups(e),this._emit("events:change",void 0)}setEventGroupVisible(e,t){this._primitives._ensureEventMarkers().setGroupVisible(e,t),this._emit("events:change",void 0)}setEventOptions(e){Object.assign(this._primitives._eventVisible,e),this._primitives._syncEvents(),this._layoutChanged("setEventOptions")}eventOptions(){return{...this._primitives._eventVisible}}addIndicator(e,t={},i={}){return this._studies.addIndicator(e,t,i)}indicators(){return this._studies.indicators()}moveIndicator(e,t,i={}){return this._studies.moveIndicator(e,t,i)}reorderIndicator(e,t,i={}){return this._studies.reorderIndicator(e,t,i)}_policyAllows(e,t,i){return!0===i.force||!1!==e.policy()[t]}seriesStack(e=this._primaryIndex()){return this._primitives.seriesStack(e)}moveInSeriesStack(e,t,i,s={}){return this._primitives.moveInSeriesStack(e,t,i,s)}setPrimitiveStackAbove(e,t){return this._primitives.setPrimitiveStackAbove(e,t)}removeIndicator(e,t={}){return this._studies.removeIndicator(e,t)}getDataContext(){return this._dataContext}get hasOpenInterest(){return this._dataContext?.hasOpenInterest}setDataContext(e){const t=!function(e,t){if(e===t)return!0;const i=e,s=t;for(const e of new Set([...Object.keys(i??{}),...Object.keys(s??{})]))if(i?.[e]!==s?.[e])return!1;return!0}(this._dataContext?.variant,e?.variant);if(this._dataContext?.symbol===e?.symbol&&this._dataContext?.exchange===e?.exchange&&this._dataContext?.hasOpenInterest===e?.hasOpenInterest&&this._dataContext?.interval===e?.interval&&!t&&!!this._dataContext==!!e)return;const i=this._dataContext?.symbol!==e?.symbol||this._dataContext?.exchange!==e?.exchange,s=i||t||this._dataContext?.interval!==e?.interval;if(this._dataContext=e?Object.freeze({...e}):void 0,s&&this._cancelBarsRequests(),s)for(const e of this._seriesProvenance.values())e.contextChanged();this._indicators.length>0&&(this._indicatorsDirty=!0,this._loop.requestFrame()),i&&this._events.length&&(this._events=[],this._primitives._syncEvents());for(const e of this._legends)e.legend.setOptions({hasOpenInterest:this.hasOpenInterest});this._appearance._syncWatermark(),this._emit("data:context",this._dataContext)}setBarsProvider(e){this._destroyed||this._destroying||e===this._barsProvider||(this._barsProvider=e,this._barsProviderRevision++,this._cancelBarsRequests(),this._emit("data:requests",{}))}_cancelBarsRequests(){if(this._destroying)return;const e=this._barsRequests;this._barsRequests=new AbortController,e.abort()}invalidateRequestedData(){this._destroyed||this._destroying||(this._requestedDataRevision++,this._emit("data:requests",{}))}hasSnapshotProvider(){return"object"==typeof this._barsProvider&&null!==this._barsProvider&&"function"==typeof this._barsProvider.requestSnapshot}hasBarsProvider(){return null!==this._barsProvider}setBranding(e){this._appearance.setBranding(e)}brandingOptions(){const e=this._appearance._brandingOptions;return!1!==e&&{...e,..."object"==typeof e.padding?{padding:{...e.padding}}:{}}}setWatermarkOptions(e){this._appearance.setWatermarkOptions(e)}watermarkOptions(){return{...this._appearance._watermarkOptions}}_indicatorHost(e){return this._studies._indicatorHost(e)}subscribeClick(e){return bn(this._input._clickCbs,e)}subscribeCrosshairMove(e){return bn(this._input._crosshairCbs,e)}setLinkedCrosshairIndex(e){if(this.isDestroyed||null!==this._cursor)return;const t=null===e?null:this._dataLayer.indexToTime(e)??null;if(t===this._input._readoutTime)return;this._input._readoutTime=t;for(const t of this._indicators)t.updateLegendValues(e??void 0);const i={source:"linked",time:t,index:e,bar:null===e||null===this._firstDataId.value?null:j(this._dataLayer,this._firstDataId.value,e)??null,price:null,point:null,paneIndex:null};mn(this._input._crosshairCbs,i),this._emit("crosshair:readout",i)}_readoutIndex(){return null===this._input._readoutTime?void 0:this._dataLayer.timeToIndex(this._input._readoutTime)}subscribeDrag(e,t){return bn(this._input._dragCbs,i=>{i.end?t?.(i.id,i.price,i.time):e(i.id,i.price,i.time)})}_ensureScaled(e){const t=this._panes[e];void 0===t||t.readoutScale().scaled||t.autoscale(this._renderContext(e))}_xToTime(e){return this._dataLayer.indexToTimeFloat(this._timeScale.xToIndex(e-this._leftAxisWidth))}timeToCoordinate(e){return this._timeScale.indexToX(this._dataLayer.timeToIndexFloat(e))+this._leftAxisWidth}coordinateToTime(e){return this._xToTime(e)}on(e,t){return this._bus.on(e,t)}once(e,t){return this._bus.once(e,t)}off(e,t){this._bus.off(e,t)}emit(e,t){this._bus.emit(e,t)}_emit(e,t){this._bus.emit(e,t)}_mutateTimeScale(e){this._timeScaleMutationDepth++;try{return e()}finally{this._timeScaleMutationDepth--}}_emitViewport(e){if(!this._bus.has(e))return;const t=this._timeScale.visibleRange();this._emit(e,{from:this._dataLayer.indexToTime(Math.round(t.from))??null,to:this._dataLayer.indexToTime(Math.round(t.to))??null,logicalFrom:t.from,logicalTo:t.to})}_emitViewportIfMoved(e){const t=this._timeScale.visibleRange();if(t.from===e.from&&t.to===e.to)return;const i=Math.abs(t.to-t.from-(e.to-e.from))>1e-9;this._emitViewport(i?"zoom":"pan")}addPrimitive(e,t){void 0!==t&&"number"!=typeof t?(this._anchored.push({primitive:e,anchor:t.anchor}),this._primitives._addPrimitive(this._primitives._anchorTarget(t.anchor),e)):this._primitives._addPrimitive("number"==typeof t?t:this._primaryIndex(),e)}priceToCoordinate(e,t=this._primaryIndex()){const i=this._layout._mappedPane(t);return i&&this._paneLayout()[t].top+i.priceToY(e)}coordinateToPrice(e,t=this._primaryIndex()){const i=this._layout._mappedPane(t);return i&&i.yToPrice(e-this._paneLayout()[t].top)}plotRect(e){const t=this._destroyed||this._layout._collapsedShown(e)?void 0:this._paneLayout()[e];if(!t||!Number.isSafeInteger(e))return null;this._ensureScaled(e);const i=this._width-this._leftAxisWidth-this._rightAxisWidth,s=t.height-(e===this._bottomPaneIndex()?this._timeAxisHeight:0);return i>0&&s>0?{left:this._leftAxisWidth,top:t.top,width:i,height:s}:null}setGridOptions(e){void 0!==e.vertLines&&(this._gridVert=e.vertLines),void 0!==e.horzLines&&(this._gridHorz=e.horzLines),this._canvas.grid={...this._canvas.grid,...e},this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setGridOptions")}gridOptions(){return{...this._canvas.grid,vertLines:this._gridVert,horzLines:this._gridHorz}}setCanvasOptions(e){this._withinLayoutChange(()=>{e.grid&&this.setGridOptions(e.grid),e.crosshair&&(this._canvas.crosshair={...this._canvas.crosshair,...e.crosshair}),e.scales&&(this._canvas.scales={...this._canvas.scales,...e.scales}),e.margins&&(this._canvas.margins={...this._canvas.margins,...e.margins},this.setPriceScaleOptions(q(this._canvas.margins),"axes"))}),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setCanvasOptions")}canvasOptions(){return{...this._canvas}}setPriceScaleOptions(e,t="primary"){this._priceScaleOptions={...this._priceScaleOptions,...e};for(const i of this._panes){const s="all"===t?i.scales():"axes"===t?i.axisScales():[i.priceScale],r=this._scales._scalePatchFor(i,e);for(const e of s)e.setOptions(r)}this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setPriceScaleOptions")}priceScaleDefaults(){return{...this._priceScaleOptions}}priceScaleOptions(){return{...this._primaryPane.priceScale.options}}setAutoScale(e){for(const t of this._panes)for(const{scaleId:i}of t.visibleAxes(this._emptyPriceAxis))e&&t.setRatioLock(i,!1,0,0),t.scaleFor(i).setAutoScale(e);this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setAutoScale")}priceOnlyAutoScale(){return this._priceOnlyAutoScale}setPriceOnlyAutoScale(e){"boolean"==typeof e&&e!==this._priceOnlyAutoScale&&(this._priceOnlyAutoScale=e,this.invalidate(e=>e.invalidateGlobal(i.Full)),this._emit("objects:change",{}))}priceAxisState(e=this._primaryIndex(),t="right"){return this._scales.priceAxisState(e,t)}priceAxisPlacement(e,t){return L(t)?this._priceAxisPane(e)?.axisPlacement(t)??null:null}setPriceAxisPlacement(e,t,s,r){const n=this._priceAxisPane(e);return!!(n&&L(t)&&n.setAxisPlacement(t,s,r))&&(this._layout._recomputeAxisColumns(),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._emit("priceAxisPlacementChanged",{paneIndex:e,scaleId:t,...n.axisPlacement(t)}),this._emit("objects:change",{}),!0)}priceAxisLayout(e=this._primaryIndex()){return this._priceAxisPane(e)?.axisSlots(this._renderContext(e)).map(e=>({...e,x:e.x+this._leftAxisWidth}))??[]}_priceAxisPane(e){return!this._destroyed&&Number.isInteger(e)&&e>=0?this._panes[e]:void 0}setPriceAxisOptions(e,t,s){const r=this._panes[e];void 0!==r&&L(t)&&(r.scaleFor(t).setOptions(s),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setPriceAxisOptions"))}setPriceAxisAutoFit(e,t,s){const r=this._panes[e];void 0!==r&&L(t)&&(s&&r.setRatioLock(t,!1,0,0),r.scaleFor(t).setAutoScale(s),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setPriceAxisAutoFit"))}setPriceAxisLockRatio(e,t,s){const r=this._panes[e];if(void 0===r||!L(t))return!1;s&&this._scales._ensureScaledFor(e,t);const n=r.setRatioLock(t,s,this._timeScale.barSpacing,r.scaleFor(t).height);return this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setPriceAxisLockRatio"),n}movePriceAxis(e,t,s){const r=this._panes[e];if(!this._scales._canMovePriceAxis(e,t,s)||!r.moveSeriesScale(t,s))return!1;for(const e of this._indicatorRanges.values())e.pane===r&&e.scaleId===t&&(e.scaleId=s);for(const t of this._indicators){if(t.paneIndex!==e)continue;const i=t.renderResources().series.filter(e=>!e.overlay),n=t.renderResources().primitives.filter(e=>!e.overlay&&null!==r.primitiveScaleId(e.primitive));(i.length?i.every(e=>this._seriesRecords.get(e.api)?.scaleId===s):n.length&&n.every(e=>r.primitiveScaleId(e.primitive)===s))&&t.adoptPriceScale(s)}const n=r.scaleFor(t);return this._priceScaleOptions&&n.setOptions(this._scales._scalePatchFor(r,this._priceScaleOptions)),n.setPriceFormatter(this._priceFormatter),this._layout._recomputeAxisColumns(),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._emit("priceAxisMoved",{paneIndex:e,from:t,to:s}),!0}setStatusLineOptions(e){Object.assign(this._statusLine,e);for(const e of this._legends)e.legend.setOptions({statusLine:this._statusLine});this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setStatusLineOptions")}statusLineOptions(){return{...this._statusLine}}indicatorLegendCollapsed(){return this._indicatorLegendCollapsed}setIndicatorLegendCollapsed(e){"boolean"==typeof e&&e!==this._indicatorLegendCollapsed&&(this._indicatorLegendCollapsed=e,this._legendStack._restackLegends(),this.invalidate(e=>e.invalidateGlobal(i.Cursor)),this._emit("objects:change",{}))}setLegendIconSize(e){if(Number.isFinite(e)){this._legendIconSize=e;for(const t of this._legends)t.legend.setOptions({iconSize:e});this._layout._relayout(),this.invalidate(e=>e.invalidateGlobal(i.Full))}}legendIconSize(){return this._legendIconSize}setAxisChromeOptions(e){void 0!==e.sessionClock&&("object"==typeof e.sessionClock&&(this._sessionClockForm={...e.sessionClock}),this._axisChrome.sessionClock=!0===e.sessionClock?this._sessionClockForm??!0:e.sessionClock),void 0!==e.barCountdown&&(this._axisChrome.barCountdown=e.barCountdown),void 0!==e.clock&&(this._axisChrome.clock=e.clock,this._wallClock=e.clock),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setAxisChromeOptions")}axisChromeOptions(){return{...this._axisChrome}}crosshairMode(){return this._crosshairMode}crosshairSnapToBar(){return this._crosshairSnapToBar}movablePrimaryPane(){return this._movablePrimaryPane}theme(){return this._theme}takeScreenshot(){return this._appearance.takeScreenshot()}exportSVG(e={}){return this._appearance.exportSVG(e)}removePrimitive(e){this._primitives.removePrimitive(e)}tradeHost(e){return{addPrimitive:t=>this._primitives._addPrimitive(e??this._primaryIndex(),t),removePrimitive:e=>this.removePrimitive(e)}}get rendererKind(){return this._rendererKind??"canvas2d"}get renderer(){return this.rendererKind}_newBackend(){const e=this._backendFactory()??ji("canvas2d")();if(null===e)throw new Error("openalgo-charts: the canvas2d render backend factory returned null");return null===this._rendererKind&&(this._rendererKind=e.kind,null!==this._requestedRenderer&&"auto"!==this._requestedRenderer&&e.kind!==this._requestedRenderer&&console.warn(`openalgo-charts: render backend "${this._requestedRenderer}" is unavailable on this device; using "${e.kind}"`)),e}_fallbackToCanvas2d(e){const t=this.rendererKind;this._backendFactory=ji("canvas2d");for(const e of this._panes)e.setBackend(this._newBackend());this._rendererKind="canvas2d";const s={from:t,to:"canvas2d",reason:e};this._emit("renderer:fallback",s),this.invalidate(e=>e.invalidateGlobal(i.Full))}setPriceFormatter(e){this._priceFormatter=e;for(const t of this._panes)t.priceScale.setPriceFormatter(e);for(const e of this._panes)for(const t of e.series())void 0!==t.style.precision&&this._series._applyPrecision(e.scaleOf(t),t.style.precision);this.invalidate(e=>e.invalidateGlobal(i.Full))}setTimeFormatter(e){this._timeFormatter=e,this.invalidate(e=>e.invalidateGlobal(i.Full))}timezone(){return this._timezone}setTimezone(e){const t=pa(e);t!==this._timezone&&(this._timezone=t,this._studies._invalidateIndicators(),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._emit("timezone:changed",{timezone:t}))}setPlacementMode(e){this._input._placementMode=e,e&&Un(this)}beginPick(e,t,i={}){if(this._destroyed||this._destroying)throw new Error("Cannot pick on a destroyed chart");if(this._input._placementMode)throw new Error("Finish drawing placement before picking a study value");if(!ts(i)||!is(i))throw new TypeError("Invalid pick options");const s=Object.getOwnPropertyDescriptors(i),r=s.paneIndex?.value,n=s.priceScaleId?.value;if(void 0!==r&&(!Number.isSafeInteger(r)||r<0||!this._panes[r]))throw new RangeError("Invalid pick pane");const o=r??(void 0!==n?this._layout._firstPaneSlot():void 0);if(!(void 0===n||"time"!==e&&L(n)&&Object.prototype.hasOwnProperty.call(this._panes[o]?.scaleStates()??{},n)))throw new RangeError("Invalid pick scale");return Yn(this,e,t,t=>{const i=t,s=i?.point,r=i?.paneIndex;if(void 0===r||!Number.isSafeInteger(r)||!s||!Number.isFinite(s.x)||!Number.isFinite(s.y)||i.viaDrag||void 0!==i.id&&null!==i.id||void 0!==o&&r!==o)return null;const a=this._panes[r],l=(this._paneLayout()[r]?.height??0)-(r===this._bottomPaneIndex()?this._timeAxisHeight:0);if(!a||s.x<this._leftAxisWidth||s.x>=this._width-this._rightAxisWidth||s.y<0||s.y>=l)return null;if("time"===e)return i.time??null;this._ensureScaled(r);const h=void 0===n?i.price??null:Object.prototype.hasOwnProperty.call(a.scaleStates(),n)?a.scaleFor(n).yToPrice(s.y):null;return"price"===e||null===h?h:{time:i.time??Number.NaN,price:h}})}setTheme(e){this._theme=e,this._container.style.background=e.background,this._layout._syncSeparators(),this.invalidate(e=>e.invalidateGlobal(i.Full))}applyOptions(e){this._appearance.applyOptions(e)}panes(){return this._panes}getState(){return this._persistence.getState()}restoreState(e,t={}){return this._persistence.restoreState(e,t)}drawingState(){return this._drawingState}setDrawingState(e){this._drawingState=e,this._emit("objects:change",{})}snapPrice(e,t){if(!Number.isFinite(t))return t;if(null!==this._tickSchedule&&e===this._primaryIndex())return this._tickSchedule.round(t);const i=this._panes[e]?.priceScale.options.minMove??0;return i>0?l(t,i):t}tickSchedule(){return this._tickSchedule}setTickSchedule(e){const t=e;if(null!=t&&("function"!=typeof t.round||"function"!=typeof t.step))throw new TypeError("chart.setTickSchedule takes a schedule built with new TickSchedule(bands), or null");this._tickSchedule=e??null,this._trading?.setTickSchedule(this._tickSchedule)}alertState(){return void 0===this._alertState?void 0:fn(this._alertState)}setAlertState(e){if(void 0!==e){if(1!==e.version)throw new Error("Unsupported alert document");for(const t of e.alerts)cn(t)}this._alertState=void 0===e?void 0:{version:1,alerts:e.alerts.map(dn)},this._reserveAlertStudyIds(this._alertState,this._indicatorReservedIds)}_reserveAlertStudyIds(e,t){for(const{source:i}of e?.alerts??[])"indicator"===i.kind?t.add(i.instanceId):"drawing"===i.kind&&i.input&&t.add(i.input.instanceId)}invalidate(e){null===this._pending&&(this._pending=new s),e(this._pending),0!==this._scaleMutationDepth||this._framing||this._loop.requestFrame()}applySize(e,t){e===this._width&&t===this._height||(this._width=e,this._height=t,this._layout._relayout(),this._hasFitContent||(this._hasFitContent=this._fitDefaultView()),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._emit("resize",{width:e,height:t}))}_bottomPaneIndex(e=!1){return this._layout._bottomPaneIndex(e)}setPaneWeight(e,t){const s=this._panes[e];void 0!==s&&Number.isFinite(t)&&(s.weight=Math.max(.05,t),this._layout._relayout(),this.invalidate(e=>e.invalidateGlobal(i.Full)),this._layoutChanged("setPaneWeight"))}paneWeight(e){return this._panes[e]?.weight??0}removePane(e,t={}){return this._layout.removePane(e,t)}movePane(e,t){return this._layout.movePane(e,t)}primaryPaneIndex(){return this._primaryIndex()}setPrimaryPaneIndex(e){return this._layout.setPrimaryPaneIndex(e)}_primaryIndex(){return Math.max(0,this._panes.indexOf(this._primaryPane))}maximizePane(e){return this._layout.maximizePane(e)}maximizedPane(){return this._layout._maximizedPane}setPaneCollapsed(e,t){return this._layout.setPaneCollapsed(e,t)}paneCollapsed(e){return this._collapsed.has(this._panes[e])}_handleLegendAction(e){return this._legendStack._handleLegendAction(e)}_paneLayout(){return this._layout._paneLayout()}_navHints(e){if(void 0!==e?.hints)return e.hints;if(null===this._shortcuts)return{};const t={};for(const e of this._shortcuts.list()){if("zoomIn"!==e.command&&"zoomOut"!==e.command&&"resetScale"!==e.command)continue;const i=e.combos[0];void 0!==i&&(t[e.command]=ga(i))}return t}_syncTimeNavPane(){const e=this._timeNav;if(null===e)return;const t=this._bottomPaneIndex(!0),i=this._panes.findIndex(t=>t.hasPrimitive(e));this._timeNavPane=t,t===i||t<0||(this._panes[i]?.removePrimitive(e),this._primitives._addPrimitive(t,e))}_feedTimeNav(e){const t=this._timeNav;null!==t&&(t.setPointer(e),t.animating()&&this.invalidate(e=>e.invalidateGlobal(i.Light)))}_renderContext(e){return{timeScale:this._timeScale,dataLayer:this._dataLayer,priceOnlyAutoScale:this._priceOnlyAutoScale,primaryDataId:this._primary?.record.dataId,dpr:this._pixelRatio(),priceAxisWidth:this._rightAxisWidth,axisColumnWidth:this._axisColumnWidth,emptyPriceAxis:this._emptyPriceAxis,timeAxisHeight:this._timeAxisHeight,showTimeAxis:e===this._bottomPaneIndex(),collapsed:this._layout._collapsedShown(e),conflate:this._conflate,conflationFactor:this._conflationFactor,theme:this._theme,showVertGrid:this._gridVert,showHorzGrid:this._gridHorz,canvasOptions:this._canvas,timeFormatter:this._timeFormatter,timezone:this._timezone,leftAxisWidth:this._leftAxisWidth,hoverId:this._input._hoverId,hoverKey:this._input._hoverKey,dragId:this._input._dragId,sessionClock:this._sessionClockOptions(),barCountdown:this._barCountdownOptions(),stackSlot:t=>this._primitives._stackSlot(e,t)}}_sessionClockOptions(){const e=this._axisChrome.sessionClock;if(void 0!==e&&!1!==e)return{visible:!0,now:this._wallClock,timezone:this._timezone,showOffset:!0===e?void 0:e.showOffset}}_barCountdownOptions(){if(!0!==this._axisChrome.barCountdown)return;const e=this._series._underlying(this._firstDataId.value)?.bars,t=void 0===e?this._dataLayer:{baseIndex:e.length-1,indexToTime:t=>e[t]?.time},i=t.indexToTime(t.baseIndex);return void 0!==i?{visible:!0,now:this._wallClock,lastBarTime:i,intervalSec:ii(t)}:void 0}_paintNow(){this._rafInjected||this._destroyed||this._destroying||null===this._pending||this._scaleMutationDepth>0||(this._loop.stop(),this._onFrame())}_onFrame(){if(this._destroyed||this._destroying)return;this._framing=!0;let e=!1;try{e=this._motion._step(),this._studies._flushIndicators()}finally{this._framing=!1}const t=this._pending;this._pending=null;try{null===t||t.isEmpty()||this._paintFrame(t)}finally{e&&!this._destroyed&&this._loop.requestFrame()}}_paintFrame(e){const t=e.globalLevel;let s=!1;const r=this._motion._autoscaleFraction();let n=this._cursor?.x??0;const o=this._firstDataId.value??this._primaryPane.series()[0]?.dataId;if(null!==this._cursor&&this._crosshairSnapToBar&&void 0!==o){const e=Math.round(this._timeScale.xToIndex(n));void 0!==j(this._dataLayer,o,e)&&(n=this._timeScale.indexToX(e))}for(let o=0;o<this._panes.length;o++){const a=this._panes[o],l=e.paneInvalidation(o),h=Math.max(t,l?.level??i.None),c=this._renderContext(o);if((h>=i.Full||l?.autoScale||null!==this._motion._autoscaleTime)&&(s=a.autoscale(c,r)||s),h>=i.Light&&a.paintBase(c),h>=i.Cursor&&!this._input._overlayFrozen){const e=null===this._cursor?null:{x:n,yLocal:o===this._input._cursorPane?this._cursor.y:null,showTimeTag:c.showTimeAxis};a.paintTop(e,c)}}s?this.invalidate(e=>e.invalidateGlobal(i.Light)):this._motion._autoscaleTime=null;const a=this._timeNav;if(a?.animating()&&!this._schedulingTimeNav&&(!this._input._overlayFrozen||"top"!==a.zOrder())&&this._panes[this._timeNavPane]?.primitives().includes(a)){this._schedulingTimeNav=!0;try{this.invalidate(e=>e.invalidateGlobal(i.Light))}finally{this._schedulingTimeNav=!1}}if(null!==this._rendererKind&&"canvas2d"!==this._rendererKind)for(const e of this._panes){const t=e.backendDegradation;if(null!==t){this._fallbackToCanvas2d(t);break}}}_runShortcut(e){return this._input._runShortcut(e)}get _cursor(){return this._input._cursor}resetScale(){this._motion._stopNavigationMotion();const e=this._timeScale.visibleRange();this._hasFitContent=this._fitDefaultView();for(const e of this._panes){e.clearRatioLocks();for(const t of e.scales())t.setAutoScale(!0)}this.invalidate(e=>e.invalidateGlobal(i.Full)),this._emitViewportIfMoved(e)}downloadScreenshot(e="chart.png"){try{const t=this.takeScreenshot(),i=this._doc.createElement("a");i.href=t.toDataURL("image/png"),i.download=e,i.click()}catch{}}_updateAccessibleSummary(){if(null===this._liveRegion)return;const e=this._dataLayer.length;let t=`${e} bar${1===e?"":"s"}`;if(null!==this._firstDataId.value&&this._panes.includes(this._primaryPane)){const e=this._dataLayer.lastIndexedBar(this._firstDataId.value);null!==e&&(t+=`, latest price ${this._primaryPane.priceScale.format(e.bar.close)}`)}this._liveRegion.textContent=`Financial chart, ${t}`}_maybeLoadHistory(){if(null===this._historyLoader||this._loadingHistory)return;const e=this._timeScale.visibleRange();e.from<10&&(this._loadingHistory=!0,this._emit("lazy-load",{from:this._dataLayer.indexToTime(Math.round(e.from))??null,to:this._dataLayer.indexToTime(Math.round(e.to))??null,direction:"backward"}),this._historyLoader())}_startKinetic(e){this._motion._startKinetic(e)}get isDestroyed(){return this._destroyed}destroy(){if(!this._destroyed&&!this._destroying){this._destroying=!0,this._barsRequests.abort(),this._timeScale.setChangeHandler(null),this._loop.stop(),null!==this._remeasureHandle&&(this._raf.cancel(this._remeasureHandle),this._remeasureHandle=null),this._motion._stopNavigationMotion();for(const e of this._indicators.splice(0))e.remove({force:!0});this._pixels._resizeObserver?.disconnect(),this._pixels._resizeObserver=null,this._pixels._deviceObserver?.disconnect(),this._pixels._deviceObserver=null,this._pixels._unwatchPixelRatio(),this._input._detachInput(),this._liveRegion?.remove(),this._liveRegion=null,this._container.style.cursor="",this._input._pointers.clear();for(const e of this._panes)e.destroy();this._panes.length=0,this._seriesProvenance.clear(),this._destroyed=!0,this._emit("destroy",{}),this._bus.clear()}}}function ba(e,t={}){return new ma(e,t)}function ga(e){const t={Equal:"+",Minus:"-",NumpadAdd:"+",NumpadSubtract:"-",ArrowLeft:"<",ArrowRight:">",ArrowUp:"^",ArrowDown:"v"};return e.split("+").map(e=>e.trim()).map(e=>"Mod"===e?wn()?"Cmd":"Ctrl":t[e]??e.replace(/^(Key|Digit)/,"")).join(" + ")}const va={bars:0,matched:0,gaps:0,dropped:0};function ya(e,t){const i=new Map;for(const e of t){const t=Yo(e);isFinite(t.close)?i.set(t.time,t):i.delete(t.time)}const s=[];let r=0;for(const t of e){const e=i.get(t.time);void 0!==e?(s.push(e),r++):s.push({time:t.time})}return{items:s,alignment:{bars:s.length,matched:r,gaps:s.length-r,dropped:i.size-r}}}class wa{constructor(e,t={}){Object.defineProperty(this,"_chart",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_mode",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_baseline",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_panes",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_items",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_alignedAt",{enumerable:!0,configurable:!0,writable:!0,value:-1}),Object.defineProperty(this,"_realigning",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_nextScale",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_syncing",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_alignedPrimary",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_primaryLength",{enumerable:!0,configurable:!0,writable:!0,value:-1}),Object.defineProperty(this,"_primaryFirst",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_primaryLast",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_off",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._chart=e,this._mode=t.mode??"percentage",this._baseline=t.baseline??"first-visible",this._off.push(yo(e,()=>this.realign())),e.on&&(this._off.push(e.on("data:update",()=>{this._needsAlignment()&&this.realign()})),this._off.push(e.on("destroy",()=>this._dispose())))}add(e){if(this._destroyed)throw new Error("openalgo-charts: comparison controller is destroyed");if(null===this._chart.primarySeries())throw new Error("openalgo-charts: a comparison needs a primary series to align against");const t=e.paneIndex??this._chart.primaryPaneIndex?.()??0,i={...e.style};void 0===i.color&&void 0!==e.color&&(i.color=e.color);const s=this._chart.panes()[t],r=void 0===s?void 0:this._panes.get(s),n=this._scaleIdFor(t),o=this._chart.addSeries(e.type??"line",{paneIndex:t,style:i,priceScaleId:n}),a=r??this._openPane(this._chart.panes()[t],t);a.count++;const l=this._chart;let h=t;const c={series:o,entry:a,scale:o.priceScale(),savedScaleMode:o.priceScale().options.mode,savedInverted:o.priceScale().options.inverted,appliedInverted:null,scaleId:n,type:e.type??"line",style:i,items:[],values:new Map,suppressed:!1,bars:e.bars,alignment:va,removed:!1};"none"!==this._mode&&c.scale.setOptions({mode:this._mode});const d={symbol:e.symbol,series:o,get paneIndex(){const e=l.panes().indexOf(a.pane);return e>=0&&(h=e),h},priceScale:()=>c.scale,alignment:()=>c.alignment,barAt:e=>{const t=c.values.get(e);return!c.removed&&!c.suppressed&&t&&Number.isFinite(t.close)?t:null},setBars:e=>{c.removed||(c.bars=e,this._align(c,this._primaryBars()))},remove:()=>{this.remove(d)},list:()=>this.list()};return this._items.set(d,c),this._align(c,this._primaryBars()),this._rememberAlignment(),d}remove(e){const t=this._items.get(e);if(void 0===t||t.removed)return!1;this._restoreScale(t),t.removed=!0,this._items.delete(e);const i=t.entry;return--i.count<=0&&this._closePane(i),t.series.remove(),!0}list(){return Array.from(this._items.keys())}clear(){for(const e of this.list())this.remove(e)}setMode(e){if(e!==this._mode){this._mode=e,this._syncing=!0;try{for(const e of this._panes.values())this._restoreMode(e),this._applyMode(e);for(const e of this._items.values())this._setSuppressed(e,!1)}finally{this._syncing=!1}this._repaint()}}get mode(){return this._mode}setBaseline(e){if(e!==this._baseline){this._baseline=e,this._syncing=!0;try{for(const e of this._items.values())this._setSuppressed(e,!1)}finally{this._syncing=!1}this._repaint()}}get baseline(){return this._baseline}baselineTime(e){const t=this._chart.panes()[e??this._chart.primaryPaneIndex?.()??0],i=void 0===t?void 0:this._panes.get(t);return"common"===this._baseline&&"none"!==this._mode&&i&&i.primary.options.mode===i.applied?this._commonAnchor(i,this._visiblePrimary())?.time??null:null}realign(){if(this._realigning||this._destroyed||!this._items.size)return;this._realigning=!0;const e=this._syncing;this._syncing=!0;try{const e=this._primaryBars();for(const t of this._items.values())this._align(t,e);this._rememberAlignment()}finally{this._realigning=!1,this._syncing=e}e||this._repaint()}sync(){if(!this._syncing){this._syncing=!0;try{this._needsAlignment()&&this.realign();for(const e of this._panes.values())this._mirror(e)}finally{this._syncing=!1}}}destroy(){this._destroyed||(this.clear(),this._dispose())}_dispose(){if(!this._destroyed){this._destroyed=!0;for(const e of this._off.splice(0))e();for(const e of this._items.values())e.removed=!0,e.bars=[],e.items=[],e.values.clear();this._items.clear(),this._panes.clear(),this._alignedPrimary=null,xa.delete(this._chart)}}_needsAlignment(){if(this._destroyed||!this._items.size)return!1;const e=this._chart.primaryBars?.();return this._chart.dataLayer.length!==this._alignedAt||Boolean(e&&(e!==this._alignedPrimary||e.length!==this._primaryLength||e[0]?.time!==this._primaryFirst||e[e.length-1]?.time!==this._primaryLast))}_rememberAlignment(){this._alignedAt=this._chart.dataLayer.length,this._alignedPrimary=this._chart.primaryBars?.()??null,this._primaryLength=this._alignedPrimary?.length??-1,this._primaryFirst=this._alignedPrimary?.[0]?.time,this._primaryLast=this._alignedPrimary?.[this._primaryLength-1]?.time}_scaleIdFor(e){const t=this._chart.panes()[e],i=e=>t?.series().some(t=>t.scaleId===e)??!1;if(void 0===t||!this._panes.has(t)){if(!i(""))return"";if(!i("left"))return"left"}let s;do{s="overlay:comparison-"+ ++this._nextScale}while(i(s));return s}_openPane(e,t){const i=e.priceScale,s={pane:e,primary:i,savedPrimaryMode:i.options.mode,applied:null,sync:{zOrder:()=>"bottom",draw:()=>{},afterAutoscale:()=>{this.sync()}},count:0};return this._panes.set(e,s),this._applyMode(s),this._chart.addPrimitive(s.sync,t),s}_closePane(e){this._restoreMode(e),this._chart.removePrimitive(e.sync),this._panes.delete(e.pane)}_applyMode(e){if("none"!==this._mode){e.applied=this._mode,e.primary.setOptions({mode:this._mode});for(const t of this._items.values())t.entry===e&&t.scale.setOptions({mode:this._mode})}}_restoreScale(e){null!==e.entry.applied&&e.scale.options.mode===e.entry.applied&&e.scale.setOptions({mode:e.savedScaleMode}),null!==e.appliedInverted&&e.scale.options.inverted===e.appliedInverted&&e.scale.setOptions({inverted:e.savedInverted}),e.appliedInverted=null}_restoreMode(e){const t=e.applied;if(null!==t){for(const t of this._items.values())t.entry===e&&this._restoreScale(t);e.applied=null,e.primary.options.mode===t&&e.primary.setOptions({mode:e.savedPrimaryMode})}}_mirror(e){if(null===e.applied)return;const t=Array.from(this._items.values()).filter(t=>t.entry===e);if(e.primary.options.mode!==e.applied){for(const e of t)this._setSuppressed(e,!1);return}if("common"===this._baseline&&e.primary.options.mode===e.applied){const i=this._visiblePrimary(),s=this._commonAnchor(e,i);for(const e of t){const t=s&&e.values.get(s.time)?.close;this._setSuppressed(e,!t||!Number.isFinite(t)||t<=0)}if(null===s)return;e.primary.setBaseline(s.close);const r=e.primary.priceRange();for(const n of t){const t=n.values.get(s.time)?.close;if(n.scale.setBaseline(t&&t>0?t:null),!e.primary.autoScale||!t||t<=0||!this._visible(n))continue;const o=je(n.type),a=this._record(n)?.style??n.style;let l=1/0,h=-1/0;for(const e of i){const t=n.values.get(e.time);if(!t)continue;const i=o.extents(t,a);Number.isFinite(i.min)&&Number.isFinite(i.max)&&(l=Math.min(l,i.min),h=Math.max(h,i.max))}if(!(l<=h))continue;const c=v(l,h,e.primary.options.marginTop,e.primary.options.marginBottom),d=s.close/t;r.min=Math.min(r.min,c.min*d),r.max=Math.max(r.max,c.max*d)}e.primary.autoScale&&e.primary.setComputedRange(r)}const i=e.primary.baseline;if(null===i||!Number.isFinite(i)||i<=0||!e.primary.scaled)return;const s=e.primary.priceRange();for(const t of this._items.values()){if(t.entry!==e)continue;const r=t.scale.baseline;if(null===r||!Number.isFinite(r)||r<=0)continue;const n=r/i;t.appliedInverted=e.primary.options.inverted,t.scale.setOptions({inverted:t.appliedInverted}),t.scale.setComputedRange({min:s.min*n,max:s.max*n})}}_primaryBars(){return this._chart.primaryBars?.()??this._chart.primarySeries()?.getData()??[]}_record(e){return e.entry.pane.series().find(t=>t.scaleId===e.scaleId)}_visible(e){return!1!==this._record(e)?.style?.visible}_visiblePrimary(){const e=this._primaryBars(),t=this._chart.getVisibleLogicalRange?.();if(!t)return e;const i=Math.max(0,Math.floor(t.from)),s=Math.min(this._chart.dataLayer.length-1,Math.ceil(t.to));if(i>s)return[];const r=this._chart.dataLayer.indexToTime?.(i)??e[i]?.time,n=this._chart.dataLayer.indexToTime?.(s)??e[s]?.time;if(void 0===r||void 0===n)return[];let o=0,a=e.length;for(;o<a;){const t=o+a>>>1;e[t].time<r?o=t+1:a=t}let l=o;for(;l<e.length&&e[l].time<=n;)l++;return e.slice(o,l)}_commonAnchor(e,t){const i=Array.from(this._items.values()).filter(t=>t.entry===e&&this._visible(t));return i.length?t.find(e=>Number.isFinite(e.close)&&e.close>0&&i.every(t=>{const i=t.values.get(e.time)?.close;return void 0!==i&&Number.isFinite(i)&&i>0}))??null:null}_setSuppressed(e,t){t!==e.suppressed&&(e.suppressed=t,this._write(e))}_write(e){e.series.setData(e.suppressed?e.items.map(e=>({time:e.time})):e.items)}_align(e,t){const i=bo(this._chart),s=ya(i?t.filter(e=>e.time<=i.time):t,i?.forming?e.bars.filter(e=>e.time!==i.time):e.bars);e.alignment=s.alignment,e.items=s.items,e.values=new Map(s.items.map(e=>{const t=Yo(e);return[t.time,t]})),this._write(e)}_repaint(){for(const e of this._items.values())e.series.applyOptions({})}}const xa=new WeakMap;function Sa(e,t){let i=xa.get(e);return void 0===i?(i=new wa(e,t),xa.set(e,i)):(void 0!==t?.mode&&i.setMode(t.mode),void 0!==t?.baseline&&i.setBaseline(t.baseline)),i}function Pa(e,t){return Sa(e).add(t)}const Oa=["open","high","low","close","volume","oi"],ka=["time",...Oa],Ia=["open","high","low","close"],Ma=e=>"number"==typeof e&&Number.isFinite(e)?e:void 0,Ta=(e,t)=>(void 0===t.from||e>=t.from)&&(void 0===t.to||e<=t.to);function Ca(e,t,i,s,r){const n=e.primaryBars().map(({time:e,open:t,high:i,low:s,close:r,volume:n,oi:o})=>({time:e,open:t,high:i,low:s,close:r,volume:n,oi:o})),o=!1===t?[]:e.indicators().slice(),a=new Map(o.map(e=>[e.id,e])),l="boolean"==typeof t?o:t.map(e=>{const t=a.get(e);if(!t)throw new RangeError(`Unknown CSV indicator ID: ${e}`);return t}),h=Oa.map(e=>({meta:Object.freeze({key:e,title:e,kind:"primary",field:e}),values:n.map(t=>Ma(t[e])),offset:0}));for(const t of l){const s=t.values();for(const r of ds(t.indicatorId).plots){let n=0;if("display"===i){const i=t.series(r.key),s=i&&e.seriesStyle(i);if(!s)throw new Error(`CSV plot resource is unavailable: ${t.id}:${r.key}`);if(n=void 0===s.barOffset?0:s.barOffset,"number"!=typeof n||!Number.isFinite(n))throw new TypeError("CSV plot offset must be finite")}const o="display"===i&&r.ohlc?Ia:[void 0];for(const e of o){const i=`indicator:${t.id}:${r.key}${e?":"+e:""}`,o=s[e?r.ohlc[e]:r.key];h.push({meta:Object.freeze({key:i,title:e?`${r.title} ${e}`:r.title,kind:"indicator",instanceId:t.id,plotKey:r.key,...e?{field:e}:{}}),values:Array.isArray(o)?o.map(Ma):[],offset:n})}}}const c=e.primaryBars();if(n.length!==c.length||n.some((e,t)=>ka.some(i=>!Object.is(e[i],c[t][i]))))throw new Error("Chart data changed during CSV snapshot");const d=bo(e),u="display"===i?Array.from({length:e.dataLayer.length},(t,i)=>e.dataLayer.indexToTime(i)).filter(e=>!d||e<=d.time):[];if(u.some((e,t)=>!Number.isFinite(e)||t>0&&e<=u[t-1]))throw new Error("CSV axis times must be finite and increasing");const _=Object.freeze({axisTimes:Object.freeze(u),...d?{replay:Object.freeze({...d})}:{}}),p=(r??function(e){return xa.get(e)?.list()??[]}(e)).map(e=>({symbol:e.symbol,item:e})),f=p.map(({symbol:e},t)=>{const i=`comparison:${t+1}:${e}:close`;return{meta:Object.freeze({key:i,title:e,kind:"comparison",comparisonIndex:t+1,symbol:e,field:"close"}),values:[],offset:0}});return n.forEach((e,t)=>{("source"!==i||Ta(e.time,s))&&p.forEach(({item:i},s)=>{f[s].values[t]=Ma(i.barAt(e.time)?.close)})}),h.push(...f),{times:n.map(e=>e.time),columns:h,context:_}}const ja=e=>/[",\r\n]/.test(e)?`"${e.replace(/"/g,'""')}"`:e;function Aa(e){if("string"!=typeof e)throw new TypeError("CSV formatter must return a string");const t=e.charCodeAt(0);return t<=31||127===t||/^[\s\ufeff]*[=+@-]/.test(e)?"'"+e:e}const La=(e,t)=>Object.freeze({key:t,title:t,kind:e});function Na(e,t){if(null===e||"object"!=typeof e)throw new TypeError("Invalid CSV options");const i=Object.getOwnPropertyDescriptor(e,t);if(i?!("value"in i):t in e)throw new TypeError(`CSV ${t} must be an own data property`);return i?.value}function Ra(e,t={}){const i=function(e){if(void 0===e||!0===e)return!0;if(!1===e)return!1;if(!Array.isArray(e))throw new TypeError("CSV indicators must be a boolean or an array of instance IDs");const t=Object.getOwnPropertyDescriptors(e);if(Reflect.ownKeys(t).length!==e.length+1)throw new TypeError("Invalid CSV indicator ID array");const i=[],s=new Set;for(let r=0;r<e.length;r++){const e=t[String(r)];if(!e||!("value"in e)||"string"!=typeof e.value||0===e.value.trim().length)throw new TypeError("CSV indicator IDs must be nonempty strings in a dense data array");const n=e.value;if(s.has(n))throw new RangeError(`Duplicate CSV indicator ID: ${n}`);s.add(n),i.push(n)}return 0!==i.length&&i}(Na(t,"indicators")),s=function(e){if(void 0===e)return{};if(!ts(e))throw new TypeError("Invalid CSV range");const t=Object.getOwnPropertyDescriptors(e);for(const e of Reflect.ownKeys(t))if("from"!==e&&"to"!==e||!("value"in t[e]))throw new TypeError("Invalid CSV range property");const i={};for(const s of["from","to"]){const r=t[s];if(!r&&s in e)throw new TypeError("CSV range bounds must be own properties");const n=r?.value;if(void 0!==n){if("number"!=typeof n||!Number.isFinite(n))throw new TypeError("CSV range bounds must be finite UTC seconds");i[s]=n}}if(void 0!==i.from&&void 0!==i.to&&i.from>i.to)throw new RangeError("CSV range is reversed");return i}(Na(t,"range")),r=Na(t,"alignment"),n=void 0===r?"source":r;if("source"!==n&&"display"!==n)throw new TypeError("Invalid CSV alignment");const o=Na(t,"projectTime");if(void 0!==o&&("function"!=typeof o||"display"!==n))throw new TypeError("CSV projectTime requires display alignment and a function");const a=function(e){if(void 0===e)return{};if(!ts(e))throw new TypeError("Invalid CSV formatters");const t=Object.getOwnPropertyDescriptors(e);for(const e of Reflect.ownKeys(t))if(!["time","value","header"].includes(e)||!("value"in t[e]))throw new TypeError("Invalid CSV formatter property");const i={};for(const s of["time","value","header"]){const r=t[s];if(!r&&s in e)throw new TypeError("CSV formatters must be own properties");const n=r?.value;if(void 0!==n&&"function"!=typeof n)throw new TypeError("CSV formatters must be functions");i[s]=n}return i}(Na(t,"formatters")),l=Ca(e,i,n,s,t.comparisons),h=function(e,t,i,s){if("source"===t)return e.times.flatMap((t,s)=>Ta(t,i)?[{time:t,values:e.columns.map(e=>e.values[s])}]:[]);const r=e.context.axisTimes,n=new Map(r.map((e,t)=>[e,t])),o=e.times.map(e=>{const t=n.get(e);if(void 0===t)throw new Error("CSV primary time is absent from the installed axis");return t}),a=new Map,l=e=>{let t=a.get(e);return t||(t={time:null,logicalIndex:e,values:[]},a.set(e,t)),t};o.forEach(l),e.columns.forEach((e,t)=>{const i=new Set;o.forEach((s,r)=>{const n=e.values[r];if("indicator"!==e.meta.kind)return void(l(s).values[t]=n);if(void 0===n)return;const o=s+e.offset;if(!Number.isFinite(o)||Math.abs(o)>Number.MAX_SAFE_INTEGER||0!==e.offset&&o===s||!Number.isInteger(e.offset)&&Number.isInteger(o)||i.has(o))throw new RangeError("CSV plot position cannot be represented safely");i.add(o),l(o).values[t]=n})});const h=[...a.values()].sort((e,t)=>e.logicalIndex-t.logicalIndex);let c;for(const t of h){const i=t.logicalIndex;if(Number.isInteger(i)&&i>=0&&i<r.length)t.time=r[i],t.origin="axis";else if(i>=0&&i<r.length-1){const e=Math.floor(i);if(t.time=r[e]+(r[e+1]-r[e])*(i-e),t.origin="interpolated",!(t.time>r[e]&&t.time<r[e+1]))throw new RangeError("CSV interpolated time collides with the axis")}else{if(t.origin="unknown",s)t.time=s(i,e.context);else if(r.length>1){const e=i<0?0:r.length-1,s=i<0?r[1]-r[0]:r[e]-r[e-1];t.time=r[e]+(i-e)*s}if(null!==t.time){if("number"!=typeof t.time||!Number.isFinite(t.time))throw new TypeError("CSV projected time must be finite or null");if(r.length&&(i<0?t.time>=r[0]:t.time<=r[r.length-1]))throw new RangeError("CSV projected time collides with the axis");t.origin="projected"}}if(null!==t.time){if(!Number.isFinite(t.time)||void 0!==c&&t.time<=c)throw new RangeError("CSV times must be strictly increasing");c=t.time}}return h.filter(e=>null===e.time?void 0===i.from&&void 0===i.to:Ta(e.time,i))}(l,n,s,o);return function(e,t,i,s){const r=[La("time","time"),...s.time?[La("timeLabel","time_label")]:[],..."display"===i?[La("logicalIndex","logical_index"),La("timeOrigin","time_origin")]:[],...e.map(e=>e.meta)],n=r.map(e=>s.header?Aa(s.header(e)):e.key),o=new Map;n.forEach(e=>o.set(e,(o.get(e)??0)+1));const a=n.map((e,t)=>s.header&&o.get(e)>1?`${r[t].key}:${e}`:e);!s.header&&"display"!==i||new Set(a).size===a.length||a.forEach((e,t)=>{a[t]=`${t+1}:${r[t].key}:${e}`});const l=[a.map(ja).join(",")];for(const r of t){const t=null!==r.time&&Number.isFinite(r.time)?r.time:null,n=[null===t?"":String(t)];s.time&&n.push(null===t?"":ja(Aa(s.time(t)))),"display"===i&&n.push(String(r.logicalIndex),r.origin),e.forEach((e,t)=>{const i=r.values[t];n.push(void 0===i?"":s.value?ja(Aa(s.value(i,e.meta))):String(i))}),l.push(n.join(","))}return l.join("\r\n")+"\r\n"}(l.columns,h,n,a)}const Fa=Object.freeze([]),za=(e,t)=>e.length===t.length&&e.every((e,i)=>{const s=t[i];return e.id===s.id&&e.name===s.name&&e.kind===s.kind&&e.paneIndex===s.paneIndex&&e.band===s.band&&e.stackAbove===s.stackAbove&&e.hiddenOnInterval===s.hiddenOnInterval&&e.visible===s.visible&&e.locked===s.locked&&e.selected===s.selected&&e.groupId===s.groupId&&e.dataStatus?.state===s.dataStatus?.state&&("error"!==e.dataStatus?.state||"error"===s.dataStatus?.state&&e.dataStatus.error===s.dataStatus.error)&&Object.keys(e.capabilities).every(t=>e.capabilities[t]===s.capabilities[t])});class Ea{constructor(e,t={}){if(Object.defineProperty(this,"_chart",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_options",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_off",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_providers",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_listeners",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_entries",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_order",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_stackKey",{enumerable:!0,configurable:!0,writable:!0,value:""}),Object.defineProperty(this,"_publishedStackKey",{enumerable:!0,configurable:!0,writable:!0,value:""}),Object.defineProperty(this,"_rows",{enumerable:!0,configurable:!0,writable:!0,value:Fa}),Object.defineProperty(this,"_selected",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_refreshing",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_pending",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._chart=e,this._options=t,e.isDestroyed)return void(this._destroyed=!0);for(const t of["objects:change","indicatorRemoved","paneRemoved","paneMoved","paneAdded","data:context","indicator:data-status","drawing:change","drawing:select"])this._off.push(e.on(t,()=>this.refresh()));let i=e.dataLayer.length>0;this._off.push(e.on("data:range",()=>{const t=e.dataLayer.length>0;t!==i&&(i=t,this.refresh())})),this._off.push(e.on("destroy",()=>this.destroy())),this.refresh()}list(){return this._refreshing||this.refresh(),this._rows}get(e){return this._refreshing||this.refresh(),this._entries.get(e)?.row}subscribe(e){this.refresh(),this._destroyed||this._listeners.add(e);try{e(this._rows)}catch{}return()=>{this._listeners.delete(e)}}register(e){if(this._destroyed)return()=>{};if("string"!=typeof e.id||""===e.id.trim())throw new Error("An object provider needs an id");const t="custom:"+e.id;if(this._providers.has(t))throw new Error("Object provider already registered: "+e.id);const i={provider:e};this._providers.set(t,i);try{i.off=e.subscribe?.(()=>this.refresh())}catch(e){throw this._providers.get(t)===i&&this._providers.delete(t),this.refresh(),e}if(this._destroyed||this._providers.get(t)!==i){try{i.off?.()}catch{}return()=>{}}return this.refresh(),()=>{if(this._providers.get(t)===i){this._providers.delete(t);try{i.off?.()}catch{}this.refresh()}}}refresh(){if(!this._destroyed)if(this._refreshing)this._pending=!0;else{this._refreshing=!0;try{do{this._pending=!1;const e=this._read();if(this._destroyed)break;this._entries=e,null===this._selected||e.has(this._selected)||(this._selected=null);const t=Object.freeze([...e.values()].map(e=>e.row));if(!za(t,this._rows)||this._stackKey!==this._publishedStackKey){this._rows=t,this._publishedStackKey=this._stackKey;for(const e of[...this._listeners]){if(this._destroyed)break;try{e(t)}catch{}}}}while(this._pending&&!this._destroyed)}finally{this._refreshing=!1}}}select(e,t=!1){if(this._destroyed)return!1;if(null===e)return this._selected=null,this._options.drawings?.select(null),this.refresh(),!0;const i=this.get(e);if(!i?.capabilities.select)return!1;try{return this._selected="drawing"===i.kind?null:e,e.startsWith("drawing:")?this._options.drawings?.select(i.sourceId,t):(this._options.drawings?.select(null),this._entries.get(e)?.actions.select?.()),this.refresh(),!0}catch{return this.refresh(),!1}}paneCount(){return this._chart.panes().length}primaryPaneIndex(){return this._chart.primaryPaneIndex()}canReorder(e,t){const i=this.get(e);if(!i?.capabilities.reorder||-1!==t&&1!==t)return!1;if(this._providers.has(e))return!0;const s="drawing"===i.kind?this._options.drawings?.get(i.sourceId):void 0,r=this._rows.filter(e=>e.kind===i.kind&&e.paneIndex===i.paneIndex&&(void 0===s||(this._options.drawings?.get(e.sourceId)?.zIndex??0)<0==(s.zIndex??0)<0)),n=r.findIndex(t=>t.id===e)+t;return n>=0&&n<r.length}reorder(e,t){if(!this.get(e)?.capabilities.reorder||this._destroyed)return!1;try{const i=!0===this._entries.get(e)?.actions.reorder?.(t);return this.refresh(),i}catch{return this.refresh(),!1}}move(e,t){if(!this.get(e)?.capabilities.move||this._destroyed)return!1;try{const i=!0===this._entries.get(e)?.actions.move?.(t);return this.refresh(),i}catch{return this.refresh(),!1}}stack(e){return this._refreshing||this.refresh(),this._stackOf(e)}canPlace(e,t,i){return null!==this._plan(e,t,i)}place(e,t,i){const s=this._plan(e,t,i);if(null===s)return!1;try{const e=s();return this.refresh(),e}catch{return this.refresh(),!1}}_stackOf(e){const t=this._rows.filter(t=>t.paneIndex===e&&void 0!==t.band),i=(e,i)=>t.filter(t=>"drawing"===t.kind&&t.band===e&&t.stackAbove===i).sort((e,t)=>{const i=this._order.get(e.id),s=this._order.get(t.id);return i[0]-s[0]||i[1]-s[1]}),s=i("below");for(const r of this._chart.seriesStack(e)){const e=t.find(e=>e.id===r&&"drawing"!==e.kind);e&&s.push(e),s.push(...i("series",r))}return[...s,...i("above")]}_plan(e,t,i){if(this._destroyed||e===t||"above"!==i&&"below"!==i)return null;const s=this.get(e),r=this.get(t);if(!s?.capabilities.place||!r||s.paneIndex!==r.paneIndex)return null;const n=this._stackOf(s.paneIndex),o=n.findIndex(t=>t.id===e),a=n.findIndex(e=>e.id===t);if(o<0||a<0)return null;const l=e=>"series"===e.band?"series:"+e.stackAbove:e.band;if("drawing"===s.kind){const e=this._options.drawings;return"drawing"===r.kind?l(s)===l(r)&&o===a+("above"===i?1:-1)?null:()=>e.placeInStack(s.sourceId,{drawing:r.sourceId},i):("above"===i?s.stackAbove===r.id&&o===a+1:o===a-1)?null:()=>e.placeInStack(s.sourceId,{entry:r.id},i)}const h=this._chart.seriesStack(s.paneIndex),c=n[a+1],d=n[a-1];let u,_=i;if("drawing"!==r.kind?"below"!==i&&"drawing"===c?.kind&&c.stackAbove===r.id||(u=r.id):"series"!==r.band||"above"!==i||void 0!==c&&l(c)===l(r)?"below"===r.band&&"above"===i&&"below"!==c?.band?[u,_]=[h[0],"below"]:"above"===r.band&&"below"===i&&"above"!==d?.band&&([u,_]=[h[h.length-1],"above"]):u=r.stackAbove,void 0===u||u===e||!h.includes(u))return null;const p=h.filter(t=>t!==e);if(p.splice(p.indexOf(u)+("above"===_?1:0),0,e),p.every((e,t)=>e===h[t]))return null;const[f,m]=[u,_];return()=>!0===this._entries.get(e)?.actions.place?.(f,m)}canGroup(){return!this._destroyed&&"function"==typeof this._options.drawings?.createGroup}createGroup(e,t){if(!this.canGroup())return null;const i=t.flatMap(e=>{const t=this.get(e);return"drawing"===t?.kind&&e==="drawing:"+t.sourceId?[t.sourceId]:[]}),s=this._options.drawings.createGroup(e,i);return this.refresh(),s?"group:"+s.id:null}renameGroup(e,t){const i=this.get(e);if(this._destroyed||"group"!==i?.kind||e!=="group:"+i.sourceId)return!1;const s=!0===this._options.drawings?.renameGroup?.(i.sourceId,t);return this.refresh(),s}ungroup(e){const t=this._destroyed?void 0:this._entries.get(e)?.actions.ungroup;if(!t)return!1;const i=t();return this.refresh(),i}setVisible(e,t){return this._act(e,"visibility",e=>e.setVisible(t))}setLocked(e,t){return this._act(e,"lock",e=>e.setLocked(t))}remove(e){return this._act(e,"remove",e=>e.remove())}openSettings(e){return this._act(e,"settings",e=>e.openSettings())}focus(e){return this._act(e,"focus",e=>e.focus())}_act(e,t,i){const s=this.get(e);if(this._destroyed||!s?.capabilities[t])return!1;try{return i(this._entries.get(e).actions),this.refresh(),!0}catch{return this.refresh(),!1}}_read(){const e=new Map,t=new Map,i=this._options.drawings,s=i?.selection()??[];s.length>0&&(this._selected=null);const r=(t,i,s,r,n,o)=>{const a=Object.freeze({select:"function"==typeof r.select,visibility:"function"==typeof r.setVisible,lock:"function"==typeof r.setLocked,remove:"function"==typeof r.remove,settings:"function"==typeof r.openSettings,focus:"function"==typeof r.focus,reorder:"function"==typeof r.reorder,move:"function"==typeof r.move,place:"function"==typeof r.place&&!t.startsWith("custom:")}),l=Object.freeze({id:t,sourceId:i,kind:s.kind,name:s.name,paneIndex:s.paneIndex??this._chart.primaryPaneIndex(),visible:!1!==s.visible,locked:s.locked,groupId:s.groupId,selected:!0===s.selected||this._selected===t,dataStatus:s.dataStatus?Object.freeze({...s.dataStatus}):void 0,capabilities:a,...void 0===n?{}:{band:n},...void 0===o?{}:{stackAbove:o},...!0===s.hiddenOnInterval?{hiddenOnInterval:!0}:{}});e.set(t,{row:l,actions:r})},n=e=>this._options.onSettings?{openSettings:()=>{const t=this.get(e);t&&this._options.onSettings(t)}}:{},o=this._chart,a=new Map,l=e=>{let t=a.get(e);return t||a.set(e,t=o.seriesStack(e)),t},h=e=>({place:(t,i)=>o.moveInSeriesStack(e,t,i)});if(null!==o.primarySeries()){const e=o.getDataContext(),t=l(o.primaryPaneIndex()).includes("source:primary");r("source:primary","primary",{kind:"source",name:e?.symbol||"Price",visible:!1!==o.primarySeriesInfo()?.style.visible},{...n("source:primary"),...t?h("source:primary"):{}},t?"series":void 0)}for(const e of o.indicators()){const t=e.policy();if(!1===t.listed)continue;const i="indicator:"+e.id,s=l(e.paneIndex).includes(i);r(i,e.id,{kind:"indicator",name:e.name,paneIndex:e.paneIndex,visible:e.visible(),dataStatus:e.dataStatus()??void 0},{select:()=>{},setVisible:t=>e.setVisible(t),...!1!==t.movable?{reorder:t=>o.reorderIndicator(e.id,t),move:t=>o.moveIndicator(e.id,t),...s?h(i):{}}:{},...!1!==t.removable?{remove:()=>{o.removeIndicator(e.id)}}:{},...!1!==t.configurable?n(i):{}},s?"series":void 0)}if(i){const e=e=>!1!==e.policy?.listed,a=new Set(i.hiddenOnInterval?.()??[]),h=e=>a.has(e.id),c=new Map;for(const t of i.groups?.()??[]){const n=t.members.flatMap(e=>{const t=i.get(e);return t?[t]:[]}),o=n.filter(e);if(!o.length)continue;const a="group:"+t.id;for(const e of o)c.set(e.id,a);const l=e=>{if(i.updateMany)i.updateMany(o.map(t=>({id:t.id,patch:e})));else for(const t of o)i.update(t.id,e)},d=o.filter(e=>!1!==e.policy?.selectable),u=o.length===n.length,_=u?i.removeGroup&&(()=>{i.removeGroup(t.id,!0)}):i.removeMany&&(()=>{i.removeMany(o.map(e=>e.id))});r(a,t.id,{kind:"group",name:t.name,paneIndex:o[0].paneIndex,visible:o.some(e=>!1!==e.visible),locked:o.every(e=>!0===e.locked),selected:d.length>0&&d.every(e=>s.includes(e.id)),hiddenOnInterval:o.every(h)},{...d.length?{select:()=>i.select(d.map(e=>e.id))}:{},...u&&i.removeGroup?{ungroup:()=>!0===i.removeGroup(t.id,!1)}:{},...o.every(e=>!1!==e.policy?.editable)?{setVisible:e=>l({visible:e}),setLocked:e=>l({locked:e}),..._?{remove:_}:{}}:{}})}const d=new Map(i.drawings().map((e,t)=>[e.id,t]));for(const a of[...i.drawings()].filter(e).sort((e,t)=>(e.zIndex??0)-(t.zIndex??0))){const e="drawing:"+a.id,u=void 0!==a.stackAbove&&l(a.paneIndex).includes(a.stackAbove)?a.stackAbove:void 0,_=void 0!==u?"series":(a.zIndex??0)<0?"below":"above";t.set(e,[a.zIndex??0,d.get(a.id)??0]);const p=h(a),f=!p&&o.dataLayer.length>0&&a.points.length>0&&a.points.every(e=>Number.isFinite(e.time)&&Number.isFinite(e.price))&&void 0!==o.panes()[a.paneIndex];r(e,a.id,{kind:"drawing",groupId:c.get(a.id),name:this._options.drawingName?.(a.tool)??a.tool.replace(/-/g," ").replace(/^./,e=>e.toUpperCase()),paneIndex:a.paneIndex,visible:!1!==a.visible,locked:!0===a.locked,selected:s.includes(a.id),hiddenOnInterval:p},{...i.reorder?{reorder:e=>i.reorder(a.id,e)}:{},...i.placeInStack?{place:()=>!1}:{},...!1!==a.policy?.selectable?{select:()=>i.select(a.id)}:{},...!1!==a.policy?.editable?{setVisible:e=>i.update(a.id,{visible:e}),setLocked:e=>i.update(a.id,{locked:e}),remove:()=>{i.remove(a.id)}}:{},...f?{focus:()=>this._focusDrawing(a)}:{},...n(e)},_,u)}}for(const[e,{provider:t}]of this._providers)try{const i=t.get();if(!i||"string"!=typeof i.name||!["source","indicator","drawing","profile","group"].includes(i.kind))continue;r(e,t.id,i,t)}catch{}return this._order=t,this._stackKey=JSON.stringify([[...a],[...t]]),e}_focusDrawing(e){const t=this._chart,i=e.points.map(e=>t.dataLayer.timeToIndexFloat(e.time));if(!i.every(Number.isFinite))return;const s=Math.min(...i),r=Math.max(...i),n=t.getVisibleLogicalRange(),o=Math.max(n.to-n.from,1.2*(r-s),10),a=t.panes()[e.paneIndex].readoutScale(),l=e.points.map(e=>e.price),h=Math.min(...l),c=Math.max(...l),d=Math.max(.15*(c-h),.001*Math.abs(c),a.options.minMove);a.setAutoScale(!1),a.setPriceRange({min:h-d,max:c+d});const u=t.maximizedPane();null!==u&&u!==e.paneIndex&&t.maximizePane(u),t.setPaneCollapsed(e.paneIndex,!1),t.setVisibleLogicalRange({from:(s+r-o)/2,to:(s+r+o)/2})}destroy(){if(this._destroyed)return;this._destroyed=!0;for(const e of this._off.splice(0))e();const e=[...this._providers.values()];this._providers.clear(),this._entries.clear(),this._rows=Fa,this._selected=null;for(const{off:t}of e)try{t?.()}catch{}const t=[...this._listeners];this._listeners.clear();for(const e of t)try{e(Fa)}catch{}}}const Ba=new Set([..."precision priceLineVisible lastValueVisible colorByPreviousClose upColor downColor bodyVisible borderUpColor borderDownColor borderVisible wickUpColor wickDownColor wickVisible topColor bottomColor closeColor color lineWidth lineStyle areaTopColor areaBottomColor".split(" ").map(e=>`symbol.${e}`),..."titleMode title logo marketStatus chartValues barChange volume openInterest lastDayChange lastValueLabel background backgroundColor backgroundOpacity".split(" ").map(e=>`statusLine.${e}`),..."visible color opacity fontSize".split(" ").map(e=>`watermark.${e}`),..."mode autoScale priceOnly inverted".split(" ").map(e=>`scales.${e}`),"axisChrome.sessionClock","axisChrome.barCountdown",..."grid.vertLines grid.vertColor grid.vertStyle grid.horzLines grid.horzColor grid.horzStyle grid.lineWidth grid.spacing crosshairMode crosshairSnapToBar crosshair.color crosshair.style crosshair.width scales.textColor scales.fontSize scales.lineColor margins.top margins.bottom".split(" ").map(e=>`canvas.${e}`)]);function $a(e){const t={};for(const[i,s]of Object.entries(e))Ba.has(i)&&("string"==typeof s||"boolean"==typeof s||"number"==typeof s&&Number.isFinite(s))&&(t[i]=s);return t}function Da(e,t,i,s,r,n){return{input:{key:e,type:"boolean",label:t,default:s,group:i},fields:[{key:e,read:r,write:(e,t)=>n(e,!0===t)}]}}function Wa(e,t,i,s,r,n){return{input:{key:e,type:"color",label:t,default:s,group:i},fields:[{key:e,read:r,write:(e,t)=>n(e,String(t))}]}}function qa(e,t,i,s,r,n,o){return{input:{key:e,type:"number",label:t,default:s,min:r.min,max:r.max,step:r.step,group:i},fields:[{key:e,read:n,write:(e,t)=>o(e,Number(t))}]}}function Va(e,t,i,s,r,n,o,a){return{input:{key:e,type:"select",label:t,default:s,options:r,group:i,tooltip:a},fields:[{key:e,read:n,write:(e,t)=>o(e,String(t))}]}}function Ha(e,t,i,s,r,n){const o={key:e,type:"colorPair",label:t,group:i,up:{key:s.key,label:s.label,default:s.def},down:{key:r.key,label:r.label,default:r.def}},a=[{key:s.key,read:s.get,write:(e,t)=>s.set(e,String(t))},{key:r.key,read:r.get,write:(e,t)=>r.set(e,String(t))}];return void 0!==n&&(o.enabled={key:n.key,default:n.def},a.unshift({key:n.key,read:n.get,write:(e,t)=>n.set(e,!0===t)})),{input:o,fields:a}}const Ga=[{label:"Solid",value:"solid"},{label:"Dashed",value:"dashed"},{label:"Dotted",value:"dotted"}],Ua=[{label:"Default",value:"default"},...Array.from({length:9},(e,t)=>({label:String(t),value:String(t)}))],Ka=e=>e.primarySeriesInfo()?.style??{},Ya=(e,t)=>{e.primarySeries()?.applyOptions(t)},Xa=(e,t,i)=>Ya(e,{[t]:i});function Ja(e,t,i,s,r,n){const o=e=>({key:`symbol.${e.key}`,label:e.label,def:e.def,get:t=>Ka(t)[e.key]??e.def,set:(t,i)=>Xa(t,e.key,i)}),a=void 0===n?void 0:{key:`symbol.${n.key}`,def:!0,get:e=>!1!==Ka(e)[n.key],set:(e,t)=>Xa(e,n.key,t)};return Ha(e,t,i,o(s),o(r),a)}const Za=e=>Math.round(1e3*e)/10;const Qa=e=>Da("symbol.colorByPreviousClose","Color from previous close",e,!1,e=>!0===Ka(e).colorByPreviousClose,(e,t)=>Ya(e,{colorByPreviousClose:t}));function el(e,t){return"all"in e?{all:e.all.map(e=>el(e,t))}:"any"in e?{any:e.any.map(e=>el(e,t))}:{...e,key:t+e.key}}function tl(e){const t=e.theme(),i=e.primarySeriesInfo()?.type,s=[];if(void 0===i)return[];if(s.push(...function(e){const t=e.primarySeries(),i=null===t?null:e.seriesTransform(t);if(null===i)return[];const{name:s,inputs:r}=Go(i.type),n="transform.";return r.map(e=>{const t=n+e.key,r={...e,key:t,group:s};e.visibleWhen&&(r.visibleWhen=el(e.visibleWhen,n)),e.activeWhen&&(r.activeWhen=el(e.activeWhen,n));const o=e=>{const t=e.primarySeries(),s=null===t?null:e.seriesTransform(t);return s?.type===i.type?s:null};return{input:r,fields:[{key:t,read:t=>o(t)?.options?.[e.key]??e.default,write:(t,i)=>{const s=o(t);if(null===s)return;const r={...s.options};i===e.default?delete r[e.key]:r[e.key]=i;try{t.setSeriesTransform(t.primarySeries(),{type:s.type,options:r})}catch{}}}]}})}(e)),"candlestick"===i||"hollow-candle"===i||"volume-candle"===i)s.push(Ja("symbol.body","Body","Candles",{key:"upColor",label:"Up",def:t.upColor},{key:"downColor",label:"Down",def:t.downColor},{key:"bodyVisible"}),Ja("symbol.borders","Borders","Candles",{key:"borderUpColor",label:"Up",def:t.upColor},{key:"borderDownColor",label:"Down",def:t.downColor},{key:"borderVisible"}),Ja("symbol.wick","Wick","Candles",{key:"wickUpColor",label:"Up",def:t.wickUpColor},{key:"wickDownColor",label:"Down",def:t.wickDownColor},{key:"wickVisible"}),Qa("Candles"));else if("bar"===i||"high-low"===i||"column"===i){const e="column"===i?"Columns":"Bars";s.push(Ja("symbol.body","column"===i?"Columns":"Bars",e,{key:"upColor",label:"Up",def:t.upColor},{key:"downColor",label:"Down",def:t.downColor})),"column"!==i&&s.push(Qa(e))}else"point-figure"===i?s.push(Ja("symbol.body","Boxes","Columns",{key:"upColor",label:"X",def:"#26a69a"},{key:"downColor",label:"O",def:"#ef5350"})):"kagi"===i?s.push(Ja("symbol.kagi","Line","Kagi",{key:"thickColor",label:"Thick",def:"#26a69a"},{key:"thinColor",label:"Thin",def:"#ef5350"})):("baseline"===i?s.push(Ja("symbol.baseline","Line","Baseline",{key:"topColor",label:"Above",def:t.baselineTopLine},{key:"bottomColor",label:"Below",def:t.baselineBottomLine})):"hlc-area"===i?s.push(Wa("symbol.closeColor","Close line","Line",t.lineColor,e=>Ka(e).closeColor??t.lineColor,(e,t)=>Ya(e,{closeColor:t}))):s.push(Wa("symbol.color","Line","Line",t.lineColor,e=>Ka(e).color??t.lineColor,(e,t)=>Ya(e,{color:t}))),"histogram"!==i&&s.push(qa("symbol.lineWidth","Thickness","Line",1.5,{min:.5,max:8,step:.5},e=>Ka(e).lineWidth??1.5,(e,t)=>Ya(e,{lineWidth:t}))),"line"!==i&&"line-markers"!==i&&"step"!==i&&"area"!==i||s.push(Va("symbol.lineStyle","Line style","Line","solid",Ga,e=>Ka(e).lineStyle??"solid",(e,t)=>Ya(e,{lineStyle:t}))),"area"===i?s.push(Ja("symbol.fill","Fill","Line",{key:"areaTopColor",label:"Top",def:t.areaTopColor},{key:"areaBottomColor",label:"Bottom",def:t.areaBottomColor})):"baseline"===i?s.push(Ja("symbol.fill","Fill","Baseline",{key:"areaTopColor",label:"Above",def:t.baselineTopFill},{key:"areaBottomColor",label:"Below",def:t.baselineBottomFill})):"hlc-area"===i&&s.push(Wa("symbol.areaTopColor","Band","Line",ye,e=>Ka(e).areaTopColor??ye,(e,t)=>Ya(e,{areaTopColor:t}))));return[...s,Va("symbol.precision","Precision","Values","default",Ua,e=>{const t=Ka(e).precision;return void 0===t?"default":String(t)},(e,t)=>Ya(e,{precision:"default"===t?void 0:Number(t)})),Da("symbol.priceLineVisible","Price line","Values",!0,e=>!1!==Ka(e).priceLineVisible,(e,t)=>Ya(e,{priceLineVisible:t})),Da("symbol.lastValueVisible","Last value label","Values",!0,e=>!1!==Ka(e).lastValueVisible,(e,t)=>Ya(e,{lastValueVisible:t}))]}const il=[{label:"Symbol",value:"symbol"},{label:"Description",value:"description"},{label:"Ticker",value:"ticker"}];function sl(e,t,i){return Da(`statusLine.${e}`,t,i,!0,t=>!1!==t.statusLineOptions()[e],(t,i)=>t.setStatusLineOptions({[e]:i}))}function rl(e){const t=e.theme();return[sl("logo","Logo","Show"),sl("title","Title","Show"),Va("statusLine.titleMode","Title shows","Show","symbol",il,e=>e.statusLineOptions().titleMode??"symbol",(e,t)=>e.setStatusLineOptions({titleMode:t})),sl("marketStatus","Session state","Show"),sl("chartValues","Open, high, low, close","Show"),sl("barChange","Bar change","Show"),sl("volume","Volume","Show"),Da("statusLine.openInterest","Open interest","Show",!1,e=>!0===e.statusLineOptions().openInterest,(e,t)=>e.setStatusLineOptions({openInterest:t})),sl("lastDayChange","Change since previous close","Show"),sl("lastValueLabel","Indicator values","Show"),Da("statusLine.indicatorsCollapsed","Collapse indicator legends","Show",!1,e=>e.indicatorLegendCollapsed(),(e,t)=>e.setIndicatorLegendCollapsed(t)),Da("statusLine.background","Background","Background",!1,e=>!0===e.statusLineOptions().background,(e,t)=>e.setStatusLineOptions({background:t})),Wa("statusLine.backgroundColor","Color","Background",t.background,e=>e.statusLineOptions().backgroundColor??t.background,(e,t)=>e.setStatusLineOptions({backgroundColor:t})),qa("statusLine.backgroundOpacity","Opacity","Background",.8,{min:0,max:1,step:.05},e=>e.statusLineOptions().backgroundOpacity??.8,(e,t)=>e.setStatusLineOptions({backgroundOpacity:t}))]}const nl={linear:"Linear",logarithmic:"Logarithmic",percentage:"Percent","indexed-to-100":"Indexed to 100"},ol=m.map(e=>({label:nl[e],value:e})),al=["UTC","Europe/London","Europe/Berlin","Europe/Moscow","Asia/Dubai","Asia/Kolkata","Asia/Singapore","Asia/Hong_Kong","Asia/Tokyo","Australia/Sydney","America/Sao_Paulo","America/New_York","America/Chicago","America/Los_Angeles"];function ll(e){const t=e.timezone();return(al.includes(t)?al:[...al,t]).map(e=>({label:e,value:e}))}function hl(e){const t=[Da("navigation.panEnabled","Enable panning","Navigation",!0,e=>!1!==e.navigationOptions().panEnabled,(e,t)=>e.setNavigationOptions({panEnabled:t})),Da("navigation.zoomEnabled","Enable zooming","Navigation",!0,e=>!1!==e.navigationOptions().zoomEnabled,(e,t)=>e.setNavigationOptions({zoomEnabled:t})),Va("navigation.mousePan","Mouse drag","Navigation","both",[{label:"Horizontal only",value:"horizontal"},{label:"Time and price",value:"both"}],e=>e.navigationOptions().mousePan,(e,t)=>e.setNavigationOptions({mousePan:t})),qa("navigation.defaultVisibleBars","Default visible bars (0 = all)","Navigation",0,{min:0,max:1e5,step:1},e=>e.navigationOptions().defaultVisibleBars,(e,t)=>e.setNavigationOptions({defaultVisibleBars:t})),qa("navigation.defaultBarSpacing","Default bar spacing (0 = use bar count)","Navigation",0,{min:0,max:1e4,step:.5},e=>e.navigationOptions().defaultBarSpacing??0,(e,t)=>e.setNavigationOptions({defaultBarSpacing:t})),Va("scales.mode","Scale","Price scale","linear",ol,e=>e.priceScaleOptions().mode,(e,t)=>e.setPriceScaleOptions({mode:t}),"Linear plots the price. Logarithmic gives equal space to equal percentage moves. Percent and Indexed to 100 both rebase to the left edge of the visible range, the first as a change from it, the second as a level starting at 100."),Da("scales.autoScale","Auto-fit to the data","Price scale",!0,e=>e.panes()[e.primaryPaneIndex()].priceScale.autoScale,(e,t)=>e.setAutoScale(t)),Da("scales.priceOnly","Fit primary prices only","Price scale",!1,e=>e.priceOnlyAutoScale(),(e,t)=>e.setPriceOnlyAutoScale(t)),Da("scales.inverted","Invert scale","Price scale",!1,e=>e.priceScaleOptions().inverted,(e,t)=>e.setPriceScaleOptions({inverted:t})),Va("time.timezone","Timezone","Time axis",ht,ll(e),e=>e.timezone(),(e,t)=>{_t(t)&&e.setTimezone(t)},"The calendar the axis and crosshair label in, and the one every session-anchored study resets on. Changing it moves VWAP and pivot values, not only the labels."),Da("axisChrome.sessionClock","Corner clock","Axis chrome",!1,e=>void 0!==e.axisChromeOptions().sessionClock&&!1!==e.axisChromeOptions().sessionClock,(e,t)=>e.setAxisChromeOptions({sessionClock:t}))];return function(e){const t=e.primarySeriesInfo()?.type;return void 0!==t&&je(t).isPriceSeries}(e)&&t.push(Da("axisChrome.barCountdown","Countdown to bar close","Axis chrome",!1,e=>!0===e.axisChromeOptions().barCountdown,(e,t)=>e.setAxisChromeOptions({barCountdown:t}))),t}const cl=[{label:"Cross",value:"normal"},{label:"Magnet",value:"magnet"}];function dl(e){const t=e.theme(),i=e=>e.gridOptions(),s=e=>e.canvasOptions().crosshair??{},r=e=>e.canvasOptions().scales??{};return[Da("watermark.visible","Show watermark","Watermark",!1,e=>e.watermarkOptions().visible??!1,(e,t)=>e.setWatermarkOptions({visible:t})),{input:{key:"watermark.text",type:"text",label:"Text (blank uses symbol and interval)",group:"Watermark",default:""},fields:[{key:"watermark.text",read:e=>e.watermarkOptions().text??"",write:(e,t)=>e.setWatermarkOptions({text:String(t)})}]},Wa("watermark.color","Color","Watermark","#9aa4b2",e=>e.watermarkOptions().color??"#9aa4b2",(e,t)=>e.setWatermarkOptions({color:t})),qa("watermark.opacity","Opacity","Watermark",.08,{min:0,max:1,step:.01},e=>e.watermarkOptions().opacity??.08,(e,t)=>e.setWatermarkOptions({opacity:t})),qa("watermark.fontSize","Text size","Watermark",64,{min:10,max:200,step:1},e=>e.watermarkOptions().fontSize??64,(e,t)=>e.setWatermarkOptions({fontSize:t})),Da("canvas.grid.vertLines","Vert grid lines","Grid",!0,e=>e.gridOptions().vertLines,(e,t)=>e.setGridOptions({vertLines:t})),Wa("canvas.grid.vertColor","Vert color","Grid",t.grid,e=>i(e)?.vertColor??t.grid,(e,t)=>e.setGridOptions({vertColor:t})),Va("canvas.grid.vertStyle","Vert style","Grid",t.gridStyle??"solid",Ga,e=>i(e)?.vertStyle??t.gridStyle??"solid",(e,t)=>e.setGridOptions({vertStyle:t})),Da("canvas.grid.horzLines","Horz grid lines","Grid",!0,e=>e.gridOptions().horzLines,(e,t)=>e.setGridOptions({horzLines:t})),Wa("canvas.grid.horzColor","Horz color","Grid",t.grid,e=>i(e)?.horzColor??t.grid,(e,t)=>e.setGridOptions({horzColor:t})),Va("canvas.grid.horzStyle","Horz style","Grid",t.gridStyle??"solid",Ga,e=>i(e)?.horzStyle??t.gridStyle??"solid",(e,t)=>e.setGridOptions({horzStyle:t})),qa("canvas.grid.lineWidth","Width","Grid",1,{min:1,max:4,step:1},e=>i(e)?.lineWidth??1,(e,t)=>e.setGridOptions({lineWidth:t})),qa("canvas.grid.spacing","Spacing","Grid",60,{min:20,max:200,step:10},e=>i(e)?.spacing??60,(e,t)=>e.setGridOptions({spacing:t})),Va("canvas.crosshairMode","Mode","Crosshair","normal",cl,e=>e.crosshairMode(),(e,t)=>e.applyOptions({crosshairMode:"magnet"===t?"magnet":"normal"})),Da("canvas.crosshairSnapToBar","Snap to candle center","Crosshair",!1,e=>e.crosshairSnapToBar(),(e,t)=>e.applyOptions({crosshairSnapToBar:t})),Wa("canvas.crosshair.color","Color","Crosshair",t.crosshair,e=>s(e).color??t.crosshair,(e,t)=>e.setCanvasOptions({crosshair:{color:t}})),Va("canvas.crosshair.style","Style","Crosshair",t.crosshairStyle??"dashed",Ga,e=>s(e).style??t.crosshairStyle??"dashed",(e,t)=>e.setCanvasOptions({crosshair:{style:t}})),qa("canvas.crosshair.width","Width","Crosshair",1,{min:1,max:4,step:1},e=>s(e).width??t.crosshairWidth??1,(e,t)=>e.setCanvasOptions({crosshair:{width:t}})),Wa("canvas.scales.textColor","Text color","Scale text",t.axisText,e=>r(e).textColor??t.axisText,(e,t)=>e.setCanvasOptions({scales:{textColor:t}})),qa("canvas.scales.fontSize","Text size","Scale text",t.axisFontSize??11,{min:10,max:14,step:1},e=>r(e).fontSize??t.axisFontSize??11,(e,t)=>e.setCanvasOptions({scales:{fontSize:t}})),Wa("canvas.scales.lineColor","Lines color","Scale text",t.axisLine,e=>r(e).lineColor??t.axisLine,(e,t)=>e.setCanvasOptions({scales:{lineColor:t}})),qa("canvas.margins.top","Top margin %","Plot margins",10,{min:0,max:49,step:1},e=>Za(e.priceScaleOptions().marginTop),(e,t)=>e.setCanvasOptions({margins:{top:t}})),qa("canvas.margins.bottom","Bottom margin %","Plot margins",10,{min:0,max:49,step:1},e=>Za(e.priceScaleOptions().marginBottom),(e,t)=>e.setCanvasOptions({margins:{bottom:t}}))]}function ul(e){const t=(t,i,s)=>function(e,t,i,s){return{key:`trading.${t}`,label:s,def:e.tradingSettings()[i],get:e=>e.tradingSettings()[i],set:(e,i)=>e.setTradingSettings({[t]:i})}}(e,t,i,s);return[Ha("trading.positions","Position","Positions",t("longColor","long","Long"),t("shortColor","short","Short")),Wa("trading.orderColor","Order","Orders",e.tradingSettings().order,e=>e.tradingSettings().order,(e,t)=>e.setTradingSettings({orderColor:t})),Ha("trading.bracket","Bracket","Orders",t("tpColor","tp","Take profit"),t("slColor","sl","Stop loss")),Ha("trading.executions","Execution","Executions",t("buyColor","buy","Buy"),t("sellColor","sell","Sell"))]}function _l(e){return[{id:"price",label:"Price",controls:tl(e)},{id:"readout",label:"Readout",controls:rl(e)},{id:"axes",label:"Axes",controls:hl(e)},{id:"appearance",label:"Appearance",controls:dl(e)},{id:"trading",label:"Trading",controls:ul(e)}]}function pl(e){return _l(e).map(e=>({id:e.id,label:e.label,inputs:e.controls.map(e=>e.input)}))}function fl(e){const t={};for(const i of _l(e))for(const s of i.controls)for(const i of s.fields)t[i.key]=i.read(e);return t}function ml(e,t){const i=new Map,s={};for(const t of _l(e))for(const e of t.controls)for(const t of e.fields)i.set(t.key,t);for(const r of Object.keys(t)){const n=t[r];if(void 0===n)continue;const o=i.get(r);o&&(o.write(e,n),s[r]=o.read(e))}const r=$a(s);Object.keys(r).length&&e.emit("style:change",r)}function bl(e,t){const i=t.open,s=Math.max(t.high,t.open,t.close),r=Math.min(t.low,t.open,t.close);if(!Number.isFinite(i)||!Number.isFinite(s)||!Number.isFinite(r))return e;const n=e=>e>s?s:e<r?r:e,o=n(e.close),a={time:t.time,open:i,high:Math.max(i,n(e.high),o),low:Math.min(i,n(e.low),o),close:o};return void 0!==e.volume&&(a.volume=void 0!==t.volume&&Number.isFinite(t.volume)?Math.min(e.volume,t.volume):e.volume),void 0!==e.oi&&(a.oi=e.oi),a}function gl(e,t){const i=Math.max(t.high,t.open,t.close),s=Math.min(t.low,t.open,t.close);i>e.high&&(e.high=i),s<e.low&&(e.low=s),e.close=t.close,e.volume=(e.volume??0)+(t.volume??0),void 0!==t.oi&&(e.oi=t.oi)}function vl(e,t){const i={time:t,open:e.open,high:Math.max(e.high,e.open,e.close),low:Math.min(e.low,e.open,e.close),close:e.close,volume:e.volume??0};return void 0!==e.oi&&(i.oi=e.oi),i}function yl(e,t,i,s){const{open:r,high:n,low:o,close:a}=e,l=Math.min(1,(t+1)/i),h=r-o<n-r||r-o===n-r&&a>=r?[r,o,n,a]:[r,n,o,a];let c=l*(Math.abs(h[1]-h[0])+Math.abs(h[2]-h[1])+Math.abs(h[3]-h[2])),d=r,u=r,_=r;for(let e=1;e<h.length&&c>0;e++){const t=Math.abs(h[e]-h[e-1]);c>=t?(d=h[e],c-=t):(d=h[e-1]+Math.sign(h[e]-h[e-1])*c,c=0),d>u&&(u=d),d<_&&(_=d)}const p={time:e.time,open:r,high:u,low:_,close:d};if(void 0!==e.volume&&Number.isFinite(e.volume)){const t=e.volume*l;p.volume=Number.isInteger(e.volume)?Math.floor(t):t}return void 0!==s&&(p.oi=s),bl(p,e)}function wl(e,t){return e.map((i,s)=>{const r=i.time,n=t(i,s),o=e[s+1]?.time;if(!Number.isFinite(r)||!Number.isFinite(n)||n<r||s>0&&r<=e[s-1].time||void 0!==o&&n>o)throw new Error("openalgo-charts: replay time must be finite, ordered and within its candle interval");return n})}class xl{constructor(e,t,i,s=0){if(Object.defineProperty(this,"points",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"ends",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"steps",{enumerable:!0,configurable:!0,writable:!0,value:[]}),this.ends=wl(e,i.barEndTime),t.length&&!i.subBarEndTime)throw new Error("openalgo-charts: replay timing needs subBarEndTime");const r=t.length?wl(t,i.subBarEndTime):[];let n=0;for(let i=0;i<e.length;i++){const o=e[i],a=this.ends[i],l=this.points.length;for(;n<t.length&&t[n].time<o.time;)n++;let h,c=o.time,d=!1;for(;n<t.length&&t[n].time<a;){const e=t[n],s=r[n++];if((e.time!==c||s>a)&&(d=!0),d)continue;if(c=s,h?gl(h,e):h=vl(e,o.time),s>=a)continue;const l=bl(h,o),u=this.points[this.points.length-1];u?.index===i&&u.time===s?u.bar=l:this.points.push({time:s,index:i,subIndex:0,subSteps:0,bar:l})}const u=s>1&&this.points.length===l&&a>o.time;if(u)for(let t=1;t<s;t++){const r=yl(o,t-1,s,e[i-1]?.oi);this.points.push({time:o.time+(a-o.time)*t/s,index:i,subIndex:0,subSteps:0,bar:r,simulated:u})}this.points.push({time:a,index:i,subIndex:0,subSteps:0,bar:o,...u?{simulated:u}:{}});const _=this.points.length-l;this.steps[i]=_;for(let e=l;e<this.points.length;e++)this.points[e].subIndex=e-l,this.points[e].subSteps=_}}at(e){let t=0,i=this.points.length;for(;t<i;){const s=t+i>>>1;this.points[s].time<=e?t=s+1:i=s}return t-1}}const Sl=2147483647;function Pl(e,t){const i=setInterval(e,Math.min(Sl,Math.max(1,t)));return()=>clearInterval(i)}function Ol(e,t){const i=setTimeout(e,Math.min(Sl,t));return i.unref?.(),i}const kl=()=>"undefined"!=typeof performance?performance.now():0;function Il(e,t){let i=0,s=e.length;for(;i<s;){const r=i+s>>1;e[r].time<=t?i=r+1:s=r}return i}class Ml{constructor(e,t={}){Object.defineProperty(this,"_chart",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_series",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_bars",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_restore",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_restoreConfirmation",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_view",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_onFrame",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_now",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_schedule",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_barMs",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_startIndex",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_timeline",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_startTime",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_time",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_pointIndex",{enumerable:!0,configurable:!0,writable:!0,value:-1}),Object.defineProperty(this,"_subBars",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_subStart",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_subCount",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_intra",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_simSteps",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_speed",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_index",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_sub",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_playing",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_active",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_transition",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_cancel",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_interval",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_lastAdvance",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_release",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_tick",{enumerable:!0,configurable:!0,writable:!0,value:()=>{if(!this._playing)return;const e=this._now();let t=Math.floor((e-this._lastAdvance)/this._interval);t<=0||(t>10?(t=10,this._lastAdvance=e):this._lastAdvance+=t*this._interval,this._advance(t),this._atEnd()&&this._end())}}),Object.defineProperty(this,"_gone",{enumerable:!0,configurable:!0,writable:!0,value:()=>{this._transition++,this._playing=!1,this._stopTimer(),this._active=!1,this._release=void 0}}),this._chart=e;const i=t.series,s=void 0===i?[]:"setData"in i?[i]:[...i];if(0===s.length){const t=e.primarySeries?.()??null;if(null===t)throw new Error("openalgo-charts: replay needs a series to drive (pass options.series)");s.push(t)}this._series=s,this._restore=s.map(e=>e.getData()),this._restoreConfirmation=s.map(Qi),this._bars=t.bars??this._restore[0],this._view={barSpacing:e.timeScale.barSpacing,rightOffset:e.timeScale.rightOffset},this._subBars=t.subBars??[],this._simSteps=function(e){if(void 0===e)return 0;const t=e.steps;if(!Number.isInteger(t)||t<2||t>240)throw new Error("openalgo-charts: replay simulate.steps must be a whole number from 2 to 240");return t}(t.simulate),this._intra=(this._subBars.length>0||this._simSteps>0)&&this._bars.length>0;const r=this._buildBuckets();if(this._subStart=r.start,this._subCount=r.count,this._startIndex=o(Math.floor(t.startIndex??0),0,Math.max(0,this._bars.length-1)),this._index=this._startIndex,this._timeline=t.timing?new xl(this._bars,this._subBars,t.timing,this._simSteps):null,!(void 0===t.startTime||this._timeline&&Number.isFinite(t.startTime)))throw new Error("openalgo-charts: replay start time must be finite and requires timing");this._startTime=t.startTime??this._timeline?.ends[this._startIndex]??null,this._time=this._startTime,this._timeline&&(this._index=-1);const n=t.barMs??1e3;this._barMs=n>0?n:1e3;const a=t.speed??1;this._speed=a>0?a:1,this._onFrame=t.onFrame??null,this._now=t.now??kl,this._schedule=t.scheduler??Pl,!1!==t.autoStart&&(this._timeline?null!==this._startTime&&this.seekTime(this._startTime):this._apply(this._startIndex,this._steps(this._startIndex)-1))}_buildBuckets(){const e=this._bars.length,t=new Int32Array(e),i=new Int32Array(e);if(!this._intra)return{start:t,count:i};const s=this._subBars;let r=-1;for(let n=0;n<s.length;n++){const o=s[n].time;for(;r+1<e&&this._bars[r+1].time<=o;)r++,t[r]=n;r>=0&&i[r]++}return{start:t,count:i}}_steps(e){if(this._timeline)return this._timeline.steps[e]??1;if(!this._intra)return 1;const t=this._subCount[e];return t>0?t:Math.max(1,this._simSteps)}_simulated(e){return this._timeline?!!this._timeline.points[this._pointIndex]?.simulated:this._simSteps>0&&e>=0&&e<this._bars.length&&0===this._subCount[e]}_formingAt(e,t){const i=this._bars[e];if(this._subCount[e]>0){const s=this._subStart[e];return function(e,t,i,s){const r=vl(e[t],s.time);for(let s=t+1;s<=i;s++)gl(r,e[s]);return bl(r,s)}(this._subBars,s,s+t,i)}return yl(i,t,this._simSteps,this._bars[e-1]?.oi)}seek(e){if(this._timeline){const t=this._timeline.ends[o(Math.floor(e),0,Math.max(0,this._bars.length-1))];return void(void 0!==t&&this.seekTime(t))}this._apply(e,this._steps(o(Math.floor(e),0,Math.max(0,this._bars.length-1)))-1)}seekTime(e){if(!this._timeline)throw new Error("openalgo-charts: replay seekTime requires timing");if(!Number.isFinite(e))throw new Error("openalgo-charts: replay time must be finite");const t=this._timeline.at(e),i=this._time!==e;if(this._time=e,this._active&&t===this._pointIndex){if(i){const i=this._timeline.points[t];vo(this._chart,{time:i?.bar.time??Number.NEGATIVE_INFINITY,asOf:e,forming:!!i&&i.subIndex<i.subSteps-1})}return}const s=!this._active,r=this._timeline.points[t];this._active=!0,this._pointIndex=t,this._index=r?.index??-1,this._sub=r?.subIndex??0;const n=this._bars.slice(0,this._index+1);r&&(n[this._index]=r.bar),this._write(n,!!r&&r.subIndex<r.subSteps-1,s)}time(){return this._timeline?this._time:this.state().bar?.time??null}timePoints(){if(!this._timeline)throw new Error("openalgo-charts: replay timePoints requires timing");return this._timeline.points.map(e=>e.time)}step(e=1){this._advance(e)}stepBack(e=1){this._advance(-e)}_advance(e){if(this._timeline){if(!Number.isFinite(e))throw new Error("openalgo-charts: replay step must be finite");const t=Math.trunc(e);if(0===t||t<0&&this._pointIndex<0)return;const i=o(this._pointIndex+t,0,this._timeline.points.length-1),s=this._timeline.points[i];return void(s&&this.seekTime(s.time))}if(!this._intra)return void this._apply(this._index+e,0);let t=this._index,i=this._sub;const s=this._bars.length-1;for(let r=0;r<Math.abs(e);r++)if(e>0)if(i+1<this._steps(t))i++;else{if(!(t<s))break;t++,i=0}else if(i>0)i--;else{if(!(t>0))break;t--,i=this._steps(t)-1}this._apply(t,i)}play(e={}){const t=e.speed;void 0!==t&&t>0&&(this._speed=t),0!==this._bars.length&&(this._timeline?null!==this._time&&this.seekTime(this._time):this._apply(this._index,this._sub),this._atEnd()?this._end():(this._stopTimer(),this._interval=this._barMs/this._speed,this._lastAdvance=this._now(),this._playing=!0,this._cancel=this._schedule(this._tick,this._interval),this._chart.emit("replay:play",this.state())))}pause(){this._playing&&(this._playing=!1,this._stopTimer(),this._chart.emit("replay:pause",this.state()))}stop(){if(this._playing=!1,this._stopTimer(),!this._active)return;this._release?.(),this._release=void 0;const e=++this._transition;if(this._active=!1,this._index=this._startIndex,this._sub=0,this._timeline){this._time=this._startTime,this._pointIndex=null===this._time?-1:this._timeline.at(this._time);const e=this._timeline.points[this._pointIndex];this._index=e?.index??-1,this._sub=e?.subIndex??0}if(vo(this._chart),e!==this._transition)return;for(let t=0;t<this._series.length;t++)if(this._series[t].setData(this._restore[t],this._restoreConfirmation[t]),e!==this._transition)return;const t=this._chart.timeScale;t.setBarSpacing(this._view.barSpacing),e===this._transition&&(t.setRightOffset(this._view.rightOffset),e===this._transition&&this._chart.emit("replay:stop",this.state()))}state(){const e=this._bars.length,t=0===e?1:this._steps(this._index);let i=null;return this._timeline?i=this._timeline.points[this._pointIndex]?.bar??null:e>0&&(i=this._bars[this._index],this._intra&&this._sub<t-1&&(i=this._formingAt(this._index,this._sub))),{index:this._index,total:e,playing:this._playing,speed:this._speed,bar:i,subIndex:this._sub,subSteps:t,simulated:e>0&&this._simulated(this._index)}}_apply(e,t=0){const i=this._bars.length;if(0===i)return;const s=o(Math.floor(e),0,i-1),r=this._steps(s),n=o(Math.floor(t),0,r-1);if(this._active&&s===this._index&&n===this._sub)return;const a=!this._active;this._active=!0,this._index=s,this._sub=n;const l=this._bars.slice(0,s+1);this._intra&&n<r-1&&(l[s]=this._formingAt(s,n));const h=this._intra&&n<r-1;this._write(l,h,a)}_write(e,t,i){const s=++this._transition;i&&(this._release=this._chart.on?.("destroy",this._gone));const r=e[e.length-1]?.time??Number.NEGATIVE_INFINITY;if(vo(this._chart,{time:r,forming:t,...this._timeline&&null!==this._time?{asOf:this._time}:{}}),s!==this._transition)return;if(this._series[0].setData(e),s!==this._transition)return;const n=t?e[e.length-2]?.time??Number.NEGATIVE_INFINITY:r;for(let e=1;e<this._series.length;e++){const t=this._restore[e];if(this._series[e].setData(t.slice(0,Il(t,n))),s!==this._transition)return}if(i&&this._chart.emit("replay:start",this.state()),s!==this._transition)return;const o=this.state();this._onFrame?.(o),s===this._transition&&this._chart.emit("replay:frame",o)}_atEnd(){if(this._timeline)return this._pointIndex>=this._timeline.points.length-1;const e=this._bars.length-1;return this._index>=e&&this._sub>=this._steps(e)-1}_end(){this.pause(),this._chart.emit("replay:end",this.state())}_stopTimer(){this._cancel?.(),this._cancel=null}}class Tl extends Ml{constructor(e,t,i){super(e,{...t,autoStart:!1}),Object.defineProperty(this,"_transport",{enumerable:!0,configurable:!0,writable:!0,value:i})}state(){return{...super.state(),...this._transport()}}}const Cl=new WeakMap,jl=e=>new Error("openalgo-charts: replay group "+e);function Al(e,t){if(!Number.isFinite(e))throw jl(t+" must be finite")}function Ll(e,t){if(Al(e,t),e<=0)throw jl(t+" must be positive")}function Nl(e,t){if(null===t)return-1;let i=0,s=e.length;for(;i<s;){const r=i+s>>>1;e[r]<=t?i=r+1:s=r}return i-1}class Rl{constructor(e,t={}){if(Object.defineProperty(this,"_members",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_activeIds",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_times",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_scope",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_focusedId",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_time",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_startTime",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_active",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_playing",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_busy",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_closing",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_revision",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_speed",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_barMs",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_now",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_scheduler",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_onChange",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_cancel",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_clockRevision",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_lastAdvance",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_pending",{enumerable:!0,configurable:!0,writable:!0,value:[]}),!e.length)throw jl("needs at least one member");this._scope=t.scope??"focused",this._focusedId=t.focusedId??e[0].id,this._speed=t.speed??1,this._barMs=t.barMs??1e3,Ll(this._speed,"speed"),Ll(this._barMs,"barMs"),Ll(this._barMs/this._speed,"clock interval"),void 0!==t.startTime&&Al(t.startTime,"start time"),this._now=t.now??kl,this._scheduler=t.scheduler??Pl,this._onChange=t.onChange;const i=new Set,s=new Set;for(const t of e){if("string"!=typeof t.id||!t.id.trim()||i.has(t.id))throw jl("member id must be unique and nonempty");if(s.has(t.chart))throw jl("cannot drive the same chart twice");if(Cl.has(t.chart))throw jl("chart already has a replay group owner");if(t.chart.isDestroyed)throw jl("cannot own a destroyed chart");if(bo(t.chart))throw jl("chart already has active replay");if("function"!=typeof t.options?.timing?.barEndTime)throw jl("member needs explicit timing");i.add(t.id),s.add(t.chart)}this._validateScope(this._scope,this._focusedId,i);for(const t of e){const e={id:t.id,chart:t.chart,options:{...t.options,timing:{...t.options.timing}}};this._members.push({...e,controller:this._prepare(e),dead:!1})}this._execute(()=>{for(const e of this._members)Cl.set(e.chart,this),e.dispose=e.chart.on?.("destroy",()=>this._chartGone(e));this._rebuildTimes(),this._startTime=t.startTime??this._times[0]??null,this._apply(this._startTime)})}state(){return{active:this._active,destroyed:this._destroyed,scope:this._scope,focusedId:this._focusedId,time:this._time,index:Nl(this._times,this._time),total:this._times.length,playing:this._playing,speed:this._speed,members:this._members.map(e=>({id:e.id,active:this._activeIds.has(e.id),state:e.controller.state()}))}}seekTime(e){if(Al(e,"time"),this._check(),this._active&&e===this._time)return;const t=this._prepareEntering();this._execute(()=>{this._install(t),this._apply(e)})}seek(e){Al(e,"index"),this._check();const t=this._prepareEntering();this._execute(()=>{this._install(t);const i=this._times[Math.max(0,Math.min(this._times.length-1,Math.floor(e)))];void 0===i||this._active&&this._time===i||this._apply(i)})}step(e=1){this._move(e)}stepBack(e=1){this._move(-e)}_move(e){Al(e,"step"),this._check();const t=Math.trunc(e);if(!t)return;const i=this._prepareEntering();this._execute(()=>{this._install(i);const e=Nl(this._times,this._time);if(t<0&&e<0)return;const s=this._times[Math.max(0,Math.min(this._times.length-1,e+t))];void 0===s||this._active&&this._time===s||this._apply(s)})}setScope(e,t=this._focusedId){if(this._check(),this._validateScope(e,t,new Set(this._members.map(e=>e.id))),e===this._scope&&t===this._focusedId)return;const i=this._prepareEntering(e,t);this._execute(()=>{this._scope=e,this._focusedId=t,this._install(i)||this._rebuildTimes(),this._apply(this._time??this._times[0]??null)})}play(e={}){const t=e.speed??this._speed;Ll(t,"speed"),Ll(this._barMs/t,"clock interval"),this._check();const i=this._prepareEntering();this._execute(()=>{if(this._install(i),this._active||this._apply(this._startTime??this._times[0]??null),!this._active||this._destroyed)return;if(this._speed=t,this._atEnd())return this._emit("replay:end"),void this._notify();this._clearClock(),this._lastAdvance=this._now(),Al(this._lastAdvance,"clock"),this._playing=!0;const e=++this._clockRevision,s=this._scheduler(()=>{e===this._clockRevision&&this._tick()},this._barMs/this._speed);this._playing&&!this._destroyed&&e===this._clockRevision?this._cancel=s:s(),this._emit("replay:play"),this._notify()})}pause(){this._check(),this._playing&&this._execute(()=>{this._clearClock(),this._emit("replay:pause"),this._notify()})}stop(){this._destroyed||this._closing||!this._active||this._execute(()=>{this._cleanup(!1),this._notify()})}destroy(){this._destroyed||this._closing||this._execute(()=>{this._cleanup(!0),this._notify()})}_validateScope(e,t,i){if("all"!==e&&"focused"!==e)throw jl("scope must be focused or all");if(!i.has(t))throw jl("focus must name a member")}_prepare(e){const t=e.options.onFrame;return new Tl(e.chart,{...e.options,onFrame:t?e=>this._pending.push({callback:t,state:e}):void 0},()=>({playing:this._playing&&this._activeIds.has(e.id),speed:this._speed}))}_prepareEntering(e=this._scope,t=this._focusedId){const i=new Map;for(const s of this._members)if(("all"===e||s.id===t)&&!this._activeIds.has(s.id)){if(s.dead||s.chart.isDestroyed)throw jl("cannot enter a destroyed chart");if(bo(s.chart))throw jl("chart already has active replay");i.set(s,this._prepare(s))}return i}_install(e){if(!e.size)return!1;for(const[t,i]of e)t.controller=i;return this._rebuildTimes(),!0}_wanted(e){return"all"===this._scope||e.id===this._focusedId}_rebuildTimes(){const e=new Set;for(const t of this._members)if(this._wanted(t))for(const i of t.controller.timePoints())e.add(i);this._times=[...e].sort((e,t)=>e-t)}_check(){if(this._destroyed)throw jl("has been destroyed");if(this._busy)throw jl("cannot change transport during a member frame");if(this._members.some(e=>this._activeIds.has(e.id)&&e.chart.isDestroyed))throw this.destroy(),jl("active chart was destroyed")}_apply(e){const t=++this._revision,i=this._activeIds;this._busy=!0,this._pending=[],this._time=e,this._active=!0,this._activeIds=new Set(this._members.filter(e=>this._wanted(e)).map(e=>e.id));try{for(const e of this._members){if(t!==this._revision)return;i.has(e.id)&&!this._activeIds.has(e.id)&&e.controller.stop()}for(const i of this._members){if(t!==this._revision)return;this._activeIds.has(i.id)&&i.controller.seekTime(e??0)}}finally{this._busy=!1}const s=this._pending;this._pending=[];for(const e of s){if(t!==this._revision)return;e.callback(e.state)}t===this._revision&&(this._playing&&this._atEnd()?(this._clearClock(),this._emit("replay:pause"),this._emit("replay:end")):this._playing&&this._emit("replay:play",e=>!i.has(e.id)),t===this._revision&&this._notify())}_atEnd(){return!this._times.length||Nl(this._times,this._time)>=this._times.length-1}_tick(){this._playing&&this._execute(()=>{const e=this._now();Al(e,"clock");const t=this._barMs/this._speed;if(e<this._lastAdvance)return void(this._lastAdvance=e);let i=Math.floor((e-this._lastAdvance)/t);i<=0||(i>10?(i=10,this._lastAdvance=e):this._lastAdvance+=i*t,this._move(i))})}_clearClock(){this._playing=!1,this._clockRevision++;const e=this._cancel;this._cancel=null,e?.()}_emit(e,t=()=>!0){const i=this._revision;for(const s of this._members){if(i!==this._revision)return;this._activeIds.has(s.id)&&t(s)&&s.chart.emit(e,s.controller.state())}}_notify(){this._onChange?.(this.state())}_execute(e){try{e()}catch(e){try{this._cleanup(!0)}catch{}throw e}}_chartGone(e){e.dead=!0,this._closing||this._destroyed||(this._activeIds.has(e.id)||e.id===this._focusedId?this.destroy():this._execute(()=>{const t=this._busy;t||this._revision++,e.dispose?.(),Cl.delete(e.chart),this._members=this._members.filter(t=>t!==e),this._rebuildTimes(),t||this._notify()}))}_cleanup(e){if(this._closing)return;this._closing=!0,this._revision++;const t=this._activeIds;let i;this._activeIds=new Set,this._active=!1,this._pending=[];const s=e=>{try{e()}catch(e){i??(i=e)}};s(()=>this._clearClock());for(const e of this._members)t.has(e.id)&&(e.dead||e.chart.isDestroyed?s(()=>vo(e.chart)):s(()=>e.controller.stop()));e||(e=void 0!==i||this._members.some(e=>e.dead&&(t.has(e.id)||e.id===this._focusedId)));for(const t of this._members)(e||t.dead)&&(s(()=>t.dispose?.()),Cl.get(t.chart)===this&&Cl.delete(t.chart));if(this._time=this._startTime,e?(this._destroyed=!0,this._members=[],this._times=[]):(this._members=this._members.filter(e=>!e.dead),this._rebuildTimes()),this._closing=!1,void 0!==i)throw i}}const Fl=.55;class zl{constructor(){Object.defineProperty(this,"_index",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null})}attached(e){this._host=e}detached(){this._host=null}zOrder(){return"top"}index(){return this._index}setIndex(e){e!==this._index&&(this._index=e,this._host?.requestUpdate())}draw(e,t){const i=this._index;if(null===i)return;const s=pi(t.theme,void 0,t.dpr);fi(e,t.timeScale.indexToX(i),null,t.plotWidth,t.plotHeight,t.dpr,ie(s.color,.55),s.width,s.dash)}}function El(e,t,i="nearest"){if(!Number.isFinite(t)||0===e.length)return null;const s=e.indexToTime(0),r=e.indexToTime(e.length-1);if(void 0===s||void 0===r)return null;if(t<s||t>r)return null;const n=e.timeToIndex(t);if(void 0!==n)return n;if("hide"===i)return null;const o=Math.floor(e.timeToIndexFloat(t));return void 0===e.indexToTime(o)?null:Math.max(0,Math.min(o,e.length-1))}function Bl(e,t,i){if(0===e.length||t.length<2)return null;const s=e.indexToTimeFloat(i.from),r=e.indexToTimeFloat(i.to);if(!Number.isFinite(s)||!Number.isFinite(r)||r<=s)return null;const n=t.timeToIndexFloat(s),o=t.timeToIndexFloat(r);return!Number.isFinite(n)||!Number.isFinite(o)||o<=n?null:{from:n,to:o}}const $l={crosshair:!0,viewport:!0,symbol:!1,interval:!1,chartType:!1,appearance:!1,drawings:!1,whenMissing:"nearest"},Dl=["interval","chartType"],Wl={interval:"onInterval",chartType:"onChartType"};function ql(e){return!0!==e.isDestroyed&&e.panes().length>0}function Vl(e){return"string"==typeof e&&e.trim().length>0}class Hl{constructor(e={}){Object.defineProperty(this,"_members",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_options",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_broadcasting",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_symbol",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_tokens",{enumerable:!0,configurable:!0,writable:!0,value:{interval:null,chartType:null}}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._options={...$l,...e}}options(){return{...this._options}}setOptions(e){const t=this._options;if(this._options={...t,...e},t.crosshair&&!this._options.crosshair)for(const e of this._members)this._detachCrosshairs(e);!t.symbol&&this._options.symbol&&this._convergeSymbol();for(const e of Dl)!t[e]&&this._options[e]&&this._convergeToken(e);const i=!0===this._options.drawings;if(t.drawings!==i)for(const e of[...this._members])this._share(e,i)}members(){return this._prune(),this._members.map(e=>e.chart)}has(e){return null!==this._find(e)}symbol(){return this._symbol}interval(){return this._tokens.interval}chartType(){return this._tokens.chartType}crosshairIndex(e){return this._find(e)?.crosshairs[0]?.index()??null}add(e,t={}){if(this._destroyed||!ql(e))return;const i=this._find(e);if(null!==i){void 0!==t.symbol&&(i.symbol=t.symbol),void 0!==t.onSymbol&&(i.onSymbol=t.onSymbol);for(const e of Dl){const s=t[e],r=t[Wl[e]];Vl(s)&&(i.tokens[e]=s),void 0!==r&&(i.follow[e]=r)}void 0!==t.appearance&&(i.appearance=t.appearance),void 0!==t.drawings&&t.drawings!==i.drawings&&(this._share(i,!1),i.drawings=t.drawings);for(const e of Dl)this._options[e]&&this._convergeToken(e);return void this._share(i,!0===this._options.drawings)}const s={chart:e,appearance:t.appearance??null,symbol:t.symbol??null,onSymbol:t.onSymbol??null,tokens:{interval:Vl(t.interval)?t.interval:null,chartType:Vl(t.chartType)?t.chartType:null},follow:{interval:t.onInterval??null,chartType:t.onChartType??null},drawings:t.drawings??null,sharing:!1,unsubscribe:[],crosshairs:[]};s.unsubscribe.push(e.on("crosshair:move",e=>this._onCrosshair(s,e)),e.on("pan",()=>this._onViewport(s)),e.on("zoom",()=>this._onViewport(s)),e.on("symbol",e=>this._onSymbolEvent(s,e)),e.on("style:change",()=>this.syncAppearance(e)),...Dl.map(t=>e.on(t,e=>{const i="string"==typeof e?e:e?.[t];Vl(i)&&this._applyToken(s,t,i)})),e.on("destroy",()=>this._prune())),this._members.push(s),null===this._symbol&&null!==s.symbol&&(this._symbol=s.symbol),this._options.symbol&&this._convergeSymbol();for(const e of Dl)null===this._tokens[e]&&(this._tokens[e]=s.tokens[e]),this._options[e]&&this._convergeToken(e);this._share(s,!0===this._options.drawings)}remove(e){const t=this._members.findIndex(t=>t.chart===e);if(t<0)return;const[i]=this._members.splice(t,1);this._release(i)}setSymbol(e,t){const i=this._find(e);null!==i&&this._applySymbol(i,t)}setInterval(e,t){this._report(e,"interval",t)}setChartType(e,t){this._report(e,"chartType",t)}syncAppearance(e){if(this._destroyed||this._broadcasting||!this._options.appearance||!ql(e))return;const t=this._find(e);if(null===t?.appearance||null===t)return;const i=$a(t.appearance.read());0!==Object.keys(i).length&&this._broadcast(t,e=>e.appearance?.apply({...i}))}destroy(){this._destroyed=!0;for(const e of this._members.splice(0))this._release(e);this._symbol=null,this._tokens.interval=null,this._tokens.chartType=null}_onCrosshair(e,t){if(!this._options.crosshair)return;const i=t?.time??null;this._broadcast(e,e=>{const t=null===i?null:El(e.chart.dataLayer,i,this._options.whenMissing);this._setCrosshair(e,t)},e=>{this._setCrosshair(e,null)})}_onViewport(e){if(!this._options.viewport)return;if(!ql(e.chart))return void this._prune();const t=e.chart.getVisibleLogicalRange(),i=e.chart.dataLayer;this._broadcast(e,e=>{const s=Bl(i,e.chart.dataLayer,t);null!==s&&e.chart.setVisibleLogicalRange(s)})}_onSymbolEvent(e,t){const i="string"==typeof t?t:t?.symbol;"string"==typeof i&&0!==i.length&&this._applySymbol(e,i)}_applySymbol(e,t){this._broadcasting||this._destroyed||!ql(e.chart)||(e.symbol=t,this._symbol=t,this._options.symbol&&this._broadcast(e,e=>{e.symbol!==t&&(e.symbol=t,e.onSymbol?.(t,e.chart))}))}_convergeSymbol(){const e=this._symbol;null!==e&&this._options.symbol&&this._broadcast(null,t=>{t.symbol!==e&&(t.symbol=e,t.onSymbol?.(e,t.chart))})}_report(e,t,i){const s=this._find(e);null!==s&&Vl(i)&&this._applyToken(s,t,i)}_applyToken(e,t,i){this._broadcasting||this._destroyed||!ql(e.chart)||(e.tokens[t]=i,this._tokens[t]=i,this._options[t]&&this._broadcast(e,e=>this._followToken(e,t,i)))}_followToken(e,t,i){const s=e.follow[t];e.tokens[t]!==i&&null!==s&&!1!==s(i,e.chart)&&(e.tokens[t]=i)}_convergeToken(e){const t=this._tokens[e];null!==t&&this._options[e]&&this._broadcast(null,i=>this._followToken(i,e,t))}_share(e,t){null!==e.drawings&&e.sharing!==t&&(!t||!this._destroyed&&this._members.includes(e)&&ql(e.chart))&&(e.sharing=t,t?e.drawings.join():e.drawings.leave())}_broadcast(e,t,i){if(!this._broadcasting){this._broadcasting=!0;try{this._prune(),null!==e&&this._members.includes(e)&&i?.(e);for(const i of[...this._members])i!==e&&this._members.includes(i)&&ql(i.chart)&&t(i)}finally{this._broadcasting=!1}}}_prune(){for(let e=this._members.length-1;e>=0;e--){const t=this._members[e];void 0===t||ql(t.chart)||(this._members.splice(e,1),this._share(t,!1))}}_find(e){return this._members.find(t=>t.chart===e)??null}_release(e){for(const t of e.unsubscribe)t();e.unsubscribe.length=0,this._detachCrosshairs(e),this._share(e,!1)}_setCrosshair(e,t){if(!ql(e.chart))return;const i=null===t?e.crosshairs.length:e.chart.panes().length;for(;e.crosshairs.length>i;){const t=e.crosshairs.pop();void 0!==t&&e.chart.removePrimitive(t)}for(;e.crosshairs.length<i;){const t=new zl;e.chart.addPrimitive(t,e.crosshairs.length),e.crosshairs.push(t)}for(const i of e.crosshairs)i.setIndex(t);e.chart.setLinkedCrosshairIndex?.(t)}_detachCrosshairs(e){if(ql(e.chart)){for(const t of e.crosshairs)e.chart.removePrimitive(t);e.chart.setLinkedCrosshairIndex?.(null)}e.crosshairs.length=0}}function Gl(e={}){return new Hl(e)}function Ul(e,t){return{...e,high:Math.max(e.high,t.high),low:Math.min(e.low,t.low),volume:void 0===e.volume&&void 0===t.volume?void 0:Math.max(e.volume??0,t.volume??0)}}const Kl={intervalSec:60,volumeMode:"ltq-sum",lateTickPolicy:"foldIntoBar",sessionAnchorSec:0};class Yl{constructor(e={}){Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_current",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_cumAtBarStart",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_lastCum",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_hasCum",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_streamed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_provisional",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._opts={...Kl,...e}}seed(e,t){this._current={...e},this._hasCum=!1,this._streamed=!1,this._provisional=!1,void 0!==t&&(this._lastCum=t,this._cumAtBarStart=t-(e.volume??0),this._hasCum=!0)}current(){return null===this._current?null:{...this._current}}isProvisional(){return null!==this._current&&this._provisional}reconcile(e){const t=this._current;if(null===t||e.time!==t.time)return null;const i=Ul(t,e);this._provisional&&(i.open=e.open);const s=i.volume;return"day-delta"===this._opts.volumeMode&&this._hasCum&&void 0!==s&&(this._cumAtBarStart=this._lastCum-s),this._current=i,this._provisional=!1,{...i}}bucketStart(e){const t=this._opts.sessionAnchorSec,i=this._opts.intervalSec;return t+Math.floor((e-t)/i)*i}onTick(e){const t=this.bucketStart(e.time);if(null!==this._current&&t<this._current.time)return"dropOlderThanPrevBar"===this._opts.lateTickPolicy?null:(this._foldInto(this._current,e),this._streamed=!0,{bar:{...this._current},isNew:!1,provisional:this._provisional});if(null===this._current||t>this._current.time){const i=null===this._current||!this._streamed,s=this._volumeForNewBar(e),r={time:t,open:e.price,high:e.price,low:e.price,close:e.price,volume:s};return void 0!==e.oi&&Number.isFinite(e.oi)&&(r.oi=e.oi),this._current=r,this._streamed=!0,this._provisional=i,{bar:{...r},isNew:!0,provisional:i}}return this._foldInto(this._current,e),this._streamed=!0,{bar:{...this._current},isNew:!1,provisional:this._provisional}}_foldInto(e,t){t.price>e.high&&(e.high=t.price),t.price<e.low&&(e.low=t.price),e.close=t.price,e.volume=this._volumeForSameBar(e,t),void 0!==t.oi&&Number.isFinite(t.oi)?e.oi=t.oi:delete e.oi}_volumeForNewBar(e){if("ltq-sum"===this._opts.volumeMode)return e.ltq??0;const t=e.cumDayVolume??0;return this._hasCum?t<this._lastCum?this._cumAtBarStart=0:this._cumAtBarStart=this._lastCum:this._cumAtBarStart=t,this._lastCum=t,this._hasCum=!0,Math.max(0,t-this._cumAtBarStart)}_volumeForSameBar(e,t){if("ltq-sum"===this._opts.volumeMode)return(e.volume??0)+(t.ltq??0);const i=t.cumDayVolume??0;return this._hasCum||(this._cumAtBarStart=i-(e.volume??0),this._hasCum=!0),this._lastCum=i,Math.max(0,i-this._cumAtBarStart)}}function Xl(e){const t={top:2,normal:1,bottom:0};let i=null;for(const s of e)null!==s&&(null===i||s.distance<i.distance||s.distance===i.distance&&t[s.zOrder]>t[i.zOrder])&&(i=s);return i}const Jl=["previousClose","sessionHigh","sessionLow","lastPrice","preMarketOpen","preMarketClose","postMarketOpen","postMarketClose","bid","ask"];const Zl={previousClose:{line:!0,label:!0,lineStyle:"dashed"},sessionHigh:{line:!1,label:!1,lineStyle:"dashed"},sessionLow:{line:!1,label:!1,lineStyle:"dashed"},lastPrice:{line:!1,label:!1,lineStyle:"dashed"},preMarketOpen:{line:!1,label:!1,lineStyle:"dotted"},preMarketClose:{line:!1,label:!1,lineStyle:"dotted"},postMarketOpen:{line:!1,label:!1,lineStyle:"dotted"},postMarketClose:{line:!1,label:!1,lineStyle:"dotted"},bid:{line:!1,label:!1,lineStyle:"dotted"},ask:{line:!1,label:!1,lineStyle:"dotted"}};function Ql(e){return"number"==typeof e&&Number.isFinite(e)?e:null}const eh=new WeakMap;function th(e){const t={previousClose:null,sessionHigh:null,sessionLow:null,lastPrice:null,preMarketOpen:null,preMarketClose:null,postMarketOpen:null,postMarketClose:null,bid:null,ask:null},i=e.quote;null!=i&&(t.bid=Ql(i.bid),t.ask=Ql(i.ask));const s=e.bars,r=s.length;if(0===r)return t;t.lastPrice=Ql(s[r-1].close);const n=function(e,t){const i=e.length;if(void 0===t||!Number.isFinite(t))return i-1;if(t>=e[i-1].time)return i-1;if(t<e[0].time)return-1;let s=0,r=i-1;for(;s<r;){const i=s+r+1>>1;e[i].time<=t?s=i:r=i-1}return s}(s,e.anchorTime);if(n<0)return t;const o=function(e,t){const i=e.length;if(0===i)return[];const s=e[0].time,r=e[i-1].time,n=eh.get(e);if(void 0!==n&&n.len===i&&n.first===s&&n.last===r&&n.zone===t)return n.starts;const o=new Array(i);for(let t=0;t<i;t++)o[t]=e[t].time;const a=Ft(o,t),l=[0];for(let e=1;e<i;e++)a[e]&&l.push(e);return eh.set(e,{len:i,first:s,last:r,zone:t,starts:l}),l}(s,e.timezone??ht),a=function(e,t){let i=0,s=e.length-1;for(;i<s;){const r=i+s+1>>1;e[r]<=t?i=r:s=r-1}return i}(o,n),l=o[a],h=a+1<o.length?o[a+1]-1:r-1;let c=-1/0,d=1/0;for(let e=l;e<=h;e++){const t=s[e];Number.isFinite(t.high)&&t.high>c&&(c=t.high),Number.isFinite(t.low)&&t.low<d&&(d=t.low)}if(c>-1/0&&(t.sessionHigh=c),d<1/0&&(t.sessionLow=d),a>0){const e=o[a-1];for(let i=l-1;i>=e;i--){const e=Ql(s[i].close);if(null!==e){t.previousClose=e;break}}}const u=e.marketPhase;if(null!=u)for(let e=l;e<=h;e++){const i=s[e],r=u(i);if("pre"===r){null===t.preMarketOpen&&(t.preMarketOpen=Ql(i.open));const e=Ql(i.close);null!==e&&(t.preMarketClose=e)}else if("post"===r){null===t.postMarketOpen&&(t.postMarketOpen=Ql(i.open));const e=Ql(i.close);null!==e&&(t.postMarketClose=e)}}return t}function ih(e,t,i){switch(e){case"sessionHigh":return t.upColor;case"sessionLow":return t.downColor;case"bid":return t.buy;case"ask":return t.sell;case"lastPrice":return i?t.lastPriceUp:t.lastPriceDown;default:return t.crosshair}}function sh(e){return{line:!1!==e.priceLineVisible,label:!1!==e.lastValueVisible}}function rh(e){return{priceLineVisible:e.line,lastValueVisible:e.label}}class nh{constructor(e={}){Object.defineProperty(this,"_levels",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_timezone",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_phase",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_quote",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_values",{enumerable:!0,configurable:!0,writable:!0,value:{previousClose:null,sessionHigh:null,sessionLow:null,lastPrice:null,preMarketOpen:null,preMarketClose:null,postMarketOpen:null,postMarketClose:null,bid:null,ask:null}}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._levels={...Zl};for(const t of Jl){const i=e.levels?.[t];void 0!==i&&(this._levels[t]={...Zl[t],...i})}this._timezone=e.timezone??ht,this._phase=e.marketPhase??null,this._quote=e.quote??null}attached(e){this._host=e}detached(){this._host=null}zOrder(){return"normal"}level(e){return this._levels[e]}setLevel(e,t){this._levels[e]={...this._levels[e],...t},this._host?.requestUpdate()}setOptions(e){for(const t of Jl){const i=e.levels?.[t];void 0!==i&&(this._levels[t]={...this._levels[t],...i})}void 0!==e.timezone&&(this._timezone=e.timezone),void 0!==e.marketPhase&&(this._phase=e.marketPhase),void 0!==e.quote&&(this._quote=e.quote),this._host?.requestUpdate()}setQuote(e){this._quote=e,this._host?.requestUpdate()}values(){return this._values}available(e){return null!==this._values[e]}draw(e,t){const i=t.bars?.()??[],s=t.dataLayer.indexToTimeFloat(t.timeScale.visibleRange().to);this._values=th({bars:i,anchorTime:s,timezone:this._timezone,marketPhase:this._phase,quote:"function"==typeof this._quote?this._quote()??null:this._quote});const r=t.dpr,n=Math.round(t.plotWidth*r),o=t.plotHeight*r,a=i.length>0?i[i.length-1]:null,l=null!==a&&a.close>=a.open;e.save(),e.font=`500 ${11*r}px system-ui, sans-serif`,e.textBaseline="middle",e.textAlign="left";for(const i of Jl){const s=this._levels[i];if(!s.line&&!s.label)continue;const a=this._values[i];if(null===a)continue;const h=Math.round(t.priceScale.priceToY(a)*r)+.5;if(h<0||h>o)continue;const c=s.color??ih(i,t.theme,l);if(s.line){const i=Math.max(0,Math.min(1,s.extentFromRight??1)),o=Math.round(t.plotWidth*(1-i)*r);e.strokeStyle=c,e.lineWidth=Math.max(1,Math.round((s.lineWidth??1)*r)),e.setLineDash(z(s.lineStyle,r)),e.beginPath(),e.moveTo(o,h),e.lineTo(n,h),e.stroke(),e.setLineDash([])}s.label&&"hidden"!==t.priceAxisSide&&t.priceAxisWidth>0&&Rn(e,t,h,s.text??t.priceScale.format(a),c,Q(c),16*r,!0)}e.restore()}}class oh{constructor(e){Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._opts={index:e.index,color:e.color??"rgba(12,14,20,0.62)",lineColor:e.lineColor??"#3b82f6",lineWidth:e.lineWidth??1,lineVisible:e.lineVisible??!0,zOrder:e.zOrder??"top",id:e.id??"replay-shade"}}attached(e){this._host=e}detached(){this._host=null}zOrder(){return this._opts.zOrder}autoscaleInfo(){return null}hitTest(){return null}setOptions(e){this._opts={...this._opts,...e},this._host?.requestUpdate()}draw(e,t){const i=this._opts;if(null===i.index)return;const s=t.dpr,r=t.plotWidth*s,n=t.plotHeight*s;if(r<=0||n<=0)return;const o=t.timeScale.barSpacing*s/2,a=Math.round(t.timeScale.indexToX(i.index)*s+o);if(a>=r)return;const l=Math.max(0,a);e.save(),e.fillStyle=i.color,e.fillRect(l,0,r-l,n),i.lineVisible&&a>=0&&(e.strokeStyle=i.lineColor,e.lineWidth=Math.max(1,Math.round(i.lineWidth*s)),e.beginPath(),e.moveTo(a+.5,0),e.lineTo(a+.5,n),e.stroke()),e.restore()}}function ah(e,t,i,s){for(;i<=s;){const r=i+s>>1;e.indexToTime(r)<t?i=r+1:s=r-1}return i}class lh{constructor(e={}){Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_cache",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._opts={...e}}attached(e){this._host=e}detached(){this._host=null}zOrder(){return"bottom"}autoscaleInfo(){return null}hitTest(){return null}hitBounds(){return null}options(){return{...this._opts}}setOptions(e){this._opts={...this._opts,...e},this._cache=null,this._host?.requestUpdate()}draw(e,t){const i=this._opts,s=t.dataLayer,r=s.length;if(!1===i.visible||r<2)return;const n=s.sessionCalendar,o=i.calendar??(n?.phaseSpans?n:null);if(null===o)return;const a=t.timeScale.visibleRange(),l=Math.max(0,Math.floor(a.from)),h=Math.min(r-1,Math.ceil(a.to));if(l>h||!function(e,t,i){const s=[];for(let r=Math.max(1,t),n=Math.max(r,Math.min(i,t+15));r<=n;r++)s.push(e.indexToTime(r)-e.indexToTime(r-1));return s.sort((e,t)=>e-t),s[s.length-1>>1]<72e3}(s,l,h))return;const c=s.indexToTime(l),d=s.indexToTime(h)+1;if(d-c>7776e3)return;const u=this._spans(o,c,d),[_,p,f]=function(e){const t=Z(e);return null===t?.5:te(t)}(t.theme.background)<.5?[.1,.13,Ui]:[.07,.1,Ki],m=(e,i)=>ie(Z(t.theme[e])?t.theme[e]:f[e],i),b={pre:void 0!==i.preColor?i.preColor:m("lineColor",_),post:void 0!==i.postColor?i.postColor:ie("#f59e0b",_),extended:void 0!==i.extendedColor?i.extendedColor:m("axisText",p)},g=t.dpr,v=t.plotWidth*g,y=t.plotHeight*g,w=e=>Math.min(v,Math.max(0,Math.round(t.timeScale.indexToX(e)*g)));e.save();for(const t of u){const i=b[t.phase];if(!i||t.end<=c||t.start>=d)continue;const r=ah(s,t.start,l,h),n=ah(s,t.end,l,h)-1;if(r>n)continue;const o=w(r-.5);e.fillStyle=i,e.fillRect(o,0,w(n+.5)-o,y)}e.restore()}_spans(e,t,i){const s=this._cache;if(s&&s.source===e&&s.from<=t&&s.to>=i)return s.spans;const r=Math.max(86400,i-t);let n;try{n=e.phaseSpans(t-r,i+r)}catch{n=[]}return this._cache={source:e,from:t-r,to:i+r,spans:n},n}}const hh=new WeakMap;function ch(e,t={}){const i=hh.get(e);if(i)return e.isDestroyed||e.panes().some(e=>e.primitives().includes(i.shade))||e.addPrimitive(i.shade,{anchor:"primary-pane"}),i.shading.setOptions(t),i.shading;const s=new lh(t);e.addPrimitive(s,{anchor:"primary-pane"});const r={setOptions:e=>s.setOptions(e),options:()=>s.options(),destroy:()=>{hh.get(e)?.shading===r&&(hh.delete(e),e.isDestroyed||e.removePrimitive(s))}};return hh.set(e,{shading:r,shade:s}),r}class dh{constructor(e={}){Object.defineProperty(this,"_id",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_position",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_mx",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_my",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_scale",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_btnW",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_qtyW",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_h",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_buyLabel",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_sellLabel",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_showPrices",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_qty",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_buyColor",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_sellColor",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_bid",{enumerable:!0,configurable:!0,writable:!0,value:NaN}),Object.defineProperty(this,"_ask",{enumerable:!0,configurable:!0,writable:!0,value:NaN}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_sellRect",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_buyRect",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_qtyRect",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._id=e.id??"trade",this._position=e.position??"top-left";const t=e.margin??12;this._mx="number"==typeof t?t:t.x,this._my="number"==typeof t?t:t.y,this._buyLabel=e.buyLabel??"BUY",this._sellLabel=e.sellLabel??"SELL",this._showPrices=e.showPrices??!0,this._scale=Math.max(.6,Math.min(1.5,e.scale??1)),this._btnW=74*this._scale,this._qtyW=40*this._scale,this._h=42*this._scale,this._qty=void 0!==e.qty?String(e.qty):"",this._buyColor=e.buyColor,this._sellColor=e.sellColor}attached(e){this._host=e}detached(){this._host=null}zOrder(){return"top"}autoscaleInfo(){return null}setPrices(e,t){this._bid=e,this._ask=t,this._host?.requestUpdate()}setMark(e){this.setPrices(e,e)}setQty(e){this._qty=String(e),this._host?.requestUpdate()}setColors(e,t){this._buyColor=e,this._sellColor=t,this._host?.requestUpdate()}_origin(e,t){const i=2*this._btnW+this._qtyW+2,s=this._mx,r=e-this._mx-i,n=this._my,o=t-this._my-this._h;switch(this._position){case"top-right":return{x:r,y:n,w:i};case"bottom-left":return{x:s,y:o,w:i};case"bottom-right":return{x:r,y:o,w:i};case"center":return{x:(e-i)/2,y:(t-this._h)/2,w:i};default:return{x:s,y:n,w:i}}}draw(e,t){this._sellRect=this._buyRect=this._qtyRect=null;const i=t.dpr,s=this._origin(t.plotWidth,t.plotHeight),r=this._sellColor??t.theme.sell,n=this._buyColor??t.theme.buy;this._sellRect={x:s.x,y:s.y,w:this._btnW,h:this._h},this._qtyRect={x:s.x+this._btnW+1,y:s.y,w:this._qtyW,h:this._h},this._buyRect={x:s.x+this._btnW+this._qtyW+2,y:s.y,w:this._btnW,h:this._h},e.save(),e.shadowColor="rgba(0,0,0,0.35)",e.shadowBlur=8*i,e.shadowOffsetY=2*i;const o=(e,i)=>null!==e&&t.hoverId===`${this._id}:${i}`;this._drawButton(e,this._sellRect,i,o(this._sellRect,"sell")?re(r,.12):r,this._sellLabel,this._showPrices?this._fmt(t,this._bid):"","left");const a="transparent"===t.theme.background?re(r,-.6):t.theme.grid;this._drawChip(e,this._qtyRect,i,a,this._qty||"-",t.theme.axisText),this._drawButton(e,this._buyRect,i,o(this._buyRect,"buy")?re(n,.12):n,this._buyLabel,this._showPrices?this._fmt(t,this._ask):"","right"),e.restore()}_fmt(e,t){return Number.isNaN(t)?"":e.priceScale.format(t)}_drawButton(e,t,i,s,r,n,o){const a=7*this._scale*i;e.beginPath(),this._roundedSide(e,t.x*i,t.y*i,t.w*i,t.h*i,a,o),e.fillStyle=s,e.fill();const l=Q(s);e.textAlign="center";const h=(t.x+t.w/2)*i;""!==n?(e.fillStyle=l,e.font=10*this._scale*i+"px system-ui, sans-serif",e.textBaseline="alphabetic",e.fillText(n,h,(t.y+t.h*(18/42))*i),e.font=`700 ${12*this._scale*i}px system-ui, sans-serif`,e.fillText(r,h,(t.y+t.h*(33/42))*i)):(e.fillStyle=l,e.font=`700 ${13*this._scale*i}px system-ui, sans-serif`,e.textBaseline="middle",e.fillText(r,h,(t.y+t.h/2)*i))}_drawChip(e,t,i,s,r,n){e.beginPath(),e.rect(t.x*i,t.y*i,t.w*i,t.h*i),e.fillStyle=s,e.fill(),e.fillStyle=n,e.font=`600 ${13*this._scale*i}px system-ui, sans-serif`,e.textAlign="center",e.textBaseline="middle",e.fillText(r,(t.x+t.w/2)*i,(t.y+t.h/2)*i)}_roundedSide(e,t,i,s,r,n,o){const a=e;if("function"==typeof a.roundRect){const e="left"===o?[n,0,0,n]:[0,n,n,0];a.roundRect(t,i,s,r,e)}else ne(e,t,i,s,r,n)}hitTest(e,t,i){const s=i=>null!==i&&e>=i.x&&e<=i.x+i.w&&t>=i.y&&t<=i.y+i.h;return s(this._buyRect)?{externalId:`${this._id}:buy`,zOrder:"top",distance:0,cursor:"pointer"}:s(this._sellRect)?{externalId:`${this._id}:sell`,zOrder:"top",distance:0,cursor:"pointer"}:s(this._qtyRect)?{externalId:`${this._id}:qty`,zOrder:"top",distance:0,cursor:"pointer"}:null}}function uh(e,t){if(t<=0)throw new Error("openalgo-charts: EMA period must be > 0");const i=[];if(0===e.length)return i;const s=2/(t+1);let r=e[0];i.push(r);for(let t=1;t<e.length;t++)r=e[t]*s+r*(1-s),i.push(r);return i}function _h(e,t){const i=uh(e.map(e=>e.close),t);return e.map((e,t)=>({time:e.time,open:i[t],high:i[t],low:i[t],close:i[t]}))}function ph(e,t=14){if(t<=0)throw new Error("openalgo-charts: RSI period must be > 0");const i=e.length,s=new Array(i).fill(NaN);if(i<=t||!Number.isInteger(t))return s;let r=NaN,n=NaN,o=!1,a=!1,l=0;for(let h=1;h<i;h++){const i=e[h]-e[h-1];if(!Number.isFinite(i)){l=0;continue}l++;if(o&&(r=(r*(t-1)+(i>0?i:0))/t),a&&(n=(n*(t-1)+(i<0?-i:0))/t),(!o||!a)&&l>=t){let i=0,s=0;for(let r=h-t+1;r<=h;r++){const t=e[r]-e[r-1];t>=0?i+=t:s-=t}!o&&Number.isFinite(i/t)&&(r=i/t,o=!0),!a&&Number.isFinite(s/t)&&(n=s/t,a=!0)}if(Number.isFinite(r)&&Number.isFinite(n)){const e=0===n?100:100-100/(1+r/n);Number.isFinite(e)&&(s[h]=e)}}return s}function fh(e,t=14){const i=ph(e.map(e=>e.close),t);return e.map((e,t)=>({time:e.time,open:i[t],high:i[t],low:i[t],close:i[t]}))}function mh(e,t,i){const s=e.length,r=new Array(s);if(0===s)return r;r[0]=e[0]-t[0];for(let n=1;n<s;n++){const s=e[n]-t[n],o=Math.abs(e[n]-i[n-1]),a=Math.abs(t[n]-i[n-1]);r[n]=Math.max(s,o,a)}return r}function bh(e,t,i,s=14){if(s<=0)throw new Error("openalgo-charts: ATR period must be > 0");const r=mh(e,t,i),n=r.length,o=new Array(n).fill(NaN);if(n<s||!Number.isInteger(s))return o;let a=NaN,l=0;for(let e=0;e<n;e++){const t=r[e];if(Number.isFinite(t)){if(l++,Number.isNaN(a)){if(l>=s){let t=0;for(let i=e-s+1;i<=e;i++)t+=r[i];Number.isFinite(t/s)&&(a=t/s)}}else a=(a*(s-1)+t)/s;Number.isFinite(a)&&(o[e]=a)}else l=0}return o}function gh(e,t=10,i=3){const s=e.length,r=e.map(()=>({value:NaN,direction:1})),n=e.map(e=>e.high),o=e.map(e=>e.low),a=e.map(e=>e.close),l=bh(n,o,a,t);let h=NaN,c=NaN,d=NaN,u=NaN,_=!1;for(let e=0;e<s;e++){if(!Number.isFinite(l[e])||!Number.isFinite(a[e]))continue;const t=(n[e]+o[e])/2,s=t+i*l[e],p=t-i*l[e],f=_?s<h||u>h?s:h:s,m=_?p>c||u<c?p:c:p;let b,g;_&&d!==h?a[e]>=m?(b=m,g=-1):(b=f,g=1):a[e]<=f?(b=f,g=1):(b=m,g=-1),r[e]={value:b,direction:g},h=f,c=m,d=b,u=a[e],_=!0}return r}function vh(e,t=10,i=3){const s=gh(e,t,i),r=(e,t)=>({time:e,open:t,high:t,low:t,close:t}),n=[],o=[];for(let t=0;t<e.length;t++){const i=s[t],a=Number.isFinite(i.value);n.push(r(e[t].time,a&&-1===i.direction?i.value:NaN)),o.push(r(e[t].time,a&&1===i.direction?i.value:NaN))}return{up:n,down:o}}function yh(e,t){return e===t||e.time===t.time&&e.open===t.open&&e.high===t.high&&e.low===t.low&&e.close===t.close&&e.volume===t.volume&&e.oi===t.oi}function wh(e,t,i,s){const r=e._host,n=r._seriesRecords.get(t),o=r._seriesOwners.get(t),a=n.dataId,l=e._transforms.get(a),h=null===i?void 0:e._newTransform(i);if(void 0===h?void 0===l:void 0!==l&&function(e,t){const i=e.options??{},s=t.options??{};return e.type===t.type&&Object.keys(i).length===Object.keys(s).length&&Object.keys(i).every(e=>i[e]===s[e])}(l.spec,h.spec))return!1;const c=l?.run.source()??r._dataLayer.seriesBars(a),d=r._seriesProvenance.get(a)?.snapshot().confirmation;return void 0===h?e._transforms.delete(a):(h.run.setData(c),e._transforms.set(a,h),l?.spec.type!==h.spec.type&&e._setSeriesType(t,Go(h.spec.type).renderer,!1)),r._hasFitContent=!1,e._setData(a,h?.run.elements()??c,void 0===d?void 0:{confirmation:d},o),s&&r._emit("objects:change",{}),!0}function xh(e,t,i,s,r,n){const o=e._host,a=o._dataLayer,l=o._timeScale,h=e._sharedAxis(),c=a.seriesBars(t),d=c.length,u=l.rightOffset>=0;let _=i.update(s);const p=i.elements();for(;_<d&&_<p.length&&yh(c[_],p[_]);)_++;const f=_===d-1&&p.length>=d,m=_>=d||f&&p[_].time===c[_].time;if(m)for(let e=_;e<p.length;e++)a.update(t,p[e]);else a.setSeriesData(t,p);const b=p[p.length-1]?.time,g=m||f&&p[_].time>c[_].time;o._seriesProvenance.get(t)?.record(g?p.length>d?"append":"replace":"correction",b,r),l.setBaseIndex(a.baseIndex),p.length===d||u||o._mutateTimeScale(()=>l.setRightOffset(l.rightOffset-(p.length-d)));const v=t===o._firstDataId.value;v&&o._studies._invalidateIndicators(),e._invalidateWrite([n.pane],h),o._updateAccessibleSummary(),v&&o._emit("data:update",{kind:"update",time:b??s.time})}function Sh(e,t){Jo={set:wh,tick:xh},function(e,t){Ho.set(e,t)}(e,t)}function Ph(e){throw new Error(`Invalid tick schedule: ${e}`)}function Oh(e,t){return null!==e&&"object"==typeof e?Object.getOwnPropertyDescriptor(e,t)?.value:void 0}function kh(e){for(let t=0;t<=12;t++){const i=e*10**t;if(i>=1&&Math.abs(i-Math.round(i))<=8*Number.EPSILON*i)return t}return-1}function Ih(e,t){const i=e/t;return Math.abs(i-Math.round(i))<=8*Number.EPSILON*Math.max(1,Math.abs(i))}function Mh(e,t){return t?Mh(t,e%t):e}class Th{constructor(e){Object.defineProperty(this,"bands",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"minMove",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_digits",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),(!Array.isArray(e)||!e.length||e.length>64)&&Ph("expected 1 to 64 bands");const t=[],i=[];for(let s=0;s<e.length;s++){const r=e[s],n=Oh(r,"tick"),o=Oh(r,"from"),a=`bands[${s}]`;if(("number"!=typeof n||!Number.isFinite(n)||n<=0||kh(n)<0)&&Ph(`${a}.tick must be a positive number with at most 12 decimals`),i.push(kh(n)),!s){void 0!==o&&Ph(`${a} covers every lower price, including zero and negatives, so it takes no from; use price limits for a floor`),t.push(Object.freeze({tick:n}));continue}"number"==typeof o&&Number.isFinite(o)||Ph(`${a}.from must be a finite number`);const l=t[s-1];void 0!==l.from&&o<=l.from&&Ph(`${a}.from ${o} must be above bands[${s-1}].from ${l.from}; bounds are strictly ascending`),Ih(o,n)||Ph(`${a}.from ${o} is not a multiple of ${a}.tick ${n}`),Ih(o,l.tick)||Ph(`${a}.from ${o} is not a multiple of bands[${s-1}].tick ${l.tick}`),t.push(Object.freeze({from:+(Math.round(o/n)*n).toFixed(i[s]),tick:n}))}const s=10**Math.max(...i),r=t.map(e=>Math.round(e.tick*s));r.some(e=>!Number.isSafeInteger(e))&&Ph("the ticks have no common grid in safe integers"),this.bands=Object.freeze(t),this._digits=i,this.minMove=+(r.reduce(Mh)/s).toFixed(Math.max(...i))}_band(e){let t=this.bands.length-1;for(;t>0&&e<this.bands[t].from;)t--;return t}_at(e,t){return+(e*this.bands[t].tick).toFixed(this._digits[t])}tickAt(e){return Number.isFinite(e)?this.bands[this._band(e)].tick:NaN}round(e){if(!Number.isFinite(e))return NaN;const t=this._band(e),{tick:i}=this.bands[t],s=Math.floor(e/i),r=+((s+.5)*i).toFixed(this._digits[t]+1);return this._at(e<r?s:s+1,t)}step(e,t){if(!Number.isInteger(t))throw new RangeError("TickSchedule.step needs a whole number of ticks");let i=this.round(e),s=t;for(;s&&Number.isFinite(i);){const e=s>0;let t=this._band(i);!e&&t&&i===this.bands[t].from&&t--;const{tick:r}=this.bands[t],n=e?this.bands[t+1]?.from:this.bands[t].from,o=void 0===n?1/0:Math.max(1,Math.round(Math.abs(n-i)/r)),a=Math.min(Math.abs(s),o);i=this._at(Math.round(i/r)+(e?a:-a),t),s+=e?-a:a}return i}}const Ch=["regular","pre","post","extended","holiday"];const jh=86400;function Ah(e,t){const i=t-172800,s=t+1209600,r=e.phaseSpans(i,s),n=r.findIndex(e=>t>=e.start&&t<e.end),o=r[n]??{phase:"closed",start:t,end:t},a=n>=0&&o.end<s?o.end:null,l=null===a?void 0:r.find(e=>e.start===a),h="regular"===o.phase?o:r.find(e=>"regular"===e.phase&&e.start>t);return{phase:o.phase,changesAt:a,nextPhase:null===a?null:l?.phase??"closed",opensAt:h&&h.start>i?h.start:null,closesAt:h&&h.end<s?h.end:null}}function Lh(e,t){return e?.phaseSpans?Ah(e,t):null}function Nh(e){let t=[],i=NaN,s=NaN,r=0;return n=>{const o=n.time;if(!(o>=i&&o<s)){if(!Number.isFinite(o))return null;i=o-jh,s=o+7*jh,r=0;try{t=e.phaseSpans(i,s)}catch{t=[]}}for(t[r]&&o<t[r].start&&(r=0);r<t.length&&o>=t[r].end;)r++;const a=t[r]?.phase;return"pre"===a||"regular"===a||"post"===a?a:null}}const Rh=e=>t=>{throw new Error(`Invalid ${e}: ${t}`)},Fh=Rh("instrument");function zh(e,t=Fh){return e&&"object"==typeof e&&!Array.isArray(e)&&[Object.prototype,null].includes(Object.getPrototypeOf(e))?Object.values(Object.getOwnPropertyDescriptors(e)).some(e=>!("value"in e))?t("accessors are not metadata"):e:t("expected a plain object")}function Eh(e,t,i=Fh){return"string"!=typeof e||!e.trim()||e.length>200||Array.from(e).some(e=>e.charCodeAt(0)<32)?i(t):e}function Bh(e,t){return"number"!=typeof e||!Number.isFinite(e)||e<=0?Fh(t):e}function $h(e,t,i=Fh){return!Array.isArray(e)||e.length>t?i("invalid list"):e.map(e=>Eh(e,"invalid list item",i))}function Dh(e,t,i="invalid session"){const s=$h(e,16,t);return s.some(e=>!$t(e))?t(i):Object.freeze(s)}function Wh(e){return e.toISOString().slice(0,10)}function qh(e,t){if(!/^\d{4}-\d{2}-\d{2}$/.test(e))return t("invalid exception date");const i=new Date(e+"T00:00:00Z");return Number.isFinite(i.getTime())&&Wh(i)===e?i:t("invalid exception date")}function Vh(e,t){const i=Eh(e,"timezone",t);return _t(i)?i:t("unknown timezone")}function Hh(e,t){const i={};if(void 0!==e.exceptions){const s=zh(e.exceptions,t);if(Object.keys(s).length>3660)return t("too many session exceptions");for(const[e,r]of Object.entries(s))qh(e,t),i[e]=Dh(r,t)}return Object.freeze(i)}function Gh(e,t){const i={};for(const s of["preMarketMinutes","postMarketMinutes"]){const r=e[s];if(void 0!==r){if("number"!=typeof r||!Number.isInteger(r)||r<0||r>=1440)return t(`${s} must be whole minutes under a day`);i[s]=r}}return void 0!==e.extendedHours&&(i.extendedHours=Dh(e.extendedHours,t,"invalid extended hours")),i}function Uh(e,t,i,s){const r=new Date(e.getTime()+6e4*t),n=r.getUTCFullYear(),o=r.getUTCMonth()+1,a=r.getUTCDate(),l=r.getUTCHours(),h=r.getUTCMinutes(),c=kt(n,o,a,l,h,0,i),d=ft(c,i);return d.year!==n||d.month!==o||d.day!==a||d.hour!==l||d.minute!==h?s("session boundary is absent in this timezone"):c}class Kh{constructor(e,t,i){Object.defineProperty(this,"_zone",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"_fail",{enumerable:!0,configurable:!0,writable:!0,value:i}),Object.defineProperty(this,"_sessions",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_exceptions",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_extended",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_pre",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_post",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._sessions=t.sessions.map(e=>$t(e)),this._exceptions=new Map(Object.entries(t.exceptions??{}).map(([e,t])=>[e,t.map(e=>$t(e))])),this._extended=(t.extendedHours??[]).map(e=>$t(e)),this._pre=60*(t.preMarketMinutes??0),this._post=60*(t.postMarketMinutes??0)}_windows(e,t,i){const s=[],r=Wh(e);for(const n of i??this._exceptions.get(r)??this._sessions)n.days&&!n.days.includes(e.getUTCDay()+1)||t&&n.end>n.start||s.push({date:r,open:Uh(e,n.start,this._zone,this._fail),close:Uh(e,n.end+(n.end<=n.start?1440:0),this._zone,this._fail)});return s}_day(e){if(!Number.isFinite(e)||!Number.isFinite(new Date(1e3*e).getTime()))return this._fail("invalid timestamp");const t=ft(e,this._zone);return e=>new Date(Date.UTC(t.year,t.month-1,t.day+e))}at(e){const t=this._day(e);let i=null;for(const s of[0,-1])for(const r of this._windows(t(s),-1===s))if(!(e<r.open||e>=r.close)){if(i)return this._fail("overlapping active sessions");i=r}return i}from(e){const t=this._day(e);for(let i=-1;i<=370;i++){let s=null;for(const r of this._windows(t(i),-1===i))r.close>e&&(null===s||r.open<s.open)&&(s=r);if(s)return s}return null}_layOut(e,t){const i=this._windows(e,!1);for(const e of i)t.push({rank:0,start:e.open,end:e.close});if(i.length>0){const e=Math.min(...i.map(e=>e.open)),s=Math.max(...i.map(e=>e.close));this._pre>0&&t.push({rank:1,start:e-this._pre,end:e}),this._post>0&&t.push({rank:2,start:s,end:s+this._post})}if(0!==this._exceptions.get(Wh(e))?.length){for(const i of this._windows(e,!1,this._extended))t.push({rank:3,start:i.open,end:i.close});return}const s=e.getUTCDay()+1;if(!this._sessions.some(e=>!e.days||e.days.includes(s)))return;const r=e=>kt(e.getUTCFullYear(),e.getUTCMonth()+1,e.getUTCDate(),0,0,0,this._zone);t.push({rank:4,start:r(e),end:r(new Date(e.getTime()+864e5))})}spans(e,t){const i=this._day(e),s=this._day(t)(0);if(!(t>e)||t-e>3456e4)return this._fail("phase range must be ascending and at most 400 days");const r=[],n=Math.round((s.getTime()-i(0).getTime())/864e5);for(let e=-2;e<=n+1;e++)this._layOut(i(e),r);return function(e,t,i){const s=[];for(const r of i){const i=Math.max(r.start,e),n=Math.min(r.end,t);i<n&&s.push([i,r.rank,1],[n,r.rank,-1])}s.sort((e,t)=>e[0]-t[0]);const r=Ch.map(()=>0),n=[];for(let i=e,o=0;i<t;){for(;o<s.length&&s[o][0]<=i;)r[s[o][1]]+=s[o++][2];const e=o<s.length?s[o][0]:t,a=r.findIndex(e=>e>0),l=a<0?"closed":Ch[a],h=n[n.length-1];h?.phase===l?h.end=e:n.push({phase:l,start:i,end:e}),i=e}return n}(e,t,r)}phaseAt(e){return this.spans(e,e+1)[0].phase}}class Yh{constructor(e){Object.defineProperty(this,"timezone",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"calendar",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_hours",{enumerable:!0,configurable:!0,writable:!0,value:void 0});const t=Rh("session calendar"),i=zh(e,t);this.timezone=Vh(i.timezone,t);const s=Hh(i,t);this.calendar=Object.freeze({sessions:Dh(i.sessions,t),exceptions:s,...Gh(i,t)}),this._hours=new Kh(this.timezone,this.calendar,t)}sessionAt(e){return this._hours.at(e)}sessionFrom(e){return this._hours.from(e)}phaseAt(e){return this._hours.phaseAt(e)}phaseSpans(e,t){return this._hours.spans(e,t)}marketStatusAt(e){return Ah(this,e)}applyTo(e){if(e.isDestroyed)return Rh("session calendar")("chart is destroyed");e.setSessionCalendar(this)}}const Xh=new WeakSet;class Jh{constructor(e){Object.defineProperty(this,"metadata",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"tickSchedule",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_hours",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.metadata=function(e){const t=zh(e),i=zh(t.calendar),s=Vh(t.timezone,Fh),r=Bh(t.priceTick,"price tick"),n=t.pricePrecision;if("number"!=typeof n||!Number.isInteger(n)||n<0||n>12)return Fh("price precision must be 0..12");const o=r*10**n;if(!Number.isFinite(o)||o<1||Math.abs(o-Math.round(o))>8*Number.EPSILON*Math.max(1,o))return Fh("price precision cannot represent the tick");const a=$h(t.intervals,128);if(!a.length||new Set(a).size!==a.length)return Fh("intervals must be nonempty and unique");for(const e of a){const t=wr(e)?.bucketing;if(!t||"interval"===t.mode&&(!Number.isFinite(t.seconds)||t.seconds<=0))return Fh(`unsupported interval ${e}`)}const l=Hh(i,Fh);if(void 0!==t.hasOpenInterest&&"boolean"!=typeof t.hasOpenInterest)return Fh("invalid OI capability");const h=void 0===t.tickBands?void 0:new Th(t.tickBands);return h&&h.minMove!==r?Fh(`price tick ${r} must equal the schedule's minimum move ${h.minMove}`):Object.freeze({symbol:Eh(t.symbol,"symbol"),exchange:Eh(t.exchange,"exchange"),timezone:s,priceTick:r,pricePrecision:n,quantityStep:Bh(t.quantityStep,"quantity step"),intervals:Object.freeze(a),calendar:Object.freeze({sessions:Dh(i.sessions,Fh),exceptions:l,...Gh(i,Fh)}),...void 0===t.hasOpenInterest?{}:{hasOpenInterest:t.hasOpenInterest},...h?{tickBands:h.bands}:{}})}(e),this.tickSchedule=this.metadata.tickBands?new Th(this.metadata.tickBands):null,this._hours=new Kh(this.metadata.timezone,this.metadata.calendar,Fh)}supportsInterval(e){return this.metadata.intervals.includes(e)}formatPrice(e){return Number.isFinite(e)?e.toFixed(this.metadata.pricePrecision):""}sessionAt(e){return this._hours.at(e)}sessionFrom(e){return this._hours.from(e)}phaseAt(e){return this._hours.phaseAt(e)}phaseSpans(e,t){return this._hours.spans(e,t)}marketStatusAt(e){return Ah(this,e)}applyTo(e,t){if(!this.supportsInterval(t))return Fh(`unsupported interval ${t}`);if(e.isDestroyed)return Fh("chart is destroyed");const i=e.primarySeries();if(!i)return Fh("chart needs a primary series");const s=e.getDataContext(),r=this.metadata;if(s&&(s.symbol!==r.symbol||s.exchange!==r.exchange||s.interval!==t)&&e.primaryBars().length)return Fh("clear previous source bars before applying metadata");e.setTimezone(r.timezone),e.setPriceScaleOptions({minMove:r.priceTick}),i.priceScale().setOptions({minMove:r.priceTick}),i.priceScale().setPriceFormatter(e=>this.formatPrice(e)),e.setDataContext({symbol:r.symbol,exchange:r.exchange,interval:t,hasOpenInterest:r.hasOpenInterest,...void 0===s?.variant?{}:{variant:s.variant}}),function(e){Xh.has(e)||(Xh.add(e),e.on("data:context",()=>{const t=e.dataLayer.sessionCalendar,i=e.getDataContext();t instanceof Jh&&(t.metadata.symbol!==i?.symbol||t.metadata.exchange!==i?.exchange)&&e.dataLayer.setSessionCalendar(null)}))}(e),e.setSessionCalendar(this),e.setTickSchedule(this.tickSchedule)}}const Zh={place:"placing orders",modify:"modifying orders",cancel:"cancelling orders"},Qh={MARKET:"Market orders",LIMIT:"Limit orders",SL:"Stop-loss orders","SL-M":"Stop-loss market orders"},ec={live:"Live trading",analyzer:"Analyzer mode"};function tc(e,t){if(void 0===e)return{supported:!0};try{const s="function"==typeof e?e(Object.freeze({...t})):e;if(!s||"object"!=typeof s||Array.isArray(s)||"then"in s)return{supported:!1,reason:"Trading capabilities are unavailable"};const r=s[t.operation];if(!1===r)return{supported:!1,reason:`${i=Zh[t.operation]??t.operation,i[0].toUpperCase()+i.slice(1)} is not supported`};if(void 0!==r&&!0!==r)return{supported:!1,reason:`Support for ${Zh[t.operation]??t.operation} is unknown`};if("place"===t.operation){if(void 0!==s.orderTypes){if(!Array.isArray(s.orderTypes)||void 0===t.type)return{supported:!1,reason:"Supported order types are unavailable for this request"};if(!s.orderTypes.includes(t.type))return{supported:!1,reason:`${Qh[t.type]??`${t.type} orders`} are not supported`}}if(void 0!==s.modes){if(!Array.isArray(s.modes)||void 0===t.mode)return{supported:!1,reason:"Supported trading modes are unavailable for this request"};if(!s.modes.includes(t.mode))return{supported:!1,reason:`${ec[t.mode]??`${t.mode} mode`} is not supported`}}}return{supported:!0}}catch{return{supported:!1,reason:"Trading capabilities are unavailable"}}var i}class ic extends Error{constructor(e,t){super(t),Object.defineProperty(this,"preflight",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"operation",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.name="TradingCapabilityError",this.operation=e}}function sc(e,t){const i=tc(e,t);if(!i.supported)throw new ic(t.operation,i.reason)}function rc(e,t){const i=new Error(t);return i.name=e,i}function nc(e){if(!Number.isFinite(e)||e<=0)throw new RangeError("History timeout must be positive and finite");return Math.min(e,Sl)}function oc(e,t){if(e.signal?.aborted)return Promise.reject(rc("AbortError","History request cancelled"));const i=nc(e.timeoutMs??15e3);return new Promise((s,r)=>{const n=new AbortController;let o=!1;const a=(t,i)=>{o||(o=!0,clearTimeout(h),e.signal?.removeEventListener("abort",l),t?s(i):(n.abort(),r(i)))},l=()=>a(!1,rc("AbortError","History request cancelled")),h=setTimeout(()=>a(!1,rc("TimeoutError","History request timed out")),i);e.signal?.addEventListener("abort",l,{once:!0});try{t(n.signal).then(e=>a(!0,e),e=>a(!1,e))}catch(e){a(!1,e)}})}function ac(e){const t=e=>e.map(e=>{if(null===e||"object"!=typeof e)throw new TypeError("Invalid history bar");return{...e}});return Array.isArray(e)?t(e):{...e,bars:t(e.bars)}}class lc{constructor(e,t={}){if(Object.defineProperty(this,"_feed",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_max",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_timeout",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_jobs",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_active",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_draining",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._feed=e,this._max=t.maxConcurrent??4,!Number.isInteger(this._max)||this._max<1)throw new RangeError("History concurrency must be a positive integer");this._timeout=nc(t.timeoutMs??15e3)}getBars(e,t=0){return this._request(e,!1,t)}getBarsPage(e,t=0){return this._feed.getBarsPage?this._request(e,!0,t):Promise.reject(new Error("This feed does not support history pages"))}_request(e,t,i){if(e.signal?.aborted)return Promise.reject(rc("AbortError","History request cancelled"));if(!Number.isFinite(i))return Promise.reject(new RangeError("History priority must be finite"));let s,r;try{s=nc(e.timeoutMs??this._timeout)}catch(e){return Promise.reject(e)}try{r=JSON.stringify([t,e.symbol,e.exchange,e.interval,e.from,e.to,e.countBack,!0===e.noCache,"before"in e?e.before:null,rn(e.variant)])}catch(e){return Promise.reject(e)}let n=this._jobs.get(r);n||(n={key:r,req:{...e,signal:void 0,timeoutMs:Sl},page:t,priority:i,controller:new AbortController,consumers:new Set,running:!1,done:!1},this._jobs.set(r,n));const o=n;o.priority=Math.max(o.priority,i);const a=new Promise((t,i)=>{const r=e=>{o.consumers.delete(l)&&(l.cleanup(),i(e),0===o.consumers.size&&(this._remove(o),o.controller.abort(),this._drain()))},n=()=>r(rc("AbortError","History request cancelled")),a=setTimeout(()=>r(rc("TimeoutError","History request timed out")),s),l={resolve:t,reject:i,cleanup:()=>{clearTimeout(a),e.signal?.removeEventListener("abort",n)}};o.consumers.add(l),e.signal?.addEventListener("abort",n,{once:!0})});return this._drain(),a}_remove(e){e.done||(e.done=!0,this._jobs.get(e.key)===e&&this._jobs.delete(e.key),e.running&&this._active--)}_finish(e,t,i){if(e.done)return;let s;if(t)try{if(e.page===Array.isArray(i))throw new TypeError("Invalid history result shape");s=ac(i)}catch(e){t=!1,i=e}this._remove(e);for(const r of e.consumers)r.cleanup(),t?r.resolve(ac(s)):r.reject(i);e.consumers.clear(),this._drain()}_drain(){if(!this._draining){this._draining=!0;try{for(;this._active<this._max;){let e;for(const t of this._jobs.values())t.running||e&&!(t.priority>e.priority)||(e=t);if(!e)return;const t=e;t.running=!0,this._active++;const i={...t.req,signal:t.controller.signal};try{(t.page?this._feed.getBarsPage(i):this._feed.getBars(i)).then(e=>this._finish(t,!0,e),e=>this._finish(t,!1,e))}catch(e){this._finish(t,!1,e)}}}finally{this._draining=!1}}}}const hc=new WeakMap;function cc(e){let t=hc.get(e);return t||(t=new lc(e),hc.set(e,t)),t}const dc={delayMs:2500,retries:2,retryDelayMs:5e3};function uc(e){return e instanceof Error?e:new Error(String(e??"History request failed"))}function _c(e,t){if(!Number.isFinite(e)||e<=0)throw new RangeError(`${t} must be positive and finite`);return e}function pc(e){const t=new Map;for(const i of e){if(![i.time,i.open,i.high,i.low,i.close].every(Number.isFinite)||i.high<Math.max(i.open,i.close,i.low)||i.low>Math.min(i.open,i.close)||void 0!==i.volume&&(!Number.isFinite(i.volume)||i.volume<0))throw new Error("History contains an invalid candle");t.set(i.time,{...i})}return[...t.values()].sort((e,t)=>e.time-t.time)}function fc(...e){return pc(e.flat())}class mc{constructor(e,t={}){Object.defineProperty(this,"_feed",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_pool",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_options",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_listeners",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_state",{enumerable:!0,configurable:!0,writable:!0,value:{request:null,bars:[],status:"idle",historyStatus:"idle",hasMore:null,reason:"state",paused:!1}}),Object.defineProperty(this,"_bars",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_scope",{enumerable:!0,configurable:!0,writable:!0,value:new AbortController}),Object.defineProperty(this,"_refreshAbort",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_pageAbort",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_loadWork",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_pageWork",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_unsubscribe",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_stream",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_generation",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_refreshId",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_before",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_buffer",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_poll",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_visible",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_declared",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"_seconds",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_provisionalTime",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_repairTimer",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_repairRetries",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_pendingRepair",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_barClose",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._feed=e,this._pool=t.requestPool??cc(e),this._options={pageSize:500,maxEmptyPages:4,maxBars:1e5,pollIntervalMs:0,...t};for(const e of["pageSize","maxEmptyPages","maxBars"]){const t=_c(this._options[e],e);if(!Number.isInteger(t))throw new RangeError(`${e} must be an integer`)}if(void 0!==t.pageWindowSec&&_c(t.pageWindowSec,"pageWindowSec"),void 0!==t.timeoutMs&&_c(t.timeoutMs,"timeoutMs"),!Number.isFinite(this._options.pollIntervalMs)||this._options.pollIntervalMs<0)throw new RangeError("pollIntervalMs must be nonnegative");if(void 0!==t.refreshWindowBars&&!Number.isInteger(_c(t.refreshWindowBars,"refreshWindowBars")))throw new RangeError("refreshWindowBars must be an integer");const i=t.refreshOnBarClose;if(this._barClose=i?{...dc,...!0===i?{}:i}:null,this._barClose){for(const e of["delayMs","retries","retryDelayMs"]){const t=this._barClose[e];if(!Number.isFinite(t)||t<0)throw new RangeError(`refreshOnBarClose.${e} must be nonnegative`)}if(!Number.isInteger(this._barClose.retries))throw new RangeError("refreshOnBarClose.retries must be an integer")}}getState(){return this._state}bars(){return this._bars}subscribe(e){return this._destroyed?()=>{}:(this._listeners.add(e),()=>{this._listeners.delete(e)})}load(e){if(this._destroyed)return Promise.resolve([]);const t=++this._generation;if(this._cancel(),!this._current(t))return this._loadWork??Promise.resolve(this._bars);this._scope=new AbortController,this._bars=[],this._before=void 0,this._provisionalTime=null;const i=wr(e.interval);this._seconds="interval"===i?.bucketing.mode?i.bucketing.seconds:null;const s={...e,signal:void 0,timeoutMs:e.timeoutMs??this._options.timeoutMs};try{const t=sn(e.variant);t?s.variant=t:delete s.variant}catch{}let r;this._state={request:s,bars:[],status:"loading",historyStatus:"idle",hasMore:null,reason:"load",paused:!1},this._declared=void 0===s.variant;const n=new Promise(e=>{r=e});return this._loadWork=n,this._publish("load"),this._load(s,e.signal,t).then(r),n.then(()=>{this._loadWork===n&&(this._loadWork=null)}),n}async _load(e,t,i){if(!this._current(i))return this._bars;const s=this._scope,r=()=>s.abort();t?.addEventListener("abort",r,{once:!0}),t?.aborted&&s.abort();try{if(void 0!==e.variant){const t=this._feed.dataVariants,r=void 0===t?void 0:await oc({signal:s.signal,timeoutMs:e.timeoutMs},i=>Promise.resolve(t.call(this._feed,{symbol:e.symbol,exchange:e.exchange,interval:e.interval,signal:i})));if(!this._current(i))return this._bars;const n=nn(r,e.variant);if(null!==n)return this._publish("state",{status:"unsupported",unsupported:n,error:on(n,e.variant)}),this._bars;this._declared=!0}let t;if(!e.noCache&&this._feed.getCachedBars)try{const i=await oc({signal:s.signal,timeoutMs:Math.min(e.timeoutMs??500,500)},t=>this._feed.getCachedBars({...e,signal:t}));t=i?pc(i):void 0}catch{}if(!this._current(i))return this._bars;if(s.signal.aborted)throw s.signal.reason;t?.length&&(this._bars=t,this._publish("cache",{status:"refreshing"}));const r=t?.length&&void 0!==e.from&&t[0].time<=e.from?Math.max(e.from,t[Math.max(0,t.length-2)].time):e.from,n=await this._pool.getBars({...e,from:r,signal:s.signal,noCache:e.noCache||!!this._feed.getCachedBars},10);if(!this._current(i))return this._bars;this._bars=this._replaceWindow(pc(n),r,e.to),this._limit(),this._before=this._bars[0]?.time??e.from,this._publish("load",{status:this._bars.length?"ready":"empty",error:void 0}),this._startStream(i),this._current(i)&&this._schedulePoll()}catch(e){this._current(i)&&this._publish("state",{status:s.signal.aborted?"idle":this._bars.length?"stale":"error",error:s.signal.aborted?void 0:uc(e)})}finally{t?.removeEventListener("abort",r)}return this._bars}refresh(){return this._refresh()}async _refresh(e){if(this._destroyed||!this._state.request||"unsupported"===this._state.status)return this._bars;if(this._loadWork)return this._loadWork;if(!this._declared)return this.load(this._state.request);const t=this._generation,i=++this._refreshId,s=this._refreshAbort;if(this._refreshAbort=null,s?.abort(),!this._current(t)||i!==this._refreshId)return this._bars;const r=new AbortController;this._refreshAbort=r,this._clearPoll(),this._buffer??(this._buffer=new Map);const n=this._state.request,o=this._bars[this._bars.length-1],{from:a,to:l}=this._refreshWindow(n,o,e);this._publish("state",{status:"refreshing",error:void 0});try{const s=await this._pool.getBars({...n,from:a,to:l,noCache:!0,signal:r.signal},5);if(!this._current(t)||i!==this._refreshId)return this._bars;if(0===s.length&&this._bars.length)throw new Error("History refresh returned no bars");const h=this._mergeRefresh(s,a,l,o);this._buffer=null,this._limit(),this._publish("refresh",{status:this._bars.length?"ready":"empty",error:void 0}),this._startStream(t),void 0!==e?.expect&&h<e.expect&&this._barClose&&this._repairRetries<this._barClose.retries&&(this._repairRetries++,this._scheduleRepair({expect:e.expect},this._barClose.retryDelayMs))}catch(e){if(!this._current(t)||i!==this._refreshId||r.signal.aborted)return this._bars;this._limit(),this._publish("refresh",{status:this._bars.length?"stale":"error",error:uc(e)})}finally{if(this._current(t)&&i===this._refreshId){this._refreshAbort=null,this._schedulePoll();const e=this._pendingRepair;this._pendingRepair=null,e&&this._refresh(e)}}return this._bars}_refreshWindow(e,t,i){const s=Math.max(e.to??0,(this._options.now??(()=>Date.now()/1e3))()),r=void 0!==e.from&&void 0!==e.to?e.to-e.from:void 0;let n=void 0===r?e.from:s-r;return void 0!==this._options.refreshWindowBars&&null!==this._seconds&&t&&(n=t.time-this._options.refreshWindowBars*this._seconds,void 0!==i?.from&&(n=Math.min(n,i.from))),{from:n,to:s}}_mergeRefresh(e,t,i,s){const r=pc(e);let n=-1/0;for(const e of r)e.time>n&&(n=e.time);const o=r.length?Math.min(i,n):i,a=this._replaceWindow(r,t,o),l=new Map(a.map(e=>[e.time,e])),h=s&&s.time===n?l.get(s.time):void 0;s&&h&&l.set(s.time,Ul(h,s)),null!==this._provisionalTime&&l.has(this._provisionalTime)&&this._provisionalTime<=o&&(this._provisionalTime=null);for(const e of this._buffer?.values()??[]){const t=l.get(e.time);l.set(e.time,t?{...Ul(t,e),close:e.close,oi:e.oi}:e)}return this._bars=pc([...l.values()]),n}pushBar(e,t){if(this._destroyed||!this._state.request||"unsupported"===this._state.status)return;let i;try{i=pc([e])[0]}catch(e){return void this._publish("state",{status:this._bars.length?"stale":"error",error:uc(e)})}const s=this._bars[this._bars.length-1];if(s&&i.time<s.time)return;const r=!0===t?.provisional,n=!s||i.time>s.time;if(n?r&&(this._provisionalTime=i.time):r&&this._provisionalTime!==i.time&&(i={...Ul(i,s),open:s.open}),this._buffer)for(this._buffer.set(i.time,i);this._buffer.size>this._options.maxBars;)this._buffer.delete(this._buffer.keys().next().value);const o=this._bars.slice();s?.time===i.time?o[o.length-1]={...i,volume:i.volume??s.volume}:o.push(i),this._bars=o,this._limit(),this._buffer||this._publish("live",{status:"stale"===this._state.status?"stale":"ready"}),n&&this._onNewBucket(s?.time,i.time)}_onNewBucket(e,t){this._options.refreshOnGap&&null!==this._seconds&&void 0!==e&&t-e>this._seconds?this._requestRepair({from:e}):this._barClose&&(this._repairRetries=0,this._scheduleRepair({expect:e},this._barClose.delayMs))}_scheduleRepair(e,t){this._clearRepairTimer(),!this._destroyed&&this._visible&&(this._repairTimer=setTimeout(()=>{this._repairTimer=null,this._requestRepair(e)},t))}_requestRepair(e){if(!this._destroyed&&this._visible&&this._state.request&&!this._loadWork){if(this._refreshAbort){const t=this._pendingRepair;return void(this._pendingRepair={from:void 0===t?.from?e.from:void 0===e.from?t.from:Math.min(t.from,e.from),expect:void 0===t?.expect?e.expect:void 0===e.expect?t.expect:Math.max(t.expect,e.expect)})}this._refresh(e)}}_clearRepairTimer(){null!==this._repairTimer&&clearTimeout(this._repairTimer),this._repairTimer=null}loadMore(e){if(this._pageWork)return this._pageWork;if(this._destroyed||!this._state.request||this._loadWork||this._state.paused||!1===this._state.hasMore||!this._declared)return Promise.resolve(this._bars);if(this._bars.length>=this._options.maxBars)return this._publish("state",{historyStatus:"limited"}),Promise.resolve(this._bars);const t=this._generation;let i;const s=new Promise(e=>{i=e});return this._pageWork=s,this._loadMore(t,e).then(i),s.then(()=>{this._pageWork===s&&(this._pageWork=null)}),s}async _loadMore(e,t){const i=this._state.request,s=this._options.pageWindowSec??Math.max(1,(i.to??0)-(i.from??0)||86400),r=this._feed.getBarsPage||!Number.isFinite(t)?0:this._options.maxBars*(this._seconds??1/0),n=new AbortController;this._pageAbort=n,this._publish("state",{historyStatus:"loading",historyError:void 0});try{for(let o=0;o<this._options.maxEmptyPages;o++){const o=this._before??this._bars[0]?.time??i.from;if(void 0===o)throw new Error("Older history needs a starting time");const a=r>0?Math.max(s,Math.min(r,o-t)):s,l={...i,from:o-a,to:o-1e-6,before:o,countBack:this._options.pageSize,signal:n.signal},h=this._feed.getBarsPage?await this._pool.getBarsPage(l):{bars:await this._pool.getBars(l),hasMore:void 0,nextBefore:void 0};if(!this._current(e)||n.signal.aborted)return this._bars;const c=pc(h.bars).filter(e=>e.time<o),d=h.nextBefore??c[0]?.time??o-a;if(!Number.isFinite(d)||d>=o)throw new Error("History page did not advance its cursor");if(this._before=d,c.length){const e=new Set(this._bars.map(e=>e.time)),t=c.filter(t=>!e.has(t.time)),i=Math.max(0,this._options.maxBars-this._bars.length);this._bars=fc(i?t.slice(-i):[],this._bars);const s=t.length>i||this._bars.length>=this._options.maxBars;return this._publish("prepend",{hasMore:t.length>i||(h.hasMore??null),historyStatus:s?"limited":!1===h.hasMore?"exhausted":"idle"}),this._bars}if(!1===h.hasMore)return this._publish("state",{hasMore:!1,historyStatus:"exhausted"}),this._bars}this._publish("state",{hasMore:null,historyStatus:"idle"})}catch(t){this._current(e)&&!n.signal.aborted&&this._publish("state",{historyStatus:"error",historyError:uc(t)})}finally{this._pageAbort===n&&(this._pageAbort=null)}return this._bars}setPaused(e){this._destroyed||e===this._state.paused||(this._state={...this._state,paused:e},this._publish(e?"state":"resume"))}setVisible(e){this._destroyed||e===this._visible||(this._visible=e,e?this.refresh():(this._clearPoll(),this._clearRepairTimer(),this._pendingRepair=null))}destroy(){this._destroyed||(this._destroyed=!0,this._generation++,this._cancel(),this._listeners.clear())}_current(e){return!this._destroyed&&e===this._generation}_replaceWindow(e,t,i){return void 0===t||void 0===i?e:fc(this._bars.filter(e=>e.time<t||e.time>i),e)}_limit(){this._bars.length>this._options.maxBars&&(this._bars=this._bars.slice(-this._options.maxBars))}_publish(e,t={}){if(this._destroyed)return;this._state={...this._state,...t,reason:e,bars:this._state.paused||this._buffer?this._state.bars:this._bars};const i=this._state;for(const e of[...this._listeners]){if(this._destroyed||this._state!==i)break;try{e(i)}catch{}}}_startStream(e){if(!this._current(e))return;const t=this._stream,i=++this._stream,s=this._unsubscribe;if(this._unsubscribe=null,this._feed.subscribeBars)try{const t=this._feed.subscribeBars(this._state.request,(t,s)=>{this._current(e)&&i===this._stream&&this.pushBar(t,s)},{seedFrom:this._bars[this._bars.length-1],onResync:()=>{this._current(e)&&i===this._stream&&this.refresh()}});this._current(e)&&i===this._stream?this._unsubscribe=t:this._release(t),this._release(s)}catch(r){this._current(e)&&i===this._stream?(this._stream=t,this._unsubscribe=s,this._buffer??(this._buffer=new Map),this._publish("state",{status:this._bars.length?"stale":"error",error:uc(r)})):this._release(s)}else this._release(s)}_release(e){try{e?.()}catch{}}_clearPoll(){null!==this._poll&&clearTimeout(this._poll),this._poll=null}_schedulePoll(){this._clearPoll(),!this._destroyed&&this._visible&&this._options.pollIntervalMs>0&&(this._poll=setTimeout(()=>{this._poll=null,this.refresh()},this._options.pollIntervalMs))}_cancel(){const e=this._scope,t=this._refreshAbort,i=this._pageAbort,s=this._unsubscribe;this._refreshAbort=null,this._pageAbort=null,this._loadWork=null,this._pageWork=null,this._buffer=null,this._pendingRepair=null,this._stream++,this._unsubscribe=null,this._clearPoll(),this._clearRepairTimer(),e.abort(),t?.abort(),i?.abort(),this._release(s)}}function bc(e){if("number"==typeof e)return e>1e12?Je(e):Math.floor(e);const t=Number(e);if(""!==e.trim()&&!Number.isNaN(t)&&!/[-T :]/.test(e.trim()))return t>1e12?Je(t):Math.floor(t);if(/(?:Z|[+-]\d{2}:?\d{2})$/i.test(e.trim())){const t=Date.parse(e.trim().replace(" ","T"));if(Number.isFinite(t))return Je(t)}return Ze(e)}function gc(e,t){if("error"===e.status)throw new Error(e.message??"OpenAlgo history request failed");const i=e.data??[],s=[];for(const e of i){const i=e.timestamp??e.time;void 0!==i&&s.push({time:bc(i),open:e.open,high:e.high,low:e.low,close:e.close,volume:e.volume,...!1!==t&&Number.isFinite(e.oi)?{oi:e.oi}:{}})}return s.sort((e,t)=>e.time-t.time)}class vc{constructor(e){Object.defineProperty(this,"_config",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_fetch",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._config=e;const t=e.fetchImpl??("undefined"!=typeof fetch?fetch.bind(globalThis):void 0);if(void 0===t)throw new Error("openalgo-charts: no fetch available; pass config.fetchImpl");this._fetch=t}async getBars(e){const t=nn(void 0,e.variant);if(t)throw on(t,e.variant);const i=this._config.hasOpenInterest?.(e),{from:s,to:r}=e;if(void 0===s||void 0===r)throw new Error("openalgo-charts: getBars requires `from` and `to` (UTC seconds): OpenAlgo history needs a date range");return oc(e,async t=>{const n=await this._fetch(`${this._config.baseUrl}/api/v1/history`,{method:"POST",signal:t,headers:{"Content-Type":"application/json"},body:JSON.stringify({apikey:this._config.apiKey,symbol:e.symbol,exchange:e.exchange,interval:{"1d":"D","1D":"D","1w":"W","1W":"W","1M":"M",MN:"M"}[e.interval]??e.interval,start_date:st(s),end_date:st(r)})});if(!n.ok)throw new Error(`openalgo-charts: history request failed (${n.status})`);return gc(await n.json(),i)})}async searchSymbols(e){return""===e.query.trim()?[]:oc(e,async t=>{const i=await this._fetch(`${this._config.baseUrl}/api/v1/search`,{method:"POST",signal:t,headers:{"Content-Type":"application/json"},body:JSON.stringify({apikey:this._config.apiKey,query:e.query})});if(!i.ok)throw new Error(`openalgo-charts: symbol search failed (${i.status})`);return function(e){if("error"===e.status)throw new Error("string"==typeof e.message?e.message:"openalgo-charts: symbol search failed");const t=[],i=new Map;for(const s of Array.isArray(e.data)?e.data:[]){const e=e=>"string"==typeof s?.[e]?s[e].trim():"",[r,n,o,a]=[e("symbol"),e("exchange"),e("name"),e("expiry")];if(""===r)continue;const l=""!==a&&/(?:CE|PE)$/.test(r),h={symbol:r,...n?{exchange:n}:{},...o?{name:o}:{},assetClass:n.endsWith("_INDEX")?"Index":""===a?"Equity":l?"Options":"Futures"};if(!l){t.push(h);continue}const c=`${n}\n${o}\n${a}`;let d=i.get(c);if(void 0===d){i.set(c,d=[]);const e=`${o} ${a}`.trim();t.push({symbol:e,...n?{exchange:n}:{},assetClass:"Options",contractGroup:{label:e,contracts:d}})}d.push(h)}return t.slice(0,50).map(e=>1===e.contractGroup?.contracts.length?e.contractGroup.contracts[0]:e)}(await i.json())})}}const yc={LTP:1,Quote:2,Depth:3};function wc(){return JSON.stringify({action:"subscribe_orders"})}function xc(e,t,i,s){const r={action:"subscribe",symbol:t,exchange:i,mode:yc[e]};return"Depth"===e&&void 0!==s&&(r.depth=s,r.depth_level=s),JSON.stringify(r)}function Sc(e,t,i){return JSON.stringify({action:"unsubscribe",symbol:t,exchange:i,mode:yc[e]})}function Pc(e){if("string"==typeof e&&/^[+-]?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i.test(e.trim())&&(e=Number(e)),"number"==typeof e)return Number.isFinite(e)&&e>0?e>1e12?Je(e):Math.floor(e):0;if("string"==typeof e&&""!==e.trim()){const t=Date.parse(e);if(Number.isFinite(t)&&t>0)return Math.floor(t/1e3)}return 0}function Oc(e){for(const t of[e.last_trade_time,e.ltt,e.exchange_timestamp,e.timestamp]){const e=Pc(t);if(e>0)return e}return 0}function kc(e){if("string"!=typeof e)return null;const t=e.split(".");if(2!==t.length)return null;const[i,s]=t;return""===i||""===s?null:{symbol:i,exchange:s}}function Ic(e){if("object"!=typeof e||null===e)return null;const t=e,i="string"==typeof t.type?t.type.toLowerCase():"",s="string"==typeof t.status?t.status.toLowerCase():"",r="success"===s||"ok"===s||"authenticated"===s;return"auth"===i||"authenticate"===i||"auth_response"===i?r?"ok":"failed":"error"===i||"error"===s?"failed":""===i&&r&&"string"==typeof t.message&&/auth/i.test(t.message)?"ok":null}function Mc(e){if("object"!=typeof e||null===e)return;const t=e,i=e=>{if(void 0!==e)for(const t of["seq","sequence","sequence_number"]){const i=e[t];if("number"==typeof i&&Number.isFinite(i))return i}};return i(t)??i(t.data)}function Tc(e,t,i=Math.random){const s=Math.min(t.maxDelayMs,t.baseDelayMs*2**e);return!1===t.jitter?s:Math.floor(s/2+i()*(s/2))}function Cc(e){if("object"!=typeof e||null===e)return null;const t=e,i=t.data??t,s=kc(t.topic),r=i.symbol||t.symbol||s?.symbol||"",n=i.exchange||t.exchange||s?.exchange||"";if(i.depth&&(i.depth.buy||i.depth.sell)){const e=(i.depth.buy??[]).map(e=>({price:e.price,qty:e.quantity})),t=(i.depth.sell??[]).map(e=>({price:e.price,qty:e.quantity})),s=i.ltp??i.last_price??e[0]?.price??0,o=Oc(i);return{kind:"depth",symbol:r,exchange:n,depth:{bids:e,asks:t,ltp:s,...Number.isFinite(o)&&o>0?{timeSec:o}:{}}}}const o=i.ltp??i.last_price;return"number"==typeof o?{kind:"ltp",event:{symbol:r,exchange:n,ltp:o,ltq:i.last_trade_quantity??i.ltq,volume:i.volume,timeSec:Oc(i)}}:null}class jc{constructor(e){Object.defineProperty(this,"_config",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_factory",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_sock",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_phase",{enumerable:!0,configurable:!0,writable:!0,value:"idle"}),Object.defineProperty(this,"_epoch",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_ltpCbs",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_depthCbs",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_stateCbs",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_controlCbs",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_orderCbs",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_ordersSubscribed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_subs",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_seq",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_userClosed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_fatal",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_everAuthed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_attempts",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_reconnectTimer",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_authTimer",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_liveTimer",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_rc",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_auth",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_hbTimeoutMs",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_hbProbeMs",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._config=e;const t=e.socketFactory??(e=>new WebSocket(e));this._factory=t;const i=e.reconnect??{};this._rc={enabled:i.enabled??!0,baseDelayMs:i.baseDelayMs??1e3,maxDelayMs:i.maxDelayMs??3e4,maxAttempts:i.maxAttempts??1/0,jitter:i.jitter??!0,random:i.random??Math.random};const s=e.auth??{};this._auth={requireAck:s.requireAck??!0,ackTimeoutMs:s.ackTimeoutMs??5e3},this._hbTimeoutMs=e.heartbeat?.timeoutMs??45e3,this._hbProbeMs=e.heartbeat?.probeMs??5e3}connect(){null===this._sock&&(this._userClosed=!1,this._fatal=!1,this._attempts=0,null!==this._reconnectTimer&&(clearTimeout(this._reconnectTimer),this._reconnectTimer=null),this._openSocket())}isReady(){return"ready"===this._phase}_openSocket(){if(null!==this._sock)return;this._phase="connecting",this._emitState("connecting");const e=this._factory(this._config.url),t=++this._epoch;e.onmessage=e=>{t===this._epoch&&this._dispatch(e.data)},e.onopen=()=>{t===this._epoch&&this._onOpen()},e.onclose=()=>{t===this._epoch&&this._onClose()},e.onerror=()=>{t===this._epoch&&this._emitState("error")},this._sock=e,1===e.readyState&&this._onOpen()}onState(e){return this._stateCbs.add(e),()=>this._stateCbs.delete(e)}onControl(e){return this._controlCbs.add(e),()=>this._controlCbs.delete(e)}_emitState(e){mn(this._stateCbs,e)}_emitControl(e){mn(this._controlCbs,e)}_warn(e,t){const i={type:"client_warning",code:e,message:t};this._emitControl(i)}_onOpen(){if("connecting"===this._phase){var e;if(this._phase="authenticating",this._sock?.send((e=this._config.apiKey,JSON.stringify({action:"authenticate",api_key:e}))),!this._auth.requireAck)return this._warn("AUTH_UNACKNOWLEDGED","proceeding without an auth acknowledgement (auth.requireAck is false)"),void this._onAuthenticated();this._authTimer=Ol(()=>this._failConnection("AUTH_TIMEOUT",`no auth acknowledgement within ${this._auth.ackTimeoutMs}ms`),this._auth.ackTimeoutMs)}}_onAuthenticated(){if("authenticating"===this._phase){this._clearTimer("auth"),this._phase="ready",this._attempts=0,this._armLiveness();for(const e of Array.from(this._subs.values()))this._sock?.send(xc(e.mode,e.symbol,e.exchange,e.depthLevel));this._ordersSubscribed&&this._sock?.send(wc()),this._everAuthed&&(this._seq.clear(),this._warn("STREAM_RESYNC","reconnected: ticks were missed and the forming bar is stale, refetch it")),this._everAuthed=!0,this._emitState("open")}}_onAuthFailed(e){const t="object"==typeof e&&null!==e&&"string"==typeof e.message?e.message:"authentication refused";this._fatal=!0,this._warn("AUTH_FAILED",t),this._emitState("error"),this._dropSocket(),this._clearTimer("auth"),this._clearTimer("live"),this._phase="fatal",this._emitState("closed")}_failConnection(e,t){this._warn(e,t),this._emitState("error"),this._dropSocket(),this._clearTimer("auth"),this._clearTimer("live"),this._phase="idle",this._emitState("closed"),this._maybeReconnect()}_onClose(){this._sock=null,this._clearTimer("auth"),this._clearTimer("live"),this._phase=this._userClosed?"closed":"idle",this._emitState("closed"),this._maybeReconnect()}_maybeReconnect(){if(this._userClosed||this._fatal||!this._rc.enabled)return;if(this._attempts>=this._rc.maxAttempts)return void("fatal"!==this._phase&&(this._phase="fatal",this._warn("RECONNECT_ABANDONED",`gave up after ${this._attempts} attempts; call connect() to retry`)));const e=Tc(this._attempts++,this._rc,this._rc.random);this._phase="backoff",this._emitState("reconnecting"),this._reconnectTimer=Ol(()=>{this._reconnectTimer=null,this._sock=null,this._openSocket()},e)}_sendGated(e){"ready"===this._phase&&null!==this._sock&&this._sock.send(e)}_armLiveness(){this._hbTimeoutMs<=0||"ready"!==this._phase||(this._clearTimer("live"),this._liveTimer=Ol(()=>this._probeLiveness(),this._hbTimeoutMs))}_probeLiveness(){"ready"===this._phase&&null!==this._sock&&(this._sock.send(JSON.stringify({action:"ping"})),this._liveTimer=Ol(()=>this._failConnection("HEARTBEAT_DEAD",`no answer to a liveness ping within ${this._hbProbeMs}ms`),this._hbProbeMs))}_clearTimer(e){const t="auth"===e?this._authTimer:"live"===e?this._liveTimer:this._reconnectTimer;null!==t&&clearTimeout(t),"auth"===e?this._authTimer=null:"live"===e?this._liveTimer=null:this._reconnectTimer=null}_dropSocket(){const e=this._sock;if(this._sock=null,this._epoch++,null!==e)try{e.close()}catch{}}onLtp(e){return this._ltpCbs.add(e),()=>this._ltpCbs.delete(e)}onDepth(e){return this._depthCbs.add(e),()=>this._depthCbs.delete(e)}subscribe(e,t,i,s){this._subs.set(`${e}:${t}:${i}`,{mode:e,symbol:t,exchange:i,depthLevel:s}),this._sendGated(xc(e,t,i,s))}unsubscribe(e,t,i){this._subs.delete(`${e}:${t}:${i}`),this._sendGated(Sc(e,t,i))}onOrderUpdate(e){return this._orderCbs.add(e),()=>this._orderCbs.delete(e)}subscribeOrders(){this._ordersSubscribed=!0,this._sendGated(wc())}unsubscribeOrders(){this._ordersSubscribed=!1,this._sendGated(JSON.stringify({action:"unsubscribe_orders"}))}close(){this._userClosed=!0,this._clearTimer("reconnect"),this._clearTimer("auth"),this._clearTimer("live"),this._dropSocket(),this._subs.clear(),this._ordersSubscribed=!1,this._seq.clear(),this._attempts=0,this._everAuthed=!1,this._fatal=!1,this._phase="closed",this._emitState("closed")}_sequenceOk(e,t){const i=Mc(t);if(void 0===i)return!0;const s=this._seq.get(e);return!(void 0!==s&&i<=s)&&(void 0!==s&&i>s+1&&this._warn("SEQUENCE_GAP",`${e}: ${i-s-1} frame(s) missing, the forming bar is suspect`),this._seq.set(e,i),!0)}_dispatch(e){let t;try{t=JSON.parse(e)}catch{t=e}if(this._armLiveness(),function(e){return"ping"===e||"object"==typeof e&&null!==e&&"ping"===e.type}(t))return void this._sock?.send(JSON.stringify({action:"pong"}));if("authenticating"===this._phase){const e=Ic(t);if(null!==e)return this._emitControl(t),void("ok"===e?this._onAuthenticated():this._onAuthFailed(t))}const i=function(e){if("object"!=typeof e||null===e)return null;const t=e;if("order_update"!==t.type)return null;const i=e=>{if("number"==typeof e)return e;if("string"==typeof e&&""!==e.trim()){const t=Number(e);return Number.isNaN(t)?0:t}return 0},s=e=>"string"==typeof e?e:"",r=i(t.trigger_price);return{orderId:s(t.orderid),symbol:s(t.symbol),exchange:s(t.exchange),action:"SELL"===t.action?"SELL":"BUY",quantity:i(t.quantity),price:i(t.price),triggerPrice:r>0?r:void 0,pricetype:""===s(t.pricetype)?"LIMIT":s(t.pricetype),product:s(t.product),status:s(t.order_status).toLowerCase(),filledQuantity:i(t.filled_quantity),pendingQuantity:i(t.pending_quantity),averagePrice:i(t.average_price),rejectionReason:s(t.rejection_reason),mode:s(t.mode)}}(t);if(null!==i){if(!this._sequenceOk("orders",t))return;return void mn(this._orderCbs,i)}const s=Cc(t);if(null===s)return void("object"==typeof t&&null!==t&&this._emitControl(t));const r=t.topic;if("ltp"===s.kind){if(!this._sequenceOk(r??`ltp:${s.event.symbol}.${s.event.exchange}`,t))return;mn(this._ltpCbs,s.event)}else{if(!this._sequenceOk(r??`depth:${s.symbol}.${s.exchange}`,t))return;const{symbol:e,exchange:i,depth:n}=s;mn([...this._depthCbs].map(t=>()=>t(e,i,n)),void 0)}}}function Ac(e,t){if(!Number.isFinite(e))return{ok:!1,code:"QTY_INVALID",reason:"quantity must be a finite number"};if(e<=0)return{ok:!1,code:"QTY_INVALID",reason:"quantity must be positive"};const i=t.lotSize??(!0===t.allowFractionalQty?void 0:1);return void 0!==i&&function(e,t){if(!Number.isFinite(t)||t<=0)return!1;const i=e/t,s=Math.round(i);if(!Number.isSafeInteger(s)||s<1)return!0;if(s*t===e)return!1;const r=Math.min(1e-7,8*Number.EPSILON*Math.max(1,Math.abs(i)));return Math.abs(i-s)>r}(e,i)?{ok:!1,code:"QTY_STEP",reason:void 0!==t.lotSize?`quantity ${e} is not a multiple of lot size ${t.lotSize}`:`quantity ${e} must be a whole number`}:void 0!==t.freezeQty&&e>t.freezeQty?{ok:!1,code:"QTY_FREEZE_LIMIT",reason:`quantity ${e} exceeds freeze limit ${t.freezeQty}`}:{ok:!0}}function Lc(e){return Object.assign(new Error(e),{preflight:!0})}class Nc{constructor(e){Object.defineProperty(this,"_config",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_fetch",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_strategy",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_defaultProduct",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_ctx",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_claimed",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_verifyMode",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_modeCacheMs",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_now",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_mode",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_modeAt",{enumerable:!0,configurable:!0,writable:!0,value:0}),this._config=e,this._strategy=e.strategy??"openalgo-charts",this._defaultProduct=e.defaultProduct??"MIS",this._verifyMode=e.verifyMode??"auto",this._modeCacheMs=e.modeCacheMs??5e3,this._now=e.now??Date.now;const t=e.fetchImpl??("undefined"!=typeof fetch?fetch.bind(globalThis):void 0);if(void 0===t)throw new Error("openalgo-charts: no fetch available; pass config.fetchImpl");this._fetch=t}async _post(e,t){const i=await this._fetch(`${this._config.baseUrl}${e}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({apikey:this._config.apiKey,...t})});if(!i.ok){let t="";try{const e=await i.json();"string"==typeof e.message&&""!==e.message&&(t=`: ${e.message}`)}catch{}throw new Error(`openalgo-charts: ${e} failed (${i.status})${t}`)}const s=await i.json(),r=Gc(s);return void 0!==r&&(this._mode=r,this._modeAt=this._now()),s}async getServerMode(e=this._modeCacheMs){const t=this._freshMode(e);if(void 0!==t)return t;const i=Gc(await this._post("/api/v1/analyzer/",{}));if(void 0===i)throw new Error("openalgo-charts: /api/v1/analyzer/ returned no readable mode");return i}serverMode(e=this._modeCacheMs){return this._freshMode(e)}_freshMode(e){if(void 0!==this._mode)return this._now()-this._modeAt<=e?this._mode:void 0}async _assertMode(e){if("off"===this._verifyMode)return;if("live"!==e&&"analyzer"!==e)return;let t=this._freshMode(this._modeCacheMs);if(void 0===t&&("always"===this._verifyMode||"analyzer"===e))try{t=await this.getServerMode()}catch(e){throw Lc(`openalgo-charts: cannot confirm the server mode, no order was sent (${String(e.message??e)})`)}if(void 0!==t&&t!==e)throw Lc(`openalgo-charts: refusing to place, caller expects ${e} mode but the OpenAlgo server is in ${t} mode`)}_assertQuantity(e){const t=e.exchange??"NSE",i=this._config.constraints?.(e.symbol,t)??{},s=Ac(e.qty,i);if(!s.ok)throw Lc(`openalgo-charts: ${e.symbol} ${t}: ${s.reason}`)}_claimToken(e){if(void 0===e||!0===this._config.disableIdempotency)return;const t=this._claimed.get(e);if(void 0!==t)throw Lc("ambiguous"===t?`openalgo-charts: clientToken ${e} may already be live at the broker; check the order book before retrying`:`openalgo-charts: duplicate clientToken ${e}`);return this._claimed.set(e,"inflight"),e}get capabilities(){return this._config.capabilities}async place(e){const t={...e};if(void 0!==t.account||void 0!==t.duration||void 0!==t.expiresAt||void 0!==t.leverage)throw Lc("openalgo-charts: the OpenAlgo placeorder route has no account, duration, expiry or leverage field; nothing was sent");const i={operation:"place",symbol:t.symbol,exchange:t.exchange??"NSE",type:t.type,mode:t.mode};sc(this.capabilities,i),await this._assertMode(t.mode),sc(this.capabilities,i),this._assertQuantity(t);const s=this._claimToken(t.clientToken),r={symbol:t.symbol,exchange:t.exchange??"NSE",action:t.side,pricetype:t.type,product:t.product??this._defaultProduct,quantity:t.qty,price:t.price??0,triggerPrice:t.triggerPrice??0};let n;try{n=await this._post("/api/v1/placeorder",{strategy:this._strategy,symbol:r.symbol,action:r.action,exchange:r.exchange,pricetype:r.pricetype,product:r.product,quantity:t.qty,price:t.price??0,trigger_price:t.triggerPrice??0,disclosed_quantity:0})}catch(e){throw void 0!==s&&this._claimed.set(s,"ambiguous"),e}const o=n.orderid??n.order_id;if(void 0===o)throw new Error("openalgo-charts: placeorder returned no orderid");return void 0!==s&&this._claimed.set(s,"sent"),this._ctx.set(o,r),{orderId:o}}tokenState(e){return this._claimed.get(e)??"unknown"}releaseToken(e){this._claimed.delete(e)}async modify(e,t){const i={...t},s=this._ctx.get(e);if(sc(this.capabilities,{operation:"modify",orderId:e,symbol:s?.symbol,exchange:s?.exchange,type:s?.pricetype}),void 0===s)throw Lc(`openalgo-charts: cannot modify ${e}, unknown order context (place it or load the order book first)`);void 0!==i.qty&&(s.quantity=i.qty),void 0!==i.price&&(s.price=i.price),void 0!==i.triggerPrice&&(s.triggerPrice=i.triggerPrice),await this._post("/api/v1/modifyorder",{orderid:e,strategy:this._strategy,symbol:s.symbol,action:s.action,exchange:s.exchange,pricetype:s.pricetype,product:s.product,quantity:s.quantity,price:s.price,trigger_price:s.triggerPrice,disclosed_quantity:0})}async cancel(e){const t=this._ctx.get(e);sc(this.capabilities,{operation:"cancel",orderId:e,symbol:t?.symbol,exchange:t?.exchange,type:t?.pricetype}),await this._post("/api/v1/cancelorder",{orderid:e,strategy:this._strategy})}async getOrderBook(){const e=await this._post("/api/v1/orderbook",{}),t=Array.isArray(e.data)?e.data:e.data?.orders??[],i=[],s=[];for(let e=0;e<t.length;e++){const r=t[e],n=qc(r,`orders[${e}]`);if(!n.ok){s.push({issue:n.issue,raw:r});continue}i.push(n.order);const o=Fc(r.exchange),a=Fc(r.product);void 0!==o&&void 0!==a&&this._ctx.set(n.order.id,{symbol:n.order.symbol,exchange:o,action:n.order.side,pricetype:n.order.type,product:a,quantity:n.order.qty,price:n.order.price,triggerPrice:n.order.triggerPrice??0})}return s.length>0&&this._config.onDecodeIssue?.({orders:i,quarantined:s}),{orders:i,quarantined:s}}async getOrders(){return(await this.getOrderBook()).orders}async getPositions(){return((await this._post("/api/v1/positionbook",{})).data??[]).map(Hc)}}function Rc(e){const t="number"==typeof e?e:"string"==typeof e&&""!==e.trim()?Number(e):NaN;return Number.isFinite(t)?t:void 0}function Fc(e){return"string"==typeof e&&""!==e.trim()?e.trim():void 0}const zc="a non-empty string",Ec="a finite number",Bc="a number of 0 or more";function $c(e,t,i,s){return{ok:!1,issue:{path:e,code:t,expected:i,got:("string"==typeof s?s:String(s)).slice(0,64)}}}const Dc={open:"working",pending:"pending",trigger_pending:"working","trigger pending":"working",complete:"filled",cancelled:"cancelled",rejected:"rejected"};function Wc(e){return Dc[e.trim().toLowerCase()]??"unknown"}function qc(e,t="order"){const i=Fc(e.orderid);if(void 0===i)return $c(`${t}.orderid`,"MISSING",zc,e.orderid);const s=Fc(e.symbol);if(void 0===s)return $c(`${t}.symbol`,"MISSING",zc,e.symbol);const r=Fc(e.action)?.toUpperCase();if("BUY"!==r&&"SELL"!==r)return $c(`${t}.action`,"UNKNOWN_ENUM","BUY or SELL",e.action);const n=Fc(e.pricetype)?.toUpperCase();if("MARKET"!==n&&"LIMIT"!==n&&"SL"!==n&&"SL-M"!==n)return $c(`${t}.pricetype`,"UNKNOWN_ENUM","MARKET, LIMIT, SL or SL-M",e.pricetype);const o=Rc(e.quantity);if(void 0===o)return $c(`${t}.quantity`,void 0===e.quantity?"MISSING":"NON_FINITE",Ec,e.quantity);if(o<0)return $c(`${t}.quantity`,"RANGE",Bc,e.quantity);const a=void 0===e.filled_quantity?0:Rc(e.filled_quantity);if(void 0===a)return $c(`${t}.filled_quantity`,"NON_FINITE",Ec,e.filled_quantity);const l=void 0===e.price?0:Rc(e.price);if(void 0===l)return $c(`${t}.price`,"NON_FINITE",Ec,e.price);if(l<0)return $c(`${t}.price`,"RANGE",Bc,e.price);const h=void 0===e.trigger_price?0:Rc(e.trigger_price);if(void 0===h)return $c(`${t}.trigger_price`,"NON_FINITE",Ec,e.trigger_price);const c=Fc(e.order_status)??"",d=Dc[c.toLowerCase()],u={id:i,symbol:s,side:r,type:n,qty:o,filledQty:a,price:l,triggerPrice:h>0?h:void 0,status:d??"unknown"};return void 0===d&&(u.rawStatus=c),{ok:!0,order:u}}function Vc(e){const t=qc(e);return t.ok?t.order:{id:Fc(e.orderid)??"",symbol:Fc(e.symbol)??"",side:Fc(e.action)??"",type:Fc(e.pricetype)??"",qty:Rc(e.quantity)??0,filledQty:Rc(e.filled_quantity)??0,price:Rc(e.price)??0,status:"unknown",rawStatus:Fc(e.order_status)??""}}function Hc(e){return{symbol:e.symbol??"",netQty:Rc(e.quantity)??0,avgPrice:Rc(e.average_price)??0}}function Gc(e){if("object"!=typeof e||null===e)return;const t=e,i=t.mode??t.data?.mode;if("analyze"===i)return"analyzer";if("live"===i)return"live";const s=t.data?.analyze_mode;return"boolean"==typeof s?s?"analyzer":"live":void 0}class Uc{constructor(e,t={}){Object.defineProperty(this,"_tf",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_zone",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_cur",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_count",{enumerable:!0,configurable:!0,writable:!0,value:0}),this._tf=e,this._zone=t.timezone}current(){return null===this._cur?null:{...this._cur}}seed(e){Pr(this._tf)&&(this._cur={...e},this._count=0)}onTick(e){const t=null===this._cur;let i=t;if(t||null===this._cur||(i="interval"===this._tf.mode||"calendar"===this._tf.mode?Cr(this._tf,e.time,this._zone)!==this._cur.time:"ticks"===this._tf.mode?this._count>=this._tf.count:(this._cur.volume??0)>=this._tf.perBar),i){const t=Cr(this._tf,e.time,this._zone);return this._cur={time:t,open:e.price,high:e.price,low:e.price,close:e.price,volume:e.qty},void 0!==e.oi&&Number.isFinite(e.oi)&&(this._cur.oi=e.oi),this._count=1,{bar:{...this._cur},isNew:!0}}const s=this._cur;return e.price>s.high&&(s.high=e.price),e.price<s.low&&(s.low=e.price),s.close=e.price,s.volume=(s.volume??0)+e.qty,void 0!==e.oi&&Number.isFinite(e.oi)?s.oi=e.oi:delete s.oi,this._count+=1,{bar:{...s},isNew:!1}}}function Kc(e){const{bucketing:t}=Sr(e);if("interval"!==t.mode)throw new Error(`openalgo-charts: interval "${e}" is ${t.mode}-bucketed and has no fixed length. Use resolveInterval() and bucketStartOf() instead of intervalToSeconds().`);return t.seconds}class Yc{constructor(e){Object.defineProperty(this,"_rest",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_ws",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_volumeMode",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_timezone",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_depthLevel",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_tickMode",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_subs",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),this._rest=new vc(e),this._ws=new jc({url:e.wsUrl,apiKey:e.apiKey,socketFactory:e.socketFactory,reconnect:e.reconnect,auth:e.auth,heartbeat:e.heartbeat}),this._volumeMode=e.volumeMode??"ltq-sum",this._timezone=e.timezone,this._depthLevel=e.depthLevel,this._tickMode="day-delta"===this._volumeMode?"Quote":"LTP",this._ws.connect()}_acquire(e,t,i,s){const r=`${e}:${t}:${i}`,n=this._subs.get(r);void 0===n?(this._subs.set(r,{count:1,level:s}),this._ws.subscribe(e,t,i,s)):(n.count+=1,void 0!==s&&(void 0===n.level||s>n.level)&&(n.level=s,this._ws.subscribe(e,t,i,s)));let o=!1;return()=>{if(o)return;o=!0;const s=this._subs.get(r);void 0!==s&&(s.count-=1,s.count>0||(this._subs.delete(r),this._ws.unsubscribe(e,t,i)))}}getBars(e){return this._rest.getBars(e)}searchSymbols(e){return this._rest.searchSymbols(e)}subscribeBars(e,t,i){const s=nn(void 0,e.variant);if(s)throw on(s,e.variant);const{bucketing:r}=Sr(e.interval),n="interval"===r.mode?this._candleReader(r,t,i):this._aggregatorReader(r,t,i),o=this._ws.onLtp(t=>{t.symbol===e.symbol&&(t.exchange&&e.exchange&&t.exchange!==e.exchange||n(t))}),a=i?.onResync?this._ws.onControl(e=>{"client_warning"===e.type&&"STREAM_RESYNC"===e.code&&i.onResync?.()}):void 0,l=this._acquire(this._tickMode,e.symbol,e.exchange);return()=>{o(),a?.(),l()}}static _tickTime(e){return e.timeSec&&e.timeSec>0?e.timeSec:Math.floor(Date.now()/1e3)}_candleReader(e,t,i){const s=new Yl({intervalSec:e.seconds,sessionAnchorSec:e.anchorSec??0,volumeMode:this._volumeMode});return i?.seedFrom&&s.seed(i.seedFrom,i.cumDayVolumeSoFar),e=>{const i=s.onTick({time:Yc._tickTime(e),price:e.ltp,ltq:e.ltq,cumDayVolume:e.volume});null!==i&&t(i.bar,i.provisional?{provisional:!0}:void 0)}}_aggregatorReader(e,t,i){const s=new Uc(e,{timezone:this._timezone});i?.seedFrom&&Pr(e)&&s.seed(i.seedFrom);const r=function(e,t){if("ltq-sum"===e)return e=>e.ltq??0;let i=t??null;return e=>{const t=e.volume??0,s=null===i?0:t<i?t:t-i;return i=t,Math.max(0,s)}}(this._volumeMode,i?.cumDayVolumeSoFar);return e=>{const i=s.onTick({time:Yc._tickTime(e),price:e.ltp,qty:r(e)});t(i.bar)}}subscribeDepth(e,t,i){const s=this._ws.onDepth((i,s,r)=>{i===e.symbol&&(s&&e.exchange&&s!==e.exchange||t(r))}),r=this._acquire("Depth",e.symbol,e.exchange,i?.depthLevel??this._depthLevel);return()=>{s(),r()}}close(){this._subs.clear(),this._ws.close()}}function Xc(e){const t=nn(void 0,e.variant);if(t)throw on(t,e.variant)}class Jc{constructor(e=60,t=Pl){Object.defineProperty(this,"intervalSec",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_schedule",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.intervalSec=e,this._schedule=t}async getBars(e){Xc(e);return Zc(e.from??17e8,500,this.intervalSec)}subscribeBars(e,t,i){Xc(e);let s=i?.seedFrom?.time??e.from??17e8,r=i?.seedFrom?.close??100,n=19088743;const o=()=>(n^=n<<13,n>>>=0,n^=n>>17,n^=n<<5,n>>>=0,n/4294967295);return this._schedule(()=>{s+=this.intervalSec;const e=r,i=Math.max(1,e+2*(o()-.5)),n=Math.max(e,i)+o(),a=Math.max(.01,Math.min(e,i)-o());t({time:s,open:e,high:n,low:a,close:i,volume:Math.round(1e3+9e3*o())}),r=i},i?.tickMs??1e3)}}function Zc(e,t,i){const s=[];let r=100,n=2654435769;const o=()=>(n^=n<<13,n>>>=0,n^=n>>17,n^=n<<5,n>>>=0,n/4294967295);for(let n=0;n<t;n++){const t=2*(o()-.5),a=r,l=Math.max(1,a+t),h=Math.max(a,l)+o(),c=Math.min(a,l)-o();s.push({time:e+n*i,open:a,high:h,low:c,close:l,volume:Math.round(1e3+9e3*o())}),r=l}return s}const Qc=1;function ed(e,t,i){const s=wr(e);if(null===s)return null;const r=s.bucketing;return"interval"===r.mode?t+r.seconds:jr(r,t,i)}function td(e){const t=rn(e.variant);return`${e.symbol}|${e.exchange}|${e.interval}${t&&"|"+t}`}function id(e){const t=new Array(e.length);for(let i=0;i<e.length;i++)t[i]={...e[i]};return t}function sd(e){e?.throwIfAborted()}class rd{constructor(e,t={}){Object.defineProperty(this,"source",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_backing",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_memory",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_tombstones",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_writes",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_ttlMs",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_max",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_maxBars",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_now",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_barCloses",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_index",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_tick",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_hits",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_misses",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_evictions",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"subscribeBars",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"subscribeDepth",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"getBarsPage",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"dataVariants",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"searchSymbols",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.source=e,this._backing=t.storage,this._ttlMs=t.ttlMs??3e5,this._max=t.max??24,this._maxBars=t.maxBars??25e4,this._now=t.now??(()=>Date.now());const i=t.timezone;this._barCloses=t.barCloses??((e,t)=>ed(e,t,i)),"function"==typeof e.subscribeBars&&(this.subscribeBars=(t,i,...s)=>e.subscribeBars(t,i,...s)),"function"==typeof e.subscribeDepth&&(this.subscribeDepth=(t,i,...s)=>e.subscribeDepth(t,i,...s)),"function"==typeof e.getBarsPage&&(this.getBarsPage=t=>e.getBarsPage(t)),"function"==typeof e.dataVariants&&(this.dataVariants=t=>e.dataVariants(t)),"function"==typeof e.searchSymbols&&(this.searchSymbols=t=>e.searchSymbols(t))}async getBars(e){if(sd(e.signal),void 0===e.from||void 0===e.to){const t=await this.source.getBars(e);return sd(e.signal),t}const t=td(e);if(!0!==e.noCache){const i=await this._lookup(t,e.interval,e.from,e.to);if(sd(e.signal),void 0!==i)return this._hits++,i;this._misses++}sd(e.signal);const i=await this.source.getBars(e);return sd(e.signal),await this._put(t,e.from,e.to,e.interval,i,e.signal),sd(e.signal),i}async getCachedBars(e){if(sd(e.signal),!0===e.noCache||void 0===e.from||void 0===e.to)return;const t=td(e),i=await this._readEntry(t,e.interval);if(sd(e.signal),void 0===i)return;if(this._now()-i.storedAt>this._ttlMs)return await this._drop(t),void sd(e.signal);const s=this._slice(i,e.from,e.to);return 0!==s.length?(this._remember(t,i),await this._evict(),sd(e.signal),s):void 0}async invalidate(e){if(void 0===e)return this.clear();const t=td(e);await this._drop(t)}async clear(){const e=new Set([...this._memory.keys(),...this._tombstones]);this._memory.clear(),this._index.clear();for(const t of e)await this._deleteBacking(t)}stats(){let e=0;for(const t of this._index.values())e+=t.bars;return{entries:this._index.size,bars:e,hits:this._hits,misses:this._misses,evictions:this._evictions}}async _lookup(e,t,i,s){const r=await this._readEntry(e,t);if(void 0===r)return;const n=this._now();if(n-r.storedAt>this._ttlMs)return void await this._drop(e);if(i<r.from)return;const o=Math.floor(n/1e3);return s>r.to&&o>=r.nextClose?void 0:(this._remember(e,r),await this._evict(),this._slice(r,i,s))}_slice(e,t,i){const s=[];for(const r of e.bars)if(!(r.time<t)){if(r.time>i)break;s.push({...r})}return s}async _put(e,t,i,s,r,n){const o=this._now(),a=Math.floor(o/1e3),l=r.filter(e=>e.time>=t&&e.time<=i);let h=l.length;for(;h>0;){const e=this._barCloses(s,l[h-1].time);if(null!==e&&e<=a)break;h--}if(0===h)return;if(h>this._maxBars)return;const c=id(l.slice(0,h)),d=c[c.length-1],u=this._barCloses(s,d.time),_=this._barCloses(s,u)??u,p={version:1,bars:c,from:t,to:Math.min(i,u-1),storedAt:o,nextClose:_};if(!this._validEntry(p,s,o))return;sd(n);const f=Symbol(e);if(this._writes.set(e,f),this._remember(e,p),await this._writeBacking(e,p),this._writes.get(e)!==f)return await this._restoreLatestBacking(e),void sd(n);!0===n?.aborted&&(this._memory.delete(e),this._index.delete(e),this._writes.delete(e),await this._deleteBacking(e),sd(n)),await this._evict(),!0===n?.aborted&&this._writes.get(e)===f&&(this._memory.delete(e),this._index.delete(e),this._writes.delete(e),await this._deleteBacking(e),sd(n)),this._writes.get(e)===f&&this._writes.delete(e)}async _drop(e){this._memory.delete(e),this._index.delete(e),await this._deleteBacking(e)}async _evict(){for(;;){let e,t=0;for(const e of this._index.values())t+=e.bars;if(this._index.size<=this._max&&t<=this._maxBars)return;let i=1/0;for(const[t,s]of this._index)s.lastUsed<i&&(i=s.lastUsed,e=t);if(void 0===e)return;this._evictions++,await this._drop(e)}}_remember(e,t){const i=this._cloneEntry(t);this._memory.set(e,i),this._index.set(e,{lastUsed:++this._tick,bars:i.bars.length})}async _readEntry(e,t){const i=this._memory.get(e);if(void 0===this._backing||this._tombstones.has(e))return i;let s;try{s=await this._backing.get(e)}catch{return i}if(void 0===s)return i;const r=this._now();if(!this._validEntry(s,t,r))return await this._deleteBacking(e),i;const n=this._cloneEntry(s);return void 0!==i&&i.storedAt>=n.storedAt?i:n}_validEntry(e,t,i){if("object"!=typeof e||null===e)return!1;const s=e;if(void 0!==s.version&&1!==s.version)return!1;if(!Number.isFinite(s.from)||!Number.isInteger(s.from))return!1;if(!Number.isFinite(s.to)||!Number.isInteger(s.to))return!1;if(!Number.isFinite(s.storedAt)||s.storedAt<0||s.storedAt>i)return!1;if(!Number.isFinite(s.nextClose)||!Number.isInteger(s.nextClose))return!1;if(s.from>s.to||s.nextClose<=s.to)return!1;if(!Array.isArray(s.bars)||0===s.bars.length)return!1;if(s.bars.length>this._maxBars||this._max<1)return!1;const r=Math.floor(i/1e3);let n=-1/0,o=null;for(const e of s.bars){if(!this._validBar(e)||e.time<=n)return!1;if(e.time<s.from||e.time>s.to)return!1;if(o=this._barCloses(t,e.time),null===o||o>r)return!1;n=e.time}const a=this._barCloses(t,o)??o;return s.nextClose===a}_validBar(e){if("object"!=typeof e||null===e)return!1;const t=e;return!!Number.isInteger(t.time)&&(!![t.open,t.high,t.low,t.close].every(Number.isFinite)&&(!(void 0!==t.volume&&!Number.isFinite(t.volume))&&(!(void 0!==t.oi&&!Number.isFinite(t.oi))&&(void 0===t.color||"string"==typeof t.color))))}_cloneEntry(e){const t={bars:id(e.bars),from:e.from,to:e.to,storedAt:e.storedAt,nextClose:e.nextClose};return void 0!==e.version&&(t.version=e.version),t}async _writeBacking(e,t){if(void 0!==this._backing)try{await this._backing.set(e,this._cloneEntry(t)),this._tombstones.delete(e)}catch{this._tombstones.add(e)}}async _deleteBacking(e){if(void 0!==this._backing)try{await this._backing.delete(e),this._tombstones.delete(e)}catch{this._tombstones.add(e)}}async _restoreLatestBacking(e){const t=this._memory.get(e);void 0===t?await this._deleteBacking(e):await this._writeBacking(e,t)}}function nd(e,t){return new rd(e,t)}let od;function ad(){if(!od){od=new Map([{id:"bullish",title:"Bullish bar",when:({bars:e,index:t})=>e[t].close>e[t].open},{id:"bearish",title:"Bearish bar",when:({bars:e,index:t})=>e[t].close<e[t].open},{id:"inside",title:"Inside bar",when:({bars:e,index:t})=>t>0&&e[t].high<e[t-1].high&&e[t].low>e[t-1].low},{id:"outside",title:"Outside bar",when:({bars:e,index:t})=>t>0&&e[t].high>e[t-1].high&&e[t].low<e[t-1].low},{id:"gap-up",title:"Gap up",when:({bars:e,index:t})=>t>0&&e[t].low>e[t-1].high},{id:"gap-down",title:"Gap down",when:({bars:e,index:t})=>t>0&&e[t].high<e[t-1].low}].map(e=>[e.id,Object.freeze(e)]))}return od}function ld(e){if("string"!=typeof e.id||!e.id.trim()||"string"!=typeof e.title||!e.title.trim()||"function"!=typeof e.when)throw new Error("Invalid bar condition");if(ad().has(e.id))throw new Error(`Bar condition id already registered: ${e.id}`);ad().set(e.id,Object.freeze({...e}))}function hd(e){return ad().delete(e)}function cd(e){return ad().get(e)}function dd(){return[...ad().values()]}const ud={armed:"#3b82f6",triggered:"#22c55e",disabled:"#64748b",expired:"#d97706"};function _d(e,t){return t?"Paused":"armed"===e?"Alert":e[0].toUpperCase()+e.slice(1)}class pd extends Fn{constructor(){super(...arguments),Object.defineProperty(this,"_movable",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_committedPrice",{enumerable:!0,configurable:!0,writable:!0,value:this.price}),Object.defineProperty(this,"_context",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_resolveScale",{enumerable:!0,configurable:!0,writable:!0,value:e=>e.priceScale}),Object.defineProperty(this,"_canAutoscale",{enumerable:!0,configurable:!0,writable:!0,value:()=>!0})}bind(e,t,i,s){this._movable=e,this._committedPrice=t,this._resolveScale=i,this._canAutoscale=s}zOrder(){return"top"}autoscaleInfo(){return this._canAutoscale()?this._context&&this._resolveScale(this._context)!==this._context.priceScale?null:{min:this._committedPrice,max:this._committedPrice}:null}draw(e,t){this._context=t;const i=this._resolveScale(t);e.save(),i!==t.priceScale&&(e.beginPath(),e.rect(0,0,t.plotWidth*t.dpr,t.plotHeight*t.dpr),e.clip()),super.draw(e,{...t,priceScale:i}),e.restore()}hitTest(e,t,i){this._context=i;const s=this._movable?super.hitTest(e,t,{...i,priceScale:this._resolveScale(i)}):null;return s?{...s,zOrder:"top",draggable:!0,cancelOnEscape:!0}:null}coordinateToPrice(e){return this._context?this._resolveScale(this._context).yToPrice(e):void 0}}function fd(e){const t=/^alert:(.+):(\d+)$/.exec(e);if(null===t)return null;const i=Number(t[2]);return 0===i||1===i?{id:t[1],index:i}:null}class md{constructor(e){Object.defineProperty(this,"_chart",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"_lines",{enumerable:!0,configurable:!0,writable:!0,value:new Map})}update(e,t,i,s){if(!this._chart.addPrimitive||!this._chart.removePrimitive)return;if(!t)return void this.remove(e.id);const r=void 0===t.upperPrice?[t.price]:[t.price,t.upperPrice],n=function(e){return"price"===e.source.kind||"indicator"===e.source.kind}(e)&&"armed"===e.state&&!i,{source:o}=e,a=()=>"indicator"===o.kind?this._chart.indicators?.().find(e=>e.id===o.instanceId)?.series(o.plotKey)?.priceScale():"price"===o.kind?this._chart.primarySeries?.()?.priceScale():void 0,l=e=>"indicator"===o.kind?a()??e.priceScale:"price"===o.kind?e.readoutPriceScale??e.priceScale:e.priceScale,h=()=>{const e=this._chart.panes?.()[t.paneIndex]?.priceScale;return void 0===e||(a()??e)===e};let c=this._lines.get(e.id);!c||c.pane===t.paneIndex&&c.lines.length===r.length||(this.remove(e.id),c=void 0),c||(c={pane:t.paneIndex,lines:[]},this._lines.set(e.id,c));for(let o=0;o<r.length;o++){const a={price:r[o],color:ud[e.state],lineStyle:"armed"===e.state?"dashed":"dotted",badge:_d(e.state,i),leftLabel:e.title+(void 0===s?"":` (${s})`)+(2===r.length?0===o?" (lower)":" (upper)":""),cursor:n?"ns-resize":void 0},d=c.lines[o];if(d){d.bind(n,r[o],l,h);const e=d.options();Object.entries(a).some(([t,i])=>e[t]!==i)&&d.setOptions(a)}else{const i=new pd({...a,id:`alert:${e.id}:${o}`});i.bind(n,r[o],l,h),c.lines.push(i),this._chart.addPrimitive(i,t.paneIndex)}}}preview(e,t){const i=fd(e);i&&this._lines.get(i.id)?.lines[i.index]?.setPrice(t)}coordinateToPrice(e,t){const i=fd(e);return i?this._lines.get(i.id)?.lines[i.index]?.coordinateToPrice(t):void 0}remove(e){const t=this._lines.get(e);if(t){this._lines.delete(e);for(const e of t.lines)this._chart.removePrimitive?.(e)}}destroy(){for(const e of this._lines.keys())this.remove(e)}}const bd=new WeakSet;let gd=1;const vd=e=>{const t=e.getDataContext();let i=t?.variant;try{i=sn(i)}catch{}return{symbol:t?.symbol,exchange:t?.exchange,interval:t?.interval,...i?{variant:{...i}}:{}}},yd=e=>{try{return rn(e.variant)}catch{return"invalid"}},wd=(e,t)=>e.symbol===t.symbol&&e.exchange===t.exchange,xd=(e,t)=>wd(e,t)&&e.interval===t.interval&&yd(e)===yd(t);class Sd{constructor(e,t={}){if(Object.defineProperty(this,"_chart",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"_records",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_hovered",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_off",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_now",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_drawings",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_spentLines",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_visuals",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_timer",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_timerAt",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_paused",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_restoring",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_replay",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_revision",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_drag",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),bd.has(e))throw new Error("An alert controller already owns this chart");this._now=t.now??(()=>Date.now()/1e3),this._drawings=t.drawings,this._spentLines=t.spentLines??"show",this._visuals=!1===t.visuals?void 0:new md(e),bd.add(e),this._off=[e.on("data:update",e=>this._onData(e)),e.on("data:context",e=>{(function(e){return"object"==typeof e&&null!==e&&!0===e[an]})(e)||this._seedAll()}),e.on("objects:change",()=>this._onObjects()),e.on("paneMoved",()=>this._onObjects()),e.on("state:restore:start",()=>{this._hovered=void 0,this._cancelDrag(),this._restoring=!0,this._revision++}),e.on("state:restore:end",()=>{this._restoring=!1,this._seedAll()}),e.on("alerts:restore",e=>this.fromJSON(e)),e.on("replay:start",()=>{this._replay=!0,this._seedAll()}),e.on("replay:stop",()=>{this._replay=!1,this._seedAll()}),e.on("destroy",()=>this.destroy()),e.on("drag:start",e=>this._startDrag(e)),e.on("hover",e=>{const t=e?.id,i="string"==typeof t?fd(t):null;this._hovered=null!==i&&this._records.has(i.id)?i.id:void 0}),e.on("drag",e=>this._onDrag(e,!1)),e.on("drag:end",e=>this._onDrag(e,!0)),e.on("drag:cancel",e=>{e.id===this._drag?.externalId&&this._cancelDrag()})];try{const t=e.alertState?.();void 0!==t&&this.fromJSON(t)}catch(e){throw this.destroy(),e}}_dragAvailability(e,t){const i=e.alert.source;return this._destroyed||this._restoring||"armed"!==e.alert.state||this._records.get(e.alert.id)!==e||"price"!==i.kind&&"indicator"!==i.kind||0!==t&&(1!==t||void 0===("price"===i.kind?i.upperPrice:i.upperValue))?{available:!1}:this.availability(e.alert.id)}hovered(){const e=void 0===this._hovered?void 0:this._records.get(this._hovered);return e&&this._dragAvailability(e,0).available?e.alert.id:void 0}_dragPrice(e,t,i){const s=e.point?.y,r=("number"==typeof s&&Number.isFinite(s)?this._visuals?.coordinateToPrice(t,s):void 0)??e.price;if("number"!=typeof r||!Number.isFinite(r))return;const n=fd(t),o=n&&this._records.get(n.id)?.alert.source,a="price"===o?.kind?this._chart.primarySeries?.()?.priceScale():"indicator"===o?.kind?this._chart.indicators?.().find(e=>e.id===o.instanceId)?.series(o.plotKey)?.priceScale():void 0,h=i??e.paneIndex,c="price"===o?.kind?this._chart.tickSchedule?.()??null:null,d=c?c.round(r):a?l(r,a.options.minMove):"number"==typeof h?this._chart.snapPrice?.(h,r)??r:r;let u=Number.isFinite(d)?d:r;if(n&&("price"===o?.kind||"indicator"===o?.kind)){const e=0===n.index?"price"===o.kind?o.upperPrice:o.upperValue:"price"===o.kind?o.price:o.value;if(void 0!==e&&(0===n.index?u>e:u<e)){u=e;const t=a?.options.minMove??0;if(c)u=c.round(e),(0===n.index?u>e:u<e)&&(u=c.step(u,0===n.index?-1:1));else if(t>0){u=l(e,t);const i=Number.EPSILON*Math.max(1,Math.abs(e))*4;Math.abs(u-e)<=i?u=e:(0===n.index?u>e:u<e)&&(u=l(e+(0===n.index?-t:t),t))}else if(!a&&"number"==typeof h&&this._chart.snapPrice&&(u=this._chart.snapPrice(h,e),!Number.isFinite(u)||(0===n.index?u>e:u<e)))return}}return u}_startDrag(e){if(this._cancelDrag(),"string"!=typeof e.id)return;const t=fd(e.id),i=t&&this._records.get(t.id);if(!t||!i)return;const s=this._dragAvailability(i,t.index),r=this._dragPrice(e,e.id,s.paneIndex);!s.available||void 0===s.paneIndex||void 0===r||void 0!==e.paneIndex&&e.paneIndex!==s.paneIndex||(this._drag={externalId:e.id,record:i,index:t.index,paneIndex:s.paneIndex,startPrice:r,startY:"number"==typeof e.point?.y?e.point.y:void 0,moved:!1})}_cancelDrag(e){const t=this._drag;if(!t||void 0!==e&&t.record.alert.id!==e)return;this._drag=void 0;const i=this._records.get(t.record.alert.id);i&&this._syncVisual(i)}_onDrag(e,t){this._expireDue();const i=this._drag;if(!i||e.id!==i.externalId)return;const s=this._dragAvailability(i.record,i.index);if(!s.available||s.paneIndex!==i.paneIndex||void 0!==e.paneIndex&&e.paneIndex!==i.paneIndex)return void this._cancelDrag();const r=this._dragPrice(e,i.externalId,i.paneIndex);if(void 0===r)return void(t&&this._cancelDrag());const n=e.point?.y;i.moved||(i.moved=void 0!==i.startY&&"number"==typeof n?n!==i.startY:r!==i.startPrice);const o=i.record.alert.source;if("price"!==o.kind&&"indicator"!==o.kind)return void this._cancelDrag();const a="price"===o.kind?o.price:o.value,l="price"===o.kind?o.upperPrice:o.upperValue,h=0===i.index?Math.min(r,l??1/0):Math.max(r,a);if(!t)return void(i.moved&&this._visuals?.preview(i.externalId,h));if(this._cancelDrag(),!i.moved||h===(0===i.index?a:l))return;const c="price"===o.kind?{...o,...0===i.index?{price:h}:{upperPrice:h}}:{...o,...0===i.index?{value:h}:{upperValue:h}};this.update(i.record.alert.id,{source:c}),this._chart.emit("alerts:changed",{id:i.record.alert.id,reason:"dragged"})}add(e){this._assertAlive();let t=e.id;if(void 0===t)do{t="alert-"+gd++}while(this._records.has(t));if(this._records.has(t))throw new Error(`Duplicate alert id: ${t}`);const i={...e,id:t,source:{...e.source},condition:e.condition??("barCondition"===e.source.kind?"matches":"crossing"),policy:e.policy??"onBarClose",repeat:e.repeat??"once",state:e.state??"armed",title:e.title??"Chart alert",cooldownSeconds:e.cooldownSeconds??0,scope:vd(this._chart)};cn(i),i.source=dn(i).source,"armed"===i.state&&void 0!==i.expiresAt&&this._now()>=i.expiresAt&&(i.state="expired");const s={alert:i};return this._seed(s),this._records.set(t,s),this._syncVisual(s),this._scheduleExpiry(),this._saveState(),this._chart.emit("alert:created",{alert:dn(i)}),dn(i)}update(e,t){this._assertAlive();const i=this._records.get(e);if(!i)return;const s={...i.alert,...t,id:e,source:{...t.source??i.alert.source}};cn(s),s.source=dn(s).source,this._cancelDrag(e),"armed"===s.state&&void 0!==s.expiresAt&&this._now()>=s.expiresAt&&(s.state="expired");const r={...i,alert:s};return this._seed(r),this._records.set(e,r),this._syncVisual(r),this._scheduleExpiry(),this._saveState(),this._chart.emit("alert:updated",{alert:dn(s)}),dn(s)}remove(e){return this._remove(e,"removed")}_remove(e,t){const i=this._records.get(e);return!!i&&(this._cancelDrag(e),this._records.delete(e),this._hovered===e&&(this._hovered=void 0),this._visuals?.remove(e),this._scheduleExpiry(),this._saveState(),this._chart.emit("alert:removed",{alert:dn(i.alert),reason:t}),!0)}list(){return[...this._records.values()].map(e=>dn(e.alert))}toJSON(){return fn({version:1,alerts:this.list()})}fromJSON(e){this._assertAlive();const t=fn(e),i=new Map,s=[],r=vd(this._chart);for(const e of t.alerts){const t=e.source;let n;this._drawingGone(e,r)&&(n="drawing-missing");const o="indicator"===t.kind?t:"drawing"===t.kind?t.input:void 0;if(!n&&o&&this._chart.indicators){const e=this._chart.indicators().find(e=>e.id===o.instanceId);e?e.series(o.plotKey)||(n="plot-missing"):n="indicator-missing"}if(n){s.push({alert:e,reason:n});continue}if("armed"===e.state&&void 0!==e.expiresAt&&this._now()>=e.expiresAt&&(e.state="expired"),void 0!==e.lastTriggeredTime){const t="onTouch"===e.policy?"lastTouchedTime":"lastClosedTime";e[t]=Math.max(e[t]??-1/0,e.lastTriggeredTime)}const a={alert:e};this._seed(a),i.set(e.id,a)}this._cancelDrag(),this._revision++,this._visuals?.destroy(),this._hovered=void 0,this._records.clear();for(const[e,t]of i)this._records.set(e,t),this._syncVisual(t);this._scheduleExpiry(),this._saveState();for(const e of s){if(this._destroyed)break;this._chart.emit("alert:removed",{...e,alert:dn(e.alert)})}this._destroyed||this._chart.emit("alerts:restored",{alerts:this.list()})}_saveState(){this._destroyed||this._chart.setAlertState?.({version:1,alerts:this.list()})}availability(e){const t=this._records.get(e);if(!t)return{available:!1,reason:"Alert is unavailable"};if(this._paused||this._replay)return{available:!1,reason:"Alerts are paused"};const i=vd(this._chart);if(!wd(t.alert.scope,i))return{available:!1,reason:"Instrument context differs"};if(t.alert.scope.interval!==i.interval)return{available:!1,reason:`Switch to ${t.alert.scope.interval??"the original timeframe"} to evaluate this alert`};if(yd(t.alert.scope)!==yd(i))return{available:!1,reason:"Switch to the data variant this alert was set on to evaluate it"};const{source:s}=t.alert,r=this._chart.primaryBars();if("drawing"===s.kind){const e=this._drawings?.alertInfo(s.drawingId);if(!e?.available)return{available:!1,reason:e?.reason??"Drawing provider is unavailable"};if(e.paneIndex!==this._pricePane()&&!s.input)return{available:!1,reason:"Select an input plot for this drawing pane"};const i=this._drawingValue(t.alert,r[r.length-1]?.time);return i?{available:!0,paneIndex:i.paneIndex}:{available:!1,reason:"Drawing level, time, input plot or condition is unavailable",paneIndex:e.paneIndex}}if("indicator"===s.kind){const e=this._plot(s);return e.values?Number.isFinite(e.values[r.length-1])?{available:!0,paneIndex:e.paneIndex}:{available:!1,reason:"Plot value is unavailable",paneIndex:e.paneIndex}:{available:!1,reason:e.reason}}return"barCondition"!==s.kind||cd(s.id)?r.length?{available:!0,paneIndex:this._pricePane()}:{available:!1,reason:"Source data is unavailable"}:{available:!1,reason:"Bar condition is unavailable"}}enable(e){return this.update(e,{state:"armed"})}disable(e){return this.update(e,{state:"disabled"})}setPaused(e){this._assertAlive(),this._paused!==e&&(this._paused=e,this._seedAll())}destroy(){if(!this._destroyed){this._destroyed=!0,this._drag=void 0,this._revision++;for(const e of this._off)e();this._clearTimer(),this._visuals?.destroy(),this._hovered=void 0,this._records.clear(),bd.delete(this._chart)}}_drawingGone(e,t){const i=e.source;return"drawing"===i.kind&&void 0!==this._drawings&&wd(e.scope,t)&&null==this._drawings.get(i.drawingId)}_pricePane(){return this._chart.primaryPaneIndex?.()??0}_assertAlive(){if(this._destroyed)throw new Error("Alert controller is destroyed")}_seed(e){if(!xd(e.alert.scope,vd(this._chart)))return e.tail=void 0,e.index=void 0,e.value=void 0,void(e.plotPane=void 0);const t=this._chart.primaryBars(),i=t[t.length-1];if(e.tail=i?{...i}:void 0,e.index=i?t.length-1:void 0,"indicator"===e.alert.source.kind){const i=this._plot(e.alert.source);e.value=this._reading(i.values,t.length-1),e.plotPane=i.paneIndex}else"drawing"===e.alert.source.kind&&e.alert.source.input&&(e.value=this._reading(this._plot(e.alert.source.input).values,t.length-1));const s=t[t.length-2]?.time;void 0!==s&&(e.alert.lastClosedTime=Math.max(e.alert.lastClosedTime??-1/0,s))}_seedAll(){this._cancelDrag(),this._revision++;for(const e of this._records.values())this._seed(e),this._syncVisual(e);this._restoring||(this._saveState(),!this._destroyed&&this._records.size&&this._chart.emit("alerts:checkpoint",{}))}_onObjects(){if(this._destroyed||this._restoring)return;this._cancelDrag();const e=++this._revision,t=vd(this._chart);for(const i of[...this._records.values()]){if(this._destroyed||this._revision!==e)break;const s=i.alert.source;this._drawingGone(i.alert,t)?this._remove(i.alert.id,"drawing-removed"):"indicator"===s.kind||"drawing"===s.kind?(this._seed(i),this._syncVisual(i)):"price"===s.kind&&this._syncVisual(i)}}_syncVisual(e){if(!this._visuals)return;const{alert:t}=e,{source:i}=t;if("hide"===this._spentLines&&("triggered"===t.state||"expired"===t.state))return void this._visuals.update(t,void 0,!0);const s=vd(this._chart),r=xd(t.scope,s);let n;var o,a;if("price"===i.kind&&(o=t.scope,wd(o,a=s)&&o.variant?.currency===a.variant?.currency&&o.variant?.unit===a.variant?.unit)&&(n={price:i.price,upperPrice:i.upperPrice,paneIndex:this._pricePane()}),r&&("indicator"===i.kind&&void 0!==e.plotPane&&(n={price:i.value,upperPrice:i.upperValue,paneIndex:e.plotPane}),"drawing"===i.kind)){const e=this._chart.primaryBars();n=this._drawingValue(t,e[e.length-1]?.time)}this._visuals.update(t,n,this._paused||this._replay||!r&&"armed"===t.state,r?void 0:((e,t)=>{const i=e.interval??"original timeframe";if(yd(e)===yd(t))return i;const s=e.variant,r=s?[s.session,s.adjustment,s.currency,s.unit].filter(Boolean):[];return`${i}, ${r.length?r.join(" "):"default series"}`})(t.scope,s))}_onData(e){if(this._destroyed||this._restoring)return;const t=++this._revision;if(this._expireDue(),this._drag&&!this._dragAvailability(this._drag.record,this._drag.index).available&&this._cancelDrag(),this._destroyed||t!==this._revision)return;if(this._paused||this._replay)return;if("update"!==e.kind)return void this._seedAll();const i=this._chart.primaryBars(),s=i[i.length-1];if(!s||e.time!==s.time)return;const r=vd(this._chart),n=[...this._records.values()].filter(e=>xd(e.alert.scope,r)).map(e=>({record:e,from:this._firstNew(e,i)}));let o=!1;for(let e=Math.min(...n.map(e=>e.from));e<i.length&&t===this._revision;e++)for(const{record:s,from:r}of n){if(this._destroyed||t!==this._revision)break;e>=r&&this._records.get(s.alert.id)===s&&(o=this._judge(s,i,e,t)||o)}o&&(this._saveState(),this._destroyed||this._chart.emit("alerts:checkpoint",{}))}_firstNew(e,t){const i=t.length-1,{tail:s,index:r}=e;if(!s||void 0===r||r>=i)return i;const n=t[r];return!n||n.time<s.time?i:(a=s,(o=n).time===a.time&&o.open===a.open&&o.high===a.high&&o.low===a.low&&o.close===a.close&&o.volume===a.volume&&o.oi===a.oi?r+1:r);var o,a}_judge(e,t,i,s){const r=t[i],n=e.tail;if(e.tail={...r},e.index=i,"drawing"===e.alert.source.kind&&i===t.length-1&&n?.time!==r.time&&this._syncVisual(e),this._destroyed||s!==this._revision||"armed"!==e.alert.state||!n)return!1;const{alert:o}=e;if("onBarClose"===o.policy){const a=t[i-1];if(r.time<=n.time||!a||a.time!==n.time||void 0!==o.lastClosedTime&&a.time<=o.lastClosedTime)return!1;o.lastClosedTime=a.time;const l=this._closedMatch(e,t,i-1);return void 0!==l&&s===this._revision&&this._records.get(o.id)===e&&this._trigger(e,a,i-1,l),!0}if(r.time<n.time||void 0!==o.lastTouchedTime&&r.time<=o.lastTouchedTime)return!1;const a=this._touchMatch(e,t,i,n);return void 0!==a&&s===this._revision&&this._records.get(o.id)===e&&(o.lastTouchedTime=r.time,this._trigger(e,r,i,a),!0)}_plot(e){const t=this._chart.indicators?.().find(t=>t.id===e.instanceId);if(!t)return{reason:"Indicator instance is unavailable"};if(!t.series(e.plotKey))return{reason:"Indicator plot is unavailable"};const i=t.values()[e.plotKey],s=t.indicatorId&&us(t.indicatorId)&&ds(t.indicatorId).plots.some(t=>t.key===e.plotKey&&t.overlay);return i?{values:i,paneIndex:s?this._pricePane():t.paneIndex}:{reason:"Indicator plot is unavailable"}}_reading(e,t){const i=e?.[t];return"number"==typeof i&&Number.isFinite(i)?i:void 0}_closedMatch(e,t,i){const{source:s,condition:r}=e.alert;if("barCondition"===s.kind)return this._barMatch(e,t,i)?t[i].close:void 0;if("drawing"===s.kind){const n=this._drawingValue(e.alert,t[i].time),o=this._drawingValue(e.alert,t[i-1]?.time),a=s.input?this._plot(s.input).values:void 0,l=s.input?this._reading(a,i):t[i].close,h=s.input?this._reading(a,i-1):t[i-1]?.close;if(!n||void 0===l)return;return Zr(r,o?h:void 0,l,n.price,n.upperPrice,o?.price,o?.upperPrice)?l:void 0}if("price"===s.kind)return Zr(r,t[i-1]?.close,t[i].close,s.price,s.upperPrice)?t[i].close:void 0;const n=this._plot(s).values,o=this._reading(n,i);return void 0!==o&&Zr(r,this._reading(n,i-1),o,s.value,s.upperValue)?o:void 0}_touchMatch(e,t,i,s){const{source:r,condition:n}=e.alert,o=t[i];if("barCondition"===r.kind)return this._barMatch(e,t,i)?o.close:void 0;if("drawing"===r.kind)return this._drawingTouch(e,r,t,i,s);if("indicator"===r.kind){const t=this._plot(r).values,a=this._reading(t,i),l=o.time>s.time?this._reading(t,i-1):e.value;if(e.value=a,void 0===a||a===l&&o.time===s.time)return;return Zr(n,l,a,r.value,r.upperValue)?a:void 0}const a=o.time>s.time;if(!(a||o.close!==s.close||o.high>s.high||o.low<s.low))return;const l=Math.max(s.close,o.close,a||o.high>s.high?o.high:-1/0),h=Math.min(s.close,o.close,a||o.low<s.low?o.low:1/0);return Qr(n,s.close,h,l,r.price,r.upperPrice)}_barMatch(e,t,i){const s=e.alert.source;if("barCondition"!==s.kind||e.errorTime===t[i].time)return!1;const r=cd(s.id);if(!r)return!1;try{return!0===r.when({bars:t.slice(0,i+1),index:i})}catch(s){return e.errorTime=t[i].time,this._chart.emit("alert:error",{alert:dn(e.alert),error:s}),!1}}_drawingValue(e,t){const i=e.source;if("drawing"!==i.kind||void 0===t)return;const s=this._drawings?.valueAt(i.drawingId,t,i.level);if(!s||s.paneIndex!==this._pricePane()&&!i.input)return;if(i.input&&this._plot(i.input).paneIndex!==s.paneIndex)return;return("enteringRange"===e.condition||"leavingRange"===e.condition)===(void 0!==s.upperPrice)?s:void 0}_drawingTouch(e,t,i,s,r){const n=i[s],o=this._drawingValue(e.alert,n.time),a=this._drawingValue(e.alert,r.time);if(t.input){const i=this._plot(t.input).values,l=this._reading(i,s),h=n.time>r.time?this._reading(i,s-1):e.value;if(e.value=l,!o||void 0===l||l===h&&n.time===r.time)return;return Zr(e.alert.condition,a?h:void 0,l,o.price,o.upperPrice,a?.price,a?.upperPrice)?l:void 0}if(!o||!a)return;const l=n.time>r.time;if(!l&&n.close===r.close&&n.high<=r.high&&n.low>=r.low)return;const h=Math.max(r.close,n.close,l||n.high>r.high?n.high:-1/0),c=Math.min(r.close,n.close,l||n.low<r.low?n.low:1/0);return Qr(e.alert.condition,r.close,c,h,o.price,o.upperPrice,a.price,a.upperPrice)}_trigger(e,t,i,s){const{alert:r}=e,n=this._now();if(void 0!==r.expiresAt&&n>=r.expiresAt)return void this._expireDue();if(void 0!==r.lastTriggeredAt&&n-r.lastTriggeredAt<r.cooldownSeconds)return;this._cancelDrag(r.id),r.lastTriggeredAt=n,r.lastTriggeredTime=t.time,"once"===r.repeat&&(r.state="triggered"),this._syncVisual(e),this._scheduleExpiry(),this._saveState();const o={alertId:r.id,title:r.title,message:r.message,time:t.time,index:i,price:s,alert:dn(r)};this._chart.emit("alert:triggered",o)}_expireDue(){const e=this._now(),t=[];for(const i of this._records.values()){const{alert:s}=i;"armed"===s.state&&void 0!==s.expiresAt&&e>=s.expiresAt&&(this._cancelDrag(s.id),s.state="expired",this._syncVisual(i),t.push(i))}this._scheduleExpiry(),t.length&&this._saveState();for(const e of t){if(this._destroyed)break;this._records.get(e.alert.id)===e&&this._chart.emit("alert:expired",{alert:dn(e.alert)})}}_clearTimer(){void 0!==this._timer&&clearTimeout(this._timer),this._timer=void 0,this._timerAt=void 0}_scheduleExpiry(){if(this._destroyed)return;let e;for(const{alert:t}of this._records.values())"armed"===t.state&&void 0!==t.expiresAt&&(e=Math.min(e??1/0,t.expiresAt));e!==this._timerAt&&(this._clearTimer(),void 0!==e&&(this._timerAt=e,this._timer=Ol(()=>{this._timer=void 0,this._timerAt=void 0,this._destroyed||this._expireDue()},Math.max(1,1e3*(e-this._now())))))}}const Pd={crossing:"Crossing",crossingUp:"Crossing up",crossingDown:"Crossing down",greaterThan:"Above",lessThan:"Below",enteringRange:"Entering range",leavingRange:"Leaving range",matches:"Matches"},Od=["crossing","crossingUp","crossingDown","greaterThan","lessThan"],kd=["enteringRange","leavingRange"];function Id(e,t){const i="barCondition"===e.kind?["matches"]:"drawing"===e.kind?"band"===e.level?kd:Od:[...Od,...kd],s=t&&i.includes(t)?t:i[0],r=kd.includes(s),n=[{key:"title",type:"text",label:"Name",default:"Chart alert"},{key:"condition",type:"select",label:"Condition",default:s,options:i.map(e=>({value:e,label:Pd[e]}))}];if("price"===e.kind||"indicator"===e.kind){const t="price"===e.kind;n.push({key:t?"price":"value",type:"number",label:r?"Lower bound":"Threshold",default:t?e.price:e.value,step:.01}),r&&n.push({key:t?"upperPrice":"upperValue",type:"number",label:"Upper bound",default:t?e.upperPrice??e.price:e.upperValue??e.value,step:.01})}return n.push({key:"policy",type:"select",label:"Evaluate",default:"onBarClose",options:[{value:"onBarClose",label:"Bar close"},{value:"onTouch",label:"Intrabar touch"}]},{key:"repeat",type:"select",label:"Repeat",default:"once",options:[{value:"once",label:"Once"},{value:"everyTime",label:"Every match"}]},{key:"cooldownSeconds",type:"number",label:"Cooldown (seconds)",default:0,min:0,step:1},{key:"expiresAt",type:"text",label:"Expires",default:"",tooltip:"Leave empty for no expiry."},{key:"message",type:"text",label:"Message",default:""},{key:"enabled",type:"boolean",label:"Enabled",default:!0}),n}export{jn as ALT_PRESET,Sd as AlertController,Qc as BAR_CACHE_VERSION,An as BUILTIN_COMMANDS,rd as BarCache,dh as BuySellButtons,ao as CHART_STATE_VERSION,Yl as CandleBuilder,Li as Canvas2dBackend,ma as Chart,Ea as ChartObjects,jo as ChartTable,wa as ComparisonController,Kl as DEFAULT_CANDLE_BUILDER_OPTIONS,V as DEFAULT_CANDLE_STYLE,wo as DEFAULT_CHART_TABLE_OPTIONS,xe as DEFAULT_HISTOGRAM_STYLE,Cn as DEFAULT_KEYMAP,g as DEFAULT_PRICE_SCALE_OPTIONS,Yi as DEFAULT_THEME,ht as DEFAULT_TIMEZONE,to as DEFAULT_TIME_NAVIGATOR_OPTIONS,w as DEFAULT_TIME_SCALE_OPTIONS,zn as DEFAULT_TRADING_COLORS,Wi as DEFAULT_ZOOM_GLIDE_OPTIONS,mc as DataLoadingController,Qn as EventMarkers,Jc as FakeDataFeed,lc as HistoryRequestPool,rs as INDICATOR_LINE_STYLES,as as INDICATOR_PLOT_STYLES,ls as INDICATOR_SOURCES,Xe as IST_OFFSET_SECONDS,or as IndicatorBackground,nr as IndicatorDrawings,ir as IndicatorFill,es as IndicatorInputError,Jh as Instrument,i as InvalidationLevel,Fl as LINK_CROSSHAIR_ALPHA,zl as LinkCrosshair,Hl as LinkGroup,oo as LogoWatermark,vc as OpenAlgoDataFeed,Yc as OpenAlgoLiveDataFeed,Nc as OpenAlgoTradeFeed,jc as OpenAlgoWsFeed,Jl as PRICE_LEVEL_KINDS,m as PRICE_SCALE_MODES,Bi as Pane,Oi as PaneLegend,nh as PriceLevels,Fn as PriceLine,y as PriceScale,Ml as ReplayController,Rl as ReplayGroup,oh as ReplayShade,D as SCALE_FONT_MAX,$ as SCALE_FONT_MIN,er as SeriesMarkers,Yh as SessionCalendar,lh as SessionShade,Nn as ShortcutManager,ha as SvgContext,ta as SvgLinearGradient,da as TextWatermark,Uc as TickBarAggregator,Th as TickSchedule,io as TimeNavigator,x as TimeScale,qn as TradeMarkersPrimitive,ic as TradingCapabilityError,Vn as TradingController,_r as UnknownIntervalError,e as VERSION,qi as ZoomGlide,Pa as addComparison,Id as alertSettingsSchema,ya as alignToPrimary,ml as applyChartSettings,sc as assertTradingCapability,bh as atr,ch as attachSessionShading,v as autoscaleRange,ki as backendDegradation,Tc as backoffDelayMs,td as barCacheKey,ed as barCloseSec,Kn as beginPick,Xl as bestHit,h as bitmapSize,Cr as bucketStartOf,Nh as calendarMarketPhase,zt as calendarPeriodFlags,K as candleGeometry,U as candleTier,pl as chartSettingsSchema,tc as checkTradingCapability,o as clamp,Ic as classifyAuthAck,Gi as compactVolume,Sa as comparisonController,th as computePriceLevels,Re as conflateBars,Fe as conflateItems,Le as conflationGroupSize,ba as createChart,Gl as createLinkGroup,Ai as createRenderBackend,Ui as darkTheme,z as dashPattern,on as dataVariantError,rn as dataVariantKey,qc as decodeOrder,Ds as drawLabel,Es as drawShape,zs as effectiveMarkerPx,uh as ema,_h as emaSeries,Je as epochMsToUtcSeconds,In as eventToCombo,Ra as exportChartDataCsv,$a as filterLinkAppearance,El as followerIndex,Bl as followerRange,kn as formatCombo,at as formatIstCrosshairLabel,nt as formatIstDate,tt as formatIstTime,it as formatIstTimeSeconds,xc as formatSubscribe,Sc as formatUnsubscribe,Nt as formatZonedCrosshairLabel,At as formatZonedDate,Ct as formatZonedTime,jt as formatZonedTimeSeconds,le as fromGradient,Zc as generateBars,cd as getBarCondition,je as getChartType,ds as getIndicator,Go as getSeriesTransform,us as hasIndicator,Wt as inSessionAt,ps as indicatorDefaults,os as indicatorStyleInputs,Ar as intervalParts,Kc as intervalToSeconds,Fr as isDailyInterval,Rr as isIntradayInterval,xr as isKnownInterval,lt as isNewIstDay,vt as isNewZonedDay,wt as isNewZonedMonth,Pt as isNewZonedPeriod,xt as isNewZonedQuarter,yt as isNewZonedWeek,St as isNewZonedYear,b as isRebasing,go as isReplaying,Tn as isReservedCombo,Nr as isSecondsInterval,zr as isTickInterval,Pr as isTimeBucketed,Pn as isValidCombo,_t as isValidTimezone,Ko as isWhitespace,Ze as istStringToUtcSeconds,sh as lastPriceLevelFromSeriesStyle,a as lerp,Ki as lightTheme,gc as mapHistoryResponse,Vc as mapOrder,Wc as mapOrderStatus,Hc as mapPosition,Fs as markerSizePx,Lh as marketStatusAt,Ne as mergeBars,jr as nextBucketStart,p as niceTicks,Sn as normalizeCombo,sn as normalizeDataVariant,G as optimalBarWidth,fn as parseAlertsDocument,xn as parseCombo,js as parseIndicatorPolicy,Cc as parseMessage,_o as parsePaneState,$t as parseSessionSpec,kc as parseTopic,ns as plotStyleKeys,f as precisionForStep,ln as publishDataContext,fl as readChartSettings,Mc as readSequence,ld as registerBarCondition,Ce as registerChartType,cs as registerIndicator,gr as registerInterval,Mi as registerRenderBackend,Sh as registerSeriesTransform,dd as registeredBarConditions,Ae as registeredChartTypes,_s as registeredIndicators,yr as registeredIntervals,Ci as registeredRenderBackends,Uo as registeredSeriesTransforms,pi as resolveCrosshairStyle,E as resolveGridStyle,Sr as resolveInterval,q as resolvePlotMargins,ji as resolveRenderBackend,W as resolveScaleStyle,l as roundToTick,bc as rowTimeToUtcSeconds,ph as rsi,fh as rsiSeries,rh as seriesStyleForLastPriceLevel,qt as sessionFlags,Ft as sessionStartFlags,Rt as sessionStartIndices,cc as sharedHistoryRequests,c as snapToDevicePixel,fs as sourceValue,ms as sourceValues,It as startOfZonedDay,Tt as startOfZonedMonth,Mt as startOfZonedWeek,gh as supertrend,vh as supertrendSeries,To as tableOrigin,Yo as toBar,mh as trueRange,wr as tryResolveInterval,hd as unregisterBarCondition,vr as unregisterInterval,Ti as unregisterRenderBackend,nn as unsupportedDataVariant,st as utcSecondsToIstDateString,Qe as utcSecondsToIstParts,Lt as utcSecondsToZonedDateString,ft as utcSecondsToZonedParts,t as version,ae as verticalGradient,no as watermarkRect,ie as withAlpha,nd as withBarCache,gt as zoneOffsetSeconds,mt as zonedDayIndex,Ot as zonedStringToUtcSeconds,kt as zonedWallClockToUtcSeconds,bt as zonedWeekIndex};
//# sourceMappingURL=openalgo-charts.mjs.map