ema.src.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 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/EMA/EMAIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var __extends = (this && this.__extends) || (function () {
  40. var extendStatics = function (d,
  41. b) {
  42. extendStatics = Object.setPrototypeOf ||
  43. ({ __proto__: [] } instanceof Array && function (d,
  44. b) { d.__proto__ = b; }) ||
  45. function (d,
  46. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  47. return extendStatics(d, b);
  48. };
  49. return function (d, b) {
  50. extendStatics(d, b);
  51. function __() { this.constructor = d; }
  52. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  53. };
  54. })();
  55. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  56. var correctFloat = U.correctFloat,
  57. isArray = U.isArray,
  58. merge = U.merge;
  59. /* *
  60. *
  61. * Class
  62. *
  63. * */
  64. /**
  65. * The EMA series type.
  66. *
  67. * @private
  68. * @class
  69. * @name Highcharts.seriesTypes.ema
  70. *
  71. * @augments Highcharts.Series
  72. */
  73. var EMAIndicator = /** @class */ (function (_super) {
  74. __extends(EMAIndicator, _super);
  75. function EMAIndicator() {
  76. var _this = _super !== null && _super.apply(this,
  77. arguments) || this;
  78. /* *
  79. *
  80. * Properties
  81. *
  82. * */
  83. _this.data = void 0;
  84. _this.options = void 0;
  85. _this.points = void 0;
  86. return _this;
  87. }
  88. /* *
  89. *
  90. * Functions
  91. *
  92. * */
  93. EMAIndicator.prototype.accumulatePeriodPoints = function (period, index, yVal) {
  94. var sum = 0,
  95. i = 0,
  96. y = 0;
  97. while (i < period) {
  98. y = index < 0 ? yVal[i] : yVal[i][index];
  99. sum = sum + y;
  100. i++;
  101. }
  102. return sum;
  103. };
  104. EMAIndicator.prototype.calculateEma = function (xVal, yVal, i, EMApercent, calEMA, index, SMA) {
  105. var x = xVal[i - 1],
  106. yValue = index < 0 ?
  107. yVal[i - 1] :
  108. yVal[i - 1][index],
  109. y;
  110. y = typeof calEMA === 'undefined' ?
  111. SMA : correctFloat((yValue * EMApercent) +
  112. (calEMA * (1 - EMApercent)));
  113. return [x, y];
  114. };
  115. EMAIndicator.prototype.getValues = function (series, params) {
  116. var period = params.period,
  117. xVal = series.xData,
  118. yVal = series.yData,
  119. yValLen = yVal ? yVal.length : 0,
  120. EMApercent = 2 / (period + 1),
  121. sum = 0,
  122. EMA = [],
  123. xData = [],
  124. yData = [],
  125. index = -1,
  126. SMA = 0,
  127. calEMA,
  128. EMAPoint,
  129. i;
  130. // Check period, if bigger than points length, skip
  131. if (yValLen < period) {
  132. return;
  133. }
  134. // Switch index for OHLC / Candlestick / Arearange
  135. if (isArray(yVal[0])) {
  136. index = params.index ? params.index : 0;
  137. }
  138. // Accumulate first N-points
  139. sum = this.accumulatePeriodPoints(period, index, yVal);
  140. // first point
  141. SMA = sum / period;
  142. // Calculate value one-by-one for each period in visible data
  143. for (i = period; i < yValLen + 1; i++) {
  144. EMAPoint = this.calculateEma(xVal, yVal, i, EMApercent, calEMA, index, SMA);
  145. EMA.push(EMAPoint);
  146. xData.push(EMAPoint[0]);
  147. yData.push(EMAPoint[1]);
  148. calEMA = EMAPoint[1];
  149. }
  150. return {
  151. values: EMA,
  152. xData: xData,
  153. yData: yData
  154. };
  155. };
  156. /**
  157. * Exponential moving average indicator (EMA). This series requires the
  158. * `linkedTo` option to be set.
  159. *
  160. * @sample stock/indicators/ema
  161. * Exponential moving average indicator
  162. *
  163. * @extends plotOptions.sma
  164. * @since 6.0.0
  165. * @product highstock
  166. * @requires stock/indicators/indicators
  167. * @requires stock/indicators/ema
  168. * @optionparent plotOptions.ema
  169. */
  170. EMAIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  171. params: {
  172. /**
  173. * The point index which indicator calculations will base. For
  174. * example using OHLC data, index=2 means the indicator will be
  175. * calculated using Low values.
  176. *
  177. * By default index value used to be set to 0. Since
  178. * Highcharts Stock 7 by default index is set to 3
  179. * which means that the ema indicator will be
  180. * calculated using Close values.
  181. */
  182. index: 3,
  183. period: 9 // @merge 14 in v6.2
  184. }
  185. });
  186. return EMAIndicator;
  187. }(SMAIndicator));
  188. SeriesRegistry.registerSeriesType('ema', EMAIndicator);
  189. /* *
  190. *
  191. * Default Export
  192. *
  193. * */
  194. /**
  195. * A `EMA` series. If the [type](#series.ema.type) option is not
  196. * specified, it is inherited from [chart.type](#chart.type).
  197. *
  198. * @extends series,plotOptions.ema
  199. * @since 6.0.0
  200. * @product highstock
  201. * @excluding dataParser, dataURL
  202. * @requires stock/indicators/indicators
  203. * @requires stock/indicators/ema
  204. * @apioption series.ema
  205. */
  206. ''; // adds doclet above to the transpiled file
  207. return EMAIndicator;
  208. });
  209. _registerModule(_modules, 'masters/indicators/ema.src.js', [], function () {
  210. });
  211. }));