vases-indicatorts
Version:
Stock technical indicators and strategies in TypeScript for browser and server programs.
3 lines (2 loc) • 34.3 kB
JavaScript
function x(...t){if(t.length>0){let r=t[0].length;for(let n=1;n<t.length;n++)if(t[n].length!==r)throw new Error(`values length at ${n} not ${r}`)}}function B(t){return t.map(r=>Math.abs(r))}function A(t,r){x(t,r);let n=new Array(t.length);for(let e=0;e<n.length;e++)n[e]=t[e]+r[e];return n}function br(t,r){x(t,r);let n=new Array(t.length);for(let e=0;e<n.length;e++)n[e]=(t[e]+r[e])/2;return n}function L(t,r){let n=new Array(r.length);for(let e=0;e<r.length;e++)n[e]=r[e]+t;return n}function l(t,r){x(t,r);let n=new Array(t.length);for(let e=0;e<n.length;e++)n[e]=t[e]/r[e];return n}function D(t,r){let n=new Array(r.length);for(let e=0;e<r.length;e++)n[e]=r[e]/t;return n}function C(t,r){x(t,r);let n=new Array(t.length);for(let e=0;e<n.length;e++)n[e]=t[e]*r[e];return n}function p(t,r){let n=new Array(r.length);for(let e=0;e<n.length;e++)n[e]=r[e]*t;return n}function c(t,r){x(t,r);let n=new Array(t.length);for(let e=0;e<n.length;e++)n[e]=t[e]-r[e];return n}function xr(t,r){let n=new Array(r.length);for(let e=0;e<r.length;e++)n[e]=r[e]-t;return n}function O(t,r,n){let e=new Array(n.length);for(let i=0;i<e.length;i++)i<t?e[i]=r:e[i]=n[i-t];return e}function q(t,r){return O(t,0,r)}function Mt(t,r,n){let e=n.length,i=Array(e).fill(r);for(let o=t;o<e;o++){let s=(o-t+e)%e;i[s]=n[o]}return i}function ye(t,r){return Mt(t,0,r)}function k(t,r){return c(r,q(t,r))}function Se(t){return t.map(r=>r>=0?1:-1)}function Dt(...t){x(...t);let r=new Array(t[0].length);for(let n=0;n<r.length;n++){r[n]=new Array(t.length);for(let e=0;e<r[n].length;e++)r[n][e]=t[e][n]}return r}function U(...t){return Dt(...t).map(r=>Math.max(...r))}function j(t,r){let n=Math.pow(10,t);return Math.round(r*n)/n}function hr(t,r){return r.map(n=>j(t,n))}function H(t,r,n){let e=new Array((r-t)/n);for(let i=0;i<e.length;i++)e[i]=t+n*i;return e}function Re(t,r){return t.map(n=>Math.pow(n,r))}function we(t){return t.map(r=>Math.sqrt(r))}var h=(e=>(e[e.SELL=-1]="SELL",e[e.HOLD=0]="HOLD",e[e.BUY=1]="BUY",e))(h||{});function yr(t){return t.map(r=>r===1?-1:r===-1?1:0)}function Me(t,r){x(t,r);let n=new Array(t.length),e=1,i=e,o=0;for(let s=0;s<n.length;s++)r[s]===1?i>0&&(o=i/t[s],i=0):r[s]===-1&&o>0&&(i=o*t[s],o=0),n[s]=(o*t[s]+i-e)/e;return n}function wr(t,r){let n=new Array(r.length);for(let e=0;e<n.length;e++){let{actions:i}=r[e].strategy(t),o=Me(t.closings,i);n[e]={info:r[e],gain:o[o.length-1],lastAction:i[i.length-1]}}return n.sort((e,i)=>i.gain-e.gain),n}var vt=(o=>(o[o.SYMBOL=0]="SYMBOL",o[o.NAME=1]="NAME",o[o.STRATEGY=2]="STRATEGY",o[o.GAIN=3]="GAIN",o[o.ACTION=4]="ACTION",o))(vt||{});function Dr(t,r,n){let e=[];switch(r){case 0:e=t.sort((i,o)=>i.companyInfo.symbol.localeCompare(o.companyInfo.symbol));break;case 1:e=t.sort((i,o)=>i.companyInfo.name.localeCompare(o.companyInfo.name));break;case 2:e=t.sort((i,o)=>i.strategyResults[0].info.name.localeCompare(o.strategyResults[0].info.name));break;case 3:e=t.sort((i,o)=>i.strategyResults[0].gain-o.strategyResults[0].gain);break;case 4:e=t.sort((i,o)=>i.strategyResults[0].lastAction-o.strategyResults[0].lastAction);break}return n||(e=e.reverse()),e}function De(t){return{actions:Array(t.closings.length).fill(1),result:t.closings}}var Lt={period:2};function d(t,r={}){let{period:n}={...Lt,...r},e=new Array(t.length),i=0;for(let o=0;o<t.length;o++)i+=t[o],o>=n?(i-=t[o-n],e[o]=i/n):e[o]=i/(o+1);return e}var K={fast:5,slow:34};function ve(t,r,n={}){let{fast:e,slow:i}={...K,...n},o=D(2,A(r,t)),s=d(o,{period:e}),m=d(o,{period:i});return c(s,m)}function Le(t,r={}){let n={...K,...r},e=ve(t.highs,t.lows,n);return{actions:e.map(o=>o>0?1:o<0?-1:0),result:e}}var X={short:9,medium:26,long:52,close:26},J=({period:t,highs:r,lows:n,projection:e=0})=>(i,o,s)=>{if(s<t-1)return[...i,0];let m=s+1-t,a=s-e+1,u=Math.max(...r.slice(m,a)),f=Math.min(...n.slice(m,a));return[...i,(u+f)/2]},Bt=({highs:t,lows:r,short:n})=>t.reduce(J({period:n,highs:t,lows:r}),[]),It=({highs:t,lows:r,medium:n})=>t.reduce(J({period:n,highs:t,lows:r}),[]),Ot=({tenkanSen:t,kijunSen:r,medium:n})=>{let e=new Array(r.length+n).fill(0);return r.forEach((i,o)=>{i&&(e[o+n]=(i+t[o])/2)}),e},Pt=({highs:t,lows:r,long:n,medium:e})=>new Array(t.length+e).fill(0).reduce(J({period:n+e,highs:t,lows:r,projection:e}),[]);function Be(t,r,n,e={}){x(t,r,n);let{short:i,medium:o,long:s,close:m}={...X,...e},a=Bt({highs:t,lows:r,short:i}),u=It({highs:t,lows:r,medium:o});return{tenkan:a,kijun:u,ssa:Ot({tenkanSen:a,kijunSen:u,medium:o}),ssb:Pt({highs:t,lows:r,medium:o,long:s}),laggingSpan:ye(m,n)}}function Ie(t,r={}){let n={...X,...r},e=Be(t.highs,t.lows,t.closings,n),i=new Array(e.kijun.length).fill(0);for(let o=1;o<i.length;o++){let s=t.closings[o],m=e.ssa[o],a=e.ssb[o],u=s>m&&s>a,f=s<m&&s<a,b=e.tenkan[o]>e.kijun[o]&&e.tenkan[o-1]<=e.kijun[o-1],R=e.tenkan[o]<e.kijun[o]&&e.tenkan[o-1]>=e.kijun[o-1];b&&u?i[o]=1:R&&f?i[o]=-1:i[o]=0}return{actions:i,result:e}}var Et={period:4};function Q(t,r={}){let{period:n}={...Et,...r},e=new Array(t.length),i=0;for(let o=0;o<t.length;o++){let s=o+1;o<n?i+=t[o]:(i=e[o-1]*(n-1)+t[o],s=n),e[o]=i/s}return e}var kt={period:14};function Oe(t,r={}){let{period:n}={...kt,...r},e=new Array(t.length),i=new Array(t.length);e[0]=i[0]=0;for(let u=1;u<t.length;u++){let f=t[u]-t[u-1];f>0?(e[u]=f,i[u]=0):(i[u]=-f,e[u]=0)}let o=Q(e,{period:n}),s=Q(i,{period:n}),m=new Array(t.length),a=new Array(t.length);m[0]=a[0]=0;for(let u=1;u<t.length;u++)a[u]=o[u]/s[u],m[u]=100-100/(1+a[u]);return m}var Ft={period:12};function g(t,r={}){let{period:n}={...Ft,...r},e=new Array(t.length);if(e.length>0){let i=2/(1+n),o=1-i;e[0]=t[0];for(let s=1;s<e.length;s++)e[s]=t[s]*i+e[s-1]*o}return e}function Pe(t,r,n,e){let i=l(c(c(n,r),c(t,n)),c(t,r)),o=C(i,e),s=new Array(o.length);for(let m=0;m<s.length;m++)s[m]=o[m],m>0&&(s[m]+=s[m-1]);return s}var Tt={fast:3,slow:10};function en(t,r,n,e,i={}){let{fast:o,slow:s}={...Tt,...i},m=Pe(t,r,n,e),a=c(g(m,{period:o}),g(m,{period:s}));return{adResult:m,cmoResult:a}}var Vt={fast:12,slow:26,signal:9};function on(t,r={}){let{fast:n,slow:e,signal:i}={...Vt,...r},o=g(t,{period:n}),s=g(t,{period:e}),m=p(100,l(c(o,s),s)),a=g(m,{period:i}),u=c(m,a);return{ppoResult:m,signal:a,histogram:u}}var _t={fast:12,slow:26,signal:9};function an(t,r={}){let{fast:n,slow:e,signal:i}={..._t,...r},o=g(t,{period:n}),s=g(t,{period:e}),m=p(100,l(c(o,s),s)),a=g(m,{period:i}),u=c(m,a);return{pvoResult:m,signal:a,histogram:u}}var Nt={period:3};function fn(t,r={}){let{period:n}={...Nt,...r},e=new Array(t.length);for(let i=0;i<t.length;i++)i<n?e[i]=0:e[i]=(t[i]/t[i-n]-1)*100;return e}var F=class t{constructor(){this.root=null}insert(r){let n={value:r,left:null,right:null};if(this.root===null){this.root=n;return}let e=this.root,i=!1;for(;!i;)n.value<=e.value?e.left===null?(e.left=n,i=!0):e=e.left:e.right===null?(e.right=n,i=!0):e=e.right}remove(r){let n={node:this.root,parent:null};for(;n.node!==null;){if(r===n.node.value)return this.removeNode(n),!0;n.parent=n.node,r<n.node.value?n.node=n.node.left:n.node=n.node.right}return!1}min(){let r=t.minNode(this.root);if(r.node===null)throw new Error("Tree empty");return r.node.value}max(){let r=t.maxNode(this.root);if(r.node===null)throw new Error("Tree empty");return r.node?.value}removeNode(r){if(r.node!==null)if(r.node.left!==null&&r.node.right!==null){let n=t.minNode(r.node.right);n.parent===null&&(n.parent=r.node),this.removeNode(n),n.node!==null&&(r.node.value=n.node.value)}else{let n=null;r.node.left!==null?n=r.node.left:n=r.node.right,r.parent===null?this.root=n:r.parent.left===r.node?r.parent.left=n:r.parent.right=n}}static minNode(r){let n={node:null,parent:null};if(r!==null)for(n.node=r;n.node.left!==null;)n.parent=n.node,n.node=n.node.left;return n}static maxNode(r){let n={node:null,parent:null};if(r!==null)for(n.node=r;n.node.right!==null;)n.parent=n.node,n.node=n.node.right;return n}};var qt={period:4};function M(t,r={}){let{period:n}={...qt,...r},e=new Array(t.length),i=new F;for(let o=0;o<t.length;o++)i.insert(t[o]),o>=n&&i.remove(t[o-n]),e[o]=i.min();return e}var Ht={period:4};function w(t,r={}){let{period:n}={...Ht,...r},e=new Array(t.length),i=new F;for(let o=0;o<t.length;o++)i.insert(t[o]),o>=n&&i.remove(t[o-n]),e[o]=i.max();return e}var $={kPeriod:14,dPeriod:3};function Ee(t,r,n,e={}){let{kPeriod:i,dPeriod:o}={...$,...e},s=w(t,{period:i}),m=M(r,{period:i}),a=p(100,l(c(n,m),c(s,m))),u=d(a,{period:o});return{k:a,d:u}}var z={period:14};function ke(t,r,n,e={}){let{period:i}={...z,...e},o=w(t,{period:i}),s=M(r,{period:i});return p(-100,l(c(o,n),c(o,s)))}var Z={fast:14,slow:30};function Fe(t,r={}){let{fast:n,slow:e}={...Z,...r},i=g(t,{period:n}),o=g(t,{period:e});return c(i,o)}function ee(t){let r=new Array(t.length);if(t.length>0){let n=null,e=0;for(let i=0;i<t.length;i++)n!==t[i]?(n=t[i],e=0):e++,r[i]=e}return r}var te={period:25};function Te(t,r,n={}){x(t,r);let{period:e}={...te,...n},i=ee(w(t,{period:e})),o=ee(M(r,{period:e})),s=p(100,D(e,L(e,p(-1,i)))),m=p(100,D(e,L(e,p(-1,o))));return{up:s,down:m}}function Ve(t,r,n,e){return l(c(e,t),c(r,n))}function Ut(t,r){x(t,r);let n=0,e=0,i=0,o=0;for(let u=0;u<t.length;u++)n+=t[u],e+=t[u]*t[u],i+=r[u],o+=t[u]*r[u];let s=t.length,m=(s*o-n*i)/(s*e-n*n),a=(i-m*n)/s;return{m,b:a}}function Y(t,r,n){x(r,n);let e=new Array(r.length),i=new Array(r.length),o=0,s=0,m=0,a=0;for(let u=0;u<r.length;u++){o+=r[u],s+=r[u]*r[u],m+=n[u],a+=r[u]*n[u];let f=u+1;u>=t&&(o-=r[u-t],s-=r[u-t]*r[u-t],m-=n[u-t],a-=r[u-t]*n[u-t],f=t),u>0?(e[u]=(f*a-o*m)/(f*s-o*o),i[u]=(m-e[u]*o)/f):(e[u]=0,i[u]=0)}return{m:e,b:i}}function _e(t,r){let n=Ut(t,r);return L(n.b,p(n.m,t))}function Ne(t,r,n){let e=Y(t,r,n);return A(C(e.m,r),e.b)}var zn={period:4};function qe(t){let r=H(0,t.length,1),n=_e(r,t);return p(100,l(c(t,n),t))}var Yt={period:4};function Zn(t,r={}){let{period:n}={...Yt,...r},e=H(0,t.length,1),i=Ne(n,e,t);return p(100,l(c(t,i),t))}function T(t,r,n){return D(3,A(A(t,r),n))}var Gt={period:20};function so(t,r,n,e={}){let{period:i}={...Gt,...e},o=T(t,r,n),s=d(o,{period:i}),m=d(B(c(o,s)),{period:i});return l(c(o,s),p(.015,m))}var Wt={period:12};function co(t,r={}){let{period:n}={...Wt,...r},e=g(t,{period:n}),i=g(e,{period:n});return c(p(2,e),i)}var jt={period:4};function S(t,r={}){let{period:n}={...jt,...r},e=new Array(t.length),i=0;for(let o=0;o<t.length;o++)i+=t[o],o>=n&&(i-=t[o-n]),e[o]=i;return e}var Kt={emaPeriod:9,miPeriod:25};function xo(t,r,n={}){let{emaPeriod:e,miPeriod:i}={...Kt,...n},o=g(c(t,r),{period:e}),s=g(o,{period:e}),m=l(o,s);return S(m,{period:i})}var re={fast:12,slow:26,signal:9};function He(t,r={}){let{fast:n,slow:e,signal:i}={...re,...r},o=g(t,{period:n}),s=g(t,{period:e}),m=c(o,s),a=g(m,{period:i});return{macdLine:m,signalLine:a}}var ne=(e=>(e[e.FALLING=-1]="FALLING",e[e.STABLE=0]="STABLE",e[e.RISING=1]="RISING",e))(ne||{});var oe={step:.02,max:.2};function Ue(t,r,n,e={}){x(t,r,n);let{step:i,max:o}={...oe,...e},s=new Array(t.length),m=new Array(t.length);s[0]=-1,m[0]=t[0];let a=i,u=r[0];for(let f=1;f<m.length;f++){m[f]=m[f-1]-(m[f-1]-u)*a,s[f-1]===-1?(m[f]=Math.max(m[f],t[f-1]),f>1&&(m[f]=Math.max(m[f],t[f-2])),t[f]>=m[f]&&(m[f]=u)):(m[f]=Math.min(m[f],r[f-1]),f>1&&(m[f]=Math.min(m[f],r[f-2])),r[f]<=m[f]&&(m[f]=u));let b=u;m[f]>n[f]?(s[f]=-1,u=Math.min(u,r[f])):(s[f]=1,u=Math.max(u,t[f])),s[f]!==s[f-1]?a=i:b!==u&&a<o&&(a+=i)}return{trends:s,psarResult:m}}var Xt={period:14};function Lo(t,r,n={}){let{period:e}={...Xt,...n};return d(c(r,t),{period:e})}var ie={rPeriod:9,kPeriod:3,dPeriod:3};function Ye(t,r,n,e={}){let{rPeriod:i,kPeriod:o,dPeriod:s}={...ie,...e},m=w(t,{period:i}),a=M(r,{period:i}),u=p(100,l(c(n,a),c(m,a))),f=d(u,{period:o}),b=d(f,{period:s}),R=c(p(3,f),p(2,b));return{k:f,d:b,j:R}}var Jt={period:4};function To(t,r={}){let{period:n}={...Jt,...r},e=0,i=0;return n%2===0?(e=n/2,i=e+1):(e=(n+1)/2,i=e),d(d(t,{period:i}),{period:e})}var Qt={period:4};function qo(t,r={}){let{period:n}={...Qt,...r},e=g(t,{period:n}),i=g(e,{period:n}),o=g(i,{period:n}),s=O(1,o[0],o);return l(c(o,s),s)}var $t={period:2};function Go(t,r={}){let{period:n}={...$t,...r},e=g(t,{period:n}),i=g(e,{period:n}),o=g(i,{period:n});return A(c(p(3,e),p(3,i)),o)}var se={period:20};function Ge(t,r,n={}){let{period:e}={...se,...n};return l(S(C(t,r),{period:e}),S(r,{period:e}))}var ue={period:14};function We(t,r,n,e={}){x(t,r,n);let{period:i}={...ue,...e},o=q(1,n),s=B(c(t,q(1,r))),m=B(c(r,q(1,t))),a=S(s,{period:i}),u=S(m,{period:i}),f=U(c(t,r),B(c(t,o)),B(c(r,o))),b=S(f,{period:i}),R=l(a,b),y=l(u,b);return{plus:R,minus:y}}var me={period:20,multiplier:4};function je(t,r,n,e={}){x(t,r,n);let{period:i,multiplier:o}={...me,...e},s=l(c(t,r),A(t,r)),m=d(C(t,L(1,p(o,s))),{period:i}),a=d(n,{period:i}),u=d(C(r,L(1,p(-1*o,s))),{period:i});return{upper:m,middle:a,lower:u}}function Ke(t,r,n){x(t,r,n);let e=O(1,n[0],n);return U(c(t,r),B(c(t,e)),B(c(r,e)))}var zt={period:14};function G(t,r,n,e={}){let{period:i}={...zt,...e},o=Ke(t,r,n),s=d(o,{period:i});return{trLine:o,atrLine:s}}var Zt={period:4};function Xe(t,r={}){let{period:n}={...Zt,...r},e=new Array(t.length),i=d(t,{period:n});for(let o=0;o<t.length;o++)if(e[o]=0,o>=n-1){let s=0;for(let m=o-(n-1);m<=o;m++)s+=(t[m]-i[o])*(t[m]-i[o]);e[o]=Math.sqrt(s/n)}return e}var ae={period:20};function Je(t,r={}){let{period:n}={...ae,...r},e=p(2,Xe(t,{period:n})),i=d(t,{period:n}),o=A(i,e),s=c(i,e);return{upper:o,middle:i,lower:s}}var er={period:90};function Ni(t,r={}){let{period:n}={...er,...r},e=l(c(t.upper,t.lower),t.middle),i=g(e,{period:n});return{width:e,widthEma:i}}var tr={period:22};function Wi(t,r,n,e={}){let{period:i}={...tr,...e},o=G(t,r,n,{period:i}),s=p(3,o.atrLine),m=w(t,{period:i}),a=M(r,{period:i}),u=c(m,s),f=A(a,s);return{long:u,short:f}}var rr={period:4};function Qi(t,r={}){let{period:n}={...rr,...r},e=w(t,{period:n}),i=M(t,{period:n}),o=D(2,A(e,i));return{upper:e,middle:o,lower:i}}var nr={period:20};function ts(t,r,n,e={}){let{period:i}={...nr,...e},o=G(t,r,n,{period:i}),s=p(2,o.atrLine),m=g(n,{period:i}),a=A(m,s),u=c(m,s);return{middle:m,upper:a,lower:u}}var ce={period:14,smooth:3};function Qe(t,r,n,e={}){let{period:i,smooth:o}={...ce,...e},s=H(0,n.length,1),m=Y(i,s,t),a=Y(i,s,r),u=A(t,C(m.m,s)),f=A(r,C(a.m,s)),b=w(u,{period:i}),R=M(f,{period:i}),y=l(p(100,c(n,R)),c(b,R)),v=g(y,{period:o});return{poResult:y,spoResult:v}}var or={period:14};function ls(t,r={}){let{period:n}={...or,...r},e=w(t,{period:n}),i=p(100,l(c(t,e),e)),o=d(C(i,i),{period:n});return we(o)}var fe={period:20};function $e(t,r,n,e,i={}){let{period:o}={...fe,...i},s=l(c(c(n,r),c(t,n)),c(t,r)),m=C(s,e);return l(S(m,{period:o}),S(e,{period:o}))}var le={period:14};function ze(t,r,n,e={}){let{period:i}={...le,...e},o=k(1,D(2,A(t,r))),s=l(D(1e8,n),c(t,r));return d(l(o,s),{period:i})}var pe={period:13};function Ze(t,r,n={}){let{period:e}={...pe,...n};return g(C(k(1,t),r),{period:e})}var ge={period:14};function et(t,r,n,e,i={}){let{period:o}={...ge,...i},s=C(T(t,r,n),e),m=Se(k(1,s)),a=C(m,s),u=a.map(y=>y>=0?y:0),f=a.map(y=>y<0?y:0),b=l(S(u,{period:o}),S(p(-1,f),{period:o}));return L(100,p(-100,Re(L(1,b),-1)))}var de={start:1e3,period:255};function tt(t,r,n={}){x(t,r);let{start:e}={...de,...n},i=new Array(t.length);for(let o=0;o<i.length;o++)o===0?i[o]=e:r[o-1]<r[o]?i[o]=i[o-1]:i[o]=i[o-1]+(t[o]-t[o-1])/t[o-1]*i[o-1];return i}function Us(t,r){x(t,r);let n=new Array(t.length);n[0]=0;for(let e=1;e<n.length;e++)n[e]=n[e-1],t[e]>t[e-1]?n[e]+=r[e]:t[e]<t[e-1]&&(n[e]-=r[e]);return n}function js(t,r){let n=O(1,t[0],t),e=C(r,l(c(t,n),n));return S(e,{period:e.length})}var be={period:14};function rt(t,r,n={}){let{period:e}={...be,...n};return l(S(C(t,r),{period:e}),S(r,{period:e}))}var xe=class{constructor(){}segmentationByClose(r,n){if(r.length>1){let s=r.reduce(function(a,u){return a>u?a:u}),m=r.reduce(function(a,u){return a<u?a:u});var e=r.indexOf(m),i=r.indexOf(s);if(e!==i){let a=e<i?"upward":"downward",u=a=="upward"?[e,i+1]:[i,e+1];switch(n.init_trend||(n.init_trend=a=="upward"?1:-1),a){case"upward":var o=Math.atan2(s-m,u[1]-u[0])*180/Math.PI;n.segmentation.push({from:m,to:s,min:m,max:s,degree:o,type:a,upward_point:[],downward_point:[]}),this.trend([...r].slice(u[0],u[1]),n,a,m),r=r.slice(i),n.curr_trend=a=="upward"?1:-1;break;case"downward":var o=Math.atan2(m-s,u[1]-u[0])*180/Math.PI;n.segmentation.push({from:s,to:m,min:m,max:s,degree:o,type:a,upward_point:[],downward_point:[]}),this.trend([...r].slice(u[0],u[1]),n,a,s),r=r.slice(e),n.curr_trend=a=="upward"?1:-1;break}this.segmentationByClose(r,n)}}}trend(r,n,e,i){if(r.length>1){var o=1e3,s=r[0],m=r.length*1e3,a=r[r.length-1],u=Math.atan2(Math.abs(a-s),Math.abs(m-o))*180/Math.PI,f=null,b=0,R=0,y=0,v=0;r.forEach((E,V)=>{var Ae=(V+1)*1e3,_=E,N=Math.atan2(Math.abs(_-s),Math.abs(Ae-o))*180/Math.PI;y++,u>N&&N!=0&&(b>0?N<b&&(v=_,b=N,R=(_-i)/y,f=V):(v=_,b=N,R=(_-i)/y,f=V))}),f&&(n.segmentation[n.segmentation.length-1].avg=(n.segmentation[n.segmentation.length-1].avg+v)/2,n.segmentation[n.segmentation.length-1][e+"_point"].push({degree:b*(e==="upward"?1:-1),close:v,diff:R,date:(f+1)*1e3,seg_idx:n.segmentation.length-1}),n[e+"_point"].push({degree:b*(e==="upward"?1:-1),close:v,diff:R,date:(f+1)*1e3,seg_idx:n.segmentation.length-1}),this.trend(r.slice(f,r.length),n,e,r[f]))}}getLineIntersect(r,n,e,i){var o=0,s=0;o=(n*r.date-i*e.date+e.close-r.close)/(n-i),s=n*(o-r.date)+r.close;var m={date:o,close:s};return m}cross_point(r,n,e){let i=[];r.upward_point.forEach((u,f)=>{r.downward_point.forEach((b,R)=>{var y=this.getLineIntersect(u,u.diff/1e3,b,b.diff/1e3),v=u.date<b.date?u:b,E=v.seg_idx;y.close>=r.segmentation[E].min&&r.segmentation[E].max>=y.close&&i.push(y)})});var o=i.filter(u=>u.close>=n&&e>u.date),s=i.filter(u=>u.close<=n&&e>u.date),m=i.filter(u=>u.close>=n&&e<=u.date),a=i.filter(u=>u.close<=n&&e<=u.date);return{resist:o,support:s,future_resist:m,future_support:a}}};function ir(t,r){let n=new xe,e=t[t.length-1],i=t.length-1,{resist:o,support:s,future_resist:m,future_support:a}=n.cross_point(r,e,i);return{resist_count:o.length,support_count:s.length,future_resist_count:m.length,future_support_count:a.length,resist:o.length>0?o.map(u=>u.close).reduce((u,f)=>u+f,0)/o.length:e,support:s.length>0?s.map(u=>u.close).reduce((u,f)=>u+f,0)/s.length:e,future_resist:m.length>0?m.map(u=>u.close).reduce((u,f)=>u+f,0)/m.length:e,future_support:a.length>0?a.map(u=>u.close).reduce((u,f)=>u+f,0)/a.length:e}}function sr(t){if(t.length===0)return 0;let r=s=>{let m=0,a=0;return s.type==="upward"?(a=s.upward_point.length,a>0&&(m=s.upward_point.reduce((u,f)=>u+f.degree,0)/a)):(a=s.downward_point.length,a>0&&(m=s.downward_point.reduce((u,f)=>u+f.degree,0)/a)),(s.degree+m)/2},n=t[t.length-1],e=r(n),o=t.reduce((s,m)=>s+r(m),0)/t.length;return e-o}var uu={cacheDisable:!1};function mu(t){let r,n={recent_trend:[],strength:[],support:[],resist:[],future_support:[],future_resist:[],support_count:[],resist_count:[],future_support_count:[],future_resist_count:[]};for(let e=0;e<t.segs.length;e++){let i=t.segs[e];r!==void 0&&(r.curr_trend==i.curr_trend&&r.segmentation.length-i.segmentation.length>0?i.recent_trend=i.curr_trend>0?1:-1:i.recent_trend=r.recent_trend);let o=sr(i.segmentation),s=ir(t.closings.slice(0,e+1),i);n.strength.push(o),n.support.push(s.support),n.support_count.push(s.support_count),n.resist.push(s.resist),n.resist_count.push(s.resist_count),n.future_support.push(s.future_support),n.future_support_count.push(s.future_support_count),n.future_resist.push(s.future_resist),n.future_resist_count.push(s.future_resist_count),n.recent_trend.push(i.recent_trend),r=i}return n}function nt(t,r={}){let n=Oe(t.closings,{...r}),e=d(t.closings,{period:50}),i=new Array(n.length).fill(0);for(let o=2;o<i.length;o++){let[s,m,a]=[n[o-2],n[o-1],n[o]],u=t.closings[o]>e[o],f=t.closings[o]>t.closings[o-1],b=t.closings[o]<t.closings[o-1],R=a>10&&m<=10&&s<=10,y=a>m&&m>s,v=R&&y&&u&&f,E=a<90&&m>=90&&s>=90,V=a<m&&m<s;v?i[o]=1:E&&V&&!u&&b&&(i[o]=-1)}return{actions:i,result:{rsi:n,sma:e}}}function ot(t,r={}){let n={...$,...r},e=Ee(t.highs,t.lows,t.closings,n),i=new Array(e.k.length);for(let o=1;o<i.length;o++)e.k[o]>e.d[o]&&e.k[o-1]<=e.d[o-1]&&e.k[o]<20?i[o]=1:e.k[o]<e.d[o]&&e.k[o-1]>=e.d[o-1]&&e.k[o]>80?i[o]=-1:i[o]=0;return{actions:i,result:e}}function it(t,r={}){let n={...z,...r},e=ke(t.highs,t.lows,t.closings,n);return{actions:e.map(o=>o<=-80?1:o>=-20?-1:0),result:e}}function st(t,r={}){let{fast:n,slow:e}={...Z,...r},i=Fe(t.closings,{fast:n,slow:e}),o=new Array(i.length);for(let s=0;s<o.length;s++)i[s]>0?o[s]=1:i[s]<0?o[s]=-1:o[s]=0;return{actions:o,result:i}}function ut(t,r={}){let n={...te,...r},e=Te(t.highs,t.lows,n),i=new Array(e.up.length);for(let o=0;o<i.length;o++)e.up[o]>e.down[o]?i[o]=1:e.down[o]>e.up[o]?i[o]=-1:i[o]=0;return{actions:i,result:e}}function mt(t){let r=Ve(t.openings,t.highs,t.lows,t.closings);return{actions:r.map(e=>e>0?1:e<0?-1:0),result:r}}function at(t){let r=qe(t.closings);return{actions:r.map(e=>e>0?1:e<0?-1:0),result:r}}function ct(t,r={}){let n={...ie,...r},e=Ye(t.highs,t.lows,t.closings,n),i=new Array(e.k.length);for(let o=0;o<i.length;o++)e.k[o]>e.d[o]&&e.k[o]<=20?i[o]=1:e.k[o]<e.d[o]&&e.k[o]>=80?i[o]=-1:i[o]=0;return{actions:i,result:e}}function ft(t,r={}){let n={...re,...r},e=He(t.closings,n),i=new Array(e.macdLine.length);for(let o=0;o<i.length;o++)o>0?e.macdLine[o]>e.signalLine[o]&&e.macdLine[o-1]<=e.signalLine[o-1]?i[o]=1:e.macdLine[o]<e.signalLine[o]&&e.macdLine[o-1]>=e.signalLine[o-1]?i[o]=-1:i[o]=0:i[o]=0;return{actions:i,result:e}}function lt(t,r={}){let n={...oe,...r},e=Ue(t.highs,t.lows,t.closings,n);return{actions:e.trends.map(o=>{switch(o){case-1:return-1;case 1:return 1;case 0:return 0;default:return 0}}),result:e}}function pt(t){let r=T(t.highs,t.lows,t.closings),n=new Array(r.length);n[0]=0;for(let e=1;e<n.length;e++)r[e]>r[e-1]?n[e]=1:r[e]<r[e-1]?n[e]=-1:n[e]=0;return{actions:n,result:r}}function gt(t,r={}){let n={...ue,...r},e=We(t.highs,t.lows,t.closings,n),i=new Array(e.plus.length);for(let o=0;o<i.length;o++)e.plus[o]>e.minus[o]?i[o]=1:e.plus[o]<e.minus[o]?i[o]=-1:i[o]=0;return{actions:i,result:e}}function dt(t,r={}){let n={...me,...r},e=je(t.highs,t.lows,t.closings,n),i=new Array(e.upper.length);for(let o=0;o<i.length;o++)t.closings[o]>=e.upper[o]?i[o]=1:i[o]=-1;return{actions:i,result:e}}function bt(t,r={}){let n={...ae,...r},e=Je(t.closings,n),i=new Array(e.upper.length);for(let o=0;o<i.length;o++)t.closings[o]>e.upper[o]?i[o]=-1:t.closings[o]<e.lower[o]?i[o]=1:i[o]=0;return{actions:i,result:e}}function xt(t,r={}){let n={...ce,...r},e=Qe(t.highs,t.lows,t.closings,n),i=new Array(e.poResult.length);for(let o=0;o<i.length;o++)e.poResult[o]>e.spoResult[o]?i[o]=1:e.poResult[o]<e.spoResult[o]?i[o]=-1:i[o]=0;return{actions:i,result:e}}function ht(t,r={}){let n={...fe,...r},e=$e(t.highs,t.lows,t.closings,t.volumes,n);return{actions:e.map(o=>o<0?1:o>0?-1:0),result:e}}function Ct(t,r={}){let n={...le,...r},e=ze(t.highs,t.lows,t.volumes,n);return{actions:e.map(o=>o>0?1:o<0?-1:0),result:e}}function At(t,r={}){let n={...pe,...r},e=Ze(t.closings,t.volumes,n);return{actions:e.map(o=>o>0?1:o<0?-1:0),result:e}}function yt(t,r={}){let n={...ge,...r},e=et(t.highs,t.lows,t.closings,t.volumes,n);return{actions:e.map(o=>o>=80?-1:o<=20?1:0),result:e}}function St(t,r={}){let n={...de,...r},e=tt(t.closings,t.volumes,n),i=g(e,{period:n.period}),o=new Array(e.length);for(let s=0;s<o.length;s++)e[s]<i[s]?o[s]=1:e[s]>i[s]?o[s]=-1:o[s]=0;return{actions:o,result:{nvi:e,ema:i}}}function Rt(t,r={}){let n={...be,...r},e=rt(t.closings,t.volumes,n);return{actions:c(e,t.closings).map(s=>s>0?1:s<0?-1:0),result:e}}var Ba=[{name:"Buy Hold",strategy:De},{name:"Awesome Oscillator",strategy:Le},{name:"RSI 2",strategy:nt},{name:"Ichimoku Cloud",strategy:Ie},{name:"Stochastic Oscillator",strategy:ot},{name:"Williams R",strategy:it},{name:"Absolute Price Oscillator (APO)",strategy:st},{name:"Aroon Strategy",strategy:ut},{name:"Balance of Power (BOM)",strategy:mt},{name:"Chande Forecast Oscillator (CFO)",strategy:at},{name:"KDJ Strategy",strategy:ct},{name:"MACD Strategy",strategy:ft},{name:"Parabolic SAR",strategy:lt},{name:"Typical Price",strategy:pt},{name:"Vortex Strategy",strategy:gt},{name:"Acceleration Bands",strategy:dt},{name:"Bollinger Bands",strategy:bt},{name:"Projection Oscillator",strategy:xt},{name:"Chaikin Money Flow (CMF)",strategy:ht},{name:"Ease of Movement (EMV)",strategy:Ct},{name:"Force Index",strategy:At},{name:"Money Flow Index",strategy:yt},{name:"Negative Volume Index (NVI)",strategy:St},{name:"Volume Weighted Average Price",strategy:Rt}];function ur(t){return{strategyInfo:t.info,score:1,minGain:t.gain,maxGain:t.gain,averageGain:t.gain}}function mr(t,r){t.score++,t.minGain=Math.min(t.minGain,r.gain),t.maxGain=Math.max(t.maxGain,r.gain),t.averageGain=(t.averageGain+r.gain)/2}function Oa(t){let r=new Map;for(let e of t){let i=e.strategyResults[0],o=r.get(i.info.name);o!==void 0?mr(o,i):r.set(i.info.name,ur(i))}let n=Array.from(r.values());return n.sort((e,i)=>i.score-e.score),n}var ar=(o=>(o[o.STRATEGY=0]="STRATEGY",o[o.SCORE=1]="SCORE",o[o.MIN=2]="MIN",o[o.MAX=3]="MAX",o[o.AVERAGE=4]="AVERAGE",o))(ar||{});function Pa(t,r,n){let e=[];switch(r){case 0:e=t.sort((i,o)=>i.strategyInfo.name.localeCompare(o.strategyInfo.name));break;case 1:e=t.sort((i,o)=>i.score-o.score);break;case 2:e=t.sort((i,o)=>i.minGain-o.minGain);break;case 3:e=t.sort((i,o)=>i.maxGain-o.maxGain);break;case 4:e=t.sort((i,o)=>i.averageGain-o.averageGain);break}return n||(e=e.reverse()),e}var I=class t{constructor(r,n){this.min=r,this.max=n}getMin(){return this.min}getMax(){return this.max}span(){return this.max-this.min}static merge(r){return r.reduce((n,e)=>new t(Math.min(n.min,e.min),Math.max(n.max,e.max)))}static from(r){return new t(Math.min(...r),Math.max(...r))}};var P=class{constructor(r,n){this.fromDelta=r.getMin(),this.toDelta=n.getMin(),this.multiplier=n.span()/r.span()}scale(r){return(r-this.fromDelta)*this.multiplier+this.toDelta}descale(r){return(r-this.toDelta)/this.multiplier+this.fromDelta}};var cr="black",fr=1,lr="red",pr=1,gr=30,he=4,dr="14px Arial",Ce=-1,W=new I(0,1),wt=class{constructor(r){let n=document.getElementById(r);if(n===null)throw new Error("Canvas element not found");if(!(n instanceof HTMLCanvasElement))throw new Error("Not a canvas element");this.canvas=n;let e=this.canvas.getContext("2d");if(e===null)throw new Error("Canvas context is null");this.context=e,this.dataSets=new Map,this.dateSetsChanged=!1,this.xScaler=new P(W,W),this.yScaler=new P(W,W),this.xFocus=Ce,this.canvas.addEventListener("mousemove",i=>this.onMouseMove(i)),this.canvas.addEventListener("mouseout",()=>this.onMouseOut())}add(r){this.dataSets.set(r.legend,r),this.dateSetsChanged=!0}remove(r){let n=this.dataSets.delete(r);return n&&(this.dateSetsChanged=!0),n}draw(){this.dateSetsChanged&&(this.updateScalers(),this.dateSetsChanged=!1),this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.drawDataSets(),this.drawFocus()}drawDataSets(){let r=this.chartHeight();for(let n of this.dataSets.values()){this.context.lineWidth=n.width??fr;let e=0,i=0;for(let o=0;o<n.values.length;o++){let s=this.xScaler.scale(o),m=r-this.yScaler.scale(n.values[o]);o>0&&(this.context.strokeStyle=this.styleAtIndex(n,o),this.context.beginPath(),this.context.moveTo(e,i),this.context.lineTo(s,m),this.context.stroke()),e=s,i=m}}}styleAtIndex(r,n){return r.style===void 0?cr:Array.isArray(r.style)?r.style[n]:r.style}drawFocus(){this.xFocus!==Ce&&(this.drawFocusLine(),this.drawFocusedValues())}drawFocusLine(){this.context.strokeStyle=lr,this.context.lineWidth=pr,this.context.beginPath(),this.context.moveTo(this.xFocus,0),this.context.lineTo(this.xFocus,this.chartHeight()),this.context.stroke()}drawFocusedValues(){let r=Math.round(this.xScaler.descale(this.xFocus)),n=he;this.context.font=dr,this.context.textBaseline="bottom";for(let e of this.dataSets.values())if(r>=0&&r<e.values.length){let i=e.legend+" "+j(2,e.values[r]).toString();this.context.fillStyle=this.styleAtIndex(e,r),this.context.fillText(i,n,this.canvas.height-he),n+=this.context.measureText(i).width+he}}chartHeight(){return this.canvas.height-gr}updateScalers(){this.dataSets.size!==0&&(this.xScaler=new P(new I(0,Math.max(...Array.from(this.dataSets.values(),r=>r.values.length))),new I(0,this.canvas.width)),this.yScaler=new P(I.merge(Array.from(this.dataSets.values(),r=>I.from(r.values))),new I(0,this.chartHeight())))}onMouseMove(r){let n=this.canvas.getBoundingClientRect();this.xFocus=r.clientX-n.left,this.draw()}onMouseOut(){this.xFocus=Ce,this.draw()}};function $a(t){return t.toISOString().substring(0,10)}function za(t){let r=new Date;return r.setDate(r.getDate()-t),r}function Za(t,r){let n=new Date(t);return n.setDate(n.getDate()+r),n}function ec(t){return(t.getMonth()+1).toString()+"/"+t.getDate().toString()}function xc(t,r={}){let n={...se,...r},e=d(t.closings,n),i=Ge(t.closings,t.volumes,n),o=new Array(i.length);for(let s=0;s<o.length;s++)i[s]>e[s]?o[s]=1:i[s]<e[s]?o[s]=-1:o[s]=0;return{actions:o,result:{sma:e,vwma:i}}}function Hc(t){return{dates:new Array(t),openings:new Array(t),closings:new Array(t),highs:new Array(t),lows:new Array(t),volumes:new Array(t)}}function Uc(t,r){return{dates:[...t.dates,...r.dates],openings:[...t.openings,...r.openings],closings:[...t.closings,...r.closings],highs:[...t.highs,...r.highs],lows:[...t.lows,...r.lows],volumes:[...t.volumes,...r.volumes]}}export{me as ABDefaultConfig,K as AODefaultConfig,Z as APODefaultConfig,zt as ATRDefaultConfig,h as Action,te as AroonDefaultConfig,ae as BBDefaultConfig,er as BBWDefaultConfig,F as BinarySearchTree,Gt as CCIDefaultConfig,tr as CEDefaultConfig,zn as CFODefaultConfig,fe as CMFDefaultConfig,Tt as CMODefaultConfig,wt as Chart,vt as CompanyResultSortBy,rr as DCDefaultConfig,Wt as DEMADefaultConfig,Ft as EMADefaultConfig,le as EMVDefaultConfig,pe as FIDefaultConfig,X as IchimokuCloudDefaultConfig,nr as KCDefaultConfig,ie as KDJDefaultConfig,re as MACDDefaultConfig,Yt as MCFODefaultConfig,ge as MFIDefaultConfig,Kt as MIDefaultConfig,Ht as MMaxDefaultConfig,qt as MMinDefaultConfig,Zt as MSTDDefaultConfig,jt as MSumDefaultConfig,de as NVIDefaultConfig,I as NumRange,P as NumScaler,ce as PODefaultConfig,Vt as PPODefaultConfig,oe as PSARDefaultConfig,_t as PVODefaultConfig,Xt as QstickDefaultConfig,Et as RMADefaultConfig,Nt as ROCDefaultConfig,kt as RSIDefaultConfig,uu as RTSDefaultConfig,Lt as SMADefaultConfig,Ba as STRATEGY_INFOS,$ as StochDefaultConfig,xe as StockAnalysis,ar as StrategyStatsSortBy,$t as TEMADefaultConfig,Jt as TRIMADefaultConfig,Qt as TRIXDefaultConfig,ne as Trend,or as UIDefaultConfig,be as VWAPDefaultConfig,se as VWMADefaultConfig,ue as VortexDefaultConfig,z as WillrDefaultConfig,je as ab,dt as abStrategy,B as abs,Fe as absolutePriceOscillator,st as absolutePriceOscillatorStrategy,je as accelerationBands,dt as accelerationBandsStrategy,Pe as accumulationDistribution,Pe as ad,A as add,L as addBy,ve as ao,Le as aoStrategy,Fe as apo,st as apoStrategy,Me as applyActions,Te as aroon,ut as aroonStrategy,G as atr,G as averageTrueRange,br as avg,ve as awesomeOscillator,Le as awesomeOscillatorStrategy,wr as backtest,Ve as balanceOfPower,mt as balanceOfPowerStrategy,Je as bb,bt as bbStrategy,Ni as bbw,Je as bollingerBands,bt as bollingerBandsStrategy,Ni as bollingerBandsWidth,Ve as bop,mt as bopStrategy,De as buyAndHoldStrategy,so as cci,Wi as ce,qe as cfo,at as cfoStrategy,$e as chaikinMoneyFlow,ht as chaikinMoneyFlowStrategy,en as chaikinOscillator,qe as chandeForecastOscillator,at as chandeForecastOscillatorStrategy,Wi as chandelierExit,k as changes,x as checkSameLength,$e as cmf,ht as cmfStrategy,en as cmo,so as communityChannelIndex,Oa as computeStrategyStats,Uc as concatAssets,za as daysAgo,Za as daysLaterFrom,Qi as dc,co as dema,l as divide,D as divideBy,Qi as donchianChannel,co as doubleExponentialMovingAverage,ze as easeOfMovement,Ct as easeOfMovementStrategy,g as ema,ze as emv,Ct as emvStrategy,g as exponentialMovingAverage,Se as extractSigns,Ze as fi,At as fiStrategy,Ze as forceIndex,At as forceIndexStrategy,H as generateNumbers,Be as ichimokuCloud,Ie as ichimokuCloudStrategy,ts as kc,Ye as kdj,ct as kdjStrategy,ts as keltnerChannel,Ut as leastSquare,_e as linearRegressionUsingLeastSquare,He as macd,ft as macdStrategy,xo as massIndex,U as max,Zn as mcfo,et as mfi,yt as mfiStrategy,xo as mi,w as mmax,M as mmin,et as moneyFlowIndex,yt as moneyFlowIndexStrategy,He as movingAverageConvergenceDivergence,ft as movingAverageConvergenceDivergenceStrategy,Zn as movingChandeForecastOscillator,Y as movingLeastSquare,Ne as movingLinearRegressionUsingLeastSquare,w as movingMax,M as movingMin,Xe as movingStandardDeviation,S as movingSum,Xe as mstd,S as msum,C as multiply,p as multiplyBy,tt as negativeVolumeIndex,St as negativeVolumeIndexStrategy,Hc as newAssetWithLength,tt as nvi,St as nviStrategy,Us as obv,Us as onBalanceVolume,Ue as parabolicSAR,lt as parabolicSARStrategy,on as percentagePriceOscillator,an as percentageVolumeOscillator,Qe as po,xt as poStrategy,Re as pow,on as ppo,fn as priceRateOfChange,Qe as projectionOscillator,xt as projectionOscillatorStrategy,Ue as psar,lt as psarStrategy,an as pvo,Lo as qstick,Ye as randomIndex,mu as recursiveTrendStrength,Oe as relativeStrengthIndex,yr as reverseActions,Q as rma,fn as roc,Q as rollingMovingAverage,j as roundDigits,hr as roundDigitsAll,Oe as rsi,nt as rsi2Strategy,mu as rts,Mt as shiftLeftAndFillBy,ye as shiftLeftBy,O as shiftRightAndFillBy,q as shiftRightBy,d as simpleMovingAverage,ee as since,d as sma,Dr as sortCompanyResults,Pa as sortStrategyStats,we as sqrt,Ee as stoch,ot as stochStrategy,Ee as stochasticOscillator,ot as stochasticOscillatorStrategy,c as subtract,xr as subtractBy,Go as tema,$a as toISODateString,ec as toMonthAndDay,Ke as tr,Dt as transpose,To as triangularMovingAverage,To as trima,qo as tripleExponentialAverage,Go as tripleExponentialMovingAverage,qo as trix,Ke as trueRange,T as typicalPrice,pt as typicalPriceStrategy,T as typprice,pt as typpriceStrategy,ls as ui,ls as ulcerIndex,js as volumePriceTrend,rt as volumeWeightedAveragePrice,Rt as volumeWeightedAveragePriceStrategy,Ge as volumeWeightedMovingAverage,xc as volumeWeightedMovingAverageStrategy,We as vortex,gt as vortexStrategy,js as vpt,rt as vwap,Rt as vwapStrategy,Ge as vwma,xc as vwmaStrategy,it as willRStrategy,ke as williamsR,it as williamsRStrategy,ke as willr};
//# sourceMappingURL=index.js.map