EMAIndicator.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 correctFloat = U.correctFloat, isArray = U.isArray, seriesType = U.seriesType;
  11. /**
  12. * The EMA series type.
  13. *
  14. * @private
  15. * @class
  16. * @name Highcharts.seriesTypes.ema
  17. *
  18. * @augments Highcharts.Series
  19. */
  20. seriesType('ema', 'sma',
  21. /**
  22. * Exponential moving average indicator (EMA). This series requires the
  23. * `linkedTo` option to be set.
  24. *
  25. * @sample stock/indicators/ema
  26. * Exponential moving average indicator
  27. *
  28. * @extends plotOptions.sma
  29. * @since 6.0.0
  30. * @product highstock
  31. * @requires stock/indicators/indicators
  32. * @requires stock/indicators/ema
  33. * @optionparent plotOptions.ema
  34. */
  35. {
  36. params: {
  37. /**
  38. * The point index which indicator calculations will base. For
  39. * example using OHLC data, index=2 means the indicator will be
  40. * calculated using Low values.
  41. *
  42. * By default index value used to be set to 0. Since Highstock 7
  43. * by default index is set to 3 which means that the ema
  44. * indicator will be calculated using Close values.
  45. */
  46. index: 3,
  47. period: 9 // @merge 14 in v6.2
  48. }
  49. },
  50. /**
  51. * @lends Highcharts.Series#
  52. */
  53. {
  54. accumulatePeriodPoints: function (period, index, yVal) {
  55. var sum = 0, i = 0, y = 0;
  56. while (i < period) {
  57. y = index < 0 ? yVal[i] : yVal[i][index];
  58. sum = sum + y;
  59. i++;
  60. }
  61. return sum;
  62. },
  63. calculateEma: function (xVal, yVal, i, EMApercent, calEMA, index, SMA) {
  64. var x = xVal[i - 1], yValue = index < 0 ?
  65. yVal[i - 1] :
  66. yVal[i - 1][index], y;
  67. y = typeof calEMA === 'undefined' ?
  68. SMA : correctFloat((yValue * EMApercent) +
  69. (calEMA * (1 - EMApercent)));
  70. return [x, y];
  71. },
  72. getValues: function (series, params) {
  73. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, EMApercent = 2 / (period + 1), sum = 0, EMA = [], xData = [], yData = [], index = -1, SMA = 0, calEMA, EMAPoint, i;
  74. // Check period, if bigger than points length, skip
  75. if (yValLen < period) {
  76. return;
  77. }
  78. // Switch index for OHLC / Candlestick / Arearange
  79. if (isArray(yVal[0])) {
  80. index = params.index ? params.index : 0;
  81. }
  82. // Accumulate first N-points
  83. sum = this.accumulatePeriodPoints(period, index, yVal);
  84. // first point
  85. SMA = sum / period;
  86. // Calculate value one-by-one for each period in visible data
  87. for (i = period; i < yValLen + 1; i++) {
  88. EMAPoint = this.calculateEma(xVal, yVal, i, EMApercent, calEMA, index, SMA);
  89. EMA.push(EMAPoint);
  90. xData.push(EMAPoint[0]);
  91. yData.push(EMAPoint[1]);
  92. calEMA = EMAPoint[1];
  93. }
  94. return {
  95. values: EMA,
  96. xData: xData,
  97. yData: yData
  98. };
  99. }
  100. });
  101. /**
  102. * A `EMA` series. If the [type](#series.ema.type) option is not
  103. * specified, it is inherited from [chart.type](#chart.type).
  104. *
  105. * @extends series,plotOptions.ema
  106. * @since 6.0.0
  107. * @product highstock
  108. * @excluding dataParser, dataURL
  109. * @requires stock/indicators/indicators
  110. * @requires stock/indicators/ema
  111. * @apioption series.ema
  112. */
  113. ''; // adds doclet above to the transpiled file