ema.src.js 3.5 KB

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