all files / technicalindicators/lib/candlestick/ CandlestickFinder.js

66.67% Statements 32/48
41.67% Branches 5/12
85.71% Functions 6/7
66.67% Lines 32/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                12×                                                 12×   11×           11× 11×       13× 13× 12×                         11× 11×           11× 33× 33× 33× 33×     11× 11×      
//data :
// {
//  open : [],
//  high : [],
//  low : [],
//  close : []
// }
class CandlestickFinder {
 
    constructor() {
        // if (new.target === Abstract) {
        //     throw new TypeError("Abstract class");
        // }
    }
 
    approximateEqual(a, b) {
        return (((Math.abs(a - b)).toPrecision(4) * 1) <=
                ((a * 0.001).toPrecision(4)) * 1)
    }
 
    getAllPatternIndex (data) {
        if(data.close.length < this.requiredCount) {
            console.warn('Data count less than data required for the strategy ', this.name);
            return [];
        }
        if(data.reversedInput) {
            data.open.reverse();
            data.high.reverse();
            data.low.reverse();
            data.close.reverse();
        }
        let strategyFn = this.logic;
        return this._generateDataForCandleStick(data)
                .map((current, index) => {
                            return strategyFn.call(this, current) ? index : undefined;
                        }).filter((hasIndex) => {
                            return hasIndex;
                        });
    }
 
    hasPattern (data) {
        if(data.close.length < this.requiredCount) {
            console.warn('Data count less than data required for the strategy ', this.name);
            return false;
        }
        if(Idata.reversedInput) {
            data.open.reverse();
            data.high.reverse();
            data.low.reverse();
            data.close.reverse();
        }
        let strategyFn = this.logic;
        return strategyFn.call(this, this._getLastDataForCandleStick(data));
    }
 
    _getLastDataForCandleStick(data) {
        let requiredCount = this.requiredCount;
        if (data.close.length === requiredCount) {
            return data;
        } else {
            let returnVal = {
                open : [],
                high : [],
                low  : [],
                close: []
            };
            let i = 0;
            let index = data.close.length - requiredCount;
            while (i < requiredCount) {
                returnVal.open.push(data.open[index + i]);
                returnVal.high.push(data.high[index + i]);
                returnVal.low.push(data.low[index + i]);
                returnVal.close.push(data.close[index + i]);
                i++;
            }
            return returnVal;
        }
    }
 
    _generateDataForCandleStick (data) {
            let requiredCount = this.requiredCount;
            let generatedData = data.close.map(function(currentData, index) {
                let i = 0;
                let returnVal = {
                    open : [],
                    high : [],
                    low  : [],
                    close: []
                };
                while(i < requiredCount) {
                    returnVal.open.push(data.open[index + i]);
                    returnVal.high.push(data.high[index + i]);
                    returnVal.low.push(data.low[index + i]);
                    returnVal.close.push(data.close[index + i]);
                    i++;
                }
                return returnVal;
            }).filter((val, index) => { return (index <= (data.close.length - requiredCount))  });
            return generatedData;
    }
}
 
module.exports = CandlestickFinder