'use strict'; import chai from 'chai'; const assert = chai.assert; import _ from 'underscore'; import ROC from '../../lib/indicator/parabolic-stop-and-reverse.js'; describe('Parabolic Stop and Reverse', () => { let data_uptrend = [ //high, low // [47.85, 47.48 ], // [47.83, 47.55], // [47.95, 47.32 ], [48.11, 47.25], [48.30, 47.77], [48.17, 47.91], [48.60, 47.90], [48.33, 47.74], [48.40, 48.10], [48.55, 48.06], [48.45, 48.07], [48.70, 47.79], // [48.72, 48.14 ], // [48.90, 48.39 ], // [48.87, 48.37 ], // [48.82, 48.24 ], // [49.05, 48.64 ], // [49.20, 48.94 ], // [49.35, 48.86 ], ]; let expectedResults_uptrend = [ { sar: 47.25, ep: 48.30, ep_sar: 1.05, af: 0.02, af_ep_sar: 0.02 }, { sar: 47.25, ep: 48.30, ep_sar: 1.05, af: 0.02, af_ep_sar: 0.02 }, { sar: 47.27, ep: 48.60, ep_sar: 1.33, af: 0.04, af_ep_sar: 0.05 }, { sar: 47.32, ep: 48.60, ep_sar: 1.28, af: 0.04, af_ep_sar: 0.05 }, { sar: 47.38, ep: 48.60, ep_sar: 1.22, af: 0.04, af_ep_sar: 0.05 }, { sar: 47.42, ep: 48.60, ep_sar: 1.18, af: 0.04, af_ep_sar: 0.05 }, { sar: 47.47, ep: 48.60, ep_sar: 1.13, af: 0.04, af_ep_sar: 0.05 }, { sar: 47.52, ep: 48.70, ep_sar: 1.18, af: 0.06, af_ep_sar: 0.07 }, ]; let data_downtrend = [ [46.59, 45.90], [46.55, 45.38], [46.30, 45.25], [45.43, 43.99], [44.55, 44.07], [44.84, 44.00], [44.80, 43.96], [44.38, 43.27], ]; let expectedResults_downtrend = [ { sar: 46.59, ep: 45.38, sar_ep: 1.21, af: 0.02, af_sar_ep: 0.024 }, { sar: 46.56, ep: 45.25, sar_ep: 1.31, af: 0.04, af_sar_ep: 0.054 }, { sar: 46.51, ep: 43.99, sar_ep: 2.52, af: 0.06, af_sar_ep: 0.154 }, { sar: 46.36, ep: 43.99, sar_ep: 2.37, af: 0.06, af_sar_ep: 0.144 }, { sar: 46.22, ep: 43.99, sar_ep: 2.23, af: 0.06, af_sar_ep: 0.136 }, { sar: 46.10, ep: 43.96, sar_ep: 2.14, af: 0.08, af_sar_ep: 0.173 }, { sar: 45.93, ep: 43.27, sar_ep: 2.66, af: 0.10, af_sar_ep: 0.267 }, ]; let runTest = (collection, expectedResults, direction, options = {}) => { let roc = new ROC(options); roc.setValues(collection); let results = roc.calculate(direction); assert.isArray(results); assert.isTrue(results.length == expectedResults.length); results.forEach((result, idx) => { assert.closeTo(result.sar, expectedResults[idx].sar, 0.03); assert.closeTo(result.ep, expectedResults[idx].ep, 0.02); assert.closeTo(result.af, expectedResults[idx].af, 0.02); if (direction == 'uptrend') { assert.closeTo(result.ep_sar, expectedResults[idx].ep_sar, 0.03); assert.closeTo(result.af_ep_sar, expectedResults[idx].af_ep_sar, 0.02); } if (direction == 'downtrend') { assert.closeTo(result.sar_ep, expectedResults[idx].sar_ep, 0.03); assert.closeTo(result.af_sar_ep, expectedResults[idx].af_sar_ep, 0.02); } }); } let arr2Collection = (data) => { return data.map((arr) => { return { high: arr[0], low: arr[1] }; }); }; it('should calculate uptrend correctly and return results', () => { runTest(arr2Collection(data_uptrend), expectedResults_uptrend, 'uptrend'); }); it('should calculate uptrend correctly within range and return results', () => { let dataCopy = [...data_uptrend]; dataCopy.unshift([48.05, 47.10]); dataCopy.push([48.99, 47.99]); runTest(arr2Collection(dataCopy), expectedResults_uptrend, 'uptrend', { startIndex: 1, endIndex: dataCopy.length - 2 }); }); it('should calculate downtrend correctly and return results', () => { runTest(arr2Collection(data_downtrend), expectedResults_downtrend, 'downtrend'); }); it('should calculate downtrend correctly within range and return results', () => { let dataCopy = [...data_downtrend]; dataCopy.unshift([48.05, 47.10]); dataCopy.push([48.99, 47.99]); runTest(arr2Collection(dataCopy), expectedResults_downtrend, 'downtrend', { startIndex: 1, endIndex: dataCopy.length - 2 }); }); it('should just test direction', () => { let roc = new ROC(); roc.setValues(arr2Collection([...data_downtrend])); let results = roc.calculate('uptrend'); console.log(results); }); });