WMAIndicator.js 3.6 KB

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