momentum.src.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* *
  2. *
  3. * License: www.highcharts.com/license
  4. *
  5. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  6. *
  7. * */
  8. 'use strict';
  9. import H from '../parts/Globals.js';
  10. import U from '../parts/Utilities.js';
  11. var isArray = U.isArray;
  12. var seriesType = H.seriesType;
  13. /* eslint-disable require-jsdoc */
  14. function populateAverage(points, xVal, yVal, i, period) {
  15. var mmY = yVal[i - 1][3] - yVal[i - period - 1][3], mmX = xVal[i - 1];
  16. points.shift(); // remove point until range < period
  17. return [mmX, mmY];
  18. }
  19. /* eslint-enable require-jsdoc */
  20. /**
  21. * The Momentum series type.
  22. *
  23. * @private
  24. * @class
  25. * @name Highcharts.seriesTypes.momentum
  26. *
  27. * @augments Highcharts.Series
  28. */
  29. seriesType('momentum', 'sma',
  30. /**
  31. * Momentum. This series requires `linkedTo` option to be set.
  32. *
  33. * @sample stock/indicators/momentum
  34. * Momentum indicator
  35. *
  36. * @extends plotOptions.sma
  37. * @since 6.0.0
  38. * @product highstock
  39. * @requires stock/indicators/indicators
  40. * @requires stock/indicators/momentum
  41. * @optionparent plotOptions.momentum
  42. */
  43. {
  44. params: {
  45. period: 14
  46. }
  47. },
  48. /**
  49. * @lends Highcharts.Series#
  50. */
  51. {
  52. nameBase: 'Momentum',
  53. getValues: function (series, params) {
  54. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, xValue = xVal[0], yValue = yVal[0], MM = [], xData = [], yData = [], index, i, points, MMPoint;
  55. if (xVal.length <= period) {
  56. return;
  57. }
  58. // Switch index for OHLC / Candlestick / Arearange
  59. if (isArray(yVal[0])) {
  60. yValue = yVal[0][3];
  61. }
  62. else {
  63. return;
  64. }
  65. // Starting point
  66. points = [
  67. [xValue, yValue]
  68. ];
  69. // Calculate value one-by-one for each period in visible data
  70. for (i = (period + 1); i < yValLen; i++) {
  71. MMPoint = populateAverage(points, xVal, yVal, i, period, index);
  72. MM.push(MMPoint);
  73. xData.push(MMPoint[0]);
  74. yData.push(MMPoint[1]);
  75. }
  76. MMPoint = populateAverage(points, xVal, yVal, i, period, index);
  77. MM.push(MMPoint);
  78. xData.push(MMPoint[0]);
  79. yData.push(MMPoint[1]);
  80. return {
  81. values: MM,
  82. xData: xData,
  83. yData: yData
  84. };
  85. }
  86. });
  87. /**
  88. * A `Momentum` series. If the [type](#series.momentum.type) option is not
  89. * specified, it is inherited from [chart.type](#chart.type).
  90. *
  91. * @extends series,plotOptions.momentum
  92. * @since 6.0.0
  93. * @excluding dataParser, dataURL
  94. * @product highstock
  95. * @requires stock/indicators/indicators
  96. * @requires stock/indicators/momentum
  97. * @apioption series.momentum
  98. */
  99. ''; // to include the above in the js output