ema.src.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @license Highstock JS v8.0.0 (2019-12-10)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Sebastian Bochan
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/indicators/ema', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'indicators/ema.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var correctFloat = U.correctFloat, isArray = U.isArray;
  40. var seriesType = H.seriesType;
  41. /**
  42. * The EMA series type.
  43. *
  44. * @private
  45. * @class
  46. * @name Highcharts.seriesTypes.ema
  47. *
  48. * @augments Highcharts.Series
  49. */
  50. seriesType('ema', 'sma',
  51. /**
  52. * Exponential moving average indicator (EMA). This series requires the
  53. * `linkedTo` option to be set.
  54. *
  55. * @sample stock/indicators/ema
  56. * Exponential moving average indicator
  57. *
  58. * @extends plotOptions.sma
  59. * @since 6.0.0
  60. * @product highstock
  61. * @requires stock/indicators/indicators
  62. * @requires stock/indicators/ema
  63. * @optionparent plotOptions.ema
  64. */
  65. {
  66. params: {
  67. /**
  68. * The point index which indicator calculations will base. For
  69. * example using OHLC data, index=2 means the indicator will be
  70. * calculated using Low values.
  71. *
  72. * By default index value used to be set to 0. Since Highstock 7
  73. * by default index is set to 3 which means that the ema
  74. * indicator will be calculated using Close values.
  75. */
  76. index: 3,
  77. period: 9 // @merge 14 in v6.2
  78. }
  79. },
  80. /**
  81. * @lends Highcharts.Series#
  82. */
  83. {
  84. accumulatePeriodPoints: function (period, index, yVal) {
  85. var sum = 0, i = 0, y = 0;
  86. while (i < period) {
  87. y = index < 0 ? yVal[i] : yVal[i][index];
  88. sum = sum + y;
  89. i++;
  90. }
  91. return sum;
  92. },
  93. calculateEma: function (xVal, yVal, i, EMApercent, calEMA, index, SMA) {
  94. var x = xVal[i - 1], yValue = index < 0 ?
  95. yVal[i - 1] :
  96. yVal[i - 1][index], y;
  97. y = typeof calEMA === 'undefined' ?
  98. SMA : correctFloat((yValue * EMApercent) +
  99. (calEMA * (1 - EMApercent)));
  100. return [x, y];
  101. },
  102. getValues: function (series, params) {
  103. 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;
  104. // Check period, if bigger than points length, skip
  105. if (yValLen < period) {
  106. return;
  107. }
  108. // Switch index for OHLC / Candlestick / Arearange
  109. if (isArray(yVal[0])) {
  110. index = params.index ? params.index : 0;
  111. }
  112. // Accumulate first N-points
  113. sum = this.accumulatePeriodPoints(period, index, yVal);
  114. // first point
  115. SMA = sum / period;
  116. // Calculate value one-by-one for each period in visible data
  117. for (i = period; i < yValLen + 1; i++) {
  118. EMAPoint = this.calculateEma(xVal, yVal, i, EMApercent, calEMA, index, SMA);
  119. EMA.push(EMAPoint);
  120. xData.push(EMAPoint[0]);
  121. yData.push(EMAPoint[1]);
  122. calEMA = EMAPoint[1];
  123. }
  124. return {
  125. values: EMA,
  126. xData: xData,
  127. yData: yData
  128. };
  129. }
  130. });
  131. /**
  132. * A `EMA` series. If the [type](#series.ema.type) option is not
  133. * specified, it is inherited from [chart.type](#chart.type).
  134. *
  135. * @extends series,plotOptions.ema
  136. * @since 6.0.0
  137. * @product highstock
  138. * @excluding dataParser, dataURL
  139. * @requires stock/indicators/indicators
  140. * @requires stock/indicators/ema
  141. * @apioption series.ema
  142. */
  143. ''; // adds doclet above to the transpiled file
  144. });
  145. _registerModule(_modules, 'masters/indicators/ema.src.js', [], function () {
  146. });
  147. }));