ta-crypto 0.1.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,142 @@
1
+ export function createSMA(period = 14) {
2
+ if (period <= 0) {
3
+ throw new Error("period must be > 0");
4
+ }
5
+ const window = [];
6
+ let sum = 0;
7
+ return {
8
+ next(value) {
9
+ if (!Number.isFinite(value)) {
10
+ throw new Error("value must be a finite number");
11
+ }
12
+ window.push(value);
13
+ sum += value;
14
+ if (window.length < period) {
15
+ return null;
16
+ }
17
+ if (window.length > period) {
18
+ sum -= window.shift();
19
+ }
20
+ return sum / period;
21
+ },
22
+ reset() {
23
+ window.length = 0;
24
+ sum = 0;
25
+ }
26
+ };
27
+ }
28
+ export function createEMA(period = 14) {
29
+ if (period <= 0) {
30
+ throw new Error("period must be > 0");
31
+ }
32
+ const k = 2 / (period + 1);
33
+ let seedSum = 0;
34
+ let seedCount = 0;
35
+ let prev = null;
36
+ return {
37
+ next(value) {
38
+ if (!Number.isFinite(value)) {
39
+ throw new Error("value must be a finite number");
40
+ }
41
+ if (prev === null) {
42
+ seedSum += value;
43
+ seedCount += 1;
44
+ if (seedCount < period) {
45
+ return null;
46
+ }
47
+ prev = seedSum / period;
48
+ return prev;
49
+ }
50
+ prev = (value - prev) * k + prev;
51
+ return prev;
52
+ },
53
+ reset() {
54
+ seedSum = 0;
55
+ seedCount = 0;
56
+ prev = null;
57
+ }
58
+ };
59
+ }
60
+ export function createRSI(period = 14) {
61
+ if (period <= 0) {
62
+ throw new Error("period must be > 0");
63
+ }
64
+ let prevPrice = null;
65
+ let avgGain = 0;
66
+ let avgLoss = 0;
67
+ let seedGain = 0;
68
+ let seedLoss = 0;
69
+ let seedCount = 0;
70
+ let count = 0;
71
+ return {
72
+ next(price) {
73
+ if (!Number.isFinite(price)) {
74
+ throw new Error("price must be a finite number");
75
+ }
76
+ count += 1;
77
+ if (prevPrice === null) {
78
+ prevPrice = price;
79
+ return null;
80
+ }
81
+ const diff = price - prevPrice;
82
+ prevPrice = price;
83
+ const gain = diff > 0 ? diff : 0;
84
+ const loss = diff < 0 ? -diff : 0;
85
+ if (seedCount < period) {
86
+ seedGain += gain;
87
+ seedLoss += loss;
88
+ seedCount += 1;
89
+ if (seedCount < period)
90
+ return null;
91
+ avgGain = seedGain / period;
92
+ avgLoss = seedLoss / period;
93
+ }
94
+ else {
95
+ avgGain = (avgGain * (period - 1) + gain) / period;
96
+ avgLoss = (avgLoss * (period - 1) + loss) / period;
97
+ }
98
+ if (count < period + 1)
99
+ return null;
100
+ if (avgLoss === 0)
101
+ return 100;
102
+ const rs = avgGain / avgLoss;
103
+ return 100 - 100 / (1 + rs);
104
+ },
105
+ reset() {
106
+ prevPrice = null;
107
+ avgGain = 0;
108
+ avgLoss = 0;
109
+ seedGain = 0;
110
+ seedLoss = 0;
111
+ seedCount = 0;
112
+ count = 0;
113
+ }
114
+ };
115
+ }
116
+ export function createVWAPSession() {
117
+ let cumPV = 0;
118
+ let cumV = 0;
119
+ let lastSession;
120
+ return {
121
+ next(candle) {
122
+ const { high, low, close, volume, sessionId } = candle;
123
+ if (!Number.isFinite(high) || !Number.isFinite(low) || !Number.isFinite(close) || !Number.isFinite(volume)) {
124
+ throw new Error("high, low, close and volume must be finite numbers");
125
+ }
126
+ if (lastSession !== sessionId) {
127
+ cumPV = 0;
128
+ cumV = 0;
129
+ lastSession = sessionId;
130
+ }
131
+ const typical = (high + low + close) / 3;
132
+ cumPV += typical * volume;
133
+ cumV += volume;
134
+ return cumV === 0 ? null : cumPV / cumV;
135
+ },
136
+ reset() {
137
+ cumPV = 0;
138
+ cumV = 0;
139
+ lastSession = undefined;
140
+ }
141
+ };
142
+ }
package/dist/types.d.ts CHANGED
@@ -1,10 +1,38 @@
1
1
  export type NumericSeries = number[];
2
2
  export type Series = Array<number | null>;
