PPOIndicator.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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, error = U.error, seriesType = U.seriesType;
  12. import requiredIndicator from '../../Mixins/IndicatorRequired.js';
  13. var EMA = H.seriesTypes.ema;
  14. /**
  15. * The PPO series type.
  16. *
  17. * @private
  18. * @class
  19. * @name Highcharts.seriesTypes.ppo
  20. *
  21. * @augments Highcharts.Series
  22. */
  23. seriesType('ppo', 'ema',
  24. /**
  25. * Percentage Price Oscillator. This series requires the
  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/ppo
  30. * Percentage 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/ppo
  41. * @optionparent plotOptions.ppo
  42. */
  43. {
  44. /**
  45. * Paramters used in calculation of Percentage Price Oscillator series
  46. * points.
  47. *
  48. * @excluding period
  49. */
  50. params: {
  51. /**
  52. * Periods for Percentage Price Oscillator calculations.
  53. *
  54. * @type {Array<number>}
  55. * @default [12, 26]
  56. */
  57. periods: [12, 26]
  58. }
  59. },
  60. /**
  61. * @lends Highcharts.Series.prototype
  62. */
  63. {
  64. nameBase: 'PPO',
  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- Percentage Price Oscillator
  76. PPO = [], 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: "PPO 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 = correctFloat((SPE.yData[i + periodsOffset] -
  102. LPE.yData[i]) /
  103. LPE.yData[i] *
  104. 100);
  105. PPO.push([LPE.xData[i], oscillator]);
  106. xData.push(LPE.xData[i]);
  107. yData.push(oscillator);
  108. }
  109. return {
  110. values: PPO,
  111. xData: xData,
  112. yData: yData
  113. };
  114. }
  115. });
  116. /**
  117. * A `Percentage Price Oscillator` series. If the [type](#series.ppo.type)
  118. * option is not specified, it is inherited from [chart.type](#chart.type).
  119. *
  120. * @extends series,plotOptions.ppo
  121. * @since 7.0.0
  122. * @product highstock
  123. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  124. * navigatorOptions, pointInterval, pointIntervalUnit,
  125. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  126. * @requires stock/indicators/indicators
  127. * @requires stock/indicators/ema
  128. * @requires stock/indicators/ppo
  129. * @apioption series.ppo
  130. */
  131. ''; // to include the above in the js output