-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathta.min.js
1 lines (1 loc) · 28 KB
/
ta.min.js
1
/* ta.js (c) Nino Kroesen. MIT License. */ function median(e,n=e.length){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r);t.sort((e,n)=>e-n),_.push(t[Math.round(t.length/2)-1])}return _}function kmeans(e,n){for(var r,_=[],t=[],l=[],$=!1,a=Math.round(e.length/(n+1)),h=0;h<n;h++)t[h]=e[a*(h+1)];do{for(var h=0;h<n;h++)_[h]=[];for(var s=0;s<e.length;s++){for(var u=-1,o=0;o<n;o++){var i=Math.abs(t[o]-e[s]);-1===u?(u=i,r=o):i<=u&&(u=i,r=o)}_[r].push(e[s])}l=t;for(var s=0;s<n;s++){for(var p=0,o=0;o<_[s].length;o++)p+=_[s][o];var g=p/_[r].length;t[s]=g}for(var s=0;s<n;s++)t[s]!==l[s]&&($=!0)}while($);return _}function normalize(e,n=0){for(var r=0,_=Math.max.apply(null,e.slice())*(1+n),t=Math.min.apply(null,e.slice())*(1-n),l=[];r<e.length;r++)l.push(1-(_-e[r])/(_-t));return l}function denormalize(e,n,r=0){for(var _=0,t=Math.max.apply(null,e.slice())*(1+r),l=Math.min.apply(null,e.slice())*(1-r),$=[];_<n.length;_++)$.push(l+n[_]*(t-l));return $}function normalize_pair(e,n){for(var r=1,_=(e[0]+n[0])/2,t=[[_,_]];r<e.length;r++)t.push([t[t.length-1][0]*((e[r]-e[r-1])/e[r-1]+1),t[t.length-1][1]*((n[r]-n[r-1])/n[r-1]+1)]);return t}function normalize_from(e,n){for(var r=1,_=[n];r<e.length;r++)_.push(_[_.length-1]*((e[r]-e[r-1])/e[r-1]+1));return _}function standardize(e){for(var n=sma(e,e.length),r=std(e),_=[],t=0;t<e.length;t++)_.push((e[t]-n[0])/r);return _}function mad(e,n=e.length){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=median(t),$=t.map(e=>Math.abs(e-l[l.length-1]));$=median($),_.push($[$.length-1])}return _}function aad(e,n=e.length){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=0,$=sma(t,n);for(var a in t)l+=Math.abs(t[a]-$[$.length-1]);_.push(l/n)}return _}function ssd(e,n=e.length){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=sma(t,n),$=0;for(var a in t)$+=(t[a]-l[l.length-1])**2;_.push(Math.sqrt($))}return _}function er(e){var n=[],r=[],_=1,t=1;for(var l in e)e[l]>=0?n.push(1+e[l]):r.push(1+e[l]);var $=n.length/e.length,a=r.length/e.length;for(var l in r)t*=r[l];for(var l in n)_*=n[l];return((_**(1/n.length)-1)*100*$+(t**(1/r.length)-1)*100*a)/100}function ar(e,n=e.length){for(var r=n,_=[];r<e.length;r++){var t=er(e.slice(r-n,r));_.push(e[r]-t)}return _}function kelly(e){var n=er(e)+1,r=winratio(e);return isNaN(n)&&(n=1),r-(1-r)/n}function martingale(e,n,r,_=2){var t=n;for(var l in e)e[l]<0?t*=_:e[l]>0&&(t=n);return t>r?r:t}function antimartingale(e,n,r,_=2){var t=n;for(var l in e)e[l]>0?t*=_:e[l]<0&&(t=n);return t>r?r:t}function winratio(e){var n=0,r=0;for(var _ in e)e[_]>=0?n++:r++;return n/(r+n)}function avgwin(e){for(var n=0,r=[];n<e.length;n++)e[n]>=0&&r.push(e[n]);return sma(r,r.length)[0]}function avgloss(e){for(var n=[],r=0;r<e.length;r++)e[r]<0&&n.push(e[r]);return sma(n,n.length)[0]}function zscore(e,n=e.length){for(var r=n-1,_=[],t=e.slice(0,n-1);r<e.length;r++){t.push(e[r]);var l=sma(t,n),$=std(t,n);_.push((e[r]-l[0])/$),t.splice(0,1)}return _}function rsi(e,n=14){for(var r=n-1,_=0,t=0,l=[],$=e.slice(0,n-1);r<e.length;r++,_=0,t=0){$.push(e[r]);for(var a=1;a<$.length;a++)$[a]-$[a-1]<0?t+=Math.abs($[a]-$[a-1]):_+=$[a]-$[a-1];var h=100-100/(1+_/n/(t/n));l.push(h),$.splice(0,1)}return l}function wrsi(e,n=14){for(var r=1,_=[],t=[],l=[];r<e.length;r++)e[r]-e[r-1]<0?(l.push(Math.abs(e[r]-e[r-1])),t.push(0)):(l.push(0),t.push(e[r]-e[r-1]));for(var r in l=wsma(l,n),t=wsma(t,n),l)_.push(100-100/(1+t[r]/l[r]));return _}function tsi(e,n=25,r=13,_=13){for(var t=1,l=[],$=[],a=[],h=[];t<e.length;t++)l.push(e[t]-e[t-1]),$.push(Math.abs(e[t]-e[t-1]));for(var s=ema(l,n),u=ema($,n),o=ema(s,r),i=ema(u,r),t=0;t<o.length;t++)a.push(o[t]/i[t]);var p=ema(a,_);a.splice(0,a.length-p.length);for(var t=0;t<p.length;t++)h.push([p[t],a[t]]);return h}function bop(e,n=14){var r=e.map(e=>(e[3]-e[0])/(e[1]-e[2])),r=sma(r,n);return r}function fi(e,n=13){for(var r=1,_=[],t=[];r<e.length;r++)if(_.push((e[r][0]-e[r-1][0])*e[r][1]),_.length>=n){var l=ema(_,n);t.push(l[l.length-1]),_.splice(0,1)}return t}function asi(e){for(var n=1,r=[];n<e.length;n++){var _,t=e[n][1],l=e[n-1][1],$=e[n][0],a=e[n-1][0],h=e[n][2],s=e[n-1][2],u=a-t>s-t?a-t:s-t,o=e[n][0],i=e[n-1][0],p=Math.max(e[n][0],e[n-1][0])-Math.min(e[n][2],e[n-1][2]);$-l>h-l&&$-l>$-h&&(_=$-l-(h-l)/2+(l-i)/4),h-l>$-l&&h-l>$-h&&(_=h-l-($-l)/2+(l-i)/4),$-h>$-l&&$-h>h-l&&(_=$-h+(l-i)/4),r.push(50*((l-t+(l-i)/2+(t-o)/2)/_)*u/p)}return r}function ao(e,n=5,r=35){e=e.map(e=>(e[0]+e[1])/2);for(var _=r,t=[];_<=e.length;_++){var l=e.slice(_-r,_),$=sma(l,n),a=sma(l,r);t.push($[$.length-1]-a[a.length-1])}return t}function ac(e,n=5,r=35){var _=ao(e,n,r),t=sma(_,n),l=[];for(var $ in _.length>t.length?_.splice(0,_.length-t.length):t.splice(0,t.length-_.length),_)l.push(_[$]-t[$]);return l}const fib=(e,n)=>[e,(n-e)*.236+e,(n-e)*.382+e,(n-e)*.5+e,(n-e)*.618+e,(n-e)*.786+e,n,(n-e)*1.618+e,(n-e)*2.618+e,(n-e)*3.618+e,(n-e)*4.236+e];function pr(e,n=14){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=Math.max.apply(null,t),$=Math.min.apply(null,t);_.push(-((l-e[r-1])/(l-$)*100))}return _}function lsma(e,n=25){for(var r=n,_=[];r<=e.length;r++){for(var t,l,$=e.slice(r-n,r),a=0,h=0,s=0,u=0,o=0,i=1;i<=n;i++)a+=i,h+=$[i-1],s+=$[i-1]*i,u+=i*i,o+=$[i-1]*$[i-1];t=(s-a*h/n)/(u-a*a/n),l=h/n-t*a/n,_.push(t*n+l)}return _}function don(e,n=20){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=[],$=[];for(var a in t)l.push(t[a][0]),$.push(t[a][1]);var h=Math.max.apply(null,l.slice()),s=Math.min.apply(null,$.slice());_.push([h,(h+s)/2,s])}return _}function ichimoku(e,n=9,r=26,_=52,t=26){for(var l=0,$=[],a=[],h=[];l<e.length;l++)if($.push(e[l]),$.length>=_){var s=[],u=[];for(var o in $)s.push($[o][0]),u.push($[o][2]);var i=(Math.max.apply(null,s.slice(s.length-n,s.length))+Math.min.apply(null,u.slice(u.length-n,u.length)))/2,p=(Math.max.apply(null,s.slice(s.length-r,s.length))+Math.min.apply(null,u.slice(u.length-r,u.length)))/2,g=e[l][1]+p,v=(Math.max.apply(null,s.slice(s.length-_,s.length))+Math.min.apply(null,u.slice(u.length-r,u.length)))/2;chik=e[l][1],h.push([i,p,g,v,chik]),$.splice(0,1)}for(var l=t;l<h.length-t;l++)a.push([h[l][0],h[l][1],h[l+t][2],h[l+t][3],h[l-t][4]]);return a}function stoch(e,n=14,r=3,_=3){n<r&&([n,r]=[r,n]),_>r&&([_,r]=[r,_]);for(var t=0,l=[],$=[],a=[],h=[];t<e.length;t++){if($.push(e[t][0]),a.push(e[t][2]),$.length>=n){var s=Math.max.apply(null,$),u=Math.min.apply(null,a),o=100*(e[t][1]-u)/(s-u);h.push(o)}if(_>0&&h.length>_){var i=sma(h,_);h.push(i[i.length-1])}if(h.length-_>=r){var p=sma(h,r);l.push([o,p[p.length-1]]),$.splice(0,1),a.splice(0,1),h.splice(0,1)}}return l}function atr(e,n=14){for(var r=1,_=[e[0][0]-e[0][2]];r<e.length;r++){var t=Math.max(e[r][0]-e[r-1][1],e[r][2]-e[r-1][1],e[r][0]-e[r][2]);_.push((_[_.length-1]*(n-1)+t)/n)}return _}function sma(e,n=14){for(var r=n,_=[];r<=e.length;r++){var t=sum(e.slice(r-n,r));_.push(t/n)}return _}function smma(e,n=14){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=0;for(var $ in t)l+=t[$];_.length<=0?_.push(l/n):_.push((l-_[_.length-1])/n)}return _.splice(0,1),_}function wma(e,n=14){for(var r=1,_=0,t=[];r<=n;r++)_+=r;for(var r=n;r<=e.length;r++){var l=e.slice(r-n,r),$=0;for(var a in l)$+=l[a]*(Number(a)+1)/_;t.push($)}return t}function wsma(e,n=14){for(var r=n,_=[],t=1/n;r<=e.length;r++){if(_.length>0){_.push((e[r-1]-_[_.length-1])*t+_[_.length-1]);continue}var l=e.slice(r-n,r),$=0;for(var a in l)$+=l[a];_.push($/n)}return _}function pwma(e,n=14){for(var r=n/2,_=n,t=0,l=[],$=[];r>=1;r--,_--)r%1!=0?t+=(r=Math.round(r))*_:($.push(r*_),t+=r*_*2),$.unshift(r*_);for(var r=n;r<=e.length;r++){for(var a=0,h=e.slice(r-n,r),s=n-1;s>=0;s--)a+=h[s]*$[s]/t;l.push(a)}return l}function hwma(e,n=14){for(var r=1,_=n,t=0,l=[],$=[];r<=n/2;r++,_--)r%1!=0?t+=(r=Math.round(r))*_:($.push(r*_),t+=r*_*2),$.unshift(r*_);for(var r=n;r<=e.length;r++){for(var a=0,h=e.slice(r-n,r),s=n-1;s>=0;s--)a+=h[s]*$[s]/t;l.push(a)}return l}function vwma(e,n=20){e=e.map(e=>[e[0]*e[1],e[1]]);for(var r=n,_=[];r<=e.length;r++){for(var t=e.slice(r-n,r),l=0,$=0,a=0;a<t.length;a++)l+=t[a][1],$+=t[a][0];_.push($/l)}return _}function ema(e,n=12){for(var r=n,_=[],t=2/(n+1);r<=e.length;r++){if(_.length>0){_.push((e[r-1]-_[_.length-1])*t+_[_.length-1]);continue}var l=e.slice(r-n,r),$=0;for(var a in l)$+=l[a];_.push($/n)}return _}function hull(e,n=14){var r=[],_=[],t=wma(e,n),l=Math.round(Math.sqrt(n)),$=wma(e,Math.round(n/2));for(var a in $.splice(0,$.length-t.length),t)if(r.push(2*$[a]-t[a]),r.length>=l){var h=wma(r,l);_.push(h[0]),r.splice(0,1)}return _}function kama(e,n=10,r=2,_=30){for(var t=n+1,l=sma(e,n),l=[l[l.length-1]];t<e.length;t++){for(var $=0,a=Math.abs(e[t]-e[t-n]),h=1;h<n;h++)$+=Math.abs(e[t-h]-e[t-h-1]);var s=(a/$*(2/(r+1)-2/(_+1)+2/(_+1)))**2;l.push(l[l.length-1]+s*(e[t]-l[l.length-1]))}return l}function macd(e,n=12,r=26){n>r&&([n,r]=[r,n]);var _=ema(e,n),t=ema(e,r),l=[];_.splice(0,r-n);for(var $=0;$<t.length;$++)l.push(_[$]-t[$]);return l}function macd_signal(e,n=12,r=26,_=9){var t=macd(e,n,r);return ema(t,_)}function macd_bars(e,n=12,r=26,_=9){var t=macd(e,n,r),l=ema(t,_),$=[];for(var a in t.splice(0,t.length-l.length),t)$.push(t[a]-l[a]);return $}function bands(e,n=14,r=1){for(var _=0,t=[],l=[],$=[],a=sma(e,n);_<e.length;_++)if(t.push(e[_]),t.length>=n){var h=std(t,n);l.push(h),t.splice(0,1)}for(var _=0;_<a.length;_++)$.push([a[_]+l[_]*r,a[_],a[_]-l[_]*r]);return $}function bandwidth(e,n=14,r=1){for(var _=0,t=[],l=bands(e,n,r);_<l.length;_++)t.push((l[_][0]-l[_][2])/l[_][1]);return t}function keltner(e,n=14,r=1){var _,t=[],l=atr(e,n),$=[];for(var a in e)t.push((e[a][0]+e[a][1]+e[a][2])/3);_=sma(t,n),l.splice(0,n-1);for(var a=0;a<_.length;a++)$.push([_[a]+l[a]*r,_[a],_[a]-l[a]*r]);return $}function variance(e,n=e.length){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=sma(t,n),$=0;for(var a in t)$+=(t[a]-l[l.length-1])**2;_.push($/n)}return _}function std(e,n=e.length){n<e.length&&e.splice(0,e.length-n);var r=e.reduce((e,n)=>e+n)/n;return Math.sqrt(e.reduce((e,n)=>e+Math.pow(n-r,2),0)/n)}function normsinv(e){if(e<0||e>1)return 0;if(e<.02425){var n=Math.sqrt(-2*Math.log(e));return(((((-.00778489400243029*n+-.322396458041136)*n+-2.40075827716184)*n+-2.54973253934373)*n+4.37466414146497)*n+2.93816398269878)/((((.00778469570904146*n+.32246712907004)*n+2.445134137143)*n+3.75440866190742)*n+1)}if(e<=.97575){var n=e-.5,r=n*n;return(((((-39.6968302866538*r+220.946098424521)*r+-275.928510446969)*r+138.357751867269)*r+-30.6647980661472)*r+2.50662827745924)*n/(((((-54.4760987982241*r+161.585836858041)*r+-155.698979859887)*r+66.8013118877197)*r+-13.2806815528857)*r+1)}var n=Math.sqrt(-2*Math.log(1-e));return-(((((-.00778489400243029*n+-.322396458041136)*n+-2.40075827716184)*n+-2.54973253934373)*n+4.37466414146497)*n+2.93816398269878)/((((.00778469570904146*n+.32246712907004)*n+2.445134137143)*n+3.75440866190742)*n+1)}function sim(e,n=50,r=1e3,_){for(var t=0,l=[];t<r;t++){for(var $=e.slice(),a=0;a<n;a++){for(var h=[],s=1;s<$.length;s++){var u=dif($[s],$[s-1]);h.push(u)}var o=sma(h,h.length),i=std(h),p=normsinv(Math.random());$.push($[$.length-1]*Math.exp(o[0]-i*i/2+i*p))}l.push($)}return _?percentile(l,_):l}function percentile(e,n){for(var r=0,_=[];r<e[0].length;r++)e.sort((e,n)=>e[r]-n[r]),_.push(e[Math.round((e.length-1)*n)][r]);return _}function cor(e,n){for(var r=sma(e,e.length),_=sma(n,n.length),t=0,l=0,$=0,a=0;a<e.length;a++){var h=e[a]-r,s=n[a]-_;t+=h*s,l+=Math.pow(h,2),$+=Math.pow(s,2)}var u=e.length-1;return l/=u,$/=u,t/(u*(l=Math.sqrt(l))*($=Math.sqrt($)))}const dif=(e,n)=>(e-n)/n,log=e=>e.map(e=>Math.log(e)),exp=e=>e.map(e=>Math.exp(e));function drawdown(e){for(var n=1,r=e[0],_=e[0],t=0;n<e.length;n++){if(e[n]>r){if(0!=_){var l=dif(_,r);l<t&&(t=l),_=e[n]}r=e[n]}e[n]<_&&(_=e[n])}var l=dif(_,r);return l<t&&(t=l),t}function aroon_up(e,n=10){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=t.slice();l.sort((e,n)=>e-n),_.push(100*(n-1-t.reverse().findIndex(e=>e===l[n-1]))/(n-1))}return _}function aroon_down(e,n=10){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=t.slice();l.sort((e,n)=>e-n),_.push(100*(n-1-t.reverse().findIndex(e=>e===l[0]))/(n-1))}return _}function aroon_osc(e,n=25){for(var r=0,_=[],t=aroon.up(e,n),l=aroon.down(e,n);r<t.length;r++)_.push(t[r]-l[r]);return _}function mfi(e,n=14){for(var r=n,_=[],t=e.map(e=>e[1]),l=e.map(e=>e[0]),$=0,a=0;r<=e.length;r++,$=0,a=0){for(var h=r-n;h<r;h++)$+=l[h],a+=t[h];_.push(100-100/(1+$/a))}return _}function roc(e,n=14){for(var r=n,_=[];r<=e.length;r++)_.push((e[r-1]-e[r-n])/e[r-n]);return _}function cop(e,n=11,r=14,_=10){for(var t=Math.max(n,r),l=[],$=t+_;$<e.length;$++){var a=e.slice($-(t+_),$),h=a.slice(),s=[];a=roc(a,n),h=roc(h,r),a.splice(0,a.length-h.length),h.splice(0,h.length-a.length);for(var u=0;u<a.length;u++)s.push(a[u]+h[u]);s=wma(s,_),l.push(s[s.length-1])}return l}function kst(e,n=10,r=15,_=20,t=30,l=10,$=10,a=10,h=15,s=9){for(var u=[],o=[],i=Math.max(n,r,_,t)+Math.max(l,$,a,h),p=i;p<=e.length;p++){var g=e.slice(p-i,p),v=roc(g,n),f=roc(g,r),c=roc(g,_),m=roc(g,t);v=sma(v,l),f=sma(f,$),c=sma(c,a),m=sma(m,h),u.push(v[v.length-1]+f[f.length-1]+c[c.length-1]+m[m.length-1])}var x=sma(u,s);for(var p in u.splice(0,u.length-x.length),x)o.push([u[p],x[p]]);return o}function obv(e){for(var n=1,r=[0];n<e.length;n++)e[n][1]>e[n-1][1]&&r.push(r[r.length-1]+e[n][0]),e[n][1]<e[n-1][1]&&r.push(r[r.length-1]-e[n][0]),e[n][1]===e[n-1][1]&&r.push(r[r.length-1]);return r}function vwap(e,n=e.length){e=e.map(e=>[e[0]*e[1],e[1]]);for(var r=n,_=[];r<=e.length;r++){for(var t=e.slice(r-n,r),l=0,$=0,a=0;a<t.length;a++)l+=t[a][1],$+=t[a][0];_.push($/l)}return _}function mom(e,n=10,r){for(var _=n-1,t=[];_<e.length;_++)r?t.push(e[_]/e[_-(n-1)]*100):t.push(e[_]-e[_-(n-1)]);return t}function mom_osc(e,n=10){n++;for(var r=n,_=[],t=0,l=0;r<=e.length;r++,t=0,l=0){for(var $=1;$<n;$++)e[r-n+($-1)]<e[r-n+$]?t+=e[r-n+$]:l+=e[r-n+$];_.push((t-l)/(t+l)*100)}return _}function ha(e){for(var n=[[(e[0][0]+e[0][3])/2,e[0][1],e[0][2],(e[0][0]+e[0][1]+e[0][2]+e[0][3])/4]],r=1;r<e.length;r++)n.push([(n[n.length-1][0]+n[n.length-1][3])/2,Math.max(n[n.length-1][0],n[n.length-1][3],e[r][1]),Math.min(n[n.length-1][0],n[n.length-1][3],e[r][2]),(e[r][0]+e[r][1]+e[r][2]+e[r][3])/4]);return n}function ren(e,n=1){for(var r=[],_=Math.floor(n)===n?0:n.toString().split(".")[1].length||0,t=Math.ceil(e[0][0]/n*10**_)/10**_*n,l=t-n,$=1;$<e.length;$++){if(e[$][0]>t+n)do r.push([t,t+n,t,t+n]),t+=n,l+=n;while(e[$][0]>t+n);if(e[$][1]<l-n)do r.push([l,l,l-n,l-n]),t-=n,l-=n;while(e[$][1]<l-n)}return r}function envelope(e,n=10,r=.005){for(var _=n,t=[];_<e.length;_++){var l=sma(e.slice(_-n,_),n);t.push([l[0]*(1+r),l[0],l[0]*(1-r)])}return t}function chaikin_osc(e,n=3,r=10){for(var _=0,t=[],l=[];_<e.length;_++){var $=(e[_][1]-e[_][2]-(e[_][0]-e[_][1]))/(e[_][0]-e[_][2]);isNaN($)?l.push(0):l.push($*e[_][3])}var a=ema(l,n),h=ema(l,r);a.length>h.length?a.splice(0,a.length-h.length):h.splice(0,h.length-a.length);for(var _=0;_<a.length;_++)t.push(a[_]-h[_]);return t}function fractals(e,n=!1){for(var r=n?[[-1,-1],[-1,-1]]:[[!1,!1],[!1,!1]],_=2;_<e.length-2;_++){var t=e[_-2][0]<e[_][0]&&e[_-1][0]<e[_][0]&&e[_][0]>e[_+1][0]&&e[_][0]>e[_+2][0]?!n||e[_][0]:!!n&&-1,l=e[_-2][1]>e[_][1]&&e[_-1][1]>e[_][1]&&e[_][1]<e[_+1][1]&&e[_][1]<e[_+2][1]?!n||e[_][1]:!!n&&-1;r.push([t,l])}return n?r.push([[-1,-1],[-1,-1]]):r.push([!1,!1],[!1,!1]),r}function alligator(e,n=13,r=8,_=5,t=8,l=5,$=3){var a=[],h=smma(e,n),s=smma(e,r),u=smma(e,_);s.splice(0,s.length-h.length),u.splice(0,u.length-h.length);for(var o=h.length-1;o>=7;o--)a.push([h[o-(t-1)],s[o-(l-1)],u[o-($-1)]]);return a}function gator(e,n=13,r=8,_=5,t=8,l=5,$=3){var a=[],h=smma(e,n),s=smma(e,r),u=smma(e,_);s.splice(0,s.length-h.length),u.splice(0,u.length-h.length);for(var o=h.length-1;o>=t-1;o--)a.push([h[o-(t-1)]-s[o-(l-1)],-Math.abs(s[o-(l-1)]-u[o-($-1)])]);return a}function recent_high(e,n=25){for(var r=e.length-2,_=n,t=0,l=e[e.length-1];r>=0&&(e[r]>=l&&_>0?(l=e[r],t=r,_=n):_--,!(_<=0));r--);return{index:t,value:l}}function recent_low(e,n=25){for(var r=e.length-2,_=n,t=0,l=e[e.length-1];r>=0&&(e[r]<=l&&_>0?(l=e[r],t=r,_=n):_--,!(_<=0));r--);return{index:t,value:l}}function support(e,n){var r,_,t=(n=n||recent_low(e)).value;do{for(var l=n.index;l<e.length;l++){var $=(n.value-e[l])/(n.index-l);$<t&&(t=$,r=l)}n.index+1==r&&r!=e.length-1?(n.index=r,t=Math.min.apply(null,e.slice()),n.value=e[n.index],_=!1):_=!0,n.index==e.length-1&&(_=!0)}while(!_);return r==e.length-1||n.index==e.length-1?{calculate:e=>n.value,slope:0,lowest:n.value,index:n.index}:{calculate:e=>e*t+n.value,slope:t,lowest:n.value,index:n.index}}function resistance(e,n){var r,_,t=(n=n||recent_high(e)).value;do{for(var l=n.index;l<e.length;l++){var $=(e[l]-n.value)/(n.index-l);$<t&&(t=$,r=l)}n.index+1==r&&r!=e.length-1?(n.index=r,t=Math.max.apply(null,e.slice()),n.value=e[n.index],_=!1):_=!0,n.index==e.length-1&&(_=!0)}while(!_);return r==e.length-1||n.index==e.length-1?{calculate:e=>n.value,slope:0,highest:n.value,index:n.index}:{calculate:e=>-(e*t)+n.value,slope:t,highest:n.value,index:n.index}}function fisher(e,n){for(var r=[],_=0,t=0,l=n;l<=e.length;l++){var $=e.slice(l-n,l),a=_,h=Math.min.apply(null,$),t=.66*((e[l-1]-h)/(Math.max.apply(null,$)-h)-.5)+.67*t;t>.99&&(t=.999),t<-.99&&(t=-.999),_=.5*Math.log((1+t)/(1-t))+.5*a,r.push([_,a])}return r.slice(1,r.length)}function cross(e,n){e.splice(0,e.length-n.length);for(var r=e[0]>n[0],_=[],t=0;t<e.length;t++)e[t]<n[t]&&r&&(_.push({index:t,cross:!1}),r=!1),e[t]>n[t]&&!r&&(_.push({index:t,cross:!0}),r=!0);return _}function se(e,n){return n=n||e.length,std(e)/n**.5}function halftrend(e,n,r,_){for(var t=[],l=[0],$=[0],a=[0],h=[0],s=void 0,u=n;u<=e.length;u++){var o=e.slice(u-n,u);maxlow=o[o.length-2][2],minhigh=o[o.length-2][0];var i=_*(atr2=(atr2=atr(o,n))[atr2.length-1]/2),p=Math.max.apply(null,o.slice(o.length-1,o.length).map(e=>e[0])),g=Math.min.apply(null,o.slice(o.length-1,o.length).map(e=>e[2])),v=sma(o.slice(o.length-r,r).map(e=>e[0]),r),f=sma(o.slice(o.length-r,r).map(e=>e[2]),r);if(1==l[l.length-1]?(maxlow=Math.max(g,maxlow),v[0]<maxlow&&o[o.length-1][1]<o[o.length-2][2]&&($.push(1),l.push(0),minhigh=o[o.length-2][0])):(minhigh=Math.min(p,minhigh),f[0]>minhigh&&o[o.length-1][1]<o[o.length-2][0]&&($.push(0),l.push(1),maxlow=g)),0==$[$.length-1]){isNaN($[$.length-2])||0==$[$.length-2]?a.push(isNaN(a[a.length-2])?maxlow:Math.max(a[a.length-2],maxlow)):a.push(isNaN(h[h.length-2])?h[h.length-1]:h[h.length-2]),s="long";var c=a[a.length-1]+i,m=a[a.length-1]-i}else{isNaN($[$.length-2])||1==$[$.length-2]?h.push(isNaN(h[h.length-2])?minhigh:Math.min(minhigh,h[h.length-2])):h.push(isNaN(a[a.len-2])?a[a.length-1]:a[a.length-2]),s="short";var c=h[h.length-1]+i,m=h[h.length-1]-i}t.push([c,0==$[$.length-1]?a[a.length-1]:h[h.length-1],m,s])}return t}const sum=e=>e.reduce((e,n)=>e+n);function covariance(e,n,r){for(var _=[],t=sma(e,e.length),l=sma(n,n.length),$=r;$<=e.length;$++){for(var a=e.slice($-r,$),h=n.slice($-r,$),t=sma(a,r),s=[],l=sma(h,r),u=0;u<r;u++)s.push((a[u]-t[0])*(h[u]-l[0]));s=sum(s),_.push(s/r)}return _}const range=(e,n,r)=>Math.floor(r?r()*(Math.floor(n)-Math.ceil(e))+Math.ceil(e):Math.random()*(Math.floor(n)-Math.ceil(e))+Math.ceil(e)),pick=(e,n)=>e[Math.floor(n?n()*Math.floor(e.length):Math.random()*Math.floor(e.length))],float=(e,n,r)=>r?r()*(n-e)+e:Math.random()*(n-e)+e;function order(e,n){var r=[];e=e.slice();do{var _=Math.floor(n?n()*Math.floor(e.length):Math.random()*Math.floor(e.length));r.push(e[_]),e.splice(_,1)}while(e.length>0);return r}function prng(e){for(var n=0,r=1779033703^e.length;n<e.length;n++)r=(r=Math.imul(r^e.charCodeAt(n),3432918353))<<13|r>>>19;return r=Math.imul((r=Math.imul(r^r>>>16,2246822507))^r>>>13,3266489909),e=(r^=r>>>16)>>>0,function(){var n=e+=1831565813;return n=Math.imul(n^n>>>15,1|n),(((n^=n+Math.imul(n^n>>>7,61|n))^n>>>14)>>>0)/4294967296}}function ncdf(e,n,r){var _=1/(1+.2315419*Math.abs(e=n||r?(e-n)/r:e)),t=.3989423*Math.exp(-e*e/2)*_*(.3193815+_*(-.3565638+_*(1.781478+_*(-1.821256+1.330274*_))));return e>0?1-t:t}function zigzag(e,n=.05){var r=[],_=1/0,t=-1/0,l=!1,$=!1,a=[];if(Array.isArray(e[0]))for(var h=0;h<e.length;h++)if(l){r[r.length-1].value>=e[h][1]&&(r[r.length-1].value=e[h][1],r[r.length-1].index=h),_>=e[h][1]&&(_=e[h][1]);var s=(e[h][0]-_)/_;s>n&&(r.push({index:h,value:e[h][0]}),$=!0,l=!1,_=1/0)}else if($){r[r.length-1].value<=e[h][0]&&(r[r.length-1].value=e[h][0],r[r.length-1].index=h),t<=e[h][1]&&(t=e[h][1]);var u=(t-e[h][1])/e[h][1];u>n&&(r.push({index:h,value:e[h][1]}),l=!0,$=!1,t=-1/0)}else{_>=e[h][1]&&(_=e[h][1]),t<=e[h][0]&&(t=e[h][0]);var s=(e[h][0]-_)/_,u=(t-e[h][1])/t;u>n&&s<n?(l=!0,r.push({index:0,value:e[0][0]}),r.push({index:h,value:e[h][1]})):s>n&&u<n?($=!0,r.push({index:0,value:e[0][1]}),r.push({index:h,value:e[h][0]})):u>s?(l=!0,r.push({index:0,value:e[0][0]}),r.push({index:h,value:e[h][1]})):($=!0,r.push({index:0,value:e[0][1]}),r.push({index:h,value:e[h][0]}))}else for(var h=0;h<e.length;h++)if(l){r[r.length-1].value>=e[h]&&(r[r.length-1].value=e[h],r[r.length-1].index=h),_>=e[h]&&(_=e[h]);var s=(e[h]-_)/_;s>n&&(r.push({index:h,value:e[h]}),$=!0,l=!1,_=1/0)}else if($){r[r.length-1].value<=e[h]&&(r[r.length-1].value=e[h],r[r.length-1].index=h),t<=e[h]&&(t=e[h]);var u=(t-e[h])/e[h];u>n&&(r.push({index:h,value:e[h]}),l=!0,$=!1,t=-1/0)}else{_>=e[h]&&(_=e[h]),t<=e[h]&&(t=e[h]);var s=(e[h]-_)/_,u=(t-e[h])/t;u>n&&s<n?(l=!0,r.push({index:0,value:e[0]}),r.push({index:h,value:e[h]})):s>n&&u<n?($=!0,r.push({index:0,value:e[0]}),r.push({index:h,value:e[h]})):u>s?(l=!0,r.push({index:0,value:e[0]}),r.push({index:h,value:e[h]})):($=!0,r.push({index:0,value:e[0]}),r.push({index:h,value:e[h]}))}a=[r[0].value];for(var h=1;h<r.length;h++)for(var o=r[h].index-r[h-1].index,i=(r[h].value-r[h-1].value)/o,p=1;p<=o;p++)a.push(p*i+r[h-1].value);return a}function psar(e,n=.02,r=.2){for(var _=e[0],t=!0,l=n,$=e[0],a=e[0][1],h=e[0][0],s=[a],u=1;u<e.length;u++)a+=l*(h-a),t?(a=Math.min(a,_[1],$[1]),e[u][0]>h&&(h=e[u][0],l=Math.min(l+n,r))):(a=Math.max(a,_[0],$[0]),e[u][1]<h&&(h=e[u][0],l=Math.min(l+n,r))),(t&&e[u][1]<a||!t&&e[u][0]>a)&&(l=n,a=h,h=(t=!t)?e[u][0]:e[u][1]),_=$,$=e[u],s.push(a);return s}function fibbands(e,n=20,r=3){for(var _=0,t=[],l=[],$=vwma(e,n);_<e.length;_++)if(t.push(e[_][0]),t.length>=n){var a=std(t,n);l.push(a*r),t.splice(0,1)}for(var _=0,h=[];_<$.length;_++){var s=$[_]+.236*l[_],u=$[_]+.382*l[_],o=$[_]+.5*l[_],i=$[_]+.618*l[_],p=$[_]+.764*l[_],g=$[_]+l[_],v=$[_]-.236*l[_],f=$[_]-.382*l[_],c=$[_]-.5*l[_],m=$[_]-.618*l[_],x=$[_]-.764*l[_],d=$[_]-l[_];h.push([g,p,i,o,u,s,$[_],v,f,c,m,x,d])}return h}function supertrend(e,n=20,r=3){for(var _=n-1,t=atr(e,n),l=[];_<e.length;_++)l.push([(e[_][0]+e[_][2])/2+r*t[_],(e[_][0]+e[_][2])/2-r*t[_]]);return l}function cwma(e,n){for(var r=n.length,_=[];r<=e.length;r++){for(var t=e.slice(r-n.length,r),l=0,$=0,a=0;a<n.length;a++)l+=t[a]*n[a],$+=n[a];_.push(l/$)}return _}const fibnumbers=[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181],permutations=e=>e.reduce((e,n)=>e*n);function mse(e,n){for(var r=0,_=0;r<e.length;r++)_+=Math.pow(n[r]-e[r],2);return _/e.length}function cum(e,n){for(var r=n,_=[];r<=e.length;r++)_.push(sum(e.slice(r-n,r)));return _}function vwwma(e,n=20){e=e.map(e=>[e[0]*e[1],e[1]]);for(var r=1,_=0;r<=n;r++)_+=r;for(var r=n,t=[];r<=e.length;r++){var l=e.slice(r-n,r),$=0,a=0;for(var h in l)$+=l[h][1]*(Number(h)+1)/_,a+=l[h][0]*(Number(h)+1)/_;t.push(a/$)}return t}function elderray(e,n=13){for(var r=n,_=[];r<=e.length;r++){var t=e.slice(r-n,r),l=Math.min.apply(void 0,t),$=Math.max.apply(void 0,t),a=ema(t,t.length);_.push([$-a[0],l-a[0]])}return _}function hv(e,n=10){for(var r=n,_=[];r<=e.length;r++){var t=ssd(e.slice(r-n,r))/n;_.push(Math.sqrt(t))}return _}const t_table=[{value:.5,1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0,24:0,25:0},{value:.25,1:1,2:.816,3:.765,4:.741,5:.727,6:.718,7:.711,8:.706,9:.703,10:.7,11:.697,12:.695,13:.694,14:.692,15:.691,16:.69,17:.689,18:.688,19:.688,20:.687,21:.686,22:.686,23:.685,24:.685,25:.684},{value:.2,1:1.376,2:1.061,3:.978,4:.941,5:.92,6:.906,7:.896,8:.889,9:.883,10:.879,11:.876,12:.873,13:.87,14:.868,15:.866,16:.865,17:.863,18:.862,19:.861,20:.86,21:.859,22:.858,23:.858,24:.857,25:.856},{value:.15,1:1.963,2:1.386,3:1.25,4:1.19,5:1.156,6:1.134,7:1.119,8:1.108,9:1.1,10:1.093,11:1.088,12:1.088,13:1.079,14:1.076,15:1.074,16:1.071,17:1.069,18:1.067,19:1.066,20:1.064,21:1.063,22:1.061,23:1.06,24:1.059,25:1.058},{value:.1,1:3.078,2:1.886,3:1.638,4:1.533,5:1.476,6:1.44,7:1.415,8:1.397,9:1.383,10:1.372,11:1.363,12:1.356,13:1.35,14:1.345,15:1.341,16:1.337,17:1.333,18:1.33,19:1.328,20:1.325,21:1.323,22:1.321,23:1.319,24:1.318,25:1.316},{value:.05,1:6.314,2:2.92,3:2.353,4:2.132,5:2.015,6:1.943,7:1.895,8:1.86,9:1.833,10:1.812,11:1.796,12:1.782,13:1.771,14:1.761,15:1.753,16:1.746,17:1.74,18:1.734,19:1.729,20:1.725,21:1.721,22:1.717,23:1.714,24:1.711,25:1.708},{value:.025,1:12.71,2:4.303,3:3.182,4:2.776,5:2.571,6:2.447,7:2.365,8:2.306,9:2.262,10:2.228,11:2.201,12:2.179,13:2.16,14:2.145,15:2.131,16:2.12,17:2.11,18:2.101,19:2.093,20:2.086,21:2.08,22:2.074,23:2.069,24:2.064,25:2.06},{value:.01,1:31.82,2:6.965,3:4.541,4:3.747,5:3.365,6:3.143,7:2.998,8:2.896,9:2.821,10:2.764,11:2.718,12:2.681,13:2.65,14:2.624,15:2.602,16:2.583,17:2.567,18:2.552,19:2.539,20:2.528,21:2.518,22:2.508,23:2.5,24:2.492,25:2.485},{value:.005,1:63.66,2:9.925,3:5.841,4:4.604,5:4.032,6:3.707,7:3.499,8:3.355,9:3.25,10:3.169,11:3.106,12:3.055,13:3.012,14:2.977,15:2.947,16:2.921,17:2.898,18:2.878,19:2.861,20:2.845,21:2.831,22:2.819,23:2.807,24:2.797,25:2.787},{value:.001,1:318.13,2:22.327,3:10.215,4:7.173,5:5.893,6:5.208,7:4.785,8:4.501,9:4.297,10:4.144,11:4.025,12:3.93,13:3.852,14:3.787,15:3.733,16:3.686,17:3.646,18:3.61,19:3.579,20:3.552,21:3.527,22:3.505,23:3.485,24:3.467,25:3.45},{value:5e-4,1:636.62,2:31.599,3:12.924,4:8.61,5:6.869,6:5.959,7:5.408,8:5.041,9:4.781,10:4.587,11:4.437,12:4.318,13:4.221,14:4.14,15:4.073,16:4.015,17:3.965,18:3.922,19:3.883,20:3.85,21:3.819,22:3.792,23:3.768,24:3.745,25:3.725}];function pvalue(e,n){if(n>25)throw Error("ta.js | pvalue | df too high!");if(n<1)throw Error("ta.js | pvalue | df too low!");for(var r=0;r<t_table.length-1;r++)if(e>=t_table[r][Number(n).toString()]&&e<=t_table[Number(r)+1][Number(n).toString()])return t_table[r+1].value+(t_table[Number(r)+1].value-t_table[r].value)*(t_table[r+1][Number(n).toString()]-e)/(t_table[r][Number(n).toString()]-t_table[r+1][Number(n).toString()]);return 1e-4}function rvi(e,n=20){for(var r=3,_=[],t=[],l=[];r<e.length;r++)if(_.push((e[r][3]-e[r][0]+2*(e[r][3]-e[r-1][0])+2*(e[r][3]-e[r-2][0])+(e[r][3]-e[r-3][0]))/6),t.push((e[r][1]-e[r][2]+2*(e[r][1]-e[r-1][2])+2*(e[r][1]-e[r-2][2])+(e[r][1]-e[r-3][2]))/6),_.length>=n){var $=sma(_,n),a=sma(t,n);l.push($[0]/a[0]),_.splice(0,1),t.splice(0,1)}return l}function rvi_signal(e){for(var n=3,r=[];n<e.length;n++)r.push((e[n]+2*e[n-1]+2*e[n-2]+e[n-3])/6);return r}function rsi_divergence(e,n,r){r||(r=wrsi);var _=r(e,n),t=[];e=mom(e.slice(n-1,e.length),2);for(var l=0;l<e.length;l++)e[l]>0&&_[l]<0||e[l]<0&&_[l]>0?t.push(1):t.push(0);return t}function divergence(e,n){e.length>n.length&&e.splice(0,e.length-n.length),n.length>e.length&&n.splice(0,n.length-e.length);for(var r=1,_=[];r<e.length;r++)e[r]>e[r-1]&&n[r]<n[r-1]||e[r]<e[r-1]&&n[r]>n[r-1]?_.push(1):_.push(0);return _}function times_up(e,n){for(var r=[],_=n;_<e.length;_++){for(var t=1,l=_-n+1;l<=_;l++)if(e[l-1]>e[l]){t=0;break}r.push(t)}return r}function times_down(e,n){for(var r=[],_=n;_<e.length;_++){for(var t=1,l=_-n+1;l<=_;l++)if(e[l-1]<e[l]){t=0;break}r.push(t)}return r}function divergence_state(e,n,r,_,t=1,l=.03,$=.01){e.length>n.length&&e.splice(0,e.length-n.length),n.length>e.length&&n.splice(0,n.length-e.length);for(var a=r,h=[];a<e.length;a++){var s=e.slice(a-r,a+1),u=support(s,recent_low(s,_)),o=u.slope/u.lowest,i=resistance(s,recent_high(s,_)),p=i.slope/i.highest,g=n.slice(a-r,a+1),v=support(g,recent_low(g,_)),f=v.slope/v.lowest,c=resistance(g,recent_high(g,_)),m=c.slope/c.highest;if(e[a]>e[a-t]&&n[a]<n[a-t]||e[a]<e[a-t]&&n[a]>n[a-t]){var x=[];p<-l&&m>-$&&x.push("exaggerated_bearish"),o<$&&f>l&&x.push("exaggerated_bullish"),p<-$&&m<$&&x.push("hidden_bearish"),o>$&&f<-$&&x.push("hidden_bullish"),p>$&&m<-$&&x.push("regular_bearish"),o<-$&&f>$&&x.push("regular_bullish"),x.length<=0&&x.push("divergence"),h.push(x)}else h.push(["convergence"])}return h}function return_negative(e){for(var n=0,r=[];n<e.length;n++)e[n]<0&&r.push(e[n]);return r}function return_positive(e){for(var n=0,r=[];n<e.length;n++)e[n]>0&&r.push(e[n]);return r}const random={range,pick,float,order,prng};const aroon={up:aroon_up,down:aroon_down,osc:aroon_osc};