MomentumIndicator.js 2.7 KB

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