Skip to content

Commit 3125d9f

Browse files
committed
Adjust UnitCode.roundToNearestTick to accept minimum tick value (instead of point value)
1 parent 7e05552 commit 3125d9f

File tree

5 files changed

+58
-41
lines changed

5 files changed

+58
-41
lines changed

docs/content/release_notes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release Notes
22

3+
## 6.2.1
4+
**Bug Fixes**
5+
6+
* Adjusted `UnitCode.roundToNearestTick` to accept minimum tick value (instead of point value).
7+
38
## 6.2.0
49
**New Features**
510

docs/content/releases/6.2.1.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**Bug Fixes**
2+
3+
* Adjusted `UnitCode.roundToNearestTick` to accept minimum tick value (instead of point value).

docs/content/sdk/lib-utilities-data.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
* [.getFractionDigits([special])](#UnitCodegetFractionDigits) ⇒ <code>Number</code> \| <code>undefined</code>
168168
* [.getMinimumTick(tickIncrement)](#UnitCodegetMinimumTick) ⇒ <code>Number</code>
169169
* [.getMinimumTickValue(tickIncrement, pointValue)](#UnitCodegetMinimumTickValue) ⇒ <code>Number</code>
170-
* [.roundToNearestTick(value, tickIncrement, [roundToZero])](#UnitCoderoundToNearestTick) ⇒ <code>Number</code>
170+
* [.roundToNearestTick(value, minimumTick, [roundToZero])](#UnitCoderoundToNearestTick) ⇒ <code>Number</code>
171171
* _static_
172172
* [.parse(code)](#UnitCodeparse)[<code>UnitCode</code>](#UnitCode) \| <code>null</code>
173173
* [.fromBaseCode(code)](#UnitCodefromBaseCode)[<code>UnitCode</code>](#UnitCode) \| <code>null</code>
@@ -318,17 +318,17 @@
318318

319319
* * *
320320

321-
### unitCode.roundToNearestTick(value, tickIncrement, [roundToZero]) :id=unitcoderoundtonearesttick
321+
### unitCode.roundToNearestTick(value, minimumTick, [roundToZero]) :id=unitcoderoundtonearesttick
322322
> Rounds a value to the nearest valid tick.
323323
324324
**Kind**: instance method of [<code>UnitCode</code>](#UnitCode)
325325
**Returns**: <code>Number</code>
326326

327-
| Param | Type |
328-
| --- | --- |
329-
| value | <code>Number</code> \| <code>Decimal</code> |
330-
| tickIncrement | <code>Number</code> |
331-
| [roundToZero] | <code>Boolean</code> |
327+
| Param | Type | Description |
328+
| --- | --- | --- |
329+
| value | <code>Number</code> \| <code>Decimal</code> | <p>The price to round.</p> |
330+
| minimumTick | <code>Number</code> \| <code>Decimal</code> | <p>The minimum tick size (see [getMinimumTick](#unitcodegetminimumtick))</p> |
331+
| [roundToZero] | <code>Boolean</code> | <p>When true, the rounding will always go towards zero.</p> |
332332

333333

334334
* * *

lib/utilities/data/UnitCode.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,16 @@ module.exports = (() => {
216216
/**
217217
* Rounds a value to the nearest valid tick.
218218
*
219-
* @param {Number|Decimal} value
220-
* @param {Number} tickIncrement
221-
* @param {Boolean=} roundToZero
219+
* @param {Number|Decimal} value - The price to round.
220+
* @param {Number|Decimal} minimumTick - The minimum tick size (see {@link UnitCode#getMinimumTick})
221+
* @param {Boolean=} roundToZero - When true, the rounding will always go towards zero.
222222
* @returns {Number}
223223
*/
224-
roundToNearestTick(value, tickIncrement, roundToZero) {
224+
roundToNearestTick(value, minimumTick, roundToZero) {
225225
assert.argumentIsValid(value, 'value', x => is.number(x) || x instanceof Decimal, 'must be a number primitive or a Decimal instance');
226+
assert.argumentIsValid(minimumTick, 'minimumTick', x => is.number(x) || x instanceof Decimal, 'must be a number primitive or a Decimal instance');
226227
assert.argumentIsOptional(roundToZero, 'roundToZero', Boolean);
227228

228-
const minimumTick = this.getMinimumTick(tickIncrement);
229-
230229
let valueToUse;
231230

232231
if (value instanceof Decimal) {

test/specs/utilities/data/UnitCodeSpec.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,153 +1021,163 @@ describe('When calculating minimum ticks and minimum tick values', () => {
10211021
describe('When rounding a value to the nearest tick value', () => {
10221022
describe('For unit code "2" and with a tickIncrement of 2 (e.g. corn)', () => {
10231023
let uc;
1024+
let mt;
10241025

10251026
beforeEach(() => {
10261027
uc = UnitCode.parse('2');
1028+
mt = uc.getMinimumTick(2);
10271029
});
10281030

10291031
it('A value of 0 should be rounded to 0', () => {
1030-
expect(uc.roundToNearestTick(0, 2)).toEqual(0);
1032+
expect(uc.roundToNearestTick(0, mt)).toEqual(0);
10311033
});
10321034

10331035
it('A value of 488.5 should be rounded to 488.5', () => {
1034-
expect(uc.roundToNearestTick(488.5, 2)).toEqual(488.5);
1036+
expect(uc.roundToNearestTick(488.5, mt)).toEqual(488.5);
10351037
});
10361038

10371039
it('A value of 488.51 should be rounded to 488.5', () => {
1038-
expect(uc.roundToNearestTick(488.51, 2)).toEqual(488.5);
1040+
expect(uc.roundToNearestTick(488.51, mt)).toEqual(488.5);
10391041
});
10401042

10411043
it('A value of 488.74 should be rounded to 488.75', () => {
1042-
expect(uc.roundToNearestTick(488.74, 2)).toEqual(488.75);
1044+
expect(uc.roundToNearestTick(488.74, mt)).toEqual(488.75);
10431045
});
10441046

10451047
it('A value of 488.625 should be rounded to 488.75', () => {
1046-
expect(uc.roundToNearestTick(488.625, 2)).toEqual(488.75);
1048+
expect(uc.roundToNearestTick(488.625, mt)).toEqual(488.75);
10471049
});
10481050

10491051
it('A value of 488.625 should be rounded to 488.5 (when using "roundDown" option)', () => {
1050-
expect(uc.roundToNearestTick(488.625, 2, true)).toEqual(488.5);
1052+
expect(uc.roundToNearestTick(488.625, mt, true)).toEqual(488.5);
10511053
});
10521054
});
10531055

10541056
describe('For unit code "A" and with a tickIncrement of 25 (e.g. e-mini)', () => {
10551057
let uc;
1058+
let mt;
10561059

10571060
beforeEach(() => {
10581061
uc = UnitCode.parse('A');
1062+
mt = uc.getMinimumTick(25);
10591063
});
10601064

10611065
it('A value of 4455.5 should be rounded to 4455.5', () => {
1062-
expect(uc.roundToNearestTick(4455.5, 25)).toEqual(4455.5);
1066+
expect(uc.roundToNearestTick(4455.5, mt)).toEqual(4455.5);
10631067
});
10641068

10651069
it('A value of 4455.51 should be rounded to 4455.5', () => {
1066-
expect(uc.roundToNearestTick(4455.51, 25)).toEqual(4455.5);
1070+
expect(uc.roundToNearestTick(4455.51, mt)).toEqual(4455.5);
10671071
});
10681072

10691073
it('A value of 4455.74 should be rounded to 4455.75', () => {
1070-
expect(uc.roundToNearestTick(4455.74, 25)).toEqual(4455.75);
1074+
expect(uc.roundToNearestTick(4455.74, mt)).toEqual(4455.75);
10711075
});
10721076

10731077
it('A value of 4455.625 should be rounded to 4455.75', () => {
1074-
expect(uc.roundToNearestTick(4455.625, 25)).toEqual(4455.75);
1078+
expect(uc.roundToNearestTick(4455.625, mt)).toEqual(4455.75);
10751079
});
10761080

10771081
it('A value of 4455.625 should be rounded to 4455.5 (when using "roundDown" option)', () => {
1078-
expect(uc.roundToNearestTick(4455.625, 25, true)).toEqual(4455.5);
1082+
expect(uc.roundToNearestTick(4455.625, mt, true)).toEqual(4455.5);
10791083
});
10801084
});
10811085

10821086
describe('For unit code "A" and with a tickIncrement of 1 (e.g. crude)', () => {
10831087
let uc;
1088+
let mt;
10841089

10851090
beforeEach(() => {
10861091
uc = UnitCode.parse('A');
1092+
mt = uc.getMinimumTick(1);
10871093
});
10881094

10891095
it('A value of 87.30 should be rounded to 87.30', () => {
1090-
expect(uc.roundToNearestTick(87.30, 1)).toEqual(87.30);
1096+
expect(uc.roundToNearestTick(87.30, mt)).toEqual(87.30);
10911097
});
10921098

10931099
it('A value of 87.31 should be rounded to 87.31', () => {
1094-
expect(uc.roundToNearestTick(87.31, 1)).toEqual(87.31);
1100+
expect(uc.roundToNearestTick(87.31, mt)).toEqual(87.31);
10951101
});
10961102

10971103
it('A value of 87.312 should be rounded to 87.31', () => {
1098-
expect(uc.roundToNearestTick(87.312, 1)).toEqual(87.31);
1104+
expect(uc.roundToNearestTick(87.312, mt)).toEqual(87.31);
10991105
});
11001106

11011107
it('A value of 87.318 should be rounded to 87.32', () => {
1102-
expect(uc.roundToNearestTick(87.318, 1)).toEqual(87.32);
1108+
expect(uc.roundToNearestTick(87.318, mt)).toEqual(87.32);
11031109
});
11041110

11051111
it('A value of 87.325 should be rounded to 87.33', () => {
1106-
expect(uc.roundToNearestTick(87.325, 1)).toEqual(87.33);
1112+
expect(uc.roundToNearestTick(87.325, mt)).toEqual(87.33);
11071113
});
11081114

11091115
it('A value of 87.325 should be rounded to 87.32 (when using "roundDown" option)', () => {
1110-
expect(uc.roundToNearestTick(87.325, 1, true)).toEqual(87.32);
1116+
expect(uc.roundToNearestTick(87.325, mt, true)).toEqual(87.32);
11111117
});
11121118
});
11131119

11141120
describe('For unit code "9" and with a tickIncrement of 1 (e.g. gold)', () => {
11151121
let uc;
1122+
let mt;
11161123

11171124
beforeEach(() => {
11181125
uc = UnitCode.parse('9');
1126+
mt = uc.getMinimumTick(1);
11191127
});
11201128

11211129
it('A value of 1922.5 should be rounded to 1922.5', () => {
1122-
expect(uc.roundToNearestTick(1922.5, 1)).toEqual(1922.5);
1130+
expect(uc.roundToNearestTick(1922.5, mt)).toEqual(1922.5);
11231131
});
11241132

11251133
it('A value of 1922.6 should be rounded to 1922.6', () => {
1126-
expect(uc.roundToNearestTick(1922.6, 1)).toEqual(1922.6);
1134+
expect(uc.roundToNearestTick(1922.6, mt)).toEqual(1922.6);
11271135
});
11281136

11291137
it('A value of 1922.51 should be rounded to 1922.5', () => {
1130-
expect(uc.roundToNearestTick(1922.51, 1)).toEqual(1922.5);
1138+
expect(uc.roundToNearestTick(1922.51, mt)).toEqual(1922.5);
11311139
});
11321140

11331141
it('A value of 1922.59 should be rounded to 1922.6', () => {
1134-
expect(uc.roundToNearestTick(1922.59, 1)).toEqual(1922.6);
1142+
expect(uc.roundToNearestTick(1922.59, mt)).toEqual(1922.6);
11351143
});
11361144

11371145
it('A value of 1922.55 should be rounded to 1922.6', () => {
1138-
expect(uc.roundToNearestTick(1922.55, 1)).toEqual(1922.6);
1146+
expect(uc.roundToNearestTick(1922.55, mt)).toEqual(1922.6);
11391147
});
11401148

11411149
it('A value of 1922.55 should be rounded to 1922.5 (when using "roundDown" option)', () => {
1142-
expect(uc.roundToNearestTick(1922.55, 1, true)).toEqual(1922.5);
1150+
expect(uc.roundToNearestTick(1922.55, mt, true)).toEqual(1922.5);
11431151
});
11441152
});
11451153

11461154
describe('For unit code "5" and with a tickIncrement of 1 (e.g. t-notes)', () => {
11471155
let uc;
1156+
let mt;
11481157

11491158
beforeEach(() => {
11501159
uc = UnitCode.parse('5');
1160+
mt = uc.getMinimumTick(1);
11511161
});
11521162

11531163
it('A value of 110.328125 should be rounded to 110.328125', () => {
1154-
expect(uc.roundToNearestTick(110.328125, 1)).toEqual(110.328125);
1164+
expect(uc.roundToNearestTick(110.328125, mt)).toEqual(110.328125);
11551165
});
11561166

11571167
it('A value of 110.34375 should be rounded to 110.34375', () => {
1158-
expect(uc.roundToNearestTick(110.34375, 1)).toEqual(110.34375);
1168+
expect(uc.roundToNearestTick(110.34375, mt)).toEqual(110.34375);
11591169
});
11601170

11611171
it('A value of 110.328126 should be rounded to 110.328125', () => {
1162-
expect(uc.roundToNearestTick(110.328126, 1)).toEqual(110.328125);
1172+
expect(uc.roundToNearestTick(110.328126, mt)).toEqual(110.328125);
11631173
});
11641174

11651175
it('A value of 110.34374 should be rounded to 110.34375', () => {
1166-
expect(uc.roundToNearestTick(110.34374, 1)).toEqual(110.34375);
1176+
expect(uc.roundToNearestTick(110.34374, mt)).toEqual(110.34375);
11671177
});
11681178

11691179
it('A value of 110.34374 should be rounded to 110.328125 (when using "roundDown" option)', () => {
1170-
expect(uc.roundToNearestTick(110.34374, 1, true)).toEqual(110.328125);
1180+
expect(uc.roundToNearestTick(110.34374, mt, true)).toEqual(110.328125);
11711181
});
11721182
});
11731183
});

0 commit comments

Comments
 (0)