From 3125d9f36626bc27e0d7f23c6f5c14ffac1e4886 Mon Sep 17 00:00:00 2001 From: Bryan Ingle Date: Sun, 10 Sep 2023 16:01:36 -0600 Subject: [PATCH] Adjust `UnitCode.roundToNearestTick` to accept minimum tick value (instead of point value) --- docs/content/release_notes.md | 5 ++ docs/content/releases/6.2.1.md | 3 ++ docs/content/sdk/lib-utilities-data.md | 14 ++--- lib/utilities/data/UnitCode.js | 11 ++-- test/specs/utilities/data/UnitCodeSpec.js | 66 +++++++++++++---------- 5 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 docs/content/releases/6.2.1.md diff --git a/docs/content/release_notes.md b/docs/content/release_notes.md index 7c25cf9..85aa2d0 100644 --- a/docs/content/release_notes.md +++ b/docs/content/release_notes.md @@ -1,5 +1,10 @@ # Release Notes +## 6.2.1 +**Bug Fixes** + +* Adjusted `UnitCode.roundToNearestTick` to accept minimum tick value (instead of point value). + ## 6.2.0 **New Features** diff --git a/docs/content/releases/6.2.1.md b/docs/content/releases/6.2.1.md new file mode 100644 index 0000000..def3ba1 --- /dev/null +++ b/docs/content/releases/6.2.1.md @@ -0,0 +1,3 @@ +**Bug Fixes** + +* Adjusted `UnitCode.roundToNearestTick` to accept minimum tick value (instead of point value). \ No newline at end of file diff --git a/docs/content/sdk/lib-utilities-data.md b/docs/content/sdk/lib-utilities-data.md index c17dff3..7e5ca85 100644 --- a/docs/content/sdk/lib-utilities-data.md +++ b/docs/content/sdk/lib-utilities-data.md @@ -167,7 +167,7 @@ * [.getFractionDigits([special])](#UnitCodegetFractionDigits) ⇒ Number \| undefined * [.getMinimumTick(tickIncrement)](#UnitCodegetMinimumTick) ⇒ Number * [.getMinimumTickValue(tickIncrement, pointValue)](#UnitCodegetMinimumTickValue) ⇒ Number - * [.roundToNearestTick(value, tickIncrement, [roundToZero])](#UnitCoderoundToNearestTick) ⇒ Number + * [.roundToNearestTick(value, minimumTick, [roundToZero])](#UnitCoderoundToNearestTick) ⇒ Number * _static_ * [.parse(code)](#UnitCodeparse) ⇒ [UnitCode](#UnitCode) \| null * [.fromBaseCode(code)](#UnitCodefromBaseCode) ⇒ [UnitCode](#UnitCode) \| null @@ -318,17 +318,17 @@ * * * -### unitCode.roundToNearestTick(value, tickIncrement, [roundToZero]) :id=unitcoderoundtonearesttick +### unitCode.roundToNearestTick(value, minimumTick, [roundToZero]) :id=unitcoderoundtonearesttick > Rounds a value to the nearest valid tick. **Kind**: instance method of [UnitCode](#UnitCode) **Returns**: Number -| Param | Type | -| --- | --- | -| value | Number \| Decimal | -| tickIncrement | Number | -| [roundToZero] | Boolean | +| Param | Type | Description | +| --- | --- | --- | +| value | Number \| Decimal |

The price to round.

| +| minimumTick | Number \| Decimal |

