dpo.src.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 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/DPO/DPOIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var __extends = (this && this.__extends) || (function () {
  40. var extendStatics = function (d,
  41. b) {
  42. extendStatics = Object.setPrototypeOf ||
  43. ({ __proto__: [] } instanceof Array && function (d,
  44. b) { d.__proto__ = b; }) ||
  45. function (d,
  46. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  47. return extendStatics(d, b);
  48. };
  49. return function (d, b) {
  50. extendStatics(d, b);
  51. function __() { this.constructor = d; }
  52. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  53. };
  54. })();
  55. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  56. var extend = U.extend,
  57. merge = U.merge,
  58. correctFloat = U.correctFloat,
  59. pick = U.pick;
  60. /* eslint-disable valid-jsdoc */
  61. // Utils:
  62. /**
  63. * @private
  64. */
  65. function accumulatePoints(sum, yVal, i, index, subtract) {
  66. var price = pick(yVal[i][index],
  67. yVal[i]);
  68. if (subtract) {
  69. return correctFloat(sum - price);
  70. }
  71. return correctFloat(sum + price);
  72. }
  73. /* *
  74. *
  75. * Class
  76. *
  77. * */
  78. /**
  79. * The DPO series type.
  80. *
  81. * @private
  82. * @class
  83. * @name Highcharts.seriesTypes.dpo
  84. *
  85. * @augments Highcharts.Series
  86. */
  87. var DPOIndicator = /** @class */ (function (_super) {
  88. __extends(DPOIndicator, _super);
  89. function DPOIndicator() {
  90. var _this = _super !== null && _super.apply(this,
  91. arguments) || this;
  92. /* *
  93. *
  94. * Properties
  95. *
  96. * */
  97. _this.options = void 0;
  98. _this.data = void 0;
  99. _this.points = void 0;
  100. return _this;
  101. }
  102. /* *
  103. *
  104. * Functions
  105. *
  106. * */
  107. /**
  108. * @lends Highcharts.Series#
  109. */
  110. DPOIndicator.prototype.getValues = function (series, params) {
  111. var period = params.period,
  112. index = params.index,
  113. offset = Math.floor(period / 2 + 1),
  114. range = period + offset,
  115. xVal = series.xData || [],
  116. yVal = series.yData || [],
  117. yValLen = yVal.length,
  118. // 0- date, 1- Detrended Price Oscillator
  119. DPO = [],
  120. xData = [],
  121. yData = [],
  122. sum = 0,
  123. oscillator,
  124. periodIndex,
  125. rangeIndex,
  126. price,
  127. i,
  128. j;
  129. if (xVal.length <= range) {
  130. return;
  131. }
  132. // Accumulate first N-points for SMA
  133. for (i = 0; i < period - 1; i++) {
  134. sum = accumulatePoints(sum, yVal, i, index);
  135. }
  136. // Detrended Price Oscillator formula:
  137. // DPO = Price - Simple moving average [from (n / 2 + 1) days ago]
  138. for (j = 0; j <= yValLen - range; j++) {
  139. periodIndex = j + period - 1;
  140. rangeIndex = j + range - 1;
  141. // adding the last period point
  142. sum = accumulatePoints(sum, yVal, periodIndex, index);
  143. price = pick(yVal[rangeIndex][index], yVal[rangeIndex]);
  144. oscillator = price - sum / period;
  145. // substracting the first period point
  146. sum = accumulatePoints(sum, yVal, j, index, true);
  147. DPO.push([xVal[rangeIndex], oscillator]);
  148. xData.push(xVal[rangeIndex]);
  149. yData.push(oscillator);
  150. }
  151. return {
  152. values: DPO,
  153. xData: xData,
  154. yData: yData
  155. };
  156. };
  157. /**
  158. * Detrended Price Oscillator. This series requires the `linkedTo` option to
  159. * be set and should be loaded after the `stock/indicators/indicators.js`.
  160. *
  161. * @sample {highstock} stock/indicators/dpo
  162. * Detrended Price Oscillator
  163. *
  164. * @extends plotOptions.sma
  165. * @since 7.0.0
  166. * @product highstock
  167. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  168. * navigatorOptions, pointInterval, pointIntervalUnit,
  169. * pointPlacement, pointRange, pointStart, showInNavigator,
  170. * stacking
  171. * @requires stock/indicators/indicators
  172. * @requires stock/indicators/dpo
  173. * @optionparent plotOptions.dpo
  174. */
  175. DPOIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  176. /**
  177. * Parameters used in calculation of Detrended Price Oscillator series
  178. * points.
  179. */
  180. params: {
  181. index: 0,
  182. /**
  183. * Period for Detrended Price Oscillator
  184. */
  185. period: 21
  186. }
  187. });
  188. return DPOIndicator;
  189. }(SMAIndicator));
  190. extend(DPOIndicator.prototype, {
  191. nameBase: 'DPO'
  192. });
  193. SeriesRegistry.registerSeriesType('dpo', DPOIndicator);
  194. /* *
  195. *
  196. * Default Export
  197. *
  198. * */
  199. /**
  200. * A Detrended Price Oscillator. If the [type](#series.dpo.type) option is not
  201. * specified, it is inherited from [chart.type](#chart.type).
  202. *
  203. * @extends series,plotOptions.dpo
  204. * @since 7.0.0
  205. * @product highstock
  206. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  207. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  208. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  209. * @requires stock/indicators/indicators
  210. * @requires stock/indicators/dpo
  211. * @apioption series.dpo
  212. */
  213. ''; // to include the above in the js output'
  214. return DPOIndicator;
  215. });
  216. _registerModule(_modules, 'masters/indicators/dpo.src.js', [], function () {
  217. });
  218. }));