ema.src.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @license Highstock JS v8.1.2 (2020-06-16)
  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/Utilities.js']], function (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, seriesType = U.seriesType;
  40. /**
  41. * The EMA series type.
  42. *
  43. * @private
  44. * @class
  45. * @name Highcharts.seriesTypes.ema
  46. *
  47. * @augments Highcharts.Series
  48. */
  49. seriesType('ema', 'sma',
  50. /**
  51. * Exponential moving average indicator (EMA). This series requires the
  52. * `linkedTo` option to be set.
  53. *
  54. * @sample stock/indicators/ema
  55. * Exponential moving average indicator
  56. *
  57. * @extends plotOptions.sma
  58. * @since 6.0.0
  59. * @product highstock
  60. * @requires stock/indicators/indicators
  61. * @requires stock/indicators/ema
  62. * @optionparent plotOptions.ema
  63. */
  64. {
  65. params: {
  66. /**
  67. * The point index which indicator calculations will base. For
  68. * example using OHLC data, index=2 means the indicator will be
  69. * calculated using Low values.
  70. *
  71. * By default index value used to be set to 0. Since Highstock 7
  72. * by default index is set to 3 which means that the ema
  73. * indicator will be calculated using Close values.
  74. */
  75. index: 3,
  76. period: 9 // @merge 14 in v6.2
  77. }
  78. },
  79. /**
  80. * @lends Highcharts.Series#
  81. */
  82. {
  83. accumulatePeriodPoints: function (period, index, yVal) {
  84. var sum = 0, i = 0, y = 0;
  85. while (i < period) {
  86. y = index < 0 ? yVal[i] : yVal[i][index];
  87. sum = sum + y;
  88. i++;
  89. }
  90. return sum;
  91. },
  92. calculateEma: function (xVal, yVal, i, EMApercent, calEMA, index, SMA) {
  93. var x = xVal[i - 1], yValue = index < 0 ?
  94. yVal[i - 1] :
  95. yVal[i - 1][index], y;
  96. y = typeof calEMA === 'undefined' ?
  97. SMA : correctFloat((yValue * EMApercent) +
  98. (calEMA * (1 - EMApercent)));
  99. return [x, y];
  100. },
  101. getValues: function (series, params) {
  102. 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;
  103. // Check period, if bigger than points length, skip
  104. if (yValLen < period) {
  105. return;
  106. }
  107. // Switch index for OHLC / Candlestick / Arearange
  108. if (isArray(yVal[0])) {
  109. index = params.index ? params.index : 0;
  110. }
  111. // Accumulate first N-points
  112. sum = this.accumulatePeriodPoints(period, index, yVal);
  113. // first point
  114. SMA = sum / period;
  115. // Calculate value one-by-one for each period in visible data
  116. for (i = period; i < yValLen + 1; i++) {
  117. EMAPoint = this.calculateEma(xVal, yVal, i, EMApercent, calEMA, index, SMA);
  118. EMA.push(EMAPoint);
  119. xData.push(EMAPoint[0]);
  120. yData.push(EMAPoint[1]);
  121. calEMA = EMAPoint[1];
  122. }
  123. return {
  124. values: EMA,
  125. xData: xData,
  126. yData: yData
  127. };
  128. }
  129. });
  130. /**
  131. * A `EMA` series. If the [type](#series.ema.type) option is not
  132. * specified, it is inherited from [chart.type](#chart.type).
  133. *
  134. * @extends series,plotOptions.ema
  135. * @since 6.0.0
  136. * @product highstock
  137. * @excluding dataParser, dataURL
  138. * @requires stock/indicators/indicators
  139. * @requires stock/indicators/ema
  140. * @apioption series.ema
  141. */
  142. ''; // adds doclet above to the transpiled file
  143. });
  144. _registerModule(_modules, 'masters/indicators/ema.src.js', [], function () {
  145. });
  146. }));