Skip to content

Commit

Permalink
feat: Accept input of type number, string & Big
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed May 30, 2020
1 parent 3fbf3af commit 13aaa99
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
6 changes: 4 additions & 2 deletions src/EMA/EMA.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/MA/MovingAverage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Big from 'big.js';
import Big, {BigSource} from 'big.js';

export abstract class MovingAverage {
protected result: Big | undefined;
Expand All @@ -12,5 +12,5 @@ export abstract class MovingAverage {
return this.result;
}

abstract update(price: Big): void;
abstract update(price: BigSource): void;
}
6 changes: 3 additions & 3 deletions src/SMA/SMA.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/SMMA/SMMA.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Big from 'big.js';
import Big, {BigSource} from 'big.js';
import {SMA} from '../';

class SMMA {
Expand All @@ -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);
Expand Down

0 comments on commit 13aaa99

Please sign in to comment.