wma.src.js 5.6 KB

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