mytt-ts
Version:
通达信、同花顺、文华麦语言等指标公式的 TypeScript 实现
2 lines (1 loc) • 10.3 kB
JavaScript
;function t(t,n=3){return Number(Math.round(Number(t+"e"+n))+"e-"+n)}function n(t,n=1){const r=new Array(t.length).fill(NaN);for(let e=n;e<t.length;e++)r[e]=t[e-n];return r}function r(t,n){const r=new Array(t.length).fill(NaN);for(let e=n-1;e<t.length;e++){const o=t.slice(e-n+1,e+1),a=o.reduce(((t,n)=>t+n),0)/n,p=o.reduce(((t,n)=>t+Math.pow(n-a,2)),0);r[e]=Math.sqrt(p/n)}return r}function e(t,n){const r=new Array(t.length).fill(NaN);if(n<=0){let n=0;return t.map((t=>n+=t))}for(let e=0;e<t.length;e++)e<n-1||(r[e]=t.slice(e-n+1,e+1).reduce(((t,n)=>t+n),0));return r}function o(t,n){const r=new Array(t.length).fill(NaN);for(let e=0;e<t.length;e++)e<n-1||(r[e]=Math.max(...t.slice(e-n+1,e+1)));return r}function a(t,n){const r=new Array(t.length).fill(NaN);for(let e=0;e<t.length;e++)e<n-1||(r[e]=Math.min(...t.slice(e-n+1,e+1)));return r}function p(t,n){const r=new Array(t.length).fill(NaN);for(let e=n-1;e<t.length;e++)r[e]=t.slice(e-n+1,e+1).reduce(((t,n)=>t+n),0)/n;return r}function l(t,n){const r=2/(n+1),e=new Array(t.length).fill(NaN);e[0]=t[0];for(let n=1;n<t.length;n++)e[n]=r*t[n]+(1-r)*e[n-1];return e}function s(t,n,r=1){const e=r/n,o=new Array(t.length).fill(NaN);o[0]=t[0];for(let n=1;n<t.length;n++)o[n]=e*t[n]+(1-e)*o[n-1];return o}function u(t,n){const r=new Array(t.length).fill(NaN);if(r[0]=t[0],"number"==typeof n)for(let e=1;e<t.length;e++)r[e]=n*t[e]+(1-n)*r[e-1];else for(let e=1;e<t.length;e++){const o=isNaN(n[e])?1:n[e];r[e]=o*t[e]+(1-o)*r[e-1]}return r}function c(t,n){const r=new Array(t.length).fill(NaN);for(let e=n-1;e<t.length;e++){const o=t.slice(e-n+1,e+1),a=o.reduce(((t,n)=>t+n),0)/n;r[e]=o.reduce(((t,n)=>t+Math.abs(n-a)),0)/n}return r}function f(t,n,r,e=20){return p(t.map(((e,o)=>0===o?n[o]-r[o]:Math.max(Math.max(n[o]-r[o],Math.abs(t[o-1]-n[o])),Math.abs(t[o-1]-r[o])))),e)}exports.ABS=function(t){return t.map(Math.abs)},exports.ASI=function(t,r,o,a,l=26,s=10){const u=n(r,1),c=o.map(((t,n)=>Math.abs(t-u[n]))),f=a.map(((t,n)=>Math.abs(t-u[n]))),m=o.map(((t,n)=>Math.abs(t-(0===n?a[n]:a[n-1])))),i=u.map(((n,r)=>Math.abs(n-(0===r?t[r]:t[r-1])))),h=c.map(((t,n)=>t>f[n]&&t>m[n]?t+f[n]/2+i[n]/4:f[n]>m[n]&&f[n]>t?f[n]+t/2+i[n]/4:m[n]+i[n]/4)),x=e(r.map(((n,r)=>n-u[r]+(n-t[r])/2+u[r]-(0===r?t[r]:t[r-1]))).map(((t,n)=>16*t/h[n]*Math.max(c[n],f[n]))),l);return[x,p(x,s)]},exports.ATR=f,exports.AVEDEV=c,exports.BARSLAST=function(t){const n=new Array(t.length).fill(0);let r=0;for(let e=0;e<t.length;e++)t[e]?r=0:r++,n[e]=r;return n},exports.BARSLASTCOUNT=function(t){const n=new Array(t.length).fill(0);let r=0;for(let e=0;e<t.length;e++)t[e]?r++:r=0,n[e]=r;return n},exports.BARSSINCEN=function(t,n){const r=new Array(t.length).fill(0);for(let e=0;e<t.length;e++){if(e<n-1)continue;const o=t.slice(e-n+1,e+1).findIndex((t=>t));r[e]=-1===o?0:n-1-o}return r},exports.BBI=function(t,n=3,r=6,e=12,o=20){return t.map(((a,l)=>(p(t,n)[l]+p(t,r)[l]+p(t,e)[l]+p(t,o)[l])/4))},exports.BETWEEN=function(t,n,r){return t.map(((t,e)=>{const o=n[e],a=r[e];return o<t&&t<a||o>t&&t>a}))},exports.BIAS=function(n,r=6,e=12,o=24){const a=p(n,r),l=p(n,e),s=p(n,o),u=n.map(((t,n)=>(t-a[n])/a[n]*100)),c=n.map(((t,n)=>(t-l[n])/l[n]*100)),f=n.map(((t,n)=>(t-s[n])/s[n]*100));return[u.map((n=>t(n))),c.map((n=>t(n))),f.map((n=>t(n)))]},exports.BOLL=function(n,e=20,o=2){const a=p(n,e),l=r(n,e),s=a.map(((t,n)=>t+l[n]*o)),u=a.map(((t,n)=>t-l[n]*o));return[s.map((n=>t(n))),a.map((n=>t(n))),u.map((n=>t(n)))]},exports.BRAR=function(t,r,o,a,p=26){return[e(o.map(((n,r)=>n-t[r])),p).map(((n,r)=>n/e(t.map(((t,n)=>t-a[n])),p)[r]*100)),e(o.map(((t,e)=>Math.max(0,t-n(r,1)[e]))),p).map(((t,o)=>t/e(n(r,1).map(((t,n)=>Math.max(0,t-a[n]))),p)[o]*100))]},exports.CCI=function(t,n,r,e=14){const o=t.map(((t,e)=>(n[e]+r[e]+t)/3)),a=p(o,e),l=c(o,e);return o.map(((t,n)=>(t-a[n])/(.015*l[n])))},exports.CONST=function(t){const n=t[t.length-1];return new Array(t.length).fill(n)},exports.COS=function(t){return t.map(Math.cos)},exports.COUNT=function(t,n){return e(t.map((t=>t?1:0)),n)},exports.CR=function(t,r,o,a=20){const p=n(r.map(((n,r)=>(n+o[r]+t[r])/3)),1),l=r.map(((t,n)=>Math.max(0,t-p[n]))),s=o.map(((t,n)=>Math.max(0,p[n]-t)));return e(l,a).map(((t,n)=>t/e(s,a)[n]*100))},exports.CROSS=function(t,n){const r=new Array(t.length).fill(!1);for(let e=1;e<t.length;e++)r[e]=t[e]>n[e]&&t[e-1]<=n[e-1];return r},exports.DFMA=function(t,n=10,r=50,e=10){const o=p(t,n).map(((n,e)=>n-p(t,r)[e]));return[o,p(o,e)]},exports.DIFF=function(t,n=1){const r=new Array(t.length).fill(NaN);for(let e=n;e<t.length;e++)r[e]=t[e]-t[e-n];return r},exports.DMA=u,exports.DMI=function(t,n,r,o=14,a=6){const l=t.map(((e,o)=>0===o?n[o]-r[o]:Math.max(Math.max(n[o]-r[o],Math.abs(n[o]-t[o-1])),Math.abs(r[o]-t[o-1])))),s=n.map(((t,r)=>0===r?0:t-n[r-1])),u=r.map(((t,n)=>0===n?0:r[n-1]-t)),c=e(s.map(((t,n)=>t>0&&t>u[n]?t:0)),o),f=e(u.map(((t,n)=>t>0&&t>s[n]?t:0)),o),m=e(l,o),i=c.map(((t,n)=>100*t/m[n])),h=f.map(((t,n)=>100*t/m[n])),x=p(i.map(((t,n)=>Math.abs(h[n]-t)/(h[n]+t)*100)),a),g=x.map(((t,n)=>n<a?t:(t+x[n-a])/2));return[i,h,x,g]},exports.DPO=function(t,n=20,r=10,e=6){const o=t.map(((e,o)=>{const a=p(t,n)[o];return e-(o>=r?p(t,n)[o-r]:a)}));return[o,p(o,e)]},exports.EMA=l,exports.EMV=function(t,n,r,e=14,o=9){const a=p(r,e).map(((t,n)=>t/r[n])),l=p(t.map(((r,e)=>100*(r+n[e]-(0===e?r+n[e]:t[e-1]+n[e-1]))/(r+n[e]))).map(((r,o)=>r*a[o]*(t[o]-n[o])/p(t.map(((t,r)=>t-n[r])),e)[o])),e);return[l,p(l,o)]},exports.EVERY=function(t,n){const r=new Array(t.length).fill(!1);for(let e=n-1;e<t.length;e++){let o=!0;for(let r=0;r<n;r++)if(!t[e-r]){o=!1;break}r[e]=o}return r},exports.EXIST=function(t,n){const r=new Array(t.length).fill(!1);for(let e=n-1;e<t.length;e++){let o=!1;for(let r=0;r<n;r++)if(t[e-r]){o=!0;break}r[e]=o}return r},exports.EXPMA=function(t,n=12,r=50){return[l(t,n),l(t,r)]},exports.FILTER=function(t,n){const r=[...t];for(let t=0;t<r.length;t++)if(r[t])for(let e=1;e<=n&&t+e<r.length;e++)r[t+e]=0;return r},exports.HHV=o,exports.HHVBARS=function(t,n){const r=new Array(t.length).fill(NaN);for(let e=n-1;e<t.length;e++){const o=t.slice(e-n+1,e+1),a=Math.max(...o);r[e]=o.length-1-o.lastIndexOf(a)}return r},exports.IF=function(t,n,r){const e="number"==typeof n,o="number"==typeof r;return t.map(((t,a)=>{const p=e?n:n[a],l=o?r:r[a];return t?p:l}))},exports.KDJ=function(t,n,r,e=9,p=3,l=3){const u=s(t.map(((t,p)=>{const l=a(r,e)[p];return(t-l)/(o(n,e)[p]-l)*100})),2*p-1),c=s(u,2*l-1),f=u.map(((t,n)=>3*t-2*c[n]));return[u,c,f]},exports.KTN=function(t,n,r,e=20,o=10){const a=l(n.map(((n,e)=>(n+r[e]+t[e])/3)),e),p=f(t,n,r,o),s=a.map(((t,n)=>t+2*p[n])),u=a.map(((t,n)=>t-2*p[n]));return[s,a,u]},exports.LLV=a,exports.LLVBARS=function(t,n){const r=new Array(t.length).fill(NaN);for(let e=n-1;e<t.length;e++){const o=t.slice(e-n+1,e+1),a=Math.min(...o);r[e]=o.length-1-o.lastIndexOf(a)}return r},exports.LN=function(t){return t.map(Math.log)},exports.LONGCROSS=function(t,n,r){const e=new Array(t.length).fill(!1);for(let o=r;o<t.length;o++){if(t[o]<=n[o])continue;let a=!0;for(let e=1;e<=r;e++)if(t[o-e]>=n[o-e]){a=!1;break}e[o]=a}return e},exports.LOWRANGE=function(t){const n=new Array(t.length).fill(0);for(let r=1;r<t.length;r++){let e=0;for(let n=r-1;n>=0&&!(t[n]<=t[r]);n--)e++;n[r]=e}return n},exports.MA=p,exports.MACD=function(n,r=12,e=26,o=9){const a=l(n,r).map(((t,r)=>t-l(n,e)[r])),p=l(a,o),s=a.map(((t,n)=>2*(t-p[n])));return[a.map((n=>t(n))),p.map((n=>t(n))),s.map((n=>t(n)))]},exports.MASS=function(t,n,r=9,o=25,a=6){const l=e(p(t.map(((t,r)=>t-n[r])),r).map(((e,o)=>e/p(p(t.map(((t,r)=>t-n[r])),r),r)[o])),o);return[l,p(l,a)]},exports.MAX=function(t,n){return t.map(((t,r)=>Math.max(t,n[r])))},exports.MFI=function(t,n,r,o,a=14){const p=n.map(((n,e)=>(n+r[e]+t[e])/3));return e(p.map(((t,n)=>0===n?0:t>p[n-1]?t*o[n]:0)),a).map(((t,n)=>t/e(p.map(((t,n)=>0===n?0:t<p[n-1]?t*o[n]:0)),a)[n])).map((t=>100-100/(1+t)))},exports.MIN=function(t,n){return t.map(((t,r)=>Math.min(t,n[r])))},exports.MTM=function(t,n=12,r=6){const e=t.map(((r,e)=>e<n?0:r-t[e-n]));return[e,p(e,r)]},exports.OBV=function(t,n){const r=new Array(t.length).fill(0);r[0]=n[0];for(let e=1;e<t.length;e++)t[e]>t[e-1]?r[e]=r[e-1]+n[e]:t[e]<t[e-1]?r[e]=r[e-1]-n[e]:r[e]=r[e-1];return r.map((t=>t/1e4))},exports.POW=function(t,n){return t.map((t=>Math.pow(t,n)))},exports.PSY=function(n,r=12,o=6){const a=e(n.map(((t,r)=>0===r?0:t>n[r-1]?1:0)),r).map((t=>t/r*100)),l=p(a,o);return[a.map((n=>t(n))),l.map((n=>t(n)))]},exports.RD=t,exports.REF=n,exports.RET=function(t,n=1){return t[t.length-n]},exports.ROC=function(t,n=12,r=6){const e=t.map(((r,e)=>e<n?0:(r-t[e-n])/t[e-n]*100));return[e,p(e,r)]},exports.RSI=function(n,r=24){const e=n.map(((t,r)=>0===r?0:t-n[r-1])),o=e.map((t=>Math.max(t,0))),a=e.map(Math.abs);return s(o,r).map(((n,e)=>t(n/s(a,r)[e]*100)))},exports.SIN=function(t){return t.map(Math.sin)},exports.SMA=s,exports.SQRT=function(t){return t.map(Math.sqrt)},exports.STD=r,exports.SUM=e,exports.TAN=function(t){return t.map(Math.tan)},exports.TAQ=function(t,n,r){const e=o(t,r),p=a(n,r),l=e.map(((t,n)=>(t+p[n])/2));return[e,l,p]},exports.TOPRANGE=function(t){const n=new Array(t.length).fill(0);for(let r=1;r<t.length;r++){let e=0;for(let n=r-1;n>=0&&!(t[n]>=t[r]);n--)e++;n[r]=e}return n},exports.TRIX=function(t,n=12,r=20){const e=l(l(l(t,n),n),n),o=e.map(((t,n)=>0===n?0:(t-e[n-1])/e[n-1]*100));return[o,p(o,r)]},exports.VALUEWHEN=function(t,n){const r=new Array(t.length).fill(NaN);let e=NaN;for(let o=0;o<t.length;o++)t[o]&&(e=n[o]),r[o]=e;return r},exports.VR=function(t,r,o=26){const a=n(t,1),p=r.map(((n,r)=>t[r]>a[r]?n:0)),l=r.map(((n,r)=>t[r]<=a[r]?n:0));return e(p,o).map(((t,n)=>t/e(l,o)[n]*100))},exports.WMA=function(t,n){const r=new Array(t.length).fill(NaN),e=Array.from({length:n},((t,n)=>n+1)).reduce(((t,n)=>t+n),0);for(let o=n-1;o<t.length;o++){let a=0;for(let r=0;r<n;r++)a+=t[o-r]*(n-r);r[o]=a/e}return r},exports.WR=function(n,r,e,p=10,l=6){const s=n.map(((t,n)=>{const l=o(r,p)[n];return(l-t)/(l-a(e,p)[n])*100})),u=n.map(((t,n)=>{const p=o(r,l)[n];return(p-t)/(p-a(e,l)[n])*100}));return[s.map((n=>t(n))),u.map((n=>t(n)))]},exports.XSII=function(t,n,r,e=102,o=7){const a=p(n.map(((n,e)=>(2*t[e]+n+r[e])/4)),5),l=a.map((t=>t*e/100)),s=a.map((t=>t*(200-e)/100)),c=t.map(((e,o)=>Math.abs((2*e+n[o]+r[o])/4-p(t,20)[o])/p(t,20)[o])),f=u(t,c);return[l,s,f.map((t=>t*(1+o/100))),f.map((t=>t*(1-o/100)))]};