@ch20026103/anysis 0.0.8-alpha → 0.0.10-alpha

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/demo/main.js ADDED
@@ -0,0 +1,125 @@
1
+ /* eslint @typescript-eslint/no-var-requires: "off" */
2
+ const axios = require("axios");
3
+ const { Obv, Macd, Kd, Boll, Vma } = require("../dist/cjs/index.js");
4
+ const obv = new Obv();
5
+ const macd = new Macd();
6
+ const kd = new Kd();
7
+ const boll = new Boll();
8
+ const vma = new Vma();
9
+ const stockId = 1702;
10
+ // week
11
+ const { getWeekLine, Ma } = require("../dist/cjs/index.js");
12
+ const ma = new Ma();
13
+
14
+ function DemoDay() {
15
+ axios
16
+ .get(
17
+ `https://tw.quote.finance.yahoo.net/quote/q?type=ta&perd=d&mkt=10&sym=${stockId}&v=1&callback=`
18
+ )
19
+ .then((res) => {
20
+ let json = res.data.match(/"ta":(\S*),"ex"/)[1];
21
+ let data = JSON.parse(json).slice(0, -21);
22
+ let obvData = obv.init(data[0], 5);
23
+ let macdData = macd.init(data[0]);
24
+ let kdData = kd.init(data[0]);
25
+ let bollData = boll.init(data[0]);
26
+ let vmaData = vma.init(data[0], 5);
27
+ let finallyData = [
28
+ {
29
+ ...data[0],
30
+ vma: vmaData.vma,
31
+ ema12: macdData.ema12,
32
+ ema26: macdData.ema26,
33
+ macd: macdData.macd,
34
+ osc: macdData.osc,
35
+ dif: macdData.dif[macdData.dif.length - 1],
36
+ obv: obvData.obv,
37
+ obvMa: obvData.obvMa,
38
+ rsv: kdData.rsv,
39
+ k: kdData.k,
40
+ d: kdData.d,
41
+ "k-d": kdData["k-d"],
42
+ bollMa: bollData.bollMa,
43
+ bollUb: bollData.bollUb,
44
+ bollLb: bollData.bollLb,
45
+ },
46
+ ];
47
+
48
+ for (let i = 1; i < data.length; i++) {
49
+ obvData = obv.next(data[i], obvData, 5);
50
+ macdData = macd.next(data[i], macdData);
51
+ kdData = kd.next(data[i], kdData, 9);
52
+ bollData = boll.next(data[i], bollData, 20);
53
+ vmaData = vma.next(data[i], vmaData, 5);
54
+ finallyData.push({
55
+ ...data[i],
56
+ vma: vmaData.vma,
57
+ ema12: macdData.ema12,
58
+ ema26: macdData.ema26,
59
+ macd: macdData.macd,
60
+ osc: macdData.osc,
61
+ dif: macdData.dif[macdData.dif.length - 1],
62
+ obv: obvData.obv,
63
+ obvMa: obvData.obvMa,
64
+ rsv: kdData.rsv,
65
+ k: kdData.k,
66
+ d: kdData.d,
67
+ "k-d": kdData["k-d"],
68
+ bollMa: bollData.bollMa,
69
+ bollUb: bollData.bollUb,
70
+ bollLb: bollData.bollLb,
71
+ });
72
+ }
73
+
74
+ console.log(finallyData[finallyData.length - 1]);
75
+ })
76
+ .catch((error) => {
77
+ console.error(error);
78
+ })
79
+ .finally(() => {
80
+ console.log("day done");
81
+ });
82
+ }
83
+
84
+ function DemoWeek() {
85
+ axios
86
+ .get(
87
+ `https://tw.quote.finance.yahoo.net/quote/q?type=ta&perd=d&mkt=10&sym=${stockId}&v=1&callback=`
88
+ )
89
+ .then((res) => {
90
+ let json = res.data.match(/"ta":(\S*),"ex"/)[1];
91
+ let data = JSON.parse(json);
92
+ // week
93
+ let weekLine = getWeekLine(data);
94
+ let weekMaData5 = ma.init(weekLine[0], 5);
95
+ let weekMaData10 = ma.init(weekLine[0], 10);
96
+ let weekMaData20 = ma.init(weekLine[0], 20);
97
+ for (let i = 1; i < weekLine.length; i++) {
98
+ weekMaData5 = ma.next(weekLine[i], weekMaData5, 5);
99
+ weekMaData10 = ma.next(weekLine[i], weekMaData10, 10);
100
+ weekMaData20 = ma.next(weekLine[i], weekMaData20, 20);
101
+ }
102
+ console.log({
103
+ t: weekMaData5.dataset[weekMaData5.dataset.length - 1].t,
104
+ ma5: weekMaData5.ma,
105
+ ma5ExclusionValue: weekMaData5.exclusionValue,
106
+ ma10: weekMaData10.ma,
107
+ ma10ExclusionValue: weekMaData10.exclusionValue,
108
+ ma20: weekMaData20.ma,
109
+ ma20ExclusionValue: weekMaData20.exclusionValue,
110
+ });
111
+ })
112
+ .catch((error) => {
113
+ console.error(error);
114
+ })
115
+ .finally(() => {
116
+ console.log("week done");
117
+ });
118
+ }
119
+
120
+ // DemoDay();
121
+
122
+ const showWeek = true;
123
+ if (showWeek) {
124
+ DemoWeek();
125
+ }
@@ -7,6 +7,7 @@ export { default as Ema } from "./stockSkills/ema.js";
7
7
  export { default as Gold } from "./stockSkills/gold.js";
