dpo.src.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @license Highstock JS v8.1.2 (2020-06-16)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Wojciech Chmiel
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/indicators/dpo', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'indicators/dpo.src.js', [_modules['parts/Utilities.js']], function (U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var correctFloat = U.correctFloat, pick = U.pick, seriesType = U.seriesType;
  40. /* eslint-disable valid-jsdoc */
  41. // Utils
  42. /**
  43. * @private
  44. */
  45. function accumulatePoints(sum, yVal, i, index, subtract) {
  46. var price = pick(yVal[i][index], yVal[i]);
  47. if (subtract) {
  48. return correctFloat(sum - price);
  49. }
  50. return correctFloat(sum + price);
  51. }
  52. /* eslint-enable valid-jsdoc */
  53. /**
  54. * The DPO series type.
  55. *
  56. * @private
  57. * @class
  58. * @name Highcharts.seriesTypes.dpo
  59. *
  60. * @augments Highcharts.Series
  61. */
  62. seriesType('dpo', 'sma',
  63. /**
  64. * Detrended Price Oscillator. This series requires the `linkedTo` option to
  65. * be set and should be loaded after the `stock/indicators/indicators.js`.
  66. *
  67. * @sample {highstock} stock/indicators/dpo
  68. * Detrended Price Oscillator
  69. *
  70. * @extends plotOptions.sma
  71. * @since 7.0.0
  72. * @product highstock
  73. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  74. * navigatorOptions, pointInterval, pointIntervalUnit,
  75. * pointPlacement, pointRange, pointStart, showInNavigator,
  76. * stacking
  77. * @requires stock/indicators/indicators
  78. * @requires stock/indicators/dpo
  79. * @optionparent plotOptions.dpo
  80. */
  81. {
  82. /**
  83. * Parameters used in calculation of Detrended Price Oscillator series
  84. * points.
  85. */
  86. params: {
  87. /**
  88. * Period for Detrended Price Oscillator
  89. */
  90. period: 21
  91. }
  92. },
  93. /**
  94. * @lends Highcharts.Series#
  95. */
  96. {
  97. nameBase: 'DPO',
  98. getValues: function (series, params) {
  99. 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,
  100. // 0- date, 1- Detrended Price Oscillator
  101. DPO = [], xData = [], yData = [], sum = 0, oscillator, periodIndex, rangeIndex, price, i, j;
  102. if (xVal.length <= range) {
  103. return;
  104. }
  105. // Accumulate first N-points for SMA
  106. for (i = 0; i < period - 1; i++) {
  107. sum = accumulatePoints(sum, yVal, i, index);
  108. }
  109. // Detrended Price Oscillator formula:
  110. // DPO = Price - Simple moving average [from (n / 2 + 1) days ago]
  111. for (j = 0; j <= yValLen - range; j++) {
  112. periodIndex = j + period - 1;
  113. rangeIndex = j + range - 1;
  114. // adding the last period point
  115. sum = accumulatePoints(sum, yVal, periodIndex, index);
  116. price = pick(yVal[rangeIndex][index], yVal[rangeIndex]);
  117. oscillator = price - sum / period;
  118. // substracting the first period point
  119. sum = accumulatePoints(sum, yVal, j, index, true);
  120. DPO.push([xVal[rangeIndex], oscillator]);
  121. xData.push(xVal[rangeIndex]);
  122. yData.push(oscillator);
  123. }
  124. return {
  125. values: DPO,
  126. xData: xData,
  127. yData: yData
  128. };
  129. }
  130. });
  131. /**
  132. * A Detrended Price Oscillator. If the [type](#series.dpo.type) option is not
  133. * specified, it is inherited from [chart.type](#chart.type).
  134. *
  135. * @extends series,plotOptions.dpo
  136. * @since 7.0.0
  137. * @product highstock
  138. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  139. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  140. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  141. * @requires stock/indicators/indicators
  142. * @requires stock/indicators/dpo
  143. * @apioption series.dpo
  144. */
  145. ''; // to include the above in the js output'
  146. });
  147. _registerModule(_modules, 'masters/indicators/dpo.src.js', [], function () {
  148. });
  149. }));