Skip to content

Commit

Permalink
Adjust UnitCode.roundToNearestTick to accept minimum tick value (in…
Browse files Browse the repository at this point in the history
…stead of point value)
  • Loading branch information
bryaningl3 committed Sep 10, 2023
1 parent 7e05552 commit 3125d9f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 41 deletions.
5 changes: 5 additions & 0 deletions docs/content/release_notes.md
Original file line number Diff line number Diff line change
@@ -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**

Expand Down
3 changes: 3 additions & 0 deletions docs/content/releases/6.2.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug Fixes**

* Adjusted `UnitCode.roundToNearestTick` to accept minimum tick value (instead of point value).
14 changes: 7 additions & 7 deletions docs/content/sdk/lib-utilities-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
* [.getFractionDigits([special])](#UnitCodegetFractionDigits) ⇒ <code>Number</code> \| <code>undefined</code>
* [.getMinimumTick(tickIncrement)](#UnitCodegetMinimumTick) ⇒ <code>Number</code>
* [.getMinimumTickValue(tickIncrement, pointValue)](#UnitCodegetMinimumTickValue) ⇒ <code>Number</code>
* [.roundToNearestTick(value, tickIncrement, [roundToZero])](#UnitCoderoundToNearestTick) ⇒ <code>Number</code>
* [.roundToNearestTick(value, minimumTick, [roundToZero])](#UnitCoderoundToNearestTick) ⇒ <code>Number</code>
* _static_
* [.parse(code)](#UnitCodeparse)[<code>UnitCode</code>](#UnitCode) \| <code>null</code>
* [.fromBaseCode(code)](#UnitCodefromBaseCode)[<code>UnitCode</code>](#UnitCode) \| <code>null</code>
Expand Down Expand Up @@ -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 [<code>UnitCode</code>](#UnitCode)
**Returns**: <code>Number</code>

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


* * *
Expand Down
11 changes: 5 additions & 6 deletions lib/utilities/data/UnitCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
66 changes: 38 additions & 28 deletions test/specs/utilities/data/UnitCodeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Expand Down

0 comments on commit 3125d9f

Please sign in to comment.