DPOIndicator.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 U from '../../Core/Utilities.js';
  10. var correctFloat = U.correctFloat, pick = U.pick, seriesType = U.seriesType;
  11. /* eslint-disable valid-jsdoc */
  12. // Utils
  13. /**
  14. * @private
  15. */
  16. function accumulatePoints(sum, yVal, i, index, subtract) {
  17. var price = pick(yVal[i][index], yVal[i]);
  18. if (subtract) {
  19. return correctFloat(sum - price);
  20. }
  21. return correctFloat(sum + price);
  22. }
  23. /* eslint-enable valid-jsdoc */
  24. /**
  25. * The DPO series type.
  26. *
  27. * @private
  28. * @class
  29. * @name Highcharts.seriesTypes.dpo
  30. *
  31. * @augments Highcharts.Series
  32. */
  33. seriesType('dpo', 'sma',
  34. /**
  35. * Detrended Price Oscillator. This series requires the `linkedTo` option to
  36. * be set and should be loaded after the `stock/indicators/indicators.js`.
  37. *
  38. * @sample {highstock} stock/indicators/dpo
  39. * Detrended Price Oscillator
  40. *
  41. * @extends plotOptions.sma
  42. * @since 7.0.0
  43. * @product highstock
  44. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  45. * navigatorOptions, pointInterval, pointIntervalUnit,
  46. * pointPlacement, pointRange, pointStart, showInNavigator,
  47. * stacking
  48. * @requires stock/indicators/indicators
  49. * @requires stock/indicators/dpo
  50. * @optionparent plotOptions.dpo
  51. */
  52. {
  53. /**
  54. * Parameters used in calculation of Detrended Price Oscillator series
  55. * points.
  56. */
  57. params: {
  58. /**
  59. * Period for Detrended Price Oscillator
  60. */
  61. period: 21
  62. }
  63. },
  64. /**
  65. * @lends Highcharts.Series#
  66. */
  67. {
  68. nameBase: 'DPO',
  69. getValues: function (series, params) {
  70. var period = params.period, index = params.index, offset = Math.floor(period / 2 + 1), range = period + offset, xVal = series.xData || [], yVal = series.yData || [], yValLen = yVal.length,
  71. // 0- date, 1- Detrended Price Oscillator
  72. DPO = [], xData = [], yData = [], sum = 0, oscillator, periodIndex, rangeIndex, price, i, j;
  73. if (xVal.length <= range) {
  74. return;
  75. }
  76. // Accumulate first N-points for SMA
  77. for (i = 0; i < period - 1; i++) {
  78. sum = accumulatePoints(sum, yVal, i, index);
  79. }
  80. // Detrended Price Oscillator formula:
  81. // DPO = Price - Simple moving average [from (n / 2 + 1) days ago]
  82. for (j = 0; j <= yValLen - range; j++) {
  83. periodIndex = j + period - 1;
  84. rangeIndex = j + range - 1;
  85. // adding the last period point
  86. sum = accumulatePoints(sum, yVal, periodIndex, index);
  87. price = pick(yVal[rangeIndex][index], yVal[rangeIndex]);
  88. oscillator = price - sum / period;
  89. // substracting the first period point
  90. sum = accumulatePoints(sum, yVal, j, index, true);
  91. DPO.push([xVal[rangeIndex], oscillator]);
  92. xData.push(xVal[rangeIndex]);
  93. yData.push(oscillator);
  94. }
  95. return {
  96. values: DPO,
  97. xData: xData,
  98. yData: yData
  99. };
  100. }
  101. });
  102. /**
  103. * A Detrended Price Oscillator. If the [type](#series.dpo.type) option is not
  104. * specified, it is inherited from [chart.type](#chart.type).
  105. *
  106. * @extends series,plotOptions.dpo
  107. * @since 7.0.0
  108. * @product highstock
  109. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  110. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  111. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  112. * @requires stock/indicators/indicators
  113. * @requires stock/indicators/dpo
  114. * @apioption series.dpo
  115. */
  116. ''; // to include the above in the js output'