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 133 kB
function t(t,e,n,i){const o=i.x-n.x,r=i.y-n.y,s=o*o+r*r;if(0===s)return Math.hypot(t-n.x,e-n.y);let l=((t-n.x)*o+(e-n.y)*r)/s;return l=l<0?0:l>1?1:l,Math.hypot(t-(n.x+l*o),e-(n.y+l*r))}function e(t,e,n,i){const o=i.x-n.x,r=i.y-n.y,s=Math.hypot(o,r);return 0===s?Math.hypot(t-n.x,e-n.y):Math.abs(r*(t-n.x)-o*(e-n.y))/s}function n(t,e){return Math.abs(t-e)}function i(t,e){return Math.abs(t-e)}function o(e,n,i){let o=1/0;for(let r=1;r<i.length;r++){const s=t(e,n,i[r-1],i[r]);s<o&&(o=s)}return o}function r(t,e){return{x0:Math.min(t.x,e.x),y0:Math.min(t.y,e.y),x1:Math.max(t.x,e.x),y1:Math.max(t.y,e.y)}}function s(t){let e=1/0,n=1/0,i=-1/0,o=-1/0;for(const r of t)r.x<e&&(e=r.x),r.y<n&&(n=r.y),r.x>i&&(i=r.x),r.y>o&&(o=r.y);return{x0:e,y0:n,x1:i,y1:o}}function l(e,n,i,o,s){const l=r(i,o);if(e>=l.x0&&e<=l.x1&&n>=l.y0&&n<=l.y1&&s)return 0;const a=[{x:l.x0,y:l.y0},{x:l.x1,y:l.y0},{x:l.x1,y:l.y1},{x:l.x0,y:l.y1}];let c=1/0;for(let i=0;i<4;i++){const o=t(e,n,a[i],a[(i+1)%4]);o<c&&(c=o)}return c}function a(t,e,n,i,o){const s=r(n,i),l=(s.x0+s.x1)/2,a=(s.y0+s.y1)/2,c=Math.max(1e-6,(s.x1-s.x0)/2),h=Math.max(1e-6,(s.y1-s.y0)/2),d=(t-l)/c,p=(e-a)/h,u=Math.hypot(d,p);return o&&u<=1?0:Math.abs(u-1)*Math.min(c,h)}function c(t,e,n,i,o){const r=e.x-t.x;if(0===r)return[t,e];const s=(e.y-t.y)/r;return[i?{x:0,y:t.y+(0-t.x)*s}:t,o?{x:n,y:t.y+(n-t.x)*s}:e]}function h(t){const e=function(t){const e=t.trim();if(e.startsWith("#")){const t=e.slice(1);if(3===t.length||4===t.length){const[e,n,i,o]=[...t].map(t=>parseInt(t+t,16));return[e,n,i].some(Number.isNaN)?null:{r:e,g:n,b:i,a:4===t.length?o/255:1}}if(6===t.length||8===t.length){const e=parseInt(t.slice(0,6),16);if(Number.isNaN(e))return null;const n=8===t.length?parseInt(t.slice(6,8),16)/255:1;return{r:e>>16&255,g:e>>8&255,b:255&e,a:Number.isNaN(n)?1:n}}return null}const n=/^rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*(?:,\s*([\d.]+)\s*)?\)$/i.exec(e);return null===n?null:{r:Number(n[1]),g:Number(n[2]),b:Number(n[3]),a:void 0===n[4]?1:Number(n[4])}}(t);if(null===e)return.5;const n=t=>{const e=t/255;return e<=.03928?e/12.92:Math.pow((e+.055)/1.055,2.4)};return.2126*n(e.r)+.7152*n(e.g)+.0722*n(e.b)}function d(t){return h(t)>.45?"#10131a":"#ffffff"}function p(t,e,n,i,o,r){const s=t.roundRect;"function"==typeof s?s.call(t,e,n,i,o,Math.max(0,Math.min(r,i/2,o/2))):t.rect(e,n,i,o)}const u=[{value:"solid",label:"Solid"},{value:"dashed",label:"Dashed"},{value:"dotted",label:"Dotted"}],x=[{value:"left",label:"Left"},{value:"center",label:"Center"},{value:"right",label:"Right"}],f=[{value:"top",label:"Top"},{value:"middle",label:"Middle"},{value:"bottom",label:"Bottom"}],y=[{value:"inside",label:"Inside"},{value:"outside",label:"Outside"}],g=[{value:"ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif",label:"Sans"},{value:"ui-serif, Georgia, Times New Roman, serif",label:"Serif"},{value:"ui-monospace, Menlo, Consolas, monospace",label:"Mono"}],m={path:"style.color",label:"Color",kind:"color",group:"line"},b={path:"style.lineWidth",label:"Width",kind:"number",min:1,max:12,step:.5,group:"line"},M={path:"style.lineStyle",label:"Line style",kind:"lineStyle",options:u,group:"line"},v=[m,b,M],w=[{path:"style.fill",label:"Fill",kind:"boolean",group:"fill"},{path:"style.fillColor",label:"Fill color",kind:"color",group:"fill"},{path:"style.fillOpacity",label:"Fill opacity",kind:"opacity",min:0,max:1,step:.01,group:"fill"}],_=[{path:"style.extendLeft",label:"Extend left",kind:"boolean",group:"behavior"},{path:"style.extendRight",label:"Extend right",kind:"boolean",group:"behavior"}],k={path:"style.showLabels",label:"Show labels",kind:"boolean",group:"behavior"},S=[{path:"style.levels",label:"Levels",kind:"levels",group:"levels"},{...k,group:"levels"}],T=[{path:"text.color",label:"Text color",kind:"color",group:"text"},{path:"text.fontSize",label:"Font size",kind:"number",min:6,max:96,step:1,group:"text"},{path:"text.fontFamily",label:"Font",kind:"select",options:g,group:"text"},{path:"text.bold",label:"Bold",kind:"boolean",group:"text"},{path:"text.italic",label:"Italic",kind:"boolean",group:"text"}],P={path:"text.value",label:"Text",kind:"text",group:"text"},L=[P,...T,{path:"text.align",label:"Align",kind:"select",options:x,group:"text"},{path:"text.valign",label:"Vertical align",kind:"select",options:f,group:"text"},{path:"text.wrap",label:"Wrap",kind:"boolean",group:"text"},{path:"text.wrapWidth",label:"Wrap width",kind:"number",min:40,max:2e3,step:10,group:"text"},{path:"text.background",label:"Background",kind:"boolean",group:"text"},{path:"text.backgroundColor",label:"Background color",kind:"color",group:"text"},{path:"text.backgroundOpacity",label:"Background opacity",kind:"opacity",min:0,max:1,step:.01,group:"text"},{path:"text.border",label:"Border",kind:"boolean",group:"text"},{path:"text.borderColor",label:"Border color",kind:"color",group:"text"}],F=[P,...T,{path:"text.align",label:"Align",kind:"select",options:x,group:"text"},{path:"text.valign",label:"Vertical align",kind:"select",options:f,group:"text"},{path:"text.wrap",label:"Wrap",kind:"boolean",group:"text"},{path:"text.position",label:"Label position",kind:"select",options:y,group:"text"}],I=[P,...T,{path:"text.backgroundColor",label:"Plate color",kind:"color",group:"text"},{path:"text.backgroundOpacity",label:"Plate opacity",kind:"opacity",min:0,max:1,step:.01,group:"text"},{path:"text.border",label:"Border",kind:"boolean",group:"text"},{path:"text.borderColor",label:"Border color",kind:"color",group:"text"}];function z(t,e={}){const n=new Set,i=[];for(const e of t)for(const t of Array.isArray(e)?e:[e])n.has(t.path)||(n.add(t.path),i.push(t));return!0===e.textIsContent?{fields:i,textIsContent:!0}:{fields:i}}const C=new Set(["style","text","props"]),A=new Set(["locked","visible","zIndex"]);function N(t){const e=t.split(".");return 1===e.length&&A.has(e[0])?{root:e[0],key:null}:2===e.length&&C.has(e[0])&&""!==e[1]?{root:e[0],key:e[1]}:null}function $(t,e){const n=N(e);if(null===n)return;if(null===n.key)return t[n.root];const i=t[n.root],o=i?.[n.key];return Array.isArray(o)?o.map(t=>({...t})):o}function O(t,e){const n={};for(const i of e.fields)n[i.path]=$(t,i.path);return n}const D=new Set(["solid","dashed","dotted"]);function W(t){return"boolean"==typeof t?t:"true"===t||1===t||"1"===t||"false"!==t&&0!==t&&"0"!==t&&void 0}function H(t){const e="number"==typeof t?t:"string"==typeof t&&""!==t.trim()?Number(t):NaN;return Number.isFinite(e)?e:void 0}function B(t){if("object"!=typeof t||null===t)return null;const e=t,n=H(e.ratio);if(void 0===n)return null;const i={ratio:n};"string"==typeof e.color&&""!==e.color&&(i.color=e.color);const o=W(e.enabled);return void 0!==o&&(i.enabled=o),"string"==typeof e.label&&""!==e.label&&(i.label=e.label),i}function R(t,e){if(null!=e)switch(t.kind){case"color":case"text":return"string"==typeof e?e:String(e);case"number":{const n=H(e);if(void 0===n)return;const i=t.min??-1/0,o=t.max??1/0;return n<i?i:n>o?o:n}case"opacity":{const t=H(e);return void 0===t?void 0:t<0?0:t>1?1:t}case"boolean":return W(e);case"lineStyle":return"string"==typeof e&&D.has(e)?e:void 0;case"select":if("string"!=typeof e)return;return void 0===t.options||t.options.some(t=>t.value===e)?e:void 0;case"levels":{if(!Array.isArray(e))return;const t=[];for(const n of e){const e=B(n);null!==e&&t.push(e)}return t}default:return}}function j(t,e,n){const i=void 0===n?null:new Map(n.fields.map(t=>[t.path,t])),o={},r={};for(const n of Object.keys(e)){const s=N(n);if(null===s)continue;let l=e[n];if(null!==i){const t=i.get(n);if(void 0===t)continue;l=R(t,l)}if(null===s.key){if(void 0===l)continue;"zIndex"===s.root?"number"==typeof l&&Number.isFinite(l)&&(o.zIndex=l):"boolean"==typeof l&&(o[s.root]=l);continue}const a=r[s.root]??{...t[s.root]};r[s.root]=a,void 0===l?delete a[s.key]:a[s.key]=l}if(void 0!==r.style&&(o.style=r.style),void 0!==r.props&&(o.props=r.props),void 0!==r.text){const t=r.text;"string"!=typeof t.value&&(t.value=""),o.text=t}return o}const E="#787b86",U=[[.236,"#f23645"],[.25,"#7e57c2"],[.382,"#ff9800"],[.5,"#4caf50"],[.618,"#089981"],[.75,"#26c6da"],[.786,"#00bcd4"],[1.272,"#2962ff"],[1.414,"#9c27b0"],[1.618,"#e91e63"],[2.618,"#ff5252"],[3.618,"#9575cd"],[4.236,"#64b5f6"]],V=1e-6;function q(t){if(!Number.isFinite(t))return E;for(const[e,n]of U)if(Math.abs(e-t)<=V)return n;return E}const J=["#2962ff","#f23645","#089981","#ff9800","#9c27b0","#00bcd4","#e91e63","#4caf50"];function K(t){const e=J.length,n=Number.isFinite(t)?Math.trunc(t):0;return J[(n%e+e)%e]}function X(t){return`${(100*t).toFixed(1)}%`}function Y(t){if(!Number.isFinite(t)||t<=0)return String(t);if(t>=1)return`${t}x1`;const e=1/t;return Math.abs(e-Math.round(e))<=V?`1x${Math.round(e)}`:`${t}x1`}function G(t){for(const e of t)Object.freeze(e);return Object.freeze(t)}function Z(t){return t.map(t=>({...t}))}const Q=G([0,.236,.382,.5,.618,.786,1].map(t=>({ratio:t,color:q(t)}))),tt=G([.236,.382,.5,.618,.786,1].map(t=>({ratio:t,color:q(t)}))),et=G([0,.25,.382,.5,.618,.75,1].map(t=>({ratio:t,color:q(t)}))),nt=G([.125,.25,.5,1,2,4,8].map((t,e)=>({ratio:t,color:K(e),label:Y(t)}))),it=G([0,1,2,3,5,8,13,21,34,55].map(t=>({ratio:t})));function ot(t,e,n){return t<e?e:t>n?n:t}function rt(e,n){const i=e.length;if(!(n>0)||i<3)return e.map(t=>({x:t.x,y:t.y}));const o=new Uint8Array(i);o[0]=1,o[i-1]=1;const r=[0,i-1];for(;r.length>0;){const i=r.pop(),s=r.pop(),l=e[s],a=e[i];let c=-1,h=n;for(let n=s+1;n<i;n++){const i=t(e[n].x,e[n].y,l,a);i>h&&(h=i,c=n)}c<0||(o[c]=1,r.push(s,c,c,i))}const s=[];for(let t=0;t<i;t++)1===o[t]&&s.push({x:e[t].x,y:e[t].y});return s}function st(t,e,n=.5){const i=e.length;if(i<2)return;if(t.moveTo(e[0].x,e[0].y),2===i)return void t.lineTo(e[1].x,e[1].y);const o=ot(n,0,1)/3;for(let n=0;n<i-1;n++){const r=e[0===n?0:n-1],s=e[n],l=e[n+1],a=e[n+2<i?n+2:i-1];t.bezierCurveTo(s.x+(l.x-r.x)*o,s.y+(l.y-r.y)*o,l.x-(a.x-s.x)*o,l.y-(a.y-s.y)*o,l.x,l.y)}}function lt(t,e,n=.6){return t*(1+2*((Number.isFinite(e)?ot(e,0,1):.5)-.5)*ot(n,0,1))}const at=new WeakMap;function ct(t,e){const n=at.get(t);if(!n)return t.measureText(e).width;const i=t.font+"\0"+e,o=n.get(i);if(void 0!==o)return o;const r=t.measureText(e).width;return n.size<1024&&n.set(i,r),r}const ht=(t,e)=>({x:(t.x+e.x)/2,y:(t.y+e.y)/2}),dt=(t,e,n)=>({x:t.x+(e.x-t.x)*n,y:t.y+(e.y-t.y)*n}),pt=t=>Number.isFinite(t.x)&&Number.isFinite(t.y);function ut(t,e){return{x:e.timeScale.indexToX(e.dataLayer.timeToIndexFloat(t.time)),y:e.priceScale.priceToY(t.price)}}function xt(t,e,n,i,o){const r=t.props?.[e];return"number"==typeof r&&Number.isFinite(r)?Math.max(i,Math.min(o,r)):n}function ft(t,e){return(t.style.levels??e).filter(t=>!1!==t.enabled&&Number.isFinite(t.ratio))}function yt(t,e,n,i=0,o=1){if(!pt(t)||!pt(e))return[];const r=e.x-t.x,s=e.y-t.y;if(0===r&&0===s)return t.x>=0&&t.x<=n.plotWidth&&t.y>=0&&t.y<=n.plotHeight?[t,t]:[];for(const[e,l,a]of[[t.x,r,n.plotWidth],[t.y,s,n.plotHeight]]){if(0===l){if(e<0||e>a)return[];continue}const t=-e/l,n=(a-e)/l;if((i=Math.max(i,Math.min(t,n)))>(o=Math.min(o,Math.max(t,n))))return[]}return[dt(t,e,i),dt(t,e,o)]}function gt(t,e,n){if(t.x===e.x)return yt(t,e,n.rc);const i=e.x>=t.x;return yt(t,e,n.rc,!0===(i?n.drawing.style.extendLeft:n.drawing.style.extendRight)?-1/0:0,!0===(i?n.drawing.style.extendRight:n.drawing.style.extendLeft)?1/0:1)}function mt(t,e){if(!t.every(pt))return[];let n=t.slice();for(const[t,i,o]of[["x",0,1],["x",e.plotWidth,-1],["y",0,1],["y",e.plotHeight,-1]]){const e=n;n=[];for(let r=0;r<e.length;r++){const s=e[r],l=e[(r+1)%e.length],a=(s[t]-i)*o>=0,c=(l[t]-i)*o>=0;a&&n.push(s),a!==c&&n.push(dt(s,l,(i-s[t])/(l[t]-s[t])))}}return n}function bt(t,e,n){let i=!1;for(let o=0,r=n.length-1;o<n.length;r=o++){const s=n[o],l=n[r];s.y>e!=l.y>e&&t<(l.x-s.x)*(e-s.y)/(l.y-s.y)+s.x&&(i=!i)}return i}function Mt(t,e,n,i,o){const r=Math.max(2,Math.min(96,Math.ceil(Math.abs(o)*Math.max(Math.abs(e),Math.abs(n))/4)));return Array.from({length:r+1},(s,l)=>{const a=i+o*l/r;return{x:t.x+e*Math.cos(a),y:t.y+n*Math.sin(a)}})}const vt=2*Math.PI;function wt(t,e){return pt(t)&&Number.isFinite(t.x*e)&&Number.isFinite(t.y*e)}function _t(t,e){const n=t.arc;return void 0===n?t.points.length>=2&&t.points.every(t=>wt(t,e)):function(t){return pt(t.center)&&[t.rx,t.ry,t.start,t.sweep,t.start+t.sweep].every(Number.isFinite)&&t.rx>0&&t.ry>0}(n)&&wt(n.center,e)&&Number.isFinite(n.rx*e)&&Number.isFinite(n.ry*e)}function kt(t,e){return{x:t.center.x+t.rx*Math.cos(e),y:t.center.y+t.ry*Math.sin(e)}}function St(t,e){if(0===e.sweep)return!1;const n=((t-e.start)*Math.sign(e.sweep)%vt+vt)%vt;return Math.abs(e.sweep)>=vt-1e-12||n<=Math.abs(e.sweep)+1e-12}function Tt(t,e,n){if(n.rx===n.ry&&Math.abs(n.sweep)>=vt-1e-12)return Math.abs(Math.hypot(t-n.center.x,e-n.center.y)-n.rx);const i=kt(n,n.start),o=kt(n,n.start+n.sweep),r=Math.hypot(t-i.x,e-i.y),s=Math.hypot(t-o.x,e-o.y),l=Math.min(r,s);if(n.rx===n.ry)return St(Math.atan2(e-n.center.y,t-n.center.x),n)?Math.abs(Math.hypot(t-n.center.x,e-n.center.y)-n.rx):l;const a=i=>{const o=kt(n,n.start+n.sweep*i);return Math.hypot(t-o.x,e-o.y)};let c=l,h=s<r?32:0;for(let t=0;t<=32;t++){const e=a(t/32);e<c&&(c=e,h=t)}let d=Math.max(0,(h-1)/32),p=Math.min(1,(h+1)/32);const u=Math.min(80,Math.max(32,2*Math.ceil(Math.log2(Math.max(n.rx,n.ry)))+8));for(let t=0;t<u;t++){const t=d+(p-d)/3,e=p-(p-d)/3;a(t)<a(e)?p=e:d=t}return Math.min(c,a((d+p)/2))}function Pt(t,e,n){const i=(t-n.center.x)/n.rx,o=(e-n.center.y)/n.ry;if(i*i+o*o>1)return!1;if(Math.abs(n.sweep)>=vt-1e-12)return!0;if(!0===n.sector)return St(Math.atan2(o,i),n);const r=n.start+n.sweep/2;return i*Math.cos(r)+o*Math.sin(r)>=Math.cos(Math.abs(n.sweep)/2)}function Lt(t,e){const{ctx:n,rc:i,style:o}=t,r=i.dpr;n.save(),n.strokeStyle=o.color,n.lineWidth=Math.max(1,o.lineWidth*r),n.lineJoin="round",n.lineCap="round",n.setLineDash("dashed"===o.lineStyle?[6*r,4*r]:"dotted"===o.lineStyle?[r,3*r]:[]);for(const t of e.paths)if(_t(t,r)){if(n.beginPath(),void 0!==t.arc){const e=t.arc;!0===e.sector&&n.moveTo(e.center.x*r,e.center.y*r),e.rx===e.ry?n.arc(e.center.x*r,e.center.y*r,e.rx*r,e.start,e.start+e.sweep,e.sweep<0):n.ellipse(e.center.x*r,e.center.y*r,e.rx*r,e.ry*r,0,e.start,e.start+e.sweep,e.sweep<0)}else{n.moveTo(t.points[0].x*r,t.points[0].y*r);for(let e=1;e<t.points.length;e++)n.lineTo(t.points[e].x*r,t.points[e].y*r)}!0===t.closed&&n.closePath(),!0===t.fill&&!0===o.fill&&(n.save(),n.globalAlpha*=Math.max(0,Math.min(1,o.fillOpacity??.12)),n.fillStyle=o.fillColor??o.color,n.fill("evenodd"),n.restore()),!1!==t.stroke&&(n.strokeStyle=t.color??o.color,n.stroke())}const s=t.drawing.text,l=(s?.fontSize??11)*r;n.font=`${!0===s?.italic?"italic ":""}${!0===s?.bold?"700 ":""}${l}px ${s?.fontFamily||"ui-sans-serif, system-ui, sans-serif"}`,n.textAlign="left",n.textBaseline="bottom";const a=[],c=l/r*1.3;for(const t of e.labels??[]){if(!wt(t.at,r))continue;let{x:e,y:h}=t.at;if(e>=0&&e<=i.plotWidth&&h>=0&&h<=i.plotHeight){const o=ct(n,t.text)/r;e=Math.max(0,Math.min(e,i.plotWidth-o));let s=!1;for(const t of[0,-1,1,-2,2,-3,3,-4]){const n=h+t*c;if(!(n<l/r||n>i.plotHeight)&&!a.some(t=>e<t.x+t.width+3&&e+o+3>t.x&&Math.abs(n-t.y)<c-.01)){h=n,a.push({x:e,y:h,width:o}),s=!0;break}}if(!s)continue}n.fillStyle=s?.color??t.color??o.color,n.fillText(t.text,e*r,h*r)}n.restore()}function Ft(e,n){return{...e,draw(t){const e=t.pts.map(e=>({x:e.x/t.rc.dpr,y:e.y/t.rc.dpr}));Lt(t,n({pts:e,drawing:{...t.drawing,style:t.style},rc:t.rc}))},distance:(e,i,o)=>e<0||i<0||e>o.rc.plotWidth||i>o.rc.plotHeight?null:function(e,n,i,o,r=1){let s=1/0;for(const l of i.paths){if(!_t(l,r))continue;if(void 0!==l.arc){const i=l.arc;if(!0===l.fill&&!0===o.style.fill&&(o.style.fillOpacity??.12)>0&&Pt(e,n,i))return 0;if(!1!==l.stroke&&(s=Math.min(s,Tt(e,n,i)),!0===i.sector||!0===l.closed)){const o=kt(i,i.start),r=kt(i,i.start+i.sweep);!0===i.sector?(s=Math.min(s,t(e,n,i.center,o)),!0===l.closed&&(s=Math.min(s,t(e,n,r,i.center)))):s=Math.min(s,t(e,n,r,o))}continue}const i=l.points;if(!0===l.fill&&!0===o.style.fill&&(o.style.fillOpacity??.12)>0&&bt(e,n,i))return 0;if(!1!==l.stroke){for(let o=1;o<i.length;o++)s=Math.min(s,t(e,n,i[o-1],i[o]));!0===l.closed&&(s=Math.min(s,t(e,n,i[i.length-1],i[0])))}}return Number.isFinite(s)?s:null}(e,i,n(o),o.drawing,o.rc.dpr)}}const It=z([v,w,_]),zt=[{ratio:0},{ratio:.5},{ratio:1}],Ct=[0,.382,.618,1,1.272,1.618,2,2.618,3.618,4.236].map(t=>({ratio:t})),At=[0,.382,.5,.618,1].map(t=>({ratio:t,color:q(t)})),Nt=(t,e)=>({points:[t,e]});function $t(t,e,n){const i=e.x-t.x;if(0===i)return[t,e];const o=!0===n.drawing.style.extendLeft?0:Math.min(t.x,e.x),r=!0===n.drawing.style.extendRight?n.rc.plotWidth:Math.max(t.x,e.x),s=n=>({x:n,y:t.y+(e.y-t.y)*(n-t.x)/i});return i>0?[s(o),s(r)]:[s(r),s(o)]}function Ot(t,e){const[n,i]=t.pts,o=gt(n,i,t),r=gt(e[0],e[1],t),s=[];if(!0===t.drawing.style.fill){const o=$t(n,i,t),r=$t(e[0],e[1],t);s.push({points:mt([o[0],o[1],r[1],r[0]],t.rc),closed:!0,fill:!0,stroke:!1})}return s.push({points:o},{points:r}),{paths:s}}const Dt=Ft({id:"disjoint-channel",name:"Disjoint Channel",points:4,defaultStyle:{fill:!0},settings:It},t=>t.pts.length<4?{paths:[]}:Ot(t,t.pts.slice(2,4))),Wt=Ft({id:"flat-top-bottom",name:"Flat Top/Bottom",points:3,defaultStyle:{fill:!0},settings:It,constrain(t){const e=t.map(t=>({...t}));return e.length>=3&&(e[2].time=(e[0].time+e[1].time)/2),e}},t=>t.pts.length<3?{paths:[]}:Ot(t,[{x:t.pts[0].x,y:t.pts[2].y},{x:t.pts[1].x,y:t.pts[2].y}]));function Ht(t,e,n){let i=0,o=t.length;for(;i<o;){const r=i+o>>>1;t[r].time<e||n&&t[r].time===e?i=r+1:o=r}return i}const Bt=Ft({id:"regression-channel",name:"Regression Channel",points:2,defaultStyle:{fill:!0,showLabels:!0},settings:z([v,w,_,k,T,{path:"props.deviation",label:"Deviation multiplier",kind:"number",min:.01,max:20,step:.1,group:"behavior"}])},t=>{if(t.pts.length<2)return{paths:[]};const e=t.rc.bars?.()??[],[n,i]=t.drawing.points,o=Ht(e,Math.min(n.time,i.time),!1),r=Ht(e,Math.max(n.time,i.time),!0);let s=0,l=0,a=0,c=0,h=0,d=0,p=0,u=0,x=0,f=0;for(let n=o;n<r;n++){const i=e[n],o=i.close,r=t.rc.dataLayer.timeToIndex?.(i.time)??t.rc.dataLayer.timeToIndexFloat(i.time);if(!Number.isFinite(r)||!Number.isFinite(o))continue;0===s&&(p=i.time,x=r),u=i.time,f=r,s++;const y=r-l,g=o-a;l+=y/s,a+=g/s,c+=y*(r-l),h+=y*(o-a),d+=g*(o-a)}if(0===s)return{paths:[],labels:!1===t.drawing.style.showLabels?[]:[{at:t.pts[0],text:"No bars in range"}]};const y=c>0?h/c:0,g=Math.sqrt(Math.max(0,d-y*h)/s)*xt(t.drawing,"deviation",2,.01,20),m=d>0?Math.max(0,Math.min(1,y*h/d)):1,b=(e,n,i)=>{return ut({time:e,price:(o=n,a+y*(o-l)+i)},t.rc);var o},M=[0,g,-g].map(e=>({points:gt(b(p,x,e),b(u,f,e),t)}));if(!0===t.drawing.style.fill){const e=$t(b(p,x,g),b(u,f,g),t),n=$t(b(p,x,-g),b(u,f,-g),t);M.push({points:mt([e[0],e[1],n[1],n[0]],t.rc),closed:!0,fill:!0,stroke:!1})}return{paths:M,labels:!1===t.drawing.style.showLabels?[]:[{at:b(p,x,g),text:`R^2 ${m.toFixed(3)}  ${s} bars`}]}});function Rt(t,e,n){return Ft({id:t,name:e,points:3,defaultStyle:{fill:!0,showLabels:!0,levels:Z(zt)},settings:z([v,w,S,T])},t=>{if(t.pts.length<3)return{paths:[]};const[e,i,o]=t.pts,r=ht(i,o),[s,l]=t.drawing.points,a={time:"schiff"===n?s.time:(s.time+l.time)/2,price:(s.price+l.price)/2},c="standard"===n?e:ut(a,t.rc),h="inside"===n?r:c,d="inside"===n?{x:o.x-c.x,y:o.y-c.y}:{x:r.x-c.x,y:r.y-c.y},p=e=>yt(e,{x:e.x+d.x,y:e.y+d.y},t.rc,0,1/0),u=[],x=[],f=ft(t.drawing,zt);for(const e of f){const n=e.color,s=0===e.ratio?[h]:[dt(r,i,e.ratio),dt(r,o,e.ratio)];for(const i of s){const o=p(i);u.push({points:o,color:n}),!1!==t.drawing.style.showLabels&&2===o.length&&x.push({at:{x:o[0].x+4,y:o[0].y-4},text:e.label??X(e.ratio),color:n})}}const y=f.reduce((t,e)=>Math.max(t,Math.abs(e.ratio)),0);if(!0===t.drawing.style.fill&&y>0){const e=dt(r,i,y),n=dt(r,o,y),s=Math.hypot(d.x,d.y);if(s>0){const i=(Math.max(Math.hypot(e.x,e.y),Math.hypot(n.x,n.y))+Math.hypot(t.rc.plotWidth,t.rc.plotHeight))/s+1,o=t=>({x:t.x+d.x*i,y:t.y+d.y*i});u.unshift({points:mt([e,o(e),o(n),n],t.rc),closed:!0,fill:!0,stroke:!1})}}return u.push(Nt(i,o)),"inside"===n&&u.push(Nt(c,o),Nt(e,i)),{paths:u,labels:x}})}const jt=Ft({id:"info-line",name:"Info Line",points:2,angleLock:!0,defaultStyle:{showLabels:!0},settings:z([v,_,k,T])},t=>{if(t.pts.length<2)return{paths:[]};const[e,n]=t.pts,[i,o]=t.drawing.points,r=o.price-i.price,s=r>=0?"+":"",l=0===i.price?"n/a":`${s}${(r/Math.abs(i.price)*100).toFixed(2)}%`,a=Math.abs(Math.round(t.rc.dataLayer.timeToIndexFloat(o.time)-t.rc.dataLayer.timeToIndexFloat(i.time)));return{paths:[{points:gt(e,n,t)}],labels:!1===t.drawing.style.showLabels?[]:[{at:{x:(e.x+n.x)/2,y:(e.y+n.y)/2-8},text:`${s}${t.rc.priceScale.format(r)} (${l})  ${a} bars`}]}}),Et=Ft({id:"trend-angle",name:"Trend Angle",points:2,angleLock:!0,defaultStyle:{showLabels:!0},settings:z([v,k,T])},t=>{if(t.pts.length<2)return{paths:[]};const[e,n]=t.pts,i=Math.atan2(n.y-e.y,n.x-e.x),o=Math.max(12,Math.min(40,Math.hypot(n.x-e.x,n.y-e.y)/3));return{paths:[Nt(e,n),Nt(e,{x:e.x+1.5*o,y:e.y}),{points:Mt(e,o,o,0,i)}],labels:!1===t.drawing.style.showLabels?[]:[{at:{x:e.x+o+6,y:e.y-6},text:`${(180*-i/Math.PI).toFixed(1)} deg`}]}}),Ut=Ft({id:"fib-extension-two-point",name:"Fib Extension (Two Point)",points:2,defaultStyle:{levels:Z(Ct),showLabels:!0,fill:!0,fillOpacity:.06},settings:z([v,S,w,_,T])},t=>{if(t.pts.length<2)return{paths:[]};const[e,n]=t.pts,[i,o]=t.drawing.points,r=[],s=[],l=ft(t.drawing,Ct);if(!0===t.drawing.style.fill&&e.x!==n.x){const s=l.map(e=>t.rc.priceScale.priceToY(i.price+(o.price-i.price)*e.ratio)).filter(Number.isFinite).sort((t,e)=>t-e),a=!0===t.drawing.style.extendLeft?0:Math.min(e.x,n.x),c=!0===t.drawing.style.extendRight?t.rc.plotWidth:Math.max(e.x,n.x);for(let e=1;e<s.length;e++)s[e]!==s[e-1]&&r.push({points:mt([{x:a,y:s[e-1]},{x:c,y:s[e-1]},{x:c,y:s[e]},{x:a,y:s[e]}],t.rc),closed:!0,fill:!0,stroke:!1})}r.push(Nt(e,n));for(const a of l){const l=i.price+(o.price-i.price)*a.ratio,c=t.rc.priceScale.priceToY(l),h=a.color??q(a.ratio);r.push({points:gt({x:e.x,y:c},{x:n.x,y:c},t),color:h}),!1!==t.drawing.style.showLabels&&s.push({at:{x:Math.min(e.x,n.x)+4,y:c-3},text:`${a.label??X(a.ratio)}  ${t.rc.priceScale.format(l)}`,color:h})}return{paths:r,labels:s}});function Vt(t,e){if(t.pts.length<2)return{paths:[]};const[n,i]=t.pts,o=[],r=[];o.push({points:[{x:n.x,y:i.y},i,{x:i.x,y:n.y}]});const s=1.2*(t.drawing.text?.fontSize??11),l=[],a=(i,o,a,c,h)=>{const d=Math.min(n.x,o.x),p=Math.max(n.x,o.x),u=Math.min(n.y,o.y),x=Math.max(n.y,o.y);if(!e||!1===t.drawing.style.showLabels||l.length>=32)return;const f=e(c);if(!(!Number.isFinite(f)||f<=0||!Number.isFinite(s)||s<=0||(a?x-u<s:p-d<f)))for(const e of[0,-s,s]){let y,g;a?(y=o.x>=n.x?o.x+4:o.x-f-4,(y<0||y+f>t.rc.plotWidth)&&(y=o.x>=n.x?o.x-f-4:o.x+4),g=Math.max(u+s,Math.min(x,i.y+s/2+e))):(y=Math.max(d,Math.min(p-f,i.x-f/2+e)),g=o.y<n.y?o.y-4:o.y+s+4,(g-s<0||g>t.rc.plotHeight)&&(g=o.y<n.y?o.y+s+4:o.y-4));const m={x:y,y:g-s,width:f,height:s};if(!(y<0||y+f>t.rc.plotWidth||m.y<0||g>t.rc.plotHeight)&&!l.some(t=>y<t.x+t.width+3&&y+f+3>t.x&&m.y<t.y+t.height+3&&m.y+s+3>t.y))return l.push(m),void r.push({at:{x:y,y:g},text:c,color:h})}};for(const e of ft(t.drawing,At)){const r=[{x:i.x,y:n.y+(i.y-n.y)*e.ratio}];1!==e.ratio&&r.push({x:n.x+(i.x-n.x)*e.ratio,y:i.y}),r.forEach((r,s)=>{const l=yt(n,r,t.rc,0,1/0),c=e.color??q(e.ratio);if(o.push({points:l,color:c}),2===l.length){const t={x:n.x+(Math.sign(r.x-n.x)||Math.sign(i.x-n.x))*Math.abs(i.x-n.x),y:n.y+(Math.sign(r.y-n.y)||Math.sign(i.y-n.y))*Math.abs(i.y-n.y)},o=Math.abs(e.ratio),l=dt(n,r,1/Math.max(1,o));a(l,t,o<=1?0===s:0!==s,e.label??String(e.ratio),c)}})}return{paths:o,labels:r}}const qt={...Ft({id:"fib-speed-resistance-fan",name:"Fib Speed Resistance Fan",points:2,defaultStyle:{levels:Z(At),showLabels:!0},settings:z([v,S,T])},t=>Vt(t)),draw(t){const{ctx:e,rc:n}=t,i=t.drawing.text;let o;e.save();try{e.font=`${!0===i?.italic?"italic ":""}${!0===i?.bold?"700 ":""}${(i?.fontSize??11)*n.dpr}px ${i?.fontFamily||"ui-sans-serif, system-ui, sans-serif"}`,o=Vt({rc:n,drawing:{...t.drawing,style:t.style},pts:t.pts.map(t=>({x:t.x/n.dpr,y:t.y/n.dpr}))},t=>e.measureText(t).width/n.dpr)}finally{e.restore()}Lt(t,o)}},Jt=Ft({id:"icon-stamp",name:"Icon Stamp",points:1,defaultStyle:{fill:!0,fillOpacity:.8},settings:z([v,w,{path:"props.shape",label:"Shape",kind:"select",group:"behavior",options:["star","diamond","circle","arrow up","arrow down","check","cross"].map(t=>({value:t.replace(" ","-"),label:t[0].toUpperCase()+t.slice(1)}))},{path:"props.size",label:"Size",kind:"number",min:8,max:160,step:2,group:"behavior"}])},t=>{if(t.pts.length<1)return{paths:[]};const e=t.pts[0],n=xt(t.drawing,"size",28,8,160)/2,i=(t,i)=>({x:e.x+t*n,y:e.y+i*n}),o=t.drawing.props?.shape;let r;if("diamond"===o)r=[i(0,-1),i(1,0),i(0,1),i(-1,0)];else if("circle"===o)r=Mt(e,n,n,0,2*Math.PI);else if("arrow-up"===o||"arrow-down"===o){const t="arrow-up"===o?1:-1;r=[[0,-1],[1,0],[.35,0],[.35,1],[-.35,1],[-.35,0],[-1,0]].map(([e,n])=>i(e,n*t))}else r="check"===o?[[-1,0],[-.65,-.3],[-.2,.2],[.75,-.85],[1,-.55],[-.2,.85]].map(([t,e])=>i(t,e)):"cross"===o?[[-1,-.65],[-.65,-1],[0,-.35],[.65,-1],[1,-.65],[.35,0],[1,.65],[.65,1],[0,.35],[-.65,1],[-1,.65],[-.35,0]].map(([t,e])=>i(t,e)):Array.from({length:10},(t,e)=>{const n=-Math.PI/2+e*Math.PI/5,o=e%2==0?1:.45;return i(Math.cos(n)*o,Math.sin(n)*o)});return{paths:[{points:r,closed:!0,fill:!0}]}}),Kt=[Dt,Wt,Bt,Rt("pitchfork","Pitchfork","standard"),Rt("schiff-pitchfork","Schiff Pitchfork","schiff"),Rt("modified-schiff-pitchfork","Modified Schiff Pitchfork","modified"),Rt("inside-pitchfork","Inside Pitchfork","inside"),jt,Et,Ut,qt,Jt],Xt=2*Math.PI,Yt=(1+Math.sqrt(5))/2,Gt=t=>t.map(t=>({ratio:t})),Zt=Gt([.236,.382,.5,.618,.786,1,1.618,2.618]),Qt=Zt.slice(0,6),te=Gt([0,.382,.618,1,1.382,1.618,2,2.382,2.618,3]),ee=Gt([0,.25,.382,.5,.618,.75,1]),ne=Gt(Array.from({length:12},(t,e)=>e+1)),ie=Gt([.236,.382,.5,.618,.786,1,1.618,2.618,4.236,6.854,11.09]),oe=z([v,S,T]),re=z([v,w,S,T]),se=(t,e,n)=>({points:[t,e],color:n}),le=(t,e,n=e,i=0,o=Xt,r)=>({points:[],arc:{center:t,rx:e,ry:n,start:i,sweep:o},color:r}),ae=(t,e=2)=>t.pts.length>=e&&t.pts.every(pt),ce=(t,e,n)=>!1===t.drawing.style.showLabels?[]:[{at:n,text:e.label??String(e.ratio),color:e.color}];function he(t){return ae(t)?{paths:[se(t.pts[0],t.pts[1])]}:{paths:[]}}const de=Ft({id:"trend-fib-time",name:"Trend Fib Time",points:3,defaultStyle:{levels:Z(te)},settings:oe},t=>{if(!ae(t,3))return he(t);const[e,n,i]=t.drawing.points,o=e=>t.rc.dataLayer.timeToIndexFloat(e),r=o(n.time)-o(e.time),s=o(i.time),l=[],a=[];for(const e of ft(t.drawing,te)){const n=t.rc.timeScale.indexToX(s+r*e.ratio);!Number.isFinite(n)||n<0||n>t.rc.plotWidth||(l.push(se({x:n,y:0},{x:n,y:t.rc.plotHeight},e.color)),a.push(...ce(t,e,{x:n+3,y:15})))}return{paths:l,labels:a}});function pe(t,e,n){const i="wedge"===n?Qt:Zt;return Ft({id:t,name:e,points:"wedge"===n?3:2,defaultStyle:{fill:!1,levels:Z(i)},settings:re},t=>{if(!ae(t,"wedge"===n?3:2))return he(t);const[e,o,r]=t.pts,s=Math.hypot(o.x-e.x,o.y-e.y);if(!Number.isFinite(s)||0===s)return he(t);let l=0,a=Xt;const c=[],h=[];if("circle"!==n)if(l=Math.atan2(o.y-e.y,o.x-e.x),c.push(se(e,o)),"arc"===n)l-=Math.PI/2,a=Math.PI;else{const t=Math.atan2(r.y-e.y,r.x-e.x);a=(t-l+3*Math.PI)%Xt-Math.PI,c.push(se(e,{x:e.x+s*Math.cos(t),y:e.y+s*Math.sin(t)}))}let d=0;for(const o of ft(t.drawing,i)){const i=s*o.ratio;if(!(i>0&&Number.isFinite(i)))continue;d=Math.max(d,i),c.push(le(e,i,i,l,a,o.color));const r="circle"===n?0:l+a/2;h.push(...ce(t,o,{x:e.x+i*Math.cos(r)+3,y:e.y+i*Math.sin(r)}))}if(d>0&&!0===t.drawing.style.fill){const t=le(e,d,d,l,a);t.arc.sector="circle"!==n,c.unshift({...t,fill:!0,stroke:!1,closed:!0})}return{paths:c,labels:h}})}const ue=Ft({id:"fib-spiral",name:"Fib Spiral",points:2,settings:z([v])},t=>{if(!ae(t))return{paths:[]};const[e,n]=t.pts,i=Math.hypot(n.x-e.x,n.y-e.y);if(!Number.isFinite(i)||i<.5)return he(t);const o=Math.log(Yt)/(Math.PI/2),r=Math.atan2(n.y-e.y,n.x-e.x),s=Math.max(-16*Math.PI,Math.log(.5/i)/o),l=[],a=[s];for(let t=Math.ceil(s/(Math.PI/2));t<2;t++)a.push(t*Math.PI/2);a.push(Math.PI);for(let t=1;t<a.length;t++){const n=a[t-1],s=a[t],c=Math.max(2,Math.min(32,Math.ceil((s-n)*Math.sqrt(i*Math.exp(o*s)/.8))));for(let a=1===t?0:1;a<=c;a++){const t=n+(s-n)*a/c,h=i*Math.exp(o*t);l.push({x:e.x+h*Math.cos(r+t),y:e.y+h*Math.sin(r+t)})}}return{paths:[{points:l}]}}),xe=Ft({id:"gann-square",name:"Gann Square",points:2,defaultStyle:{fill:!1,levels:Z(ee)},settings:re},t=>{if(!ae(t))return{paths:[]};const[e,n]=t.pts,[i,o]=t.drawing.points,r=n.x-e.x,s=n.y-e.y,l=Math.min(e.x,n.x),a=Math.max(e.x,n.x),c=Math.min(e.y,n.y),h=Math.max(e.y,n.y),d=[],p=[];!0===t.drawing.style.fill&&d.push({points:[{x:l,y:c},{x:a,y:c},{x:a,y:h},{x:l,y:h}],closed:!0,fill:!0,stroke:!1});const u=t.rc.dataLayer.timeToIndexFloat(i.time),x=t.rc.dataLayer.timeToIndexFloat(o.time);for(const e of ft(t.drawing,ee)){const n=t.rc.timeScale.indexToX(u+(x-u)*e.ratio),r=t.rc.priceScale.priceToY(i.price+(o.price-i.price)*e.ratio);d.push(se({x:l,y:r},{x:a,y:r},e.color),se({x:n,y:c},{x:n,y:h},e.color)),p.push(...ce(t,e,{x:l+3,y:r-3}))}for(const[n,i,o]of[[1,1/3,"3x1"],[1,.5,"2x1"],[1,1,"1x1"],[.5,1,"1x2"],[1/3,1,"1x3"]]){const l={x:e.x+r*n,y:e.y+s*i};d.push(se(e,l)),!1!==t.drawing.style.showLabels&&p.push({at:{x:l.x+3,y:l.y},text:o})}if(0!==r&&0!==s)for(const t of[.25,.5,.75,1])d.push(le(e,Math.abs(r)*t,Math.abs(s)*t,r>0?0:Math.PI,Math.sign(r*s)*Math.PI/2));return{paths:d,labels:p}});function fe(t,e,n,i,o,r){const s=[-Math.PI,0];for(const o of[n,i]){const n=(o-t.x)/e;Math.abs(n)<1&&s.push(-Math.acos(n))}for(const n of[o,r]){const i=(n-t.y)/e;if(i>-1&&i<0){const t=Math.asin(i);s.push(t,-Math.PI-t)}}s.sort((t,e)=>t-e);const l=[];for(let a=1;a<s.length;a++){const c=s[a-1],h=s[a]-c;if(h<1e-12)continue;const d=c+h/2,p=t.x+e*Math.cos(d),u=t.y+e*Math.sin(d);p<n||p>i||u<o||u>r||l.push(le(t,e,e,c,h))}return l}const ye=Ft({id:"dedekind-tessellation",name:"Dedekind Tessellation",points:2,settings:z([v,{path:"props.maxCurvature",label:"Max curvature",kind:"number",min:1,max:64,step:1,group:"behavior"}])},t=>{if(!ae(t))return{paths:[]};const[e,n]=t.pts,i=Math.min(e.x,n.x),o=Math.max(e.x,n.x),r=Math.min(e.y,n.y),s=Math.max(e.y,n.y),l=s-r;if(l<.75||o-i<.75||!Number.isFinite(l))return he(t);const a=Math.max(0,i),c=Math.min(t.rc.plotWidth,o),h=Math.max(0,r),d=Math.min(t.rc.plotHeight,s);if(a>c||h>d)return{paths:[]};const p=[{x:i,y:r},{x:o,y:r},{x:o,y:s},{x:i,y:s}],u=p.map((e,n)=>({points:yt(e,p[(n+1)%4],t.rc)})),x=(a-i)/l,f=(c-i)/l,y=Math.ceil(x-.5)+.5,g=Math.min(2048,Math.floor(f-y)+1);for(let t=0;t<g;t++){const e=i+(y+t)*l;u.push(se({x:e,y:d},{x:e,y:h}))}const m=Math.min(Math.round(xt(t.drawing,"maxCurvature",24,1,64)),Math.floor(l/.75));for(let t=1;t<=m&&u.length<4096;t++){if(t%2==0&&t%8!=0)continue;const e=l/t;if(!(s-e>d))for(let n=0;n<t&&u.length<4096;n++){const o=n*n-1;if(o%t!==0||t%8==0&&Math.abs(o/t)%2!=1)continue;const r=Math.ceil(x-1/t-n/t),p=Math.floor(f+1/t-n/t),y=Math.min(4096-u.length,p-r+1);for(let o=0;o<y;o++){const p={x:i+(r+o+n/t)*l,y:s};u.push(...fe(p,e,a,c,h,d))}}}return{paths:u}});function ge(t,e,n,i){const o=n?ie:ne;return Ft({id:t,name:e,points:2,defaultStyle:{levels:Z(o)},settings:z([v,S,T,...n?[]:[{path:"props.waveCount",label:"Wave limit",kind:"number",min:1,max:12,step:1,group:"behavior"}],...i?[{path:"props.mach",label:"Mach number",kind:"number",min:1.01,max:20,step:.5,group:"behavior"}]:[]])},t=>{if(!ae(t))return{paths:[]};const[e,r]=t.pts,s=ht(e,r),l=Math.hypot(r.x-e.x,r.y-e.y)/2;if(!Number.isFinite(l)||l<.5)return he(t);const a=(r.x-e.x)/(2*l),c=(r.y-e.y)/(2*l),h=i?xt(t.drawing,"mach",2,1.01,20):1,d={x:s.x-h*l*a,y:s.y-h*l*c},p=ft(t.drawing,o).filter(t=>t.ratio>0).slice().sort((t,e)=>t.ratio-e.ratio);n||p.splice(Math.round(xt(t.drawing,"waveCount",6,1,12)));const u=[],x=[];let f=0;for(const e of p){const n=l*e.ratio,i={x:d.x+h*n*a,y:d.y+h*n*c};Number.isFinite(n)&&pt(i)&&(f=Math.max(f,n),u.push(le(i,n,n,0,Xt,e.color)),x.push(...ce(t,e,{x:i.x+a*n+4,y:i.y+c*n})))}if(0===f)return{paths:[]};const y=h*f+2*l;if(i){const t=1/h,e=Math.sqrt(1-t*t);for(const n of[-1,1])u.push(se(d,{x:d.x+y*(a*e-n*c*t),y:d.y+y*(c*e+n*a*t)}))}else u.push(se({x:d.x-c*y,y:d.y+a*y},{x:d.x+c*y,y:d.y-a*y}));return{paths:u,labels:x}})}const me=[de,pe("fib-circles","Fib Circles","circle"),pe("fib-speed-resistance-arcs","Fib Speed Resistance Arcs","arc"),pe("fib-wedge","Fib Wedge","wedge"),ue,xe,ye,ge("sonic","Sonic",!1,!1),ge("supersonic","Supersonic",!1,!0),ge("golden-sonic","Golden Sonic",!0,!1),ge("golden-supersonic","Golden Supersonic",!0,!0)],be="#16a34a",Me="#dc2626",ve={min:1.272,max:1.414},we={min:.74,max:.83},_e={gartley:{ab:{min:.55,max:.68},bc:{min:.382,max:.886},cd:{min:1.13,max:1.618},ad:{min:.74,max:.83}},bat:{ab:{min:.382,max:.5},bc:{min:.382,max:.886},cd:{min:1.618,max:2.618},ad:{min:.84,max:.92}},butterfly:{ab:{min:.74,max:.83},bc:{min:.382,max:.886},cd:{min:1.618,max:2.618},ad:{min:1.272,max:1.618}},crab:{ab:{min:.382,max:.618},bc:{min:.382,max:.886},cd:{min:2.618,max:3.618},ad:{min:1.55,max:1.69}},shark:{ab:{min:.382,max:.618},bc:{min:1.13,max:1.618},cd:{min:1.618,max:2.24},ad:{min:.886,max:1.13}}},ke=z([v,k,T]),Se=z([v,w,k,T]),Te=(t,e,n)=>({points:yt(t,e,n.rc)});function Pe(t,e){return Number.isFinite(t)&&Number.isFinite(e)&&Math.abs(e)>=1e-9?Math.abs(t)/Math.abs(e):null}function Le(t,e,n){const i=t[e]?.price,o=t[n]?.price;return Number.isFinite(i)&&Number.isFinite(o)?Math.abs(o-i):NaN}function Fe(t){return null!==t.value&&void 0!==t.band&&t.value>=t.band.min&&t.value<=t.band.max}function Ie(t,e,n,i){const o=e.x-t.x,r=e.y-t.y,s=i.x-n.x,l=i.y-n.y,a=s*r-l*o;if(!Number.isFinite(a)||Math.abs(a)<1e-9)return null;const c=((n.y-t.y)*o-(n.x-t.x)*r)/a;return!Number.isFinite(c)||c<0||c>1?null:{x:n.x+c*s,y:n.y+c*l}}function ze(t,e){const n=e.pts.slice(0,t.labels.length),i=e.drawing.points.slice(0,t.labels.length);if(n.length<2)return{paths:[]};const o=[];if(!0===t.fill)for(const t of[[0,1,2],[2,3,4]]){const i=t.map(t=>n[t]);i.every(t=>void 0!==t&&pt(t))&&o.push({points:mt(i,e.rc),closed:!0,fill:!0,stroke:!1})}for(let t=1;t<n.length;t++)o.push(Te(n[t-1],n[t],e));if(void 0!==t.neckline&&n.length>=t.labels.length){const[i,r]=t.neckline,s=n[i],l=n[r],a=Ie(s,l,n[0],n[1])??s,c=n.length-1,h=Ie(s,l,n[c-1],n[c])??l;o.push(...function(t,e,n){const i=yt(t,e,n.rc);if(i.length<2)return[];const[o,r]=i,s=r.x-o.x,l=r.y-o.y,a=Math.hypot(s,l);if(!(a>0&&Number.isFinite(a)))return[{points:i}];const c=[];for(let t=0;t<a;t+=12){const e=t/a,n=Math.min(t+7,a)/a;c.push({points:[{x:o.x+s*e,y:o.y+l*e},{x:o.x+s*n,y:o.y+l*n}]})}return c}(a,h,e))}if(!1===e.drawing.style.showLabels)return{paths:o};const r=function(t,e){return e.flatMap((n,i)=>{const o=t.labels[i];if(!o||!pt(n))return[];const r=e[i-1]?.y??n.y,s=e[i+1]?.y??n.y,l=n.y<=(r+s)/2;return[{at:{x:n.x+4,y:n.y+(l?-6:14)},text:o}]})}(t,n),s=void 0!==t.harmonic?function(t,e){const n=Le(t,0,1),i=Le(t,1,2);if("cypher"===e){const e=Le(t,0,3);return[{name:"AB/XA",value:Pe(i,n),band:{min:.382,max:.618},anchor:2},{name:"XC/XA",value:Pe(e,n),band:ve,anchor:3},{name:"CD/XC",value:Pe(Le(t,3,4),e),band:we,anchor:4}]}const o=Le(t,2,3);return[{name:"AB/XA",value:Pe(i,n),band:e.ab,anchor:2},{name:"BC/AB",value:Pe(o,i),band:e.bc,anchor:3},{name:"CD/BC",value:Pe(Le(t,3,4),o),band:e.cd,anchor:4},{name:"AD/XA",value:Pe(Le(t,1,4),n),band:e.ad,anchor:4}]}(i,t.harmonic):void 0!==t.ratios?function(t,e){if("xabcd"===e){const e=Le(t,0,1),n=Le(t,1,2),i=Le(t,2,3);return[{name:"AB/XA",value:Pe(n,e),anchor:2},{name:"BC/AB",value:Pe(i,n),anchor:3},{name:"CD/BC",value:Pe(Le(t,3,4),i),anchor:4}]}const n=Le(t,0,1),i=Le(t,1,2);return[{name:"BC/AB",value:Pe(i,n),anchor:2},{name:"CD/BC",value:Pe(Le(t,2,3),i),anchor:3}]}(i,t.ratios):[];if(r.push(...function(t,e){const n=[];for(let i=0;i<t.length;i++){const o=t[i];if(null===o.value||!Number.isFinite(o.value))continue;const r=e[o.anchor],s=e[Math.max(0,o.anchor-1)];if(!(r&&s&&pt(r)&&pt(s)))continue;const l=void 0!==o.band,a=l&&Fe(o),c=l?a?" OK":" OUT":"",h=ht(s,r);n.push({at:{x:h.x+6,y:h.y+(i===t.length-1&&o.anchor===t[i-1]?.anchor?14:-3)},text:`${o.name} ${o.value.toFixed(3)}${c}`,color:l?a?be:Me:void 0})}return n}(s,n)),void 0!==t.harmonic&&n.length>=t.labels.length){const e=function(t,e){if(t.length<e)return!1;let n=t[1].price-t[0].price;if(!Number.isFinite(n)||0===n)return!1;for(let i=2;i<e;i++){const e=t[i].price-t[i-1].price;if(!Number.isFinite(e)||0===e||Math.sign(e)===Math.sign(n))return!1;n=e}return!0}(i,t.labels.length),o=e&&s.length>0&&s.every(Fe),l=n[t.labels.length-1];l&&pt(l)&&r.push({at:{x:l.x+4,y:l.y+30},text:`${t.name}: ${e?o?"valid":"outside range":"invalid sequence"}`,color:o?be:Me})}return{paths:o,labels:r}}function Ce(t){return Ft({id:t.id,name:t.name,points:t.labels.length,defaultStyle:!0===t.fill?{fill:!0,fillOpacity:.12,showLabels:!0}:{showLabels:!0},settings:!0===t.fill?Se:ke},e=>ze(t,e))}const Ae=["X","A","B","C","D"],Ne=[Ce({id:"xabcd-pattern",name:"XABCD Pattern",labels:Ae,fill:!0,ratios:"xabcd"}),Ce({id:"abcd-pattern",name:"ABCD Pattern",labels:["A","B","C","D"],ratios:"abcd"}),Ce({id:"elliott-impulse",name:"Elliott Impulse",labels:["1","2","3","4","5"]}),Ce({id:"elliott-correction",name:"Elliott Correction",labels:["A","B","C"]}),Ce({id:"head-shoulders",name:"Head and Shoulders",labels:["","LS","","H","","RS",""],neckline:[2,4]}),Ce({id:"gartley",name:"Gartley",labels:Ae,fill:!0,harmonic:_e.gartley}),Ce({id:"bat",name:"Bat",labels:Ae,fill:!0,harmonic:_e.bat}),Ce({id:"butterfly",name:"Butterfly",labels:Ae,fill:!0,harmonic:_e.butterfly}),Ce({id:"crab",name:"Crab",labels:Ae,fill:!0,harmonic:_e.crab}),Ce({id:"shark",name:"Shark",labels:Ae,fill:!0,harmonic:_e.shark}),Ce({id:"cypher",name:"Cypher",labels:Ae,fill:!0,harmonic:"cypher"})],$e=new Map;function Oe(t){$e.set(t.id,t)}function De(t){const e=$e.get(t);if(void 0===e)throw new Error(`openalgo-charts: unknown drawing tool "${t}"`);return e}function We(t){return $e.has(t)}function He(){return Array.from($e.values())}function Be(t){const e=t.split("+").map(t=>t.trim().toLowerCase());return{key:e[e.length-1]??"",alt:e.includes("alt"),ctrl:e.includes("ctrl")||e.includes("control"),shift:e.includes("shift")}}function Re(t){const e=(t.key??"").toLowerCase();if(""===e)return null;const n=!0===t.altKey,i=!0===t.ctrlKey||!0===t.metaKey,o=!0===t.shiftKey;if(!n&&!i)return null;for(const t of $e.values()){if(void 0===t.shortcut)continue;const r=Be(t.shortcut);if(r.key===e&&r.alt===n&&r.ctrl===i&&r.shift===o)return t.id}return null}function je(){const t={};for(const e of $e.values())void 0!==e.shortcut&&(t[e.id]=e.shortcut);return t}const Ee=z([v]);function Ue(t){return $e.get(t)?.settings??Ee}const Ve={path:"style.fillOpacity",label:"Opacity",kind:"opacity",min:0,max:1,step:.01,group:"fill"},qe={...k,label:"Show price"},Je={path:"style.showStats",label:"Show stats",kind:"boolean",group:"behavior"},Ke={path:"style.pressure",label:"Pen pressure",kind:"boolean",group:"line"},Xe=[{path:"style.accountSize",label:"Account size",kind:"number",min:0,step:1e3,group:"behavior"},{path:"style.risk",label:"Risk %",kind:"number",min:0,max:100,step:.1,group:"behavior"},{path:"props.showHeader",label:"Show direction and ratio",kind:"boolean",group:"behavior"},{path:"props.showLossSize",label:"Show loss and size",kind:"boolean",group:"behavior"},{path:"props.showTargetLabel",label:"Show target label",kind:"boolean",group:"behavior"},{path:"props.showStopLabel",label:"Show stop label",kind:"boolean",group:"behavior"},{path:"props.showPrices",label:"Show level prices",kind:"boolean",group:"behavior"},{path:"props.profitColor",label:"Profit zone",kind:"color",group:"fill"},{path:"props.lossColor",label:"Loss zone",kind:"color",group:"fill"}];function Ye(t){const{ctx:e,rc:n,style:i}=t,o=n.dpr;e.strokeStyle=i.color,e.lineWidth=Math.max(1,Math.round(i.lineWidth*o)),e.lineJoin="round",e.lineCap="round",e.setLineDash("dashed"===i.lineStyle?[6*o,4*o]:"dotted"===i.lineStyle?[1*o,3*o]:[])}function Ge(t){return t.style.fillColor??t.style.color}function Ze(t,e){if(!0!==t.style.fill)return;const{ctx:n}=t;n.save(),n.globalAlpha=t.style.fillOpacity??.12,n.fillStyle=Ge(t),e(),n.restore()}const Qe={value:""};function tn(t){return t.text??Qe}function en(t,e,n){const i=n??(!0===t.bold?"700":"");return`${!0===t.italic?"italic ":""}${""===i?"":`${i} `}${e}px ${void 0===t.fontFamily||""===t.fontFamily?"ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif":t.fontFamily}`}function nn(t,e){const n=t.text?.value;return void 0===n||""===n?e:n}function on(t,e,n,i,o,r={}){const{ctx:s,rc:l,style:a}=t,c=tn(t.drawing),h=(c.fontSize??11)*l.dpr;s.save(),s.setLineDash([]),s.font=en(c,h),s.textBaseline="middle",s.textAlign="left";const d=ct(s,e),p=!0===r.center?n-d/2:n;s.globalAlpha=.85,s.fillStyle=l.theme.background,s.fillRect(p-2*l.dpr,i-.75*h,d+4*l.dpr,1.5*h),void 0!==r.tint&&(s.globalAlpha=.22,s.fillStyle=r.tint,s.fillRect(p-2*l.dpr,i-.75*h,d+4*l.dpr,1.5*h)),s.globalAlpha=1,s.fillStyle=c.color??o??a.color,s.fillText(e,p,i),s.restore()}function rn(t,e,n,i,o,r={}){const{ctx:s,rc:l}=t,a=l.dpr,c=tn(t.drawing),h=(c.fontSize??11)*a;s.save(),s.setLineDash([]),s.globalAlpha=1,s.font=en(c,h),s.textBaseline="middle",s.textAlign="left";const u=5*a,x=3*a,f=1.35*h;let y=0;for(const t of e)y=Math.max(y,ct(s,t));const g=y+2*u,m=f*e.length+2*x,b=r.align??"left",M="center"===b?n-g/2:"right"===b?n-g:n,v=r.place??"above",w="above"===v?i-m-4*a:"below"===v?i+4*a:i-m/2;s.beginPath(),p(s,M,w,g,m,3*a),s.fillStyle=o,s.fill(),s.fillStyle=c.color??d(o);for(let t=0;t<e.length;t++)s.fillText(e[t],M+u,w+x+f*(t+.5));return s.restore(),m}function sn(t,e=0){const[n,i]=Math.abs(t).toFixed(e).split(".");return(t<0?"-":"")+n.replace(/\B(?=(\d{3})+(?!\d))/g,",")+(void 0===i?"":"."+i)}function ln(t){const e=new Date(1e3*t),n=t=>String(t).padStart(2,"0");return`${e.getUTCFullYear()}-${n(e.getUTCMonth()+1)}-${n(e.getUTCDate())}`}function an(t){const e=Math.max(0,Math.round(Math.abs(t))),n=Math.floor(e/86400),i=Math.floor(e%86400/3600),o=Math.floor(e%3600/60);return n>0?`${n}d ${i}h`:i>0?`${i}h ${o}m`:`${o}m`}function cn(t,e,n){const i=Math.max(8,5*t.style.lineWidth)*t.rc.dpr,o=Math.atan2(n.y-e.y,n.x-e.x);t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(n.x-i*Math.cos(o-.4),n.y-i*Math.sin(o-.4)),t.ctx.lineTo(n.x-i*Math.cos(o+.4),n.y-i*Math.sin(o+.4)),t.ctx.closePath(),t.ctx.fillStyle=t.style.color,t.ctx.fill()}const hn="#26a69a",dn="#ef5350";function pn(t){if(!0!==t.style.showStats)return;const[e,n]=t.pts,[i,o]=t.drawing.points,r=o.price-i.price,s=0!==i.price?r/i.price*100:0,l=r>=0,a=l?"+":"",c=t.rc.dataLayer.timeToIndexFloat(i.time),h=t.rc.dataLayer.timeToIndexFloat(o.time),d=Math.abs(Math.round(h-c)),p=180*Math.atan2(e.y-n.y,n.x-e.x)/Math.PI,u=`${a}${t.formatPrice(r)} (${a}${s.toFixed(2)}%)  ${sn(d)} bars  ${p.toFixed(1)} deg`;on(t,u,(e.x+n.x)/2,(e.y+n.y)/2-10*t.rc.dpr,t.style.color,{tint:l?hn:dn,center:!0})}function un(t,e,n){const{ctx:i,style:o}=t,r=t.pts;if(i.save(),i.globalAlpha=n,i.lineCap="round",i.lineJoin="round",!0!==o.pressure)return i.strokeStyle=o.color,i.lineWidth=e,i.beginPath(),st(i,r),i.stroke(),void i.restore();const s=r.length,l=[],a=[];for(let n=0;n<s;n++){const i=r[0===n?0:n-1],o=r[n===s-1?s-1:n+1],c=Math.hypot(o.x-i.x,o.y-i.y)||1,h=-(o.y-i.y)/c,d=(o.x-i.x)/c,p=t.drawing.points[n]?.pressure,u=lt(e,p??.5)/2;l.push({x:r[n].x+h*u,y:r[n].y+d*u}),a.push({x:r[n].x-h*u,y:r[n].y-d*u})}a.reverse(),i.fillStyle=o.color,i.beginPath(),st(i,l.concat(a)),i.closePath(),i.fill(),i.restore()}function xn(t,e){return(t??e).filter(t=>!1!==t.enabled&&Number.isFinite(t.ratio))}function fn(t){return t.color??q(t.ratio)}function yn(t){return t.label??X(t.ratio)}function gn(n,i,o,r){return{id:n,name:i,points:2,angleLock:!0,defaultStyle:{extendLeft:o,extendRight:r},settings:z([v,_,Je]),draw:t=>{const[e,n]=c(t.pts[0],t.pts[1],t.rc.plotWidth*t.rc.dpr,t.style.extendLeft??o,t.style.extendRight??r);Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(n.x,n.y),t.ctx.stroke(),t.ctx.setLineDash([]),pn(t)},distance:(n,i,s)=>{const l=s.drawing.style.extendLeft??o,a=s.drawing.style.extendRight??r;if(l&&a)return e(n,i,s.pts[0],s.pts[1]);const[h,d]=c(s.pts[0],s.pts[1],s.rc.plotWidth,l,a);return t(n,i,h,d)}}}const mn={...gn("trend-line","Trend Line",!1,!1),shortcut:"Alt+T"},bn=gn("ray","Ray",!1,!0),Mn=gn("extended-line","Extended Line",!0,!0),vn={id:"arrow",name:"Arrow",points:2,angleLock:!0,settings:z([v,Je]),draw:t=>{const[e,n]=t.pts;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(n.x,n.y),t.ctx.stroke(),cn(t,e,n),t.ctx.setLineDash([]),pn(t)},distance:(e,n,i)=>t(e,n,i.pts[0],i.pts[1])},wn={id:"horizontal-line",name:"Horizontal Line",points:1,shortcut:"Alt+H",defaultStyle:{showLabels:!0},settings:z([v,qe,T]),draw:t=>{const e=Math.round(t.pts[0].y)+.5;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(0,e),t.ctx.lineTo(t.rc.plotWidth*t.rc.dpr,e),t.ctx.stroke(),t.ctx.setLineDash([]),!1!==t.style.showLabels&&on(t,t.formatPrice(t.drawing.points[0].price),4*t.rc.dpr,e-8*t.rc.dpr)},distance:(t,e,i)=>n(e,i.pts[0].y)},_n={id:"horizontal-ray",name:"Horizontal Ray",points:1,shortcut:"Alt+J",defaultStyle:{showLabels:!0},settings:z([v,qe,T]),draw:t=>{const e=Math.round(t.pts[0].y)+.5;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,e),t.ctx.lineTo(t.rc.plotWidth*t.rc.dpr,e),t.ctx.stroke(),t.ctx.setLineDash([]),!1!==t.style.showLabels&&on(t,t.formatPrice(t.drawing.points[0].price),t.pts[0].x+4*t.rc.dpr,e-8*t.rc.dpr)},distance:(t,e,i)=>t<i.pts[0].x?null:n(e,i.pts[0].y)},kn={id:"vertical-line",name:"Vertical Line",points:1,shortcut:"Alt+V",settings:Ee,draw:t=>{const e=Math.round(t.pts[0].x)+.5;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e,0),t.ctx.lineTo(e,t.rc.plotHeight*t.rc.dpr),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,n)=>i(t,n.pts[0].x)},Sn={id:"cross-line",name:"Cross Line",points:1,shortcut:"Alt+C",settings:Ee,draw:t=>{const e=Math.round(t.pts[0].x)+.5,n=Math.round(t.pts[0].y)+.5;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e,0),t.ctx.lineTo(e,t.rc.plotHeight*t.rc.dpr),t.ctx.moveTo(0,n),t.ctx.lineTo(t.rc.plotWidth*t.rc.dpr,n),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,o)=>Math.min(i(t,o.pts[0].x),n(e,o.pts[0].y))},Tn=z([v,w,F]),Pn={id:"rectangle",name:"Rectangle",points:2,defaultStyle:{fill:!0},settings:Tn,draw:t=>{const e=r(t.pts[0],t.pts[1]);Ze(t,()=>t.ctx.fillRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0)),Ye(t),t.ctx.strokeRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0),t.ctx.setLineDash([]),Un(t,e)},distance:(t,e,n)=>l(t,e,n.pts[0],n.pts[1],!0===n.drawing.style.fill)},Ln={id:"ellipse",name:"Ellipse",points:2,defaultStyle:{fill:!0},settings:Tn,draw:t=>{const e=r(t.pts[0],t.pts[1]),n=(e.x0+e.x1)/2,i=(e.y0+e.y1)/2,o=Math.max(1,(e.x1-e.x0)/2),s=Math.max(1,(e.y1-e.y0)/2),l=()=>{t.ctx.beginPath(),t.ctx.ellipse(n,i,o,s,0,0,2*Math.PI)};Ze(t,()=>{l(),t.ctx.fill()}),Ye(t),l(),t.ctx.stroke(),t.ctx.setLineDash([]),Un(t,e,"center","middle")},distance:(t,e,n)=>a(t,e,n.pts[0],n.pts[1],!0===n.drawing.style.fill)},Fn={id:"parallel-channel",name:"Parallel Channel",points:3,defaultStyle:{fill:!0},settings:Tn,draw:t=>{const[e,n,i]=t.pts,o=i.y-(e.y+.5*(n.y-e.y)),r={x:e.x,y:e.y+o},l={x:n.x,y:n.y+o};Ze(t,()=>{t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(n.x,n.y),t.ctx.lineTo(l.x,l.y),t.ctx.lineTo(r.x,r.y),t.ctx.closePath(),t.ctx.fill()}),Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(n.x,n.y),t.ctx.moveTo(r.x,r.y),t.ctx.lineTo(l.x,l.y),t.ctx.stroke(),t.ctx.setLineDash([]),Un(t,s([e,n,r,l]))},distance:(e,n,i)=>{const[o,r,s]=i.pts,l=s.y-(o.y+.5*(r.y-o.y)),a=t(e,n,o,r),c=t(e,n,{x:o.x,y:o.y+l},{x:r.x,y:r.y+l});return Math.min(a,c)}},In=z([v,S,w,_,T]);function zn(t,e,n){return{id:t,name:e,points:n,defaultStyle:{showLabels:!0,levels:Z(Q),fill:!0,fillOpacity:.06},settings:In,draw:t=>{const e=xn(t.style.levels,Q),i=t.drawing.points,o=t.rc.dpr,r=2===n?i[0].price:i[2].price,s=i[1].price-i[0].price,l=Math.min(t.pts[0].x,t.pts[n-1].x),a=Math.max(t.pts[0].x,t.pts[n-1].x),c=!0===t.style.extendLeft?0:l,h=!0===t.style.extendRight?t.rc.plotWidth*o:a;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y),t.ctx.lineTo(t.pts[1].x,t.pts[1].y),3===n&&t.ctx.lineTo(t.pts[2].x,t.pts[2].y),t.ctx.stroke();let d=null;for(const n of e){const e=r+s*n.ratio,i=Math.round(t.rc.priceScale.priceToY(e)*o)+.5,l=fn(n);!0===t.style.fill&&null!==d&&(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.06,t.ctx.fillStyle=t.style.fillColor??l,t.ctx.fillRect(c,Math.min(d,i),h-c,Math.abs(i-d)),t.ctx.restore()),d=i,t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(c,i),t.ctx.lineTo(h,i),t.ctx.stroke(),!1!==t.style.showLabels&&on(t,`${yn(n)}  ${t.formatPrice(e)}`,c+4*o,i-8*o,l)}t.ctx.setLineDash([])},distance:(t,e,i)=>{const o=xn(i.drawing.style.levels,Q),r=i.drawing.points,s=2===n?r[0].price:r[2].price,l=r[1].price-r[0].price,a=!0===i.drawing.style.extendLeft?0:Math.min(i.pts[0].x,i.pts[n-1].x),c=!0===i.drawing.style.extendRight?i.rc.plotWidth:Math.max(i.pts[0].x,i.pts[n-1].x);if(t<a-4||t>c+4)return null;let h=1/0;for(const t of o){const n=Math.abs(e-i.rc.priceScale.priceToY(s+l*t.ratio));n<h&&(h=n)}return h}}}const Cn=zn("fib-retracement","Fib Retracement",2),An=zn("fib-extension","Fib Extension",3),Nn=z([b,M,Ve,k,T]),$n={id:"measure",name:"Measure",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.14},settings:Nn,draw:t=>{const e=r(t.pts[0],t.pts[1]),n=t.rc.dpr,i=t.drawing.points,o=i[1].price-i[0].price,s=0!==i[0].price?o/i[0].price*100:0,l=o>=0,a=l?hn:dn;t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.14,t.ctx.fillStyle=a,t.ctx.fillRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0),t.ctx.restore(),Ye(t),t.ctx.strokeStyle=a,t.ctx.strokeRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0),t.ctx.setLineDash([]);const c=(e.x0+e.x1)/2,h=t.pts[0].y,d=t.pts[1].y;if(t.ctx.strokeStyle=a,t.ctx.beginPath(),t.ctx.moveTo(e.x0,h),t.ctx.lineTo(e.x1,h),t.ctx.stroke(),cn(t,{x:e.x0,y:h},{x:e.x1,y:h}),t.ctx.beginPath(),t.ctx.moveTo(c,h),t.ctx.lineTo(c,d),t.ctx.stroke(),cn(t,{x:c,y:h},{x:c,y:d}),!1===t.style.showLabels)return;const p=t.rc.dataLayer.timeToIndexFloat(i[0].time),u=t.rc.dataLayer.timeToIndexFloat(i[1].time),x=Math.abs(Math.round(u-p)),f=l?"+":"",y=[`${f}${t.formatPrice(o)} (${f}${s.toFixed(2)}%)`,`${sn(x)} bars, ${an(i[1].time-i[0].time)}`],g=t.rc.bars?.();if(void 0!==g&&g.length>0){const t=Math.min(i[0].time,i[1].time),e=Math.max(i[0].time,i[1].time);let n=0,o=!1,r=0,s=g.length;for(;r<s;){const e=r+s>>>1;g[e].time<t?r=e+1:s=e}for(let t=r;t<g.length;t++){const i=g[t];if(i.time>e)break;void 0!==i.volume&&(n+=i.volume,o=!0)}o&&y.push(`Vol ${function(t){const e=Math.abs(t);return e>=1e9?`${(t/1e9).toFixed(2)}B`:e>=1e6?`${(t/1e6).toFixed(2)}M`:e>=1e3?`${(t/1e3).toFixed(2)}K`:sn(t)}(n)}`)}rn(t,y,c,Math.max(h,d)+6*n,a,{align:"center",place:"below"})},distance:(t,e,n)=>l(t,e,n.pts[0],n.pts[1],!0)};function On(t){return!Number.isFinite(t)||t<=0?"0":t>=100?sn(t):t>=1?sn(t,2).replace(/\.?0+$/,""):t.toPrecision(3).replace(/\.?0+$/,"")}function Dn(t,e,n){return{id:t,name:e,points:2,defaultStyle:{showLabels:!0,fillOpacity:.13,accountSize:1e5,risk:1},settings:z([v,Ve,k,Xe,T]),expand:(t,e)=>function(t,e,n){const i=t[0],o=t[1]??i,r=n?1:-1;if(void 0!==e.toPixel&&void 0!==e.fromPixel){const t=e.toPixel(i),n=e.toPixel(o);if(null!==t&&null!==n){const o=Math.abs(n.y-t.y)>=6,s=o?n.y:t.y-64*r*2,l=t.x+(o?Math.max(Math.abs(n.x-t.x),48):150),a=t.y-(s-t.y)/2,c=e.fromPixel({x:l,y:s}),h=e.fromPixel({x:l,y:a});if(null!==c&&null!==h)return[{...i},c,h]}}const s=o.price!==i.price,l=s?o.price-i.price:r*(.02*Math.abs(i.price)||2),a=Math.max(5,Math.round(.08*e.visibleBars)),c=Math.max(1,e.barSeconds)*a,h=s&&o.time>i.time?o.time:i.time+c;return[{...i},{time:h,price:i.price+l},{time:h,price:i.price-l/2}]}(t,e,n),constrain:(t,e)=>function(t,e){const n=t.map(t=>({...t}));if(n.length<3)return n;const[i,o,r]=n,s=Math.sign(o.price-i.price),l=Math.sign(r.price-i.price);return 0===s||0===l||s!==l||(1===e?r.price=2*i.price-r.price:o.price=2*i.price-o.price),n}(t,e),draw:t=>{const e=t.drawing.points;if(e.length<2||t.pts.length<2)return;const[i,o]=e,r=e.length>=3?e[2]:{time:o.time,price:i.price-(o.price-i.price)/2},s=t.rc.dpr,l=t.pts.map(t=>t.x),a=Math.min(...l),c=Math.max(...l),h=t.rc.priceScale.priceToY(i.price)*s,d=t.rc.priceScale.priceToY(o.price)*s,p=t.rc.priceScale.priceToY(r.price)*s,u=t.drawing.props??{},x="string"==typeof u.profitColor?u.profitColor:hn,f="string"==typeof u.lossColor?u.lossColor:dn,y=(e,n,i)=>{t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.13,t.ctx.fillStyle=i,t.ctx.fillRect(a,Math.min(e,n),c-a,Math.abs(n-e)),t.ctx.restore()};y(h,d,x),y(h,p,f),Ye(t);for(const[e,n]of[[d,x],[p,f],[h,t.style.color]]){const i=Math.round(e)+.5;t.ctx.strokeStyle=n,t.ctx.beginPath(),t.ctx.moveTo(a,i),t.ctx.lineTo(c,i),t.ctx.stroke()}if(t.ctx.setLineDash([]),!1===t.style.showLabels)return;const g=Math.abs(i.price-r.price),m=Math.abs(o.price-i.price),b=g>0?m/g:0,M=t=>(0!==i.price?t/i.price*100:0).toFixed(2),v=o.price===i.price?n?"Long":"Short":o.price>i.price?"Long":"Short",w=t.style.accountSize??0,_=w>0?w*(t.style.risk??0)/100:0,k=g>0?_/g:0,S=(a+c)/2,T=e=>!0===u.showPrices?`  @ ${t.formatPrice(e)}`:"";let P=Math.min(h,d,p);!1!==u.showLossSize&&_>0&&(P-=rn(t,[`Loss ${sn(_,2)}  Size ${On(k)}`],S,P,t.rc.theme.background,{align:"center",place:"above"})+4*s),!1!==u.showHeader&&rn(t,[`${v}  R:R ${b.toFixed(2)}`],S,P,t.rc.theme.background,{align:"center",place:"above"}),!1!==u.showTargetLabel&&m>0&&rn(t,[`Target +${M(m)}%${T(o.price)}`],S,(h+d)/2,x,{align:"center",place:"middle"}),!1!==u.showStopLabel&&g>0&&rn(t,[`Stop -${M(g)}%${T(r.price)}`],S,(h+p)/2,f,{align:"center",place:"middle"})},distance:(t,e,n)=>{const i=n.drawing.points;if(n.pts.length<2||i.length<2)return null;const o=n.pts.map(t=>t.x),r=Math.min(...o),s=Math.max(...o);if(t<r-4||t>s+4)return null;const[l,a]=i,c=i.length>=3?i[2].price:l.price-(a.price-l.price)/2,h=[l.price,a.price,c].map(t=>n.rc.priceScale.priceToY(t)),d=Math.min(...h),p=Math.max(...h);return e>=d&&e<=p?0:Math.min(Math.abs(e-d),Math.abs(e-p))}}}const Wn=Dn("long-position","Long Position",!0),Hn=Dn("short-position","Short Position",!1),Bn=1.35,Rn=14;function jn(t,e,n,i){const o=n.split("\n");if(!0!==e.wrap)return o;const r=[];for(const e of o){const n=e.split(/\s+/).filter(t=>""!==t);if(0===n.length){r.push("");continue}let o=n[0];for(let e=1;e<n.length;e++){const s=`${o} ${n[e]}`;t.measureText(s).width>i?(r.push(o),o=n[e]):o=s}r.push(o)}return r}function En(t,e,n,i){const o=(e.fontSize??Rn)*i;t.font=en(e,o);const r=jn(t,e,n,(e.wrapWidth??220)*i);let s=0;for(const e of r)s=Math.max(s,t.measureText(e).width);const l=o*Bn;return{lines:r,width:s+10*i,height:r.length*l+10*i,lineHeight:l}}function Un(t,e,n="left",i="top"){const{ctx:o,rc:r,style:s}=t,l=tn(t.drawing);if(""===l.value)return;const a=r.dpr,c=(l.fontSize??Rn)*a,h=6*a;o.save(),o.setLineDash([]),o.font=en(l,c),o.fillStyle=l.color??s.color,o.textBaseline="top";const d=jn(o,l,l.value,Math.max(20*a,e.x1-e.x0-2*h)),p=c*Bn,u=d.length*p,x=l.align??n;o.textAlign=x;const f="center"===x?(e.x0+e.x1)/2:"right"===x?e.x1-h:e.x0+h;let y;if("outside"===l.position)y=e.y0-u-h;else{const t=l.valign??i;y="middle"===t?(e.y0+e.y1-u)/2:"bottom"===t?e.y1-u-h:e.y0+h}for(const t of d)o.fillText(t,f,y),y+=p;o.restore()}const Vn={id:"text",name:"Text",points:1,defaultText:{value:"Text",fontSize:Rn},settings:z([L],{textIsContent:!0}),draw:t=>{const{ctx:e,rc:n,style:i}=t,o=n.dpr,r=tn(t.drawing),s=En(e,r,nn(t.drawing,"Text"),o),l=t.pts[0].x,a=r.valign??"top",c="middle"===a?t.pts[0].y-s.height/2:"bottom"===a?t.pts[0].y-s.height:t.pts[0].y;e.save(),e.setLineDash([]),!0===r.background&&(e.globalAlpha=r.backgroundOpacity??1,e.fillStyle=r.backgroundColor??n.theme.background,e.beginPath(),e.roundRect(l,c,s.width,s.height,4*o),e.fill(),e.globalAlpha=1),!0===r.border&&(e.strokeStyle=r.borderColor??i.color,e.lineWidth=Math.max(1,i.lineWidth*o),e.beginPath(),e.roundRect(l,c,s.width,s.height,4*o),e.stroke()),e.font=en(r,(r.fontSize??Rn)*o),e.fillStyle=r.color??i.color,e.textBaseline="top";const h=r.align??"left";e.textAlign=h;const d="center"===h?l+s.width/2:"right"===h?l+s.width-5*o:l+5*o;let p=c+5*o;for(const t of s.lines)e.fillText(t,d,p),p+=s.lineHeight;e.restore()},distance:(t,e,n)=>{const i=tn(n.drawing),o=nn(n.drawing,"Text"),r=i.fontSize??Rn,s=n.pts[0],l=Jn(),a=null===l?{width:o.length*r*.6+10,height:r*Bn+10}:En(l,i,o,1),c=i.valign??"top",h="middle"===c?s.y-a.height/2:"bottom"===c?s.y-a.height:s.y;return t>=s.x-3&&t<=s.x+a.width+3&&e>=h-3&&e<=h+a.height+3?0:null}};let qn;function Jn(){if(void 0!==qn)return qn;try{qn="undefined"==typeof document?null:document.createElement("canvas").getContext("2d")}catch{qn=null}return qn}const Kn={id:"path",name:"Path",points:0,settings:Ee,draw:t=>{if(!(t.pts.length<2)){Ye(t),t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y);for(let e=1;e<t.pts.length;e++)t.ctx.lineTo(t.pts[e].x,t.pts[e].y);t.ctx.stroke(),t.ctx.setLineDash([]),cn(t,t.pts[t.pts.length-2],t.pts[t.pts.length-1])}},distance:(t,e,n)=>n.pts.length<2?null:o(t,e,n.pts)},Xn={id:"brush",name:"Brush",points:0,freehand:!0,defaultStyle:{lineWidth:2},settings:z([v,Ke]),draw:t=>{t.pts.length<2||(Ye(t),un(t,Math.max(1,Math.round(t.style.lineWidth*t.rc.dpr)),1),t.ctx.setLineDash([]))},distance:(t,e,n)=>n.pts.length<2?null:o(t,e,n.pts)},Yn={id:"rotated-rectangle",name:"Rotated Rectangle",points:3,defaultStyle:{fill:!0,fillOpacity:.12},settings:Tn,draw:t=>{if(t.pts.length<3)return;const e=Gn(t.pts[0],t.pts[1],t.pts[2]);t.ctx.beginPath(),t.ctx.moveTo(e[0].x,e[0].y);for(let n=1;n<4;n++)t.ctx.lineTo(e[n].x,e[n].y);t.ctx.closePath(),Ze(t,()=>t.ctx.fill()),Ye(t),t.ctx.stroke(),t.ctx.setLineDash([]),Un(t,s(e),"center","middle")},distance:(e,n,i)=>{if(i.pts.length<3)return null;const o=Gn(i.pts[0],i.pts[1],i.pts[2]);if(!0===i.drawing.style.fill&&function(t,e,n){let i=!1;for(let o=0,r=n.length-1;o<n.length;r=o++){const s=n[o],l=n[r];s.y>e!=l.y>e&&t<(l.x-s.x)*(e-s.y)/(l.y-s.y)+s.x&&(i=!i)}return i}(e,n,o))return 0;let r=1/0;for(let i=0;i<4;i++)r=Math.min(r,t(e,n,o[i],o[(i+1)%4]));return r}};function Gn(t,e,n){const i=e.x-t.x,o=e.y-t.y,r=Math.hypot(i,o)||1,s=-o/r,l=i/r,a=(n.x-e.x)*s+(n.y-e.y)*l;return[t,e,{x:e.x+s*a,y:e.y+l*a},{x:t.x+s*a,y:t.y+l*a}]}const Zn={id:"double-curve",name:"Double Curve",points:3,settings:Ee,draw:t=>{if(t.pts.length<3)return;const[e,n,i]=t.pts;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.bezierCurveTo(n.x,n.y,e.x+i.x-n.x,e.y+i.y-n.y,i.x,i.y),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,n)=>{if(n.pts.length<3)return null;const[i,r,s]=n.pts;return o(t,e,function(t,e,n,i,o){const r=[];for(let s=0;s<=o;s++){const l=s/o,a=1-l,c=a*a*a,h=3*a*a*l,d=3*a*l*l,p=l*l*l;r.push({x:c*t.x+h*e.x+d*n.x+p*i.x,y:c*t.y+h*e.y+d*n.y+p*i.y})}return r}(i,r,{x:i.x+s.x-r.x,y:i.y+s.y-r.y},s,24))}};const Qn={id:"cyclic-lines",name:"Cyclic Lines",points:2,defaultStyle:{lineStyle:"dashed"},settings:Ee,draw:t=>{const[e,n]=t.pts,i=n.x-e.x;if(!Number.isFinite(i)||Math.abs(i)<.5)return;const o=t.rc.plotHeight*t.rc.dpr,r=t.rc.plotWidth*t.rc.dpr;Ye(t),t.ctx.beginPath();for(let n=0;n<=12;n++){const s=Math.round(e.x+i*n)+.5;s<0||s>r||(t.ctx.moveTo(s,0),t.ctx.lineTo(s,o))}t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,n)=>{const i=n.pts[1].x-n.pts[0].x;if(!Number.isFinite(i)||Math.abs(i)<.5)return null;const o=Math.round((t-n.pts[0].x)/i);return o<0||o>12?null:Math.abs(t-(n.pts[0].x+i*o))}},ti={id:"time-cycles",name:"Time Cycles",points:2,settings:Ee,draw:t=>{const[e,n]=t.pts,i=n.x-e.x;if(!Number.isFinite(i)||Math.abs(i)<1)return;const o=Math.abs(i)/2;Ye(t),t.ctx.beginPath();for(let n=0;n<12;n++){const r=e.x+i*n+i/2;r+o<0||r-o>t.rc.plotWidth*t.rc.dpr||(t.ctx.moveTo(r+o,e.y),t.ctx.arc(r,e.y,o,0,Math.PI,!0))}t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,n)=>{const[i,o]=n.pts,r=o.x-i.x;if(!Number.isFinite(r)||Math.abs(r)<1)return null;const s=Math.abs(r)/2;let l=1/0;for(let n=0;n<12;n++){const o=i.x+r*n+r/2;e>i.y+2||(l=Math.min(l,Math.abs(Math.hypot(t-o,e-i.y)-s)))}return Number.isFinite(l)?l:null}},ei={id:"sine-line",name:"Sine Line",points:2,settings:Ee,draw:t=>{const e=ni(t.pts[0],t.pts[1]);if(null!==e){Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e[0].x,e[0].y);for(let n=1;n<e.length;n++)t.ctx.lineTo(e[n].x,e[n].y);t.ctx.stroke(),t.ctx.setLineDash([])}},distance:(t,e,n)=>{const i=ni(n.pts[0],n.pts[1]);return null===i?null:o(t,e,i)}};function ni(t,e){const n=e.x-t.x,i=e.y-t.y;if(!Number.isFinite(n)||Math.abs(n)<1)return null;const o=[];for(let e=0;e<=64;e++){const r=e/64;o.push({x:t.x+n*r,y:t.y+i*Math.sin(r*Math.PI*2)})}return o}const ii=z([m,P,T],{textIsContent:!0}),oi={id:"price-label",name:"Price Label",points:1,settings:z([m,{...P,label:"Text (blank shows the price)"},T]),draw:t=>{const e=t.pts[0],n=t.rc.dpr,i=nn(t.drawing,t.formatPrice(t.drawing.points[0].price)),o=si(t,[i],e.x+18*n,e.y-30*n,12);li(t,o,e,t.style.color),ai(t,o,[i],t.style.color,12)},distance:(t,e,n)=>{const i=n.pts[0],o=i.x+18,r=i.y-30-11;return t>=o&&t<=o+64&&e>=r&&e<=r+22||Math.hypot(t-i.x,e-i.y)<=10?0:null}},ri={id:"callout",name:"Callout",points:2,defaultText:{value:"Note",fontSize:12},settings:ii,draw:t=>{if(t.pts.length<2)return;const[e,n]=t.pts,i=nn(t.drawing,"Note").split("\n"),o=si(t,i,n.x,n.y,12);li(t,o,e,t.style.color),ai(t,o,i,t.style.color,12)},distance:(e,n,i)=>{if(i.pts.length<2)return null;const[o,r]=i.pts;return Math.abs(e-r.x)<=60&&Math.abs(n-r.y)<=16?0:t(e,n,o,r)}};function si(t,e,n,i,o){const r=t.rc.dpr,s=tn(t.drawing),l=(s.fontSize??o)*r;t.ctx.save(),t.ctx.font=en(s,l);let a=0;for(const n of e)a=Math.max(a,t.ctx.measureText(n).width);t.ctx.restore();const c=a+14*r,h=1.4*l*e.length+8*r;return{x:n-c/2,y:i-h/2,w:c,h:h}}function li(t,e,n,i){const o=t.rc.dpr,r=e.x+e.w/2,s=e.y+e.h/2,l=Math.atan2(n.y-s,n.x-r),a=5*o,c=r+Math.cos(l)*(e.w/2-a),h=s+Math.sin(l)*(e.h/2-a);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.fillStyle=i,t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(c-Math.sin(l)*a,h+Math.cos(l)*a),t.ctx.lineTo(c+Math.sin(l)*a,h-Math.cos(l)*a),t.ctx.closePath(),t.ctx.fill(),t.ctx.beginPath(),p(t.ctx,e.x,e.y,e.w,e.h,5*o),t.ctx.fill(),t.ctx.beginPath(),t.ctx.arc(n.x,n.y,2.5*o,0,2*Math.PI),t.ctx.fill(),t.ctx.restore()}function ai(t,e,n,i,o){const r=t.rc.dpr,s=tn(t.drawing),l=(s.fontSize??o)*r;t.ctx.save(),t.ctx.setLineDash([]),t.ctx.font=en(s,l),t.ctx.textAlign="center",t.ctx.textBaseline="middle",t.ctx.fillStyle=s.color??d(i);const a=1.4*l,c=e.y+e.h/2-a*(n.length-1)/2;for(let i=0;i<n.length;i++)t.ctx.fillText(n[i],e.x+e.w/2,c+a*i);t.ctx.restore()}const ci=z([v,w]),hi={id:"flag-mark",name:"Flag Mark",points:1,defaultStyle:{fill:!0,fillOpacity:.95},settings:ci,draw:t=>{const e=t.pts[0],n=t.rc.dpr,i=22*n,o=15*n,r=11*n;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(e.x,e.y-i),t.ctx.stroke(),t.ctx.setLineDash([]),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y-i),t.ctx.lineTo(e.x+o,e.y-i+.28*r),t.ctx.lineTo(e.x+.72*o,e.y-i+.5*r),t.ctx.lineTo(e.x+o,e.y-i+.72*r),t.ctx.lineTo(e.x,e.y-i+r),t.ctx.closePath(),!1!==t.style.fill&&(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.95,t.ctx.fillStyle=Ge(t),t.ctx.fill(),t.ctx.restore()),t.ctx.stroke()},distance:(t,e,n)=>{const i=n.pts[0];return t>=i.x-4&&t<=i.x+16&&e>=i.y-24&&e<=i.y+2||Math.hypot(t-i.x,e-i.y)<=8?0:null}};function di(t,e,n,i,o){const r=t.rc.dpr,s=tn(t.drawing),l=(s.fontSize??o.size)*r;t.ctx.save(),t.ctx.font=en(s,l);let a=0;for(const n of e)a=Math.max(a,t.ctx.measureText(n).width);return t.ctx.restore(),{x:n,y:i,w:a+16*r,h:1.45*l*e.length+10*r}}function pi(t){return t.drawing.text?.backgroundColor??t.style.color}function ui(t,e,n,i,o=6){const r=t.rc.dpr,s=tn(t.drawing),l=(s.fontSize??i.size)*r,a=pi(t);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.globalAlpha=s.backgroundOpacity??i.opacity,t.ctx.fillStyle=a,t.ctx.beginPath(),p(t.ctx,e.x,e.y,e.w,e.h,o*r),t.ctx.fill(),t.ctx.globalAlpha=1,!0===s.border&&(t.ctx.strokeStyle=s.borderColor??t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*r),t.ctx.stroke()),t.ctx.font=en(s,l),t.ctx.textAlign="left",t.ctx.textBaseline="middle",t.ctx.fillStyle=s.color??d(a);const c=1.45*l,h=e.y+e.h/2-c*(n.length-1)/2;for(let i=0;i<n.length;i++)t.ctx.fillText(n[i],e.x+8*r,h+c*i);t.ctx.restore()}function xi(t,e,n,i,o,r){return t>=n&&t<=n+o&&e>=i&&e<=i+r?0:null}function fi(t,e,n){return{w:120,h:1.45*(t.text?.fontSize??n.size)*Math.max(1,e)+10}}const yi=(t,e)=>nn(t,e).split("\n").length,gi=z([m,b,I],{textIsContent:!0}),mi=z([m,I],{textIsContent:!0}),bi={size:12,opacity:.95},Mi={id:"note",name:"Note",points:1,defaultText:{value:"Note",fontSize:bi.size},settings:gi,draw:t=>{const e=t.rc.dpr,n=t.pts[0],i=nn(t.drawing,"Note").split("\n"),o=di(t,i,n.x+16*e,n.y-34*e,bi);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.strokeStyle=t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(o.x,o.y+o.h),t.ctx.stroke(),t.ctx.fillStyle=t.style.color,t.ctx.beginPath(),t.ctx.arc(n.x,n.y,3.5*e,0,2*Math.PI),t.ctx.fill(),t.ctx.restore(),ui(t,o,i,bi)},distance:(t,e,n)=>{const i=n.pts[0],o=fi(n.drawing,yi(n.drawing,"Note"),bi),r=xi(t,e,i.x+16,i.y-34,o.w,o.h);return null!==r?r:Math.hypot(t-i.x,e-i.y)<=8?0:null}},vi={size:12,opacity:.95},wi={id:"balloon",name:"Balloon",points:1,defaultText:{value:"Balloon",fontSize:vi.size},settings:mi,draw:t=>{const e=t.rc.dpr,n=t.pts[0],i=nn(t.drawing,"Balloon").split("\n"),o=di(t,i,0,0,vi),r={x:n.x-o.w/2,y:n.y-o.h-12*e,w:o.w,h:o.h};t.ctx.save(),t.ctx.setLineDash([]),t.ctx.globalAlpha=t.drawing.text?.backgroundOpacity??vi.opacity,t.ctx.fillStyle=pi(t),t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(n.x-6*e,r.y+r.h),t.ctx.lineTo(n.x+6*e,r.y+r.h),t.ctx.closePath(),t.ctx.fill(),t.ctx.restore(),ui(t,r,i,vi,8)},distance:(t,e,n)=>{const i=n.pts[0],o=fi(n.drawing,yi(n.drawing,"Balloon"),vi),r=xi(t,e,i.x-o.w/2,i.y-o.h-12,o.w,o.h);return null!==r?r:Math.hypot(t-i.x,e-i.y)<=8?0:null}},_i={size:11,opacity:.92},ki={id:"comment",name:"Comment",points:1,defaultText:{value:"Comment",fontSize:_i.size},settings:mi,draw:t=>{const e=t.rc.dpr,n=t.pts[0],i=nn(t.drawing,"Comment").split("\n"),o=di(t,i,0,0,_i),r={x:n.x+8*e,y:n.y-o.h-10*e,w:o.w,h:o.h};t.ctx.save(),t.ctx.setLineDash([]),t.ctx.globalAlpha=t.drawing.text?.backgroundOpacity??_i.opacity,t.ctx.fillStyle=pi(t),t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(r.x,r.y+r.h-5*e),t.ctx.lineTo(r.x+10*e,r.y+r.h),t.ctx.closePath(),t.ctx.fill(),t.ctx.restore(),ui(t,r,i,_i,3)},distance:(t,e,n)=>{const i=n.pts[0],o=fi(n.drawing,yi(n.drawing,"Comment"),_i),r=xi(t,e,i.x+8,i.y-o.h-10,o.w,o.h);return null!==r?r:Math.hypot(t-i.x,e-i.y)<=8?0:null}},Si={size:11,opacity:.95},Ti={id:"signpost",name:"Signpost",points:1,defaultText:{value:"Event",fontSize:Si.size},settings:gi,draw:t=>{const e=t.rc.dpr,n=t.pts[0],i=34*e,o=nn(t.drawing,"Event").split("\n"),r=di(t,o,0,0,Si);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.strokeStyle=t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(n.x,n.y-i),t.ctx.stroke(),t.ctx.fillStyle=t.style.color,t.ctx.beginPath(),t.ctx.arc(n.x,n.y,3*e,0,2*Math.PI),t.ctx.fill(),t.ctx.restore(),ui(t,{x:n.x-r.w/2,y:n.y-i-r.h,w:r.w,h:r.h},o,Si,4)},distance:(t,e,n)=>{const i=n.pts[0],o=fi(n.drawing,yi(n.drawing,"Event"),Si),r=xi(t,e,i.x-o.w/2,i.y-34-o.h,o.w,o.h);return null!==r?r:Math.abs(t-i.x)<=5&&e>=i.y-34&&e<=i.y+4?0:null}},Pi={size:11,opacity:.95},Li={id:"price-note",name:"Price Note",points:1,defaultText:{value:"Note",fontSize:Pi.size},settings:gi,draw:t=>{const e=t.rc.dpr,n=t.pts[0],i=[t.formatPrice(t.drawing.points[0].price),...nn(t.drawing,"Note").split("\n")],o=di(t,i,n.x+14*e,n.y-12*e,Pi);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.strokeStyle=t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(o.x,n.y),t.ctx.stroke(),t.ctx.restore(),ui(t,o,i,Pi,4)},distance:(t,e,n)=>{const i=n.pts[0],o=fi(n.drawing,yi(n.drawing,"Note")+1,Pi),r=xi(t,e,i.x+14,i.y-12,o.w,o.h);return null!==r?r:Math.hypot(t-i.x,e-i.y)<=8?0:null}};function Fi(t,e,n,i){const o=(e.fontSize??11)*i,r=Array.from({length:Math.max(...n.map(t=>t.length))},()=>0);t?.save();try{for(let s=0;s<n.length;s++){null!==t&&(t.font=en(e,o,0===s?!0===e.bold?"800":"600":void 0));for(let e=0;e<r.length;e++){const l=n[s][e]??"",a=null===t?l.length*o*.6:ct(t,l);r[e]=Math.max(r[e],a+14*i)}}}finally{t?.restore()}return r}const Ii={id:"table",name:"Table",points:1,defaultText:{value:"Level|Price\nEntry|-\nStop|-",fontSize:11},settings:z([m,{...b,label:"Border width"},P,T,{path:"text.backgroundColor",label:"Background color",kind:"color",group:"text"},{path:"text.backgroundOpacity",label:"Background opacity",kind:"opacity",min:0,max:1,step:.01,group:"text"},{path:"text.border",label:"Border",kind:"boolean",group:"text"},{path:"text.borderColor",label:"Border color",kind:"color",group:"text"}],{textIsContent:!0}),draw:t=>{const e=t.rc.dpr,n=t.pts[0],i=tn(t.drawing),o=zi(t.drawing),r=(i.fontSize??11)*e,s=7*e,l=1.7*r;t.ctx.save();const a=Fi(t.ctx,i,o,e),c=a.reduce((t,e)=>t+e,0),h=l*o.length;if(t.ctx.setLineDash([]),t.ctx.globalAlpha=i.backgroundOpacity??.92,t.ctx.fillStyle=i.backgroundColor??t.rc.theme.background,t.ctx.beginPath(),p(t.ctx,n.x,n.y,c,h,4*e),t.ctx.fill(),t.ctx.globalAlpha=1,!1!==i.border){t.ctx.strokeStyle=i.borderColor??t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.stroke(),t.ctx.globalAlpha=.45,t.ctx.beginPath();for(let e=1;e<o.length;e++)t.ctx.moveTo(n.x,n.y+l*e),t.ctx.lineTo(n.x+c,n.y+l*e);let r=n.x;for(let e=0;e<a.length-1;e++)r+=a[e],t.ctx.moveTo(r,n.y),t.ctx.lineTo(r,n.y+h);t.ctx.stroke(),t.ctx.globalAlpha=1}t.ctx.textBaseline="middle",t.ctx.textAlign="left";const d=en(i,r,!0===i.bold?"800":"600"),u=en(i,r);for(let e=0;e<o.length;e++){t.ctx.fillStyle=i.color??t.style.color,t.ctx.font=0===e?d:u;let r=n.x;for(let i=0;i<a.length;i++)t.ctx.fillText(o[e][i]??"",r+s,n.y+l*e+l/2),r+=a[i]}t.ctx.restore()},distance:(t,e,n)=>{const i=n.pts[0],o=zi(n.drawing),r=n.drawing.text?.fontSize??11,s=Fi(Jn(),tn(n.drawing),o,1).reduce((t,e)=>t+e,0);return xi(t,e,i.x,i.y,s,1.7*r*o.length)}};function zi(t){return nn(t,"Level|Price").split("\n").map(t=>t.split("|"))}const Ci={id:"price-range",name:"Price Range",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.1},settings:Nn,draw:t=>{const e=t.rc.dpr,n=t.drawing.points,i=n[1].price-n[0].price,o=0!==n[0].price?i/n[0].price*100:0,r=i>=0,s=r?hn:dn,l=Math.min(t.pts[0].x,t.pts[1].x),a=Math.max(t.pts[0].x,t.pts[1].x)+(t.pts[0].x===t.pts[1].x?60*e:0),c=t.pts[0].y,h=t.pts[1].y;t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.1,t.ctx.fillStyle=s,t.ctx.fillRect(l,Math.min(c,h),a-l,Math.abs(h-c)),t.ctx.restore(),Ye(t),t.ctx.strokeStyle=s,t.ctx.beginPath();for(const e of[c,h])t.ctx.moveTo(l,Math.round(e)+.5),t.ctx.lineTo(a,Math.round(e)+.5);const d=(l+a)/2;t.ctx.moveTo(d,c),t.ctx.lineTo(d,h);const p=h>c?1:-1;if(t.ctx.moveTo(d-4*e,h-5*e*p),t.ctx.lineTo(d,h),t.ctx.lineTo(d+4*e,h-5*e*p),t.ctx.moveTo(d-4*e,c+5*e*p),t.ctx.lineTo(d,c),t.ctx.lineTo(d+4*e,c+5*e*p),t.ctx.stroke(),t.ctx.setLineDash([]),!1===t.style.showLabels)return;const u=r?"+":"";on(t,`${u}${t.formatPrice(i)}  (${u}${o.toFixed(2)}%)`,d+6*e,(c+h)/2,s)},distance:(t,e,n)=>l(t,e,n.pts[0],n.pts[1],!0)},Ai={id:"date-range",name:"Date Range",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.1},settings:z([v,Ve,k,T]),draw:t=>{const e=t.rc.dpr,n=t.drawing.points,i=t.style.color,o=t.pts[0].x,r=t.pts[1].x,s=Math.min(t.pts[0].y,t.pts[1].y),l=Math.max(t.pts[0].y,t.pts[1].y)+(t.pts[0].y===t.pts[1].y?40*e:0);t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.1,t.ctx.fillStyle=i,t.ctx.fillRect(Math.min(o,r),s,Math.abs(r-o),l-s),t.ctx.restore(),Ye(t),t.ctx.beginPath();for(const e of[o,r])t.ctx.moveTo(Math.round(e)+.5,s),t.ctx.lineTo(Math.round(e)+.5,l);const a=(s+l)/2;t.ctx.moveTo(o,a),t.ctx.lineTo(r,a);const c=r>o?1:-1;if(t.ctx.moveTo(r-5*e*c,a-4*e),t.ctx.lineTo(r,a),t.ctx.lineTo(r-5*e*c,a+4*e),t.ctx.moveTo(o+5*e*c,a-4*e),t.ctx.lineTo(o,a),t.ctx.lineTo(o+5*e*c,a+4*e),t.ctx.stroke(),t.ctx.setLineDash([]),!1===t.style.showLabels)return;const h=t.rc.dataLayer.timeToIndexFloat(n[0].time),d=t.rc.dataLayer.timeToIndexFloat(n[1].time);on(t,`${Math.abs(Math.round(d-h))} bars`,(o+r)/2,s-10*e,i)},distance:(t,e,n)=>l(t,e,n.pts[0],n.pts[1],!0)},Ni={id:"forecast",name:"Forecast",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.12,lineStyle:"dashed"},settings:Nn,draw:t=>{const e=t.rc.dpr,n=t.drawing.points,[i,o]=t.pts,r=n[1].price-n[0].price,s=0!==n[0].price?r/n[0].price*100:0,l=r>=0,a=l?hn:dn,c=.35*Math.abs(o.y-i.y)+6*e;if(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.12,t.ctx.fillStyle=a,t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(o.x,o.y-c),t.ctx.lineTo(o.x,o.y+c),t.ctx.closePath(),t.ctx.fill(),t.ctx.restore(),Ye(t),t.ctx.strokeStyle=a,t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(o.x,o.y),t.ctx.stroke(),t.ctx.setLineDash([]),t.ctx.beginPath(),t.ctx.arc(i.x,i.y,3*e,0,2*Math.PI),t.ctx.fillStyle=a,t.ctx.fill(),!1===t.style.showLabels)return;const h=l?"+":"";rn(t,[t.formatPrice(n[0].price),ln(n[0].time)],i.x,i.y,a,{align:"center"}),rn(t,[`${h}${t.formatPrice(r)} (${h}${s.toFixed(2)}%) in ${an(n[1].time-n[0].time)}`,`${t.formatPrice(n[1].price)}, ${ln(n[1].time)}`],o.x,o.y,a,{align:"center",place:"below"});const d=t.rc.bars?.();if(void 0!==d&&d.length>0&&d[d.length-1].time>=n[1].time){let i=!1;for(const t of d)if(!(t.time<n[0].time)){if(t.time>n[1].time)break;if(l?t.high>=n[1].price:t.low<=n[1].price){i=!0;break}}rn(t,[i?"SUCCESS":"MISSED"],o.x,o.y+36*e,i?"#4a934a":"#8a4a4a",{align:"center",place:"below"})}},distance:(e,n,i)=>t(e,n,i.pts[0],i.pts[1])},$i={id:"circle",name:"Circle",points:2,defaultStyle:{fill:!0},settings:Tn,draw:t=>{const[e,n]=t.pts,i=Math.hypot(n.x-e.x,n.y-e.y);t.ctx.beginPath(),t.ctx.arc(e.x,e.y,i,0,2*Math.PI),Ze(t,()=>t.ctx.fill()),Ye(t),t.ctx.stroke(),t.ctx.setLineDash([]),Un(t,{x0:e.x-i,y0:e.y-i,x1:e.x+i,y1:e.y+i},"center","middle")},distance:(t,e,n)=>{const[i,o]=n.pts,r=Math.hypot(o.x-i.x,o.y-i.y),s=Math.hypot(t-i.x,e-i.y);return!0===n.drawing.style.fill&&s<=r?0:Math.abs(s-r)}},Oi={id:"triangle",name:"Triangle",points:3,defaultStyle:{fill:!0},settings:Tn,draw:t=>{t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y),t.ctx.lineTo(t.pts[1].x,t.pts[1].y),t.ctx.lineTo(t.pts[2].x,t.pts[2].y),t.ctx.closePath(),Ze(t,()=>t.ctx.fill()),Ye(t),t.ctx.stroke(),t.ctx.setLineDash([]),Un(t,s(t.pts.slice(0,3)),"center","middle")},distance:(t,e,n)=>o(t,e,[...n.pts,n.pts[0]])},Di={id:"polyline",name:"Polyline",points:0,defaultStyle:{fill:!1},settings:z([v,w]),draw:t=>{if(!(t.pts.length<2)){t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y);for(let e=1;e<t.pts.length;e++)t.ctx.lineTo(t.pts[e].x,t.pts[e].y);!0===t.style.fill&&(t.ctx.closePath(),Ze(t,()=>t.ctx.fill())),Ye(t),t.ctx.stroke(),t.ctx.setLineDash([])}},distance:(t,e,n)=>o(t,e,n.pts)};const Wi={id:"arc",name:"Arc",points:3,settings:Ee,draw:t=>{const[e,n,i]=t.pts;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.quadraticCurveTo(2*n.x-(e.x+i.x)/2,2*n.y-(e.y+i.y)/2,i.x,i.y),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,n)=>o(t,e,function(t,e,n){const i=2*e.x-(t.x+n.x)/2,o=2*e.y-(t.y+n.y)/2,r=[];for(let e=0;e<=16;e++){const s=e/16,l=1-s;r.push({x:l*l*t.x+2*l*s*i+s*s*n.x,y:l*l*t.y+2*l*s*o+s*s*n.y})}return r}(n.pts[0],n.pts[1],n.pts[2]))},Hi={id:"curve",name:"Curve",points:3,settings:Ee,draw:t=>{const[e,n,i]=t.pts;Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.quadraticCurveTo(n.x,n.y,i.x,i.y),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,n)=>{const[i,r,s]=n.pts,l=[];for(let t=0;t<=16;t++){const e=t/16,n=1-e;l.push({x:n*n*i.x+2*n*e*r.x+e*e*s.x,y:n*n*i.y+2*n*e*r.y+e*e*s.y})}return o(t,e,l)}};function Bi(t,e,n,i="vertical"){return{id:t,name:e,points:1,defaultStyle:{fill:!0,fillOpacity:.9},settings:ci,draw:t=>{const e=t.rc.dpr,{x:o,y:r}=t.pts[0],s=16*e,l=6*e,a=n?1:-1,c=r+2*a*e;if(t.ctx.beginPath(),"vertical"!==i){const n="right"===i?-1:1,a=o+2*n*e;t.ctx.moveTo(a,r),t.ctx.lineTo(a+n*l,r-l),t.ctx.lineTo(a+n*l,r-l/2.4),t.ctx.lineTo(a+n*s,r-l/2.4),t.ctx.lineTo(a+n*s,r+l/2.4),t.ctx.lineTo(a+n*l,r+l/2.4),t.ctx.lineTo(a+n*l,r+l),t.ctx.closePath()}else t.ctx.moveTo(o,c),t.ctx.lineTo(o-l,c+a*l),t.ctx.lineTo(o-l/2.4,c+a*l),t.ctx.lineTo(o-l/2.4,c+a*s),t.ctx.lineTo(o+l/2.4,c+a*s),t.ctx.lineTo(o+l/2.4,c+a*l),t.ctx.lineTo(o+l,c+a*l),t.ctx.closePath();!1!==t.style.fill&&(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.9,t.ctx.fillStyle=Ge(t),t.ctx.fill(),t.ctx.restore()),Ye(t),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>{const o=i.pts[0],r=n?e-o.y:o.y-e;return Math.abs(t-o.x)<=8&&r>=-4&&r<=20?0:null}}}const Ri=Bi("arrow-up","Arrow Up",!0),ji=Bi("arrow-down","Arrow Down",!1),Ei=Bi("arrow-left","Arrow Left",!0,"left"),Ui=Bi("arrow-right","Arrow Right",!0,"right"),Vi={id:"highlighter",name:"Highlighter",points:0,freehand:!0,defaultStyle:{lineWidth:12,fillOpacity:.28},settings:z([m,b,Ve,Ke]),draw:t=>{t.pts.length<2||(t.ctx.setLineDash([]),un(t,Math.max(2,(t.style.lineWidth||12)*t.rc.dpr),t.style.fillOpacity??.28))},distance:(t,e,n)=>{const i=o(t,e,n.pts);return i<=Math.max(6,(n.drawing.style.lineWidth??12)/2)?0:i}},qi=z([b,M,S,T]),Ji={id:"fib-channel",name:"Fib Channel",points:3,defaultStyle:{showLabels:!0,levels:Z(Q)},settings:z([b,M,S,_,T]),draw:t=>{const e=xn(t.style.levels,Q),[n,i,o]=t.pts,r=t.rc.dpr,s=o.x-i.x,l=o.y-i.y,a=!0===t.style.extendLeft,h=!0===t.style.extendRight;Ye(t);for(const o of e){const[e,d]=c({x:n.x+s*o.ratio,y:n.y+l*o.ratio},{x:i.x+s*o.ratio,y:i.y+l*o.ratio},t.rc.plotWidth*r,a,h),p=fn(o);t.ctx.strokeStyle=p,t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(d.x,d.y),t.ctx.stroke(),!1!==t.style.showLabels&&on(t,yn(o),i.x+s*o.ratio+4*r,i.y+l*o.ratio,p)}t.ctx.setLineDash([])},distance:(e,n,i)=>{const o=xn(i.drawing.style.levels,Q),[r,s,l]=i.pts,a=!0===i.drawing.style.extendLeft,h=!0===i.drawing.style.extendRight;let d=1/0;for(const p of o){const[o,u]=c({x:r.x+(l.x-s.x)*p.ratio,y:r.y+(l.y-s.y)*p.ratio},{x:s.x+(l.x-s.x)*p.ratio,y:s.y+(l.y-s.y)*p.ratio},i.rc.plotWidth,a,h),x=t(e,n,o,u);x<d&&(d=x)}return d}},Ki={id:"fib-time-zone",name:"Fib Time Zone",points:2,defaultStyle:{showLabels:!0,levels:Z(it)},settings:z([v,S,T]),draw:t=>{const[e,n]=t.pts,i=n.x-e.x;if(Math.abs(i)<.5)return;const o=t.rc.dpr,r=t.rc.plotWidth*o,s=xn(t.style.levels,it);Ye(t);for(const n of s){const s=e.x+i*n.ratio;if(s<-10||s>r+10)continue;const l=n.color??t.style.color;t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(Math.round(s)+.5,0),t.ctx.lineTo(Math.round(s)+.5,t.rc.plotHeight*o),t.ctx.stroke(),!1!==t.style.showLabels&&on(t,n.label??String(n.ratio),s+3*o,12*o,l)}t.ctx.setLineDash([])},distance:(t,e,n)=>{const[i,o]=n.pts,r=o.x-i.x;if(Math.abs(r)<.5)return null;let s=1/0;for(const e of xn(n.drawing.style.levels,it))s=Math.min(s,Math.abs(t-(i.x+r*e.ratio)));return Number.isFinite(s)?s:null}},Xi={id:"fib-fan",name:"Fib Fan",points:2,defaultStyle:{showLabels:!0,levels:Z(tt)},settings:qi,draw:t=>{const e=xn(t.style.levels,tt),[n,i]=t.pts,o=t.rc.dpr,r=t.rc.plotWidth*o;Ye(t);for(const s of e){const e=c(n,{x:i.x,y:n.y+(i.y-n.y)*s.ratio},r,!1,!0)[1],l=fn(s);t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(e.x,e.y),t.ctx.stroke(),!1!==t.style.showLabels&&on(t,yn(s),i.x+4*o,n.y+(i.y-n.y)*s.ratio,l)}t.ctx.setLineDash([])},distance:(e,n,i)=>{const o=xn(i.drawing.style.levels,tt),[r,s]=i.pts;let l=1/0;for(const a of o)l=Math.min(l,t(e,n,r,c(r,{x:s.x,y:r.y+(s.y-r.y)*a.ratio},i.rc.plotWidth,!1,!0)[1]));return Number.isFinite(l)?l:null}},Yi={id:"gann-fan",name:"Gann Fan",points:2,defaultStyle:{showLabels:!0,levels:Z(nt)},settings:qi,draw:t=>{const[e,n]=t.pts,i=t.rc.dpr,o=t.rc.plotWidth*i,r=n.x-e.x,s=n.y-e.y;if(Math.abs(r)<.5)return;const l=xn(t.style.levels,nt);Ye(t),l.forEach((l,a)=>{const h=c(e,{x:e.x+r,y:e.y+s*l.ratio},o,!1,!0)[1],d=l.color??K(a);t.ctx.strokeStyle=d,t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(h.x,h.y),t.ctx.stroke(),!1!==t.style.showLabels&&on(t,l.label??Y(l.ratio),n.x+4*i,e.y+s*l.ratio,d)}),t.ctx.setLineDash([])},distance:(e,n,i)=>{const[o,r]=i.pts,s=r.x-o.x,l=r.y-o.y;if(Math.abs(s)<.5)return null;let a=1/0;for(const r of xn(i.drawing.style.levels,nt))a=Math.min(a,t(e,n,o,c(o,{x:o.x+s,y:o.y+l*r.ratio},i.rc.plotWidth,!1,!0)[1]));return Number.isFinite(a)?a:null}},Gi={id:"gann-box",name:"Gann Box",points:2,defaultStyle:{fill:!0,fillOpacity:.05,showLabels:!0,levels:Z(et)},settings:z([v,w,S,T]),draw:t=>{const[e,n]=t.pts,i=r(e,n),o=t.rc.dpr,s=t.drawing.points,l=xn(t.style.levels,et);Ze(t,()=>t.ctx.fillRect(i.x0,i.y0,i.x1-i.x0,i.y1-i.y0)),Ye(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(n.x,n.y),t.ctx.stroke();for(const r of l){const l=Math.round(e.x+(n.x-e.x)*r.ratio)+.5,a=s[0].price+(s[1].price-s[0].price)*r.ratio,c=Math.round(t.rc.priceScale.priceToY(a)*o)+.5,h=fn(r);t.ctx.strokeStyle=h,t.ctx.beginPath(),t.ctx.moveTo(l,i.y0),t.ctx.lineTo(l,i.y1),t.ctx.moveTo(i.x0,c),t.ctx.lineTo(i.x1,c),t.ctx.stroke(),!1!==t.style.showLabels&&on(t,yn(r),i.x1+4*o,c,h)}t.ctx.setLineDash([])},distance:(t,e,n)=>l(t,e,n.pts[0],n.pts[1],!0===n.drawing.style.fill)},Zi=[mn,bn,Mn,vn,wn,_n,kn,Sn,Pn,Ln,Fn,Cn,An,Wn,Hn,Ni,$n,Ci,Ai,$i,Oi,Di,Wi,Hi,Yn,Zn,Ri,ji,Vi,Xn,Ji,Ki,Xi,Yi,Gi,Qn,ti,ei,Vn,Kn,oi,ri,hi,Mi,wi,ki,Ti,Li,Ii,Ei,Ui,...Kt,...me,...Ne];let Qi=!1;function to(){if(!Qi){Qi=!0;for(const t of Zi)Oe(t)}}const eo="0 0 24 24",no=2,io={viewBox:eo,fill:"none",stroke:"currentColor",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round"},oo={cursor:"M12 3v6M12 15v6M3 12h6M15 12h6",magnet:"M6 4v8a6 6 0 0 0 12 0V4M6 9h4M14 9h4","trend-line":"M4 20 20 4",ray:"M4 20 20 4M2 18 6 22","extended-line":"M2 22 22 2",arrow:"M4 20 18 6M18 6h-6M18 6v6","horizontal-line":"M2 12h20","horizontal-ray":"M6 12H22M4 10V14","vertical-line":"M12 2v20","cross-line":"M2 12h20M12 2v20","trend-angle":"M4 20 18 8M4 20h12M8 20a8 8 0 0 0 2-5","info-line":"M4 20 20 4M8 8h8",path:"M2 18 8 8l4 6 4-10",polyline:"M2 18 8 8l4 6 4-10 4 4","parallel-channel":"M2 16 14 4M8 22 20 10","disjoint-channel":"M2 16 14 6M8 22 22 14","flat-bottom":"M2 14 14 4M2 20h20","fib-channel":"M2 14 14 4M4 18 16 8M6 22 18 12","regression-trend":"M2 18 20 6M4 20 22 8M2 14 20 2","flat-top-bottom":"M3 6h18M3 20 21 12M3 6v14","regression-channel":"M3 18 19 6M5 21 21 9M3 12 17 2M10 16h4",pitchfork:"M4 21 16 9M4 9 16 21M10 15 22 3M4 9 10 3M16 21l6-6","schiff-pitchfork":"M4 19 18 5M4 11 12 19M8 15 20 3M4 11l6-6M12 19l8-8","modified-schiff-pitchfork":"M4 20 20 4M4 12 12 20M8 16 20 4M4 12l8-8M12 20l8-8","inside-pitchfork":"M4 20 20 4M6 10 14 18M6 10l6-6M14 18l6-6","fib-extension-two-point":"M3 4h18M3 10h18M3 16h18M3 21h18M6 4v12","fib-speed-resistance-fan":"M3 21V3M3 21h18M3 21 21 3M3 21 12 3M3 21 21 12","icon-stamp":"M12 3l3 6 6 3-6 3-3 6-3-6-6-3 6-3z",rectangle:"M3 5h18v14H3z","rotated-rectangle":"M2 14 10 4l12 6-8 10z",ellipse:"M12 5c5 0 9 3 9 7s-4 7-9 7-9-3-9-7 4-7 9-7z",circle:"M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18z",triangle:"M12 4 21 20H3z",arc:"M3 19a12 12 0 0 1 18 0",curve:"M3 18c4-12 14-12 18 0","double-curve":"M3 18c3-9 7-9 9 0 2 9 6 9 9 0",brush:"M3 19c4 0 4-8 8-8s4 8 9 4",highlighter:"M4 16 14 6l4 4-10 10H4z","fib-retracement":"M3 4h18M3 9h18M3 14h18M3 19h18","fib-extension":"M3 5h18M3 12h18M3 19h18M8 5v14","fib-fan":"M3 20 21 4M3 20 21 10M3 20 21 16M3 20h18","fib-time-zone":"M4 3v18M8 3v18M14 3v18M22 3v18","fib-circles":"M12 20A5 5 0 0 1 12 10M12 20A9 9 0 0 1 12 2M10 20h4","fib-spiral":"M12 13A3 3 0 1 1 15 10A7 7 0 1 1 8 3A9 9 0 1 1 21 12","fib-wedge":"M3 20 21 4M3 20 21 12M3 20a12 12 0 0 0 10-6","fib-speed-fan":"M3 20V4M3 20h18M3 20 21 4M3 20 13 4","gann-fan":"M3 20 21 2M3 20 21 9M3 20 21 15M3 20h18","gann-box":"M3 3h18v18H3zM3 3l18 18M3 21 21 3","trend-fib-time":"M3 20 8 8 12 15M12 3v18M16 3v18M22 3v18","fib-speed-resistance-arcs":"M3 21a6 6 0 0 0 6-6M3 21a12 12 0 0 0 12-12M3 21a18 18 0 0 0 18-18M3 21 21 3","gann-square":"M3 3h18v18H3zM9 3v18M15 3v18M3 9h18M3 15h18M3 21 21 3","dedekind-tessellation":"M3 21a9 9 0 0 1 18 0M3 21a4 4 0 0 1 8 0M13 21a4 4 0 0 1 8 0M12 3v18",sonic:"M3 12a9 9 0 0 1 18 0M6 12a6 6 0 0 1 12 0M9 12a3 3 0 0 1 6 0M3 12h18",supersonic:"M3 12 21 3M3 12l18 9M7 12a5 5 0 0 1 10 0M10 12a3 3 0 0 1 6 0","golden-sonic":"M3 12a9 9 0 0 1 18 0M7 12a5 5 0 0 1 10 0M10 12a2 2 0 0 1 4 0M3 16h18M12 16v5","golden-supersonic":"M3 12 21 3M3 12l18 9M6 12a6 6 0 0 1 12 0M10 12a2 2 0 0 1 4 0M18 11v2","xabcd-pattern":"M3 19 7 4 12 16 16 8 21 20M3 19 12 16 21 20M7 4 16 8","abcd-pattern":"M3 18 9 4 15 15 21 3M3 18 15 15M9 4 21 3","elliott-impulse":"M2 21 6 12 9 17 13 5 17 11 22 2","elliott-correction":"M3 4 9 18 15 8 21 21M3 4h4M17 21h4","head-shoulders":"M2 20 5 10 8 17 12 3 16 17 19 10 22 20M3 17h18",gartley:"M3 20 7 3 12 13 16 7 21 17M3 20 12 13 21 17",bat:"M3 21 7 3 12 11 16 6 21 19M7 3 16 6M3 21 21 19",butterfly:"M3 16 7 3 12 14 16 7 21 21M3 16 12 14 21 21",crab:"M3 12 7 3 12 10 16 5 21 22M3 12 12 10 21 22",shark:"M3 17 7 10 12 20 16 3 21 18M3 17 21 18M7 10 16 3",cypher:"M3 20 7 9 12 16 16 3 21 18M3 20 12 16M7 9 16 3",measure:"M4 16h16M4 12v8M20 12v8M8 4h8M12 4v6","price-range":"M12 4v16M8 8l4-4 4 4M8 16l4 4 4-4","date-range":"M4 12h16M8 8l-4 4 4 4M16 8l4 4-4 4","date-price-range":"M4 6h16v12H4zM4 12h16M12 6v12","cyclic-lines":"M4 4v16M10 4v16M16 4v16M22 4v16","time-cycles":"M4 12a4 4 0 0 1 8 0 4 4 0 0 0 8 0M4 4v16","sine-line":"M2 12c3-9 6-9 9 0 3 9 6 9 9 0","long-position":"M3 15h18v5H3zM3 5h18v5H3zM12 10v5","short-position":"M3 5h18v5H3zM3 15h18v5H3zM12 10v5",forecast:"M3 18 9 10l4 4 8-10M13 4h8v8","risk-reward-long":"M3 14h18v6H3zM3 4h18v6H3zM12 10v4M6 7h4","risk-reward-short":"M3 4h18v6H3zM3 14h18v6H3zM12 10v4M6 17h4",text:"M4 5h16M12 5v14M8 19h8",note:"M4 20 9 15M9 5h13v10H9zM4 20v-4",callout:"M3 4h18v11H3zM8 15l-2 5 6-5",balloon:"M3 4h18v12H9l-4 4v-4H3z",comment:"M3 5h18v10H3zM6 15v4l4-4",signpost:"M12 21V9M5 3h14v6H5z","price-label":"M3 12 8 7h13v10H8z","price-note":"M2 12h5M7 6h15v12H7zM10 12h9",table:"M3 5h18v14H3zM3 10h18M9 10v9M15 10v9","flag-mark":"M6 21V3M6 3h12l-3 4 3 4H6","arrow-up":"M12 3v18M6 9l6-6 6 6","arrow-down":"M12 3v18M6 15l6 6 6-6","arrow-left":"M3 12h18M9 6l-6 6 6 6","arrow-right":"M3 12h18M15 6l6 6-6 6"};function ro(t){return oo[t]}function so(){return Object.keys(oo)}const lo="0 0 16 16",ao=1.5,co={viewBox:lo,fill:"none",stroke:"currentColor",strokeWidth:1.5,strokeLinecap:"round",strokeLinejoin:"round"},ho={cursor:"M8 2v4M8 10v4M2 8h4M10 8h4",magnet:"M3 2v6a5 5 0 0 0 10 0V2M3 5h3M10 5h3",lock:"M3 7h10v7H3zM5 7V5a3 3 0 0 1 6 0v2",unlock:"M3 7h10v7H3zM5 7V5a3 3 0 0 1 6 0",eye:"M1 8c2-4 4-6 7-6s5 2 7 6c-2 4-4 6-7 6s-5-2-7-6zM10 8a2 2 0 1 1-4 0 2 2 0 0 1 4 0","eye-off":"M1 8c2-4 4-6 7-6s5 2 7 6c-2 4-4 6-7 6s-5-2-7-6zM2 2l12 12",star:"M8 1l2 5h5l-4 3 1 5-4-3-4 3 1-5-4-3h5z","star-filled":"M8 1 12 14 1 6h14L4 14z",trash:"M2 4h12M6 4V2h4v2M3 4l1 10h8l1-10",settings:"M2 4h12M2 8h12M2 12h12M5 2v4M11 6v4M7 10v4",undo:"M3 6h7a4 4 0 0 1 0 8H6M6 3 3 6l3 3",redo:"M13 6H6a4 4 0 0 0 0 8h4M10 3l3 3-3 3",copy:"M6 6h8v8H6zM10 6V2H2v8h4",paste:"M4 3H3v11h10V3h-1M6 2h4v2H6z",duplicate:"M6 6h8v8H6zM10 6V2H2v8h4M10 8v4M8 10h4",front:"M2 9h6v5H2zM11 14V2M8 5l3-3 3 3",back:"M2 2h6v5H2zM11 2v12M8 11l3 3 3-3",text:"M3 3h10M8 3v10M6 13h4","chevron-down":"M3 6l5 5 5-5","chevron-right":"M6 3l5 5-5 5",close:"M3 3l10 10M13 3 3 13",plus:"M8 2v12M2 8h12",minus:"M2 8h12",search:"M7 2a5 5 0 1 0 0 10 5 5 0 0 0 0-10zM11 11l3 3",grid:"M2 2h12v12H2zM2 8h12M8 2v12",link:"M6 10l4-4M7 4l1-1a3 3 0 0 1 4 4l-1 1M9 12l-1 1a3 3 0 0 1-4-4l1-1",unlink:"M7 4l1-1a3 3 0 0 1 4 4l-1 1M9 12l-1 1a3 3 0 0 1-4-4l1-1M4 4l8 8",camera:"M2 5h3l1-2h4l1 2h3v8H2zM8 11a2 2 0 1 0 0-4 2 2 0 0 0 0 4z",download:"M8 2v9M4 7l4 4 4-4M2 14h12"},po=new Set(["star-filled"]);function uo(t){return ho[t]}function xo(){return Object.keys(ho)}const fo="http://www.w3.org/2000/svg",yo="oac-icon-";function go(t){return String(t).replace(/[&<>"]/g,t=>"&"===t?"&amp;":"<"===t?"&lt;":">"===t?"&gt;":"&quot;")}function mo(t,e,n){const i=t[e];if(void 0===i)throw new Error(`openalgo-charts: no ${n} icon "${e}"`);return i}function bo(t){return Number(t.viewBox.split(" ")[2])}function Mo(t,e,n,i){const o=go(i.size??bo(e)),r=go(i.stroke??e.strokeWidth),s=void 0!==i.className&&""!==i.className?` class="${go(i.className)}"`:"";return`<svg xmlns="${fo}" viewBox="${go(e.viewBox)}" width="${o}" height="${o}" fill="${go(n)}" stroke="${go(e.stroke)}" stroke-width="${r}" stroke-linecap="${go(e.strokeLinecap)}" stroke-linejoin="${go(e.strokeLinejoin)}"${s} aria-hidden="true">${t}</svg>`}function vo(t,e={}){return Mo(`<path d="${go(mo(oo,t,"tool"))}"/>`,io,io.fill,e)}function wo(t,e={}){const n=mo(ho,t,"chrome"),i=po.has(t)?"currentColor":co.fill;return Mo(`<path d="${go(n)}"/>`,co,i,e)}function _o(t=Object.keys(oo)){let e="";for(const n of new Set(t)){const t=mo(oo,n,"tool");e+=`<symbol id="${go(yo+n)}" viewBox="${go(io.viewBox)}"><path d="${go(t)}"/></symbol>`}return`<svg xmlns="${fo}" style="display:none" aria-hidden="true">${e}</svg>`}function ko(t,e={}){return mo(oo,t,"tool"),Mo(`<use href="#${go(yo+t)}"/>`,io,io.fill,e)}function So(t,e={}){const n=mo(oo,t,"tool"),i=e.size??20;if(!(Number.isFinite(i)&&i>0&&i<=128))throw new RangeError(`openalgo-charts: cursor size ${i} is outside 1..128`);const[o,r]=e.hotspot??[i/2,i/2],s=e.color??"#fff",l=e.halo??function(t){const e=/^#([0-9a-f]{3,8})$/i.exec(t.trim());if(!e)return"#000";const n=e[1],i=n.length<6?n.split("").map(t=>t+t).join(""):n;return(.2126*parseInt(i.slice(0,2),16)+.7152*parseInt(i.slice(2,4),16)+.0722*parseInt(i.slice(4,6),16))/255>.5?"#000":"#fff"}(s),a=e.fallback??"crosshair",c=bo(io)/i,h=Math.round(100*(2+2*c))/100,d=`<svg xmlns="${fo}" viewBox="${io.viewBox}" width="${i}" height="${i}" fill="none" stroke-linecap="${io.strokeLinecap}" stroke-linejoin="${io.strokeLinejoin}"><path d="${n}" stroke="${go(l)}" stroke-width="${h}"/><path d="${n}" stroke="${go(s)}" stroke-width="2"/></svg>`;return`url("data:image/svg+xml,${encodeURIComponent(d)}") ${Math.round(o)} ${Math.round(r)}, ${a}`}const To=t=>Number.isFinite(t.zIndex)?t.zIndex:0;function Po(t){return t.slice().sort((t,e)=>To(t)-To(e))}function Lo(t,e){return We(t)&&!0===De(t).freehand&&e>2?[0,e-1]:Array.from({length:e},(t,e)=>e)}class Fo{constructor(t="top"){Object.defineProperty(this,"_order",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_drawings",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_selected",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_hovered",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_snap",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_preview",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_below",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_above",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_touch",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._order=t}attached(t){this._host=t}detached(){this._host=null}zOrder(){return this._order}autoscaleInfo(){return null}setDrawings(t){this._drawings=Po(t),this._host?.requestUpdate()}setSelected(t){t.length===this._selected.length&&t.every((t,e)=>t===this._selected[e])||(this._selected=t.slice(),this._host?.requestUpdate())}setHovered(t){t!==this._hovered&&(this._hovered=t,this._requestOverlay())}hovered(){return this._hovered}setSnapPoint(t){(null===t?null!==this._snap:null===this._snap||this._snap.time!==t.time||this._snap.price!==t.price)&&(this._snap=null===t?null:{time:t.time,price:t.price},this._requestOverlay())}snapPoint(){return this._snap}setPointerType(t){this._touch="touch"===t}setPreview(t){this._preview=t,this._host?.requestUpdate()}setBelow(t){this._below!==t&&(null!==this._below&&(this._below._above=null),this._below=t,null!==t&&(null!==t._above&&t._above!==this&&(t._above._below=null),t._above=this,t._host?.requestUpdate()),this._host?.requestUpdate())}_requestOverlay(){null===this._above&&this._host?.requestUpdate()}_isTouch(t){const e=t.pointerType;return"touch"===e||"mouse"!==e&&"pen"!==e&&this._touch}_grabRadius(t){return this._isTouch(t)?12:6}_handleRadius(t){return this._isTouch(t)?10:5}_project(t,e,n){return{x:t.timeScale.indexToX(t.dataLayer.timeToIndexFloat(e)),y:t.priceScale.priceToY(n)}}_points(t,e){return e.points.map(e=>this._project(t,e.time,e.price))}_handled(){if(0===this._selected.length)return[];const t=new Set(this._selected);return this._drawings.filter(e=>t.has(e.id)&&!0!==e.locked&&!1!==e.visible)}_hoverHandled(){const t=this._hovered;if(null===t||this._selected.includes(t))return null;const e=this._drawings.find(e=>e.id===t);return void 0===e||!0===e.locked||!1===e.visible?null:e}draw(t,e){t.save(),t.beginPath(),t.rect(0,0,Math.round(e.plotWidth*e.dpr),Math.round(e.plotHeight*e.dpr)),t.clip();try{!function(t,e){const n=at.get(t);at.set(t,new Map);try{return e()}finally{n?at.set(t,n):at.delete(t)}}(t,()=>this._drawContent(t,this._drawingContext(e)))}finally{t.restore()}}_drawingContext(t){return t.readoutPriceScale?{...t,priceScale:t.readoutPriceScale}:t}_drawContent(t,e){const n=new Set(this._selected),i=null===this._preview?this._drawings:[...this._drawings,this._preview];for(const o of i){if(!1===o.visible||!We(o.tool))continue;const i=De(o.tool),r=this._points(e,o);if(0===r.length||!r.every(t=>Number.isFinite(t.x)&&Number.isFinite(t.y)))continue;if(o.points.length<Math.max(1,i.points)){o===this._preview&&this._drawPlacementGuide(t,e,r,o);continue}const s={color:o.style.color??e.theme.lineColor,lineWidth:o.style.lineWidth??1.5,...o.style},l=e.dpr;t.save(),o===this._preview&&(t.globalAlpha=.7),i.draw({ctx:t,rc:e,drawing:o,selected:n.has(o.id),pts:r.map(t=>({x:t.x*l,y:t.y*l})),style:{...s,color:s.color,lineWidth:s.lineWidth},formatPrice:t=>e.priceScale.format(t)}),t.restore()}if(null!==this._above)return;const o=[this._hoverHandled(),this._below?._hoverHandled()??null];for(const n of o)null!==n&&this._drawHandles(t,e,this._points(e,n),n.tool,!0);for(const n of this._handled())this._drawHandles(t,e,this._points(e,n),n.tool,!1);if(null!==this._below)for(const n of this._below._handled())this._drawHandles(t,e,this._points(e,n),n.tool,!1);null!==this._snap&&this._drawSnapRing(t,e,this._project(e,this._snap.time,this._snap.price))}_drawPlacementGuide(t,e,n,i){const o=e.dpr;t.save(),t.globalAlpha*=.7,t.strokeStyle=i.style.color??e.theme.lineColor,t.lineWidth=Math.max(1,(i.style.lineWidth??1.5)*o),t.setLineDash([3*o,3*o]),t.beginPath(),t.moveTo(n[0].x*o,n[0].y*o);for(let e=1;e<n.length;e++)t.lineTo(n[e].x*o,n[e].y*o);t.stroke(),t.setLineDash([]);for(const i of n)t.beginPath(),t.arc(i.x*o,i.y*o,3*o,0,2*Math.PI),t.fillStyle=e.theme.background,t.fill(),t.stroke();t.restore()}_drawHandles(t,e,n,i,o){const r=e.dpr;t.save(),t.setLineDash([]),t.lineWidth=Math.max(1,Math.round((o?1:1.5)*r)),o&&(t.globalAlpha=.6);for(const o of Lo(i,n.length)){const i=n[o];t.beginPath(),t.arc(i.x*r,i.y*r,5*r,0,2*Math.PI),t.fillStyle=e.theme.background,t.fill(),t.strokeStyle=e.theme.lineColor,t.stroke()}t.restore()}_drawSnapRing(t,e,n){if(!Number.isFinite(n.x)||!Number.isFinite(n.y))return;const i=e.dpr;t.save(),t.setLineDash([]),t.globalAlpha=.9,t.lineWidth=Math.max(1,Math.round(1.5*i)),t.strokeStyle=e.theme.axisText,t.beginPath(),t.arc(n.x*i,n.y*i,5.5*i,0,2*Math.PI),t.stroke(),t.restore()}hitTest(t,e,n){if(n=this._drawingContext(n),null!==this._above)return null;const i=this._hitHandle(t,e,n)??this._below?._hitHandle(t,e,n)??null;return null!==i?i:this._hitBody(t,e,n)??this._below?._hitBody(t,e,n)??null}_hitHandle(t,e,n){const i=this._handleRadius(n)+2;for(const o of this._handled()){const r=this._points(n,o);for(const n of Lo(o.tool,r.length))if(Math.hypot(t-r[n].x,e-r[n].y)<=i)return{externalId:`draw:${o.id}#${n}`,zOrder:"top",distance:0,cursor:"grabbing",draggable:!0}}return null}_hitBody(t,e,n){const i=this._grabRadius(n);let o=null;for(let r=this._drawings.length-1;r>=0;r--){const s=this._drawings[r];if(!1===s.visible||!0===s.locked||!We(s.tool))continue;const l=De(s.tool);if(s.points.length<Math.max(1,l.points))continue;const a=l.distance(t,e,{pts:this._points(n,s),drawing:s,rc:n});if(null!==a&&Number.isFinite(a)&&!(a>i)&&((null===o||a<o.distance)&&(o={id:s.id,distance:a}),0===a))break}return null===o?null:{externalId:`draw:${o.id}`,zOrder:"top",distance:o.distance,cursor:"move",draggable:!0}}}const Io=2,zo=t=>"object"==typeof t&&null!==t&&!Array.isArray(t),Co=t=>"number"==typeof t&&Number.isFinite(t),Ao=(t,e)=>"string"==typeof t&&e.includes(t),No=t=>"__proto__"===t||"constructor"===t||"prototype"===t,$o=["solid","dashed","dotted"],Oo=["left","center","right"],Do=["top","middle","bottom"],Wo=["inside","outside"],Ho=["color","fillColor"],Bo=["lineWidth","fillOpacity","accountSize","risk"],Ro=["fill","extendLeft","extendRight","showLabels","showStats","pressure"],jo=["color","fontFamily","backgroundColor","borderColor"],Eo=["fontSize","wrapWidth","backgroundOpacity"],Uo=["bold","italic","wrap","background","border"];let Vo=1;function qo(t){const e=Array.isArray(t)?t:zo(t)&&Array.isArray(t.drawings)?t.drawings:null,n=[];if(null===e)return{version:2,drawings:n};const i=new Set;for(const t of e){const e=Jo(t);if(null!==e){for(;""===e.id||i.has(e.id);)e.id="m"+Vo++;i.add(e.id),n.push(e)}}return{version:2,drawings:n}}function Jo(t){if(!zo(t))return null;const e=t.tool;if("string"!=typeof e||""===e)return null;const n=function(t){if(!Array.isArray(t)||0===t.length)return null;const e=[];for(const n of t){if(!zo(n)||!Co(n.time)||!Co(n.price))return null;const t={time:n.time,price:n.price};Co(n.pressure)&&n.pressure>=0&&n.pressure<=1&&(t.pressure=n.pressure),e.push(t)}return e}(t.points);if(null===n)return null;const i=void 0===t.paneIndex?0:t.paneIndex;if(!Co(i)||i<0||!Number.isInteger(i))return null;const o=zo(t.style)?t.style:{},r={id:"string"==typeof t.id?t.id:Co(t.id)?String(t.id):"",tool:e,points:n,style:Ko(o,e),paneIndex:i,zIndex:Co(t.zIndex)?t.zIndex:0},s=(l=t.text,(zo(l)&&"string"==typeof l.value?Xo(l.value,l):null)??function(t){return"string"!=typeof t.text?null:Xo(t.text,{color:t.fontColor,fontSize:t.fontSize,fontFamily:t.fontFamily,bold:"bold"===t.fontWeight||void 0,italic:"italic"===t.fontStyle||void 0,align:t.textAlign,valign:t.textVAlign,position:t.textPosition,wrap:t.wrap,wrapWidth:t.wrapWidth,background:t.background,backgroundColor:t.backgroundColor,backgroundOpacity:t.backgroundOpacity,border:t.border,borderColor:t.borderColor})}(o));var l;return null!==s&&(r.text=s),zo(t.props)&&(r.props=Yo(t.props,0)),"boolean"==typeof t.locked&&(r.locked=t.locked),"boolean"==typeof t.visible&&(r.visible=t.visible),Co(t.createdAt)&&(r.createdAt=t.createdAt),r}function Ko(t,e){const n={};for(const e of Ho){const i=t[e];"string"==typeof i&&(n[e]=i)}for(const e of Bo){const i=t[e];Co(i)&&(n[e]=i)}for(const e of Ro){const i=t[e];"boolean"==typeof i&&(n[e]=i)}Ao(t.lineStyle,$o)&&(n.lineStyle=t.lineStyle);const i=function(t,e){if(!Array.isArray(t))return null;const n=[];return t.forEach((t,i)=>{if(Co(t))n.push(function(t,e,n){return"fib-time-zone"===t?{ratio:e}:"gann-fan"===t?{ratio:e,color:K(n)}:{ratio:e,color:q(e)}}(e,t,i));else if(zo(t)&&Co(t.ratio)){const e={ratio:t.ratio};"string"==typeof t.color&&(e.color=t.color),"boolean"==typeof t.enabled&&(e.enabled=t.enabled),"string"==typeof t.label&&(e.label=t.label),n.push(e)}}),0===n.length&&t.length>0?null:n}(t.levels,e);return null!==i&&(n.levels=i),n}function Xo(t,e){const n={value:t};for(const t of jo){const i=e[t];"string"==typeof i&&(n[t]=i)}for(const t of Eo){const i=e[t];Co(i)&&(n[t]=i)}for(const t of Uo){const i=e[t];"boolean"==typeof i&&(n[t]=i)}return Ao(e.align,Oo)&&(n.align=e.align),Ao(e.valign,Do)&&(n.valign=e.valign),Ao(e.position,Wo)&&(n.position=e.position),n}function Yo(t,e){const n={};for(const[i,o]of Object.entries(t)){if(No(i))continue;const t=Go(o,e);void 0!==t&&(n[i]=t)}return n}function Go(t,e){return null===t||"boolean"==typeof t||"string"==typeof t?t:"number"==typeof t?Number.isFinite(t)?t:void 0:e>=8?void 0:Array.isArray(t)?t.map(t=>Go(t,e+1)??null):zo(t)?Yo(t,e+1):void 0}const Zo="openalgo-charts/drawings",Qo=2,tr=4096;let er=null;function nr(){er=null}function ir(){const t=globalThis.navigator,e=t?.clipboard;return void 0===e||"function"!=typeof e.writeText||"function"!=typeof e.readText?null:e}function or(t){const e={...t,points:t.points.map(rr),style:sr(t.style)};return void 0!==t.text&&(e.text={...t.text}),void 0!==t.props&&(e.props=JSON.parse(JSON.stringify(t.props))),e}function rr(t){return void 0===t.pressure?{time:t.time,price:t.price}:{time:t.time,price:t.price,pressure:t.pressure}}function sr(t){const e={...t};return void 0!==t.levels&&(e.levels=t.levels.map(t=>({...t}))),e}function lr(t){return JSON.stringify({[Zo]:{version:2,drawings:t.map(t=>({tool:t.tool,points:t.points.map(rr),style:t.style??{},...void 0===t.text?{}:{text:t.text},...void 0===t.props?{}:{props:t.props},paneIndex:t.paneIndex,zIndex:Number.isFinite(t.zIndex)?t.zIndex:0,...void 0===t.locked?{}:{locked:t.locked},...void 0===t.visible?{}:{visible:t.visible}}))}})}const ar=t=>"object"==typeof t&&null!==t&&!Array.isArray(t),cr=t=>"number"==typeof t&&Number.isFinite(t),hr=t=>"string"==typeof t&&t.length<=tr,dr=t=>"__proto__"===t||"constructor"===t||"prototype"===t,pr=(t,e)=>"string"==typeof t&&e.includes(t),ur=["text","fontSize","fontFamily","fontWeight","fontStyle","background","backgroundColor","backgroundOpacity","border","borderColor","wrap","wrapWidth","textAlign","textVAlign","textPosition","fontColor"];function xr(t){if(!Array.isArray(t)||t.length>64)return null;const e=[];for(const n of t)if(cr(n))e.push({ratio:n});else{if(!ar(n)||!cr(n.ratio))return null;{const t={ratio:n.ratio};hr(n.color)&&(t.color=n.color),"boolean"==typeof n.enabled&&(t.enabled=n.enabled),hr(n.label)&&(t.label=n.label),e.push(t)}}return e}function fr(t){if(!ar(t)||!hr(t.value))return null;const e={value:t.value};for(const n of["color","fontFamily","backgroundColor","borderColor"]){const i=t[n];hr(i)&&(e[n]=i)}for(const n of["fontSize","wrapWidth","backgroundOpacity"]){const i=t[n];cr(i)&&(e[n]=i)}for(const n of["bold","italic","wrap","background","border"]){const i=t[n];"boolean"==typeof i&&(e[n]=i)}return pr(t.align,["left","center","right"])&&(e.align=t.align),pr(t.valign,["top","middle","bottom"])&&(e.valign=t.valign),pr(t.position,["inside","outside"])&&(e.position=t.position),e}function yr(t,e=0){if(!ar(t))return null;const n={};let i=0;for(const[o,r]of Object.entries(t)){if(dr(o))continue;if(++i>64)return null;const t=gr(r,e);void 0!==t&&(n[o]=t)}return n}function gr(t,e){if("boolean"==typeof t||cr(t))return t;if("string"==typeof t)return t.length<=tr?t:void 0;if(Array.isArray(t)){if(t.length>64||e+1>=3)return;const n=[];for(const i of t){const t=gr(i,e+1);if(void 0===t)return;n.push(t)}return n}if(ar(t)){if(e+1>=3)return;return yr(t,e+1)??void 0}}function mr(t){if(!ar(t))return null;if("string"!=typeof t.tool||""===t.tool||t.tool.length>tr)return null;if(!We(t.tool))return null;const e=function(t){if(!Array.isArray(t)||0===t.length||t.length>2e4)return null;const e=[];for(const n of t){if(!ar(n)||!cr(n.time)||!cr(n.price))return null;const t={time:n.time,price:n.price};cr(n.pressure)&&n.pressure>=0&&n.pressure<=1&&(t.pressure=n.pressure),e.push(t)}return e}(t.points);if(null===e)return null;const n=function(t){if(void 0===t)return{};if(!ar(t))return null;const e={};let n=0;for(const[i,o]of Object.entries(t))if(!dr(i)){if(++n>64)return null;if(!ur.includes(i))switch(i){case"color":case"fillColor":hr(o)&&(e[i]=o);break;case"lineWidth":case"fillOpacity":case"accountSize":case"risk":cr(o)&&(e[i]=o);break;case"lineStyle":pr(o,["solid","dashed","dotted"])&&(e[i]=o);break;case"fill":case"extendLeft":case"extendRight":case"showLabels":case"showStats":case"pressure":"boolean"==typeof o&&(e[i]=o);break;case"levels":{const t=xr(o);null!==t&&(e[i]=t);break}default:if("string"==typeof o){if(o.length>tr)return null;e[i]=o}else if("boolean"==typeof o||cr(o))e[i]=o;else if(Array.isArray(o)){if(o.length>64)return null;o.every(cr)&&(e[i]=o.slice())}}}return e}(t.style);if(null===n)return null;const i=void 0===t.paneIndex?0:t.paneIndex;if(!cr(i)||i<0||!Number.isInteger(i))return null;if(void 0!==t.locked&&"boolean"!=typeof t.locked)return null;if(void 0!==t.visible&&"boolean"!=typeof t.visible)return null;const o=fr(t.text)??(ar(t.style)?function(t){return"string"!=typeof t.text?null:fr({value:t.text,color:t.fontColor,fontSize:t.fontSize,fontFamily:t.fontFamily,bold:"bold"===t.fontWeight||void 0,italic:"italic"===t.fontStyle||void 0,align:t.textAlign,valign:t.textVAlign,wrap:t.wrap,wrapWidth:t.wrapWidth,background:t.background,backgroundColor:t.backgroundColor,backgroundOpacity:t.backgroundOpacity,border:t.border,borderColor:t.borderColor,position:t.textPosition})}(t.style):null),r=void 0===t.props?null:yr(t.props),s={tool:t.tool,points:e,style:n,paneIndex:i,zIndex:cr(t.zIndex)?t.zIndex:0};return null!==o&&(s.text=o),null!==r&&(s.props=r),void 0!==t.locked&&(s.locked=t.locked),void 0!==t.visible&&(s.visible=t.visible),cr(t.createdAt)&&(s.createdAt=t.createdAt),s}function br(t){if("string"!=typeof t||0===t.length)return null;let e;try{e=JSON.parse(t)}catch{return null}if(!ar(e))return null;const n=e[Zo];if(!ar(n))return null;if(!cr(n.version)||n.version<1||n.version>2)return null;const i=n.drawings;if(!Array.isArray(i)||0===i.length||i.length>512)return null;for(const t of i)if(null===mr(t))return null;const o=qo(i).drawings;if(o.length!==i.length)return null;const r=[];for(const t of o){const e=mr(t);if(null===e)return null;r.push(e)}return r}class Mr{constructor(t={}){Object.defineProperty(this,"_port",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_fallback",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_lastError",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._port=void 0===t.port?ir():t.port,this._fallback=t.fallbackToMemory??!0}setPort(t){this._port=t}setFallbackToMemory(t){this._fallback=t}lastError(){return this._lastError}async write(t){if(0===t.length)return!1;let e;try{e=lr(t)}catch(t){return this._lastError=String(t),!1}this._lastError=null;let n=!1;if(null!==this._port)try{await this._port.writeText(e),n=!0}catch(t){this._lastError=String(t)}else this._lastError="openalgo-charts: no system clipboard available";return this._fallback?(er=e,!0):n}async read(){if(this._lastError=null,null!==this._port)try{const t=br(await this._port.readText());if(null!==t)return t}catch(t){this._lastError=String(t)}return this._fallback?br(er):null}}const vr=Math.PI/4;let wr=1;const _r=(t,e)=>t.length===e.length&&t.every((t,n)=>t===e[n]);function kr(t){return!0===t?"strong":"weak"===t||"strong"===t?t:"off"}const Sr=t=>!0===t.modifiers?.shift||!0===t.shiftKey;class Tr{constructor(t,e={}){Object.defineProperty(this,"_chart",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_clipboard",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_layers",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_drawings",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_tool",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_pending",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_pendingPane",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_selection",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_hovered",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lifted",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_shift",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_pointerKind",{enumerable:!0,configurable:!0,writable:!0,value:"mouse"}),Object.defineProperty(this,"_undo",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_redo",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_dragStart",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_off",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_lastCursor",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lastBar",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._chart=t,this._opts={magnet:kr(e.magnet),stayInDrawingMode:e.stayInDrawingMode??!1,historyLimit:e.historyLimit??50,pasteOffsetBars:e.pasteOffsetBars??2,pasteOffsetPixels:e.pasteOffsetPixels??16,defaultStyle:e.defaultStyle??{}},this._clipboard=new Mr({...void 0===e.clipboard?{}:{port:e.clipboard},...void 0===e.clipboardFallbackToMemory?{}:{fallbackToMemory:e.clipboardFallbackToMemory}}),this._off.push(t.on("click",t=>this._onClick(t))),this._off.push(t.on("crosshair:move",t=>this._onCrosshair(t))),this._off.push(t.on("hover",t=>this._onHover(t))),this._off.push(t.on("drag",t=>this._onDrag(t))),this._off.push(t.on("drag:end",()=>this._onDragEnd())),this._off.push(t.on("dblclick",()=>{this.finish()}));const n=t.drawingState();null!=n&&(this._drawings=qo(n).drawings),this._sync()}setTool(t){if(null!==t&&!We(t))throw new Error(`openalgo-charts: unknown drawing tool "${t}"`);this._tool=t,this._pending=[],this._setPlacementMode(null!==t),this._syncPreview(),this._syncSnapRing(),this._chart.emit("draw:tool",{tool:t})}_setPlacementMode(t){const e=this._chart;e.setPlacementMode?.(t)}activeTool(){return this._tool}setOptions(t){const{clipboard:e,magnet:n,...i}=t;this._opts={...this._opts,...i,defaultStyle:t.defaultStyle??this._opts.defaultStyle,magnet:void 0===n?this._opts.magnet:kr(n)},void 0!==e&&this._clipboard.setPort(e),this._syncSnapRing()}magnetMode(){return this._opts.magnet}hovered(){return this._hovered}clipboard(){return this._clipboard}drawings(){return this._drawings}get(t){return this._drawings.find(e=>e.id===t)}add(t){this._pushUndo();const e=this._insert(t);return this._sync(),this._chart.emit("draw:add",{drawing:e}),this._emitChange([e.id],"add"),e}_insert(t){const e=De(t.tool);let n=t.id??this._mintId();for(;void 0===t.id&&void 0!==this.get(n);)n=this._mintId();const{id:i,...o}=t,r={...o,id:n,style:{...this._opts.defaultStyle,...e.defaultStyle,...t.style},zIndex:Number.isFinite(t.zIndex)?t.zIndex:0,createdAt:t.createdAt??Date.now()};return void 0===t.text&&void 0===e.defaultText||(r.text={value:"",...e.defaultText,...t.text}),this._drawings.push(r),r}_mintId(){return"d"+wr++}update(t,e){const n=this.get(t);return void 0!==n&&(this._pushUndo(),this._applyPatch(n,e),this._sync(),this._chart.emit("draw:update",{drawing:n}),this._emitChange([t],"update"),!0)}updateMany(t){const e=t.map(t=>({d:this.get(t.id),patch:t.patch})).filter(t=>void 0!==t.d);if(0!==e.length){this._pushUndo();for(const{d:t,patch:n}of e)this._applyPatch(t,n);this._sync();for(const{d:t}of e)this._chart.emit("draw:update",{drawing:t});this._emitChange(e.map(t=>t.d.id),"update")}}_applyPatch(t,e){if(void 0!==e.points){const n=e.points.map(t=>({...t})),i=We(t.tool)?De(t.tool):void 0;t.points=void 0===i?.constrain?n:i.constrain(n,function(t,e){if(t.length!==e.length)return null;let n=null;for(let i=0;i<e.length;i++)if(t[i].time!==e[i].time||t[i].price!==e[i].price){if(null!==n)return null;n=i}return n}(t.points,n))}void 0!==e.style&&(t.style={...t.style,...e.style}),void 0!==e.text&&(t.text={...t.text,...e.text}),void 0!==e.props&&(t.props={...t.props,...e.props}),void 0!==e.locked&&(t.locked=e.locked),void 0!==e.visible&&(t.visible=e.visible),void 0!==e.zIndex&&Number.isFinite(e.zIndex)&&(t.zIndex=e.zIndex)}remove(t){return this._removeIds([t],!0).length>0}removeMany(t){this._removeIds(t,!0)}_removeIds(t,e){const n=new Set(t),i=this._drawings.filter(t=>n.has(t.id));if(0===i.length)return[];e&&this._pushUndo(),this._drawings=this._drawings.filter(t=>!n.has(t.id)),this._selection=this._selection.filter(t=>!n.has(t)),this._sync();for(const t of i)this._chart.emit("draw:remove",{drawing:t});return this._emitChange(i.map(t=>t.id),"remove"),i}clear(){0!==this._drawings.length&&(this._removeIds(this._drawings.map(t=>t.id),!0),this._setSelection([]))}select(t,e=!1){const n=null===t?[]:"string"==typeof t?[t]:t,i=[];for(const t of n)void 0===this.get(t)||i.includes(t)||i.push(t);if(!e)return void this._setSelection(i);const o=this._selection.slice();for(const t of i){const e=o.indexOf(t);e>=0?o.splice(e,1):o.push(t)}this._setSelection(o)}selected(){return 0===this._selection.length?null:this._selection[0]}selection(){return this._selection}_setSelection(t){const e=!_r(t,this._selection);this._selection=t;for(const e of this._layers.values())e.bottom.setSelected(t),e.top.setSelected(t);e&&(this._chart.emit("draw:select",{id:this.selected()}),this._chart.emit("drawing:select",{ids:t.slice()}))}_emitChange(t,e){this._chart.emit("drawing:change",{ids:t.slice(),kind:e})}setZIndex(t,e){const n=this.get(t);void 0!==n&&Number.isFinite(e)&&n.zIndex!==e&&(this._pushUndo(),n.zIndex=e,this._sync(),this._chart.emit("draw:update",{drawing:n}),this._emitChange([t],"reorder"))}bringToFront(t){const e=this.get(t);if(void 0===e)return;const n=this._band(e),i=0===n.length?e.zIndex:Math.max(...n.map(t=>t.zIndex));this._reorder(e,i,"end")}sendToBack(t){const e=this.get(t);if(void 0===e)return;const n=this._band(e),i=0===n.length?e.zIndex:Math.min(...n.map(t=>t.zIndex));this._reorder(e,i,"start")}sendBehindSeries(t){const e=this.get(t);void 0!==e&&e.zIndex>=0&&this.setZIndex(t,-1)}bringAboveSeries(t){const e=this.get(t);void 0!==e&&e.zIndex<0&&this.setZIndex(t,0)}_band(t){const e=t.zIndex<0;return this._drawings.filter(n=>n!==t&&n.paneIndex===t.paneIndex&&n.zIndex<0===e)}_reorder(t,e,n){const i=this._drawings.indexOf(t);i===("end"===n?this._drawings.length-1:0)&&t.zIndex===e||(this._pushUndo(),t.zIndex=e,this._drawings.splice(i,1),"end"===n?this._drawings.push(t):this._drawings.unshift(t),this._sync(),this._chart.emit("draw:update",{drawing:t}),this._emitChange([t.id],"reorder"))}nudge(t,e,n){if(0===e&&0===n)return;const i=this._targets(t).filter(t=>!0!==t.locked);if(0!==i.length){this._pushUndo();for(const t of i)t.points=t.points.map(i=>({time:this._offsetTime(i.time,e),price:this._offsetPrice(i.price,t.paneIndex,n)}));this._sync();for(const t of i)this._chart.emit("draw:update",{drawing:t});this._emitChange(i.map(t=>t.id),"update")}}duplicate(t){const e=this._targets(t);if(0===e.length)return[];this._pushUndo();const n=e.map(t=>{const{id:e,createdAt:n,...i}=or(t);return this._insert({...i,points:this._offsetPoints(t.points,t.paneIndex)})});this._sync();for(const t of n)this._chart.emit("draw:add",{drawing:t});return this._emitChange(n.map(t=>t.id),"add"),this.select(n.map(t=>t.id)),n}async copy(t){const e=this._targets(t);if(0===e.length)return!1;const n=await this._clipboard.write(e);return n&&this._chart.emit("draw:copy",{drawings:e.map(or)}),n}async cut(t){const e=this._targets(t);if(0===e.length)return!1;if(!await this._clipboard.write(e))return!1;const n=this._removeIds(e.map(t=>t.id),!0);return 0!==n.length&&(this._chart.emit("draw:cut",{drawings:n}),!0)}async paste(){const t=await this._clipboard.read();if(null===t||0===t.length)return[];for(const e of t)if(!We(e.tool))return[];const e=t.map(t=>{const e=this._clampPane(t.paneIndex);return{...t,paneIndex:e,points:this._offsetPoints(t.points,e)}});this._pushUndo();const n=e.map(t=>this._insert(t));this._sync();for(const t of n)this._chart.emit("draw:add",{drawing:t});return this._emitChange(n.map(t=>t.id),"add"),this._chart.emit("draw:paste",{drawings:n}),this.select(n.map(t=>t.id)),n}_targets(t){const e=null==t?this._selection:"string"==typeof t?[t]:t,n=[];for(const t of e){const e=this.get(t);void 0===e||n.includes(e)||n.push(e)}return n}_clampPane(t){const e=this._chart.panes;if(void 0===e)return t;const n=e.call(this._chart).length;return 0===n?0:Math.min(t,n-1)}_offsetPoints(t,e){const n=this._barSeconds()*this._opts.pasteOffsetBars,i=this._opts.pasteOffsetPixels;return t.map(t=>({time:t.time+n,price:this._offsetPrice(t.price,e,i)}))}_offsetPrice(t,e,n){if(0===n)return t;const i=this._chart.priceToCoordinate,o=this._chart.coordinateToPrice;if(void 0===i||void 0===o)return t;const r=i.call(this._chart,t,e);if(null===r||!Number.isFinite(r))return t;const s=o.call(this._chart,r+n,e);return null!==s&&Number.isFinite(s)?s:t}_offsetTime(t,e){if(0===e)return t;const n=this._chart.timeToCoordinate,i=this._chart.coordinateToTime;if(void 0!==n&&void 0!==i){const o=n.call(this._chart,t);if(Number.isFinite(o)){const t=i.call(this._chart,o+e);if(Number.isFinite(t))return t}}return t+e/8*this._barSeconds()}undo(){const t=this._undo.pop();return void 0!==t&&(this._redo.push(JSON.stringify(this._drawings)),this._drawings=JSON.parse(t),this._pruneSelection(),this._sync(),!0)}redo(){const t=this._redo.pop();return void 0!==t&&(this._undo.push(JSON.stringify(this._drawings)),this._drawings=JSON.parse(t),this._pruneSelection(),this._sync(),!0)}_pruneSelection(){const t=this._selection.filter(t=>void 0!==this.get(t));_r(t,this._selection)||this._setSelection(t)}canUndo(){return this._undo.length>0}canRedo(){return this._redo.length>0}toJSON(){return{version:2,drawings:this._drawings.map(or)}}fromJSON(t){this._drawings=qo(t).drawings,this._undo=[],this._redo=[],this._setSelection([]),this._sync()}destroy(){this._setPlacementMode(!1);for(const t of this._off)t();this._off.length=0;for(const t of this._layers.values())t.top.setBelow(null),this._chart.removePrimitive(t.top),this._chart.removePrimitive(t.bottom);this._layers.clear()}_onCrosshair(t){const e=t.time??null,n=e??(t.point?this._chart.coordinateToTime?.(t.point.x)??null:null),i=t.price??null,o=t.paneIndex??null;this._lastCursor=null!==n&&Number.isFinite(n)&&null!==i&&null!==o?{time:n,price:i,paneIndex:o}:null;const r=t.bar??null;if(this._lastBar=null===r||null===e?null:{time:e,...r},this._shift=Sr(t),this._notePointer(t),null===n&&null===i&&this._setHovered(null),null!==this._tool&&!0===t.pressed&&this._isFreehand()&&null!==n&&null!==i&&null!==o&&Number.isFinite(n)&&Number.isFinite(i))for(const e of this._coalesced(t,{time:n,price:i},o))this._inkPoint(e,o);else this._syncSnapRing(),null!==this._tool&&this._pending.length>0&&this._syncPreview()}_onHover(t){const e=t.id??null;this._setHovered(null!==e&&e.startsWith("draw:")?e.slice(5).split("#")[0]:null)}_setHovered(t){if(t!==this._hovered){this._hovered=t;for(const e of this._layers.values())e.bottom.setHovered(t),e.top.setHovered(t);this._chart.emit("drawing:hover",{id:t})}}_notePointer(t){const e=(t=>"touch"===t.pointerType||"pen"===t.pointerType?t.pointerType:"mouse")(t);if(e!==this._pointerKind){this._pointerKind=e;for(const t of this._layers.values())t.bottom.setPointerType(e),t.top.setPointerType(e)}}_coalesced(t,e,n){const i=t.samples,o={...e,...this._pressureOf(t.pressure)},r=this._chart.coordinateToTime,s=this._chart.coordinateToPrice;if(!Array.isArray(i)||i.length<2||void 0===r||void 0===s||null===t.point||void 0===t.point)return[o];const l=i[i.length-1],a=t.point.y-l.y;if(!Number.isFinite(a))return[o];const c=[];for(let t=0;t<i.length-1;t++){const e=i[t],o=r.call(this._chart,e.x),l=s.call(this._chart,e.y+a,n);null!==l&&Number.isFinite(o)&&Number.isFinite(l)&&c.push({time:o,price:l,...this._pressureOf(e.pressure)})}return c.push({...o,...this._pressureOf(l.pressure??t.pressure)}),c}_pressureOf(t){return"number"==typeof t&&Number.isFinite(t)&&.5!==t?{pressure:Math.min(1,Math.max(0,t))}:{}}_expand(t,e){if(void 0===t.expand)return e;const n=this._chart.getVisibleLogicalRange(),i=null===n?60:Math.max(1,n.to-n.from),o=this._pendingPane,r=void 0!==this._chart.timeToCoordinate&&void 0!==this._chart.priceToCoordinate&&void 0!==this._chart.coordinateToTime&&void 0!==this._chart.coordinateToPrice,s=t.expand(e,{barSeconds:this._barSeconds(),visibleBars:i,...r?{toPixel:t=>this._toPixel(t,o),fromPixel:t=>this._fromPixel(t,o)}:{}});return void 0===t.constrain?s:t.constrain(s,null)}_barSeconds(){const t=this._chart.dataLayer,e=t.baseIndex,n=e>0?t.indexToTime(e-1):void 0,i=e>=0?t.indexToTime(e):void 0;return void 0!==n&&void 0!==i&&i>n?i-n:60}_isFreehand(){return null!==this._tool&&!0===De(this._tool).freehand}_inkPoint(t,e){if(0===this._pending.length)this._pendingPane=e;else{if(e!==this._pendingPane)return;{const e=this._pending[this._pending.length-1];if(e.time===t.time&&e.price===t.price)return}}this._pending.push(t),this._syncPreview()}_commit(t){const e=De(this._tool),n=this.add({tool:e.id,points:t,style:{},paneIndex:this._pendingPane});this._pending=[],this._opts.stayInDrawingMode||(this._tool=null,this._setPlacementMode(!1)),this._syncPreview(),this._syncSnapRing(),this.select(n.id),this._chart.emit("draw:tool",{tool:this._tool})}_finishFreehand(){const t=this._pending;this._pending=[],t.length<2?this._syncPreview():this._commit(this._thinStroke(t,this._pendingPane))}_thinStroke(t,e){const n=[];for(const i of t){const o=this._toPixel(i,e);if(null===o)return t;n.push(o)}const i=rt(n,1.5),o=[];let r=0;for(const e of i){for(;r<n.length&&(n[r].x!==e.x||n[r].y!==e.y);)r++;if(r>=n.length)return t;o.push(t[r]),r++}return o}finish(){if(null===this._tool||0===this._pending.length)return!1;const t=De(this._tool);if(0!==t.points||!0===t.freehand)return!1;const e=this._pending;return e.length<2?(this._pending=[],this._syncPreview(),!1):(this._commit(e),!0)}cancel(){if(null===this._tool)return!1;const t=this._pending.length>0;return this._pending=[],t&&this._opts.stayInDrawingMode?(this._syncPreview(),!0):(this._tool=null,this._setPlacementMode(!1),this._syncPreview(),this._syncSnapRing(),this._chart.emit("draw:tool",{tool:null}),!0)}popAnchor(){if(null===this._tool||0===this._pending.length)return!1;const t=De(this._tool);return 0===t.points&&!0!==t.freehand&&(this._pending.pop(),this._syncPreview(),!0)}_onClick(t){if(this._notePointer(t),null!==this._tool){if(this._isFreehand())return void(!0===t.viaDrag&&this._finishFreehand());if(!0===t.viaDrag&&0===this._pending.length)return;if(null===t.price||!Number.isFinite(t.price)||!Number.isFinite(t.time))return;return this._shift=Sr(t),void this._placePoint(this._aimPoint({time:t.time,price:t.price},t.paneIndex),t.paneIndex)}const e=!0===t.shiftKey||!0===t.ctrlKey||!0===t.metaKey;null!==t.id&&t.id.startsWith("draw:")?this.select(t.id.slice(5).split("#")[0],e):null!==t.id||e||this.select(null)}_placePoint(t,e){0===this._pending.length&&(this._pendingPane=e),this._pending.push(t);const n=De(this._tool);n.points>0&&this._pending.length>=n.points?this._commit(this._expand(n,this._pending)):this._syncPreview()}_aimPoint(t,e){const n=this._lockedPoint(t,e);return null!==n?n:this._snapPoint(t,e)??t}_lockedPoint(t,e){return this._shift&&null!==this._tool&&1===this._pending.length?!0!==De(this._tool).angleLock||e!==this._pendingPane?null:this._lockAngle(this._pending[0],t,e):null}_snapPoint(t,e){const n=this._opts.magnet;if("off"===n||0!==e)return null;const i=this._lastBar;if(null===i)return null;const o=[i.open,i.high,i.low,i.close];if("strong"===n){let e=o[0],n=1/0;for(const i of o){const o=Math.abs(i-t.price);o<n&&(n=o,e=i)}return{time:i.time,price:e}}const r=this._chart.priceToCoordinate;if(void 0===r)return null;const s=r.call(this._chart,t.price,e);if(null===s||!Number.isFinite(s))return null;let l=null,a=8;for(const t of o){const n=r.call(this._chart,t,e);if(null===n||!Number.isFinite(n))continue;const i=Math.abs(n-s);i<=a&&(a=i,l=t)}return null===l?null:{time:i.time,price:l}}_onDrag(t){if(!t.id.startsWith("draw:"))return;const[e,n]=t.id.slice(5).split("#"),i=this.get(e);if(void 0===i||!0===i.locked)return;const o=void 0===n?null:Number(n);if(this._notePointer(t),this._shift=Sr(t),null===this._dragStart||this._dragStart.id!==e||this._dragStart.handle!==o){null!==o||this._selection.includes(e)||this.select(e);const n=null===o?this._targets(this._selection).filter(t=>!0!==t.locked):[i];this._pushUndo(),this._dragStart={id:e,handle:o,from:{time:t.fromTime??t.time,price:t.fromPrice??t.price},items:n.map(t=>({id:t.id,paneIndex:t.paneIndex,points:t.points.map(t=>({...t}))}))};let r=!1;for(const t of n)t.zIndex<0&&(this._lifted.add(t.id),r=!0);return this._moveDrag(t,i,o),void(r?this._sync():this._syncDrag())}this._moveDrag(t,i,o),this._syncDrag()}_moveDrag(t,e,n){const i=this._dragStart;if(null===n){const e=t.time-i.from.time,n=t.price-i.from.price,o=this._pixelDelta(i.from.price,t.price,t.paneIndex);for(const r of i.items){const i=this.get(r.id);if(void 0===i)continue;const s=r.paneIndex===t.paneIndex;i.points=r.points.map(t=>({...t,time:t.time+e,price:s?t.price+n:this._offsetPrice(t.price,r.paneIndex,o)}))}}else if(n>=0&&n<e.points.length){const o=i.items[0];let r={time:t.time,price:t.price};this._shift&&2===o.points.length&&We(e.tool)&&!0===De(e.tool).angleLock&&(r=this._lockAngle(o.points[1-n],r,e.paneIndex)??r);const s=o.points.map((t,e)=>e===n?{...t,...r}:{...t}),l=We(e.tool)?De(e.tool):void 0;e.points=void 0===l?.constrain?s:l.constrain(s,n)}}_pixelDelta(t,e,n){const i=this._chart.priceToCoordinate;if(void 0===i)return 0;const o=i.call(this._chart,t,n),r=i.call(this._chart,e,n);return null!==o&&null!==r&&Number.isFinite(o)&&Number.isFinite(r)?r-o:0}_toPixel(t,e){const n=this._chart.timeToCoordinate,i=this._chart.priceToCoordinate;if(void 0===n||void 0===i)return null;const o=n.call(this._chart,t.time),r=i.call(this._chart,t.price,e);return null!==r&&Number.isFinite(o)&&Number.isFinite(r)?{x:o,y:r}:null}_fromPixel(t,e){const n=this._chart.coordinateToTime,i=this._chart.coordinateToPrice;if(void 0===n||void 0===i)return null;const o=n.call(this._chart,t.x),r=i.call(this._chart,t.y,e);return null!==r&&Number.isFinite(o)&&Number.isFinite(r)?{time:o,price:r}:null}_lockAngle(t,e,n){const i=this._toPixel(t,n),o=this._toPixel(e,n);if(null===i||null===o)return null;const r=o.x-i.x,s=o.y-i.y;if(0===r&&0===s)return null;const l=Math.round(Math.atan2(s,r)/vr)*vr,a=Math.cos(l),c=Math.sin(l),h=r*a+s*c;return this._fromPixel({x:i.x+h*a,y:i.y+h*c},n)}_onDragEnd(){if(null===this._dragStart)return;const t=this._dragStart.items.map(t=>this.get(t.id)).filter(t=>void 0!==t);this._dragStart=null,this._lifted.size>0&&(this._lifted.clear(),this._sync());for(const e of t)this._chart.emit("draw:update",{drawing:e});t.length>0&&this._emitChange(t.map(t=>t.id),"update")}_layerFor(t){let e=this._layers.get(t);return void 0===e&&(e={bottom:new Fo("bottom"),top:new Fo("top")},this._chart.addPrimitive(e.bottom,t),this._chart.addPrimitive(e.top,t),e.top.setBelow(e.bottom),e.bottom.setSelected(this._selection),e.top.setSelected(this._selection),this._layers.set(t,e)),e}_onTop(t){return t.zIndex>=0||this._lifted.has(t.id)}_sync(){const t=new Map;for(const e of this._drawings){let n=t.get(e.paneIndex);void 0===n&&(n={below:[],above:[]},t.set(e.paneIndex,n)),(this._onTop(e)?n.above:n.below).push(e)}for(const[e,n]of t){const t=this._layerFor(e);t.bottom.setDrawings(n.below),t.top.setDrawings(n.above)}for(const[e,n]of this._layers)t.has(e)||(n.bottom.setDrawings([]),n.top.setDrawings([])),n.bottom.setSelected(this._selection),n.top.setSelected(this._selection);null!==this._hovered&&void 0===this.get(this._hovered)&&this._setHovered(null),this._chart.setDrawingState(this.toJSON())}_syncDrag(){const t=this._dragStart;if(null===t)return;const e=new Set(t.items.map(t=>t.paneIndex));for(const t of e){const e=this._layers.get(t);void 0!==e&&e.top.setDrawings(this._drawings.filter(e=>e.paneIndex===t&&this._onTop(e)))}this._chart.setDrawingState(this.toJSON())}_syncPreview(){for(const t of this._layers.values())t.top.setPreview(null);if(null===this._tool||0===this._pending.length)return;const t=this._lastCursor,e=null===t||t.paneIndex!==this._pendingPane?this._pending:[...this._pending,this._aimPoint({time:t.time,price:t.price},t.paneIndex)];this._layerFor(this._pendingPane).top.setPreview({id:"__preview",tool:this._tool,points:e,style:this._opts.defaultStyle,paneIndex:this._pendingPane,zIndex:0})}_syncSnapRing(){const t=this._lastCursor;let e=null,n=t?.paneIndex??this._pendingPane;null===t||null===this._tool||this._isFreehand()||null!==this._lockedPoint({time:t.time,price:t.price},t.paneIndex)||(e=this._snapPoint({time:t.time,price:t.price},t.paneIndex),n=t.paneIndex);for(const[t,i]of this._layers)i.top.setSnapPoint(t===n?e:null);null===e||this._layers.has(n)||this._layerFor(n).top.setSnapPoint(e)}_pushUndo(){this._undo.push(JSON.stringify(this._drawings)),this._undo.length>this._opts.historyLimit&&this._undo.shift(),this._redo=[]}}const Pr=1,Lr=10;function Fr(t,e){if(e.editingText)return null;const n=t.key??"";if(""===n)return null;const i=!0===t.ctrlKey||!0===t.metaKey,o=!0===t.shiftKey;if(!0===t.altKey)return null;if(!0===e.placing&&!i&&!o){if("Escape"===n)return{type:"cancel"};if("Enter"===n)return{type:"finish"};if("Backspace"===n)return{type:"popAnchor"}}const r=e.hasSelection||e.hasTarget;if(i)switch(n.toLowerCase()){case"z":return o?{type:"redo"}:{type:"undo"};case"y":return o?null:{type:"redo"};case"c":return!o&&r?{type:"copy"}:null;case"x":return!o&&r?{type:"cut"}:null;case"v":return o?null:{type:"paste"};case"d":return!o&&r?{type:"duplicate"}:null;default:return null}if("Delete"===n||"Backspace"===n)return!o&&r?{type:"delete"}:null;if(!e.hasSelection)return null;const s=o?10:1;switch(n){case"ArrowLeft":return{type:"nudge",dx:-s,dy:0};case"ArrowRight":return{type:"nudge",dx:s,dy:0};case"ArrowUp":return{type:"nudge",dx:0,dy:-s};case"ArrowDown":return{type:"nudge",dx:0,dy:s};default:return null}}const Ir="draw";to();export{me as ADVANCED_GEOMETRY_TOOLS,Kt as ADVANCED_LINE_TOOLS,x as ALIGN_OPTIONS,Wi as ARC,vn as ARROW,ji as ARROW_DOWN,Ei as ARROW_LEFT,Ui as ARROW_RIGHT,Ri as ARROW_UP,wi as BALLOON,Xn as BRUSH,Zi as BUILTIN_DRAWING_TOOLS,ri as CALLOUT,ho as CHROME_ICONS,co as CHROME_ICON_ATTRS,po as CHROME_ICON_FILLED,ao as CHROME_ICON_STROKE,lo as CHROME_ICON_VIEWBOX,$i as CIRCLE,m as COLOR_FIELD,ki as COMMENT,Sn as CROSS_LINE,Hi as CURVE,J as CYCLE_PALETTE,Qn as CYCLIC_LINES,Ai as DATE_RANGE,Q as DEFAULT_FIB,tt as DEFAULT_FIB_FAN,it as DEFAULT_FIB_TIME_ZONE,et as DEFAULT_GANN_BOX,nt as DEFAULT_GANN_FAN,Zn as DOUBLE_CURVE,Zo as DRAWING_CLIPBOARD_KEY,Qo as DRAWING_CLIPBOARD_VERSION,Io as DRAWING_STATE_VERSION,oo as DRAWING_TOOL_ICONS,Ir as DRAW_TIER,Mr as DrawingClipboard,Tr as DrawingController,Fo as DrawingLayer,Ln as ELLIPSE,Mn as EXTENDED_LINE,_ as EXTEND_FIELDS,Ji as FIB_CHANNEL,An as FIB_EXTENSION,Xi as FIB_FAN,Cn as FIB_RETRACEMENT,Ki as FIB_TIME_ZONE,w as FILL_FIELDS,hi as FLAG_MARK,T as FONT_FIELDS,g as FONT_OPTIONS,Ni as FORECAST,Gi as GANN_BOX,Yi as GANN_FAN,Vi as HIGHLIGHTER,wn as HORIZONTAL_LINE,_n as HORIZONTAL_RAY,io as ICON_ATTRS,no as ICON_STROKE,yo as ICON_SYMBOL_PREFIX,eo as ICON_VIEWBOX,S as LEVEL_FIELDS,E as LEVEL_NEUTRAL,v as LINE_FIELDS,M as LINE_STYLE_FIELD,u as LINE_STYLE_OPTIONS,b as LINE_WIDTH_FIELD,Wn as LONG_POSITION,$n as MEASURE,Mi as NOTE,Pr as NUDGE_STEP_PX,Lr as NUDGE_STEP_SHIFT_PX,Fn as PARALLEL_CHANNEL,Kn as PATH,Ne as PATTERN_DRAWING_TOOLS,I as PLATE_TEXT_FIELDS,Di as POLYLINE,oi as PRICE_LABEL,Li as PRICE_NOTE,Ci as PRICE_RANGE,bn as RAY,Pn as RECTANGLE,Yn as ROTATED_RECTANGLE,F as SHAPE_TEXT_FIELDS,Hn as SHORT_POSITION,k as SHOW_LABELS_FIELD,Ti as SIGNPOST,ei as SINE_LINE,Ii as TABLE,Vn as TEXT,L as TEXT_FIELDS,y as TEXT_POSITION_OPTIONS,P as TEXT_VALUE_FIELD,ti as TIME_CYCLES,mn as TREND_LINE,Oi as TRIANGLE,f as VALIGN_OPTIONS,kn as VERTICAL_LINE,j as applyDrawingSettings,s as boundsOf,st as catmullRom,uo as chromeIcon,xo as chromeIconIds,wo as chromeIconSvg,nr as clearMemoryClipboard,or as cloneDrawing,Z as cloneLevels,R as coerceSettingValue,z as composeSettings,K as cycleColor,br as decodeClipboardPayload,a as distToEllipse,e as distToLine,o as distToPolyline,l as distToRect,t as distToSegment,Ue as drawingSettingsSchema,je as drawingShortcuts,ro as drawingToolIcon,so as drawingToolIconIds,lr as encodeClipboardPayload,c as extendSegment,X as formatRatio,Y as gannLabel,De as getDrawingTool,We as hasDrawingTool,_o as iconSprite,vo as iconSvg,ko as iconUse,Fr as keyToDrawingAction,q as levelColor,Re as matchDrawingShortcut,qo as migrateDrawings,lt as pressureWidth,rt as rdpSimplify,$ as readDrawingSetting,O as readDrawingSettings,r as rectOf,to as registerBuiltinDrawingTools,Oe as registerDrawingTool,He as registeredDrawingTools,mr as sanitizeDrawing,Po as sortByZIndex,ir as systemClipboard,So as toolCursor};
//# sourceMappingURL=openalgo-charts.draw.mjs.map