@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,227 @@
1
+ import { functor, merge } from "../utils";
2
+ import atr from "./atr";
3
+ import { Renko as defaultOptions } from "./defaultOptionsForComputation";
4
+
5
+ export default function () {
6
+ let options = defaultOptions;
7
+
8
+ let dateAccessor = (d: any) => d.date;
9
+ let dateMutator = (d: any, date: any) => {
10
+ d.date = date;
11
+ };
12
+
13
+ const calculator = (rawData: any[]) => {
14
+ const { reversalType, fixedBrickSize, sourcePath, windowSize, percentage } = options;
15
+
16
+ const source =
17
+ sourcePath === "high/low"
18
+ ? (d: any) => ({ high: d.high, low: d.low })
19
+ : (d: any) => ({ high: d.close, low: d.close });
20
+
21
+ const pricingMethod = source;
22
+ let brickSize: any;
23
+
24
+ if (reversalType === "ATR") {
25
+ const atrAlgorithm = atr().options({ windowSize });
26
+
27
+ const atrCalculator = merge()
28
+ .algorithm(atrAlgorithm)
29
+ .merge((d: any, c: any) => {
30
+ d["atr" + windowSize] = c;
31
+ });
32
+
33
+ atrCalculator(rawData);
34
+ brickSize = (d: any) => d["atr" + windowSize];
35
+ } else if (reversalType === "Percentage LTP") {
36
+ // Calculate brick size as a percentage of the last price
37
+ brickSize = (d: any) => d.close * (percentage / 100);
38
+ } else {
39
+ brickSize = functor(fixedBrickSize);
40
+ }
41
+
42
+ const renkoData: any[] = [];
43
+
44
+ let index = 0;
45
+ let prevBrickClose = rawData[index].open;
46
+ let prevBrickOpen = rawData[index].open;
47
+ let brick: {
48
+ added?: any;
49
+ date?: any;
50
+ direction?: any;
51
+ from?: any;
52
+ fromDate?: any;
53
+ to?: any;
54
+ toDate?: any;
55
+ open?: number;
56
+ high?: number;
57
+ low?: number;
58
+ close?: number;
59
+ fullyFormed?: any;
60
+ current?: any;
61
+ changePoint?: any;
62
+ changeTo?: any;
63
+ volume?: number;
64
+ reverseAt?: any;
65
+ startAs?: any;
66
+ startOfYear?: any;
67
+ startOfQuarter?: any;
68
+ startOfMonth?: any;
69
+ startOfWeek?: any;
70
+ } = {};
71
+ let direction = 0;
72
+
73
+ rawData.forEach(function (d, idx) {
74
+ if (brick.from === undefined) {
75
+ brick.high = d.high;
76
+ brick.low = d.low;
77
+ brick.startOfYear = d.startOfYear;
78
+ brick.startOfQuarter = d.startOfQuarter;
79
+ brick.startOfMonth = d.startOfMonth;
80
+ brick.startOfWeek = d.startOfWeek;
81
+
82
+ brick.from = idx;
83
+ brick.fromDate = dateAccessor(d);
84
+ dateMutator(brick, dateAccessor(d));
85
+ }
86
+ brick.volume = (brick.volume || 0) + d.volume;
87
+
88
+ const prevCloseToHigh = prevBrickClose - pricingMethod(d).high;
89
+ const prevCloseToLow = prevBrickClose - pricingMethod(d).low;
90
+ const prevOpenToHigh = prevBrickOpen - pricingMethod(d).high;
91
+ const prevOpenToLow = prevBrickOpen - pricingMethod(d).low;
92
+ const priceMovement = Math.min(
93
+ Math.abs(prevCloseToHigh),
94
+ Math.abs(prevCloseToLow),
95
+ Math.abs(prevOpenToHigh),
96
+ Math.abs(prevOpenToLow),
97
+ );
98
+
99
+ // @ts-ignore
100
+ brick.high = Math.max(brick.high, d.high);
101
+ // @ts-ignore
102
+ brick.low = Math.min(brick.low, d.low);
103
+
104
+ if (!brick.startOfYear) {
105
+ brick.startOfYear = d.startOfYear;
106
+ if (brick.startOfYear) {
107
+ dateMutator(brick, dateAccessor(d));
108
+ }
109
+ }
110
+
111
+ if (!brick.startOfQuarter) {
112
+ brick.startOfQuarter = d.startOfQuarter;
113
+ if (brick.startOfQuarter && !brick.startOfYear) {
114
+ dateMutator(brick, dateAccessor(d));
115
+ }
116
+ }
117
+
118
+ if (!brick.startOfMonth) {
119
+ brick.startOfMonth = d.startOfMonth;
120
+ if (brick.startOfMonth && !brick.startOfQuarter) {
121
+ dateMutator(brick, dateAccessor(d));
122
+ }
123
+ }
124
+ if (!brick.startOfWeek) {
125
+ brick.startOfWeek = d.startOfWeek;
126
+ if (brick.startOfWeek && !brick.startOfMonth) {
127
+ dateMutator(brick, dateAccessor(d));
128
+ }
129
+ }
130
+
131
+ if (brickSize(d)) {
132
+ const noOfBricks = Math.floor(priceMovement / brickSize(d));
133
+
134
+ brick.open =
135
+ Math.abs(prevCloseToHigh) < Math.abs(prevOpenToHigh) ||
136
+ Math.abs(prevCloseToLow) < Math.abs(prevOpenToLow)
137
+ ? prevBrickClose
138
+ : prevBrickOpen;
139
+
140
+ if (noOfBricks >= 1) {
141
+ let j = 0;
142
+ for (j = 0; j < noOfBricks; j++) {
143
+ brick.close =
144
+ // @ts-ignore
145
+ brick.open < pricingMethod(d).high
146
+ ? // if brick open is less than current price it means it is green/hollow brick
147
+ brick.open + brickSize(d)
148
+ : // @ts-ignore
149
+ brick.open - brickSize(d);
150
+ // @ts-ignore
151
+ direction = brick.close > brick.open ? 1 : -1;
152
+ brick.direction = direction;
153
+ brick.to = idx;
154
+ brick.toDate = dateAccessor(d);
155
+ // brick.diff = brick.open - brick.close;
156
+ // brick.atr = d.atr;
157
+ brick.fullyFormed = true;
158
+ renkoData.push(brick);
159
+
160
+ prevBrickClose = brick.close;
161
+ prevBrickOpen = brick.open;
162
+
163
+ const newBrick = {
164
+ high: brick.high,
165
+ low: brick.low,
166
+ open: brick.close,
167
+ startOfYear: false,
168
+ startOfMonth: false,
169
+ startOfQuarter: false,
170
+ startOfWeek: false,
171
+ };
172
+ brick = newBrick;
173
+ brick.from = idx;
174
+ brick.fromDate = dateAccessor(d);
175
+ // indexMutator(brick, index + j);
176
+ dateMutator(brick, dateAccessor(d));
177
+ brick.volume = (brick.volume || 0) + d.volume;
178
+ }
179
+ index = index + j - 1;
180
+ brick = {};
181
+ } else {
182
+ if (idx === rawData.length - 1) {
183
+ brick.close = direction > 0 ? pricingMethod(d).high : pricingMethod(d).low;
184
+ brick.to = idx;
185
+ brick.toDate = dateAccessor(d);
186
+ dateMutator(brick, dateAccessor(d));
187
+ brick.fullyFormed = false;
188
+ renkoData.push(brick);
189
+ }
190
+ }
191
+ }
192
+ });
193
+ return renkoData;
194
+ };
195
+
196
+ calculator.options = (newOptions?: any) => {
197
+ if (newOptions === undefined) {
198
+ return options;
199
+ }
200
+
201
+ options = { ...defaultOptions, ...newOptions };
202
+
203
+ return calculator;
204
+ };
205
+
206
+ calculator.dateMutator = (newDateMutator?: any) => {
207
+ if (newDateMutator === undefined) {
208
+ return dateMutator;
209
+ }
210
+
211
+ dateMutator = newDateMutator;
212
+
213
+ return calculator;
214
+ };
215
+
216
+ calculator.dateAccessor = (newDateAccessor?: any) => {
217
+ if (newDateAccessor === undefined) {
218
+ return dateAccessor;
219
+ }
220
+
221
+ dateAccessor = newDateAccessor;
222
+
223
+ return calculator;
224
+ };
225
+
226
+ return calculator;
227
+ }
@@ -0,0 +1,114 @@
1
+ /*
2
+ https://github.com/ScottLogic/d3fc/blob/master/src/indicator/algorithm/calculator/relativeStrengthIndex.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
+
27
+ import { mean } from "d3-array";
28
+ import { path, slidingWindow } from "../utils";
29
+ import { RSI as defaultOptions } from "./defaultOptionsForComputation";
30
+
31
+ export interface RSIOptions {
32
+ windowSize: number;
33
+ sourcePath?: string;
34
+ }
35
+
36
+ export default function () {
37
+ let options = defaultOptions;
38
+
39
+ const calculator = (data: any[]) => {
40
+ const { windowSize, sourcePath } = options;
41
+
42
+ // @ts-ignore
43
+ const source = path(sourcePath);
44
+
45
+ let prevAvgGain: any;
46
+ let prevAvgLoss: any;
47
+ const rsiAlgorithm = slidingWindow()
48
+ .windowSize(windowSize)
49
+ .accumulator((values: any[]) => {
50
+ const avgGain =
51
+ prevAvgGain !== undefined
52
+ ? (prevAvgGain * (windowSize - 1) + values[values.length - 1].gain) / windowSize
53
+ : mean<any>(values, (each) => each.gain);
54
+
55
+ if (avgGain === undefined) {
56
+ return undefined;
57
+ }
58
+
59
+ const avgLoss =
60
+ prevAvgLoss !== undefined
61
+ ? (prevAvgLoss * (windowSize - 1) + values[values.length - 1].loss) / windowSize
62
+ : mean<any>(values, (each) => each.loss);
63
+
64
+ if (avgLoss === undefined) {
65
+ return undefined;
66
+ }
67
+
68
+ const relativeStrength = avgGain / avgLoss;
69
+ const rsi = 100 - 100 / (1 + relativeStrength);
70
+
71
+ prevAvgGain = avgGain;
72
+ prevAvgLoss = avgLoss;
73
+
74
+ return rsi;
75
+ });
76
+
77
+ const gainsAndLossesCalculator = slidingWindow()
78
+ .windowSize(2)
79
+ .undefinedValue(() => [0, 0])
80
+ .accumulator((tuple: any) => {
81
+ const prev = tuple[0];
82
+ const now = tuple[1];
83
+ const change = source(now) - source(prev);
84
+ return {
85
+ gain: Math.max(change, 0),
86
+ loss: Math.abs(Math.min(change, 0)),
87
+ };
88
+ });
89
+
90
+ const gainsAndLosses = gainsAndLossesCalculator(data);
91
+
92
+ const rsiData = rsiAlgorithm(gainsAndLosses);
93
+
94
+ return rsiData;
95
+ };
96
+
97
+ calculator.undefinedLength = () => {
98
+ const { windowSize } = options;
99
+
100
+ return windowSize - 1;
101
+ };
102
+
103
+ calculator.options = (newOptions?: RSIOptions) => {
104
+ if (newOptions === undefined) {
105
+ return options;
106
+ }
107
+
108
+ options = { ...defaultOptions, ...newOptions };
109
+
110
+ return calculator;
111
+ };
112
+
113
+ return calculator;
114
+ }
@@ -0,0 +1,117 @@
1
+ import { mappedSlidingWindow } from "../utils";
2
+ import { SAR as defaultOptions } from "./defaultOptionsForComputation";
3
+
4
+ function calc(prev: any, now: any) {
5
+ const risingSar = prev.risingSar + prev.af * (prev.risingEp - prev.risingSar);
6
+
7
+ const fallingSar = prev.fallingSar - prev.af * (prev.fallingSar - prev.fallingEp);
8
+
9
+ const risingEp = Math.max(prev.risingEp, now.high);
10
+ const fallingEp = Math.min(prev.fallingEp, now.low);
11
+
12
+ return {
13
+ risingSar,
14
+ fallingSar,
15
+ risingEp,
16
+ fallingEp,
17
+ };
18
+ }
19
+
20
+ export interface SAROptions {
21
+ readonly accelerationFactor: number;
22
+ readonly maxAccelerationFactor: number;
23
+ }
24
+
25
+ export default function () {
26
+ let options = defaultOptions;
27
+
28
+ const calculator = (data: any[]) => {
29
+ const { accelerationFactor, maxAccelerationFactor } = options;
30
+
31
+ const algorithm = mappedSlidingWindow()
32
+ .windowSize(2)
33
+ // @ts-ignore
34
+ .undefinedValue(({ high, low }) => {
35
+ return {
36
+ risingSar: low,
37
+ risingEp: high,
38
+ fallingSar: high,
39
+ fallingEp: low,
40
+ af: accelerationFactor,
41
+ };
42
+ })
43
+ .accumulator(([prev, now]: any) => {
44
+ const { risingSar, fallingSar, risingEp, fallingEp } = calc(prev, now);
45
+
46
+ if (prev.use === undefined && risingSar > now.low && fallingSar < now.high) {
47
+ return {
48
+ risingSar,
49
+ fallingSar,
50
+ risingEp,
51
+ fallingEp,
52
+ };
53
+ }
54
+
55
+ const use =
56
+ prev.use !== undefined
57
+ ? prev.use === "rising"
58
+ ? risingSar > now.low
59
+ ? "falling"
60
+ : "rising"
61
+ : fallingSar < now.high
62
+ ? "rising"
63
+ : "falling"
64
+ : risingSar > now.low
65
+ ? "falling"
66
+ : "rising";
67
+
68
+ const current =
69
+ prev.use === use
70
+ ? {
71
+ af: Math.min(maxAccelerationFactor, prev.af + accelerationFactor),
72
+ fallingEp,
73
+ risingEp,
74
+ fallingSar,
75
+ risingSar,
76
+ }
77
+ : {
78
+ af: accelerationFactor,
79
+ fallingEp: now.low,
80
+ risingEp: now.high,
81
+ fallingSar: Math.max(prev.risingEp, now.high),
82
+ risingSar: Math.min(prev.fallingEp, now.low),
83
+ };
84
+
85
+ const { date, high, low } = now;
86
+
87
+ return {
88
+ date,
89
+ high,
90
+ low,
91
+ ...current,
92
+ use,
93
+ sar: use === "falling" ? current.fallingSar : current.risingSar,
94
+ };
95
+ });
96
+
97
+ const calculatedData = algorithm(data).map((d: any) => d.sar);
98
+
99
+ return calculatedData;
100
+ };
101
+
102
+ calculator.undefinedLength = () => {
103
+ return 1;
104
+ };
105
+
106
+ calculator.options = (newOptions?: SAROptions) => {
107
+ if (newOptions === undefined) {
108
+ return options;
109
+ }
110
+
111
+ options = { ...defaultOptions, ...newOptions };
112
+
113
+ return calculator;
114
+ };
115
+
116
+ return calculator;
117
+ }
@@ -0,0 +1,41 @@
1
+ import { mean } from "d3-array";
2
+ import { slidingWindow } from "../utils";
3
+ import { SMA as defaultOptions } from "./defaultOptionsForComputation";
4
+
5
+ export interface SMAOptions {
6
+ readonly sourcePath?: string;
7
+ readonly windowSize: number;
8
+ }
9
+
10
+ export default function () {
11
+ let options = defaultOptions;
12
+
13
+ const calculator = (data: any[]) => {
14
+ const { windowSize, sourcePath } = options;
15
+
16
+ const average = slidingWindow()
17
+ .windowSize(windowSize)
18
+ .sourcePath(sourcePath)
19
+ .accumulator((values: any[]) => mean(values));
20
+
21
+ return average(data);
22
+ };
23
+
24
+ calculator.undefinedLength = () => {
25
+ const { windowSize } = options;
26
+
27
+ return windowSize - 1;
28
+ };
29
+
30
+ calculator.options = (newOptions?: SMAOptions) => {
31
+ if (newOptions === undefined) {
32
+ return options;
33
+ }
34
+
35
+ options = { ...defaultOptions, ...newOptions };
36
+
37
+ return calculator;
38
+ };
39
+
40
+ return calculator;
41
+ }
@@ -0,0 +1,53 @@
1
+ import { zip } from "d3-array";
2
+ import { SmoothedForceIndex as defaultOptions } from "./defaultOptionsForComputation";
3
+ import ema from "./ema";
4
+ import forceIndex from "./forceIndex";
5
+ import sma from "./sma";
6
+
7
+ export default function () {
8
+ const underlyingAlgorithm = forceIndex();
9
+
10
+ let options = defaultOptions;
11
+
12
+ const calculator = (data: any[]) => {
13
+ const { smoothingType, smoothingWindow } = options;
14
+ const { sourcePath, volumePath } = options;
15
+
16
+ const algo = underlyingAlgorithm.options({ sourcePath, volumePath });
17
+
18
+ // @ts-ignore
19
+ const force = algo(data);
20
+
21
+ const ma = smoothingType === "ema" ? ema() : sma();
22
+ const forceMA = ma.options({
23
+ windowSize: smoothingWindow,
24
+ sourcePath: undefined,
25
+ });
26
+
27
+ // @ts-ignore
28
+ const smoothed = forceMA(force);
29
+
30
+ return zip(force, smoothed).map((d) => ({
31
+ force: d[0],
32
+ smoothed: d[1],
33
+ }));
34
+ };
35
+
36
+ calculator.undefinedLength = () => {
37
+ const { smoothingWindow } = options;
38
+
39
+ return underlyingAlgorithm.undefinedLength() + smoothingWindow - 1;
40
+ };
41
+
42
+ calculator.options = (newOptions?: any) => {
43
+ if (newOptions === undefined) {
44
+ return options;
45
+ }
46
+
47
+ options = { ...defaultOptions, ...newOptions };
48
+
49
+ return calculator;
50
+ };
51
+
52
+ return calculator;
53
+ }
@@ -0,0 +1,118 @@
1
+ /*
2
+ https://github.com/ScottLogic/d3fc/blob/master/src/indicator/algorithm/calculator/stochasticOscillator.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
+
27
+ import { max, mean, min, zip } from "d3-array";
28
+ import { slidingWindow } from "../utils";
29
+ import { FullStochasticOscillator as defaultOptions } from "./defaultOptionsForComputation";
30
+
31
+ export interface STOOptions {
32
+ readonly windowSize: number;
33
+ readonly kWindowSize: number;
34
+ readonly dWindowSize: number;
35
+ }
36
+
37
+ export default function () {
38
+ let options: STOOptions = defaultOptions;
39
+
40
+ let source = (d: any) => ({ open: d.open, high: d.high, low: d.low, close: d.close });
41
+
42
+ const calculator = (data: any[]) => {
43
+ const { windowSize, kWindowSize, dWindowSize } = options;
44
+
45
+ const high = (d: any) => source(d).high;
46
+ const low = (d: any) => source(d).low;
47
+ const close = (d: any) => source(d).close;
48
+
49
+ const kWindow = slidingWindow()
50
+ .windowSize(windowSize)
51
+ .accumulator((values: any[]) => {
52
+ const highestHigh = max<any, number>(values, high);
53
+ if (highestHigh === undefined) {
54
+ return undefined;
55
+ }
56
+
57
+ const lowestLow = min<any, number>(values, low);
58
+ if (lowestLow === undefined) {
59
+ return undefined;
60
+ }
61
+
62
+ const currentClose = close(values[values.length - 1]);
63
+ const k = ((currentClose - lowestLow) / (highestHigh - lowestLow)) * 100;
64
+
65
+ return k;
66
+ });
67
+
68
+ const kSmoothed = slidingWindow()
69
+ .skipInitial(windowSize - 1)
70
+ .windowSize(kWindowSize)
71
+ .accumulator((values: any[]) => mean(values));
72
+
73
+ const dWindow = slidingWindow()
74
+ .skipInitial(windowSize - 1 + kWindowSize - 1)
75
+ .windowSize(dWindowSize)
76
+ .accumulator((values: any[]) => mean(values));
77
+
78
+ const kData = kSmoothed(kWindow(data));
79
+ const dData = dWindow(kData);
80
+
81
+ // @ts-ignore
82
+ const indicatorData = zip(kData, dData).map((d) => {
83
+ return {
84
+ K: d[0],
85
+ D: d[1],
86
+ };
87
+ });
88
+
89
+ return indicatorData;
90
+ };
91
+
92
+ calculator.undefinedLength = () => {
93
+ const { windowSize, kWindowSize, dWindowSize } = options;
94
+ return windowSize + kWindowSize + dWindowSize;
95
+ };
96
+
97
+ calculator.source = (newSource?: any) => {
98
+ if (newSource === undefined) {
99
+ return source;
100
+ }
101
+
102
+ source = newSource;
103
+
104
+ return calculator;
105
+ };
106
+
107
+ calculator.options = (newOptions?: any) => {
108
+ if (newOptions === undefined) {
109
+ return options;
110
+ }
111
+
112
+ options = { ...defaultOptions, ...newOptions };
113
+
114
+ return calculator;
115
+ };
116
+
117
+ return calculator;
118
+ }