apo.src.js 3.9 KB

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