wma.src.js 3.6 KB

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