3
- export type Candle = {
4
- time?: number | string | Date;
3
+ export type TimeValue = number | string | Date;
4
+ export type CandleObject = {
5
5
  open: number;
6
6
  high: number;
7
7
  low: number;
8
8
  close: number;
9
9
  volume?: number;
10
+ time?: TimeValue;
10
11
  };
12
+ export type CandleAlias = {
13
+ o: number;
14
+ h: number;
15
+ l: number;
16
+ c: number;
17
+ v?: number;
18
+ t?: TimeValue;
19
+ };
20
+ export type Candle = CandleObject | CandleAlias;
21
+ export type OHLCV = {
22
+ open: number[];
23
+ high: number[];
24
+ low: number[];
25
+ close: number[];
26
+ volume: number[];
27
+ time: Array<TimeValue | undefined>;
28
+ };
29
+ export type OHLCVAlias = {
30
+ o: number[];
31
+ h: number[];
32
+ l: number[];
33
+ c: number[];
34
+ v?: number[];
35
+ t?: Array<TimeValue | undefined>;
36
+ };
37
+ export type OHLCVInput = OHLCV | OHLCVAlias;
38
+ export type PriceInput = NumericSeries | Candle[];
package/package.json CHANGED
@@ -1,55 +1,80 @@
1
- {
2
- "name": "ta-crypto",
3
- "version": "0.1.5",
4
- "description": "Technical analysis for crypto markets in Node.js",
5
- "type": "module",
6
- "license": "MIT",
7
- "author": "",
8
- "exports": {
9
- ".": {
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.js"
12
- }
13
- },
14
- "types": "dist/index.d.ts",
15
- "main": "dist/index.js",
16
- "files": [
17
- "dist",
18
- "README.md",
19
- "LICENSE"
20
- ],
21
- "scripts": {
22
- "build": "tsc -p tsconfig.json",
23
- "clean": "rimraf dist",
24
- "prepublishOnly": "npm run clean && npm run build",
25
- "release": "node ./scripts/release.js",
26
- "release:patch": "npm version patch && npm run release",
27
- "release:minor": "npm version minor && npm run release",
28
- "release:major": "npm version major && npm run release",
29
- "version:patch": "npm version patch",
30
- "version:minor": "npm version minor",
31
- "version:major": "npm version major",
32
- "changelog": "node ./scripts/changelog.js",
33
- "test": "node -e \"console.log('no tests yet')\""
34
- },
35
- "devDependencies": {
36
- "rimraf": "^5.0.5",
37
- "typescript": "^5.4.5"
38
- },
39
- "keywords": [
40
- "crypto",
41
- "bitcoin",
42
- "technical-analysis",
43
- "indicators",
44
- "trading",
45
- "ohlcv"
46
- ],
47
- "repository": {
48
- "type": "git",
49
- "url": "git+https://github.com/TDamiao/ta-crypto.git"
50
- },
51
- "bugs": {
52
- "url": "https://github.com/TDamiao/ta-crypto/issues"
53
- },
54
- "homepage": "https://github.com/TDamiao/ta-crypto#readme"
55
- }
1
+ {
2
+ "name": "ta-crypto",
3
+ "version": "0.3.0",
4
+ "description": "Technical analysis for crypto markets in Node.js",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./indicators": {
14
+ "types": "./dist/indicators.d.ts",
15
+ "import": "./dist/indicators.js"
16
+ },
17
+ "./crypto": {
18
+ "types": "./dist/crypto.d.ts",
19
+ "import": "./dist/crypto.js"
20
+ },
21
+ "./candles": {
22
+ "types": "./dist/candles.d.ts",
23
+ "import": "./dist/candles.js"
24
+ },
25
+ "./stateful": {
26
+ "types": "./dist/stateful.d.ts",
27
+ "import": "./dist/stateful.js"
28
+ }
29
+ },
30
+ "sideEffects": false,
31
+ "types": "dist/index.d.ts",
32
+ "main": "dist/index.js",
33
+ "files": [
34
+ "dist",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "scripts": {
39
+ "build": "tsc -p tsconfig.json",
40
+ "clean": "rimraf dist",
41
+ "prepublishOnly": "npm run clean && npm run build",
42
+ "release": "node ./scripts/release.js",
43
+ "release:patch": "npm version patch && npm run release",
44
+ "release:minor": "npm version minor && npm run release",
45
+ "release:major": "npm version major && npm run release",
46
+ "version:patch": "npm version patch",
47
+ "version:minor": "npm version minor",
48
+ "version:major": "npm version major",
49
+ "changelog": "node ./scripts/changelog.js",
50
+ "generate:golden": "npm run build && node ./scripts/generate-golden.js",
51
+ "generate:compat": "npm run build && node ./scripts/export-compat-vectors.js",
52
+ "bench": "npm run build && node ./scripts/bench.js",
53
+ "test": "npm run build && node --test test/*.test.mjs",
54
+ "test:golden": "npm run build && node --test test/golden.test.mjs",
55
+ "test:compat:technicalindicators": "npm run generate:compat && node ./scripts/compare-technicalindicators.js",
56
+ "test:compat:python": "npm run generate:compat && python ./scripts/compare-python-refs.py",
57
+ "test:compat": "npm run test:compat:technicalindicators && npm run test:compat:python"
58
+ },
59
+ "devDependencies": {
60
+ "rimraf": "^5.0.5",
61
+ "technicalindicators": "^3.1.0",
62
+ "typescript": "^5.4.5"
63
+ },
64
+ "keywords": [
65
+ "crypto",
66
+ "bitcoin",
67
+ "technical-analysis",
68
+ "indicators",
69
+ "trading",
70
+ "ohlcv"
71
+ ],
72
+ "repository": {
73
+ "type": "git",
74
+ "url": "git+https://github.com/TDamiao/ta-crypto.git"
75
+ },
76
+ "bugs": {
77
+ "url": "https://github.com/TDamiao/ta-crypto/issues"
78
+ },
79
+ "homepage": "https://github.com/TDamiao/ta-crypto#readme"
80
+ }