wma.src.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 Kacper Madej
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/indicators/wma', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Stock/Indicators/WMA/WMAIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * (c) 2010-2021 Kacper Madej
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var __extends = (this && this.__extends) || (function () {
  42. var extendStatics = function (d,
  43. b) {
  44. extendStatics = Object.setPrototypeOf ||
  45. ({ __proto__: [] } instanceof Array && function (d,
  46. b) { d.__proto__ = b; }) ||
  47. function (d,
  48. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  49. return extendStatics(d, b);
  50. };
  51. return function (d, b) {
  52. extendStatics(d, b);
  53. function __() { this.constructor = d; }
  54. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  55. };
  56. })();
  57. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  58. var isArray = U.isArray,
  59. merge = U.merge;
  60. /* eslint-disable valid-jsdoc */
  61. // Utils:
  62. /**
  63. * @private
  64. */
  65. function accumulateAverage(points, xVal, yVal, i, index) {
  66. var xValue = xVal[i],
  67. yValue = index < 0 ? yVal[i] : yVal[i][index];
  68. points.push([xValue, yValue]);
  69. }
  70. /**
  71. * @private
  72. */
  73. function weightedSumArray(array, pLen) {
  74. // The denominator is the sum of the number of days as a triangular number.
  75. // If there are 5 days, the triangular numbers are 5, 4, 3, 2, and 1.
  76. // The sum is 5 + 4 + 3 + 2 + 1 = 15.
  77. var denominator = (pLen + 1) / 2 * pLen;
  78. // reduce VS loop => reduce
  79. return array.reduce(function (prev, cur, i) {
  80. return [null, prev[1] + cur[1] * (i + 1)];
  81. })[1] / denominator;
  82. }
  83. /**
  84. * @private
  85. */
  86. function populateAverage(points, xVal, yVal, i) {
  87. var pLen = points.length,
  88. wmaY = weightedSumArray(points,
  89. pLen),
  90. wmaX = xVal[i - 1];
  91. points.shift(); // remove point until range < period
  92. return [wmaX, wmaY];
  93. }
  94. /* eslint-enable valid-jsdoc */
  95. /**
  96. * The SMA series type.
  97. *
  98. * @private
  99. * @class
  100. * @name Highcharts.seriesTypes.wma
  101. *
  102. * @augments Highcharts.Series
  103. */
  104. var WMAIndicator = /** @class */ (function (_super) {
  105. __extends(WMAIndicator, _super);
  106. function WMAIndicator() {
  107. var _this = _super !== null && _super.apply(this,
  108. arguments) || this;
  109. _this.data = void 0;
  110. _this.options = void 0;
  111. _this.points = void 0;
  112. return _this;
  113. }
  114. WMAIndicator.prototype.getValues = function (series, params) {
  115. var period = params.period,
  116. xVal = series.xData,
  117. yVal = series.yData,
  118. yValLen = yVal ? yVal.length : 0,
  119. range = 1,
  120. xValue = xVal[0],
  121. yValue = yVal[0],
  122. WMA = [],
  123. xData = [],
  124. yData = [],
  125. index = -1,
  126. i,
  127. points,
  128. WMAPoint;
  129. if (xVal.length < period) {
  130. return;
  131. }
  132. // Switch index for OHLC / Candlestick
  133. if (isArray(yVal[0])) {
  134. index = params.index;
  135. yValue = yVal[0][index];
  136. }
  137. // Starting point
  138. points = [[xValue, yValue]];
  139. // Accumulate first N-points
  140. while (range !== period) {
  141. accumulateAverage(points, xVal, yVal, range, index);
  142. range++;
  143. }
  144. // Calculate value one-by-one for each period in visible data
  145. for (i = range; i < yValLen; i++) {
  146. WMAPoint = populateAverage(points, xVal, yVal, i);
  147. WMA.push(WMAPoint);
  148. xData.push(WMAPoint[0]);
  149. yData.push(WMAPoint[1]);
  150. accumulateAverage(points, xVal, yVal, i, index);
  151. }
  152. WMAPoint = populateAverage(points, xVal, yVal, i);
  153. WMA.push(WMAPoint);
  154. xData.push(WMAPoint[0]);
  155. yData.push(WMAPoint[1]);
  156. return {
  157. values: WMA,
  158. xData: xData,
  159. yData: yData
  160. };
  161. };
  162. /**
  163. * Weighted moving average indicator (WMA). This series requires `linkedTo`
  164. * option to be set.
  165. *
  166. * @sample stock/indicators/wma
  167. * Weighted moving average indicator
  168. *
  169. * @extends plotOptions.sma
  170. * @since 6.0.0
  171. * @product highstock
  172. * @requires stock/indicators/indicators
  173. * @requires stock/indicators/wma
  174. * @optionparent plotOptions.wma
  175. */
  176. WMAIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  177. params: {
  178. index: 3,
  179. period: 9
  180. }
  181. });
  182. return WMAIndicator;
  183. }(SMAIndicator));
  184. SeriesRegistry.registerSeriesType('wma', WMAIndicator);
  185. /* *
  186. *
  187. * Default Export
  188. *
  189. * */
  190. /**
  191. * A `WMA` series. If the [type](#series.wma.type) option is not specified, it
  192. * is inherited from [chart.type](#chart.type).
  193. *
  194. * @extends series,plotOptions.wma
  195. * @since 6.0.0
  196. * @product highstock
  197. * @excluding dataParser, dataURL
  198. * @requires stock/indicators/indicators
  199. * @requires stock/indicators/wma
  200. * @apioption series.wma
  201. */
  202. ''; // adds doclet above to the transpiled file
  203. return WMAIndicator;
  204. });
  205. _registerModule(_modules, 'masters/indicators/wma.src.js', [], function () {
  206. });
  207. }));