@ch20026103/anysis 0.0.11-alpha → 0.0.12-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 CHANGED
@@ -1,77 +1,24 @@
1
1
  /* eslint @typescript-eslint/no-var-requires: "off" */
2
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() {
3
+ const { Williams } = require("../dist/cjs/index.js");
4
+ const williams = new Williams();
5
+ function DemoDay(stockId) {
15
6
  axios
16
7
  .get(
17
8
  `https://tw.quote.finance.yahoo.net/quote/q?type=ta&perd=d&mkt=10&sym=${stockId}&v=1&callback=`
18
9
  )
19
10
  .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
-
11
+ res = res.data.replace(/^\(|\);$/g, "");
12
+ let parse = JSON.parse(res);
13
+ let data = parse.ta;
14
+ let williams9Data = williams.init(data[0], 9);
15
+ let williams18Data = williams.init(data[0], 18);
48
16
  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
- });
17
+ williams9Data = williams.next(data[i], williams9Data, 9);
18
+ williams18Data = williams.next(data[i], williams18Data, 18);
72
19
  }
73
-
74
- console.log(finallyData[finallyData.length - 1]);
20
+ console.log(williams9Data);
21
+ console.log(williams18Data);
75
22
  })
76
23
  .catch((error) => {
77
24
  console.error(error);
@@ -81,45 +28,4 @@ function DemoDay() {
81
28
  });
82
29
  }
83
30
 
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
- }
31
+ DemoDay("2449");
@@ -7,14 +7,29 @@ describe("test Angle methods", function () {
7
7
  var angle = (0, index_1.default)(y);
8
8
  expect(angle).toEqual(45);
9
9
  });