The minimum tick size (see [getMinimumTick](#unitcodegetminimumtick))

| +| [roundToZero] | Boolean |

When true, the rounding will always go towards zero.

| * * * diff --git a/lib/utilities/data/UnitCode.js b/lib/utilities/data/UnitCode.js index d1850ea..6e732da 100644 --- a/lib/utilities/data/UnitCode.js +++ b/lib/utilities/data/UnitCode.js @@ -216,17 +216,16 @@ module.exports = (() => { /** * Rounds a value to the nearest valid tick. * - * @param {Number|Decimal} value - * @param {Number} tickIncrement - * @param {Boolean=} roundToZero + * @param {Number|Decimal} value - The price to round. + * @param {Number|Decimal} minimumTick - The minimum tick size (see {@link UnitCode#getMinimumTick}) + * @param {Boolean=} roundToZero - When true, the rounding will always go towards zero. * @returns {Number} */ - roundToNearestTick(value, tickIncrement, roundToZero) { + roundToNearestTick(value, minimumTick, roundToZero) { assert.argumentIsValid(value, 'value', x => is.number(x) || x instanceof Decimal, 'must be a number primitive or a Decimal instance'); + assert.argumentIsValid(minimumTick, 'minimumTick', x => is.number(x) || x instanceof Decimal, 'must be a number primitive or a Decimal instance'); assert.argumentIsOptional(roundToZero, 'roundToZero', Boolean); - const minimumTick = this.getMinimumTick(tickIncrement); - let valueToUse; if (value instanceof Decimal) { diff --git a/test/specs/utilities/data/UnitCodeSpec.js b/test/specs/utilities/data/UnitCodeSpec.js index cf29459..815f700 100644 --- a/test/specs/utilities/data/UnitCodeSpec.js +++ b/test/specs/utilities/data/UnitCodeSpec.js @@ -1021,153 +1021,163 @@ describe('When calculating minimum ticks and minimum tick values', () => { describe('When rounding a value to the nearest tick value', () => { describe('For unit code "2" and with a tickIncrement of 2 (e.g. corn)', () => { let uc; + let mt; beforeEach(() => { uc = UnitCode.parse('2'); + mt = uc.getMinimumTick(2); }); it('A value of 0 should be rounded to 0', () => { - expect(uc.roundToNearestTick(0, 2)).toEqual(0); + expect(uc.roundToNearestTick(0, mt)).toEqual(0); }); it('A value of 488.5 should be rounded to 488.5', () => { - expect(uc.roundToNearestTick(488.5, 2)).toEqual(488.5); + expect(uc.roundToNearestTick(488.5, mt)).toEqual(488.5); }); it('A value of 488.51 should be rounded to 488.5', () => { - expect(uc.roundToNearestTick(488.51, 2)).toEqual(488.5); + expect(uc.roundToNearestTick(488.51, mt)).toEqual(488.5); }); it('A value of 488.74 should be rounded to 488.75', () => { - expect(uc.roundToNearestTick(488.74, 2)).toEqual(488.75); + expect(uc.roundToNearestTick(488.74, mt)).toEqual(488.75); }); it('A value of 488.625 should be rounded to 488.75', () => { - expect(uc.roundToNearestTick(488.625, 2)).toEqual(488.75); + expect(uc.roundToNearestTick(488.625, mt)).toEqual(488.75); }); it('A value of 488.625 should be rounded to 488.5 (when using "roundDown" option)', () => { - expect(uc.roundToNearestTick(488.625, 2, true)).toEqual(488.5); + expect(uc.roundToNearestTick(488.625, mt, true)).toEqual(488.5); }); }); describe('For unit code "A" and with a tickIncrement of 25 (e.g. e-mini)', () => { let uc; + let mt; beforeEach(() => { uc = UnitCode.parse('A'); + mt = uc.getMinimumTick(25); }); it('A value of 4455.5 should be rounded to 4455.5', () => { - expect(uc.roundToNearestTick(4455.5, 25)).toEqual(4455.5); + expect(uc.roundToNearestTick(4455.5, mt)).toEqual(4455.5); }); it('A value of 4455.51 should be rounded to 4455.5', () => { - expect(uc.roundToNearestTick(4455.51, 25)).toEqual(4455.5); + expect(uc.roundToNearestTick(4455.51, mt)).toEqual(4455.5); }); it('A value of 4455.74 should be rounded to 4455.75', () => { - expect(uc.roundToNearestTick(4455.74, 25)).toEqual(4455.75); + expect(uc.roundToNearestTick(4455.74, mt)).toEqual(4455.75); }); it('A value of 4455.625 should be rounded to 4455.75', () => { - expect(uc.roundToNearestTick(4455.625, 25)).toEqual(4455.75); + expect(uc.roundToNearestTick(4455.625, mt)).toEqual(4455.75); }); it('A value of 4455.625 should be rounded to 4455.5 (when using "roundDown" option)', () => { - expect(uc.roundToNearestTick(4455.625, 25, true)).toEqual(4455.5); + expect(uc.roundToNearestTick(4455.625, mt, true)).toEqual(4455.5); }); }); describe('For unit code "A" and with a tickIncrement of 1 (e.g. crude)', () => { let uc; + let mt; beforeEach(() => { uc = UnitCode.parse('A'); + mt = uc.getMinimumTick(1); }); it('A value of 87.30 should be rounded to 87.30', () => { - expect(uc.roundToNearestTick(87.30, 1)).toEqual(87.30); + expect(uc.roundToNearestTick(87.30, mt)).toEqual(87.30); }); it('A value of 87.31 should be rounded to 87.31', () => { - expect(uc.roundToNearestTick(87.31, 1)).toEqual(87.31); + expect(uc.roundToNearestTick(87.31, mt)).toEqual(87.31); }); it('A value of 87.312 should be rounded to 87.31', () => { - expect(uc.roundToNearestTick(87.312, 1)).toEqual(87.31); + expect(uc.roundToNearestTick(87.312, mt)).toEqual(87.31); }); it('A value of 87.318 should be rounded to 87.32', () => { - expect(uc.roundToNearestTick(87.318, 1)).toEqual(87.32); + expect(uc.roundToNearestTick(87.318, mt)).toEqual(87.32); }); it('A value of 87.325 should be rounded to 87.33', () => { - expect(uc.roundToNearestTick(87.325, 1)).toEqual(87.33); + expect(uc.roundToNearestTick(87.325, mt)).toEqual(87.33); }); it('A value of 87.325 should be rounded to 87.32 (when using "roundDown" option)', () => { - expect(uc.roundToNearestTick(87.325, 1, true)).toEqual(87.32); + expect(uc.roundToNearestTick(87.325, mt, true)).toEqual(87.32); }); }); describe('For unit code "9" and with a tickIncrement of 1 (e.g. gold)', () => { let uc; + let mt; beforeEach(() => { uc = UnitCode.parse('9'); + mt = uc.getMinimumTick(1); }); it('A value of 1922.5 should be rounded to 1922.5', () => { - expect(uc.roundToNearestTick(1922.5, 1)).toEqual(1922.5); + expect(uc.roundToNearestTick(1922.5, mt)).toEqual(1922.5); }); it('A value of 1922.6 should be rounded to 1922.6', () => { - expect(uc.roundToNearestTick(1922.6, 1)).toEqual(1922.6); + expect(uc.roundToNearestTick(1922.6, mt)).toEqual(1922.6); }); it('A value of 1922.51 should be rounded to 1922.5', () => { - expect(uc.roundToNearestTick(1922.51, 1)).toEqual(1922.5); + expect(uc.roundToNearestTick(1922.51, mt)).toEqual(1922.5); }); it('A value of 1922.59 should be rounded to 1922.6', () => { - expect(uc.roundToNearestTick(1922.59, 1)).toEqual(1922.6); + expect(uc.roundToNearestTick(1922.59, mt)).toEqual(1922.6); }); it('A value of 1922.55 should be rounded to 1922.6', () => { - expect(uc.roundToNearestTick(1922.55, 1)).toEqual(1922.6); + expect(uc.roundToNearestTick(1922.55, mt)).toEqual(1922.6); }); it('A value of 1922.55 should be rounded to 1922.5 (when using "roundDown" option)', () => { - expect(uc.roundToNearestTick(1922.55, 1, true)).toEqual(1922.5); + expect(uc.roundToNearestTick(1922.55, mt, true)).toEqual(1922.5); }); }); describe('For unit code "5" and with a tickIncrement of 1 (e.g. t-notes)', () => { let uc; + let mt; beforeEach(() => { uc = UnitCode.parse('5'); + mt = uc.getMinimumTick(1); }); it('A value of 110.328125 should be rounded to 110.328125', () => { - expect(uc.roundToNearestTick(110.328125, 1)).toEqual(110.328125); + expect(uc.roundToNearestTick(110.328125, mt)).toEqual(110.328125); }); it('A value of 110.34375 should be rounded to 110.34375', () => { - expect(uc.roundToNearestTick(110.34375, 1)).toEqual(110.34375); + expect(uc.roundToNearestTick(110.34375, mt)).toEqual(110.34375); }); it('A value of 110.328126 should be rounded to 110.328125', () => { - expect(uc.roundToNearestTick(110.328126, 1)).toEqual(110.328125); + expect(uc.roundToNearestTick(110.328126, mt)).toEqual(110.328125); }); it('A value of 110.34374 should be rounded to 110.34375', () => { - expect(uc.roundToNearestTick(110.34374, 1)).toEqual(110.34375); + expect(uc.roundToNearestTick(110.34374, mt)).toEqual(110.34375); }); it('A value of 110.34374 should be rounded to 110.328125 (when using "roundDown" option)', () => { - expect(uc.roundToNearestTick(110.34374, 1, true)).toEqual(110.328125); + expect(uc.roundToNearestTick(110.34374, mt, true)).toEqual(110.328125); }); }); });