@tradingaction/indicators 2.0.1

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.
Files changed (219) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +5 -0
  3. package/lib/calculator/atr.d.ts +18 -0
  4. package/lib/calculator/atr.js +51 -0
  5. package/lib/calculator/atr.js.map +1 -0
  6. package/lib/calculator/bollingerband.d.ts +14 -0
  7. package/lib/calculator/bollingerband.js +72 -0
  8. package/lib/calculator/bollingerband.js.map +1 -0
  9. package/lib/calculator/change.d.ts +14 -0
  10. package/lib/calculator/change.js +30 -0
  11. package/lib/calculator/change.js.map +1 -0
  12. package/lib/calculator/compare.d.ts +10 -0
  13. package/lib/calculator/compare.js +50 -0
  14. package/lib/calculator/compare.js.map +1 -0
  15. package/lib/calculator/defaultOptionsForComputation.d.ts +91 -0
  16. package/lib/calculator/defaultOptionsForComputation.js +100 -0
  17. package/lib/calculator/defaultOptionsForComputation.js.map +1 -0
  18. package/lib/calculator/elderRay.d.ts +23 -0
  19. package/lib/calculator/elderRay.js +69 -0
  20. package/lib/calculator/elderRay.js.map +1 -0
  21. package/lib/calculator/ema.d.ts +12 -0
  22. package/lib/calculator/ema.js +73 -0
  23. package/lib/calculator/ema.js.map +1 -0
  24. package/lib/calculator/forceIndex.d.ts +12 -0
  25. package/lib/calculator/forceIndex.js +27 -0
  26. package/lib/calculator/forceIndex.js.map +1 -0
  27. package/lib/calculator/heikinAshi.d.ts +4 -0
  28. package/lib/calculator/heikinAshi.js +30 -0
  29. package/lib/calculator/heikinAshi.js.map +1 -0
  30. package/lib/calculator/index.d.ts +19 -0
  31. package/lib/calculator/index.js +20 -0
  32. package/lib/calculator/index.js.map +1 -0
  33. package/lib/calculator/kagi.d.ts +11 -0
  34. package/lib/calculator/kagi.js +200 -0
  35. package/lib/calculator/kagi.js.map +1 -0
  36. package/lib/calculator/macd.d.ts +18 -0
  37. package/lib/calculator/macd.js +57 -0
  38. package/lib/calculator/macd.js.map +1 -0
  39. package/lib/calculator/pointAndFigure.d.ts +10 -0
  40. package/lib/calculator/pointAndFigure.js +215 -0
  41. package/lib/calculator/pointAndFigure.js.map +1 -0
  42. package/lib/calculator/renko.d.ts +12 -0
  43. package/lib/calculator/renko.js +170 -0
  44. package/lib/calculator/renko.js.map +1 -0
  45. package/lib/calculator/rsi.d.ts +12 -0
  46. package/lib/calculator/rsi.js +87 -0
  47. package/lib/calculator/rsi.js.map +1 -0
  48. package/lib/calculator/sar.d.ts +12 -0
  49. package/lib/calculator/sar.js +87 -0
  50. package/lib/calculator/sar.js.map +1 -0
  51. package/lib/calculator/sma.d.ts +12 -0
  52. package/lib/calculator/sma.js +27 -0
  53. package/lib/calculator/sma.js.map +1 -0
  54. package/lib/calculator/smoothedForceIndex.d.ts +13 -0
  55. package/lib/calculator/smoothedForceIndex.js +40 -0
  56. package/lib/calculator/smoothedForceIndex.js.map +1 -0
  57. package/lib/calculator/sto.d.ts +19 -0
  58. package/lib/calculator/sto.js +91 -0
  59. package/lib/calculator/sto.js.map +1 -0
  60. package/lib/calculator/tma.d.ts +12 -0
  61. package/lib/calculator/tma.js +66 -0
  62. package/lib/calculator/tma.js.map +1 -0
  63. package/lib/calculator/wma.d.ts +12 -0
  64. package/lib/calculator/wma.js +33 -0
  65. package/lib/calculator/wma.js.map +1 -0
  66. package/lib/index.d.ts +1 -0
  67. package/lib/index.js +2 -0
  68. package/lib/index.js.map +1 -0
  69. package/lib/indicator/algorithm.d.ts +10 -0
  70. package/lib/indicator/algorithm.js +35 -0
  71. package/lib/indicator/algorithm.js.map +1 -0
  72. package/lib/indicator/atr.d.ts +26 -0
  73. package/lib/indicator/atr.js +27 -0
  74. package/lib/indicator/atr.js.map +1 -0
  75. package/lib/indicator/baseIndicator.d.ts +16 -0
  76. package/lib/indicator/baseIndicator.js +59 -0
  77. package/lib/indicator/baseIndicator.js.map +1 -0
  78. package/lib/indicator/bollingerBand.d.ts +24 -0
  79. package/lib/indicator/bollingerBand.js +27 -0
  80. package/lib/indicator/bollingerBand.js.map +1 -0
  81. package/lib/indicator/change.d.ts +24 -0
  82. package/lib/indicator/change.js +25 -0
  83. package/lib/indicator/change.js.map +1 -0
  84. package/lib/indicator/compare.d.ts +24 -0
  85. package/lib/indicator/compare.js +29 -0
  86. package/lib/indicator/compare.js.map +1 -0
  87. package/lib/indicator/defaultOptionsForAppearance.d.ts +105 -0
  88. package/lib/indicator/defaultOptionsForAppearance.js +77 -0
  89. package/lib/indicator/defaultOptionsForAppearance.js.map +1 -0
  90. package/lib/indicator/elderImpulse.d.ts +7 -0
  91. package/lib/indicator/elderImpulse.js +61 -0
  92. package/lib/indicator/elderImpulse.js.map +1 -0
  93. package/lib/indicator/elderRay.d.ts +24 -0
  94. package/lib/indicator/elderRay.js +29 -0
  95. package/lib/indicator/elderRay.js.map +1 -0
  96. package/lib/indicator/ema.d.ts +24 -0
  97. package/lib/indicator/ema.js +29 -0
  98. package/lib/indicator/ema.js.map +1 -0
  99. package/lib/indicator/forceIndex.d.ts +24 -0
  100. package/lib/indicator/forceIndex.js +29 -0
  101. package/lib/indicator/forceIndex.js.map +1 -0
  102. package/lib/indicator/heikinAshi.d.ts +3 -0
  103. package/lib/indicator/heikinAshi.js +28 -0
  104. package/lib/indicator/heikinAshi.js.map +1 -0
  105. package/lib/indicator/index.d.ts +23 -0
  106. package/lib/indicator/index.js +24 -0
  107. package/lib/indicator/index.js.map +1 -0
  108. package/lib/indicator/kagi.d.ts +11 -0
  109. package/lib/indicator/kagi.js +13 -0
  110. package/lib/indicator/kagi.js.map +1 -0
  111. package/lib/indicator/macd.d.ts +24 -0
  112. package/lib/indicator/macd.js +32 -0
  113. package/lib/indicator/macd.js.map +1 -0
  114. package/lib/indicator/pointAndFigure.d.ts +10 -0
  115. package/lib/indicator/pointAndFigure.js +13 -0
  116. package/lib/indicator/pointAndFigure.js.map +1 -0
  117. package/lib/indicator/renko.d.ts +12 -0
  118. package/lib/indicator/renko.js +13 -0
  119. package/lib/indicator/renko.js.map +1 -0
  120. package/lib/indicator/rsi.d.ts +24 -0
  121. package/lib/indicator/rsi.js +29 -0
  122. package/lib/indicator/rsi.js.map +1 -0
  123. package/lib/indicator/sar.d.ts +24 -0
  124. package/lib/indicator/sar.js +29 -0
  125. package/lib/indicator/sar.js.map +1 -0
  126. package/lib/indicator/sma.d.ts +24 -0
  127. package/lib/indicator/sma.js +29 -0
  128. package/lib/indicator/sma.js.map +1 -0
  129. package/lib/indicator/stochasticOscillator.d.ts +24 -0
  130. package/lib/indicator/stochasticOscillator.js +27 -0
  131. package/lib/indicator/stochasticOscillator.js.map +1 -0
  132. package/lib/indicator/tma.d.ts +24 -0
  133. package/lib/indicator/tma.js +29 -0
  134. package/lib/indicator/tma.js.map +1 -0
  135. package/lib/indicator/wma.d.ts +24 -0
  136. package/lib/indicator/wma.js +29 -0
  137. package/lib/indicator/wma.js.map +1 -0
  138. package/lib/utils/functor.d.ts +1 -0
  139. package/lib/utils/functor.js +4 -0
  140. package/lib/utils/functor.js.map +1 -0
  141. package/lib/utils/identity.d.ts +1 -0
  142. package/lib/utils/identity.js +2 -0
  143. package/lib/utils/identity.js.map +1 -0
  144. package/lib/utils/index.d.ts +8 -0
  145. package/lib/utils/index.js +9 -0
  146. package/lib/utils/index.js.map +1 -0
  147. package/lib/utils/mappedSlidingWindow.d.ts +15 -0
  148. package/lib/utils/mappedSlidingWindow.js +77 -0
  149. package/lib/utils/mappedSlidingWindow.js.map +1 -0
  150. package/lib/utils/merge.d.ts +11 -0
  151. package/lib/utils/merge.js +70 -0
  152. package/lib/utils/merge.js.map +1 -0
  153. package/lib/utils/path.d.ts +1 -0
  154. package/lib/utils/path.js +15 -0
  155. package/lib/utils/path.js.map +1 -0
  156. package/lib/utils/rebind.d.ts +1 -0
  157. package/lib/utils/rebind.js +18 -0
  158. package/lib/utils/rebind.js.map +1 -0
  159. package/lib/utils/slidingWindow.d.ts +19 -0
  160. package/lib/utils/slidingWindow.js +111 -0
  161. package/lib/utils/slidingWindow.js.map +1 -0
  162. package/lib/utils/zipper.d.ts +7 -0
  163. package/lib/utils/zipper.js +36 -0
  164. package/lib/utils/zipper.js.map +1 -0
  165. package/package.json +49 -0
  166. package/src/calculator/atr.ts +88 -0
  167. package/src/calculator/bollingerband.ts +102 -0
  168. package/src/calculator/change.ts +53 -0
  169. package/src/calculator/compare.ts +68 -0
  170. package/src/calculator/defaultOptionsForComputation.ts +117 -0
  171. package/src/calculator/elderRay.ts +90 -0
  172. package/src/calculator/ema.ts +92 -0
  173. package/src/calculator/forceIndex.ts +43 -0
  174. package/src/calculator/heikinAshi.ts +38 -0
  175. package/src/calculator/index.ts +19 -0
  176. package/src/calculator/kagi.ts +249 -0
  177. package/src/calculator/macd.ts +90 -0
  178. package/src/calculator/pointAndFigure.ts +255 -0
  179. package/src/calculator/renko.ts +227 -0
  180. package/src/calculator/rsi.ts +114 -0
  181. package/src/calculator/sar.ts +117 -0
  182. package/src/calculator/sma.ts +41 -0
  183. package/src/calculator/smoothedForceIndex.ts +53 -0
  184. package/src/calculator/sto.ts +118 -0
  185. package/src/calculator/tma.ts +82 -0
  186. package/src/calculator/wma.ts +49 -0
  187. package/src/index.ts +1 -0
  188. package/src/indicator/algorithm.ts +53 -0
  189. package/src/indicator/atr.ts +58 -0
  190. package/src/indicator/baseIndicator.ts +97 -0
  191. package/src/indicator/bollingerBand.ts +55 -0
  192. package/src/indicator/change.ts +51 -0
  193. package/src/indicator/compare.ts +57 -0
  194. package/src/indicator/defaultOptionsForAppearance.ts +77 -0
  195. package/src/indicator/elderImpulse.ts +73 -0
  196. package/src/indicator/elderRay.ts +56 -0
  197. package/src/indicator/ema.ts +58 -0
  198. package/src/indicator/forceIndex.ts +57 -0
  199. package/src/indicator/heikinAshi.ts +35 -0
  200. package/src/indicator/index.ts +25 -0
  201. package/src/indicator/kagi.ts +18 -0
  202. package/src/indicator/macd.ts +60 -0
  203. package/src/indicator/pointAndFigure.ts +18 -0
  204. package/src/indicator/renko.ts +18 -0
  205. package/src/indicator/rsi.ts +58 -0
  206. package/src/indicator/sar.ts +56 -0
  207. package/src/indicator/sma.ts +57 -0
  208. package/src/indicator/stochasticOscillator.ts +56 -0
  209. package/src/indicator/tma.ts +57 -0
  210. package/src/indicator/wma.ts +58 -0
  211. package/src/utils/functor.ts +3 -0
  212. package/src/utils/identity.ts +1 -0
  213. package/src/utils/index.ts +8 -0
  214. package/src/utils/mappedSlidingWindow.ts +95 -0
  215. package/src/utils/merge.ts +93 -0
  216. package/src/utils/path.ts +16 -0
  217. package/src/utils/rebind.ts +19 -0
  218. package/src/utils/slidingWindow.ts +142 -0
  219. package/src/utils/zipper.ts +45 -0
