DEMAIndicator.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 '../../Core/Globals.js';
  10. import U from '../../Core/Utilities.js';
  11. var correctFloat = U.correctFloat, isArray = U.isArray, seriesType = U.seriesType;
  12. import requiredIndicator from '../../Mixins/IndicatorRequired.js';
  13. var EMAindicator = H.seriesTypes.ema;
  14. /**
  15. * The DEMA series Type
  16. *
  17. * @private
  18. * @class
  19. * @name Highcharts.seriesTypes.dema
  20. *
  21. * @augments Highcharts.Series
  22. */
  23. seriesType('dema', 'ema',
  24. /**
  25. * Double exponential moving average (DEMA) indicator. This series requires
  26. * `linkedTo` option to be set and should be loaded after the
  27. * `stock/indicators/indicators.js` and `stock/indicators/ema.js`.
  28. *
  29. * @sample {highstock} stock/indicators/dema
  30. * DEMA indicator
  31. *
  32. * @extends plotOptions.ema
  33. * @since 7.0.0
  34. * @product highstock
  35. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  36. * navigatorOptions, pointInterval, pointIntervalUnit,
  37. * pointPlacement, pointRange, pointStart, showInNavigator,
  38. * stacking
  39. * @requires stock/indicators/indicators
  40. * @requires stock/indicators/ema
  41. * @requires stock/indicators/dema
  42. * @optionparent plotOptions.dema
  43. */
  44. {},
  45. /**
  46. * @lends Highcharts.Series#
  47. */
  48. {
  49. init: function () {
  50. var args = arguments, ctx = this;
  51. requiredIndicator.isParentLoaded(EMAindicator, 'ema', ctx.type, function (indicator) {
  52. indicator.prototype.init.apply(ctx, args);
  53. return;
  54. });
  55. },
  56. getEMA: function (yVal, prevEMA, SMA, index, i, xVal) {
  57. return EMAindicator.prototype.calculateEma(xVal || [], yVal, typeof i === 'undefined' ? 1 : i, this.chart.series[0].EMApercent, prevEMA, typeof index === 'undefined' ? -1 : index, SMA);
  58. },
  59. getValues: function (series, params) {
  60. var period = params.period, doubledPeriod = 2 * period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, index = -1, accumulatePeriodPoints = 0, SMA = 0, DEMA = [], xDataDema = [], yDataDema = [], EMA = 0,
  61. // EMA(EMA)
  62. EMAlevel2,
  63. // EMA of previous point
  64. prevEMA, prevEMAlevel2,
  65. // EMA values array
  66. EMAvalues = [], i, DEMAPoint;
  67. series.EMApercent = (2 / (period + 1));
  68. // Check period, if bigger than EMA points length, skip
  69. if (yValLen < 2 * period - 1) {
  70. return;
  71. }
  72. // Switch index for OHLC / Candlestick / Arearange
  73. if (isArray(yVal[0])) {
  74. index = params.index ? params.index : 0;
  75. }
  76. // Accumulate first N-points
  77. accumulatePeriodPoints =
  78. EMAindicator.prototype.accumulatePeriodPoints(period, index, yVal);
  79. // first point
  80. SMA = accumulatePeriodPoints / period;
  81. accumulatePeriodPoints = 0;
  82. // Calculate value one-by-one for each period in visible data
  83. for (i = period; i < yValLen + 2; i++) {
  84. if (i < yValLen + 1) {
  85. EMA = this.getEMA(yVal, prevEMA, SMA, index, i)[1];
  86. EMAvalues.push(EMA);
  87. }
  88. prevEMA = EMA;
  89. // Summing first period points for EMA(EMA)
  90. if (i < doubledPeriod) {
  91. accumulatePeriodPoints += EMA;
  92. }
  93. else {
  94. // Calculate DEMA
  95. // First DEMA point
  96. if (i === doubledPeriod) {
  97. SMA = accumulatePeriodPoints / period;
  98. }
  99. EMA = EMAvalues[i - period - 1];
  100. EMAlevel2 = this.getEMA([EMA], prevEMAlevel2, SMA)[1];
  101. DEMAPoint = [
  102. xVal[i - 2],
  103. correctFloat(2 * EMA - EMAlevel2)
  104. ];
  105. DEMA.push(DEMAPoint);
  106. xDataDema.push(DEMAPoint[0]);
  107. yDataDema.push(DEMAPoint[1]);
  108. prevEMAlevel2 = EMAlevel2;
  109. }
  110. }
  111. return {
  112. values: DEMA,
  113. xData: xDataDema,
  114. yData: yDataDema
  115. };
  116. }
  117. });
  118. /**
  119. * A `DEMA` series. If the [type](#series.ema.type) option is not
  120. * specified, it is inherited from [chart.type](#chart.type).
  121. *
  122. * @extends series,plotOptions.ema
  123. * @since 7.0.0
  124. * @product highstock
  125. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  126. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  127. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  128. * @requires stock/indicators/indicators
  129. * @requires stock/indicators/ema
  130. * @requires stock/indicators/dema
  131. * @apioption series.dema
  132. */
  133. ''; // adds doclet above to the transpiled file