8
8
  export { default as Kd } from "./stockSkills/kd.js";
9
9
  export { default as Ma } from "./stockSkills/ma.js";
10
+ export { default as Vma } from "./stockSkills/vma.js";
10
11
  export { default as Macd } from "./stockSkills/macd.js";
11
12
  export { default as Obv } from "./stockSkills/obv.js";
12
13
  export { default as Rsi } from "./stockSkills/rsi.js";
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseLsusbOutput = exports.isJSON = exports.minus = exports.add = exports.Williams = exports.getWeekLine = exports.dateFormat = exports.Rsi = exports.Obv = exports.Macd = exports.Ma = exports.Kd = exports.Gold = exports.Ema = exports.Boll = exports.calcSeasonalIndicesNoTrend = exports.weightMovingAverages = exports.movingAverages = exports.exponentialSmoothing = exports.slope = exports.simpleRegressionModel = void 0;
3
+ exports.parseLsusbOutput = exports.isJSON = exports.minus = exports.add = exports.Williams = exports.getWeekLine = exports.dateFormat = exports.Rsi = exports.Obv = exports.Macd = exports.Vma = exports.Ma = exports.Kd = exports.Gold = exports.Ema = exports.Boll = exports.calcSeasonalIndicesNoTrend = exports.weightMovingAverages = exports.movingAverages = exports.exponentialSmoothing = exports.slope = exports.simpleRegressionModel = void 0;
4
4
  /*
5
5
  請注意,在 src/index.ts 中,我的導入包含文件擴展名(.js)。
6
6
  如果需要支持 Node.js 和構建工具(ex: webpack),則不需要這樣做。 **因為commonJs默認js副檔名**
@@ -26,6 +26,8 @@ var kd_js_1 = require("./stockSkills/kd.js");
26
26
  Object.defineProperty(exports, "Kd", { enumerable: true, get: function () { return kd_js_1.default; } });
27
27
  var ma_js_1 = require("./stockSkills/ma.js");
28
28
  Object.defineProperty(exports, "Ma", { enumerable: true, get: function () { return ma_js_1.default; } });
29
+ var vma_js_1 = require("./stockSkills/vma.js");
30
+ Object.defineProperty(exports, "Vma", { enumerable: true, get: function () { return vma_js_1.default; } });
29
31
  var macd_js_1 = require("./stockSkills/macd.js");
30
32
  Object.defineProperty(exports, "Macd", { enumerable: true, get: function () { return macd_js_1.default; } });
31
33
  var obv_js_1 = require("./stockSkills/obv.js");
@@ -1,5 +1,6 @@
1
1
  type DataType = {
2
2
  c: number;
3
+ [key: string]: unknown;
3
4
  };
4
5
  type ListType = DataType[];
5
6
  type ResMa5 = {
@@ -35,15 +36,27 @@ interface MaType {
35
36
  dataset: ListType;
36
37
  ma: number;
37
38
  type: number;
39
+ exclusionValue: {
40
+ d: number;
41
+ "d-1": number;
42
+ };
38
43
  };
39
44
  next: (data: DataType, preList: {
40
45
  dataset: ListType;
41
46
  ma: number;
42
47
  type: number;
48
+ exclusionValue: {
49
+ d: number;
50
+ "d-1": number;
51
+ };
43
52
  }, type: number) => {
44
53
  dataset: ListType;
45
54
  ma: number;
46
55
  type: number;
56
+ exclusionValue: {
57
+ d: number;
58
+ "d-1": number;
59
+ };
47
60
  };
48
61
  getAllMa: (list: ListType) => ResAllMa;
49
62
  getMa5: (list: ListType) => ResMa5;
@@ -57,6 +70,10 @@ export default class Ma implements MaType {
57
70
  dataset: DataType[];
58
71
  ma: number;
59
72
  type: number;
73
+ exclusionValue: {
74
+ d: number;
75
+ "d-1": number;
76
+ };
60
77
  };
61
78
  next(data: DataType, preList: {
62
79
  dataset: ListType;
@@ -66,6 +83,10 @@ export default class Ma implements MaType {
66
83
  dataset: ListType;
67
84
  ma: number;
68
85
  type: number;
86
+ exclusionValue: {
87
+ d: number;
88
+ "d-1": number;
89
+ };
69
90
  };
70
91
  getAllMa(list: ListType): ResAllMa;
71
92
  getMa5(list: ListType): ResMa5;
@@ -19,7 +19,7 @@ var Ma = /** @class */ (function () {
19
19
  configurable: true,
20
20
  writable: true,
21
21
  value: function (data, type) {
22
- return { dataset: [data], ma: 0, type: type };
22
+ return { dataset: [data], ma: 0, type: type, exclusionValue: { d: 0, "d-1": 0 } };
23
23
  }
24
24
  });