@@ -0,0 +1,249 @@
1
+ import { functor, merge, path } from "../utils";
2
+ import atr from "./atr";
3
+ import { Kagi as defaultOptions } from "./defaultOptionsForComputation";
4
+
5
+ export default function () {
6
+ let options = defaultOptions;
7
+ let dateAccessor = (d: any) => d.date;
8
+ let dateMutator = (d: any, date: any) => {
9
+ d.date = date;
10
+ };
11
+
12
+ const calculator = (data: any[]) => {
13
+ const { reversalType, windowSize, reversal, sourcePath } = options;
14
+
15
+ const source = path(sourcePath);
16
+
17
+ let reversalThreshold: any;
18
+
19
+ if (reversalType === "ATR") {
20
+ const atrAlgorithm = atr().options({ windowSize });
21
+
22
+ const atrCalculator = merge()
23
+ .algorithm(atrAlgorithm)
24
+ .merge((d: any, c: any) => {
25
+ d["atr" + windowSize] = c;
26
+ });
27
+
28
+ atrCalculator(data);
29
+ reversalThreshold = (d: any) => d["atr" + windowSize];
30
+ } else {
31
+ reversalThreshold = functor(reversal);
32
+ }
33
+
34
+ const kagiData: any[] = [];
35
+
36
+ let prevPeak: any;
37
+ let prevTrough: any;
38
+ let direction: any;
39
+
40
+ let line: {
41
+ added?: any;
42
+ date?: any;
43
+ from?: any;
44
+ to?: any;
45
+ open?: number;
46
+ high?: number;
47
+ low?: number;
48
+ close?: number;
49
+ current?: any;
50
+ changePoint?: any;
51
+ changeTo?: any;
52
+ volume?: number;
53
+ reverseAt?: any;
54
+ startAs?: any;
55
+ startOfYear?: any;
56
+ startOfQuarter?: any;
57
+ startOfMonth?: any;
58
+ startOfWeek?: any;
59
+ } = {};
60
+
61
+ data.forEach(function (d) {
62
+ if (line.from === undefined) {
63
+ dateMutator(line, dateAccessor(d));
64
+ line.from = dateAccessor(d);
65
+
66
+ if (!line.open) {
67
+ line.open = d.open;
68
+ }
69
+ line.high = d.high;
70
+ line.low = d.low;
71
+ if (!line.close) {
72
+ line.close = source(d);
73
+ }
74
+ line.startOfYear = d.startOfYear;
75
+ line.startOfQuarter = d.startOfQuarter;
76
+ line.startOfMonth = d.startOfMonth;
77
+ line.startOfWeek = d.startOfWeek;
78
+ }
79
+
80
+ if (!line.startOfYear) {
81
+ line.startOfYear = d.startOfYear;
82
+ if (line.startOfYear) {
83
+ line.date = d.date;
84
+ }
85
+ }
86
+
87
+ if (!line.startOfQuarter) {
88
+ line.startOfQuarter = d.startOfQuarter;
89
+ if (line.startOfQuarter && !line.startOfYear) {
90
+ line.date = d.date;
91
+ }
92
+ }
93
+
94
+ if (!line.startOfMonth) {
95
+ line.startOfMonth = d.startOfMonth;
96
+ if (line.startOfMonth && !line.startOfQuarter) {
97
+ line.date = d.date;
98
+ }
99
+ }
100
+ if (!line.startOfWeek) {
101
+ line.startOfWeek = d.startOfWeek;
102
+ if (line.startOfWeek && !line.startOfMonth) {
103
+ line.date = d.date;
104
+ }
105
+ }
106
+ line.volume = (line.volume || 0) + d.volume;
107
+ // @ts-ignore
108
+ line.high = Math.max(line.high, d.high);
109
+ // @ts-ignore
110
+ line.low = Math.min(line.low, d.low);
111
+ line.to = dateAccessor(d);
112
+
113
+ // @ts-ignore
114
+ const priceMovement = source(d) - line.close;
115
+
116
+ if (
117
+ // @ts-ignore
118
+ (line.close >= line.open /* going up */ && priceMovement > 0) /* and moving in same direction */ ||
119
+ // @ts-ignore
120
+ (line.close < line.open /* going down */ && priceMovement < 0) /* and moving in same direction */
121
+ ) {
122
+ line.close = source(d);
123
+ // @ts-ignore
124
+ if (prevTrough && line.close < prevTrough) {
125
+ // going below the prevTrough, so change from yang to yin
126
+ // A yin line forms when a Kagi line breaks below the prior trough.
127
+ line.changePoint = prevTrough;
128
+ if (line.startAs !== "yin") {
129
+ line.changeTo = "yin";
130
+ // line.startAs = "yang";
131
+ }
132
+ }
133
+
134
+ // @ts-ignore
135
+ if (prevPeak && line.close > prevPeak) {
136
+ // going above the prevPeak, so change from yin to yang
137
+ // A yang line forms when a Kagi line breaks above the prior peak
138
+ line.changePoint = prevPeak;
139
+ if (line.startAs !== "yang") {
140
+ line.changeTo = "yang";
141
+ // line.startAs = "yin";
142
+ }
143
+ }
144
+ } else if (
145
+ // @ts-ignore
146
+ (line.close >= line.open /* going up */ &&
147
+ priceMovement < 0 /* and moving in other direction */ &&
148
+ Math.abs(priceMovement) > reversalThreshold(d)) /* and the movement is big enough for reversal */ ||
149
+ // @ts-ignore
150
+ (line.close < line.open /* going down */ &&
151
+ priceMovement > 0 /* and moving in other direction */ &&
152
+ /* and the movement is big enough for reversal */
153
+ Math.abs(priceMovement) > reversalThreshold(d))
154
+ ) {
155
+ // reverse direction
156
+ const nextLineOpen = line.close;
157
+
158
+ // @ts-ignore
159
+ direction = (line.close - line.open) / Math.abs(line.close - line.open);
160
+
161
+ let nextChangePoint;
162
+ let nextChangeTo;
163
+ if (direction < 0 /* if direction so far has been -ve*/) {
164
+ // compare with line.close becomes prevTrough
165
+ if (prevPeak === undefined) {
166
+ prevPeak = line.open;
167
+ }
168
+ prevTrough = line.close;
169
+ if (source(d) > prevPeak) {
170
+ nextChangePoint = prevPeak;
171
+ nextChangeTo = "yang";
172
+ }
173
+ } else {
174
+ if (prevTrough === undefined) {
175
+ prevTrough = line.open;
176
+ }
177
+ prevPeak = line.close;
178
+ if (source(d) < prevTrough) {
179
+ nextChangePoint = prevTrough;
180
+ nextChangeTo = "yin";
181
+ }
182
+ }
183
+ if (line.startAs === undefined) {
184
+ line.startAs = direction > 0 ? "yang" : "yin";
185
+ }
186
+
187
+ const startAs = line.changeTo || line.startAs;
188
+ line.added = true;
189
+ kagiData.push(line);
190
+ direction = -1 * direction; // direction is reversed
191
+
192
+ line = { ...line };
193
+ line.open = nextLineOpen;
194
+ line.close = source(d);
195
+ line.startAs = startAs;
196
+ line.changePoint = nextChangePoint;
197
+ line.changeTo = nextChangeTo;
198
+ line.added = false;
199
+ line.from = undefined;
200
+ line.volume = 0;
201
+ } else {
202
+ // console.log("MOVING IN REV DIR BUT..", line.open, line.close, source(d));
203
+ }
204
+ line.current = source(d);
205
+ // @ts-ignore
206
+ let dir = line.close - line.open;
207
+ dir = dir === 0 ? 1 : dir / Math.abs(dir);
208
+ // @ts-ignore
209
+ line.reverseAt = dir > 0 ? line.close - reversalThreshold(d) : line.open - reversalThreshold(d);
210
+ });
211
+ if (!line.added) {
212
+ kagiData.push(line);
213
+ }
214
+
215
+ return kagiData;
216
+ };
217
+
218
+ calculator.options = (newOptions?: any) => {
219
+ if (newOptions === undefined) {
220
+ return options;
221
+ }
222
+
223
+ options = { ...defaultOptions, ...newOptions };
224
+
225
+ return calculator;
226
+ };
227
+
228
+ calculator.dateMutator = (newDateMutator?: any) => {
229
+ if (newDateMutator === undefined) {
230
+ return dateMutator;
231
+ }
232
+
233
+ dateMutator = newDateMutator;
234
+
235
+ return calculator;
236
+ };
237
+
238
+ calculator.dateAccessor = (newDateAccessor?: any) => {
239
+ if (newDateAccessor === undefined) {
240
+ return dateAccessor;
241
+ }
242
+
243
+ dateAccessor = newDateAccessor;
244
+
245
+ return calculator;
246
+ };
247
+
248
+ return calculator;
249
+ }
@@ -0,0 +1,90 @@
1
+ /*
2
+ https://github.com/ScottLogic/d3fc/blob/master/src/indicator/algorithm/calculator/macd.js
3
+
4
+ The MIT License (MIT)
5
+
6
+ Copyright (c) 2014-2015 Scott Logic Ltd.
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in
16
+ all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ THE SOFTWARE.
25
+ */
26
+ import { zip } from "d3-array";
27
+ import { MACD as defaultOptions } from "./defaultOptionsForComputation";
28
+ import ema from "./ema";
29
+
30
+ export interface MACDOptions {
31
+ readonly fast: number;
32
+ readonly slow: number;
33
+ readonly signal: number;
34
+ readonly sourcePath?: string;
35
+ }
36
+
37
+ interface MACDCalculator {
38
+ (data: any[]): {
39
+ macd: number | undefined;
40
+ signal: number | undefined;
41
+ divergence: number | undefined;
42
+ }[];
43
+ undefinedLength(): number;
44
+ options(): MACDOptions;
45
+ options(newOptions: MACDOptions): MACDCalculator;
46
+ }
47
+
48
+ export default function () {
49
+ let options: MACDOptions = defaultOptions;
50
+
51
+ const calculator = (data: any[]) => {
52
+ const { fast, slow, signal, sourcePath } = options;
53
+
54
+ const fastEMA = ema().options({ windowSize: fast, sourcePath });
55
+
56
+ const slowEMA = ema().options({ windowSize: slow, sourcePath });
57
+
58
+ const signalEMA = ema().options({ windowSize: signal, sourcePath: undefined });
59
+
60
+ const diff = zip(fastEMA(data), slowEMA(data)).map((d) =>
61
+ d[0] !== undefined && d[1] !== undefined ? d[0] - d[1] : undefined,
62
+ );
63
+
64
+ const averageDiff = signalEMA(diff);
65
+
66
+ return zip(diff, averageDiff).map((d) => ({
67
+ macd: d[0],
68
+ signal: d[1],
69
+ divergence: d[0] !== undefined && d[1] !== undefined ? d[0] - d[1] : undefined,
70
+ }));
71
+ };
72
+
73
+ calculator.undefinedLength = () => {
74
+ const { slow, signal } = options;
75
+
76
+ return slow + signal - 1;
77
+ };
78
+
79
+ calculator.options = (newOptions?: MACDOptions) => {
80
+ if (newOptions === undefined) {
81
+ return options;
82
+ }
83
+
84
+ options = { ...defaultOptions, ...newOptions };
85
+
86
+ return calculator;
87
+ };
88
+
89
+ return calculator as MACDCalculator;
90
+ }
@@ -0,0 +1,255 @@
1
+ import { PointAndFigure as defaultOptions } from "./defaultOptionsForComputation";
2
+
3
+ function createBox(d: any, dateAccessor: any, dateMutator: any) {
4
+ const box = {
5
+ open: d.open,
6
+ fromDate: dateAccessor(d),
7
+ toDate: dateAccessor(d),
8
+ startOfYear: d.startOfYear,
9
+ startOfQuarter: d.startOfQuarter,
10
+ startOfMonth: d.startOfMonth,
11
+ startOfWeek: d.startOfWeek,
12
+ };
13
+ dateMutator(box, dateAccessor(d));
14
+ return box;
15
+ }
16
+
17
+ function updateColumns(columnData: any, dateAccessor: any, dateMutator: any) {
18
+ columnData.forEach(function (d: any) {
19
+ d.startOfYear = false;
20
+ d.startOfQuarter = false;
21
+ d.startOfMonth = false;
22
+ d.startOfWeek = false;
23
+
24
+ d.boxes.forEach(function (eachBox: any) {
25
+ if (d.open === undefined) {
26
+ d.open = eachBox.open;
27
+ }
28
+ d.close = eachBox.close;
29
+ d.high = Math.max(d.open, d.close);
30
+ d.low = Math.min(d.open, d.close);
31
+
32
+ if (d.fromDate === undefined) {
33
+ d.fromDate = eachBox.fromDate;
34
+ }
35
+ if (d.date === undefined) {
36
+ d.date = eachBox.date;
37
+ }
38
+ d.toDate = eachBox.toDate;
39
+
40
+ if (eachBox.startOfYear) {
41
+ d.startOfYear = d.startOfYear || eachBox.startOfYear;
42
+ d.startOfQuarter = eachBox.startOfQuarter;
43
+ d.startOfMonth = eachBox.startOfMonth;
44
+ d.startOfWeek = eachBox.startOfWeek;
45
+
46
+ dateMutator(d, dateAccessor(eachBox));
47
+ }
48
+ if (d.startOfQuarter !== true && eachBox.startOfQuarter) {
49
+ d.startOfQuarter = eachBox.startOfQuarter;
50
+ d.startOfMonth = eachBox.startOfMonth;
51
+ d.startOfWeek = eachBox.startOfWeek;
52
+ dateMutator(d, dateAccessor(eachBox));
53
+ }
54
+ if (d.startOfMonth !== true && eachBox.startOfMonth) {
55
+ d.startOfMonth = eachBox.startOfMonth;
56
+ d.startOfWeek = eachBox.startOfWeek;
57
+ dateMutator(d, dateAccessor(eachBox));
58
+ }
59
+ if (d.startOfWeek !== true && eachBox.startOfWeek) {
60
+ d.startOfWeek = eachBox.startOfWeek;
61
+ dateMutator(d, dateAccessor(eachBox));
62
+ }
63
+ });
64
+ });
65
+
66
+ return columnData;
67
+ }
68
+
69
+ export default function () {
70
+ let options = defaultOptions;
71
+ let dateAccessor = (d: any) => d.date;
72
+ let dateMutator = (d: any, date: any) => {
73
+ d.date = date;
74
+ };
75
+
76
+ const calculator = (rawData: any[]) => {
77
+ const { reversal, boxSize, sourcePath } = options;
78
+
79
+ const source =
80
+ // eslint-disable-next-line prettier/prettier
81
+ sourcePath === "high/low" ? ((d: any) => ({ high: d.high, low: d.low })) : ((d: any) => ({ high: d.close, low: d.close }));
82
+
83
+ const pricingMethod = source;
84
+ const columnData: any[] = [];
85
+
86
+ // @ts-ignore
87
+ let column: {
88
+ boxes: any[];
89
+ direction: any;
90
+ open: number;
91
+ close?: number;
92
+ } = {
93
+ boxes: [],
94
+ open: rawData[0].open,
95
+ };
96
+ let box = createBox(rawData[0], dateAccessor, dateMutator);
97
+
98
+ columnData.push(column);
99
+
100
+ rawData.forEach(function (d) {
101
+ // @ts-ignore
102
+ column.volume = (column.volume || 0) + d.volume;
103
+
104
+ if (!box.startOfYear) {
105
+ box.startOfYear = d.startOfYear;
106
+ if (box.startOfYear) {
107
+ dateMutator(box, dateAccessor(d));
108
+ }
109
+ }
110
+
111
+ if (!box.startOfYear && !box.startOfQuarter) {
112
+ box.startOfQuarter = d.startOfQuarter;
113
+ if (box.startOfQuarter && !box.startOfYear) {
114
+ dateMutator(box, dateAccessor(d));
115
+ }
116
+ }
117
+
118
+ if (!box.startOfQuarter && !box.startOfMonth) {
119
+ box.startOfMonth = d.startOfMonth;
120
+ if (box.startOfMonth && !box.startOfQuarter) {
121
+ dateMutator(box, dateAccessor(d));
122
+ }
123
+ }
124
+ if (!box.startOfMonth && !box.startOfWeek) {
125
+ box.startOfWeek = d.startOfWeek;
126
+ if (box.startOfWeek && !box.startOfMonth) {
127
+ dateMutator(box, dateAccessor(d));
128
+ }
129
+ }
130
+
131
+ if (columnData.length === 1 && column.boxes.length === 0) {
132
+ const upwardMovement = Math.max(pricingMethod(d).high - column.open, 0); // upward movement
133
+ const downwardMovement = Math.abs(Math.min(column.open - pricingMethod(d).low, 0)); // downward movement
134
+ column.direction = upwardMovement > downwardMovement ? 1 : -1;
135
+ if (boxSize * reversal < upwardMovement || boxSize * reversal < downwardMovement) {
136
+ // enough movement to trigger a reversal
137
+ box.toDate = dateAccessor(d);
138
+ box.open = column.open;
139
+ const noOfBoxes =
140
+ column.direction > 0
141
+ ? Math.floor(upwardMovement / boxSize)
142
+ : Math.floor(downwardMovement / boxSize);
143
+ for (let i = 0; i < noOfBoxes; i++) {
144
+ // @ts-ignore
145
+ box.close = box.open + column.direction * boxSize;
146
+ // @ts-ignore
147
+ const prevBoxClose = box.close;
148
+ column.boxes.push(box);
149
+ box = createBox(box, dateAccessor, dateMutator);
150
+ // box = cloneMe(box);
151
+ box.open = prevBoxClose;
152
+ }
153
+ box.fromDate = dateAccessor(d);
154
+ // @ts-ignore
155
+ box.date = dateAccessor(d);
156
+ }
157
+ } else {
158
+ // one or more boxes already formed in the current column
159
+ const upwardMovement = Math.max(pricingMethod(d).high - box.open, 0); // upward movement
160
+ const downwardMovement = Math.abs(Math.min(pricingMethod(d).low - box.open, 0)); // downward movement
161
+
162
+ if (
163
+ (column.direction > 0 && upwardMovement > boxSize) /* rising column AND box can be formed */ ||
164
+ (column.direction < 0 && downwardMovement > boxSize) /* falling column AND box can be formed */
165
+ ) {
166
+ // form another box
167
+ // @ts-ignore
168
+ box.close = box.open + column.direction * boxSize;
169
+ box.toDate = dateAccessor(d);
170
+
171
+ // @ts-ignore
172
+ const prevBoxClose = box.close;
173
+ column.boxes.push(box);
174
+ box = createBox(d, dateAccessor, dateMutator);
175
+ box.open = prevBoxClose;
176
+ box.fromDate = dateAccessor(d);
177
+ dateMutator(box, dateAccessor(d));
178
+ } else if (
179
+ /* rising column and there is downward movement to trigger a reversal */
180
+ (column.direction > 0 && downwardMovement > boxSize * reversal) ||
181
+ /* falling column and there is downward movement to trigger a reversal */
182
+ (column.direction < 0 && upwardMovement > boxSize * reversal)
183
+ ) {
184
+ // reversal
185
+
186
+ box.open = box.open + -1 * column.direction * boxSize;
187
+ box.toDate = dateAccessor(d);
188
+ // box.displayDate = d.displayDate;
189
+ dateMutator(box, dateAccessor(d));
190
+ // box.startOfYear = d.startOfYear;
191
+ // box.startOfQuarter = d.startOfQuarter;
192
+ // box.startOfMonth = d.startOfMonth;
193
+ // box.startOfWeek = d.startOfWeek;
194
+ // var idx = index + 1;
195
+ column = {
196
+ boxes: [],
197
+ // @ts-ignore
198
+ volume: 0,
199
+ direction: -1 * column.direction,
200
+ };
201
+ const noOfBoxes =
202
+ column.direction > 0
203
+ ? Math.floor(upwardMovement / boxSize)
204
+ : Math.floor(downwardMovement / boxSize);
205
+ for (let i = 0; i < noOfBoxes; i++) {
206
+ // @ts-ignore
207
+ box.close = box.open + column.direction * boxSize;
208
+ // @ts-ignore
209
+ const prevBoxClose = box.close;
210
+ column.boxes.push(box);
211
+ box = createBox(d, dateAccessor, dateMutator);
212
+ box.open = prevBoxClose;
213
+ }
214
+
215
+ columnData.push(column);
216
+ }
217
+ }
218
+ });
219
+ updateColumns(columnData, dateAccessor, dateMutator);
220
+
221
+ return columnData;
222
+ };
223
+
224
+ calculator.options = (newOptions?: any) => {
225
+ if (newOptions === undefined) {
226
+ return options;
227
+ }
228
+
229
+ options = { ...defaultOptions, ...newOptions };
230
+
231
+ return calculator;
232
+ };
233
+
234
+ calculator.dateMutator = (newDateMutator?: any) => {
235
+ if (newDateMutator === undefined) {
236
+ return dateMutator;
237
+ }
238
+
239
+ dateMutator = newDateMutator;
240
+
241
+ return calculator;
242
+ };
243
+
244
+ calculator.dateAccessor = (newDateAccessor?: any) => {
245
+ if (newDateAccessor === undefined) {
246
+ return dateAccessor;
247
+ }
248
+
249
+ dateAccessor = newDateAccessor;
250
+
251
+ return calculator;
252
+ };
253
+
254
+ return calculator;
255
+ }