10
- it("test (0,0) (1,10)", function () {
10
+ it("test [0, 200]", function () {
11
11
  var y = [0, 200];
12
12
  var angle = (0, index_1.default)(y);
13
13
  expect(angle).toEqual(63.43494882292201);
14
14
  });
15
- it("test (0,0) (1,0)", function () {
15
+ it("test [0, 0]", function () {
16
16
  var y = [0, 0];
17
17
  var angle = (0, index_1.default)(y);
18
18
  expect(angle).toEqual(0);
19
19
  });
20
+ it("test [39.73, 48.15]", function () {
21
+ var y = [39.73, 48.15];
22
+ var angle = (0, index_1.default)(y);
23
+ expect(angle).toEqual(40.09737861178007);
24
+ });
25
+ it("test [15.46, 30.19]", function () {
26
+ var y = [15.46, 30.19];
27
+ var angle = (0, index_1.default)(y);
28
+ expect(angle).toEqual(55.82794164843938);
29
+ });
30
+ it("test [35.64, 37.09]", function () {
31
+ var y = [35.64, 37.09];
32
+ var angle = (0, index_1.default)(y);
33
+ expect(angle).toEqual(8.250387228905515);
34
+ });
20
35
  });
@@ -1,8 +1,9 @@
1
- type ListType = {
1
+ type DataType = {
2
2
  h: number;
3
3
  l: number;
4
4
  c: number;
5
- }[];
5
+ };
6
+ type ListType = DataType[];
6
7
  type ResWilliams9Type = {
7
8
  c: number;
8
9
  williams9: number | null;
@@ -17,11 +18,39 @@ type ResAllWilliamsType = {
17
18
  williams9: number | null;
18
19
  }[];
19
20
  interface WilliamsType {
21
+ init: (data: DataType, type: number) => {
22
+ dataset: ListType;
23
+ williams: number;
24
+ type: number;
25
+ };
26
+ next: (data: DataType, preList: {
27
+ dataset: ListType;
28
+ williams: number;
29
+ type: number;
30
+ }, type: number) => {
31
+ dataset: ListType;
32
+ williams: number;
33
+ type: number;
34
+ };
20
35
  getWilliams9: (list: ListType) => ResWilliams9Type;
21
36
  getWilliams18: (list: ListType) => ResWilliams18Type;
22
37
  getAllWillams(list: ListType): ResAllWilliamsType;
23
38
  }
24
39
  export default class Williams implements WilliamsType {
40
+ init(data: DataType, type: number): {
41
+ dataset: DataType[];
42
+ williams: number;
43
+ type: number;
44
+ };
45
+ next(data: DataType, preList: {
46
+ dataset: ListType;
47
+ williams: number;
48
+ type: number;
49
+ }, type: number): {
50
+ dataset: ListType;
51
+ williams: number;
52
+ type: number;
53
+ };
25
54
  getAllWillams(list: ListType): ResAllWilliamsType;
26
55
  getWilliams9(list: ListType): ResWilliams9Type;
27
56
  getWilliams18(list: ListType): ResWilliams18Type;
@@ -14,6 +14,42 @@ Object.defineProperty(exports, "__esModule", { value: true });
14
14
  var Williams = /** @class */ (function () {
15
15
  function Williams() {
16
16
  }
17
+ Object.defineProperty(Williams.prototype, "init", {
18
+ enumerable: false,
19
+ configurable: true,
20
+ writable: true,
21
+ value: function (data, type) {
22
+ return { dataset: [data], williams: 0, type: type };
23
+ }
24
+ });
25
+ Object.defineProperty(Williams.prototype, "next", {
26
+ enumerable: false,
27
+ configurable: true,
28
+ writable: true,
29
+ value: function (data, preList, type) {
30
+ preList.dataset.push(data);
31
+ if (preList.dataset.length < type) {
32
+ return {
33
+ dataset: preList.dataset,
34
+ williams: 0,
35
+ type: type,
36
+ };
37
+ }
38
+ else {
39
+ if (preList.dataset.length > type) {
40
+ preList.dataset.shift();
41
+ }
42
+ var maxList = preList.dataset.map(function (item) { return item["h"]; });
43
+ var minList = preList.dataset.map(function (item) { return item["l"]; });
44
+ var max = Math.max.apply(Math, maxList);
45
+ var min = Math.min.apply(Math, minList);
46
+ var close_1 = data.c;
47
+ var williams = ((max - close_1) / (max - min)) * -100;
48
+ williams = Math.round(williams * 100) / 100;
49
+ return { dataset: preList.dataset, williams: williams, type: type };
50
+ }
51
+ }
52
+ });
17
53
  Object.defineProperty(Williams.prototype, "getAllWillams", {
18
54
  enumerable: false,
19
55
  configurable: true,
@@ -42,8 +78,8 @@ var Williams = /** @class */ (function () {
42
78
  var minList = list.slice(i - 8, i).map(function (item) { return item["l"]; });
43
79
  var max = Math.max.apply(Math, maxList);
44
80
  var min = Math.min.apply(Math, minList);
45
- var close_1 = list[i]["c"];
46
- var williams9 = ((max - close_1) / (max - min)) * -100;
81
+ var close_2 = list[i]["c"];
82
+ var williams9 = ((max - close_2) / (max - min)) * -100;
47
83
  res[i] = __assign(__assign({}, list[i]), { williams9: Math.round(williams9 * 100) / 100 });
48
84
  }
49
85
  }
@@ -64,8 +100,8 @@ var Williams = /** @class */ (function () {
64
100
  var minList = list.slice(i - 17, i).map(function (item) { return item["l"]; });
65
101
  var max = Math.max.apply(Math, maxList);
66
102
  var min = Math.min.apply(Math, minList);
67
- var close_2 = list[i]["c"];
68
- var williams18 = ((max - close_2) / (max - min)) * -100;
103
+ var close_3 = list[i]["c"];
104
+ var williams18 = ((max - close_3) / (max - min)) * -100;
69
105
  res[i] = __assign(__assign({}, list[i]), { williams18: Math.round(williams18 * 100) / 100 });
70
106
  }
71
107
  }
@@ -3,6 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var williams_1 = require("./williams");
4
4
  var test_data_test_1 = require("./test_data.test");
5
5
  describe("test williams methods", function () {
6
+ it("test next()", function () {
7
+ var index = test_data_test_1.default.length - 1;
8
+ var williams = new williams_1.default();
9
+ var init = williams.init(test_data_test_1.default[0], 9);
10
+ var res = init;
11
+ for (var i = 1; i <= index; i++) {
12
+ var item = test_data_test_1.default[i];
13
+ res = williams.next(item, res, 9);
14
+ }
15
+ expect(res.williams).toEqual(-79.59);
16
+ });
6
17
  it("test getWilliams9()", function () {
7
18
  var williams = new williams_1.default();
8
19
  expect(williams.getWilliams9(test_data_test_1.default)[test_data_test_1.default.length - 1]).toEqual({
@@ -5,14 +5,29 @@ describe("test Angle methods", function () {
5
5
  var angle = Angle(y);
6
6
  expect(angle).toEqual(45);
7
7
  });
8
- it("test (0,0) (1,10)", function () {
8
+ it("test [0, 200]", function () {
9
9
  var y = [0, 200];
10
10
  var angle = Angle(y);
11
11
  expect(angle).toEqual(63.43494882292201);
12
12
  });
13
- it("test (0,0) (1,0)", function () {
13
+ it("test [0, 0]", function () {
14
14
  var y = [0, 0];
15
15
  var angle = Angle(y);
16
16
  expect(angle).toEqual(0);
17
17
  });
18
+ it("test [39.73, 48.15]", function () {
19
+ var y = [39.73, 48.15];
20
+ var angle = Angle(y);
21
+ expect(angle).toEqual(40.09737861178007);
22
+ });
23
+ it("test [15.46, 30.19]", function () {
24
+ var y = [15.46, 30.19];
25
+ var angle = Angle(y);
26
+ expect(angle).toEqual(55.82794164843938);
27
+ });
28
+ it("test [35.64, 37.09]", function () {
29
+ var y = [35.64, 37.09];
30
+ var angle = Angle(y);
31
+ expect(angle).toEqual(8.250387228905515);
32
+ });
18
33
  });
@@ -1,8 +1,9 @@
1
- type ListType = {
1
+ type DataType = {
2
2
  h: number;
3
3
  l: number;
4
4
  c: number;
5
- }[];
5
+ };
6
+ type ListType = DataType[];
6
7
  type ResWilliams9Type = {
7
8
  c: number;
8
9
  williams9: number | null;
@@ -17,11 +18,39 @@ type ResAllWilliamsType = {
17
18
  williams9: number | null;
18
19
  }[];
19
20
  interface WilliamsType {
21
+ init: (data: DataType, type: number) => {
22
+ dataset: ListType;
23
+ williams: number;
24
+ type: number;
25
+ };
26
+ next: (data: DataType, preList: {
27
+ dataset: ListType;
28
+ williams: number;
29
+ type: number;
30
+ }, type: number) => {
31
+ dataset: ListType;
32
+ williams: number;
33
+ type: number;
34
+ };
20
35
  getWilliams9: (list: ListType) => ResWilliams9Type;
21
36
  getWilliams18: (list: ListType) => ResWilliams18Type;
22
37
  getAllWillams(list: ListType): ResAllWilliamsType;
23
38
  }
24
39
  export default class Williams implements WilliamsType {
40
+ init(data: DataType, type: number): {
41
+ dataset: DataType[];
42
+ williams: number;
43
+ type: number;
44
+ };
45
+ next(data: DataType, preList: {
46
+ dataset: ListType;
47
+ williams: number;
48
+ type: number;
49
+ }, type: number): {
50
+ dataset: ListType;
51
+ williams: number;
52
+ type: number;
53
+ };
25
54
  getAllWillams(list: ListType): ResAllWilliamsType;
26
55
  getWilliams9(list: ListType): ResWilliams9Type;
27
56
  getWilliams18(list: ListType): ResWilliams18Type;
@@ -12,6 +12,42 @@ var __assign = (this && this.__assign) || function () {
12
12
  var Williams = /** @class */ (function () {
13
13
  function Williams() {
14
14
  }
15
+ Object.defineProperty(Williams.prototype, "init", {
16
+ enumerable: false,
17
+ configurable: true,
18
+ writable: true,
19
+ value: function (data, type) {
20
+ return { dataset: [data], williams: 0, type: type };
21
+ }
22
+ });
23
+ Object.defineProperty(Williams.prototype, "next", {
24
+ enumerable: false,
25
+ configurable: true,
26
+ writable: true,
27
+ value: function (data, preList, type) {
28
+ preList.dataset.push(data);
29
+ if (preList.dataset.length < type) {
30
+ return {
31
+ dataset: preList.dataset,
32
+ williams: 0,
33
+ type: type,
34
+ };
35
+ }
36
+ else {
37
+ if (preList.dataset.length > type) {
38
+ preList.dataset.shift();
39
+ }
40
+ var maxList = preList.dataset.map(function (item) { return item["h"]; });
41
+ var minList = preList.dataset.map(function (item) { return item["l"]; });
42
+ var max = Math.max.apply(Math, maxList);
43
+ var min = Math.min.apply(Math, minList);
44
+ var close_1 = data.c;
45
+ var williams = ((max - close_1) / (max - min)) * -100;
46
+ williams = Math.round(williams * 100) / 100;
47
+ return { dataset: preList.dataset, williams: williams, type: type };
48
+ }
49
+ }
50
+ });
15
51
  Object.defineProperty(Williams.prototype, "getAllWillams", {
16
52
  enumerable: false,
17
53
  configurable: true,
@@ -40,8 +76,8 @@ var Williams = /** @class */ (function () {
40
76
  var minList = list.slice(i - 8, i).map(function (item) { return item["l"]; });
41
77
  var max = Math.max.apply(Math, maxList);
42
78
  var min = Math.min.apply(Math, minList);
43
- var close_1 = list[i]["c"];
44
- var williams9 = ((max - close_1) / (max - min)) * -100;
79
+ var close_2 = list[i]["c"];
80
+ var williams9 = ((max - close_2) / (max - min)) * -100;
45
81
  res[i] = __assign(__assign({}, list[i]), { williams9: Math.round(williams9 * 100) / 100 });
46
82
  }
47
83
  }
@@ -62,8 +98,8 @@ var Williams = /** @class */ (function () {
62
98
  var minList = list.slice(i - 17, i).map(function (item) { return item["l"]; });
63
99
  var max = Math.max.apply(Math, maxList);
64
100
  var min = Math.min.apply(Math, minList);
65
- var close_2 = list[i]["c"];
66
- var williams18 = ((max - close_2) / (max - min)) * -100;
101
+ var close_3 = list[i]["c"];
102
+ var williams18 = ((max - close_3) / (max - min)) * -100;
67
103
  res[i] = __assign(__assign({}, list[i]), { williams18: Math.round(williams18 * 100) / 100 });
68
104
  }
69
105
  }
@@ -1,6 +1,17 @@
1
1
  import Williams from "./williams";
2
2
  import data from "./test_data.test";
3
3
  describe("test williams methods", function () {
4
+ it("test next()", function () {
5
+ var index = data.length - 1;
6
+ var williams = new Williams();
7
+ var init = williams.init(data[0], 9);
8
+ var res = init;
9
+ for (var i = 1; i <= index; i++) {
10
+ var item = data[i];
11
+ res = williams.next(item, res, 9);
12
+ }
13
+ expect(res.williams).toEqual(-79.59);
14
+ });
4
15
  it("test getWilliams9()", function () {
5
16
  var williams = new Williams();
6
17
  expect(williams.getWilliams9(data)[data.length - 1]).toEqual({
package/dist/umd/index.js CHANGED
@@ -1492,6 +1492,42 @@
1492
1492
  var Williams = /** @class */ (function () {
1493
1493
  function Williams() {
1494
1494
  }
1495
+ Object.defineProperty(Williams.prototype, "init", {
1496
+ enumerable: false,
1497
+ configurable: true,
1498
+ writable: true,
1499
+ value: function (data, type) {
1500
+ return { dataset: [data], williams: 0, type: type };
1501
+ }
1502
+ });
1503
+ Object.defineProperty(Williams.prototype, "next", {
1504
+ enumerable: false,
1505
+ configurable: true,
1506
+ writable: true,
1507
+ value: function (data, preList, type) {
1508
+ preList.dataset.push(data);
1509
+ if (preList.dataset.length < type) {
1510
+ return {
1511
+ dataset: preList.dataset,
1512
+ williams: 0,
1513
+ type: type,
1514
+ };
1515
+ }
1516
+ else {
1517
+ if (preList.dataset.length > type) {
1518
+ preList.dataset.shift();
1519
+ }
1520
+ var maxList = preList.dataset.map(function (item) { return item["h"]; });
1521
+ var minList = preList.dataset.map(function (item) { return item["l"]; });
1522
+ var max = Math.max.apply(Math, maxList);
1523
+ var min = Math.min.apply(Math, minList);
1524
+ var close_1 = data.c;
1525
+ var williams = ((max - close_1) / (max - min)) * -100;
1526
+ williams = Math.round(williams * 100) / 100;
1527
+ return { dataset: preList.dataset, williams: williams, type: type };
1528
+ }
1529
+ }
1530
+ });
1495
1531
  Object.defineProperty(Williams.prototype, "getAllWillams", {
1496
1532
  enumerable: false,
1497
1533
  configurable: true,
@@ -1520,8 +1556,8 @@
1520
1556
  var minList = list.slice(i - 8, i).map(function (item) { return item["l"]; });
1521
1557
  var max = Math.max.apply(Math, maxList);
1522
1558
  var min = Math.min.apply(Math, minList);
1523
- var close_1 = list[i]["c"];
1524
- var williams9 = ((max - close_1) / (max - min)) * -100;
1559
+ var close_2 = list[i]["c"];
1560
+ var williams9 = ((max - close_2) / (max - min)) * -100;
1525
1561
  res[i] = __assign(__assign({}, list[i]), { williams9: Math.round(williams9 * 100) / 100 });
1526
1562
  }
1527
1563
  }
@@ -1542,8 +1578,8 @@
1542
1578
  var minList = list.slice(i - 17, i).map(function (item) { return item["l"]; });
1543
1579
  var max = Math.max.apply(Math, maxList);
1544
1580
  var min = Math.min.apply(Math, minList);
1545
- var close_2 = list[i]["c"];
1546
- var williams18 = ((max - close_2) / (max - min)) * -100;
1581
+ var close_3 = list[i]["c"];
1582
+ var williams18 = ((max - close_3) / (max - min)) * -100;
1547
1583
  res[i] = __assign(__assign({}, list[i]), { williams18: Math.round(williams18 * 100) / 100 });
1548
1584
  }
1549
1585
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ch20026103/anysis",
3
- "version": "0.0.11-alpha",
3
+ "version": "0.0.12-alpha",
4
4
  "description": "provide many analyze methods in the library.",
5
5
  "keywords": [],
6
6
  "bugs": "git@github.com:cosmic1330/anysis/issues",