dpo.src.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  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, 'Stock/Indicators/DPOIndicator.js', [_modules['Core/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,
  40. pick = U.pick,
  41. seriesType = U.seriesType;
  42. /* eslint-disable valid-jsdoc */
  43. // Utils
  44. /**
  45. * @private
  46. */
  47. function accumulatePoints(sum, yVal, i, index, subtract) {
  48. var price = pick(yVal[i][index],
  49. yVal[i]);
  50. if (subtract) {
  51. return correctFloat(sum - price);
  52. }
  53. return correctFloat(sum + price);
  54. }
  55. /* eslint-enable valid-jsdoc */
  56. /**
  57. * The DPO series type.
  58. *
  59. * @private
  60. * @class
  61. * @name Highcharts.seriesTypes.dpo
  62. *
  63. * @augments Highcharts.Series
  64. */
  65. seriesType('dpo', 'sma',
  66. /**
  67. * Detrended Price Oscillator. This series requires the `linkedTo` option to
  68. * be set and should be loaded after the `stock/indicators/indicators.js`.
  69. *
  70. * @sample {highstock} stock/indicators/dpo
  71. * Detrended Price Oscillator
  72. *
  73. * @extends plotOptions.sma
  74. * @since 7.0.0
  75. * @product highstock
  76. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  77. * navigatorOptions, pointInterval, pointIntervalUnit,
  78. * pointPlacement, pointRange, pointStart, showInNavigator,
  79. * stacking
  80. * @requires stock/indicators/indicators
  81. * @requires stock/indicators/dpo
  82. * @optionparent plotOptions.dpo
  83. */
  84. {
  85. /**
  86. * Parameters used in calculation of Detrended Price Oscillator series
  87. * points.
  88. */
  89. params: {
  90. /**
  91. * Period for Detrended Price Oscillator
  92. */
  93. period: 21
  94. }
  95. },
  96. /**
  97. * @lends Highcharts.Series#
  98. */
  99. {
  100. nameBase: 'DPO',
  101. getValues: function (series, params) {
  102. var period = params.period,
  103. index = params.index,
  104. offset = Math.floor(period / 2 + 1),
  105. range = period + offset,
  106. xVal = series.xData || [],
  107. yVal = series.yData || [],
  108. yValLen = yVal.length,
  109. // 0- date, 1- Detrended Price Oscillator
  110. DPO = [],
  111. xData = [],
  112. yData = [],
  113. sum = 0,
  114. oscillator,
  115. periodIndex,
  116. rangeIndex,
  117. price,
  118. i,
  119. j;
  120. if (xVal.length <= range) {
  121. return;
  122. }
  123. // Accumulate first N-points for SMA
  124. for (i = 0; i < period - 1; i++) {
  125. sum = accumulatePoints(sum, yVal, i, index);
  126. }
  127. // Detrended Price Oscillator formula:
  128. // DPO = Price - Simple moving average [from (n / 2 + 1) days ago]
  129. for (j = 0; j <= yValLen - range; j++) {
  130. periodIndex = j + period - 1;
  131. rangeIndex = j + range - 1;
  132. // adding the last period point
  133. sum = accumulatePoints(sum, yVal, periodIndex, index);
  134. price = pick(yVal[rangeIndex][index], yVal[rangeIndex]);
  135. oscillator = price - sum / period;
  136. // substracting the first period point
  137. sum = accumulatePoints(sum, yVal, j, index, true);
  138. DPO.push([xVal[rangeIndex], oscillator]);
  139. xData.push(xVal[rangeIndex]);
  140. yData.push(oscillator);
  141. }
  142. return {
  143. values: DPO,
  144. xData: xData,
  145. yData: yData
  146. };
  147. }
  148. });
  149. /**
  150. * A Detrended Price Oscillator. If the [type](#series.dpo.type) option is not
  151. * specified, it is inherited from [chart.type](#chart.type).
  152. *
  153. * @extends series,plotOptions.dpo
  154. * @since 7.0.0
  155. * @product highstock
  156. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  157. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  158. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  159. * @requires stock/indicators/indicators
  160. * @requires stock/indicators/dpo
  161. * @apioption series.dpo
  162. */
  163. ''; // to include the above in the js output'
  164. });
  165. _registerModule(_modules, 'masters/indicators/dpo.src.js', [], function () {
  166. });
  167. }));