ema.src.js 5.8 KB

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