APOIndicator.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 error = U.error, seriesType = U.seriesType;
  12. import requiredIndicator from '../../Mixins/IndicatorRequired.js';
  13. var EMA = H.seriesTypes.ema;
  14. /**
  15. * The APO series type.
  16. *
  17. * @private
  18. * @class
  19. * @name Highcharts.seriesTypes.apo
  20. *
  21. * @augments Highcharts.Series
  22. */
  23. seriesType('apo', 'ema',
  24. /**
  25. * Absolute Price Oscillator. This series requires the `linkedTo` option to
  26. * be set and should be loaded after the `stock/indicators/indicators.js`
  27. * and `stock/indicators/ema.js`.
  28. *
  29. * @sample {highstock} stock/indicators/apo
  30. * Absolute Price Oscillator
  31. *
  32. * @extends plotOptions.ema
  33. * @since 7.0.0
  34. * @product highstock
  35. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  36. * pointInterval, pointIntervalUnit, pointPlacement,
  37. * pointRange, pointStart, showInNavigator, stacking
  38. * @requires stock/indicators/indicators
  39. * @requires stock/indicators/ema
  40. * @requires stock/indicators/apo
  41. * @optionparent plotOptions.apo
  42. */
  43. {
  44. /**
  45. * Paramters used in calculation of Absolute Price Oscillator
  46. * series points.
  47. *
  48. * @excluding period
  49. */
  50. params: {
  51. /**
  52. * Periods for Absolute Price Oscillator calculations.
  53. *
  54. * @type {Array<number>}
  55. * @default [10, 20]
  56. * @since 7.0.0
  57. */
  58. periods: [10, 20]
  59. }
  60. },
  61. /**
  62. * @lends Highcharts.Series.prototype
  63. */
  64. {
  65. nameBase: 'APO',
  66. nameComponents: ['periods'],
  67. init: function () {
  68. var args = arguments, ctx = this;
  69. requiredIndicator.isParentLoaded(EMA, 'ema', ctx.type, function (indicator) {
  70. indicator.prototype.init.apply(ctx, args);
  71. return;
  72. });
  73. },
  74. getValues: function (series, params) {
  75. var periods = params.periods, index = params.index,
  76. // 0- date, 1- Absolute price oscillator
  77. APO = [], xData = [], yData = [], periodsOffset,
  78. // Shorter Period EMA
  79. SPE,
  80. // Longer Period EMA
  81. LPE, oscillator, i;
  82. // Check if periods are correct
  83. if (periods.length !== 2 || periods[1] <= periods[0]) {
  84. error('Error: "APO requires two periods. Notice, first period ' +
  85. 'should be lower than the second one."');
  86. return;
  87. }
  88. SPE = EMA.prototype.getValues.call(this, series, {
  89. index: index,
  90. period: periods[0]
  91. });
  92. LPE = EMA.prototype.getValues.call(this, series, {
  93. index: index,
  94. period: periods[1]
  95. });
  96. // Check if ema is calculated properly, if not skip
  97. if (!SPE || !LPE) {
  98. return;
  99. }
  100. periodsOffset = periods[1] - periods[0];
  101. for (i = 0; i < LPE.yData.length; i++) {
  102. oscillator = (SPE.yData[i + periodsOffset] -
  103. LPE.yData[i]);
  104. APO.push([LPE.xData[i], oscillator]);
  105. xData.push(LPE.xData[i]);
  106. yData.push(oscillator);
  107. }
  108. return {
  109. values: APO,
  110. xData: xData,
  111. yData: yData
  112. };
  113. }
  114. });
  115. /**
  116. * An `Absolute Price Oscillator` series. If the [type](#series.apo.type) option
  117. * is not specified, it is inherited from [chart.type](#chart.type).
  118. *
  119. * @extends series,plotOptions.apo
  120. * @since 7.0.0
  121. * @product highstock
  122. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  123. * navigatorOptions, pointInterval, pointIntervalUnit,
  124. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  125. * @requires stock/indicators/indicators
  126. * @requires stock/indicators/ema
  127. * @requires stock/indicators/apo
  128. * @apioption series.apo
  129. */
  130. ''; // to include the above in the js output