wma.src.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @license Highstock JS v8.0.0 (2019-12-10)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 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, 'indicators/wma.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  32. /* *
  33. *
  34. * (c) 2010-2019 Kacper Madej
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var isArray = U.isArray;
  42. var seriesType = H.seriesType;
  43. /* eslint-disable valid-jsdoc */
  44. // Utils:
  45. /**
  46. * @private
  47. */
  48. function accumulateAverage(points, xVal, yVal, i, index) {
  49. var xValue = xVal[i], yValue = index < 0 ? yVal[i] : yVal[i][index];
  50. points.push([xValue, yValue]);
  51. }
  52. /**
  53. * @private
  54. */
  55. function weightedSumArray(array, pLen) {
  56. // The denominator is the sum of the number of days as a triangular number.
  57. // If there are 5 days, the triangular numbers are 5, 4, 3, 2, and 1.
  58. // The sum is 5 + 4 + 3 + 2 + 1 = 15.
  59. var denominator = (pLen + 1) / 2 * pLen;
  60. // reduce VS loop => reduce
  61. return array.reduce(function (prev, cur, i) {
  62. return [null, prev[1] + cur[1] * (i + 1)];
  63. })[1] / denominator;
  64. }
  65. /**
  66. * @private
  67. */
  68. function populateAverage(points, xVal, yVal, i) {
  69. var pLen = points.length, wmaY = weightedSumArray(points, pLen), wmaX = xVal[i - 1];
  70. points.shift(); // remove point until range < period
  71. return [wmaX, wmaY];
  72. }
  73. /* eslint-enable valid-jsdoc */
  74. /**
  75. * The SMA series type.
  76. *
  77. * @private
  78. * @class
  79. * @name Highcharts.seriesTypes.wma
  80. *
  81. * @augments Highcharts.Series
  82. */
  83. seriesType('wma', 'sma',
  84. /**
  85. * Weighted moving average indicator (WMA). This series requires `linkedTo`
  86. * option to be set.
  87. *
  88. * @sample stock/indicators/wma
  89. * Weighted moving average indicator
  90. *
  91. * @extends plotOptions.sma
  92. * @since 6.0.0
  93. * @product highstock
  94. * @requires stock/indicators/indicators
  95. * @requires stock/indicators/wma
  96. * @optionparent plotOptions.wma
  97. */
  98. {
  99. params: {
  100. index: 3,
  101. period: 9
  102. }
  103. },
  104. /**
  105. * @lends Highcharts.Series#
  106. */
  107. {
  108. getValues: function (series, params) {
  109. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, range = 1, xValue = xVal[0], yValue = yVal[0], WMA = [], xData = [], yData = [], index = -1, i, points, WMAPoint;
  110. if (xVal.length < period) {
  111. return;
  112. }
  113. // Switch index for OHLC / Candlestick
  114. if (isArray(yVal[0])) {
  115. index = params.index;
  116. yValue = yVal[0][index];
  117. }
  118. // Starting point
  119. points = [[xValue, yValue]];
  120. // Accumulate first N-points
  121. while (range !== period) {
  122. accumulateAverage(points, xVal, yVal, range, index);
  123. range++;
  124. }
  125. // Calculate value one-by-one for each period in visible data
  126. for (i = range; i < yValLen; i++) {
  127. WMAPoint = populateAverage(points, xVal, yVal, i);
  128. WMA.push(WMAPoint);
  129. xData.push(WMAPoint[0]);
  130. yData.push(WMAPoint[1]);
  131. accumulateAverage(points, xVal, yVal, i, index);
  132. }
  133. WMAPoint = populateAverage(points, xVal, yVal, i);
  134. WMA.push(WMAPoint);
  135. xData.push(WMAPoint[0]);
  136. yData.push(WMAPoint[1]);
  137. return {
  138. values: WMA,
  139. xData: xData,
  140. yData: yData
  141. };
  142. }
  143. });
  144. /**
  145. * A `WMA` series. If the [type](#series.wma.type) option is not specified, it
  146. * is inherited from [chart.type](#chart.type).
  147. *
  148. * @extends series,plotOptions.wma
  149. * @since 6.0.0
  150. * @product highstock
  151. * @excluding dataParser, dataURL
  152. * @requires stock/indicators/indicators
  153. * @requires stock/indicators/wma
  154. * @apioption series.wma
  155. */
  156. ''; // adds doclet above to the transpiled file
  157. });
  158. _registerModule(_modules, 'masters/indicators/wma.src.js', [], function () {
  159. });
  160. }));