UNPKG

mytt-ts

Version:

通达信、同花顺、文华麦语言等指标公式的 TypeScript 实现

2 lines (1 loc) 10.2 kB
function n(n,t=3){return Number(Math.round(Number(n+"e"+t))+"e-"+t)}function t(n,t=1){return n[n.length-t]}function r(n){return n.map(Math.abs)}function e(n){return n.map(Math.log)}function a(n,t){return n.map((n=>Math.pow(n,t)))}function o(n){return n.map(Math.sqrt)}function l(n){return n.map(Math.sin)}function u(n){return n.map(Math.cos)}function c(n){return n.map(Math.tan)}function f(n,t){return n.map(((n,r)=>Math.max(n,t[r])))}function m(n,t){return n.map(((n,r)=>Math.min(n,t[r])))}function i(n,t,r){const e="number"==typeof t,a="number"==typeof r;return n.map(((n,o)=>{const l=e?t:t[o],u=a?r:r[o];return n?l:u}))}function p(n,t=1){const r=new Array(n.length).fill(NaN);for(let e=t;e<n.length;e++)r[e]=n[e-t];return r}function h(n,t=1){const r=new Array(n.length).fill(NaN);for(let e=t;e<n.length;e++)r[e]=n[e]-n[e-t];return r}function s(n,t){const r=new Array(n.length).fill(NaN);for(let e=t-1;e<n.length;e++){const a=n.slice(e-t+1,e+1),o=a.reduce(((n,t)=>n+t),0)/t,l=a.reduce(((n,t)=>n+Math.pow(t-o,2)),0);r[e]=Math.sqrt(l/t)}return r}function g(n,t){const r=new Array(n.length).fill(NaN);if(t<=0){let t=0;return n.map((n=>t+=n))}for(let e=0;e<n.length;e++)e<t-1||(r[e]=n.slice(e-t+1,e+1).reduce(((n,t)=>n+t),0));return r}function M(n){const t=n[n.length-1];return new Array(n.length).fill(t)}function N(n,t){const r=new Array(n.length).fill(NaN);for(let e=0;e<n.length;e++)e<t-1||(r[e]=Math.max(...n.slice(e-t+1,e+1)));return r}function y(n,t){const r=new Array(n.length).fill(NaN);for(let e=0;e<n.length;e++)e<t-1||(r[e]=Math.min(...n.slice(e-t+1,e+1)));return r}function w(n,t){const r=new Array(n.length).fill(NaN);for(let e=t-1;e<n.length;e++){const a=n.slice(e-t+1,e+1),o=Math.max(...a);r[e]=a.length-1-a.lastIndexOf(o)}return r}function A(n,t){const r=new Array(n.length).fill(NaN);for(let e=t-1;e<n.length;e++){const a=n.slice(e-t+1,e+1),o=Math.min(...a);r[e]=a.length-1-a.lastIndexOf(o)}return r}function b(n,t){const r=new Array(n.length).fill(NaN);for(let e=t-1;e<n.length;e++)r[e]=n.slice(e-t+1,e+1).reduce(((n,t)=>n+t),0)/t;return r}function x(n,t){const r=2/(t+1),e=new Array(n.length).fill(NaN);e[0]=n[0];for(let t=1;t<n.length;t++)e[t]=r*n[t]+(1-r)*e[t-1];return e}function d(n,t,r=1){const e=r/t,a=new Array(n.length).fill(NaN);a[0]=n[0];for(let t=1;t<n.length;t++)a[t]=e*n[t]+(1-e)*a[t-1];return a}function k(n,t){const r=new Array(n.length).fill(NaN),e=Array.from({length:t},((n,t)=>t+1)).reduce(((n,t)=>n+t),0);for(let a=t-1;a<n.length;a++){let o=0;for(let r=0;r<t;r++)o+=n[a-r]*(t-r);r[a]=o/e}return r}function I(n,t){const r=new Array(n.length).fill(NaN);if(r[0]=n[0],"number"==typeof t)for(let e=1;e<n.length;e++)r[e]=t*n[e]+(1-t)*r[e-1];else for(let e=1;e<n.length;e++){const a=isNaN(t[e])?1:t[e];r[e]=a*n[e]+(1-a)*r[e-1]}return r}function q(n,t){const r=new Array(n.length).fill(NaN);for(let e=t-1;e<n.length;e++){const a=n.slice(e-t+1,e+1),o=a.reduce(((n,t)=>n+t),0)/t;r[e]=a.reduce(((n,t)=>n+Math.abs(t-o)),0)/t}return r}function O(n,t){return g(n.map((n=>n?1:0)),t)}function j(n,t){const r=new Array(n.length).fill(!1);for(let e=t-1;e<n.length;e++){let a=!0;for(let r=0;r<t;r++)if(!n[e-r]){a=!1;break}r[e]=a}return r}function v(n,t){const r=new Array(n.length).fill(!1);for(let e=t-1;e<n.length;e++){let a=!1;for(let r=0;r<t;r++)if(n[e-r]){a=!0;break}r[e]=a}return r}function z(n,t){const r=[...n];for(let n=0;n<r.length;n++)if(r[n])for(let e=1;e<=t&&n+e<r.length;e++)r[n+e]=0;return r}function B(n){const t=new Array(n.length).fill(0);let r=0;for(let e=0;e<n.length;e++)n[e]?r=0:r++,t[e]=r;return t}function C(n){const t=new Array(n.length).fill(0);let r=0;for(let e=0;e<n.length;e++)n[e]?r++:r=0,t[e]=r;return t}function D(n,t){const r=new Array(n.length).fill(0);for(let e=0;e<n.length;e++){if(e<t-1)continue;const a=n.slice(e-t+1,e+1).findIndex((n=>n));r[e]=-1===a?0:t-1-a}return r}function E(n,t){const r=new Array(n.length).fill(!1);for(let e=1;e<n.length;e++)r[e]=n[e]>t[e]&&n[e-1]<=t[e-1];return r}function F(n,t,r){const e=new Array(n.length).fill(!1);for(let a=r;a<n.length;a++){if(n[a]<=t[a])continue;let o=!0;for(let e=1;e<=r;e++)if(n[a-e]>=t[a-e]){o=!1;break}e[a]=o}return e}function G(n,t){const r=new Array(n.length).fill(NaN);let e=NaN;for(let a=0;a<n.length;a++)n[a]&&(e=t[a]),r[a]=e;return r}function H(n,t,r){return n.map(((n,e)=>{const a=t[e],o=r[e];return a<n&&n<o||a>n&&n>o}))}function J(n){const t=new Array(n.length).fill(0);for(let r=1;r<n.length;r++){let e=0;for(let t=r-1;t>=0&&!(n[t]>=n[r]);t--)e++;t[r]=e}return t}function K(n){const t=new Array(n.length).fill(0);for(let r=1;r<n.length;r++){let e=0;for(let t=r-1;t>=0&&!(n[t]<=n[r]);t--)e++;t[r]=e}return t}function L(t,r=12,e=26,a=9){const o=x(t,r).map(((n,r)=>n-x(t,e)[r])),l=x(o,a),u=o.map(((n,t)=>2*(n-l[t])));return[o.map((t=>n(t))),l.map((t=>n(t))),u.map((t=>n(t)))]}function P(n,t,r,e=9,a=3,o=3){const l=d(n.map(((n,a)=>{const o=y(r,e)[a];return(n-o)/(N(t,e)[a]-o)*100})),2*a-1),u=d(l,2*o-1),c=l.map(((n,t)=>3*n-2*u[t]));return[l,u,c]}function Q(t,r=24){const e=t.map(((n,r)=>0===r?0:n-t[r-1])),a=e.map((n=>Math.max(n,0))),o=e.map(Math.abs);return d(a,r).map(((t,e)=>n(t/d(o,r)[e]*100)))}function R(t,r,e,a=10,o=6){const l=t.map(((n,t)=>{const o=N(r,a)[t];return(o-n)/(o-y(e,a)[t])*100})),u=t.map(((n,t)=>{const a=N(r,o)[t];return(a-n)/(a-y(e,o)[t])*100}));return[l.map((t=>n(t))),u.map((t=>n(t)))]}function S(t,r=6,e=12,a=24){const o=b(t,r),l=b(t,e),u=b(t,a),c=t.map(((n,t)=>(n-o[t])/o[t]*100)),f=t.map(((n,t)=>(n-l[t])/l[t]*100)),m=t.map(((n,t)=>(n-u[t])/u[t]*100));return[c.map((t=>n(t))),f.map((t=>n(t))),m.map((t=>n(t)))]}function T(t,r=20,e=2){const a=b(t,r),o=s(t,r),l=a.map(((n,t)=>n+o[t]*e)),u=a.map(((n,t)=>n-o[t]*e));return[l.map((t=>n(t))),a.map((t=>n(t))),u.map((t=>n(t)))]}function U(t,r=12,e=6){const a=g(t.map(((n,r)=>0===r?0:n>t[r-1]?1:0)),r).map((n=>n/r*100)),o=b(a,e);return[a.map((t=>n(t))),o.map((t=>n(t)))]}function V(n,t,r,e=14){const a=n.map(((n,e)=>(t[e]+r[e]+n)/3)),o=b(a,e),l=q(a,e);return a.map(((n,t)=>(n-o[t])/(.015*l[t])))}function W(n,t,r,e=20){return b(n.map(((e,a)=>0===a?t[a]-r[a]:Math.max(Math.max(t[a]-r[a],Math.abs(n[a-1]-t[a])),Math.abs(n[a-1]-r[a])))),e)}function X(n,t=3,r=6,e=12,a=20){return n.map(((o,l)=>(b(n,t)[l]+b(n,r)[l]+b(n,e)[l]+b(n,a)[l])/4))}function Y(n,t,r,e=14,a=6){const o=n.map(((e,a)=>0===a?t[a]-r[a]:Math.max(Math.max(t[a]-r[a],Math.abs(t[a]-n[a-1])),Math.abs(r[a]-n[a-1])))),l=t.map(((n,r)=>0===r?0:n-t[r-1])),u=r.map(((n,t)=>0===t?0:r[t-1]-n)),c=g(l.map(((n,t)=>n>0&&n>u[t]?n:0)),e),f=g(u.map(((n,t)=>n>0&&n>l[t]?n:0)),e),m=g(o,e),i=c.map(((n,t)=>100*n/m[t])),p=f.map(((n,t)=>100*n/m[t])),h=b(i.map(((n,t)=>Math.abs(p[t]-n)/(p[t]+n)*100)),a),s=h.map(((n,t)=>t<a?n:(n+h[t-a])/2));return[i,p,h,s]}function Z(n,t,r){const e=N(n,r),a=y(t,r),o=e.map(((n,t)=>(n+a[t])/2));return[e,o,a]}function $(n,t,r,e=20,a=10){const o=x(t.map(((t,e)=>(t+r[e]+n[e])/3)),e),l=W(n,t,r,a),u=o.map(((n,t)=>n+2*l[t])),c=o.map(((n,t)=>n-2*l[t]));return[u,o,c]}function _(n,t=12,r=20){const e=x(x(x(n,t),t),t),a=e.map(((n,t)=>0===t?0:(n-e[t-1])/e[t-1]*100));return[a,b(a,r)]}function nn(n,t,r=26){const e=p(n,1),a=t.map(((t,r)=>n[r]>e[r]?t:0)),o=t.map(((t,r)=>n[r]<=e[r]?t:0));return g(a,r).map(((n,t)=>n/g(o,r)[t]*100))}function tn(n,t,r,e=20){const a=p(t.map(((t,e)=>(t+r[e]+n[e])/3)),1),o=t.map(((n,t)=>Math.max(0,n-a[t]))),l=r.map(((n,t)=>Math.max(0,a[t]-n)));return g(o,e).map(((n,t)=>n/g(l,e)[t]*100))}function rn(n,t,r,e=14,a=9){const o=b(r,e).map(((n,t)=>n/r[t])),l=b(n.map(((r,e)=>100*(r+t[e]-(0===e?r+t[e]:n[e-1]+t[e-1]))/(r+t[e]))).map(((r,a)=>r*o[a]*(n[a]-t[a])/b(n.map(((n,r)=>n-t[r])),e)[a])),e);return[l,b(l,a)]}function en(n,t=20,r=10,e=6){const a=n.map(((e,a)=>{const o=b(n,t)[a];return e-(a>=r?b(n,t)[a-r]:o)}));return[a,b(a,e)]}function an(n,t,r,e,a=26){return[g(r.map(((t,r)=>t-n[r])),a).map(((t,r)=>t/g(n.map(((n,t)=>n-e[t])),a)[r]*100)),g(r.map(((n,r)=>Math.max(0,n-p(t,1)[r]))),a).map(((n,r)=>n/g(p(t,1).map(((n,t)=>Math.max(0,n-e[t]))),a)[r]*100))]}function on(n,t=10,r=50,e=10){const a=b(n,t).map(((t,e)=>t-b(n,r)[e]));return[a,b(a,e)]}function ln(n,t=12,r=6){const e=n.map(((r,e)=>e<t?0:r-n[e-t]));return[e,b(e,r)]}function un(n,t,r=9,e=25,a=6){const o=g(b(n.map(((n,r)=>n-t[r])),r).map(((e,a)=>e/b(b(n.map(((n,r)=>n-t[r])),r),r)[a])),e);return[o,b(o,a)]}function cn(n,t=12,r=6){const e=n.map(((r,e)=>e<t?0:(r-n[e-t])/n[e-t]*100));return[e,b(e,r)]}function fn(n,t=12,r=50){return[x(n,t),x(n,r)]}function mn(n,t){const r=new Array(n.length).fill(0);r[0]=t[0];for(let e=1;e<n.length;e++)n[e]>n[e-1]?r[e]=r[e-1]+t[e]:n[e]<n[e-1]?r[e]=r[e-1]-t[e]:r[e]=r[e-1];return r.map((n=>n/1e4))}function pn(n,t,r,e,a=14){const o=t.map(((t,e)=>(t+r[e]+n[e])/3));return g(o.map(((n,t)=>0===t?0:n>o[t-1]?n*e[t]:0)),a).map(((n,t)=>n/g(o.map(((n,t)=>0===t?0:n<o[t-1]?n*e[t]:0)),a)[t])).map((n=>100-100/(1+n)))}function hn(n,t,r,e,a=26,o=10){const l=p(t,1),u=r.map(((n,t)=>Math.abs(n-l[t]))),c=e.map(((n,t)=>Math.abs(n-l[t]))),f=r.map(((n,t)=>Math.abs(n-(0===t?e[t]:e[t-1])))),m=l.map(((t,r)=>Math.abs(t-(0===r?n[r]:n[r-1])))),i=u.map(((n,t)=>n>c[t]&&n>f[t]?n+c[t]/2+m[t]/4:c[t]>f[t]&&c[t]>n?c[t]+n/2+m[t]/4:f[t]+m[t]/4)),h=g(t.map(((t,r)=>t-l[r]+(t-n[r])/2+l[r]-(0===r?n[r]:n[r-1]))).map(((n,t)=>16*n/i[t]*Math.max(u[t],c[t]))),a);return[h,b(h,o)]}function sn(n,t,r,e=102,a=7){const o=b(t.map(((t,e)=>(2*n[e]+t+r[e])/4)),5),l=o.map((n=>n*e/100)),u=o.map((n=>n*(200-e)/100)),c=n.map(((e,a)=>Math.abs((2*e+t[a]+r[a])/4-b(n,20)[a])/b(n,20)[a])),f=I(n,c);return[l,u,f.map((n=>n*(1+a/100))),f.map((n=>n*(1-a/100)))]}export{r as ABS,hn as ASI,W as ATR,q as AVEDEV,B as BARSLAST,C as BARSLASTCOUNT,D as BARSSINCEN,X as BBI,H as BETWEEN,S as BIAS,T as BOLL,an as BRAR,V as CCI,M as CONST,u as COS,O as COUNT,tn as CR,E as CROSS,on as DFMA,h as DIFF,I as DMA,Y as DMI,en as DPO,x as EMA,rn as EMV,j as EVERY,v as EXIST,fn as EXPMA,z as FILTER,N as HHV,w as HHVBARS,i as IF,P as KDJ,$ as KTN,y as LLV,A as LLVBARS,e as LN,F as LONGCROSS,K as LOWRANGE,b as MA,L as MACD,un as MASS,f as MAX,pn as MFI,m as MIN,ln as MTM,mn as OBV,a as POW,U as PSY,n as RD,p as REF,t as RET,cn as ROC,Q as RSI,l as SIN,d as SMA,o as SQRT,s as STD,g as SUM,c as TAN,Z as TAQ,J as TOPRANGE,_ as TRIX,G as VALUEWHEN,nn as VR,k as WMA,R as WR,sn as XSII};