25
25
  Object.defineProperty(Ma.prototype, "next", {
@@ -29,15 +29,23 @@ var Ma = /** @class */ (function () {
29
29
  value: function (data, preList, type) {
30
30
  preList.dataset.push(data);
31
31
  if (preList.dataset.length < type) {
32
- return { dataset: preList.dataset, ma: 0, type: type };
32
+ return {
33
+ dataset: preList.dataset,
34
+ ma: 0,
35
+ type: type,
36
+ exclusionValue: { d: 0, "d-1": 0 },
37
+ };
33
38
  }
34
39
  else {
40
+ var exclusionValue = { d: preList.dataset[0].c, "d-1": 0 };
35
41
  if (preList.dataset.length > type) {
42
+ exclusionValue["d-1"] = exclusionValue.d;
36
43
  preList.dataset.shift();
44
+ exclusionValue.d = preList.dataset[0].c;
37
45
  }
38
46
  var sum = preList.dataset.reduce(function (pre, current) { return pre + current.c; }, 0);
39
47
  var ma = Math.round((sum / type) * 100) / 100;
40
- return { dataset: preList.dataset, ma: ma, type: type };
48
+ return { dataset: preList.dataset, ma: ma, type: type, exclusionValue: exclusionValue };
41
49
  }
42
50
  }
43
51
  });
@@ -8,36 +8,48 @@ type ResObv = {
8
8
  obv: number;
9
9
  } & ItemType;
10
10
  interface ObvType {
11
- init: (data: ItemType) => {
11
+ init: (data: ItemType, type: number) => {
12
12
  dataset: ItemType[];
13
13
  obv: number;
14
+ obvList: number[];
14
15
  preClose: number;
16
+ obvMa: number;
15
17
  };
16
18
  next: (data: ItemType, preList: {
17
19
  dataset: ItemType[];
18
20
  obv: number;
21
+ obvList: number[];
19
22
  preClose: number;
20
- }) => {
23
+ obvMa: number;
24
+ }, type: number) => {
21
25
  dataset: ItemType[];
22
26
  obv: number;
27
+ obvList: number[];
23
28
  preClose: number;
29
+ obvMa: number;
24
30
  };
25
31
  getObv: (list: ItemType[], period: number) => ResObv[];
26
32
  }
27
33
  export default class Obv implements ObvType {
28
- init(data: ItemType): {
34
+ init(data: ItemType, type: number): {
29
35
  dataset: ItemType[];
30
36
  obv: number;
37
+ obvList: number[];
31
38
  preClose: number;
39
+ obvMa: number;
32
40
  };
33
41
  next(data: ItemType, preList: {
34
42
  dataset: ItemType[];
35
43
  obv: number;
44
+ obvList: number[];
36
45
  preClose: number;
37
- }): {
46
+ obvMa: number;
47
+ }, type: number): {
38
48
  dataset: ItemType[];
39
49
  obv: number;
40
50
  preClose: number;
51
+ obvList: number[];
52
+ obvMa: number;
41
53
  };
42
54
  getObv(list: ItemType[]): ResObv[];
43
55
  }
@@ -27,11 +27,13 @@ var Obv = /** @class */ (function () {
27
27
  enumerable: false,
28
28
  configurable: true,
29
29
  writable: true,
30
- value: function (data) {
30
+ value: function (data, type) {
31
31
  return {
32
32
  dataset: [data],
33
33
  obv: data.v,
34
+ obvList: [data.v],
34
35
  preClose: data.c,
36
+ obvMa: 0,
35
37
  };
36
38
  }
37
39
  });
@@ -39,7 +41,7 @@ var Obv = /** @class */ (function () {
39
41
  enumerable: false,
40
42
  configurable: true,
41
43
  writable: true,
42
- value: function (data, preList) {
44
+ value: function (data, preList, type) {
43
45
  var currentVolume = data.v;
44
46
  var currentClose = data.c;
45
47
  // obv
@@ -50,10 +52,19 @@ var Obv = /** @class */ (function () {
50
52
  else if (currentClose < preList.preClose) {
51
53
  obv -= currentVolume;
52
54
  }
55
+ // obv Ma
56
+ var obvList = preList.obvList;
57
+ obvList.push(obv);
58
+ if (obvList.length > type)
59
+ obvList.shift();
60
+ var sum = obvList.reduce(function (pre, current) { return pre + current; }, 0);
61
+ var vma = Math.round((sum / type) * 100) / 100;
53
62
  return {
54
63
  dataset: __spreadArray(__spreadArray([], preList.dataset, true), [data], false),
55
64
  obv: obv,
56
65
  preClose: currentClose,
66
+ obvList: obvList,
67
+ obvMa: vma
57
68
  };
58
69
  }
59
70
  });
@@ -5,13 +5,13 @@ var test_data_test_1 = require("./test_data.test");
5
5
  describe("test obv methods", function () {
6
6
  it("test init", function () {
7
7
  var obv = new obv_1.default();
8
- var init = obv.init(test_data_test_1.data_9904[0]);
8
+ var init = obv.init(test_data_test_1.data_9904[0], 5);
9
9
  var res = init;
10
10
  for (var i = 1; i < test_data_test_1.data_9904.length; i++) {
11
11
  var item = test_data_test_1.data_9904[i];
12
- res = obv.next(item, res);
12
+ res = obv.next(item, res, 5);
13
13
  }
14
- expect(res.obv).toEqual(504538);
14
+ expect({ obv: res.obv, obvMa: res.obvMa }).toEqual({ obv: 504538, obvMa: 494302.2 });
15
15
  });
16
16
  it("test getObv()", function () {
17
17
  var kd = new obv_1.default();
@@ -0,0 +1,38 @@
1
+ type DataType = {
2
+ v: number;
3
+ [key: string]: unknown;
4
+ };
5
+ type ListType = DataType[];
6
+ interface VmaType {
7
+ init: (data: DataType, type: number) => {
8
+ dataset: ListType;
9
+ vma: number;
10
+ type: number;
11
+ };
12
+ next: (data: DataType, preList: {
13
+ dataset: ListType;
14
+ vma: number;
15
+ type: number;
16
+ }, type: number) => {
17
+ dataset: ListType;
18
+ vma: number;
19
+ type: number;
20
+ };
21
+ }
22
+ export default class Vma implements VmaType {
23
+ init(data: DataType, type: number): {
24
+ dataset: DataType[];
25
+ vma: number;
26
+ type: number;
27
+ };
28
+ next(data: DataType, preList: {
29
+ dataset: ListType;
30
+ vma: number;
31
+ type: number;
32
+ }, type: number): {
33
+ dataset: ListType;
34
+ vma: number;
35
+ type: number;
36
+ };
37
+ }
38
+ export {};
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var Vma = /** @class */ (function () {
4
+ function Vma() {
5
+ }
6
+ Object.defineProperty(Vma.prototype, "init", {
7
+ enumerable: false,
8
+ configurable: true,
9
+ writable: true,
10
+ value: function (data, type) {
11
+ return { dataset: [data], vma: 0, type: type };
12
+ }
13
+ });
14
+ Object.defineProperty(Vma.prototype, "next", {
15
+ enumerable: false,
16
+ configurable: true,
17
+ writable: true,
18
+ value: function (data, preList, type) {
19
+ preList.dataset.push(data);
20
+ if (preList.dataset.length < type) {
21
+ return { dataset: preList.dataset, vma: 0, type: type };
22
+ }
23
+ else {
24
+ if (preList.dataset.length > type) {
25
+ preList.dataset.shift();
26
+ }
27
+ var sum = preList.dataset.reduce(function (pre, current) { return pre + current.v; }, 0);
28
+ var vma = Math.round((sum / type) * 100) / 100;
29
+ return { dataset: preList.dataset, vma: vma, type: type };
30
+ }
31
+ }
32
+ });
33
+ return Vma;
34
+ }());
35
+ exports.default = Vma;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var test_data_test_1 = require("./test_data.test");
4
+ var vma_1 = require("./vma");
5
+ describe("test vma methods", function () {
6
+ it("test init & next", function () {
7
+ var index = test_data_test_1.default.length - 1;
8
+ var vma = new vma_1.default();
9
+ var init = vma.init(test_data_test_1.default[0], 5);
10
+ var res = init;
11
+ for (var i = 1; i <= index; i++) {
12
+ var item = test_data_test_1.default[i];
13
+ res = vma.next(item, res, 5);
14
+ }
15
+ expect(res.vma).toEqual(12251.6);
16
+ });
17
+ });
@@ -7,6 +7,7 @@ export { default as Ema } from "./stockSkills/ema.js";
7
7
  export { default as Gold } from "./stockSkills/gold.js";
8
8
  export { default as Kd } from "./stockSkills/kd.js";
9
9
  export { default as Ma } from "./stockSkills/ma.js";
10
+ export { default as Vma } from "./stockSkills/vma.js";
10
11
  export { default as Macd } from "./stockSkills/macd.js";
11
12
  export { default as Obv } from "./stockSkills/obv.js";
12
13
  export { default as Rsi } from "./stockSkills/rsi.js";
package/dist/esm/index.js CHANGED
@@ -12,6 +12,7 @@ export { default as Ema } from "./stockSkills/ema.js";
12
12
  export { default as Gold } from "./stockSkills/gold.js";
13
13
  export { default as Kd } from "./stockSkills/kd.js";
14
14
  export { default as Ma } from "./stockSkills/ma.js";
15
+ export { default as Vma } from "./stockSkills/vma.js";
15
16
  export { default as Macd } from "./stockSkills/macd.js";
16
17
  export { default as Obv } from "./stockSkills/obv.js";
17
18
  export { default as Rsi } from "./stockSkills/rsi.js";
@@ -1,5 +1,6 @@
1
1
  type DataType = {
2
2
  c: number;
3
+ [key: string]: unknown;
3
4
  };
4
5
  type ListType = DataType[];
5
6
  type ResMa5 = {
@@ -35,15 +36,27 @@ interface MaType {
35
36
  dataset: ListType;
36
37
  ma: number;
37
38
  type: number;
39
+ exclusionValue: {
40
+ d: number;
41
+ "d-1": number;
42
+ };
38
43
  };
39
44
  next: (data: DataType, preList: {
40
45
  dataset: ListType;
41
46
  ma: number;
42
47
  type: number;
48
+ exclusionValue: {
49
+ d: number;
50
+ "d-1": number;
51
+ };
43
52
  }, type: number) => {
44
53
  dataset: ListType;
45
54
  ma: number;
46
55
  type: number;
56
+ exclusionValue: {
57
+ d: number;
58
+ "d-1": number;
59
+ };
47
60
  };
48
61
  getAllMa: (list: ListType) => ResAllMa;
49
62
  getMa5: (list: ListType) => ResMa5;
@@ -57,6 +70,10 @@ export default class Ma implements MaType {
57
70
  dataset: DataType[];
58
71
  ma: number;
59
72
  type: number;
73
+ exclusionValue: {
74
+ d: number;
75
+ "d-1": number;
76
+ };
60
77
  };
61
78
  next(data: DataType, preList: {
62
79
  dataset: ListType;
@@ -66,6 +83,10 @@ export default class Ma implements MaType {
66
83
  dataset: ListType;
67
84
  ma: number;
68
85
  type: number;
86
+ exclusionValue: {
87
+ d: number;
88
+ "d-1": number;
89
+ };
69
90
  };
70
91
  getAllMa(list: ListType): ResAllMa;
71
92
  getMa5(list: ListType): ResMa5;
@@ -17,7 +17,7 @@ var Ma = /** @class */ (function () {
17
17
  configurable: true,
18
18
  writable: true,
19
19
  value: function (data, type) {
20
- return { dataset: [data], ma: 0, type: type };
20
+ return { dataset: [data], ma: 0, type: type, exclusionValue: { d: 0, "d-1": 0 } };
21
21
  }
22
22
  });
23
23
  Object.defineProperty(Ma.prototype, "next", {
@@ -27,15 +27,23 @@ var Ma = /** @class */ (function () {
27
27
  value: function (data, preList, type) {
28
28
  preList.dataset.push(data);
29
29
  if (preList.dataset.length < type) {
30
- return { dataset: preList.dataset, ma: 0, type: type };
30
+ return {
31
+ dataset: preList.dataset,
32
+ ma: 0,
33
+ type: type,
34
+ exclusionValue: { d: 0, "d-1": 0 },
35
+ };
31
36
  }
32
37
  else {
38
+ var exclusionValue = { d: preList.dataset[0].c, "d-1": 0 };
33
39
  if (preList.dataset.length > type) {
40
+ exclusionValue["d-1"] = exclusionValue.d;
34
41
  preList.dataset.shift();
42
+ exclusionValue.d = preList.dataset[0].c;
35
43
  }
36
44
  var sum = preList.dataset.reduce(function (pre, current) { return pre + current.c; }, 0);
37
45
  var ma = Math.round((sum / type) * 100) / 100;
38
- return { dataset: preList.dataset, ma: ma, type: type };
46
+ return { dataset: preList.dataset, ma: ma, type: type, exclusionValue: exclusionValue };
39
47
  }
40
48
  }
41
49
  });
@@ -8,36 +8,48 @@ type ResObv = {
8
8
  obv: number;
9
9
  } & ItemType;
10
10
  interface ObvType {
11
- init: (data: ItemType) => {
11
+ init: (data: ItemType, type: number) => {
12
12
  dataset: ItemType[];
13
13
  obv: number;
14
+ obvList: number[];
14
15
  preClose: number;
16
+ obvMa: number;
15
17
  };
16
18
  next: (data: ItemType, preList: {
17
19
  dataset: ItemType[];
18
20
  obv: number;
21
+ obvList: number[];
19
22
  preClose: number;
20
- }) => {
23
+ obvMa: number;
24
+ }, type: number) => {
21
25
  dataset: ItemType[];
22
26
  obv: number;
27
+ obvList: number[];
23
28
  preClose: number;
29
+ obvMa: number;
24
30
  };
25
31
  getObv: (list: ItemType[], period: number) => ResObv[];
26
32
  }
27
33
  export default class Obv implements ObvType {
28
- init(data: ItemType): {
34
+ init(data: ItemType, type: number): {
29
35
  dataset: ItemType[];
30
36
  obv: number;
37
+ obvList: number[];
31
38
  preClose: number;
39
+ obvMa: number;
32
40
  };
33
41
  next(data: ItemType, preList: {
34
42
  dataset: ItemType[];
35
43
  obv: number;
44
+ obvList: number[];
36
45
  preClose: number;
37
- }): {
46
+ obvMa: number;
47
+ }, type: number): {
38
48
  dataset: ItemType[];
39
49
  obv: number;
40
50
  preClose: number;
51
+ obvList: number[];
52
+ obvMa: number;
41
53
  };
42
54
  getObv(list: ItemType[]): ResObv[];
43
55
  }
@@ -25,11 +25,13 @@ var Obv = /** @class */ (function () {
25
25
  enumerable: false,
26
26
  configurable: true,
27
27
  writable: true,
28
- value: function (data) {
28
+ value: function (data, type) {
29
29
  return {
30
30
  dataset: [data],
31
31
  obv: data.v,
32
+ obvList: [data.v],
32
33
  preClose: data.c,
34
+ obvMa: 0,
33
35
  };
34
36
  }
35
37
  });
@@ -37,7 +39,7 @@ var Obv = /** @class */ (function () {
37
39
  enumerable: false,
38
40
  configurable: true,
39
41
  writable: true,
40
- value: function (data, preList) {
42
+ value: function (data, preList, type) {
41
43
  var currentVolume = data.v;
42
44
  var currentClose = data.c;
43
45
  // obv
@@ -48,10 +50,19 @@ var Obv = /** @class */ (function () {
48
50
  else if (currentClose < preList.preClose) {
49
51
  obv -= currentVolume;
50
52
  }
53
+ // obv Ma
54
+ var obvList = preList.obvList;
55
+ obvList.push(obv);
56
+ if (obvList.length > type)
57
+ obvList.shift();
58
+ var sum = obvList.reduce(function (pre, current) { return pre + current; }, 0);
59
+ var vma = Math.round((sum / type) * 100) / 100;
51
60
  return {
52
61
  dataset: __spreadArray(__spreadArray([], preList.dataset, true), [data], false),
53
62
  obv: obv,
54
63
  preClose: currentClose,
64
+ obvList: obvList,
65
+ obvMa: vma
55
66
  };
56
67
  }
57
68
  });
@@ -3,13 +3,13 @@ import { data_9904 as data } from "./test_data.test";
3
3
  describe("test obv methods", function () {
4
4
  it("test init", function () {
5
5
  var obv = new Obv();
6
- var init = obv.init(data[0]);
6
+ var init = obv.init(data[0], 5);
7
7
  var res = init;
8
8
  for (var i = 1; i < data.length; i++) {
9
9
  var item = data[i];
10
- res = obv.next(item, res);
10
+ res = obv.next(item, res, 5);
11
11
  }
12
- expect(res.obv).toEqual(504538);
12
+ expect({ obv: res.obv, obvMa: res.obvMa }).toEqual({ obv: 504538, obvMa: 494302.2 });
13
13
  });
14
14
  it("test getObv()", function () {
15
15
  var kd = new Obv();
@@ -0,0 +1,38 @@
1
+ type DataType = {
2
+ v: number;
3
+ [key: string]: unknown;
4
+ };
5
+ type ListType = DataType[];
6
+ interface VmaType {
7
+ init: (data: DataType, type: number) => {
8
+ dataset: ListType;
9
+ vma: number;
10
+ type: number;
11
+ };
12
+ next: (data: DataType, preList: {
13
+ dataset: ListType;
14
+ vma: number;
15
+ type: number;
16
+ }, type: number) => {
17
+ dataset: ListType;
18
+ vma: number;
19
+ type: number;
20
+ };
21
+ }
22
+ export default class Vma implements VmaType {
23
+ init(data: DataType, type: number): {
24
+ dataset: DataType[];
25
+ vma: number;
26
+ type: number;
27
+ };
28
+ next(data: DataType, preList: {
29
+ dataset: ListType;
30
+ vma: number;
31
+ type: number;
32
+ }, type: number): {
33
+ dataset: ListType;
34
+ vma: number;
35
+ type: number;
36
+ };
37
+ }
38
+ export {};
@@ -0,0 +1,33 @@
1
+ var Vma = /** @class */ (function () {
2
+ function Vma() {
3
+ }
4
+ Object.defineProperty(Vma.prototype, "init", {
5
+ enumerable: false,
6
+ configurable: true,
7
+ writable: true,
8
+ value: function (data, type) {
9
+ return { dataset: [data], vma: 0, type: type };
10
+ }
11
+ });
12
+ Object.defineProperty(Vma.prototype, "next", {
13
+ enumerable: false,
14
+ configurable: true,
15
+ writable: true,
16
+ value: function (data, preList, type) {
17
+ preList.dataset.push(data);
18
+ if (preList.dataset.length < type) {
19
+ return { dataset: preList.dataset, vma: 0, type: type };
20
+ }
21
+ else {
22
+ if (preList.dataset.length > type) {
23
+ preList.dataset.shift();
24
+ }
25
+ var sum = preList.dataset.reduce(function (pre, current) { return pre + current.v; }, 0);
26
+ var vma = Math.round((sum / type) * 100) / 100;
27
+ return { dataset: preList.dataset, vma: vma, type: type };
28
+ }
29
+ }
30
+ });
31
+ return Vma;
32
+ }());
33
+ export default Vma;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ import data from "./test_data.test";
2
+ import Vma from "./vma";
3
+ describe("test vma methods", function () {
4
+ it("test init & next", function () {
5
+ var index = data.length - 1;
6
+ var vma = new Vma();
7
+ var init = vma.init(data[0], 5);
8
+ var res = init;
9
+ for (var i = 1; i <= index; i++) {
10
+ var item = data[i];
11
+ res = vma.next(item, res, 5);
12
+ }
13
+ expect(res.vma).toEqual(12251.6);
14
+ });
15
+ });
package/dist/umd/index.js CHANGED
@@ -741,7 +741,7 @@
741
741
  configurable: true,
742
742
  writable: true,
743
743
  value: function (data, type) {
744
- return { dataset: [data], ma: 0, type: type };
744
+ return { dataset: [data], ma: 0, type: type, exclusionValue: { d: 0, "d-1": 0 } };
745
745
  }
746
746
  });
747
747
  Object.defineProperty(Ma.prototype, "next", {
@@ -751,15 +751,23 @@
751
751
  value: function (data, preList, type) {
752
752
  preList.dataset.push(data);
753
753
  if (preList.dataset.length < type) {
754
- return { dataset: preList.dataset, ma: 0, type: type };
754
+ return {
755
+ dataset: preList.dataset,
756
+ ma: 0,
757
+ type: type,
758
+ exclusionValue: { d: 0, "d-1": 0 },
759
+ };
755
760
  }
756
761
  else {
762
+ var exclusionValue = { d: preList.dataset[0].c, "d-1": 0 };
757
763
  if (preList.dataset.length > type) {
764
+ exclusionValue["d-1"] = exclusionValue.d;
758
765
  preList.dataset.shift();
766
+ exclusionValue.d = preList.dataset[0].c;
759
767
  }
760
768
  var sum = preList.dataset.reduce(function (pre, current) { return pre + current.c; }, 0);
761
769
  var ma = Math.round((sum / type) * 100) / 100;
762
- return { dataset: preList.dataset, ma: ma, type: type };
770
+ return { dataset: preList.dataset, ma: ma, type: type, exclusionValue: exclusionValue };
763
771
  }
764
772
  }
765
773
  });
@@ -882,6 +890,39 @@
882
890
  return Ma;
883
891
  }());
884
892
 
893
+ var Vma = /** @class */ (function () {
894
+ function Vma() {
895
+ }
896
+ Object.defineProperty(Vma.prototype, "init", {
897
+ enumerable: false,
898
+ configurable: true,
899
+ writable: true,
900
+ value: function (data, type) {
901
+ return { dataset: [data], vma: 0, type: type };
902
+ }
903
+ });
904
+ Object.defineProperty(Vma.prototype, "next", {
905
+ enumerable: false,
906
+ configurable: true,
907
+ writable: true,
908
+ value: function (data, preList, type) {
909
+ preList.dataset.push(data);
910
+ if (preList.dataset.length < type) {
911
+ return { dataset: preList.dataset, vma: 0, type: type };
912
+ }
913
+ else {
914
+ if (preList.dataset.length > type) {
915
+ preList.dataset.shift();
916
+ }
917
+ var sum = preList.dataset.reduce(function (pre, current) { return pre + current.v; }, 0);
918
+ var vma = Math.round((sum / type) * 100) / 100;
919
+ return { dataset: preList.dataset, vma: vma, type: type };
920
+ }
921
+ }
922
+ });
923
+ return Vma;
924
+ }());
925
+
885
926
  var __assign$3 = (undefined && undefined.__assign) || function () {
886
927
  __assign$3 = Object.assign || function(t) {
887
928
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -1138,11 +1179,13 @@
1138
1179
  enumerable: false,
1139
1180
  configurable: true,
1140
1181
  writable: true,
1141
- value: function (data) {
1182
+ value: function (data, type) {
1142
1183
  return {
1143
1184
  dataset: [data],
1144
1185
  obv: data.v,
1186
+ obvList: [data.v],
1145
1187
  preClose: data.c,
1188
+ obvMa: 0,
1146
1189
  };
1147
1190
  }
1148
1191
  });
@@ -1150,7 +1193,7 @@
1150
1193
  enumerable: false,
1151
1194
  configurable: true,
1152
1195
  writable: true,
1153
- value: function (data, preList) {
1196
+ value: function (data, preList, type) {
1154
1197
  var currentVolume = data.v;
1155
1198
  var currentClose = data.c;
1156
1199
  // obv
@@ -1161,10 +1204,19 @@
1161
1204
  else if (currentClose < preList.preClose) {
1162
1205
  obv -= currentVolume;
1163
1206
  }
1207
+ // obv Ma
1208
+ var obvList = preList.obvList;
1209
+ obvList.push(obv);
1210
+ if (obvList.length > type)
1211
+ obvList.shift();
1212
+ var sum = obvList.reduce(function (pre, current) { return pre + current; }, 0);
1213
+ var vma = Math.round((sum / type) * 100) / 100;
1164
1214
  return {
1165
1215
  dataset: __spreadArray(__spreadArray([], preList.dataset, true), [data], false),
1166
1216
  obv: obv,
1167
1217
  preClose: currentClose,
1218
+ obvList: obvList,
1219
+ obvMa: vma
1168
1220
  };
1169
1221
  }
1170
1222
  });
@@ -1515,6 +1567,7 @@
1515
1567
  exports.Macd = MACD;
1516
1568
  exports.Obv = Obv;
1517
1569
  exports.Rsi = Rsi;
1570
+ exports.Vma = Vma;
1518
1571
  exports.Williams = Williams;
1519
1572
  exports.add = add;
1520
1573
  exports.calcSeasonalIndicesNoTrend = calcSeasonalIndicesNoTrend;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ch20026103/anysis",
3
- "version": "0.0.8-alpha",
3
+ "version": "0.0.10-alpha",
4
4
  "description": "provide many analyze methods in the library.",
5
5
  "keywords": [],
6
6
  "bugs": "git@github.com:cosmic1330/anysis/issues",
@@ -12,6 +12,21 @@
12
12
  "module": "dist/esm/index.js",
13
13
  "browser": "dist/umd/index.js",
14
14
  "types": "dist/esm/index.d.ts",
15
+ "scripts": {
16
+ "build": "pnpm clean && pnpm build:esm && pnpm build:cjs && pnpm build:umd",
17
+ "build:cjs": "tsc --module commonjs --target es5 --outDir dist/cjs",
18
+ "build:esm": "tsc --module esnext --target es5 --outDir dist/esm",
19
+ "build:umd": "rollup dist/esm/index.js --file dist/umd/index.js --format umd --name anysis",
20
+ "clean": "rimraf dist",
21
+ "lint": "eslint --fix \"**/*.{js,jsx,ts,tsx,mjs}\"",
22
+ "prepare": "pnpm exec husky install",
23
+ "prettier": "pnpm exec prettier --write .",
24
+ "prepublishOnly": "pnpm build",
25
+ "published": "npm publish",
26
+ "unpublished": "npm unpublish --force --registry http://localhost:4873 ",
27
+ "test": "vitest",
28
+ "demo": "node demo/main.js"
29
+ },
15
30
  "lint-staged": {
16
31
  "*.{js,jsx,ts,tsx,mjs}": [
17
32
  "pnpm lint"
@@ -42,18 +57,5 @@
42
57
  },
43
58
  "vota": {
44
59
  "node": "16.11.0"
45
- },
46
- "scripts": {
47
- "build": "pnpm clean && pnpm build:esm && pnpm build:cjs && pnpm build:umd",
48
- "build:cjs": "tsc --module commonjs --target es5 --outDir dist/cjs",
49
- "build:esm": "tsc --module esnext --target es5 --outDir dist/esm",
50
- "build:umd": "rollup dist/esm/index.js --file dist/umd/index.js --format umd --name anysis",
51
- "clean": "rimraf dist",
52
- "lint": "eslint --fix \"**/*.{js,jsx,ts,tsx,mjs}\"",
53
- "prettier": "pnpm exec prettier --write .",
54
- "published": "npm publish",
55
- "unpublished": "npm unpublish --force --registry http://localhost:4873 ",
56
- "test": "vitest",
57
- "demo": "node demo.js"
58
60
  }
59
- }
61
+ }