@tradejs/indicators 1.0.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.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @tradejs/indicators
2
+
3
+ Built-in TradeJS indicator plugin package.
4
+
5
+ - Homepage: https://tradejs.dev
6
+ - Documentation: https://docs.tradejs.dev
7
+ - Quickstart: https://docs.tradejs.dev/getting-started/quickstart
8
+
9
+ ## What It Provides
10
+
11
+ This package contains the built-in indicator catalog used by strategies and charts.
12
+
13
+ It is intended to be connected as an indicator plugin package through `tradejs.config.ts`.
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ npm i @tradejs/indicators @tradejs/core
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ Most projects should not add this package manually. It is already included by `@tradejs/base`.
24
+
25
+ If you want to reference it explicitly:
26
+
27
+ ```ts
28
+ import { defineConfig } from '@tradejs/core/config';
29
+
30
+ export default defineConfig({
31
+ indicators: ['@tradejs/indicators'],
32
+ });
33
+ ```
34
+
35
+ ## For Custom Indicators
36
+
37
+ Use `defineIndicatorPlugin(...)` from `@tradejs/core/config` in your own package and add that package to `indicators` in `tradejs.config.ts`.
@@ -0,0 +1,39 @@
1
+ import { KLineData } from 'klinecharts';
2
+ export { createSpreadSmoother, smoothSpreadSeries } from '@tradejs/core/indicators';
3
+ import * as _tradejs_types from '@tradejs/types';
4
+ import { IndicatorPluginRenderer, IndicatorPluginEntry } from '@tradejs/types';
5
+
6
+ interface Params {
7
+ period: number;
8
+ values: number[];
9
+ }
10
+ declare class MOM {
11
+ private period;
12
+ private values;
13
+ constructor({ period, values }: Params);
14
+ nextValue(price: number): number | undefined;
15
+ }
16
+
17
+ declare const smaAligned: (values: (number | undefined)[], len: number) => (number | undefined)[];
18
+
19
+ declare const ATR_PCT: (data: KLineData[], period: number, SMA_SHORT: number, SMA_LONG: number) => {
20
+ shortLine: (number | undefined)[];
21
+ longLine: (number | undefined)[];
22
+ value: number;
23
+ };
24
+
25
+ declare const registerIndicatorEntries: (entries: readonly IndicatorPluginEntry[], source: string) => void;
26
+ declare const getRegisteredIndicatorEntries: () => IndicatorPluginEntry[];
27
+ declare const getPluginIndicatorCatalog: () => _tradejs_types.Indicator[];
28
+ type IndicatorRendererDescriptor = {
29
+ indicatorId: string;
30
+ renderer: IndicatorPluginRenderer;
31
+ };
32
+ declare const getPluginIndicatorRenderers: () => IndicatorRendererDescriptor[];
33
+
34
+ declare const indicatorEntries: readonly [];
35
+ declare const _default: {
36
+ indicatorEntries: readonly [];
37
+ };
38
+
39
+ export { ATR_PCT, MOM, _default as default, getPluginIndicatorCatalog, getPluginIndicatorRenderers, getRegisteredIndicatorEntries, indicatorEntries, registerIndicatorEntries, smaAligned };
@@ -0,0 +1,39 @@
1
+ import { KLineData } from 'klinecharts';
2
+ export { createSpreadSmoother, smoothSpreadSeries } from '@tradejs/core/indicators';
3
+ import * as _tradejs_types from '@tradejs/types';
4
+ import { IndicatorPluginRenderer, IndicatorPluginEntry } from '@tradejs/types';
5
+
6
+ interface Params {
7
+ period: number;
8
+ values: number[];
9
+ }
10
+ declare class MOM {
11
+ private period;
12
+ private values;
13
+ constructor({ period, values }: Params);
14
+ nextValue(price: number): number | undefined;
15
+ }
16
+
17
+ declare const smaAligned: (values: (number | undefined)[], len: number) => (number | undefined)[];
18
+
19
+ declare const ATR_PCT: (data: KLineData[], period: number, SMA_SHORT: number, SMA_LONG: number) => {
20
+ shortLine: (number | undefined)[];
21
+ longLine: (number | undefined)[];
22
+ value: number;
23
+ };
24
+
25
+ declare const registerIndicatorEntries: (entries: readonly IndicatorPluginEntry[], source: string) => void;
26
+ declare const getRegisteredIndicatorEntries: () => IndicatorPluginEntry[];
27
+ declare const getPluginIndicatorCatalog: () => _tradejs_types.Indicator[];
28
+ type IndicatorRendererDescriptor = {
29
+ indicatorId: string;
30
+ renderer: IndicatorPluginRenderer;
31
+ };
32
+ declare const getPluginIndicatorRenderers: () => IndicatorRendererDescriptor[];
33
+
34
+ declare const indicatorEntries: readonly [];
35
+ declare const _default: {
36
+ indicatorEntries: readonly [];
37
+ };
38
+
39
+ export { ATR_PCT, MOM, _default as default, getPluginIndicatorCatalog, getPluginIndicatorRenderers, getRegisteredIndicatorEntries, indicatorEntries, registerIndicatorEntries, smaAligned };
package/dist/index.js ADDED
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ATR_PCT: () => ATR_PCT,
24
+ MOM: () => MOM,
25
+ createSpreadSmoother: () => import_indicators.createSpreadSmoother,
26
+ default: () => index_default,
27
+ getPluginIndicatorCatalog: () => getPluginIndicatorCatalog,
28
+ getPluginIndicatorRenderers: () => getPluginIndicatorRenderers,
29
+ getRegisteredIndicatorEntries: () => getRegisteredIndicatorEntries,
30
+ indicatorEntries: () => indicatorEntries,
31
+ registerIndicatorEntries: () => registerIndicatorEntries,
32
+ smaAligned: () => smaAligned,
33
+ smoothSpreadSeries: () => import_indicators.smoothSpreadSeries
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/mom.ts
38
+ var MOM = class {
39
+ constructor({ period, values }) {
40
+ this.period = period;
41
+ this.values = values;
42
+ }
43
+ nextValue(price) {
44
+ this.values.push(price);
45
+ if (this.values.length > this.period) {
46
+ const momentum = price - this.values[this.values.length - this.period - 1];
47
+ return momentum;
48
+ }
49
+ return void 0;
50
+ }
51
+ };
52
+
53
+ // src/smaAligned.ts
54
+ var import_technicalindicators = require("technicalindicators");
55
+ var smaAligned = (values, len) => {
56
+ const numeric = values.filter(
57
+ (x) => typeof x === "number" && Number.isFinite(x)
58
+ );
59
+ const sma = import_technicalindicators.SMA.calculate({ period: len, values: numeric });
60
+ const firstNumIdx = values.findIndex(
61
+ (x) => typeof x === "number" && Number.isFinite(x)
62
+ );
63
+ const prefix = (firstNumIdx === -1 ? values.length : firstNumIdx) + (len - 1);
64
+ const out = Array(prefix).fill(void 0).concat(sma);
65
+ if (out.length > values.length) out.length = values.length;
66
+ while (out.length < values.length) out.push(void 0);
67
+ return out;
68
+ };
69
+
70
+ // src/atrPct.ts
71
+ var import_technicalindicators2 = require("technicalindicators");
72
+ var round = (value, precision = 2) => {
73
+ const factor = 10 ** precision;
74
+ return Math.round(value * factor) / factor;
75
+ };
76
+ var ATR_PCT = (data, period, SMA_SHORT, SMA_LONG) => {
77
+ const closes = data.map((x) => x.close);
78
+ const highs = data.map((x) => x.high);
79
+ const lows = data.map((x) => x.low);
80
+ const atrRaw = import_technicalindicators2.ATR.calculate({
81
+ period,
82
+ high: highs,
83
+ low: lows,
84
+ close: closes
85
+ });
86
+ const atrAligned = Array(period - 1).fill(void 0).concat(atrRaw);
87
+ const atrPctAligned = atrAligned.map((v, i) => {
88
+ const c = closes[i];
89
+ if (typeof v !== "number" || !Number.isFinite(v) || !c) return void 0;
90
+ return v / c * 100;
91
+ });
92
+ const shortLine = smaAligned(atrPctAligned, SMA_SHORT);
93
+ const longLine = smaAligned(atrPctAligned, SMA_LONG);
94
+ const lastShortLine = shortLine[shortLine.length - 1];
95
+ const lastLongLine = longLine[longLine.length - 1];
96
+ const value = typeof lastShortLine === "number" && Number.isFinite(lastShortLine) && typeof lastLongLine === "number" && Number.isFinite(lastLongLine) ? round(lastShortLine / lastLongLine, 2) : 0;
97
+ return { shortLine, longLine, value };
98
+ };
99
+
100
+ // src/spread.ts
101
+ var import_indicators = require("@tradejs/core/indicators");
102
+
103
+ // src/registry.ts
104
+ var import_indicators2 = require("@tradejs/core/indicators");
105
+ var registerIndicatorEntries = (entries, source) => (0, import_indicators2.registerIndicatorEntries)(entries, source);
106
+ var getRegisteredIndicatorEntries = () => (0, import_indicators2.getRegisteredIndicatorEntries)();
107
+ var getPluginIndicatorCatalog = () => (0, import_indicators2.getPluginIndicatorCatalog)();
108
+ var getPluginIndicatorRenderers = () => (0, import_indicators2.getPluginIndicatorRenderers)();
109
+
110
+ // src/index.ts
111
+ var indicatorEntries = [];
112
+ var index_default = { indicatorEntries };
113
+ // Annotate the CommonJS export names for ESM import in node:
114
+ 0 && (module.exports = {
115
+ ATR_PCT,
116
+ MOM,
117
+ createSpreadSmoother,
118
+ getPluginIndicatorCatalog,
119
+ getPluginIndicatorRenderers,
120
+ getRegisteredIndicatorEntries,
121
+ indicatorEntries,
122
+ registerIndicatorEntries,
123
+ smaAligned,
124
+ smoothSpreadSeries
125
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,97 @@
1
+ // src/mom.ts
2
+ var MOM = class {
3
+ constructor({ period, values }) {
4
+ this.period = period;
5
+ this.values = values;
6
+ }
7
+ nextValue(price) {
8
+ this.values.push(price);
9
+ if (this.values.length > this.period) {
10
+ const momentum = price - this.values[this.values.length - this.period - 1];
11
+ return momentum;
12
+ }
13
+ return void 0;
14
+ }
15
+ };
16
+
17
+ // src/smaAligned.ts
18
+ import { SMA } from "technicalindicators";
19
+ var smaAligned = (values, len) => {
20
+ const numeric = values.filter(
21
+ (x) => typeof x === "number" && Number.isFinite(x)
22
+ );
23
+ const sma = SMA.calculate({ period: len, values: numeric });
24
+ const firstNumIdx = values.findIndex(
25
+ (x) => typeof x === "number" && Number.isFinite(x)
26
+ );
27
+ const prefix = (firstNumIdx === -1 ? values.length : firstNumIdx) + (len - 1);
28
+ const out = Array(prefix).fill(void 0).concat(sma);
29
+ if (out.length > values.length) out.length = values.length;
30
+ while (out.length < values.length) out.push(void 0);
31
+ return out;
32
+ };
33
+
34
+ // src/atrPct.ts
35
+ import { ATR } from "technicalindicators";
36
+ var round = (value, precision = 2) => {
37
+ const factor = 10 ** precision;
38
+ return Math.round(value * factor) / factor;
39
+ };
40
+ var ATR_PCT = (data, period, SMA_SHORT, SMA_LONG) => {
41
+ const closes = data.map((x) => x.close);
42
+ const highs = data.map((x) => x.high);
43
+ const lows = data.map((x) => x.low);
44
+ const atrRaw = ATR.calculate({
45
+ period,
46
+ high: highs,
47
+ low: lows,
48
+ close: closes
49
+ });
50
+ const atrAligned = Array(period - 1).fill(void 0).concat(atrRaw);
51
+ const atrPctAligned = atrAligned.map((v, i) => {
52
+ const c = closes[i];
53
+ if (typeof v !== "number" || !Number.isFinite(v) || !c) return void 0;
54
+ return v / c * 100;
55
+ });
56
+ const shortLine = smaAligned(atrPctAligned, SMA_SHORT);
57
+ const longLine = smaAligned(atrPctAligned, SMA_LONG);
58
+ const lastShortLine = shortLine[shortLine.length - 1];
59
+ const lastLongLine = longLine[longLine.length - 1];
60
+ const value = typeof lastShortLine === "number" && Number.isFinite(lastShortLine) && typeof lastLongLine === "number" && Number.isFinite(lastLongLine) ? round(lastShortLine / lastLongLine, 2) : 0;
61
+ return { shortLine, longLine, value };
62
+ };
63
+
64
+ // src/spread.ts
65
+ import {
66
+ createSpreadSmoother,
67
+ smoothSpreadSeries
68
+ } from "@tradejs/core/indicators";
69
+
70
+ // src/registry.ts
71
+ import {
72
+ getPluginIndicatorCatalog as getPluginIndicatorCatalogCore,
73
+ getPluginIndicatorRenderers as getPluginIndicatorRenderersCore,
74
+ getRegisteredIndicatorEntries as getRegisteredIndicatorEntriesCore,
75
+ registerIndicatorEntries as registerIndicatorEntriesCore
76
+ } from "@tradejs/core/indicators";
77
+ var registerIndicatorEntries = (entries, source) => registerIndicatorEntriesCore(entries, source);
78
+ var getRegisteredIndicatorEntries = () => getRegisteredIndicatorEntriesCore();
79
+ var getPluginIndicatorCatalog = () => getPluginIndicatorCatalogCore();
80
+ var getPluginIndicatorRenderers = () => getPluginIndicatorRenderersCore();
81
+
82
+ // src/index.ts
83
+ var indicatorEntries = [];
84
+ var index_default = { indicatorEntries };
85
+ export {
86
+ ATR_PCT,
87
+ MOM,
88
+ createSpreadSmoother,
89
+ index_default as default,
90
+ getPluginIndicatorCatalog,
91
+ getPluginIndicatorRenderers,
92
+ getRegisteredIndicatorEntries,
93
+ indicatorEntries,
94
+ registerIndicatorEntries,
95
+ smaAligned,
96
+ smoothSpreadSeries
97
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@tradejs/indicators",
3
+ "version": "1.0.0",
4
+ "description": "Built-in TradeJS indicator plugin catalog.",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.mjs",
15
+ "require": "./dist/index.js"
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "@tradejs/core": "^1.0.0",
20
+ "@tradejs/types": "^1.0.0",
21
+ "klinecharts": "10.0.0-alpha9",
22
+ "technicalindicators": "^3.1.0"
23
+ },
24
+ "devDependencies": {
25
+ "tsup": "^8.5.1",
26
+ "typescript": "^5.1"
27
+ },
28
+ "scripts": {
29
+ "build": "tsup",
30
+ "typecheck": "tsc -p ./tsconfig.json --noEmit"
31
+ },
32
+ "license": "MIT",
33
+ "author": "aleksnick (https://github.com/aleksnick)"
34
+ }