2
0

dpo.src.js 3.7 KB

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