From 13aaa9965ba51a21d91da055e4e379d900f19dff Mon Sep 17 00:00:00 2001 From: Benny Neugebauer Date: Sat, 30 May 2020 11:02:54 +0200 Subject: [PATCH] feat: Accept input of type number, string & Big --- README.md | 19 +++++++++++++++---- src/EMA/EMA.ts | 6 ++++-- src/MA/MovingAverage.ts | 4 ++-- src/SMA/SMA.ts | 6 +++--- src/SMMA/SMMA.ts | 6 +++--- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 45123f617..0815d7a13 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,23 @@ Provide a TypeScript implementation for common technical indicators with arbitra ## Usage ```typescript -import Big from 'big.js'; import {SMA} from 'trading-signals'; const sma = new SMA(3); -sma.update(new Big(40)); + +// You can add numbers: +sma.update(40); +sma.update(30); +sma.update(20); + +// You can add strings: +sma.update('10'); + +// You can add arbitrary-precision decimals: +import Big from 'big.js'; sma.update(new Big(30)); -sma.update(new Big(20)); -console.log(sma.getResult().valueOf()); // "30" + +// You can get the result in various formats: +console.log(sma.getResult().valueOf()); // "20" +console.log(sma.getResult().toFixed(2)); // "20.00" ``` diff --git a/src/EMA/EMA.ts b/src/EMA/EMA.ts index 4f2d9f95f..7f08a0c79 100644 --- a/src/EMA/EMA.ts +++ b/src/EMA/EMA.ts @@ -1,8 +1,10 @@ -import Big from 'big.js'; +import Big, {BigSource} from 'big.js'; import {MovingAverage} from '../MA/MovingAverage'; export class EMA extends MovingAverage { - update(price: Big): void { + update(_price: BigSource): void { + const price = new Big(_price); + // If it's the first update there is no previous result and a default has to be set. if (!this.result) { this.result = price; diff --git a/src/MA/MovingAverage.ts b/src/MA/MovingAverage.ts index 5d3c3654c..90ffa3890 100644 --- a/src/MA/MovingAverage.ts +++ b/src/MA/MovingAverage.ts @@ -1,4 +1,4 @@ -import Big from 'big.js'; +import Big, {BigSource} from 'big.js'; export abstract class MovingAverage { protected result: Big | undefined; @@ -12,5 +12,5 @@ export abstract class MovingAverage { return this.result; } - abstract update(price: Big): void; + abstract update(price: BigSource): void; } diff --git a/src/SMA/SMA.ts b/src/SMA/SMA.ts index cb8c2505c..9bd610842 100644 --- a/src/SMA/SMA.ts +++ b/src/SMA/SMA.ts @@ -1,11 +1,11 @@ -import Big from 'big.js'; +import Big, {BigSource} from 'big.js'; import {MovingAverage} from '../MA/MovingAverage'; export class SMA extends MovingAverage { private readonly prices: Big[] = []; - update(price: Big): void { - this.prices.push(price); + update(price: BigSource): void { + this.prices.push(new Big(price)); if (this.prices.length > this.interval) { this.prices.shift(); diff --git a/src/SMMA/SMMA.ts b/src/SMMA/SMMA.ts index 1408fd01f..53d77964b 100644 --- a/src/SMMA/SMMA.ts +++ b/src/SMMA/SMMA.ts @@ -1,4 +1,4 @@ -import Big from 'big.js'; +import Big, {BigSource} from 'big.js'; import {SMA} from '../'; class SMMA { @@ -14,8 +14,8 @@ class SMMA { this.result = new Big(0); } - update(price: Big): void { - this.prices.push(price); + update(price: BigSource): void { + this.prices.push(new Big(price)); if (this.prices.length < this.interval) { this.sma.update(price);