Bitvested / ta.js

Financial Technical Analysis in JavaScript
MIT License
143 stars 33 forks source link

Fisher transform would be nice #5

Closed dspasyuk closed 3 years ago

dspasyuk commented 3 years ago

Nice library! I was wondering if it is possible to add the Fisher Transform indicator. Here is some info:

https://tulipindicators.org/fisher

Here is some code:

`/** Fisher Transform var data = [ ['2005-11-01', '82.15', '81.29', '0', '0'], ['2005-11-02', '81.89', '80.64', '0', '0'], ['2005-11-03', '83.03', '81.31', '0', '0'], ['2005-11-04', '83.30', '82.65', '0', '0'], ['2005-11-07', '83.85', '83.07', '0.34', '0.00'], ['2005-11-08', '83.90', '83.11', '0.79', '0.34'], ['2005-11-09', '83.33', '82.49', '0.83', '0.79'], ['2005-11-10', '84.30', '82.30', '0.81', '0.83'], ['2005-11-11', '84.84', '84.15', '1.07', '0.81'], ['2005-11-14', '85.00', '84.11', '1.44', '1.07'], ['2005-11-15', '85.90', '84.03', '1.85', '1.44'], ['2005-11-16', '86.58', '85.39', '2.28', '1.85'], ['2005-11-17', '86.98', '85.76', '2.70', '2.28'], ['2005-11-18', '88.00', '87.17', '3.12', '2.70'], ['2005-11-21', '87.87', '87.01', '3.19', '3.12'] ];

ta.rolling = function (a, n, clb) { var t = n || 10; for (var e=t; e <= a.length; e++) { return clb(a.slice(e - t, e)); } }

ta.hlt = function (a) { return a.map((h) => {return (parseFloat(h[1])+parseFloat(h[2]))/2}); }

ta.valt = function (hltval, i, min, max) { if (i==0){ return 0.332(((hltval[i]-min)/(max-min))-0.5); }else{ return 0.332(((hltval[i]-min)/(max-min))-0.5)+(0.670.332*(((hltval[i-1]-min)/(max-min))-0.5)); } }

ta.fishv = function (nval, i) { if (i==0){ return 0.5 Math.log((1 + nval[i]) / (1 - nval[i])); }else{ return 0.5 Math.log((1 + nval[i]) / (1 - nval[i]))+0.50.5 Math.log((1 + nval[i-1]) / (1 - nval[i-1])); } }

ta.fish = async function (a, n=5) { self = this; hltval = self.hlt(a); var min, max =null; var nval = this.rolling(hltval, n, (slice)=>{ min = Math.min.apply(Math, slice); max = Math.max.apply(Math, slice); return hltval.map((h, i)=> {return self.valt(hltval, i, min, max)}) }) console.log(nval.map((f,i) => {return self.fishv(nval, i)}));

}`

cannot make it to work for some reason, must be doing something wron.

Vultwo commented 3 years ago

I have just added the indicator

dspasyuk commented 3 years ago

WOW, this is like magic, you wished it and it appears! Thank you so much!