Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: businessduck # Replace with a single Patreon username
patreon: #businessduck # Replace with a single Patreon username
open_collective: debut-js # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
"acorn": "^7.3.1",
"bigfloat": "^0.1.1",
"eslint": "^7.5.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
Expand All @@ -77,6 +78,5 @@
"ts-jest": "^27.0.3",
"ts-node": "^8.10.2",
"typescript": "^3.9.5"
},
"optionalDependencies": {}
}
}
47 changes: 47 additions & 0 deletions tests/sma/sma.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SMA } from '../../src/sma';
import { SMA as SMA2 } from 'technicalindicators';
import { BigFloat32 } from 'bigfloat';

describe('Simple Moving Average', () => {
const ticks = [120, 150, 240, 540, 210, 380, 120, 870, 250, 1100, 500, 950];
Expand Down Expand Up @@ -59,4 +60,50 @@ describe('Simple Moving Average', () => {
expect(calc).toEqual(cross);
});
});

it('Machine precision error', () => {
const array = [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];
const localSMA = new SMA(20);
const idealSMAValue = 20.5;

let lastValue = 0;

for (let i = 0; i < array.length; i++) {
lastValue = localSMA.nextValue(array[i])!;
}

console.log(lastValue, idealSMAValue);
});

it.only('BigInt vs Float floating error', () => {

function bigFloatSMA(array: number[], period = 20) {
let sum = new BigFloat32(0);

for (let i = array.length - period; i < array.length; i++) {
sum = sum.add(array[i]);
}

return sum.mul(1/period);
}

const arraySize = 900_000;
const randomDataArray = Array.from({length: arraySize}, () => Math.floor(Math.random() * arraySize));
const localSMA = new SMA(20);

for (let i = 0; i < randomDataArray.length; i++) {
const calculatedIntSMA = localSMA.nextValue(randomDataArray[i]);

if (!calculatedIntSMA || i < 20) {
continue;
}

const calculatedBigIntSMA = bigFloatSMA(randomDataArray.slice(i-20, i + 1));
// console.log(calculatedIntSMA, Number(calculatedBigIntSMA));

expect(calculatedIntSMA).toEqual(Number(calculatedBigIntSMA));
}
});
});


Loading